gdb: fix printing of flag enums with multi-bit enumerators
[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
576 a "per_cu" handle on the symtab.
577 This PER_CU is recognized by having no section. */
8a0459fd 578#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
579 struct dwarf2_per_cu_data per_cu;
580
0186c6a7
DE
581 /* The TUs that share this DW_AT_stmt_list entry.
582 This is added to while parsing type units to build partial symtabs,
583 and is deleted afterwards and not used again. */
a8b3b8e9 584 std::vector<signatured_type *> *tus;
f4dc4d17 585
43f3e411 586 /* The compunit symtab.
094b34ac 587 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
588 so we create an essentially anonymous symtab as the compunit symtab. */
589 struct compunit_symtab *compunit_symtab;
f4dc4d17 590
094b34ac
DE
591 /* The data used to construct the hash key. */
592 struct stmt_list_hash hash;
f4dc4d17
DE
593
594 /* The number of symtabs from the line header.
595 The value here must match line_header.num_file_names. */
596 unsigned int num_symtabs;
597
598 /* The symbol tables for this TU (obtained from the files listed in
599 DW_AT_stmt_list).
600 WARNING: The order of entries here must match the order of entries
601 in the line header. After the first TU using this type_unit_group, the
602 line header for the subsequent TUs is recreated from this. This is done
603 because we need to use the same symtabs for each TU using the same
604 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
605 there's no guarantee the line header doesn't have duplicate entries. */
606 struct symtab **symtabs;
607};
608
73869dc2 609/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
610
611struct dwo_sections
612{
613 struct dwarf2_section_info abbrev;
3019eac3
DE
614 struct dwarf2_section_info line;
615 struct dwarf2_section_info loc;
43988095 616 struct dwarf2_section_info loclists;
09262596
DE
617 struct dwarf2_section_info macinfo;
618 struct dwarf2_section_info macro;
3019eac3
DE
619 struct dwarf2_section_info str;
620 struct dwarf2_section_info str_offsets;
80626a55
DE
621 /* In the case of a virtual DWO file, these two are unused. */
622 struct dwarf2_section_info info;
fd5866f6 623 std::vector<dwarf2_section_info> types;
3019eac3
DE
624};
625
c88ee1f0 626/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
627
628struct dwo_unit
629{
630 /* Backlink to the containing struct dwo_file. */
631 struct dwo_file *dwo_file;
632
633 /* The "id" that distinguishes this CU/TU.
634 .debug_info calls this "dwo_id", .debug_types calls this "signature".
635 Since signatures came first, we stick with it for consistency. */
636 ULONGEST signature;
637
638 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 639 struct dwarf2_section_info *section;
3019eac3 640
9c541725
PA
641 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
642 sect_offset sect_off;
3019eac3
DE
643 unsigned int length;
644
645 /* For types, offset in the type's DIE of the type defined by this TU. */
646 cu_offset type_offset_in_tu;
647};
648
73869dc2
DE
649/* include/dwarf2.h defines the DWP section codes.
650 It defines a max value but it doesn't define a min value, which we
651 use for error checking, so provide one. */
652
653enum dwp_v2_section_ids
654{
655 DW_SECT_MIN = 1
656};
657
80626a55 658/* Data for one DWO file.
57d63ce2
DE
659
660 This includes virtual DWO files (a virtual DWO file is a DWO file as it
661 appears in a DWP file). DWP files don't really have DWO files per se -
662 comdat folding of types "loses" the DWO file they came from, and from
663 a high level view DWP files appear to contain a mass of random types.
664 However, to maintain consistency with the non-DWP case we pretend DWP
665 files contain virtual DWO files, and we assign each TU with one virtual
666 DWO file (generally based on the line and abbrev section offsets -
667 a heuristic that seems to work in practice). */
3019eac3
DE
668
669struct dwo_file
670{
51ac9db5
SM
671 dwo_file () = default;
672 DISABLE_COPY_AND_ASSIGN (dwo_file);
673
18a8505e 674 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
675 For virtual DWO files the name is constructed from the section offsets
676 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
677 from related CU+TUs. */
51ac9db5 678 const char *dwo_name = nullptr;
0ac5b59e
DE
679
680 /* The DW_AT_comp_dir attribute. */
51ac9db5 681 const char *comp_dir = nullptr;
3019eac3 682
80626a55
DE
683 /* The bfd, when the file is open. Otherwise this is NULL.
684 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 685 gdb_bfd_ref_ptr dbfd;
3019eac3 686
73869dc2
DE
687 /* The sections that make up this DWO file.
688 Remember that for virtual DWO files in DWP V2, these are virtual
689 sections (for lack of a better name). */
51ac9db5 690 struct dwo_sections sections {};
3019eac3 691
33c5cd75
DB
692 /* The CUs in the file.
693 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
694 an extension to handle LLVM's Link Time Optimization output (where
695 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 696 htab_up cus;
3019eac3
DE
697
698 /* Table of TUs in the file.
699 Each element is a struct dwo_unit. */
b0b6a987 700 htab_up tus;
3019eac3
DE
701};
702
80626a55
DE
703/* These sections are what may appear in a DWP file. */
704
705struct dwp_sections
706{
73869dc2 707 /* These are used by both DWP version 1 and 2. */
80626a55
DE
708 struct dwarf2_section_info str;
709 struct dwarf2_section_info cu_index;
710 struct dwarf2_section_info tu_index;
73869dc2
DE
711
712 /* These are only used by DWP version 2 files.
713 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
714 sections are referenced by section number, and are not recorded here.
715 In DWP version 2 there is at most one copy of all these sections, each
716 section being (effectively) comprised of the concatenation of all of the
717 individual sections that exist in the version 1 format.
718 To keep the code simple we treat each of these concatenated pieces as a
719 section itself (a virtual section?). */
720 struct dwarf2_section_info abbrev;
721 struct dwarf2_section_info info;
722 struct dwarf2_section_info line;
723 struct dwarf2_section_info loc;
724 struct dwarf2_section_info macinfo;
725 struct dwarf2_section_info macro;
726 struct dwarf2_section_info str_offsets;
727 struct dwarf2_section_info types;
80626a55
DE
728};
729
73869dc2
DE
730/* These sections are what may appear in a virtual DWO file in DWP version 1.
731 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 732
73869dc2 733struct virtual_v1_dwo_sections
80626a55
DE
734{
735 struct dwarf2_section_info abbrev;
736 struct dwarf2_section_info line;
737 struct dwarf2_section_info loc;
738 struct dwarf2_section_info macinfo;
739 struct dwarf2_section_info macro;
740 struct dwarf2_section_info str_offsets;
741 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 742 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
743 struct dwarf2_section_info info_or_types;
744};
745
73869dc2
DE
746/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
747 In version 2, the sections of the DWO files are concatenated together
748 and stored in one section of that name. Thus each ELF section contains
749 several "virtual" sections. */
750
751struct virtual_v2_dwo_sections
752{
753 bfd_size_type abbrev_offset;
754 bfd_size_type abbrev_size;
755
756 bfd_size_type line_offset;
757 bfd_size_type line_size;
758
759 bfd_size_type loc_offset;
760 bfd_size_type loc_size;
761
762 bfd_size_type macinfo_offset;
763 bfd_size_type macinfo_size;
764
765 bfd_size_type macro_offset;
766 bfd_size_type macro_size;
767
768 bfd_size_type str_offsets_offset;
769 bfd_size_type str_offsets_size;
770
771 /* Each DWP hash table entry records one CU or one TU.
772 That is recorded here, and copied to dwo_unit.section. */
773 bfd_size_type info_or_types_offset;
774 bfd_size_type info_or_types_size;
775};
776
80626a55
DE
777/* Contents of DWP hash tables. */
778
779struct dwp_hash_table
780{
73869dc2 781 uint32_t version, nr_columns;
80626a55 782 uint32_t nr_units, nr_slots;
73869dc2
DE
783 const gdb_byte *hash_table, *unit_table;
784 union
785 {
786 struct
787 {
788 const gdb_byte *indices;
789 } v1;
790 struct
791 {
792 /* This is indexed by column number and gives the id of the section
793 in that column. */
794#define MAX_NR_V2_DWO_SECTIONS \
795 (1 /* .debug_info or .debug_types */ \
796 + 1 /* .debug_abbrev */ \
797 + 1 /* .debug_line */ \
798 + 1 /* .debug_loc */ \
799 + 1 /* .debug_str_offsets */ \
800 + 1 /* .debug_macro or .debug_macinfo */)
801 int section_ids[MAX_NR_V2_DWO_SECTIONS];
802 const gdb_byte *offsets;
803 const gdb_byte *sizes;
804 } v2;
805 } section_pool;
80626a55
DE
806};
807
808/* Data for one DWP file. */
809
810struct dwp_file
811{
400174b1
TT
812 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
813 : name (name_),
814 dbfd (std::move (abfd))
815 {
816 }
817
80626a55
DE
818 /* Name of the file. */
819 const char *name;
820
73869dc2 821 /* File format version. */
400174b1 822 int version = 0;
73869dc2 823
93417882 824 /* The bfd. */
400174b1 825 gdb_bfd_ref_ptr dbfd;
80626a55
DE
826
827 /* Section info for this file. */
400174b1 828 struct dwp_sections sections {};
80626a55 829
57d63ce2 830 /* Table of CUs in the file. */
400174b1 831 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
832
833 /* Table of TUs in the file. */
400174b1 834 const struct dwp_hash_table *tus = nullptr;
80626a55 835
19ac8c2e 836 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
837 htab_up loaded_cus;
838 htab_up loaded_tus;
80626a55 839
73869dc2
DE
840 /* Table to map ELF section numbers to their sections.
841 This is only needed for the DWP V1 file format. */
400174b1
TT
842 unsigned int num_sections = 0;
843 asection **elf_sections = nullptr;
80626a55
DE
844};
845
0963b4bd
MS
846/* Struct used to pass misc. parameters to read_die_and_children, et
847 al. which are used for both .debug_info and .debug_types dies.
848 All parameters here are unchanging for the life of the call. This
dee91e82 849 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
850
851struct die_reader_specs
852{
a32a8923 853 /* The bfd of die_section. */
93311388
DE
854 bfd* abfd;
855
856 /* The CU of the DIE we are parsing. */
857 struct dwarf2_cu *cu;
858
80626a55 859 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
860 struct dwo_file *dwo_file;
861
dee91e82 862 /* The section the die comes from.
3019eac3 863 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
864 struct dwarf2_section_info *die_section;
865
866 /* die_section->buffer. */
d521ce57 867 const gdb_byte *buffer;
f664829e
DE
868
869 /* The end of the buffer. */
870 const gdb_byte *buffer_end;
a2ce51a0 871
685af9cd
TT
872 /* The abbreviation table to use when reading the DIEs. */
873 struct abbrev_table *abbrev_table;
93311388
DE
874};
875
c0ab21c2
TT
876/* A subclass of die_reader_specs that holds storage and has complex
877 constructor and destructor behavior. */
878
879class cutu_reader : public die_reader_specs
880{
881public:
882
883 cutu_reader (struct dwarf2_per_cu_data *this_cu,
884 struct abbrev_table *abbrev_table,
6751ebae 885 int use_existing_cu,
c0ab21c2
TT
886 bool skip_partial);
887
888 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
889 struct dwarf2_cu *parent_cu = nullptr,
890 struct dwo_file *dwo_file = nullptr);
891
c0ab21c2
TT
892 DISABLE_COPY_AND_ASSIGN (cutu_reader);
893
894 const gdb_byte *info_ptr = nullptr;
895 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
896 bool dummy_p = false;
897
6751ebae
TT
898 /* Release the new CU, putting it on the chain. This cannot be done
899 for dummy CUs. */
900 void keep ();
901
c0ab21c2
TT
902private:
903 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 904 int use_existing_cu);
c0ab21c2
TT
905
906 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
907 std::unique_ptr<dwarf2_cu> m_new_cu;
908
909 /* The ordinary abbreviation table. */
910 abbrev_table_up m_abbrev_table_holder;
911
912 /* The DWO abbreviation table. */
913 abbrev_table_up m_dwo_abbrev_table;
914};
dee91e82 915
c906108c 916/* When we construct a partial symbol table entry we only
0963b4bd 917 need this much information. */
6f06d47b 918struct partial_die_info : public allocate_on_obstack
c906108c 919 {
6f06d47b
YQ
920 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
921
922 /* Disable assign but still keep copy ctor, which is needed
923 load_partial_dies. */
924 partial_die_info& operator=(const partial_die_info& rhs) = delete;
925
52356b79
YQ
926 /* Adjust the partial die before generating a symbol for it. This
927 function may set the is_external flag or change the DIE's
928 name. */
929 void fixup (struct dwarf2_cu *cu);
930
48fbe735
YQ
931 /* Read a minimal amount of information into the minimal die
932 structure. */
933 const gdb_byte *read (const struct die_reader_specs *reader,
934 const struct abbrev_info &abbrev,
935 const gdb_byte *info_ptr);
936
72bf9492 937 /* Offset of this DIE. */
6f06d47b 938 const sect_offset sect_off;
72bf9492
DJ
939
940 /* DWARF-2 tag for this DIE. */
6f06d47b 941 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 942
72bf9492 943 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
944 const unsigned int has_children : 1;
945
72bf9492
DJ
946 unsigned int is_external : 1;
947 unsigned int is_declaration : 1;
948 unsigned int has_type : 1;
949 unsigned int has_specification : 1;
950 unsigned int has_pc_info : 1;
481860b3 951 unsigned int may_be_inlined : 1;
72bf9492 952
0c1b455e
TT
953 /* This DIE has been marked DW_AT_main_subprogram. */
954 unsigned int main_subprogram : 1;
955
72bf9492
DJ
956 /* Flag set if the SCOPE field of this structure has been
957 computed. */
958 unsigned int scope_set : 1;
959
fa4028e9
JB
960 /* Flag set if the DIE has a byte_size attribute. */
961 unsigned int has_byte_size : 1;
962
ff908ebf
AW
963 /* Flag set if the DIE has a DW_AT_const_value attribute. */
964 unsigned int has_const_value : 1;
965
98bfdba5
PA
966 /* Flag set if any of the DIE's children are template arguments. */
967 unsigned int has_template_arguments : 1;
968
52356b79 969 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
970 unsigned int fixup_called : 1;
971
36586728
TT
972 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
973 unsigned int is_dwz : 1;
974
975 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
976 unsigned int spec_is_dwz : 1;
977
72bf9492 978 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 979 sometimes a default name for unnamed DIEs. */
6f06d47b 980 const char *name = nullptr;
72bf9492 981
abc72ce4 982 /* The linkage name, if present. */
6f06d47b 983 const char *linkage_name = nullptr;
abc72ce4 984
72bf9492
DJ
985 /* The scope to prepend to our children. This is generally
986 allocated on the comp_unit_obstack, so will disappear
987 when this compilation unit leaves the cache. */
6f06d47b 988 const char *scope = nullptr;
72bf9492 989
95554aad
TT
990 /* Some data associated with the partial DIE. The tag determines
991 which field is live. */
992 union
993 {
994 /* The location description associated with this DIE, if any. */
995 struct dwarf_block *locdesc;
996 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 997 sect_offset sect_off;
6f06d47b 998 } d {};
72bf9492
DJ
999
1000 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1001 CORE_ADDR lowpc = 0;
1002 CORE_ADDR highpc = 0;
72bf9492 1003
93311388 1004 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1005 DW_AT_sibling, if any. */
48fbe735
YQ
1006 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1007 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1008 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1009
1010 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1011 DW_AT_specification (or DW_AT_abstract_origin or
1012 DW_AT_extension). */
6f06d47b 1013 sect_offset spec_offset {};
72bf9492
DJ
1014
1015 /* Pointers to this DIE's parent, first child, and next sibling,
1016 if any. */
6f06d47b
YQ
1017 struct partial_die_info *die_parent = nullptr;
1018 struct partial_die_info *die_child = nullptr;
1019 struct partial_die_info *die_sibling = nullptr;
1020
1021 friend struct partial_die_info *
1022 dwarf2_cu::find_partial_die (sect_offset sect_off);
1023
1024 private:
1025 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1026 partial_die_info (sect_offset sect_off)
1027 : partial_die_info (sect_off, DW_TAG_padding, 0)
1028 {
1029 }
1030
1031 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1032 int has_children_)
1033 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1034 {
1035 is_external = 0;
1036 is_declaration = 0;
1037 has_type = 0;
1038 has_specification = 0;
1039 has_pc_info = 0;
1040 may_be_inlined = 0;
1041 main_subprogram = 0;
1042 scope_set = 0;
1043 has_byte_size = 0;
1044 has_const_value = 0;
1045 has_template_arguments = 0;
1046 fixup_called = 0;
1047 is_dwz = 0;
1048 spec_is_dwz = 0;
1049 }
c906108c
SS
1050 };
1051
0963b4bd 1052/* This data structure holds a complete die structure. */
c906108c
SS
1053struct die_info
1054 {
76815b17
DE
1055 /* DWARF-2 tag for this DIE. */
1056 ENUM_BITFIELD(dwarf_tag) tag : 16;
1057
1058 /* Number of attributes */
98bfdba5
PA
1059 unsigned char num_attrs;
1060
1061 /* True if we're presently building the full type name for the
1062 type derived from this DIE. */
1063 unsigned char building_fullname : 1;
76815b17 1064
adde2bff
DE
1065 /* True if this die is in process. PR 16581. */
1066 unsigned char in_process : 1;
1067
3e225074
TT
1068 /* True if this DIE has children. */
1069 unsigned char has_children : 1;
1070
76815b17
DE
1071 /* Abbrev number */
1072 unsigned int abbrev;
1073
93311388 1074 /* Offset in .debug_info or .debug_types section. */
9c541725 1075 sect_offset sect_off;
78ba4af6
JB
1076
1077 /* The dies in a compilation unit form an n-ary tree. PARENT
1078 points to this die's parent; CHILD points to the first child of
1079 this node; and all the children of a given node are chained
4950bc1c 1080 together via their SIBLING fields. */
639d11d3
DC
1081 struct die_info *child; /* Its first child, if any. */
1082 struct die_info *sibling; /* Its next sibling, if any. */
1083 struct die_info *parent; /* Its parent, if any. */
c906108c 1084
b60c80d6
DJ
1085 /* An array of attributes, with NUM_ATTRS elements. There may be
1086 zero, but it's not common and zero-sized arrays are not
1087 sufficiently portable C. */
1088 struct attribute attrs[1];
c906108c
SS
1089 };
1090
c906108c
SS
1091/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1092 but this would require a corresponding change in unpack_field_as_long
1093 and friends. */
1094static int bits_per_byte = 8;
1095
2ddeaf8a
TT
1096/* When reading a variant or variant part, we track a bit more
1097 information about the field, and store it in an object of this
1098 type. */
1099
1100struct variant_field
1101{
1102 /* If we see a DW_TAG_variant, then this will be the discriminant
1103 value. */
1104 ULONGEST discriminant_value;
1105 /* If we see a DW_TAG_variant, then this will be set if this is the
1106 default branch. */
1107 bool default_branch;
1108 /* While reading a DW_TAG_variant_part, this will be set if this
1109 field is the discriminant. */
1110 bool is_discriminant;
1111};
1112
52059ffd
TT
1113struct nextfield
1114{
be2daae6
TT
1115 int accessibility = 0;
1116 int virtuality = 0;
2ddeaf8a 1117 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1118 struct variant_field variant {};
1119 struct field field {};
52059ffd
TT
1120};
1121
1122struct fnfieldlist
1123{
be2daae6
TT
1124 const char *name = nullptr;
1125 std::vector<struct fn_field> fnfields;
52059ffd
TT
1126};
1127
c906108c
SS
1128/* The routines that read and process dies for a C struct or C++ class
1129 pass lists of data member fields and lists of member function fields
1130 in an instance of a field_info structure, as defined below. */
1131struct field_info
c5aa993b 1132 {
0963b4bd 1133 /* List of data member and baseclasses fields. */
be2daae6
TT
1134 std::vector<struct nextfield> fields;
1135 std::vector<struct nextfield> baseclasses;
c906108c 1136
7d0ccb61 1137 /* Number of fields (including baseclasses). */
be2daae6 1138 int nfields = 0;
c906108c 1139
85102364 1140 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1141 int non_public_fields = 0;
c906108c 1142
c5aa993b
JM
1143 /* Member function fieldlist array, contains name of possibly overloaded
1144 member function, number of overloaded member functions and a pointer
1145 to the head of the member function field chain. */
be2daae6 1146 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1147
1148 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1149 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1150 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1151
1152 /* Nested types defined by this class and the number of elements in this
1153 list. */
be2daae6 1154 std::vector<struct decl_field> nested_types_list;
c5aa993b 1155 };
c906108c 1156
ae038cb0
DJ
1157/* Loaded secondary compilation units are kept in memory until they
1158 have not been referenced for the processing of this many
1159 compilation units. Set this to zero to disable caching. Cache
1160 sizes of up to at least twenty will improve startup time for
1161 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1162static int dwarf_max_cache_age = 5;
920d2a44 1163static void
b4f54984
DE
1164show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1165 struct cmd_list_element *c, const char *value)
920d2a44 1166{
3e43a32a 1167 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1168 "DWARF compilation units is %s.\n"),
920d2a44
AC
1169 value);
1170}
4390d890 1171\f
c906108c
SS
1172/* local function prototypes */
1173
918dd910
JK
1174static void dwarf2_find_base_address (struct die_info *die,
1175 struct dwarf2_cu *cu);
1176
891813be 1177static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1178 (struct dwarf2_per_cu_data *per_cu, const char *name);
1179
f1902523
JK
1180static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1181 const gdb_byte *info_ptr,
3e225074 1182 struct die_info *type_unit_die);
f1902523 1183
ed2dc618
SM
1184static void dwarf2_build_psymtabs_hard
1185 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1186
72bf9492
DJ
1187static void scan_partial_symbols (struct partial_die_info *,
1188 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1189 int, struct dwarf2_cu *);
c906108c 1190
72bf9492
DJ
1191static void add_partial_symbol (struct partial_die_info *,
1192 struct dwarf2_cu *);
63d06c5c 1193
72bf9492
DJ
1194static void add_partial_namespace (struct partial_die_info *pdi,
1195 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1196 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1197
5d7cb8df 1198static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1199 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1200 struct dwarf2_cu *cu);
1201
72bf9492
DJ
1202static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1203 struct dwarf2_cu *cu);
91c24f0a 1204
bc30ff58
JB
1205static void add_partial_subprogram (struct partial_die_info *pdi,
1206 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1207 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1208
d521ce57 1209static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1210
dee91e82 1211static struct partial_die_info *load_partial_dies
d521ce57 1212 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1213
fb816e8b
TV
1214/* A pair of partial_die_info and compilation unit. */
1215struct cu_partial_die_info
1216{
1217 /* The compilation unit of the partial_die_info. */
1218 struct dwarf2_cu *cu;
1219 /* A partial_die_info. */
1220 struct partial_die_info *pdi;
122cf0f2
AB
1221
1222 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1223 : cu (cu),
1224 pdi (pdi)
405feb71 1225 { /* Nothing. */ }
122cf0f2
AB
1226
1227private:
1228 cu_partial_die_info () = delete;
fb816e8b
TV
1229};
1230
122cf0f2
AB
1231static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1232 struct dwarf2_cu *);
72bf9492 1233
d521ce57
TT
1234static const gdb_byte *read_attribute (const struct die_reader_specs *,
1235 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1236 const gdb_byte *, bool *need_reprocess);
1237
1238static void read_attribute_reprocess (const struct die_reader_specs *reader,
1239 struct attribute *attr);
1240
1241static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1242
c764a876 1243static LONGEST read_checked_initial_length_and_offset
d521ce57 1244 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1245 unsigned int *, unsigned int *);
613e1657 1246
ed2dc618
SM
1247static sect_offset read_abbrev_offset
1248 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1249 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1250
ed2dc618
SM
1251static const char *read_indirect_string
1252 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1253 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1254
ed2dc618
SM
1255static const char *read_indirect_line_string
1256 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1257 const struct comp_unit_head *, unsigned int *);
36586728 1258
ed2dc618
SM
1259static const char *read_indirect_string_at_offset
1260 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1261 LONGEST str_offset);
927aa2e7 1262
ed2dc618
SM
1263static const char *read_indirect_string_from_dwz
1264 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1265
d521ce57
TT
1266static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1267 const gdb_byte *,
3019eac3
DE
1268 unsigned int *);
1269
18a8505e
AT
1270static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1271 ULONGEST str_index);
1272
1273static const char *read_stub_str_index (struct dwarf2_cu *cu,
1274 ULONGEST str_index);
3019eac3 1275
e142c38c 1276static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1277
e142c38c
DJ
1278static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1279 struct dwarf2_cu *);
c906108c 1280
348e048f 1281static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1282 unsigned int);
348e048f 1283
7d45c7c3
KB
1284static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1285 struct dwarf2_cu *cu);
1286
a084a2a6
AT
1287static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1288
05cf31d1
JB
1289static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1290 struct dwarf2_cu *cu);
1291
e142c38c 1292static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1293
e142c38c 1294static struct die_info *die_specification (struct die_info *die,
f2f0e013 1295 struct dwarf2_cu **);
63d06c5c 1296
9c541725 1297static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1298 struct dwarf2_cu *cu);
debd256d 1299
f3f5162e 1300static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1301 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1302 CORE_ADDR, int decode_mapping);
c906108c 1303
804d2729
TT
1304static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1305 const char *);
c906108c 1306
a14ed312 1307static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1308 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1309
ff39bb5e 1310static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1311 struct dwarf2_cu *);
c906108c 1312
ff39bb5e 1313static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1314 struct type *type,
1315 const char *name,
1316 struct obstack *obstack,
12df843f 1317 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1318 const gdb_byte **bytes,
98bfdba5 1319 struct dwarf2_locexpr_baton **baton);
2df3850c 1320
e7c27a73 1321static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1322
b4ba55a1
JB
1323static int need_gnat_info (struct dwarf2_cu *);
1324
3e43a32a
MS
1325static struct type *die_descriptive_type (struct die_info *,
1326 struct dwarf2_cu *);
b4ba55a1
JB
1327
1328static void set_descriptive_type (struct type *, struct die_info *,
1329 struct dwarf2_cu *);
1330
e7c27a73
DJ
1331static struct type *die_containing_type (struct die_info *,
1332 struct dwarf2_cu *);
c906108c 1333
ff39bb5e 1334static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1335 struct dwarf2_cu *);
c906108c 1336
f792889a 1337static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1338
673bfd45
DE
1339static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1340
0d5cff50 1341static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1342
6e70227d 1343static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1344 const char *suffix, int physname,
1345 struct dwarf2_cu *cu);
63d06c5c 1346
e7c27a73 1347static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1348
348e048f
DE
1349static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1350
e7c27a73 1351static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1352
e7c27a73 1353static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1354
96408a79
SA
1355static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1356
71a3c369
TT
1357static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1358
ff013f42 1359static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1360 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1361
3a2b436a 1362/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1363 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1364enum pc_bounds_kind
1365{
e385593e 1366 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1367 PC_BOUNDS_NOT_PRESENT,
1368
e385593e
JK
1369 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1370 were present but they do not form a valid range of PC addresses. */
1371 PC_BOUNDS_INVALID,
1372
3a2b436a
JK
1373 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1374 PC_BOUNDS_RANGES,
1375
1376 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1377 PC_BOUNDS_HIGH_LOW,
1378};
1379
1380static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1381 CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *,
891813be 1383 dwarf2_psymtab *);
c906108c 1384
fae299cd
DC
1385static void get_scope_pc_bounds (struct die_info *,
1386 CORE_ADDR *, CORE_ADDR *,
1387 struct dwarf2_cu *);
1388
801e3a5b
JB
1389static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1390 CORE_ADDR, struct dwarf2_cu *);
1391
a14ed312 1392static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1393 struct dwarf2_cu *);
c906108c 1394
a14ed312 1395static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1396 struct type *, struct dwarf2_cu *);
c906108c 1397
a14ed312 1398static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1399 struct die_info *, struct type *,
e7c27a73 1400 struct dwarf2_cu *);
c906108c 1401
a14ed312 1402static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1403 struct type *,
1404 struct dwarf2_cu *);
c906108c 1405
134d01f1 1406static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1407
e7c27a73 1408static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1409
e7c27a73 1410static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1411
5d7cb8df
JK
1412static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1413
804d2729 1414static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1415
27aa8d6a
SW
1416static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1417
74921315
KS
1418static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1419
f55ee35c
JK
1420static struct type *read_module_type (struct die_info *die,
1421 struct dwarf2_cu *cu);
1422
38d518c9 1423static const char *namespace_name (struct die_info *die,
e142c38c 1424 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1425
134d01f1 1426static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1427
e7c27a73 1428static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1429
6e70227d 1430static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1431 struct dwarf2_cu *);
1432
bf6af496 1433static struct die_info *read_die_and_siblings_1
d521ce57 1434 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1435 struct die_info *);
639d11d3 1436
dee91e82 1437static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1438 const gdb_byte *info_ptr,
1439 const gdb_byte **new_info_ptr,
639d11d3
DC
1440 struct die_info *parent);
1441
d521ce57
TT
1442static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1443 struct die_info **, const gdb_byte *,
3e225074 1444 int);
3019eac3 1445
d521ce57 1446static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1447 struct die_info **, const gdb_byte *);
93311388 1448
e7c27a73 1449static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1450
15d034d0
TT
1451static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1452 struct obstack *);
71c25dea 1453
15d034d0 1454static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1455
15d034d0 1456static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1457 struct die_info *die,
1458 struct dwarf2_cu *cu);
1459
ca69b9e6
DE
1460static const char *dwarf2_physname (const char *name, struct die_info *die,
1461 struct dwarf2_cu *cu);
1462
e142c38c 1463static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1464 struct dwarf2_cu **);
9219021c 1465
f39c6ffd 1466static const char *dwarf_tag_name (unsigned int);
c906108c 1467
f39c6ffd 1468static const char *dwarf_attr_name (unsigned int);
c906108c 1469
f39c6ffd 1470static const char *dwarf_form_name (unsigned int);
c906108c 1471
a121b7c1 1472static const char *dwarf_bool_name (unsigned int);
c906108c 1473
f39c6ffd 1474static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1475
f9aca02d 1476static struct die_info *sibling_die (struct die_info *);
c906108c 1477
d97bc12b
DE
1478static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1479
1480static void dump_die_for_error (struct die_info *);
1481
1482static void dump_die_1 (struct ui_file *, int level, int max_level,
1483 struct die_info *);
c906108c 1484
d97bc12b 1485/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1486
51545339 1487static void store_in_ref_table (struct die_info *,
10b3939b 1488 struct dwarf2_cu *);
c906108c 1489
ff39bb5e 1490static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1491
ff39bb5e 1492static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1493
348e048f 1494static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1495 const struct attribute *,
348e048f
DE
1496 struct dwarf2_cu **);
1497
10b3939b 1498static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1499 const struct attribute *,
f2f0e013 1500 struct dwarf2_cu **);
c906108c 1501
348e048f 1502static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1503 const struct attribute *,
348e048f
DE
1504 struct dwarf2_cu **);
1505
ac9ec31b
DE
1506static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1510 const struct attribute *,
ac9ec31b
DE
1511 struct dwarf2_cu *);
1512
e5fe5e75 1513static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1514
52dc124a 1515static void read_signatured_type (struct signatured_type *);
348e048f 1516
63e43d3a
PMR
1517static int attr_to_dynamic_prop (const struct attribute *attr,
1518 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1519 struct dynamic_prop *prop, struct type *type);
63e43d3a 1520
c906108c
SS
1521/* memory allocation interface */
1522
7b5a2f43 1523static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1524
b60c80d6 1525static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1526
43f3e411 1527static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1528
8cf6f0b1
TT
1529static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1530 struct dwarf2_loclist_baton *baton,
ff39bb5e 1531 const struct attribute *attr);
8cf6f0b1 1532
ff39bb5e 1533static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1534 struct symbol *sym,
f1e6e072
TT
1535 struct dwarf2_cu *cu,
1536 int is_block);
4c2df51b 1537
d521ce57
TT
1538static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1539 const gdb_byte *info_ptr,
1540 struct abbrev_info *abbrev);
4bb7a0a7 1541
72bf9492
DJ
1542static hashval_t partial_die_hash (const void *item);
1543
1544static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1545
ae038cb0 1546static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1547 (sect_offset sect_off, unsigned int offset_in_dwz,
1548 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1549
9816fde3 1550static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1551 struct die_info *comp_unit_die,
1552 enum language pretend_language);
93311388 1553
ed2dc618 1554static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1555
dee91e82 1556static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1557
f792889a
DJ
1558static struct type *set_die_type (struct die_info *, struct type *,
1559 struct dwarf2_cu *);
1c379e20 1560
ed2dc618 1561static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1562
ed2dc618 1563static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1564
58f0c718 1565static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1566 enum language);
10b3939b 1567
95554aad
TT
1568static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1569 enum language);
10b3939b 1570
f4dc4d17
DE
1571static void process_full_type_unit (struct dwarf2_per_cu_data *,
1572 enum language);
1573
10b3939b
DJ
1574static void dwarf2_add_dependence (struct dwarf2_cu *,
1575 struct dwarf2_per_cu_data *);
1576
ae038cb0
DJ
1577static void dwarf2_mark (struct dwarf2_cu *);
1578
1579static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1580
b64f50a1 1581static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1582 struct dwarf2_per_cu_data *);
673bfd45 1583
f792889a 1584static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1585
95554aad
TT
1586static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1587 enum language pretend_language);
1588
ed2dc618 1589static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1590
b303c6f6
AB
1591/* Class, the destructor of which frees all allocated queue entries. This
1592 will only have work to do if an error was thrown while processing the
1593 dwarf. If no error was thrown then the queue entries should have all
1594 been processed, and freed, as we went along. */
1595
1596class dwarf2_queue_guard
1597{
1598public:
39856def
TT
1599 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1600 : m_per_objfile (per_objfile)
1601 {
1602 }
b303c6f6
AB
1603
1604 /* Free any entries remaining on the queue. There should only be
1605 entries left if we hit an error while processing the dwarf. */
1606 ~dwarf2_queue_guard ()
1607 {
39856def
TT
1608 /* Ensure that no memory is allocated by the queue. */
1609 std::queue<dwarf2_queue_item> empty;
1610 std::swap (m_per_objfile->queue, empty);
1611 }
b303c6f6 1612
39856def 1613 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1614
39856def
TT
1615private:
1616 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1617};
1618
39856def
TT
1619dwarf2_queue_item::~dwarf2_queue_item ()
1620{
1621 /* Anything still marked queued is likely to be in an
1622 inconsistent state, so discard it. */
1623 if (per_cu->queued)
1624 {
1625 if (per_cu->cu != NULL)
1626 free_one_cached_comp_unit (per_cu);
1627 per_cu->queued = 0;
1628 }
1629}
1630
d721ba37
PA
1631/* The return type of find_file_and_directory. Note, the enclosed
1632 string pointers are only valid while this object is valid. */
1633
1634struct file_and_directory
1635{
1636 /* The filename. This is never NULL. */
1637 const char *name;
1638
1639 /* The compilation directory. NULL if not known. If we needed to
1640 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1641 points directly to the DW_AT_comp_dir string attribute owned by
1642 the obstack that owns the DIE. */
1643 const char *comp_dir;
1644
1645 /* If we needed to build a new string for comp_dir, this is what
1646 owns the storage. */
1647 std::string comp_dir_storage;
1648};
1649
1650static file_and_directory find_file_and_directory (struct die_info *die,
1651 struct dwarf2_cu *cu);
9291a0cd 1652
b0b6a987 1653static htab_up allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1654
b0b6a987 1655static htab_up allocate_dwo_unit_table (struct objfile *objfile);
3019eac3 1656
57d63ce2 1657static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1659 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1660 ULONGEST signature, int is_debug_types);
a2ce51a0 1661
ed2dc618
SM
1662static struct dwp_file *get_dwp_file
1663 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1664
3019eac3 1665static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1666 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1667
1668static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1669 (struct signatured_type *, const char *, const char *);
3019eac3 1670
89e63ee4
DE
1671static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1672
263db9a1
TT
1673/* A unique pointer to a dwo_file. */
1674
51ac9db5 1675typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1676
ed2dc618 1677static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1678
1b80a9fa 1679static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1680
1681static void free_line_header_voidp (void *arg);
4390d890
DE
1682\f
1683/* Various complaints about symbol reading that don't abort the process. */
1684
1685static void
1686dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1687{
b98664d3 1688 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1689}
1690
1691static void
1692dwarf2_debug_line_missing_file_complaint (void)
1693{
b98664d3 1694 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1695}
1696
1697static void
1698dwarf2_debug_line_missing_end_sequence_complaint (void)
1699{
b98664d3 1700 complaint (_(".debug_line section has line "
4390d890
DE
1701 "program sequence without an end"));
1702}
1703
1704static void
1705dwarf2_complex_location_expr_complaint (void)
1706{
b98664d3 1707 complaint (_("location expression too complex"));
4390d890
DE
1708}
1709
1710static void
1711dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1712 int arg3)
1713{
b98664d3 1714 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1715 arg1, arg2, arg3);
1716}
1717
1718static void
1719dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1720{
b98664d3 1721 complaint (_("debug info runs off end of %s section"
4390d890 1722 " [in module %s]"),
96b79293
TT
1723 section->get_name (),
1724 section->get_file_name ());
4390d890 1725}
1b80a9fa 1726
4390d890
DE
1727static void
1728dwarf2_macro_malformed_definition_complaint (const char *arg1)
1729{
b98664d3 1730 complaint (_("macro debug info contains a "
4390d890
DE
1731 "malformed macro definition:\n`%s'"),
1732 arg1);
1733}
1734
1735static void
1736dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1737{
b98664d3 1738 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1739 arg1, arg2);
1740}
527f3840
JK
1741
1742/* Hash function for line_header_hash. */
1743
1744static hashval_t
1745line_header_hash (const struct line_header *ofs)
1746{
9c541725 1747 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1748}
1749
1750/* Hash function for htab_create_alloc_ex for line_header_hash. */
1751
1752static hashval_t
1753line_header_hash_voidp (const void *item)
1754{
9a3c8263 1755 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1756
1757 return line_header_hash (ofs);
1758}
1759
1760/* Equality function for line_header_hash. */
1761
1762static int
1763line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1764{
9a3c8263
SM
1765 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1766 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1767
9c541725 1768 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1769 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1770}
1771
4390d890 1772\f
9291a0cd 1773
330cdd98
PA
1774/* See declaration. */
1775
1776dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1777 const dwarf2_debug_sections *names,
1778 bool can_copy_)
1779 : objfile (objfile_),
1780 can_copy (can_copy_)
330cdd98
PA
1781{
1782 if (names == NULL)
1783 names = &dwarf2_elf_names;
1784
1785 bfd *obfd = objfile->obfd;
1786
1787 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1788 locate_sections (obfd, sec, *names);
1789}
1790
1791dwarf2_per_objfile::~dwarf2_per_objfile ()
1792{
1793 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1794 free_cached_comp_units ();
1795
b76e467d 1796 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1797 per_cu->imported_symtabs_free ();
fc8e7e75 1798
b2bdb8cf 1799 for (signatured_type *sig_type : all_type_units)
ae640021 1800 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1801
330cdd98
PA
1802 /* Everything else should be on the objfile obstack. */
1803}
1804
1805/* See declaration. */
1806
1807void
1808dwarf2_per_objfile::free_cached_comp_units ()
1809{
1810 dwarf2_per_cu_data *per_cu = read_in_chain;
1811 dwarf2_per_cu_data **last_chain = &read_in_chain;
1812 while (per_cu != NULL)
1813 {
1814 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1815
fcd3b13d 1816 delete per_cu->cu;
330cdd98
PA
1817 *last_chain = next_cu;
1818 per_cu = next_cu;
1819 }
1820}
1821
11ed8cad
TT
1822/* A helper class that calls free_cached_comp_units on
1823 destruction. */
1824
1825class free_cached_comp_units
1826{
1827public:
1828
1829 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1830 : m_per_objfile (per_objfile)
1831 {
1832 }
1833
1834 ~free_cached_comp_units ()
1835 {
1836 m_per_objfile->free_cached_comp_units ();
1837 }
1838
1839 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1840
1841private:
1842
1843 dwarf2_per_objfile *m_per_objfile;
1844};
1845
c906108c 1846/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1847 information and return true if we have enough to do something.
1848 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1849 ELF names are used. CAN_COPY is true for formats where symbol
1850 interposition is possible and so symbol values must follow copy
1851 relocation rules. */
c906108c
SS
1852
1853int
251d32d9 1854dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1855 const struct dwarf2_debug_sections *names,
1856 bool can_copy)
c906108c 1857{
97cbe998
SDJ
1858 if (objfile->flags & OBJF_READNEVER)
1859 return 0;
1860
ed2dc618
SM
1861 struct dwarf2_per_objfile *dwarf2_per_objfile
1862 = get_dwarf2_per_objfile (objfile);
1863
1864 if (dwarf2_per_objfile == NULL)
5bfd760d 1865 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1866 names,
1867 can_copy);
5bfd760d 1868
73869dc2 1869 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1870 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1871 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1872 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1873}
1874
251d32d9
TG
1875/* When loading sections, we look either for uncompressed section or for
1876 compressed section names. */
233a11ab
CS
1877
1878static int
251d32d9
TG
1879section_is_p (const char *section_name,
1880 const struct dwarf2_section_names *names)
233a11ab 1881{
251d32d9
TG
1882 if (names->normal != NULL
1883 && strcmp (section_name, names->normal) == 0)
1884 return 1;
1885 if (names->compressed != NULL
1886 && strcmp (section_name, names->compressed) == 0)
1887 return 1;
1888 return 0;
233a11ab
CS
1889}
1890
330cdd98 1891/* See declaration. */
c906108c 1892
330cdd98
PA
1893void
1894dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1895 const dwarf2_debug_sections &names)
c906108c 1896{
fd361982 1897 flagword aflag = bfd_section_flags (sectp);
251d32d9 1898
dc7650b8
JK
1899 if ((aflag & SEC_HAS_CONTENTS) == 0)
1900 {
1901 }
950b7495
KS
1902 else if (elf_section_data (sectp)->this_hdr.sh_size
1903 > bfd_get_file_size (abfd))
1904 {
1905 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1906 warning (_("Discarding section %s which has a section size (%s"
1907 ") larger than the file size [in module %s]"),
1908 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1909 bfd_get_filename (abfd));
1910 }
330cdd98 1911 else if (section_is_p (sectp->name, &names.info))
c906108c 1912 {
330cdd98 1913 this->info.s.section = sectp;
fd361982 1914 this->info.size = bfd_section_size (sectp);
c906108c 1915 }
330cdd98 1916 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1917 {
330cdd98 1918 this->abbrev.s.section = sectp;
fd361982 1919 this->abbrev.size = bfd_section_size (sectp);
c906108c 1920 }
330cdd98 1921 else if (section_is_p (sectp->name, &names.line))
c906108c 1922 {
330cdd98 1923 this->line.s.section = sectp;
fd361982 1924 this->line.size = bfd_section_size (sectp);
c906108c 1925 }
330cdd98 1926 else if (section_is_p (sectp->name, &names.loc))
c906108c 1927 {
330cdd98 1928 this->loc.s.section = sectp;
fd361982 1929 this->loc.size = bfd_section_size (sectp);
c906108c 1930 }
330cdd98 1931 else if (section_is_p (sectp->name, &names.loclists))
43988095 1932 {
330cdd98 1933 this->loclists.s.section = sectp;
fd361982 1934 this->loclists.size = bfd_section_size (sectp);
43988095 1935 }
330cdd98 1936 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1937 {
330cdd98 1938 this->macinfo.s.section = sectp;
fd361982 1939 this->macinfo.size = bfd_section_size (sectp);
c906108c 1940 }
330cdd98 1941 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1942 {
330cdd98 1943 this->macro.s.section = sectp;
fd361982 1944 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1945 }
330cdd98 1946 else if (section_is_p (sectp->name, &names.str))
c906108c 1947 {
330cdd98 1948 this->str.s.section = sectp;
fd361982 1949 this->str.size = bfd_section_size (sectp);
c906108c 1950 }
18a8505e
AT
1951 else if (section_is_p (sectp->name, &names.str_offsets))
1952 {
1953 this->str_offsets.s.section = sectp;
1954 this->str_offsets.size = bfd_section_size (sectp);
1955 }
330cdd98 1956 else if (section_is_p (sectp->name, &names.line_str))
43988095 1957 {
330cdd98 1958 this->line_str.s.section = sectp;
fd361982 1959 this->line_str.size = bfd_section_size (sectp);
43988095 1960 }
330cdd98 1961 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1962 {
330cdd98 1963 this->addr.s.section = sectp;
fd361982 1964 this->addr.size = bfd_section_size (sectp);
3019eac3 1965 }
330cdd98 1966 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1967 {
330cdd98 1968 this->frame.s.section = sectp;
fd361982 1969 this->frame.size = bfd_section_size (sectp);
b6af0555 1970 }
330cdd98 1971 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1972 {
330cdd98 1973 this->eh_frame.s.section = sectp;
fd361982 1974 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1975 }
330cdd98 1976 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1977 {
330cdd98 1978 this->ranges.s.section = sectp;
fd361982 1979 this->ranges.size = bfd_section_size (sectp);
af34e669 1980 }
330cdd98 1981 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1982 {
330cdd98 1983 this->rnglists.s.section = sectp;
fd361982 1984 this->rnglists.size = bfd_section_size (sectp);
43988095 1985 }
330cdd98 1986 else if (section_is_p (sectp->name, &names.types))
348e048f 1987 {
8b70b953
TT
1988 struct dwarf2_section_info type_section;
1989
1990 memset (&type_section, 0, sizeof (type_section));
049412e3 1991 type_section.s.section = sectp;
fd361982 1992 type_section.size = bfd_section_size (sectp);
8b70b953 1993
fd5866f6 1994 this->types.push_back (type_section);
348e048f 1995 }
330cdd98 1996 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1997 {
330cdd98 1998 this->gdb_index.s.section = sectp;
fd361982 1999 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2000 }
927aa2e7
JK
2001 else if (section_is_p (sectp->name, &names.debug_names))
2002 {
2003 this->debug_names.s.section = sectp;
fd361982 2004 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2005 }
2006 else if (section_is_p (sectp->name, &names.debug_aranges))
2007 {
2008 this->debug_aranges.s.section = sectp;
fd361982 2009 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2010 }
dce234bc 2011
fd361982
AM
2012 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2013 && bfd_section_vma (sectp) == 0)
330cdd98 2014 this->has_section_at_zero = true;
c906108c
SS
2015}
2016
dce234bc 2017/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2018 SECTION_NAME. */
af34e669 2019
dce234bc 2020void
3017a003
TG
2021dwarf2_get_section_info (struct objfile *objfile,
2022 enum dwarf2_section_enum sect,
d521ce57 2023 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2024 bfd_size_type *sizep)
2025{
5bfd760d 2026 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2027 struct dwarf2_section_info *info;
a3b2a86b
TT
2028
2029 /* We may see an objfile without any DWARF, in which case we just
2030 return nothing. */
2031 if (data == NULL)
2032 {
2033 *sectp = NULL;
2034 *bufp = NULL;
2035 *sizep = 0;
2036 return;
2037 }
3017a003
TG
2038 switch (sect)
2039 {
2040 case DWARF2_DEBUG_FRAME:
2041 info = &data->frame;
2042 break;
2043 case DWARF2_EH_FRAME:
2044 info = &data->eh_frame;
2045 break;
2046 default:
2047 gdb_assert_not_reached ("unexpected section");
2048 }
dce234bc 2049
96b79293 2050 info->read (objfile);
dce234bc 2051
96b79293 2052 *sectp = info->get_bfd_section ();
dce234bc
PP
2053 *bufp = info->buffer;
2054 *sizep = info->size;
2055}
2056
36586728
TT
2057/* A helper function to find the sections for a .dwz file. */
2058
2059static void
2060locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2061{
9a3c8263 2062 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2063
2064 /* Note that we only support the standard ELF names, because .dwz
2065 is ELF-only (at the time of writing). */
2066 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2067 {
049412e3 2068 dwz_file->abbrev.s.section = sectp;
fd361982 2069 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2070 }
2071 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2072 {
049412e3 2073 dwz_file->info.s.section = sectp;
fd361982 2074 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2075 }
2076 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2077 {
049412e3 2078 dwz_file->str.s.section = sectp;
fd361982 2079 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2080 }
2081 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2082 {
049412e3 2083 dwz_file->line.s.section = sectp;
fd361982 2084 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2085 }
2086 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2087 {
049412e3 2088 dwz_file->macro.s.section = sectp;
fd361982 2089 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2090 }
2ec9a5e0
TT
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2092 {
049412e3 2093 dwz_file->gdb_index.s.section = sectp;
fd361982 2094 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2095 }
927aa2e7
JK
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2097 {
2098 dwz_file->debug_names.s.section = sectp;
fd361982 2099 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2100 }
36586728
TT
2101}
2102
c4973306 2103/* See dwarf2read.h. */
36586728 2104
c4973306 2105struct dwz_file *
ed2dc618 2106dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2107{
36586728 2108 const char *filename;
acd13123 2109 bfd_size_type buildid_len_arg;
dc294be5
TT
2110 size_t buildid_len;
2111 bfd_byte *buildid;
36586728
TT
2112
2113 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2114 return dwarf2_per_objfile->dwz_file.get ();
36586728 2115
4db1a1dc 2116 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2117 gdb::unique_xmalloc_ptr<char> data
2118 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2119 &buildid_len_arg, &buildid));
4db1a1dc
TT
2120 if (data == NULL)
2121 {
2122 if (bfd_get_error () == bfd_error_no_error)
2123 return NULL;
2124 error (_("could not read '.gnu_debugaltlink' section: %s"),
2125 bfd_errmsg (bfd_get_error ()));
2126 }
791afaa2
TT
2127
2128 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2129
acd13123
TT
2130 buildid_len = (size_t) buildid_len_arg;
2131
791afaa2 2132 filename = data.get ();
d721ba37
PA
2133
2134 std::string abs_storage;
36586728
TT
2135 if (!IS_ABSOLUTE_PATH (filename))
2136 {
14278e1f
TT
2137 gdb::unique_xmalloc_ptr<char> abs
2138 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2139
14278e1f 2140 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2141 filename = abs_storage.c_str ();
36586728
TT
2142 }
2143
dc294be5
TT
2144 /* First try the file name given in the section. If that doesn't
2145 work, try to use the build-id instead. */
192b62ce 2146 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2147 if (dwz_bfd != NULL)
36586728 2148 {
192b62ce 2149 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2150 dwz_bfd.reset (nullptr);
36586728
TT
2151 }
2152
dc294be5
TT
2153 if (dwz_bfd == NULL)
2154 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2155
2156 if (dwz_bfd == NULL)
2157 error (_("could not find '.gnu_debugaltlink' file for %s"),
2158 objfile_name (dwarf2_per_objfile->objfile));
2159
7ff8cb8c
TT
2160 std::unique_ptr<struct dwz_file> result
2161 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2162
7ff8cb8c
TT
2163 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2164 result.get ());
36586728 2165
7ff8cb8c
TT
2166 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2167 result->dwz_bfd.get ());
2168 dwarf2_per_objfile->dwz_file = std::move (result);
2169 return dwarf2_per_objfile->dwz_file.get ();
36586728 2170}
9291a0cd 2171\f
7b9f3c50
DE
2172/* DWARF quick_symbols_functions support. */
2173
2174/* TUs can share .debug_line entries, and there can be a lot more TUs than
2175 unique line tables, so we maintain a separate table of all .debug_line
2176 derived entries to support the sharing.
2177 All the quick functions need is the list of file names. We discard the
2178 line_header when we're done and don't need to record it here. */
2179struct quick_file_names
2180{
094b34ac
DE
2181 /* The data used to construct the hash key. */
2182 struct stmt_list_hash hash;
7b9f3c50
DE
2183
2184 /* The number of entries in file_names, real_names. */
2185 unsigned int num_file_names;
2186
2187 /* The file names from the line table, after being run through
2188 file_full_name. */
2189 const char **file_names;
2190
2191 /* The file names from the line table after being run through
2192 gdb_realpath. These are computed lazily. */
2193 const char **real_names;
2194};
2195
2196/* When using the index (and thus not using psymtabs), each CU has an
2197 object of this type. This is used to hold information needed by
2198 the various "quick" methods. */
2199struct dwarf2_per_cu_quick_data
2200{
2201 /* The file table. This can be NULL if there was no file table
2202 or it's currently not read in.
2203 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2204 struct quick_file_names *file_names;
2205
2206 /* The corresponding symbol table. This is NULL if symbols for this
2207 CU have not yet been read. */
43f3e411 2208 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2209
2210 /* A temporary mark bit used when iterating over all CUs in
2211 expand_symtabs_matching. */
2212 unsigned int mark : 1;
2213
2214 /* True if we've tried to read the file table and found there isn't one.
2215 There will be no point in trying to read it again next time. */
2216 unsigned int no_file_data : 1;
2217};
2218
094b34ac
DE
2219/* Utility hash function for a stmt_list_hash. */
2220
2221static hashval_t
2222hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2223{
2224 hashval_t v = 0;
2225
2226 if (stmt_list_hash->dwo_unit != NULL)
2227 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2228 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2229 return v;
2230}
2231
2232/* Utility equality function for a stmt_list_hash. */
2233
2234static int
2235eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2236 const struct stmt_list_hash *rhs)
2237{
2238 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2239 return 0;
2240 if (lhs->dwo_unit != NULL
2241 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2242 return 0;
2243
9c541725 2244 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2245}
2246
7b9f3c50
DE
2247/* Hash function for a quick_file_names. */
2248
2249static hashval_t
2250hash_file_name_entry (const void *e)
2251{
9a3c8263
SM
2252 const struct quick_file_names *file_data
2253 = (const struct quick_file_names *) e;
7b9f3c50 2254
094b34ac 2255 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2256}
2257
2258/* Equality function for a quick_file_names. */
2259
2260static int
2261eq_file_name_entry (const void *a, const void *b)
2262{
9a3c8263
SM
2263 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2264 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2265
094b34ac 2266 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2267}
2268
2269/* Delete function for a quick_file_names. */
2270
2271static void
2272delete_file_name_entry (void *e)
2273{
9a3c8263 2274 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2275 int i;
2276
2277 for (i = 0; i < file_data->num_file_names; ++i)
2278 {
2279 xfree ((void*) file_data->file_names[i]);
2280 if (file_data->real_names)
2281 xfree ((void*) file_data->real_names[i]);
2282 }
2283
2284 /* The space for the struct itself lives on objfile_obstack,
2285 so we don't free it here. */
2286}
2287
2288/* Create a quick_file_names hash table. */
2289
5895093f 2290static htab_up
7b9f3c50
DE
2291create_quick_file_names_table (unsigned int nr_initial_entries)
2292{
5895093f
TT
2293 return htab_up (htab_create_alloc (nr_initial_entries,
2294 hash_file_name_entry, eq_file_name_entry,
2295 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2296}
9291a0cd 2297
918dd910
JK
2298/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2299 have to be created afterwards. You should call age_cached_comp_units after
2300 processing PER_CU->CU. dw2_setup must have been already called. */
2301
2302static void
58f0c718 2303load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2304{
3019eac3 2305 if (per_cu->is_debug_types)
e5fe5e75 2306 load_full_type_unit (per_cu);
918dd910 2307 else
58f0c718 2308 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2309
cc12ce38
DE
2310 if (per_cu->cu == NULL)
2311 return; /* Dummy CU. */
2dc860c0
DE
2312
2313 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2314}
2315
a0f42c21 2316/* Read in the symbols for PER_CU. */
2fdf6df6 2317
9291a0cd 2318static void
58f0c718 2319dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2320{
ed2dc618 2321 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2322
f4dc4d17
DE
2323 /* Skip type_unit_groups, reading the type units they contain
2324 is handled elsewhere. */
2325 if (IS_TYPE_UNIT_GROUP (per_cu))
2326 return;
2327
b303c6f6
AB
2328 /* The destructor of dwarf2_queue_guard frees any entries left on
2329 the queue. After this point we're guaranteed to leave this function
2330 with the dwarf queue empty. */
39856def 2331 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2332
95554aad 2333 if (dwarf2_per_objfile->using_index
43f3e411 2334 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2335 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2336 {
2337 queue_comp_unit (per_cu, language_minimal);
58f0c718 2338 load_cu (per_cu, skip_partial);
89e63ee4
DE
2339
2340 /* If we just loaded a CU from a DWO, and we're working with an index
2341 that may badly handle TUs, load all the TUs in that DWO as well.
2342 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2343 if (!per_cu->is_debug_types
cc12ce38 2344 && per_cu->cu != NULL
89e63ee4
DE
2345 && per_cu->cu->dwo_unit != NULL
2346 && dwarf2_per_objfile->index_table != NULL
2347 && dwarf2_per_objfile->index_table->version <= 7
2348 /* DWP files aren't supported yet. */
ed2dc618 2349 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2350 queue_and_load_all_dwo_tus (per_cu);
95554aad 2351 }
9291a0cd 2352
ed2dc618 2353 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2354
2355 /* Age the cache, releasing compilation units that have not
2356 been used recently. */
ed2dc618 2357 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2358}
2359
2360/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2361 the objfile from which this CU came. Returns the resulting symbol
2362 table. */
2fdf6df6 2363
43f3e411 2364static struct compunit_symtab *
58f0c718 2365dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2366{
ed2dc618
SM
2367 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2368
95554aad 2369 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2370 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2371 {
11ed8cad 2372 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2373 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2374 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2375 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2376 }
f194fefb 2377
43f3e411 2378 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2379}
2380
ff4c9fec 2381/* See declaration. */
f4dc4d17 2382
ff4c9fec
SM
2383dwarf2_per_cu_data *
2384dwarf2_per_objfile::get_cutu (int index)
2385{
b76e467d 2386 if (index >= this->all_comp_units.size ())
ff4c9fec 2387 {
b76e467d 2388 index -= this->all_comp_units.size ();
b2bdb8cf 2389 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2390 return &this->all_type_units[index]->per_cu;
2391 }
f4dc4d17 2392
ff4c9fec
SM
2393 return this->all_comp_units[index];
2394}
f4dc4d17 2395
ff4c9fec 2396/* See declaration. */
2fdf6df6 2397
ff4c9fec
SM
2398dwarf2_per_cu_data *
2399dwarf2_per_objfile::get_cu (int index)
1fd400ff 2400{
b76e467d 2401 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2402
ff4c9fec 2403 return this->all_comp_units[index];
f4dc4d17
DE
2404}
2405
ff4c9fec 2406/* See declaration. */
f4dc4d17 2407
ff4c9fec
SM
2408signatured_type *
2409dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2410{
b2bdb8cf 2411 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2412
ff4c9fec 2413 return this->all_type_units[index];
1fd400ff
TT
2414}
2415
4b514bc8
JK
2416/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2417 objfile_obstack, and constructed with the specified field
2418 values. */
2419
2420static dwarf2_per_cu_data *
ed2dc618 2421create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2422 struct dwarf2_section_info *section,
2423 int is_dwz,
2424 sect_offset sect_off, ULONGEST length)
2425{
ed2dc618 2426 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2427 dwarf2_per_cu_data *the_cu
2428 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2429 struct dwarf2_per_cu_data);
2430 the_cu->sect_off = sect_off;
2431 the_cu->length = length;
e3b94546 2432 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2433 the_cu->section = section;
2434 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2435 struct dwarf2_per_cu_quick_data);
2436 the_cu->is_dwz = is_dwz;
2437 return the_cu;
2438}
2439
2ec9a5e0
TT
2440/* A helper for create_cus_from_index that handles a given list of
2441 CUs. */
2fdf6df6 2442
74a0d9f6 2443static void
12359b5e 2444create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2445 const gdb_byte *cu_list, offset_type n_elements,
2446 struct dwarf2_section_info *section,
b76e467d 2447 int is_dwz)
9291a0cd 2448{
12359b5e 2449 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2450 {
74a0d9f6 2451 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2452
2453 sect_offset sect_off
2454 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2455 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2456 cu_list += 2 * 8;
2457
b76e467d 2458 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2459 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2460 sect_off, length);
b76e467d 2461 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2462 }
9291a0cd
TT
2463}
2464
2ec9a5e0 2465/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2466 the CU objects for this objfile. */
2ec9a5e0 2467
74a0d9f6 2468static void
12359b5e 2469create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2470 const gdb_byte *cu_list, offset_type cu_list_elements,
2471 const gdb_byte *dwz_list, offset_type dwz_elements)
2472{
b76e467d
SM
2473 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2474 dwarf2_per_objfile->all_comp_units.reserve
2475 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2476
12359b5e 2477 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2478 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2479
2480 if (dwz_elements == 0)
74a0d9f6 2481 return;
2ec9a5e0 2482
12359b5e
SM
2483 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2484 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2485 &dwz->info, 1);
2ec9a5e0
TT
2486}
2487
1fd400ff 2488/* Create the signatured type hash table from the index. */
673bfd45 2489
74a0d9f6 2490static void
12359b5e
SM
2491create_signatured_type_table_from_index
2492 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2493 struct dwarf2_section_info *section,
2494 const gdb_byte *bytes,
2495 offset_type elements)
1fd400ff 2496{
12359b5e 2497 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2498
b2bdb8cf
SM
2499 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2500 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2501
b0b6a987 2502 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 2503
12359b5e 2504 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2505 {
52dc124a 2506 struct signatured_type *sig_type;
9c541725 2507 ULONGEST signature;
1fd400ff 2508 void **slot;
9c541725 2509 cu_offset type_offset_in_tu;
1fd400ff 2510
74a0d9f6 2511 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2512 sect_offset sect_off
2513 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2514 type_offset_in_tu
2515 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2516 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2517 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2518 bytes += 3 * 8;
2519
52dc124a 2520 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2521 struct signatured_type);
52dc124a 2522 sig_type->signature = signature;
9c541725 2523 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2524 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2525 sig_type->per_cu.section = section;
9c541725 2526 sig_type->per_cu.sect_off = sect_off;
e3b94546 2527 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2528 sig_type->per_cu.v.quick
1fd400ff
TT
2529 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2530 struct dwarf2_per_cu_quick_data);
2531
b0b6a987 2532 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2533 *slot = sig_type;
1fd400ff 2534
b2bdb8cf 2535 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2536 }
2537
b0b6a987 2538 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2539}
2540
927aa2e7
JK
2541/* Create the signatured type hash table from .debug_names. */
2542
2543static void
2544create_signatured_type_table_from_debug_names
ed2dc618 2545 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2546 const mapped_debug_names &map,
2547 struct dwarf2_section_info *section,
2548 struct dwarf2_section_info *abbrev_section)
2549{
ed2dc618
SM
2550 struct objfile *objfile = dwarf2_per_objfile->objfile;
2551
96b79293
TT
2552 section->read (objfile);
2553 abbrev_section->read (objfile);
927aa2e7 2554
b2bdb8cf
SM
2555 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2556 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2557
b0b6a987 2558 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
927aa2e7
JK
2559
2560 for (uint32_t i = 0; i < map.tu_count; ++i)
2561 {
2562 struct signatured_type *sig_type;
927aa2e7 2563 void **slot;
927aa2e7
JK
2564
2565 sect_offset sect_off
2566 = (sect_offset) (extract_unsigned_integer
2567 (map.tu_table_reordered + i * map.offset_size,
2568 map.offset_size,
2569 map.dwarf5_byte_order));
2570
2571 comp_unit_head cu_header;
ed2dc618
SM
2572 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2573 abbrev_section,
927aa2e7
JK
2574 section->buffer + to_underlying (sect_off),
2575 rcuh_kind::TYPE);
2576
2577 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2578 struct signatured_type);
2579 sig_type->signature = cu_header.signature;
2580 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2581 sig_type->per_cu.is_debug_types = 1;
2582 sig_type->per_cu.section = section;
2583 sig_type->per_cu.sect_off = sect_off;
e3b94546 2584 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2585 sig_type->per_cu.v.quick
2586 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2587 struct dwarf2_per_cu_quick_data);
2588
b0b6a987 2589 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2590 *slot = sig_type;
2591
b2bdb8cf 2592 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2593 }
2594
b0b6a987 2595 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2596}
2597
9291a0cd
TT
2598/* Read the address map data from the mapped index, and use it to
2599 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2600
9291a0cd 2601static void
ed2dc618
SM
2602create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2603 struct mapped_index *index)
9291a0cd 2604{
ed2dc618 2605 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2606 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2607 const gdb_byte *iter, *end;
9291a0cd 2608 struct addrmap *mutable_map;
9291a0cd
TT
2609 CORE_ADDR baseaddr;
2610
8268c778
PA
2611 auto_obstack temp_obstack;
2612
9291a0cd
TT
2613 mutable_map = addrmap_create_mutable (&temp_obstack);
2614
f00a2de2
PA
2615 iter = index->address_table.data ();
2616 end = iter + index->address_table.size ();
9291a0cd 2617
b3b3bada 2618 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2619
2620 while (iter < end)
2621 {
2622 ULONGEST hi, lo, cu_index;
2623 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2624 iter += 8;
2625 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2626 iter += 8;
2627 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2628 iter += 4;
f652bce2 2629
24a55014 2630 if (lo > hi)
f652bce2 2631 {
b98664d3 2632 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2633 hex_string (lo), hex_string (hi));
24a55014 2634 continue;
f652bce2 2635 }
24a55014 2636
b76e467d 2637 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2638 {
b98664d3 2639 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2640 (unsigned) cu_index);
24a55014 2641 continue;
f652bce2 2642 }
24a55014 2643
79748972
TT
2644 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2645 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2646 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2647 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2648 }
2649
d320c2b5 2650 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2651 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2652}
2653
927aa2e7
JK
2654/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2655 populate the objfile's psymtabs_addrmap. */
2656
2657static void
ed2dc618 2658create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2659 struct dwarf2_section_info *section)
2660{
ed2dc618 2661 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2662 bfd *abfd = objfile->obfd;
2663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2664 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2665
2666 auto_obstack temp_obstack;
2667 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2668
2669 std::unordered_map<sect_offset,
2670 dwarf2_per_cu_data *,
2671 gdb::hash_enum<sect_offset>>
2672 debug_info_offset_to_per_cu;
b76e467d 2673 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2674 {
927aa2e7
JK
2675 const auto insertpair
2676 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2677 if (!insertpair.second)
2678 {
2679 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2680 "debug_info_offset %s, ignoring .debug_aranges."),
2681 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2682 return;
2683 }
2684 }
2685
96b79293 2686 section->read (objfile);
927aa2e7
JK
2687
2688 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2689
2690 const gdb_byte *addr = section->buffer;
2691
2692 while (addr < section->buffer + section->size)
2693 {
2694 const gdb_byte *const entry_addr = addr;
2695 unsigned int bytes_read;
2696
2697 const LONGEST entry_length = read_initial_length (abfd, addr,
2698 &bytes_read);
2699 addr += bytes_read;
2700
2701 const gdb_byte *const entry_end = addr + entry_length;
2702 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2703 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2704 if (addr + entry_length > section->buffer + section->size)
2705 {
47e3f474 2706 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2707 "length %s exceeds section length %s, "
2708 "ignoring .debug_aranges."),
47e3f474
TV
2709 objfile_name (objfile),
2710 plongest (entry_addr - section->buffer),
927aa2e7
JK
2711 plongest (bytes_read + entry_length),
2712 pulongest (section->size));
2713 return;
2714 }
2715
2716 /* The version number. */
2717 const uint16_t version = read_2_bytes (abfd, addr);
2718 addr += 2;
2719 if (version != 2)
2720 {
47e3f474 2721 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2722 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2723 objfile_name (objfile),
2724 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2725 return;
2726 }
2727
2728 const uint64_t debug_info_offset
2729 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2730 addr += offset_size;
2731 const auto per_cu_it
2732 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2733 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2734 {
47e3f474 2735 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2736 "debug_info_offset %s does not exists, "
2737 "ignoring .debug_aranges."),
47e3f474
TV
2738 objfile_name (objfile),
2739 plongest (entry_addr - section->buffer),
927aa2e7
JK
2740 pulongest (debug_info_offset));
2741 return;
2742 }
2743 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2744
2745 const uint8_t address_size = *addr++;
2746 if (address_size < 1 || address_size > 8)
2747 {
47e3f474 2748 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2749 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2750 objfile_name (objfile),
2751 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2752 return;
2753 }
2754
2755 const uint8_t segment_selector_size = *addr++;
2756 if (segment_selector_size != 0)
2757 {
47e3f474 2758 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2759 "segment_selector_size %u is not supported, "
2760 "ignoring .debug_aranges."),
47e3f474
TV
2761 objfile_name (objfile),
2762 plongest (entry_addr - section->buffer),
927aa2e7
JK
2763 segment_selector_size);
2764 return;
2765 }
2766
2767 /* Must pad to an alignment boundary that is twice the address
2768 size. It is undocumented by the DWARF standard but GCC does
2769 use it. */
2770 for (size_t padding = ((-(addr - section->buffer))
2771 & (2 * address_size - 1));
2772 padding > 0; padding--)
2773 if (*addr++ != 0)
2774 {
47e3f474 2775 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2776 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2777 objfile_name (objfile),
2778 plongest (entry_addr - section->buffer));
927aa2e7
JK
2779 return;
2780 }
2781
2782 for (;;)
2783 {
2784 if (addr + 2 * address_size > entry_end)
2785 {
47e3f474 2786 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2787 "address list is not properly terminated, "
2788 "ignoring .debug_aranges."),
47e3f474
TV
2789 objfile_name (objfile),
2790 plongest (entry_addr - section->buffer));
927aa2e7
JK
2791 return;
2792 }
2793 ULONGEST start = extract_unsigned_integer (addr, address_size,
2794 dwarf5_byte_order);
2795 addr += address_size;
2796 ULONGEST length = extract_unsigned_integer (addr, address_size,
2797 dwarf5_byte_order);
2798 addr += address_size;
2799 if (start == 0 && length == 0)
2800 break;
2801 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2802 {
2803 /* Symbol was eliminated due to a COMDAT group. */
2804 continue;
2805 }
2806 ULONGEST end = start + length;
79748972
TT
2807 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2808 - baseaddr);
2809 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2810 - baseaddr);
927aa2e7
JK
2811 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2812 }
2813 }
2814
d320c2b5 2815 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2816 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2817}
2818
9291a0cd
TT
2819/* Find a slot in the mapped index INDEX for the object named NAME.
2820 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2821 constant pool and return true. If NAME cannot be found, return
2822 false. */
2fdf6df6 2823
109483d9 2824static bool
9291a0cd
TT
2825find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2826 offset_type **vec_out)
2827{
0cf03b49 2828 offset_type hash;
9291a0cd 2829 offset_type slot, step;
559a7a62 2830 int (*cmp) (const char *, const char *);
9291a0cd 2831
791afaa2 2832 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2833 if (current_language->la_language == language_cplus
45280282
IB
2834 || current_language->la_language == language_fortran
2835 || current_language->la_language == language_d)
0cf03b49
JK
2836 {
2837 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2838 not contain any. */
a8719064 2839
72998fb3 2840 if (strchr (name, '(') != NULL)
0cf03b49 2841 {
109483d9 2842 without_params = cp_remove_params (name);
0cf03b49 2843
72998fb3 2844 if (without_params != NULL)
791afaa2 2845 name = without_params.get ();
0cf03b49
JK
2846 }
2847 }
2848
559a7a62 2849 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2850 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2851 simulate our NAME being searched is also lowercased. */
2852 hash = mapped_index_string_hash ((index->version == 4
2853 && case_sensitivity == case_sensitive_off
2854 ? 5 : index->version),
2855 name);
2856
f00a2de2
PA
2857 slot = hash & (index->symbol_table.size () - 1);
2858 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2859 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2860
2861 for (;;)
2862 {
9291a0cd 2863 const char *str;
f00a2de2
PA
2864
2865 const auto &bucket = index->symbol_table[slot];
2866 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2867 return false;
9291a0cd 2868
f00a2de2 2869 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2870 if (!cmp (name, str))
9291a0cd
TT
2871 {
2872 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2873 + MAYBE_SWAP (bucket.vec));
109483d9 2874 return true;
9291a0cd
TT
2875 }
2876
f00a2de2 2877 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2878 }
2879}
2880
4485a1c1
SM
2881/* A helper function that reads the .gdb_index from BUFFER and fills
2882 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2883 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2884 ok to use deprecated sections.
2885
2886 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2887 out parameters that are filled in with information about the CU and
2888 TU lists in the section.
2889
4485a1c1 2890 Returns true if all went well, false otherwise. */
2fdf6df6 2891
d33bc52e 2892static bool
4485a1c1
SM
2893read_gdb_index_from_buffer (struct objfile *objfile,
2894 const char *filename,
2895 bool deprecated_ok,
2896 gdb::array_view<const gdb_byte> buffer,
2897 struct mapped_index *map,
2898 const gdb_byte **cu_list,
2899 offset_type *cu_list_elements,
2900 const gdb_byte **types_list,
2901 offset_type *types_list_elements)
2902{
2903 const gdb_byte *addr = &buffer[0];
82430852 2904
9291a0cd 2905 /* Version check. */
4485a1c1 2906 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2907 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2908 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2909 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2910 indices. */
831adc1f 2911 if (version < 4)
481860b3
GB
2912 {
2913 static int warning_printed = 0;
2914 if (!warning_printed)
2915 {
2916 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2917 filename);
481860b3
GB
2918 warning_printed = 1;
2919 }
2920 return 0;
2921 }
2922 /* Index version 4 uses a different hash function than index version
2923 5 and later.
2924
2925 Versions earlier than 6 did not emit psymbols for inlined
2926 functions. Using these files will cause GDB not to be able to
2927 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2928 indices unless the user has done
2929 "set use-deprecated-index-sections on". */
2ec9a5e0 2930 if (version < 6 && !deprecated_ok)
481860b3
GB
2931 {
2932 static int warning_printed = 0;
2933 if (!warning_printed)
2934 {
e615022a
DE
2935 warning (_("\
2936Skipping deprecated .gdb_index section in %s.\n\
2937Do \"set use-deprecated-index-sections on\" before the file is read\n\
2938to use the section anyway."),
2ec9a5e0 2939 filename);
481860b3
GB
2940 warning_printed = 1;
2941 }
2942 return 0;
2943 }
796a7ff8 2944 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2945 of the TU (for symbols coming from TUs),
2946 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2947 Plus gold-generated indices can have duplicate entries for global symbols,
2948 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2949 These are just performance bugs, and we can't distinguish gdb-generated
2950 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2951
481860b3 2952 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2953 longer backward compatible. */
796a7ff8 2954 if (version > 8)
594e8718 2955 return 0;
9291a0cd 2956
559a7a62 2957 map->version = version;
9291a0cd 2958
4485a1c1 2959 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2960
4485a1c1 2961 int i = 0;
2ec9a5e0
TT
2962 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2963 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2964 / 8);
1fd400ff
TT
2965 ++i;
2966
2ec9a5e0
TT
2967 *types_list = addr + MAYBE_SWAP (metadata[i]);
2968 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2969 - MAYBE_SWAP (metadata[i]))
2970 / 8);
987d643c 2971 ++i;
1fd400ff 2972
f00a2de2
PA
2973 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2974 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2975 map->address_table
2976 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2977 ++i;
2978
f00a2de2
PA
2979 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2980 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2981 map->symbol_table
2982 = gdb::array_view<mapped_index::symbol_table_slot>
2983 ((mapped_index::symbol_table_slot *) symbol_table,
2984 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2985
f00a2de2 2986 ++i;
f9d83a0b 2987 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2988
2ec9a5e0
TT
2989 return 1;
2990}
2991
4485a1c1
SM
2992/* Callback types for dwarf2_read_gdb_index. */
2993
2994typedef gdb::function_view
2995 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2996 get_gdb_index_contents_ftype;
2997typedef gdb::function_view
2998 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2999 get_gdb_index_contents_dwz_ftype;
3000
927aa2e7 3001/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3002 elements of all the CUs and return 1. Otherwise, return 0. */
3003
3004static int
4485a1c1
SM
3005dwarf2_read_gdb_index
3006 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3007 get_gdb_index_contents_ftype get_gdb_index_contents,
3008 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3009{
2ec9a5e0
TT
3010 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3011 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3012 struct dwz_file *dwz;
12359b5e 3013 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3014
4485a1c1
SM
3015 gdb::array_view<const gdb_byte> main_index_contents
3016 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3017
3018 if (main_index_contents.empty ())
3019 return 0;
3020
3063847f 3021 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3022 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3023 use_deprecated_index_sections,
3024 main_index_contents, map.get (), &cu_list,
3025 &cu_list_elements, &types_list,
3026 &types_list_elements))
2ec9a5e0
TT
3027 return 0;
3028
0fefef59 3029 /* Don't use the index if it's empty. */
3063847f 3030 if (map->symbol_table.empty ())
0fefef59
DE
3031 return 0;
3032
2ec9a5e0
TT
3033 /* If there is a .dwz file, read it so we can get its CU list as
3034 well. */
ed2dc618 3035 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3036 if (dwz != NULL)
2ec9a5e0 3037 {
2ec9a5e0
TT
3038 struct mapped_index dwz_map;
3039 const gdb_byte *dwz_types_ignore;
3040 offset_type dwz_types_elements_ignore;
3041
4485a1c1
SM
3042 gdb::array_view<const gdb_byte> dwz_index_content
3043 = get_gdb_index_contents_dwz (objfile, dwz);
3044
3045 if (dwz_index_content.empty ())
3046 return 0;
3047
3048 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3049 bfd_get_filename (dwz->dwz_bfd.get ()),
3050 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3051 &dwz_list, &dwz_list_elements,
3052 &dwz_types_ignore,
3053 &dwz_types_elements_ignore))
2ec9a5e0
TT
3054 {
3055 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3056 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3057 return 0;
3058 }
3059 }
3060
12359b5e
SM
3061 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3062 dwz_list, dwz_list_elements);
1fd400ff 3063
8b70b953
TT
3064 if (types_list_elements)
3065 {
8b70b953
TT
3066 /* We can only handle a single .debug_types when we have an
3067 index. */
fd5866f6 3068 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3069 return 0;
3070
fd5866f6 3071 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3072
12359b5e
SM
3073 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3074 types_list, types_list_elements);
8b70b953 3075 }
9291a0cd 3076
3063847f 3077 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3078
3063847f 3079 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3080 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3081 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3082 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3083
3084 return 1;
3085}
3086
dee91e82 3087/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3088
dee91e82
DE
3089static void
3090dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3091 const gdb_byte *info_ptr,
3e225074 3092 struct die_info *comp_unit_die)
9291a0cd 3093{
dee91e82 3094 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3095 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3096 struct dwarf2_per_objfile *dwarf2_per_objfile
3097 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3098 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3099 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3100 struct attribute *attr;
7b9f3c50
DE
3101 void **slot;
3102 struct quick_file_names *qfn;
9291a0cd 3103
0186c6a7
DE
3104 gdb_assert (! this_cu->is_debug_types);
3105
07261596
TT
3106 /* Our callers never want to match partial units -- instead they
3107 will match the enclosing full CU. */
3108 if (comp_unit_die->tag == DW_TAG_partial_unit)
3109 {
3110 this_cu->v.quick->no_file_data = 1;
3111 return;
3112 }
3113
0186c6a7 3114 lh_cu = this_cu;
7b9f3c50 3115 slot = NULL;
dee91e82 3116
fff8551c 3117 line_header_up lh;
9c541725 3118 sect_offset line_offset {};
fff8551c 3119
dee91e82 3120 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3121 if (attr != nullptr)
9291a0cd 3122 {
7b9f3c50
DE
3123 struct quick_file_names find_entry;
3124
9c541725 3125 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3126
3127 /* We may have already read in this line header (TU line header sharing).
3128 If we have we're done. */
094b34ac 3129 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3130 find_entry.hash.line_sect_off = line_offset;
5895093f 3131 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3132 &find_entry, INSERT);
3133 if (*slot != NULL)
3134 {
9a3c8263 3135 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3136 return;
7b9f3c50
DE
3137 }
3138
3019eac3 3139 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3140 }
3141 if (lh == NULL)
3142 {
094b34ac 3143 lh_cu->v.quick->no_file_data = 1;
dee91e82 3144 return;
9291a0cd
TT
3145 }
3146
8d749320 3147 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3148 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3149 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3150 gdb_assert (slot != NULL);
3151 *slot = qfn;
9291a0cd 3152
d721ba37 3153 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3154
aa391654
TT
3155 int offset = 0;
3156 if (strcmp (fnd.name, "<unknown>") != 0)
3157 ++offset;
3158
7ba99d21 3159 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3160 qfn->file_names =
aa391654
TT
3161 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3162 if (offset != 0)
3163 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3164 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3165 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3166 fnd.comp_dir).release ();
7b9f3c50 3167 qfn->real_names = NULL;
9291a0cd 3168
094b34ac 3169 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3170}
3171
3172/* A helper for the "quick" functions which attempts to read the line
3173 table for THIS_CU. */
3174
3175static struct quick_file_names *
e4a48d9d 3176dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3177{
0186c6a7
DE
3178 /* This should never be called for TUs. */
3179 gdb_assert (! this_cu->is_debug_types);
3180 /* Nor type unit groups. */
3181 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3182
dee91e82
DE
3183 if (this_cu->v.quick->file_names != NULL)
3184 return this_cu->v.quick->file_names;
3185 /* If we know there is no line data, no point in looking again. */
3186 if (this_cu->v.quick->no_file_data)
3187 return NULL;
3188
c0ab21c2
TT
3189 cutu_reader reader (this_cu);
3190 if (!reader.dummy_p)
3e225074 3191 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3192
3193 if (this_cu->v.quick->no_file_data)
3194 return NULL;
3195 return this_cu->v.quick->file_names;
9291a0cd
TT
3196}
3197
3198/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3199 real path for a given file name from the line table. */
2fdf6df6 3200
9291a0cd 3201static const char *
7b9f3c50
DE
3202dw2_get_real_path (struct objfile *objfile,
3203 struct quick_file_names *qfn, int index)
9291a0cd 3204{
7b9f3c50
DE
3205 if (qfn->real_names == NULL)
3206 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3207 qfn->num_file_names, const char *);
9291a0cd 3208
7b9f3c50 3209 if (qfn->real_names[index] == NULL)
14278e1f 3210 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3211
7b9f3c50 3212 return qfn->real_names[index];
9291a0cd
TT
3213}
3214
3215static struct symtab *
3216dw2_find_last_source_symtab (struct objfile *objfile)
3217{
ed2dc618
SM
3218 struct dwarf2_per_objfile *dwarf2_per_objfile
3219 = get_dwarf2_per_objfile (objfile);
b76e467d 3220 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3221 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3222
43f3e411
DE
3223 if (cust == NULL)
3224 return NULL;
ed2dc618 3225
43f3e411 3226 return compunit_primary_filetab (cust);
9291a0cd
TT
3227}
3228
7b9f3c50
DE
3229/* Traversal function for dw2_forget_cached_source_info. */
3230
3231static int
3232dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3233{
7b9f3c50 3234 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3235
7b9f3c50 3236 if (file_data->real_names)
9291a0cd 3237 {
7b9f3c50 3238 int i;
9291a0cd 3239
7b9f3c50 3240 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3241 {
7b9f3c50
DE
3242 xfree ((void*) file_data->real_names[i]);
3243 file_data->real_names[i] = NULL;
9291a0cd
TT
3244 }
3245 }
7b9f3c50
DE
3246
3247 return 1;
3248}
3249
3250static void
3251dw2_forget_cached_source_info (struct objfile *objfile)
3252{
ed2dc618
SM
3253 struct dwarf2_per_objfile *dwarf2_per_objfile
3254 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3255
5895093f 3256 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3257 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3258}
3259
f8eba3c6
TT
3260/* Helper function for dw2_map_symtabs_matching_filename that expands
3261 the symtabs and calls the iterator. */
3262
3263static int
3264dw2_map_expand_apply (struct objfile *objfile,
3265 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3266 const char *name, const char *real_path,
14bc53a8 3267 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3268{
43f3e411 3269 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3270
3271 /* Don't visit already-expanded CUs. */
43f3e411 3272 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3273 return 0;
3274
3275 /* This may expand more than one symtab, and we want to iterate over
3276 all of them. */
58f0c718 3277 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3278
14bc53a8
PA
3279 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3280 last_made, callback);
f8eba3c6
TT
3281}
3282
3283/* Implementation of the map_symtabs_matching_filename method. */
3284
14bc53a8
PA
3285static bool
3286dw2_map_symtabs_matching_filename
3287 (struct objfile *objfile, const char *name, const char *real_path,
3288 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3289{
c011a4f4 3290 const char *name_basename = lbasename (name);
ed2dc618
SM
3291 struct dwarf2_per_objfile *dwarf2_per_objfile
3292 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3293
848e3e78
DE
3294 /* The rule is CUs specify all the files, including those used by
3295 any TU, so there's no need to scan TUs here. */
f4dc4d17 3296
b76e467d 3297 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3298 {
3d7bb9d9 3299 /* We only need to look at symtabs not already expanded. */
43f3e411 3300 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3301 continue;
3302
b76e467d 3303 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3304 if (file_data == NULL)
9291a0cd
TT
3305 continue;
3306
b76e467d 3307 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3308 {
7b9f3c50 3309 const char *this_name = file_data->file_names[j];
da235a7c 3310 const char *this_real_name;
9291a0cd 3311
af529f8f 3312 if (compare_filenames_for_search (this_name, name))
9291a0cd 3313 {
f5b95b50 3314 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3315 callback))
3316 return true;
288e77a7 3317 continue;
4aac40c8 3318 }
9291a0cd 3319
c011a4f4
DE
3320 /* Before we invoke realpath, which can get expensive when many
3321 files are involved, do a quick comparison of the basenames. */
3322 if (! basenames_may_differ
3323 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3324 continue;
3325
da235a7c
JK
3326 this_real_name = dw2_get_real_path (objfile, file_data, j);
3327 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3328 {
da235a7c 3329 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3330 callback))
3331 return true;
288e77a7 3332 continue;
da235a7c 3333 }
9291a0cd 3334
da235a7c
JK
3335 if (real_path != NULL)
3336 {
af529f8f
JK
3337 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3338 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3339 if (this_real_name != NULL
af529f8f 3340 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3341 {
f5b95b50 3342 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3343 callback))
3344 return true;
288e77a7 3345 continue;
9291a0cd
TT
3346 }
3347 }
3348 }
3349 }
3350
14bc53a8 3351 return false;
9291a0cd
TT
3352}
3353
da51c347
DE
3354/* Struct used to manage iterating over all CUs looking for a symbol. */
3355
3356struct dw2_symtab_iterator
9291a0cd 3357{
ed2dc618
SM
3358 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3359 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3360 /* If set, only look for symbols that match that block. Valid values are
3361 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3362 gdb::optional<block_enum> block_index;
da51c347
DE
3363 /* The kind of symbol we're looking for. */
3364 domain_enum domain;
3365 /* The list of CUs from the index entry of the symbol,
3366 or NULL if not found. */
3367 offset_type *vec;
3368 /* The next element in VEC to look at. */
3369 int next;
3370 /* The number of elements in VEC, or zero if there is no match. */
3371 int length;
8943b874
DE
3372 /* Have we seen a global version of the symbol?
3373 If so we can ignore all further global instances.
3374 This is to work around gold/15646, inefficient gold-generated
3375 indices. */
3376 int global_seen;
da51c347 3377};
9291a0cd 3378
2b79f376 3379/* Initialize the index symtab iterator ITER. */
2fdf6df6 3380
9291a0cd 3381static void
da51c347 3382dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3383 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3384 gdb::optional<block_enum> block_index,
da51c347
DE
3385 domain_enum domain,
3386 const char *name)
3387{
ed2dc618 3388 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3389 iter->block_index = block_index;
3390 iter->domain = domain;
3391 iter->next = 0;
8943b874 3392 iter->global_seen = 0;
da51c347 3393
3063847f 3394 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3395
3396 /* index is NULL if OBJF_READNOW. */
3397 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3398 iter->length = MAYBE_SWAP (*iter->vec);
3399 else
3400 {
3401 iter->vec = NULL;
3402 iter->length = 0;
3403 }
3404}
3405
3406/* Return the next matching CU or NULL if there are no more. */
3407
3408static struct dwarf2_per_cu_data *
3409dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3410{
ed2dc618
SM
3411 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3412
da51c347
DE
3413 for ( ; iter->next < iter->length; ++iter->next)
3414 {
3415 offset_type cu_index_and_attrs =
3416 MAYBE_SWAP (iter->vec[iter->next + 1]);
3417 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3418 gdb_index_symbol_kind symbol_kind =
3419 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3420 /* Only check the symbol attributes if they're present.
3421 Indices prior to version 7 don't record them,
3422 and indices >= 7 may elide them for certain symbols
3423 (gold does this). */
3424 int attrs_valid =
ed2dc618 3425 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3426 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3427
3190f0c6 3428 /* Don't crash on bad data. */
b76e467d 3429 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3430 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3431 {
b98664d3 3432 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3433 " [in module %s]"),
3434 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3435 continue;
3436 }
3437
ff4c9fec 3438 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3439
da51c347 3440 /* Skip if already read in. */
43f3e411 3441 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3442 continue;
3443
8943b874
DE
3444 /* Check static vs global. */
3445 if (attrs_valid)
3446 {
2b79f376
SM
3447 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3448
3449 if (iter->block_index.has_value ())
3450 {
3451 bool want_static = *iter->block_index == STATIC_BLOCK;
3452
3453 if (is_static != want_static)
3454 continue;
3455 }
3456
8943b874
DE
3457 /* Work around gold/15646. */
3458 if (!is_static && iter->global_seen)
3459 continue;
3460 if (!is_static)
3461 iter->global_seen = 1;
3462 }
da51c347
DE
3463
3464 /* Only check the symbol's kind if it has one. */
3465 if (attrs_valid)
3466 {
3467 switch (iter->domain)
3468 {
3469 case VAR_DOMAIN:
3470 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3471 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3472 /* Some types are also in VAR_DOMAIN. */
3473 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3474 continue;
3475 break;
3476 case STRUCT_DOMAIN:
3477 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3478 continue;
3479 break;
3480 case LABEL_DOMAIN:
3481 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3482 continue;
3483 break;
59c35742
AB
3484 case MODULE_DOMAIN:
3485 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3486 continue;
3487 break;
da51c347
DE
3488 default:
3489 break;
3490 }
3491 }
3492
3493 ++iter->next;
3494 return per_cu;
3495 }
3496
3497 return NULL;
3498}
3499
43f3e411 3500static struct compunit_symtab *
c7f839cb 3501dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3502 const char *name, domain_enum domain)
9291a0cd 3503{
43f3e411 3504 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3505 struct dwarf2_per_objfile *dwarf2_per_objfile
3506 = get_dwarf2_per_objfile (objfile);
9291a0cd 3507
b5ec771e
PA
3508 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3509
ed2dc618
SM
3510 struct dw2_symtab_iterator iter;
3511 struct dwarf2_per_cu_data *per_cu;
da51c347 3512
2b79f376 3513 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3514
ed2dc618
SM
3515 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3516 {
3517 struct symbol *sym, *with_opaque = NULL;
58f0c718 3518 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3519 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3520 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3521
ed2dc618
SM
3522 sym = block_find_symbol (block, name, domain,
3523 block_find_non_opaque_type_preferred,
3524 &with_opaque);
b2e2f908 3525
ed2dc618
SM
3526 /* Some caution must be observed with overloaded functions
3527 and methods, since the index will not contain any overload
3528 information (but NAME might contain it). */
da51c347 3529
ed2dc618
SM
3530 if (sym != NULL
3531 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3532 return stab;
3533 if (with_opaque != NULL
3534 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3535 stab_best = stab;
da51c347 3536
ed2dc618 3537 /* Keep looking through other CUs. */
9291a0cd 3538 }
9291a0cd 3539
da51c347 3540 return stab_best;
9291a0cd
TT
3541}
3542
3543static void
3544dw2_print_stats (struct objfile *objfile)
3545{
ed2dc618
SM
3546 struct dwarf2_per_objfile *dwarf2_per_objfile
3547 = get_dwarf2_per_objfile (objfile);
b76e467d 3548 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3549 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3550 int count = 0;
9291a0cd 3551
ed2dc618 3552 for (int i = 0; i < total; ++i)
9291a0cd 3553 {
ff4c9fec 3554 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3555
43f3e411 3556 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3557 ++count;
3558 }
e4a48d9d 3559 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3560 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3561}
3562
779bd270
DE
3563/* This dumps minimal information about the index.
3564 It is called via "mt print objfiles".
3565 One use is to verify .gdb_index has been loaded by the
3566 gdb.dwarf2/gdb-index.exp testcase. */
3567
9291a0cd
TT
3568static void
3569dw2_dump (struct objfile *objfile)
3570{
ed2dc618
SM
3571 struct dwarf2_per_objfile *dwarf2_per_objfile
3572 = get_dwarf2_per_objfile (objfile);
3573
779bd270
DE
3574 gdb_assert (dwarf2_per_objfile->using_index);
3575 printf_filtered (".gdb_index:");
3576 if (dwarf2_per_objfile->index_table != NULL)
3577 {
3578 printf_filtered (" version %d\n",
3579 dwarf2_per_objfile->index_table->version);
3580 }
3581 else
3582 printf_filtered (" faked for \"readnow\"\n");
3583 printf_filtered ("\n");
9291a0cd
TT
3584}
3585
9291a0cd
TT
3586static void
3587dw2_expand_symtabs_for_function (struct objfile *objfile,
3588 const char *func_name)
3589{
ed2dc618
SM
3590 struct dwarf2_per_objfile *dwarf2_per_objfile
3591 = get_dwarf2_per_objfile (objfile);
da51c347 3592
ed2dc618
SM
3593 struct dw2_symtab_iterator iter;
3594 struct dwarf2_per_cu_data *per_cu;
da51c347 3595
2b79f376 3596 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3597
ed2dc618 3598 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3599 dw2_instantiate_symtab (per_cu, false);
da51c347 3600
9291a0cd
TT
3601}
3602
3603static void
3604dw2_expand_all_symtabs (struct objfile *objfile)
3605{
ed2dc618
SM
3606 struct dwarf2_per_objfile *dwarf2_per_objfile
3607 = get_dwarf2_per_objfile (objfile);
b76e467d 3608 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3609 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3610
ed2dc618 3611 for (int i = 0; i < total_units; ++i)
9291a0cd 3612 {
ff4c9fec 3613 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3614
58f0c718
TT
3615 /* We don't want to directly expand a partial CU, because if we
3616 read it with the wrong language, then assertion failures can
3617 be triggered later on. See PR symtab/23010. So, tell
3618 dw2_instantiate_symtab to skip partial CUs -- any important
3619 partial CU will be read via DW_TAG_imported_unit anyway. */
3620 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3621 }
3622}
3623
3624static void
652a8996
JK
3625dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3626 const char *fullname)
9291a0cd 3627{
ed2dc618
SM
3628 struct dwarf2_per_objfile *dwarf2_per_objfile
3629 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3630
3631 /* We don't need to consider type units here.
3632 This is only called for examining code, e.g. expand_line_sal.
3633 There can be an order of magnitude (or more) more type units
3634 than comp units, and we avoid them if we can. */
3635
b76e467d 3636 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3637 {
3d7bb9d9 3638 /* We only need to look at symtabs not already expanded. */
43f3e411 3639 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3640 continue;
3641
b76e467d 3642 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3643 if (file_data == NULL)
9291a0cd
TT
3644 continue;
3645
b76e467d 3646 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3647 {
652a8996
JK
3648 const char *this_fullname = file_data->file_names[j];
3649
3650 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3651 {
58f0c718 3652 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3653 break;
3654 }
3655 }
3656 }
3657}
3658
9291a0cd 3659static void
199b4314
TT
3660dw2_map_matching_symbols
3661 (struct objfile *objfile,
b054970d 3662 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3663 int global,
3664 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3665 symbol_compare_ftype *ordered_compare)
9291a0cd 3666{
40658b94 3667 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3668 current language is Ada for a non-Ada objfile using GNU index. As Ada
3669 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3670}
3671
e1ef7d7a
PA
3672/* Starting from a search name, return the string that finds the upper
3673 bound of all strings that start with SEARCH_NAME in a sorted name
3674 list. Returns the empty string to indicate that the upper bound is
3675 the end of the list. */
3676
3677static std::string
3678make_sort_after_prefix_name (const char *search_name)
3679{
3680 /* When looking to complete "func", we find the upper bound of all
3681 symbols that start with "func" by looking for where we'd insert
3682 the closest string that would follow "func" in lexicographical
3683 order. Usually, that's "func"-with-last-character-incremented,
3684 i.e. "fund". Mind non-ASCII characters, though. Usually those
3685 will be UTF-8 multi-byte sequences, but we can't be certain.
3686 Especially mind the 0xff character, which is a valid character in
3687 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3688 rule out compilers allowing it in identifiers. Note that
3689 conveniently, strcmp/strcasecmp are specified to compare
3690 characters interpreted as unsigned char. So what we do is treat
3691 the whole string as a base 256 number composed of a sequence of
3692 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3693 to 0, and carries 1 to the following more-significant position.
3694 If the very first character in SEARCH_NAME ends up incremented
3695 and carries/overflows, then the upper bound is the end of the
3696 list. The string after the empty string is also the empty
3697 string.
3698
3699 Some examples of this operation:
3700
3701 SEARCH_NAME => "+1" RESULT
3702
3703 "abc" => "abd"
3704 "ab\xff" => "ac"
3705 "\xff" "a" "\xff" => "\xff" "b"
3706 "\xff" => ""
3707 "\xff\xff" => ""
3708 "" => ""
3709
3710 Then, with these symbols for example:
3711
3712 func
3713 func1
3714 fund
3715
3716 completing "func" looks for symbols between "func" and
3717 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3718 which finds "func" and "func1", but not "fund".
3719
3720 And with:
3721
3722 funcÿ (Latin1 'ÿ' [0xff])
3723 funcÿ1
3724 fund
3725
3726 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3727 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3728
3729 And with:
3730
3731 ÿÿ (Latin1 'ÿ' [0xff])
3732 ÿÿ1
3733
3734 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3735 the end of the list.
3736 */
3737 std::string after = search_name;
3738 while (!after.empty () && (unsigned char) after.back () == 0xff)
3739 after.pop_back ();
3740 if (!after.empty ())
3741 after.back () = (unsigned char) after.back () + 1;
3742 return after;
3743}
3744
5c58de74 3745/* See declaration. */
61d96d7e 3746
5c58de74
PA
3747std::pair<std::vector<name_component>::const_iterator,
3748 std::vector<name_component>::const_iterator>
44ed8f3e 3749mapped_index_base::find_name_components_bounds
3b00ef10 3750 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3751{
5c58de74
PA
3752 auto *name_cmp
3753 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3754
3b00ef10
TT
3755 const char *lang_name
3756 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3757
3f563c84
PA
3758 /* Comparison function object for lower_bound that matches against a
3759 given symbol name. */
3760 auto lookup_compare_lower = [&] (const name_component &elem,
3761 const char *name)
3762 {
5c58de74 3763 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3764 const char *elem_name = elem_qualified + elem.name_offset;
3765 return name_cmp (elem_name, name) < 0;
3766 };
3767
3768 /* Comparison function object for upper_bound that matches against a
3769 given symbol name. */
3770 auto lookup_compare_upper = [&] (const char *name,
3771 const name_component &elem)
3772 {
5c58de74 3773 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3774 const char *elem_name = elem_qualified + elem.name_offset;
3775 return name_cmp (name, elem_name) < 0;
3776 };
3777
5c58de74
PA
3778 auto begin = this->name_components.begin ();
3779 auto end = this->name_components.end ();
3f563c84
PA
3780
3781 /* Find the lower bound. */
3782 auto lower = [&] ()
3783 {
3b00ef10 3784 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3785 return begin;
3786 else
3b00ef10 3787 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3788 } ();
3789
3790 /* Find the upper bound. */
3791 auto upper = [&] ()
3792 {
5c58de74 3793 if (lookup_name_without_params.completion_mode ())
3f563c84 3794 {
e1ef7d7a
PA
3795 /* In completion mode, we want UPPER to point past all
3796 symbols names that have the same prefix. I.e., with
3797 these symbols, and completing "func":
3798
3799 function << lower bound
3800 function1
3801 other_function << upper bound
3802
3803 We find the upper bound by looking for the insertion
3804 point of "func"-with-last-character-incremented,
3805 i.e. "fund". */
3b00ef10 3806 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3807 if (after.empty ())
3f563c84 3808 return end;
e6b2f5ef
PA
3809 return std::lower_bound (lower, end, after.c_str (),
3810 lookup_compare_lower);
3f563c84
PA
3811 }
3812 else
3b00ef10 3813 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3814 } ();
3815
5c58de74
PA
3816 return {lower, upper};
3817}
3818
3819/* See declaration. */
3820
3821void
44ed8f3e 3822mapped_index_base::build_name_components ()
5c58de74
PA
3823{
3824 if (!this->name_components.empty ())
3825 return;
3826
3827 this->name_components_casing = case_sensitivity;
3828 auto *name_cmp
3829 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3830
3831 /* The code below only knows how to break apart components of C++
3832 symbol names (and other languages that use '::' as
3b00ef10 3833 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3834 auto count = this->symbol_name_count ();
3835 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3836 {
44ed8f3e 3837 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3838 continue;
3839
3840 const char *name = this->symbol_name_at (idx);
3841
3842 /* Add each name component to the name component table. */
3843 unsigned int previous_len = 0;
3b00ef10
TT
3844
3845 if (strstr (name, "::") != nullptr)
3846 {
3847 for (unsigned int current_len = cp_find_first_component (name);
3848 name[current_len] != '\0';
3849 current_len += cp_find_first_component (name + current_len))
3850 {
3851 gdb_assert (name[current_len] == ':');
3852 this->name_components.push_back ({previous_len, idx});
3853 /* Skip the '::'. */
3854 current_len += 2;
3855 previous_len = current_len;
3856 }
3857 }
3858 else
5c58de74 3859 {
3b00ef10
TT
3860 /* Handle the Ada encoded (aka mangled) form here. */
3861 for (const char *iter = strstr (name, "__");
3862 iter != nullptr;
3863 iter = strstr (iter, "__"))
3864 {
3865 this->name_components.push_back ({previous_len, idx});
3866 iter += 2;
3867 previous_len = iter - name;
3868 }
5c58de74 3869 }
3b00ef10 3870
5c58de74
PA
3871 this->name_components.push_back ({previous_len, idx});
3872 }
3873
3874 /* Sort name_components elements by name. */
3875 auto name_comp_compare = [&] (const name_component &left,
3876 const name_component &right)
3877 {
3878 const char *left_qualified = this->symbol_name_at (left.idx);
3879 const char *right_qualified = this->symbol_name_at (right.idx);
3880
3881 const char *left_name = left_qualified + left.name_offset;
3882 const char *right_name = right_qualified + right.name_offset;
3883
3884 return name_cmp (left_name, right_name) < 0;
3885 };
3886
3887 std::sort (this->name_components.begin (),
3888 this->name_components.end (),
3889 name_comp_compare);
3890}
3891
3892/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3893 mapped_index_base instead of the containing objfile. This is split
3894 to a separate function in order to be able to unit test the
3895 name_components matching using a mock mapped_index_base. For each
5c58de74 3896 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3897 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3898
3899static void
3900dw2_expand_symtabs_matching_symbol
44ed8f3e 3901 (mapped_index_base &index,
5c58de74
PA
3902 const lookup_name_info &lookup_name_in,
3903 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3904 enum search_domain kind,
3b00ef10 3905 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3906{
3907 lookup_name_info lookup_name_without_params
3908 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3909
3910 /* Build the symbol name component sorted vector, if we haven't
3911 yet. */
3912 index.build_name_components ();
3913
3f563c84
PA
3914 /* The same symbol may appear more than once in the range though.
3915 E.g., if we're looking for symbols that complete "w", and we have
3916 a symbol named "w1::w2", we'll find the two name components for
3917 that same symbol in the range. To be sure we only call the
3918 callback once per symbol, we first collect the symbol name
3919 indexes that matched in a temporary vector and ignore
3920 duplicates. */
3921 std::vector<offset_type> matches;
3f563c84 3922
3b00ef10
TT
3923 struct name_and_matcher
3924 {
3925 symbol_name_matcher_ftype *matcher;
3926 const std::string &name;
3927
3928 bool operator== (const name_and_matcher &other) const
3f563c84 3929 {
3b00ef10
TT
3930 return matcher == other.matcher && name == other.name;
3931 }
3932 };
3933
3934 /* A vector holding all the different symbol name matchers, for all
3935 languages. */
3936 std::vector<name_and_matcher> matchers;
3937
3938 for (int i = 0; i < nr_languages; i++)
3939 {
3940 enum language lang_e = (enum language) i;
3941
3942 const language_defn *lang = language_def (lang_e);
3943 symbol_name_matcher_ftype *name_matcher
3944 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3945
3b00ef10
TT
3946 name_and_matcher key {
3947 name_matcher,
3948 lookup_name_without_params.language_lookup_name (lang_e)
3949 };
3950
3951 /* Don't insert the same comparison routine more than once.
3952 Note that we do this linear walk. This is not a problem in
3953 practice because the number of supported languages is
3954 low. */
3955 if (std::find (matchers.begin (), matchers.end (), key)
3956 != matchers.end ())
9291a0cd 3957 continue;
3b00ef10
TT
3958 matchers.push_back (std::move (key));
3959
3960 auto bounds
3961 = index.find_name_components_bounds (lookup_name_without_params,
3962 lang_e);
3963
3964 /* Now for each symbol name in range, check to see if we have a name
3965 match, and if so, call the MATCH_CALLBACK callback. */
3966
3967 for (; bounds.first != bounds.second; ++bounds.first)
3968 {
3969 const char *qualified = index.symbol_name_at (bounds.first->idx);
3970
3971 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3972 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3973 continue;
9291a0cd 3974
3b00ef10
TT
3975 matches.push_back (bounds.first->idx);
3976 }
3f563c84
PA
3977 }
3978
3979 std::sort (matches.begin (), matches.end ());
3980
3981 /* Finally call the callback, once per match. */
3982 ULONGEST prev = -1;
3983 for (offset_type idx : matches)
3984 {
3985 if (prev != idx)
3986 {
3b00ef10
TT
3987 if (!match_callback (idx))
3988 break;
3f563c84
PA
3989 prev = idx;
3990 }
3991 }
3992
3993 /* Above we use a type wider than idx's for 'prev', since 0 and
3994 (offset_type)-1 are both possible values. */
3995 static_assert (sizeof (prev) > sizeof (offset_type), "");
3996}
3997
c62446b1
PA
3998#if GDB_SELF_TEST
3999
4000namespace selftests { namespace dw2_expand_symtabs_matching {
4001
a3c5fafd
PA
4002/* A mock .gdb_index/.debug_names-like name index table, enough to
4003 exercise dw2_expand_symtabs_matching_symbol, which works with the
4004 mapped_index_base interface. Builds an index from the symbol list
4005 passed as parameter to the constructor. */
4006class mock_mapped_index : public mapped_index_base
c62446b1
PA
4007{
4008public:
a3c5fafd
PA
4009 mock_mapped_index (gdb::array_view<const char *> symbols)
4010 : m_symbol_table (symbols)
c62446b1
PA
4011 {}
4012
a3c5fafd 4013 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4014
a3c5fafd 4015 /* Return the number of names in the symbol table. */
632e107b 4016 size_t symbol_name_count () const override
c62446b1 4017 {
a3c5fafd 4018 return m_symbol_table.size ();
c62446b1
PA
4019 }
4020
a3c5fafd 4021 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4022 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4023 {
4024 return m_symbol_table[idx];
4025 }
c62446b1 4026
a3c5fafd
PA
4027private:
4028 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4029};
4030
4031/* Convenience function that converts a NULL pointer to a "<null>"
4032 string, to pass to print routines. */
4033
4034static const char *
4035string_or_null (const char *str)
4036{
4037 return str != NULL ? str : "<null>";
4038}
4039
4040/* Check if a lookup_name_info built from
4041 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4042 index. EXPECTED_LIST is the list of expected matches, in expected
4043 matching order. If no match expected, then an empty list is
4044 specified. Returns true on success. On failure prints a warning
4045 indicating the file:line that failed, and returns false. */
4046
4047static bool
4048check_match (const char *file, int line,
4049 mock_mapped_index &mock_index,
4050 const char *name, symbol_name_match_type match_type,
4051 bool completion_mode,
4052 std::initializer_list<const char *> expected_list)
4053{
4054 lookup_name_info lookup_name (name, match_type, completion_mode);
4055
4056 bool matched = true;
4057
4058 auto mismatch = [&] (const char *expected_str,
4059 const char *got)
4060 {
4061 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4062 "expected=\"%s\", got=\"%s\"\n"),
4063 file, line,
4064 (match_type == symbol_name_match_type::FULL
4065 ? "FULL" : "WILD"),
4066 name, string_or_null (expected_str), string_or_null (got));
4067 matched = false;
4068 };
4069
4070 auto expected_it = expected_list.begin ();
4071 auto expected_end = expected_list.end ();
4072
a3c5fafd 4073 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4074 NULL, ALL_DOMAIN,
4075 [&] (offset_type idx)
4076 {
a3c5fafd 4077 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4078 const char *expected_str
4079 = expected_it == expected_end ? NULL : *expected_it++;
4080
4081 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4082 mismatch (expected_str, matched_name);
3b00ef10 4083 return true;
c62446b1
PA
4084 });
4085
4086 const char *expected_str
4087 = expected_it == expected_end ? NULL : *expected_it++;
4088 if (expected_str != NULL)
4089 mismatch (expected_str, NULL);
4090
4091 return matched;
4092}
4093
4094/* The symbols added to the mock mapped_index for testing (in
4095 canonical form). */
4096static const char *test_symbols[] = {
4097 "function",
4098 "std::bar",
4099 "std::zfunction",
4100 "std::zfunction2",
4101 "w1::w2",
4102 "ns::foo<char*>",
4103 "ns::foo<int>",
4104 "ns::foo<long>",
a20714ff
PA
4105 "ns2::tmpl<int>::foo2",
4106 "(anonymous namespace)::A::B::C",
c62446b1 4107
e1ef7d7a
PA
4108 /* These are used to check that the increment-last-char in the
4109 matching algorithm for completion doesn't match "t1_fund" when
4110 completing "t1_func". */
4111 "t1_func",
4112 "t1_func1",
4113 "t1_fund",
4114 "t1_fund1",
4115
4116 /* A UTF-8 name with multi-byte sequences to make sure that
4117 cp-name-parser understands this as a single identifier ("função"
4118 is "function" in PT). */
4119 u8"u8função",
4120
4121 /* \377 (0xff) is Latin1 'ÿ'. */
4122 "yfunc\377",
4123
4124 /* \377 (0xff) is Latin1 'ÿ'. */
4125 "\377",
4126 "\377\377123",
4127
c62446b1
PA
4128 /* A name with all sorts of complications. Starts with "z" to make
4129 it easier for the completion tests below. */
4130#define Z_SYM_NAME \
4131 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4132 "::tuple<(anonymous namespace)::ui*, " \
4133 "std::default_delete<(anonymous namespace)::ui>, void>"
4134
4135 Z_SYM_NAME
4136};
4137
a3c5fafd
PA
4138/* Returns true if the mapped_index_base::find_name_component_bounds
4139 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4140 in completion mode. */
5c58de74
PA
4141
4142static bool
a3c5fafd 4143check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4144 const char *search_name,
4145 gdb::array_view<const char *> expected_syms)
4146{
4147 lookup_name_info lookup_name (search_name,
4148 symbol_name_match_type::FULL, true);
4149
3b00ef10
TT
4150 auto bounds = index.find_name_components_bounds (lookup_name,
4151 language_cplus);
5c58de74
PA
4152
4153 size_t distance = std::distance (bounds.first, bounds.second);
4154 if (distance != expected_syms.size ())
4155 return false;
4156
4157 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4158 {
4159 auto nc_elem = bounds.first + exp_elem;
4160 const char *qualified = index.symbol_name_at (nc_elem->idx);
4161 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4162 return false;
4163 }
4164
4165 return true;
4166}
4167
4168/* Test the lower-level mapped_index::find_name_component_bounds
4169 method. */
4170
c62446b1 4171static void
5c58de74
PA
4172test_mapped_index_find_name_component_bounds ()
4173{
4174 mock_mapped_index mock_index (test_symbols);
4175
a3c5fafd 4176 mock_index.build_name_components ();
5c58de74
PA
4177
4178 /* Test the lower-level mapped_index::find_name_component_bounds
4179 method in completion mode. */
4180 {
4181 static const char *expected_syms[] = {
4182 "t1_func",
4183 "t1_func1",
5c58de74
PA
4184 };
4185
a3c5fafd 4186 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4187 "t1_func", expected_syms));
4188 }
4189
4190 /* Check that the increment-last-char in the name matching algorithm
4191 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4192 {
4193 static const char *expected_syms1[] = {
4194 "\377",
4195 "\377\377123",
4196 };
a3c5fafd 4197 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4198 "\377", expected_syms1));
4199
4200 static const char *expected_syms2[] = {
4201 "\377\377123",
4202 };
a3c5fafd 4203 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4204 "\377\377", expected_syms2));
4205 }
4206}
4207
4208/* Test dw2_expand_symtabs_matching_symbol. */
4209
4210static void
4211test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4212{
4213 mock_mapped_index mock_index (test_symbols);
4214
4215 /* We let all tests run until the end even if some fails, for debug
4216 convenience. */
4217 bool any_mismatch = false;
4218
4219 /* Create the expected symbols list (an initializer_list). Needed
4220 because lists have commas, and we need to pass them to CHECK,
4221 which is a macro. */
4222#define EXPECT(...) { __VA_ARGS__ }
4223
4224 /* Wrapper for check_match that passes down the current
4225 __FILE__/__LINE__. */
4226#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4227 any_mismatch |= !check_match (__FILE__, __LINE__, \
4228 mock_index, \
4229 NAME, MATCH_TYPE, COMPLETION_MODE, \
4230 EXPECTED_LIST)
4231
4232 /* Identity checks. */
4233 for (const char *sym : test_symbols)
4234 {
4235 /* Should be able to match all existing symbols. */
4236 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4237 EXPECT (sym));
4238
4239 /* Should be able to match all existing symbols with
4240 parameters. */
4241 std::string with_params = std::string (sym) + "(int)";
4242 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4243 EXPECT (sym));
4244
4245 /* Should be able to match all existing symbols with
4246 parameters and qualifiers. */
4247 with_params = std::string (sym) + " ( int ) const";
4248 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4249 EXPECT (sym));
4250
4251 /* This should really find sym, but cp-name-parser.y doesn't
4252 know about lvalue/rvalue qualifiers yet. */
4253 with_params = std::string (sym) + " ( int ) &&";
4254 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4255 {});
4256 }
4257
e1ef7d7a
PA
4258 /* Check that the name matching algorithm for completion doesn't get
4259 confused with Latin1 'ÿ' / 0xff. */
4260 {
4261 static const char str[] = "\377";
4262 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4263 EXPECT ("\377", "\377\377123"));
4264 }
4265
4266 /* Check that the increment-last-char in the matching algorithm for
4267 completion doesn't match "t1_fund" when completing "t1_func". */
4268 {
4269 static const char str[] = "t1_func";
4270 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4271 EXPECT ("t1_func", "t1_func1"));
4272 }
4273
c62446b1
PA
4274 /* Check that completion mode works at each prefix of the expected
4275 symbol name. */
4276 {
4277 static const char str[] = "function(int)";
4278 size_t len = strlen (str);
4279 std::string lookup;
4280
4281 for (size_t i = 1; i < len; i++)
4282 {
4283 lookup.assign (str, i);
4284 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4285 EXPECT ("function"));
4286 }
4287 }
4288
4289 /* While "w" is a prefix of both components, the match function
4290 should still only be called once. */
4291 {
4292 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4293 EXPECT ("w1::w2"));
a20714ff
PA
4294 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4295 EXPECT ("w1::w2"));
c62446b1
PA
4296 }
4297
4298 /* Same, with a "complicated" symbol. */
4299 {
4300 static const char str[] = Z_SYM_NAME;
4301 size_t len = strlen (str);
4302 std::string lookup;
4303
4304 for (size_t i = 1; i < len; i++)
4305 {
4306 lookup.assign (str, i);
4307 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4308 EXPECT (Z_SYM_NAME));
4309 }
4310 }
4311
4312 /* In FULL mode, an incomplete symbol doesn't match. */
4313 {
4314 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4315 {});
4316 }
4317
4318 /* A complete symbol with parameters matches any overload, since the
4319 index has no overload info. */
4320 {
4321 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4322 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4323 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4324 EXPECT ("std::zfunction", "std::zfunction2"));
4325 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4326 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4327 }
4328
4329 /* Check that whitespace is ignored appropriately. A symbol with a
4330 template argument list. */
4331 {
4332 static const char expected[] = "ns::foo<int>";
4333 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4334 EXPECT (expected));
a20714ff
PA
4335 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4336 EXPECT (expected));
c62446b1
PA
4337 }
4338
4339 /* Check that whitespace is ignored appropriately. A symbol with a
4340 template argument list that includes a pointer. */
4341 {
4342 static const char expected[] = "ns::foo<char*>";
4343 /* Try both completion and non-completion modes. */
4344 static const bool completion_mode[2] = {false, true};
4345 for (size_t i = 0; i < 2; i++)
4346 {
4347 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4348 completion_mode[i], EXPECT (expected));
a20714ff
PA
4349 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4350 completion_mode[i], EXPECT (expected));
c62446b1
PA
4351
4352 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4353 completion_mode[i], EXPECT (expected));
a20714ff
PA
4354 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4355 completion_mode[i], EXPECT (expected));
c62446b1
PA
4356 }
4357 }
4358
4359 {
4360 /* Check method qualifiers are ignored. */
4361 static const char expected[] = "ns::foo<char*>";
4362 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4363 symbol_name_match_type::FULL, true, EXPECT (expected));
4364 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4365 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4366 CHECK_MATCH ("foo < char * > ( int ) const",
4367 symbol_name_match_type::WILD, true, EXPECT (expected));
4368 CHECK_MATCH ("foo < char * > ( int ) &&",
4369 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4370 }
4371
4372 /* Test lookup names that don't match anything. */
4373 {
a20714ff
PA
4374 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4375 {});
4376
c62446b1
PA
4377 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4378 {});
4379 }
4380
a20714ff
PA
4381 /* Some wild matching tests, exercising "(anonymous namespace)",
4382 which should not be confused with a parameter list. */
4383 {
4384 static const char *syms[] = {
4385 "A::B::C",
4386 "B::C",
4387 "C",
4388 "A :: B :: C ( int )",
4389 "B :: C ( int )",
4390 "C ( int )",
4391 };
4392
4393 for (const char *s : syms)
4394 {
4395 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4396 EXPECT ("(anonymous namespace)::A::B::C"));
4397 }
4398 }
4399
4400 {
4401 static const char expected[] = "ns2::tmpl<int>::foo2";
4402 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4403 EXPECT (expected));
4404 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4405 EXPECT (expected));
4406 }
4407
c62446b1
PA
4408 SELF_CHECK (!any_mismatch);
4409
4410#undef EXPECT
4411#undef CHECK_MATCH
4412}
4413
5c58de74
PA
4414static void
4415run_test ()
4416{
4417 test_mapped_index_find_name_component_bounds ();
4418 test_dw2_expand_symtabs_matching_symbol ();
4419}
4420
c62446b1
PA
4421}} // namespace selftests::dw2_expand_symtabs_matching
4422
4423#endif /* GDB_SELF_TEST */
4424
4b514bc8
JK
4425/* If FILE_MATCHER is NULL or if PER_CU has
4426 dwarf2_per_cu_quick_data::MARK set (see
4427 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4428 EXPANSION_NOTIFY on it. */
4429
4430static void
4431dw2_expand_symtabs_matching_one
4432 (struct dwarf2_per_cu_data *per_cu,
4433 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4434 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4435{
4436 if (file_matcher == NULL || per_cu->v.quick->mark)
4437 {
4438 bool symtab_was_null
4439 = (per_cu->v.quick->compunit_symtab == NULL);
4440
58f0c718 4441 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4442
4443 if (expansion_notify != NULL
4444 && symtab_was_null
4445 && per_cu->v.quick->compunit_symtab != NULL)
4446 expansion_notify (per_cu->v.quick->compunit_symtab);
4447 }
4448}
4449
3f563c84
PA
4450/* Helper for dw2_expand_matching symtabs. Called on each symbol
4451 matched, to expand corresponding CUs that were marked. IDX is the
4452 index of the symbol name that matched. */
4453
4454static void
4455dw2_expand_marked_cus
ed2dc618 4456 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4457 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4458 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4459 search_domain kind)
4460{
3f563c84
PA
4461 offset_type *vec, vec_len, vec_idx;
4462 bool global_seen = false;
ed2dc618 4463 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4464
61920122 4465 vec = (offset_type *) (index.constant_pool
f00a2de2 4466 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4467 vec_len = MAYBE_SWAP (vec[0]);
4468 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4469 {
61920122
PA
4470 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4471 /* This value is only valid for index versions >= 7. */
4472 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4473 gdb_index_symbol_kind symbol_kind =
4474 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4475 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4476 /* Only check the symbol attributes if they're present.
4477 Indices prior to version 7 don't record them,
4478 and indices >= 7 may elide them for certain symbols
4479 (gold does this). */
4480 int attrs_valid =
4481 (index.version >= 7
4482 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4483
4484 /* Work around gold/15646. */
4485 if (attrs_valid)
9291a0cd 4486 {
61920122
PA
4487 if (!is_static && global_seen)
4488 continue;
4489 if (!is_static)
4490 global_seen = true;
4491 }
3190f0c6 4492
61920122
PA
4493 /* Only check the symbol's kind if it has one. */
4494 if (attrs_valid)
4495 {
4496 switch (kind)
8943b874 4497 {
61920122
PA
4498 case VARIABLES_DOMAIN:
4499 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4500 continue;
4501 break;
4502 case FUNCTIONS_DOMAIN:
4503 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4504 continue;
61920122
PA
4505 break;
4506 case TYPES_DOMAIN:
4507 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4508 continue;
4509 break;
59c35742
AB
4510 case MODULES_DOMAIN:
4511 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4512 continue;
4513 break;
61920122
PA
4514 default:
4515 break;
8943b874 4516 }
61920122 4517 }
8943b874 4518
61920122 4519 /* Don't crash on bad data. */
b76e467d 4520 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4521 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4522 {
b98664d3 4523 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4524 " [in module %s]"),
4525 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4526 continue;
4527 }
4528
ff4c9fec 4529 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4530 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4531 expansion_notify);
61920122
PA
4532 }
4533}
4534
4b514bc8
JK
4535/* If FILE_MATCHER is non-NULL, set all the
4536 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4537 that match FILE_MATCHER. */
4538
61920122 4539static void
4b514bc8 4540dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4541 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4542 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4543{
4b514bc8 4544 if (file_matcher == NULL)
61920122
PA
4545 return;
4546
4b514bc8
JK
4547 objfile *const objfile = dwarf2_per_objfile->objfile;
4548
4549 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4550 htab_eq_pointer,
4551 NULL, xcalloc, xfree));
4552 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4553 htab_eq_pointer,
4554 NULL, xcalloc, xfree));
61920122 4555
4b514bc8
JK
4556 /* The rule is CUs specify all the files, including those used by
4557 any TU, so there's no need to scan TUs here. */
61920122 4558
b76e467d 4559 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4560 {
927aa2e7
JK
4561 QUIT;
4562
4563 per_cu->v.quick->mark = 0;
4564
4565 /* We only need to look at symtabs not already expanded. */
4566 if (per_cu->v.quick->compunit_symtab)
4567 continue;
4568
b76e467d 4569 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4570 if (file_data == NULL)
4571 continue;
4572
4573 if (htab_find (visited_not_found.get (), file_data) != NULL)
4574 continue;
4575 else if (htab_find (visited_found.get (), file_data) != NULL)
4576 {
4577 per_cu->v.quick->mark = 1;
4578 continue;
4579 }
4580
b76e467d 4581 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4582 {
4583 const char *this_real_name;
4584
4585 if (file_matcher (file_data->file_names[j], false))
4586 {
4587 per_cu->v.quick->mark = 1;
4588 break;
4589 }
4590
4591 /* Before we invoke realpath, which can get expensive when many
4592 files are involved, do a quick comparison of the basenames. */
4593 if (!basenames_may_differ
4594 && !file_matcher (lbasename (file_data->file_names[j]),
4595 true))
4596 continue;
4597
4598 this_real_name = dw2_get_real_path (objfile, file_data, j);
4599 if (file_matcher (this_real_name, false))
4600 {
4601 per_cu->v.quick->mark = 1;
4602 break;
4603 }
4604 }
4605
b76e467d
SM
4606 void **slot = htab_find_slot (per_cu->v.quick->mark
4607 ? visited_found.get ()
4608 : visited_not_found.get (),
4609 file_data, INSERT);
927aa2e7
JK
4610 *slot = file_data;
4611 }
4612}
4613
4614static void
4615dw2_expand_symtabs_matching
4616 (struct objfile *objfile,
4617 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4618 const lookup_name_info &lookup_name,
4619 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4620 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4621 enum search_domain kind)
4622{
ed2dc618
SM
4623 struct dwarf2_per_objfile *dwarf2_per_objfile
4624 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4625
4626 /* index_table is NULL if OBJF_READNOW. */
4627 if (!dwarf2_per_objfile->index_table)
4628 return;
4629
ed2dc618 4630 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4631
4632 mapped_index &index = *dwarf2_per_objfile->index_table;
4633
4634 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4635 symbol_matcher,
4636 kind, [&] (offset_type idx)
4637 {
ed2dc618 4638 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4639 expansion_notify, kind);
3b00ef10 4640 return true;
927aa2e7
JK
4641 });
4642}
4643
4644/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4645 symtab. */
4646
4647static struct compunit_symtab *
4648recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4649 CORE_ADDR pc)
4650{
4651 int i;
4652
4653 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4654 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4655 return cust;
4656
4657 if (cust->includes == NULL)
4658 return NULL;
4659
4660 for (i = 0; cust->includes[i]; ++i)
4661 {
4662 struct compunit_symtab *s = cust->includes[i];
4663
4664 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4665 if (s != NULL)
4666 return s;
4667 }
4668
4669 return NULL;
4670}
4671
4672static struct compunit_symtab *
4673dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4674 struct bound_minimal_symbol msymbol,
4675 CORE_ADDR pc,
4676 struct obj_section *section,
4677 int warn_if_readin)
4678{
4679 struct dwarf2_per_cu_data *data;
4680 struct compunit_symtab *result;
4681
d320c2b5 4682 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4683 return NULL;
4684
b3b3bada 4685 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4686 data = (struct dwarf2_per_cu_data *) addrmap_find
4687 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4688 if (!data)
4689 return NULL;
4690
4691 if (warn_if_readin && data->v.quick->compunit_symtab)
4692 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4693 paddress (get_objfile_arch (objfile), pc));
4694
4695 result
58f0c718
TT
4696 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4697 false),
927aa2e7
JK
4698 pc);
4699 gdb_assert (result != NULL);
4700 return result;
4701}
4702
4703static void
4704dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4705 void *data, int need_fullname)
4706{
ed2dc618
SM
4707 struct dwarf2_per_objfile *dwarf2_per_objfile
4708 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4709
4710 if (!dwarf2_per_objfile->filenames_cache)
4711 {
4712 dwarf2_per_objfile->filenames_cache.emplace ();
4713
4714 htab_up visited (htab_create_alloc (10,
4715 htab_hash_pointer, htab_eq_pointer,
4716 NULL, xcalloc, xfree));
4717
4718 /* The rule is CUs specify all the files, including those used
4719 by any TU, so there's no need to scan TUs here. We can
4720 ignore file names coming from already-expanded CUs. */
4721
b76e467d 4722 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4723 {
927aa2e7
JK
4724 if (per_cu->v.quick->compunit_symtab)
4725 {
4726 void **slot = htab_find_slot (visited.get (),
4727 per_cu->v.quick->file_names,
4728 INSERT);
4729
4730 *slot = per_cu->v.quick->file_names;
4731 }
4732 }
4733
b76e467d 4734 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4735 {
927aa2e7
JK
4736 /* We only need to look at symtabs not already expanded. */
4737 if (per_cu->v.quick->compunit_symtab)
4738 continue;
4739
b76e467d 4740 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4741 if (file_data == NULL)
4742 continue;
4743
b76e467d 4744 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4745 if (*slot)
4746 {
4747 /* Already visited. */
4748 continue;
4749 }
4750 *slot = file_data;
4751
4752 for (int j = 0; j < file_data->num_file_names; ++j)
4753 {
4754 const char *filename = file_data->file_names[j];
4755 dwarf2_per_objfile->filenames_cache->seen (filename);
4756 }
4757 }
4758 }
4759
4760 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4761 {
4762 gdb::unique_xmalloc_ptr<char> this_real_name;
4763
4764 if (need_fullname)
4765 this_real_name = gdb_realpath (filename);
4766 (*fun) (filename, this_real_name.get (), data);
4767 });
4768}
4769
4770static int
4771dw2_has_symbols (struct objfile *objfile)
4772{
4773 return 1;
4774}
4775
4776const struct quick_symbol_functions dwarf2_gdb_index_functions =
4777{
4778 dw2_has_symbols,
4779 dw2_find_last_source_symtab,
4780 dw2_forget_cached_source_info,
4781 dw2_map_symtabs_matching_filename,
4782 dw2_lookup_symbol,
4783 dw2_print_stats,
4784 dw2_dump,
927aa2e7
JK
4785 dw2_expand_symtabs_for_function,
4786 dw2_expand_all_symtabs,
4787 dw2_expand_symtabs_with_fullname,
4788 dw2_map_matching_symbols,
4789 dw2_expand_symtabs_matching,
4790 dw2_find_pc_sect_compunit_symtab,
4791 NULL,
4792 dw2_map_symbol_filenames
4793};
4794
4795/* DWARF-5 debug_names reader. */
4796
4797/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4798static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4799
4800/* A helper function that reads the .debug_names section in SECTION
4801 and fills in MAP. FILENAME is the name of the file containing the
4802 section; it is used for error reporting.
4803
4804 Returns true if all went well, false otherwise. */
4805
4806static bool
4807read_debug_names_from_section (struct objfile *objfile,
4808 const char *filename,
4809 struct dwarf2_section_info *section,
4810 mapped_debug_names &map)
4811{
96b79293 4812 if (section->empty ())
927aa2e7
JK
4813 return false;
4814
4815 /* Older elfutils strip versions could keep the section in the main
4816 executable while splitting it for the separate debug info file. */
96b79293 4817 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4818 return false;
4819
96b79293 4820 section->read (objfile);
927aa2e7
JK
4821
4822 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4823
4824 const gdb_byte *addr = section->buffer;
4825
96b79293 4826 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4827
4828 unsigned int bytes_read;
4829 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4830 addr += bytes_read;
4831
4832 map.dwarf5_is_dwarf64 = bytes_read != 4;
4833 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4834 if (bytes_read + length != section->size)
4835 {
4836 /* There may be multiple per-CU indices. */
4837 warning (_("Section .debug_names in %s length %s does not match "
4838 "section length %s, ignoring .debug_names."),
4839 filename, plongest (bytes_read + length),
4840 pulongest (section->size));
4841 return false;
4842 }
4843
4844 /* The version number. */
4845 uint16_t version = read_2_bytes (abfd, addr);
4846 addr += 2;
4847 if (version != 5)
4848 {
4849 warning (_("Section .debug_names in %s has unsupported version %d, "
4850 "ignoring .debug_names."),
4851 filename, version);
4852 return false;
4853 }
4854
4855 /* Padding. */
4856 uint16_t padding = read_2_bytes (abfd, addr);
4857 addr += 2;
4858 if (padding != 0)
4859 {
4860 warning (_("Section .debug_names in %s has unsupported padding %d, "
4861 "ignoring .debug_names."),
4862 filename, padding);
4863 return false;
4864 }
4865
4866 /* comp_unit_count - The number of CUs in the CU list. */
4867 map.cu_count = read_4_bytes (abfd, addr);
4868 addr += 4;
4869
4870 /* local_type_unit_count - The number of TUs in the local TU
4871 list. */
4872 map.tu_count = read_4_bytes (abfd, addr);
4873 addr += 4;
4874
4875 /* foreign_type_unit_count - The number of TUs in the foreign TU
4876 list. */
4877 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4878 addr += 4;
4879 if (foreign_tu_count != 0)
4880 {
4881 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4882 "ignoring .debug_names."),
4883 filename, static_cast<unsigned long> (foreign_tu_count));
4884 return false;
4885 }
4886
4887 /* bucket_count - The number of hash buckets in the hash lookup
4888 table. */
4889 map.bucket_count = read_4_bytes (abfd, addr);
4890 addr += 4;
4891
4892 /* name_count - The number of unique names in the index. */
4893 map.name_count = read_4_bytes (abfd, addr);
4894 addr += 4;
4895
4896 /* abbrev_table_size - The size in bytes of the abbreviations
4897 table. */
4898 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4899 addr += 4;
4900
4901 /* augmentation_string_size - The size in bytes of the augmentation
4902 string. This value is rounded up to a multiple of 4. */
4903 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4904 addr += 4;
4905 map.augmentation_is_gdb = ((augmentation_string_size
4906 == sizeof (dwarf5_augmentation))
4907 && memcmp (addr, dwarf5_augmentation,
4908 sizeof (dwarf5_augmentation)) == 0);
4909 augmentation_string_size += (-augmentation_string_size) & 3;
4910 addr += augmentation_string_size;
4911
4912 /* List of CUs */
4913 map.cu_table_reordered = addr;
4914 addr += map.cu_count * map.offset_size;
4915
4916 /* List of Local TUs */
4917 map.tu_table_reordered = addr;
4918 addr += map.tu_count * map.offset_size;
4919
4920 /* Hash Lookup Table */
4921 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4922 addr += map.bucket_count * 4;
4923 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4924 addr += map.name_count * 4;
4925
4926 /* Name Table */
4927 map.name_table_string_offs_reordered = addr;
4928 addr += map.name_count * map.offset_size;
4929 map.name_table_entry_offs_reordered = addr;
4930 addr += map.name_count * map.offset_size;
4931
4932 const gdb_byte *abbrev_table_start = addr;
4933 for (;;)
4934 {
927aa2e7
JK
4935 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4936 addr += bytes_read;
4937 if (index_num == 0)
4938 break;
4939
4940 const auto insertpair
4941 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4942 if (!insertpair.second)
4943 {
4944 warning (_("Section .debug_names in %s has duplicate index %s, "
4945 "ignoring .debug_names."),
4946 filename, pulongest (index_num));
4947 return false;
4948 }
4949 mapped_debug_names::index_val &indexval = insertpair.first->second;
4950 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4951 addr += bytes_read;
4952
4953 for (;;)
4954 {
4955 mapped_debug_names::index_val::attr attr;
4956 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4957 addr += bytes_read;
4958 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4959 addr += bytes_read;
4960 if (attr.form == DW_FORM_implicit_const)
4961 {
4962 attr.implicit_const = read_signed_leb128 (abfd, addr,
4963 &bytes_read);
4964 addr += bytes_read;
4965 }
4966 if (attr.dw_idx == 0 && attr.form == 0)
4967 break;
4968 indexval.attr_vec.push_back (std::move (attr));
4969 }
4970 }
4971 if (addr != abbrev_table_start + abbrev_table_size)
4972 {
4973 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4974 "of size %s vs. written as %u, ignoring .debug_names."),
4975 filename, plongest (addr - abbrev_table_start),
4976 abbrev_table_size);
927aa2e7
JK
4977 return false;
4978 }
4979 map.entry_pool = addr;
4980
4981 return true;
4982}
4983
4984/* A helper for create_cus_from_debug_names that handles the MAP's CU
4985 list. */
4986
4987static void
ed2dc618 4988create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4989 const mapped_debug_names &map,
4990 dwarf2_section_info &section,
b76e467d 4991 bool is_dwz)
927aa2e7
JK
4992{
4993 sect_offset sect_off_prev;
4994 for (uint32_t i = 0; i <= map.cu_count; ++i)
4995 {
4996 sect_offset sect_off_next;
4997 if (i < map.cu_count)
4998 {
4999 sect_off_next
5000 = (sect_offset) (extract_unsigned_integer
5001 (map.cu_table_reordered + i * map.offset_size,
5002 map.offset_size,
5003 map.dwarf5_byte_order));
5004 }
5005 else
5006 sect_off_next = (sect_offset) section.size;
5007 if (i >= 1)
5008 {
5009 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5010 dwarf2_per_cu_data *per_cu
ed2dc618 5011 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5012 sect_off_prev, length);
b76e467d 5013 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5014 }
5015 sect_off_prev = sect_off_next;
5016 }
5017}
5018
5019/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5020 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5021
5022static void
ed2dc618 5023create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5024 const mapped_debug_names &map,
5025 const mapped_debug_names &dwz_map)
5026{
b76e467d
SM
5027 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5028 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5029
ed2dc618
SM
5030 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5031 dwarf2_per_objfile->info,
b76e467d 5032 false /* is_dwz */);
927aa2e7
JK
5033
5034 if (dwz_map.cu_count == 0)
5035 return;
5036
ed2dc618
SM
5037 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5038 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5039 true /* is_dwz */);
927aa2e7
JK
5040}
5041
5042/* Read .debug_names. If everything went ok, initialize the "quick"
5043 elements of all the CUs and return true. Otherwise, return false. */
5044
5045static bool
ed2dc618 5046dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5047{
22ca247e
TT
5048 std::unique_ptr<mapped_debug_names> map
5049 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5050 mapped_debug_names dwz_map (dwarf2_per_objfile);
5051 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5052
5053 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5054 &dwarf2_per_objfile->debug_names,
22ca247e 5055 *map))
927aa2e7
JK
5056 return false;
5057
5058 /* Don't use the index if it's empty. */
22ca247e 5059 if (map->name_count == 0)
927aa2e7
JK
5060 return false;
5061
5062 /* If there is a .dwz file, read it so we can get its CU list as
5063 well. */
ed2dc618 5064 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5065 if (dwz != NULL)
5066 {
5067 if (!read_debug_names_from_section (objfile,
00f93c44 5068 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5069 &dwz->debug_names, dwz_map))
5070 {
5071 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5072 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5073 return false;
5074 }
5075 }
5076
22ca247e 5077 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5078
22ca247e 5079 if (map->tu_count != 0)
927aa2e7
JK
5080 {
5081 /* We can only handle a single .debug_types when we have an
5082 index. */
fd5866f6 5083 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5084 return false;
5085
fd5866f6 5086 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5087
5088 create_signatured_type_table_from_debug_names
22ca247e 5089 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5090 }
5091
ed2dc618
SM
5092 create_addrmap_from_aranges (dwarf2_per_objfile,
5093 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5094
22ca247e 5095 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5096 dwarf2_per_objfile->using_index = 1;
5097 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5098 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5099
5100 return true;
5101}
5102
927aa2e7
JK
5103/* Type used to manage iterating over all CUs looking for a symbol for
5104 .debug_names. */
5105
5106class dw2_debug_names_iterator
5107{
5108public:
927aa2e7 5109 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5110 gdb::optional<block_enum> block_index,
5111 domain_enum domain,
927aa2e7 5112 const char *name)
2b79f376 5113 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5114 m_addr (find_vec_in_debug_names (map, name))
5115 {}
5116
5117 dw2_debug_names_iterator (const mapped_debug_names &map,
5118 search_domain search, uint32_t namei)
5119 : m_map (map),
5120 m_search (search),
5121 m_addr (find_vec_in_debug_names (map, namei))
5122 {}
5123
3b00ef10
TT
5124 dw2_debug_names_iterator (const mapped_debug_names &map,
5125 block_enum block_index, domain_enum domain,
5126 uint32_t namei)
5127 : m_map (map), m_block_index (block_index), m_domain (domain),
5128 m_addr (find_vec_in_debug_names (map, namei))
5129 {}
5130
927aa2e7
JK
5131 /* Return the next matching CU or NULL if there are no more. */
5132 dwarf2_per_cu_data *next ();
5133
5134private:
5135 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5136 const char *name);
5137 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5138 uint32_t namei);
5139
5140 /* The internalized form of .debug_names. */
5141 const mapped_debug_names &m_map;
5142
2b79f376
SM
5143 /* If set, only look for symbols that match that block. Valid values are
5144 GLOBAL_BLOCK and STATIC_BLOCK. */
5145 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5146
5147 /* The kind of symbol we're looking for. */
5148 const domain_enum m_domain = UNDEF_DOMAIN;
5149 const search_domain m_search = ALL_DOMAIN;
5150
5151 /* The list of CUs from the index entry of the symbol, or NULL if
5152 not found. */
5153 const gdb_byte *m_addr;
5154};
5155
5156const char *
5157mapped_debug_names::namei_to_name (uint32_t namei) const
5158{
5159 const ULONGEST namei_string_offs
5160 = extract_unsigned_integer ((name_table_string_offs_reordered
5161 + namei * offset_size),
5162 offset_size,
5163 dwarf5_byte_order);
5164 return read_indirect_string_at_offset
ed2dc618 5165 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5166}
5167
5168/* Find a slot in .debug_names for the object named NAME. If NAME is
5169 found, return pointer to its pool data. If NAME cannot be found,
5170 return NULL. */
5171
5172const gdb_byte *
5173dw2_debug_names_iterator::find_vec_in_debug_names
5174 (const mapped_debug_names &map, const char *name)
5175{
5176 int (*cmp) (const char *, const char *);
5177
54ee4252 5178 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5179 if (current_language->la_language == language_cplus
5180 || current_language->la_language == language_fortran
5181 || current_language->la_language == language_d)
5182 {
5183 /* NAME is already canonical. Drop any qualifiers as
5184 .debug_names does not contain any. */
5185
5186 if (strchr (name, '(') != NULL)
5187 {
54ee4252 5188 without_params = cp_remove_params (name);
927aa2e7 5189 if (without_params != NULL)
54ee4252 5190 name = without_params.get ();
927aa2e7
JK
5191 }
5192 }
5193
5194 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5195
5196 const uint32_t full_hash = dwarf5_djb_hash (name);
5197 uint32_t namei
5198 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5199 (map.bucket_table_reordered
5200 + (full_hash % map.bucket_count)), 4,
5201 map.dwarf5_byte_order);
5202 if (namei == 0)
5203 return NULL;
5204 --namei;
5205 if (namei >= map.name_count)
5206 {
b98664d3 5207 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5208 "[in module %s]"),
5209 namei, map.name_count,
ed2dc618 5210 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5211 return NULL;
5212 }
5213
5214 for (;;)
5215 {
5216 const uint32_t namei_full_hash
5217 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5218 (map.hash_table_reordered + namei), 4,
5219 map.dwarf5_byte_order);
5220 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5221 return NULL;
5222
5223 if (full_hash == namei_full_hash)
5224 {
5225 const char *const namei_string = map.namei_to_name (namei);
5226
5227#if 0 /* An expensive sanity check. */
5228 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5229 {
b98664d3 5230 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5231 "[in module %s]"),
5232 namei, objfile_name (dwarf2_per_objfile->objfile));
5233 return NULL;
5234 }
5235#endif
5236
5237 if (cmp (namei_string, name) == 0)
5238 {
5239 const ULONGEST namei_entry_offs
5240 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5241 + namei * map.offset_size),
5242 map.offset_size, map.dwarf5_byte_order);
5243 return map.entry_pool + namei_entry_offs;
5244 }
5245 }
5246
5247 ++namei;
5248 if (namei >= map.name_count)
5249 return NULL;
5250 }
5251}
5252
5253const gdb_byte *
5254dw2_debug_names_iterator::find_vec_in_debug_names
5255 (const mapped_debug_names &map, uint32_t namei)
5256{
5257 if (namei >= map.name_count)
5258 {
b98664d3 5259 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5260 "[in module %s]"),
5261 namei, map.name_count,
ed2dc618 5262 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5263 return NULL;
5264 }
5265
5266 const ULONGEST namei_entry_offs
5267 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5268 + namei * map.offset_size),
5269 map.offset_size, map.dwarf5_byte_order);
5270 return map.entry_pool + namei_entry_offs;
5271}
5272
5273/* See dw2_debug_names_iterator. */
5274
5275dwarf2_per_cu_data *
5276dw2_debug_names_iterator::next ()
5277{
5278 if (m_addr == NULL)
5279 return NULL;
5280
ed2dc618
SM
5281 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5282 struct objfile *objfile = dwarf2_per_objfile->objfile;
5283 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5284
5285 again:
5286
5287 unsigned int bytes_read;
5288 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5289 m_addr += bytes_read;
5290 if (abbrev == 0)
5291 return NULL;
5292
5293 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5294 if (indexval_it == m_map.abbrev_map.cend ())
5295 {
b98664d3 5296 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5297 "[in module %s]"),
ed2dc618 5298 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5299 return NULL;
5300 }
5301 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5302 enum class symbol_linkage {
5303 unknown,
5304 static_,
5305 extern_,
23c13d42 5306 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5307 dwarf2_per_cu_data *per_cu = NULL;
5308 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5309 {
5310 ULONGEST ull;
5311 switch (attr.form)
5312 {
5313 case DW_FORM_implicit_const:
5314 ull = attr.implicit_const;
5315 break;
5316 case DW_FORM_flag_present:
5317 ull = 1;
5318 break;
5319 case DW_FORM_udata:
5320 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5321 m_addr += bytes_read;
5322 break;
5323 default:
b98664d3 5324 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5325 dwarf_form_name (attr.form),
ed2dc618 5326 objfile_name (objfile));
927aa2e7
JK
5327 return NULL;
5328 }
5329 switch (attr.dw_idx)
5330 {
5331 case DW_IDX_compile_unit:
5332 /* Don't crash on bad data. */
b76e467d 5333 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5334 {
b98664d3 5335 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5336 " [in module %s]"),
5337 pulongest (ull),
5338 objfile_name (dwarf2_per_objfile->objfile));
5339 continue;
5340 }
ff4c9fec 5341 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5342 break;
8af5c486
JK
5343 case DW_IDX_type_unit:
5344 /* Don't crash on bad data. */
b2bdb8cf 5345 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5346 {
b98664d3 5347 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5348 " [in module %s]"),
5349 pulongest (ull),
5350 objfile_name (dwarf2_per_objfile->objfile));
5351 continue;
5352 }
ff4c9fec 5353 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5354 break;
927aa2e7
JK
5355 case DW_IDX_GNU_internal:
5356 if (!m_map.augmentation_is_gdb)
5357 break;
23c13d42 5358 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5359 break;
5360 case DW_IDX_GNU_external:
5361 if (!m_map.augmentation_is_gdb)
5362 break;
23c13d42 5363 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5364 break;
5365 }
5366 }
5367
5368 /* Skip if already read in. */
5369 if (per_cu->v.quick->compunit_symtab)
5370 goto again;
5371
5372 /* Check static vs global. */
23c13d42 5373 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5374 {
2b79f376 5375 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5376 const bool symbol_is_static =
5377 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5378 if (want_static != symbol_is_static)
2b79f376 5379 goto again;
927aa2e7
JK
5380 }
5381
5382 /* Match dw2_symtab_iter_next, symbol_kind
5383 and debug_names::psymbol_tag. */
5384 switch (m_domain)
5385 {
5386 case VAR_DOMAIN:
5387 switch (indexval.dwarf_tag)
5388 {
5389 case DW_TAG_variable:
5390 case DW_TAG_subprogram:
5391 /* Some types are also in VAR_DOMAIN. */
5392 case DW_TAG_typedef:
5393 case DW_TAG_structure_type:
5394 break;
5395 default:
5396 goto again;
5397 }
5398 break;
5399 case STRUCT_DOMAIN:
5400 switch (indexval.dwarf_tag)
5401 {
5402 case DW_TAG_typedef:
5403 case DW_TAG_structure_type:
5404 break;
5405 default:
5406 goto again;
5407 }
5408 break;
5409 case LABEL_DOMAIN:
5410 switch (indexval.dwarf_tag)
5411 {
5412 case 0:
5413 case DW_TAG_variable:
5414 break;
5415 default:
5416 goto again;
5417 }
5418 break;
59c35742
AB
5419 case MODULE_DOMAIN:
5420 switch (indexval.dwarf_tag)
5421 {
5422 case DW_TAG_module:
5423 break;
5424 default:
5425 goto again;
5426 }
5427 break;
927aa2e7
JK
5428 default:
5429 break;
5430 }
5431
5432 /* Match dw2_expand_symtabs_matching, symbol_kind and
5433 debug_names::psymbol_tag. */
5434 switch (m_search)
4b514bc8 5435 {
927aa2e7
JK
5436 case VARIABLES_DOMAIN:
5437 switch (indexval.dwarf_tag)
4b514bc8 5438 {
927aa2e7
JK
5439 case DW_TAG_variable:
5440 break;
5441 default:
5442 goto again;
4b514bc8 5443 }
927aa2e7
JK
5444 break;
5445 case FUNCTIONS_DOMAIN:
5446 switch (indexval.dwarf_tag)
4b514bc8 5447 {
927aa2e7
JK
5448 case DW_TAG_subprogram:
5449 break;
5450 default:
5451 goto again;
4b514bc8 5452 }
927aa2e7
JK
5453 break;
5454 case TYPES_DOMAIN:
5455 switch (indexval.dwarf_tag)
5456 {
5457 case DW_TAG_typedef:
5458 case DW_TAG_structure_type:
5459 break;
5460 default:
5461 goto again;
5462 }
5463 break;
59c35742
AB
5464 case MODULES_DOMAIN:
5465 switch (indexval.dwarf_tag)
5466 {
5467 case DW_TAG_module:
5468 break;
5469 default:
5470 goto again;
5471 }
927aa2e7
JK
5472 default:
5473 break;
4b514bc8 5474 }
927aa2e7
JK
5475
5476 return per_cu;
4b514bc8 5477}
61920122 5478
927aa2e7 5479static struct compunit_symtab *
c7f839cb 5480dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5481 const char *name, domain_enum domain)
4b514bc8 5482{
ed2dc618
SM
5483 struct dwarf2_per_objfile *dwarf2_per_objfile
5484 = get_dwarf2_per_objfile (objfile);
61920122 5485
927aa2e7
JK
5486 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5487 if (!mapp)
61920122 5488 {
927aa2e7
JK
5489 /* index is NULL if OBJF_READNOW. */
5490 return NULL;
5491 }
5492 const auto &map = *mapp;
9291a0cd 5493
2b79f376 5494 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5495
927aa2e7
JK
5496 struct compunit_symtab *stab_best = NULL;
5497 struct dwarf2_per_cu_data *per_cu;
5498 while ((per_cu = iter.next ()) != NULL)
5499 {
5500 struct symbol *sym, *with_opaque = NULL;
58f0c718 5501 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5502 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5503 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5504
927aa2e7
JK
5505 sym = block_find_symbol (block, name, domain,
5506 block_find_non_opaque_type_preferred,
5507 &with_opaque);
9703b513 5508
927aa2e7
JK
5509 /* Some caution must be observed with overloaded functions and
5510 methods, since the index will not contain any overload
5511 information (but NAME might contain it). */
a3ec0bb1 5512
927aa2e7 5513 if (sym != NULL
987012b8 5514 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5515 return stab;
5516 if (with_opaque != NULL
987012b8 5517 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5518 stab_best = stab;
9703b513 5519
927aa2e7 5520 /* Keep looking through other CUs. */
9703b513
TT
5521 }
5522
927aa2e7 5523 return stab_best;
9703b513
TT
5524}
5525
927aa2e7
JK
5526/* This dumps minimal information about .debug_names. It is called
5527 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5528 uses this to verify that .debug_names has been loaded. */
9291a0cd 5529
927aa2e7
JK
5530static void
5531dw2_debug_names_dump (struct objfile *objfile)
5532{
ed2dc618
SM
5533 struct dwarf2_per_objfile *dwarf2_per_objfile
5534 = get_dwarf2_per_objfile (objfile);
5535
927aa2e7
JK
5536 gdb_assert (dwarf2_per_objfile->using_index);
5537 printf_filtered (".debug_names:");
5538 if (dwarf2_per_objfile->debug_names_table)
5539 printf_filtered (" exists\n");
5540 else
5541 printf_filtered (" faked for \"readnow\"\n");
5542 printf_filtered ("\n");
9291a0cd
TT
5543}
5544
9291a0cd 5545static void
927aa2e7
JK
5546dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5547 const char *func_name)
9291a0cd 5548{
ed2dc618
SM
5549 struct dwarf2_per_objfile *dwarf2_per_objfile
5550 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5551
927aa2e7
JK
5552 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5553 if (dwarf2_per_objfile->debug_names_table)
24c79950 5554 {
927aa2e7 5555 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5556
2b79f376 5557 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5558
927aa2e7
JK
5559 struct dwarf2_per_cu_data *per_cu;
5560 while ((per_cu = iter.next ()) != NULL)
58f0c718 5561 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5562 }
5563}
24c79950 5564
3b00ef10
TT
5565static void
5566dw2_debug_names_map_matching_symbols
5567 (struct objfile *objfile,
5568 const lookup_name_info &name, domain_enum domain,
5569 int global,
5570 gdb::function_view<symbol_found_callback_ftype> callback,
5571 symbol_compare_ftype *ordered_compare)
5572{
5573 struct dwarf2_per_objfile *dwarf2_per_objfile
5574 = get_dwarf2_per_objfile (objfile);
5575
5576 /* debug_names_table is NULL if OBJF_READNOW. */
5577 if (!dwarf2_per_objfile->debug_names_table)
5578 return;
5579
5580 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5581 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5582
5583 const char *match_name = name.ada ().lookup_name ().c_str ();
5584 auto matcher = [&] (const char *symname)
5585 {
5586 if (ordered_compare == nullptr)
5587 return true;
5588 return ordered_compare (symname, match_name) == 0;
5589 };
5590
5591 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5592 [&] (offset_type namei)
5593 {
5594 /* The name was matched, now expand corresponding CUs that were
5595 marked. */
5596 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5597
5598 struct dwarf2_per_cu_data *per_cu;
5599 while ((per_cu = iter.next ()) != NULL)
5600 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5601 return true;
5602 });
5603
5604 /* It's a shame we couldn't do this inside the
5605 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5606 that have already been expanded. Instead, this loop matches what
5607 the psymtab code does. */
5608 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5609 {
5610 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5611 if (cust != nullptr)
5612 {
5613 const struct block *block
5614 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5615 if (!iterate_over_symbols_terminated (block, name,
5616 domain, callback))
5617 break;
5618 }
5619 }
5620}
5621
927aa2e7
JK
5622static void
5623dw2_debug_names_expand_symtabs_matching
5624 (struct objfile *objfile,
5625 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5626 const lookup_name_info &lookup_name,
5627 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5628 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5629 enum search_domain kind)
5630{
ed2dc618
SM
5631 struct dwarf2_per_objfile *dwarf2_per_objfile
5632 = get_dwarf2_per_objfile (objfile);
9291a0cd 5633
927aa2e7
JK
5634 /* debug_names_table is NULL if OBJF_READNOW. */
5635 if (!dwarf2_per_objfile->debug_names_table)
5636 return;
9291a0cd 5637
ed2dc618 5638 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5639
44ed8f3e 5640 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5641
44ed8f3e
PA
5642 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5643 symbol_matcher,
5644 kind, [&] (offset_type namei)
927aa2e7 5645 {
927aa2e7
JK
5646 /* The name was matched, now expand corresponding CUs that were
5647 marked. */
5648 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5649
927aa2e7
JK
5650 struct dwarf2_per_cu_data *per_cu;
5651 while ((per_cu = iter.next ()) != NULL)
5652 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5653 expansion_notify);
3b00ef10 5654 return true;
44ed8f3e 5655 });
9291a0cd
TT
5656}
5657
927aa2e7 5658const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5659{
5660 dw2_has_symbols,
5661 dw2_find_last_source_symtab,
5662 dw2_forget_cached_source_info,
f8eba3c6 5663 dw2_map_symtabs_matching_filename,
927aa2e7 5664 dw2_debug_names_lookup_symbol,
9291a0cd 5665 dw2_print_stats,
927aa2e7 5666 dw2_debug_names_dump,
927aa2e7 5667 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5668 dw2_expand_all_symtabs,
652a8996 5669 dw2_expand_symtabs_with_fullname,
3b00ef10 5670 dw2_debug_names_map_matching_symbols,
927aa2e7 5671 dw2_debug_names_expand_symtabs_matching,
43f3e411 5672 dw2_find_pc_sect_compunit_symtab,
71a3c369 5673 NULL,
9291a0cd
TT
5674 dw2_map_symbol_filenames
5675};
5676
4485a1c1
SM
5677/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5678 to either a dwarf2_per_objfile or dwz_file object. */
5679
5680template <typename T>
5681static gdb::array_view<const gdb_byte>
5682get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5683{
5684 dwarf2_section_info *section = &section_owner->gdb_index;
5685
96b79293 5686 if (section->empty ())
4485a1c1
SM
5687 return {};
5688
5689 /* Older elfutils strip versions could keep the section in the main
5690 executable while splitting it for the separate debug info file. */
96b79293 5691 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5692 return {};
5693
96b79293 5694 section->read (obj);
4485a1c1 5695
8bebfcda
PA
5696 /* dwarf2_section_info::size is a bfd_size_type, while
5697 gdb::array_view works with size_t. On 32-bit hosts, with
5698 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5699 is 32-bit. So we need an explicit narrowing conversion here.
5700 This is fine, because it's impossible to allocate or mmap an
5701 array/buffer larger than what size_t can represent. */
5702 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5703}
5704
87d6a7aa
SM
5705/* Lookup the index cache for the contents of the index associated to
5706 DWARF2_OBJ. */
5707
5708static gdb::array_view<const gdb_byte>
5709get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5710{
5711 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5712 if (build_id == nullptr)
5713 return {};
5714
5715 return global_index_cache.lookup_gdb_index (build_id,
5716 &dwarf2_obj->index_cache_res);
5717}
5718
5719/* Same as the above, but for DWZ. */
5720
5721static gdb::array_view<const gdb_byte>
5722get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5723{
5724 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5725 if (build_id == nullptr)
5726 return {};
5727
5728 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5729}
5730
3c0aa29a 5731/* See symfile.h. */
9291a0cd 5732
3c0aa29a
PA
5733bool
5734dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5735{
ed2dc618
SM
5736 struct dwarf2_per_objfile *dwarf2_per_objfile
5737 = get_dwarf2_per_objfile (objfile);
5738
9291a0cd
TT
5739 /* If we're about to read full symbols, don't bother with the
5740 indices. In this case we also don't care if some other debug
5741 format is making psymtabs, because they are all about to be
5742 expanded anyway. */
5743 if ((objfile->flags & OBJF_READNOW))
5744 {
9291a0cd 5745 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5746 create_all_comp_units (dwarf2_per_objfile);
5747 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5748 dwarf2_per_objfile->quick_file_names_table
5749 = create_quick_file_names_table
5750 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5751
b76e467d 5752 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5753 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5754 {
ff4c9fec 5755 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5756
e254ef6a
DE
5757 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5758 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5759 }
5760
5761 /* Return 1 so that gdb sees the "quick" functions. However,
5762 these functions will be no-ops because we will have expanded
5763 all symtabs. */
3c0aa29a
PA
5764 *index_kind = dw_index_kind::GDB_INDEX;
5765 return true;
9291a0cd
TT
5766 }
5767
ed2dc618 5768 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5769 {
5770 *index_kind = dw_index_kind::DEBUG_NAMES;
5771 return true;
5772 }
927aa2e7 5773
4485a1c1
SM
5774 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5775 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5776 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5777 {
5778 *index_kind = dw_index_kind::GDB_INDEX;
5779 return true;
5780 }
9291a0cd 5781
87d6a7aa
SM
5782 /* ... otherwise, try to find the index in the index cache. */
5783 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5784 get_gdb_index_contents_from_cache,
5785 get_gdb_index_contents_from_cache_dwz))
5786 {
5787 global_index_cache.hit ();
5788 *index_kind = dw_index_kind::GDB_INDEX;
5789 return true;
5790 }
5791
5792 global_index_cache.miss ();
3c0aa29a 5793 return false;
9291a0cd
TT
5794}
5795
5796\f
5797
dce234bc
PP
5798/* Build a partial symbol table. */
5799
5800void
f29dff0a 5801dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5802{
ed2dc618
SM
5803 struct dwarf2_per_objfile *dwarf2_per_objfile
5804 = get_dwarf2_per_objfile (objfile);
c9bf0622 5805
6eee24ce 5806 init_psymbol_list (objfile, 1024);
c906108c 5807
a70b8144 5808 try
c9bf0622
TT
5809 {
5810 /* This isn't really ideal: all the data we allocate on the
5811 objfile's obstack is still uselessly kept around. However,
5812 freeing it seems unsafe. */
906768f9 5813 psymtab_discarder psymtabs (objfile);
ed2dc618 5814 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5815 psymtabs.keep ();
87d6a7aa
SM
5816
5817 /* (maybe) store an index in the cache. */
5818 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5819 }
230d2906 5820 catch (const gdb_exception_error &except)
492d29ea
PA
5821 {
5822 exception_print (gdb_stderr, except);
5823 }
c906108c 5824}
c906108c 5825
3b80fe9b
DE
5826/* Find the base address of the compilation unit for range lists and
5827 location lists. It will normally be specified by DW_AT_low_pc.
5828 In DWARF-3 draft 4, the base address could be overridden by
5829 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5830 compilation units with discontinuous ranges. */
5831
5832static void
5833dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5834{
5835 struct attribute *attr;
5836
5837 cu->base_known = 0;
5838 cu->base_address = 0;
5839
5840 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5841 if (attr != nullptr)
3b80fe9b 5842 {
cd6c91b4 5843 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5844 cu->base_known = 1;
5845 }
5846 else
5847 {
5848 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5849 if (attr != nullptr)
3b80fe9b 5850 {
cd6c91b4 5851 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5852 cu->base_known = 1;
5853 }
5854 }
5855}
5856
36586728
TT
5857/* Helper function that returns the proper abbrev section for
5858 THIS_CU. */
5859
5860static struct dwarf2_section_info *
5861get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5862{
5863 struct dwarf2_section_info *abbrev;
ed2dc618 5864 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5865
5866 if (this_cu->is_dwz)
ed2dc618 5867 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5868 else
5869 abbrev = &dwarf2_per_objfile->abbrev;
5870
5871 return abbrev;
5872}
5873
f4dc4d17
DE
5874/* Fetch the abbreviation table offset from a comp or type unit header. */
5875
5876static sect_offset
ed2dc618
SM
5877read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5878 struct dwarf2_section_info *section,
9c541725 5879 sect_offset sect_off)
f4dc4d17 5880{
96b79293 5881 bfd *abfd = section->get_bfd_owner ();
d521ce57 5882 const gdb_byte *info_ptr;
ac298888 5883 unsigned int initial_length_size, offset_size;
43988095 5884 uint16_t version;
f4dc4d17 5885
96b79293 5886 section->read (dwarf2_per_objfile->objfile);
9c541725 5887 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5888 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5889 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5890 info_ptr += initial_length_size;
5891
5892 version = read_2_bytes (abfd, info_ptr);
5893 info_ptr += 2;
5894 if (version >= 5)
5895 {
5896 /* Skip unit type and address size. */
5897 info_ptr += 2;
5898 }
5899
24aa364d 5900 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5901}
5902
aaa75496
JB
5903/* Allocate a new partial symtab for file named NAME and mark this new
5904 partial symtab as being an include of PST. */
5905
5906static void
891813be 5907dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5908 struct objfile *objfile)
5909{
891813be 5910 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 5911
fbd9ab74
JK
5912 if (!IS_ABSOLUTE_PATH (subpst->filename))
5913 {
5914 /* It shares objfile->objfile_obstack. */
5915 subpst->dirname = pst->dirname;
5916 }
5917
a9342b62 5918 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5919 subpst->dependencies[0] = pst;
5920 subpst->number_of_dependencies = 1;
5921
aaa75496
JB
5922 /* No private part is necessary for include psymtabs. This property
5923 can be used to differentiate between such include psymtabs and
10b3939b 5924 the regular ones. */
891813be 5925 subpst->per_cu_data = nullptr;
aaa75496
JB
5926}
5927
5928/* Read the Line Number Program data and extract the list of files
5929 included by the source file represented by PST. Build an include
d85a05f0 5930 partial symtab for each of these included files. */
aaa75496
JB
5931
5932static void
5933dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5934 struct die_info *die,
891813be 5935 dwarf2_psymtab *pst)
aaa75496 5936{
fff8551c 5937 line_header_up lh;
d85a05f0 5938 struct attribute *attr;
aaa75496 5939
d85a05f0 5940 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5941 if (attr != nullptr)
9c541725 5942 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5943 if (lh == NULL)
5944 return; /* No linetable, so no includes. */
5945
79748972
TT
5946 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5947 that we pass in the raw text_low here; that is ok because we're
5948 only decoding the line table to make include partial symtabs, and
5949 so the addresses aren't really used. */
4ae976d1 5950 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5951 pst->raw_text_low (), 1);
aaa75496
JB
5952}
5953
348e048f 5954static hashval_t
52dc124a 5955hash_signatured_type (const void *item)
348e048f 5956{
9a3c8263
SM
5957 const struct signatured_type *sig_type
5958 = (const struct signatured_type *) item;
9a619af0 5959
348e048f 5960 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5961 return sig_type->signature;
348e048f
DE
5962}
5963
5964static int
52dc124a 5965eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5966{
9a3c8263
SM
5967 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5968 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5969
348e048f
DE
5970 return lhs->signature == rhs->signature;
5971}
5972
1fd400ff
TT
5973/* Allocate a hash table for signatured types. */
5974
b0b6a987 5975static htab_up
673bfd45 5976allocate_signatured_type_table (struct objfile *objfile)
1fd400ff 5977{
b0b6a987
TT
5978 return htab_up (htab_create_alloc (41,
5979 hash_signatured_type,
5980 eq_signatured_type,
5981 NULL, xcalloc, xfree));
1fd400ff
TT
5982}
5983
d467dd73 5984/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5985
5986static int
d467dd73 5987add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5988{
9a3c8263 5989 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
5990 std::vector<signatured_type *> *all_type_units
5991 = (std::vector<signatured_type *> *) datum;
1fd400ff 5992
b2bdb8cf 5993 all_type_units->push_back (sigt);
1fd400ff
TT
5994
5995 return 1;
5996}
5997
78d4d2c5 5998/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5999 and fill them into TYPES_HTAB. It will process only type units,
6000 therefore DW_UT_type. */
c88ee1f0 6001
78d4d2c5 6002static void
ed2dc618
SM
6003create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6004 struct dwo_file *dwo_file,
b0b6a987 6005 dwarf2_section_info *section, htab_up &types_htab,
43988095 6006 rcuh_kind section_kind)
348e048f 6007{
3019eac3 6008 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6009 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6010 bfd *abfd;
6011 const gdb_byte *info_ptr, *end_ptr;
348e048f 6012
4bdcc0c1
DE
6013 abbrev_section = (dwo_file != NULL
6014 ? &dwo_file->sections.abbrev
6015 : &dwarf2_per_objfile->abbrev);
6016
b4f54984 6017 if (dwarf_read_debug)
43988095 6018 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6019 section->get_name (),
6020 abbrev_section->get_file_name ());
09406207 6021
96b79293 6022 section->read (objfile);
78d4d2c5 6023 info_ptr = section->buffer;
348e048f 6024
78d4d2c5
JK
6025 if (info_ptr == NULL)
6026 return;
348e048f 6027
78d4d2c5
JK
6028 /* We can't set abfd until now because the section may be empty or
6029 not present, in which case the bfd is unknown. */
96b79293 6030 abfd = section->get_bfd_owner ();
348e048f 6031
c0ab21c2
TT
6032 /* We don't use cutu_reader here because we don't need to read
6033 any dies: the signature is in the header. */
3019eac3 6034
78d4d2c5
JK
6035 end_ptr = info_ptr + section->size;
6036 while (info_ptr < end_ptr)
6037 {
78d4d2c5
JK
6038 struct signatured_type *sig_type;
6039 struct dwo_unit *dwo_tu;
6040 void **slot;
6041 const gdb_byte *ptr = info_ptr;
6042 struct comp_unit_head header;
6043 unsigned int length;
8b70b953 6044
9c541725 6045 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6046
a49dd8dd
JK
6047 /* Initialize it due to a false compiler warning. */
6048 header.signature = -1;
9c541725 6049 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6050
78d4d2c5
JK
6051 /* We need to read the type's signature in order to build the hash
6052 table, but we don't need anything else just yet. */
348e048f 6053
ed2dc618 6054 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6055 abbrev_section, ptr, section_kind);
348e048f 6056
4057dfde 6057 length = header.get_length ();
6caca83c 6058
78d4d2c5
JK
6059 /* Skip dummy type units. */
6060 if (ptr >= info_ptr + length
43988095
JK
6061 || peek_abbrev_code (abfd, ptr) == 0
6062 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6063 {
6064 info_ptr += length;
6065 continue;
6066 }
dee91e82 6067
78d4d2c5
JK
6068 if (types_htab == NULL)
6069 {
6070 if (dwo_file)
6071 types_htab = allocate_dwo_unit_table (objfile);
6072 else
6073 types_htab = allocate_signatured_type_table (objfile);
6074 }
8b70b953 6075
78d4d2c5
JK
6076 if (dwo_file)
6077 {
6078 sig_type = NULL;
6079 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6080 struct dwo_unit);
6081 dwo_tu->dwo_file = dwo_file;
43988095 6082 dwo_tu->signature = header.signature;
9c541725 6083 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6084 dwo_tu->section = section;
9c541725 6085 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6086 dwo_tu->length = length;
6087 }
6088 else
6089 {
6090 /* N.B.: type_offset is not usable if this type uses a DWO file.
6091 The real type_offset is in the DWO file. */
6092 dwo_tu = NULL;
6093 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6094 struct signatured_type);
43988095 6095 sig_type->signature = header.signature;
9c541725 6096 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6097 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6098 sig_type->per_cu.is_debug_types = 1;
6099 sig_type->per_cu.section = section;
9c541725 6100 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6101 sig_type->per_cu.length = length;
6102 }
6103
b0b6a987 6104 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6105 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6106 INSERT);
6107 gdb_assert (slot != NULL);
6108 if (*slot != NULL)
6109 {
9c541725 6110 sect_offset dup_sect_off;
0349ea22 6111
3019eac3
DE
6112 if (dwo_file)
6113 {
78d4d2c5
JK
6114 const struct dwo_unit *dup_tu
6115 = (const struct dwo_unit *) *slot;
6116
9c541725 6117 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6118 }
6119 else
6120 {
78d4d2c5
JK
6121 const struct signatured_type *dup_tu
6122 = (const struct signatured_type *) *slot;
6123
9c541725 6124 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6125 }
8b70b953 6126
b98664d3 6127 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6128 " the entry at offset %s, signature %s"),
6129 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6130 hex_string (header.signature));
78d4d2c5
JK
6131 }
6132 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6133
78d4d2c5 6134 if (dwarf_read_debug > 1)
9d8780f0
SM
6135 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6136 sect_offset_str (sect_off),
43988095 6137 hex_string (header.signature));
3019eac3 6138
78d4d2c5
JK
6139 info_ptr += length;
6140 }
6141}
3019eac3 6142
78d4d2c5
JK
6143/* Create the hash table of all entries in the .debug_types
6144 (or .debug_types.dwo) section(s).
6145 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6146 otherwise it is NULL.
b3c8eb43 6147
78d4d2c5 6148 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6149
78d4d2c5 6150 Note: This function processes DWO files only, not DWP files. */
348e048f 6151
78d4d2c5 6152static void
ed2dc618
SM
6153create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6154 struct dwo_file *dwo_file,
fd5866f6 6155 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6156 htab_up &types_htab)
78d4d2c5 6157{
fd5866f6
SM
6158 for (dwarf2_section_info &section : type_sections)
6159 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6160 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6161}
6162
6163/* Create the hash table of all entries in the .debug_types section,
6164 and initialize all_type_units.
6165 The result is zero if there is an error (e.g. missing .debug_types section),
6166 otherwise non-zero. */
6167
6168static int
ed2dc618 6169create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6170{
b0b6a987 6171 htab_up types_htab;
3019eac3 6172
ed2dc618
SM
6173 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6174 &dwarf2_per_objfile->info, types_htab,
43988095 6175 rcuh_kind::COMPILE);
ed2dc618
SM
6176 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6177 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6178 if (types_htab == NULL)
6179 {
6180 dwarf2_per_objfile->signatured_types = NULL;
6181 return 0;
6182 }
6183
b0b6a987 6184 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6185
b2bdb8cf 6186 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6187 dwarf2_per_objfile->all_type_units.reserve
6188 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6189
b0b6a987
TT
6190 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6191 add_signatured_type_cu_to_table,
b2bdb8cf 6192 &dwarf2_per_objfile->all_type_units);
1fd400ff 6193
348e048f
DE
6194 return 1;
6195}
6196
6aa5f3a6
DE
6197/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6198 If SLOT is non-NULL, it is the entry to use in the hash table.
6199 Otherwise we find one. */
6200
6201static struct signatured_type *
ed2dc618
SM
6202add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6203 void **slot)
6aa5f3a6
DE
6204{
6205 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6206
b2bdb8cf
SM
6207 if (dwarf2_per_objfile->all_type_units.size ()
6208 == dwarf2_per_objfile->all_type_units.capacity ())
6209 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6210
b2bdb8cf
SM
6211 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6212 struct signatured_type);
6213
6214 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6215 sig_type->signature = sig;
6216 sig_type->per_cu.is_debug_types = 1;
6217 if (dwarf2_per_objfile->using_index)
6218 {
6219 sig_type->per_cu.v.quick =
6220 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6221 struct dwarf2_per_cu_quick_data);
6222 }
6223
6224 if (slot == NULL)
6225 {
b0b6a987 6226 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6227 sig_type, INSERT);
6228 }
6229 gdb_assert (*slot == NULL);
6230 *slot = sig_type;
6231 /* The rest of sig_type must be filled in by the caller. */
6232 return sig_type;
6233}
6234
a2ce51a0
DE
6235/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6236 Fill in SIG_ENTRY with DWO_ENTRY. */
6237
6238static void
ed2dc618 6239fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6240 struct signatured_type *sig_entry,
6241 struct dwo_unit *dwo_entry)
6242{
7ee85ab1 6243 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6244 gdb_assert (! sig_entry->per_cu.queued);
6245 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6246 if (dwarf2_per_objfile->using_index)
6247 {
6248 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6249 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6250 }
6251 else
6252 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6253 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6254 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6255 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6256 gdb_assert (sig_entry->dwo_unit == NULL);
6257
6258 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6259 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6260 sig_entry->per_cu.length = dwo_entry->length;
6261 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6262 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6263 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6264 sig_entry->dwo_unit = dwo_entry;
6265}
6266
6267/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6268 If we haven't read the TU yet, create the signatured_type data structure
6269 for a TU to be read in directly from a DWO file, bypassing the stub.
6270 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6271 using .gdb_index, then when reading a CU we want to stay in the DWO file
6272 containing that CU. Otherwise we could end up reading several other DWO
6273 files (due to comdat folding) to process the transitive closure of all the
6274 mentioned TUs, and that can be slow. The current DWO file will have every
6275 type signature that it needs.
a2ce51a0
DE
6276 We only do this for .gdb_index because in the psymtab case we already have
6277 to read all the DWOs to build the type unit groups. */
6278
6279static struct signatured_type *
6280lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6281{
518817b3
SM
6282 struct dwarf2_per_objfile *dwarf2_per_objfile
6283 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6284 struct objfile *objfile = dwarf2_per_objfile->objfile;
6285 struct dwo_file *dwo_file;
6286 struct dwo_unit find_dwo_entry, *dwo_entry;
6287 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6288 void **slot;
a2ce51a0
DE
6289
6290 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6291
6aa5f3a6
DE
6292 /* If TU skeletons have been removed then we may not have read in any
6293 TUs yet. */
6294 if (dwarf2_per_objfile->signatured_types == NULL)
6295 {
6296 dwarf2_per_objfile->signatured_types
6297 = allocate_signatured_type_table (objfile);
6298 }
a2ce51a0
DE
6299
6300 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6301 Use the global signatured_types array to do our own comdat-folding
6302 of types. If this is the first time we're reading this TU, and
6303 the TU has an entry in .gdb_index, replace the recorded data from
6304 .gdb_index with this TU. */
a2ce51a0 6305
a2ce51a0 6306 find_sig_entry.signature = sig;
b0b6a987 6307 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6308 &find_sig_entry, INSERT);
9a3c8263 6309 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6310
6311 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6312 read. Don't reassign the global entry to point to this DWO if that's
6313 the case. Also note that if the TU is already being read, it may not
6314 have come from a DWO, the program may be a mix of Fission-compiled
6315 code and non-Fission-compiled code. */
6316
6317 /* Have we already tried to read this TU?
6318 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6319 needn't exist in the global table yet). */
6320 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6321 return sig_entry;
6322
6aa5f3a6
DE
6323 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6324 dwo_unit of the TU itself. */
6325 dwo_file = cu->dwo_unit->dwo_file;
6326
a2ce51a0
DE
6327 /* Ok, this is the first time we're reading this TU. */
6328 if (dwo_file->tus == NULL)
6329 return NULL;
6330 find_dwo_entry.signature = sig;
b0b6a987
TT
6331 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6332 &find_dwo_entry);
a2ce51a0
DE
6333 if (dwo_entry == NULL)
6334 return NULL;
6335
6aa5f3a6
DE
6336 /* If the global table doesn't have an entry for this TU, add one. */
6337 if (sig_entry == NULL)
ed2dc618 6338 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6339
ed2dc618 6340 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6341 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6342 return sig_entry;
6343}
6344
a2ce51a0
DE
6345/* Subroutine of lookup_signatured_type.
6346 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6347 then try the DWP file. If the TU stub (skeleton) has been removed then
6348 it won't be in .gdb_index. */
a2ce51a0
DE
6349
6350static struct signatured_type *
6351lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6352{
518817b3
SM
6353 struct dwarf2_per_objfile *dwarf2_per_objfile
6354 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6355 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6356 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6357 struct dwo_unit *dwo_entry;
6358 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6359 void **slot;
a2ce51a0
DE
6360
6361 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6362 gdb_assert (dwp_file != NULL);
6363
6aa5f3a6
DE
6364 /* If TU skeletons have been removed then we may not have read in any
6365 TUs yet. */
6366 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6367 {
6aa5f3a6
DE
6368 dwarf2_per_objfile->signatured_types
6369 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6370 }
6371
6aa5f3a6 6372 find_sig_entry.signature = sig;
b0b6a987 6373 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6374 &find_sig_entry, INSERT);
9a3c8263 6375 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6376
6377 /* Have we already tried to read this TU?
6378 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6379 needn't exist in the global table yet). */
6380 if (sig_entry != NULL)
6381 return sig_entry;
6382
a2ce51a0
DE
6383 if (dwp_file->tus == NULL)
6384 return NULL;
ed2dc618 6385 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6386 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6387 if (dwo_entry == NULL)
6388 return NULL;
6389
ed2dc618
SM
6390 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6391 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6392
a2ce51a0
DE
6393 return sig_entry;
6394}
6395
380bca97 6396/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6397 Returns NULL if signature SIG is not present in the table.
6398 It is up to the caller to complain about this. */
348e048f
DE
6399
6400static struct signatured_type *
a2ce51a0 6401lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6402{
518817b3
SM
6403 struct dwarf2_per_objfile *dwarf2_per_objfile
6404 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6405
a2ce51a0
DE
6406 if (cu->dwo_unit
6407 && dwarf2_per_objfile->using_index)
6408 {
6409 /* We're in a DWO/DWP file, and we're using .gdb_index.
6410 These cases require special processing. */
ed2dc618 6411 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6412 return lookup_dwo_signatured_type (cu, sig);
6413 else
6414 return lookup_dwp_signatured_type (cu, sig);
6415 }
6416 else
6417 {
6418 struct signatured_type find_entry, *entry;
348e048f 6419
a2ce51a0
DE
6420 if (dwarf2_per_objfile->signatured_types == NULL)
6421 return NULL;
6422 find_entry.signature = sig;
9a3c8263 6423 entry = ((struct signatured_type *)
b0b6a987
TT
6424 htab_find (dwarf2_per_objfile->signatured_types.get (),
6425 &find_entry));
a2ce51a0
DE
6426 return entry;
6427 }
348e048f 6428}
18a8505e
AT
6429
6430/* Return the address base of the compile unit, which, if exists, is stored
6431 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6432static gdb::optional<ULONGEST>
6433lookup_addr_base (struct die_info *comp_unit_die)
6434{
6435 struct attribute *attr;
6436 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6437 if (attr == nullptr)
6438 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6439 if (attr == nullptr)
6440 return gdb::optional<ULONGEST> ();
6441 return DW_UNSND (attr);
6442}
6443
6444/* Return range lists base of the compile unit, which, if exists, is stored
6445 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6446static ULONGEST
6447lookup_ranges_base (struct die_info *comp_unit_die)
6448{
6449 struct attribute *attr;
6450 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6451 if (attr == nullptr)
6452 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6453 if (attr == nullptr)
6454 return 0;
6455 return DW_UNSND (attr);
6456}
6457
42e7ad6c 6458/* Low level DIE reading support. */
348e048f 6459
d85a05f0
DJ
6460/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6461
6462static void
6463init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6464 struct dwarf2_cu *cu,
3019eac3 6465 struct dwarf2_section_info *section,
685af9cd
TT
6466 struct dwo_file *dwo_file,
6467 struct abbrev_table *abbrev_table)
d85a05f0 6468{
fceca515 6469 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6470 reader->abfd = section->get_bfd_owner ();
d85a05f0 6471 reader->cu = cu;
3019eac3 6472 reader->dwo_file = dwo_file;
dee91e82
DE
6473 reader->die_section = section;
6474 reader->buffer = section->buffer;
f664829e 6475 reader->buffer_end = section->buffer + section->size;
685af9cd 6476 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6477}
6478
c0ab21c2 6479/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6480 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6481 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6482 already.
6483
6484 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6485 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6486 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6487 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6488 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6489 STUB_COMP_DIR may be non-NULL.
3e225074 6490 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6491 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6492 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6493 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6494 kept around for at least as long as *RESULT_READER.
6495
b0c7bfa9
DE
6496 The result is non-zero if a valid (non-dummy) DIE was found. */
6497
6498static int
6499read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6500 struct dwo_unit *dwo_unit,
b0c7bfa9 6501 struct die_info *stub_comp_unit_die,
a2ce51a0 6502 const char *stub_comp_dir,
b0c7bfa9 6503 struct die_reader_specs *result_reader,
d521ce57 6504 const gdb_byte **result_info_ptr,
b0c7bfa9 6505 struct die_info **result_comp_unit_die,
685af9cd 6506 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6507{
ed2dc618 6508 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6509 struct objfile *objfile = dwarf2_per_objfile->objfile;
6510 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6511 bfd *abfd;
d521ce57 6512 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6513 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6514 int i,num_extra_attrs;
6515 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6516 struct die_info *comp_unit_die;
6517
b0aeadb3
DE
6518 /* At most one of these may be provided. */
6519 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6520
b0c7bfa9
DE
6521 /* These attributes aren't processed until later:
6522 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6523 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6524 referenced later. However, these attributes are found in the stub
6525 which we won't have later. In order to not impose this complication
6526 on the rest of the code, we read them here and copy them to the
6527 DWO CU/TU die. */
b0c7bfa9
DE
6528
6529 stmt_list = NULL;
6530 low_pc = NULL;
6531 high_pc = NULL;
6532 ranges = NULL;
6533 comp_dir = NULL;
6534
6535 if (stub_comp_unit_die != NULL)
6536 {
6537 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6538 DWO file. */
6539 if (! this_cu->is_debug_types)
6540 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6541 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6542 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6543 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6544 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6545
18a8505e 6546 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6547
18a8505e
AT
6548 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6549 here (if needed). We need the value before we can process
6550 DW_AT_ranges. */
6551 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6552 }
a2ce51a0
DE
6553 else if (stub_comp_dir != NULL)
6554 {
6555 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6556 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6557 comp_dir->name = DW_AT_comp_dir;
6558 comp_dir->form = DW_FORM_string;
6559 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6560 DW_STRING (comp_dir) = stub_comp_dir;
6561 }
b0c7bfa9
DE
6562
6563 /* Set up for reading the DWO CU/TU. */
6564 cu->dwo_unit = dwo_unit;
685af9cd 6565 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6566 section->read (objfile);
6567 abfd = section->get_bfd_owner ();
9c541725
PA
6568 begin_info_ptr = info_ptr = (section->buffer
6569 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6570 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6571
6572 if (this_cu->is_debug_types)
6573 {
b0c7bfa9
DE
6574 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6575
ed2dc618
SM
6576 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6577 &cu->header, section,
b0c7bfa9 6578 dwo_abbrev_section,
43988095 6579 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6580 /* This is not an assert because it can be caused by bad debug info. */
43988095 6581 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6582 {
6583 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6584 " TU at offset %s [in module %s]"),
a2ce51a0 6585 hex_string (sig_type->signature),
43988095 6586 hex_string (cu->header.signature),
9d8780f0 6587 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6588 bfd_get_filename (abfd));
6589 }
9c541725 6590 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6591 /* For DWOs coming from DWP files, we don't know the CU length
6592 nor the type's offset in the TU until now. */
4057dfde 6593 dwo_unit->length = cu->header.get_length ();
9c541725 6594 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6595
6596 /* Establish the type offset that can be used to lookup the type.
6597 For DWO files, we don't know it until now. */
9c541725
PA
6598 sig_type->type_offset_in_section
6599 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6600 }
6601 else
6602 {
ed2dc618
SM
6603 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6604 &cu->header, section,
b0c7bfa9 6605 dwo_abbrev_section,
43988095 6606 info_ptr, rcuh_kind::COMPILE);
9c541725 6607 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6608 /* For DWOs coming from DWP files, we don't know the CU length
6609 until now. */
4057dfde 6610 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6611 }
6612
685af9cd 6613 *result_dwo_abbrev_table
86de1d91
TT
6614 = abbrev_table::read (objfile, dwo_abbrev_section,
6615 cu->header.abbrev_sect_off);
685af9cd
TT
6616 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6617 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6618
6619 /* Read in the die, but leave space to copy over the attributes
6620 from the stub. This has the benefit of simplifying the rest of
6621 the code - all the work to maintain the illusion of a single
6622 DW_TAG_{compile,type}_unit DIE is done here. */
6623 num_extra_attrs = ((stmt_list != NULL)
6624 + (low_pc != NULL)
6625 + (high_pc != NULL)
6626 + (ranges != NULL)
6627 + (comp_dir != NULL));
6628 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6629 num_extra_attrs);
b0c7bfa9
DE
6630
6631 /* Copy over the attributes from the stub to the DIE we just read in. */
6632 comp_unit_die = *result_comp_unit_die;
6633 i = comp_unit_die->num_attrs;
6634 if (stmt_list != NULL)
6635 comp_unit_die->attrs[i++] = *stmt_list;
6636 if (low_pc != NULL)
6637 comp_unit_die->attrs[i++] = *low_pc;
6638 if (high_pc != NULL)
6639 comp_unit_die->attrs[i++] = *high_pc;
6640 if (ranges != NULL)
6641 comp_unit_die->attrs[i++] = *ranges;
6642 if (comp_dir != NULL)
6643 comp_unit_die->attrs[i++] = *comp_dir;
6644 comp_unit_die->num_attrs += num_extra_attrs;
6645
b4f54984 6646 if (dwarf_die_debug)
bf6af496
DE
6647 {
6648 fprintf_unfiltered (gdb_stdlog,
6649 "Read die from %s@0x%x of %s:\n",
96b79293 6650 section->get_name (),
bf6af496
DE
6651 (unsigned) (begin_info_ptr - section->buffer),
6652 bfd_get_filename (abfd));
b4f54984 6653 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6654 }
6655
b0c7bfa9
DE
6656 /* Skip dummy compilation units. */
6657 if (info_ptr >= begin_info_ptr + dwo_unit->length
6658 || peek_abbrev_code (abfd, info_ptr) == 0)
6659 return 0;
6660
6661 *result_info_ptr = info_ptr;
6662 return 1;
6663}
6664
a084a2a6
AT
6665/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6666 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6667 signature is part of the header. */
6668static gdb::optional<ULONGEST>
6669lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6670{
6671 if (cu->header.version >= 5)
6672 return cu->header.signature;
6673 struct attribute *attr;
6674 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6675 if (attr == nullptr)
6676 return gdb::optional<ULONGEST> ();
6677 return DW_UNSND (attr);
6678}
6679
c0ab21c2 6680/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6681 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6682 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6683
6684static struct dwo_unit *
6685lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6686 struct die_info *comp_unit_die,
6687 const char *dwo_name)
b0c7bfa9
DE
6688{
6689 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6690 struct dwo_unit *dwo_unit;
c0ab21c2 6691 const char *comp_dir;
b0c7bfa9 6692
a2ce51a0
DE
6693 gdb_assert (cu != NULL);
6694
b0c7bfa9 6695 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6696 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6697 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6698
6699 if (this_cu->is_debug_types)
6700 {
6701 struct signatured_type *sig_type;
6702
6703 /* Since this_cu is the first member of struct signatured_type,
6704 we can go from a pointer to one to a pointer to the other. */
6705 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6706 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6707 }
6708 else
6709 {
a084a2a6
AT
6710 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6711 if (!signature.has_value ())
b0c7bfa9
DE
6712 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6713 " [in module %s]"),
e3b94546 6714 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6715 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6716 *signature);
b0c7bfa9
DE
6717 }
6718
b0c7bfa9
DE
6719 return dwo_unit;
6720}
6721
c0ab21c2 6722/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6723 See it for a description of the parameters.
fcd3b13d 6724 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6725
c0ab21c2
TT
6726void
6727cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6728 int use_existing_cu)
a2ce51a0 6729{
a2ce51a0 6730 struct signatured_type *sig_type;
a2ce51a0 6731 struct die_reader_specs reader;
a2ce51a0
DE
6732
6733 /* Verify we can do the following downcast, and that we have the
6734 data we need. */
6735 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6736 sig_type = (struct signatured_type *) this_cu;
6737 gdb_assert (sig_type->dwo_unit != NULL);
6738
6aa5f3a6
DE
6739 if (use_existing_cu && this_cu->cu != NULL)
6740 {
6741 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6742 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6743 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6744 }
6745 else
6746 {
6747 /* If !use_existing_cu, this_cu->cu must be NULL. */
6748 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6749 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6750 }
6751
6752 /* A future optimization, if needed, would be to use an existing
6753 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6754 could share abbrev tables. */
a2ce51a0
DE
6755
6756 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6757 NULL /* stub_comp_unit_die */,
6758 sig_type->dwo_unit->dwo_file->comp_dir,
6759 &reader, &info_ptr,
3e225074 6760 &comp_unit_die,
c0ab21c2 6761 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6762 {
6763 /* Dummy die. */
c0ab21c2 6764 dummy_p = true;
a2ce51a0 6765 }
a2ce51a0
DE
6766}
6767
fd820528 6768/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6769 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6770
f4dc4d17
DE
6771 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6772 Otherwise the table specified in the comp unit header is read in and used.
6773 This is an optimization for when we already have the abbrev table.
6774
dee91e82 6775 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6776 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6777
c0ab21c2
TT
6778cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6779 struct abbrev_table *abbrev_table,
6751ebae 6780 int use_existing_cu,
c0ab21c2
TT
6781 bool skip_partial)
6782 : die_reader_specs {},
6751ebae 6783 m_this_cu (this_cu)
c906108c 6784{
ed2dc618 6785 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6786 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6787 struct dwarf2_section_info *section = this_cu->section;
96b79293 6788 bfd *abfd = section->get_bfd_owner ();
dee91e82 6789 struct dwarf2_cu *cu;
c0ab21c2 6790 const gdb_byte *begin_info_ptr;
dee91e82 6791 struct signatured_type *sig_type = NULL;
4bdcc0c1 6792 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6793 /* Non-zero if CU currently points to a DWO file and we need to
6794 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6795 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6796 int rereading_dwo_cu = 0;
c906108c 6797
b4f54984 6798 if (dwarf_die_debug)
9d8780f0 6799 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6800 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6801 sect_offset_str (this_cu->sect_off));
09406207 6802
a2ce51a0
DE
6803 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6804 file (instead of going through the stub), short-circuit all of this. */
6805 if (this_cu->reading_dwo_directly)
6806 {
6807 /* Narrow down the scope of possibilities to have to understand. */
6808 gdb_assert (this_cu->is_debug_types);
6809 gdb_assert (abbrev_table == NULL);
6751ebae 6810 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6811 return;
6812 }
6813
dee91e82 6814 /* This is cheap if the section is already read in. */
96b79293 6815 section->read (objfile);
dee91e82 6816
9c541725 6817 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6818
6819 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6820
6821 if (use_existing_cu && this_cu->cu != NULL)
6822 {
6823 cu = this_cu->cu;
42e7ad6c
DE
6824 /* If this CU is from a DWO file we need to start over, we need to
6825 refetch the attributes from the skeleton CU.
6826 This could be optimized by retrieving those attributes from when we
6827 were here the first time: the previous comp_unit_die was stored in
6828 comp_unit_obstack. But there's no data yet that we need this
6829 optimization. */
6830 if (cu->dwo_unit != NULL)
6831 rereading_dwo_cu = 1;
dee91e82
DE
6832 }
6833 else
6834 {
6835 /* If !use_existing_cu, this_cu->cu must be NULL. */
6836 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6837 m_new_cu.reset (new dwarf2_cu (this_cu));
6838 cu = m_new_cu.get ();
42e7ad6c 6839 }
dee91e82 6840
b0c7bfa9 6841 /* Get the header. */
9c541725 6842 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6843 {
6844 /* We already have the header, there's no need to read it in again. */
9c541725 6845 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6846 }
6847 else
6848 {
3019eac3 6849 if (this_cu->is_debug_types)
dee91e82 6850 {
ed2dc618
SM
6851 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6852 &cu->header, section,
4bdcc0c1 6853 abbrev_section, info_ptr,
43988095 6854 rcuh_kind::TYPE);
dee91e82 6855
42e7ad6c
DE
6856 /* Since per_cu is the first member of struct signatured_type,
6857 we can go from a pointer to one to a pointer to the other. */
6858 sig_type = (struct signatured_type *) this_cu;
43988095 6859 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6860 gdb_assert (sig_type->type_offset_in_tu
6861 == cu->header.type_cu_offset_in_tu);
6862 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6863
42e7ad6c
DE
6864 /* LENGTH has not been set yet for type units if we're
6865 using .gdb_index. */
4057dfde 6866 this_cu->length = cu->header.get_length ();
3019eac3
DE
6867
6868 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6869 sig_type->type_offset_in_section =
6870 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6871
6872 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6873 }
6874 else
6875 {
ed2dc618
SM
6876 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6877 &cu->header, section,
4bdcc0c1 6878 abbrev_section,
43988095
JK
6879 info_ptr,
6880 rcuh_kind::COMPILE);
dee91e82 6881
9c541725 6882 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6883 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6884 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6885 }
6886 }
10b3939b 6887
6caca83c 6888 /* Skip dummy compilation units. */
dee91e82 6889 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6890 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6891 {
6892 dummy_p = true;
6893 return;
6894 }
6caca83c 6895
433df2d4
DE
6896 /* If we don't have them yet, read the abbrevs for this compilation unit.
6897 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6898 done. */
f4dc4d17 6899 if (abbrev_table != NULL)
685af9cd
TT
6900 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6901 else
f4dc4d17 6902 {
c0ab21c2 6903 m_abbrev_table_holder
86de1d91
TT
6904 = abbrev_table::read (objfile, abbrev_section,
6905 cu->header.abbrev_sect_off);
c0ab21c2 6906 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6907 }
af703f96 6908
dee91e82 6909 /* Read the top level CU/TU die. */
c0ab21c2 6910 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6911 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6912
58f0c718 6913 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6914 {
6915 dummy_p = true;
6916 return;
6917 }
58f0c718 6918
b0c7bfa9 6919 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6920 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6921 table from the DWO file and pass the ownership over to us. It will be
6922 referenced from READER, so we must make sure to free it after we're done
6923 with READER.
6924
b0c7bfa9
DE
6925 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6926 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6927 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6928 if (dwo_name != nullptr)
3019eac3 6929 {
3019eac3 6930 struct dwo_unit *dwo_unit;
b0c7bfa9 6931 struct die_info *dwo_comp_unit_die;
3019eac3 6932
3e225074 6933 if (comp_unit_die->has_children)
6a506a2d 6934 {
b98664d3 6935 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6936 " has children (offset %s) [in module %s]"),
6937 sect_offset_str (this_cu->sect_off),
6938 bfd_get_filename (abfd));
6a506a2d 6939 }
c0ab21c2 6940 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6941 if (dwo_unit != NULL)
3019eac3 6942 {
6a506a2d 6943 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6944 comp_unit_die, NULL,
c0ab21c2 6945 this, &info_ptr,
3e225074 6946 &dwo_comp_unit_die,
c0ab21c2 6947 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6948 {
6949 /* Dummy die. */
c0ab21c2 6950 dummy_p = true;
6a506a2d
DE
6951 return;
6952 }
6953 comp_unit_die = dwo_comp_unit_die;
6954 }
6955 else
6956 {
6957 /* Yikes, we couldn't find the rest of the DIE, we only have
6958 the stub. A complaint has already been logged. There's
6959 not much more we can do except pass on the stub DIE to
6960 die_reader_func. We don't want to throw an error on bad
6961 debug info. */
3019eac3
DE
6962 }
6963 }
c0ab21c2 6964}
3019eac3 6965
6751ebae
TT
6966void
6967cutu_reader::keep ()
c0ab21c2 6968{
b0c7bfa9 6969 /* Done, clean up. */
6751ebae
TT
6970 gdb_assert (!dummy_p);
6971 if (m_new_cu != NULL)
348e048f 6972 {
c0ab21c2
TT
6973 struct dwarf2_per_objfile *dwarf2_per_objfile
6974 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6975 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6976 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6977 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6978 /* The chain owns it now. */
c0ab21c2 6979 m_new_cu.release ();
348e048f 6980 }
dee91e82
DE
6981}
6982
18a8505e
AT
6983/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6984 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6985 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6986
6987 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6988 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6989
6990 We fill in THIS_CU->length.
6991
dee91e82 6992 THIS_CU->cu is always freed when done.
3019eac3 6993 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6994 to care whether it refers to the "main" CU or the DWO CU.
6995
6996 When parent_cu is passed, it is used to provide a default value for
6997 str_offsets_base and addr_base from the parent. */
dee91e82 6998
c0ab21c2
TT
6999cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7000 struct dwarf2_cu *parent_cu,
7001 struct dwo_file *dwo_file)
7002 : die_reader_specs {},
7003 m_this_cu (this_cu)
dee91e82 7004{
ed2dc618 7005 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7006 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7007 struct dwarf2_section_info *section = this_cu->section;
96b79293 7008 bfd *abfd = section->get_bfd_owner ();
33e80786 7009 struct dwarf2_section_info *abbrev_section;
d521ce57 7010 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7011
b4f54984 7012 if (dwarf_die_debug)
9d8780f0 7013 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7014 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7015 sect_offset_str (this_cu->sect_off));
09406207 7016
dee91e82
DE
7017 gdb_assert (this_cu->cu == NULL);
7018
33e80786
DE
7019 abbrev_section = (dwo_file != NULL
7020 ? &dwo_file->sections.abbrev
7021 : get_abbrev_section_for_cu (this_cu));
7022
dee91e82 7023 /* This is cheap if the section is already read in. */
96b79293 7024 section->read (objfile);
dee91e82 7025
c0ab21c2 7026 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7027
9c541725 7028 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7029 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7030 &m_new_cu->header, section,
4bdcc0c1 7031 abbrev_section, info_ptr,
43988095
JK
7032 (this_cu->is_debug_types
7033 ? rcuh_kind::TYPE
7034 : rcuh_kind::COMPILE));
dee91e82 7035
18a8505e
AT
7036 if (parent_cu != nullptr)
7037 {
c0ab21c2
TT
7038 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7039 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7040 }
4057dfde 7041 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7042
7043 /* Skip dummy compilation units. */
7044 if (info_ptr >= begin_info_ptr + this_cu->length
7045 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7046 {
7047 dummy_p = true;
7048 return;
7049 }
72bf9492 7050
c0ab21c2 7051 m_abbrev_table_holder
86de1d91
TT
7052 = abbrev_table::read (objfile, abbrev_section,
7053 m_new_cu->header.abbrev_sect_off);
dee91e82 7054
c0ab21c2
TT
7055 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7056 m_abbrev_table_holder.get ());
3e225074 7057 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7058}
7059
0018ea6f
DE
7060\f
7061/* Type Unit Groups.
dee91e82 7062
0018ea6f
DE
7063 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7064 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7065 so that all types coming from the same compilation (.o file) are grouped
7066 together. A future step could be to put the types in the same symtab as
7067 the CU the types ultimately came from. */
ff013f42 7068
f4dc4d17
DE
7069static hashval_t
7070hash_type_unit_group (const void *item)
7071{
9a3c8263
SM
7072 const struct type_unit_group *tu_group
7073 = (const struct type_unit_group *) item;
f4dc4d17 7074
094b34ac 7075 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7076}
348e048f
DE
7077
7078static int
f4dc4d17 7079eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7080{
9a3c8263
SM
7081 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7082 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7083
094b34ac 7084 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7085}
348e048f 7086
f4dc4d17
DE
7087/* Allocate a hash table for type unit groups. */
7088
eaa5fa8b 7089static htab_up
ed2dc618 7090allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17 7091{
eaa5fa8b
TT
7092 return htab_up (htab_create_alloc (3,
7093 hash_type_unit_group,
7094 eq_type_unit_group,
7095 NULL, xcalloc, xfree));
f4dc4d17 7096}
dee91e82 7097
f4dc4d17
DE
7098/* Type units that don't have DW_AT_stmt_list are grouped into their own
7099 partial symtabs. We combine several TUs per psymtab to not let the size
7100 of any one psymtab grow too big. */
7101#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7102#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7103
094b34ac 7104/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7105 Create the type_unit_group object used to hold one or more TUs. */
7106
7107static struct type_unit_group *
094b34ac 7108create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7109{
518817b3
SM
7110 struct dwarf2_per_objfile *dwarf2_per_objfile
7111 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7112 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7113 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7114 struct type_unit_group *tu_group;
f4dc4d17
DE
7115
7116 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7117 struct type_unit_group);
094b34ac 7118 per_cu = &tu_group->per_cu;
518817b3 7119 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7120
094b34ac
DE
7121 if (dwarf2_per_objfile->using_index)
7122 {
7123 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7124 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7125 }
7126 else
7127 {
9c541725 7128 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7129 dwarf2_psymtab *pst;
528e1572 7130 std::string name;
094b34ac
DE
7131
7132 /* Give the symtab a useful name for debug purposes. */
7133 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7134 name = string_printf ("<type_units_%d>",
7135 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7136 else
528e1572 7137 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7138
528e1572 7139 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7140 pst->anonymous = true;
094b34ac 7141 }
f4dc4d17 7142
094b34ac 7143 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7144 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7145
7146 return tu_group;
7147}
7148
094b34ac
DE
7149/* Look up the type_unit_group for type unit CU, and create it if necessary.
7150 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7151
7152static struct type_unit_group *
ff39bb5e 7153get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7154{
518817b3
SM
7155 struct dwarf2_per_objfile *dwarf2_per_objfile
7156 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7157 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7158 struct type_unit_group *tu_group;
7159 void **slot;
7160 unsigned int line_offset;
7161 struct type_unit_group type_unit_group_for_lookup;
7162
7163 if (dwarf2_per_objfile->type_unit_groups == NULL)
7164 {
7165 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7166 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7167 }
7168
7169 /* Do we need to create a new group, or can we use an existing one? */
7170
7171 if (stmt_list)
7172 {
7173 line_offset = DW_UNSND (stmt_list);
7174 ++tu_stats->nr_symtab_sharers;
7175 }
7176 else
7177 {
7178 /* Ugh, no stmt_list. Rare, but we have to handle it.
7179 We can do various things here like create one group per TU or
7180 spread them over multiple groups to split up the expansion work.
7181 To avoid worst case scenarios (too many groups or too large groups)
7182 we, umm, group them in bunches. */
7183 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7184 | (tu_stats->nr_stmt_less_type_units
7185 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7186 ++tu_stats->nr_stmt_less_type_units;
7187 }
7188
094b34ac 7189 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7190 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7191 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7192 &type_unit_group_for_lookup, INSERT);
7193 if (*slot != NULL)
7194 {
9a3c8263 7195 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7196 gdb_assert (tu_group != NULL);
7197 }
7198 else
7199 {
9c541725 7200 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7201 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7202 *slot = tu_group;
7203 ++tu_stats->nr_symtabs;
7204 }
7205
7206 return tu_group;
7207}
0018ea6f
DE
7208\f
7209/* Partial symbol tables. */
7210
7211/* Create a psymtab named NAME and assign it to PER_CU.
7212
7213 The caller must fill in the following details:
7214 dirname, textlow, texthigh. */
7215
891813be 7216static dwarf2_psymtab *
0018ea6f
DE
7217create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7218{
e3b94546 7219 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7220 dwarf2_psymtab *pst;
0018ea6f 7221
891813be 7222 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7223
6d94535f 7224 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7225
7226 /* This is the glue that links PST into GDB's symbol API. */
891813be 7227 pst->per_cu_data = per_cu;
0018ea6f
DE
7228 per_cu->v.psymtab = pst;
7229
7230 return pst;
7231}
7232
c0ab21c2 7233/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7234
7235static void
7236process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7237 const gdb_byte *info_ptr,
0018ea6f 7238 struct die_info *comp_unit_die,
c0ab21c2 7239 enum language pretend_language)
0018ea6f
DE
7240{
7241 struct dwarf2_cu *cu = reader->cu;
518817b3 7242 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7243 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7244 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7245 CORE_ADDR baseaddr;
7246 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7247 dwarf2_psymtab *pst;
3a2b436a 7248 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7249 const char *filename;
0018ea6f 7250
0018ea6f
DE
7251 gdb_assert (! per_cu->is_debug_types);
7252
c0ab21c2 7253 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7254
0018ea6f 7255 /* Allocate a new partial symbol table structure. */
2e927613
TV
7256 gdb::unique_xmalloc_ptr<char> debug_filename;
7257 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7258 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7259 if (filename == NULL)
0018ea6f 7260 filename = "";
2e927613
TV
7261 else if (strcmp (filename, artificial) == 0)
7262 {
7263 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7264 sect_offset_str (per_cu->sect_off),
7265 (char *) NULL));
2e927613
TV
7266 filename = debug_filename.get ();
7267 }
0018ea6f
DE
7268
7269 pst = create_partial_symtab (per_cu, filename);
7270
7271 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7272 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7273
b3b3bada 7274 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7275
7276 dwarf2_find_base_address (comp_unit_die, cu);
7277
7278 /* Possibly set the default values of LOWPC and HIGHPC from
7279 `DW_AT_ranges'. */
3a2b436a
JK
7280 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7281 &best_highpc, cu, pst);
7282 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7283 {
7284 CORE_ADDR low
7285 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7286 - baseaddr);
7287 CORE_ADDR high
7288 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7289 - baseaddr - 1);
7290 /* Store the contiguous range if it is not empty; it can be
7291 empty for CUs with no code. */
d320c2b5
TT
7292 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7293 low, high, pst);
79748972 7294 }
0018ea6f
DE
7295
7296 /* Check if comp unit has_children.
7297 If so, read the rest of the partial symbols from this comp unit.
7298 If not, there's no more debug_info for this comp unit. */
3e225074 7299 if (comp_unit_die->has_children)
0018ea6f
DE
7300 {
7301 struct partial_die_info *first_die;
7302 CORE_ADDR lowpc, highpc;
7303
7304 lowpc = ((CORE_ADDR) -1);
7305 highpc = ((CORE_ADDR) 0);
7306
7307 first_die = load_partial_dies (reader, info_ptr, 1);
7308
7309 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7310 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7311
7312 /* If we didn't find a lowpc, set it to highpc to avoid
7313 complaints from `maint check'. */
7314 if (lowpc == ((CORE_ADDR) -1))
7315 lowpc = highpc;
7316
7317 /* If the compilation unit didn't have an explicit address range,
7318 then use the information extracted from its child dies. */
e385593e 7319 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7320 {
7321 best_lowpc = lowpc;
7322 best_highpc = highpc;
7323 }
7324 }
4ae976d1 7325 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7326 best_lowpc + baseaddr)
7327 - baseaddr);
4ae976d1 7328 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7329 best_highpc + baseaddr)
7330 - baseaddr);
0018ea6f 7331
8763cede 7332 end_psymtab_common (objfile, pst);
0018ea6f 7333
ae640021 7334 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7335 {
7336 int i;
ae640021 7337 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7338
7339 /* Fill in 'dependencies' here; we fill in 'users' in a
7340 post-pass. */
7341 pst->number_of_dependencies = len;
a9342b62
TT
7342 pst->dependencies
7343 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7344 for (i = 0; i < len; ++i)
7345 {
7346 pst->dependencies[i]
7347 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7348 }
0018ea6f 7349
ae640021 7350 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7351 }
7352
7353 /* Get the list of files included in the current compilation unit,
7354 and build a psymtab for each of them. */
7355 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7356
b4f54984 7357 if (dwarf_read_debug)
b926417a
TT
7358 fprintf_unfiltered (gdb_stdlog,
7359 "Psymtab for %s unit @%s: %s - %s"
7360 ", %d global, %d static syms\n",
7361 per_cu->is_debug_types ? "type" : "comp",
7362 sect_offset_str (per_cu->sect_off),
7363 paddress (gdbarch, pst->text_low (objfile)),
7364 paddress (gdbarch, pst->text_high (objfile)),
7365 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7366}
7367
7368/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7369 Process compilation unit THIS_CU for a psymtab. */
7370
7371static void
7372process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7373 bool want_partial_unit,
b93601f3 7374 enum language pretend_language)
0018ea6f
DE
7375{
7376 /* If this compilation unit was already read in, free the
7377 cached copy in order to read it in again. This is
7378 necessary because we skipped some symbols when we first
7379 read in the compilation unit (see load_partial_dies).
7380 This problem could be avoided, but the benefit is unclear. */
7381 if (this_cu->cu != NULL)
7382 free_one_cached_comp_unit (this_cu);
7383
6751ebae 7384 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2
TT
7385
7386 if (reader.dummy_p)
f1902523 7387 {
c0ab21c2 7388 /* Nothing. */
f1902523 7389 }
c0ab21c2 7390 else if (this_cu->is_debug_types)
3e225074
TT
7391 build_type_psymtabs_reader (&reader, reader.info_ptr,
7392 reader.comp_unit_die);
135f5437
TT
7393 else if (want_partial_unit
7394 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7395 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7396 reader.comp_unit_die,
c0ab21c2 7397 pretend_language);
0018ea6f
DE
7398
7399 /* Age out any secondary CUs. */
ed2dc618 7400 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7401}
f4dc4d17
DE
7402
7403/* Reader function for build_type_psymtabs. */
7404
7405static void
7406build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7407 const gdb_byte *info_ptr,
3e225074 7408 struct die_info *type_unit_die)
f4dc4d17 7409{
ed2dc618 7410 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7411 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7412 struct objfile *objfile = dwarf2_per_objfile->objfile;
7413 struct dwarf2_cu *cu = reader->cu;
7414 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7415 struct signatured_type *sig_type;
f4dc4d17
DE
7416 struct type_unit_group *tu_group;
7417 struct attribute *attr;
7418 struct partial_die_info *first_die;
7419 CORE_ADDR lowpc, highpc;
891813be 7420 dwarf2_psymtab *pst;
f4dc4d17 7421
0186c6a7
DE
7422 gdb_assert (per_cu->is_debug_types);
7423 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7424
3e225074 7425 if (! type_unit_die->has_children)
f4dc4d17
DE
7426 return;
7427
7428 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7429 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7430
df07e2c7 7431 if (tu_group->tus == nullptr)
a8b3b8e9 7432 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7433 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7434
7435 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7436 pst = create_partial_symtab (per_cu, "");
6d94535f 7437 pst->anonymous = true;
f4dc4d17
DE
7438
7439 first_die = load_partial_dies (reader, info_ptr, 1);
7440
7441 lowpc = (CORE_ADDR) -1;
7442 highpc = (CORE_ADDR) 0;
7443 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7444
8763cede 7445 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7446}
7447
73051182
DE
7448/* Struct used to sort TUs by their abbreviation table offset. */
7449
7450struct tu_abbrev_offset
7451{
b2bdb8cf
SM
7452 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7453 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7454 {}
7455
7456 signatured_type *sig_type;
73051182
DE
7457 sect_offset abbrev_offset;
7458};
7459
484cf504 7460/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7461
484cf504
TT
7462static bool
7463sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7464 const struct tu_abbrev_offset &b)
73051182 7465{
484cf504 7466 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7467}
7468
7469/* Efficiently read all the type units.
7470 This does the bulk of the work for build_type_psymtabs.
7471
7472 The efficiency is because we sort TUs by the abbrev table they use and
7473 only read each abbrev table once. In one program there are 200K TUs
7474 sharing 8K abbrev tables.
7475
7476 The main purpose of this function is to support building the
7477 dwarf2_per_objfile->type_unit_groups table.
7478 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7479 can collapse the search space by grouping them by stmt_list.
7480 The savings can be significant, in the same program from above the 200K TUs
7481 share 8K stmt_list tables.
7482
7483 FUNC is expected to call get_type_unit_group, which will create the
7484 struct type_unit_group if necessary and add it to
7485 dwarf2_per_objfile->type_unit_groups. */
7486
7487static void
ed2dc618 7488build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7489{
73051182 7490 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7491 abbrev_table_up abbrev_table;
73051182 7492 sect_offset abbrev_offset;
73051182
DE
7493
7494 /* It's up to the caller to not call us multiple times. */
7495 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7496
b2bdb8cf 7497 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7498 return;
7499
7500 /* TUs typically share abbrev tables, and there can be way more TUs than
7501 abbrev tables. Sort by abbrev table to reduce the number of times we
7502 read each abbrev table in.
7503 Alternatives are to punt or to maintain a cache of abbrev tables.
7504 This is simpler and efficient enough for now.
7505
7506 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7507 symtab to use). Typically TUs with the same abbrev offset have the same
7508 stmt_list value too so in practice this should work well.
7509
7510 The basic algorithm here is:
7511
7512 sort TUs by abbrev table
7513 for each TU with same abbrev table:
7514 read abbrev table if first user
7515 read TU top level DIE
7516 [IWBN if DWO skeletons had DW_AT_stmt_list]
7517 call FUNC */
7518
b4f54984 7519 if (dwarf_read_debug)
73051182
DE
7520 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7521
7522 /* Sort in a separate table to maintain the order of all_type_units
7523 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7524 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7525 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7526
7527 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7528 sorted_by_abbrev.emplace_back
7529 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7530 sig_type->per_cu.section,
7531 sig_type->per_cu.sect_off));
73051182 7532
484cf504
TT
7533 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7534 sort_tu_by_abbrev_offset);
73051182 7535
9c541725 7536 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7537
b2bdb8cf 7538 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7539 {
73051182
DE
7540 /* Switch to the next abbrev table if necessary. */
7541 if (abbrev_table == NULL
b2bdb8cf 7542 || tu.abbrev_offset != abbrev_offset)
73051182 7543 {
b2bdb8cf 7544 abbrev_offset = tu.abbrev_offset;
73051182 7545 abbrev_table =
86de1d91
TT
7546 abbrev_table::read (dwarf2_per_objfile->objfile,
7547 &dwarf2_per_objfile->abbrev,
7548 abbrev_offset);
73051182
DE
7549 ++tu_stats->nr_uniq_abbrev_tables;
7550 }
7551
c0ab21c2 7552 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7553 0, false);
c0ab21c2
TT
7554 if (!reader.dummy_p)
7555 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7556 reader.comp_unit_die);
73051182 7557 }
6aa5f3a6 7558}
73051182 7559
6aa5f3a6
DE
7560/* Print collected type unit statistics. */
7561
7562static void
ed2dc618 7563print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7564{
7565 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7566
7567 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7568 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7569 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7570 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7571 tu_stats->nr_uniq_abbrev_tables);
7572 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7573 tu_stats->nr_symtabs);
7574 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7575 tu_stats->nr_symtab_sharers);
7576 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7577 tu_stats->nr_stmt_less_type_units);
7578 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7579 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7580}
7581
f4dc4d17
DE
7582/* Traversal function for build_type_psymtabs. */
7583
7584static int
7585build_type_psymtab_dependencies (void **slot, void *info)
7586{
ed2dc618
SM
7587 struct dwarf2_per_objfile *dwarf2_per_objfile
7588 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7589 struct objfile *objfile = dwarf2_per_objfile->objfile;
7590 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7591 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7592 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7593 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7594 int i;
7595
7596 gdb_assert (len > 0);
0186c6a7 7597 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
7598
7599 pst->number_of_dependencies = len;
a9342b62 7600 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7601 for (i = 0; i < len; ++i)
f4dc4d17 7602 {
df07e2c7 7603 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7604 gdb_assert (iter->per_cu.is_debug_types);
7605 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7606 iter->type_unit_group = tu_group;
f4dc4d17
DE
7607 }
7608
df07e2c7
AB
7609 delete tu_group->tus;
7610 tu_group->tus = nullptr;
348e048f
DE
7611
7612 return 1;
7613}
7614
7615/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7616 Build partial symbol tables for the .debug_types comp-units. */
7617
7618static void
ed2dc618 7619build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7620{
ed2dc618 7621 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7622 return;
7623
ed2dc618 7624 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7625}
f4dc4d17 7626
6aa5f3a6
DE
7627/* Traversal function for process_skeletonless_type_unit.
7628 Read a TU in a DWO file and build partial symbols for it. */
7629
7630static int
7631process_skeletonless_type_unit (void **slot, void *info)
7632{
7633 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7634 struct dwarf2_per_objfile *dwarf2_per_objfile
7635 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7636 struct signatured_type find_entry, *entry;
7637
7638 /* If this TU doesn't exist in the global table, add it and read it in. */
7639
7640 if (dwarf2_per_objfile->signatured_types == NULL)
7641 {
7642 dwarf2_per_objfile->signatured_types
ed2dc618 7643 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
7644 }
7645
7646 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7647 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7648 &find_entry, INSERT);
6aa5f3a6
DE
7649 /* If we've already seen this type there's nothing to do. What's happening
7650 is we're doing our own version of comdat-folding here. */
7651 if (*slot != NULL)
7652 return 1;
7653
7654 /* This does the job that create_all_type_units would have done for
7655 this TU. */
ed2dc618
SM
7656 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7657 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7658 *slot = entry;
7659
7660 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7661 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7662 if (!reader.dummy_p)
7663 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7664 reader.comp_unit_die);
6aa5f3a6
DE
7665
7666 return 1;
7667}
7668
7669/* Traversal function for process_skeletonless_type_units. */
7670
7671static int
7672process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7673{
7674 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7675
7676 if (dwo_file->tus != NULL)
b0b6a987
TT
7677 htab_traverse_noresize (dwo_file->tus.get (),
7678 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7679
7680 return 1;
7681}
7682
7683/* Scan all TUs of DWO files, verifying we've processed them.
7684 This is needed in case a TU was emitted without its skeleton.
7685 Note: This can't be done until we know what all the DWO files are. */
7686
7687static void
ed2dc618 7688process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7689{
7690 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7691 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7692 && dwarf2_per_objfile->dwo_files != NULL)
7693 {
51ac9db5 7694 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7695 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7696 dwarf2_per_objfile);
6aa5f3a6 7697 }
348e048f
DE
7698}
7699
ed2dc618 7700/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7701
7702static void
ed2dc618 7703set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7704{
b76e467d 7705 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7706 {
891813be 7707 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7708
36586728
TT
7709 if (pst == NULL)
7710 continue;
7711
b76e467d 7712 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7713 {
7714 /* Set the 'user' field only if it is not already set. */
7715 if (pst->dependencies[j]->user == NULL)
7716 pst->dependencies[j]->user = pst;
7717 }
7718 }
7719}
7720
93311388
DE
7721/* Build the partial symbol table by doing a quick pass through the
7722 .debug_info and .debug_abbrev sections. */
72bf9492 7723
93311388 7724static void
ed2dc618 7725dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7726{
ed2dc618 7727 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7728
b4f54984 7729 if (dwarf_read_debug)
45cfd468
DE
7730 {
7731 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7732 objfile_name (objfile));
45cfd468
DE
7733 }
7734
98bfdba5
PA
7735 dwarf2_per_objfile->reading_partial_symbols = 1;
7736
96b79293 7737 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7738
93311388
DE
7739 /* Any cached compilation units will be linked by the per-objfile
7740 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7741 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7742
ed2dc618 7743 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7744
ed2dc618 7745 create_all_comp_units (dwarf2_per_objfile);
c906108c 7746
60606b2c
TT
7747 /* Create a temporary address map on a temporary obstack. We later
7748 copy this to the final obstack. */
8268c778 7749 auto_obstack temp_obstack;
791afaa2
TT
7750
7751 scoped_restore save_psymtabs_addrmap
d320c2b5 7752 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7753 addrmap_create_mutable (&temp_obstack));
72bf9492 7754
b76e467d 7755 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7756 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7757
6aa5f3a6 7758 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7759 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7760
7761 /* Now that all TUs have been processed we can fill in the dependencies. */
7762 if (dwarf2_per_objfile->type_unit_groups != NULL)
7763 {
eaa5fa8b 7764 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7765 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7766 }
7767
b4f54984 7768 if (dwarf_read_debug)
ed2dc618 7769 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7770
ed2dc618 7771 set_partial_user (dwarf2_per_objfile);
95554aad 7772
d320c2b5
TT
7773 objfile->partial_symtabs->psymtabs_addrmap
7774 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7775 objfile->partial_symtabs->obstack ());
791afaa2
TT
7776 /* At this point we want to keep the address map. */
7777 save_psymtabs_addrmap.release ();
ff013f42 7778
b4f54984 7779 if (dwarf_read_debug)
45cfd468 7780 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7781 objfile_name (objfile));
ae038cb0
DJ
7782}
7783
dee91e82
DE
7784/* Load the partial DIEs for a secondary CU into memory.
7785 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7786
dee91e82
DE
7787static void
7788load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7789{
6751ebae 7790 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7791
7792 if (!reader.dummy_p)
7793 {
7794 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7795 language_minimal);
7796
7797 /* Check if comp unit has_children.
7798 If so, read the rest of the partial symbols from this comp unit.
7799 If not, there's no more debug_info for this comp unit. */
3e225074 7800 if (reader.comp_unit_die->has_children)
c0ab21c2 7801 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7802
7803 reader.keep ();
c0ab21c2 7804 }
ae038cb0
DJ
7805}
7806
ae038cb0 7807static void
ed2dc618 7808read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7809 struct dwarf2_section_info *section,
f1902523 7810 struct dwarf2_section_info *abbrev_section,
b76e467d 7811 unsigned int is_dwz)
ae038cb0 7812{
d521ce57 7813 const gdb_byte *info_ptr;
ed2dc618 7814 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7815
b4f54984 7816 if (dwarf_read_debug)
bf6af496 7817 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7818 section->get_name (),
7819 section->get_file_name ());
bf6af496 7820
96b79293 7821 section->read (objfile);
ae038cb0 7822
36586728 7823 info_ptr = section->buffer;
6e70227d 7824
36586728 7825 while (info_ptr < section->buffer + section->size)
ae038cb0 7826 {
ae038cb0 7827 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7828
9c541725 7829 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7830
f1902523 7831 comp_unit_head cu_header;
ed2dc618
SM
7832 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7833 abbrev_section, info_ptr,
7834 rcuh_kind::COMPILE);
ae038cb0
DJ
7835
7836 /* Save the compilation unit for later lookup. */
f1902523
JK
7837 if (cu_header.unit_type != DW_UT_type)
7838 {
7839 this_cu = XOBNEW (&objfile->objfile_obstack,
7840 struct dwarf2_per_cu_data);
7841 memset (this_cu, 0, sizeof (*this_cu));
7842 }
7843 else
7844 {
7845 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7846 struct signatured_type);
7847 memset (sig_type, 0, sizeof (*sig_type));
7848 sig_type->signature = cu_header.signature;
7849 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7850 this_cu = &sig_type->per_cu;
7851 }
7852 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7853 this_cu->sect_off = sect_off;
f1902523 7854 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7855 this_cu->is_dwz = is_dwz;
e3b94546 7856 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7857 this_cu->section = section;
ae038cb0 7858
b76e467d 7859 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7860
7861 info_ptr = info_ptr + this_cu->length;
7862 }
36586728
TT
7863}
7864
7865/* Create a list of all compilation units in OBJFILE.
7866 This is only done for -readnow and building partial symtabs. */
7867
7868static void
ed2dc618 7869create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7870{
b76e467d 7871 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7872 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7873 &dwarf2_per_objfile->abbrev, 0);
36586728 7874
b76e467d 7875 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7876 if (dwz != NULL)
ed2dc618 7877 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7878 1);
c906108c
SS
7879}
7880
5734ee8b 7881/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7882 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7883 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7884 DW_AT_ranges). See the comments of add_partial_subprogram on how
7885 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7886
72bf9492
DJ
7887static void
7888scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7889 CORE_ADDR *highpc, int set_addrmap,
7890 struct dwarf2_cu *cu)
c906108c 7891{
72bf9492 7892 struct partial_die_info *pdi;
c906108c 7893
91c24f0a
DC
7894 /* Now, march along the PDI's, descending into ones which have
7895 interesting children but skipping the children of the other ones,
7896 until we reach the end of the compilation unit. */
c906108c 7897
72bf9492 7898 pdi = first_die;
91c24f0a 7899
72bf9492
DJ
7900 while (pdi != NULL)
7901 {
52356b79 7902 pdi->fixup (cu);
c906108c 7903
f55ee35c 7904 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7905 children, so we need to look at them. Ditto for anonymous
7906 enums. */
933c6fe4 7907
72bf9492 7908 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7909 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7910 || pdi->tag == DW_TAG_imported_unit
7911 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7912 {
72bf9492 7913 switch (pdi->tag)
c906108c
SS
7914 {
7915 case DW_TAG_subprogram:
b1dc1806 7916 case DW_TAG_inlined_subroutine:
cdc07690 7917 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7918 break;
72929c62 7919 case DW_TAG_constant:
c906108c
SS
7920 case DW_TAG_variable:
7921 case DW_TAG_typedef:
91c24f0a 7922 case DW_TAG_union_type:
72bf9492 7923 if (!pdi->is_declaration)
63d06c5c 7924 {
72bf9492 7925 add_partial_symbol (pdi, cu);
63d06c5c
DC
7926 }
7927 break;
c906108c 7928 case DW_TAG_class_type:
680b30c7 7929 case DW_TAG_interface_type:
c906108c 7930 case DW_TAG_structure_type:
72bf9492 7931 if (!pdi->is_declaration)
c906108c 7932 {
72bf9492 7933 add_partial_symbol (pdi, cu);
c906108c 7934 }
b7fee5a3
KS
7935 if ((cu->language == language_rust
7936 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7937 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7938 set_addrmap, cu);
c906108c 7939 break;
91c24f0a 7940 case DW_TAG_enumeration_type:
72bf9492
DJ
7941 if (!pdi->is_declaration)
7942 add_partial_enumeration (pdi, cu);
c906108c
SS
7943 break;
7944 case DW_TAG_base_type:
a02abb62 7945 case DW_TAG_subrange_type:
c906108c 7946 /* File scope base type definitions are added to the partial
c5aa993b 7947 symbol table. */
72bf9492 7948 add_partial_symbol (pdi, cu);
c906108c 7949 break;
d9fa45fe 7950 case DW_TAG_namespace:
cdc07690 7951 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7952 break;
5d7cb8df 7953 case DW_TAG_module:
59c35742
AB
7954 if (!pdi->is_declaration)
7955 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7956 break;
95554aad
TT
7957 case DW_TAG_imported_unit:
7958 {
7959 struct dwarf2_per_cu_data *per_cu;
7960
f4dc4d17
DE
7961 /* For now we don't handle imported units in type units. */
7962 if (cu->per_cu->is_debug_types)
7963 {
7964 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7965 " supported in type units [in module %s]"),
518817b3 7966 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7967 }
7968
e3b94546
SM
7969 per_cu = dwarf2_find_containing_comp_unit
7970 (pdi->d.sect_off, pdi->is_dwz,
518817b3 7971 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
7972
7973 /* Go read the partial unit, if needed. */
7974 if (per_cu->v.psymtab == NULL)
135f5437 7975 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 7976
ae640021 7977 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7978 }
7979 break;
74921315
KS
7980 case DW_TAG_imported_declaration:
7981 add_partial_symbol (pdi, cu);
7982 break;
c906108c
SS
7983 default:
7984 break;
7985 }
7986 }
7987
72bf9492
DJ
7988 /* If the die has a sibling, skip to the sibling. */
7989
7990 pdi = pdi->die_sibling;
7991 }
7992}
7993
7994/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7995
72bf9492 7996 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7997 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7998 Enumerators are an exception; they use the scope of their parent
7999 enumeration type, i.e. the name of the enumeration type is not
8000 prepended to the enumerator.
91c24f0a 8001
72bf9492
DJ
8002 There are two complexities. One is DW_AT_specification; in this
8003 case "parent" means the parent of the target of the specification,
8004 instead of the direct parent of the DIE. The other is compilers
8005 which do not emit DW_TAG_namespace; in this case we try to guess
8006 the fully qualified name of structure types from their members'
8007 linkage names. This must be done using the DIE's children rather
8008 than the children of any DW_AT_specification target. We only need
8009 to do this for structures at the top level, i.e. if the target of
8010 any DW_AT_specification (if any; otherwise the DIE itself) does not
8011 have a parent. */
8012
8013/* Compute the scope prefix associated with PDI's parent, in
8014 compilation unit CU. The result will be allocated on CU's
8015 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8016 field. NULL is returned if no prefix is necessary. */
15d034d0 8017static const char *
72bf9492
DJ
8018partial_die_parent_scope (struct partial_die_info *pdi,
8019 struct dwarf2_cu *cu)
8020{
15d034d0 8021 const char *grandparent_scope;
72bf9492 8022 struct partial_die_info *parent, *real_pdi;
91c24f0a 8023
72bf9492
DJ
8024 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8025 then this means the parent of the specification DIE. */
8026
8027 real_pdi = pdi;
72bf9492 8028 while (real_pdi->has_specification)
fb816e8b 8029 {
122cf0f2
AB
8030 auto res = find_partial_die (real_pdi->spec_offset,
8031 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8032 real_pdi = res.pdi;
8033 cu = res.cu;
8034 }
72bf9492
DJ
8035
8036 parent = real_pdi->die_parent;
8037 if (parent == NULL)
8038 return NULL;
8039
8040 if (parent->scope_set)
8041 return parent->scope;
8042
52356b79 8043 parent->fixup (cu);
72bf9492 8044
10b3939b 8045 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8046
acebe513
UW
8047 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8048 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8049 Work around this problem here. */
8050 if (cu->language == language_cplus
6e70227d 8051 && parent->tag == DW_TAG_namespace
acebe513
UW
8052 && strcmp (parent->name, "::") == 0
8053 && grandparent_scope == NULL)
8054 {
8055 parent->scope = NULL;
8056 parent->scope_set = 1;
8057 return NULL;
8058 }
8059
0a4b0913 8060 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8061 if (pdi->tag == DW_TAG_enumerator)
8062 /* Enumerators should not get the name of the enumeration as a prefix. */
8063 parent->scope = grandparent_scope;
8064 else if (parent->tag == DW_TAG_namespace
f55ee35c 8065 || parent->tag == DW_TAG_module
72bf9492
DJ
8066 || parent->tag == DW_TAG_structure_type
8067 || parent->tag == DW_TAG_class_type
680b30c7 8068 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8069 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8070 || parent->tag == DW_TAG_enumeration_type
8071 || (cu->language == language_fortran
8072 && parent->tag == DW_TAG_subprogram
8073 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8074 {
8075 if (grandparent_scope == NULL)
8076 parent->scope = parent->name;
8077 else
3e43a32a
MS
8078 parent->scope = typename_concat (&cu->comp_unit_obstack,
8079 grandparent_scope,
f55ee35c 8080 parent->name, 0, cu);
72bf9492 8081 }
72bf9492
DJ
8082 else
8083 {
8084 /* FIXME drow/2004-04-01: What should we be doing with
8085 function-local names? For partial symbols, we should probably be
8086 ignoring them. */
fa9c3fa0
TT
8087 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8088 dwarf_tag_name (parent->tag),
8089 sect_offset_str (pdi->sect_off));
72bf9492 8090 parent->scope = grandparent_scope;
c906108c
SS
8091 }
8092
72bf9492
DJ
8093 parent->scope_set = 1;
8094 return parent->scope;
8095}
8096
8097/* Return the fully scoped name associated with PDI, from compilation unit
8098 CU. The result will be allocated with malloc. */
4568ecf9 8099
43816ebc 8100static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8101partial_die_full_name (struct partial_die_info *pdi,
8102 struct dwarf2_cu *cu)
8103{
15d034d0 8104 const char *parent_scope;
72bf9492 8105
98bfdba5
PA
8106 /* If this is a template instantiation, we can not work out the
8107 template arguments from partial DIEs. So, unfortunately, we have
8108 to go through the full DIEs. At least any work we do building
8109 types here will be reused if full symbols are loaded later. */
8110 if (pdi->has_template_arguments)
8111 {
52356b79 8112 pdi->fixup (cu);
98bfdba5
PA
8113
8114 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8115 {
8116 struct die_info *die;
8117 struct attribute attr;
8118 struct dwarf2_cu *ref_cu = cu;
8119
b64f50a1 8120 /* DW_FORM_ref_addr is using section offset. */
b4069958 8121 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8122 attr.form = DW_FORM_ref_addr;
9c541725 8123 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8124 die = follow_die_ref (NULL, &attr, &ref_cu);
8125
43816ebc 8126 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8127 }
8128 }
8129
72bf9492
DJ
8130 parent_scope = partial_die_parent_scope (pdi, cu);
8131 if (parent_scope == NULL)
8132 return NULL;
8133 else
43816ebc
TT
8134 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8135 pdi->name, 0, cu));
c906108c
SS
8136}
8137
8138static void
72bf9492 8139add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8140{
518817b3
SM
8141 struct dwarf2_per_objfile *dwarf2_per_objfile
8142 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8143 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8144 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8145 CORE_ADDR addr = 0;
15d034d0 8146 const char *actual_name = NULL;
e142c38c
DJ
8147 CORE_ADDR baseaddr;
8148
b3b3bada 8149 baseaddr = objfile->text_section_offset ();
c906108c 8150
43816ebc
TT
8151 gdb::unique_xmalloc_ptr<char> built_actual_name
8152 = partial_die_full_name (pdi, cu);
15d034d0 8153 if (built_actual_name != NULL)
43816ebc 8154 actual_name = built_actual_name.get ();
63d06c5c 8155
72bf9492
DJ
8156 if (actual_name == NULL)
8157 actual_name = pdi->name;
8158
c906108c
SS
8159 switch (pdi->tag)
8160 {
b1dc1806 8161 case DW_TAG_inlined_subroutine:
c906108c 8162 case DW_TAG_subprogram:
79748972
TT
8163 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8164 - baseaddr);
0a4b0913
AB
8165 if (pdi->is_external
8166 || cu->language == language_ada
8167 || (cu->language == language_fortran
8168 && pdi->die_parent != NULL
8169 && pdi->die_parent->tag == DW_TAG_subprogram))
8170 {
8171 /* Normally, only "external" DIEs are part of the global scope.
8172 But in Ada and Fortran, we want to be able to access nested
8173 procedures globally. So all Ada and Fortran subprograms are
8174 stored in the global scope. */
31edb802 8175 add_psymbol_to_list (actual_name,
15d034d0 8176 built_actual_name != NULL,
f47fb265 8177 VAR_DOMAIN, LOC_BLOCK,
79748972 8178 SECT_OFF_TEXT (objfile),
75aedd27 8179 psymbol_placement::GLOBAL,
79748972
TT
8180 addr,
8181 cu->language, objfile);
c906108c
SS
8182 }
8183 else
8184 {
31edb802 8185 add_psymbol_to_list (actual_name,
15d034d0 8186 built_actual_name != NULL,
f47fb265 8187 VAR_DOMAIN, LOC_BLOCK,
79748972 8188 SECT_OFF_TEXT (objfile),
75aedd27 8189 psymbol_placement::STATIC,
1762568f 8190 addr, cu->language, objfile);
c906108c 8191 }
0c1b455e
TT
8192
8193 if (pdi->main_subprogram && actual_name != NULL)
8194 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8195 break;
72929c62 8196 case DW_TAG_constant:
31edb802 8197 add_psymbol_to_list (actual_name,
75aedd27
TT
8198 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8199 -1, (pdi->is_external
8200 ? psymbol_placement::GLOBAL
8201 : psymbol_placement::STATIC),
8202 0, cu->language, objfile);
72929c62 8203 break;
c906108c 8204 case DW_TAG_variable:
95554aad
TT
8205 if (pdi->d.locdesc)
8206 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8207
95554aad 8208 if (pdi->d.locdesc
caac4577
JG
8209 && addr == 0
8210 && !dwarf2_per_objfile->has_section_at_zero)
8211 {
8212 /* A global or static variable may also have been stripped
8213 out by the linker if unused, in which case its address
8214 will be nullified; do not add such variables into partial
8215 symbol table then. */
8216 }
8217 else if (pdi->is_external)
c906108c
SS
8218 {
8219 /* Global Variable.
8220 Don't enter into the minimal symbol tables as there is
8221 a minimal symbol table entry from the ELF symbols already.
8222 Enter into partial symbol table if it has a location
8223 descriptor or a type.
8224 If the location descriptor is missing, new_symbol will create
8225 a LOC_UNRESOLVED symbol, the address of the variable will then
8226 be determined from the minimal symbol table whenever the variable
8227 is referenced.
8228 The address for the partial symbol table entry is not
8229 used by GDB, but it comes in handy for debugging partial symbol
8230 table building. */
8231
95554aad 8232 if (pdi->d.locdesc || pdi->has_type)
31edb802 8233 add_psymbol_to_list (actual_name,
15d034d0 8234 built_actual_name != NULL,
f47fb265 8235 VAR_DOMAIN, LOC_STATIC,
79748972 8236 SECT_OFF_TEXT (objfile),
75aedd27 8237 psymbol_placement::GLOBAL,
79748972 8238 addr, cu->language, objfile);
c906108c
SS
8239 }
8240 else
8241 {
ff908ebf
AW
8242 int has_loc = pdi->d.locdesc != NULL;
8243
8244 /* Static Variable. Skip symbols whose value we cannot know (those
8245 without location descriptors or constant values). */
8246 if (!has_loc && !pdi->has_const_value)
43816ebc 8247 return;
ff908ebf 8248
31edb802 8249 add_psymbol_to_list (actual_name,
15d034d0 8250 built_actual_name != NULL,
f47fb265 8251 VAR_DOMAIN, LOC_STATIC,
79748972 8252 SECT_OFF_TEXT (objfile),
75aedd27 8253 psymbol_placement::STATIC,
79748972 8254 has_loc ? addr : 0,
f47fb265 8255 cu->language, objfile);
c906108c
SS
8256 }
8257 break;
8258 case DW_TAG_typedef:
8259 case DW_TAG_base_type:
a02abb62 8260 case DW_TAG_subrange_type:
31edb802 8261 add_psymbol_to_list (actual_name,
15d034d0 8262 built_actual_name != NULL,
79748972 8263 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8264 psymbol_placement::STATIC,
1762568f 8265 0, cu->language, objfile);
c906108c 8266 break;
74921315 8267 case DW_TAG_imported_declaration:
72bf9492 8268 case DW_TAG_namespace:
31edb802 8269 add_psymbol_to_list (actual_name,
15d034d0 8270 built_actual_name != NULL,
79748972 8271 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8272 psymbol_placement::GLOBAL,
1762568f 8273 0, cu->language, objfile);
72bf9492 8274 break;
530e8392 8275 case DW_TAG_module:
a5fd13a9
BH
8276 /* With Fortran 77 there might be a "BLOCK DATA" module
8277 available without any name. If so, we skip the module as it
8278 doesn't bring any value. */
8279 if (actual_name != nullptr)
31edb802 8280 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8281 built_actual_name != NULL,
8282 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8283 psymbol_placement::GLOBAL,
8284 0, cu->language, objfile);
530e8392 8285 break;
c906108c 8286 case DW_TAG_class_type:
680b30c7 8287 case DW_TAG_interface_type:
c906108c
SS
8288 case DW_TAG_structure_type:
8289 case DW_TAG_union_type:
8290 case DW_TAG_enumeration_type:
fa4028e9
JB
8291 /* Skip external references. The DWARF standard says in the section
8292 about "Structure, Union, and Class Type Entries": "An incomplete
8293 structure, union or class type is represented by a structure,
8294 union or class entry that does not have a byte size attribute
8295 and that has a DW_AT_declaration attribute." */
8296 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8297 return;
fa4028e9 8298
63d06c5c
DC
8299 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8300 static vs. global. */
31edb802 8301 add_psymbol_to_list (actual_name,
15d034d0 8302 built_actual_name != NULL,
79748972 8303 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8304 cu->language == language_cplus
75aedd27
TT
8305 ? psymbol_placement::GLOBAL
8306 : psymbol_placement::STATIC,
1762568f 8307 0, cu->language, objfile);
c906108c 8308
c906108c
SS
8309 break;
8310 case DW_TAG_enumerator:
31edb802 8311 add_psymbol_to_list (actual_name,
15d034d0 8312 built_actual_name != NULL,
79748972 8313 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8314 cu->language == language_cplus
75aedd27
TT
8315 ? psymbol_placement::GLOBAL
8316 : psymbol_placement::STATIC,
1762568f 8317 0, cu->language, objfile);
c906108c
SS
8318 break;
8319 default:
8320 break;
8321 }
8322}
8323
5c4e30ca
DC
8324/* Read a partial die corresponding to a namespace; also, add a symbol
8325 corresponding to that namespace to the symbol table. NAMESPACE is
8326 the name of the enclosing namespace. */
91c24f0a 8327
72bf9492
DJ
8328static void
8329add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8330 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8331 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8332{
72bf9492 8333 /* Add a symbol for the namespace. */
e7c27a73 8334
72bf9492 8335 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8336
8337 /* Now scan partial symbols in that namespace. */
8338
91c24f0a 8339 if (pdi->has_children)
cdc07690 8340 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8341}
8342
5d7cb8df
JK
8343/* Read a partial die corresponding to a Fortran module. */
8344
8345static void
8346add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8347 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8348{
530e8392
KB
8349 /* Add a symbol for the namespace. */
8350
8351 add_partial_symbol (pdi, cu);
8352
f55ee35c 8353 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8354
8355 if (pdi->has_children)
cdc07690 8356 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8357}
8358
b1dc1806
XR
8359/* Read a partial die corresponding to a subprogram or an inlined
8360 subprogram and create a partial symbol for that subprogram.
8361 When the CU language allows it, this routine also defines a partial
8362 symbol for each nested subprogram that this subprogram contains.
8363 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8364 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8365
cdc07690
YQ
8366 PDI may also be a lexical block, in which case we simply search
8367 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8368 Again, this is only performed when the CU language allows this
8369 type of definitions. */
8370
8371static void
8372add_partial_subprogram (struct partial_die_info *pdi,
8373 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8374 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8375{
b1dc1806 8376 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8377 {
8378 if (pdi->has_pc_info)
8379 {
8380 if (pdi->lowpc < *lowpc)
8381 *lowpc = pdi->lowpc;
8382 if (pdi->highpc > *highpc)
8383 *highpc = pdi->highpc;
cdc07690 8384 if (set_addrmap)
5734ee8b 8385 {
518817b3 8386 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8387 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8388 CORE_ADDR baseaddr;
b926417a
TT
8389 CORE_ADDR this_highpc;
8390 CORE_ADDR this_lowpc;
5734ee8b 8391
b3b3bada 8392 baseaddr = objfile->text_section_offset ();
b926417a
TT
8393 this_lowpc
8394 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8395 pdi->lowpc + baseaddr)
8396 - baseaddr);
8397 this_highpc
8398 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8399 pdi->highpc + baseaddr)
8400 - baseaddr);
d320c2b5 8401 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8402 this_lowpc, this_highpc - 1,
9291a0cd 8403 cu->per_cu->v.psymtab);
5734ee8b 8404 }
481860b3
GB
8405 }
8406
8407 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8408 {
bc30ff58 8409 if (!pdi->is_declaration)
e8d05480
JB
8410 /* Ignore subprogram DIEs that do not have a name, they are
8411 illegal. Do not emit a complaint at this point, we will
8412 do so when we convert this psymtab into a symtab. */
8413 if (pdi->name)
8414 add_partial_symbol (pdi, cu);
bc30ff58
JB
8415 }
8416 }
6e70227d 8417
bc30ff58
JB
8418 if (! pdi->has_children)
8419 return;
8420
0a4b0913 8421 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8422 {
8423 pdi = pdi->die_child;
8424 while (pdi != NULL)
8425 {
52356b79 8426 pdi->fixup (cu);
bc30ff58 8427 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8428 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8429 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8430 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8431 pdi = pdi->die_sibling;
8432 }
8433 }
8434}
8435
91c24f0a
DC
8436/* Read a partial die corresponding to an enumeration type. */
8437
72bf9492
DJ
8438static void
8439add_partial_enumeration (struct partial_die_info *enum_pdi,
8440 struct dwarf2_cu *cu)
91c24f0a 8441{
72bf9492 8442 struct partial_die_info *pdi;
91c24f0a
DC
8443
8444 if (enum_pdi->name != NULL)
72bf9492
DJ
8445 add_partial_symbol (enum_pdi, cu);
8446
8447 pdi = enum_pdi->die_child;
8448 while (pdi)
91c24f0a 8449 {
72bf9492 8450 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8451 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8452 else
72bf9492
DJ
8453 add_partial_symbol (pdi, cu);
8454 pdi = pdi->die_sibling;
91c24f0a 8455 }
91c24f0a
DC
8456}
8457
6caca83c
CC
8458/* Return the initial uleb128 in the die at INFO_PTR. */
8459
8460static unsigned int
d521ce57 8461peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8462{
8463 unsigned int bytes_read;
8464
8465 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8466}
8467
685af9cd
TT
8468/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8469 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8470
4bb7a0a7
DJ
8471 Return the corresponding abbrev, or NULL if the number is zero (indicating
8472 an empty DIE). In either case *BYTES_READ will be set to the length of
8473 the initial number. */
8474
8475static struct abbrev_info *
685af9cd
TT
8476peek_die_abbrev (const die_reader_specs &reader,
8477 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8478{
685af9cd 8479 dwarf2_cu *cu = reader.cu;
518817b3 8480 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8481 unsigned int abbrev_number
8482 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8483
8484 if (abbrev_number == 0)
8485 return NULL;
8486
685af9cd 8487 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8488 if (!abbrev)
8489 {
422b9917 8490 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8491 " at offset %s [in module %s]"),
422b9917 8492 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8493 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8494 }
8495
8496 return abbrev;
8497}
8498
93311388
DE
8499/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8500 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8501 DIE. Any children of the skipped DIEs will also be skipped. */
8502
d521ce57
TT
8503static const gdb_byte *
8504skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8505{
4bb7a0a7
DJ
8506 while (1)
8507 {
685af9cd
TT
8508 unsigned int bytes_read;
8509 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8510
4bb7a0a7
DJ
8511 if (abbrev == NULL)
8512 return info_ptr + bytes_read;
8513 else
dee91e82 8514 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8515 }
8516}
8517
93311388
DE
8518/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8519 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8520 abbrev corresponding to that skipped uleb128 should be passed in
8521 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8522 children. */
8523
d521ce57
TT
8524static const gdb_byte *
8525skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8526 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8527{
8528 unsigned int bytes_read;
8529 struct attribute attr;
dee91e82
DE
8530 bfd *abfd = reader->abfd;
8531 struct dwarf2_cu *cu = reader->cu;
d521ce57 8532 const gdb_byte *buffer = reader->buffer;
f664829e 8533 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8534 unsigned int form, i;
8535
8536 for (i = 0; i < abbrev->num_attrs; i++)
8537 {
8538 /* The only abbrev we care about is DW_AT_sibling. */
8539 if (abbrev->attrs[i].name == DW_AT_sibling)
8540 {
18a8505e
AT
8541 bool ignored;
8542 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8543 &ignored);
4bb7a0a7 8544 if (attr.form == DW_FORM_ref_addr)
b98664d3 8545 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8546 else
b9502d3f 8547 {
9c541725
PA
8548 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8549 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8550
8551 if (sibling_ptr < info_ptr)
b98664d3 8552 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
8553 else if (sibling_ptr > reader->buffer_end)
8554 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8555 else
8556 return sibling_ptr;
8557 }
4bb7a0a7
DJ
8558 }
8559
8560 /* If it isn't DW_AT_sibling, skip this attribute. */
8561 form = abbrev->attrs[i].form;
8562 skip_attribute:
8563 switch (form)
8564 {
4bb7a0a7 8565 case DW_FORM_ref_addr:
ae411497
TT
8566 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8567 and later it is offset sized. */
8568 if (cu->header.version == 2)
8569 info_ptr += cu->header.addr_size;
8570 else
8571 info_ptr += cu->header.offset_size;
8572 break;
36586728
TT
8573 case DW_FORM_GNU_ref_alt:
8574 info_ptr += cu->header.offset_size;
8575 break;
ae411497 8576 case DW_FORM_addr:
4bb7a0a7
DJ
8577 info_ptr += cu->header.addr_size;
8578 break;
8579 case DW_FORM_data1:
8580 case DW_FORM_ref1:
8581 case DW_FORM_flag:
8fe0f950 8582 case DW_FORM_strx1:
4bb7a0a7
DJ
8583 info_ptr += 1;
8584 break;
2dc7f7b3 8585 case DW_FORM_flag_present:
43988095 8586 case DW_FORM_implicit_const:
2dc7f7b3 8587 break;
4bb7a0a7
DJ
8588 case DW_FORM_data2:
8589 case DW_FORM_ref2:
8fe0f950 8590 case DW_FORM_strx2:
4bb7a0a7
DJ
8591 info_ptr += 2;
8592 break;
8fe0f950
AT
8593 case DW_FORM_strx3:
8594 info_ptr += 3;
8595 break;
4bb7a0a7
DJ
8596 case DW_FORM_data4:
8597 case DW_FORM_ref4:
8fe0f950 8598 case DW_FORM_strx4:
4bb7a0a7
DJ
8599 info_ptr += 4;
8600 break;
8601 case DW_FORM_data8:
8602 case DW_FORM_ref8:
55f1336d 8603 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8604 info_ptr += 8;
8605 break;
0224619f
JK
8606 case DW_FORM_data16:
8607 info_ptr += 16;
8608 break;
4bb7a0a7 8609 case DW_FORM_string:
9b1c24c8 8610 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8611 info_ptr += bytes_read;
8612 break;
2dc7f7b3 8613 case DW_FORM_sec_offset:
4bb7a0a7 8614 case DW_FORM_strp:
36586728 8615 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8616 info_ptr += cu->header.offset_size;
8617 break;
2dc7f7b3 8618 case DW_FORM_exprloc:
4bb7a0a7
DJ
8619 case DW_FORM_block:
8620 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8621 info_ptr += bytes_read;
8622 break;
8623 case DW_FORM_block1:
8624 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8625 break;
8626 case DW_FORM_block2:
8627 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8628 break;
8629 case DW_FORM_block4:
8630 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8631 break;
336d760d 8632 case DW_FORM_addrx:
cf532bd1 8633 case DW_FORM_strx:
4bb7a0a7
DJ
8634 case DW_FORM_sdata:
8635 case DW_FORM_udata:
8636 case DW_FORM_ref_udata:
3019eac3
DE
8637 case DW_FORM_GNU_addr_index:
8638 case DW_FORM_GNU_str_index:
18a8505e 8639 case DW_FORM_rnglistx:
d521ce57 8640 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8641 break;
8642 case DW_FORM_indirect:
8643 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8644 info_ptr += bytes_read;
8645 /* We need to continue parsing from here, so just go back to
8646 the top. */
8647 goto skip_attribute;
8648
8649 default:
3e43a32a
MS
8650 error (_("Dwarf Error: Cannot handle %s "
8651 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8652 dwarf_form_name (form),
8653 bfd_get_filename (abfd));
8654 }
8655 }
8656
8657 if (abbrev->has_children)
dee91e82 8658 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8659 else
8660 return info_ptr;
8661}
8662
93311388 8663/* Locate ORIG_PDI's sibling.
dee91e82 8664 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8665
d521ce57 8666static const gdb_byte *
dee91e82
DE
8667locate_pdi_sibling (const struct die_reader_specs *reader,
8668 struct partial_die_info *orig_pdi,
d521ce57 8669 const gdb_byte *info_ptr)
91c24f0a
DC
8670{
8671 /* Do we know the sibling already? */
72bf9492 8672
91c24f0a
DC
8673 if (orig_pdi->sibling)
8674 return orig_pdi->sibling;
8675
8676 /* Are there any children to deal with? */
8677
8678 if (!orig_pdi->has_children)
8679 return info_ptr;
8680
4bb7a0a7 8681 /* Skip the children the long way. */
91c24f0a 8682
dee91e82 8683 return skip_children (reader, info_ptr);
91c24f0a
DC
8684}
8685
257e7a09 8686/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8687 not NULL. */
c906108c 8688
891813be
TT
8689void
8690dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8691{
ed2dc618
SM
8692 struct dwarf2_per_objfile *dwarf2_per_objfile
8693 = get_dwarf2_per_objfile (objfile);
8694
077cbab2
TT
8695 gdb_assert (!readin);
8696 /* If this psymtab is constructed from a debug-only objfile, the
8697 has_section_at_zero flag will not necessarily be correct. We
8698 can get the correct value for this flag by looking at the data
8699 associated with the (presumably stripped) associated objfile. */
8700 if (objfile->separate_debug_objfile_backlink)
c906108c 8701 {
077cbab2
TT
8702 struct dwarf2_per_objfile *dpo_backlink
8703 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8704
077cbab2
TT
8705 dwarf2_per_objfile->has_section_at_zero
8706 = dpo_backlink->has_section_at_zero;
8707 }
98bfdba5 8708
077cbab2 8709 dwarf2_per_objfile->reading_partial_symbols = 0;
c906108c 8710
8566b89b 8711 expand_psymtab (objfile);
95554aad 8712
ed2dc618 8713 process_cu_includes (dwarf2_per_objfile);
c906108c 8714}
9cdd5dbd
DE
8715\f
8716/* Reading in full CUs. */
c906108c 8717
10b3939b
DJ
8718/* Add PER_CU to the queue. */
8719
8720static void
95554aad
TT
8721queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8722 enum language pretend_language)
10b3939b 8723{
10b3939b 8724 per_cu->queued = 1;
39856def 8725 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8726}
8727
89e63ee4
DE
8728/* If PER_CU is not yet queued, add it to the queue.
8729 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8730 dependency.
0907af0c 8731 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8732 meaning either PER_CU is already queued or it is already loaded.
8733
8734 N.B. There is an invariant here that if a CU is queued then it is loaded.
8735 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8736
8737static int
89e63ee4 8738maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8739 struct dwarf2_per_cu_data *per_cu,
8740 enum language pretend_language)
8741{
8742 /* We may arrive here during partial symbol reading, if we need full
8743 DIEs to process an unusual case (e.g. template arguments). Do
8744 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8745 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8746 {
8747 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8748 return 1;
8749 return 0;
8750 }
8751
8752 /* Mark the dependence relation so that we don't flush PER_CU
8753 too early. */
89e63ee4
DE
8754 if (dependent_cu != NULL)
8755 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8756
8757 /* If it's already on the queue, we have nothing to do. */
8758 if (per_cu->queued)
8759 return 0;
8760
8761 /* If the compilation unit is already loaded, just mark it as
8762 used. */
8763 if (per_cu->cu != NULL)
8764 {
8765 per_cu->cu->last_used = 0;
8766 return 0;
8767 }
8768
8769 /* Add it to the queue. */
8770 queue_comp_unit (per_cu, pretend_language);
8771
8772 return 1;
8773}
8774
10b3939b
DJ
8775/* Process the queue. */
8776
8777static void
ed2dc618 8778process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8779{
b4f54984 8780 if (dwarf_read_debug)
45cfd468
DE
8781 {
8782 fprintf_unfiltered (gdb_stdlog,
8783 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8784 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8785 }
8786
03dd20cc
DJ
8787 /* The queue starts out with one item, but following a DIE reference
8788 may load a new CU, adding it to the end of the queue. */
39856def 8789 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8790 {
39856def
TT
8791 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8792
cc12ce38 8793 if ((dwarf2_per_objfile->using_index
39856def
TT
8794 ? !item.per_cu->v.quick->compunit_symtab
8795 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8796 /* Skip dummy CUs. */
39856def 8797 && item.per_cu->cu != NULL)
f4dc4d17 8798 {
39856def 8799 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8800 unsigned int debug_print_threshold;
247f5c4f 8801 char buf[100];
f4dc4d17 8802
247f5c4f 8803 if (per_cu->is_debug_types)
f4dc4d17 8804 {
247f5c4f
DE
8805 struct signatured_type *sig_type =
8806 (struct signatured_type *) per_cu;
8807
9d8780f0 8808 sprintf (buf, "TU %s at offset %s",
73be47f5 8809 hex_string (sig_type->signature),
9d8780f0 8810 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8811 /* There can be 100s of TUs.
8812 Only print them in verbose mode. */
8813 debug_print_threshold = 2;
f4dc4d17 8814 }
247f5c4f 8815 else
73be47f5 8816 {
9d8780f0
SM
8817 sprintf (buf, "CU at offset %s",
8818 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8819 debug_print_threshold = 1;
8820 }
247f5c4f 8821
b4f54984 8822 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8823 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8824
8825 if (per_cu->is_debug_types)
39856def 8826 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8827 else
39856def 8828 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8829
b4f54984 8830 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8831 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8832 }
10b3939b 8833
39856def
TT
8834 item.per_cu->queued = 0;
8835 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8836 }
8837
b4f54984 8838 if (dwarf_read_debug)
45cfd468
DE
8839 {
8840 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8841 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8842 }
10b3939b
DJ
8843}
8844
10b3939b
DJ
8845/* Read in full symbols for PST, and anything it depends on. */
8846
8566b89b
TT
8847void
8848dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8849{
10b3939b 8850 struct dwarf2_per_cu_data *per_cu;
aaa75496 8851
8566b89b 8852 if (readin)
95554aad
TT
8853 return;
8854
0494dbec 8855 read_dependencies (objfile);
aaa75496 8856
8566b89b 8857 per_cu = per_cu_data;
10b3939b
DJ
8858
8859 if (per_cu == NULL)
aaa75496
JB
8860 {
8861 /* It's an include file, no symbols to read for it.
8862 Everything is in the parent symtab. */
8566b89b 8863 readin = true;
aaa75496
JB
8864 return;
8865 }
c906108c 8866
58f0c718 8867 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
8868}
8869
dee91e82
DE
8870/* Trivial hash function for die_info: the hash value of a DIE
8871 is its offset in .debug_info for this objfile. */
10b3939b 8872
dee91e82
DE
8873static hashval_t
8874die_hash (const void *item)
10b3939b 8875{
9a3c8263 8876 const struct die_info *die = (const struct die_info *) item;
6502dd73 8877
9c541725 8878 return to_underlying (die->sect_off);
dee91e82 8879}
63d06c5c 8880
dee91e82
DE
8881/* Trivial comparison function for die_info structures: two DIEs
8882 are equal if they have the same offset. */
98bfdba5 8883
dee91e82
DE
8884static int
8885die_eq (const void *item_lhs, const void *item_rhs)
8886{
9a3c8263
SM
8887 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8888 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8889
9c541725 8890 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8891}
c906108c 8892
c0ab21c2 8893/* Load the DIEs associated with PER_CU into memory. */
c906108c 8894
dee91e82 8895static void
c0ab21c2
TT
8896load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8897 bool skip_partial,
8898 enum language pretend_language)
dee91e82 8899{
c0ab21c2
TT
8900 gdb_assert (! this_cu->is_debug_types);
8901
6751ebae 8902 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8903 if (reader.dummy_p)
8904 return;
8905
8906 struct dwarf2_cu *cu = reader.cu;
8907 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8908
dee91e82
DE
8909 gdb_assert (cu->die_hash == NULL);
8910 cu->die_hash =
8911 htab_create_alloc_ex (cu->header.length / 12,
8912 die_hash,
8913 die_eq,
8914 NULL,
8915 &cu->comp_unit_obstack,
8916 hashtab_obstack_allocate,
8917 dummy_obstack_deallocate);
e142c38c 8918
3e225074 8919 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8920 reader.comp_unit_die->child
8921 = read_die_and_siblings (&reader, reader.info_ptr,
8922 &info_ptr, reader.comp_unit_die);
8923 cu->dies = reader.comp_unit_die;
dee91e82 8924 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8925
8926 /* We try not to read any attributes in this function, because not
9cdd5dbd 8927 all CUs needed for references have been loaded yet, and symbol
10b3939b 8928 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8929 or we won't be able to build types correctly.
8930 Similarly, if we do not read the producer, we can not apply
8931 producer-specific interpretation. */
c0ab21c2 8932 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8933
8934 reader.keep ();
10b3939b
DJ
8935}
8936
3da10d80
KS
8937/* Add a DIE to the delayed physname list. */
8938
8939static void
8940add_to_method_list (struct type *type, int fnfield_index, int index,
8941 const char *name, struct die_info *die,
8942 struct dwarf2_cu *cu)
8943{
8944 struct delayed_method_info mi;
8945 mi.type = type;
8946 mi.fnfield_index = fnfield_index;
8947 mi.index = index;
8948 mi.name = name;
8949 mi.die = die;
c89b44cd 8950 cu->method_list.push_back (mi);
3da10d80
KS
8951}
8952
3693fdb3
PA
8953/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8954 "const" / "volatile". If so, decrements LEN by the length of the
8955 modifier and return true. Otherwise return false. */
8956
8957template<size_t N>
8958static bool
8959check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8960{
8961 size_t mod_len = sizeof (mod) - 1;
8962 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8963 {
8964 len -= mod_len;
8965 return true;
8966 }
8967 return false;
8968}
8969
3da10d80
KS
8970/* Compute the physnames of any methods on the CU's method list.
8971
8972 The computation of method physnames is delayed in order to avoid the
8973 (bad) condition that one of the method's formal parameters is of an as yet
8974 incomplete type. */
8975
8976static void
8977compute_delayed_physnames (struct dwarf2_cu *cu)
8978{
3693fdb3 8979 /* Only C++ delays computing physnames. */
c89b44cd 8980 if (cu->method_list.empty ())
3693fdb3
PA
8981 return;
8982 gdb_assert (cu->language == language_cplus);
8983
52941706 8984 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8985 {
1d06ead6 8986 const char *physname;
3da10d80 8987 struct fn_fieldlist *fn_flp
c89b44cd
TT
8988 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8989 physname = dwarf2_physname (mi.name, mi.die, cu);
8990 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8991 = physname ? physname : "";
3693fdb3
PA
8992
8993 /* Since there's no tag to indicate whether a method is a
8994 const/volatile overload, extract that information out of the
8995 demangled name. */
8996 if (physname != NULL)
8997 {
8998 size_t len = strlen (physname);
8999
9000 while (1)
9001 {
9002 if (physname[len] == ')') /* shortcut */
9003 break;
9004 else if (check_modifier (physname, len, " const"))
c89b44cd 9005 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9006 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9007 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9008 else
9009 break;
9010 }
9011 }
3da10d80 9012 }
c89b44cd
TT
9013
9014 /* The list is no longer needed. */
9015 cu->method_list.clear ();
3da10d80
KS
9016}
9017
a766d390
DE
9018/* Go objects should be embedded in a DW_TAG_module DIE,
9019 and it's not clear if/how imported objects will appear.
9020 To keep Go support simple until that's worked out,
9021 go back through what we've read and create something usable.
9022 We could do this while processing each DIE, and feels kinda cleaner,
9023 but that way is more invasive.
9024 This is to, for example, allow the user to type "p var" or "b main"
9025 without having to specify the package name, and allow lookups
9026 of module.object to work in contexts that use the expression
9027 parser. */
9028
9029static void
9030fixup_go_packaging (struct dwarf2_cu *cu)
9031{
421d1616 9032 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9033 struct pending *list;
9034 int i;
9035
c24bdb02 9036 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9037 list != NULL;
9038 list = list->next)
a766d390
DE
9039 {
9040 for (i = 0; i < list->nsyms; ++i)
9041 {
9042 struct symbol *sym = list->symbol[i];
9043
c1b5c1eb 9044 if (sym->language () == language_go
a766d390
DE
9045 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9046 {
421d1616
TT
9047 gdb::unique_xmalloc_ptr<char> this_package_name
9048 (go_symbol_package_name (sym));
a766d390
DE
9049
9050 if (this_package_name == NULL)
9051 continue;
9052 if (package_name == NULL)
421d1616 9053 package_name = std::move (this_package_name);
a766d390
DE
9054 else
9055 {
518817b3
SM
9056 struct objfile *objfile
9057 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9058 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9059 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9060 (symbol_symtab (sym) != NULL
9061 ? symtab_to_filename_for_display
9062 (symbol_symtab (sym))
e3b94546 9063 : objfile_name (objfile)),
421d1616 9064 this_package_name.get (), package_name.get ());
a766d390
DE
9065 }
9066 }
9067 }
9068 }
9069
9070 if (package_name != NULL)
9071 {
518817b3 9072 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9073 const char *saved_package_name
421d1616 9074 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9075 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9076 saved_package_name);
a766d390
DE
9077 struct symbol *sym;
9078
e623cf5d 9079 sym = allocate_symbol (objfile);
d3ecddab 9080 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9081 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9082 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9083 e.g., "main" finds the "main" module and not C's main(). */
9084 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9085 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9086 SYMBOL_TYPE (sym) = type;
9087
c24bdb02 9088 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9089 }
9090}
9091
c9317f21
TT
9092/* Allocate a fully-qualified name consisting of the two parts on the
9093 obstack. */
9094
9095static const char *
9096rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9097{
9098 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9099}
9100
9101/* A helper that allocates a struct discriminant_info to attach to a
9102 union type. */
9103
9104static struct discriminant_info *
9105alloc_discriminant_info (struct type *type, int discriminant_index,
9106 int default_index)
9107{
9108 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9109 gdb_assert (discriminant_index == -1
9110 || (discriminant_index >= 0
9111 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9112 gdb_assert (default_index == -1
c7b15a66 9113 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9114
9115 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9116
9117 struct discriminant_info *disc
9118 = ((struct discriminant_info *)
9119 TYPE_ZALLOC (type,
9120 offsetof (struct discriminant_info, discriminants)
9121 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9122 disc->default_index = default_index;
9123 disc->discriminant_index = discriminant_index;
9124
9125 struct dynamic_prop prop;
9126 prop.kind = PROP_UNDEFINED;
9127 prop.data.baton = disc;
9128
9129 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9130
9131 return disc;
9132}
9133
9134/* Some versions of rustc emitted enums in an unusual way.
9135
9136 Ordinary enums were emitted as unions. The first element of each
9137 structure in the union was named "RUST$ENUM$DISR". This element
9138 held the discriminant.
9139
9140 These versions of Rust also implemented the "non-zero"
9141 optimization. When the enum had two values, and one is empty and
9142 the other holds a pointer that cannot be zero, the pointer is used
9143 as the discriminant, with a zero value meaning the empty variant.
9144 Here, the union's first member is of the form
9145 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9146 where the fieldnos are the indices of the fields that should be
9147 traversed in order to find the field (which may be several fields deep)
9148 and the variantname is the name of the variant of the case when the
9149 field is zero.
9150
9151 This function recognizes whether TYPE is of one of these forms,
9152 and, if so, smashes it to be a variant type. */
9153
9154static void
9155quirk_rust_enum (struct type *type, struct objfile *objfile)
9156{
9157 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9158
9159 /* We don't need to deal with empty enums. */
9160 if (TYPE_NFIELDS (type) == 0)
9161 return;
9162
9163#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9164 if (TYPE_NFIELDS (type) == 1
9165 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9166 {
9167 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9168
9169 /* Decode the field name to find the offset of the
9170 discriminant. */
9171 ULONGEST bit_offset = 0;
9172 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9173 while (name[0] >= '0' && name[0] <= '9')
9174 {
9175 char *tail;
9176 unsigned long index = strtoul (name, &tail, 10);
9177 name = tail;
9178 if (*name != '$'
9179 || index >= TYPE_NFIELDS (field_type)
9180 || (TYPE_FIELD_LOC_KIND (field_type, index)
9181 != FIELD_LOC_KIND_BITPOS))
9182 {
b98664d3 9183 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9184 "[in module %s]"),
9185 TYPE_FIELD_NAME (type, 0),
9186 objfile_name (objfile));
9187 return;
9188 }
9189 ++name;
9190
9191 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9192 field_type = TYPE_FIELD_TYPE (field_type, index);
9193 }
9194
9195 /* Make a union to hold the variants. */
9196 struct type *union_type = alloc_type (objfile);
9197 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9198 TYPE_NFIELDS (union_type) = 3;
9199 TYPE_FIELDS (union_type)
9200 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9201 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9202 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9203
9204 /* Put the discriminant must at index 0. */
9205 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9206 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9207 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9208 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9209
9210 /* The order of fields doesn't really matter, so put the real
9211 field at index 1 and the data-less field at index 2. */
9212 struct discriminant_info *disc
9213 = alloc_discriminant_info (union_type, 0, 1);
9214 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9215 TYPE_FIELD_NAME (union_type, 1)
9216 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9217 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9218 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9219 TYPE_FIELD_NAME (union_type, 1));
9220
9221 const char *dataless_name
9222 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9223 name);
9224 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9225 dataless_name);
9226 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9227 /* NAME points into the original discriminant name, which
9228 already has the correct lifetime. */
9229 TYPE_FIELD_NAME (union_type, 2) = name;
9230 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9231 disc->discriminants[2] = 0;
9232
9233 /* Smash this type to be a structure type. We have to do this
9234 because the type has already been recorded. */
9235 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9236 TYPE_NFIELDS (type) = 1;
9237 TYPE_FIELDS (type)
9238 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9239
9240 /* Install the variant part. */
9241 TYPE_FIELD_TYPE (type, 0) = union_type;
9242 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9243 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9244 }
77c2dba3
TT
9245 /* A union with a single anonymous field is probably an old-style
9246 univariant enum. */
9247 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9248 {
c9317f21
TT
9249 /* Smash this type to be a structure type. We have to do this
9250 because the type has already been recorded. */
9251 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9252
9253 /* Make a union to hold the variants. */
9254 struct type *union_type = alloc_type (objfile);
9255 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9256 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9257 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9258 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9259 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9260
9261 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9262 const char *variant_name
9263 = rust_last_path_segment (TYPE_NAME (field_type));
9264 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9265 TYPE_NAME (field_type)
9266 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9267 TYPE_NAME (type), variant_name);
c9317f21
TT
9268
9269 /* Install the union in the outer struct type. */
9270 TYPE_NFIELDS (type) = 1;
9271 TYPE_FIELDS (type)
9272 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9273 TYPE_FIELD_TYPE (type, 0) = union_type;
9274 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9275 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9276
9277 alloc_discriminant_info (union_type, -1, 0);
9278 }
9279 else
9280 {
9281 struct type *disr_type = nullptr;
9282 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9283 {
9284 disr_type = TYPE_FIELD_TYPE (type, i);
9285
a037790e
TT
9286 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9287 {
9288 /* All fields of a true enum will be structs. */
9289 return;
9290 }
9291 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9292 {
9293 /* Could be data-less variant, so keep going. */
a037790e 9294 disr_type = nullptr;
c9317f21
TT
9295 }
9296 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9297 "RUST$ENUM$DISR") != 0)
9298 {
9299 /* Not a Rust enum. */
9300 return;
9301 }
9302 else
9303 {
9304 /* Found one. */
9305 break;
9306 }
9307 }
9308
9309 /* If we got here without a discriminant, then it's probably
9310 just a union. */
9311 if (disr_type == nullptr)
9312 return;
9313
9314 /* Smash this type to be a structure type. We have to do this
9315 because the type has already been recorded. */
9316 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9317
9318 /* Make a union to hold the variants. */
9319 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9320 struct type *union_type = alloc_type (objfile);
9321 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9322 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9323 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9324 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9325 TYPE_FIELDS (union_type)
9326 = (struct field *) TYPE_ZALLOC (union_type,
9327 (TYPE_NFIELDS (union_type)
9328 * sizeof (struct field)));
9329
9330 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9331 TYPE_NFIELDS (type) * sizeof (struct field));
9332
9333 /* Install the discriminant at index 0 in the union. */
9334 TYPE_FIELD (union_type, 0) = *disr_field;
9335 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9336 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9337
9338 /* Install the union in the outer struct type. */
9339 TYPE_FIELD_TYPE (type, 0) = union_type;
9340 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9341 TYPE_NFIELDS (type) = 1;
9342
9343 /* Set the size and offset of the union type. */
9344 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9345
9346 /* We need a way to find the correct discriminant given a
9347 variant name. For convenience we build a map here. */
9348 struct type *enum_type = FIELD_TYPE (*disr_field);
9349 std::unordered_map<std::string, ULONGEST> discriminant_map;
9350 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9351 {
9352 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9353 {
9354 const char *name
9355 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9356 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9357 }
9358 }
9359
9360 int n_fields = TYPE_NFIELDS (union_type);
9361 struct discriminant_info *disc
9362 = alloc_discriminant_info (union_type, 0, -1);
9363 /* Skip the discriminant here. */
9364 for (int i = 1; i < n_fields; ++i)
9365 {
9366 /* Find the final word in the name of this variant's type.
9367 That name can be used to look up the correct
9368 discriminant. */
9369 const char *variant_name
9370 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9371 i)));
9372
9373 auto iter = discriminant_map.find (variant_name);
9374 if (iter != discriminant_map.end ())
9375 disc->discriminants[i] = iter->second;
9376
bedda9ac 9377 /* Remove the discriminant field, if it exists. */
c9317f21 9378 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9379 if (TYPE_NFIELDS (sub_type) > 0)
9380 {
9381 --TYPE_NFIELDS (sub_type);
9382 ++TYPE_FIELDS (sub_type);
9383 }
c9317f21
TT
9384 TYPE_FIELD_NAME (union_type, i) = variant_name;
9385 TYPE_NAME (sub_type)
9386 = rust_fully_qualify (&objfile->objfile_obstack,
9387 TYPE_NAME (type), variant_name);
9388 }
9389 }
9390}
9391
9392/* Rewrite some Rust unions to be structures with variants parts. */
9393
9394static void
9395rust_union_quirks (struct dwarf2_cu *cu)
9396{
9397 gdb_assert (cu->language == language_rust);
52941706
SM
9398 for (type *type_ : cu->rust_unions)
9399 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9400 /* We don't need this any more. */
9401 cu->rust_unions.clear ();
c9317f21
TT
9402}
9403
95554aad
TT
9404/* Return the symtab for PER_CU. This works properly regardless of
9405 whether we're using the index or psymtabs. */
9406
43f3e411
DE
9407static struct compunit_symtab *
9408get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9409{
ed2dc618 9410 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9411 ? per_cu->v.quick->compunit_symtab
9412 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9413}
9414
9415/* A helper function for computing the list of all symbol tables
9416 included by PER_CU. */
9417
9418static void
4c39bc03 9419recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9420 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9421 struct dwarf2_per_cu_data *per_cu,
43f3e411 9422 struct compunit_symtab *immediate_parent)
95554aad
TT
9423{
9424 void **slot;
43f3e411 9425 struct compunit_symtab *cust;
95554aad
TT
9426
9427 slot = htab_find_slot (all_children, per_cu, INSERT);
9428 if (*slot != NULL)
9429 {
9430 /* This inclusion and its children have been processed. */
9431 return;
9432 }
9433
9434 *slot = per_cu;
9435 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9436 cust = get_compunit_symtab (per_cu);
9437 if (cust != NULL)
ec94af83
DE
9438 {
9439 /* If this is a type unit only add its symbol table if we haven't
9440 seen it yet (type unit per_cu's can share symtabs). */
9441 if (per_cu->is_debug_types)
9442 {
43f3e411 9443 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9444 if (*slot == NULL)
9445 {
43f3e411 9446 *slot = cust;
4c39bc03 9447 result->push_back (cust);
43f3e411
DE
9448 if (cust->user == NULL)
9449 cust->user = immediate_parent;
ec94af83
DE
9450 }
9451 }
9452 else
f9125b6c 9453 {
4c39bc03 9454 result->push_back (cust);
43f3e411
DE
9455 if (cust->user == NULL)
9456 cust->user = immediate_parent;
f9125b6c 9457 }
ec94af83 9458 }
95554aad 9459
ae640021
AB
9460 if (!per_cu->imported_symtabs_empty ())
9461 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9462 {
9463 recursively_compute_inclusions (result, all_children,
9464 all_type_symtabs, ptr, cust);
9465 }
95554aad
TT
9466}
9467
43f3e411 9468/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9469 PER_CU. */
9470
9471static void
43f3e411 9472compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9473{
f4dc4d17
DE
9474 gdb_assert (! per_cu->is_debug_types);
9475
ae640021 9476 if (!per_cu->imported_symtabs_empty ())
95554aad 9477 {
ae640021 9478 int len;
4c39bc03 9479 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9480 htab_t all_children, all_type_symtabs;
43f3e411 9481 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9482
9483 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9484 if (cust == NULL)
95554aad
TT
9485 return;
9486
9487 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9488 NULL, xcalloc, xfree);
ec94af83
DE
9489 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9490 NULL, xcalloc, xfree);
95554aad 9491
ae640021 9492 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9493 {
9494 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9495 all_type_symtabs, ptr, cust);
ec94af83 9496 }
95554aad 9497
ec94af83 9498 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9499 len = result_symtabs.size ();
43f3e411 9500 cust->includes
ed2dc618 9501 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9502 struct compunit_symtab *, len + 1);
4c39bc03
TT
9503 memcpy (cust->includes, result_symtabs.data (),
9504 len * sizeof (compunit_symtab *));
43f3e411 9505 cust->includes[len] = NULL;
95554aad 9506
95554aad 9507 htab_delete (all_children);
ec94af83 9508 htab_delete (all_type_symtabs);
95554aad
TT
9509 }
9510}
9511
9512/* Compute the 'includes' field for the symtabs of all the CUs we just
9513 read. */
9514
9515static void
ed2dc618 9516process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9517{
71b73764 9518 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9519 {
9520 if (! iter->is_debug_types)
43f3e411 9521 compute_compunit_symtab_includes (iter);
f4dc4d17 9522 }
95554aad 9523
c5d0225d 9524 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9525}
9526
9cdd5dbd 9527/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9528 already been loaded into memory. */
9529
9530static void
95554aad
TT
9531process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9532 enum language pretend_language)
10b3939b 9533{
10b3939b 9534 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9535 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9536 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9537 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9538 CORE_ADDR lowpc, highpc;
43f3e411 9539 struct compunit_symtab *cust;
10b3939b 9540 CORE_ADDR baseaddr;
4359dff1 9541 struct block *static_block;
3e29f34a 9542 CORE_ADDR addr;
10b3939b 9543
b3b3bada 9544 baseaddr = objfile->text_section_offset ();
10b3939b 9545
c89b44cd
TT
9546 /* Clear the list here in case something was left over. */
9547 cu->method_list.clear ();
10b3939b 9548
95554aad
TT
9549 cu->language = pretend_language;
9550 cu->language_defn = language_def (cu->language);
9551
c906108c 9552 /* Do line number decoding in read_file_scope () */
10b3939b 9553 process_die (cu->dies, cu);
c906108c 9554
a766d390
DE
9555 /* For now fudge the Go package. */
9556 if (cu->language == language_go)
9557 fixup_go_packaging (cu);
9558
5f48f8f3 9559 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9560 should be complete, and it should now be safe to compute all of the
9561 physnames. */
9562 compute_delayed_physnames (cu);
3da10d80 9563
c9317f21
TT
9564 if (cu->language == language_rust)
9565 rust_union_quirks (cu);
9566
fae299cd
DC
9567 /* Some compilers don't define a DW_AT_high_pc attribute for the
9568 compilation unit. If the DW_AT_high_pc is missing, synthesize
9569 it, by scanning the DIE's below the compilation unit. */
10b3939b 9570 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9571
3e29f34a 9572 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9573 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9574
9575 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9576 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9577 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9578 addrmap to help ensure it has an accurate map of pc values belonging to
9579 this comp unit. */
9580 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9581
c24bdb02 9582 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9583 SECT_OFF_TEXT (objfile),
9584 0);
c906108c 9585
43f3e411 9586 if (cust != NULL)
c906108c 9587 {
df15bd07 9588 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9589
8be455d7
JK
9590 /* Set symtab language to language from DW_AT_language. If the
9591 compilation is from a C file generated by language preprocessors, do
9592 not set the language if it was already deduced by start_subfile. */
43f3e411 9593 if (!(cu->language == language_c
40e3ad0e 9594 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9595 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9596
9597 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9598 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9599 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9600 there were bugs in prologue debug info, fixed later in GCC-4.5
9601 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9602
9603 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9604 needed, it would be wrong due to missing DW_AT_producer there.
9605
9606 Still one can confuse GDB by using non-standard GCC compilation
9607 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9608 */
ab260dad 9609 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9610 cust->locations_valid = 1;
e0d00bc7
JK
9611
9612 if (gcc_4_minor >= 5)
43f3e411 9613 cust->epilogue_unwind_valid = 1;
96408a79 9614
43f3e411 9615 cust->call_site_htab = cu->call_site_htab;
c906108c 9616 }
9291a0cd
TT
9617
9618 if (dwarf2_per_objfile->using_index)
43f3e411 9619 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9620 else
9621 {
891813be 9622 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9623 pst->compunit_symtab = cust;
6d94535f 9624 pst->readin = true;
9291a0cd 9625 }
c906108c 9626
95554aad 9627 /* Push it for inclusion processing later. */
c5d0225d 9628 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9629
9630 /* Not needed any more. */
c24bdb02 9631 cu->reset_builder ();
f4dc4d17 9632}
45cfd468 9633
f4dc4d17
DE
9634/* Generate full symbol information for type unit PER_CU, whose DIEs have
9635 already been loaded into memory. */
9636
9637static void
9638process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9639 enum language pretend_language)
9640{
9641 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9642 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9643 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9644 struct compunit_symtab *cust;
0186c6a7
DE
9645 struct signatured_type *sig_type;
9646
9647 gdb_assert (per_cu->is_debug_types);
9648 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9649
c89b44cd
TT
9650 /* Clear the list here in case something was left over. */
9651 cu->method_list.clear ();
f4dc4d17 9652
f4dc4d17
DE
9653 cu->language = pretend_language;
9654 cu->language_defn = language_def (cu->language);
9655
9656 /* The symbol tables are set up in read_type_unit_scope. */
9657 process_die (cu->dies, cu);
9658
9659 /* For now fudge the Go package. */
9660 if (cu->language == language_go)
9661 fixup_go_packaging (cu);
9662
5f48f8f3 9663 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9664 should be complete, and it should now be safe to compute all of the
9665 physnames. */
9666 compute_delayed_physnames (cu);
f4dc4d17 9667
c9317f21
TT
9668 if (cu->language == language_rust)
9669 rust_union_quirks (cu);
9670
f4dc4d17
DE
9671 /* TUs share symbol tables.
9672 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9673 of it with end_expandable_symtab. Otherwise, complete the addition of
9674 this TU's symbols to the existing symtab. */
43f3e411 9675 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9676 {
c24bdb02
KS
9677 buildsym_compunit *builder = cu->get_builder ();
9678 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9679 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9680
43f3e411 9681 if (cust != NULL)
f4dc4d17
DE
9682 {
9683 /* Set symtab language to language from DW_AT_language. If the
9684 compilation is from a C file generated by language preprocessors,
9685 do not set the language if it was already deduced by
9686 start_subfile. */
43f3e411
DE
9687 if (!(cu->language == language_c
9688 && COMPUNIT_FILETABS (cust)->language != language_c))
9689 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9690 }
9691 }
9692 else
9693 {
c24bdb02 9694 cu->get_builder ()->augment_type_symtab ();
43f3e411 9695 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9696 }
9697
9698 if (dwarf2_per_objfile->using_index)
43f3e411 9699 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9700 else
9701 {
891813be 9702 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9703 pst->compunit_symtab = cust;
6d94535f 9704 pst->readin = true;
45cfd468 9705 }
804d2729
TT
9706
9707 /* Not needed any more. */
c24bdb02 9708 cu->reset_builder ();
c906108c
SS
9709}
9710
95554aad
TT
9711/* Process an imported unit DIE. */
9712
9713static void
9714process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9715{
9716 struct attribute *attr;
9717
f4dc4d17
DE
9718 /* For now we don't handle imported units in type units. */
9719 if (cu->per_cu->is_debug_types)
9720 {
9721 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9722 " supported in type units [in module %s]"),
518817b3 9723 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9724 }
9725
95554aad
TT
9726 attr = dwarf2_attr (die, DW_AT_import, cu);
9727 if (attr != NULL)
9728 {
9c541725
PA
9729 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9730 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9731 dwarf2_per_cu_data *per_cu
e3b94546 9732 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9733 cu->per_cu->dwarf2_per_objfile);
95554aad 9734
69d751e3 9735 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9736 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9737 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9738
ae640021 9739 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9740 }
9741}
9742
4c8aa72d
PA
9743/* RAII object that represents a process_die scope: i.e.,
9744 starts/finishes processing a DIE. */
9745class process_die_scope
adde2bff 9746{
4c8aa72d
PA
9747public:
9748 process_die_scope (die_info *die, dwarf2_cu *cu)
9749 : m_die (die), m_cu (cu)
9750 {
9751 /* We should only be processing DIEs not already in process. */
9752 gdb_assert (!m_die->in_process);
9753 m_die->in_process = true;
9754 }
8c3cb9fa 9755
4c8aa72d
PA
9756 ~process_die_scope ()
9757 {
9758 m_die->in_process = false;
9759
9760 /* If we're done processing the DIE for the CU that owns the line
9761 header, we don't need the line header anymore. */
9762 if (m_cu->line_header_die_owner == m_die)
9763 {
9764 delete m_cu->line_header;
9765 m_cu->line_header = NULL;
9766 m_cu->line_header_die_owner = NULL;
9767 }
9768 }
9769
9770private:
9771 die_info *m_die;
9772 dwarf2_cu *m_cu;
9773};
adde2bff 9774
c906108c
SS
9775/* Process a die and its children. */
9776
9777static void
e7c27a73 9778process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9779{
4c8aa72d 9780 process_die_scope scope (die, cu);
adde2bff 9781
c906108c
SS
9782 switch (die->tag)
9783 {
9784 case DW_TAG_padding:
9785 break;
9786 case DW_TAG_compile_unit:
95554aad 9787 case DW_TAG_partial_unit:
e7c27a73 9788 read_file_scope (die, cu);
c906108c 9789 break;
348e048f
DE
9790 case DW_TAG_type_unit:
9791 read_type_unit_scope (die, cu);
9792 break;
c906108c 9793 case DW_TAG_subprogram:
0a4b0913
AB
9794 /* Nested subprograms in Fortran get a prefix. */
9795 if (cu->language == language_fortran
9796 && die->parent != NULL
9797 && die->parent->tag == DW_TAG_subprogram)
9798 cu->processing_has_namespace_info = true;
9799 /* Fall through. */
c906108c 9800 case DW_TAG_inlined_subroutine:
edb3359d 9801 read_func_scope (die, cu);
c906108c
SS
9802 break;
9803 case DW_TAG_lexical_block:
14898363
L
9804 case DW_TAG_try_block:
9805 case DW_TAG_catch_block:
e7c27a73 9806 read_lexical_block_scope (die, cu);
c906108c 9807 break;
216f72a1 9808 case DW_TAG_call_site:
96408a79
SA
9809 case DW_TAG_GNU_call_site:
9810 read_call_site_scope (die, cu);
9811 break;
c906108c 9812 case DW_TAG_class_type:
680b30c7 9813 case DW_TAG_interface_type:
c906108c
SS
9814 case DW_TAG_structure_type:
9815 case DW_TAG_union_type:
134d01f1 9816 process_structure_scope (die, cu);
c906108c
SS
9817 break;
9818 case DW_TAG_enumeration_type:
134d01f1 9819 process_enumeration_scope (die, cu);
c906108c 9820 break;
134d01f1 9821
f792889a
DJ
9822 /* These dies have a type, but processing them does not create
9823 a symbol or recurse to process the children. Therefore we can
9824 read them on-demand through read_type_die. */
c906108c 9825 case DW_TAG_subroutine_type:
72019c9c 9826 case DW_TAG_set_type:
c906108c 9827 case DW_TAG_array_type:
c906108c 9828 case DW_TAG_pointer_type:
c906108c 9829 case DW_TAG_ptr_to_member_type:
c906108c 9830 case DW_TAG_reference_type:
4297a3f0 9831 case DW_TAG_rvalue_reference_type:
c906108c 9832 case DW_TAG_string_type:
c906108c 9833 break;
134d01f1 9834
c906108c 9835 case DW_TAG_base_type:
a02abb62 9836 case DW_TAG_subrange_type:
cb249c71 9837 case DW_TAG_typedef:
134d01f1
DJ
9838 /* Add a typedef symbol for the type definition, if it has a
9839 DW_AT_name. */
f792889a 9840 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9841 break;
c906108c 9842 case DW_TAG_common_block:
e7c27a73 9843 read_common_block (die, cu);
c906108c
SS
9844 break;
9845 case DW_TAG_common_inclusion:
9846 break;
d9fa45fe 9847 case DW_TAG_namespace:
9068261f 9848 cu->processing_has_namespace_info = true;
e7c27a73 9849 read_namespace (die, cu);
d9fa45fe 9850 break;
5d7cb8df 9851 case DW_TAG_module:
9068261f 9852 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9853 read_module (die, cu);
9854 break;
d9fa45fe 9855 case DW_TAG_imported_declaration:
9068261f 9856 cu->processing_has_namespace_info = true;
74921315
KS
9857 if (read_namespace_alias (die, cu))
9858 break;
86a73007
TT
9859 /* The declaration is not a global namespace alias. */
9860 /* Fall through. */
d9fa45fe 9861 case DW_TAG_imported_module:
9068261f 9862 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9863 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9864 || cu->language != language_fortran))
b98664d3 9865 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9866 dwarf_tag_name (die->tag));
9867 read_import_statement (die, cu);
d9fa45fe 9868 break;
95554aad
TT
9869
9870 case DW_TAG_imported_unit:
9871 process_imported_unit_die (die, cu);
9872 break;
9873
71a3c369
TT
9874 case DW_TAG_variable:
9875 read_variable (die, cu);
9876 break;
9877
c906108c 9878 default:
e7c27a73 9879 new_symbol (die, NULL, cu);
c906108c
SS
9880 break;
9881 }
9882}
ca69b9e6
DE
9883\f
9884/* DWARF name computation. */
c906108c 9885
94af9270
KS
9886/* A helper function for dwarf2_compute_name which determines whether DIE
9887 needs to have the name of the scope prepended to the name listed in the
9888 die. */
9889
9890static int
9891die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9892{
1c809c68
TT
9893 struct attribute *attr;
9894
94af9270
KS
9895 switch (die->tag)
9896 {
9897 case DW_TAG_namespace:
9898 case DW_TAG_typedef:
9899 case DW_TAG_class_type:
9900 case DW_TAG_interface_type:
9901 case DW_TAG_structure_type:
9902 case DW_TAG_union_type:
9903 case DW_TAG_enumeration_type:
9904 case DW_TAG_enumerator:
9905 case DW_TAG_subprogram:
08a76f8a 9906 case DW_TAG_inlined_subroutine:
94af9270 9907 case DW_TAG_member:
74921315 9908 case DW_TAG_imported_declaration:
94af9270
KS
9909 return 1;
9910
9911 case DW_TAG_variable:
c2b0a229 9912 case DW_TAG_constant:
94af9270
KS
9913 /* We only need to prefix "globally" visible variables. These include
9914 any variable marked with DW_AT_external or any variable that
9915 lives in a namespace. [Variables in anonymous namespaces
9916 require prefixing, but they are not DW_AT_external.] */
9917
9918 if (dwarf2_attr (die, DW_AT_specification, cu))
9919 {
9920 struct dwarf2_cu *spec_cu = cu;
9a619af0 9921
94af9270
KS
9922 return die_needs_namespace (die_specification (die, &spec_cu),
9923 spec_cu);
9924 }
9925
1c809c68 9926 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9927 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9928 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9929 return 0;
9930 /* A variable in a lexical block of some kind does not need a
9931 namespace, even though in C++ such variables may be external
9932 and have a mangled name. */
9933 if (die->parent->tag == DW_TAG_lexical_block
9934 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9935 || die->parent->tag == DW_TAG_catch_block
9936 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9937 return 0;
9938 return 1;
94af9270
KS
9939
9940 default:
9941 return 0;
9942 }
9943}
9944
73b9be8b
KS
9945/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9946 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9947 defined for the given DIE. */
9948
9949static struct attribute *
9950dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9951{
9952 struct attribute *attr;
9953
9954 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9955 if (attr == NULL)
9956 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9957
9958 return attr;
9959}
9960
9961/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9962 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9963 defined for the given DIE. */
9964
9965static const char *
9966dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9967{
9968 const char *linkage_name;
9969
9970 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9971 if (linkage_name == NULL)
9972 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9973
9974 return linkage_name;
9975}
9976
94af9270 9977/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9978 compute the physname for the object, which include a method's:
9c37b5ae 9979 - formal parameters (C++),
a766d390 9980 - receiver type (Go),
a766d390
DE
9981
9982 The term "physname" is a bit confusing.
9983 For C++, for example, it is the demangled name.
9984 For Go, for example, it's the mangled name.
94af9270 9985
af6b7be1
JB
9986 For Ada, return the DIE's linkage name rather than the fully qualified
9987 name. PHYSNAME is ignored..
9988
94af9270
KS
9989 The result is allocated on the objfile_obstack and canonicalized. */
9990
9991static const char *
15d034d0
TT
9992dwarf2_compute_name (const char *name,
9993 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9994 int physname)
9995{
518817b3 9996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 9997
94af9270
KS
9998 if (name == NULL)
9999 name = dwarf2_name (die, cu);
10000
2ee7123e
DE
10001 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10002 but otherwise compute it by typename_concat inside GDB.
10003 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10004 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10005 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10006 will set the demangled name to the result of dwarf2_full_name, and it is
10007 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10008 if (cu->language == language_ada
10009 || (cu->language == language_fortran && physname))
10010 {
10011 /* For Ada unit, we prefer the linkage name over the name, as
10012 the former contains the exported name, which the user expects
10013 to be able to reference. Ideally, we want the user to be able
10014 to reference this entity using either natural or linkage name,
10015 but we haven't started looking at this enhancement yet. */
73b9be8b 10016 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10017
2ee7123e
DE
10018 if (linkage_name != NULL)
10019 return linkage_name;
f55ee35c
JK
10020 }
10021
94af9270
KS
10022 /* These are the only languages we know how to qualify names in. */
10023 if (name != NULL
9c37b5ae 10024 && (cu->language == language_cplus
c44af4eb
TT
10025 || cu->language == language_fortran || cu->language == language_d
10026 || cu->language == language_rust))
94af9270
KS
10027 {
10028 if (die_needs_namespace (die, cu))
10029 {
0d5cff50 10030 const char *prefix;
34a68019 10031 const char *canonical_name = NULL;
94af9270 10032
d7e74731
PA
10033 string_file buf;
10034
94af9270 10035 prefix = determine_prefix (die, cu);
94af9270
KS
10036 if (*prefix != '\0')
10037 {
43816ebc
TT
10038 gdb::unique_xmalloc_ptr<char> prefixed_name
10039 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10040
43816ebc 10041 buf.puts (prefixed_name.get ());
94af9270
KS
10042 }
10043 else
d7e74731 10044 buf.puts (name);
94af9270 10045
98bfdba5
PA
10046 /* Template parameters may be specified in the DIE's DW_AT_name, or
10047 as children with DW_TAG_template_type_param or
10048 DW_TAG_value_type_param. If the latter, add them to the name
10049 here. If the name already has template parameters, then
10050 skip this step; some versions of GCC emit both, and
10051 it is more efficient to use the pre-computed name.
10052
10053 Something to keep in mind about this process: it is very
10054 unlikely, or in some cases downright impossible, to produce
10055 something that will match the mangled name of a function.
10056 If the definition of the function has the same debug info,
10057 we should be able to match up with it anyway. But fallbacks
10058 using the minimal symbol, for instance to find a method
10059 implemented in a stripped copy of libstdc++, will not work.
10060 If we do not have debug info for the definition, we will have to
10061 match them up some other way.
10062
10063 When we do name matching there is a related problem with function
10064 templates; two instantiated function templates are allowed to
10065 differ only by their return types, which we do not add here. */
10066
10067 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10068 {
10069 struct attribute *attr;
10070 struct die_info *child;
10071 int first = 1;
10072
10073 die->building_fullname = 1;
10074
10075 for (child = die->child; child != NULL; child = child->sibling)
10076 {
10077 struct type *type;
12df843f 10078 LONGEST value;
d521ce57 10079 const gdb_byte *bytes;
98bfdba5
PA
10080 struct dwarf2_locexpr_baton *baton;
10081 struct value *v;
10082
10083 if (child->tag != DW_TAG_template_type_param
10084 && child->tag != DW_TAG_template_value_param)
10085 continue;
10086
10087 if (first)
10088 {
d7e74731 10089 buf.puts ("<");
98bfdba5
PA
10090 first = 0;
10091 }
10092 else
d7e74731 10093 buf.puts (", ");
98bfdba5
PA
10094
10095 attr = dwarf2_attr (child, DW_AT_type, cu);
10096 if (attr == NULL)
10097 {
b98664d3 10098 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10099 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10100 continue;
10101 }
10102 type = die_type (child, cu);
10103
10104 if (child->tag == DW_TAG_template_type_param)
10105 {
c1ec8cea
TT
10106 c_print_type (type, "", &buf, -1, 0, cu->language,
10107 &type_print_raw_options);
98bfdba5
PA
10108 continue;
10109 }
10110
10111 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10112 if (attr == NULL)
10113 {
b98664d3 10114 complaint (_("template parameter missing "
3e43a32a 10115 "DW_AT_const_value"));
d7e74731 10116 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10117 continue;
10118 }
10119
10120 dwarf2_const_value_attr (attr, type, name,
10121 &cu->comp_unit_obstack, cu,
10122 &value, &bytes, &baton);
10123
10124 if (TYPE_NOSIGN (type))
10125 /* GDB prints characters as NUMBER 'CHAR'. If that's
10126 changed, this can use value_print instead. */
d7e74731 10127 c_printchar (value, type, &buf);
98bfdba5
PA
10128 else
10129 {
10130 struct value_print_options opts;
10131
10132 if (baton != NULL)
10133 v = dwarf2_evaluate_loc_desc (type, NULL,
10134 baton->data,
10135 baton->size,
10136 baton->per_cu);
10137 else if (bytes != NULL)
10138 {
10139 v = allocate_value (type);
10140 memcpy (value_contents_writeable (v), bytes,
10141 TYPE_LENGTH (type));
10142 }
10143 else
10144 v = value_from_longest (type, value);
10145
3e43a32a
MS
10146 /* Specify decimal so that we do not depend on
10147 the radix. */
98bfdba5
PA
10148 get_formatted_print_options (&opts, 'd');
10149 opts.raw = 1;
d7e74731 10150 value_print (v, &buf, &opts);
98bfdba5 10151 release_value (v);
98bfdba5
PA
10152 }
10153 }
10154
10155 die->building_fullname = 0;
10156
10157 if (!first)
10158 {
10159 /* Close the argument list, with a space if necessary
10160 (nested templates). */
d7e74731
PA
10161 if (!buf.empty () && buf.string ().back () == '>')
10162 buf.puts (" >");
98bfdba5 10163 else
d7e74731 10164 buf.puts (">");
98bfdba5
PA
10165 }
10166 }
10167
9c37b5ae 10168 /* For C++ methods, append formal parameter type
94af9270 10169 information, if PHYSNAME. */
6e70227d 10170
94af9270 10171 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10172 && cu->language == language_cplus)
94af9270
KS
10173 {
10174 struct type *type = read_type_die (die, cu);
10175
d7e74731 10176 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10177 &type_print_raw_options);
94af9270 10178
9c37b5ae 10179 if (cu->language == language_cplus)
94af9270 10180 {
60430eff
DJ
10181 /* Assume that an artificial first parameter is
10182 "this", but do not crash if it is not. RealView
10183 marks unnamed (and thus unused) parameters as
10184 artificial; there is no way to differentiate
10185 the two cases. */
94af9270
KS
10186 if (TYPE_NFIELDS (type) > 0
10187 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10188 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10189 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10190 0))))
d7e74731 10191 buf.puts (" const");
94af9270
KS
10192 }
10193 }
10194
d7e74731 10195 const std::string &intermediate_name = buf.string ();
94af9270
KS
10196
10197 if (cu->language == language_cplus)
34a68019 10198 canonical_name
322a8516 10199 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10200 &objfile->per_bfd->storage_obstack);
10201
10202 /* If we only computed INTERMEDIATE_NAME, or if
10203 INTERMEDIATE_NAME is already canonical, then we need to
10204 copy it to the appropriate obstack. */
322a8516 10205 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10206 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10207 intermediate_name);
34a68019
TT
10208 else
10209 name = canonical_name;
94af9270
KS
10210 }
10211 }
10212
10213 return name;
10214}
10215
0114d602
DJ
10216/* Return the fully qualified name of DIE, based on its DW_AT_name.
10217 If scope qualifiers are appropriate they will be added. The result
34a68019 10218 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10219 not have a name. NAME may either be from a previous call to
10220 dwarf2_name or NULL.
10221
9c37b5ae 10222 The output string will be canonicalized (if C++). */
0114d602
DJ
10223
10224static const char *
15d034d0 10225dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10226{
94af9270
KS
10227 return dwarf2_compute_name (name, die, cu, 0);
10228}
0114d602 10229
94af9270
KS
10230/* Construct a physname for the given DIE in CU. NAME may either be
10231 from a previous call to dwarf2_name or NULL. The result will be
10232 allocated on the objfile_objstack or NULL if the DIE does not have a
10233 name.
0114d602 10234
9c37b5ae 10235 The output string will be canonicalized (if C++). */
0114d602 10236
94af9270 10237static const char *
15d034d0 10238dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10239{
518817b3 10240 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10241 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10242 int need_copy = 1;
10243
10244 /* In this case dwarf2_compute_name is just a shortcut not building anything
10245 on its own. */
10246 if (!die_needs_namespace (die, cu))
10247 return dwarf2_compute_name (name, die, cu, 1);
10248
73b9be8b 10249 mangled = dw2_linkage_name (die, cu);
900e11f9 10250
e98c9e7c
TT
10251 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10252 See https://github.com/rust-lang/rust/issues/32925. */
10253 if (cu->language == language_rust && mangled != NULL
10254 && strchr (mangled, '{') != NULL)
10255 mangled = NULL;
10256
900e11f9
JK
10257 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10258 has computed. */
791afaa2 10259 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10260 if (mangled != NULL)
900e11f9 10261 {
900e11f9 10262
59cc4834
JB
10263 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10264 {
10265 /* Do nothing (do not demangle the symbol name). */
10266 }
10267 else if (cu->language == language_go)
a766d390 10268 {
5e2db402
TT
10269 /* This is a lie, but we already lie to the caller new_symbol.
10270 new_symbol assumes we return the mangled name.
a766d390 10271 This just undoes that lie until things are cleaned up. */
a766d390
DE
10272 }
10273 else
10274 {
0eb876f5
JB
10275 /* Use DMGL_RET_DROP for C++ template functions to suppress
10276 their return type. It is easier for GDB users to search
10277 for such functions as `name(params)' than `long name(params)'.
10278 In such case the minimal symbol names do not match the full
10279 symbol names but for template functions there is never a need
10280 to look up their definition from their declaration so
10281 the only disadvantage remains the minimal symbol variant
10282 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10283 demangled.reset (gdb_demangle (mangled,
10284 (DMGL_PARAMS | DMGL_ANSI
10285 | DMGL_RET_DROP)));
a766d390 10286 }
900e11f9 10287 if (demangled)
791afaa2 10288 canon = demangled.get ();
900e11f9
JK
10289 else
10290 {
10291 canon = mangled;
10292 need_copy = 0;
10293 }
10294 }
10295
10296 if (canon == NULL || check_physname)
10297 {
10298 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10299
10300 if (canon != NULL && strcmp (physname, canon) != 0)
10301 {
10302 /* It may not mean a bug in GDB. The compiler could also
10303 compute DW_AT_linkage_name incorrectly. But in such case
10304 GDB would need to be bug-to-bug compatible. */
10305
b98664d3 10306 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10307 "(from linkage <%s>) - DIE at %s [in module %s]"),
10308 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10309 objfile_name (objfile));
900e11f9
JK
10310
10311 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10312 is available here - over computed PHYSNAME. It is safer
10313 against both buggy GDB and buggy compilers. */
10314
10315 retval = canon;
10316 }
10317 else
10318 {
10319 retval = physname;
10320 need_copy = 0;
10321 }
10322 }
10323 else
10324 retval = canon;
10325
10326 if (need_copy)
021887d8 10327 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 10328
900e11f9 10329 return retval;
0114d602
DJ
10330}
10331
74921315
KS
10332/* Inspect DIE in CU for a namespace alias. If one exists, record
10333 a new symbol for it.
10334
10335 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10336
10337static int
10338read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10339{
10340 struct attribute *attr;
10341
10342 /* If the die does not have a name, this is not a namespace
10343 alias. */
10344 attr = dwarf2_attr (die, DW_AT_name, cu);
10345 if (attr != NULL)
10346 {
10347 int num;
10348 struct die_info *d = die;
10349 struct dwarf2_cu *imported_cu = cu;
10350
10351 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10352 keep inspecting DIEs until we hit the underlying import. */
10353#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10354 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10355 {
10356 attr = dwarf2_attr (d, DW_AT_import, cu);
10357 if (attr == NULL)
10358 break;
10359
10360 d = follow_die_ref (d, attr, &imported_cu);
10361 if (d->tag != DW_TAG_imported_declaration)
10362 break;
10363 }
10364
10365 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10366 {
b98664d3 10367 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10368 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10369 return 0;
10370 }
10371
10372 if (attr != NULL)
10373 {
10374 struct type *type;
9c541725 10375 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10376
9c541725 10377 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10378 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10379 {
10380 /* This declaration is a global namespace alias. Add
10381 a symbol for it whose type is the aliased namespace. */
10382 new_symbol (die, type, cu);
10383 return 1;
10384 }
10385 }
10386 }
10387
10388 return 0;
10389}
10390
22cee43f 10391/* Return the using directives repository (global or local?) to use in the
804d2729 10392 current context for CU.
22cee43f
PMR
10393
10394 For Ada, imported declarations can materialize renamings, which *may* be
10395 global. However it is impossible (for now?) in DWARF to distinguish
10396 "external" imported declarations and "static" ones. As all imported
10397 declarations seem to be static in all other languages, make them all CU-wide
10398 global only in Ada. */
10399
10400static struct using_direct **
804d2729 10401using_directives (struct dwarf2_cu *cu)
22cee43f 10402{
c24bdb02
KS
10403 if (cu->language == language_ada
10404 && cu->get_builder ()->outermost_context_p ())
10405 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10406 else
c24bdb02 10407 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10408}
10409
27aa8d6a
SW
10410/* Read the import statement specified by the given die and record it. */
10411
10412static void
10413read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10414{
518817b3 10415 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10416 struct attribute *import_attr;
32019081 10417 struct die_info *imported_die, *child_die;
de4affc9 10418 struct dwarf2_cu *imported_cu;
27aa8d6a 10419 const char *imported_name;
794684b6 10420 const char *imported_name_prefix;
13387711
SW
10421 const char *canonical_name;
10422 const char *import_alias;
10423 const char *imported_declaration = NULL;
794684b6 10424 const char *import_prefix;
eb1e02fd 10425 std::vector<const char *> excludes;
13387711 10426
27aa8d6a
SW
10427 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10428 if (import_attr == NULL)
10429 {
b98664d3 10430 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10431 dwarf_tag_name (die->tag));
10432 return;
10433 }
10434
de4affc9
CC
10435 imported_cu = cu;
10436 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10437 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10438 if (imported_name == NULL)
10439 {
10440 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10441
10442 The import in the following code:
10443 namespace A
10444 {
10445 typedef int B;
10446 }
10447
10448 int main ()
10449 {
10450 using A::B;
10451 B b;
10452 return b;
10453 }
10454
10455 ...
10456 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10457 <52> DW_AT_decl_file : 1
10458 <53> DW_AT_decl_line : 6
10459 <54> DW_AT_import : <0x75>
10460 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10461 <59> DW_AT_name : B
10462 <5b> DW_AT_decl_file : 1
10463 <5c> DW_AT_decl_line : 2
10464 <5d> DW_AT_type : <0x6e>
10465 ...
10466 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10467 <76> DW_AT_byte_size : 4
10468 <77> DW_AT_encoding : 5 (signed)
10469
10470 imports the wrong die ( 0x75 instead of 0x58 ).
10471 This case will be ignored until the gcc bug is fixed. */
10472 return;
10473 }
10474
82856980
SW
10475 /* Figure out the local name after import. */
10476 import_alias = dwarf2_name (die, cu);
27aa8d6a 10477
794684b6
SW
10478 /* Figure out where the statement is being imported to. */
10479 import_prefix = determine_prefix (die, cu);
10480
10481 /* Figure out what the scope of the imported die is and prepend it
10482 to the name of the imported die. */
de4affc9 10483 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10484
f55ee35c
JK
10485 if (imported_die->tag != DW_TAG_namespace
10486 && imported_die->tag != DW_TAG_module)
794684b6 10487 {
13387711
SW
10488 imported_declaration = imported_name;
10489 canonical_name = imported_name_prefix;
794684b6 10490 }
13387711 10491 else if (strlen (imported_name_prefix) > 0)
12aaed36 10492 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10493 imported_name_prefix,
10494 (cu->language == language_d ? "." : "::"),
10495 imported_name, (char *) NULL);
13387711
SW
10496 else
10497 canonical_name = imported_name;
794684b6 10498
32019081
JK
10499 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10500 for (child_die = die->child; child_die && child_die->tag;
10501 child_die = sibling_die (child_die))
10502 {
10503 /* DWARF-4: A Fortran use statement with a “rename list” may be
10504 represented by an imported module entry with an import attribute
10505 referring to the module and owned entries corresponding to those
10506 entities that are renamed as part of being imported. */
10507
10508 if (child_die->tag != DW_TAG_imported_declaration)
10509 {
b98664d3 10510 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10511 "- DIE at %s [in module %s]"),
10512 sect_offset_str (child_die->sect_off),
10513 objfile_name (objfile));
32019081
JK
10514 continue;
10515 }
10516
10517 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10518 if (import_attr == NULL)
10519 {
b98664d3 10520 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10521 dwarf_tag_name (child_die->tag));
10522 continue;
10523 }
10524
10525 imported_cu = cu;
10526 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10527 &imported_cu);
10528 imported_name = dwarf2_name (imported_die, imported_cu);
10529 if (imported_name == NULL)
10530 {
b98664d3 10531 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10532 "imported name - DIE at %s [in module %s]"),
10533 sect_offset_str (child_die->sect_off),
10534 objfile_name (objfile));
32019081
JK
10535 continue;
10536 }
10537
eb1e02fd 10538 excludes.push_back (imported_name);
32019081
JK
10539
10540 process_die (child_die, cu);
10541 }
10542
804d2729 10543 add_using_directive (using_directives (cu),
22cee43f
PMR
10544 import_prefix,
10545 canonical_name,
10546 import_alias,
10547 imported_declaration,
10548 excludes,
10549 0,
10550 &objfile->objfile_obstack);
27aa8d6a
SW
10551}
10552
5230b05a
WT
10553/* ICC<14 does not output the required DW_AT_declaration on incomplete
10554 types, but gives them a size of zero. Starting with version 14,
10555 ICC is compatible with GCC. */
10556
9068261f 10557static bool
5230b05a
WT
10558producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10559{
10560 if (!cu->checked_producer)
10561 check_producer (cu);
10562
10563 return cu->producer_is_icc_lt_14;
10564}
10565
eb77c9df
AB
10566/* ICC generates a DW_AT_type for C void functions. This was observed on
10567 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10568 which says that void functions should not have a DW_AT_type. */
10569
10570static bool
10571producer_is_icc (struct dwarf2_cu *cu)
10572{
10573 if (!cu->checked_producer)
10574 check_producer (cu);
10575
10576 return cu->producer_is_icc;
10577}
10578
1b80a9fa
JK
10579/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10580 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10581 this, it was first present in GCC release 4.3.0. */
10582
9068261f 10583static bool
1b80a9fa
JK
10584producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10585{
10586 if (!cu->checked_producer)
10587 check_producer (cu);
10588
10589 return cu->producer_is_gcc_lt_4_3;
10590}
10591
d721ba37
PA
10592static file_and_directory
10593find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10594{
d721ba37
PA
10595 file_and_directory res;
10596
9291a0cd
TT
10597 /* Find the filename. Do not use dwarf2_name here, since the filename
10598 is not a source language identifier. */
d721ba37
PA
10599 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10600 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10601
d721ba37
PA
10602 if (res.comp_dir == NULL
10603 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10604 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10605 {
d721ba37
PA
10606 res.comp_dir_storage = ldirname (res.name);
10607 if (!res.comp_dir_storage.empty ())
10608 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10609 }
d721ba37 10610 if (res.comp_dir != NULL)
9291a0cd
TT
10611 {
10612 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10613 directory, get rid of it. */
d721ba37 10614 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10615
d721ba37
PA
10616 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10617 res.comp_dir = cp + 1;
9291a0cd
TT
10618 }
10619
d721ba37
PA
10620 if (res.name == NULL)
10621 res.name = "<unknown>";
10622
10623 return res;
9291a0cd
TT
10624}
10625
f4dc4d17
DE
10626/* Handle DW_AT_stmt_list for a compilation unit.
10627 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10628 COMP_DIR is the compilation directory. LOWPC is passed to
10629 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10630
10631static void
10632handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10633 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10634{
518817b3
SM
10635 struct dwarf2_per_objfile *dwarf2_per_objfile
10636 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10637 struct attribute *attr;
527f3840
JK
10638 struct line_header line_header_local;
10639 hashval_t line_header_local_hash;
527f3840
JK
10640 void **slot;
10641 int decode_mapping;
2ab95328 10642
f4dc4d17
DE
10643 gdb_assert (! cu->per_cu->is_debug_types);
10644
2ab95328 10645 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10646 if (attr == NULL)
10647 return;
10648
9c541725 10649 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10650
10651 /* The line header hash table is only created if needed (it exists to
10652 prevent redundant reading of the line table for partial_units).
10653 If we're given a partial_unit, we'll need it. If we're given a
10654 compile_unit, then use the line header hash table if it's already
10655 created, but don't create one just yet. */
10656
10657 if (dwarf2_per_objfile->line_header_hash == NULL
10658 && die->tag == DW_TAG_partial_unit)
2ab95328 10659 {
527f3840 10660 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10661 .reset (htab_create_alloc (127, line_header_hash_voidp,
10662 line_header_eq_voidp,
10663 free_line_header_voidp,
10664 xcalloc, xfree));
527f3840 10665 }
2ab95328 10666
9c541725 10667 line_header_local.sect_off = line_offset;
527f3840
JK
10668 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10669 line_header_local_hash = line_header_hash (&line_header_local);
10670 if (dwarf2_per_objfile->line_header_hash != NULL)
10671 {
d15acc42 10672 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10673 &line_header_local,
10674 line_header_local_hash, NO_INSERT);
10675
10676 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10677 is not present in *SLOT (since if there is something in *SLOT then
10678 it will be for a partial_unit). */
10679 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10680 {
527f3840 10681 gdb_assert (*slot != NULL);
9a3c8263 10682 cu->line_header = (struct line_header *) *slot;
527f3840 10683 return;
dee91e82 10684 }
2ab95328 10685 }
527f3840
JK
10686
10687 /* dwarf_decode_line_header does not yet provide sufficient information.
10688 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10689 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10690 if (lh == NULL)
527f3840 10691 return;
4c8aa72d
PA
10692
10693 cu->line_header = lh.release ();
10694 cu->line_header_die_owner = die;
527f3840
JK
10695
10696 if (dwarf2_per_objfile->line_header_hash == NULL)
10697 slot = NULL;
10698 else
10699 {
d15acc42 10700 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10701 &line_header_local,
10702 line_header_local_hash, INSERT);
10703 gdb_assert (slot != NULL);
10704 }
10705 if (slot != NULL && *slot == NULL)
10706 {
10707 /* This newly decoded line number information unit will be owned
10708 by line_header_hash hash table. */
10709 *slot = cu->line_header;
4c8aa72d 10710 cu->line_header_die_owner = NULL;
527f3840
JK
10711 }
10712 else
10713 {
10714 /* We cannot free any current entry in (*slot) as that struct line_header
10715 may be already used by multiple CUs. Create only temporary decoded
10716 line_header for this CU - it may happen at most once for each line
10717 number information unit. And if we're not using line_header_hash
10718 then this is what we want as well. */
10719 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10720 }
10721 decode_mapping = (die->tag != DW_TAG_partial_unit);
10722 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10723 decode_mapping);
fff8551c 10724
2ab95328
TT
10725}
10726
95554aad 10727/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10728
c906108c 10729static void
e7c27a73 10730read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10731{
518817b3
SM
10732 struct dwarf2_per_objfile *dwarf2_per_objfile
10733 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10734 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10735 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10736 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10737 CORE_ADDR highpc = ((CORE_ADDR) 0);
10738 struct attribute *attr;
c906108c 10739 struct die_info *child_die;
e142c38c 10740 CORE_ADDR baseaddr;
6e70227d 10741
380618d6 10742 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10743 baseaddr = objfile->text_section_offset ();
c906108c 10744
fae299cd 10745 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10746
10747 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10748 from finish_block. */
2acceee2 10749 if (lowpc == ((CORE_ADDR) -1))
c906108c 10750 lowpc = highpc;
3e29f34a 10751 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10752
d721ba37 10753 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10754
f4b8a18d
KW
10755 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10756 standardised yet. As a workaround for the language detection we fall
10757 back to the DW_AT_producer string. */
10758 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10759 cu->language = language_opencl;
10760
3019eac3
DE
10761 /* Similar hack for Go. */
10762 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10763 set_cu_language (DW_LANG_Go, cu);
10764
c24bdb02 10765 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10766
10767 /* Decode line number information if present. We do this before
10768 processing child DIEs, so that the line header table is available
10769 for DW_AT_decl_file. */
d721ba37 10770 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10771
10772 /* Process all dies in compilation unit. */
10773 if (die->child != NULL)
10774 {
10775 child_die = die->child;
10776 while (child_die && child_die->tag)
10777 {
10778 process_die (child_die, cu);
10779 child_die = sibling_die (child_die);
10780 }
10781 }
10782
10783 /* Decode macro information, if present. Dwarf 2 macro information
10784 refers to information in the line number info statement program
10785 header, so we can only read it if we've read the header
10786 successfully. */
0af92d60
JK
10787 attr = dwarf2_attr (die, DW_AT_macros, cu);
10788 if (attr == NULL)
10789 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10790 if (attr && cu->line_header)
10791 {
10792 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10793 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10794
43f3e411 10795 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10796 }
10797 else
10798 {
10799 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10800 if (attr && cu->line_header)
10801 {
10802 unsigned int macro_offset = DW_UNSND (attr);
10803
43f3e411 10804 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10805 }
10806 }
3019eac3
DE
10807}
10808
c24bdb02
KS
10809void
10810dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10811{
f4dc4d17
DE
10812 struct type_unit_group *tu_group;
10813 int first_time;
3019eac3 10814 struct attribute *attr;
9c541725 10815 unsigned int i;
0186c6a7 10816 struct signatured_type *sig_type;
3019eac3 10817
f4dc4d17 10818 gdb_assert (per_cu->is_debug_types);
0186c6a7 10819 sig_type = (struct signatured_type *) per_cu;
3019eac3 10820
c24bdb02 10821 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10822
f4dc4d17 10823 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10824 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10825 if (sig_type->type_unit_group == NULL)
c24bdb02 10826 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10827 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10828
10829 /* If we've already processed this stmt_list there's no real need to
10830 do it again, we could fake it and just recreate the part we need
10831 (file name,index -> symtab mapping). If data shows this optimization
10832 is useful we can do it then. */
43f3e411 10833 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10834
10835 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10836 debug info. */
fff8551c 10837 line_header_up lh;
f4dc4d17 10838 if (attr != NULL)
3019eac3 10839 {
9c541725 10840 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10841 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10842 }
10843 if (lh == NULL)
10844 {
10845 if (first_time)
c24bdb02 10846 start_symtab ("", NULL, 0);
f4dc4d17
DE
10847 else
10848 {
10849 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10850 gdb_assert (m_builder == nullptr);
804d2729 10851 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10852 m_builder.reset (new struct buildsym_compunit
10853 (COMPUNIT_OBJFILE (cust), "",
10854 COMPUNIT_DIRNAME (cust),
10855 compunit_language (cust),
10856 0, cust));
f4dc4d17 10857 }
f4dc4d17 10858 return;
3019eac3
DE
10859 }
10860
c24bdb02
KS
10861 line_header = lh.release ();
10862 line_header_die_owner = die;
3019eac3 10863
f4dc4d17
DE
10864 if (first_time)
10865 {
c24bdb02 10866 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10867
1fd60fc0
DE
10868 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10869 still initializing it, and our caller (a few levels up)
10870 process_full_type_unit still needs to know if this is the first
10871 time. */
10872
7ba99d21 10873 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 10874 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 10875 line_header->file_names_size ());
3019eac3 10876
7ba99d21
AT
10877 auto &file_names = line_header->file_names ();
10878 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10879 {
7ba99d21 10880 file_entry &fe = file_names[i];
c24bdb02
KS
10881 dwarf2_start_subfile (this, fe.name,
10882 fe.include_dir (line_header));
10883 buildsym_compunit *b = get_builder ();
10884 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10885 {
4c8aa72d
PA
10886 /* NOTE: start_subfile will recognize when it's been
10887 passed a file it has already seen. So we can't
10888 assume there's a simple mapping from
10889 cu->line_header->file_names to subfiles, plus
10890 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10891 b->get_current_subfile ()->symtab
10892 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10893 }
10894
c24bdb02 10895 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10896 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10897 }
10898 }
10899 else
3019eac3 10900 {
c24bdb02 10901 gdb_assert (m_builder == nullptr);
804d2729 10902 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10903 m_builder.reset (new struct buildsym_compunit
10904 (COMPUNIT_OBJFILE (cust), "",
10905 COMPUNIT_DIRNAME (cust),
10906 compunit_language (cust),
10907 0, cust));
f4dc4d17 10908
7ba99d21
AT
10909 auto &file_names = line_header->file_names ();
10910 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10911 {
7ba99d21 10912 file_entry &fe = file_names[i];
4c8aa72d 10913 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10914 }
3019eac3
DE
10915 }
10916
f4dc4d17
DE
10917 /* The main symtab is allocated last. Type units don't have DW_AT_name
10918 so they don't have a "real" (so to speak) symtab anyway.
10919 There is later code that will assign the main symtab to all symbols
10920 that don't have one. We need to handle the case of a symbol with a
10921 missing symtab (DW_AT_decl_file) anyway. */
10922}
3019eac3 10923
f4dc4d17
DE
10924/* Process DW_TAG_type_unit.
10925 For TUs we want to skip the first top level sibling if it's not the
10926 actual type being defined by this TU. In this case the first top
10927 level sibling is there to provide context only. */
3019eac3 10928
f4dc4d17
DE
10929static void
10930read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10931{
10932 struct die_info *child_die;
3019eac3 10933
f4dc4d17
DE
10934 prepare_one_comp_unit (cu, die, language_minimal);
10935
10936 /* Initialize (or reinitialize) the machinery for building symtabs.
10937 We do this before processing child DIEs, so that the line header table
10938 is available for DW_AT_decl_file. */
c24bdb02 10939 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10940
10941 if (die->child != NULL)
10942 {
10943 child_die = die->child;
10944 while (child_die && child_die->tag)
10945 {
10946 process_die (child_die, cu);
10947 child_die = sibling_die (child_die);
10948 }
10949 }
3019eac3
DE
10950}
10951\f
80626a55
DE
10952/* DWO/DWP files.
10953
10954 http://gcc.gnu.org/wiki/DebugFission
10955 http://gcc.gnu.org/wiki/DebugFissionDWP
10956
10957 To simplify handling of both DWO files ("object" files with the DWARF info)
10958 and DWP files (a file with the DWOs packaged up into one file), we treat
10959 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10960
10961static hashval_t
10962hash_dwo_file (const void *item)
10963{
9a3c8263 10964 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10965 hashval_t hash;
3019eac3 10966
a2ce51a0
DE
10967 hash = htab_hash_string (dwo_file->dwo_name);
10968 if (dwo_file->comp_dir != NULL)
10969 hash += htab_hash_string (dwo_file->comp_dir);
10970 return hash;
3019eac3
DE
10971}
10972
10973static int
10974eq_dwo_file (const void *item_lhs, const void *item_rhs)
10975{
9a3c8263
SM
10976 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10977 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10978
a2ce51a0
DE
10979 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10980 return 0;
10981 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10982 return lhs->comp_dir == rhs->comp_dir;
10983 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10984}
10985
10986/* Allocate a hash table for DWO files. */
10987
51ac9db5 10988static htab_up
ed2dc618 10989allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 10990{
51ac9db5
SM
10991 auto delete_dwo_file = [] (void *item)
10992 {
10993 struct dwo_file *dwo_file = (struct dwo_file *) item;
10994
10995 delete dwo_file;
10996 };
10997
bc68fb19
TT
10998 return htab_up (htab_create_alloc (41,
10999 hash_dwo_file,
11000 eq_dwo_file,
11001 delete_dwo_file,
11002 xcalloc, xfree));
3019eac3
DE
11003}
11004
80626a55
DE
11005/* Lookup DWO file DWO_NAME. */
11006
11007static void **
ed2dc618
SM
11008lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11009 const char *dwo_name,
11010 const char *comp_dir)
80626a55
DE
11011{
11012 struct dwo_file find_entry;
11013 void **slot;
11014
11015 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11016 dwarf2_per_objfile->dwo_files
11017 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11018
0ac5b59e
DE
11019 find_entry.dwo_name = dwo_name;
11020 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11021 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11022 INSERT);
80626a55
DE
11023
11024 return slot;
11025}
11026
3019eac3
DE
11027static hashval_t
11028hash_dwo_unit (const void *item)
11029{
9a3c8263 11030 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11031
11032 /* This drops the top 32 bits of the id, but is ok for a hash. */
11033 return dwo_unit->signature;
11034}
11035
11036static int
11037eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11038{
9a3c8263
SM
11039 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11040 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11041
11042 /* The signature is assumed to be unique within the DWO file.
11043 So while object file CU dwo_id's always have the value zero,
11044 that's OK, assuming each object file DWO file has only one CU,
11045 and that's the rule for now. */
11046 return lhs->signature == rhs->signature;
11047}
11048
11049/* Allocate a hash table for DWO CUs,TUs.
11050 There is one of these tables for each of CUs,TUs for each DWO file. */
11051
b0b6a987 11052static htab_up
3019eac3
DE
11053allocate_dwo_unit_table (struct objfile *objfile)
11054{
11055 /* Start out with a pretty small number.
11056 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11057 return htab_up (htab_create_alloc (3,
11058 hash_dwo_unit,
11059 eq_dwo_unit,
11060 NULL, xcalloc, xfree));
3019eac3
DE
11061}
11062
19c3d4c9 11063/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11064
11065static void
19c3d4c9
DE
11066create_dwo_cu_reader (const struct die_reader_specs *reader,
11067 const gdb_byte *info_ptr,
11068 struct die_info *comp_unit_die,
c0ab21c2
TT
11069 struct dwo_file *dwo_file,
11070 struct dwo_unit *dwo_unit)
3019eac3
DE
11071{
11072 struct dwarf2_cu *cu = reader->cu;
9c541725 11073 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11074 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11075
a084a2a6
AT
11076 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11077 if (!signature.has_value ())
3019eac3 11078 {
b98664d3 11079 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11080 " its dwo_id [in module %s]"),
9d8780f0 11081 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11082 return;
11083 }
11084
3019eac3 11085 dwo_unit->dwo_file = dwo_file;
a084a2a6 11086 dwo_unit->signature = *signature;
8a0459fd 11087 dwo_unit->section = section;
9c541725 11088 dwo_unit->sect_off = sect_off;
3019eac3
DE
11089 dwo_unit->length = cu->per_cu->length;
11090
b4f54984 11091 if (dwarf_read_debug)
9d8780f0
SM
11092 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11093 sect_offset_str (sect_off),
9c541725 11094 hex_string (dwo_unit->signature));
3019eac3
DE
11095}
11096
33c5cd75 11097/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11098 Note: This function processes DWO files only, not DWP files. */
3019eac3 11099
33c5cd75 11100static void
ed2dc618 11101create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11102 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11103 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11104{
11105 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11106 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11107
96b79293 11108 section.read (objfile);
33c5cd75 11109 info_ptr = section.buffer;
3019eac3
DE
11110
11111 if (info_ptr == NULL)
33c5cd75 11112 return;
3019eac3 11113
b4f54984 11114 if (dwarf_read_debug)
19c3d4c9
DE
11115 {
11116 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11117 section.get_name (),
11118 section.get_file_name ());
19c3d4c9 11119 }
3019eac3 11120
33c5cd75 11121 end_ptr = info_ptr + section.size;
3019eac3
DE
11122 while (info_ptr < end_ptr)
11123 {
11124 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11125 struct dwo_unit read_unit {};
33c5cd75
DB
11126 struct dwo_unit *dwo_unit;
11127 void **slot;
11128 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11129
11130 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11131 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11132 per_cu.is_debug_types = 0;
33c5cd75
DB
11133 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11134 per_cu.section = &section;
11135
c0ab21c2
TT
11136 cutu_reader reader (&per_cu, cu, &dwo_file);
11137 if (!reader.dummy_p)
11138 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11139 &dwo_file, &read_unit);
33c5cd75
DB
11140 info_ptr += per_cu.length;
11141
11142 // If the unit could not be parsed, skip it.
c0ab21c2 11143 if (read_unit.dwo_file == NULL)
33c5cd75 11144 continue;
3019eac3 11145
33c5cd75
DB
11146 if (cus_htab == NULL)
11147 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11148
33c5cd75 11149 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11150 *dwo_unit = read_unit;
b0b6a987 11151 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11152 gdb_assert (slot != NULL);
11153 if (*slot != NULL)
19c3d4c9 11154 {
33c5cd75
DB
11155 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11156 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11157
b98664d3 11158 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11159 " the entry at offset %s, signature %s"),
11160 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11161 hex_string (dwo_unit->signature));
19c3d4c9 11162 }
33c5cd75 11163 *slot = (void *)dwo_unit;
3019eac3 11164 }
3019eac3
DE
11165}
11166
80626a55
DE
11167/* DWP file .debug_{cu,tu}_index section format:
11168 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11169
d2415c6c
DE
11170 DWP Version 1:
11171
80626a55
DE
11172 Both index sections have the same format, and serve to map a 64-bit
11173 signature to a set of section numbers. Each section begins with a header,
11174 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11175 indexes, and a pool of 32-bit section numbers. The index sections will be
11176 aligned at 8-byte boundaries in the file.
11177
d2415c6c
DE
11178 The index section header consists of:
11179
11180 V, 32 bit version number
11181 -, 32 bits unused
11182 N, 32 bit number of compilation units or type units in the index
11183 M, 32 bit number of slots in the hash table
80626a55 11184
d2415c6c 11185 Numbers are recorded using the byte order of the application binary.
80626a55 11186
d2415c6c
DE
11187 The hash table begins at offset 16 in the section, and consists of an array
11188 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11189 order of the application binary). Unused slots in the hash table are 0.
11190 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11191
d2415c6c
DE
11192 The parallel table begins immediately after the hash table
11193 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11194 array of 32-bit indexes (using the byte order of the application binary),
11195 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11196 table contains a 32-bit index into the pool of section numbers. For unused
11197 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11198
73869dc2
DE
11199 The pool of section numbers begins immediately following the hash table
11200 (at offset 16 + 12 * M from the beginning of the section). The pool of
11201 section numbers consists of an array of 32-bit words (using the byte order
11202 of the application binary). Each item in the array is indexed starting
11203 from 0. The hash table entry provides the index of the first section
11204 number in the set. Additional section numbers in the set follow, and the
11205 set is terminated by a 0 entry (section number 0 is not used in ELF).
11206
11207 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11208 section must be the first entry in the set, and the .debug_abbrev.dwo must
11209 be the second entry. Other members of the set may follow in any order.
11210
11211 ---
11212
11213 DWP Version 2:
11214
11215 DWP Version 2 combines all the .debug_info, etc. sections into one,
11216 and the entries in the index tables are now offsets into these sections.
11217 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11218 section.
11219
11220 Index Section Contents:
11221 Header
11222 Hash Table of Signatures dwp_hash_table.hash_table
11223 Parallel Table of Indices dwp_hash_table.unit_table
11224 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11225 Table of Section Sizes dwp_hash_table.v2.sizes
11226
11227 The index section header consists of:
11228
11229 V, 32 bit version number
11230 L, 32 bit number of columns in the table of section offsets
11231 N, 32 bit number of compilation units or type units in the index
11232 M, 32 bit number of slots in the hash table
11233
11234 Numbers are recorded using the byte order of the application binary.
11235
11236 The hash table has the same format as version 1.
11237 The parallel table of indices has the same format as version 1,
11238 except that the entries are origin-1 indices into the table of sections
11239 offsets and the table of section sizes.
11240
11241 The table of offsets begins immediately following the parallel table
11242 (at offset 16 + 12 * M from the beginning of the section). The table is
11243 a two-dimensional array of 32-bit words (using the byte order of the
11244 application binary), with L columns and N+1 rows, in row-major order.
11245 Each row in the array is indexed starting from 0. The first row provides
11246 a key to the remaining rows: each column in this row provides an identifier
11247 for a debug section, and the offsets in the same column of subsequent rows
11248 refer to that section. The section identifiers are:
11249
11250 DW_SECT_INFO 1 .debug_info.dwo
11251 DW_SECT_TYPES 2 .debug_types.dwo
11252 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11253 DW_SECT_LINE 4 .debug_line.dwo
11254 DW_SECT_LOC 5 .debug_loc.dwo
11255 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11256 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11257 DW_SECT_MACRO 8 .debug_macro.dwo
11258
11259 The offsets provided by the CU and TU index sections are the base offsets
11260 for the contributions made by each CU or TU to the corresponding section
11261 in the package file. Each CU and TU header contains an abbrev_offset
11262 field, used to find the abbreviations table for that CU or TU within the
11263 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11264 be interpreted as relative to the base offset given in the index section.
11265 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11266 should be interpreted as relative to the base offset for .debug_line.dwo,
11267 and offsets into other debug sections obtained from DWARF attributes should
11268 also be interpreted as relative to the corresponding base offset.
11269
11270 The table of sizes begins immediately following the table of offsets.
11271 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11272 with L columns and N rows, in row-major order. Each row in the array is
11273 indexed starting from 1 (row 0 is shared by the two tables).
11274
11275 ---
11276
11277 Hash table lookup is handled the same in version 1 and 2:
11278
11279 We assume that N and M will not exceed 2^32 - 1.
11280 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11281
d2415c6c
DE
11282 Given a 64-bit compilation unit signature or a type signature S, an entry
11283 in the hash table is located as follows:
80626a55 11284
d2415c6c
DE
11285 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11286 the low-order k bits all set to 1.
80626a55 11287
d2415c6c 11288 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11289
d2415c6c
DE
11290 3) If the hash table entry at index H matches the signature, use that
11291 entry. If the hash table entry at index H is unused (all zeroes),
11292 terminate the search: the signature is not present in the table.
80626a55 11293
d2415c6c 11294 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11295
d2415c6c 11296 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11297 to stop at an unused slot or find the match. */
80626a55
DE
11298
11299/* Create a hash table to map DWO IDs to their CU/TU entry in
11300 .debug_{info,types}.dwo in DWP_FILE.
11301 Returns NULL if there isn't one.
11302 Note: This function processes DWP files only, not DWO files. */
11303
11304static struct dwp_hash_table *
ed2dc618
SM
11305create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11306 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11307{
11308 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11309 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11310 const gdb_byte *index_ptr, *index_end;
80626a55 11311 struct dwarf2_section_info *index;
73869dc2 11312 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11313 struct dwp_hash_table *htab;
11314
11315 if (is_debug_types)
11316 index = &dwp_file->sections.tu_index;
11317 else
11318 index = &dwp_file->sections.cu_index;
11319
96b79293 11320 if (index->empty ())
80626a55 11321 return NULL;
96b79293 11322 index->read (objfile);
80626a55
DE
11323
11324 index_ptr = index->buffer;
11325 index_end = index_ptr + index->size;
11326
11327 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11328 index_ptr += 4;
11329 if (version == 2)
11330 nr_columns = read_4_bytes (dbfd, index_ptr);
11331 else
11332 nr_columns = 0;
11333 index_ptr += 4;
80626a55
DE
11334 nr_units = read_4_bytes (dbfd, index_ptr);
11335 index_ptr += 4;
11336 nr_slots = read_4_bytes (dbfd, index_ptr);
11337 index_ptr += 4;
11338
73869dc2 11339 if (version != 1 && version != 2)
80626a55 11340 {
21aa081e 11341 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11342 " [in module %s]"),
21aa081e 11343 pulongest (version), dwp_file->name);
80626a55
DE
11344 }
11345 if (nr_slots != (nr_slots & -nr_slots))
11346 {
21aa081e 11347 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11348 " is not power of 2 [in module %s]"),
21aa081e 11349 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11350 }
11351
11352 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11353 htab->version = version;
11354 htab->nr_columns = nr_columns;
80626a55
DE
11355 htab->nr_units = nr_units;
11356 htab->nr_slots = nr_slots;
11357 htab->hash_table = index_ptr;
11358 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11359
11360 /* Exit early if the table is empty. */
11361 if (nr_slots == 0 || nr_units == 0
11362 || (version == 2 && nr_columns == 0))
11363 {
11364 /* All must be zero. */
11365 if (nr_slots != 0 || nr_units != 0
11366 || (version == 2 && nr_columns != 0))
11367 {
b98664d3 11368 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11369 " all zero [in modules %s]"),
11370 dwp_file->name);
11371 }
11372 return htab;
11373 }
11374
11375 if (version == 1)
11376 {
11377 htab->section_pool.v1.indices =
11378 htab->unit_table + sizeof (uint32_t) * nr_slots;
11379 /* It's harder to decide whether the section is too small in v1.
11380 V1 is deprecated anyway so we punt. */
11381 }
11382 else
11383 {
11384 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11385 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11386 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11387 /* Reverse map for error checking. */
11388 int ids_seen[DW_SECT_MAX + 1];
11389 int i;
11390
11391 if (nr_columns < 2)
11392 {
11393 error (_("Dwarf Error: bad DWP hash table, too few columns"
11394 " in section table [in module %s]"),
11395 dwp_file->name);
11396 }
11397 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11398 {
11399 error (_("Dwarf Error: bad DWP hash table, too many columns"
11400 " in section table [in module %s]"),
11401 dwp_file->name);
11402 }
04fd5eed
GB
11403 memset (ids, 255, sizeof_ids);
11404 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11405 for (i = 0; i < nr_columns; ++i)
11406 {
11407 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11408
11409 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11410 {
11411 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11412 " in section table [in module %s]"),
11413 id, dwp_file->name);
11414 }
11415 if (ids_seen[id] != -1)
11416 {
11417 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11418 " id %d in section table [in module %s]"),
11419 id, dwp_file->name);
11420 }
11421 ids_seen[id] = i;
11422 ids[i] = id;
11423 }
11424 /* Must have exactly one info or types section. */
11425 if (((ids_seen[DW_SECT_INFO] != -1)
11426 + (ids_seen[DW_SECT_TYPES] != -1))
11427 != 1)
11428 {
11429 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11430 " DWO info/types section [in module %s]"),
11431 dwp_file->name);
11432 }
11433 /* Must have an abbrev section. */
11434 if (ids_seen[DW_SECT_ABBREV] == -1)
11435 {
11436 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11437 " section [in module %s]"),
11438 dwp_file->name);
11439 }
11440 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11441 htab->section_pool.v2.sizes =
11442 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11443 * nr_units * nr_columns);
11444 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11445 * nr_units * nr_columns))
11446 > index_end)
11447 {
11448 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11449 " [in module %s]"),
11450 dwp_file->name);
11451 }
11452 }
80626a55
DE
11453
11454 return htab;
11455}
11456
11457/* Update SECTIONS with the data from SECTP.
11458
11459 This function is like the other "locate" section routines that are
11460 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11461 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11462
11463 The result is non-zero for success, or zero if an error was found. */
11464
11465static int
73869dc2
DE
11466locate_v1_virtual_dwo_sections (asection *sectp,
11467 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11468{
11469 const struct dwop_section_names *names = &dwop_section_names;
11470
11471 if (section_is_p (sectp->name, &names->abbrev_dwo))
11472 {
11473 /* There can be only one. */
049412e3 11474 if (sections->abbrev.s.section != NULL)
80626a55 11475 return 0;
049412e3 11476 sections->abbrev.s.section = sectp;
fd361982 11477 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11478 }
11479 else if (section_is_p (sectp->name, &names->info_dwo)
11480 || section_is_p (sectp->name, &names->types_dwo))
11481 {
11482 /* There can be only one. */
049412e3 11483 if (sections->info_or_types.s.section != NULL)
80626a55 11484 return 0;
049412e3 11485 sections->info_or_types.s.section = sectp;
fd361982 11486 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11487 }
11488 else if (section_is_p (sectp->name, &names->line_dwo))
11489 {
11490 /* There can be only one. */
049412e3 11491 if (sections->line.s.section != NULL)
80626a55 11492 return 0;
049412e3 11493 sections->line.s.section = sectp;
fd361982 11494 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11495 }
11496 else if (section_is_p (sectp->name, &names->loc_dwo))
11497 {
11498 /* There can be only one. */
049412e3 11499 if (sections->loc.s.section != NULL)
80626a55 11500 return 0;
049412e3 11501 sections->loc.s.section = sectp;
fd361982 11502 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11503 }
11504 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11505 {
11506 /* There can be only one. */
049412e3 11507 if (sections->macinfo.s.section != NULL)
80626a55 11508 return 0;
049412e3 11509 sections->macinfo.s.section = sectp;
fd361982 11510 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11511 }
11512 else if (section_is_p (sectp->name, &names->macro_dwo))
11513 {
11514 /* There can be only one. */
049412e3 11515 if (sections->macro.s.section != NULL)
80626a55 11516 return 0;
049412e3 11517 sections->macro.s.section = sectp;
fd361982 11518 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11519 }
11520 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11521 {
11522 /* There can be only one. */
049412e3 11523 if (sections->str_offsets.s.section != NULL)
80626a55 11524 return 0;
049412e3 11525 sections->str_offsets.s.section = sectp;
fd361982 11526 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11527 }
11528 else
11529 {
11530 /* No other kind of section is valid. */
11531 return 0;
11532 }
11533
11534 return 1;
11535}
11536
73869dc2
DE
11537/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11538 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11539 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11540 This is for DWP version 1 files. */
80626a55
DE
11541
11542static struct dwo_unit *
ed2dc618
SM
11543create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11544 struct dwp_file *dwp_file,
73869dc2
DE
11545 uint32_t unit_index,
11546 const char *comp_dir,
11547 ULONGEST signature, int is_debug_types)
80626a55
DE
11548{
11549 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11550 const struct dwp_hash_table *dwp_htab =
11551 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11552 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11553 const char *kind = is_debug_types ? "TU" : "CU";
11554 struct dwo_file *dwo_file;
11555 struct dwo_unit *dwo_unit;
73869dc2 11556 struct virtual_v1_dwo_sections sections;
80626a55 11557 void **dwo_file_slot;
80626a55
DE
11558 int i;
11559
73869dc2
DE
11560 gdb_assert (dwp_file->version == 1);
11561
b4f54984 11562 if (dwarf_read_debug)
80626a55 11563 {
73869dc2 11564 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11565 kind,
73869dc2 11566 pulongest (unit_index), hex_string (signature),
80626a55
DE
11567 dwp_file->name);
11568 }
11569
19ac8c2e 11570 /* Fetch the sections of this DWO unit.
80626a55
DE
11571 Put a limit on the number of sections we look for so that bad data
11572 doesn't cause us to loop forever. */
11573
73869dc2 11574#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11575 (1 /* .debug_info or .debug_types */ \
11576 + 1 /* .debug_abbrev */ \
11577 + 1 /* .debug_line */ \
11578 + 1 /* .debug_loc */ \
11579 + 1 /* .debug_str_offsets */ \
19ac8c2e 11580 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11581 + 1 /* trailing zero */)
11582
11583 memset (&sections, 0, sizeof (sections));
80626a55 11584
73869dc2 11585 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11586 {
11587 asection *sectp;
11588 uint32_t section_nr =
11589 read_4_bytes (dbfd,
73869dc2
DE
11590 dwp_htab->section_pool.v1.indices
11591 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11592
11593 if (section_nr == 0)
11594 break;
11595 if (section_nr >= dwp_file->num_sections)
11596 {
11597 error (_("Dwarf Error: bad DWP hash table, section number too large"
11598 " [in module %s]"),
11599 dwp_file->name);
11600 }
11601
11602 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11603 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11604 {
11605 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11606 " [in module %s]"),
11607 dwp_file->name);
11608 }
11609 }
11610
11611 if (i < 2
96b79293
TT
11612 || sections.info_or_types.empty ()
11613 || sections.abbrev.empty ())
80626a55
DE
11614 {
11615 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11616 " [in module %s]"),
11617 dwp_file->name);
11618 }
73869dc2 11619 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11620 {
11621 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11622 " [in module %s]"),
11623 dwp_file->name);
11624 }
11625
11626 /* It's easier for the rest of the code if we fake a struct dwo_file and
11627 have dwo_unit "live" in that. At least for now.
11628
11629 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11630 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11631 file, we can combine them back into a virtual DWO file to save space
11632 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11633 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11634
791afaa2
TT
11635 std::string virtual_dwo_name =
11636 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11637 sections.abbrev.get_id (),
11638 sections.line.get_id (),
11639 sections.loc.get_id (),
11640 sections.str_offsets.get_id ());
80626a55 11641 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11642 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11643 virtual_dwo_name.c_str (),
11644 comp_dir);
80626a55
DE
11645 /* Create one if necessary. */
11646 if (*dwo_file_slot == NULL)
11647 {
b4f54984 11648 if (dwarf_read_debug)
80626a55
DE
11649 {
11650 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11651 virtual_dwo_name.c_str ());
80626a55 11652 }
51ac9db5 11653 dwo_file = new struct dwo_file;
efba19b0
TT
11654 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11655 virtual_dwo_name);
0ac5b59e 11656 dwo_file->comp_dir = comp_dir;
80626a55
DE
11657 dwo_file->sections.abbrev = sections.abbrev;
11658 dwo_file->sections.line = sections.line;
11659 dwo_file->sections.loc = sections.loc;
11660 dwo_file->sections.macinfo = sections.macinfo;
11661 dwo_file->sections.macro = sections.macro;
11662 dwo_file->sections.str_offsets = sections.str_offsets;
11663 /* The "str" section is global to the entire DWP file. */
11664 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11665 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11666 there's no need to record it in dwo_file.
11667 Also, we can't simply record type sections in dwo_file because
11668 we record a pointer into the vector in dwo_unit. As we collect more
11669 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11670 for it, invalidating all copies of pointers into the previous
11671 contents. */
80626a55
DE
11672 *dwo_file_slot = dwo_file;
11673 }
11674 else
11675 {
b4f54984 11676 if (dwarf_read_debug)
80626a55
DE
11677 {
11678 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11679 virtual_dwo_name.c_str ());
80626a55 11680 }
9a3c8263 11681 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11682 }
80626a55
DE
11683
11684 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11685 dwo_unit->dwo_file = dwo_file;
11686 dwo_unit->signature = signature;
8d749320
SM
11687 dwo_unit->section =
11688 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11689 *dwo_unit->section = sections.info_or_types;
57d63ce2 11690 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11691
11692 return dwo_unit;
11693}
11694
73869dc2
DE
11695/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11696 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11697 piece within that section used by a TU/CU, return a virtual section
11698 of just that piece. */
11699
11700static struct dwarf2_section_info
ed2dc618
SM
11701create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11702 struct dwarf2_section_info *section,
73869dc2
DE
11703 bfd_size_type offset, bfd_size_type size)
11704{
11705 struct dwarf2_section_info result;
11706 asection *sectp;
11707
11708 gdb_assert (section != NULL);
11709 gdb_assert (!section->is_virtual);
11710
11711 memset (&result, 0, sizeof (result));
11712 result.s.containing_section = section;
dc4ccb6f 11713 result.is_virtual = true;
73869dc2
DE
11714
11715 if (size == 0)
11716 return result;
11717
96b79293 11718 sectp = section->get_bfd_section ();
73869dc2
DE
11719
11720 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11721 bounds of the real section. This is a pretty-rare event, so just
11722 flag an error (easier) instead of a warning and trying to cope. */
11723 if (sectp == NULL
fd361982 11724 || offset + size > bfd_section_size (sectp))
73869dc2 11725 {
73869dc2
DE
11726 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11727 " in section %s [in module %s]"),
fd361982 11728 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11729 objfile_name (dwarf2_per_objfile->objfile));
11730 }
11731
11732 result.virtual_offset = offset;
11733 result.size = size;
11734 return result;
11735}
11736
11737/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11738 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11739 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11740 This is for DWP version 2 files. */
11741
11742static struct dwo_unit *
ed2dc618
SM
11743create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11744 struct dwp_file *dwp_file,
73869dc2
DE
11745 uint32_t unit_index,
11746 const char *comp_dir,
11747 ULONGEST signature, int is_debug_types)
11748{
11749 struct objfile *objfile = dwarf2_per_objfile->objfile;
11750 const struct dwp_hash_table *dwp_htab =
11751 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11752 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11753 const char *kind = is_debug_types ? "TU" : "CU";
11754 struct dwo_file *dwo_file;
11755 struct dwo_unit *dwo_unit;
11756 struct virtual_v2_dwo_sections sections;
11757 void **dwo_file_slot;
73869dc2
DE
11758 int i;
11759
11760 gdb_assert (dwp_file->version == 2);
11761
b4f54984 11762 if (dwarf_read_debug)
73869dc2
DE
11763 {
11764 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11765 kind,
11766 pulongest (unit_index), hex_string (signature),
11767 dwp_file->name);
11768 }
11769
11770 /* Fetch the section offsets of this DWO unit. */
11771
11772 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11773
11774 for (i = 0; i < dwp_htab->nr_columns; ++i)
11775 {
11776 uint32_t offset = read_4_bytes (dbfd,
11777 dwp_htab->section_pool.v2.offsets
11778 + (((unit_index - 1) * dwp_htab->nr_columns
11779 + i)
11780 * sizeof (uint32_t)));
11781 uint32_t size = read_4_bytes (dbfd,
11782 dwp_htab->section_pool.v2.sizes
11783 + (((unit_index - 1) * dwp_htab->nr_columns
11784 + i)
11785 * sizeof (uint32_t)));
11786
11787 switch (dwp_htab->section_pool.v2.section_ids[i])
11788 {
11789 case DW_SECT_INFO:
11790 case DW_SECT_TYPES:
11791 sections.info_or_types_offset = offset;
11792 sections.info_or_types_size = size;
11793 break;
11794 case DW_SECT_ABBREV:
11795 sections.abbrev_offset = offset;
11796 sections.abbrev_size = size;
11797 break;
11798 case DW_SECT_LINE:
11799 sections.line_offset = offset;
11800 sections.line_size = size;
11801 break;
11802 case DW_SECT_LOC:
11803 sections.loc_offset = offset;
11804 sections.loc_size = size;
11805 break;
11806 case DW_SECT_STR_OFFSETS:
11807 sections.str_offsets_offset = offset;
11808 sections.str_offsets_size = size;
11809 break;
11810 case DW_SECT_MACINFO:
11811 sections.macinfo_offset = offset;
11812 sections.macinfo_size = size;
11813 break;
11814 case DW_SECT_MACRO:
11815 sections.macro_offset = offset;
11816 sections.macro_size = size;
11817 break;
11818 }
11819 }
11820
11821 /* It's easier for the rest of the code if we fake a struct dwo_file and
11822 have dwo_unit "live" in that. At least for now.
11823
11824 The DWP file can be made up of a random collection of CUs and TUs.
11825 However, for each CU + set of TUs that came from the same original DWO
11826 file, we can combine them back into a virtual DWO file to save space
11827 (fewer struct dwo_file objects to allocate). Remember that for really
11828 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11829
791afaa2
TT
11830 std::string virtual_dwo_name =
11831 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11832 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11833 (long) (sections.line_size ? sections.line_offset : 0),
11834 (long) (sections.loc_size ? sections.loc_offset : 0),
11835 (long) (sections.str_offsets_size
11836 ? sections.str_offsets_offset : 0));
73869dc2 11837 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11838 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11839 virtual_dwo_name.c_str (),
11840 comp_dir);
73869dc2
DE
11841 /* Create one if necessary. */
11842 if (*dwo_file_slot == NULL)
11843 {
b4f54984 11844 if (dwarf_read_debug)
73869dc2
DE
11845 {
11846 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11847 virtual_dwo_name.c_str ());
73869dc2 11848 }
51ac9db5 11849 dwo_file = new struct dwo_file;
efba19b0
TT
11850 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11851 virtual_dwo_name);
73869dc2
DE
11852 dwo_file->comp_dir = comp_dir;
11853 dwo_file->sections.abbrev =
ed2dc618 11854 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11855 sections.abbrev_offset, sections.abbrev_size);
11856 dwo_file->sections.line =
ed2dc618 11857 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11858 sections.line_offset, sections.line_size);
11859 dwo_file->sections.loc =
ed2dc618 11860 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11861 sections.loc_offset, sections.loc_size);
11862 dwo_file->sections.macinfo =
ed2dc618 11863 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11864 sections.macinfo_offset, sections.macinfo_size);
11865 dwo_file->sections.macro =
ed2dc618 11866 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11867 sections.macro_offset, sections.macro_size);
11868 dwo_file->sections.str_offsets =
ed2dc618
SM
11869 create_dwp_v2_section (dwarf2_per_objfile,
11870 &dwp_file->sections.str_offsets,
73869dc2
DE
11871 sections.str_offsets_offset,
11872 sections.str_offsets_size);
11873 /* The "str" section is global to the entire DWP file. */
11874 dwo_file->sections.str = dwp_file->sections.str;
11875 /* The info or types section is assigned below to dwo_unit,
11876 there's no need to record it in dwo_file.
11877 Also, we can't simply record type sections in dwo_file because
11878 we record a pointer into the vector in dwo_unit. As we collect more
11879 types we'll grow the vector and eventually have to reallocate space
11880 for it, invalidating all copies of pointers into the previous
11881 contents. */
11882 *dwo_file_slot = dwo_file;
11883 }
11884 else
11885 {
b4f54984 11886 if (dwarf_read_debug)
73869dc2
DE
11887 {
11888 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11889 virtual_dwo_name.c_str ());
73869dc2 11890 }
9a3c8263 11891 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11892 }
73869dc2
DE
11893
11894 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11895 dwo_unit->dwo_file = dwo_file;
11896 dwo_unit->signature = signature;
8d749320
SM
11897 dwo_unit->section =
11898 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11899 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11900 is_debug_types
73869dc2
DE
11901 ? &dwp_file->sections.types
11902 : &dwp_file->sections.info,
11903 sections.info_or_types_offset,
11904 sections.info_or_types_size);
11905 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11906
11907 return dwo_unit;
11908}
11909
57d63ce2
DE
11910/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11911 Returns NULL if the signature isn't found. */
80626a55
DE
11912
11913static struct dwo_unit *
ed2dc618
SM
11914lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11915 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11916 ULONGEST signature, int is_debug_types)
80626a55 11917{
57d63ce2
DE
11918 const struct dwp_hash_table *dwp_htab =
11919 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11920 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11921 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11922 uint32_t hash = signature & mask;
11923 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11924 unsigned int i;
11925 void **slot;
870f88f7 11926 struct dwo_unit find_dwo_cu;
80626a55
DE
11927
11928 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11929 find_dwo_cu.signature = signature;
19ac8c2e 11930 slot = htab_find_slot (is_debug_types
48b490f2
TT
11931 ? dwp_file->loaded_tus.get ()
11932 : dwp_file->loaded_cus.get (),
19ac8c2e 11933 &find_dwo_cu, INSERT);
80626a55
DE
11934
11935 if (*slot != NULL)
9a3c8263 11936 return (struct dwo_unit *) *slot;
80626a55
DE
11937
11938 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11939 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11940 {
11941 ULONGEST signature_in_table;
11942
11943 signature_in_table =
57d63ce2 11944 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11945 if (signature_in_table == signature)
11946 {
57d63ce2
DE
11947 uint32_t unit_index =
11948 read_4_bytes (dbfd,
11949 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11950
73869dc2
DE
11951 if (dwp_file->version == 1)
11952 {
ed2dc618
SM
11953 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11954 dwp_file, unit_index,
73869dc2
DE
11955 comp_dir, signature,
11956 is_debug_types);
11957 }
11958 else
11959 {
ed2dc618
SM
11960 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11961 dwp_file, unit_index,
73869dc2
DE
11962 comp_dir, signature,
11963 is_debug_types);
11964 }
9a3c8263 11965 return (struct dwo_unit *) *slot;
80626a55
DE
11966 }
11967 if (signature_in_table == 0)
11968 return NULL;
11969 hash = (hash + hash2) & mask;
11970 }
11971
11972 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11973 " [in module %s]"),
11974 dwp_file->name);
11975}
11976
ab5088bf 11977/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11978 Open the file specified by FILE_NAME and hand it off to BFD for
11979 preliminary analysis. Return a newly initialized bfd *, which
11980 includes a canonicalized copy of FILE_NAME.
80626a55 11981 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11982 SEARCH_CWD is true if the current directory is to be searched.
11983 It will be searched before debug-file-directory.
13aaf454
DE
11984 If successful, the file is added to the bfd include table of the
11985 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11986 If unable to find/open the file, return NULL.
3019eac3
DE
11987 NOTE: This function is derived from symfile_bfd_open. */
11988
192b62ce 11989static gdb_bfd_ref_ptr
ed2dc618
SM
11990try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11991 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11992{
24b9144d 11993 int desc;
9c02c129
DE
11994 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11995 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11996 to debug_file_directory. */
e0cc99a6 11997 const char *search_path;
9c02c129
DE
11998 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11999
e0cc99a6 12000 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12001 if (search_cwd)
12002 {
12003 if (*debug_file_directory != '\0')
e0cc99a6
TT
12004 {
12005 search_path_holder.reset (concat (".", dirname_separator_string,
12006 debug_file_directory,
12007 (char *) NULL));
12008 search_path = search_path_holder.get ();
12009 }
6ac97d4c 12010 else
e0cc99a6 12011 search_path = ".";
6ac97d4c 12012 }
9c02c129 12013 else
e0cc99a6 12014 search_path = debug_file_directory;
3019eac3 12015
24b9144d 12016 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12017 if (is_dwp)
12018 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12019
12020 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12021 desc = openp (search_path, flags, file_name,
3019eac3
DE
12022 O_RDONLY | O_BINARY, &absolute_name);
12023 if (desc < 0)
12024 return NULL;
12025
e0cc99a6
TT
12026 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12027 gnutarget, desc));
9c02c129
DE
12028 if (sym_bfd == NULL)
12029 return NULL;
192b62ce 12030 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12031
192b62ce
TT
12032 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12033 return NULL;
3019eac3 12034
13aaf454
DE
12035 /* Success. Record the bfd as having been included by the objfile's bfd.
12036 This is important because things like demangled_names_hash lives in the
12037 objfile's per_bfd space and may have references to things like symbol
12038 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12039 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12040
3019eac3
DE
12041 return sym_bfd;
12042}
12043
ab5088bf 12044/* Try to open DWO file FILE_NAME.
3019eac3
DE
12045 COMP_DIR is the DW_AT_comp_dir attribute.
12046 The result is the bfd handle of the file.
12047 If there is a problem finding or opening the file, return NULL.
12048 Upon success, the canonicalized path of the file is stored in the bfd,
12049 same as symfile_bfd_open. */
12050
192b62ce 12051static gdb_bfd_ref_ptr
ed2dc618
SM
12052open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12053 const char *file_name, const char *comp_dir)
3019eac3 12054{
80626a55 12055 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12056 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12057 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12058
12059 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12060
12061 if (comp_dir != NULL)
12062 {
43816ebc
TT
12063 gdb::unique_xmalloc_ptr<char> path_to_try
12064 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12065
12066 /* NOTE: If comp_dir is a relative path, this will also try the
12067 search path, which seems useful. */
ed2dc618 12068 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12069 path_to_try.get (),
ed2dc618 12070 0 /*is_dwp*/,
192b62ce 12071 1 /*search_cwd*/));
3019eac3
DE
12072 if (abfd != NULL)
12073 return abfd;
12074 }
12075
12076 /* That didn't work, try debug-file-directory, which, despite its name,
12077 is a list of paths. */
12078
12079 if (*debug_file_directory == '\0')
12080 return NULL;
12081
ed2dc618
SM
12082 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12083 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12084}
12085
80626a55
DE
12086/* This function is mapped across the sections and remembers the offset and
12087 size of each of the DWO debugging sections we are interested in. */
12088
12089static void
12090dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12091{
9a3c8263 12092 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12093 const struct dwop_section_names *names = &dwop_section_names;
12094
12095 if (section_is_p (sectp->name, &names->abbrev_dwo))
12096 {
049412e3 12097 dwo_sections->abbrev.s.section = sectp;
fd361982 12098 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12099 }
12100 else if (section_is_p (sectp->name, &names->info_dwo))
12101 {
049412e3 12102 dwo_sections->info.s.section = sectp;
fd361982 12103 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12104 }
12105 else if (section_is_p (sectp->name, &names->line_dwo))
12106 {
049412e3 12107 dwo_sections->line.s.section = sectp;
fd361982 12108 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12109 }
12110 else if (section_is_p (sectp->name, &names->loc_dwo))
12111 {
049412e3 12112 dwo_sections->loc.s.section = sectp;
fd361982 12113 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12114 }
12115 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12116 {
049412e3 12117 dwo_sections->macinfo.s.section = sectp;
fd361982 12118 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12119 }
12120 else if (section_is_p (sectp->name, &names->macro_dwo))
12121 {
049412e3 12122 dwo_sections->macro.s.section = sectp;
fd361982 12123 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12124 }
12125 else if (section_is_p (sectp->name, &names->str_dwo))
12126 {
049412e3 12127 dwo_sections->str.s.section = sectp;
fd361982 12128 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12129 }
12130 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12131 {
049412e3 12132 dwo_sections->str_offsets.s.section = sectp;
fd361982 12133 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12134 }
12135 else if (section_is_p (sectp->name, &names->types_dwo))
12136 {
12137 struct dwarf2_section_info type_section;
12138
12139 memset (&type_section, 0, sizeof (type_section));
049412e3 12140 type_section.s.section = sectp;
fd361982 12141 type_section.size = bfd_section_size (sectp);
fd5866f6 12142 dwo_sections->types.push_back (type_section);
80626a55
DE
12143 }
12144}
12145
ab5088bf 12146/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12147 by PER_CU. This is for the non-DWP case.
80626a55 12148 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12149
12150static struct dwo_file *
0ac5b59e
DE
12151open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12152 const char *dwo_name, const char *comp_dir)
3019eac3 12153{
ed2dc618 12154 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12155
fb1eb2f9 12156 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12157 if (dbfd == NULL)
12158 {
b4f54984 12159 if (dwarf_read_debug)
80626a55
DE
12160 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12161 return NULL;
12162 }
263db9a1 12163
51ac9db5 12164 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12165 dwo_file->dwo_name = dwo_name;
12166 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12167 dwo_file->dbfd = std::move (dbfd);
3019eac3 12168
fb1eb2f9 12169 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12170 &dwo_file->sections);
3019eac3 12171
18a8505e
AT
12172 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12173 dwo_file->sections.info, dwo_file->cus);
3019eac3 12174
263db9a1 12175 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12176 dwo_file->sections.types, dwo_file->tus);
3019eac3 12177
b4f54984 12178 if (dwarf_read_debug)
80626a55
DE
12179 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12180
263db9a1 12181 return dwo_file.release ();
3019eac3
DE
12182}
12183
80626a55 12184/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12185 size of each of the DWP debugging sections common to version 1 and 2 that
12186 we are interested in. */
3019eac3 12187
80626a55 12188static void
73869dc2
DE
12189dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12190 void *dwp_file_ptr)
3019eac3 12191{
9a3c8263 12192 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12193 const struct dwop_section_names *names = &dwop_section_names;
12194 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12195
80626a55 12196 /* Record the ELF section number for later lookup: this is what the
73869dc2 12197 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12198 gdb_assert (elf_section_nr < dwp_file->num_sections);
12199 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12200
80626a55
DE
12201 /* Look for specific sections that we need. */
12202 if (section_is_p (sectp->name, &names->str_dwo))
12203 {
049412e3 12204 dwp_file->sections.str.s.section = sectp;
fd361982 12205 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12206 }
12207 else if (section_is_p (sectp->name, &names->cu_index))
12208 {
049412e3 12209 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12210 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12211 }
12212 else if (section_is_p (sectp->name, &names->tu_index))
12213 {
049412e3 12214 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12215 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12216 }
12217}
3019eac3 12218
73869dc2
DE
12219/* This function is mapped across the sections and remembers the offset and
12220 size of each of the DWP version 2 debugging sections that we are interested
12221 in. This is split into a separate function because we don't know if we
12222 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12223
12224static void
12225dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12226{
9a3c8263 12227 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12228 const struct dwop_section_names *names = &dwop_section_names;
12229 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12230
12231 /* Record the ELF section number for later lookup: this is what the
12232 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12233 gdb_assert (elf_section_nr < dwp_file->num_sections);
12234 dwp_file->elf_sections[elf_section_nr] = sectp;
12235
12236 /* Look for specific sections that we need. */
12237 if (section_is_p (sectp->name, &names->abbrev_dwo))
12238 {
049412e3 12239 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12240 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12241 }
12242 else if (section_is_p (sectp->name, &names->info_dwo))
12243 {
049412e3 12244 dwp_file->sections.info.s.section = sectp;
fd361982 12245 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12246 }
12247 else if (section_is_p (sectp->name, &names->line_dwo))
12248 {
049412e3 12249 dwp_file->sections.line.s.section = sectp;
fd361982 12250 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12251 }
12252 else if (section_is_p (sectp->name, &names->loc_dwo))
12253 {
049412e3 12254 dwp_file->sections.loc.s.section = sectp;
fd361982 12255 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12256 }
12257 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12258 {
049412e3 12259 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12260 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12261 }
12262 else if (section_is_p (sectp->name, &names->macro_dwo))
12263 {
049412e3 12264 dwp_file->sections.macro.s.section = sectp;
fd361982 12265 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12266 }
12267 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12268 {
049412e3 12269 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12270 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12271 }
12272 else if (section_is_p (sectp->name, &names->types_dwo))
12273 {
049412e3 12274 dwp_file->sections.types.s.section = sectp;
fd361982 12275 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12276 }
12277}
12278
80626a55 12279/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12280
80626a55
DE
12281static hashval_t
12282hash_dwp_loaded_cutus (const void *item)
12283{
9a3c8263 12284 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12285
80626a55
DE
12286 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12287 return dwo_unit->signature;
3019eac3
DE
12288}
12289
80626a55 12290/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12291
80626a55
DE
12292static int
12293eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12294{
9a3c8263
SM
12295 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12296 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12297
80626a55
DE
12298 return dua->signature == dub->signature;
12299}
3019eac3 12300
80626a55 12301/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12302
48b490f2 12303static htab_up
80626a55
DE
12304allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12305{
48b490f2
TT
12306 return htab_up (htab_create_alloc (3,
12307 hash_dwp_loaded_cutus,
12308 eq_dwp_loaded_cutus,
12309 NULL, xcalloc, xfree));
80626a55 12310}
3019eac3 12311
ab5088bf
DE
12312/* Try to open DWP file FILE_NAME.
12313 The result is the bfd handle of the file.
12314 If there is a problem finding or opening the file, return NULL.
12315 Upon success, the canonicalized path of the file is stored in the bfd,
12316 same as symfile_bfd_open. */
12317
192b62ce 12318static gdb_bfd_ref_ptr
ed2dc618
SM
12319open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12320 const char *file_name)
ab5088bf 12321{
ed2dc618
SM
12322 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12323 1 /*is_dwp*/,
192b62ce 12324 1 /*search_cwd*/));
6ac97d4c
DE
12325 if (abfd != NULL)
12326 return abfd;
12327
12328 /* Work around upstream bug 15652.
12329 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12330 [Whether that's a "bug" is debatable, but it is getting in our way.]
12331 We have no real idea where the dwp file is, because gdb's realpath-ing
12332 of the executable's path may have discarded the needed info.
12333 [IWBN if the dwp file name was recorded in the executable, akin to
12334 .gnu_debuglink, but that doesn't exist yet.]
12335 Strip the directory from FILE_NAME and search again. */
12336 if (*debug_file_directory != '\0')
12337 {
12338 /* Don't implicitly search the current directory here.
12339 If the user wants to search "." to handle this case,
12340 it must be added to debug-file-directory. */
ed2dc618
SM
12341 return try_open_dwop_file (dwarf2_per_objfile,
12342 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12343 0 /*search_cwd*/);
12344 }
12345
12346 return NULL;
ab5088bf
DE
12347}
12348
80626a55
DE
12349/* Initialize the use of the DWP file for the current objfile.
12350 By convention the name of the DWP file is ${objfile}.dwp.
12351 The result is NULL if it can't be found. */
a766d390 12352
400174b1 12353static std::unique_ptr<struct dwp_file>
ed2dc618 12354open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12355{
12356 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12357
82bf32bc
JK
12358 /* Try to find first .dwp for the binary file before any symbolic links
12359 resolving. */
6c447423
DE
12360
12361 /* If the objfile is a debug file, find the name of the real binary
12362 file and get the name of dwp file from there. */
d721ba37 12363 std::string dwp_name;
6c447423
DE
12364 if (objfile->separate_debug_objfile_backlink != NULL)
12365 {
12366 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12367 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12368
d721ba37 12369 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12370 }
12371 else
d721ba37
PA
12372 dwp_name = objfile->original_name;
12373
12374 dwp_name += ".dwp";
80626a55 12375
ed2dc618 12376 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12377 if (dbfd == NULL
12378 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12379 {
12380 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12381 dwp_name = objfile_name (objfile);
12382 dwp_name += ".dwp";
ed2dc618 12383 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12384 }
12385
80626a55
DE
12386 if (dbfd == NULL)
12387 {
b4f54984 12388 if (dwarf_read_debug)
d721ba37 12389 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12390 return std::unique_ptr<dwp_file> ();
3019eac3 12391 }
400174b1
TT
12392
12393 const char *name = bfd_get_filename (dbfd.get ());
12394 std::unique_ptr<struct dwp_file> dwp_file
12395 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12396
0a0f4c01 12397 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12398 dwp_file->elf_sections =
12399 OBSTACK_CALLOC (&objfile->objfile_obstack,
12400 dwp_file->num_sections, asection *);
12401
400174b1
TT
12402 bfd_map_over_sections (dwp_file->dbfd.get (),
12403 dwarf2_locate_common_dwp_sections,
12404 dwp_file.get ());
80626a55 12405
400174b1
TT
12406 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12407 0);
80626a55 12408
400174b1
TT
12409 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12410 1);
80626a55 12411
73869dc2 12412 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12413 if (dwp_file->cus && dwp_file->tus
12414 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12415 {
12416 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12417 pretty bizarre. We use pulongest here because that's the established
4d65956b 12418 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12419 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12420 " TU version %s [in DWP file %s]"),
12421 pulongest (dwp_file->cus->version),
d721ba37 12422 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12423 }
08302ed2
DE
12424
12425 if (dwp_file->cus)
12426 dwp_file->version = dwp_file->cus->version;
12427 else if (dwp_file->tus)
12428 dwp_file->version = dwp_file->tus->version;
12429 else
12430 dwp_file->version = 2;
73869dc2
DE
12431
12432 if (dwp_file->version == 2)
400174b1
TT
12433 bfd_map_over_sections (dwp_file->dbfd.get (),
12434 dwarf2_locate_v2_dwp_sections,
12435 dwp_file.get ());
73869dc2 12436
19ac8c2e
DE
12437 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12438 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 12439
b4f54984 12440 if (dwarf_read_debug)
80626a55
DE
12441 {
12442 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12443 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12444 " %s CUs, %s TUs\n",
12445 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12446 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12447 }
12448
12449 return dwp_file;
3019eac3 12450}
c906108c 12451
ab5088bf
DE
12452/* Wrapper around open_and_init_dwp_file, only open it once. */
12453
12454static struct dwp_file *
ed2dc618 12455get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12456{
12457 if (! dwarf2_per_objfile->dwp_checked)
12458 {
ed2dc618
SM
12459 dwarf2_per_objfile->dwp_file
12460 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12461 dwarf2_per_objfile->dwp_checked = 1;
12462 }
400174b1 12463 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12464}
12465
80626a55
DE
12466/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12467 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12468 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12469 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12470 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12471
12472 This is called, for example, when wanting to read a variable with a
12473 complex location. Therefore we don't want to do file i/o for every call.
12474 Therefore we don't want to look for a DWO file on every call.
12475 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12476 then we check if we've already seen DWO_NAME, and only THEN do we check
12477 for a DWO file.
12478
1c658ad5 12479 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12480 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12481
3019eac3 12482static struct dwo_unit *
80626a55
DE
12483lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12484 const char *dwo_name, const char *comp_dir,
12485 ULONGEST signature, int is_debug_types)
3019eac3 12486{
ed2dc618 12487 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12488 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12489 const char *kind = is_debug_types ? "TU" : "CU";
12490 void **dwo_file_slot;
3019eac3 12491 struct dwo_file *dwo_file;
80626a55 12492 struct dwp_file *dwp_file;
cb1df416 12493
6a506a2d
DE
12494 /* First see if there's a DWP file.
12495 If we have a DWP file but didn't find the DWO inside it, don't
12496 look for the original DWO file. It makes gdb behave differently
12497 depending on whether one is debugging in the build tree. */
cf2c3c16 12498
ed2dc618 12499 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12500 if (dwp_file != NULL)
cf2c3c16 12501 {
80626a55
DE
12502 const struct dwp_hash_table *dwp_htab =
12503 is_debug_types ? dwp_file->tus : dwp_file->cus;
12504
12505 if (dwp_htab != NULL)
12506 {
12507 struct dwo_unit *dwo_cutu =
ed2dc618 12508 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12509 signature, is_debug_types);
80626a55
DE
12510
12511 if (dwo_cutu != NULL)
12512 {
b4f54984 12513 if (dwarf_read_debug)
80626a55
DE
12514 {
12515 fprintf_unfiltered (gdb_stdlog,
12516 "Virtual DWO %s %s found: @%s\n",
12517 kind, hex_string (signature),
12518 host_address_to_string (dwo_cutu));
12519 }
12520 return dwo_cutu;
12521 }
12522 }
12523 }
6a506a2d 12524 else
80626a55 12525 {
6a506a2d 12526 /* No DWP file, look for the DWO file. */
80626a55 12527
ed2dc618
SM
12528 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12529 dwo_name, comp_dir);
6a506a2d 12530 if (*dwo_file_slot == NULL)
80626a55 12531 {
6a506a2d
DE
12532 /* Read in the file and build a table of the CUs/TUs it contains. */
12533 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12534 }
6a506a2d 12535 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12536 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12537
6a506a2d 12538 if (dwo_file != NULL)
19c3d4c9 12539 {
6a506a2d
DE
12540 struct dwo_unit *dwo_cutu = NULL;
12541
12542 if (is_debug_types && dwo_file->tus)
12543 {
12544 struct dwo_unit find_dwo_cutu;
12545
12546 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12547 find_dwo_cutu.signature = signature;
9a3c8263 12548 dwo_cutu
b0b6a987
TT
12549 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12550 &find_dwo_cutu);
6a506a2d 12551 }
33c5cd75 12552 else if (!is_debug_types && dwo_file->cus)
80626a55 12553 {
33c5cd75
DB
12554 struct dwo_unit find_dwo_cutu;
12555
12556 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12557 find_dwo_cutu.signature = signature;
b0b6a987 12558 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12559 &find_dwo_cutu);
6a506a2d
DE
12560 }
12561
12562 if (dwo_cutu != NULL)
12563 {
b4f54984 12564 if (dwarf_read_debug)
6a506a2d
DE
12565 {
12566 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12567 kind, dwo_name, hex_string (signature),
12568 host_address_to_string (dwo_cutu));
12569 }
12570 return dwo_cutu;
80626a55
DE
12571 }
12572 }
2e276125 12573 }
9cdd5dbd 12574
80626a55
DE
12575 /* We didn't find it. This could mean a dwo_id mismatch, or
12576 someone deleted the DWO/DWP file, or the search path isn't set up
12577 correctly to find the file. */
12578
b4f54984 12579 if (dwarf_read_debug)
80626a55
DE
12580 {
12581 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12582 kind, dwo_name, hex_string (signature));
12583 }
3019eac3 12584
6656a72d
DE
12585 /* This is a warning and not a complaint because it can be caused by
12586 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12587 {
12588 /* Print the name of the DWP file if we looked there, helps the user
12589 better diagnose the problem. */
791afaa2 12590 std::string dwp_text;
43942612
DE
12591
12592 if (dwp_file != NULL)
791afaa2
TT
12593 dwp_text = string_printf (" [in DWP file %s]",
12594 lbasename (dwp_file->name));
43942612 12595
9d8780f0 12596 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12597 " [in module %s]"),
12598 kind, dwo_name, hex_string (signature),
791afaa2 12599 dwp_text.c_str (),
43942612 12600 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12601 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12602 }
3019eac3 12603 return NULL;
5fb290d7
DJ
12604}
12605
80626a55
DE
12606/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12607 See lookup_dwo_cutu_unit for details. */
12608
12609static struct dwo_unit *
12610lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12611 const char *dwo_name, const char *comp_dir,
12612 ULONGEST signature)
12613{
12614 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12615}
12616
12617/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12618 See lookup_dwo_cutu_unit for details. */
12619
12620static struct dwo_unit *
12621lookup_dwo_type_unit (struct signatured_type *this_tu,
12622 const char *dwo_name, const char *comp_dir)
12623{
12624 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12625}
12626
89e63ee4
DE
12627/* Traversal function for queue_and_load_all_dwo_tus. */
12628
12629static int
12630queue_and_load_dwo_tu (void **slot, void *info)
12631{
12632 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12633 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12634 ULONGEST signature = dwo_unit->signature;
12635 struct signatured_type *sig_type =
12636 lookup_dwo_signatured_type (per_cu->cu, signature);
12637
12638 if (sig_type != NULL)
12639 {
12640 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12641
12642 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12643 a real dependency of PER_CU on SIG_TYPE. That is detected later
12644 while processing PER_CU. */
12645 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12646 load_full_type_unit (sig_cu);
ae640021 12647 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12648 }
12649
12650 return 1;
12651}
12652
12653/* Queue all TUs contained in the DWO of PER_CU to be read in.
12654 The DWO may have the only definition of the type, though it may not be
12655 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12656 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12657
12658static void
12659queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12660{
12661 struct dwo_unit *dwo_unit;
12662 struct dwo_file *dwo_file;
12663
12664 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12665 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12666 gdb_assert (per_cu->cu != NULL);
12667
12668 dwo_unit = per_cu->cu->dwo_unit;
12669 gdb_assert (dwo_unit != NULL);
12670
12671 dwo_file = dwo_unit->dwo_file;
12672 if (dwo_file->tus != NULL)
b0b6a987
TT
12673 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12674 per_cu);
89e63ee4
DE
12675}
12676
3019eac3 12677/* Read in various DIEs. */
348e048f 12678
d389af10 12679/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12680 Inherit only the children of the DW_AT_abstract_origin DIE not being
12681 already referenced by DW_AT_abstract_origin from the children of the
12682 current DIE. */
d389af10
JK
12683
12684static void
12685inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12686{
12687 struct die_info *child_die;
791afaa2 12688 sect_offset *offsetp;
d389af10
JK
12689 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12690 struct die_info *origin_die;
12691 /* Iterator of the ORIGIN_DIE children. */
12692 struct die_info *origin_child_die;
d389af10 12693 struct attribute *attr;
cd02d79d
PA
12694 struct dwarf2_cu *origin_cu;
12695 struct pending **origin_previous_list_in_scope;
d389af10
JK
12696
12697 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12698 if (!attr)
12699 return;
12700
cd02d79d
PA
12701 /* Note that following die references may follow to a die in a
12702 different cu. */
12703
12704 origin_cu = cu;
12705 origin_die = follow_die_ref (die, attr, &origin_cu);
12706
12707 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12708 symbols in. */
12709 origin_previous_list_in_scope = origin_cu->list_in_scope;
12710 origin_cu->list_in_scope = cu->list_in_scope;
12711
edb3359d
DJ
12712 if (die->tag != origin_die->tag
12713 && !(die->tag == DW_TAG_inlined_subroutine
12714 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12715 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12716 sect_offset_str (die->sect_off),
12717 sect_offset_str (origin_die->sect_off));
d389af10 12718
791afaa2 12719 std::vector<sect_offset> offsets;
d389af10 12720
3ea89b92
PMR
12721 for (child_die = die->child;
12722 child_die && child_die->tag;
12723 child_die = sibling_die (child_die))
12724 {
12725 struct die_info *child_origin_die;
12726 struct dwarf2_cu *child_origin_cu;
12727
12728 /* We are trying to process concrete instance entries:
216f72a1 12729 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12730 it's not relevant to our analysis here. i.e. detecting DIEs that are
12731 present in the abstract instance but not referenced in the concrete
12732 one. */
216f72a1
JK
12733 if (child_die->tag == DW_TAG_call_site
12734 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12735 continue;
12736
c38f313d
DJ
12737 /* For each CHILD_DIE, find the corresponding child of
12738 ORIGIN_DIE. If there is more than one layer of
12739 DW_AT_abstract_origin, follow them all; there shouldn't be,
12740 but GCC versions at least through 4.4 generate this (GCC PR
12741 40573). */
3ea89b92
PMR
12742 child_origin_die = child_die;
12743 child_origin_cu = cu;
c38f313d
DJ
12744 while (1)
12745 {
cd02d79d
PA
12746 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12747 child_origin_cu);
c38f313d
DJ
12748 if (attr == NULL)
12749 break;
cd02d79d
PA
12750 child_origin_die = follow_die_ref (child_origin_die, attr,
12751 &child_origin_cu);
c38f313d
DJ
12752 }
12753
d389af10
JK
12754 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12755 counterpart may exist. */
c38f313d 12756 if (child_origin_die != child_die)
d389af10 12757 {
edb3359d
DJ
12758 if (child_die->tag != child_origin_die->tag
12759 && !(child_die->tag == DW_TAG_inlined_subroutine
12760 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12761 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12762 "different tags"),
9d8780f0
SM
12763 sect_offset_str (child_die->sect_off),
12764 sect_offset_str (child_origin_die->sect_off));
c38f313d 12765 if (child_origin_die->parent != origin_die)
b98664d3 12766 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12767 "different parents"),
9d8780f0
SM
12768 sect_offset_str (child_die->sect_off),
12769 sect_offset_str (child_origin_die->sect_off));
c38f313d 12770 else
791afaa2 12771 offsets.push_back (child_origin_die->sect_off);
d389af10 12772 }
d389af10 12773 }
791afaa2
TT
12774 std::sort (offsets.begin (), offsets.end ());
12775 sect_offset *offsets_end = offsets.data () + offsets.size ();
12776 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12777 if (offsetp[-1] == *offsetp)
b98664d3 12778 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12779 "to DIE %s as their abstract origin"),
12780 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12781
791afaa2 12782 offsetp = offsets.data ();
d389af10
JK
12783 origin_child_die = origin_die->child;
12784 while (origin_child_die && origin_child_die->tag)
12785 {
12786 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12787 while (offsetp < offsets_end
9c541725 12788 && *offsetp < origin_child_die->sect_off)
d389af10 12789 offsetp++;
b64f50a1 12790 if (offsetp >= offsets_end
9c541725 12791 || *offsetp > origin_child_die->sect_off)
d389af10 12792 {
adde2bff
DE
12793 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12794 Check whether we're already processing ORIGIN_CHILD_DIE.
12795 This can happen with mutually referenced abstract_origins.
12796 PR 16581. */
12797 if (!origin_child_die->in_process)
12798 process_die (origin_child_die, origin_cu);
d389af10
JK
12799 }
12800 origin_child_die = sibling_die (origin_child_die);
12801 }
cd02d79d 12802 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12803
12804 if (cu != origin_cu)
12805 compute_delayed_physnames (origin_cu);
d389af10
JK
12806}
12807
c906108c 12808static void
e7c27a73 12809read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12810{
518817b3 12811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12812 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12813 struct context_stack *newobj;
c906108c
SS
12814 CORE_ADDR lowpc;
12815 CORE_ADDR highpc;
12816 struct die_info *child_die;
edb3359d 12817 struct attribute *attr, *call_line, *call_file;
15d034d0 12818 const char *name;
e142c38c 12819 CORE_ADDR baseaddr;
801e3a5b 12820 struct block *block;
edb3359d 12821 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12822 std::vector<struct symbol *> template_args;
34eaf542 12823 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12824
12825 if (inlined_func)
12826 {
12827 /* If we do not have call site information, we can't show the
12828 caller of this inlined function. That's too confusing, so
12829 only use the scope for local variables. */
12830 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12831 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12832 if (call_line == NULL || call_file == NULL)
12833 {
12834 read_lexical_block_scope (die, cu);
12835 return;
12836 }
12837 }
c906108c 12838
b3b3bada 12839 baseaddr = objfile->text_section_offset ();
e142c38c 12840
94af9270 12841 name = dwarf2_name (die, cu);
c906108c 12842
e8d05480
JB
12843 /* Ignore functions with missing or empty names. These are actually
12844 illegal according to the DWARF standard. */
12845 if (name == NULL)
12846 {
b98664d3 12847 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12848 sect_offset_str (die->sect_off));
e8d05480
JB
12849 return;
12850 }
12851
12852 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12853 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12854 <= PC_BOUNDS_INVALID)
e8d05480 12855 {
ae4d0c03
PM
12856 attr = dwarf2_attr (die, DW_AT_external, cu);
12857 if (!attr || !DW_UNSND (attr))
b98664d3 12858 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12859 "for subprogram DIE at %s"),
12860 sect_offset_str (die->sect_off));
e8d05480
JB
12861 return;
12862 }
c906108c 12863
3e29f34a
MR
12864 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12865 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12866
34eaf542
TT
12867 /* If we have any template arguments, then we must allocate a
12868 different sort of symbol. */
12869 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12870 {
12871 if (child_die->tag == DW_TAG_template_type_param
12872 || child_die->tag == DW_TAG_template_value_param)
12873 {
e623cf5d 12874 templ_func = allocate_template_symbol (objfile);
cf724bc9 12875 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12876 break;
12877 }
12878 }
12879
c24bdb02 12880 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12881 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12882 (struct symbol *) templ_func);
4c2df51b 12883
81873cc8 12884 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12885 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12886 cu->language);
12887
4cecd739
DJ
12888 /* If there is a location expression for DW_AT_frame_base, record
12889 it. */
e142c38c 12890 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12891 if (attr != nullptr)
fe978cb0 12892 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12893
63e43d3a
PMR
12894 /* If there is a location for the static link, record it. */
12895 newobj->static_link = NULL;
12896 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12897 if (attr != nullptr)
63e43d3a 12898 {
224c3ddb
SM
12899 newobj->static_link
12900 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12901 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12902 cu->per_cu->addr_type ());
63e43d3a
PMR
12903 }
12904
c24bdb02 12905 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12906
639d11d3 12907 if (die->child != NULL)
c906108c 12908 {
639d11d3 12909 child_die = die->child;
c906108c
SS
12910 while (child_die && child_die->tag)
12911 {
34eaf542
TT
12912 if (child_die->tag == DW_TAG_template_type_param
12913 || child_die->tag == DW_TAG_template_value_param)
12914 {
12915 struct symbol *arg = new_symbol (child_die, NULL, cu);
12916
f1078f66 12917 if (arg != NULL)
2f4732b0 12918 template_args.push_back (arg);
34eaf542
TT
12919 }
12920 else
12921 process_die (child_die, cu);
c906108c
SS
12922 child_die = sibling_die (child_die);
12923 }
12924 }
12925
d389af10
JK
12926 inherit_abstract_dies (die, cu);
12927
4a811a97
UW
12928 /* If we have a DW_AT_specification, we might need to import using
12929 directives from the context of the specification DIE. See the
12930 comment in determine_prefix. */
12931 if (cu->language == language_cplus
12932 && dwarf2_attr (die, DW_AT_specification, cu))
12933 {
12934 struct dwarf2_cu *spec_cu = cu;
12935 struct die_info *spec_die = die_specification (die, &spec_cu);
12936
12937 while (spec_die)
12938 {
12939 child_die = spec_die->child;
12940 while (child_die && child_die->tag)
12941 {
12942 if (child_die->tag == DW_TAG_imported_module)
12943 process_die (child_die, spec_cu);
12944 child_die = sibling_die (child_die);
12945 }
12946
12947 /* In some cases, GCC generates specification DIEs that
12948 themselves contain DW_AT_specification attributes. */
12949 spec_die = die_specification (spec_die, &spec_cu);
12950 }
12951 }
12952
c24bdb02 12953 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12954 /* Make a block for the local symbols within. */
c24bdb02 12955 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12956 cstk.static_link, lowpc, highpc);
801e3a5b 12957
df8a16a1 12958 /* For C++, set the block's scope. */
45280282
IB
12959 if ((cu->language == language_cplus
12960 || cu->language == language_fortran
c44af4eb
TT
12961 || cu->language == language_d
12962 || cu->language == language_rust)
4d4ec4e5 12963 && cu->processing_has_namespace_info)
195a3f6c
TT
12964 block_set_scope (block, determine_prefix (die, cu),
12965 &objfile->objfile_obstack);
df8a16a1 12966
801e3a5b
JB
12967 /* If we have address ranges, record them. */
12968 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12969
a60f3166 12970 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12971
34eaf542 12972 /* Attach template arguments to function. */
2f4732b0 12973 if (!template_args.empty ())
34eaf542
TT
12974 {
12975 gdb_assert (templ_func != NULL);
12976
2f4732b0 12977 templ_func->n_template_arguments = template_args.size ();
34eaf542 12978 templ_func->template_arguments
8d749320
SM
12979 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12980 templ_func->n_template_arguments);
34eaf542 12981 memcpy (templ_func->template_arguments,
2f4732b0 12982 template_args.data (),
34eaf542 12983 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
12984
12985 /* Make sure that the symtab is set on the new symbols. Even
12986 though they don't appear in this symtab directly, other parts
12987 of gdb assume that symbols do, and this is reasonably
12988 true. */
8634679f 12989 for (symbol *sym : template_args)
3e1d3d8c 12990 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
12991 }
12992
208d8187
JB
12993 /* In C++, we can have functions nested inside functions (e.g., when
12994 a function declares a class that has methods). This means that
12995 when we finish processing a function scope, we may need to go
12996 back to building a containing block's symbol lists. */
c24bdb02
KS
12997 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12998 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 12999
921e78cf
JB
13000 /* If we've finished processing a top-level function, subsequent
13001 symbols go in the file symbol list. */
c24bdb02
KS
13002 if (cu->get_builder ()->outermost_context_p ())
13003 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13004}
13005
13006/* Process all the DIES contained within a lexical block scope. Start
13007 a new scope, process the dies, and then close the scope. */
13008
13009static void
e7c27a73 13010read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13011{
518817b3 13012 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13013 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13014 CORE_ADDR lowpc, highpc;
13015 struct die_info *child_die;
e142c38c
DJ
13016 CORE_ADDR baseaddr;
13017
b3b3bada 13018 baseaddr = objfile->text_section_offset ();
c906108c
SS
13019
13020 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13021 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13022 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13023 be nasty. Might be easier to properly extend generic blocks to
af34e669 13024 describe ranges. */
e385593e
JK
13025 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13026 {
13027 case PC_BOUNDS_NOT_PRESENT:
13028 /* DW_TAG_lexical_block has no attributes, process its children as if
13029 there was no wrapping by that DW_TAG_lexical_block.
13030 GCC does no longer produces such DWARF since GCC r224161. */
13031 for (child_die = die->child;
13032 child_die != NULL && child_die->tag;
13033 child_die = sibling_die (child_die))
13034 process_die (child_die, cu);
13035 return;
13036 case PC_BOUNDS_INVALID:
13037 return;
13038 }
3e29f34a
MR
13039 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13040 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13041
c24bdb02 13042 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13043 if (die->child != NULL)
c906108c 13044 {
639d11d3 13045 child_die = die->child;
c906108c
SS
13046 while (child_die && child_die->tag)
13047 {
e7c27a73 13048 process_die (child_die, cu);
c906108c
SS
13049 child_die = sibling_die (child_die);
13050 }
13051 }
3ea89b92 13052 inherit_abstract_dies (die, cu);
c24bdb02 13053 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13054
c24bdb02
KS
13055 if (*cu->get_builder ()->get_local_symbols () != NULL
13056 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13057 {
801e3a5b 13058 struct block *block
c24bdb02 13059 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13060 cstk.start_addr, highpc);
801e3a5b
JB
13061
13062 /* Note that recording ranges after traversing children, as we
13063 do here, means that recording a parent's ranges entails
13064 walking across all its children's ranges as they appear in
13065 the address map, which is quadratic behavior.
13066
13067 It would be nicer to record the parent's ranges before
13068 traversing its children, simply overriding whatever you find
13069 there. But since we don't even decide whether to create a
13070 block until after we've traversed its children, that's hard
13071 to do. */
13072 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13073 }
c24bdb02
KS
13074 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13075 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13076}
13077
216f72a1 13078/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13079
13080static void
13081read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13082{
518817b3 13083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13084 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13085 CORE_ADDR pc, baseaddr;
13086 struct attribute *attr;
13087 struct call_site *call_site, call_site_local;
13088 void **slot;
13089 int nparams;
13090 struct die_info *child_die;
13091
b3b3bada 13092 baseaddr = objfile->text_section_offset ();
96408a79 13093
216f72a1
JK
13094 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13095 if (attr == NULL)
13096 {
13097 /* This was a pre-DWARF-5 GNU extension alias
13098 for DW_AT_call_return_pc. */
13099 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13100 }
96408a79
SA
13101 if (!attr)
13102 {
b98664d3 13103 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13104 "DIE %s [in module %s]"),
13105 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13106 return;
13107 }
cd6c91b4 13108 pc = attr->value_as_address () + baseaddr;
3e29f34a 13109 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13110
13111 if (cu->call_site_htab == NULL)
13112 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13113 NULL, &objfile->objfile_obstack,
13114 hashtab_obstack_allocate, NULL);
13115 call_site_local.pc = pc;
13116 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13117 if (*slot != NULL)
13118 {
b98664d3 13119 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13120 "DIE %s [in module %s]"),
13121 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13122 objfile_name (objfile));
96408a79
SA
13123 return;
13124 }
13125
13126 /* Count parameters at the caller. */
13127
13128 nparams = 0;
13129 for (child_die = die->child; child_die && child_die->tag;
13130 child_die = sibling_die (child_die))
13131 {
216f72a1
JK
13132 if (child_die->tag != DW_TAG_call_site_parameter
13133 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13134 {
b98664d3 13135 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13136 "DW_TAG_call_site child DIE %s [in module %s]"),
13137 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13138 objfile_name (objfile));
96408a79
SA
13139 continue;
13140 }
13141
13142 nparams++;
13143 }
13144
224c3ddb
SM
13145 call_site
13146 = ((struct call_site *)
13147 obstack_alloc (&objfile->objfile_obstack,
13148 sizeof (*call_site)
13149 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13150 *slot = call_site;
13151 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13152 call_site->pc = pc;
13153
216f72a1
JK
13154 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13155 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13156 {
13157 struct die_info *func_die;
13158
13159 /* Skip also over DW_TAG_inlined_subroutine. */
13160 for (func_die = die->parent;
13161 func_die && func_die->tag != DW_TAG_subprogram
13162 && func_die->tag != DW_TAG_subroutine_type;
13163 func_die = func_die->parent);
13164
216f72a1
JK
13165 /* DW_AT_call_all_calls is a superset
13166 of DW_AT_call_all_tail_calls. */
96408a79 13167 if (func_die
216f72a1 13168 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13169 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13170 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13171 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13172 {
13173 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13174 not complete. But keep CALL_SITE for look ups via call_site_htab,
13175 both the initial caller containing the real return address PC and
13176 the final callee containing the current PC of a chain of tail
13177 calls do not need to have the tail call list complete. But any
13178 function candidate for a virtual tail call frame searched via
13179 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13180 determined unambiguously. */
13181 }
13182 else
13183 {
13184 struct type *func_type = NULL;
13185
13186 if (func_die)
13187 func_type = get_die_type (func_die, cu);
13188 if (func_type != NULL)
13189 {
13190 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13191
13192 /* Enlist this call site to the function. */
13193 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13194 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13195 }
13196 else
b98664d3 13197 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13198 "DIE %s [in module %s]"),
13199 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13200 }
13201 }
13202
216f72a1
JK
13203 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13204 if (attr == NULL)
13205 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13206 if (attr == NULL)
13207 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13208 if (attr == NULL)
216f72a1
JK
13209 {
13210 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13211 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13212 }
96408a79 13213 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13214 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13215 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13216 else if (attr->form_is_block ())
96408a79
SA
13217 {
13218 struct dwarf2_locexpr_baton *dlbaton;
13219
8d749320 13220 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13221 dlbaton->data = DW_BLOCK (attr)->data;
13222 dlbaton->size = DW_BLOCK (attr)->size;
13223 dlbaton->per_cu = cu->per_cu;
13224
13225 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13226 }
cd6c91b4 13227 else if (attr->form_is_ref ())
96408a79 13228 {
96408a79
SA
13229 struct dwarf2_cu *target_cu = cu;
13230 struct die_info *target_die;
13231
ac9ec31b 13232 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13233 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13234 if (die_is_declaration (target_die, target_cu))
13235 {
7d45c7c3 13236 const char *target_physname;
9112db09
JK
13237
13238 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13239 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13240 if (target_physname == NULL)
9112db09 13241 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13242 if (target_physname == NULL)
b98664d3 13243 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13244 "physname, for referencing DIE %s [in module %s]"),
13245 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13246 else
7d455152 13247 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13248 }
13249 else
13250 {
13251 CORE_ADDR lowpc;
13252
13253 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13254 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13255 <= PC_BOUNDS_INVALID)
b98664d3 13256 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13257 "low pc, for referencing DIE %s [in module %s]"),
13258 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13259 else
3e29f34a
MR
13260 {
13261 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13262 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13263 }
96408a79
SA
13264 }
13265 }
13266 else
b98664d3 13267 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13268 "block nor reference, for DIE %s [in module %s]"),
13269 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13270
13271 call_site->per_cu = cu->per_cu;
13272
13273 for (child_die = die->child;
13274 child_die && child_die->tag;
13275 child_die = sibling_die (child_die))
13276 {
96408a79 13277 struct call_site_parameter *parameter;
1788b2d3 13278 struct attribute *loc, *origin;
96408a79 13279
216f72a1
JK
13280 if (child_die->tag != DW_TAG_call_site_parameter
13281 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13282 {
13283 /* Already printed the complaint above. */
13284 continue;
13285 }
13286
13287 gdb_assert (call_site->parameter_count < nparams);
13288 parameter = &call_site->parameter[call_site->parameter_count];
13289
1788b2d3
JK
13290 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13291 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13292 register is contained in DW_AT_call_value. */
96408a79 13293
24c5c679 13294 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13295 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13296 if (origin == NULL)
13297 {
13298 /* This was a pre-DWARF-5 GNU extension alias
13299 for DW_AT_call_parameter. */
13300 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13301 }
cd6c91b4 13302 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13303 {
1788b2d3 13304 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13305
13306 sect_offset sect_off
13307 = (sect_offset) dwarf2_get_ref_die_offset (origin);
4057dfde 13308 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13309 {
13310 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13311 binding can be done only inside one CU. Such referenced DIE
13312 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13313 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13314 "DW_TAG_call_site child DIE %s [in module %s]"),
13315 sect_offset_str (child_die->sect_off),
9c541725 13316 objfile_name (objfile));
d76b7dbc
JK
13317 continue;
13318 }
9c541725
PA
13319 parameter->u.param_cu_off
13320 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13321 }
4fc6c0d5 13322 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13323 {
b98664d3 13324 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13325 "DW_TAG_call_site child DIE %s [in module %s]"),
13326 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13327 continue;
13328 }
24c5c679 13329 else
96408a79 13330 {
24c5c679
JK
13331 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13332 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13333 if (parameter->u.dwarf_reg != -1)
13334 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13335 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13336 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13337 &parameter->u.fb_offset))
13338 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13339 else
13340 {
b98664d3 13341 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13342 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13343 "DW_TAG_call_site child DIE %s "
24c5c679 13344 "[in module %s]"),
9d8780f0 13345 sect_offset_str (child_die->sect_off),
9c541725 13346 objfile_name (objfile));
24c5c679
JK
13347 continue;
13348 }
96408a79
SA
13349 }
13350
216f72a1
JK
13351 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13352 if (attr == NULL)
13353 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13354 if (attr == NULL || !attr->form_is_block ())
96408a79 13355 {
b98664d3 13356 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13357 "DW_TAG_call_site child DIE %s [in module %s]"),
13358 sect_offset_str (child_die->sect_off),
9c541725 13359 objfile_name (objfile));
96408a79
SA
13360 continue;
13361 }
13362 parameter->value = DW_BLOCK (attr)->data;
13363 parameter->value_size = DW_BLOCK (attr)->size;
13364
13365 /* Parameters are not pre-cleared by memset above. */
13366 parameter->data_value = NULL;
13367 parameter->data_value_size = 0;
13368 call_site->parameter_count++;
13369
216f72a1
JK
13370 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13371 if (attr == NULL)
13372 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13373 if (attr != nullptr)
96408a79 13374 {
4fc6c0d5 13375 if (!attr->form_is_block ())
b98664d3 13376 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13377 "DW_TAG_call_site child DIE %s [in module %s]"),
13378 sect_offset_str (child_die->sect_off),
9c541725 13379 objfile_name (objfile));
96408a79
SA
13380 else
13381 {
13382 parameter->data_value = DW_BLOCK (attr)->data;
13383 parameter->data_value_size = DW_BLOCK (attr)->size;
13384 }
13385 }
13386 }
13387}
13388
71a3c369
TT
13389/* Helper function for read_variable. If DIE represents a virtual
13390 table, then return the type of the concrete object that is
13391 associated with the virtual table. Otherwise, return NULL. */
13392
13393static struct type *
13394rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13395{
13396 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13397 if (attr == NULL)
13398 return NULL;
13399
13400 /* Find the type DIE. */
13401 struct die_info *type_die = NULL;
13402 struct dwarf2_cu *type_cu = cu;
13403
cd6c91b4 13404 if (attr->form_is_ref ())
71a3c369
TT
13405 type_die = follow_die_ref (die, attr, &type_cu);
13406 if (type_die == NULL)
13407 return NULL;
13408
13409 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13410 return NULL;
13411 return die_containing_type (type_die, type_cu);
13412}
13413
13414/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13415
13416static void
13417read_variable (struct die_info *die, struct dwarf2_cu *cu)
13418{
13419 struct rust_vtable_symbol *storage = NULL;
13420
13421 if (cu->language == language_rust)
13422 {
13423 struct type *containing_type = rust_containing_type (die, cu);
13424
13425 if (containing_type != NULL)
13426 {
518817b3 13427 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13428
468c0cbb 13429 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13430 initialize_objfile_symbol (storage);
13431 storage->concrete_type = containing_type;
cf724bc9 13432 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13433 }
13434 }
13435
e4a62c65
TV
13436 struct symbol *res = new_symbol (die, NULL, cu, storage);
13437 struct attribute *abstract_origin
13438 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13439 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13440 if (res == NULL && loc && abstract_origin)
13441 {
13442 /* We have a variable without a name, but with a location and an abstract
13443 origin. This may be a concrete instance of an abstract variable
13444 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13445 later. */
13446 struct dwarf2_cu *origin_cu = cu;
13447 struct die_info *origin_die
13448 = follow_die_ref (die, abstract_origin, &origin_cu);
13449 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13450 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13451 }
71a3c369
TT
13452}
13453
43988095
JK
13454/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13455 reading .debug_rnglists.
13456 Callback's type should be:
13457 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13458 Return true if the attributes are present and valid, otherwise,
13459 return false. */
13460
13461template <typename Callback>
13462static bool
13463dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13464 Callback &&callback)
13465{
ed2dc618 13466 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13467 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13468 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13469 bfd *obfd = objfile->obfd;
43988095
JK
13470 /* Base address selection entry. */
13471 CORE_ADDR base;
13472 int found_base;
43988095 13473 const gdb_byte *buffer;
43988095
JK
13474 CORE_ADDR baseaddr;
13475 bool overflow = false;
13476
13477 found_base = cu->base_known;
13478 base = cu->base_address;
13479
96b79293 13480 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13481 if (offset >= dwarf2_per_objfile->rnglists.size)
13482 {
b98664d3 13483 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13484 offset);
13485 return false;
13486 }
13487 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13488
b3b3bada 13489 baseaddr = objfile->text_section_offset ();
43988095
JK
13490
13491 while (1)
13492 {
7814882a
JK
13493 /* Initialize it due to a false compiler warning. */
13494 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13495 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13496 + dwarf2_per_objfile->rnglists.size);
13497 unsigned int bytes_read;
13498
13499 if (buffer == buf_end)
13500 {
13501 overflow = true;
13502 break;
13503 }
13504 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13505 switch (rlet)
13506 {
13507 case DW_RLE_end_of_list:
13508 break;
13509 case DW_RLE_base_address:
13510 if (buffer + cu->header.addr_size > buf_end)
13511 {
13512 overflow = true;
13513 break;
13514 }
c8a7a66f 13515 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13516 found_base = 1;
13517 buffer += bytes_read;
13518 break;
13519 case DW_RLE_start_length:
13520 if (buffer + cu->header.addr_size > buf_end)
13521 {
13522 overflow = true;
13523 break;
13524 }
c8a7a66f
TT
13525 range_beginning = cu->header.read_address (obfd, buffer,
13526 &bytes_read);
43988095
JK
13527 buffer += bytes_read;
13528 range_end = (range_beginning
13529 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13530 buffer += bytes_read;
13531 if (buffer > buf_end)
13532 {
13533 overflow = true;
13534 break;
13535 }
13536 break;
13537 case DW_RLE_offset_pair:
13538 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13539 buffer += bytes_read;
13540 if (buffer > buf_end)
13541 {
13542 overflow = true;
13543 break;
13544 }
13545 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13546 buffer += bytes_read;
13547 if (buffer > buf_end)
13548 {
13549 overflow = true;
13550 break;
13551 }
13552 break;
13553 case DW_RLE_start_end:
13554 if (buffer + 2 * cu->header.addr_size > buf_end)
13555 {
13556 overflow = true;
13557 break;
13558 }
c8a7a66f
TT
13559 range_beginning = cu->header.read_address (obfd, buffer,
13560 &bytes_read);
43988095 13561 buffer += bytes_read;
c8a7a66f 13562 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13563 buffer += bytes_read;
13564 break;
13565 default:
b98664d3 13566 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13567 return false;
13568 }
13569 if (rlet == DW_RLE_end_of_list || overflow)
13570 break;
13571 if (rlet == DW_RLE_base_address)
13572 continue;
13573
13574 if (!found_base)
13575 {
13576 /* We have no valid base address for the ranges
13577 data. */
b98664d3 13578 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13579 return false;
13580 }
13581
13582 if (range_beginning > range_end)
13583 {
13584 /* Inverted range entries are invalid. */
b98664d3 13585 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13586 return false;
13587 }
13588
13589 /* Empty range entries have no effect. */
13590 if (range_beginning == range_end)
13591 continue;
13592
13593 range_beginning += base;
13594 range_end += base;
13595
13596 /* A not-uncommon case of bad debug info.
13597 Don't pollute the addrmap with bad data. */
13598 if (range_beginning + baseaddr == 0
13599 && !dwarf2_per_objfile->has_section_at_zero)
13600 {
b98664d3 13601 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13602 " [in module %s]"), objfile_name (objfile));
13603 continue;
13604 }
13605
13606 callback (range_beginning, range_end);
13607 }
13608
13609 if (overflow)
13610 {
b98664d3 13611 complaint (_("Offset %d is not terminated "
43988095
JK
13612 "for DW_AT_ranges attribute"),
13613 offset);
13614 return false;
13615 }
13616
13617 return true;
13618}
13619
13620/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13621 Callback's type should be:
13622 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13623 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13624
43988095 13625template <typename Callback>
43039443 13626static int
5f46c5a5 13627dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13628 Callback &&callback)
43039443 13629{
ed2dc618 13630 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13631 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13632 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13633 struct comp_unit_head *cu_header = &cu->header;
13634 bfd *obfd = objfile->obfd;
13635 unsigned int addr_size = cu_header->addr_size;
13636 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13637 /* Base address selection entry. */
13638 CORE_ADDR base;
13639 int found_base;
13640 unsigned int dummy;
d521ce57 13641 const gdb_byte *buffer;
ff013f42 13642 CORE_ADDR baseaddr;
43039443 13643
43988095
JK
13644 if (cu_header->version >= 5)
13645 return dwarf2_rnglists_process (offset, cu, callback);
13646
d00adf39
DE
13647 found_base = cu->base_known;
13648 base = cu->base_address;
43039443 13649
96b79293 13650 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13651 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13652 {
b98664d3 13653 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13654 offset);
13655 return 0;
13656 }
dce234bc 13657 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13658
b3b3bada 13659 baseaddr = objfile->text_section_offset ();
ff013f42 13660
43039443
JK
13661 while (1)
13662 {
13663 CORE_ADDR range_beginning, range_end;
13664
c8a7a66f 13665 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13666 buffer += addr_size;
c8a7a66f 13667 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13668 buffer += addr_size;
13669 offset += 2 * addr_size;
13670
13671 /* An end of list marker is a pair of zero addresses. */
13672 if (range_beginning == 0 && range_end == 0)
13673 /* Found the end of list entry. */
13674 break;
13675
13676 /* Each base address selection entry is a pair of 2 values.
13677 The first is the largest possible address, the second is
13678 the base address. Check for a base address here. */
13679 if ((range_beginning & mask) == mask)
13680 {
28d2bfb9
AB
13681 /* If we found the largest possible address, then we already
13682 have the base address in range_end. */
13683 base = range_end;
43039443
JK
13684 found_base = 1;
13685 continue;
13686 }
13687
13688 if (!found_base)
13689 {
13690 /* We have no valid base address for the ranges
13691 data. */
b98664d3 13692 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13693 return 0;
13694 }
13695
9277c30c
UW
13696 if (range_beginning > range_end)
13697 {
13698 /* Inverted range entries are invalid. */
b98664d3 13699 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13700 return 0;
13701 }
13702
13703 /* Empty range entries have no effect. */
13704 if (range_beginning == range_end)
13705 continue;
13706
43039443
JK
13707 range_beginning += base;
13708 range_end += base;
13709
01093045
DE
13710 /* A not-uncommon case of bad debug info.
13711 Don't pollute the addrmap with bad data. */
13712 if (range_beginning + baseaddr == 0
13713 && !dwarf2_per_objfile->has_section_at_zero)
13714 {
b98664d3 13715 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13716 " [in module %s]"), objfile_name (objfile));
01093045
DE
13717 continue;
13718 }
13719
5f46c5a5
JK
13720 callback (range_beginning, range_end);
13721 }
13722
13723 return 1;
13724}
13725
13726/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13727 Return 1 if the attributes are present and valid, otherwise, return 0.
13728 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13729
13730static int
13731dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13732 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13733 dwarf2_psymtab *ranges_pst)
5f46c5a5 13734{
518817b3 13735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13736 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13737 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13738 int low_set = 0;
13739 CORE_ADDR low = 0;
13740 CORE_ADDR high = 0;
13741 int retval;
13742
13743 retval = dwarf2_ranges_process (offset, cu,
13744 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13745 {
9277c30c 13746 if (ranges_pst != NULL)
3e29f34a
MR
13747 {
13748 CORE_ADDR lowpc;
13749 CORE_ADDR highpc;
13750
79748972
TT
13751 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13752 range_beginning + baseaddr)
13753 - baseaddr);
13754 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13755 range_end + baseaddr)
13756 - baseaddr);
d320c2b5
TT
13757 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13758 lowpc, highpc - 1, ranges_pst);
3e29f34a 13759 }
ff013f42 13760
43039443
JK
13761 /* FIXME: This is recording everything as a low-high
13762 segment of consecutive addresses. We should have a
13763 data structure for discontiguous block ranges
13764 instead. */
13765 if (! low_set)
13766 {
13767 low = range_beginning;
13768 high = range_end;
13769 low_set = 1;
13770 }
13771 else
13772 {
13773 if (range_beginning < low)
13774 low = range_beginning;
13775 if (range_end > high)
13776 high = range_end;
13777 }
5f46c5a5
JK
13778 });
13779 if (!retval)
13780 return 0;
43039443
JK
13781
13782 if (! low_set)
13783 /* If the first entry is an end-of-list marker, the range
13784 describes an empty scope, i.e. no instructions. */
13785 return 0;
13786
13787 if (low_return)
13788 *low_return = low;
13789 if (high_return)
13790 *high_return = high;
13791 return 1;
13792}
13793
3a2b436a
JK
13794/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13795 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13796 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13797
3a2b436a 13798static enum pc_bounds_kind
af34e669 13799dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13800 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13801 dwarf2_psymtab *pst)
c906108c 13802{
518817b3
SM
13803 struct dwarf2_per_objfile *dwarf2_per_objfile
13804 = cu->per_cu->dwarf2_per_objfile;
c906108c 13805 struct attribute *attr;
91da1414 13806 struct attribute *attr_high;
af34e669
DJ
13807 CORE_ADDR low = 0;
13808 CORE_ADDR high = 0;
e385593e 13809 enum pc_bounds_kind ret;
c906108c 13810
91da1414
MW
13811 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13812 if (attr_high)
af34e669 13813 {
e142c38c 13814 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13815 if (attr != nullptr)
91da1414 13816 {
cd6c91b4
TT
13817 low = attr->value_as_address ();
13818 high = attr_high->value_as_address ();
13819 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13820 high += low;
91da1414 13821 }
af34e669
DJ
13822 else
13823 /* Found high w/o low attribute. */
e385593e 13824 return PC_BOUNDS_INVALID;
af34e669
DJ
13825
13826 /* Found consecutive range of addresses. */
3a2b436a 13827 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13828 }
c906108c 13829 else
af34e669 13830 {
e142c38c 13831 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13832 if (attr != NULL)
13833 {
18a8505e 13834 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13835 We take advantage of the fact that DW_AT_ranges does not appear
13836 in DW_TAG_compile_unit of DWO files. */
13837 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13838 unsigned int ranges_offset = (DW_UNSND (attr)
13839 + (need_ranges_base
13840 ? cu->ranges_base
13841 : 0));
2e3cf129 13842
af34e669 13843 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13844 .debug_ranges section. */
2e3cf129 13845 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13846 return PC_BOUNDS_INVALID;
43039443 13847 /* Found discontinuous range of addresses. */
3a2b436a 13848 ret = PC_BOUNDS_RANGES;
af34e669 13849 }
e385593e
JK
13850 else
13851 return PC_BOUNDS_NOT_PRESENT;
af34e669 13852 }
c906108c 13853
48fbe735 13854 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13855 if (high <= low)
e385593e 13856 return PC_BOUNDS_INVALID;
c906108c
SS
13857
13858 /* When using the GNU linker, .gnu.linkonce. sections are used to
13859 eliminate duplicate copies of functions and vtables and such.
13860 The linker will arbitrarily choose one and discard the others.
13861 The AT_*_pc values for such functions refer to local labels in
13862 these sections. If the section from that file was discarded, the
13863 labels are not in the output, so the relocs get a value of 0.
13864 If this is a discarded function, mark the pc bounds as invalid,
13865 so that GDB will ignore it. */
72dca2f5 13866 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13867 return PC_BOUNDS_INVALID;
c906108c
SS
13868
13869 *lowpc = low;
96408a79
SA
13870 if (highpc)
13871 *highpc = high;
af34e669 13872 return ret;
c906108c
SS
13873}
13874
b084d499
JB
13875/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13876 its low and high PC addresses. Do nothing if these addresses could not
13877 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13878 and HIGHPC to the high address if greater than HIGHPC. */
13879
13880static void
13881dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13882 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13883 struct dwarf2_cu *cu)
13884{
13885 CORE_ADDR low, high;
13886 struct die_info *child = die->child;
13887
e385593e 13888 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13889 {
325fac50
PA
13890 *lowpc = std::min (*lowpc, low);
13891 *highpc = std::max (*highpc, high);
b084d499
JB
13892 }
13893
13894 /* If the language does not allow nested subprograms (either inside
13895 subprograms or lexical blocks), we're done. */
13896 if (cu->language != language_ada)
13897 return;
6e70227d 13898
b084d499
JB
13899 /* Check all the children of the given DIE. If it contains nested
13900 subprograms, then check their pc bounds. Likewise, we need to
13901 check lexical blocks as well, as they may also contain subprogram
13902 definitions. */
13903 while (child && child->tag)
13904 {
13905 if (child->tag == DW_TAG_subprogram
13906 || child->tag == DW_TAG_lexical_block)
13907 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13908 child = sibling_die (child);
13909 }
13910}
13911
fae299cd
DC
13912/* Get the low and high pc's represented by the scope DIE, and store
13913 them in *LOWPC and *HIGHPC. If the correct values can't be
13914 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13915
13916static void
13917get_scope_pc_bounds (struct die_info *die,
13918 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13919 struct dwarf2_cu *cu)
13920{
13921 CORE_ADDR best_low = (CORE_ADDR) -1;
13922 CORE_ADDR best_high = (CORE_ADDR) 0;
13923 CORE_ADDR current_low, current_high;
13924
3a2b436a 13925 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13926 >= PC_BOUNDS_RANGES)
fae299cd
DC
13927 {
13928 best_low = current_low;
13929 best_high = current_high;
13930 }
13931 else
13932 {
13933 struct die_info *child = die->child;
13934
13935 while (child && child->tag)
13936 {
13937 switch (child->tag) {
13938 case DW_TAG_subprogram:
b084d499 13939 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13940 break;
13941 case DW_TAG_namespace:
f55ee35c 13942 case DW_TAG_module:
fae299cd
DC
13943 /* FIXME: carlton/2004-01-16: Should we do this for
13944 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13945 that current GCC's always emit the DIEs corresponding
13946 to definitions of methods of classes as children of a
13947 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13948 the DIEs giving the declarations, which could be
13949 anywhere). But I don't see any reason why the
13950 standards says that they have to be there. */
13951 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13952
13953 if (current_low != ((CORE_ADDR) -1))
13954 {
325fac50
PA
13955 best_low = std::min (best_low, current_low);
13956 best_high = std::max (best_high, current_high);
fae299cd
DC
13957 }
13958 break;
13959 default:
0963b4bd 13960 /* Ignore. */
fae299cd
DC
13961 break;
13962 }
13963
13964 child = sibling_die (child);
13965 }
13966 }
13967
13968 *lowpc = best_low;
13969 *highpc = best_high;
13970}
13971
801e3a5b
JB
13972/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13973 in DIE. */
380bca97 13974
801e3a5b
JB
13975static void
13976dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13977 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13978{
518817b3 13979 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13980 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13981 struct attribute *attr;
91da1414 13982 struct attribute *attr_high;
801e3a5b 13983
91da1414
MW
13984 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13985 if (attr_high)
801e3a5b 13986 {
801e3a5b 13987 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13988 if (attr != nullptr)
801e3a5b 13989 {
cd6c91b4
TT
13990 CORE_ADDR low = attr->value_as_address ();
13991 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 13992
cd6c91b4 13993 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13994 high += low;
9a619af0 13995
3e29f34a
MR
13996 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13997 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 13998 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
13999 }
14000 }
14001
14002 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14003 if (attr != nullptr)
801e3a5b 14004 {
18a8505e 14005 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14006 We take advantage of the fact that DW_AT_ranges does not appear
14007 in DW_TAG_compile_unit of DWO files. */
14008 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14009
14010 /* The value of the DW_AT_ranges attribute is the offset of the
14011 address range list in the .debug_ranges section. */
ab435259
DE
14012 unsigned long offset = (DW_UNSND (attr)
14013 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14014
2d5f09ec 14015 std::vector<blockrange> blockvec;
5f46c5a5
JK
14016 dwarf2_ranges_process (offset, cu,
14017 [&] (CORE_ADDR start, CORE_ADDR end)
14018 {
58fdfd2c
JK
14019 start += baseaddr;
14020 end += baseaddr;
5f46c5a5
JK
14021 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14022 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14023 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14024 blockvec.emplace_back (start, end);
5f46c5a5 14025 });
2d5f09ec
KB
14026
14027 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14028 }
14029}
14030
685b1105
JK
14031/* Check whether the producer field indicates either of GCC < 4.6, or the
14032 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14033
685b1105
JK
14034static void
14035check_producer (struct dwarf2_cu *cu)
60d5a603 14036{
38360086 14037 int major, minor;
60d5a603
JK
14038
14039 if (cu->producer == NULL)
14040 {
14041 /* For unknown compilers expect their behavior is DWARF version
14042 compliant.
14043
14044 GCC started to support .debug_types sections by -gdwarf-4 since
14045 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14046 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14047 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14048 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14049 }
b1ffba5a 14050 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14051 {
38360086
MW
14052 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14053 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14054 }
5230b05a 14055 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14056 {
14057 cu->producer_is_icc = true;
14058 cu->producer_is_icc_lt_14 = major < 14;
14059 }
c258c396
JD
14060 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14061 cu->producer_is_codewarrior = true;
685b1105
JK
14062 else
14063 {
14064 /* For other non-GCC compilers, expect their behavior is DWARF version
14065 compliant. */
60d5a603
JK
14066 }
14067
9068261f 14068 cu->checked_producer = true;
685b1105 14069}
ba919b58 14070
685b1105
JK
14071/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14072 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14073 during 4.6.0 experimental. */
14074
9068261f 14075static bool
685b1105
JK
14076producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14077{
14078 if (!cu->checked_producer)
14079 check_producer (cu);
14080
14081 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14082}
14083
c258c396
JD
14084
14085/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14086 with incorrect is_stmt attributes. */
14087
14088static bool
14089producer_is_codewarrior (struct dwarf2_cu *cu)
14090{
14091 if (!cu->checked_producer)
14092 check_producer (cu);
14093
14094 return cu->producer_is_codewarrior;
14095}
14096
405feb71 14097/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14098 DW_AT_accessibility. */
14099
14100static enum dwarf_access_attribute
14101dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14102{
14103 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14104 {
14105 /* The default DWARF 2 accessibility for members is public, the default
14106 accessibility for inheritance is private. */
14107
14108 if (die->tag != DW_TAG_inheritance)
14109 return DW_ACCESS_public;
14110 else
14111 return DW_ACCESS_private;
14112 }
14113 else
14114 {
14115 /* DWARF 3+ defines the default accessibility a different way. The same
14116 rules apply now for DW_TAG_inheritance as for the members and it only
14117 depends on the container kind. */
14118
14119 if (die->parent->tag == DW_TAG_class_type)
14120 return DW_ACCESS_private;
14121 else
14122 return DW_ACCESS_public;
14123 }
14124}
14125
74ac6d43
TT
14126/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14127 offset. If the attribute was not found return 0, otherwise return
14128 1. If it was found but could not properly be handled, set *OFFSET
14129 to 0. */
14130
14131static int
14132handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14133 LONGEST *offset)
14134{
14135 struct attribute *attr;
14136
14137 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14138 if (attr != NULL)
14139 {
14140 *offset = 0;
14141
14142 /* Note that we do not check for a section offset first here.
14143 This is because DW_AT_data_member_location is new in DWARF 4,
14144 so if we see it, we can assume that a constant form is really
14145 a constant and not a section offset. */
cd6c91b4 14146 if (attr->form_is_constant ())
74ac6d43 14147 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14148 else if (attr->form_is_section_offset ())
74ac6d43 14149 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14150 else if (attr->form_is_block ())
74ac6d43
TT
14151 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14152 else
14153 dwarf2_complex_location_expr_complaint ();
14154
14155 return 1;
14156 }
14157
14158 return 0;
14159}
14160
c906108c
SS
14161/* Add an aggregate field to the field list. */
14162
14163static void
107d2387 14164dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14165 struct dwarf2_cu *cu)
6e70227d 14166{
518817b3 14167 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14168 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14169 struct nextfield *new_field;
14170 struct attribute *attr;
14171 struct field *fp;
15d034d0 14172 const char *fieldname = "";
c906108c 14173
7d0ccb61
DJ
14174 if (die->tag == DW_TAG_inheritance)
14175 {
be2daae6
TT
14176 fip->baseclasses.emplace_back ();
14177 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14178 }
14179 else
14180 {
be2daae6
TT
14181 fip->fields.emplace_back ();
14182 new_field = &fip->fields.back ();
7d0ccb61 14183 }
be2daae6 14184
c906108c
SS
14185 fip->nfields++;
14186
e142c38c 14187 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14188 if (attr != nullptr)
c906108c 14189 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14190 else
14191 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14192 if (new_field->accessibility != DW_ACCESS_public)
14193 fip->non_public_fields = 1;
60d5a603 14194
e142c38c 14195 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14196 if (attr != nullptr)
c906108c 14197 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14198 else
14199 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14200
14201 fp = &new_field->field;
a9a9bd0f 14202
e142c38c 14203 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14204 {
74ac6d43
TT
14205 LONGEST offset;
14206
a9a9bd0f 14207 /* Data member other than a C++ static data member. */
6e70227d 14208
c906108c 14209 /* Get type of field. */
e7c27a73 14210 fp->type = die_type (die, cu);
c906108c 14211
d6a843b5 14212 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14213
c906108c 14214 /* Get bit size of field (zero if none). */
e142c38c 14215 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14216 if (attr != nullptr)
c906108c
SS
14217 {
14218 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14219 }
14220 else
14221 {
14222 FIELD_BITSIZE (*fp) = 0;
14223 }
14224
14225 /* Get bit offset of field. */
74ac6d43
TT
14226 if (handle_data_member_location (die, cu, &offset))
14227 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14228 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14229 if (attr != nullptr)
c906108c 14230 {
d5a22e77 14231 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14232 {
14233 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14234 additional bit offset from the MSB of the containing
14235 anonymous object to the MSB of the field. We don't
14236 have to do anything special since we don't need to
14237 know the size of the anonymous object. */
f41f5e61 14238 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14239 }
14240 else
14241 {
14242 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14243 MSB of the anonymous object, subtract off the number of
14244 bits from the MSB of the field to the MSB of the
14245 object, and then subtract off the number of bits of
14246 the field itself. The result is the bit offset of
14247 the LSB of the field. */
c906108c
SS
14248 int anonymous_size;
14249 int bit_offset = DW_UNSND (attr);
14250
e142c38c 14251 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14252 if (attr != nullptr)
c906108c
SS
14253 {
14254 /* The size of the anonymous object containing
14255 the bit field is explicit, so use the
14256 indicated size (in bytes). */
14257 anonymous_size = DW_UNSND (attr);
14258 }
14259 else
14260 {
14261 /* The size of the anonymous object containing
14262 the bit field must be inferred from the type
14263 attribute of the data member containing the
14264 bit field. */
14265 anonymous_size = TYPE_LENGTH (fp->type);
14266 }
f41f5e61
PA
14267 SET_FIELD_BITPOS (*fp,
14268 (FIELD_BITPOS (*fp)
14269 + anonymous_size * bits_per_byte
14270 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14271 }
14272 }
da5b30da
AA
14273 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14274 if (attr != NULL)
14275 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14276 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14277
14278 /* Get name of field. */
39cbfefa
DJ
14279 fieldname = dwarf2_name (die, cu);
14280 if (fieldname == NULL)
14281 fieldname = "";
d8151005
DJ
14282
14283 /* The name is already allocated along with this objfile, so we don't
14284 need to duplicate it for the type. */
14285 fp->name = fieldname;
c906108c
SS
14286
14287 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14288 pointer or virtual base class pointer) to private. */
e142c38c 14289 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14290 {
d48cc9dd 14291 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14292 new_field->accessibility = DW_ACCESS_private;
14293 fip->non_public_fields = 1;
14294 }
14295 }
a9a9bd0f 14296 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14297 {
a9a9bd0f
DC
14298 /* C++ static member. */
14299
14300 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14301 is a declaration, but all versions of G++ as of this writing
14302 (so through at least 3.2.1) incorrectly generate
14303 DW_TAG_variable tags. */
6e70227d 14304
ff355380 14305 const char *physname;
c906108c 14306
a9a9bd0f 14307 /* Get name of field. */
39cbfefa
DJ
14308 fieldname = dwarf2_name (die, cu);
14309 if (fieldname == NULL)
c906108c
SS
14310 return;
14311
254e6b9e 14312 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14313 if (attr
14314 /* Only create a symbol if this is an external value.
14315 new_symbol checks this and puts the value in the global symbol
14316 table, which we want. If it is not external, new_symbol
14317 will try to put the value in cu->list_in_scope which is wrong. */
14318 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14319 {
14320 /* A static const member, not much different than an enum as far as
14321 we're concerned, except that we can support more types. */
14322 new_symbol (die, NULL, cu);
14323 }
14324
2df3850c 14325 /* Get physical name. */
ff355380 14326 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14327
d8151005
DJ
14328 /* The name is already allocated along with this objfile, so we don't
14329 need to duplicate it for the type. */
14330 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14331 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14332 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14333 }
14334 else if (die->tag == DW_TAG_inheritance)
14335 {
74ac6d43 14336 LONGEST offset;
d4b96c9a 14337
74ac6d43
TT
14338 /* C++ base class field. */
14339 if (handle_data_member_location (die, cu, &offset))
14340 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14341 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14342 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14343 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14344 }
2ddeaf8a
TT
14345 else if (die->tag == DW_TAG_variant_part)
14346 {
14347 /* process_structure_scope will treat this DIE as a union. */
14348 process_structure_scope (die, cu);
14349
14350 /* The variant part is relative to the start of the enclosing
14351 structure. */
14352 SET_FIELD_BITPOS (*fp, 0);
14353 fp->type = get_die_type (die, cu);
14354 fp->artificial = 1;
14355 fp->name = "<<variant>>";
c8c81635
TT
14356
14357 /* Normally a DW_TAG_variant_part won't have a size, but our
14358 representation requires one, so set it to the maximum of the
489dbda6
TT
14359 child sizes, being sure to account for the offset at which
14360 each child is seen. */
c8c81635
TT
14361 if (TYPE_LENGTH (fp->type) == 0)
14362 {
14363 unsigned max = 0;
14364 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14365 {
14366 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14367 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14368 if (len > max)
14369 max = len;
14370 }
c8c81635
TT
14371 TYPE_LENGTH (fp->type) = max;
14372 }
2ddeaf8a
TT
14373 }
14374 else
14375 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14376}
14377
883fd55a
KS
14378/* Can the type given by DIE define another type? */
14379
14380static bool
14381type_can_define_types (const struct die_info *die)
14382{
14383 switch (die->tag)
14384 {
14385 case DW_TAG_typedef:
14386 case DW_TAG_class_type:
14387 case DW_TAG_structure_type:
14388 case DW_TAG_union_type:
14389 case DW_TAG_enumeration_type:
14390 return true;
14391
14392 default:
14393 return false;
14394 }
14395}
14396
14397/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14398
14399static void
883fd55a
KS
14400dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14401 struct dwarf2_cu *cu)
6e70227d 14402{
be2daae6
TT
14403 struct decl_field fp;
14404 memset (&fp, 0, sizeof (fp));
98751a41 14405
883fd55a 14406 gdb_assert (type_can_define_types (die));
98751a41 14407
883fd55a 14408 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14409 fp.name = dwarf2_name (die, cu);
14410 fp.type = read_type_die (die, cu);
98751a41 14411
c191a687
KS
14412 /* Save accessibility. */
14413 enum dwarf_access_attribute accessibility;
14414 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14415 if (attr != NULL)
14416 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14417 else
14418 accessibility = dwarf2_default_access_attribute (die, cu);
14419 switch (accessibility)
14420 {
14421 case DW_ACCESS_public:
14422 /* The assumed value if neither private nor protected. */
14423 break;
14424 case DW_ACCESS_private:
be2daae6 14425 fp.is_private = 1;
c191a687
KS
14426 break;
14427 case DW_ACCESS_protected:
be2daae6 14428 fp.is_protected = 1;
c191a687
KS
14429 break;
14430 default:
b98664d3 14431 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14432 }
14433
883fd55a 14434 if (die->tag == DW_TAG_typedef)
be2daae6 14435 fip->typedef_field_list.push_back (fp);
883fd55a 14436 else
be2daae6 14437 fip->nested_types_list.push_back (fp);
98751a41
JK
14438}
14439
c906108c
SS
14440/* Create the vector of fields, and attach it to the type. */
14441
14442static void
fba45db2 14443dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14444 struct dwarf2_cu *cu)
c906108c
SS
14445{
14446 int nfields = fip->nfields;
14447
14448 /* Record the field count, allocate space for the array of fields,
14449 and create blank accessibility bitfields if necessary. */
14450 TYPE_NFIELDS (type) = nfields;
14451 TYPE_FIELDS (type) = (struct field *)
be2daae6 14452 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14453
b4ba55a1 14454 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14455 {
14456 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14457
14458 TYPE_FIELD_PRIVATE_BITS (type) =
14459 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14460 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14461
14462 TYPE_FIELD_PROTECTED_BITS (type) =
14463 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14464 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14465
774b6a14
TT
14466 TYPE_FIELD_IGNORE_BITS (type) =
14467 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14468 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14469 }
14470
14471 /* If the type has baseclasses, allocate and clear a bit vector for
14472 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14473 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14474 {
be2daae6 14475 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14476 unsigned char *pointer;
c906108c
SS
14477
14478 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14479 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14480 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14481 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14482 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14483 }
14484
2ddeaf8a
TT
14485 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14486 {
14487 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14488
be2daae6 14489 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14490 {
be2daae6
TT
14491 struct nextfield &field = fip->fields[index];
14492
14493 if (field.variant.is_discriminant)
2ddeaf8a 14494 di->discriminant_index = index;
be2daae6 14495 else if (field.variant.default_branch)
2ddeaf8a
TT
14496 di->default_index = index;
14497 else
be2daae6 14498 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14499 }
14500 }
14501
be2daae6
TT
14502 /* Copy the saved-up fields into the field vector. */
14503 for (int i = 0; i < nfields; ++i)
c906108c 14504 {
be2daae6
TT
14505 struct nextfield &field
14506 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14507 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14508
be2daae6
TT
14509 TYPE_FIELD (type, i) = field.field;
14510 switch (field.accessibility)
c906108c 14511 {
c5aa993b 14512 case DW_ACCESS_private:
b4ba55a1 14513 if (cu->language != language_ada)
be2daae6 14514 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14515 break;
c906108c 14516
c5aa993b 14517 case DW_ACCESS_protected:
b4ba55a1 14518 if (cu->language != language_ada)
be2daae6 14519 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14520 break;
c906108c 14521
c5aa993b
JM
14522 case DW_ACCESS_public:
14523 break;
c906108c 14524
c5aa993b
JM
14525 default:
14526 /* Unknown accessibility. Complain and treat it as public. */
14527 {
b98664d3 14528 complaint (_("unsupported accessibility %d"),
be2daae6 14529 field.accessibility);
c5aa993b
JM
14530 }
14531 break;
c906108c 14532 }
be2daae6 14533 if (i < fip->baseclasses.size ())
c906108c 14534 {
be2daae6 14535 switch (field.virtuality)
c906108c 14536 {
c5aa993b
JM
14537 case DW_VIRTUALITY_virtual:
14538 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14539 if (cu->language == language_ada)
a73c6dcd 14540 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14541 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14542 break;
c906108c
SS
14543 }
14544 }
c906108c
SS
14545 }
14546}
14547
7d27a96d
TT
14548/* Return true if this member function is a constructor, false
14549 otherwise. */
14550
14551static int
14552dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14553{
14554 const char *fieldname;
fe978cb0 14555 const char *type_name;
7d27a96d
TT
14556 int len;
14557
14558 if (die->parent == NULL)
14559 return 0;
14560
14561 if (die->parent->tag != DW_TAG_structure_type
14562 && die->parent->tag != DW_TAG_union_type
14563 && die->parent->tag != DW_TAG_class_type)
14564 return 0;
14565
14566 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14567 type_name = dwarf2_name (die->parent, cu);
14568 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14569 return 0;
14570
14571 len = strlen (fieldname);
fe978cb0
PA
14572 return (strncmp (fieldname, type_name, len) == 0
14573 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14574}
14575
e35000a7
TBA
14576/* Check if the given VALUE is a recognized enum
14577 dwarf_defaulted_attribute constant according to DWARF5 spec,
14578 Table 7.24. */
14579
14580static bool
14581is_valid_DW_AT_defaulted (ULONGEST value)
14582{
14583 switch (value)
14584 {
14585 case DW_DEFAULTED_no:
14586 case DW_DEFAULTED_in_class:
14587 case DW_DEFAULTED_out_of_class:
14588 return true;
14589 }
14590
3142e908 14591 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14592 return false;
14593}
14594
c906108c
SS
14595/* Add a member function to the proper fieldlist. */
14596
14597static void
107d2387 14598dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14599 struct type *type, struct dwarf2_cu *cu)
c906108c 14600{
518817b3 14601 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14602 struct attribute *attr;
c906108c 14603 int i;
be2daae6 14604 struct fnfieldlist *flp = nullptr;
c906108c 14605 struct fn_field *fnp;
15d034d0 14606 const char *fieldname;
f792889a 14607 struct type *this_type;
60d5a603 14608 enum dwarf_access_attribute accessibility;
c906108c 14609
b4ba55a1 14610 if (cu->language == language_ada)
a73c6dcd 14611 error (_("unexpected member function in Ada type"));
b4ba55a1 14612
2df3850c 14613 /* Get name of member function. */
39cbfefa
DJ
14614 fieldname = dwarf2_name (die, cu);
14615 if (fieldname == NULL)
2df3850c 14616 return;
c906108c 14617
c906108c 14618 /* Look up member function name in fieldlist. */
be2daae6 14619 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14620 {
27bfe10e 14621 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14622 {
14623 flp = &fip->fnfieldlists[i];
14624 break;
14625 }
c906108c
SS
14626 }
14627
be2daae6
TT
14628 /* Create a new fnfieldlist if necessary. */
14629 if (flp == nullptr)
c906108c 14630 {
be2daae6
TT
14631 fip->fnfieldlists.emplace_back ();
14632 flp = &fip->fnfieldlists.back ();
c906108c 14633 flp->name = fieldname;
be2daae6 14634 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14635 }
14636
be2daae6
TT
14637 /* Create a new member function field and add it to the vector of
14638 fnfieldlists. */
14639 flp->fnfields.emplace_back ();
14640 fnp = &flp->fnfields.back ();
3da10d80
KS
14641
14642 /* Delay processing of the physname until later. */
9c37b5ae 14643 if (cu->language == language_cplus)
be2daae6
TT
14644 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14645 die, cu);
3da10d80
KS
14646 else
14647 {
1d06ead6 14648 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14649 fnp->physname = physname ? physname : "";
14650 }
14651
c906108c 14652 fnp->type = alloc_type (objfile);
f792889a
DJ
14653 this_type = read_type_die (die, cu);
14654 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14655 {
f792889a 14656 int nparams = TYPE_NFIELDS (this_type);
c906108c 14657
f792889a 14658 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14659 of the method itself (TYPE_CODE_METHOD). */
14660 smash_to_method_type (fnp->type, type,
f792889a
DJ
14661 TYPE_TARGET_TYPE (this_type),
14662 TYPE_FIELDS (this_type),
14663 TYPE_NFIELDS (this_type),
14664 TYPE_VARARGS (this_type));
c906108c
SS
14665
14666 /* Handle static member functions.
c5aa993b 14667 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14668 member functions. G++ helps GDB by marking the first
14669 parameter for non-static member functions (which is the this
14670 pointer) as artificial. We obtain this information from
14671 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14672 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14673 fnp->voffset = VOFFSET_STATIC;
14674 }
14675 else
b98664d3 14676 complaint (_("member function type missing for '%s'"),
3da10d80 14677 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14678
14679 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14680 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14681 fnp->fcontext = die_containing_type (die, cu);
c906108c 14682
3e43a32a
MS
14683 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14684 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14685
14686 /* Get accessibility. */
e142c38c 14687 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14688 if (attr != nullptr)
aead7601 14689 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14690 else
14691 accessibility = dwarf2_default_access_attribute (die, cu);
14692 switch (accessibility)
c906108c 14693 {
60d5a603
JK
14694 case DW_ACCESS_private:
14695 fnp->is_private = 1;
14696 break;
14697 case DW_ACCESS_protected:
14698 fnp->is_protected = 1;
14699 break;
c906108c
SS
14700 }
14701
b02dede2 14702 /* Check for artificial methods. */
e142c38c 14703 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14704 if (attr && DW_UNSND (attr) != 0)
14705 fnp->is_artificial = 1;
14706
e35000a7
TBA
14707 /* Check for defaulted methods. */
14708 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14709 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14710 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14711
14712 /* Check for deleted methods. */
14713 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14714 if (attr != nullptr && DW_UNSND (attr) != 0)
14715 fnp->is_deleted = 1;
14716
7d27a96d
TT
14717 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14718
0d564a31 14719 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14720 function. For older versions of GCC, this is an offset in the
14721 appropriate virtual table, as specified by DW_AT_containing_type.
14722 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14723 to the object address. */
14724
e142c38c 14725 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14726 if (attr != nullptr)
8e19ed76 14727 {
4fc6c0d5 14728 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14729 {
aec5aa8b
TT
14730 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14731 {
14732 /* Old-style GCC. */
14733 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14734 }
14735 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14736 || (DW_BLOCK (attr)->size > 1
14737 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14738 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14739 {
aec5aa8b
TT
14740 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14741 if ((fnp->voffset % cu->header.addr_size) != 0)
14742 dwarf2_complex_location_expr_complaint ();
14743 else
14744 fnp->voffset /= cu->header.addr_size;
14745 fnp->voffset += 2;
14746 }
14747 else
14748 dwarf2_complex_location_expr_complaint ();
14749
14750 if (!fnp->fcontext)
7e993ebf
KS
14751 {
14752 /* If there is no `this' field and no DW_AT_containing_type,
14753 we cannot actually find a base class context for the
14754 vtable! */
14755 if (TYPE_NFIELDS (this_type) == 0
14756 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14757 {
b98664d3 14758 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14759 "function \"%s\" (offset %s)"),
14760 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14761 }
14762 else
14763 {
14764 fnp->fcontext
14765 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14766 }
14767 }
aec5aa8b 14768 }
cd6c91b4 14769 else if (attr->form_is_section_offset ())
8e19ed76 14770 {
4d3c2250 14771 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14772 }
14773 else
14774 {
4d3c2250
KB
14775 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14776 fieldname);
8e19ed76 14777 }
0d564a31 14778 }
d48cc9dd
DJ
14779 else
14780 {
14781 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14782 if (attr && DW_UNSND (attr))
14783 {
14784 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14785 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14786 "but the vtable offset is not specified"),
9d8780f0 14787 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14788 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14789 TYPE_CPLUS_DYNAMIC (type) = 1;
14790 }
14791 }
c906108c
SS
14792}
14793
14794/* Create the vector of member function fields, and attach it to the type. */
14795
14796static void
fba45db2 14797dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14798 struct dwarf2_cu *cu)
c906108c 14799{
b4ba55a1 14800 if (cu->language == language_ada)
a73c6dcd 14801 error (_("unexpected member functions in Ada type"));
b4ba55a1 14802
c906108c
SS
14803 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14804 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14805 TYPE_ALLOC (type,
14806 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14807
be2daae6 14808 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14809 {
be2daae6 14810 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14811 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14812
be2daae6
TT
14813 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14814 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14815 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14816 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14817
14818 for (int k = 0; k < nf.fnfields.size (); ++k)
14819 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14820 }
14821
be2daae6 14822 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14823}
14824
1168df01
JB
14825/* Returns non-zero if NAME is the name of a vtable member in CU's
14826 language, zero otherwise. */
14827static int
14828is_vtable_name (const char *name, struct dwarf2_cu *cu)
14829{
14830 static const char vptr[] = "_vptr";
14831
9c37b5ae
TT
14832 /* Look for the C++ form of the vtable. */
14833 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14834 return 1;
14835
14836 return 0;
14837}
14838
c0dd20ea 14839/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14840 functions, with the ABI-specified layout. If TYPE describes
14841 such a structure, smash it into a member function type.
61049d3b
DJ
14842
14843 GCC shouldn't do this; it should just output pointer to member DIEs.
14844 This is GCC PR debug/28767. */
c0dd20ea 14845
0b92b5bb
TT
14846static void
14847quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14848{
09e2d7c7 14849 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14850
14851 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14852 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14853 return;
c0dd20ea
DJ
14854
14855 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14856 if (TYPE_FIELD_NAME (type, 0) == NULL
14857 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14858 || TYPE_FIELD_NAME (type, 1) == NULL
14859 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14860 return;
c0dd20ea
DJ
14861
14862 /* Find the type of the method. */
0b92b5bb 14863 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14864 if (pfn_type == NULL
14865 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14866 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14867 return;
c0dd20ea
DJ
14868
14869 /* Look for the "this" argument. */
14870 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14871 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14872 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14873 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14874 return;
c0dd20ea 14875
09e2d7c7 14876 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14877 new_type = alloc_type (objfile);
09e2d7c7 14878 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14879 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14880 TYPE_VARARGS (pfn_type));
0b92b5bb 14881 smash_to_methodptr_type (type, new_type);
c0dd20ea 14882}
1168df01 14883
2b4424c3
TT
14884/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14885 appropriate error checking and issuing complaints if there is a
14886 problem. */
14887
14888static ULONGEST
14889get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14890{
14891 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14892
14893 if (attr == nullptr)
14894 return 0;
14895
cd6c91b4 14896 if (!attr->form_is_constant ())
2b4424c3 14897 {
b98664d3 14898 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14899 " - DIE at %s [in module %s]"),
14900 sect_offset_str (die->sect_off),
14901 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14902 return 0;
14903 }
14904
14905 ULONGEST align;
14906 if (attr->form == DW_FORM_sdata)
14907 {
14908 LONGEST val = DW_SND (attr);
14909 if (val < 0)
14910 {
b98664d3 14911 complaint (_("DW_AT_alignment value must not be negative"
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 align = val;
14918 }
14919 else
14920 align = DW_UNSND (attr);
14921
14922 if (align == 0)
14923 {
b98664d3 14924 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14925 " - DIE at %s [in module %s]"),
14926 sect_offset_str (die->sect_off),
14927 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14928 return 0;
14929 }
14930 if ((align & (align - 1)) != 0)
14931 {
b98664d3 14932 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14933 " - DIE at %s [in module %s]"),
14934 sect_offset_str (die->sect_off),
14935 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14936 return 0;
14937 }
14938
14939 return align;
14940}
14941
14942/* If the DIE has a DW_AT_alignment attribute, use its value to set
14943 the alignment for TYPE. */
14944
14945static void
14946maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14947 struct type *type)
14948{
14949 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14950 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14951 " - DIE at %s [in module %s]"),
14952 sect_offset_str (die->sect_off),
14953 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14954}
685b1105 14955
e35000a7
TBA
14956/* Check if the given VALUE is a valid enum dwarf_calling_convention
14957 constant for a type, according to DWARF5 spec, Table 5.5. */
14958
14959static bool
14960is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14961{
14962 switch (value)
14963 {
14964 case DW_CC_normal:
14965 case DW_CC_pass_by_reference:
14966 case DW_CC_pass_by_value:
14967 return true;
14968
14969 default:
14970 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14971 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14972 return false;
14973 }
14974}
14975
d0922fcf
TBA
14976/* Check if the given VALUE is a valid enum dwarf_calling_convention
14977 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14978 also according to GNU-specific values (see include/dwarf2.h). */
14979
14980static bool
14981is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14982{
14983 switch (value)
14984 {
14985 case DW_CC_normal:
14986 case DW_CC_program:
14987 case DW_CC_nocall:
14988 return true;
14989
14990 case DW_CC_GNU_renesas_sh:
14991 case DW_CC_GNU_borland_fastcall_i386:
14992 case DW_CC_GDB_IBM_OpenCL:
14993 return true;
14994
14995 default:
14996 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14997 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
14998 return false;
14999 }
15000}
15001
c906108c 15002/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15003 (definition) to create a type for the structure or union. Fill in
15004 the type's name and general properties; the members will not be
83655187
DE
15005 processed until process_structure_scope. A symbol table entry for
15006 the type will also not be done until process_structure_scope (assuming
15007 the type has a name).
c906108c 15008
c767944b
DJ
15009 NOTE: we need to call these functions regardless of whether or not the
15010 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15011 structure or union. This gets the type entered into our set of
83655187 15012 user defined types. */
c906108c 15013
f792889a 15014static struct type *
134d01f1 15015read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15016{
518817b3 15017 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15018 struct type *type;
15019 struct attribute *attr;
15d034d0 15020 const char *name;
c906108c 15021
348e048f
DE
15022 /* If the definition of this type lives in .debug_types, read that type.
15023 Don't follow DW_AT_specification though, that will take us back up
15024 the chain and we want to go down. */
45e58e77 15025 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15026 if (attr != nullptr)
348e048f 15027 {
ac9ec31b 15028 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15029
ac9ec31b 15030 /* The type's CU may not be the same as CU.
02142a6c 15031 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15032 return set_die_type (die, type, cu);
15033 }
15034
c0dd20ea 15035 type = alloc_type (objfile);
c906108c 15036 INIT_CPLUS_SPECIFIC (type);
93311388 15037
39cbfefa
DJ
15038 name = dwarf2_name (die, cu);
15039 if (name != NULL)
c906108c 15040 {
987504bb 15041 if (cu->language == language_cplus
c44af4eb
TT
15042 || cu->language == language_d
15043 || cu->language == language_rust)
63d06c5c 15044 {
15d034d0 15045 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15046
15047 /* dwarf2_full_name might have already finished building the DIE's
15048 type. If so, there is no need to continue. */
15049 if (get_die_type (die, cu) != NULL)
15050 return get_die_type (die, cu);
15051
e86ca25f 15052 TYPE_NAME (type) = full_name;
63d06c5c
DC
15053 }
15054 else
15055 {
d8151005
DJ
15056 /* The name is already allocated along with this objfile, so
15057 we don't need to duplicate it for the type. */
e86ca25f 15058 TYPE_NAME (type) = name;
63d06c5c 15059 }
c906108c
SS
15060 }
15061
15062 if (die->tag == DW_TAG_structure_type)
15063 {
15064 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15065 }
15066 else if (die->tag == DW_TAG_union_type)
15067 {
15068 TYPE_CODE (type) = TYPE_CODE_UNION;
15069 }
2ddeaf8a
TT
15070 else if (die->tag == DW_TAG_variant_part)
15071 {
15072 TYPE_CODE (type) = TYPE_CODE_UNION;
15073 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15074 }
c906108c
SS
15075 else
15076 {
4753d33b 15077 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15078 }
15079
0cc2414c
TT
15080 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15081 TYPE_DECLARED_CLASS (type) = 1;
15082
e35000a7
TBA
15083 /* Store the calling convention in the type if it's available in
15084 the die. Otherwise the calling convention remains set to
15085 the default value DW_CC_normal. */
15086 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15087 if (attr != nullptr
15088 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15089 {
15090 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15091 TYPE_CPLUS_CALLING_CONVENTION (type)
15092 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15093 }
15094
e142c38c 15095 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15096 if (attr != nullptr)
c906108c 15097 {
cd6c91b4 15098 if (attr->form_is_constant ())
155bfbd3
JB
15099 TYPE_LENGTH (type) = DW_UNSND (attr);
15100 else
15101 {
15102 /* For the moment, dynamic type sizes are not supported
15103 by GDB's struct type. The actual size is determined
15104 on-demand when resolving the type of a given object,
15105 so set the type's length to zero for now. Otherwise,
15106 we record an expression as the length, and that expression
15107 could lead to a very large value, which could eventually
15108 lead to us trying to allocate that much memory when creating
15109 a value of that type. */
15110 TYPE_LENGTH (type) = 0;
15111 }
c906108c
SS
15112 }
15113 else
15114 {
15115 TYPE_LENGTH (type) = 0;
15116 }
15117
2b4424c3
TT
15118 maybe_set_alignment (cu, die, type);
15119
5230b05a 15120 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15121 {
5230b05a
WT
15122 /* ICC<14 does not output the required DW_AT_declaration on
15123 incomplete types, but gives them a size of zero. */
422b1cb0 15124 TYPE_STUB (type) = 1;
685b1105
JK
15125 }
15126 else
15127 TYPE_STUB_SUPPORTED (type) = 1;
15128
dc718098 15129 if (die_is_declaration (die, cu))
876cecd0 15130 TYPE_STUB (type) = 1;
a6c727b2
DJ
15131 else if (attr == NULL && die->child == NULL
15132 && producer_is_realview (cu->producer))
15133 /* RealView does not output the required DW_AT_declaration
15134 on incomplete types. */
15135 TYPE_STUB (type) = 1;
dc718098 15136
c906108c
SS
15137 /* We need to add the type field to the die immediately so we don't
15138 infinitely recurse when dealing with pointers to the structure
0963b4bd 15139 type within the structure itself. */
1c379e20 15140 set_die_type (die, type, cu);
c906108c 15141
7e314c57
JK
15142 /* set_die_type should be already done. */
15143 set_descriptive_type (type, die, cu);
15144
c767944b
DJ
15145 return type;
15146}
15147
2ddeaf8a
TT
15148/* A helper for process_structure_scope that handles a single member
15149 DIE. */
15150
15151static void
15152handle_struct_member_die (struct die_info *child_die, struct type *type,
15153 struct field_info *fi,
15154 std::vector<struct symbol *> *template_args,
15155 struct dwarf2_cu *cu)
15156{
15157 if (child_die->tag == DW_TAG_member
15158 || child_die->tag == DW_TAG_variable
15159 || child_die->tag == DW_TAG_variant_part)
15160 {
15161 /* NOTE: carlton/2002-11-05: A C++ static data member
15162 should be a DW_TAG_member that is a declaration, but
15163 all versions of G++ as of this writing (so through at
15164 least 3.2.1) incorrectly generate DW_TAG_variable
15165 tags for them instead. */
15166 dwarf2_add_field (fi, child_die, cu);
15167 }
15168 else if (child_die->tag == DW_TAG_subprogram)
15169 {
15170 /* Rust doesn't have member functions in the C++ sense.
15171 However, it does emit ordinary functions as children
15172 of a struct DIE. */
15173 if (cu->language == language_rust)
15174 read_func_scope (child_die, cu);
15175 else
15176 {
15177 /* C++ member function. */
15178 dwarf2_add_member_fn (fi, child_die, type, cu);
15179 }
15180 }
15181 else if (child_die->tag == DW_TAG_inheritance)
15182 {
15183 /* C++ base class field. */
15184 dwarf2_add_field (fi, child_die, cu);
15185 }
15186 else if (type_can_define_types (child_die))
15187 dwarf2_add_type_defn (fi, child_die, cu);
15188 else if (child_die->tag == DW_TAG_template_type_param
15189 || child_die->tag == DW_TAG_template_value_param)
15190 {
15191 struct symbol *arg = new_symbol (child_die, NULL, cu);
15192
15193 if (arg != NULL)
15194 template_args->push_back (arg);
15195 }
15196 else if (child_die->tag == DW_TAG_variant)
15197 {
15198 /* In a variant we want to get the discriminant and also add a
15199 field for our sole member child. */
15200 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15201
bde09ab7 15202 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15203 variant_child != NULL;
15204 variant_child = sibling_die (variant_child))
15205 {
15206 if (variant_child->tag == DW_TAG_member)
15207 {
15208 handle_struct_member_die (variant_child, type, fi,
15209 template_args, cu);
15210 /* Only handle the one. */
15211 break;
15212 }
15213 }
15214
15215 /* We don't handle this but we might as well report it if we see
15216 it. */
15217 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15218 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15219 " - DIE at %s [in module %s]"),
15220 sect_offset_str (child_die->sect_off),
15221 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15222
15223 /* The first field was just added, so we can stash the
15224 discriminant there. */
be2daae6 15225 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15226 if (discr == NULL)
be2daae6 15227 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15228 else
be2daae6 15229 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15230 }
15231}
15232
c767944b
DJ
15233/* Finish creating a structure or union type, including filling in
15234 its members and creating a symbol for it. */
15235
15236static void
15237process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15238{
518817b3 15239 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15240 struct die_info *child_die;
c767944b
DJ
15241 struct type *type;
15242
15243 type = get_die_type (die, cu);
15244 if (type == NULL)
15245 type = read_structure_type (die, cu);
15246
2ddeaf8a
TT
15247 /* When reading a DW_TAG_variant_part, we need to notice when we
15248 read the discriminant member, so we can record it later in the
15249 discriminant_info. */
15250 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15251 sect_offset discr_offset {};
3e1d3d8c 15252 bool has_template_parameters = false;
2ddeaf8a
TT
15253
15254 if (is_variant_part)
15255 {
15256 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15257 if (discr == NULL)
15258 {
15259 /* Maybe it's a univariant form, an extension we support.
15260 In this case arrange not to check the offset. */
15261 is_variant_part = false;
15262 }
cd6c91b4 15263 else if (discr->form_is_ref ())
2ddeaf8a
TT
15264 {
15265 struct dwarf2_cu *target_cu = cu;
15266 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15267
15268 discr_offset = target_die->sect_off;
15269 }
15270 else
15271 {
b98664d3 15272 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15273 " - DIE at %s [in module %s]"),
15274 sect_offset_str (die->sect_off),
15275 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15276 is_variant_part = false;
15277 }
15278 }
15279
e142c38c 15280 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15281 {
15282 struct field_info fi;
2f4732b0 15283 std::vector<struct symbol *> template_args;
c906108c 15284
639d11d3 15285 child_die = die->child;
c906108c
SS
15286
15287 while (child_die && child_die->tag)
15288 {
2ddeaf8a 15289 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15290
2ddeaf8a 15291 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15292 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15293
c906108c
SS
15294 child_die = sibling_die (child_die);
15295 }
15296
34eaf542 15297 /* Attach template arguments to type. */
2f4732b0 15298 if (!template_args.empty ())
34eaf542 15299 {
3e1d3d8c 15300 has_template_parameters = true;
34eaf542 15301 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15302 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15303 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15304 = XOBNEWVEC (&objfile->objfile_obstack,
15305 struct symbol *,
15306 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15307 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15308 template_args.data (),
34eaf542
TT
15309 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15310 * sizeof (struct symbol *)));
34eaf542
TT
15311 }
15312
c906108c
SS
15313 /* Attach fields and member functions to the type. */
15314 if (fi.nfields)
e7c27a73 15315 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15316 if (!fi.fnfieldlists.empty ())
c906108c 15317 {
e7c27a73 15318 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15319
c5aa993b 15320 /* Get the type which refers to the base class (possibly this
c906108c 15321 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15322 class from the DW_AT_containing_type attribute. This use of
15323 DW_AT_containing_type is a GNU extension. */
c906108c 15324
e142c38c 15325 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15326 {
e7c27a73 15327 struct type *t = die_containing_type (die, cu);
c906108c 15328
ae6ae975 15329 set_type_vptr_basetype (type, t);
c906108c
SS
15330 if (type == t)
15331 {
c906108c
SS
15332 int i;
15333
15334 /* Our own class provides vtbl ptr. */
15335 for (i = TYPE_NFIELDS (t) - 1;
15336 i >= TYPE_N_BASECLASSES (t);
15337 --i)
15338 {
0d5cff50 15339 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15340
1168df01 15341 if (is_vtable_name (fieldname, cu))
c906108c 15342 {
ae6ae975 15343 set_type_vptr_fieldno (type, i);
c906108c
SS
15344 break;
15345 }
15346 }
15347
15348 /* Complain if virtual function table field not found. */
15349 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15350 complaint (_("virtual function table pointer "
3e43a32a 15351 "not found when defining class '%s'"),
e86ca25f 15352 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15353 }
15354 else
15355 {
ae6ae975 15356 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15357 }
15358 }
f6235d4c 15359 else if (cu->producer
61012eef 15360 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15361 {
15362 /* The IBM XLC compiler does not provide direct indication
15363 of the containing type, but the vtable pointer is
15364 always named __vfp. */
15365
15366 int i;
15367
15368 for (i = TYPE_NFIELDS (type) - 1;
15369 i >= TYPE_N_BASECLASSES (type);
15370 --i)
15371 {
15372 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15373 {
ae6ae975
DE
15374 set_type_vptr_fieldno (type, i);
15375 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15376 break;
15377 }
15378 }
15379 }
c906108c 15380 }
98751a41
JK
15381
15382 /* Copy fi.typedef_field_list linked list elements content into the
15383 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15384 if (!fi.typedef_field_list.empty ())
98751a41 15385 {
be2daae6 15386 int count = fi.typedef_field_list.size ();
98751a41 15387
a0d7a4ff 15388 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15389 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15390 = ((struct decl_field *)
be2daae6
TT
15391 TYPE_ALLOC (type,
15392 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15393 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15394
be2daae6
TT
15395 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15396 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15397 }
c767944b 15398
883fd55a
KS
15399 /* Copy fi.nested_types_list linked list elements content into the
15400 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15401 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15402 {
be2daae6 15403 int count = fi.nested_types_list.size ();
883fd55a
KS
15404
15405 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15406 TYPE_NESTED_TYPES_ARRAY (type)
15407 = ((struct decl_field *)
be2daae6
TT
15408 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15409 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15410
be2daae6
TT
15411 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15412 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15413 }
c906108c 15414 }
63d06c5c 15415
bb5ed363 15416 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15417 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15418 cu->rust_unions.push_back (type);
0b92b5bb 15419
90aeadfc
DC
15420 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15421 snapshots) has been known to create a die giving a declaration
15422 for a class that has, as a child, a die giving a definition for a
15423 nested class. So we have to process our children even if the
15424 current die is a declaration. Normally, of course, a declaration
15425 won't have any children at all. */
134d01f1 15426
ca040673
DE
15427 child_die = die->child;
15428
90aeadfc
DC
15429 while (child_die != NULL && child_die->tag)
15430 {
15431 if (child_die->tag == DW_TAG_member
15432 || child_die->tag == DW_TAG_variable
34eaf542
TT
15433 || child_die->tag == DW_TAG_inheritance
15434 || child_die->tag == DW_TAG_template_value_param
15435 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15436 {
90aeadfc 15437 /* Do nothing. */
134d01f1 15438 }
90aeadfc
DC
15439 else
15440 process_die (child_die, cu);
134d01f1 15441
90aeadfc 15442 child_die = sibling_die (child_die);
134d01f1
DJ
15443 }
15444
fa4028e9
JB
15445 /* Do not consider external references. According to the DWARF standard,
15446 these DIEs are identified by the fact that they have no byte_size
15447 attribute, and a declaration attribute. */
15448 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15449 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15450 {
15451 struct symbol *sym = new_symbol (die, type, cu);
15452
15453 if (has_template_parameters)
15454 {
a776957c
TT
15455 struct symtab *symtab;
15456 if (sym != nullptr)
15457 symtab = symbol_symtab (sym);
15458 else if (cu->line_header != nullptr)
15459 {
15460 /* Any related symtab will do. */
15461 symtab
7ba99d21 15462 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15463 }
15464 else
15465 {
15466 symtab = nullptr;
15467 complaint (_("could not find suitable "
15468 "symtab for template parameter"
15469 " - DIE at %s [in module %s]"),
15470 sect_offset_str (die->sect_off),
15471 objfile_name (objfile));
15472 }
15473
15474 if (symtab != nullptr)
15475 {
15476 /* Make sure that the symtab is set on the new symbols.
15477 Even though they don't appear in this symtab directly,
15478 other parts of gdb assume that symbols do, and this is
15479 reasonably true. */
15480 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15481 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15482 }
3e1d3d8c
TT
15483 }
15484 }
134d01f1
DJ
15485}
15486
55426c9d
JB
15487/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15488 update TYPE using some information only available in DIE's children. */
15489
15490static void
15491update_enumeration_type_from_children (struct die_info *die,
15492 struct type *type,
15493 struct dwarf2_cu *cu)
15494{
60f7655a 15495 struct die_info *child_die;
55426c9d
JB
15496 int unsigned_enum = 1;
15497 int flag_enum = 1;
15498 ULONGEST mask = 0;
55426c9d 15499
8268c778 15500 auto_obstack obstack;
55426c9d 15501
60f7655a
DE
15502 for (child_die = die->child;
15503 child_die != NULL && child_die->tag;
15504 child_die = sibling_die (child_die))
55426c9d
JB
15505 {
15506 struct attribute *attr;
15507 LONGEST value;
15508 const gdb_byte *bytes;
15509 struct dwarf2_locexpr_baton *baton;
15510 const char *name;
60f7655a 15511
55426c9d
JB
15512 if (child_die->tag != DW_TAG_enumerator)
15513 continue;
15514
15515 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15516 if (attr == NULL)
15517 continue;
15518
15519 name = dwarf2_name (child_die, cu);
15520 if (name == NULL)
15521 name = "<anonymous enumerator>";
15522
15523 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15524 &value, &bytes, &baton);
15525 if (value < 0)
15526 {
15527 unsigned_enum = 0;
15528 flag_enum = 0;
15529 }
55426c9d 15530 else
edd45eb0
SM
15531 {
15532 if (count_one_bits_ll (value) >= 2)
15533 flag_enum = 0;
15534 else if ((mask & value) != 0)
15535 flag_enum = 0;
15536 else
15537 mask |= value;
15538 }
55426c9d
JB
15539
15540 /* If we already know that the enum type is neither unsigned, nor
15541 a flag type, no need to look at the rest of the enumerates. */
15542 if (!unsigned_enum && !flag_enum)
15543 break;
55426c9d
JB
15544 }
15545
15546 if (unsigned_enum)
15547 TYPE_UNSIGNED (type) = 1;
15548 if (flag_enum)
15549 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15550}
15551
134d01f1
DJ
15552/* Given a DW_AT_enumeration_type die, set its type. We do not
15553 complete the type's fields yet, or create any symbols. */
c906108c 15554
f792889a 15555static struct type *
134d01f1 15556read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15557{
518817b3 15558 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15559 struct type *type;
c906108c 15560 struct attribute *attr;
0114d602 15561 const char *name;
134d01f1 15562
348e048f
DE
15563 /* If the definition of this type lives in .debug_types, read that type.
15564 Don't follow DW_AT_specification though, that will take us back up
15565 the chain and we want to go down. */
45e58e77 15566 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15567 if (attr != nullptr)
348e048f 15568 {
ac9ec31b 15569 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15570
ac9ec31b 15571 /* The type's CU may not be the same as CU.
02142a6c 15572 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15573 return set_die_type (die, type, cu);
15574 }
15575
c906108c
SS
15576 type = alloc_type (objfile);
15577
15578 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15579 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15580 if (name != NULL)
e86ca25f 15581 TYPE_NAME (type) = name;
c906108c 15582
0626fc76
TT
15583 attr = dwarf2_attr (die, DW_AT_type, cu);
15584 if (attr != NULL)
15585 {
15586 struct type *underlying_type = die_type (die, cu);
15587
15588 TYPE_TARGET_TYPE (type) = underlying_type;
15589 }
15590
e142c38c 15591 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15592 if (attr != nullptr)
c906108c
SS
15593 {
15594 TYPE_LENGTH (type) = DW_UNSND (attr);
15595 }
15596 else
15597 {
15598 TYPE_LENGTH (type) = 0;
15599 }
15600
2b4424c3
TT
15601 maybe_set_alignment (cu, die, type);
15602
137033e9
JB
15603 /* The enumeration DIE can be incomplete. In Ada, any type can be
15604 declared as private in the package spec, and then defined only
15605 inside the package body. Such types are known as Taft Amendment
15606 Types. When another package uses such a type, an incomplete DIE
15607 may be generated by the compiler. */
02eb380e 15608 if (die_is_declaration (die, cu))
876cecd0 15609 TYPE_STUB (type) = 1;
02eb380e 15610
0626fc76
TT
15611 /* Finish the creation of this type by using the enum's children.
15612 We must call this even when the underlying type has been provided
15613 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15614 update_enumeration_type_from_children (die, type, cu);
15615
0626fc76
TT
15616 /* If this type has an underlying type that is not a stub, then we
15617 may use its attributes. We always use the "unsigned" attribute
15618 in this situation, because ordinarily we guess whether the type
15619 is unsigned -- but the guess can be wrong and the underlying type
15620 can tell us the reality. However, we defer to a local size
15621 attribute if one exists, because this lets the compiler override
15622 the underlying type if needed. */
15623 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15624 {
15625 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15626 if (TYPE_LENGTH (type) == 0)
15627 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15628 if (TYPE_RAW_ALIGN (type) == 0
15629 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15630 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15631 }
15632
3d567982
TT
15633 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15634
f792889a 15635 return set_die_type (die, type, cu);
134d01f1
DJ
15636}
15637
15638/* Given a pointer to a die which begins an enumeration, process all
15639 the dies that define the members of the enumeration, and create the
15640 symbol for the enumeration type.
15641
15642 NOTE: We reverse the order of the element list. */
15643
15644static void
15645process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15646{
f792889a 15647 struct type *this_type;
134d01f1 15648
f792889a
DJ
15649 this_type = get_die_type (die, cu);
15650 if (this_type == NULL)
15651 this_type = read_enumeration_type (die, cu);
9dc481d3 15652
639d11d3 15653 if (die->child != NULL)
c906108c 15654 {
9dc481d3
DE
15655 struct die_info *child_die;
15656 struct symbol *sym;
43816ebc 15657 std::vector<struct field> fields;
15d034d0 15658 const char *name;
9dc481d3 15659
639d11d3 15660 child_die = die->child;
c906108c
SS
15661 while (child_die && child_die->tag)
15662 {
15663 if (child_die->tag != DW_TAG_enumerator)
15664 {
e7c27a73 15665 process_die (child_die, cu);
c906108c
SS
15666 }
15667 else
15668 {
39cbfefa
DJ
15669 name = dwarf2_name (child_die, cu);
15670 if (name)
c906108c 15671 {
f792889a 15672 sym = new_symbol (child_die, this_type, cu);
c906108c 15673
43816ebc
TT
15674 fields.emplace_back ();
15675 struct field &field = fields.back ();
c906108c 15676
43816ebc
TT
15677 FIELD_NAME (field) = sym->linkage_name ();
15678 FIELD_TYPE (field) = NULL;
15679 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15680 FIELD_BITSIZE (field) = 0;
c906108c
SS
15681 }
15682 }
15683
15684 child_die = sibling_die (child_die);
15685 }
15686
43816ebc 15687 if (!fields.empty ())
c906108c 15688 {
43816ebc 15689 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15690 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15691 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15692 memcpy (TYPE_FIELDS (this_type), fields.data (),
15693 sizeof (struct field) * fields.size ());
c906108c 15694 }
c906108c 15695 }
134d01f1 15696
6c83ed52
TT
15697 /* If we are reading an enum from a .debug_types unit, and the enum
15698 is a declaration, and the enum is not the signatured type in the
15699 unit, then we do not want to add a symbol for it. Adding a
15700 symbol would in some cases obscure the true definition of the
15701 enum, giving users an incomplete type when the definition is
15702 actually available. Note that we do not want to do this for all
15703 enums which are just declarations, because C++0x allows forward
15704 enum declarations. */
3019eac3 15705 if (cu->per_cu->is_debug_types
6c83ed52
TT
15706 && die_is_declaration (die, cu))
15707 {
52dc124a 15708 struct signatured_type *sig_type;
6c83ed52 15709
c0f78cd4 15710 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15711 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15712 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15713 return;
15714 }
15715
f792889a 15716 new_symbol (die, this_type, cu);
c906108c
SS
15717}
15718
15719/* Extract all information from a DW_TAG_array_type DIE and put it in
15720 the DIE's type field. For now, this only handles one dimensional
15721 arrays. */
15722
f792889a 15723static struct type *
e7c27a73 15724read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15725{
518817b3 15726 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15727 struct die_info *child_die;
7e314c57 15728 struct type *type;
c906108c 15729 struct type *element_type, *range_type, *index_type;
c906108c 15730 struct attribute *attr;
15d034d0 15731 const char *name;
a405673c 15732 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15733 unsigned int bit_stride = 0;
c906108c 15734
e7c27a73 15735 element_type = die_type (die, cu);
c906108c 15736
7e314c57
JK
15737 /* The die_type call above may have already set the type for this DIE. */
15738 type = get_die_type (die, cu);
15739 if (type)
15740 return type;
15741
dc53a7ad
JB
15742 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15743 if (attr != NULL)
a405673c
JB
15744 {
15745 int stride_ok;
09ba997f 15746 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15747
15748 byte_stride_prop
15749 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15750 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15751 prop_type);
a405673c
JB
15752 if (!stride_ok)
15753 {
b98664d3 15754 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15755 " - DIE at %s [in module %s]"),
15756 sect_offset_str (die->sect_off),
518817b3 15757 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15758 /* Ignore this attribute. We will likely not be able to print
15759 arrays of this type correctly, but there is little we can do
15760 to help if we cannot read the attribute's value. */
15761 byte_stride_prop = NULL;
15762 }
15763 }
dc53a7ad
JB
15764
15765 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15766 if (attr != NULL)
15767 bit_stride = DW_UNSND (attr);
15768
c906108c
SS
15769 /* Irix 6.2 native cc creates array types without children for
15770 arrays with unspecified length. */
639d11d3 15771 if (die->child == NULL)
c906108c 15772 {
46bf5051 15773 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15774 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15775 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15776 byte_stride_prop, bit_stride);
f792889a 15777 return set_die_type (die, type, cu);
c906108c
SS
15778 }
15779
791afaa2 15780 std::vector<struct type *> range_types;
639d11d3 15781 child_die = die->child;
c906108c
SS
15782 while (child_die && child_die->tag)
15783 {
15784 if (child_die->tag == DW_TAG_subrange_type)
15785 {
f792889a 15786 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15787
f792889a 15788 if (child_type != NULL)
a02abb62 15789 {
0963b4bd
MS
15790 /* The range type was succesfully read. Save it for the
15791 array type creation. */
791afaa2 15792 range_types.push_back (child_type);
a02abb62 15793 }
c906108c
SS
15794 }
15795 child_die = sibling_die (child_die);
15796 }
15797
15798 /* Dwarf2 dimensions are output from left to right, create the
15799 necessary array types in backwards order. */
7ca2d3a3 15800
c906108c 15801 type = element_type;
7ca2d3a3
DL
15802
15803 if (read_array_order (die, cu) == DW_ORD_col_major)
15804 {
15805 int i = 0;
9a619af0 15806
791afaa2 15807 while (i < range_types.size ())
dc53a7ad 15808 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15809 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15810 }
15811 else
15812 {
791afaa2 15813 size_t ndim = range_types.size ();
7ca2d3a3 15814 while (ndim-- > 0)
dc53a7ad 15815 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15816 byte_stride_prop, bit_stride);
7ca2d3a3 15817 }
c906108c 15818
f5f8a009
EZ
15819 /* Understand Dwarf2 support for vector types (like they occur on
15820 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15821 array type. This is not part of the Dwarf2/3 standard yet, but a
15822 custom vendor extension. The main difference between a regular
15823 array and the vector variant is that vectors are passed by value
15824 to functions. */
e142c38c 15825 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15826 if (attr != nullptr)
ea37ba09 15827 make_vector_type (type);
f5f8a009 15828
dbc98a8b
KW
15829 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15830 implementation may choose to implement triple vectors using this
15831 attribute. */
15832 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15833 if (attr != nullptr)
dbc98a8b
KW
15834 {
15835 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15836 TYPE_LENGTH (type) = DW_UNSND (attr);
15837 else
b98664d3 15838 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15839 "than the total size of elements"));
dbc98a8b
KW
15840 }
15841
39cbfefa
DJ
15842 name = dwarf2_name (die, cu);
15843 if (name)
15844 TYPE_NAME (type) = name;
6e70227d 15845
2b4424c3
TT
15846 maybe_set_alignment (cu, die, type);
15847
0963b4bd 15848 /* Install the type in the die. */
7e314c57
JK
15849 set_die_type (die, type, cu);
15850
15851 /* set_die_type should be already done. */
b4ba55a1
JB
15852 set_descriptive_type (type, die, cu);
15853
7e314c57 15854 return type;
c906108c
SS
15855}
15856
7ca2d3a3 15857static enum dwarf_array_dim_ordering
6e70227d 15858read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15859{
15860 struct attribute *attr;
15861
15862 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15863
435d3d88 15864 if (attr != nullptr)
aead7601 15865 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15866
0963b4bd
MS
15867 /* GNU F77 is a special case, as at 08/2004 array type info is the
15868 opposite order to the dwarf2 specification, but data is still
15869 laid out as per normal fortran.
7ca2d3a3 15870
0963b4bd
MS
15871 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15872 version checking. */
7ca2d3a3 15873
905e0470
PM
15874 if (cu->language == language_fortran
15875 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15876 {
15877 return DW_ORD_row_major;
15878 }
15879
6e70227d 15880 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15881 {
15882 case array_column_major:
15883 return DW_ORD_col_major;
15884 case array_row_major:
15885 default:
15886 return DW_ORD_row_major;
15887 };
15888}
15889
72019c9c 15890/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15891 the DIE's type field. */
72019c9c 15892
f792889a 15893static struct type *
72019c9c
GM
15894read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15895{
7e314c57
JK
15896 struct type *domain_type, *set_type;
15897 struct attribute *attr;
f792889a 15898
7e314c57
JK
15899 domain_type = die_type (die, cu);
15900
15901 /* The die_type call above may have already set the type for this DIE. */
15902 set_type = get_die_type (die, cu);
15903 if (set_type)
15904 return set_type;
15905
15906 set_type = create_set_type (NULL, domain_type);
15907
15908 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15909 if (attr != nullptr)
d09039dd 15910 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15911
2b4424c3
TT
15912 maybe_set_alignment (cu, die, set_type);
15913
f792889a 15914 return set_die_type (die, set_type, cu);
72019c9c 15915}
7ca2d3a3 15916
0971de02
TT
15917/* A helper for read_common_block that creates a locexpr baton.
15918 SYM is the symbol which we are marking as computed.
15919 COMMON_DIE is the DIE for the common block.
15920 COMMON_LOC is the location expression attribute for the common
15921 block itself.
15922 MEMBER_LOC is the location expression attribute for the particular
15923 member of the common block that we are processing.
15924 CU is the CU from which the above come. */
15925
15926static void
15927mark_common_block_symbol_computed (struct symbol *sym,
15928 struct die_info *common_die,
15929 struct attribute *common_loc,
15930 struct attribute *member_loc,
15931 struct dwarf2_cu *cu)
15932{
518817b3
SM
15933 struct dwarf2_per_objfile *dwarf2_per_objfile
15934 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15935 struct objfile *objfile = dwarf2_per_objfile->objfile;
15936 struct dwarf2_locexpr_baton *baton;
15937 gdb_byte *ptr;
15938 unsigned int cu_off;
15939 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15940 LONGEST offset = 0;
15941
15942 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15943 gdb_assert (common_loc->form_is_block ());
15944 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15945 || member_loc->form_is_constant ());
0971de02 15946
8d749320 15947 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15948 baton->per_cu = cu->per_cu;
15949 gdb_assert (baton->per_cu);
15950
15951 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15952
cd6c91b4 15953 if (member_loc->form_is_constant ())
0971de02
TT
15954 {
15955 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15956 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15957 }
15958 else
15959 baton->size += DW_BLOCK (member_loc)->size;
15960
224c3ddb 15961 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15962 baton->data = ptr;
15963
15964 *ptr++ = DW_OP_call4;
9c541725 15965 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15966 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15967 ptr += 4;
15968
cd6c91b4 15969 if (member_loc->form_is_constant ())
0971de02
TT
15970 {
15971 *ptr++ = DW_OP_addr;
15972 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15973 ptr += cu->header.addr_size;
15974 }
15975 else
15976 {
15977 /* We have to copy the data here, because DW_OP_call4 will only
15978 use a DW_AT_location attribute. */
15979 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15980 ptr += DW_BLOCK (member_loc)->size;
15981 }
15982
15983 *ptr++ = DW_OP_plus;
15984 gdb_assert (ptr - baton->data == baton->size);
15985
0971de02 15986 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15987 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15988}
15989
4357ac6c
TT
15990/* Create appropriate locally-scoped variables for all the
15991 DW_TAG_common_block entries. Also create a struct common_block
15992 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 15993 is used to separate the common blocks name namespace from regular
4357ac6c 15994 variable names. */
c906108c
SS
15995
15996static void
e7c27a73 15997read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15998{
0971de02
TT
15999 struct attribute *attr;
16000
16001 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16002 if (attr != nullptr)
0971de02
TT
16003 {
16004 /* Support the .debug_loc offsets. */
4fc6c0d5 16005 if (attr->form_is_block ())
0971de02
TT
16006 {
16007 /* Ok. */
16008 }
cd6c91b4 16009 else if (attr->form_is_section_offset ())
0971de02
TT
16010 {
16011 dwarf2_complex_location_expr_complaint ();
16012 attr = NULL;
16013 }
16014 else
16015 {
16016 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16017 "common block member");
16018 attr = NULL;
16019 }
16020 }
16021
639d11d3 16022 if (die->child != NULL)
c906108c 16023 {
518817b3 16024 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16025 struct die_info *child_die;
16026 size_t n_entries = 0, size;
16027 struct common_block *common_block;
16028 struct symbol *sym;
74ac6d43 16029
4357ac6c
TT
16030 for (child_die = die->child;
16031 child_die && child_die->tag;
16032 child_die = sibling_die (child_die))
16033 ++n_entries;
16034
16035 size = (sizeof (struct common_block)
16036 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16037 common_block
16038 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16039 size);
4357ac6c
TT
16040 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16041 common_block->n_entries = 0;
16042
16043 for (child_die = die->child;
16044 child_die && child_die->tag;
16045 child_die = sibling_die (child_die))
16046 {
16047 /* Create the symbol in the DW_TAG_common_block block in the current
16048 symbol scope. */
e7c27a73 16049 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16050 if (sym != NULL)
16051 {
16052 struct attribute *member_loc;
16053
16054 common_block->contents[common_block->n_entries++] = sym;
16055
16056 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16057 cu);
16058 if (member_loc)
16059 {
16060 /* GDB has handled this for a long time, but it is
16061 not specified by DWARF. It seems to have been
16062 emitted by gfortran at least as recently as:
16063 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16064 complaint (_("Variable in common block has "
0971de02 16065 "DW_AT_data_member_location "
9d8780f0
SM
16066 "- DIE at %s [in module %s]"),
16067 sect_offset_str (child_die->sect_off),
518817b3 16068 objfile_name (objfile));
0971de02 16069
cd6c91b4 16070 if (member_loc->form_is_section_offset ())
0971de02 16071 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16072 else if (member_loc->form_is_constant ()
4fc6c0d5 16073 || member_loc->form_is_block ())
0971de02 16074 {
435d3d88 16075 if (attr != nullptr)
0971de02
TT
16076 mark_common_block_symbol_computed (sym, die, attr,
16077 member_loc, cu);
16078 }
16079 else
16080 dwarf2_complex_location_expr_complaint ();
16081 }
16082 }
c906108c 16083 }
4357ac6c
TT
16084
16085 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16086 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16087 }
16088}
16089
0114d602 16090/* Create a type for a C++ namespace. */
d9fa45fe 16091
0114d602
DJ
16092static struct type *
16093read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16094{
518817b3 16095 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16096 const char *previous_prefix, *name;
9219021c 16097 int is_anonymous;
0114d602
DJ
16098 struct type *type;
16099
16100 /* For extensions, reuse the type of the original namespace. */
16101 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16102 {
16103 struct die_info *ext_die;
16104 struct dwarf2_cu *ext_cu = cu;
9a619af0 16105
0114d602
DJ
16106 ext_die = dwarf2_extension (die, &ext_cu);
16107 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16108
16109 /* EXT_CU may not be the same as CU.
02142a6c 16110 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16111 return set_die_type (die, type, cu);
16112 }
9219021c 16113
e142c38c 16114 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16115
16116 /* Now build the name of the current namespace. */
16117
0114d602
DJ
16118 previous_prefix = determine_prefix (die, cu);
16119 if (previous_prefix[0] != '\0')
16120 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16121 previous_prefix, name, 0, cu);
0114d602
DJ
16122
16123 /* Create the type. */
19f392bc 16124 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16125
60531b24 16126 return set_die_type (die, type, cu);
0114d602
DJ
16127}
16128
22cee43f 16129/* Read a namespace scope. */
0114d602
DJ
16130
16131static void
16132read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16133{
518817b3 16134 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16135 int is_anonymous;
9219021c 16136
5c4e30ca
DC
16137 /* Add a symbol associated to this if we haven't seen the namespace
16138 before. Also, add a using directive if it's an anonymous
16139 namespace. */
9219021c 16140
f2f0e013 16141 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16142 {
16143 struct type *type;
16144
0114d602 16145 type = read_type_die (die, cu);
e7c27a73 16146 new_symbol (die, type, cu);
5c4e30ca 16147
e8e80198 16148 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16149 if (is_anonymous)
0114d602
DJ
16150 {
16151 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16152
eb1e02fd 16153 std::vector<const char *> excludes;
804d2729 16154 add_using_directive (using_directives (cu),
22cee43f 16155 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16156 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16157 }
5c4e30ca 16158 }
9219021c 16159
639d11d3 16160 if (die->child != NULL)
d9fa45fe 16161 {
639d11d3 16162 struct die_info *child_die = die->child;
6e70227d 16163
d9fa45fe
DC
16164 while (child_die && child_die->tag)
16165 {
e7c27a73 16166 process_die (child_die, cu);
d9fa45fe
DC
16167 child_die = sibling_die (child_die);
16168 }
16169 }
38d518c9
EZ
16170}
16171
f55ee35c
JK
16172/* Read a Fortran module as type. This DIE can be only a declaration used for
16173 imported module. Still we need that type as local Fortran "use ... only"
16174 declaration imports depend on the created type in determine_prefix. */
16175
16176static struct type *
16177read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16178{
518817b3 16179 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16180 const char *module_name;
f55ee35c
JK
16181 struct type *type;
16182
16183 module_name = dwarf2_name (die, cu);
19f392bc 16184 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16185
f55ee35c
JK
16186 return set_die_type (die, type, cu);
16187}
16188
5d7cb8df
JK
16189/* Read a Fortran module. */
16190
16191static void
16192read_module (struct die_info *die, struct dwarf2_cu *cu)
16193{
16194 struct die_info *child_die = die->child;
530e8392
KB
16195 struct type *type;
16196
16197 type = read_type_die (die, cu);
16198 new_symbol (die, type, cu);
5d7cb8df 16199
5d7cb8df
JK
16200 while (child_die && child_die->tag)
16201 {
16202 process_die (child_die, cu);
16203 child_die = sibling_die (child_die);
16204 }
16205}
16206
38d518c9
EZ
16207/* Return the name of the namespace represented by DIE. Set
16208 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16209 namespace. */
16210
16211static const char *
e142c38c 16212namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16213{
16214 struct die_info *current_die;
16215 const char *name = NULL;
16216
16217 /* Loop through the extensions until we find a name. */
16218
16219 for (current_die = die;
16220 current_die != NULL;
f2f0e013 16221 current_die = dwarf2_extension (die, &cu))
38d518c9 16222 {
96553a0c
DE
16223 /* We don't use dwarf2_name here so that we can detect the absence
16224 of a name -> anonymous namespace. */
7d45c7c3 16225 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16226
38d518c9
EZ
16227 if (name != NULL)
16228 break;
16229 }
16230
16231 /* Is it an anonymous namespace? */
16232
16233 *is_anonymous = (name == NULL);
16234 if (*is_anonymous)
2b1dbab0 16235 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16236
16237 return name;
d9fa45fe
DC
16238}
16239
c906108c
SS
16240/* Extract all information from a DW_TAG_pointer_type DIE and add to
16241 the user defined type vector. */
16242
f792889a 16243static struct type *
e7c27a73 16244read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16245{
518817b3
SM
16246 struct gdbarch *gdbarch
16247 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16248 struct comp_unit_head *cu_header = &cu->header;
c906108c 16249 struct type *type;
8b2dbe47
KB
16250 struct attribute *attr_byte_size;
16251 struct attribute *attr_address_class;
16252 int byte_size, addr_class;
7e314c57
JK
16253 struct type *target_type;
16254
16255 target_type = die_type (die, cu);
c906108c 16256
7e314c57
JK
16257 /* The die_type call above may have already set the type for this DIE. */
16258 type = get_die_type (die, cu);
16259 if (type)
16260 return type;
16261
16262 type = lookup_pointer_type (target_type);
8b2dbe47 16263
e142c38c 16264 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16265 if (attr_byte_size)
16266 byte_size = DW_UNSND (attr_byte_size);
c906108c 16267 else
8b2dbe47
KB
16268 byte_size = cu_header->addr_size;
16269
e142c38c 16270 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16271 if (attr_address_class)
16272 addr_class = DW_UNSND (attr_address_class);
16273 else
16274 addr_class = DW_ADDR_none;
16275
2b4424c3
TT
16276 ULONGEST alignment = get_alignment (cu, die);
16277
16278 /* If the pointer size, alignment, or address class is different
16279 than the default, create a type variant marked as such and set
16280 the length accordingly. */
16281 if (TYPE_LENGTH (type) != byte_size
16282 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16283 && alignment != TYPE_RAW_ALIGN (type))
16284 || addr_class != DW_ADDR_none)
c906108c 16285 {
5e2b427d 16286 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16287 {
16288 int type_flags;
16289
849957d9 16290 type_flags = gdbarch_address_class_type_flags
5e2b427d 16291 (gdbarch, byte_size, addr_class);
876cecd0
TT
16292 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16293 == 0);
8b2dbe47
KB
16294 type = make_type_with_address_space (type, type_flags);
16295 }
16296 else if (TYPE_LENGTH (type) != byte_size)
16297 {
b98664d3 16298 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16299 }
2b4424c3
TT
16300 else if (TYPE_RAW_ALIGN (type) != alignment)
16301 {
b98664d3 16302 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16303 " - DIE at %s [in module %s]"),
16304 sect_offset_str (die->sect_off),
16305 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16306 }
6e70227d 16307 else
9a619af0
MS
16308 {
16309 /* Should we also complain about unhandled address classes? */
16310 }
c906108c 16311 }
8b2dbe47
KB
16312
16313 TYPE_LENGTH (type) = byte_size;
2b4424c3 16314 set_type_align (type, alignment);
f792889a 16315 return set_die_type (die, type, cu);
c906108c
SS
16316}
16317
16318/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16319 the user defined type vector. */
16320
f792889a 16321static struct type *
e7c27a73 16322read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16323{
16324 struct type *type;
16325 struct type *to_type;
16326 struct type *domain;
16327
e7c27a73
DJ
16328 to_type = die_type (die, cu);
16329 domain = die_containing_type (die, cu);
0d5de010 16330
7e314c57
JK
16331 /* The calls above may have already set the type for this DIE. */
16332 type = get_die_type (die, cu);
16333 if (type)
16334 return type;
16335
0d5de010
DJ
16336 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16337 type = lookup_methodptr_type (to_type);
7078baeb
TT
16338 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16339 {
518817b3
SM
16340 struct type *new_type
16341 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16342
16343 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16344 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16345 TYPE_VARARGS (to_type));
16346 type = lookup_methodptr_type (new_type);
16347 }
0d5de010
DJ
16348 else
16349 type = lookup_memberptr_type (to_type, domain);
c906108c 16350
f792889a 16351 return set_die_type (die, type, cu);
c906108c
SS
16352}
16353
4297a3f0 16354/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16355 the user defined type vector. */
16356
f792889a 16357static struct type *
4297a3f0
AV
16358read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16359 enum type_code refcode)
c906108c 16360{
e7c27a73 16361 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16362 struct type *type, *target_type;
c906108c
SS
16363 struct attribute *attr;
16364
4297a3f0
AV
16365 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16366
7e314c57
JK
16367 target_type = die_type (die, cu);
16368
16369 /* The die_type call above may have already set the type for this DIE. */
16370 type = get_die_type (die, cu);
16371 if (type)
16372 return type;
16373
4297a3f0 16374 type = lookup_reference_type (target_type, refcode);
e142c38c 16375 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16376 if (attr != nullptr)
c906108c
SS
16377 {
16378 TYPE_LENGTH (type) = DW_UNSND (attr);
16379 }
16380 else
16381 {
107d2387 16382 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16383 }
2b4424c3 16384 maybe_set_alignment (cu, die, type);
f792889a 16385 return set_die_type (die, type, cu);
c906108c
SS
16386}
16387
cf363f18
MW
16388/* Add the given cv-qualifiers to the element type of the array. GCC
16389 outputs DWARF type qualifiers that apply to an array, not the
16390 element type. But GDB relies on the array element type to carry
16391 the cv-qualifiers. This mimics section 6.7.3 of the C99
16392 specification. */
16393
16394static struct type *
16395add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16396 struct type *base_type, int cnst, int voltl)
16397{
16398 struct type *el_type, *inner_array;
16399
16400 base_type = copy_type (base_type);
16401 inner_array = base_type;
16402
16403 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16404 {
16405 TYPE_TARGET_TYPE (inner_array) =
16406 copy_type (TYPE_TARGET_TYPE (inner_array));
16407 inner_array = TYPE_TARGET_TYPE (inner_array);
16408 }
16409
16410 el_type = TYPE_TARGET_TYPE (inner_array);
16411 cnst |= TYPE_CONST (el_type);
16412 voltl |= TYPE_VOLATILE (el_type);
16413 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16414
16415 return set_die_type (die, base_type, cu);
16416}
16417
f792889a 16418static struct type *
e7c27a73 16419read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16420{
f792889a 16421 struct type *base_type, *cv_type;
c906108c 16422
e7c27a73 16423 base_type = die_type (die, cu);
7e314c57
JK
16424
16425 /* The die_type call above may have already set the type for this DIE. */
16426 cv_type = get_die_type (die, cu);
16427 if (cv_type)
16428 return cv_type;
16429
2f608a3a
KW
16430 /* In case the const qualifier is applied to an array type, the element type
16431 is so qualified, not the array type (section 6.7.3 of C99). */
16432 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16433 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16434
f792889a
DJ
16435 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16436 return set_die_type (die, cv_type, cu);
c906108c
SS
16437}
16438
f792889a 16439static struct type *
e7c27a73 16440read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16441{
f792889a 16442 struct type *base_type, *cv_type;
c906108c 16443
e7c27a73 16444 base_type = die_type (die, cu);
7e314c57
JK
16445
16446 /* The die_type call above may have already set the type for this DIE. */
16447 cv_type = get_die_type (die, cu);
16448 if (cv_type)
16449 return cv_type;
16450
cf363f18
MW
16451 /* In case the volatile qualifier is applied to an array type, the
16452 element type is so qualified, not the array type (section 6.7.3
16453 of C99). */
16454 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16455 return add_array_cv_type (die, cu, base_type, 0, 1);
16456
f792889a
DJ
16457 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16458 return set_die_type (die, cv_type, cu);
c906108c
SS
16459}
16460
06d66ee9
TT
16461/* Handle DW_TAG_restrict_type. */
16462
16463static struct type *
16464read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16465{
16466 struct type *base_type, *cv_type;
16467
16468 base_type = die_type (die, cu);
16469
16470 /* The die_type call above may have already set the type for this DIE. */
16471 cv_type = get_die_type (die, cu);
16472 if (cv_type)
16473 return cv_type;
16474
16475 cv_type = make_restrict_type (base_type);
16476 return set_die_type (die, cv_type, cu);
16477}
16478
a2c2acaf
MW
16479/* Handle DW_TAG_atomic_type. */
16480
16481static struct type *
16482read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16483{
16484 struct type *base_type, *cv_type;
16485
16486 base_type = die_type (die, cu);
16487
16488 /* The die_type call above may have already set the type for this DIE. */
16489 cv_type = get_die_type (die, cu);
16490 if (cv_type)
16491 return cv_type;
16492
16493 cv_type = make_atomic_type (base_type);
16494 return set_die_type (die, cv_type, cu);
16495}
16496
c906108c
SS
16497/* Extract all information from a DW_TAG_string_type DIE and add to
16498 the user defined type vector. It isn't really a user defined type,
16499 but it behaves like one, with other DIE's using an AT_user_def_type
16500 attribute to reference it. */
16501
f792889a 16502static struct type *
e7c27a73 16503read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16504{
518817b3 16505 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16507 struct type *type, *range_type, *index_type, *char_type;
16508 struct attribute *attr;
216a7e6b
AB
16509 struct dynamic_prop prop;
16510 bool length_is_constant = true;
16511 LONGEST length;
16512
16513 /* There are a couple of places where bit sizes might be made use of
16514 when parsing a DW_TAG_string_type, however, no producer that we know
16515 of make use of these. Handling bit sizes that are a multiple of the
16516 byte size is easy enough, but what about other bit sizes? Lets deal
16517 with that problem when we have to. Warn about these attributes being
16518 unsupported, then parse the type and ignore them like we always
16519 have. */
16520 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16521 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16522 {
16523 static bool warning_printed = false;
16524 if (!warning_printed)
16525 {
16526 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16527 "currently supported on DW_TAG_string_type."));
16528 warning_printed = true;
16529 }
16530 }
c906108c 16531
e142c38c 16532 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16533 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16534 {
16535 /* The string length describes the location at which the length of
16536 the string can be found. The size of the length field can be
16537 specified with one of the attributes below. */
16538 struct type *prop_type;
16539 struct attribute *len
16540 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16541 if (len == nullptr)
16542 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16543 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16544 {
16545 /* Pass 0 as the default as we know this attribute is constant
16546 and the default value will not be returned. */
16547 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
09ba997f 16548 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16549 }
16550 else
16551 {
16552 /* If the size is not specified then we assume it is the size of
16553 an address on this target. */
09ba997f 16554 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16555 }
16556
16557 /* Convert the attribute into a dynamic property. */
16558 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16559 length = 1;
16560 else
16561 length_is_constant = false;
16562 }
16563 else if (attr != nullptr)
16564 {
16565 /* This DW_AT_string_length just contains the length with no
16566 indirection. There's no need to create a dynamic property in this
16567 case. Pass 0 for the default value as we know it will not be
16568 returned in this case. */
16569 length = dwarf2_get_attr_constant_value (attr, 0);
16570 }
16571 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16572 {
216a7e6b
AB
16573 /* We don't currently support non-constant byte sizes for strings. */
16574 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16575 }
16576 else
16577 {
216a7e6b
AB
16578 /* Use 1 as a fallback length if we have nothing else. */
16579 length = 1;
c906108c 16580 }
6ccb9162 16581
46bf5051 16582 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16583 if (length_is_constant)
16584 range_type = create_static_range_type (NULL, index_type, 1, length);
16585 else
16586 {
16587 struct dynamic_prop low_bound;
16588
16589 low_bound.kind = PROP_CONST;
16590 low_bound.data.const_val = 1;
16591 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16592 }
3b7538c0
UW
16593 char_type = language_string_char_type (cu->language_defn, gdbarch);
16594 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16595
f792889a 16596 return set_die_type (die, type, cu);
c906108c
SS
16597}
16598
4d804846
JB
16599/* Assuming that DIE corresponds to a function, returns nonzero
16600 if the function is prototyped. */
16601
16602static int
16603prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16604{
16605 struct attribute *attr;
16606
16607 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16608 if (attr && (DW_UNSND (attr) != 0))
16609 return 1;
16610
16611 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16612 is only meaningful for C, but the concept also extends to other
4d804846
JB
16613 languages that allow unprototyped functions (Eg: Objective C).
16614 For all other languages, assume that functions are always
16615 prototyped. */
16616 if (cu->language != language_c
16617 && cu->language != language_objc
16618 && cu->language != language_opencl)
16619 return 1;
16620
16621 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16622 prototyped and unprototyped functions; default to prototyped,
16623 since that is more common in modern code (and RealView warns
16624 about unprototyped functions). */
16625 if (producer_is_realview (cu->producer))
16626 return 1;
16627
16628 return 0;
16629}
16630
c906108c
SS
16631/* Handle DIES due to C code like:
16632
16633 struct foo
c5aa993b
JM
16634 {
16635 int (*funcp)(int a, long l);
16636 int b;
16637 };
c906108c 16638
0963b4bd 16639 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16640
f792889a 16641static struct type *
e7c27a73 16642read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16643{
518817b3 16644 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16645 struct type *type; /* Type that this function returns. */
16646 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16647 struct attribute *attr;
16648
e7c27a73 16649 type = die_type (die, cu);
7e314c57
JK
16650
16651 /* The die_type call above may have already set the type for this DIE. */
16652 ftype = get_die_type (die, cu);
16653 if (ftype)
16654 return ftype;
16655
0c8b41f1 16656 ftype = lookup_function_type (type);
c906108c 16657
4d804846 16658 if (prototyped_function_p (die, cu))
a6c727b2 16659 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16660
c055b101
CV
16661 /* Store the calling convention in the type if it's available in
16662 the subroutine die. Otherwise set the calling convention to
16663 the default value DW_CC_normal. */
16664 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16665 if (attr != nullptr
16666 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16667 TYPE_CALLING_CONVENTION (ftype)
16668 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16669 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16670 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16671 else
16672 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16673
743649fd
MW
16674 /* Record whether the function returns normally to its caller or not
16675 if the DWARF producer set that information. */
16676 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16677 if (attr && (DW_UNSND (attr) != 0))
16678 TYPE_NO_RETURN (ftype) = 1;
16679
76c10ea2
GM
16680 /* We need to add the subroutine type to the die immediately so
16681 we don't infinitely recurse when dealing with parameters
0963b4bd 16682 declared as the same subroutine type. */
76c10ea2 16683 set_die_type (die, ftype, cu);
6e70227d 16684
639d11d3 16685 if (die->child != NULL)
c906108c 16686 {
bb5ed363 16687 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16688 struct die_info *child_die;
8072405b 16689 int nparams, iparams;
c906108c
SS
16690
16691 /* Count the number of parameters.
16692 FIXME: GDB currently ignores vararg functions, but knows about
16693 vararg member functions. */
8072405b 16694 nparams = 0;
639d11d3 16695 child_die = die->child;
c906108c
SS
16696 while (child_die && child_die->tag)
16697 {
16698 if (child_die->tag == DW_TAG_formal_parameter)
16699 nparams++;
16700 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16701 TYPE_VARARGS (ftype) = 1;
c906108c
SS
16702 child_die = sibling_die (child_die);
16703 }
16704
16705 /* Allocate storage for parameters and fill them in. */
16706 TYPE_NFIELDS (ftype) = nparams;
16707 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16708 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16709
8072405b
JK
16710 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16711 even if we error out during the parameters reading below. */
16712 for (iparams = 0; iparams < nparams; iparams++)
16713 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16714
16715 iparams = 0;
639d11d3 16716 child_die = die->child;
c906108c
SS
16717 while (child_die && child_die->tag)
16718 {
16719 if (child_die->tag == DW_TAG_formal_parameter)
16720 {
3ce3b1ba
PA
16721 struct type *arg_type;
16722
16723 /* DWARF version 2 has no clean way to discern C++
16724 static and non-static member functions. G++ helps
16725 GDB by marking the first parameter for non-static
16726 member functions (which is the this pointer) as
16727 artificial. We pass this information to
16728 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16729
16730 DWARF version 3 added DW_AT_object_pointer, which GCC
16731 4.5 does not yet generate. */
e142c38c 16732 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16733 if (attr != nullptr)
c906108c
SS
16734 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16735 else
9c37b5ae 16736 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16737 arg_type = die_type (child_die, cu);
16738
16739 /* RealView does not mark THIS as const, which the testsuite
16740 expects. GCC marks THIS as const in method definitions,
16741 but not in the class specifications (GCC PR 43053). */
16742 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16743 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16744 {
16745 int is_this = 0;
16746 struct dwarf2_cu *arg_cu = cu;
16747 const char *name = dwarf2_name (child_die, cu);
16748
16749 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16750 if (attr != nullptr)
3ce3b1ba
PA
16751 {
16752 /* If the compiler emits this, use it. */
16753 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16754 is_this = 1;
16755 }
16756 else if (name && strcmp (name, "this") == 0)
16757 /* Function definitions will have the argument names. */
16758 is_this = 1;
16759 else if (name == NULL && iparams == 0)
16760 /* Declarations may not have the names, so like
16761 elsewhere in GDB, assume an artificial first
16762 argument is "this". */
16763 is_this = 1;
16764
16765 if (is_this)
16766 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16767 arg_type, 0);
16768 }
16769
16770 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16771 iparams++;
16772 }
16773 child_die = sibling_die (child_die);
16774 }
16775 }
16776
76c10ea2 16777 return ftype;
c906108c
SS
16778}
16779
f792889a 16780static struct type *
e7c27a73 16781read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16782{
518817b3 16783 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16784 const char *name = NULL;
3c8e0968 16785 struct type *this_type, *target_type;
c906108c 16786
94af9270 16787 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16788 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16789 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16790 set_die_type (die, this_type, cu);
3c8e0968
DE
16791 target_type = die_type (die, cu);
16792 if (target_type != this_type)
16793 TYPE_TARGET_TYPE (this_type) = target_type;
16794 else
16795 {
16796 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16797 spec and cause infinite loops in GDB. */
b98664d3 16798 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16799 "- DIE at %s [in module %s]"),
16800 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16801 TYPE_TARGET_TYPE (this_type) = NULL;
16802 }
f792889a 16803 return this_type;
c906108c
SS
16804}
16805
9b790ce7
UW
16806/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16807 (which may be different from NAME) to the architecture back-end to allow
16808 it to guess the correct format if necessary. */
16809
16810static struct type *
16811dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16812 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16813{
16814 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16815 const struct floatformat **format;
16816 struct type *type;
16817
16818 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16819 if (format)
103a685e 16820 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16821 else
77b7c781 16822 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16823
16824 return type;
16825}
16826
eb77c9df
AB
16827/* Allocate an integer type of size BITS and name NAME. */
16828
16829static struct type *
16830dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16831 int bits, int unsigned_p, const char *name)
16832{
16833 struct type *type;
16834
16835 /* Versions of Intel's C Compiler generate an integer type called "void"
16836 instead of using DW_TAG_unspecified_type. This has been seen on
16837 at least versions 14, 17, and 18. */
35ee2dc2
AB
16838 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16839 && strcmp (name, "void") == 0)
eb77c9df
AB
16840 type = objfile_type (objfile)->builtin_void;
16841 else
16842 type = init_integer_type (objfile, bits, unsigned_p, name);
16843
16844 return type;
16845}
16846
8bdc1658
AB
16847/* Initialise and return a floating point type of size BITS suitable for
16848 use as a component of a complex number. The NAME_HINT is passed through
16849 when initialising the floating point type and is the name of the complex
16850 type.
16851
16852 As DWARF doesn't currently provide an explicit name for the components
16853 of a complex number, but it can be helpful to have these components
16854 named, we try to select a suitable name based on the size of the
16855 component. */
16856static struct type *
16857dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16858 struct objfile *objfile,
103a685e
TT
16859 int bits, const char *name_hint,
16860 enum bfd_endian byte_order)
8bdc1658
AB
16861{
16862 gdbarch *gdbarch = get_objfile_arch (objfile);
16863 struct type *tt = nullptr;
16864
35add35e
AB
16865 /* Try to find a suitable floating point builtin type of size BITS.
16866 We're going to use the name of this type as the name for the complex
16867 target type that we are about to create. */
1db455a7 16868 switch (cu->language)
8bdc1658 16869 {
1db455a7
AB
16870 case language_fortran:
16871 switch (bits)
16872 {
16873 case 32:
16874 tt = builtin_f_type (gdbarch)->builtin_real;
16875 break;
16876 case 64:
16877 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16878 break;
16879 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16880 case 128:
16881 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16882 break;
16883 }
8bdc1658 16884 break;
1db455a7
AB
16885 default:
16886 switch (bits)
16887 {
16888 case 32:
16889 tt = builtin_type (gdbarch)->builtin_float;
16890 break;
16891 case 64:
16892 tt = builtin_type (gdbarch)->builtin_double;
16893 break;
16894 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16895 case 128:
16896 tt = builtin_type (gdbarch)->builtin_long_double;
16897 break;
16898 }
8bdc1658
AB
16899 break;
16900 }
16901
35add35e
AB
16902 /* If the type we found doesn't match the size we were looking for, then
16903 pretend we didn't find a type at all, the complex target type we
16904 create will then be nameless. */
a12e5744 16905 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16906 tt = nullptr;
16907
8bdc1658 16908 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16909 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16910}
16911
c906108c
SS
16912/* Find a representation of a given base type and install
16913 it in the TYPE field of the die. */
16914
f792889a 16915static struct type *
e7c27a73 16916read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16917{
518817b3 16918 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16919 struct type *type;
16920 struct attribute *attr;
19f392bc 16921 int encoding = 0, bits = 0;
15d034d0 16922 const char *name;
34877895 16923 gdbarch *arch;
c906108c 16924
e142c38c 16925 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16926 if (attr != nullptr)
34877895 16927 encoding = DW_UNSND (attr);
e142c38c 16928 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16929 if (attr != nullptr)
34877895 16930 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16931 name = dwarf2_name (die, cu);
6ccb9162 16932 if (!name)
34877895 16933 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16934
16935 arch = get_objfile_arch (objfile);
16936 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16937
34877895
PJ
16938 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16939 if (attr)
103a685e
TT
16940 {
16941 int endianity = DW_UNSND (attr);
16942
16943 switch (endianity)
16944 {
16945 case DW_END_big:
16946 byte_order = BFD_ENDIAN_BIG;
16947 break;
16948 case DW_END_little:
16949 byte_order = BFD_ENDIAN_LITTLE;
16950 break;
16951 default:
16952 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16953 break;
16954 }
16955 }
6ccb9162
UW
16956
16957 switch (encoding)
c906108c 16958 {
6ccb9162
UW
16959 case DW_ATE_address:
16960 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16961 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16962 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16963 break;
16964 case DW_ATE_boolean:
19f392bc 16965 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16966 break;
16967 case DW_ATE_complex_float:
103a685e
TT
16968 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16969 byte_order);
19f392bc 16970 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16971 break;
16972 case DW_ATE_decimal_float:
19f392bc 16973 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16974 break;
16975 case DW_ATE_float:
103a685e 16976 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16977 break;
16978 case DW_ATE_signed:
eb77c9df 16979 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16980 break;
16981 case DW_ATE_unsigned:
3b2b8fea
TT
16982 if (cu->language == language_fortran
16983 && name
61012eef 16984 && startswith (name, "character("))
19f392bc
UW
16985 type = init_character_type (objfile, bits, 1, name);
16986 else
eb77c9df 16987 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
16988 break;
16989 case DW_ATE_signed_char:
6e70227d 16990 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16991 || cu->language == language_pascal
16992 || cu->language == language_fortran)
19f392bc
UW
16993 type = init_character_type (objfile, bits, 0, name);
16994 else
eb77c9df 16995 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16996 break;
16997 case DW_ATE_unsigned_char:
868a0084 16998 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16999 || cu->language == language_pascal
c44af4eb
TT
17000 || cu->language == language_fortran
17001 || cu->language == language_rust)
19f392bc
UW
17002 type = init_character_type (objfile, bits, 1, name);
17003 else
eb77c9df 17004 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17005 break;
75079b2b 17006 case DW_ATE_UTF:
53e710ac 17007 {
53e710ac
PA
17008 if (bits == 16)
17009 type = builtin_type (arch)->builtin_char16;
17010 else if (bits == 32)
17011 type = builtin_type (arch)->builtin_char32;
17012 else
17013 {
b98664d3 17014 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17015 bits);
eb77c9df 17016 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17017 }
17018 return set_die_type (die, type, cu);
17019 }
75079b2b
TT
17020 break;
17021
6ccb9162 17022 default:
b98664d3 17023 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17024 dwarf_type_encoding_name (encoding));
77b7c781 17025 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17026 break;
c906108c 17027 }
6ccb9162 17028
0114d602 17029 if (name && strcmp (name, "char") == 0)
876cecd0 17030 TYPE_NOSIGN (type) = 1;
0114d602 17031
2b4424c3
TT
17032 maybe_set_alignment (cu, die, type);
17033
103a685e 17034 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17035
f792889a 17036 return set_die_type (die, type, cu);
c906108c
SS
17037}
17038
80180f79
SA
17039/* Parse dwarf attribute if it's a block, reference or constant and put the
17040 resulting value of the attribute into struct bound_prop.
17041 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17042
17043static int
17044attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17045 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17046 struct type *default_type)
80180f79
SA
17047{
17048 struct dwarf2_property_baton *baton;
518817b3
SM
17049 struct obstack *obstack
17050 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17051
9a49df9d
AB
17052 gdb_assert (default_type != NULL);
17053
80180f79
SA
17054 if (attr == NULL || prop == NULL)
17055 return 0;
17056
4fc6c0d5 17057 if (attr->form_is_block ())
80180f79 17058 {
8d749320 17059 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17060 baton->property_type = default_type;
80180f79
SA
17061 baton->locexpr.per_cu = cu->per_cu;
17062 baton->locexpr.size = DW_BLOCK (attr)->size;
17063 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17064 switch (attr->name)
17065 {
17066 case DW_AT_string_length:
17067 baton->locexpr.is_reference = true;
17068 break;
17069 default:
17070 baton->locexpr.is_reference = false;
17071 break;
17072 }
80180f79
SA
17073 prop->data.baton = baton;
17074 prop->kind = PROP_LOCEXPR;
17075 gdb_assert (prop->data.baton != NULL);
17076 }
cd6c91b4 17077 else if (attr->form_is_ref ())
80180f79
SA
17078 {
17079 struct dwarf2_cu *target_cu = cu;
17080 struct die_info *target_die;
17081 struct attribute *target_attr;
17082
17083 target_die = follow_die_ref (die, attr, &target_cu);
17084 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17085 if (target_attr == NULL)
17086 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17087 target_cu);
80180f79
SA
17088 if (target_attr == NULL)
17089 return 0;
17090
df25ebbd 17091 switch (target_attr->name)
80180f79 17092 {
df25ebbd 17093 case DW_AT_location:
cd6c91b4 17094 if (target_attr->form_is_section_offset ())
df25ebbd 17095 {
8d749320 17096 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17097 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17098 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17099 prop->data.baton = baton;
17100 prop->kind = PROP_LOCLIST;
17101 gdb_assert (prop->data.baton != NULL);
17102 }
4fc6c0d5 17103 else if (target_attr->form_is_block ())
df25ebbd 17104 {
8d749320 17105 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17106 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17107 baton->locexpr.per_cu = cu->per_cu;
17108 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17109 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17110 baton->locexpr.is_reference = true;
df25ebbd
JB
17111 prop->data.baton = baton;
17112 prop->kind = PROP_LOCEXPR;
17113 gdb_assert (prop->data.baton != NULL);
17114 }
17115 else
17116 {
17117 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17118 "dynamic property");
17119 return 0;
17120 }
17121 break;
17122 case DW_AT_data_member_location:
17123 {
17124 LONGEST offset;
17125
17126 if (!handle_data_member_location (target_die, target_cu,
17127 &offset))
17128 return 0;
17129
8d749320 17130 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17131 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17132 target_cu);
df25ebbd
JB
17133 baton->offset_info.offset = offset;
17134 baton->offset_info.type = die_type (target_die, target_cu);
17135 prop->data.baton = baton;
17136 prop->kind = PROP_ADDR_OFFSET;
17137 break;
17138 }
80180f79
SA
17139 }
17140 }
cd6c91b4 17141 else if (attr->form_is_constant ())
80180f79
SA
17142 {
17143 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17144 prop->kind = PROP_CONST;
17145 }
17146 else
17147 {
17148 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17149 dwarf2_name (die, cu));
17150 return 0;
17151 }
17152
17153 return 1;
17154}
17155
09ba997f 17156/* See read.h. */
9a49df9d 17157
09ba997f
TT
17158struct type *
17159dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17160{
09ba997f 17161 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17162 struct type *int_type;
17163
17164 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17165#define TRY_TYPE(F) \
17166 int_type = (unsigned_p \
17167 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17168 : objfile_type (objfile)->builtin_ ## F); \
17169 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17170 return int_type
17171
17172 TRY_TYPE (char);
17173 TRY_TYPE (short);
17174 TRY_TYPE (int);
17175 TRY_TYPE (long);
17176 TRY_TYPE (long_long);
17177
17178#undef TRY_TYPE
17179
17180 gdb_assert_not_reached ("unable to find suitable integer type");
17181}
17182
09ba997f 17183/* See read.h. */
11a8b164 17184
09ba997f
TT
17185struct type *
17186dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17187{
09ba997f
TT
17188 int addr_size = this->addr_size ();
17189 return int_type (addr_size, unsigned_p);
11a8b164
AB
17190}
17191
b86352cf
AB
17192/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17193 present (which is valid) then compute the default type based on the
17194 compilation units address size. */
17195
17196static struct type *
17197read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17198{
17199 struct type *index_type = die_type (die, cu);
17200
17201 /* Dwarf-2 specifications explicitly allows to create subrange types
17202 without specifying a base type.
17203 In that case, the base type must be set to the type of
17204 the lower bound, upper bound or count, in that order, if any of these
17205 three attributes references an object that has a type.
17206 If no base type is found, the Dwarf-2 specifications say that
17207 a signed integer type of size equal to the size of an address should
17208 be used.
17209 For the following C code: `extern char gdb_int [];'
17210 GCC produces an empty range DIE.
17211 FIXME: muller/2010-05-28: Possible references to object for low bound,
17212 high bound or count are not yet handled by this code. */
17213 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17214 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17215
17216 return index_type;
17217}
17218
a02abb62
JB
17219/* Read the given DW_AT_subrange DIE. */
17220
f792889a 17221static struct type *
a02abb62
JB
17222read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17223{
4c9ad8c2 17224 struct type *base_type, *orig_base_type;
a02abb62
JB
17225 struct type *range_type;
17226 struct attribute *attr;
729efb13 17227 struct dynamic_prop low, high;
4fae6e18 17228 int low_default_is_valid;
c451ebe5 17229 int high_bound_is_count = 0;
15d034d0 17230 const char *name;
d359392f 17231 ULONGEST negative_mask;
e77813c8 17232
b86352cf
AB
17233 orig_base_type = read_subrange_index_type (die, cu);
17234
4c9ad8c2
TT
17235 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17236 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17237 creating the range type, but we use the result of check_typedef
17238 when examining properties of the type. */
17239 base_type = check_typedef (orig_base_type);
a02abb62 17240
7e314c57
JK
17241 /* The die_type call above may have already set the type for this DIE. */
17242 range_type = get_die_type (die, cu);
17243 if (range_type)
17244 return range_type;
17245
729efb13
SA
17246 low.kind = PROP_CONST;
17247 high.kind = PROP_CONST;
17248 high.data.const_val = 0;
17249
4fae6e18
JK
17250 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17251 omitting DW_AT_lower_bound. */
17252 switch (cu->language)
6e70227d 17253 {
4fae6e18
JK
17254 case language_c:
17255 case language_cplus:
729efb13 17256 low.data.const_val = 0;
4fae6e18
JK
17257 low_default_is_valid = 1;
17258 break;
17259 case language_fortran:
729efb13 17260 low.data.const_val = 1;
4fae6e18
JK
17261 low_default_is_valid = 1;
17262 break;
17263 case language_d:
4fae6e18 17264 case language_objc:
c44af4eb 17265 case language_rust:
729efb13 17266 low.data.const_val = 0;
4fae6e18
JK
17267 low_default_is_valid = (cu->header.version >= 4);
17268 break;
17269 case language_ada:
17270 case language_m2:
17271 case language_pascal:
729efb13 17272 low.data.const_val = 1;
4fae6e18
JK
17273 low_default_is_valid = (cu->header.version >= 4);
17274 break;
17275 default:
729efb13 17276 low.data.const_val = 0;
4fae6e18
JK
17277 low_default_is_valid = 0;
17278 break;
a02abb62
JB
17279 }
17280
e142c38c 17281 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17282 if (attr != nullptr)
9a49df9d 17283 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17284 else if (!low_default_is_valid)
b98664d3 17285 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17286 "- DIE at %s [in module %s]"),
17287 sect_offset_str (die->sect_off),
518817b3 17288 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17289
506f5c41
TV
17290 struct attribute *attr_ub, *attr_count;
17291 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17292 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17293 {
506f5c41 17294 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17295 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17296 {
c451ebe5
SA
17297 /* If bounds are constant do the final calculation here. */
17298 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17299 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17300 else
17301 high_bound_is_count = 1;
c2ff108b 17302 }
506f5c41
TV
17303 else
17304 {
17305 if (attr_ub != NULL)
17306 complaint (_("Unresolved DW_AT_upper_bound "
17307 "- DIE at %s [in module %s]"),
17308 sect_offset_str (die->sect_off),
17309 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17310 if (attr_count != NULL)
17311 complaint (_("Unresolved DW_AT_count "
17312 "- DIE at %s [in module %s]"),
17313 sect_offset_str (die->sect_off),
17314 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17315 }
e77813c8 17316 }
a02abb62 17317
4e962e74
TT
17318 LONGEST bias = 0;
17319 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17320 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17321 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17322
dbb9c2b1
JB
17323 /* Normally, the DWARF producers are expected to use a signed
17324 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17325 But this is unfortunately not always the case, as witnessed
17326 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17327 is used instead. To work around that ambiguity, we treat
17328 the bounds as signed, and thus sign-extend their values, when
17329 the base type is signed. */
6e70227d 17330 negative_mask =
d359392f 17331 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17332 if (low.kind == PROP_CONST
17333 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17334 low.data.const_val |= negative_mask;
17335 if (high.kind == PROP_CONST
17336 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17337 high.data.const_val |= negative_mask;
43bbcdc2 17338
5bbd8269
AB
17339 /* Check for bit and byte strides. */
17340 struct dynamic_prop byte_stride_prop;
17341 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17342 if (attr_byte_stride != nullptr)
17343 {
09ba997f 17344 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17345 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17346 prop_type);
17347 }
17348
17349 struct dynamic_prop bit_stride_prop;
17350 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17351 if (attr_bit_stride != nullptr)
17352 {
17353 /* It only makes sense to have either a bit or byte stride. */
17354 if (attr_byte_stride != nullptr)
17355 {
17356 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17357 "- DIE at %s [in module %s]"),
17358 sect_offset_str (die->sect_off),
17359 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17360 attr_bit_stride = nullptr;
17361 }
17362 else
17363 {
09ba997f 17364 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17365 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17366 prop_type);
17367 }
17368 }
17369
17370 if (attr_byte_stride != nullptr
17371 || attr_bit_stride != nullptr)
17372 {
17373 bool byte_stride_p = (attr_byte_stride != nullptr);
17374 struct dynamic_prop *stride
17375 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17376
17377 range_type
17378 = create_range_type_with_stride (NULL, orig_base_type, &low,
17379 &high, bias, stride, byte_stride_p);
17380 }
17381 else
17382 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17383
c451ebe5
SA
17384 if (high_bound_is_count)
17385 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17386
c2ff108b
JK
17387 /* Ada expects an empty array on no boundary attributes. */
17388 if (attr == NULL && cu->language != language_ada)
729efb13 17389 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17390
39cbfefa
DJ
17391 name = dwarf2_name (die, cu);
17392 if (name)
17393 TYPE_NAME (range_type) = name;
6e70227d 17394
e142c38c 17395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17396 if (attr != nullptr)
a02abb62
JB
17397 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17398
2b4424c3
TT
17399 maybe_set_alignment (cu, die, range_type);
17400
7e314c57
JK
17401 set_die_type (die, range_type, cu);
17402
17403 /* set_die_type should be already done. */
b4ba55a1
JB
17404 set_descriptive_type (range_type, die, cu);
17405
7e314c57 17406 return range_type;
a02abb62 17407}
6e70227d 17408
f792889a 17409static struct type *
81a17f79
JB
17410read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17411{
17412 struct type *type;
81a17f79 17413
518817b3
SM
17414 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17415 NULL);
0114d602 17416 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17417
74a2f8ff 17418 /* In Ada, an unspecified type is typically used when the description
85102364 17419 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17420 such a type, we treat it as a stub, and try to resolve it later on,
17421 when needed. */
17422 if (cu->language == language_ada)
17423 TYPE_STUB (type) = 1;
17424
f792889a 17425 return set_die_type (die, type, cu);
81a17f79 17426}
a02abb62 17427
639d11d3
DC
17428/* Read a single die and all its descendents. Set the die's sibling
17429 field to NULL; set other fields in the die correctly, and set all
17430 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17431 location of the info_ptr after reading all of those dies. PARENT
17432 is the parent of the die in question. */
17433
17434static struct die_info *
dee91e82 17435read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17436 const gdb_byte *info_ptr,
17437 const gdb_byte **new_info_ptr,
dee91e82 17438 struct die_info *parent)
639d11d3
DC
17439{
17440 struct die_info *die;
d521ce57 17441 const gdb_byte *cur_ptr;
639d11d3 17442
3e225074 17443 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17444 if (die == NULL)
17445 {
17446 *new_info_ptr = cur_ptr;
17447 return NULL;
17448 }
93311388 17449 store_in_ref_table (die, reader->cu);
639d11d3 17450
3e225074 17451 if (die->has_children)
bf6af496 17452 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17453 else
17454 {
17455 die->child = NULL;
17456 *new_info_ptr = cur_ptr;
17457 }
17458
17459 die->sibling = NULL;
17460 die->parent = parent;
17461 return die;
17462}
17463
17464/* Read a die, all of its descendents, and all of its siblings; set
17465 all of the fields of all of the dies correctly. Arguments are as
17466 in read_die_and_children. */
17467
17468static struct die_info *
bf6af496 17469read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17470 const gdb_byte *info_ptr,
17471 const gdb_byte **new_info_ptr,
bf6af496 17472 struct die_info *parent)
639d11d3
DC
17473{
17474 struct die_info *first_die, *last_sibling;
d521ce57 17475 const gdb_byte *cur_ptr;
639d11d3 17476
c906108c 17477 cur_ptr = info_ptr;
639d11d3
DC
17478 first_die = last_sibling = NULL;
17479
17480 while (1)
c906108c 17481 {
639d11d3 17482 struct die_info *die
dee91e82 17483 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17484
1d325ec1 17485 if (die == NULL)
c906108c 17486 {
639d11d3
DC
17487 *new_info_ptr = cur_ptr;
17488 return first_die;
c906108c 17489 }
1d325ec1
DJ
17490
17491 if (!first_die)
17492 first_die = die;
c906108c 17493 else
1d325ec1
DJ
17494 last_sibling->sibling = die;
17495
17496 last_sibling = die;
c906108c 17497 }
c906108c
SS
17498}
17499
bf6af496
DE
17500/* Read a die, all of its descendents, and all of its siblings; set
17501 all of the fields of all of the dies correctly. Arguments are as
17502 in read_die_and_children.
17503 This the main entry point for reading a DIE and all its children. */
17504
17505static struct die_info *
17506read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17507 const gdb_byte *info_ptr,
17508 const gdb_byte **new_info_ptr,
bf6af496
DE
17509 struct die_info *parent)
17510{
17511 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17512 new_info_ptr, parent);
17513
b4f54984 17514 if (dwarf_die_debug)
bf6af496
DE
17515 {
17516 fprintf_unfiltered (gdb_stdlog,
17517 "Read die from %s@0x%x of %s:\n",
96b79293 17518 reader->die_section->get_name (),
bf6af496
DE
17519 (unsigned) (info_ptr - reader->die_section->buffer),
17520 bfd_get_filename (reader->abfd));
b4f54984 17521 dump_die (die, dwarf_die_debug);
bf6af496
DE
17522 }
17523
17524 return die;
17525}
17526
3019eac3
DE
17527/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17528 attributes.
17529 The caller is responsible for filling in the extra attributes
17530 and updating (*DIEP)->num_attrs.
17531 Set DIEP to point to a newly allocated die with its information,
3e225074 17532 except for its child, sibling, and parent fields. */
93311388 17533
d521ce57 17534static const gdb_byte *
3019eac3 17535read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17536 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17537 int num_extra_attrs)
93311388 17538{
b64f50a1 17539 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17540 struct abbrev_info *abbrev;
17541 struct die_info *die;
17542 struct dwarf2_cu *cu = reader->cu;
17543 bfd *abfd = reader->abfd;
17544
9c541725 17545 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17546 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17547 info_ptr += bytes_read;
17548 if (!abbrev_number)
17549 {
17550 *diep = NULL;
93311388
DE
17551 return info_ptr;
17552 }
17553
685af9cd 17554 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17555 if (!abbrev)
348e048f
DE
17556 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17557 abbrev_number,
17558 bfd_get_filename (abfd));
17559
3019eac3 17560 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17561 die->sect_off = sect_off;
93311388
DE
17562 die->tag = abbrev->tag;
17563 die->abbrev = abbrev_number;
3e225074 17564 die->has_children = abbrev->has_children;
93311388 17565
3019eac3
DE
17566 /* Make the result usable.
17567 The caller needs to update num_attrs after adding the extra
17568 attributes. */
93311388
DE
17569 die->num_attrs = abbrev->num_attrs;
17570
18a8505e 17571 std::vector<int> indexes_that_need_reprocess;
93311388 17572 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17573 {
17574 bool need_reprocess;
17575 info_ptr =
17576 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17577 info_ptr, &need_reprocess);
17578 if (need_reprocess)
17579 indexes_that_need_reprocess.push_back (i);
17580 }
17581
17582 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17583 if (attr != nullptr)
17584 cu->str_offsets_base = DW_UNSND (attr);
93311388 17585
18a8505e
AT
17586 auto maybe_addr_base = lookup_addr_base(die);
17587 if (maybe_addr_base.has_value ())
17588 cu->addr_base = *maybe_addr_base;
17589 for (int index : indexes_that_need_reprocess)
17590 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17591 *diep = die;
93311388
DE
17592 return info_ptr;
17593}
17594
3019eac3
DE
17595/* Read a die and all its attributes.
17596 Set DIEP to point to a newly allocated die with its information,
3e225074 17597 except for its child, sibling, and parent fields. */
3019eac3 17598
d521ce57 17599static const gdb_byte *
3019eac3 17600read_full_die (const struct die_reader_specs *reader,
3e225074 17601 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17602{
d521ce57 17603 const gdb_byte *result;
bf6af496 17604
3e225074 17605 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17606
b4f54984 17607 if (dwarf_die_debug)
bf6af496
DE
17608 {
17609 fprintf_unfiltered (gdb_stdlog,
17610 "Read die from %s@0x%x of %s:\n",
96b79293 17611 reader->die_section->get_name (),
bf6af496
DE
17612 (unsigned) (info_ptr - reader->die_section->buffer),
17613 bfd_get_filename (reader->abfd));
b4f54984 17614 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17615 }
17616
17617 return result;
3019eac3 17618}
433df2d4 17619\f
c906108c 17620
72bf9492
DJ
17621/* Returns nonzero if TAG represents a type that we might generate a partial
17622 symbol for. */
17623
17624static int
17625is_type_tag_for_partial (int tag)
17626{
17627 switch (tag)
17628 {
17629#if 0
17630 /* Some types that would be reasonable to generate partial symbols for,
17631 that we don't at present. */
17632 case DW_TAG_array_type:
17633 case DW_TAG_file_type:
17634 case DW_TAG_ptr_to_member_type:
17635 case DW_TAG_set_type:
17636 case DW_TAG_string_type:
17637 case DW_TAG_subroutine_type:
17638#endif
17639 case DW_TAG_base_type:
17640 case DW_TAG_class_type:
680b30c7 17641 case DW_TAG_interface_type:
72bf9492
DJ
17642 case DW_TAG_enumeration_type:
17643 case DW_TAG_structure_type:
17644 case DW_TAG_subrange_type:
17645 case DW_TAG_typedef:
17646 case DW_TAG_union_type:
17647 return 1;
17648 default:
17649 return 0;
17650 }
17651}
17652
17653/* Load all DIEs that are interesting for partial symbols into memory. */
17654
17655static struct partial_die_info *
dee91e82 17656load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17657 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17658{
dee91e82 17659 struct dwarf2_cu *cu = reader->cu;
518817b3 17660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17661 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17662 unsigned int bytes_read;
5afb4e99 17663 unsigned int load_all = 0;
72bf9492
DJ
17664 int nesting_level = 1;
17665
17666 parent_die = NULL;
17667 last_die = NULL;
17668
7adf1e79
DE
17669 gdb_assert (cu->per_cu != NULL);
17670 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17671 load_all = 1;
17672
72bf9492
DJ
17673 cu->partial_dies
17674 = htab_create_alloc_ex (cu->header.length / 12,
17675 partial_die_hash,
17676 partial_die_eq,
17677 NULL,
17678 &cu->comp_unit_obstack,
17679 hashtab_obstack_allocate,
17680 dummy_obstack_deallocate);
17681
72bf9492
DJ
17682 while (1)
17683 {
685af9cd 17684 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17685
17686 /* A NULL abbrev means the end of a series of children. */
17687 if (abbrev == NULL)
17688 {
17689 if (--nesting_level == 0)
cd9983dd
YQ
17690 return first_die;
17691
72bf9492
DJ
17692 info_ptr += bytes_read;
17693 last_die = parent_die;
17694 parent_die = parent_die->die_parent;
17695 continue;
17696 }
17697
98bfdba5
PA
17698 /* Check for template arguments. We never save these; if
17699 they're seen, we just mark the parent, and go on our way. */
17700 if (parent_die != NULL
17701 && cu->language == language_cplus
17702 && (abbrev->tag == DW_TAG_template_type_param
17703 || abbrev->tag == DW_TAG_template_value_param))
17704 {
17705 parent_die->has_template_arguments = 1;
17706
17707 if (!load_all)
17708 {
17709 /* We don't need a partial DIE for the template argument. */
dee91e82 17710 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17711 continue;
17712 }
17713 }
17714
0d99eb77 17715 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17716 Skip their other children. */
17717 if (!load_all
17718 && cu->language == language_cplus
17719 && parent_die != NULL
17720 && parent_die->tag == DW_TAG_subprogram)
17721 {
dee91e82 17722 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17723 continue;
17724 }
17725
5afb4e99
DJ
17726 /* Check whether this DIE is interesting enough to save. Normally
17727 we would not be interested in members here, but there may be
17728 later variables referencing them via DW_AT_specification (for
17729 static members). */
17730 if (!load_all
17731 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17732 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17733 && abbrev->tag != DW_TAG_enumerator
17734 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17735 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17736 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17737 && abbrev->tag != DW_TAG_variable
5afb4e99 17738 && abbrev->tag != DW_TAG_namespace
f55ee35c 17739 && abbrev->tag != DW_TAG_module
95554aad 17740 && abbrev->tag != DW_TAG_member
74921315
KS
17741 && abbrev->tag != DW_TAG_imported_unit
17742 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17743 {
17744 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17745 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17746 continue;
17747 }
17748
6f06d47b
YQ
17749 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17750 abbrev);
cd9983dd 17751
48fbe735 17752 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17753
17754 /* This two-pass algorithm for processing partial symbols has a
17755 high cost in cache pressure. Thus, handle some simple cases
17756 here which cover the majority of C partial symbols. DIEs
17757 which neither have specification tags in them, nor could have
17758 specification tags elsewhere pointing at them, can simply be
17759 processed and discarded.
17760
17761 This segment is also optional; scan_partial_symbols and
17762 add_partial_symbol will handle these DIEs if we chain
17763 them in normally. When compilers which do not emit large
17764 quantities of duplicate debug information are more common,
17765 this code can probably be removed. */
17766
17767 /* Any complete simple types at the top level (pretty much all
17768 of them, for a language without namespaces), can be processed
17769 directly. */
17770 if (parent_die == NULL
cd9983dd
YQ
17771 && pdi.has_specification == 0
17772 && pdi.is_declaration == 0
17773 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17774 || pdi.tag == DW_TAG_base_type
17775 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17776 {
cd9983dd 17777 if (building_psymtab && pdi.name != NULL)
31edb802 17778 add_psymbol_to_list (pdi.name, false,
79748972 17779 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17780 psymbol_placement::STATIC,
1762568f 17781 0, cu->language, objfile);
cd9983dd 17782 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17783 continue;
17784 }
17785
d8228535
JK
17786 /* The exception for DW_TAG_typedef with has_children above is
17787 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17788 type_name_or_error will error on such types later.
d8228535
JK
17789
17790 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17791 it could not find the child DIEs referenced later, this is checked
17792 above. In correct DWARF DW_TAG_typedef should have no children. */
17793
cd9983dd 17794 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17795 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17796 "- DIE at %s [in module %s]"),
cd9983dd 17797 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17798
72bf9492
DJ
17799 /* If we're at the second level, and we're an enumerator, and
17800 our parent has no specification (meaning possibly lives in a
17801 namespace elsewhere), then we can add the partial symbol now
17802 instead of queueing it. */
cd9983dd 17803 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17804 && parent_die != NULL
17805 && parent_die->die_parent == NULL
17806 && parent_die->tag == DW_TAG_enumeration_type
17807 && parent_die->has_specification == 0)
17808 {
cd9983dd 17809 if (pdi.name == NULL)
b98664d3 17810 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17811 else if (building_psymtab)
31edb802 17812 add_psymbol_to_list (pdi.name, false,
79748972 17813 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17814 cu->language == language_cplus
75aedd27
TT
17815 ? psymbol_placement::GLOBAL
17816 : psymbol_placement::STATIC,
1762568f 17817 0, cu->language, objfile);
72bf9492 17818
cd9983dd 17819 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17820 continue;
17821 }
17822
cd9983dd 17823 struct partial_die_info *part_die
6f06d47b 17824 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17825
72bf9492
DJ
17826 /* We'll save this DIE so link it in. */
17827 part_die->die_parent = parent_die;
17828 part_die->die_sibling = NULL;
17829 part_die->die_child = NULL;
17830
17831 if (last_die && last_die == parent_die)
17832 last_die->die_child = part_die;
17833 else if (last_die)
17834 last_die->die_sibling = part_die;
17835
17836 last_die = part_die;
17837
17838 if (first_die == NULL)
17839 first_die = part_die;
17840
17841 /* Maybe add the DIE to the hash table. Not all DIEs that we
17842 find interesting need to be in the hash table, because we
17843 also have the parent/sibling/child chains; only those that we
17844 might refer to by offset later during partial symbol reading.
17845
17846 For now this means things that might have be the target of a
17847 DW_AT_specification, DW_AT_abstract_origin, or
17848 DW_AT_extension. DW_AT_extension will refer only to
17849 namespaces; DW_AT_abstract_origin refers to functions (and
17850 many things under the function DIE, but we do not recurse
17851 into function DIEs during partial symbol reading) and
17852 possibly variables as well; DW_AT_specification refers to
17853 declarations. Declarations ought to have the DW_AT_declaration
17854 flag. It happens that GCC forgets to put it in sometimes, but
17855 only for functions, not for types.
17856
17857 Adding more things than necessary to the hash table is harmless
17858 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17859 wasted time in find_partial_die, when we reread the compilation
17860 unit with load_all_dies set. */
72bf9492 17861
5afb4e99 17862 if (load_all
72929c62 17863 || abbrev->tag == DW_TAG_constant
5afb4e99 17864 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17865 || abbrev->tag == DW_TAG_variable
17866 || abbrev->tag == DW_TAG_namespace
17867 || part_die->is_declaration)
17868 {
17869 void **slot;
17870
17871 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17872 to_underlying (part_die->sect_off),
17873 INSERT);
72bf9492
DJ
17874 *slot = part_die;
17875 }
17876
72bf9492 17877 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17878 we have no reason to follow the children of structures; for other
98bfdba5
PA
17879 languages we have to, so that we can get at method physnames
17880 to infer fully qualified class names, for DW_AT_specification,
17881 and for C++ template arguments. For C++, we also look one level
17882 inside functions to find template arguments (if the name of the
17883 function does not already contain the template arguments).
bc30ff58 17884
0a4b0913
AB
17885 For Ada and Fortran, we need to scan the children of subprograms
17886 and lexical blocks as well because these languages allow the
17887 definition of nested entities that could be interesting for the
17888 debugger, such as nested subprograms for instance. */
72bf9492 17889 if (last_die->has_children
5afb4e99
DJ
17890 && (load_all
17891 || last_die->tag == DW_TAG_namespace
f55ee35c 17892 || last_die->tag == DW_TAG_module
72bf9492 17893 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17894 || (cu->language == language_cplus
17895 && last_die->tag == DW_TAG_subprogram
17896 && (last_die->name == NULL
17897 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17898 || (cu->language != language_c
17899 && (last_die->tag == DW_TAG_class_type
680b30c7 17900 || last_die->tag == DW_TAG_interface_type
72bf9492 17901 || last_die->tag == DW_TAG_structure_type
bc30ff58 17902 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17903 || ((cu->language == language_ada
17904 || cu->language == language_fortran)
bc30ff58
JB
17905 && (last_die->tag == DW_TAG_subprogram
17906 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17907 {
17908 nesting_level++;
17909 parent_die = last_die;
17910 continue;
17911 }
17912
17913 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17914 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17915
17916 /* Back to the top, do it again. */
17917 }
17918}
17919
6f06d47b
YQ
17920partial_die_info::partial_die_info (sect_offset sect_off_,
17921 struct abbrev_info *abbrev)
17922 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17923{
17924}
17925
35cc7ed7
YQ
17926/* Read a minimal amount of information into the minimal die structure.
17927 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17928
48fbe735
YQ
17929const gdb_byte *
17930partial_die_info::read (const struct die_reader_specs *reader,
17931 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17932{
dee91e82 17933 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17934 struct dwarf2_per_objfile *dwarf2_per_objfile
17935 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17936 unsigned int i;
c5aa993b 17937 int has_low_pc_attr = 0;
c906108c 17938 int has_high_pc_attr = 0;
91da1414 17939 int high_pc_relative = 0;
c906108c 17940
18a8505e 17941 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17942 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17943 {
18a8505e
AT
17944 bool need_reprocess;
17945 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17946 info_ptr, &need_reprocess);
17947 /* String and address offsets that need to do the reprocessing have
17948 already been read at this point, so there is no need to wait until
17949 the loop terminates to do the reprocessing. */
17950 if (need_reprocess)
17951 read_attribute_reprocess (reader, &attr_vec[i]);
17952 attribute &attr = attr_vec[i];
c906108c 17953 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17954 partial symbol table. */
c906108c
SS
17955 switch (attr.name)
17956 {
17957 case DW_AT_name:
48fbe735 17958 switch (tag)
71c25dea
TT
17959 {
17960 case DW_TAG_compile_unit:
95554aad 17961 case DW_TAG_partial_unit:
348e048f 17962 case DW_TAG_type_unit:
71c25dea
TT
17963 /* Compilation units have a DW_AT_name that is a filename, not
17964 a source language identifier. */
17965 case DW_TAG_enumeration_type:
17966 case DW_TAG_enumerator:
17967 /* These tags always have simple identifiers already; no need
17968 to canonicalize them. */
48fbe735 17969 name = DW_STRING (&attr);
71c25dea
TT
17970 break;
17971 default:
48fbe735
YQ
17972 {
17973 struct objfile *objfile = dwarf2_per_objfile->objfile;
17974
17975 name
17976 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17977 &objfile->per_bfd->storage_obstack);
17978 }
71c25dea
TT
17979 break;
17980 }
c906108c 17981 break;
31ef98ae 17982 case DW_AT_linkage_name:
c906108c 17983 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17984 /* Note that both forms of linkage name might appear. We
17985 assume they will be the same, and we only store the last
17986 one we see. */
48fbe735 17987 linkage_name = DW_STRING (&attr);
c906108c
SS
17988 break;
17989 case DW_AT_low_pc:
17990 has_low_pc_attr = 1;
cd6c91b4 17991 lowpc = attr.value_as_address ();
c906108c
SS
17992 break;
17993 case DW_AT_high_pc:
17994 has_high_pc_attr = 1;
cd6c91b4
TT
17995 highpc = attr.value_as_address ();
17996 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 17997 high_pc_relative = 1;
c906108c
SS
17998 break;
17999 case DW_AT_location:
0963b4bd 18000 /* Support the .debug_loc offsets. */
4fc6c0d5 18001 if (attr.form_is_block ())
8e19ed76 18002 {
48fbe735 18003 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18004 }
cd6c91b4 18005 else if (attr.form_is_section_offset ())
8e19ed76 18006 {
4d3c2250 18007 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18008 }
18009 else
18010 {
4d3c2250
KB
18011 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18012 "partial symbol information");
8e19ed76 18013 }
c906108c 18014 break;
c906108c 18015 case DW_AT_external:
48fbe735 18016 is_external = DW_UNSND (&attr);
c906108c
SS
18017 break;
18018 case DW_AT_declaration:
48fbe735 18019 is_declaration = DW_UNSND (&attr);
c906108c
SS
18020 break;
18021 case DW_AT_type:
48fbe735 18022 has_type = 1;
c906108c
SS
18023 break;
18024 case DW_AT_abstract_origin:
18025 case DW_AT_specification:
72bf9492 18026 case DW_AT_extension:
48fbe735
YQ
18027 has_specification = 1;
18028 spec_offset = dwarf2_get_ref_die_offset (&attr);
18029 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18030 || cu->per_cu->is_dwz);
c906108c
SS
18031 break;
18032 case DW_AT_sibling:
18033 /* Ignore absolute siblings, they might point outside of
18034 the current compile unit. */
18035 if (attr.form == DW_FORM_ref_addr)
b98664d3 18036 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18037 else
b9502d3f 18038 {
48fbe735 18039 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18040 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18041 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18042
18043 if (sibling_ptr < info_ptr)
b98664d3 18044 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18045 else if (sibling_ptr > reader->buffer_end)
18046 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18047 else
48fbe735 18048 sibling = sibling_ptr;
b9502d3f 18049 }
c906108c 18050 break;
fa4028e9 18051 case DW_AT_byte_size:
48fbe735 18052 has_byte_size = 1;
fa4028e9 18053 break;
ff908ebf 18054 case DW_AT_const_value:
48fbe735 18055 has_const_value = 1;
ff908ebf 18056 break;
68511cec
CES
18057 case DW_AT_calling_convention:
18058 /* DWARF doesn't provide a way to identify a program's source-level
18059 entry point. DW_AT_calling_convention attributes are only meant
18060 to describe functions' calling conventions.
18061
18062 However, because it's a necessary piece of information in
0c1b455e
TT
18063 Fortran, and before DWARF 4 DW_CC_program was the only
18064 piece of debugging information whose definition refers to
18065 a 'main program' at all, several compilers marked Fortran
18066 main programs with DW_CC_program --- even when those
18067 functions use the standard calling conventions.
18068
18069 Although DWARF now specifies a way to provide this
18070 information, we support this practice for backward
18071 compatibility. */
68511cec 18072 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18073 && cu->language == language_fortran)
48fbe735 18074 main_subprogram = 1;
68511cec 18075 break;
481860b3
GB
18076 case DW_AT_inline:
18077 if (DW_UNSND (&attr) == DW_INL_inlined
18078 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18079 may_be_inlined = 1;
481860b3 18080 break;
95554aad
TT
18081
18082 case DW_AT_import:
48fbe735 18083 if (tag == DW_TAG_imported_unit)
36586728 18084 {
48fbe735
YQ
18085 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18086 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18087 || cu->per_cu->is_dwz);
18088 }
95554aad
TT
18089 break;
18090
0c1b455e 18091 case DW_AT_main_subprogram:
48fbe735 18092 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18093 break;
18094
05caa1d2
TT
18095 case DW_AT_ranges:
18096 {
18097 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18098 but that requires a full DIE, so instead we just
18099 reimplement it. */
18100 int need_ranges_base = tag != DW_TAG_compile_unit;
18101 unsigned int ranges_offset = (DW_UNSND (&attr)
18102 + (need_ranges_base
18103 ? cu->ranges_base
18104 : 0));
18105
18106 /* Value of the DW_AT_ranges attribute is the offset in the
18107 .debug_ranges section. */
18108 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18109 nullptr))
18110 has_pc_info = 1;
18111 }
18112 break;
18113
c906108c
SS
18114 default:
18115 break;
18116 }
18117 }
18118
10d06d82
TT
18119 /* For Ada, if both the name and the linkage name appear, we prefer
18120 the latter. This lets "catch exception" work better, regardless
18121 of the order in which the name and linkage name were emitted.
18122 Really, though, this is just a workaround for the fact that gdb
18123 doesn't store both the name and the linkage name. */
18124 if (cu->language == language_ada && linkage_name != nullptr)
18125 name = linkage_name;
18126
91da1414 18127 if (high_pc_relative)
48fbe735 18128 highpc += lowpc;
91da1414 18129
9373cf26
JK
18130 if (has_low_pc_attr && has_high_pc_attr)
18131 {
18132 /* When using the GNU linker, .gnu.linkonce. sections are used to
18133 eliminate duplicate copies of functions and vtables and such.
18134 The linker will arbitrarily choose one and discard the others.
18135 The AT_*_pc values for such functions refer to local labels in
18136 these sections. If the section from that file was discarded, the
18137 labels are not in the output, so the relocs get a value of 0.
18138 If this is a discarded function, mark the pc bounds as invalid,
18139 so that GDB will ignore it. */
48fbe735 18140 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18141 {
48fbe735 18142 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18143 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18144
b98664d3 18145 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18146 "for DIE at %s [in module %s]"),
48fbe735
YQ
18147 paddress (gdbarch, lowpc),
18148 sect_offset_str (sect_off),
9d8780f0 18149 objfile_name (objfile));
9373cf26
JK
18150 }
18151 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18152 else if (lowpc >= highpc)
9373cf26 18153 {
48fbe735 18154 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18155 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18156
b98664d3 18157 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18158 "for DIE at %s [in module %s]"),
48fbe735
YQ
18159 paddress (gdbarch, lowpc),
18160 paddress (gdbarch, highpc),
18161 sect_offset_str (sect_off),
9c541725 18162 objfile_name (objfile));
9373cf26
JK
18163 }
18164 else
48fbe735 18165 has_pc_info = 1;
9373cf26 18166 }
85cbf3d3 18167
c906108c
SS
18168 return info_ptr;
18169}
18170
72bf9492
DJ
18171/* Find a cached partial DIE at OFFSET in CU. */
18172
d590ff25
YQ
18173struct partial_die_info *
18174dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18175{
18176 struct partial_die_info *lookup_die = NULL;
6f06d47b 18177 struct partial_die_info part_die (sect_off);
72bf9492 18178
9a3c8263 18179 lookup_die = ((struct partial_die_info *)
d590ff25 18180 htab_find_with_hash (partial_dies, &part_die,
9c541725 18181 to_underlying (sect_off)));
72bf9492 18182
72bf9492
DJ
18183 return lookup_die;
18184}
18185
348e048f
DE
18186/* Find a partial DIE at OFFSET, which may or may not be in CU,
18187 except in the case of .debug_types DIEs which do not reference
18188 outside their CU (they do however referencing other types via
55f1336d 18189 DW_FORM_ref_sig8). */
72bf9492 18190
122cf0f2 18191static const struct cu_partial_die_info
9c541725 18192find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18193{
518817b3
SM
18194 struct dwarf2_per_objfile *dwarf2_per_objfile
18195 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18196 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18197 struct dwarf2_per_cu_data *per_cu = NULL;
18198 struct partial_die_info *pd = NULL;
72bf9492 18199
36586728 18200 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18201 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18202 {
d590ff25 18203 pd = cu->find_partial_die (sect_off);
5afb4e99 18204 if (pd != NULL)
fb816e8b 18205 return { cu, pd };
0d99eb77
DE
18206 /* We missed recording what we needed.
18207 Load all dies and try again. */
18208 per_cu = cu->per_cu;
5afb4e99 18209 }
0d99eb77
DE
18210 else
18211 {
18212 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18213 if (cu->per_cu->is_debug_types)
0d99eb77 18214 {
9d8780f0
SM
18215 error (_("Dwarf Error: Type Unit at offset %s contains"
18216 " external reference to offset %s [in module %s].\n"),
18217 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18218 bfd_get_filename (objfile->obfd));
18219 }
9c541725 18220 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18221 dwarf2_per_objfile);
72bf9492 18222
0d99eb77
DE
18223 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18224 load_partial_comp_unit (per_cu);
ae038cb0 18225
0d99eb77 18226 per_cu->cu->last_used = 0;
d590ff25 18227 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18228 }
5afb4e99 18229
dee91e82
DE
18230 /* If we didn't find it, and not all dies have been loaded,
18231 load them all and try again. */
18232
5afb4e99
DJ
18233 if (pd == NULL && per_cu->load_all_dies == 0)
18234 {
5afb4e99 18235 per_cu->load_all_dies = 1;
fd820528
DE
18236
18237 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18238 THIS_CU->cu may already be in use. So we can't just free it and
18239 replace its DIEs with the ones we read in. Instead, we leave those
18240 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18241 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18242 set. */
dee91e82 18243 load_partial_comp_unit (per_cu);
5afb4e99 18244
d590ff25 18245 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18246 }
18247
18248 if (pd == NULL)
18249 internal_error (__FILE__, __LINE__,
9d8780f0 18250 _("could not find partial DIE %s "
3e43a32a 18251 "in cache [from module %s]\n"),
9d8780f0 18252 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18253 return { per_cu->cu, pd };
72bf9492
DJ
18254}
18255
abc72ce4
DE
18256/* See if we can figure out if the class lives in a namespace. We do
18257 this by looking for a member function; its demangled name will
18258 contain namespace info, if there is any. */
18259
18260static void
18261guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18262 struct dwarf2_cu *cu)
18263{
18264 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18265 what template types look like, because the demangler
18266 frequently doesn't give the same name as the debug info. We
18267 could fix this by only using the demangled name to get the
18268 prefix (but see comment in read_structure_type). */
18269
18270 struct partial_die_info *real_pdi;
18271 struct partial_die_info *child_pdi;
18272
18273 /* If this DIE (this DIE's specification, if any) has a parent, then
18274 we should not do this. We'll prepend the parent's fully qualified
18275 name when we create the partial symbol. */
18276
18277 real_pdi = struct_pdi;
18278 while (real_pdi->has_specification)
fb816e8b 18279 {
122cf0f2
AB
18280 auto res = find_partial_die (real_pdi->spec_offset,
18281 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18282 real_pdi = res.pdi;
18283 cu = res.cu;
18284 }
abc72ce4
DE
18285
18286 if (real_pdi->die_parent != NULL)
18287 return;
18288
18289 for (child_pdi = struct_pdi->die_child;
18290 child_pdi != NULL;
18291 child_pdi = child_pdi->die_sibling)
18292 {
18293 if (child_pdi->tag == DW_TAG_subprogram
18294 && child_pdi->linkage_name != NULL)
18295 {
43816ebc
TT
18296 gdb::unique_xmalloc_ptr<char> actual_class_name
18297 (language_class_name_from_physname (cu->language_defn,
18298 child_pdi->linkage_name));
abc72ce4
DE
18299 if (actual_class_name != NULL)
18300 {
518817b3 18301 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18302 struct_pdi->name
021887d8 18303 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 18304 actual_class_name.get ());
abc72ce4
DE
18305 }
18306 break;
18307 }
18308 }
18309}
18310
52356b79
YQ
18311void
18312partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18313{
abc72ce4
DE
18314 /* Once we've fixed up a die, there's no point in doing so again.
18315 This also avoids a memory leak if we were to call
18316 guess_partial_die_structure_name multiple times. */
52356b79 18317 if (fixup_called)
abc72ce4
DE
18318 return;
18319
72bf9492
DJ
18320 /* If we found a reference attribute and the DIE has no name, try
18321 to find a name in the referred to DIE. */
18322
52356b79 18323 if (name == NULL && has_specification)
72bf9492
DJ
18324 {
18325 struct partial_die_info *spec_die;
72bf9492 18326
122cf0f2 18327 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18328 spec_die = res.pdi;
18329 cu = res.cu;
72bf9492 18330
52356b79 18331 spec_die->fixup (cu);
72bf9492
DJ
18332
18333 if (spec_die->name)
18334 {
52356b79 18335 name = spec_die->name;
72bf9492
DJ
18336
18337 /* Copy DW_AT_external attribute if it is set. */
18338 if (spec_die->is_external)
52356b79 18339 is_external = spec_die->is_external;
72bf9492
DJ
18340 }
18341 }
18342
18343 /* Set default names for some unnamed DIEs. */
72bf9492 18344
52356b79
YQ
18345 if (name == NULL && tag == DW_TAG_namespace)
18346 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18347
abc72ce4
DE
18348 /* If there is no parent die to provide a namespace, and there are
18349 children, see if we can determine the namespace from their linkage
122d1940 18350 name. */
abc72ce4 18351 if (cu->language == language_cplus
fd5866f6 18352 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18353 && die_parent == NULL
18354 && has_children
18355 && (tag == DW_TAG_class_type
18356 || tag == DW_TAG_structure_type
18357 || tag == DW_TAG_union_type))
18358 guess_partial_die_structure_name (this, cu);
abc72ce4 18359
53832f31
TT
18360 /* GCC might emit a nameless struct or union that has a linkage
18361 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18362 if (name == NULL
18363 && (tag == DW_TAG_class_type
18364 || tag == DW_TAG_interface_type
18365 || tag == DW_TAG_structure_type
18366 || tag == DW_TAG_union_type)
18367 && linkage_name != NULL)
53832f31 18368 {
43816ebc
TT
18369 gdb::unique_xmalloc_ptr<char> demangled
18370 (gdb_demangle (linkage_name, DMGL_TYPES));
18371 if (demangled != nullptr)
53832f31 18372 {
96408a79
SA
18373 const char *base;
18374
18375 /* Strip any leading namespaces/classes, keep only the base name.
18376 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18377 base = strrchr (demangled.get (), ':');
18378 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18379 base++;
18380 else
43816ebc 18381 base = demangled.get ();
96408a79 18382
518817b3 18383 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 18384 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
18385 }
18386 }
18387
52356b79 18388 fixup_called = 1;
72bf9492
DJ
18389}
18390
18a8505e
AT
18391/* Process the attributes that had to be skipped in the first round. These
18392 attributes are the ones that need str_offsets_base or addr_base attributes.
18393 They could not have been processed in the first round, because at the time
18394 the values of str_offsets_base or addr_base may not have been known. */
18395void read_attribute_reprocess (const struct die_reader_specs *reader,
18396 struct attribute *attr)
18397{
18398 struct dwarf2_cu *cu = reader->cu;
18399 switch (attr->form)
18400 {
18401 case DW_FORM_addrx:
18402 case DW_FORM_GNU_addr_index:
18403 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18404 break;
18405 case DW_FORM_strx:
18406 case DW_FORM_strx1:
18407 case DW_FORM_strx2:
18408 case DW_FORM_strx3:
18409 case DW_FORM_strx4:
18410 case DW_FORM_GNU_str_index:
18411 {
18412 unsigned int str_index = DW_UNSND (attr);
18413 if (reader->dwo_file != NULL)
18414 {
18415 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18416 DW_STRING_IS_CANONICAL (attr) = 0;
18417 }
18418 else
18419 {
18420 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18421 DW_STRING_IS_CANONICAL (attr) = 0;
18422 }
18423 break;
18424 }
18425 default:
18426 gdb_assert_not_reached (_("Unexpected DWARF form."));
18427 }
18428}
18429
a8329558 18430/* Read an attribute value described by an attribute form. */
c906108c 18431
d521ce57 18432static const gdb_byte *
dee91e82
DE
18433read_attribute_value (const struct die_reader_specs *reader,
18434 struct attribute *attr, unsigned form,
18a8505e
AT
18435 LONGEST implicit_const, const gdb_byte *info_ptr,
18436 bool *need_reprocess)
c906108c 18437{
dee91e82 18438 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18439 struct dwarf2_per_objfile *dwarf2_per_objfile
18440 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18441 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18442 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18443 bfd *abfd = reader->abfd;
e7c27a73 18444 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18445 unsigned int bytes_read;
18446 struct dwarf_block *blk;
18a8505e 18447 *need_reprocess = false;
c906108c 18448
aead7601 18449 attr->form = (enum dwarf_form) form;
a8329558 18450 switch (form)
c906108c 18451 {
c906108c 18452 case DW_FORM_ref_addr:
ae411497 18453 if (cu->header.version == 2)
c8a7a66f
TT
18454 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18455 &bytes_read);
ae411497 18456 else
8266302d
TT
18457 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18458 &bytes_read);
ae411497
TT
18459 info_ptr += bytes_read;
18460 break;
36586728 18461 case DW_FORM_GNU_ref_alt:
8266302d 18462 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18463 info_ptr += bytes_read;
18464 break;
ae411497 18465 case DW_FORM_addr:
c8a7a66f 18466 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18467 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18468 info_ptr += bytes_read;
c906108c
SS
18469 break;
18470 case DW_FORM_block2:
7b5a2f43 18471 blk = dwarf_alloc_block (cu);
c906108c
SS
18472 blk->size = read_2_bytes (abfd, info_ptr);
18473 info_ptr += 2;
18474 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18475 info_ptr += blk->size;
18476 DW_BLOCK (attr) = blk;
18477 break;
18478 case DW_FORM_block4:
7b5a2f43 18479 blk = dwarf_alloc_block (cu);
c906108c
SS
18480 blk->size = read_4_bytes (abfd, info_ptr);
18481 info_ptr += 4;
18482 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18483 info_ptr += blk->size;
18484 DW_BLOCK (attr) = blk;
18485 break;
18486 case DW_FORM_data2:
18487 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18488 info_ptr += 2;
18489 break;
18490 case DW_FORM_data4:
18491 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18492 info_ptr += 4;
18493 break;
18494 case DW_FORM_data8:
18495 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18496 info_ptr += 8;
18497 break;
0224619f
JK
18498 case DW_FORM_data16:
18499 blk = dwarf_alloc_block (cu);
18500 blk->size = 16;
18501 blk->data = read_n_bytes (abfd, info_ptr, 16);
18502 info_ptr += 16;
18503 DW_BLOCK (attr) = blk;
18504 break;
2dc7f7b3 18505 case DW_FORM_sec_offset:
8266302d 18506 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18507 info_ptr += bytes_read;
18508 break;
c906108c 18509 case DW_FORM_string:
9b1c24c8 18510 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18511 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18512 info_ptr += bytes_read;
18513 break;
4bdf3d34 18514 case DW_FORM_strp:
36586728
TT
18515 if (!cu->per_cu->is_dwz)
18516 {
ed2dc618
SM
18517 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18518 abfd, info_ptr, cu_header,
36586728
TT
18519 &bytes_read);
18520 DW_STRING_IS_CANONICAL (attr) = 0;
18521 info_ptr += bytes_read;
18522 break;
18523 }
18524 /* FALLTHROUGH */
43988095
JK
18525 case DW_FORM_line_strp:
18526 if (!cu->per_cu->is_dwz)
18527 {
ed2dc618
SM
18528 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18529 abfd, info_ptr,
43988095
JK
18530 cu_header, &bytes_read);
18531 DW_STRING_IS_CANONICAL (attr) = 0;
18532 info_ptr += bytes_read;
18533 break;
18534 }
18535 /* FALLTHROUGH */
36586728
TT
18536 case DW_FORM_GNU_strp_alt:
18537 {
ed2dc618 18538 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18539 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18540 &bytes_read);
36586728 18541
ed2dc618
SM
18542 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18543 dwz, str_offset);
36586728
TT
18544 DW_STRING_IS_CANONICAL (attr) = 0;
18545 info_ptr += bytes_read;
18546 }
4bdf3d34 18547 break;
2dc7f7b3 18548 case DW_FORM_exprloc:
c906108c 18549 case DW_FORM_block:
7b5a2f43 18550 blk = dwarf_alloc_block (cu);
c906108c
SS
18551 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18552 info_ptr += bytes_read;
18553 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18554 info_ptr += blk->size;
18555 DW_BLOCK (attr) = blk;
18556 break;
18557 case DW_FORM_block1:
7b5a2f43 18558 blk = dwarf_alloc_block (cu);
c906108c
SS
18559 blk->size = read_1_byte (abfd, info_ptr);
18560 info_ptr += 1;
18561 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18562 info_ptr += blk->size;
18563 DW_BLOCK (attr) = blk;
18564 break;
18565 case DW_FORM_data1:
18566 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18567 info_ptr += 1;
18568 break;
18569 case DW_FORM_flag:
18570 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18571 info_ptr += 1;
18572 break;
2dc7f7b3
TT
18573 case DW_FORM_flag_present:
18574 DW_UNSND (attr) = 1;
18575 break;
c906108c
SS
18576 case DW_FORM_sdata:
18577 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18578 info_ptr += bytes_read;
18579 break;
18580 case DW_FORM_udata:
18a8505e 18581 case DW_FORM_rnglistx:
c906108c
SS
18582 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18583 info_ptr += bytes_read;
18584 break;
18585 case DW_FORM_ref1:
9c541725 18586 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18587 + read_1_byte (abfd, info_ptr));
c906108c
SS
18588 info_ptr += 1;
18589 break;
18590 case DW_FORM_ref2:
9c541725 18591 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18592 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18593 info_ptr += 2;
18594 break;
18595 case DW_FORM_ref4:
9c541725 18596 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18597 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18598 info_ptr += 4;
18599 break;
613e1657 18600 case DW_FORM_ref8:
9c541725 18601 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18602 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18603 info_ptr += 8;
18604 break;
55f1336d 18605 case DW_FORM_ref_sig8:
ac9ec31b 18606 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18607 info_ptr += 8;
18608 break;
c906108c 18609 case DW_FORM_ref_udata:
9c541725 18610 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18611 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18612 info_ptr += bytes_read;
18613 break;
c906108c 18614 case DW_FORM_indirect:
a8329558
KW
18615 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18616 info_ptr += bytes_read;
43988095
JK
18617 if (form == DW_FORM_implicit_const)
18618 {
18619 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18620 info_ptr += bytes_read;
18621 }
18622 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18623 info_ptr, need_reprocess);
43988095
JK
18624 break;
18625 case DW_FORM_implicit_const:
18626 DW_SND (attr) = implicit_const;
a8329558 18627 break;
336d760d 18628 case DW_FORM_addrx:
3019eac3 18629 case DW_FORM_GNU_addr_index:
18a8505e
AT
18630 *need_reprocess = true;
18631 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18632 info_ptr += bytes_read;
18633 break;
cf532bd1 18634 case DW_FORM_strx:
15f18d14
AT
18635 case DW_FORM_strx1:
18636 case DW_FORM_strx2:
18637 case DW_FORM_strx3:
18638 case DW_FORM_strx4:
3019eac3 18639 case DW_FORM_GNU_str_index:
3019eac3 18640 {
15f18d14
AT
18641 ULONGEST str_index;
18642 if (form == DW_FORM_strx1)
18643 {
18644 str_index = read_1_byte (abfd, info_ptr);
18645 info_ptr += 1;
18646 }
18647 else if (form == DW_FORM_strx2)
18648 {
18649 str_index = read_2_bytes (abfd, info_ptr);
18650 info_ptr += 2;
18651 }
18652 else if (form == DW_FORM_strx3)
18653 {
18654 str_index = read_3_bytes (abfd, info_ptr);
18655 info_ptr += 3;
18656 }
18657 else if (form == DW_FORM_strx4)
18658 {
18659 str_index = read_4_bytes (abfd, info_ptr);
18660 info_ptr += 4;
18661 }
18662 else
18663 {
18664 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18665 info_ptr += bytes_read;
18666 }
18a8505e
AT
18667 *need_reprocess = true;
18668 DW_UNSND (attr) = str_index;
18669 }
3019eac3 18670 break;
c906108c 18671 default:
8a3fe4f8 18672 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18673 dwarf_form_name (form),
18674 bfd_get_filename (abfd));
c906108c 18675 }
28e94949 18676
36586728 18677 /* Super hack. */
cd6c91b4 18678 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18679 attr->form = DW_FORM_GNU_ref_alt;
18680
28e94949
JB
18681 /* We have seen instances where the compiler tried to emit a byte
18682 size attribute of -1 which ended up being encoded as an unsigned
18683 0xffffffff. Although 0xffffffff is technically a valid size value,
18684 an object of this size seems pretty unlikely so we can relatively
18685 safely treat these cases as if the size attribute was invalid and
18686 treat them as zero by default. */
18687 if (attr->name == DW_AT_byte_size
18688 && form == DW_FORM_data4
18689 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18690 {
18691 complaint
b98664d3 18692 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18693 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18694 DW_UNSND (attr) = 0;
18695 }
28e94949 18696
c906108c
SS
18697 return info_ptr;
18698}
18699
a8329558
KW
18700/* Read an attribute described by an abbreviated attribute. */
18701
d521ce57 18702static const gdb_byte *
dee91e82
DE
18703read_attribute (const struct die_reader_specs *reader,
18704 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18705 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18706{
18707 attr->name = abbrev->name;
43988095 18708 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18709 abbrev->implicit_const, info_ptr,
18710 need_reprocess);
a8329558
KW
18711}
18712
c764a876
DE
18713/* Cover function for read_initial_length.
18714 Returns the length of the object at BUF, and stores the size of the
18715 initial length in *BYTES_READ and stores the size that offsets will be in
18716 *OFFSET_SIZE.
18717 If the initial length size is not equivalent to that specified in
18718 CU_HEADER then issue a complaint.
18719 This is useful when reading non-comp-unit headers. */
dd373385 18720
c764a876 18721static LONGEST
d521ce57 18722read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18723 const struct comp_unit_head *cu_header,
18724 unsigned int *bytes_read,
18725 unsigned int *offset_size)
18726{
18727 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18728
18729 gdb_assert (cu_header->initial_length_size == 4
18730 || cu_header->initial_length_size == 8
18731 || cu_header->initial_length_size == 12);
18732
18733 if (cu_header->initial_length_size != *bytes_read)
b98664d3 18734 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18735
c764a876 18736 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18737 return length;
613e1657
KB
18738}
18739
43988095
JK
18740/* Return pointer to string at section SECT offset STR_OFFSET with error
18741 reporting strings FORM_NAME and SECT_NAME. */
18742
d521ce57 18743static const char *
ed2dc618
SM
18744read_indirect_string_at_offset_from (struct objfile *objfile,
18745 bfd *abfd, LONGEST str_offset,
43988095
JK
18746 struct dwarf2_section_info *sect,
18747 const char *form_name,
18748 const char *sect_name)
18749{
96b79293 18750 sect->read (objfile);
43988095
JK
18751 if (sect->buffer == NULL)
18752 error (_("%s used without %s section [in module %s]"),
18753 form_name, sect_name, bfd_get_filename (abfd));
18754 if (str_offset >= sect->size)
18755 error (_("%s pointing outside of %s section [in module %s]"),
18756 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18757 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18758 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18759 return NULL;
43988095
JK
18760 return (const char *) (sect->buffer + str_offset);
18761}
18762
18763/* Return pointer to string at .debug_str offset STR_OFFSET. */
18764
18765static const char *
ed2dc618
SM
18766read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18767 bfd *abfd, LONGEST str_offset)
43988095 18768{
ed2dc618
SM
18769 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18770 abfd, str_offset,
43988095
JK
18771 &dwarf2_per_objfile->str,
18772 "DW_FORM_strp", ".debug_str");
18773}
18774
18775/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18776
18777static const char *
ed2dc618
SM
18778read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18779 bfd *abfd, LONGEST str_offset)
43988095 18780{
ed2dc618
SM
18781 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18782 abfd, str_offset,
43988095
JK
18783 &dwarf2_per_objfile->line_str,
18784 "DW_FORM_line_strp",
18785 ".debug_line_str");
c906108c
SS
18786}
18787
36586728
TT
18788/* Read a string at offset STR_OFFSET in the .debug_str section from
18789 the .dwz file DWZ. Throw an error if the offset is too large. If
18790 the string consists of a single NUL byte, return NULL; otherwise
18791 return a pointer to the string. */
18792
d521ce57 18793static const char *
ed2dc618
SM
18794read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18795 LONGEST str_offset)
36586728 18796{
96b79293 18797 dwz->str.read (objfile);
36586728
TT
18798
18799 if (dwz->str.buffer == NULL)
18800 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18801 "section [in module %s]"),
00f93c44 18802 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18803 if (str_offset >= dwz->str.size)
18804 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18805 ".debug_str section [in module %s]"),
00f93c44 18806 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18807 gdb_assert (HOST_CHAR_BIT == 8);
18808 if (dwz->str.buffer[str_offset] == '\0')
18809 return NULL;
d521ce57 18810 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
18811}
18812
43988095
JK
18813/* Return pointer to string at .debug_str offset as read from BUF.
18814 BUF is assumed to be in a compilation unit described by CU_HEADER.
18815 Return *BYTES_READ_PTR count of bytes read from BUF. */
18816
d521ce57 18817static const char *
ed2dc618
SM
18818read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18819 const gdb_byte *buf,
cf2c3c16
TT
18820 const struct comp_unit_head *cu_header,
18821 unsigned int *bytes_read_ptr)
18822{
8266302d 18823 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18824
ed2dc618 18825 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
18826}
18827
43988095
JK
18828/* Return pointer to string at .debug_line_str offset as read from BUF.
18829 BUF is assumed to be in a compilation unit described by CU_HEADER.
18830 Return *BYTES_READ_PTR count of bytes read from BUF. */
18831
18832static const char *
ed2dc618
SM
18833read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18834 bfd *abfd, const gdb_byte *buf,
43988095
JK
18835 const struct comp_unit_head *cu_header,
18836 unsigned int *bytes_read_ptr)
18837{
8266302d 18838 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18839
ed2dc618
SM
18840 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18841 str_offset);
43988095
JK
18842}
18843
3019eac3 18844/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18845 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18846 ADDR_SIZE is the size of addresses from the CU header. */
18847
18848static CORE_ADDR
ed2dc618 18849read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18850 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18851 int addr_size)
3019eac3
DE
18852{
18853 struct objfile *objfile = dwarf2_per_objfile->objfile;
18854 bfd *abfd = objfile->obfd;
18855 const gdb_byte *info_ptr;
18a8505e 18856 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18857
96b79293 18858 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18859 if (dwarf2_per_objfile->addr.buffer == NULL)
18860 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18861 objfile_name (objfile));
18a8505e
AT
18862 if (addr_base_or_zero + addr_index * addr_size
18863 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18864 error (_("DW_FORM_addr_index pointing outside of "
18865 ".debug_addr section [in module %s]"),
4262abfb 18866 objfile_name (objfile));
3019eac3 18867 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18868 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18869 if (addr_size == 4)
18870 return bfd_get_32 (abfd, info_ptr);
18871 else
18872 return bfd_get_64 (abfd, info_ptr);
18873}
18874
18875/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18876
18877static CORE_ADDR
18878read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18879{
518817b3
SM
18880 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18881 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18882}
18883
18884/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18885
18886static CORE_ADDR
d521ce57 18887read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18888 unsigned int *bytes_read)
18889{
518817b3 18890 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18891 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18892
18893 return read_addr_index (cu, addr_index);
18894}
18895
3019eac3
DE
18896/* Given an index in .debug_addr, fetch the value.
18897 NOTE: This can be called during dwarf expression evaluation,
18898 long after the debug information has been read, and thus per_cu->cu
18899 may no longer exist. */
18900
18901CORE_ADDR
18902dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18903 unsigned int addr_index)
18904{
ed2dc618 18905 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18906 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18907 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18908 int addr_size;
18909
3019eac3
DE
18910 /* We need addr_base and addr_size.
18911 If we don't have PER_CU->cu, we have to get it.
18912 Nasty, but the alternative is storing the needed info in PER_CU,
18913 which at this point doesn't seem justified: it's not clear how frequently
18914 it would get used and it would increase the size of every PER_CU.
18915 Entry points like dwarf2_per_cu_addr_size do a similar thing
18916 so we're not in uncharted territory here.
18917 Alas we need to be a bit more complicated as addr_base is contained
18918 in the DIE.
18919
18920 We don't need to read the entire CU(/TU).
18921 We just need the header and top level die.
a1b64ce1 18922
3019eac3 18923 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18924 For now we skip this optimization. */
3019eac3
DE
18925
18926 if (cu != NULL)
18927 {
18928 addr_base = cu->addr_base;
18929 addr_size = cu->header.addr_size;
18930 }
18931 else
18932 {
6751ebae 18933 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18934 addr_base = reader.cu->addr_base;
18935 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18936 }
18937
ed2dc618
SM
18938 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18939 addr_size);
3019eac3
DE
18940}
18941
18a8505e
AT
18942/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18943 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18944 DWO file. */
3019eac3 18945
d521ce57 18946static const char *
18a8505e
AT
18947read_str_index (struct dwarf2_cu *cu,
18948 struct dwarf2_section_info *str_section,
18949 struct dwarf2_section_info *str_offsets_section,
18950 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18951{
518817b3
SM
18952 struct dwarf2_per_objfile *dwarf2_per_objfile
18953 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18954 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18955 const char *objf_name = objfile_name (objfile);
3019eac3 18956 bfd *abfd = objfile->obfd;
d521ce57 18957 const gdb_byte *info_ptr;
3019eac3 18958 ULONGEST str_offset;
cf532bd1 18959 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18960
96b79293
TT
18961 str_section->read (objfile);
18962 str_offsets_section->read (objfile);
73869dc2 18963 if (str_section->buffer == NULL)
18a8505e 18964 error (_("%s used without %s section"
9d8780f0 18965 " in CU at offset %s [in module %s]"),
96b79293 18966 form_name, str_section->get_name (),
18a8505e 18967 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18968 if (str_offsets_section->buffer == NULL)
18a8505e 18969 error (_("%s used without %s section"
9d8780f0 18970 " in CU at offset %s [in module %s]"),
96b79293 18971 form_name, str_section->get_name (),
18a8505e 18972 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18973 info_ptr = (str_offsets_section->buffer
18a8505e 18974 + str_offsets_base
3019eac3
DE
18975 + str_index * cu->header.offset_size);
18976 if (cu->header.offset_size == 4)
18977 str_offset = bfd_get_32 (abfd, info_ptr);
18978 else
18979 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18980 if (str_offset >= str_section->size)
57d63ce2 18981 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18982 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18983 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18984 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18985}
18986
18a8505e
AT
18987/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18988
18989static const char *
18990read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18991{
18992 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18993 ? reader->cu->header.addr_size : 0;
18994 return read_str_index (reader->cu,
18995 &reader->dwo_file->sections.str,
18996 &reader->dwo_file->sections.str_offsets,
18997 str_offsets_base, str_index);
18998}
18999
19000/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19001
19002static const char *
19003read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19004{
19005 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19006 const char *objf_name = objfile_name (objfile);
19007 static const char form_name[] = "DW_FORM_GNU_str_index";
19008 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19009
19010 if (!cu->str_offsets_base.has_value ())
19011 error (_("%s used in Fission stub without %s"
19012 " in CU at offset 0x%lx [in module %s]"),
19013 form_name, str_offsets_attr_name,
19014 (long) cu->header.offset_size, objf_name);
19015
19016 return read_str_index (cu,
19017 &cu->per_cu->dwarf2_per_objfile->str,
19018 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19019 *cu->str_offsets_base, str_index);
19020}
19021
3019eac3
DE
19022/* Return the length of an LEB128 number in BUF. */
19023
19024static int
19025leb128_size (const gdb_byte *buf)
19026{
19027 const gdb_byte *begin = buf;
19028 gdb_byte byte;
19029
19030 while (1)
19031 {
19032 byte = *buf++;
19033 if ((byte & 128) == 0)
19034 return buf - begin;
19035 }
19036}
19037
c906108c 19038static void
e142c38c 19039set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19040{
19041 switch (lang)
19042 {
19043 case DW_LANG_C89:
76bee0cc 19044 case DW_LANG_C99:
0cfd832f 19045 case DW_LANG_C11:
c906108c 19046 case DW_LANG_C:
d1be3247 19047 case DW_LANG_UPC:
e142c38c 19048 cu->language = language_c;
c906108c 19049 break;
9c37b5ae 19050 case DW_LANG_Java:
c906108c 19051 case DW_LANG_C_plus_plus:
0cfd832f
MW
19052 case DW_LANG_C_plus_plus_11:
19053 case DW_LANG_C_plus_plus_14:
e142c38c 19054 cu->language = language_cplus;
c906108c 19055 break;
6aecb9c2
JB
19056 case DW_LANG_D:
19057 cu->language = language_d;
19058 break;
c906108c
SS
19059 case DW_LANG_Fortran77:
19060 case DW_LANG_Fortran90:
b21b22e0 19061 case DW_LANG_Fortran95:
f7de9aab
MW
19062 case DW_LANG_Fortran03:
19063 case DW_LANG_Fortran08:
e142c38c 19064 cu->language = language_fortran;
c906108c 19065 break;
a766d390
DE
19066 case DW_LANG_Go:
19067 cu->language = language_go;
19068 break;
c906108c 19069 case DW_LANG_Mips_Assembler:
e142c38c 19070 cu->language = language_asm;
c906108c
SS
19071 break;
19072 case DW_LANG_Ada83:
8aaf0b47 19073 case DW_LANG_Ada95:
bc5f45f8
JB
19074 cu->language = language_ada;
19075 break;
72019c9c
GM
19076 case DW_LANG_Modula2:
19077 cu->language = language_m2;
19078 break;
fe8e67fd
PM
19079 case DW_LANG_Pascal83:
19080 cu->language = language_pascal;
19081 break;
22566fbd
DJ
19082 case DW_LANG_ObjC:
19083 cu->language = language_objc;
19084 break;
c44af4eb
TT
19085 case DW_LANG_Rust:
19086 case DW_LANG_Rust_old:
19087 cu->language = language_rust;
19088 break;
c906108c
SS
19089 case DW_LANG_Cobol74:
19090 case DW_LANG_Cobol85:
c906108c 19091 default:
e142c38c 19092 cu->language = language_minimal;
c906108c
SS
19093 break;
19094 }
e142c38c 19095 cu->language_defn = language_def (cu->language);
c906108c
SS
19096}
19097
19098/* Return the named attribute or NULL if not there. */
19099
19100static struct attribute *
e142c38c 19101dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19102{
a48e046c 19103 for (;;)
c906108c 19104 {
a48e046c
TT
19105 unsigned int i;
19106 struct attribute *spec = NULL;
19107
19108 for (i = 0; i < die->num_attrs; ++i)
19109 {
19110 if (die->attrs[i].name == name)
19111 return &die->attrs[i];
19112 if (die->attrs[i].name == DW_AT_specification
19113 || die->attrs[i].name == DW_AT_abstract_origin)
19114 spec = &die->attrs[i];
19115 }
19116
19117 if (!spec)
19118 break;
c906108c 19119
f2f0e013 19120 die = follow_die_ref (die, spec, &cu);
f2f0e013 19121 }
c5aa993b 19122
c906108c
SS
19123 return NULL;
19124}
19125
348e048f
DE
19126/* Return the named attribute or NULL if not there,
19127 but do not follow DW_AT_specification, etc.
19128 This is for use in contexts where we're reading .debug_types dies.
19129 Following DW_AT_specification, DW_AT_abstract_origin will take us
19130 back up the chain, and we want to go down. */
19131
19132static struct attribute *
45e58e77 19133dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19134{
19135 unsigned int i;
19136
19137 for (i = 0; i < die->num_attrs; ++i)
19138 if (die->attrs[i].name == name)
19139 return &die->attrs[i];
19140
19141 return NULL;
19142}
19143
7d45c7c3
KB
19144/* Return the string associated with a string-typed attribute, or NULL if it
19145 is either not found or is of an incorrect type. */
19146
19147static const char *
19148dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19149{
19150 struct attribute *attr;
19151 const char *str = NULL;
19152
19153 attr = dwarf2_attr (die, name, cu);
19154
19155 if (attr != NULL)
19156 {
43988095 19157 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19158 || attr->form == DW_FORM_string
cf532bd1 19159 || attr->form == DW_FORM_strx
8fe0f950
AT
19160 || attr->form == DW_FORM_strx1
19161 || attr->form == DW_FORM_strx2
19162 || attr->form == DW_FORM_strx3
19163 || attr->form == DW_FORM_strx4
b3340438 19164 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19165 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19166 str = DW_STRING (attr);
19167 else
b98664d3 19168 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19169 "DIE at %s in module %s"),
19170 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19171 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19172 }
19173
19174 return str;
19175}
19176
a084a2a6 19177/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19178 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19179static const char *
19180dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19181{
19182 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19183 if (dwo_name == nullptr)
19184 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19185 return dwo_name;
19186}
19187
05cf31d1
JB
19188/* Return non-zero iff the attribute NAME is defined for the given DIE,
19189 and holds a non-zero value. This function should only be used for
2dc7f7b3 19190 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19191
19192static int
19193dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19194{
19195 struct attribute *attr = dwarf2_attr (die, name, cu);
19196
19197 return (attr && DW_UNSND (attr));
19198}
19199
3ca72b44 19200static int
e142c38c 19201die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19202{
05cf31d1
JB
19203 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19204 which value is non-zero. However, we have to be careful with
19205 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19206 (via dwarf2_flag_true_p) follows this attribute. So we may
19207 end up accidently finding a declaration attribute that belongs
19208 to a different DIE referenced by the specification attribute,
19209 even though the given DIE does not have a declaration attribute. */
19210 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19211 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19212}
19213
63d06c5c 19214/* Return the die giving the specification for DIE, if there is
f2f0e013 19215 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19216 containing the return value on output. If there is no
19217 specification, but there is an abstract origin, that is
19218 returned. */
63d06c5c
DC
19219
19220static struct die_info *
f2f0e013 19221die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19222{
f2f0e013
DJ
19223 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19224 *spec_cu);
63d06c5c 19225
edb3359d
DJ
19226 if (spec_attr == NULL)
19227 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19228
63d06c5c
DC
19229 if (spec_attr == NULL)
19230 return NULL;
19231 else
f2f0e013 19232 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19233}
c906108c 19234
527f3840
JK
19235/* Stub for free_line_header to match void * callback types. */
19236
19237static void
19238free_line_header_voidp (void *arg)
19239{
9a3c8263 19240 struct line_header *lh = (struct line_header *) arg;
527f3840 19241
fff8551c 19242 delete lh;
527f3840
JK
19243}
19244
83769d0b 19245/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19246
19247static struct dwarf2_section_info *
19248get_debug_line_section (struct dwarf2_cu *cu)
19249{
19250 struct dwarf2_section_info *section;
518817b3
SM
19251 struct dwarf2_per_objfile *dwarf2_per_objfile
19252 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19253
19254 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19255 DWO file. */
19256 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19257 section = &cu->dwo_unit->dwo_file->sections.line;
19258 else if (cu->per_cu->is_dwz)
19259 {
ed2dc618 19260 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19261
19262 section = &dwz->line;
19263 }
19264 else
19265 section = &dwarf2_per_objfile->line;
19266
19267 return section;
19268}
19269
43988095
JK
19270/* Read directory or file name entry format, starting with byte of
19271 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19272 entries count and the entries themselves in the described entry
19273 format. */
19274
19275static void
ed2dc618
SM
19276read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19277 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19278 struct line_header *lh,
19279 const struct comp_unit_head *cu_header,
19280 void (*callback) (struct line_header *lh,
19281 const char *name,
ecfb656c 19282 dir_index d_index,
43988095
JK
19283 unsigned int mod_time,
19284 unsigned int length))
19285{
19286 gdb_byte format_count, formati;
19287 ULONGEST data_count, datai;
19288 const gdb_byte *buf = *bufp;
19289 const gdb_byte *format_header_data;
43988095
JK
19290 unsigned int bytes_read;
19291
19292 format_count = read_1_byte (abfd, buf);
19293 buf += 1;
19294 format_header_data = buf;
19295 for (formati = 0; formati < format_count; formati++)
19296 {
19297 read_unsigned_leb128 (abfd, buf, &bytes_read);
19298 buf += bytes_read;
19299 read_unsigned_leb128 (abfd, buf, &bytes_read);
19300 buf += bytes_read;
19301 }
19302
19303 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19304 buf += bytes_read;
19305 for (datai = 0; datai < data_count; datai++)
19306 {
19307 const gdb_byte *format = format_header_data;
19308 struct file_entry fe;
19309
43988095
JK
19310 for (formati = 0; formati < format_count; formati++)
19311 {
ecfb656c 19312 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19313 format += bytes_read;
43988095 19314
ecfb656c 19315 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19316 format += bytes_read;
ecfb656c
PA
19317
19318 gdb::optional<const char *> string;
19319 gdb::optional<unsigned int> uint;
19320
43988095
JK
19321 switch (form)
19322 {
19323 case DW_FORM_string:
ecfb656c 19324 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19325 buf += bytes_read;
19326 break;
19327
19328 case DW_FORM_line_strp:
ed2dc618
SM
19329 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19330 abfd, buf,
ecfb656c
PA
19331 cu_header,
19332 &bytes_read));
43988095
JK
19333 buf += bytes_read;
19334 break;
19335
19336 case DW_FORM_data1:
ecfb656c 19337 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19338 buf += 1;
19339 break;
19340
19341 case DW_FORM_data2:
ecfb656c 19342 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19343 buf += 2;
19344 break;
19345
19346 case DW_FORM_data4:
ecfb656c 19347 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19348 buf += 4;
19349 break;
19350
19351 case DW_FORM_data8:
ecfb656c 19352 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19353 buf += 8;
19354 break;
19355
7ba99d21
AT
19356 case DW_FORM_data16:
19357 /* This is used for MD5, but file_entry does not record MD5s. */
19358 buf += 16;
19359 break;
19360
43988095 19361 case DW_FORM_udata:
ecfb656c 19362 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19363 buf += bytes_read;
19364 break;
19365
19366 case DW_FORM_block:
19367 /* It is valid only for DW_LNCT_timestamp which is ignored by
19368 current GDB. */
19369 break;
19370 }
ecfb656c
PA
19371
19372 switch (content_type)
19373 {
19374 case DW_LNCT_path:
19375 if (string.has_value ())
19376 fe.name = *string;
19377 break;
19378 case DW_LNCT_directory_index:
19379 if (uint.has_value ())
19380 fe.d_index = (dir_index) *uint;
19381 break;
19382 case DW_LNCT_timestamp:
19383 if (uint.has_value ())
19384 fe.mod_time = *uint;
19385 break;
19386 case DW_LNCT_size:
19387 if (uint.has_value ())
19388 fe.length = *uint;
19389 break;
19390 case DW_LNCT_MD5:
19391 break;
19392 default:
b98664d3 19393 complaint (_("Unknown format content type %s"),
ecfb656c
PA
19394 pulongest (content_type));
19395 }
43988095
JK
19396 }
19397
ecfb656c 19398 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
19399 }
19400
19401 *bufp = buf;
19402}
19403
debd256d 19404/* Read the statement program header starting at OFFSET in
3019eac3 19405 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19406 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19407 Returns NULL if there is a problem reading the header, e.g., if it
19408 has a version we don't understand.
debd256d
JB
19409
19410 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19411 the returned object point into the dwarf line section buffer,
19412 and must not be freed. */
ae2de4f8 19413
fff8551c 19414static line_header_up
9c541725 19415dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19416{
d521ce57 19417 const gdb_byte *line_ptr;
c764a876 19418 unsigned int bytes_read, offset_size;
debd256d 19419 int i;
d521ce57 19420 const char *cur_dir, *cur_file;
3019eac3
DE
19421 struct dwarf2_section_info *section;
19422 bfd *abfd;
518817b3
SM
19423 struct dwarf2_per_objfile *dwarf2_per_objfile
19424 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19425
36586728 19426 section = get_debug_line_section (cu);
96b79293 19427 section->read (dwarf2_per_objfile->objfile);
3019eac3 19428 if (section->buffer == NULL)
debd256d 19429 {
3019eac3 19430 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19431 complaint (_("missing .debug_line.dwo section"));
3019eac3 19432 else
b98664d3 19433 complaint (_("missing .debug_line section"));
debd256d
JB
19434 return 0;
19435 }
19436
fceca515
DE
19437 /* We can't do this until we know the section is non-empty.
19438 Only then do we know we have such a section. */
96b79293 19439 abfd = section->get_bfd_owner ();
fceca515 19440
a738430d
MK
19441 /* Make sure that at least there's room for the total_length field.
19442 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 19443 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 19444 {
4d3c2250 19445 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19446 return 0;
19447 }
19448
fff8551c 19449 line_header_up lh (new line_header ());
debd256d 19450
9c541725 19451 lh->sect_off = sect_off;
527f3840
JK
19452 lh->offset_in_dwz = cu->per_cu->is_dwz;
19453
9c541725 19454 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 19455
a738430d 19456 /* Read in the header. */
6e70227d 19457 lh->total_length =
c764a876
DE
19458 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19459 &bytes_read, &offset_size);
debd256d 19460 line_ptr += bytes_read;
7ba99d21
AT
19461
19462 const gdb_byte *start_here = line_ptr;
19463
3019eac3 19464 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 19465 {
4d3c2250 19466 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19467 return 0;
19468 }
7ba99d21 19469 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
19470 lh->version = read_2_bytes (abfd, line_ptr);
19471 line_ptr += 2;
43988095 19472 if (lh->version > 5)
cd366ee8
DE
19473 {
19474 /* This is a version we don't understand. The format could have
19475 changed in ways we don't handle properly so just punt. */
b98664d3 19476 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
19477 return NULL;
19478 }
43988095
JK
19479 if (lh->version >= 5)
19480 {
19481 gdb_byte segment_selector_size;
19482
19483 /* Skip address size. */
19484 read_1_byte (abfd, line_ptr);
19485 line_ptr += 1;
19486
19487 segment_selector_size = read_1_byte (abfd, line_ptr);
19488 line_ptr += 1;
19489 if (segment_selector_size != 0)
19490 {
b98664d3 19491 complaint (_("unsupported segment selector size %u "
43988095
JK
19492 "in .debug_line section"),
19493 segment_selector_size);
19494 return NULL;
19495 }
19496 }
24aa364d 19497 lh->header_length = read_offset (abfd, line_ptr, offset_size);
c764a876 19498 line_ptr += offset_size;
7ba99d21 19499 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
19500 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19501 line_ptr += 1;
2dc7f7b3
TT
19502 if (lh->version >= 4)
19503 {
19504 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19505 line_ptr += 1;
19506 }
19507 else
19508 lh->maximum_ops_per_instruction = 1;
19509
19510 if (lh->maximum_ops_per_instruction == 0)
19511 {
19512 lh->maximum_ops_per_instruction = 1;
b98664d3 19513 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 19514 "in `.debug_line' section"));
2dc7f7b3
TT
19515 }
19516
debd256d
JB
19517 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19518 line_ptr += 1;
19519 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19520 line_ptr += 1;
19521 lh->line_range = read_1_byte (abfd, line_ptr);
19522 line_ptr += 1;
19523 lh->opcode_base = read_1_byte (abfd, line_ptr);
19524 line_ptr += 1;
fff8551c 19525 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
19526
19527 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19528 for (i = 1; i < lh->opcode_base; ++i)
19529 {
19530 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19531 line_ptr += 1;
19532 }
19533
43988095 19534 if (lh->version >= 5)
debd256d 19535 {
43988095 19536 /* Read directory table. */
ed2dc618
SM
19537 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19538 &cu->header,
b926417a 19539 [] (struct line_header *header, const char *name,
ecfb656c 19540 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19541 unsigned int length)
19542 {
b926417a 19543 header->add_include_dir (name);
fff8551c 19544 });
debd256d 19545
43988095 19546 /* Read file name table. */
ed2dc618
SM
19547 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19548 &cu->header,
b926417a 19549 [] (struct line_header *header, const char *name,
ecfb656c 19550 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19551 unsigned int length)
19552 {
b926417a 19553 header->add_file_name (name, d_index, mod_time, length);
fff8551c 19554 });
43988095
JK
19555 }
19556 else
debd256d 19557 {
43988095
JK
19558 /* Read directory table. */
19559 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19560 {
19561 line_ptr += bytes_read;
fff8551c 19562 lh->add_include_dir (cur_dir);
43988095 19563 }
debd256d
JB
19564 line_ptr += bytes_read;
19565
43988095
JK
19566 /* Read file name table. */
19567 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19568 {
ecfb656c
PA
19569 unsigned int mod_time, length;
19570 dir_index d_index;
43988095
JK
19571
19572 line_ptr += bytes_read;
ecfb656c 19573 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
19574 line_ptr += bytes_read;
19575 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19576 line_ptr += bytes_read;
19577 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19578 line_ptr += bytes_read;
19579
ecfb656c 19580 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
19581 }
19582 line_ptr += bytes_read;
debd256d 19583 }
debd256d 19584
3019eac3 19585 if (line_ptr > (section->buffer + section->size))
b98664d3 19586 complaint (_("line number info header doesn't "
3e43a32a 19587 "fit in `.debug_line' section"));
debd256d 19588
debd256d
JB
19589 return lh;
19590}
c906108c 19591
c6da4cef 19592/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19593 Return the file name of the psymtab for the given file_entry.
c6da4cef 19594 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19595 If space for the result is malloc'd, *NAME_HOLDER will be set.
19596 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19597
d521ce57 19598static const char *
7ba99d21 19599psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19600 const dwarf2_psymtab *pst,
c89b44cd
TT
19601 const char *comp_dir,
19602 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19603{
d521ce57
TT
19604 const char *include_name = fe.name;
19605 const char *include_name_to_compare = include_name;
72b9f47f 19606 const char *pst_filename;
c6da4cef
DE
19607 int file_is_pst;
19608
8c43009f 19609 const char *dir_name = fe.include_dir (lh);
c6da4cef 19610
c89b44cd 19611 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19612 if (!IS_ABSOLUTE_PATH (include_name)
19613 && (dir_name != NULL || comp_dir != NULL))
19614 {
19615 /* Avoid creating a duplicate psymtab for PST.
19616 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19617 Before we do the comparison, however, we need to account
19618 for DIR_NAME and COMP_DIR.
19619 First prepend dir_name (if non-NULL). If we still don't
19620 have an absolute path prepend comp_dir (if non-NULL).
19621 However, the directory we record in the include-file's
19622 psymtab does not contain COMP_DIR (to match the
19623 corresponding symtab(s)).
19624
19625 Example:
19626
19627 bash$ cd /tmp
19628 bash$ gcc -g ./hello.c
19629 include_name = "hello.c"
19630 dir_name = "."
19631 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19632 DW_AT_name = "./hello.c"
19633
19634 */
c6da4cef
DE
19635
19636 if (dir_name != NULL)
19637 {
c89b44cd
TT
19638 name_holder->reset (concat (dir_name, SLASH_STRING,
19639 include_name, (char *) NULL));
19640 include_name = name_holder->get ();
c6da4cef 19641 include_name_to_compare = include_name;
c6da4cef
DE
19642 }
19643 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19644 {
c89b44cd
TT
19645 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19646 include_name, (char *) NULL));
19647 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19648 }
19649 }
19650
19651 pst_filename = pst->filename;
c89b44cd 19652 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19653 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19654 {
c89b44cd
TT
19655 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19656 pst_filename, (char *) NULL));
19657 pst_filename = copied_name.get ();
c6da4cef
DE
19658 }
19659
1e3fad37 19660 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19661
c6da4cef
DE
19662 if (file_is_pst)
19663 return NULL;
19664 return include_name;
19665}
19666
d9b3de22
DE
19667/* State machine to track the state of the line number program. */
19668
6f77053d 19669class lnp_state_machine
d9b3de22 19670{
6f77053d
PA
19671public:
19672 /* Initialize a machine state for the start of a line number
19673 program. */
804d2729
TT
19674 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19675 bool record_lines_p);
6f77053d 19676
8c43009f
PA
19677 file_entry *current_file ()
19678 {
19679 /* lh->file_names is 0-based, but the file name numbers in the
19680 statement program are 1-based. */
6f77053d
PA
19681 return m_line_header->file_name_at (m_file);
19682 }
19683
19684 /* Record the line in the state machine. END_SEQUENCE is true if
19685 we're processing the end of a sequence. */
19686 void record_line (bool end_sequence);
19687
7ab6656f
OJ
19688 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19689 nop-out rest of the lines in this sequence. */
6f77053d
PA
19690 void check_line_address (struct dwarf2_cu *cu,
19691 const gdb_byte *line_ptr,
7ab6656f 19692 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19693
19694 void handle_set_discriminator (unsigned int discriminator)
19695 {
19696 m_discriminator = discriminator;
19697 m_line_has_non_zero_discriminator |= discriminator != 0;
19698 }
19699
19700 /* Handle DW_LNE_set_address. */
19701 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19702 {
19703 m_op_index = 0;
19704 address += baseaddr;
19705 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19706 }
19707
19708 /* Handle DW_LNS_advance_pc. */
19709 void handle_advance_pc (CORE_ADDR adjust);
19710
19711 /* Handle a special opcode. */
19712 void handle_special_opcode (unsigned char op_code);
19713
19714 /* Handle DW_LNS_advance_line. */
19715 void handle_advance_line (int line_delta)
19716 {
19717 advance_line (line_delta);
19718 }
19719
19720 /* Handle DW_LNS_set_file. */
19721 void handle_set_file (file_name_index file);
19722
19723 /* Handle DW_LNS_negate_stmt. */
19724 void handle_negate_stmt ()
19725 {
19726 m_is_stmt = !m_is_stmt;
19727 }
19728
19729 /* Handle DW_LNS_const_add_pc. */
19730 void handle_const_add_pc ();
19731
19732 /* Handle DW_LNS_fixed_advance_pc. */
19733 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19734 {
19735 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19736 m_op_index = 0;
19737 }
19738
19739 /* Handle DW_LNS_copy. */
19740 void handle_copy ()
19741 {
19742 record_line (false);
19743 m_discriminator = 0;
19744 }
19745
19746 /* Handle DW_LNE_end_sequence. */
19747 void handle_end_sequence ()
19748 {
804d2729 19749 m_currently_recording_lines = true;
6f77053d
PA
19750 }
19751
19752private:
19753 /* Advance the line by LINE_DELTA. */
19754 void advance_line (int line_delta)
19755 {
19756 m_line += line_delta;
19757
19758 if (line_delta != 0)
19759 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19760 }
19761
804d2729
TT
19762 struct dwarf2_cu *m_cu;
19763
6f77053d
PA
19764 gdbarch *m_gdbarch;
19765
19766 /* True if we're recording lines.
19767 Otherwise we're building partial symtabs and are just interested in
19768 finding include files mentioned by the line number program. */
19769 bool m_record_lines_p;
19770
8c43009f 19771 /* The line number header. */
6f77053d 19772 line_header *m_line_header;
8c43009f 19773
6f77053d
PA
19774 /* These are part of the standard DWARF line number state machine,
19775 and initialized according to the DWARF spec. */
d9b3de22 19776
6f77053d 19777 unsigned char m_op_index = 0;
7ba99d21
AT
19778 /* The line table index of the current file. */
19779 file_name_index m_file = 1;
6f77053d
PA
19780 unsigned int m_line = 1;
19781
19782 /* These are initialized in the constructor. */
19783
19784 CORE_ADDR m_address;
19785 bool m_is_stmt;
19786 unsigned int m_discriminator;
d9b3de22
DE
19787
19788 /* Additional bits of state we need to track. */
19789
19790 /* The last file that we called dwarf2_start_subfile for.
19791 This is only used for TLLs. */
6f77053d 19792 unsigned int m_last_file = 0;
d9b3de22 19793 /* The last file a line number was recorded for. */
6f77053d 19794 struct subfile *m_last_subfile = NULL;
d9b3de22 19795
804d2729
TT
19796 /* When true, record the lines we decode. */
19797 bool m_currently_recording_lines = false;
d9b3de22
DE
19798
19799 /* The last line number that was recorded, used to coalesce
19800 consecutive entries for the same line. This can happen, for
19801 example, when discriminators are present. PR 17276. */
6f77053d
PA
19802 unsigned int m_last_line = 0;
19803 bool m_line_has_non_zero_discriminator = false;
8c43009f 19804};
d9b3de22 19805
6f77053d
PA
19806void
19807lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19808{
19809 CORE_ADDR addr_adj = (((m_op_index + adjust)
19810 / m_line_header->maximum_ops_per_instruction)
19811 * m_line_header->minimum_instruction_length);
19812 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19813 m_op_index = ((m_op_index + adjust)
19814 % m_line_header->maximum_ops_per_instruction);
19815}
d9b3de22 19816
6f77053d
PA
19817void
19818lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19819{
6f77053d 19820 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19821 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19822 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19823 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19824 / m_line_header->maximum_ops_per_instruction)
19825 * m_line_header->minimum_instruction_length);
19826 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19827 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19828 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19829
258bf0ee 19830 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19831 advance_line (line_delta);
19832 record_line (false);
19833 m_discriminator = 0;
19834}
d9b3de22 19835
6f77053d
PA
19836void
19837lnp_state_machine::handle_set_file (file_name_index file)
19838{
19839 m_file = file;
19840
19841 const file_entry *fe = current_file ();
19842 if (fe == NULL)
19843 dwarf2_debug_line_missing_file_complaint ();
19844 else if (m_record_lines_p)
19845 {
19846 const char *dir = fe->include_dir (m_line_header);
19847
c24bdb02 19848 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19849 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19850 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19851 }
19852}
19853
19854void
19855lnp_state_machine::handle_const_add_pc ()
19856{
19857 CORE_ADDR adjust
19858 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19859
19860 CORE_ADDR addr_adj
19861 = (((m_op_index + adjust)
19862 / m_line_header->maximum_ops_per_instruction)
19863 * m_line_header->minimum_instruction_length);
19864
19865 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19866 m_op_index = ((m_op_index + adjust)
19867 % m_line_header->maximum_ops_per_instruction);
19868}
d9b3de22 19869
a05a36a5
DE
19870/* Return non-zero if we should add LINE to the line number table.
19871 LINE is the line to add, LAST_LINE is the last line that was added,
19872 LAST_SUBFILE is the subfile for LAST_LINE.
19873 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19874 had a non-zero discriminator.
19875
19876 We have to be careful in the presence of discriminators.
19877 E.g., for this line:
19878
19879 for (i = 0; i < 100000; i++);
19880
19881 clang can emit four line number entries for that one line,
19882 each with a different discriminator.
19883 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19884
19885 However, we want gdb to coalesce all four entries into one.
19886 Otherwise the user could stepi into the middle of the line and
19887 gdb would get confused about whether the pc really was in the
19888 middle of the line.
19889
19890 Things are further complicated by the fact that two consecutive
19891 line number entries for the same line is a heuristic used by gcc
19892 to denote the end of the prologue. So we can't just discard duplicate
19893 entries, we have to be selective about it. The heuristic we use is
19894 that we only collapse consecutive entries for the same line if at least
19895 one of those entries has a non-zero discriminator. PR 17276.
19896
19897 Note: Addresses in the line number state machine can never go backwards
19898 within one sequence, thus this coalescing is ok. */
19899
19900static int
804d2729
TT
19901dwarf_record_line_p (struct dwarf2_cu *cu,
19902 unsigned int line, unsigned int last_line,
a05a36a5
DE
19903 int line_has_non_zero_discriminator,
19904 struct subfile *last_subfile)
19905{
c24bdb02 19906 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19907 return 1;
19908 if (line != last_line)
19909 return 1;
19910 /* Same line for the same file that we've seen already.
19911 As a last check, for pr 17276, only record the line if the line
19912 has never had a non-zero discriminator. */
19913 if (!line_has_non_zero_discriminator)
19914 return 1;
19915 return 0;
19916}
19917
804d2729
TT
19918/* Use the CU's builder to record line number LINE beginning at
19919 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19920
19921static void
d9b3de22
DE
19922dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19923 unsigned int line, CORE_ADDR address,
804d2729 19924 struct dwarf2_cu *cu)
252a6764
DE
19925{
19926 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19927
27e0867f
DE
19928 if (dwarf_line_debug)
19929 {
19930 fprintf_unfiltered (gdb_stdlog,
19931 "Recording line %u, file %s, address %s\n",
19932 line, lbasename (subfile->name),
19933 paddress (gdbarch, address));
19934 }
19935
804d2729 19936 if (cu != nullptr)
c24bdb02 19937 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
19938}
19939
19940/* Subroutine of dwarf_decode_lines_1 to simplify it.
19941 Mark the end of a set of line number records.
d9b3de22 19942 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19943 If SUBFILE is NULL the request is ignored. */
19944
19945static void
19946dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19947 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19948{
27e0867f
DE
19949 if (subfile == NULL)
19950 return;
19951
19952 if (dwarf_line_debug)
19953 {
19954 fprintf_unfiltered (gdb_stdlog,
19955 "Finishing current line, file %s, address %s\n",
19956 lbasename (subfile->name),
19957 paddress (gdbarch, address));
19958 }
19959
804d2729 19960 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
19961}
19962
6f77053d
PA
19963void
19964lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19965{
d9b3de22
DE
19966 if (dwarf_line_debug)
19967 {
19968 fprintf_unfiltered (gdb_stdlog,
19969 "Processing actual line %u: file %u,"
94a72be7 19970 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19971 m_line, m_file,
6f77053d 19972 paddress (m_gdbarch, m_address),
94a72be7
AB
19973 m_is_stmt, m_discriminator,
19974 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19975 }
19976
6f77053d 19977 file_entry *fe = current_file ();
8c43009f
PA
19978
19979 if (fe == NULL)
d9b3de22
DE
19980 dwarf2_debug_line_missing_file_complaint ();
19981 /* For now we ignore lines not starting on an instruction boundary.
19982 But not when processing end_sequence for compatibility with the
19983 previous version of the code. */
6f77053d 19984 else if (m_op_index == 0 || end_sequence)
d9b3de22 19985 {
8c43009f 19986 fe->included_p = 1;
94a72be7
AB
19987 if (m_record_lines_p
19988 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 19989 {
c24bdb02 19990 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19991 || end_sequence)
d9b3de22 19992 {
804d2729
TT
19993 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19994 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19995 }
19996
19997 if (!end_sequence)
19998 {
804d2729 19999 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20000 m_line_has_non_zero_discriminator,
20001 m_last_subfile))
d9b3de22 20002 {
c24bdb02 20003 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20004 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20005 builder->get_current_subfile (),
6f77053d 20006 m_line, m_address,
804d2729 20007 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20008 }
c24bdb02 20009 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20010 m_last_line = m_line;
d9b3de22
DE
20011 }
20012 }
20013 }
20014}
20015
804d2729
TT
20016lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20017 line_header *lh, bool record_lines_p)
d9b3de22 20018{
804d2729 20019 m_cu = cu;
6f77053d
PA
20020 m_gdbarch = arch;
20021 m_record_lines_p = record_lines_p;
20022 m_line_header = lh;
d9b3de22 20023
804d2729 20024 m_currently_recording_lines = true;
d9b3de22 20025
d9b3de22
DE
20026 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20027 was a line entry for it so that the backend has a chance to adjust it
20028 and also record it in case it needs it. This is currently used by MIPS
20029 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20030 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20031 m_is_stmt = lh->default_is_stmt;
20032 m_discriminator = 0;
252a6764
DE
20033}
20034
6f77053d
PA
20035void
20036lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20037 const gdb_byte *line_ptr,
7ab6656f 20038 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20039{
7ab6656f
OJ
20040 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20041 the pc range of the CU. However, we restrict the test to only ADDRESS
20042 values of zero to preserve GDB's previous behaviour which is to handle
20043 the specific case of a function being GC'd by the linker. */
924c2928 20044
7ab6656f 20045 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20046 {
20047 /* This line table is for a function which has been
20048 GCd by the linker. Ignore it. PR gdb/12528 */
20049
518817b3 20050 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20051 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20052
b98664d3 20053 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20054 line_offset, objfile_name (objfile));
804d2729
TT
20055 m_currently_recording_lines = false;
20056 /* Note: m_currently_recording_lines is left as false until we see
20057 DW_LNE_end_sequence. */
924c2928
DE
20058 }
20059}
20060
f3f5162e 20061/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20062 Process the line number information in LH.
20063 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20064 program in order to set included_p for every referenced header. */
debd256d 20065
c906108c 20066static void
43f3e411
DE
20067dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20068 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20069{
d521ce57
TT
20070 const gdb_byte *line_ptr, *extended_end;
20071 const gdb_byte *line_end;
a8c50c1f 20072 unsigned int bytes_read, extended_len;
699ca60a 20073 unsigned char op_code, extended_op;
e142c38c 20074 CORE_ADDR baseaddr;
518817b3 20075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20076 bfd *abfd = objfile->obfd;
fbf65064 20077 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20078 /* True if we're recording line info (as opposed to building partial
20079 symtabs and just interested in finding include files mentioned by
20080 the line number program). */
20081 bool record_lines_p = !decode_for_pst_p;
e142c38c 20082
b3b3bada 20083 baseaddr = objfile->text_section_offset ();
c906108c 20084
debd256d
JB
20085 line_ptr = lh->statement_program_start;
20086 line_end = lh->statement_program_end;
c906108c
SS
20087
20088 /* Read the statement sequences until there's nothing left. */
20089 while (line_ptr < line_end)
20090 {
6f77053d
PA
20091 /* The DWARF line number program state machine. Reset the state
20092 machine at the start of each sequence. */
804d2729 20093 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20094 bool end_sequence = false;
d9b3de22 20095
8c43009f 20096 if (record_lines_p)
c906108c 20097 {
8c43009f
PA
20098 /* Start a subfile for the current file of the state
20099 machine. */
20100 const file_entry *fe = state_machine.current_file ();
20101
20102 if (fe != NULL)
804d2729 20103 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20104 }
20105
a738430d 20106 /* Decode the table. */
d9b3de22 20107 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20108 {
20109 op_code = read_1_byte (abfd, line_ptr);
20110 line_ptr += 1;
9aa1fe7e 20111
debd256d 20112 if (op_code >= lh->opcode_base)
6e70227d 20113 {
8e07a239 20114 /* Special opcode. */
6f77053d 20115 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20116 }
20117 else switch (op_code)
c906108c
SS
20118 {
20119 case DW_LNS_extended_op:
3e43a32a
MS
20120 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20121 &bytes_read);
473b7be6 20122 line_ptr += bytes_read;
a8c50c1f 20123 extended_end = line_ptr + extended_len;
c906108c
SS
20124 extended_op = read_1_byte (abfd, line_ptr);
20125 line_ptr += 1;
20126 switch (extended_op)
20127 {
20128 case DW_LNE_end_sequence:
6f77053d
PA
20129 state_machine.handle_end_sequence ();
20130 end_sequence = true;
c906108c
SS
20131 break;
20132 case DW_LNE_set_address:
d9b3de22
DE
20133 {
20134 CORE_ADDR address
c8a7a66f 20135 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20136 line_ptr += bytes_read;
6f77053d
PA
20137
20138 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20139 lowpc - baseaddr, address);
6f77053d 20140 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20141 }
c906108c
SS
20142 break;
20143 case DW_LNE_define_file:
debd256d 20144 {
d521ce57 20145 const char *cur_file;
ecfb656c
PA
20146 unsigned int mod_time, length;
20147 dir_index dindex;
6e70227d 20148
3e43a32a
MS
20149 cur_file = read_direct_string (abfd, line_ptr,
20150 &bytes_read);
debd256d 20151 line_ptr += bytes_read;
ecfb656c 20152 dindex = (dir_index)
debd256d
JB
20153 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20154 line_ptr += bytes_read;
20155 mod_time =
20156 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20157 line_ptr += bytes_read;
20158 length =
20159 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20160 line_ptr += bytes_read;
ecfb656c 20161 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20162 }
c906108c 20163 break;
d0c6ba3d 20164 case DW_LNE_set_discriminator:
6f77053d
PA
20165 {
20166 /* The discriminator is not interesting to the
20167 debugger; just ignore it. We still need to
20168 check its value though:
20169 if there are consecutive entries for the same
20170 (non-prologue) line we want to coalesce them.
20171 PR 17276. */
20172 unsigned int discr
20173 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20174 line_ptr += bytes_read;
20175
20176 state_machine.handle_set_discriminator (discr);
20177 }
d0c6ba3d 20178 break;
c906108c 20179 default:
b98664d3 20180 complaint (_("mangled .debug_line section"));
debd256d 20181 return;
c906108c 20182 }
a8c50c1f
DJ
20183 /* Make sure that we parsed the extended op correctly. If e.g.
20184 we expected a different address size than the producer used,
20185 we may have read the wrong number of bytes. */
20186 if (line_ptr != extended_end)
20187 {
b98664d3 20188 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20189 return;
20190 }
c906108c
SS
20191 break;
20192 case DW_LNS_copy:
6f77053d 20193 state_machine.handle_copy ();
c906108c
SS
20194 break;
20195 case DW_LNS_advance_pc:
2dc7f7b3
TT
20196 {
20197 CORE_ADDR adjust
20198 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20199 line_ptr += bytes_read;
6f77053d
PA
20200
20201 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20202 }
c906108c
SS
20203 break;
20204 case DW_LNS_advance_line:
a05a36a5
DE
20205 {
20206 int line_delta
20207 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20208 line_ptr += bytes_read;
6f77053d
PA
20209
20210 state_machine.handle_advance_line (line_delta);
a05a36a5 20211 }
c906108c
SS
20212 break;
20213 case DW_LNS_set_file:
d9b3de22 20214 {
6f77053d 20215 file_name_index file
ecfb656c
PA
20216 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20217 &bytes_read);
d9b3de22 20218 line_ptr += bytes_read;
8c43009f 20219
6f77053d 20220 state_machine.handle_set_file (file);
d9b3de22 20221 }
c906108c
SS
20222 break;
20223 case DW_LNS_set_column:
0ad93d4f 20224 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20225 line_ptr += bytes_read;
20226 break;
20227 case DW_LNS_negate_stmt:
6f77053d 20228 state_machine.handle_negate_stmt ();
c906108c
SS
20229 break;
20230 case DW_LNS_set_basic_block:
c906108c 20231 break;
c2c6d25f
JM
20232 /* Add to the address register of the state machine the
20233 address increment value corresponding to special opcode
a738430d
MK
20234 255. I.e., this value is scaled by the minimum
20235 instruction length since special opcode 255 would have
b021a221 20236 scaled the increment. */
c906108c 20237 case DW_LNS_const_add_pc:
6f77053d 20238 state_machine.handle_const_add_pc ();
c906108c
SS
20239 break;
20240 case DW_LNS_fixed_advance_pc:
3e29f34a 20241 {
6f77053d 20242 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20243 line_ptr += 2;
6f77053d
PA
20244
20245 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20246 }
c906108c 20247 break;
9aa1fe7e 20248 default:
a738430d
MK
20249 {
20250 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20251 int i;
a738430d 20252
debd256d 20253 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20254 {
20255 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20256 line_ptr += bytes_read;
20257 }
20258 }
c906108c
SS
20259 }
20260 }
d9b3de22
DE
20261
20262 if (!end_sequence)
20263 dwarf2_debug_line_missing_end_sequence_complaint ();
20264
20265 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20266 in which case we still finish recording the last line). */
6f77053d 20267 state_machine.record_line (true);
c906108c 20268 }
f3f5162e
DE
20269}
20270
20271/* Decode the Line Number Program (LNP) for the given line_header
20272 structure and CU. The actual information extracted and the type
20273 of structures created from the LNP depends on the value of PST.
20274
20275 1. If PST is NULL, then this procedure uses the data from the program
20276 to create all necessary symbol tables, and their linetables.
20277
20278 2. If PST is not NULL, this procedure reads the program to determine
20279 the list of files included by the unit represented by PST, and
20280 builds all the associated partial symbol tables.
20281
20282 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20283 It is used for relative paths in the line table.
20284 NOTE: When processing partial symtabs (pst != NULL),
20285 comp_dir == pst->dirname.
20286
20287 NOTE: It is important that psymtabs have the same file name (via strcmp)
20288 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20289 symtab we don't use it in the name of the psymtabs we create.
20290 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20291 A good testcase for this is mb-inline.exp.
20292
527f3840
JK
20293 LOWPC is the lowest address in CU (or 0 if not known).
20294
20295 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20296 for its PC<->lines mapping information. Otherwise only the filename
20297 table is read in. */
f3f5162e
DE
20298
20299static void
20300dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20301 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20302 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20303{
518817b3 20304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20305 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20306
527f3840
JK
20307 if (decode_mapping)
20308 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20309
20310 if (decode_for_pst_p)
20311 {
aaa75496
JB
20312 /* Now that we're done scanning the Line Header Program, we can
20313 create the psymtab of each included file. */
7ba99d21
AT
20314 for (auto &file_entry : lh->file_names ())
20315 if (file_entry.included_p == 1)
aaa75496 20316 {
c89b44cd 20317 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20318 const char *include_name =
7ba99d21
AT
20319 psymtab_include_file_name (lh, file_entry, pst,
20320 comp_dir, &name_holder);
c6da4cef 20321 if (include_name != NULL)
aaa75496
JB
20322 dwarf2_create_include_psymtab (include_name, pst, objfile);
20323 }
20324 }
cb1df416
DJ
20325 else
20326 {
20327 /* Make sure a symtab is created for every file, even files
20328 which contain only variables (i.e. no code with associated
20329 line numbers). */
c24bdb02
KS
20330 buildsym_compunit *builder = cu->get_builder ();
20331 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20332
7ba99d21 20333 for (auto &fe : lh->file_names ())
cb1df416 20334 {
804d2729 20335 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20336 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20337 {
c24bdb02 20338 builder->get_current_subfile ()->symtab
804d2729 20339 = allocate_symtab (cust,
c24bdb02 20340 builder->get_current_subfile ()->name);
43f3e411 20341 }
c24bdb02 20342 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20343 }
20344 }
c906108c
SS
20345}
20346
20347/* Start a subfile for DWARF. FILENAME is the name of the file and
20348 DIRNAME the name of the source directory which contains FILENAME
4d663531 20349 or NULL if not known.
c906108c
SS
20350 This routine tries to keep line numbers from identical absolute and
20351 relative file names in a common subfile.
20352
20353 Using the `list' example from the GDB testsuite, which resides in
20354 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20355 of /srcdir/list0.c yields the following debugging information for list0.c:
20356
c5aa993b 20357 DW_AT_name: /srcdir/list0.c
4d663531 20358 DW_AT_comp_dir: /compdir
357e46e7 20359 files.files[0].name: list0.h
c5aa993b 20360 files.files[0].dir: /srcdir
357e46e7 20361 files.files[1].name: list0.c
c5aa993b 20362 files.files[1].dir: /srcdir
c906108c
SS
20363
20364 The line number information for list0.c has to end up in a single
4f1520fb
FR
20365 subfile, so that `break /srcdir/list0.c:1' works as expected.
20366 start_subfile will ensure that this happens provided that we pass the
20367 concatenation of files.files[1].dir and files.files[1].name as the
20368 subfile's name. */
c906108c
SS
20369
20370static void
804d2729
TT
20371dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20372 const char *dirname)
c906108c 20373{
43816ebc 20374 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20375
4d663531 20376 /* In order not to lose the line information directory,
4f1520fb
FR
20377 we concatenate it to the filename when it makes sense.
20378 Note that the Dwarf3 standard says (speaking of filenames in line
20379 information): ``The directory index is ignored for file names
20380 that represent full path names''. Thus ignoring dirname in the
20381 `else' branch below isn't an issue. */
c906108c 20382
d5166ae1 20383 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20384 {
43816ebc
TT
20385 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20386 filename = copy.get ();
d521ce57 20387 }
c906108c 20388
c24bdb02 20389 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20390}
20391
804d2729
TT
20392/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20393 buildsym_compunit constructor. */
f4dc4d17 20394
c24bdb02
KS
20395struct compunit_symtab *
20396dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20397 CORE_ADDR low_pc)
f4dc4d17 20398{
c24bdb02 20399 gdb_assert (m_builder == nullptr);
43f3e411 20400
c24bdb02
KS
20401 m_builder.reset (new struct buildsym_compunit
20402 (per_cu->dwarf2_per_objfile->objfile,
20403 name, comp_dir, language, low_pc));
93b8bea4 20404
c24bdb02 20405 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20406
c24bdb02
KS
20407 get_builder ()->record_debugformat ("DWARF 2");
20408 get_builder ()->record_producer (producer);
f4dc4d17 20409
c24bdb02 20410 processing_has_namespace_info = false;
43f3e411 20411
c24bdb02 20412 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20413}
20414
4c2df51b
DJ
20415static void
20416var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20417 struct dwarf2_cu *cu)
4c2df51b 20418{
518817b3 20419 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20420 struct comp_unit_head *cu_header = &cu->header;
20421
4c2df51b
DJ
20422 /* NOTE drow/2003-01-30: There used to be a comment and some special
20423 code here to turn a symbol with DW_AT_external and a
20424 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20425 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20426 with some versions of binutils) where shared libraries could have
20427 relocations against symbols in their debug information - the
20428 minimal symbol would have the right address, but the debug info
20429 would not. It's no longer necessary, because we will explicitly
20430 apply relocations when we read in the debug information now. */
20431
20432 /* A DW_AT_location attribute with no contents indicates that a
20433 variable has been optimized away. */
4fc6c0d5 20434 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20435 {
f1e6e072 20436 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20437 return;
20438 }
20439
20440 /* Handle one degenerate form of location expression specially, to
20441 preserve GDB's previous behavior when section offsets are
336d760d
AT
20442 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20443 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20444
4fc6c0d5 20445 if (attr->form_is_block ()
3019eac3
DE
20446 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20447 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20448 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20449 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20450 && (DW_BLOCK (attr)->size
20451 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20452 {
891d2f0b 20453 unsigned int dummy;
4c2df51b 20454
3019eac3 20455 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20456 SET_SYMBOL_VALUE_ADDRESS
20457 (sym, cu->header.read_address (objfile->obfd,
20458 DW_BLOCK (attr)->data + 1,
20459 &dummy));
3019eac3 20460 else
38583298
TT
20461 SET_SYMBOL_VALUE_ADDRESS
20462 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20463 &dummy));
f1e6e072 20464 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20465 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20466 SET_SYMBOL_VALUE_ADDRESS
20467 (sym,
20468 SYMBOL_VALUE_ADDRESS (sym)
20469 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20470 return;
20471 }
20472
20473 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20474 expression evaluator, and use LOC_COMPUTED only when necessary
20475 (i.e. when the value of a register or memory location is
20476 referenced, or a thread-local block, etc.). Then again, it might
20477 not be worthwhile. I'm assuming that it isn't unless performance
20478 or memory numbers show me otherwise. */
20479
f1e6e072 20480 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20481
f1e6e072 20482 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20483 cu->has_loclist = true;
4c2df51b
DJ
20484}
20485
c906108c
SS
20486/* Given a pointer to a DWARF information entry, figure out if we need
20487 to make a symbol table entry for it, and if so, create a new entry
20488 and return a pointer to it.
20489 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20490 used the passed type.
20491 If SPACE is not NULL, use it to hold the new symbol. If it is
20492 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20493
20494static struct symbol *
5e2db402
TT
20495new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20496 struct symbol *space)
c906108c 20497{
518817b3
SM
20498 struct dwarf2_per_objfile *dwarf2_per_objfile
20499 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20500 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20501 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20502 struct symbol *sym = NULL;
15d034d0 20503 const char *name;
c906108c
SS
20504 struct attribute *attr = NULL;
20505 struct attribute *attr2 = NULL;
e142c38c 20506 CORE_ADDR baseaddr;
e37fd15a
SW
20507 struct pending **list_to_add = NULL;
20508
edb3359d 20509 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20510
b3b3bada 20511 baseaddr = objfile->text_section_offset ();
c906108c 20512
94af9270 20513 name = dwarf2_name (die, cu);
c906108c
SS
20514 if (name)
20515 {
94af9270 20516 const char *linkagename;
34eaf542 20517 int suppress_add = 0;
94af9270 20518
34eaf542
TT
20519 if (space)
20520 sym = space;
20521 else
e623cf5d 20522 sym = allocate_symbol (objfile);
c906108c 20523 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20524
20525 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20526 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20527 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20528 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20529
f55ee35c
JK
20530 /* Fortran does not have mangling standard and the mangling does differ
20531 between gfortran, iFort etc. */
20532 if (cu->language == language_fortran
468c0cbb
CB
20533 && symbol_get_demangled_name (sym) == NULL)
20534 symbol_set_demangled_name (sym,
cfc594ee 20535 dwarf2_full_name (name, die, cu),
29df156d 20536 NULL);
f55ee35c 20537
c906108c 20538 /* Default assumptions.
c5aa993b 20539 Use the passed type or decode it from the die. */
176620f1 20540 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20541 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20542 if (type != NULL)
20543 SYMBOL_TYPE (sym) = type;
20544 else
e7c27a73 20545 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20546 attr = dwarf2_attr (die,
20547 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20548 cu);
435d3d88 20549 if (attr != nullptr)
c906108c
SS
20550 {
20551 SYMBOL_LINE (sym) = DW_UNSND (attr);
20552 }
cb1df416 20553
edb3359d
DJ
20554 attr = dwarf2_attr (die,
20555 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20556 cu);
435d3d88 20557 if (attr != nullptr)
cb1df416 20558 {
ecfb656c 20559 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20560 struct file_entry *fe;
9a619af0 20561
ecfb656c
PA
20562 if (cu->line_header != NULL)
20563 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20564 else
20565 fe = NULL;
20566
20567 if (fe == NULL)
b98664d3 20568 complaint (_("file index out of range"));
8c43009f
PA
20569 else
20570 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20571 }
20572
c906108c
SS
20573 switch (die->tag)
20574 {
20575 case DW_TAG_label:
e142c38c 20576 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20577 if (attr != nullptr)
3e29f34a
MR
20578 {
20579 CORE_ADDR addr;
20580
cd6c91b4 20581 addr = attr->value_as_address ();
3e29f34a 20582 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20583 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20584 }
0f5238ed
TT
20585 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20586 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20587 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20588 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20589 break;
20590 case DW_TAG_subprogram:
20591 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20592 finish_block. */
f1e6e072 20593 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20594 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20595 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20596 || cu->language == language_ada
20597 || cu->language == language_fortran)
c906108c 20598 {
2cfa0c8d 20599 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20600 Ada and Fortran subprograms, whether marked external or
20601 not, are always stored as a global symbol, because we want
20602 to be able to access them globally. For instance, we want
20603 to be able to break on a nested subprogram without having
20604 to specify the context. */
c24bdb02 20605 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20606 }
20607 else
20608 {
e37fd15a 20609 list_to_add = cu->list_in_scope;
c906108c
SS
20610 }
20611 break;
edb3359d
DJ
20612 case DW_TAG_inlined_subroutine:
20613 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20614 finish_block. */
f1e6e072 20615 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20616 SYMBOL_INLINED (sym) = 1;
481860b3 20617 list_to_add = cu->list_in_scope;
edb3359d 20618 break;
34eaf542
TT
20619 case DW_TAG_template_value_param:
20620 suppress_add = 1;
20621 /* Fall through. */
72929c62 20622 case DW_TAG_constant:
c906108c 20623 case DW_TAG_variable:
254e6b9e 20624 case DW_TAG_member:
0963b4bd
MS
20625 /* Compilation with minimal debug info may result in
20626 variables with missing type entries. Change the
20627 misleading `void' type to something sensible. */
c906108c 20628 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20629 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20630
e142c38c 20631 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20632 /* In the case of DW_TAG_member, we should only be called for
20633 static const members. */
20634 if (die->tag == DW_TAG_member)
20635 {
3863f96c
DE
20636 /* dwarf2_add_field uses die_is_declaration,
20637 so we do the same. */
254e6b9e
DE
20638 gdb_assert (die_is_declaration (die, cu));
20639 gdb_assert (attr);
20640 }
435d3d88 20641 if (attr != nullptr)
c906108c 20642 {
e7c27a73 20643 dwarf2_const_value (attr, sym, cu);
e142c38c 20644 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20645 if (!suppress_add)
34eaf542
TT
20646 {
20647 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20648 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20649 else
e37fd15a 20650 list_to_add = cu->list_in_scope;
34eaf542 20651 }
c906108c
SS
20652 break;
20653 }
e142c38c 20654 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20655 if (attr != nullptr)
c906108c 20656 {
e7c27a73 20657 var_decode_location (attr, sym, cu);
e142c38c 20658 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20659
20660 /* Fortran explicitly imports any global symbols to the local
20661 scope by DW_TAG_common_block. */
20662 if (cu->language == language_fortran && die->parent
20663 && die->parent->tag == DW_TAG_common_block)
20664 attr2 = NULL;
20665
caac4577
JG
20666 if (SYMBOL_CLASS (sym) == LOC_STATIC
20667 && SYMBOL_VALUE_ADDRESS (sym) == 0
20668 && !dwarf2_per_objfile->has_section_at_zero)
20669 {
20670 /* When a static variable is eliminated by the linker,
20671 the corresponding debug information is not stripped
20672 out, but the variable address is set to null;
20673 do not add such variables into symbol table. */
20674 }
20675 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20676 {
4b610737
TT
20677 if (SYMBOL_CLASS (sym) == LOC_STATIC
20678 && (objfile->flags & OBJF_MAINLINE) == 0
20679 && dwarf2_per_objfile->can_copy)
20680 {
20681 /* A global static variable might be subject to
20682 copy relocation. We first check for a local
20683 minsym, though, because maybe the symbol was
20684 marked hidden, in which case this would not
20685 apply. */
20686 bound_minimal_symbol found
20687 = (lookup_minimal_symbol_linkage
987012b8 20688 (sym->linkage_name (), objfile));
4b610737
TT
20689 if (found.minsym != nullptr)
20690 sym->maybe_copied = 1;
20691 }
f55ee35c 20692
1c809c68
TT
20693 /* A variable with DW_AT_external is never static,
20694 but it may be block-scoped. */
804d2729 20695 list_to_add
c24bdb02
KS
20696 = ((cu->list_in_scope
20697 == cu->get_builder ()->get_file_symbols ())
20698 ? cu->get_builder ()->get_global_symbols ()
804d2729 20699 : cu->list_in_scope);
1c809c68 20700 }
c906108c 20701 else
e37fd15a 20702 list_to_add = cu->list_in_scope;
c906108c
SS
20703 }
20704 else
20705 {
20706 /* We do not know the address of this symbol.
c5aa993b
JM
20707 If it is an external symbol and we have type information
20708 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20709 The address of the variable will then be determined from
20710 the minimal symbol table whenever the variable is
20711 referenced. */
e142c38c 20712 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20713
20714 /* Fortran explicitly imports any global symbols to the local
20715 scope by DW_TAG_common_block. */
20716 if (cu->language == language_fortran && die->parent
20717 && die->parent->tag == DW_TAG_common_block)
20718 {
20719 /* SYMBOL_CLASS doesn't matter here because
20720 read_common_block is going to reset it. */
20721 if (!suppress_add)
20722 list_to_add = cu->list_in_scope;
20723 }
20724 else if (attr2 && (DW_UNSND (attr2) != 0)
20725 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20726 {
0fe7935b
DJ
20727 /* A variable with DW_AT_external is never static, but it
20728 may be block-scoped. */
804d2729 20729 list_to_add
c24bdb02
KS
20730 = ((cu->list_in_scope
20731 == cu->get_builder ()->get_file_symbols ())
20732 ? cu->get_builder ()->get_global_symbols ()
804d2729 20733 : cu->list_in_scope);
0fe7935b 20734
f1e6e072 20735 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20736 }
442ddf59
JK
20737 else if (!die_is_declaration (die, cu))
20738 {
20739 /* Use the default LOC_OPTIMIZED_OUT class. */
20740 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20741 if (!suppress_add)
20742 list_to_add = cu->list_in_scope;
442ddf59 20743 }
c906108c
SS
20744 }
20745 break;
20746 case DW_TAG_formal_parameter:
a60f3166
TT
20747 {
20748 /* If we are inside a function, mark this as an argument. If
20749 not, we might be looking at an argument to an inlined function
20750 when we do not have enough information to show inlined frames;
20751 pretend it's a local variable in that case so that the user can
20752 still see it. */
804d2729 20753 struct context_stack *curr
c24bdb02 20754 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20755 if (curr != nullptr && curr->name != nullptr)
20756 SYMBOL_IS_ARGUMENT (sym) = 1;
20757 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20758 if (attr != nullptr)
a60f3166
TT
20759 {
20760 var_decode_location (attr, sym, cu);
20761 }
20762 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20763 if (attr != nullptr)
a60f3166
TT
20764 {
20765 dwarf2_const_value (attr, sym, cu);
20766 }
f346a30d 20767
a60f3166
TT
20768 list_to_add = cu->list_in_scope;
20769 }
c906108c
SS
20770 break;
20771 case DW_TAG_unspecified_parameters:
20772 /* From varargs functions; gdb doesn't seem to have any
20773 interest in this information, so just ignore it for now.
20774 (FIXME?) */
20775 break;
34eaf542
TT
20776 case DW_TAG_template_type_param:
20777 suppress_add = 1;
20778 /* Fall through. */
c906108c 20779 case DW_TAG_class_type:
680b30c7 20780 case DW_TAG_interface_type:
c906108c
SS
20781 case DW_TAG_structure_type:
20782 case DW_TAG_union_type:
72019c9c 20783 case DW_TAG_set_type:
c906108c 20784 case DW_TAG_enumeration_type:
f1e6e072 20785 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20786 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20787
63d06c5c 20788 {
9c37b5ae 20789 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20790 really ever be static objects: otherwise, if you try
20791 to, say, break of a class's method and you're in a file
20792 which doesn't mention that class, it won't work unless
20793 the check for all static symbols in lookup_symbol_aux
20794 saves you. See the OtherFileClass tests in
20795 gdb.c++/namespace.exp. */
20796
e37fd15a 20797 if (!suppress_add)
34eaf542 20798 {
c24bdb02 20799 buildsym_compunit *builder = cu->get_builder ();
804d2729 20800 list_to_add
c24bdb02 20801 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20802 && cu->language == language_cplus
c24bdb02 20803 ? builder->get_global_symbols ()
804d2729 20804 : cu->list_in_scope);
63d06c5c 20805
64382290 20806 /* The semantics of C++ state that "struct foo {
9c37b5ae 20807 ... }" also defines a typedef for "foo". */
64382290 20808 if (cu->language == language_cplus
45280282 20809 || cu->language == language_ada
c44af4eb
TT
20810 || cu->language == language_d
20811 || cu->language == language_rust)
64382290
TT
20812 {
20813 /* The symbol's name is already allocated along
20814 with this objfile, so we don't need to
20815 duplicate it for the type. */
20816 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20817 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20818 }
63d06c5c
DC
20819 }
20820 }
c906108c
SS
20821 break;
20822 case DW_TAG_typedef:
f1e6e072 20823 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20824 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20825 list_to_add = cu->list_in_scope;
63d06c5c 20826 break;
c906108c 20827 case DW_TAG_base_type:
a02abb62 20828 case DW_TAG_subrange_type:
f1e6e072 20829 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20830 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20831 list_to_add = cu->list_in_scope;
c906108c
SS
20832 break;
20833 case DW_TAG_enumerator:
e142c38c 20834 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20835 if (attr != nullptr)
c906108c 20836 {
e7c27a73 20837 dwarf2_const_value (attr, sym, cu);
c906108c 20838 }
63d06c5c
DC
20839 {
20840 /* NOTE: carlton/2003-11-10: See comment above in the
20841 DW_TAG_class_type, etc. block. */
20842
804d2729 20843 list_to_add
c24bdb02 20844 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20845 && cu->language == language_cplus
c24bdb02 20846 ? cu->get_builder ()->get_global_symbols ()
804d2729 20847 : cu->list_in_scope);
63d06c5c 20848 }
c906108c 20849 break;
74921315 20850 case DW_TAG_imported_declaration:
5c4e30ca 20851 case DW_TAG_namespace:
f1e6e072 20852 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20853 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20854 break;
530e8392
KB
20855 case DW_TAG_module:
20856 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20857 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20858 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20859 break;
4357ac6c 20860 case DW_TAG_common_block:
f1e6e072 20861 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20862 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20863 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20864 break;
c906108c
SS
20865 default:
20866 /* Not a tag we recognize. Hopefully we aren't processing
20867 trash data, but since we must specifically ignore things
20868 we don't recognize, there is nothing else we should do at
0963b4bd 20869 this point. */
b98664d3 20870 complaint (_("unsupported tag: '%s'"),
4d3c2250 20871 dwarf_tag_name (die->tag));
c906108c
SS
20872 break;
20873 }
df8a16a1 20874
e37fd15a
SW
20875 if (suppress_add)
20876 {
20877 sym->hash_next = objfile->template_symbols;
20878 objfile->template_symbols = sym;
20879 list_to_add = NULL;
20880 }
20881
20882 if (list_to_add != NULL)
d3cb6808 20883 add_symbol_to_list (sym, list_to_add);
e37fd15a 20884
df8a16a1
DJ
20885 /* For the benefit of old versions of GCC, check for anonymous
20886 namespaces based on the demangled name. */
4d4ec4e5 20887 if (!cu->processing_has_namespace_info
94af9270 20888 && cu->language == language_cplus)
c24bdb02 20889 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20890 }
20891 return (sym);
20892}
20893
98bfdba5
PA
20894/* Given an attr with a DW_FORM_dataN value in host byte order,
20895 zero-extend it as appropriate for the symbol's type. The DWARF
20896 standard (v4) is not entirely clear about the meaning of using
20897 DW_FORM_dataN for a constant with a signed type, where the type is
20898 wider than the data. The conclusion of a discussion on the DWARF
20899 list was that this is unspecified. We choose to always zero-extend
20900 because that is the interpretation long in use by GCC. */
c906108c 20901
98bfdba5 20902static gdb_byte *
ff39bb5e 20903dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20904 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20905{
518817b3 20906 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20907 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20908 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20909 LONGEST l = DW_UNSND (attr);
20910
20911 if (bits < sizeof (*value) * 8)
20912 {
20913 l &= ((LONGEST) 1 << bits) - 1;
20914 *value = l;
20915 }
20916 else if (bits == sizeof (*value) * 8)
20917 *value = l;
20918 else
20919 {
224c3ddb 20920 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20921 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20922 return bytes;
20923 }
20924
20925 return NULL;
20926}
20927
20928/* Read a constant value from an attribute. Either set *VALUE, or if
20929 the value does not fit in *VALUE, set *BYTES - either already
20930 allocated on the objfile obstack, or newly allocated on OBSTACK,
20931 or, set *BATON, if we translated the constant to a location
20932 expression. */
20933
20934static void
ff39bb5e 20935dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20936 const char *name, struct obstack *obstack,
20937 struct dwarf2_cu *cu,
d521ce57 20938 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20939 struct dwarf2_locexpr_baton **baton)
20940{
518817b3 20941 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20942 struct comp_unit_head *cu_header = &cu->header;
c906108c 20943 struct dwarf_block *blk;
98bfdba5
PA
20944 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20945 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20946
20947 *value = 0;
20948 *bytes = NULL;
20949 *baton = NULL;
c906108c
SS
20950
20951 switch (attr->form)
20952 {
20953 case DW_FORM_addr:
336d760d 20954 case DW_FORM_addrx:
3019eac3 20955 case DW_FORM_GNU_addr_index:
ac56253d 20956 {
ac56253d
TT
20957 gdb_byte *data;
20958
98bfdba5
PA
20959 if (TYPE_LENGTH (type) != cu_header->addr_size)
20960 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20961 cu_header->addr_size,
98bfdba5 20962 TYPE_LENGTH (type));
ac56253d
TT
20963 /* Symbols of this form are reasonably rare, so we just
20964 piggyback on the existing location code rather than writing
20965 a new implementation of symbol_computed_ops. */
8d749320 20966 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20967 (*baton)->per_cu = cu->per_cu;
20968 gdb_assert ((*baton)->per_cu);
ac56253d 20969
98bfdba5 20970 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20971 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20972 (*baton)->data = data;
ac56253d
TT
20973
20974 data[0] = DW_OP_addr;
20975 store_unsigned_integer (&data[1], cu_header->addr_size,
20976 byte_order, DW_ADDR (attr));
20977 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20978 }
c906108c 20979 break;
4ac36638 20980 case DW_FORM_string:
93b5768b 20981 case DW_FORM_strp:
cf532bd1 20982 case DW_FORM_strx:
3019eac3 20983 case DW_FORM_GNU_str_index:
36586728 20984 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20985 /* DW_STRING is already allocated on the objfile obstack, point
20986 directly to it. */
d521ce57 20987 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20988 break;
c906108c
SS
20989 case DW_FORM_block1:
20990 case DW_FORM_block2:
20991 case DW_FORM_block4:
20992 case DW_FORM_block:
2dc7f7b3 20993 case DW_FORM_exprloc:
0224619f 20994 case DW_FORM_data16:
c906108c 20995 blk = DW_BLOCK (attr);
98bfdba5
PA
20996 if (TYPE_LENGTH (type) != blk->size)
20997 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20998 TYPE_LENGTH (type));
20999 *bytes = blk->data;
c906108c 21000 break;
2df3850c
JM
21001
21002 /* The DW_AT_const_value attributes are supposed to carry the
21003 symbol's value "represented as it would be on the target
21004 architecture." By the time we get here, it's already been
21005 converted to host endianness, so we just need to sign- or
21006 zero-extend it as appropriate. */
21007 case DW_FORM_data1:
3aef2284 21008 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21009 break;
c906108c 21010 case DW_FORM_data2:
3aef2284 21011 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21012 break;
c906108c 21013 case DW_FORM_data4:
3aef2284 21014 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21015 break;
c906108c 21016 case DW_FORM_data8:
3aef2284 21017 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21018 break;
21019
c906108c 21020 case DW_FORM_sdata:
663c44ac 21021 case DW_FORM_implicit_const:
98bfdba5 21022 *value = DW_SND (attr);
2df3850c
JM
21023 break;
21024
c906108c 21025 case DW_FORM_udata:
98bfdba5 21026 *value = DW_UNSND (attr);
c906108c 21027 break;
2df3850c 21028
c906108c 21029 default:
b98664d3 21030 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21031 dwarf_form_name (attr->form));
98bfdba5 21032 *value = 0;
c906108c
SS
21033 break;
21034 }
21035}
21036
2df3850c 21037
98bfdba5
PA
21038/* Copy constant value from an attribute to a symbol. */
21039
2df3850c 21040static void
ff39bb5e 21041dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21042 struct dwarf2_cu *cu)
2df3850c 21043{
518817b3 21044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21045 LONGEST value;
d521ce57 21046 const gdb_byte *bytes;
98bfdba5 21047 struct dwarf2_locexpr_baton *baton;
2df3850c 21048
98bfdba5 21049 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21050 sym->print_name (),
98bfdba5
PA
21051 &objfile->objfile_obstack, cu,
21052 &value, &bytes, &baton);
2df3850c 21053
98bfdba5
PA
21054 if (baton != NULL)
21055 {
98bfdba5 21056 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21057 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21058 }
21059 else if (bytes != NULL)
21060 {
21061 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21062 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21063 }
21064 else
21065 {
21066 SYMBOL_VALUE (sym) = value;
f1e6e072 21067 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21068 }
2df3850c
JM
21069}
21070
c906108c
SS
21071/* Return the type of the die in question using its DW_AT_type attribute. */
21072
21073static struct type *
e7c27a73 21074die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21075{
c906108c 21076 struct attribute *type_attr;
c906108c 21077
e142c38c 21078 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21079 if (!type_attr)
21080 {
518817b3 21081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21082 /* A missing DW_AT_type represents a void type. */
518817b3 21083 return objfile_type (objfile)->builtin_void;
c906108c 21084 }
348e048f 21085
673bfd45 21086 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21087}
21088
b4ba55a1
JB
21089/* True iff CU's producer generates GNAT Ada auxiliary information
21090 that allows to find parallel types through that information instead
21091 of having to do expensive parallel lookups by type name. */
21092
21093static int
21094need_gnat_info (struct dwarf2_cu *cu)
21095{
de4cb04a
JB
21096 /* Assume that the Ada compiler was GNAT, which always produces
21097 the auxiliary information. */
21098 return (cu->language == language_ada);
b4ba55a1
JB
21099}
21100
b4ba55a1
JB
21101/* Return the auxiliary type of the die in question using its
21102 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21103 attribute is not present. */
21104
21105static struct type *
21106die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21107{
b4ba55a1 21108 struct attribute *type_attr;
b4ba55a1
JB
21109
21110 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21111 if (!type_attr)
21112 return NULL;
21113
673bfd45 21114 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21115}
21116
21117/* If DIE has a descriptive_type attribute, then set the TYPE's
21118 descriptive type accordingly. */
21119
21120static void
21121set_descriptive_type (struct type *type, struct die_info *die,
21122 struct dwarf2_cu *cu)
21123{
21124 struct type *descriptive_type = die_descriptive_type (die, cu);
21125
21126 if (descriptive_type)
21127 {
21128 ALLOCATE_GNAT_AUX_TYPE (type);
21129 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21130 }
21131}
21132
c906108c
SS
21133/* Return the containing type of the die in question using its
21134 DW_AT_containing_type attribute. */
21135
21136static struct type *
e7c27a73 21137die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21138{
c906108c 21139 struct attribute *type_attr;
518817b3 21140 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21141
e142c38c 21142 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21143 if (!type_attr)
21144 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21145 "[in module %s]"), objfile_name (objfile));
33ac96f0 21146
673bfd45 21147 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21148}
21149
ac9ec31b
DE
21150/* Return an error marker type to use for the ill formed type in DIE/CU. */
21151
21152static struct type *
21153build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21154{
518817b3
SM
21155 struct dwarf2_per_objfile *dwarf2_per_objfile
21156 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21157 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21158 char *saved;
ac9ec31b 21159
528e1572
SM
21160 std::string message
21161 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21162 objfile_name (objfile),
21163 sect_offset_str (cu->header.sect_off),
21164 sect_offset_str (die->sect_off));
efba19b0 21165 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21166
19f392bc 21167 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21168}
21169
673bfd45 21170/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21171 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21172 DW_AT_containing_type.
673bfd45
DE
21173 If there is no type substitute an error marker. */
21174
c906108c 21175static struct type *
ff39bb5e 21176lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21177 struct dwarf2_cu *cu)
c906108c 21178{
518817b3
SM
21179 struct dwarf2_per_objfile *dwarf2_per_objfile
21180 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21181 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21182 struct type *this_type;
21183
ac9ec31b
DE
21184 gdb_assert (attr->name == DW_AT_type
21185 || attr->name == DW_AT_GNAT_descriptive_type
21186 || attr->name == DW_AT_containing_type);
21187
673bfd45
DE
21188 /* First see if we have it cached. */
21189
36586728
TT
21190 if (attr->form == DW_FORM_GNU_ref_alt)
21191 {
21192 struct dwarf2_per_cu_data *per_cu;
9c541725 21193 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21194
ed2dc618
SM
21195 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21196 dwarf2_per_objfile);
9c541725 21197 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21198 }
cd6c91b4 21199 else if (attr->form_is_ref ())
673bfd45 21200 {
9c541725 21201 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21202
9c541725 21203 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21204 }
55f1336d 21205 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21206 {
ac9ec31b 21207 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21208
ac9ec31b 21209 return get_signatured_type (die, signature, cu);
673bfd45
DE
21210 }
21211 else
21212 {
b98664d3 21213 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21214 " at %s [in module %s]"),
21215 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21216 objfile_name (objfile));
ac9ec31b 21217 return build_error_marker_type (cu, die);
673bfd45
DE
21218 }
21219
21220 /* If not cached we need to read it in. */
21221
21222 if (this_type == NULL)
21223 {
ac9ec31b 21224 struct die_info *type_die = NULL;
673bfd45
DE
21225 struct dwarf2_cu *type_cu = cu;
21226
cd6c91b4 21227 if (attr->form_is_ref ())
ac9ec31b
DE
21228 type_die = follow_die_ref (die, attr, &type_cu);
21229 if (type_die == NULL)
21230 return build_error_marker_type (cu, die);
21231 /* If we find the type now, it's probably because the type came
3019eac3
DE
21232 from an inter-CU reference and the type's CU got expanded before
21233 ours. */
ac9ec31b 21234 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21235 }
21236
21237 /* If we still don't have a type use an error marker. */
21238
21239 if (this_type == NULL)
ac9ec31b 21240 return build_error_marker_type (cu, die);
673bfd45 21241
f792889a 21242 return this_type;
c906108c
SS
21243}
21244
673bfd45
DE
21245/* Return the type in DIE, CU.
21246 Returns NULL for invalid types.
21247
02142a6c 21248 This first does a lookup in die_type_hash,
673bfd45
DE
21249 and only reads the die in if necessary.
21250
21251 NOTE: This can be called when reading in partial or full symbols. */
21252
f792889a 21253static struct type *
e7c27a73 21254read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21255{
f792889a
DJ
21256 struct type *this_type;
21257
21258 this_type = get_die_type (die, cu);
21259 if (this_type)
21260 return this_type;
21261
673bfd45
DE
21262 return read_type_die_1 (die, cu);
21263}
21264
21265/* Read the type in DIE, CU.
21266 Returns NULL for invalid types. */
21267
21268static struct type *
21269read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21270{
21271 struct type *this_type = NULL;
21272
c906108c
SS
21273 switch (die->tag)
21274 {
21275 case DW_TAG_class_type:
680b30c7 21276 case DW_TAG_interface_type:
c906108c
SS
21277 case DW_TAG_structure_type:
21278 case DW_TAG_union_type:
f792889a 21279 this_type = read_structure_type (die, cu);
c906108c
SS
21280 break;
21281 case DW_TAG_enumeration_type:
f792889a 21282 this_type = read_enumeration_type (die, cu);
c906108c
SS
21283 break;
21284 case DW_TAG_subprogram:
21285 case DW_TAG_subroutine_type:
edb3359d 21286 case DW_TAG_inlined_subroutine:
f792889a 21287 this_type = read_subroutine_type (die, cu);
c906108c
SS
21288 break;
21289 case DW_TAG_array_type:
f792889a 21290 this_type = read_array_type (die, cu);
c906108c 21291 break;
72019c9c 21292 case DW_TAG_set_type:
f792889a 21293 this_type = read_set_type (die, cu);
72019c9c 21294 break;
c906108c 21295 case DW_TAG_pointer_type:
f792889a 21296 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21297 break;
21298 case DW_TAG_ptr_to_member_type:
f792889a 21299 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21300 break;
21301 case DW_TAG_reference_type:
4297a3f0
AV
21302 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21303 break;
21304 case DW_TAG_rvalue_reference_type:
21305 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21306 break;
21307 case DW_TAG_const_type:
f792889a 21308 this_type = read_tag_const_type (die, cu);
c906108c
SS
21309 break;
21310 case DW_TAG_volatile_type:
f792889a 21311 this_type = read_tag_volatile_type (die, cu);
c906108c 21312 break;
06d66ee9
TT
21313 case DW_TAG_restrict_type:
21314 this_type = read_tag_restrict_type (die, cu);
21315 break;
c906108c 21316 case DW_TAG_string_type:
f792889a 21317 this_type = read_tag_string_type (die, cu);
c906108c
SS
21318 break;
21319 case DW_TAG_typedef:
f792889a 21320 this_type = read_typedef (die, cu);
c906108c 21321 break;
a02abb62 21322 case DW_TAG_subrange_type:
f792889a 21323 this_type = read_subrange_type (die, cu);
a02abb62 21324 break;
c906108c 21325 case DW_TAG_base_type:
f792889a 21326 this_type = read_base_type (die, cu);
c906108c 21327 break;
81a17f79 21328 case DW_TAG_unspecified_type:
f792889a 21329 this_type = read_unspecified_type (die, cu);
81a17f79 21330 break;
0114d602
DJ
21331 case DW_TAG_namespace:
21332 this_type = read_namespace_type (die, cu);
21333 break;
f55ee35c
JK
21334 case DW_TAG_module:
21335 this_type = read_module_type (die, cu);
21336 break;
a2c2acaf
MW
21337 case DW_TAG_atomic_type:
21338 this_type = read_tag_atomic_type (die, cu);
21339 break;
c906108c 21340 default:
b98664d3 21341 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21342 dwarf_tag_name (die->tag));
c906108c
SS
21343 break;
21344 }
63d06c5c 21345
f792889a 21346 return this_type;
63d06c5c
DC
21347}
21348
abc72ce4
DE
21349/* See if we can figure out if the class lives in a namespace. We do
21350 this by looking for a member function; its demangled name will
21351 contain namespace info, if there is any.
21352 Return the computed name or NULL.
21353 Space for the result is allocated on the objfile's obstack.
21354 This is the full-die version of guess_partial_die_structure_name.
21355 In this case we know DIE has no useful parent. */
21356
43816ebc 21357static const char *
abc72ce4
DE
21358guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21359{
21360 struct die_info *spec_die;
21361 struct dwarf2_cu *spec_cu;
21362 struct die_info *child;
518817b3 21363 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21364
21365 spec_cu = cu;
21366 spec_die = die_specification (die, &spec_cu);
21367 if (spec_die != NULL)
21368 {
21369 die = spec_die;
21370 cu = spec_cu;
21371 }
21372
21373 for (child = die->child;
21374 child != NULL;
21375 child = child->sibling)
21376 {
21377 if (child->tag == DW_TAG_subprogram)
21378 {
73b9be8b 21379 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21380
7d45c7c3 21381 if (linkage_name != NULL)
abc72ce4 21382 {
43816ebc
TT
21383 gdb::unique_xmalloc_ptr<char> actual_name
21384 (language_class_name_from_physname (cu->language_defn,
21385 linkage_name));
21386 const char *name = NULL;
abc72ce4
DE
21387
21388 if (actual_name != NULL)
21389 {
15d034d0 21390 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21391
21392 if (die_name != NULL
43816ebc 21393 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21394 {
21395 /* Strip off the class name from the full name.
21396 We want the prefix. */
21397 int die_name_len = strlen (die_name);
43816ebc
TT
21398 int actual_name_len = strlen (actual_name.get ());
21399 const char *ptr = actual_name.get ();
abc72ce4
DE
21400
21401 /* Test for '::' as a sanity check. */
21402 if (actual_name_len > die_name_len + 2
43816ebc 21403 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21404 name = obstack_strndup (
e3b94546 21405 &objfile->per_bfd->storage_obstack,
43816ebc 21406 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21407 }
21408 }
abc72ce4
DE
21409 return name;
21410 }
21411 }
21412 }
21413
21414 return NULL;
21415}
21416
96408a79
SA
21417/* GCC might emit a nameless typedef that has a linkage name. Determine the
21418 prefix part in such case. See
21419 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21420
a121b7c1 21421static const char *
96408a79
SA
21422anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21423{
21424 struct attribute *attr;
e6a959d6 21425 const char *base;
96408a79
SA
21426
21427 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21428 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21429 return NULL;
21430
7d45c7c3 21431 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21432 return NULL;
21433
73b9be8b 21434 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21435 if (attr == NULL || DW_STRING (attr) == NULL)
21436 return NULL;
21437
21438 /* dwarf2_name had to be already called. */
21439 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21440
21441 /* Strip the base name, keep any leading namespaces/classes. */
21442 base = strrchr (DW_STRING (attr), ':');
21443 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21444 return "";
21445
518817b3 21446 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21447 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21448 DW_STRING (attr),
21449 &base[-1] - DW_STRING (attr));
96408a79
SA
21450}
21451
fdde2d81 21452/* Return the name of the namespace/class that DIE is defined within,
0114d602 21453 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21454
0114d602
DJ
21455 For example, if we're within the method foo() in the following
21456 code:
21457
21458 namespace N {
21459 class C {
21460 void foo () {
21461 }
21462 };
21463 }
21464
21465 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21466
0d5cff50 21467static const char *
e142c38c 21468determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21469{
518817b3
SM
21470 struct dwarf2_per_objfile *dwarf2_per_objfile
21471 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21472 struct die_info *parent, *spec_die;
21473 struct dwarf2_cu *spec_cu;
21474 struct type *parent_type;
a121b7c1 21475 const char *retval;
63d06c5c 21476
9c37b5ae 21477 if (cu->language != language_cplus
c44af4eb
TT
21478 && cu->language != language_fortran && cu->language != language_d
21479 && cu->language != language_rust)
0114d602
DJ
21480 return "";
21481
96408a79
SA
21482 retval = anonymous_struct_prefix (die, cu);
21483 if (retval)
21484 return retval;
21485
0114d602
DJ
21486 /* We have to be careful in the presence of DW_AT_specification.
21487 For example, with GCC 3.4, given the code
21488
21489 namespace N {
21490 void foo() {
21491 // Definition of N::foo.
21492 }
21493 }
21494
21495 then we'll have a tree of DIEs like this:
21496
21497 1: DW_TAG_compile_unit
21498 2: DW_TAG_namespace // N
21499 3: DW_TAG_subprogram // declaration of N::foo
21500 4: DW_TAG_subprogram // definition of N::foo
21501 DW_AT_specification // refers to die #3
21502
21503 Thus, when processing die #4, we have to pretend that we're in
21504 the context of its DW_AT_specification, namely the contex of die
21505 #3. */
21506 spec_cu = cu;
21507 spec_die = die_specification (die, &spec_cu);
21508 if (spec_die == NULL)
21509 parent = die->parent;
21510 else
63d06c5c 21511 {
0114d602
DJ
21512 parent = spec_die->parent;
21513 cu = spec_cu;
63d06c5c 21514 }
0114d602
DJ
21515
21516 if (parent == NULL)
21517 return "";
98bfdba5
PA
21518 else if (parent->building_fullname)
21519 {
21520 const char *name;
21521 const char *parent_name;
21522
21523 /* It has been seen on RealView 2.2 built binaries,
21524 DW_TAG_template_type_param types actually _defined_ as
21525 children of the parent class:
21526
21527 enum E {};
21528 template class <class Enum> Class{};
21529 Class<enum E> class_e;
21530
21531 1: DW_TAG_class_type (Class)
21532 2: DW_TAG_enumeration_type (E)
21533 3: DW_TAG_enumerator (enum1:0)
21534 3: DW_TAG_enumerator (enum2:1)
21535 ...
21536 2: DW_TAG_template_type_param
21537 DW_AT_type DW_FORM_ref_udata (E)
21538
21539 Besides being broken debug info, it can put GDB into an
21540 infinite loop. Consider:
21541
21542 When we're building the full name for Class<E>, we'll start
21543 at Class, and go look over its template type parameters,
21544 finding E. We'll then try to build the full name of E, and
21545 reach here. We're now trying to build the full name of E,
21546 and look over the parent DIE for containing scope. In the
21547 broken case, if we followed the parent DIE of E, we'd again
21548 find Class, and once again go look at its template type
21549 arguments, etc., etc. Simply don't consider such parent die
21550 as source-level parent of this die (it can't be, the language
21551 doesn't allow it), and break the loop here. */
21552 name = dwarf2_name (die, cu);
21553 parent_name = dwarf2_name (parent, cu);
b98664d3 21554 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21555 name ? name : "<unknown>",
21556 parent_name ? parent_name : "<unknown>");
21557 return "";
21558 }
63d06c5c 21559 else
0114d602
DJ
21560 switch (parent->tag)
21561 {
63d06c5c 21562 case DW_TAG_namespace:
0114d602 21563 parent_type = read_type_die (parent, cu);
acebe513
UW
21564 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21565 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21566 Work around this problem here. */
21567 if (cu->language == language_cplus
e86ca25f 21568 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21569 return "";
0114d602 21570 /* We give a name to even anonymous namespaces. */
e86ca25f 21571 return TYPE_NAME (parent_type);
63d06c5c 21572 case DW_TAG_class_type:
680b30c7 21573 case DW_TAG_interface_type:
63d06c5c 21574 case DW_TAG_structure_type:
0114d602 21575 case DW_TAG_union_type:
f55ee35c 21576 case DW_TAG_module:
0114d602 21577 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21578 if (TYPE_NAME (parent_type) != NULL)
21579 return TYPE_NAME (parent_type);
0114d602
DJ
21580 else
21581 /* An anonymous structure is only allowed non-static data
21582 members; no typedefs, no member functions, et cetera.
21583 So it does not need a prefix. */
21584 return "";
abc72ce4 21585 case DW_TAG_compile_unit:
95554aad 21586 case DW_TAG_partial_unit:
abc72ce4
DE
21587 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21588 if (cu->language == language_cplus
fd5866f6 21589 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21590 && die->child != NULL
21591 && (die->tag == DW_TAG_class_type
21592 || die->tag == DW_TAG_structure_type
21593 || die->tag == DW_TAG_union_type))
21594 {
43816ebc 21595 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21596 if (name != NULL)
21597 return name;
21598 }
21599 return "";
0a4b0913
AB
21600 case DW_TAG_subprogram:
21601 /* Nested subroutines in Fortran get a prefix with the name
21602 of the parent's subroutine. */
21603 if (cu->language == language_fortran)
21604 {
21605 if ((die->tag == DW_TAG_subprogram)
21606 && (dwarf2_name (parent, cu) != NULL))
21607 return dwarf2_name (parent, cu);
21608 }
21609 return determine_prefix (parent, cu);
3d567982
TT
21610 case DW_TAG_enumeration_type:
21611 parent_type = read_type_die (parent, cu);
21612 if (TYPE_DECLARED_CLASS (parent_type))
21613 {
e86ca25f
TT
21614 if (TYPE_NAME (parent_type) != NULL)
21615 return TYPE_NAME (parent_type);
3d567982
TT
21616 return "";
21617 }
21618 /* Fall through. */
63d06c5c 21619 default:
8176b9b8 21620 return determine_prefix (parent, cu);
63d06c5c 21621 }
63d06c5c
DC
21622}
21623
3e43a32a
MS
21624/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21625 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21626 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21627 an obconcat, otherwise allocate storage for the result. The CU argument is
21628 used to determine the language and hence, the appropriate separator. */
987504bb 21629
f55ee35c 21630#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21631
21632static char *
f55ee35c
JK
21633typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21634 int physname, struct dwarf2_cu *cu)
63d06c5c 21635{
f55ee35c 21636 const char *lead = "";
5c315b68 21637 const char *sep;
63d06c5c 21638
3e43a32a
MS
21639 if (suffix == NULL || suffix[0] == '\0'
21640 || prefix == NULL || prefix[0] == '\0')
987504bb 21641 sep = "";
45280282
IB
21642 else if (cu->language == language_d)
21643 {
21644 /* For D, the 'main' function could be defined in any module, but it
21645 should never be prefixed. */
21646 if (strcmp (suffix, "D main") == 0)
21647 {
21648 prefix = "";
21649 sep = "";
21650 }
21651 else
21652 sep = ".";
21653 }
f55ee35c
JK
21654 else if (cu->language == language_fortran && physname)
21655 {
21656 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21657 DW_AT_MIPS_linkage_name is preferred and used instead. */
21658
21659 lead = "__";
21660 sep = "_MOD_";
21661 }
987504bb
JJ
21662 else
21663 sep = "::";
63d06c5c 21664
6dd47d34
DE
21665 if (prefix == NULL)
21666 prefix = "";
21667 if (suffix == NULL)
21668 suffix = "";
21669
987504bb
JJ
21670 if (obs == NULL)
21671 {
3e43a32a 21672 char *retval
224c3ddb
SM
21673 = ((char *)
21674 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21675
f55ee35c
JK
21676 strcpy (retval, lead);
21677 strcat (retval, prefix);
6dd47d34
DE
21678 strcat (retval, sep);
21679 strcat (retval, suffix);
63d06c5c
DC
21680 return retval;
21681 }
987504bb
JJ
21682 else
21683 {
21684 /* We have an obstack. */
f55ee35c 21685 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21686 }
63d06c5c
DC
21687}
21688
c906108c
SS
21689/* Return sibling of die, NULL if no sibling. */
21690
f9aca02d 21691static struct die_info *
fba45db2 21692sibling_die (struct die_info *die)
c906108c 21693{
639d11d3 21694 return die->sibling;
c906108c
SS
21695}
21696
71c25dea
TT
21697/* Get name of a die, return NULL if not found. */
21698
15d034d0
TT
21699static const char *
21700dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
21701 struct obstack *obstack)
21702{
21703 if (name && cu->language == language_cplus)
21704 {
2f408ecb 21705 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21706
2f408ecb 21707 if (!canon_name.empty ())
71c25dea 21708 {
2f408ecb 21709 if (canon_name != name)
efba19b0 21710 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
21711 }
21712 }
21713
21714 return name;
c906108c
SS
21715}
21716
96553a0c
DE
21717/* Get name of a die, return NULL if not found.
21718 Anonymous namespaces are converted to their magic string. */
9219021c 21719
15d034d0 21720static const char *
e142c38c 21721dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21722{
21723 struct attribute *attr;
518817b3 21724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21725
e142c38c 21726 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21727 if ((!attr || !DW_STRING (attr))
96553a0c 21728 && die->tag != DW_TAG_namespace
53832f31
TT
21729 && die->tag != DW_TAG_class_type
21730 && die->tag != DW_TAG_interface_type
21731 && die->tag != DW_TAG_structure_type
21732 && die->tag != DW_TAG_union_type)
71c25dea
TT
21733 return NULL;
21734
21735 switch (die->tag)
21736 {
21737 case DW_TAG_compile_unit:
95554aad 21738 case DW_TAG_partial_unit:
71c25dea
TT
21739 /* Compilation units have a DW_AT_name that is a filename, not
21740 a source language identifier. */
21741 case DW_TAG_enumeration_type:
21742 case DW_TAG_enumerator:
21743 /* These tags always have simple identifiers already; no need
21744 to canonicalize them. */
21745 return DW_STRING (attr);
907af001 21746
96553a0c
DE
21747 case DW_TAG_namespace:
21748 if (attr != NULL && DW_STRING (attr) != NULL)
21749 return DW_STRING (attr);
21750 return CP_ANONYMOUS_NAMESPACE_STR;
21751
907af001
UW
21752 case DW_TAG_class_type:
21753 case DW_TAG_interface_type:
21754 case DW_TAG_structure_type:
21755 case DW_TAG_union_type:
21756 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21757 structures or unions. These were of the form "._%d" in GCC 4.1,
21758 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21759 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21760 if (attr && DW_STRING (attr)
61012eef
GB
21761 && (startswith (DW_STRING (attr), "._")
21762 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21763 return NULL;
53832f31
TT
21764
21765 /* GCC might emit a nameless typedef that has a linkage name. See
21766 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21767 if (!attr || DW_STRING (attr) == NULL)
21768 {
73b9be8b 21769 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21770 if (attr == NULL || DW_STRING (attr) == NULL)
21771 return NULL;
21772
df5c6c50
JK
21773 /* Avoid demangling DW_STRING (attr) the second time on a second
21774 call for the same DIE. */
21775 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21776 {
43816ebc
TT
21777 gdb::unique_xmalloc_ptr<char> demangled
21778 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21779
e6a959d6 21780 const char *base;
96408a79 21781
53832f31 21782 /* FIXME: we already did this for the partial symbol... */
34a68019 21783 DW_STRING (attr)
021887d8 21784 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 21785 demangled.get ());
53832f31 21786 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
21787
21788 /* Strip any leading namespaces/classes, keep only the base name.
21789 DW_AT_name for named DIEs does not contain the prefixes. */
21790 base = strrchr (DW_STRING (attr), ':');
21791 if (base && base > DW_STRING (attr) && base[-1] == ':')
21792 return &base[1];
21793 else
21794 return DW_STRING (attr);
53832f31
TT
21795 }
21796 }
907af001
UW
21797 break;
21798
71c25dea 21799 default:
907af001
UW
21800 break;
21801 }
21802
21803 if (!DW_STRING_IS_CANONICAL (attr))
21804 {
21805 DW_STRING (attr)
21806 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 21807 &objfile->per_bfd->storage_obstack);
907af001 21808 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21809 }
907af001 21810 return DW_STRING (attr);
9219021c
DC
21811}
21812
21813/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21814 is none. *EXT_CU is the CU containing DIE on input, and the CU
21815 containing the return value on output. */
9219021c
DC
21816
21817static struct die_info *
f2f0e013 21818dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21819{
21820 struct attribute *attr;
9219021c 21821
f2f0e013 21822 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21823 if (attr == NULL)
21824 return NULL;
21825
f2f0e013 21826 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21827}
21828
fa9c3fa0
TT
21829/* A convenience function that returns an "unknown" DWARF name,
21830 including the value of V. STR is the name of the entity being
21831 printed, e.g., "TAG". */
21832
21833static const char *
21834dwarf_unknown (const char *str, unsigned v)
21835{
21836 char *cell = get_print_cell ();
21837 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21838 return cell;
21839}
21840
c906108c
SS
21841/* Convert a DIE tag into its string name. */
21842
f39c6ffd 21843static const char *
aa1ee363 21844dwarf_tag_name (unsigned tag)
c906108c 21845{
f39c6ffd
TT
21846 const char *name = get_DW_TAG_name (tag);
21847
21848 if (name == NULL)
fa9c3fa0 21849 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
21850
21851 return name;
c906108c
SS
21852}
21853
21854/* Convert a DWARF attribute code into its string name. */
21855
f39c6ffd 21856static const char *
aa1ee363 21857dwarf_attr_name (unsigned attr)
c906108c 21858{
f39c6ffd
TT
21859 const char *name;
21860
c764a876 21861#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21862 if (attr == DW_AT_MIPS_fde)
21863 return "DW_AT_MIPS_fde";
21864#else
21865 if (attr == DW_AT_HP_block_index)
21866 return "DW_AT_HP_block_index";
c764a876 21867#endif
f39c6ffd
TT
21868
21869 name = get_DW_AT_name (attr);
21870
21871 if (name == NULL)
fa9c3fa0 21872 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
21873
21874 return name;
c906108c
SS
21875}
21876
21877/* Convert a DWARF value form code into its string name. */
21878
f39c6ffd 21879static const char *
aa1ee363 21880dwarf_form_name (unsigned form)
c906108c 21881{
f39c6ffd
TT
21882 const char *name = get_DW_FORM_name (form);
21883
21884 if (name == NULL)
fa9c3fa0 21885 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
21886
21887 return name;
c906108c
SS
21888}
21889
a121b7c1 21890static const char *
fba45db2 21891dwarf_bool_name (unsigned mybool)
c906108c
SS
21892{
21893 if (mybool)
21894 return "TRUE";
21895 else
21896 return "FALSE";
21897}
21898
21899/* Convert a DWARF type code into its string name. */
21900
f39c6ffd 21901static const char *
aa1ee363 21902dwarf_type_encoding_name (unsigned enc)
c906108c 21903{
f39c6ffd 21904 const char *name = get_DW_ATE_name (enc);
c906108c 21905
f39c6ffd 21906 if (name == NULL)
fa9c3fa0 21907 return dwarf_unknown ("ATE", enc);
c906108c 21908
f39c6ffd 21909 return name;
c906108c 21910}
c906108c 21911
f9aca02d 21912static void
d97bc12b 21913dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21914{
21915 unsigned int i;
21916
d97bc12b 21917 print_spaces (indent, f);
9d8780f0 21918 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21919 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21920 sect_offset_str (die->sect_off));
d97bc12b
DE
21921
21922 if (die->parent != NULL)
21923 {
21924 print_spaces (indent, f);
9d8780f0
SM
21925 fprintf_unfiltered (f, " parent at offset: %s\n",
21926 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21927 }
21928
21929 print_spaces (indent, f);
21930 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21931 dwarf_bool_name (die->child != NULL));
c906108c 21932
d97bc12b
DE
21933 print_spaces (indent, f);
21934 fprintf_unfiltered (f, " attributes:\n");
21935
c906108c
SS
21936 for (i = 0; i < die->num_attrs; ++i)
21937 {
d97bc12b
DE
21938 print_spaces (indent, f);
21939 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21940 dwarf_attr_name (die->attrs[i].name),
21941 dwarf_form_name (die->attrs[i].form));
d97bc12b 21942
c906108c
SS
21943 switch (die->attrs[i].form)
21944 {
c906108c 21945 case DW_FORM_addr:
336d760d 21946 case DW_FORM_addrx:
3019eac3 21947 case DW_FORM_GNU_addr_index:
d97bc12b 21948 fprintf_unfiltered (f, "address: ");
5af949e3 21949 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21950 break;
21951 case DW_FORM_block2:
21952 case DW_FORM_block4:
21953 case DW_FORM_block:
21954 case DW_FORM_block1:
56eb65bd
SP
21955 fprintf_unfiltered (f, "block: size %s",
21956 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21957 break;
2dc7f7b3 21958 case DW_FORM_exprloc:
56eb65bd
SP
21959 fprintf_unfiltered (f, "expression: size %s",
21960 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21961 break;
0224619f
JK
21962 case DW_FORM_data16:
21963 fprintf_unfiltered (f, "constant of 16 bytes");
21964 break;
4568ecf9
DE
21965 case DW_FORM_ref_addr:
21966 fprintf_unfiltered (f, "ref address: ");
21967 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21968 break;
36586728
TT
21969 case DW_FORM_GNU_ref_alt:
21970 fprintf_unfiltered (f, "alt ref address: ");
21971 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21972 break;
10b3939b
DJ
21973 case DW_FORM_ref1:
21974 case DW_FORM_ref2:
21975 case DW_FORM_ref4:
4568ecf9
DE
21976 case DW_FORM_ref8:
21977 case DW_FORM_ref_udata:
d97bc12b 21978 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21979 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21980 break;
c906108c
SS
21981 case DW_FORM_data1:
21982 case DW_FORM_data2:
21983 case DW_FORM_data4:
ce5d95e1 21984 case DW_FORM_data8:
c906108c
SS
21985 case DW_FORM_udata:
21986 case DW_FORM_sdata:
43bbcdc2
PH
21987 fprintf_unfiltered (f, "constant: %s",
21988 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21989 break;
2dc7f7b3
TT
21990 case DW_FORM_sec_offset:
21991 fprintf_unfiltered (f, "section offset: %s",
21992 pulongest (DW_UNSND (&die->attrs[i])));
21993 break;
55f1336d 21994 case DW_FORM_ref_sig8:
ac9ec31b
DE
21995 fprintf_unfiltered (f, "signature: %s",
21996 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21997 break;
c906108c 21998 case DW_FORM_string:
4bdf3d34 21999 case DW_FORM_strp:
43988095 22000 case DW_FORM_line_strp:
cf532bd1 22001 case DW_FORM_strx:
3019eac3 22002 case DW_FORM_GNU_str_index:
36586728 22003 case DW_FORM_GNU_strp_alt:
8285870a 22004 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22005 DW_STRING (&die->attrs[i])
8285870a
JK
22006 ? DW_STRING (&die->attrs[i]) : "",
22007 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22008 break;
22009 case DW_FORM_flag:
22010 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22011 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22012 else
d97bc12b 22013 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22014 break;
2dc7f7b3
TT
22015 case DW_FORM_flag_present:
22016 fprintf_unfiltered (f, "flag: TRUE");
22017 break;
a8329558 22018 case DW_FORM_indirect:
0963b4bd
MS
22019 /* The reader will have reduced the indirect form to
22020 the "base form" so this form should not occur. */
5f48f8f3 22021 fprintf_unfiltered (f,
3e43a32a 22022 "unexpected attribute form: DW_FORM_indirect");
a8329558 22023 break;
663c44ac
JK
22024 case DW_FORM_implicit_const:
22025 fprintf_unfiltered (f, "constant: %s",
22026 plongest (DW_SND (&die->attrs[i])));
22027 break;
c906108c 22028 default:
d97bc12b 22029 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22030 die->attrs[i].form);
d97bc12b 22031 break;
c906108c 22032 }
d97bc12b 22033 fprintf_unfiltered (f, "\n");
c906108c
SS
22034 }
22035}
22036
f9aca02d 22037static void
d97bc12b 22038dump_die_for_error (struct die_info *die)
c906108c 22039{
d97bc12b
DE
22040 dump_die_shallow (gdb_stderr, 0, die);
22041}
22042
22043static void
22044dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22045{
22046 int indent = level * 4;
22047
22048 gdb_assert (die != NULL);
22049
22050 if (level >= max_level)
22051 return;
22052
22053 dump_die_shallow (f, indent, die);
22054
22055 if (die->child != NULL)
c906108c 22056 {
d97bc12b
DE
22057 print_spaces (indent, f);
22058 fprintf_unfiltered (f, " Children:");
22059 if (level + 1 < max_level)
22060 {
22061 fprintf_unfiltered (f, "\n");
22062 dump_die_1 (f, level + 1, max_level, die->child);
22063 }
22064 else
22065 {
3e43a32a
MS
22066 fprintf_unfiltered (f,
22067 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22068 }
22069 }
22070
22071 if (die->sibling != NULL && level > 0)
22072 {
22073 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22074 }
22075}
22076
d97bc12b
DE
22077/* This is called from the pdie macro in gdbinit.in.
22078 It's not static so gcc will keep a copy callable from gdb. */
22079
22080void
22081dump_die (struct die_info *die, int max_level)
22082{
22083 dump_die_1 (gdb_stdlog, 0, max_level, die);
22084}
22085
f9aca02d 22086static void
51545339 22087store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22088{
51545339 22089 void **slot;
c906108c 22090
9c541725
PA
22091 slot = htab_find_slot_with_hash (cu->die_hash, die,
22092 to_underlying (die->sect_off),
b64f50a1 22093 INSERT);
51545339
DJ
22094
22095 *slot = die;
c906108c
SS
22096}
22097
b64f50a1
JK
22098/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22099 required kind. */
22100
22101static sect_offset
ff39bb5e 22102dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22103{
cd6c91b4 22104 if (attr->form_is_ref ())
9c541725 22105 return (sect_offset) DW_UNSND (attr);
93311388 22106
b98664d3 22107 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22108 dwarf_form_name (attr->form));
9c541725 22109 return {};
c906108c
SS
22110}
22111
43bbcdc2
PH
22112/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22113 * the value held by the attribute is not constant. */
a02abb62 22114
43bbcdc2 22115static LONGEST
ff39bb5e 22116dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22117{
663c44ac 22118 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22119 return DW_SND (attr);
22120 else if (attr->form == DW_FORM_udata
22121 || attr->form == DW_FORM_data1
22122 || attr->form == DW_FORM_data2
22123 || attr->form == DW_FORM_data4
22124 || attr->form == DW_FORM_data8)
22125 return DW_UNSND (attr);
22126 else
22127 {
cd6c91b4 22128 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22129 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22130 dwarf_form_name (attr->form));
22131 return default_value;
22132 }
22133}
22134
348e048f
DE
22135/* Follow reference or signature attribute ATTR of SRC_DIE.
22136 On entry *REF_CU is the CU of SRC_DIE.
22137 On exit *REF_CU is the CU of the result. */
22138
22139static struct die_info *
ff39bb5e 22140follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22141 struct dwarf2_cu **ref_cu)
22142{
22143 struct die_info *die;
22144
cd6c91b4 22145 if (attr->form_is_ref ())
348e048f 22146 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22147 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22148 die = follow_die_sig (src_die, attr, ref_cu);
22149 else
22150 {
22151 dump_die_for_error (src_die);
22152 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22153 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22154 }
22155
22156 return die;
03dd20cc
DJ
22157}
22158
5c631832 22159/* Follow reference OFFSET.
673bfd45
DE
22160 On entry *REF_CU is the CU of the source die referencing OFFSET.
22161 On exit *REF_CU is the CU of the result.
22162 Returns NULL if OFFSET is invalid. */
f504f079 22163
f9aca02d 22164static struct die_info *
9c541725 22165follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22166 struct dwarf2_cu **ref_cu)
c906108c 22167{
10b3939b 22168 struct die_info temp_die;
f2f0e013 22169 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22170 struct dwarf2_per_objfile *dwarf2_per_objfile
22171 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22172
348e048f
DE
22173 gdb_assert (cu->per_cu != NULL);
22174
98bfdba5
PA
22175 target_cu = cu;
22176
3019eac3 22177 if (cu->per_cu->is_debug_types)
348e048f
DE
22178 {
22179 /* .debug_types CUs cannot reference anything outside their CU.
22180 If they need to, they have to reference a signatured type via
55f1336d 22181 DW_FORM_ref_sig8. */
4057dfde 22182 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22183 return NULL;
348e048f 22184 }
36586728 22185 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22186 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22187 {
22188 struct dwarf2_per_cu_data *per_cu;
9a619af0 22189
9c541725 22190 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22191 dwarf2_per_objfile);
03dd20cc
DJ
22192
22193 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22194 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22195 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22196
10b3939b
DJ
22197 target_cu = per_cu->cu;
22198 }
98bfdba5
PA
22199 else if (cu->dies == NULL)
22200 {
22201 /* We're loading full DIEs during partial symbol reading. */
22202 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22203 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22204 }
c906108c 22205
f2f0e013 22206 *ref_cu = target_cu;
9c541725 22207 temp_die.sect_off = sect_off;
c24bdb02
KS
22208
22209 if (target_cu != cu)
22210 target_cu->ancestor = cu;
22211
9a3c8263 22212 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22213 &temp_die,
22214 to_underlying (sect_off));
5c631832 22215}
10b3939b 22216
5c631832
JK
22217/* Follow reference attribute ATTR of SRC_DIE.
22218 On entry *REF_CU is the CU of SRC_DIE.
22219 On exit *REF_CU is the CU of the result. */
22220
22221static struct die_info *
ff39bb5e 22222follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22223 struct dwarf2_cu **ref_cu)
22224{
9c541725 22225 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22226 struct dwarf2_cu *cu = *ref_cu;
22227 struct die_info *die;
22228
9c541725 22229 die = follow_die_offset (sect_off,
36586728
TT
22230 (attr->form == DW_FORM_GNU_ref_alt
22231 || cu->per_cu->is_dwz),
22232 ref_cu);
5c631832 22233 if (!die)
9d8780f0
SM
22234 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22235 "at %s [in module %s]"),
22236 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22237 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22238
5c631832
JK
22239 return die;
22240}
22241
9c541725 22242/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22243 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22244 dwarf2_locexpr_baton->data has lifetime of
22245 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22246
22247struct dwarf2_locexpr_baton
9c541725 22248dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22249 struct dwarf2_per_cu_data *per_cu,
22250 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22251 void *baton, bool resolve_abstract_p)
5c631832 22252{
918dd910 22253 struct dwarf2_cu *cu;
5c631832
JK
22254 struct die_info *die;
22255 struct attribute *attr;
22256 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22257 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22258 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22259
918dd910 22260 if (per_cu->cu == NULL)
58f0c718 22261 load_cu (per_cu, false);
918dd910 22262 cu = per_cu->cu;
cc12ce38
DE
22263 if (cu == NULL)
22264 {
22265 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22266 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22267 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22268 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22269 }
918dd910 22270
9c541725 22271 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22272 if (!die)
9d8780f0
SM
22273 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22274 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22275
22276 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22277 if (!attr && resolve_abstract_p
3360b6e7 22278 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22279 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22280 {
22281 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22282 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22283 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22284
3360b6e7
TV
22285 for (const auto &cand_off
22286 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22287 {
3360b6e7
TV
22288 struct dwarf2_cu *cand_cu = cu;
22289 struct die_info *cand
22290 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22291 if (!cand
22292 || !cand->parent
e4a62c65
TV
22293 || cand->parent->tag != DW_TAG_subprogram)
22294 continue;
22295
22296 CORE_ADDR pc_low, pc_high;
22297 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22298 if (pc_low == ((CORE_ADDR) -1))
22299 continue;
22300 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22301 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22302 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22303 continue;
22304
22305 die = cand;
22306 attr = dwarf2_attr (die, DW_AT_location, cu);
22307 break;
22308 }
22309 }
22310
5c631832
JK
22311 if (!attr)
22312 {
e103e986
JK
22313 /* DWARF: "If there is no such attribute, then there is no effect.".
22314 DATA is ignored if SIZE is 0. */
5c631832 22315
e103e986 22316 retval.data = NULL;
5c631832
JK
22317 retval.size = 0;
22318 }
cd6c91b4 22319 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22320 {
22321 struct dwarf2_loclist_baton loclist_baton;
22322 CORE_ADDR pc = (*get_frame_pc) (baton);
22323 size_t size;
22324
22325 fill_in_loclist_baton (cu, &loclist_baton, attr);
22326
22327 retval.data = dwarf2_find_location_expression (&loclist_baton,
22328 &size, pc);
22329 retval.size = size;
22330 }
5c631832
JK
22331 else
22332 {
4fc6c0d5 22333 if (!attr->form_is_block ())
9d8780f0 22334 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22335 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22336 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22337
22338 retval.data = DW_BLOCK (attr)->data;
22339 retval.size = DW_BLOCK (attr)->size;
22340 }
22341 retval.per_cu = cu->per_cu;
918dd910 22342
ed2dc618 22343 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22344
5c631832 22345 return retval;
348e048f
DE
22346}
22347
8b9737bf
TT
22348/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22349 offset. */
22350
22351struct dwarf2_locexpr_baton
22352dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22353 struct dwarf2_per_cu_data *per_cu,
22354 CORE_ADDR (*get_frame_pc) (void *baton),
22355 void *baton)
22356{
9c541725 22357 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22358
9c541725 22359 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22360}
22361
b6807d98
TT
22362/* Write a constant of a given type as target-ordered bytes into
22363 OBSTACK. */
22364
22365static const gdb_byte *
22366write_constant_as_bytes (struct obstack *obstack,
22367 enum bfd_endian byte_order,
22368 struct type *type,
22369 ULONGEST value,
22370 LONGEST *len)
22371{
22372 gdb_byte *result;
22373
22374 *len = TYPE_LENGTH (type);
224c3ddb 22375 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22376 store_unsigned_integer (result, *len, byte_order, value);
22377
22378 return result;
22379}
22380
22381/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22382 pointer to the constant bytes and set LEN to the length of the
22383 data. If memory is needed, allocate it on OBSTACK. If the DIE
22384 does not have a DW_AT_const_value, return NULL. */
22385
22386const gdb_byte *
9c541725 22387dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
22388 struct dwarf2_per_cu_data *per_cu,
22389 struct obstack *obstack,
22390 LONGEST *len)
22391{
22392 struct dwarf2_cu *cu;
22393 struct die_info *die;
22394 struct attribute *attr;
22395 const gdb_byte *result = NULL;
22396 struct type *type;
22397 LONGEST value;
22398 enum bfd_endian byte_order;
e3b94546 22399 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22400
b6807d98 22401 if (per_cu->cu == NULL)
58f0c718 22402 load_cu (per_cu, false);
b6807d98 22403 cu = per_cu->cu;
cc12ce38
DE
22404 if (cu == NULL)
22405 {
22406 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22407 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22408 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22409 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22410 }
b6807d98 22411
9c541725 22412 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22413 if (!die)
9d8780f0
SM
22414 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22415 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22416
22417 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22418 if (attr == NULL)
22419 return NULL;
22420
e3b94546 22421 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22422 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22423
22424 switch (attr->form)
22425 {
22426 case DW_FORM_addr:
336d760d 22427 case DW_FORM_addrx:
b6807d98
TT
22428 case DW_FORM_GNU_addr_index:
22429 {
22430 gdb_byte *tem;
22431
22432 *len = cu->header.addr_size;
224c3ddb 22433 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22434 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22435 result = tem;
22436 }
22437 break;
22438 case DW_FORM_string:
22439 case DW_FORM_strp:
cf532bd1 22440 case DW_FORM_strx:
b6807d98
TT
22441 case DW_FORM_GNU_str_index:
22442 case DW_FORM_GNU_strp_alt:
22443 /* DW_STRING is already allocated on the objfile obstack, point
22444 directly to it. */
22445 result = (const gdb_byte *) DW_STRING (attr);
22446 *len = strlen (DW_STRING (attr));
22447 break;
22448 case DW_FORM_block1:
22449 case DW_FORM_block2:
22450 case DW_FORM_block4:
22451 case DW_FORM_block:
22452 case DW_FORM_exprloc:
0224619f 22453 case DW_FORM_data16:
b6807d98
TT
22454 result = DW_BLOCK (attr)->data;
22455 *len = DW_BLOCK (attr)->size;
22456 break;
22457
22458 /* The DW_AT_const_value attributes are supposed to carry the
22459 symbol's value "represented as it would be on the target
22460 architecture." By the time we get here, it's already been
22461 converted to host endianness, so we just need to sign- or
22462 zero-extend it as appropriate. */
22463 case DW_FORM_data1:
22464 type = die_type (die, cu);
22465 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22466 if (result == NULL)
22467 result = write_constant_as_bytes (obstack, byte_order,
22468 type, value, len);
22469 break;
22470 case DW_FORM_data2:
22471 type = die_type (die, cu);
22472 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22473 if (result == NULL)
22474 result = write_constant_as_bytes (obstack, byte_order,
22475 type, value, len);
22476 break;
22477 case DW_FORM_data4:
22478 type = die_type (die, cu);
22479 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22480 if (result == NULL)
22481 result = write_constant_as_bytes (obstack, byte_order,
22482 type, value, len);
22483 break;
22484 case DW_FORM_data8:
22485 type = die_type (die, cu);
22486 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22487 if (result == NULL)
22488 result = write_constant_as_bytes (obstack, byte_order,
22489 type, value, len);
22490 break;
22491
22492 case DW_FORM_sdata:
663c44ac 22493 case DW_FORM_implicit_const:
b6807d98
TT
22494 type = die_type (die, cu);
22495 result = write_constant_as_bytes (obstack, byte_order,
22496 type, DW_SND (attr), len);
22497 break;
22498
22499 case DW_FORM_udata:
22500 type = die_type (die, cu);
22501 result = write_constant_as_bytes (obstack, byte_order,
22502 type, DW_UNSND (attr), len);
22503 break;
22504
22505 default:
b98664d3 22506 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22507 dwarf_form_name (attr->form));
22508 break;
22509 }
22510
22511 return result;
22512}
22513
7942e96e
AA
22514/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22515 valid type for this die is found. */
22516
22517struct type *
9c541725 22518dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
22519 struct dwarf2_per_cu_data *per_cu)
22520{
22521 struct dwarf2_cu *cu;
22522 struct die_info *die;
22523
7942e96e 22524 if (per_cu->cu == NULL)
58f0c718 22525 load_cu (per_cu, false);
7942e96e
AA
22526 cu = per_cu->cu;
22527 if (!cu)
22528 return NULL;
22529
9c541725 22530 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22531 if (!die)
22532 return NULL;
22533
22534 return die_type (die, cu);
22535}
22536
8a9b8146
TT
22537/* Return the type of the DIE at DIE_OFFSET in the CU named by
22538 PER_CU. */
22539
22540struct type *
b64f50a1 22541dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22542 struct dwarf2_per_cu_data *per_cu)
22543{
9c541725 22544 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22545 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22546}
22547
ac9ec31b 22548/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22549 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22550 On exit *REF_CU is the CU of the result.
22551 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22552
22553static struct die_info *
ac9ec31b
DE
22554follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22555 struct dwarf2_cu **ref_cu)
348e048f 22556{
348e048f 22557 struct die_info temp_die;
c24bdb02 22558 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22559 struct die_info *die;
22560
ac9ec31b
DE
22561 /* While it might be nice to assert sig_type->type == NULL here,
22562 we can get here for DW_AT_imported_declaration where we need
22563 the DIE not the type. */
348e048f
DE
22564
22565 /* If necessary, add it to the queue and load its DIEs. */
22566
95554aad 22567 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22568 read_signatured_type (sig_type);
348e048f 22569
348e048f 22570 sig_cu = sig_type->per_cu.cu;
69d751e3 22571 gdb_assert (sig_cu != NULL);
9c541725
PA
22572 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22573 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22574 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22575 to_underlying (temp_die.sect_off));
348e048f
DE
22576 if (die)
22577 {
ed2dc618 22578 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22579 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22580
796a7ff8
DE
22581 /* For .gdb_index version 7 keep track of included TUs.
22582 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22583 if (dwarf2_per_objfile->index_table != NULL
22584 && dwarf2_per_objfile->index_table->version <= 7)
22585 {
ae640021 22586 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22587 }
22588
348e048f 22589 *ref_cu = sig_cu;
c24bdb02
KS
22590 if (sig_cu != cu)
22591 sig_cu->ancestor = cu;
22592
348e048f
DE
22593 return die;
22594 }
22595
ac9ec31b
DE
22596 return NULL;
22597}
22598
22599/* Follow signatured type referenced by ATTR in SRC_DIE.
22600 On entry *REF_CU is the CU of SRC_DIE.
22601 On exit *REF_CU is the CU of the result.
22602 The result is the DIE of the type.
22603 If the referenced type cannot be found an error is thrown. */
22604
22605static struct die_info *
ff39bb5e 22606follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22607 struct dwarf2_cu **ref_cu)
22608{
22609 ULONGEST signature = DW_SIGNATURE (attr);
22610 struct signatured_type *sig_type;
22611 struct die_info *die;
22612
22613 gdb_assert (attr->form == DW_FORM_ref_sig8);
22614
a2ce51a0 22615 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22616 /* sig_type will be NULL if the signatured type is missing from
22617 the debug info. */
22618 if (sig_type == NULL)
22619 {
22620 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22621 " from DIE at %s [in module %s]"),
22622 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22623 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22624 }
22625
22626 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22627 if (die == NULL)
22628 {
22629 dump_die_for_error (src_die);
22630 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22631 " from DIE at %s [in module %s]"),
22632 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22633 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22634 }
22635
22636 return die;
22637}
22638
22639/* Get the type specified by SIGNATURE referenced in DIE/CU,
22640 reading in and processing the type unit if necessary. */
22641
22642static struct type *
22643get_signatured_type (struct die_info *die, ULONGEST signature,
22644 struct dwarf2_cu *cu)
22645{
518817b3
SM
22646 struct dwarf2_per_objfile *dwarf2_per_objfile
22647 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22648 struct signatured_type *sig_type;
22649 struct dwarf2_cu *type_cu;
22650 struct die_info *type_die;
22651 struct type *type;
22652
a2ce51a0 22653 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22654 /* sig_type will be NULL if the signatured type is missing from
22655 the debug info. */
22656 if (sig_type == NULL)
22657 {
b98664d3 22658 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22659 " from DIE at %s [in module %s]"),
22660 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22661 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22662 return build_error_marker_type (cu, die);
22663 }
22664
22665 /* If we already know the type we're done. */
22666 if (sig_type->type != NULL)
22667 return sig_type->type;
22668
22669 type_cu = cu;
22670 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22671 if (type_die != NULL)
22672 {
22673 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22674 is created. This is important, for example, because for c++ classes
22675 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22676 type = read_type_die (type_die, type_cu);
22677 if (type == NULL)
22678 {
b98664d3 22679 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22680 " referenced from DIE at %s [in module %s]"),
22681 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22682 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22683 type = build_error_marker_type (cu, die);
22684 }
22685 }
22686 else
22687 {
b98664d3 22688 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22689 " from DIE at %s [in module %s]"),
22690 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22691 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22692 type = build_error_marker_type (cu, die);
22693 }
22694 sig_type->type = type;
22695
22696 return type;
22697}
22698
22699/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22700 reading in and processing the type unit if necessary. */
22701
22702static struct type *
ff39bb5e 22703get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22704 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22705{
22706 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22707 if (attr->form_is_ref ())
ac9ec31b
DE
22708 {
22709 struct dwarf2_cu *type_cu = cu;
22710 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22711
22712 return read_type_die (type_die, type_cu);
22713 }
22714 else if (attr->form == DW_FORM_ref_sig8)
22715 {
22716 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22717 }
22718 else
22719 {
518817b3
SM
22720 struct dwarf2_per_objfile *dwarf2_per_objfile
22721 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22722
b98664d3 22723 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22724 " at %s [in module %s]"),
22725 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22726 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22727 return build_error_marker_type (cu, die);
22728 }
348e048f
DE
22729}
22730
e5fe5e75 22731/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22732
22733static void
e5fe5e75 22734load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22735{
52dc124a 22736 struct signatured_type *sig_type;
348e048f 22737
f4dc4d17
DE
22738 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22739 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22740
6721b2ec
DE
22741 /* We have the per_cu, but we need the signatured_type.
22742 Fortunately this is an easy translation. */
22743 gdb_assert (per_cu->is_debug_types);
22744 sig_type = (struct signatured_type *) per_cu;
348e048f 22745
6721b2ec 22746 gdb_assert (per_cu->cu == NULL);
348e048f 22747
52dc124a 22748 read_signatured_type (sig_type);
348e048f 22749
6721b2ec 22750 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22751}
22752
3019eac3
DE
22753/* Read in a signatured type and build its CU and DIEs.
22754 If the type is a stub for the real type in a DWO file,
22755 read in the real type from the DWO file as well. */
dee91e82
DE
22756
22757static void
22758read_signatured_type (struct signatured_type *sig_type)
22759{
22760 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22761
3019eac3 22762 gdb_assert (per_cu->is_debug_types);
dee91e82 22763 gdb_assert (per_cu->cu == NULL);
348e048f 22764
6751ebae 22765 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22766
22767 if (!reader.dummy_p)
22768 {
22769 struct dwarf2_cu *cu = reader.cu;
22770 const gdb_byte *info_ptr = reader.info_ptr;
22771
22772 gdb_assert (cu->die_hash == NULL);
22773 cu->die_hash =
22774 htab_create_alloc_ex (cu->header.length / 12,
22775 die_hash,
22776 die_eq,
22777 NULL,
22778 &cu->comp_unit_obstack,
22779 hashtab_obstack_allocate,
22780 dummy_obstack_deallocate);
22781
3e225074 22782 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22783 reader.comp_unit_die->child
22784 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22785 reader.comp_unit_die);
22786 cu->dies = reader.comp_unit_die;
22787 /* comp_unit_die is not stored in die_hash, no need. */
22788
22789 /* We try not to read any attributes in this function, because
22790 not all CUs needed for references have been loaded yet, and
22791 symbol table processing isn't initialized. But we have to
22792 set the CU language, or we won't be able to build types
22793 correctly. Similarly, if we do not read the producer, we can
22794 not apply producer-specific interpretation. */
22795 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22796
22797 reader.keep ();
c0ab21c2
TT
22798 }
22799
7ee85ab1 22800 sig_type->per_cu.tu_read = 1;
c906108c
SS
22801}
22802
c906108c
SS
22803/* Decode simple location descriptions.
22804 Given a pointer to a dwarf block that defines a location, compute
22805 the location and return the value.
22806
4cecd739
DJ
22807 NOTE drow/2003-11-18: This function is called in two situations
22808 now: for the address of static or global variables (partial symbols
22809 only) and for offsets into structures which are expected to be
22810 (more or less) constant. The partial symbol case should go away,
22811 and only the constant case should remain. That will let this
22812 function complain more accurately. A few special modes are allowed
22813 without complaint for global variables (for instance, global
22814 register values and thread-local values).
c906108c
SS
22815
22816 A location description containing no operations indicates that the
4cecd739 22817 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22818 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22819 callers will only want a very basic result and this can become a
21ae7a4d
JK
22820 complaint.
22821
22822 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22823
22824static CORE_ADDR
e7c27a73 22825decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22826{
518817b3 22827 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22828 size_t i;
22829 size_t size = blk->size;
d521ce57 22830 const gdb_byte *data = blk->data;
21ae7a4d
JK
22831 CORE_ADDR stack[64];
22832 int stacki;
22833 unsigned int bytes_read, unsnd;
22834 gdb_byte op;
c906108c 22835
21ae7a4d
JK
22836 i = 0;
22837 stacki = 0;
22838 stack[stacki] = 0;
22839 stack[++stacki] = 0;
22840
22841 while (i < size)
22842 {
22843 op = data[i++];
22844 switch (op)
22845 {
22846 case DW_OP_lit0:
22847 case DW_OP_lit1:
22848 case DW_OP_lit2:
22849 case DW_OP_lit3:
22850 case DW_OP_lit4:
22851 case DW_OP_lit5:
22852 case DW_OP_lit6:
22853 case DW_OP_lit7:
22854 case DW_OP_lit8:
22855 case DW_OP_lit9:
22856 case DW_OP_lit10:
22857 case DW_OP_lit11:
22858 case DW_OP_lit12:
22859 case DW_OP_lit13:
22860 case DW_OP_lit14:
22861 case DW_OP_lit15:
22862 case DW_OP_lit16:
22863 case DW_OP_lit17:
22864 case DW_OP_lit18:
22865 case DW_OP_lit19:
22866 case DW_OP_lit20:
22867 case DW_OP_lit21:
22868 case DW_OP_lit22:
22869 case DW_OP_lit23:
22870 case DW_OP_lit24:
22871 case DW_OP_lit25:
22872 case DW_OP_lit26:
22873 case DW_OP_lit27:
22874 case DW_OP_lit28:
22875 case DW_OP_lit29:
22876 case DW_OP_lit30:
22877 case DW_OP_lit31:
22878 stack[++stacki] = op - DW_OP_lit0;
22879 break;
f1bea926 22880
21ae7a4d
JK
22881 case DW_OP_reg0:
22882 case DW_OP_reg1:
22883 case DW_OP_reg2:
22884 case DW_OP_reg3:
22885 case DW_OP_reg4:
22886 case DW_OP_reg5:
22887 case DW_OP_reg6:
22888 case DW_OP_reg7:
22889 case DW_OP_reg8:
22890 case DW_OP_reg9:
22891 case DW_OP_reg10:
22892 case DW_OP_reg11:
22893 case DW_OP_reg12:
22894 case DW_OP_reg13:
22895 case DW_OP_reg14:
22896 case DW_OP_reg15:
22897 case DW_OP_reg16:
22898 case DW_OP_reg17:
22899 case DW_OP_reg18:
22900 case DW_OP_reg19:
22901 case DW_OP_reg20:
22902 case DW_OP_reg21:
22903 case DW_OP_reg22:
22904 case DW_OP_reg23:
22905 case DW_OP_reg24:
22906 case DW_OP_reg25:
22907 case DW_OP_reg26:
22908 case DW_OP_reg27:
22909 case DW_OP_reg28:
22910 case DW_OP_reg29:
22911 case DW_OP_reg30:
22912 case DW_OP_reg31:
22913 stack[++stacki] = op - DW_OP_reg0;
22914 if (i < size)
22915 dwarf2_complex_location_expr_complaint ();
22916 break;
c906108c 22917
21ae7a4d
JK
22918 case DW_OP_regx:
22919 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22920 i += bytes_read;
22921 stack[++stacki] = unsnd;
22922 if (i < size)
22923 dwarf2_complex_location_expr_complaint ();
22924 break;
c906108c 22925
21ae7a4d 22926 case DW_OP_addr:
c8a7a66f
TT
22927 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22928 &bytes_read);
21ae7a4d
JK
22929 i += bytes_read;
22930 break;
d53d4ac5 22931
21ae7a4d
JK
22932 case DW_OP_const1u:
22933 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22934 i += 1;
22935 break;
22936
22937 case DW_OP_const1s:
22938 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22939 i += 1;
22940 break;
22941
22942 case DW_OP_const2u:
22943 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22944 i += 2;
22945 break;
22946
22947 case DW_OP_const2s:
22948 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22949 i += 2;
22950 break;
d53d4ac5 22951
21ae7a4d
JK
22952 case DW_OP_const4u:
22953 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22954 i += 4;
22955 break;
22956
22957 case DW_OP_const4s:
22958 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22959 i += 4;
22960 break;
22961
585861ea
JK
22962 case DW_OP_const8u:
22963 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22964 i += 8;
22965 break;
22966
21ae7a4d
JK
22967 case DW_OP_constu:
22968 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22969 &bytes_read);
22970 i += bytes_read;
22971 break;
22972
22973 case DW_OP_consts:
22974 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22975 i += bytes_read;
22976 break;
22977
22978 case DW_OP_dup:
22979 stack[stacki + 1] = stack[stacki];
22980 stacki++;
22981 break;
22982
22983 case DW_OP_plus:
22984 stack[stacki - 1] += stack[stacki];
22985 stacki--;
22986 break;
22987
22988 case DW_OP_plus_uconst:
22989 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22990 &bytes_read);
22991 i += bytes_read;
22992 break;
22993
22994 case DW_OP_minus:
22995 stack[stacki - 1] -= stack[stacki];
22996 stacki--;
22997 break;
22998
22999 case DW_OP_deref:
23000 /* If we're not the last op, then we definitely can't encode
23001 this using GDB's address_class enum. This is valid for partial
23002 global symbols, although the variable's address will be bogus
23003 in the psymtab. */
23004 if (i < size)
23005 dwarf2_complex_location_expr_complaint ();
23006 break;
23007
23008 case DW_OP_GNU_push_tls_address:
4aa4e28b 23009 case DW_OP_form_tls_address:
21ae7a4d
JK
23010 /* The top of the stack has the offset from the beginning
23011 of the thread control block at which the variable is located. */
23012 /* Nothing should follow this operator, so the top of stack would
23013 be returned. */
23014 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23015 address will be bogus in the psymtab. Make it always at least
23016 non-zero to not look as a variable garbage collected by linker
23017 which have DW_OP_addr 0. */
21ae7a4d
JK
23018 if (i < size)
23019 dwarf2_complex_location_expr_complaint ();
585861ea 23020 stack[stacki]++;
21ae7a4d
JK
23021 break;
23022
23023 case DW_OP_GNU_uninit:
23024 break;
23025
336d760d 23026 case DW_OP_addrx:
3019eac3 23027 case DW_OP_GNU_addr_index:
49f6c839 23028 case DW_OP_GNU_const_index:
3019eac3
DE
23029 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23030 &bytes_read);
23031 i += bytes_read;
23032 break;
23033
21ae7a4d
JK
23034 default:
23035 {
f39c6ffd 23036 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23037
23038 if (name)
b98664d3 23039 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23040 name);
23041 else
b98664d3 23042 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23043 op);
23044 }
23045
23046 return (stack[stacki]);
d53d4ac5 23047 }
3c6e0cb3 23048
21ae7a4d
JK
23049 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23050 outside of the allocated space. Also enforce minimum>0. */
23051 if (stacki >= ARRAY_SIZE (stack) - 1)
23052 {
b98664d3 23053 complaint (_("location description stack overflow"));
21ae7a4d
JK
23054 return 0;
23055 }
23056
23057 if (stacki <= 0)
23058 {
b98664d3 23059 complaint (_("location description stack underflow"));
21ae7a4d
JK
23060 return 0;
23061 }
23062 }
23063 return (stack[stacki]);
c906108c
SS
23064}
23065
23066/* memory allocation interface */
23067
c906108c 23068static struct dwarf_block *
7b5a2f43 23069dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23070{
8d749320 23071 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23072}
23073
c906108c 23074static struct die_info *
b60c80d6 23075dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23076{
23077 struct die_info *die;
b60c80d6
DJ
23078 size_t size = sizeof (struct die_info);
23079
23080 if (num_attrs > 1)
23081 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23082
b60c80d6 23083 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23084 memset (die, 0, sizeof (struct die_info));
23085 return (die);
23086}
2e276125
JB
23087
23088\f
23089/* Macro support. */
23090
2e276125 23091static struct macro_source_file *
804d2729
TT
23092macro_start_file (struct dwarf2_cu *cu,
23093 int file, int line,
2e276125 23094 struct macro_source_file *current_file,
43f3e411 23095 struct line_header *lh)
2e276125 23096{
233d95b5 23097 /* File name relative to the compilation directory of this source file. */
03075812 23098 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
2e276125 23099
2e276125 23100 if (! current_file)
abc9d0dc 23101 {
fc474241
DE
23102 /* Note: We don't create a macro table for this compilation unit
23103 at all until we actually get a filename. */
c24bdb02 23104 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 23105
abc9d0dc
TT
23106 /* If we have no current file, then this must be the start_file
23107 directive for the compilation unit's main source file. */
03075812 23108 current_file = macro_set_main (macro_table, file_name.get ());
fc474241 23109 macro_define_special (macro_table);
abc9d0dc 23110 }
2e276125 23111 else
03075812 23112 current_file = macro_include (current_file, line, file_name.get ());
6e70227d 23113
2e276125
JB
23114 return current_file;
23115}
23116
2e276125
JB
23117static const char *
23118consume_improper_spaces (const char *p, const char *body)
23119{
23120 if (*p == ' ')
23121 {
b98664d3 23122 complaint (_("macro definition contains spaces "
3e43a32a 23123 "in formal argument list:\n`%s'"),
4d3c2250 23124 body);
2e276125
JB
23125
23126 while (*p == ' ')
23127 p++;
23128 }
23129
23130 return p;
23131}
23132
23133
23134static void
23135parse_macro_definition (struct macro_source_file *file, int line,
23136 const char *body)
23137{
23138 const char *p;
23139
23140 /* The body string takes one of two forms. For object-like macro
23141 definitions, it should be:
23142
23143 <macro name> " " <definition>
23144
23145 For function-like macro definitions, it should be:
23146
23147 <macro name> "() " <definition>
23148 or
23149 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23150
23151 Spaces may appear only where explicitly indicated, and in the
23152 <definition>.
23153
23154 The Dwarf 2 spec says that an object-like macro's name is always
23155 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23156 the space when the macro's definition is the empty string.
2e276125
JB
23157
23158 The Dwarf 2 spec says that there should be no spaces between the
23159 formal arguments in a function-like macro's formal argument list,
23160 but versions of GCC around March 2002 include spaces after the
23161 commas. */
23162
23163
23164 /* Find the extent of the macro name. The macro name is terminated
23165 by either a space or null character (for an object-like macro) or
23166 an opening paren (for a function-like macro). */
23167 for (p = body; *p; p++)
23168 if (*p == ' ' || *p == '(')
23169 break;
23170
23171 if (*p == ' ' || *p == '\0')
23172 {
23173 /* It's an object-like macro. */
23174 int name_len = p - body;
456e800a 23175 std::string name (body, name_len);
2e276125
JB
23176 const char *replacement;
23177
23178 if (*p == ' ')
23179 replacement = body + name_len + 1;
23180 else
23181 {
4d3c2250 23182 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23183 replacement = body + name_len;
23184 }
6e70227d 23185
456e800a 23186 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23187 }
23188 else if (*p == '(')
23189 {
23190 /* It's a function-like macro. */
456e800a 23191 std::string name (body, p - body);
2e276125
JB
23192 int argc = 0;
23193 int argv_size = 1;
8d749320 23194 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23195
23196 p++;
23197
23198 p = consume_improper_spaces (p, body);
23199
23200 /* Parse the formal argument list. */
23201 while (*p && *p != ')')
23202 {
23203 /* Find the extent of the current argument name. */
23204 const char *arg_start = p;
23205
23206 while (*p && *p != ',' && *p != ')' && *p != ' ')
23207 p++;
23208
23209 if (! *p || p == arg_start)
4d3c2250 23210 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23211 else
23212 {
23213 /* Make sure argv has room for the new argument. */
23214 if (argc >= argv_size)
23215 {
23216 argv_size *= 2;
224c3ddb 23217 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23218 }
23219
3f8a7804 23220 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23221 }
23222
23223 p = consume_improper_spaces (p, body);
23224
23225 /* Consume the comma, if present. */
23226 if (*p == ',')
23227 {
23228 p++;
23229
23230 p = consume_improper_spaces (p, body);
23231 }
23232 }
23233
23234 if (*p == ')')
23235 {
23236 p++;
23237
23238 if (*p == ' ')
23239 /* Perfectly formed definition, no complaints. */
456e800a 23240 macro_define_function (file, line, name.c_str (),
6e70227d 23241 argc, (const char **) argv,
2e276125
JB
23242 p + 1);
23243 else if (*p == '\0')
23244 {
23245 /* Complain, but do define it. */
4d3c2250 23246 dwarf2_macro_malformed_definition_complaint (body);
456e800a 23247 macro_define_function (file, line, name.c_str (),
6e70227d 23248 argc, (const char **) argv,
2e276125
JB
23249 p);
23250 }
23251 else
23252 /* Just complain. */
4d3c2250 23253 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23254 }
23255 else
23256 /* Just complain. */
4d3c2250 23257 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23258
2e276125
JB
23259 {
23260 int i;
23261
23262 for (i = 0; i < argc; i++)
23263 xfree (argv[i]);
23264 }
23265 xfree (argv);
23266 }
23267 else
4d3c2250 23268 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23269}
23270
cf2c3c16
TT
23271/* Skip some bytes from BYTES according to the form given in FORM.
23272 Returns the new pointer. */
2e276125 23273
d521ce57
TT
23274static const gdb_byte *
23275skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23276 enum dwarf_form form,
23277 unsigned int offset_size,
23278 struct dwarf2_section_info *section)
2e276125 23279{
cf2c3c16 23280 unsigned int bytes_read;
2e276125 23281
cf2c3c16 23282 switch (form)
2e276125 23283 {
cf2c3c16
TT
23284 case DW_FORM_data1:
23285 case DW_FORM_flag:
23286 ++bytes;
23287 break;
23288
23289 case DW_FORM_data2:
23290 bytes += 2;
23291 break;
23292
23293 case DW_FORM_data4:
23294 bytes += 4;
23295 break;
23296
23297 case DW_FORM_data8:
23298 bytes += 8;
23299 break;
23300
0224619f
JK
23301 case DW_FORM_data16:
23302 bytes += 16;
23303 break;
23304
cf2c3c16
TT
23305 case DW_FORM_string:
23306 read_direct_string (abfd, bytes, &bytes_read);
23307 bytes += bytes_read;
23308 break;
23309
23310 case DW_FORM_sec_offset:
23311 case DW_FORM_strp:
36586728 23312 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23313 bytes += offset_size;
23314 break;
23315
23316 case DW_FORM_block:
23317 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23318 bytes += bytes_read;
23319 break;
23320
23321 case DW_FORM_block1:
23322 bytes += 1 + read_1_byte (abfd, bytes);
23323 break;
23324 case DW_FORM_block2:
23325 bytes += 2 + read_2_bytes (abfd, bytes);
23326 break;
23327 case DW_FORM_block4:
23328 bytes += 4 + read_4_bytes (abfd, bytes);
23329 break;
23330
336d760d 23331 case DW_FORM_addrx:
cf2c3c16 23332 case DW_FORM_sdata:
cf532bd1 23333 case DW_FORM_strx:
cf2c3c16 23334 case DW_FORM_udata:
3019eac3
DE
23335 case DW_FORM_GNU_addr_index:
23336 case DW_FORM_GNU_str_index:
d521ce57 23337 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23338 if (bytes == NULL)
23339 {
23340 dwarf2_section_buffer_overflow_complaint (section);
23341 return NULL;
23342 }
cf2c3c16
TT
23343 break;
23344
663c44ac
JK
23345 case DW_FORM_implicit_const:
23346 break;
23347
cf2c3c16
TT
23348 default:
23349 {
b98664d3 23350 complaint (_("invalid form 0x%x in `%s'"),
96b79293 23351 form, section->get_name ());
cf2c3c16
TT
23352 return NULL;
23353 }
2e276125
JB
23354 }
23355
cf2c3c16
TT
23356 return bytes;
23357}
757a13d0 23358
cf2c3c16
TT
23359/* A helper for dwarf_decode_macros that handles skipping an unknown
23360 opcode. Returns an updated pointer to the macro data buffer; or,
23361 on error, issues a complaint and returns NULL. */
757a13d0 23362
d521ce57 23363static const gdb_byte *
cf2c3c16 23364skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23365 const gdb_byte **opcode_definitions,
23366 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23367 bfd *abfd,
23368 unsigned int offset_size,
23369 struct dwarf2_section_info *section)
23370{
23371 unsigned int bytes_read, i;
23372 unsigned long arg;
d521ce57 23373 const gdb_byte *defn;
2e276125 23374
cf2c3c16 23375 if (opcode_definitions[opcode] == NULL)
2e276125 23376 {
b98664d3 23377 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23378 opcode);
23379 return NULL;
23380 }
2e276125 23381
cf2c3c16
TT
23382 defn = opcode_definitions[opcode];
23383 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23384 defn += bytes_read;
2e276125 23385
cf2c3c16
TT
23386 for (i = 0; i < arg; ++i)
23387 {
aead7601
SM
23388 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23389 (enum dwarf_form) defn[i], offset_size,
f664829e 23390 section);
cf2c3c16
TT
23391 if (mac_ptr == NULL)
23392 {
23393 /* skip_form_bytes already issued the complaint. */
23394 return NULL;
23395 }
23396 }
757a13d0 23397
cf2c3c16
TT
23398 return mac_ptr;
23399}
757a13d0 23400
cf2c3c16
TT
23401/* A helper function which parses the header of a macro section.
23402 If the macro section is the extended (for now called "GNU") type,
23403 then this updates *OFFSET_SIZE. Returns a pointer to just after
23404 the header, or issues a complaint and returns NULL on error. */
757a13d0 23405
d521ce57
TT
23406static const gdb_byte *
23407dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 23408 bfd *abfd,
d521ce57 23409 const gdb_byte *mac_ptr,
cf2c3c16
TT
23410 unsigned int *offset_size,
23411 int section_is_gnu)
23412{
23413 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 23414
cf2c3c16
TT
23415 if (section_is_gnu)
23416 {
23417 unsigned int version, flags;
757a13d0 23418
cf2c3c16 23419 version = read_2_bytes (abfd, mac_ptr);
0af92d60 23420 if (version != 4 && version != 5)
cf2c3c16 23421 {
b98664d3 23422 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
23423 version);
23424 return NULL;
23425 }
23426 mac_ptr += 2;
757a13d0 23427
cf2c3c16
TT
23428 flags = read_1_byte (abfd, mac_ptr);
23429 ++mac_ptr;
23430 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 23431
cf2c3c16
TT
23432 if ((flags & 2) != 0)
23433 /* We don't need the line table offset. */
23434 mac_ptr += *offset_size;
757a13d0 23435
cf2c3c16
TT
23436 /* Vendor opcode descriptions. */
23437 if ((flags & 4) != 0)
23438 {
23439 unsigned int i, count;
757a13d0 23440
cf2c3c16
TT
23441 count = read_1_byte (abfd, mac_ptr);
23442 ++mac_ptr;
23443 for (i = 0; i < count; ++i)
23444 {
23445 unsigned int opcode, bytes_read;
23446 unsigned long arg;
23447
23448 opcode = read_1_byte (abfd, mac_ptr);
23449 ++mac_ptr;
23450 opcode_definitions[opcode] = mac_ptr;
23451 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23452 mac_ptr += bytes_read;
23453 mac_ptr += arg;
23454 }
757a13d0 23455 }
cf2c3c16 23456 }
757a13d0 23457
cf2c3c16
TT
23458 return mac_ptr;
23459}
757a13d0 23460
cf2c3c16 23461/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 23462 including DW_MACRO_import. */
cf2c3c16
TT
23463
23464static void
804d2729 23465dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 23466 bfd *abfd,
d521ce57 23467 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 23468 struct macro_source_file *current_file,
43f3e411 23469 struct line_header *lh,
cf2c3c16 23470 struct dwarf2_section_info *section,
36586728 23471 int section_is_gnu, int section_is_dwz,
cf2c3c16 23472 unsigned int offset_size,
8fc3fc34 23473 htab_t include_hash)
cf2c3c16 23474{
804d2729
TT
23475 struct dwarf2_per_objfile *dwarf2_per_objfile
23476 = cu->per_cu->dwarf2_per_objfile;
4d663531 23477 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
23478 enum dwarf_macro_record_type macinfo_type;
23479 int at_commandline;
d521ce57 23480 const gdb_byte *opcode_definitions[256];
757a13d0 23481
cf2c3c16
TT
23482 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23483 &offset_size, section_is_gnu);
23484 if (mac_ptr == NULL)
23485 {
23486 /* We already issued a complaint. */
23487 return;
23488 }
757a13d0
JK
23489
23490 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23491 GDB is still reading the definitions from command line. First
23492 DW_MACINFO_start_file will need to be ignored as it was already executed
23493 to create CURRENT_FILE for the main source holding also the command line
23494 definitions. On first met DW_MACINFO_start_file this flag is reset to
23495 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23496
23497 at_commandline = 1;
23498
23499 do
23500 {
23501 /* Do we at least have room for a macinfo type byte? */
23502 if (mac_ptr >= mac_end)
23503 {
f664829e 23504 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
23505 break;
23506 }
23507
aead7601 23508 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
23509 mac_ptr++;
23510
cf2c3c16
TT
23511 /* Note that we rely on the fact that the corresponding GNU and
23512 DWARF constants are the same. */
132448f8
SM
23513 DIAGNOSTIC_PUSH
23514 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
23515 switch (macinfo_type)
23516 {
23517 /* A zero macinfo type indicates the end of the macro
23518 information. */
23519 case 0:
23520 break;
2e276125 23521
0af92d60
JK
23522 case DW_MACRO_define:
23523 case DW_MACRO_undef:
23524 case DW_MACRO_define_strp:
23525 case DW_MACRO_undef_strp:
23526 case DW_MACRO_define_sup:
23527 case DW_MACRO_undef_sup:
2e276125 23528 {
891d2f0b 23529 unsigned int bytes_read;
2e276125 23530 int line;
d521ce57 23531 const char *body;
cf2c3c16 23532 int is_define;
2e276125 23533
cf2c3c16
TT
23534 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23535 mac_ptr += bytes_read;
23536
0af92d60
JK
23537 if (macinfo_type == DW_MACRO_define
23538 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
23539 {
23540 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23541 mac_ptr += bytes_read;
23542 }
23543 else
23544 {
23545 LONGEST str_offset;
23546
24aa364d 23547 str_offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16 23548 mac_ptr += offset_size;
2e276125 23549
0af92d60
JK
23550 if (macinfo_type == DW_MACRO_define_sup
23551 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 23552 || section_is_dwz)
36586728 23553 {
ed2dc618
SM
23554 struct dwz_file *dwz
23555 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 23556
ed2dc618
SM
23557 body = read_indirect_string_from_dwz (objfile,
23558 dwz, str_offset);
36586728
TT
23559 }
23560 else
ed2dc618
SM
23561 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23562 abfd, str_offset);
cf2c3c16
TT
23563 }
23564
0af92d60
JK
23565 is_define = (macinfo_type == DW_MACRO_define
23566 || macinfo_type == DW_MACRO_define_strp
23567 || macinfo_type == DW_MACRO_define_sup);
2e276125 23568 if (! current_file)
757a13d0
JK
23569 {
23570 /* DWARF violation as no main source is present. */
b98664d3 23571 complaint (_("debug info with no main source gives macro %s "
757a13d0 23572 "on line %d: %s"),
cf2c3c16
TT
23573 is_define ? _("definition") : _("undefinition"),
23574 line, body);
757a13d0
JK
23575 break;
23576 }
3e43a32a
MS
23577 if ((line == 0 && !at_commandline)
23578 || (line != 0 && at_commandline))
b98664d3 23579 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 23580 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 23581 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
23582 line == 0 ? _("zero") : _("non-zero"), line, body);
23583
955b06fa 23584 if (body == NULL)
7bede828 23585 {
955b06fa
SDJ
23586 /* Fedora's rpm-build's "debugedit" binary
23587 corrupted .debug_macro sections.
23588
23589 For more info, see
23590 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23591 complaint (_("debug info gives %s invalid macro %s "
23592 "without body (corrupted?) at line %d "
23593 "on file %s"),
23594 at_commandline ? _("command-line") : _("in-file"),
23595 is_define ? _("definition") : _("undefinition"),
23596 line, current_file->filename);
7bede828 23597 }
955b06fa
SDJ
23598 else if (is_define)
23599 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
23600 else
23601 {
0af92d60
JK
23602 gdb_assert (macinfo_type == DW_MACRO_undef
23603 || macinfo_type == DW_MACRO_undef_strp
23604 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
23605 macro_undef (current_file, line, body);
23606 }
2e276125
JB
23607 }
23608 break;
23609
0af92d60 23610 case DW_MACRO_start_file:
2e276125 23611 {
891d2f0b 23612 unsigned int bytes_read;
2e276125
JB
23613 int line, file;
23614
23615 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23616 mac_ptr += bytes_read;
23617 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23618 mac_ptr += bytes_read;
23619
3e43a32a
MS
23620 if ((line == 0 && !at_commandline)
23621 || (line != 0 && at_commandline))
b98664d3 23622 complaint (_("debug info gives source %d included "
757a13d0
JK
23623 "from %s at %s line %d"),
23624 file, at_commandline ? _("command-line") : _("file"),
23625 line == 0 ? _("zero") : _("non-zero"), line);
23626
23627 if (at_commandline)
23628 {
0af92d60 23629 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23630 pass one. */
757a13d0
JK
23631 at_commandline = 0;
23632 }
23633 else
804d2729
TT
23634 current_file = macro_start_file (cu, file, line, current_file,
23635 lh);
2e276125
JB
23636 }
23637 break;
23638
0af92d60 23639 case DW_MACRO_end_file:
2e276125 23640 if (! current_file)
b98664d3 23641 complaint (_("macro debug info has an unmatched "
3e43a32a 23642 "`close_file' directive"));
2e276125
JB
23643 else
23644 {
23645 current_file = current_file->included_by;
23646 if (! current_file)
23647 {
cf2c3c16 23648 enum dwarf_macro_record_type next_type;
2e276125
JB
23649
23650 /* GCC circa March 2002 doesn't produce the zero
23651 type byte marking the end of the compilation
23652 unit. Complain if it's not there, but exit no
23653 matter what. */
23654
23655 /* Do we at least have room for a macinfo type byte? */
23656 if (mac_ptr >= mac_end)
23657 {
f664829e 23658 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23659 return;
23660 }
23661
23662 /* We don't increment mac_ptr here, so this is just
23663 a look-ahead. */
aead7601
SM
23664 next_type
23665 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23666 mac_ptr);
2e276125 23667 if (next_type != 0)
b98664d3 23668 complaint (_("no terminating 0-type entry for "
3e43a32a 23669 "macros in `.debug_macinfo' section"));
2e276125
JB
23670
23671 return;
23672 }
23673 }
23674 break;
23675
0af92d60
JK
23676 case DW_MACRO_import:
23677 case DW_MACRO_import_sup:
cf2c3c16
TT
23678 {
23679 LONGEST offset;
8fc3fc34 23680 void **slot;
a036ba48
TT
23681 bfd *include_bfd = abfd;
23682 struct dwarf2_section_info *include_section = section;
d521ce57 23683 const gdb_byte *include_mac_end = mac_end;
a036ba48 23684 int is_dwz = section_is_dwz;
d521ce57 23685 const gdb_byte *new_mac_ptr;
cf2c3c16 23686
24aa364d 23687 offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16
TT
23688 mac_ptr += offset_size;
23689
0af92d60 23690 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 23691 {
ed2dc618 23692 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 23693
96b79293 23694 dwz->macro.read (objfile);
a036ba48 23695
a036ba48 23696 include_section = &dwz->macro;
96b79293 23697 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
23698 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23699 is_dwz = 1;
23700 }
23701
23702 new_mac_ptr = include_section->buffer + offset;
23703 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23704
8fc3fc34
TT
23705 if (*slot != NULL)
23706 {
23707 /* This has actually happened; see
23708 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 23709 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
23710 ".debug_macro section"));
23711 }
23712 else
23713 {
d521ce57 23714 *slot = (void *) new_mac_ptr;
36586728 23715
804d2729 23716 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 23717 include_mac_end, current_file, lh,
36586728 23718 section, section_is_gnu, is_dwz,
4d663531 23719 offset_size, include_hash);
8fc3fc34 23720
d521ce57 23721 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23722 }
cf2c3c16
TT
23723 }
23724 break;
23725
2e276125 23726 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23727 if (!section_is_gnu)
23728 {
23729 unsigned int bytes_read;
2e276125 23730
ac298888
TT
23731 /* This reads the constant, but since we don't recognize
23732 any vendor extensions, we ignore it. */
23733 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23734 mac_ptr += bytes_read;
23735 read_direct_string (abfd, mac_ptr, &bytes_read);
23736 mac_ptr += bytes_read;
2e276125 23737
cf2c3c16
TT
23738 /* We don't recognize any vendor extensions. */
23739 break;
23740 }
23741 /* FALLTHROUGH */
23742
23743 default:
23744 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23745 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23746 section);
23747 if (mac_ptr == NULL)
23748 return;
23749 break;
2e276125 23750 }
132448f8 23751 DIAGNOSTIC_POP
757a13d0 23752 } while (macinfo_type != 0);
2e276125 23753}
8e19ed76 23754
cf2c3c16 23755static void
09262596 23756dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 23757 int section_is_gnu)
cf2c3c16 23758{
518817b3
SM
23759 struct dwarf2_per_objfile *dwarf2_per_objfile
23760 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23761 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
23762 struct line_header *lh = cu->line_header;
23763 bfd *abfd;
d521ce57 23764 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23765 struct macro_source_file *current_file = 0;
23766 enum dwarf_macro_record_type macinfo_type;
23767 unsigned int offset_size = cu->header.offset_size;
d521ce57 23768 const gdb_byte *opcode_definitions[256];
8fc3fc34 23769 void **slot;
09262596
DE
23770 struct dwarf2_section_info *section;
23771 const char *section_name;
23772
23773 if (cu->dwo_unit != NULL)
23774 {
23775 if (section_is_gnu)
23776 {
23777 section = &cu->dwo_unit->dwo_file->sections.macro;
23778 section_name = ".debug_macro.dwo";
23779 }
23780 else
23781 {
23782 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23783 section_name = ".debug_macinfo.dwo";
23784 }
23785 }
23786 else
23787 {
23788 if (section_is_gnu)
23789 {
23790 section = &dwarf2_per_objfile->macro;
23791 section_name = ".debug_macro";
23792 }
23793 else
23794 {
23795 section = &dwarf2_per_objfile->macinfo;
23796 section_name = ".debug_macinfo";
23797 }
23798 }
cf2c3c16 23799
96b79293 23800 section->read (objfile);
cf2c3c16
TT
23801 if (section->buffer == NULL)
23802 {
b98664d3 23803 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
23804 return;
23805 }
96b79293 23806 abfd = section->get_bfd_owner ();
cf2c3c16
TT
23807
23808 /* First pass: Find the name of the base filename.
23809 This filename is needed in order to process all macros whose definition
23810 (or undefinition) comes from the command line. These macros are defined
23811 before the first DW_MACINFO_start_file entry, and yet still need to be
23812 associated to the base file.
23813
23814 To determine the base file name, we scan the macro definitions until we
23815 reach the first DW_MACINFO_start_file entry. We then initialize
23816 CURRENT_FILE accordingly so that any macro definition found before the
23817 first DW_MACINFO_start_file can still be associated to the base file. */
23818
23819 mac_ptr = section->buffer + offset;
23820 mac_end = section->buffer + section->size;
23821
23822 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23823 &offset_size, section_is_gnu);
23824 if (mac_ptr == NULL)
23825 {
23826 /* We already issued a complaint. */
23827 return;
23828 }
23829
23830 do
23831 {
23832 /* Do we at least have room for a macinfo type byte? */
23833 if (mac_ptr >= mac_end)
23834 {
23835 /* Complaint is printed during the second pass as GDB will probably
23836 stop the first pass earlier upon finding
23837 DW_MACINFO_start_file. */
23838 break;
23839 }
23840
aead7601 23841 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23842 mac_ptr++;
23843
23844 /* Note that we rely on the fact that the corresponding GNU and
23845 DWARF constants are the same. */
132448f8
SM
23846 DIAGNOSTIC_PUSH
23847 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
23848 switch (macinfo_type)
23849 {
23850 /* A zero macinfo type indicates the end of the macro
23851 information. */
23852 case 0:
23853 break;
23854
0af92d60
JK
23855 case DW_MACRO_define:
23856 case DW_MACRO_undef:
cf2c3c16
TT
23857 /* Only skip the data by MAC_PTR. */
23858 {
23859 unsigned int bytes_read;
23860
23861 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23862 mac_ptr += bytes_read;
23863 read_direct_string (abfd, mac_ptr, &bytes_read);
23864 mac_ptr += bytes_read;
23865 }
23866 break;
23867
0af92d60 23868 case DW_MACRO_start_file:
cf2c3c16
TT
23869 {
23870 unsigned int bytes_read;
23871 int line, file;
23872
23873 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23874 mac_ptr += bytes_read;
23875 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23876 mac_ptr += bytes_read;
23877
804d2729 23878 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
23879 }
23880 break;
23881
0af92d60 23882 case DW_MACRO_end_file:
cf2c3c16
TT
23883 /* No data to skip by MAC_PTR. */
23884 break;
23885
0af92d60
JK
23886 case DW_MACRO_define_strp:
23887 case DW_MACRO_undef_strp:
23888 case DW_MACRO_define_sup:
23889 case DW_MACRO_undef_sup:
cf2c3c16
TT
23890 {
23891 unsigned int bytes_read;
23892
23893 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23894 mac_ptr += bytes_read;
23895 mac_ptr += offset_size;
23896 }
23897 break;
23898
0af92d60
JK
23899 case DW_MACRO_import:
23900 case DW_MACRO_import_sup:
cf2c3c16 23901 /* Note that, according to the spec, a transparent include
0af92d60 23902 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23903 skip this opcode. */
23904 mac_ptr += offset_size;
23905 break;
23906
23907 case DW_MACINFO_vendor_ext:
23908 /* Only skip the data by MAC_PTR. */
23909 if (!section_is_gnu)
23910 {
23911 unsigned int bytes_read;
23912
23913 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23914 mac_ptr += bytes_read;
23915 read_direct_string (abfd, mac_ptr, &bytes_read);
23916 mac_ptr += bytes_read;
23917 }
23918 /* FALLTHROUGH */
23919
23920 default:
23921 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23922 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23923 section);
23924 if (mac_ptr == NULL)
23925 return;
23926 break;
23927 }
132448f8 23928 DIAGNOSTIC_POP
cf2c3c16
TT
23929 } while (macinfo_type != 0 && current_file == NULL);
23930
23931 /* Second pass: Process all entries.
23932
23933 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23934 command-line macro definitions/undefinitions. This flag is unset when we
23935 reach the first DW_MACINFO_start_file entry. */
23936
fc4007c9
TT
23937 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23938 htab_eq_pointer,
23939 NULL, xcalloc, xfree));
8fc3fc34 23940 mac_ptr = section->buffer + offset;
fc4007c9 23941 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23942 *slot = (void *) mac_ptr;
804d2729 23943 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 23944 current_file, lh, section,
fc4007c9
TT
23945 section_is_gnu, 0, offset_size,
23946 include_hash.get ());
cf2c3c16
TT
23947}
23948
3019eac3
DE
23949/* Return the .debug_loc section to use for CU.
23950 For DWO files use .debug_loc.dwo. */
23951
23952static struct dwarf2_section_info *
23953cu_debug_loc_section (struct dwarf2_cu *cu)
23954{
518817b3
SM
23955 struct dwarf2_per_objfile *dwarf2_per_objfile
23956 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23957
3019eac3 23958 if (cu->dwo_unit)
43988095
JK
23959 {
23960 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23961
43988095
JK
23962 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23963 }
23964 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23965 : &dwarf2_per_objfile->loc);
3019eac3
DE
23966}
23967
8cf6f0b1
TT
23968/* A helper function that fills in a dwarf2_loclist_baton. */
23969
23970static void
23971fill_in_loclist_baton (struct dwarf2_cu *cu,
23972 struct dwarf2_loclist_baton *baton,
ff39bb5e 23973 const struct attribute *attr)
8cf6f0b1 23974{
518817b3
SM
23975 struct dwarf2_per_objfile *dwarf2_per_objfile
23976 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23977 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23978
96b79293 23979 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23980
23981 baton->per_cu = cu->per_cu;
23982 gdb_assert (baton->per_cu);
23983 /* We don't know how long the location list is, but make sure we
23984 don't run off the edge of the section. */
3019eac3
DE
23985 baton->size = section->size - DW_UNSND (attr);
23986 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23987 baton->base_address = cu->base_address;
f664829e 23988 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23989}
23990
4c2df51b 23991static void
ff39bb5e 23992dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23993 struct dwarf2_cu *cu, int is_block)
4c2df51b 23994{
518817b3
SM
23995 struct dwarf2_per_objfile *dwarf2_per_objfile
23996 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23997 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23998 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23999
cd6c91b4 24000 if (attr->form_is_section_offset ()
3019eac3 24001 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24002 the section. If so, fall through to the complaint in the
24003 other branch. */
2c7d5afc 24004 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 24005 {
0d53c4c4 24006 struct dwarf2_loclist_baton *baton;
4c2df51b 24007
8d749320 24008 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24009
8cf6f0b1 24010 fill_in_loclist_baton (cu, baton, attr);
be391dca 24011
d00adf39 24012 if (cu->base_known == 0)
b98664d3 24013 complaint (_("Location list used without "
3e43a32a 24014 "specifying the CU base address."));
4c2df51b 24015
f1e6e072
TT
24016 SYMBOL_ACLASS_INDEX (sym) = (is_block
24017 ? dwarf2_loclist_block_index
24018 : dwarf2_loclist_index);
0d53c4c4
DJ
24019 SYMBOL_LOCATION_BATON (sym) = baton;
24020 }
24021 else
24022 {
24023 struct dwarf2_locexpr_baton *baton;
24024
8d749320 24025 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24026 baton->per_cu = cu->per_cu;
24027 gdb_assert (baton->per_cu);
0d53c4c4 24028
4fc6c0d5 24029 if (attr->form_is_block ())
0d53c4c4
DJ
24030 {
24031 /* Note that we're just copying the block's data pointer
24032 here, not the actual data. We're still pointing into the
6502dd73
DJ
24033 info_buffer for SYM's objfile; right now we never release
24034 that buffer, but when we do clean up properly this may
24035 need to change. */
0d53c4c4
DJ
24036 baton->size = DW_BLOCK (attr)->size;
24037 baton->data = DW_BLOCK (attr)->data;
24038 }
24039 else
24040 {
24041 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24042 sym->natural_name ());
0d53c4c4 24043 baton->size = 0;
0d53c4c4 24044 }
6e70227d 24045
f1e6e072
TT
24046 SYMBOL_ACLASS_INDEX (sym) = (is_block
24047 ? dwarf2_locexpr_block_index
24048 : dwarf2_locexpr_index);
0d53c4c4
DJ
24049 SYMBOL_LOCATION_BATON (sym) = baton;
24050 }
4c2df51b 24051}
6502dd73 24052
09ba997f 24053/* See read.h. */
ae0d2f24
UW
24054
24055struct objfile *
09ba997f 24056dwarf2_per_cu_data::objfile () const
ae0d2f24 24057{
09ba997f 24058 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
24059
24060 /* Return the master objfile, so that we can report and look up the
24061 correct file containing this variable. */
24062 if (objfile->separate_debug_objfile_backlink)
24063 objfile = objfile->separate_debug_objfile_backlink;
24064
24065 return objfile;
24066}
24067
96408a79
SA
24068/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24069 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24070 CU_HEADERP first. */
24071
24072static const struct comp_unit_head *
24073per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 24074 const struct dwarf2_per_cu_data *per_cu)
96408a79 24075{
d521ce57 24076 const gdb_byte *info_ptr;
96408a79
SA
24077
24078 if (per_cu->cu)
24079 return &per_cu->cu->header;
24080
9c541725 24081 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24082
24083 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24084 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24085 rcuh_kind::COMPILE);
96408a79
SA
24086
24087 return cu_headerp;
24088}
24089
09ba997f 24090/* See read.h. */
ae0d2f24 24091
98714339 24092int
09ba997f 24093dwarf2_per_cu_data::addr_size () const
ae0d2f24 24094{
96408a79
SA
24095 struct comp_unit_head cu_header_local;
24096 const struct comp_unit_head *cu_headerp;
c471e790 24097
09ba997f 24098 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24099
24100 return cu_headerp->addr_size;
ae0d2f24
UW
24101}
24102
09ba997f 24103/* See read.h. */
9eae7c52
TT
24104
24105int
09ba997f 24106dwarf2_per_cu_data::offset_size () const
9eae7c52 24107{
96408a79
SA
24108 struct comp_unit_head cu_header_local;
24109 const struct comp_unit_head *cu_headerp;
9c6c53f7 24110
09ba997f 24111 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24112
24113 return cu_headerp->offset_size;
24114}
24115
09ba997f 24116/* See read.h. */
96408a79
SA
24117
24118int
09ba997f 24119dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
24120{
24121 struct comp_unit_head cu_header_local;
24122 const struct comp_unit_head *cu_headerp;
24123
09ba997f 24124 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24125
24126 if (cu_headerp->version == 2)
24127 return cu_headerp->addr_size;
24128 else
24129 return cu_headerp->offset_size;
181cebd4
JK
24130}
24131
09ba997f 24132/* See read.h. */
9aa1f1e3
TT
24133
24134CORE_ADDR
09ba997f 24135dwarf2_per_cu_data::text_offset () const
9aa1f1e3 24136{
09ba997f
TT
24137 struct objfile *objfile = dwarf2_per_objfile->objfile;
24138
24139 return objfile->text_section_offset ();
9aa1f1e3
TT
24140}
24141
09ba997f
TT
24142/* See read.h. */
24143
24144struct type *
24145dwarf2_per_cu_data::addr_type () const
9a49df9d 24146{
09ba997f 24147 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
24148 struct type *void_type = objfile_type (objfile)->builtin_void;
24149 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 24150 int addr_size = this->addr_size ();
9a49df9d
AB
24151
24152 if (TYPE_LENGTH (addr_type) == addr_size)
24153 return addr_type;
24154
09ba997f 24155 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
24156 return addr_type;
24157}
24158
348e048f
DE
24159/* Locate the .debug_info compilation unit from CU's objfile which contains
24160 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24161
24162static struct dwarf2_per_cu_data *
9c541725 24163dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24164 unsigned int offset_in_dwz,
ed2dc618 24165 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24166{
24167 struct dwarf2_per_cu_data *this_cu;
24168 int low, high;
24169
ae038cb0 24170 low = 0;
b76e467d 24171 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24172 while (high > low)
24173 {
36586728 24174 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24175 int mid = low + (high - low) / 2;
9a619af0 24176
36586728 24177 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 24178 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24179 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 24180 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
24181 high = mid;
24182 else
24183 low = mid + 1;
24184 }
24185 gdb_assert (low == high);
36586728 24186 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 24187 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24188 {
36586728 24189 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24190 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24191 "offset %s [in module %s]"),
24192 sect_offset_str (sect_off),
ed2dc618 24193 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24194
9c541725
PA
24195 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24196 <= sect_off);
ae038cb0
DJ
24197 return dwarf2_per_objfile->all_comp_units[low-1];
24198 }
24199 else
24200 {
b76e467d 24201 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24202 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24203 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24204 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24205 return this_cu;
24206 }
24207}
24208
23745b47 24209/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24210
fcd3b13d
SM
24211dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24212 : per_cu (per_cu_),
9068261f
AB
24213 mark (false),
24214 has_loclist (false),
24215 checked_producer (false),
24216 producer_is_gxx_lt_4_6 (false),
24217 producer_is_gcc_lt_4_3 (false),
eb77c9df 24218 producer_is_icc (false),
9068261f 24219 producer_is_icc_lt_14 (false),
c258c396 24220 producer_is_codewarrior (false),
9068261f 24221 processing_has_namespace_info (false)
93311388 24222{
fcd3b13d
SM
24223 per_cu->cu = this;
24224}
24225
24226/* Destroy a dwarf2_cu. */
24227
24228dwarf2_cu::~dwarf2_cu ()
24229{
24230 per_cu->cu = NULL;
9816fde3
JK
24231}
24232
24233/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24234
24235static void
95554aad
TT
24236prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24237 enum language pretend_language)
9816fde3
JK
24238{
24239 struct attribute *attr;
24240
24241 /* Set the language we're debugging. */
24242 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24243 if (attr != nullptr)
9816fde3
JK
24244 set_cu_language (DW_UNSND (attr), cu);
24245 else
9cded63f 24246 {
95554aad 24247 cu->language = pretend_language;
9cded63f
TT
24248 cu->language_defn = language_def (cu->language);
24249 }
dee91e82 24250
7d45c7c3 24251 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24252}
24253
ae038cb0
DJ
24254/* Increase the age counter on each cached compilation unit, and free
24255 any that are too old. */
24256
24257static void
ed2dc618 24258age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24259{
24260 struct dwarf2_per_cu_data *per_cu, **last_chain;
24261
24262 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24263 per_cu = dwarf2_per_objfile->read_in_chain;
24264 while (per_cu != NULL)
24265 {
24266 per_cu->cu->last_used ++;
b4f54984 24267 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24268 dwarf2_mark (per_cu->cu);
24269 per_cu = per_cu->cu->read_in_chain;
24270 }
24271
24272 per_cu = dwarf2_per_objfile->read_in_chain;
24273 last_chain = &dwarf2_per_objfile->read_in_chain;
24274 while (per_cu != NULL)
24275 {
24276 struct dwarf2_per_cu_data *next_cu;
24277
24278 next_cu = per_cu->cu->read_in_chain;
24279
24280 if (!per_cu->cu->mark)
24281 {
fcd3b13d 24282 delete per_cu->cu;
ae038cb0
DJ
24283 *last_chain = next_cu;
24284 }
24285 else
24286 last_chain = &per_cu->cu->read_in_chain;
24287
24288 per_cu = next_cu;
24289 }
24290}
24291
24292/* Remove a single compilation unit from the cache. */
24293
24294static void
dee91e82 24295free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24296{
24297 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24298 struct dwarf2_per_objfile *dwarf2_per_objfile
24299 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24300
24301 per_cu = dwarf2_per_objfile->read_in_chain;
24302 last_chain = &dwarf2_per_objfile->read_in_chain;
24303 while (per_cu != NULL)
24304 {
24305 struct dwarf2_per_cu_data *next_cu;
24306
24307 next_cu = per_cu->cu->read_in_chain;
24308
dee91e82 24309 if (per_cu == target_per_cu)
ae038cb0 24310 {
fcd3b13d 24311 delete per_cu->cu;
dee91e82 24312 per_cu->cu = NULL;
ae038cb0
DJ
24313 *last_chain = next_cu;
24314 break;
24315 }
24316 else
24317 last_chain = &per_cu->cu->read_in_chain;
24318
24319 per_cu = next_cu;
24320 }
24321}
24322
dee91e82
DE
24323/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24324 We store these in a hash table separate from the DIEs, and preserve them
24325 when the DIEs are flushed out of cache.
24326
24327 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24328 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24329 or the type may come from a DWO file. Furthermore, while it's more logical
24330 to use per_cu->section+offset, with Fission the section with the data is in
24331 the DWO file but we don't know that section at the point we need it.
24332 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24333 because we can enter the lookup routine, get_die_type_at_offset, from
24334 outside this file, and thus won't necessarily have PER_CU->cu.
24335 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24336
dee91e82 24337struct dwarf2_per_cu_offset_and_type
1c379e20 24338{
dee91e82 24339 const struct dwarf2_per_cu_data *per_cu;
9c541725 24340 sect_offset sect_off;
1c379e20
DJ
24341 struct type *type;
24342};
24343
dee91e82 24344/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24345
24346static hashval_t
dee91e82 24347per_cu_offset_and_type_hash (const void *item)
1c379e20 24348{
9a3c8263
SM
24349 const struct dwarf2_per_cu_offset_and_type *ofs
24350 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24351
9c541725 24352 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24353}
24354
dee91e82 24355/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24356
24357static int
dee91e82 24358per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24359{
9a3c8263
SM
24360 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24361 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24362 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24363 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24364
dee91e82 24365 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24366 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24367}
24368
24369/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24370 table if necessary. For convenience, return TYPE.
24371
24372 The DIEs reading must have careful ordering to:
85102364 24373 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24374 reading current DIE.
24375 * Not trying to dereference contents of still incompletely read in types
24376 while reading in other DIEs.
24377 * Enable referencing still incompletely read in types just by a pointer to
24378 the type without accessing its fields.
24379
24380 Therefore caller should follow these rules:
24381 * Try to fetch any prerequisite types we may need to build this DIE type
24382 before building the type and calling set_die_type.
e71ec853 24383 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24384 possible before fetching more types to complete the current type.
24385 * Make the type as complete as possible before fetching more types. */
1c379e20 24386
f792889a 24387static struct type *
1c379e20
DJ
24388set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24389{
518817b3
SM
24390 struct dwarf2_per_objfile *dwarf2_per_objfile
24391 = cu->per_cu->dwarf2_per_objfile;
dee91e82 24392 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 24393 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
24394 struct attribute *attr;
24395 struct dynamic_prop prop;
1c379e20 24396
b4ba55a1
JB
24397 /* For Ada types, make sure that the gnat-specific data is always
24398 initialized (if not already set). There are a few types where
24399 we should not be doing so, because the type-specific area is
24400 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24401 where the type-specific area is used to store the floatformat).
24402 But this is not a problem, because the gnat-specific information
24403 is actually not needed for these types. */
24404 if (need_gnat_info (cu)
24405 && TYPE_CODE (type) != TYPE_CODE_FUNC
24406 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
24407 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24408 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24409 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
24410 && !HAVE_GNAT_AUX_INFO (type))
24411 INIT_GNAT_SPECIFIC (type);
24412
3f2f83dd
KB
24413 /* Read DW_AT_allocated and set in type. */
24414 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 24415 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24416 {
09ba997f 24417 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24418 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24419 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
24420 }
24421 else if (attr != NULL)
24422 {
b98664d3 24423 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 24424 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24425 sect_offset_str (die->sect_off));
3f2f83dd
KB
24426 }
24427
24428 /* Read DW_AT_associated and set in type. */
24429 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 24430 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24431 {
09ba997f 24432 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24433 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24434 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
24435 }
24436 else if (attr != NULL)
24437 {
b98664d3 24438 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 24439 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24440 sect_offset_str (die->sect_off));
3f2f83dd
KB
24441 }
24442
3cdcd0ce
JB
24443 /* Read DW_AT_data_location and set in type. */
24444 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 24445 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 24446 cu->per_cu->addr_type ()))
50a82047 24447 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 24448
dee91e82 24449 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
24450 dwarf2_per_objfile->die_type_hash
24451 = htab_up (htab_create_alloc (127,
24452 per_cu_offset_and_type_hash,
24453 per_cu_offset_and_type_eq,
24454 NULL, xcalloc, xfree));
1c379e20 24455
dee91e82 24456 ofs.per_cu = cu->per_cu;
9c541725 24457 ofs.sect_off = die->sect_off;
1c379e20 24458 ofs.type = type;
dee91e82 24459 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 24460 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24461 if (*slot)
b98664d3 24462 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24463 sect_offset_str (die->sect_off));
8d749320
SM
24464 *slot = XOBNEW (&objfile->objfile_obstack,
24465 struct dwarf2_per_cu_offset_and_type);
1c379e20 24466 **slot = ofs;
f792889a 24467 return type;
1c379e20
DJ
24468}
24469
9c541725 24470/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24471 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24472
24473static struct type *
9c541725 24474get_die_type_at_offset (sect_offset sect_off,
673bfd45 24475 struct dwarf2_per_cu_data *per_cu)
1c379e20 24476{
dee91e82 24477 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 24478 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 24479
dee91e82 24480 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24481 return NULL;
1c379e20 24482
dee91e82 24483 ofs.per_cu = per_cu;
9c541725 24484 ofs.sect_off = sect_off;
9a3c8263 24485 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 24486 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24487 if (slot)
24488 return slot->type;
24489 else
24490 return NULL;
24491}
24492
02142a6c 24493/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24494 or return NULL if DIE does not have a saved type. */
24495
24496static struct type *
24497get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24498{
9c541725 24499 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24500}
24501
10b3939b
DJ
24502/* Add a dependence relationship from CU to REF_PER_CU. */
24503
24504static void
24505dwarf2_add_dependence (struct dwarf2_cu *cu,
24506 struct dwarf2_per_cu_data *ref_per_cu)
24507{
24508 void **slot;
24509
24510 if (cu->dependencies == NULL)
24511 cu->dependencies
24512 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24513 NULL, &cu->comp_unit_obstack,
24514 hashtab_obstack_allocate,
24515 dummy_obstack_deallocate);
24516
24517 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24518 if (*slot == NULL)
24519 *slot = ref_per_cu;
24520}
1c379e20 24521
f504f079
DE
24522/* Subroutine of dwarf2_mark to pass to htab_traverse.
24523 Set the mark field in every compilation unit in the
ae038cb0
DJ
24524 cache that we must keep because we are keeping CU. */
24525
10b3939b
DJ
24526static int
24527dwarf2_mark_helper (void **slot, void *data)
24528{
24529 struct dwarf2_per_cu_data *per_cu;
24530
24531 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24532
24533 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24534 reading of the chain. As such dependencies remain valid it is not much
24535 useful to track and undo them during QUIT cleanups. */
24536 if (per_cu->cu == NULL)
24537 return 1;
24538
10b3939b
DJ
24539 if (per_cu->cu->mark)
24540 return 1;
9068261f 24541 per_cu->cu->mark = true;
10b3939b
DJ
24542
24543 if (per_cu->cu->dependencies != NULL)
24544 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24545
24546 return 1;
24547}
24548
f504f079
DE
24549/* Set the mark field in CU and in every other compilation unit in the
24550 cache that we must keep because we are keeping CU. */
24551
ae038cb0
DJ
24552static void
24553dwarf2_mark (struct dwarf2_cu *cu)
24554{
24555 if (cu->mark)
24556 return;
9068261f 24557 cu->mark = true;
10b3939b
DJ
24558 if (cu->dependencies != NULL)
24559 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24560}
24561
24562static void
24563dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24564{
24565 while (per_cu)
24566 {
9068261f 24567 per_cu->cu->mark = false;
ae038cb0
DJ
24568 per_cu = per_cu->cu->read_in_chain;
24569 }
72bf9492
DJ
24570}
24571
72bf9492
DJ
24572/* Trivial hash function for partial_die_info: the hash value of a DIE
24573 is its offset in .debug_info for this objfile. */
24574
24575static hashval_t
24576partial_die_hash (const void *item)
24577{
9a3c8263
SM
24578 const struct partial_die_info *part_die
24579 = (const struct partial_die_info *) item;
9a619af0 24580
9c541725 24581 return to_underlying (part_die->sect_off);
72bf9492
DJ
24582}
24583
24584/* Trivial comparison function for partial_die_info structures: two DIEs
24585 are equal if they have the same offset. */
24586
24587static int
24588partial_die_eq (const void *item_lhs, const void *item_rhs)
24589{
9a3c8263
SM
24590 const struct partial_die_info *part_die_lhs
24591 = (const struct partial_die_info *) item_lhs;
24592 const struct partial_die_info *part_die_rhs
24593 = (const struct partial_die_info *) item_rhs;
9a619af0 24594
9c541725 24595 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24596}
24597
3c3bb058
AB
24598struct cmd_list_element *set_dwarf_cmdlist;
24599struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24600
24601static void
981a3fb3 24602set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24603{
b4f54984 24604 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24605 gdb_stdout);
ae038cb0
DJ
24606}
24607
24608static void
981a3fb3 24609show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24610{
b4f54984 24611 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24612}
24613
9291a0cd 24614static void
cd4fb1b2
SM
24615show_check_physname (struct ui_file *file, int from_tty,
24616 struct cmd_list_element *c, const char *value)
9291a0cd 24617{
cd4fb1b2
SM
24618 fprintf_filtered (file,
24619 _("Whether to check \"physname\" is %s.\n"),
24620 value);
9291a0cd
TT
24621}
24622
6c265988 24623void _initialize_dwarf2_read ();
cd4fb1b2 24624void
6c265988 24625_initialize_dwarf2_read ()
9291a0cd 24626{
cd4fb1b2
SM
24627 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24628Set DWARF specific variables.\n\
590042fc 24629Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
24630 &set_dwarf_cmdlist, "maintenance set dwarf ",
24631 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24632
cd4fb1b2 24633 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
24634Show DWARF specific variables.\n\
24635Show DWARF variables such as the cache size."),
cd4fb1b2
SM
24636 &show_dwarf_cmdlist, "maintenance show dwarf ",
24637 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24638
cd4fb1b2
SM
24639 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24640 &dwarf_max_cache_age, _("\
24641Set the upper bound on the age of cached DWARF compilation units."), _("\
24642Show the upper bound on the age of cached DWARF compilation units."), _("\
24643A higher limit means that cached compilation units will be stored\n\
24644in memory longer, and more total memory will be used. Zero disables\n\
24645caching, which can slow down startup."),
24646 NULL,
24647 show_dwarf_max_cache_age,
24648 &set_dwarf_cmdlist,
24649 &show_dwarf_cmdlist);
156942c7 24650
cd4fb1b2
SM
24651 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24652Set debugging of the DWARF reader."), _("\
24653Show debugging of the DWARF reader."), _("\
24654When enabled (non-zero), debugging messages are printed during DWARF\n\
24655reading and symtab expansion. A value of 1 (one) provides basic\n\
24656information. A value greater than 1 provides more verbose information."),
24657 NULL,
24658 NULL,
24659 &setdebuglist, &showdebuglist);
9291a0cd 24660
cd4fb1b2
SM
24661 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24662Set debugging of the DWARF DIE reader."), _("\
24663Show debugging of the DWARF DIE reader."), _("\
24664When enabled (non-zero), DIEs are dumped after they are read in.\n\
24665The value is the maximum depth to print."),
24666 NULL,
24667 NULL,
24668 &setdebuglist, &showdebuglist);
9291a0cd 24669
cd4fb1b2
SM
24670 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24671Set debugging of the dwarf line reader."), _("\
24672Show debugging of the dwarf line reader."), _("\
24673When enabled (non-zero), line number entries are dumped as they are read in.\n\
24674A value of 1 (one) provides basic information.\n\
24675A value greater than 1 provides more verbose information."),
24676 NULL,
24677 NULL,
24678 &setdebuglist, &showdebuglist);
437afbb8 24679
cd4fb1b2
SM
24680 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24681Set cross-checking of \"physname\" code against demangler."), _("\
24682Show cross-checking of \"physname\" code against demangler."), _("\
24683When enabled, GDB's internal \"physname\" code is checked against\n\
24684the demangler."),
24685 NULL, show_check_physname,
24686 &setdebuglist, &showdebuglist);
900e11f9 24687
e615022a
DE
24688 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24689 no_class, &use_deprecated_index_sections, _("\
24690Set whether to use deprecated gdb_index sections."), _("\
24691Show whether to use deprecated gdb_index sections."), _("\
24692When enabled, deprecated .gdb_index sections are used anyway.\n\
24693Normally they are ignored either because of a missing feature or\n\
24694performance issue.\n\
24695Warning: This option must be enabled before gdb reads the file."),
24696 NULL,
24697 NULL,
24698 &setlist, &showlist);
24699
f1e6e072
TT
24700 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24701 &dwarf2_locexpr_funcs);
24702 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24703 &dwarf2_loclist_funcs);
24704
24705 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24706 &dwarf2_block_frame_base_locexpr_funcs);
24707 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24708 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24709
24710#if GDB_SELF_TEST
24711 selftests::register_test ("dw2_expand_symtabs_matching",
24712 selftests::dw2_expand_symtabs_matching::run_test);
24713#endif
6502dd73 24714}
This page took 5.610497 seconds and 4 git commands to generate.