[gdb/testsuite] Capture many-headers.exp progress and output in gdb.log
[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"
437afbb8 85
73be47f5
DE
86/* When == 1, print basic high level tracing messages.
87 When > 1, be more verbose.
b4f54984
DE
88 This is in contrast to the low level DIE reading of dwarf_die_debug. */
89static unsigned int dwarf_read_debug = 0;
45cfd468 90
d97bc12b 91/* When non-zero, dump DIEs after they are read in. */
b4f54984 92static unsigned int dwarf_die_debug = 0;
d97bc12b 93
27e0867f 94/* When non-zero, dump line number entries as they are read in. */
8fdd972c 95unsigned int dwarf_line_debug = 0;
27e0867f 96
491144b5
CB
97/* When true, cross-check physname against demangler. */
98static bool check_physname = false;
900e11f9 99
491144b5
CB
100/* When true, do not reject deprecated .gdb_index sections. */
101static bool use_deprecated_index_sections = false;
481860b3 102
5bfd760d 103static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 104
f1e6e072
TT
105/* The "aclass" indices for various kinds of computed DWARF symbols. */
106
107static int dwarf2_locexpr_index;
108static int dwarf2_loclist_index;
109static int dwarf2_locexpr_block_index;
110static int dwarf2_loclist_block_index;
111
3f563c84
PA
112/* An index into a (C++) symbol name component in a symbol name as
113 recorded in the mapped_index's symbol table. For each C++ symbol
114 in the symbol table, we record one entry for the start of each
115 component in the symbol in a table of name components, and then
116 sort the table, in order to be able to binary search symbol names,
117 ignoring leading namespaces, both completion and regular look up.
118 For example, for symbol "A::B::C", we'll have an entry that points
119 to "A::B::C", another that points to "B::C", and another for "C".
120 Note that function symbols in GDB index have no parameter
121 information, just the function/method names. You can convert a
122 name_component to a "const char *" using the
123 'mapped_index::symbol_name_at(offset_type)' method. */
124
125struct name_component
126{
127 /* Offset in the symbol name where the component starts. Stored as
128 a (32-bit) offset instead of a pointer to save memory and improve
129 locality on 64-bit architectures. */
130 offset_type name_offset;
131
132 /* The symbol's index in the symbol and constant pool tables of a
133 mapped_index. */
134 offset_type idx;
135};
136
44ed8f3e
PA
137/* Base class containing bits shared by both .gdb_index and
138 .debug_name indexes. */
139
140struct mapped_index_base
141{
22ca247e
TT
142 mapped_index_base () = default;
143 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
144
44ed8f3e
PA
145 /* The name_component table (a sorted vector). See name_component's
146 description above. */
147 std::vector<name_component> name_components;
148
149 /* How NAME_COMPONENTS is sorted. */
150 enum case_sensitivity name_components_casing;
151
152 /* Return the number of names in the symbol table. */
153 virtual size_t symbol_name_count () const = 0;
154
155 /* Get the name of the symbol at IDX in the symbol table. */
156 virtual const char *symbol_name_at (offset_type idx) const = 0;
157
158 /* Return whether the name at IDX in the symbol table should be
159 ignored. */
160 virtual bool symbol_name_slot_invalid (offset_type idx) const
161 {
162 return false;
163 }
164
165 /* Build the symbol name component sorted vector, if we haven't
166 yet. */
167 void build_name_components ();
168
169 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
170 possible matches for LN_NO_PARAMS in the name component
171 vector. */
172 std::pair<std::vector<name_component>::const_iterator,
173 std::vector<name_component>::const_iterator>
3b00ef10
TT
174 find_name_components_bounds (const lookup_name_info &ln_no_params,
175 enum language lang) const;
44ed8f3e
PA
176
177 /* Prevent deleting/destroying via a base class pointer. */
178protected:
179 ~mapped_index_base() = default;
180};
181
9291a0cd
TT
182/* A description of the mapped index. The file format is described in
183 a comment by the code that writes the index. */
fc898b42 184struct mapped_index final : public mapped_index_base
9291a0cd 185{
f00a2de2
PA
186 /* A slot/bucket in the symbol table hash. */
187 struct symbol_table_slot
188 {
189 const offset_type name;
190 const offset_type vec;
191 };
192
559a7a62 193 /* Index data format version. */
3063847f 194 int version = 0;
559a7a62 195
f00a2de2
PA
196 /* The address table data. */
197 gdb::array_view<const gdb_byte> address_table;
b11b1f88 198
3876f04e 199 /* The symbol table, implemented as a hash table. */
f00a2de2 200 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 201
9291a0cd 202 /* A pointer to the constant pool. */
3063847f 203 const char *constant_pool = nullptr;
3f563c84 204
44ed8f3e
PA
205 bool symbol_name_slot_invalid (offset_type idx) const override
206 {
207 const auto &bucket = this->symbol_table[idx];
9ab08412 208 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 209 }
5c58de74 210
3f563c84
PA
211 /* Convenience method to get at the name of the symbol at IDX in the
212 symbol table. */
44ed8f3e 213 const char *symbol_name_at (offset_type idx) const override
f00a2de2 214 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 215
44ed8f3e
PA
216 size_t symbol_name_count () const override
217 { return this->symbol_table.size (); }
9291a0cd
TT
218};
219
927aa2e7
JK
220/* A description of the mapped .debug_names.
221 Uninitialized map has CU_COUNT 0. */
fc898b42 222struct mapped_debug_names final : public mapped_index_base
927aa2e7 223{
ed2dc618
SM
224 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
225 : dwarf2_per_objfile (dwarf2_per_objfile_)
226 {}
227
228 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
229 bfd_endian dwarf5_byte_order;
230 bool dwarf5_is_dwarf64;
231 bool augmentation_is_gdb;
232 uint8_t offset_size;
233 uint32_t cu_count = 0;
234 uint32_t tu_count, bucket_count, name_count;
235 const gdb_byte *cu_table_reordered, *tu_table_reordered;
236 const uint32_t *bucket_table_reordered, *hash_table_reordered;
237 const gdb_byte *name_table_string_offs_reordered;
238 const gdb_byte *name_table_entry_offs_reordered;
239 const gdb_byte *entry_pool;
240
241 struct index_val
242 {
243 ULONGEST dwarf_tag;
244 struct attr
245 {
246 /* Attribute name DW_IDX_*. */
247 ULONGEST dw_idx;
248
249 /* Attribute form DW_FORM_*. */
250 ULONGEST form;
251
252 /* Value if FORM is DW_FORM_implicit_const. */
253 LONGEST implicit_const;
254 };
255 std::vector<attr> attr_vec;
256 };
257
258 std::unordered_map<ULONGEST, index_val> abbrev_map;
259
260 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
261
262 /* Implementation of the mapped_index_base virtual interface, for
263 the name_components cache. */
264
265 const char *symbol_name_at (offset_type idx) const override
266 { return namei_to_name (idx); }
267
268 size_t symbol_name_count () const override
269 { return this->name_count; }
927aa2e7
JK
270};
271
cd4fb1b2 272/* See dwarf2read.h. */
ed2dc618 273
cd4fb1b2 274dwarf2_per_objfile *
ed2dc618
SM
275get_dwarf2_per_objfile (struct objfile *objfile)
276{
5bfd760d 277 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 278}
c906108c 279
251d32d9 280/* Default names of the debugging sections. */
c906108c 281
233a11ab
CS
282/* Note that if the debugging section has been compressed, it might
283 have a name like .zdebug_info. */
284
9cdd5dbd
DE
285static const struct dwarf2_debug_sections dwarf2_elf_names =
286{
251d32d9
TG
287 { ".debug_info", ".zdebug_info" },
288 { ".debug_abbrev", ".zdebug_abbrev" },
289 { ".debug_line", ".zdebug_line" },
290 { ".debug_loc", ".zdebug_loc" },
43988095 291 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 292 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 293 { ".debug_macro", ".zdebug_macro" },
251d32d9 294 { ".debug_str", ".zdebug_str" },
18a8505e 295 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 296 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 297 { ".debug_ranges", ".zdebug_ranges" },
43988095 298 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 299 { ".debug_types", ".zdebug_types" },
3019eac3 300 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
301 { ".debug_frame", ".zdebug_frame" },
302 { ".eh_frame", NULL },
24d3216f 303 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
304 { ".debug_names", ".zdebug_names" },
305 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 306 23
251d32d9 307};
c906108c 308
80626a55 309/* List of DWO/DWP sections. */
3019eac3 310
80626a55 311static const struct dwop_section_names
3019eac3
DE
312{
313 struct dwarf2_section_names abbrev_dwo;
314 struct dwarf2_section_names info_dwo;
315 struct dwarf2_section_names line_dwo;
316 struct dwarf2_section_names loc_dwo;
43988095 317 struct dwarf2_section_names loclists_dwo;
09262596
DE
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
3019eac3
DE
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
80626a55
DE
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
3019eac3 325}
80626a55 326dwop_section_names =
3019eac3
DE
327{
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 332 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
333 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
334 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
335 { ".debug_str.dwo", ".zdebug_str.dwo" },
336 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
337 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
338 { ".debug_cu_index", ".zdebug_cu_index" },
339 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
340};
341
c906108c
SS
342/* local data types */
343
3da10d80
KS
344/* Type used for delaying computation of method physnames.
345 See comments for compute_delayed_physnames. */
346struct delayed_method_info
347{
348 /* The type to which the method is attached, i.e., its parent class. */
349 struct type *type;
350
351 /* The index of the method in the type's function fieldlists. */
352 int fnfield_index;
353
354 /* The index of the method in the fieldlist. */
355 int index;
356
357 /* The name of the DIE. */
358 const char *name;
359
360 /* The DIE associated with this method. */
361 struct die_info *die;
362};
363
e7c27a73
DJ
364/* Internal state when decoding a particular compilation unit. */
365struct dwarf2_cu
366{
fcd3b13d
SM
367 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
368 ~dwarf2_cu ();
369
370 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
371
c24bdb02
KS
372 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
373 Create the set of symtabs used by this TU, or if this TU is sharing
374 symtabs with another TU and the symtabs have already been created
375 then restore those symtabs in the line header.
376 We don't need the pc/line-number mapping for type units. */
377 void setup_type_unit_groups (struct die_info *die);
378
379 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
380 buildsym_compunit constructor. */
381 struct compunit_symtab *start_symtab (const char *name,
382 const char *comp_dir,
383 CORE_ADDR low_pc);
384
385 /* Reset the builder. */
386 void reset_builder () { m_builder.reset (); }
387
d00adf39 388 /* The header of the compilation unit. */
fcd3b13d 389 struct comp_unit_head header {};
e142c38c 390
d00adf39 391 /* Base address of this compilation unit. */
fcd3b13d 392 CORE_ADDR base_address = 0;
d00adf39
DE
393
394 /* Non-zero if base_address has been set. */
fcd3b13d 395 int base_known = 0;
d00adf39 396
e142c38c 397 /* The language we are debugging. */
fcd3b13d
SM
398 enum language language = language_unknown;
399 const struct language_defn *language_defn = nullptr;
e142c38c 400
fcd3b13d 401 const char *producer = nullptr;
b0f35d58 402
c24bdb02 403private:
804d2729
TT
404 /* The symtab builder for this CU. This is only non-NULL when full
405 symbols are being read. */
c24bdb02 406 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 407
c24bdb02 408public:
e142c38c
DJ
409 /* The generic symbol table building routines have separate lists for
410 file scope symbols and all all other scopes (local scopes). So
411 we need to select the right one to pass to add_symbol_to_list().
412 We do it by keeping a pointer to the correct list in list_in_scope.
413
414 FIXME: The original dwarf code just treated the file scope as the
415 first local scope, and all other local scopes as nested local
416 scopes, and worked fine. Check to see if we really need to
417 distinguish these in buildsym.c. */
fcd3b13d 418 struct pending **list_in_scope = nullptr;
e142c38c 419
b64f50a1
JK
420 /* Hash table holding all the loaded partial DIEs
421 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 422 htab_t partial_dies = nullptr;
72bf9492
DJ
423
424 /* Storage for things with the same lifetime as this read-in compilation
425 unit, including partial DIEs. */
fcd3b13d 426 auto_obstack comp_unit_obstack;
72bf9492 427
ae038cb0
DJ
428 /* When multiple dwarf2_cu structures are living in memory, this field
429 chains them all together, so that they can be released efficiently.
430 We will probably also want a generation counter so that most-recently-used
431 compilation units are cached... */
fcd3b13d 432 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 433
69d751e3 434 /* Backlink to our per_cu entry. */
ae038cb0
DJ
435 struct dwarf2_per_cu_data *per_cu;
436
437 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 438 int last_used = 0;
ae038cb0 439
b64f50a1
JK
440 /* A hash table of DIE cu_offset for following references with
441 die_info->offset.sect_off as hash. */
fcd3b13d 442 htab_t die_hash = nullptr;
10b3939b
DJ
443
444 /* Full DIEs if read in. */
fcd3b13d 445 struct die_info *dies = nullptr;
10b3939b
DJ
446
447 /* A set of pointers to dwarf2_per_cu_data objects for compilation
448 units referenced by this one. Only set during full symbol processing;
449 partial symbol tables do not have dependencies. */
fcd3b13d 450 htab_t dependencies = nullptr;
10b3939b 451
cb1df416 452 /* Header data from the line table, during full symbol processing. */
fcd3b13d 453 struct line_header *line_header = nullptr;
4c8aa72d
PA
454 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
455 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
456 this is the DW_TAG_compile_unit die for this CU. We'll hold on
457 to the line header as long as this DIE is being processed. See
458 process_die_scope. */
fcd3b13d 459 die_info *line_header_die_owner = nullptr;
cb1df416 460
3da10d80
KS
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
c89b44cd 463 std::vector<delayed_method_info> method_list;
3da10d80 464
96408a79 465 /* To be copied to symtab->call_site_htab. */
fcd3b13d 466 htab_t call_site_htab = nullptr;
96408a79 467
034e5797
DE
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
18a8505e 472 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
fcd3b13d 477 struct dwo_unit *dwo_unit = nullptr;
3019eac3 478
18a8505e 479 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 480 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 481 gdb::optional<ULONGEST> addr_base;
3019eac3 482
18a8505e 483 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 484 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 485 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
486 be used without needing to know whether DWO files are in use or not.
487 N.B. This does not apply to DW_AT_ranges appearing in
488 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
489 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 490 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 491 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 492 ULONGEST ranges_base = 0;
2e3cf129 493
c9317f21
TT
494 /* When reading debug info generated by older versions of rustc, we
495 have to rewrite some union types to be struct types with a
496 variant part. This rewriting must be done after the CU is fully
497 read in, because otherwise at the point of rewriting some struct
498 type might not have been fully processed. So, we keep a list of
499 all such types here and process them after expansion. */
500 std::vector<struct type *> rust_unions;
501
18a8505e
AT
502 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
503 files, the value is implicitly zero. For DWARF 5 version DWO files, the
504 value is often implicit and is the size of the header of
505 .debug_str_offsets section (8 or 4, depending on the address size). */
506 gdb::optional<ULONGEST> str_offsets_base;
507
ae038cb0 508 /* Mark used when releasing cached dies. */
9068261f 509 bool mark : 1;
ae038cb0 510
8be455d7
JK
511 /* This CU references .debug_loc. See the symtab->locations_valid field.
512 This test is imperfect as there may exist optimized debug code not using
513 any location list and still facing inlining issues if handled as
514 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 515 bool has_loclist : 1;
ba919b58 516
9068261f 517 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
518 if all the producer_is_* fields are valid. This information is cached
519 because profiling CU expansion showed excessive time spent in
520 producer_is_gxx_lt_4_6. */
9068261f
AB
521 bool checked_producer : 1;
522 bool producer_is_gxx_lt_4_6 : 1;
523 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 524 bool producer_is_icc : 1;
9068261f 525 bool producer_is_icc_lt_14 : 1;
c258c396 526 bool producer_is_codewarrior : 1;
4d4ec4e5 527
9068261f 528 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
529 debugging info for C++ namespaces. GCC 3.3.x did not produce
530 this information, but later versions do. */
531
9068261f 532 bool processing_has_namespace_info : 1;
d590ff25
YQ
533
534 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
535
536 /* If this CU was inherited by another CU (via specification,
537 abstract_origin, etc), this is the ancestor CU. */
538 dwarf2_cu *ancestor;
539
540 /* Get the buildsym_compunit for this CU. */
541 buildsym_compunit *get_builder ()
542 {
543 /* If this CU has a builder associated with it, use that. */
544 if (m_builder != nullptr)
545 return m_builder.get ();
546
547 /* Otherwise, search ancestors for a valid builder. */
548 if (ancestor != nullptr)
549 return ancestor->get_builder ();
550
551 return nullptr;
552 }
e7c27a73
DJ
553};
554
094b34ac
DE
555/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
556 This includes type_unit_group and quick_file_names. */
557
558struct stmt_list_hash
559{
560 /* The DWO unit this table is from or NULL if there is none. */
561 struct dwo_unit *dwo_unit;
562
563 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 564 sect_offset line_sect_off;
094b34ac
DE
565};
566
f4dc4d17
DE
567/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
568 an object of this type. */
569
570struct type_unit_group
571{
0186c6a7 572 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
573 To simplify things we create an artificial CU that "includes" all the
574 type units using this stmt_list so that the rest of the code still has
575 a "per_cu" handle on the symtab.
576 This PER_CU is recognized by having no section. */
8a0459fd 577#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
578 struct dwarf2_per_cu_data per_cu;
579
0186c6a7
DE
580 /* The TUs that share this DW_AT_stmt_list entry.
581 This is added to while parsing type units to build partial symtabs,
582 and is deleted afterwards and not used again. */
a8b3b8e9 583 std::vector<signatured_type *> *tus;
f4dc4d17 584
43f3e411 585 /* The compunit symtab.
094b34ac 586 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
587 so we create an essentially anonymous symtab as the compunit symtab. */
588 struct compunit_symtab *compunit_symtab;
f4dc4d17 589
094b34ac
DE
590 /* The data used to construct the hash key. */
591 struct stmt_list_hash hash;
f4dc4d17
DE
592
593 /* The number of symtabs from the line header.
594 The value here must match line_header.num_file_names. */
595 unsigned int num_symtabs;
596
597 /* The symbol tables for this TU (obtained from the files listed in
598 DW_AT_stmt_list).
599 WARNING: The order of entries here must match the order of entries
600 in the line header. After the first TU using this type_unit_group, the
601 line header for the subsequent TUs is recreated from this. This is done
602 because we need to use the same symtabs for each TU using the same
603 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
604 there's no guarantee the line header doesn't have duplicate entries. */
605 struct symtab **symtabs;
606};
607
73869dc2 608/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
609
610struct dwo_sections
611{
612 struct dwarf2_section_info abbrev;
3019eac3
DE
613 struct dwarf2_section_info line;
614 struct dwarf2_section_info loc;
43988095 615 struct dwarf2_section_info loclists;
09262596
DE
616 struct dwarf2_section_info macinfo;
617 struct dwarf2_section_info macro;
3019eac3
DE
618 struct dwarf2_section_info str;
619 struct dwarf2_section_info str_offsets;
80626a55
DE
620 /* In the case of a virtual DWO file, these two are unused. */
621 struct dwarf2_section_info info;
fd5866f6 622 std::vector<dwarf2_section_info> types;
3019eac3
DE
623};
624
c88ee1f0 625/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
626
627struct dwo_unit
628{
629 /* Backlink to the containing struct dwo_file. */
630 struct dwo_file *dwo_file;
631
632 /* The "id" that distinguishes this CU/TU.
633 .debug_info calls this "dwo_id", .debug_types calls this "signature".
634 Since signatures came first, we stick with it for consistency. */
635 ULONGEST signature;
636
637 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 638 struct dwarf2_section_info *section;
3019eac3 639
9c541725
PA
640 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
641 sect_offset sect_off;
3019eac3
DE
642 unsigned int length;
643
644 /* For types, offset in the type's DIE of the type defined by this TU. */
645 cu_offset type_offset_in_tu;
646};
647
73869dc2
DE
648/* include/dwarf2.h defines the DWP section codes.
649 It defines a max value but it doesn't define a min value, which we
650 use for error checking, so provide one. */
651
652enum dwp_v2_section_ids
653{
654 DW_SECT_MIN = 1
655};
656
80626a55 657/* Data for one DWO file.
57d63ce2
DE
658
659 This includes virtual DWO files (a virtual DWO file is a DWO file as it
660 appears in a DWP file). DWP files don't really have DWO files per se -
661 comdat folding of types "loses" the DWO file they came from, and from
662 a high level view DWP files appear to contain a mass of random types.
663 However, to maintain consistency with the non-DWP case we pretend DWP
664 files contain virtual DWO files, and we assign each TU with one virtual
665 DWO file (generally based on the line and abbrev section offsets -
666 a heuristic that seems to work in practice). */
3019eac3
DE
667
668struct dwo_file
669{
51ac9db5
SM
670 dwo_file () = default;
671 DISABLE_COPY_AND_ASSIGN (dwo_file);
672
18a8505e 673 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
674 For virtual DWO files the name is constructed from the section offsets
675 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
676 from related CU+TUs. */
51ac9db5 677 const char *dwo_name = nullptr;
0ac5b59e
DE
678
679 /* The DW_AT_comp_dir attribute. */
51ac9db5 680 const char *comp_dir = nullptr;
3019eac3 681
80626a55
DE
682 /* The bfd, when the file is open. Otherwise this is NULL.
683 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 684 gdb_bfd_ref_ptr dbfd;
3019eac3 685
73869dc2
DE
686 /* The sections that make up this DWO file.
687 Remember that for virtual DWO files in DWP V2, these are virtual
688 sections (for lack of a better name). */
51ac9db5 689 struct dwo_sections sections {};
3019eac3 690
33c5cd75
DB
691 /* The CUs in the file.
692 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
693 an extension to handle LLVM's Link Time Optimization output (where
694 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 695 htab_up cus;
3019eac3
DE
696
697 /* Table of TUs in the file.
698 Each element is a struct dwo_unit. */
b0b6a987 699 htab_up tus;
3019eac3
DE
700};
701
80626a55
DE
702/* These sections are what may appear in a DWP file. */
703
704struct dwp_sections
705{
73869dc2 706 /* These are used by both DWP version 1 and 2. */
80626a55
DE
707 struct dwarf2_section_info str;
708 struct dwarf2_section_info cu_index;
709 struct dwarf2_section_info tu_index;
73869dc2
DE
710
711 /* These are only used by DWP version 2 files.
712 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
713 sections are referenced by section number, and are not recorded here.
714 In DWP version 2 there is at most one copy of all these sections, each
715 section being (effectively) comprised of the concatenation of all of the
716 individual sections that exist in the version 1 format.
717 To keep the code simple we treat each of these concatenated pieces as a
718 section itself (a virtual section?). */
719 struct dwarf2_section_info abbrev;
720 struct dwarf2_section_info info;
721 struct dwarf2_section_info line;
722 struct dwarf2_section_info loc;
723 struct dwarf2_section_info macinfo;
724 struct dwarf2_section_info macro;
725 struct dwarf2_section_info str_offsets;
726 struct dwarf2_section_info types;
80626a55
DE
727};
728
73869dc2
DE
729/* These sections are what may appear in a virtual DWO file in DWP version 1.
730 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 731
73869dc2 732struct virtual_v1_dwo_sections
80626a55
DE
733{
734 struct dwarf2_section_info abbrev;
735 struct dwarf2_section_info line;
736 struct dwarf2_section_info loc;
737 struct dwarf2_section_info macinfo;
738 struct dwarf2_section_info macro;
739 struct dwarf2_section_info str_offsets;
740 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 741 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
742 struct dwarf2_section_info info_or_types;
743};
744
73869dc2
DE
745/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
746 In version 2, the sections of the DWO files are concatenated together
747 and stored in one section of that name. Thus each ELF section contains
748 several "virtual" sections. */
749
750struct virtual_v2_dwo_sections
751{
752 bfd_size_type abbrev_offset;
753 bfd_size_type abbrev_size;
754
755 bfd_size_type line_offset;
756 bfd_size_type line_size;
757
758 bfd_size_type loc_offset;
759 bfd_size_type loc_size;
760
761 bfd_size_type macinfo_offset;
762 bfd_size_type macinfo_size;
763
764 bfd_size_type macro_offset;
765 bfd_size_type macro_size;
766
767 bfd_size_type str_offsets_offset;
768 bfd_size_type str_offsets_size;
769
770 /* Each DWP hash table entry records one CU or one TU.
771 That is recorded here, and copied to dwo_unit.section. */
772 bfd_size_type info_or_types_offset;
773 bfd_size_type info_or_types_size;
774};
775
80626a55
DE
776/* Contents of DWP hash tables. */
777
778struct dwp_hash_table
779{
73869dc2 780 uint32_t version, nr_columns;
80626a55 781 uint32_t nr_units, nr_slots;
73869dc2
DE
782 const gdb_byte *hash_table, *unit_table;
783 union
784 {
785 struct
786 {
787 const gdb_byte *indices;
788 } v1;
789 struct
790 {
791 /* This is indexed by column number and gives the id of the section
792 in that column. */
793#define MAX_NR_V2_DWO_SECTIONS \
794 (1 /* .debug_info or .debug_types */ \
795 + 1 /* .debug_abbrev */ \
796 + 1 /* .debug_line */ \
797 + 1 /* .debug_loc */ \
798 + 1 /* .debug_str_offsets */ \
799 + 1 /* .debug_macro or .debug_macinfo */)
800 int section_ids[MAX_NR_V2_DWO_SECTIONS];
801 const gdb_byte *offsets;
802 const gdb_byte *sizes;
803 } v2;
804 } section_pool;
80626a55
DE
805};
806
807/* Data for one DWP file. */
808
809struct dwp_file
810{
400174b1
TT
811 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
812 : name (name_),
813 dbfd (std::move (abfd))
814 {
815 }
816
80626a55
DE
817 /* Name of the file. */
818 const char *name;
819
73869dc2 820 /* File format version. */
400174b1 821 int version = 0;
73869dc2 822
93417882 823 /* The bfd. */
400174b1 824 gdb_bfd_ref_ptr dbfd;
80626a55
DE
825
826 /* Section info for this file. */
400174b1 827 struct dwp_sections sections {};
80626a55 828
57d63ce2 829 /* Table of CUs in the file. */
400174b1 830 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
831
832 /* Table of TUs in the file. */
400174b1 833 const struct dwp_hash_table *tus = nullptr;
80626a55 834
19ac8c2e 835 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
836 htab_up loaded_cus;
837 htab_up loaded_tus;
80626a55 838
73869dc2
DE
839 /* Table to map ELF section numbers to their sections.
840 This is only needed for the DWP V1 file format. */
400174b1
TT
841 unsigned int num_sections = 0;
842 asection **elf_sections = nullptr;
80626a55
DE
843};
844
0963b4bd
MS
845/* Struct used to pass misc. parameters to read_die_and_children, et
846 al. which are used for both .debug_info and .debug_types dies.
847 All parameters here are unchanging for the life of the call. This
dee91e82 848 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
849
850struct die_reader_specs
851{
a32a8923 852 /* The bfd of die_section. */
93311388
DE
853 bfd* abfd;
854
855 /* The CU of the DIE we are parsing. */
856 struct dwarf2_cu *cu;
857
80626a55 858 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
859 struct dwo_file *dwo_file;
860
dee91e82 861 /* The section the die comes from.
3019eac3 862 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
863 struct dwarf2_section_info *die_section;
864
865 /* die_section->buffer. */
d521ce57 866 const gdb_byte *buffer;
f664829e
DE
867
868 /* The end of the buffer. */
869 const gdb_byte *buffer_end;
a2ce51a0 870
685af9cd
TT
871 /* The abbreviation table to use when reading the DIEs. */
872 struct abbrev_table *abbrev_table;
93311388
DE
873};
874
c0ab21c2
TT
875/* A subclass of die_reader_specs that holds storage and has complex
876 constructor and destructor behavior. */
877
878class cutu_reader : public die_reader_specs
879{
880public:
881
882 cutu_reader (struct dwarf2_per_cu_data *this_cu,
883 struct abbrev_table *abbrev_table,
6751ebae 884 int use_existing_cu,
c0ab21c2
TT
885 bool skip_partial);
886
887 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
888 struct dwarf2_cu *parent_cu = nullptr,
889 struct dwo_file *dwo_file = nullptr);
890
c0ab21c2
TT
891 DISABLE_COPY_AND_ASSIGN (cutu_reader);
892
893 const gdb_byte *info_ptr = nullptr;
894 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
895 bool dummy_p = false;
896
6751ebae
TT
897 /* Release the new CU, putting it on the chain. This cannot be done
898 for dummy CUs. */
899 void keep ();
900
c0ab21c2
TT
901private:
902 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 903 int use_existing_cu);
c0ab21c2
TT
904
905 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
906 std::unique_ptr<dwarf2_cu> m_new_cu;
907
908 /* The ordinary abbreviation table. */
909 abbrev_table_up m_abbrev_table_holder;
910
911 /* The DWO abbreviation table. */
912 abbrev_table_up m_dwo_abbrev_table;
913};
dee91e82 914
c906108c 915/* When we construct a partial symbol table entry we only
0963b4bd 916 need this much information. */
6f06d47b 917struct partial_die_info : public allocate_on_obstack
c906108c 918 {
6f06d47b
YQ
919 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
920
921 /* Disable assign but still keep copy ctor, which is needed
922 load_partial_dies. */
923 partial_die_info& operator=(const partial_die_info& rhs) = delete;
924
52356b79
YQ
925 /* Adjust the partial die before generating a symbol for it. This
926 function may set the is_external flag or change the DIE's
927 name. */
928 void fixup (struct dwarf2_cu *cu);
929
48fbe735
YQ
930 /* Read a minimal amount of information into the minimal die
931 structure. */
932 const gdb_byte *read (const struct die_reader_specs *reader,
933 const struct abbrev_info &abbrev,
934 const gdb_byte *info_ptr);
935
72bf9492 936 /* Offset of this DIE. */
6f06d47b 937 const sect_offset sect_off;
72bf9492
DJ
938
939 /* DWARF-2 tag for this DIE. */
6f06d47b 940 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 941
72bf9492 942 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
943 const unsigned int has_children : 1;
944
72bf9492
DJ
945 unsigned int is_external : 1;
946 unsigned int is_declaration : 1;
947 unsigned int has_type : 1;
948 unsigned int has_specification : 1;
949 unsigned int has_pc_info : 1;
481860b3 950 unsigned int may_be_inlined : 1;
72bf9492 951
0c1b455e
TT
952 /* This DIE has been marked DW_AT_main_subprogram. */
953 unsigned int main_subprogram : 1;
954
72bf9492
DJ
955 /* Flag set if the SCOPE field of this structure has been
956 computed. */
957 unsigned int scope_set : 1;
958
fa4028e9
JB
959 /* Flag set if the DIE has a byte_size attribute. */
960 unsigned int has_byte_size : 1;
961
ff908ebf
AW
962 /* Flag set if the DIE has a DW_AT_const_value attribute. */
963 unsigned int has_const_value : 1;
964
98bfdba5
PA
965 /* Flag set if any of the DIE's children are template arguments. */
966 unsigned int has_template_arguments : 1;
967
52356b79 968 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
969 unsigned int fixup_called : 1;
970
36586728
TT
971 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
972 unsigned int is_dwz : 1;
973
974 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
975 unsigned int spec_is_dwz : 1;
976
72bf9492 977 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 978 sometimes a default name for unnamed DIEs. */
6f06d47b 979 const char *name = nullptr;
72bf9492 980
abc72ce4 981 /* The linkage name, if present. */
6f06d47b 982 const char *linkage_name = nullptr;
abc72ce4 983
72bf9492
DJ
984 /* The scope to prepend to our children. This is generally
985 allocated on the comp_unit_obstack, so will disappear
986 when this compilation unit leaves the cache. */
6f06d47b 987 const char *scope = nullptr;
72bf9492 988
95554aad
TT
989 /* Some data associated with the partial DIE. The tag determines
990 which field is live. */
991 union
992 {
993 /* The location description associated with this DIE, if any. */
994 struct dwarf_block *locdesc;
995 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 996 sect_offset sect_off;
6f06d47b 997 } d {};
72bf9492
DJ
998
999 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1000 CORE_ADDR lowpc = 0;
1001 CORE_ADDR highpc = 0;
72bf9492 1002
93311388 1003 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1004 DW_AT_sibling, if any. */
48fbe735
YQ
1005 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1006 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1007 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1008
1009 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1010 DW_AT_specification (or DW_AT_abstract_origin or
1011 DW_AT_extension). */
6f06d47b 1012 sect_offset spec_offset {};
72bf9492
DJ
1013
1014 /* Pointers to this DIE's parent, first child, and next sibling,
1015 if any. */
6f06d47b
YQ
1016 struct partial_die_info *die_parent = nullptr;
1017 struct partial_die_info *die_child = nullptr;
1018 struct partial_die_info *die_sibling = nullptr;
1019
1020 friend struct partial_die_info *
1021 dwarf2_cu::find_partial_die (sect_offset sect_off);
1022
1023 private:
1024 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1025 partial_die_info (sect_offset sect_off)
1026 : partial_die_info (sect_off, DW_TAG_padding, 0)
1027 {
1028 }
1029
1030 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1031 int has_children_)
1032 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1033 {
1034 is_external = 0;
1035 is_declaration = 0;
1036 has_type = 0;
1037 has_specification = 0;
1038 has_pc_info = 0;
1039 may_be_inlined = 0;
1040 main_subprogram = 0;
1041 scope_set = 0;
1042 has_byte_size = 0;
1043 has_const_value = 0;
1044 has_template_arguments = 0;
1045 fixup_called = 0;
1046 is_dwz = 0;
1047 spec_is_dwz = 0;
1048 }
c906108c
SS
1049 };
1050
0963b4bd 1051/* This data structure holds a complete die structure. */
c906108c
SS
1052struct die_info
1053 {
76815b17
DE
1054 /* DWARF-2 tag for this DIE. */
1055 ENUM_BITFIELD(dwarf_tag) tag : 16;
1056
1057 /* Number of attributes */
98bfdba5
PA
1058 unsigned char num_attrs;
1059
1060 /* True if we're presently building the full type name for the
1061 type derived from this DIE. */
1062 unsigned char building_fullname : 1;
76815b17 1063
adde2bff
DE
1064 /* True if this die is in process. PR 16581. */
1065 unsigned char in_process : 1;
1066
3e225074
TT
1067 /* True if this DIE has children. */
1068 unsigned char has_children : 1;
1069
76815b17
DE
1070 /* Abbrev number */
1071 unsigned int abbrev;
1072
93311388 1073 /* Offset in .debug_info or .debug_types section. */
9c541725 1074 sect_offset sect_off;
78ba4af6
JB
1075
1076 /* The dies in a compilation unit form an n-ary tree. PARENT
1077 points to this die's parent; CHILD points to the first child of
1078 this node; and all the children of a given node are chained
4950bc1c 1079 together via their SIBLING fields. */
639d11d3
DC
1080 struct die_info *child; /* Its first child, if any. */
1081 struct die_info *sibling; /* Its next sibling, if any. */
1082 struct die_info *parent; /* Its parent, if any. */
c906108c 1083
b60c80d6
DJ
1084 /* An array of attributes, with NUM_ATTRS elements. There may be
1085 zero, but it's not common and zero-sized arrays are not
1086 sufficiently portable C. */
1087 struct attribute attrs[1];
c906108c
SS
1088 };
1089
c906108c
SS
1090/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1091 but this would require a corresponding change in unpack_field_as_long
1092 and friends. */
1093static int bits_per_byte = 8;
1094
2ddeaf8a
TT
1095/* When reading a variant or variant part, we track a bit more
1096 information about the field, and store it in an object of this
1097 type. */
1098
1099struct variant_field
1100{
1101 /* If we see a DW_TAG_variant, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value;
1104 /* If we see a DW_TAG_variant, then this will be set if this is the
1105 default branch. */
1106 bool default_branch;
1107 /* While reading a DW_TAG_variant_part, this will be set if this
1108 field is the discriminant. */
1109 bool is_discriminant;
1110};
1111
52059ffd
TT
1112struct nextfield
1113{
be2daae6
TT
1114 int accessibility = 0;
1115 int virtuality = 0;
2ddeaf8a 1116 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1117 struct variant_field variant {};
1118 struct field field {};
52059ffd
TT
1119};
1120
1121struct fnfieldlist
1122{
be2daae6
TT
1123 const char *name = nullptr;
1124 std::vector<struct fn_field> fnfields;
52059ffd
TT
1125};
1126
c906108c
SS
1127/* The routines that read and process dies for a C struct or C++ class
1128 pass lists of data member fields and lists of member function fields
1129 in an instance of a field_info structure, as defined below. */
1130struct field_info
c5aa993b 1131 {
0963b4bd 1132 /* List of data member and baseclasses fields. */
be2daae6
TT
1133 std::vector<struct nextfield> fields;
1134 std::vector<struct nextfield> baseclasses;
c906108c 1135
7d0ccb61 1136 /* Number of fields (including baseclasses). */
be2daae6 1137 int nfields = 0;
c906108c 1138
85102364 1139 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1140 int non_public_fields = 0;
c906108c 1141
c5aa993b
JM
1142 /* Member function fieldlist array, contains name of possibly overloaded
1143 member function, number of overloaded member functions and a pointer
1144 to the head of the member function field chain. */
be2daae6 1145 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1146
1147 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1148 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1149 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1150
1151 /* Nested types defined by this class and the number of elements in this
1152 list. */
be2daae6 1153 std::vector<struct decl_field> nested_types_list;
c5aa993b 1154 };
c906108c 1155
ae038cb0
DJ
1156/* Loaded secondary compilation units are kept in memory until they
1157 have not been referenced for the processing of this many
1158 compilation units. Set this to zero to disable caching. Cache
1159 sizes of up to at least twenty will improve startup time for
1160 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1161static int dwarf_max_cache_age = 5;
920d2a44 1162static void
b4f54984
DE
1163show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1164 struct cmd_list_element *c, const char *value)
920d2a44 1165{
3e43a32a 1166 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1167 "DWARF compilation units is %s.\n"),
920d2a44
AC
1168 value);
1169}
4390d890 1170\f
c906108c
SS
1171/* local function prototypes */
1172
918dd910
JK
1173static void dwarf2_find_base_address (struct die_info *die,
1174 struct dwarf2_cu *cu);
1175
891813be 1176static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1177 (struct dwarf2_per_cu_data *per_cu, const char *name);
1178
f1902523
JK
1179static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1180 const gdb_byte *info_ptr,
3e225074 1181 struct die_info *type_unit_die);
f1902523 1182
ed2dc618
SM
1183static void dwarf2_build_psymtabs_hard
1184 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1185
72bf9492
DJ
1186static void scan_partial_symbols (struct partial_die_info *,
1187 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1188 int, struct dwarf2_cu *);
c906108c 1189
72bf9492
DJ
1190static void add_partial_symbol (struct partial_die_info *,
1191 struct dwarf2_cu *);
63d06c5c 1192
72bf9492
DJ
1193static void add_partial_namespace (struct partial_die_info *pdi,
1194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1195 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1196
5d7cb8df 1197static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1198 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1199 struct dwarf2_cu *cu);
1200
72bf9492
DJ
1201static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1202 struct dwarf2_cu *cu);
91c24f0a 1203
bc30ff58
JB
1204static void add_partial_subprogram (struct partial_die_info *pdi,
1205 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1206 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1207
d521ce57 1208static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1209
dee91e82 1210static struct partial_die_info *load_partial_dies
d521ce57 1211 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1212
fb816e8b
TV
1213/* A pair of partial_die_info and compilation unit. */
1214struct cu_partial_die_info
1215{
1216 /* The compilation unit of the partial_die_info. */
1217 struct dwarf2_cu *cu;
1218 /* A partial_die_info. */
1219 struct partial_die_info *pdi;
122cf0f2
AB
1220
1221 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1222 : cu (cu),
1223 pdi (pdi)
405feb71 1224 { /* Nothing. */ }
122cf0f2
AB
1225
1226private:
1227 cu_partial_die_info () = delete;
fb816e8b
TV
1228};
1229
122cf0f2
AB
1230static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1231 struct dwarf2_cu *);
72bf9492 1232
d521ce57
TT
1233static const gdb_byte *read_attribute (const struct die_reader_specs *,
1234 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1235 const gdb_byte *, bool *need_reprocess);
1236
1237static void read_attribute_reprocess (const struct die_reader_specs *reader,
1238 struct attribute *attr);
1239
1240static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1241
c764a876 1242static LONGEST read_checked_initial_length_and_offset
d521ce57 1243 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1244 unsigned int *, unsigned int *);
613e1657 1245
ed2dc618
SM
1246static sect_offset read_abbrev_offset
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1248 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1249
ed2dc618
SM
1250static const char *read_indirect_string
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1252 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1253
ed2dc618
SM
1254static const char *read_indirect_line_string
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1256 const struct comp_unit_head *, unsigned int *);
36586728 1257
ed2dc618
SM
1258static const char *read_indirect_string_at_offset
1259 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1260 LONGEST str_offset);
927aa2e7 1261
ed2dc618
SM
1262static const char *read_indirect_string_from_dwz
1263 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1264
d521ce57
TT
1265static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1266 const gdb_byte *,
3019eac3
DE
1267 unsigned int *);
1268
18a8505e
AT
1269static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1270 ULONGEST str_index);
1271
1272static const char *read_stub_str_index (struct dwarf2_cu *cu,
1273 ULONGEST str_index);
3019eac3 1274
e142c38c 1275static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1276
e142c38c
DJ
1277static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1278 struct dwarf2_cu *);
c906108c 1279
348e048f 1280static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1281 unsigned int);
348e048f 1282
7d45c7c3
KB
1283static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1284 struct dwarf2_cu *cu);
1285
a084a2a6
AT
1286static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1287
05cf31d1
JB
1288static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1289 struct dwarf2_cu *cu);
1290
e142c38c 1291static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1292
e142c38c 1293static struct die_info *die_specification (struct die_info *die,
f2f0e013 1294 struct dwarf2_cu **);
63d06c5c 1295
9c541725 1296static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1297 struct dwarf2_cu *cu);
debd256d 1298
f3f5162e 1299static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1300 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1301 CORE_ADDR, int decode_mapping);
c906108c 1302
804d2729
TT
1303static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1304 const char *);
c906108c 1305
a14ed312 1306static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1307 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1308
ff39bb5e 1309static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1310 struct dwarf2_cu *);
c906108c 1311
ff39bb5e 1312static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1313 struct type *type,
1314 const char *name,
1315 struct obstack *obstack,
12df843f 1316 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1317 const gdb_byte **bytes,
98bfdba5 1318 struct dwarf2_locexpr_baton **baton);
2df3850c 1319
e7c27a73 1320static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1321
b4ba55a1
JB
1322static int need_gnat_info (struct dwarf2_cu *);
1323
3e43a32a
MS
1324static struct type *die_descriptive_type (struct die_info *,
1325 struct dwarf2_cu *);
b4ba55a1
JB
1326
1327static void set_descriptive_type (struct type *, struct die_info *,
1328 struct dwarf2_cu *);
1329
e7c27a73
DJ
1330static struct type *die_containing_type (struct die_info *,
1331 struct dwarf2_cu *);
c906108c 1332
ff39bb5e 1333static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1334 struct dwarf2_cu *);
c906108c 1335
f792889a 1336static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1337
673bfd45
DE
1338static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1339
0d5cff50 1340static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1341
6e70227d 1342static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1343 const char *suffix, int physname,
1344 struct dwarf2_cu *cu);
63d06c5c 1345
e7c27a73 1346static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1347
348e048f
DE
1348static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1349
e7c27a73 1350static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1351
e7c27a73 1352static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1353
96408a79
SA
1354static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1355
71a3c369
TT
1356static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1357
ff013f42 1358static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1359 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1360
3a2b436a 1361/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1362 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1363enum pc_bounds_kind
1364{
e385593e 1365 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1366 PC_BOUNDS_NOT_PRESENT,
1367
e385593e
JK
1368 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1369 were present but they do not form a valid range of PC addresses. */
1370 PC_BOUNDS_INVALID,
1371
3a2b436a
JK
1372 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1373 PC_BOUNDS_RANGES,
1374
1375 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1376 PC_BOUNDS_HIGH_LOW,
1377};
1378
1379static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1380 CORE_ADDR *, CORE_ADDR *,
1381 struct dwarf2_cu *,
891813be 1382 dwarf2_psymtab *);
c906108c 1383
fae299cd
DC
1384static void get_scope_pc_bounds (struct die_info *,
1385 CORE_ADDR *, CORE_ADDR *,
1386 struct dwarf2_cu *);
1387
801e3a5b
JB
1388static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1389 CORE_ADDR, struct dwarf2_cu *);
1390
a14ed312 1391static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1392 struct dwarf2_cu *);
c906108c 1393
a14ed312 1394static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1395 struct type *, struct dwarf2_cu *);
c906108c 1396
a14ed312 1397static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1398 struct die_info *, struct type *,
e7c27a73 1399 struct dwarf2_cu *);
c906108c 1400
a14ed312 1401static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1402 struct type *,
1403 struct dwarf2_cu *);
c906108c 1404
134d01f1 1405static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1406
e7c27a73 1407static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1408
e7c27a73 1409static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1410
5d7cb8df
JK
1411static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1412
804d2729 1413static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1414
27aa8d6a
SW
1415static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1416
74921315
KS
1417static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1418
f55ee35c
JK
1419static struct type *read_module_type (struct die_info *die,
1420 struct dwarf2_cu *cu);
1421
38d518c9 1422static const char *namespace_name (struct die_info *die,
e142c38c 1423 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1424
134d01f1 1425static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1426
e7c27a73 1427static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1428
6e70227d 1429static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1430 struct dwarf2_cu *);
1431
bf6af496 1432static struct die_info *read_die_and_siblings_1
d521ce57 1433 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1434 struct die_info *);
639d11d3 1435
dee91e82 1436static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1437 const gdb_byte *info_ptr,
1438 const gdb_byte **new_info_ptr,
639d11d3
DC
1439 struct die_info *parent);
1440
d521ce57
TT
1441static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1442 struct die_info **, const gdb_byte *,
3e225074 1443 int);
3019eac3 1444
d521ce57 1445static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1446 struct die_info **, const gdb_byte *);
93311388 1447
e7c27a73 1448static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1449
15d034d0
TT
1450static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1451 struct obstack *);
71c25dea 1452
15d034d0 1453static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1454
15d034d0 1455static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1456 struct die_info *die,
1457 struct dwarf2_cu *cu);
1458
ca69b9e6
DE
1459static const char *dwarf2_physname (const char *name, struct die_info *die,
1460 struct dwarf2_cu *cu);
1461
e142c38c 1462static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1463 struct dwarf2_cu **);
9219021c 1464
f39c6ffd 1465static const char *dwarf_tag_name (unsigned int);
c906108c 1466
f39c6ffd 1467static const char *dwarf_attr_name (unsigned int);
c906108c 1468
f39c6ffd 1469static const char *dwarf_form_name (unsigned int);
c906108c 1470
a121b7c1 1471static const char *dwarf_bool_name (unsigned int);
c906108c 1472
f39c6ffd 1473static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1474
f9aca02d 1475static struct die_info *sibling_die (struct die_info *);
c906108c 1476
d97bc12b
DE
1477static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479static void dump_die_for_error (struct die_info *);
1480
1481static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
c906108c 1483
d97bc12b 1484/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1485
51545339 1486static void store_in_ref_table (struct die_info *,
10b3939b 1487 struct dwarf2_cu *);
c906108c 1488
ff39bb5e 1489static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1490
ff39bb5e 1491static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1492
348e048f 1493static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1494 const struct attribute *,
348e048f
DE
1495 struct dwarf2_cu **);
1496
10b3939b 1497static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1498 const struct attribute *,
f2f0e013 1499 struct dwarf2_cu **);
c906108c 1500
348e048f 1501static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1502 const struct attribute *,
348e048f
DE
1503 struct dwarf2_cu **);
1504
ac9ec31b
DE
1505static struct type *get_signatured_type (struct die_info *, ULONGEST,
1506 struct dwarf2_cu *);
1507
1508static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1509 const struct attribute *,
ac9ec31b
DE
1510 struct dwarf2_cu *);
1511
e5fe5e75 1512static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1513
52dc124a 1514static void read_signatured_type (struct signatured_type *);
348e048f 1515
63e43d3a
PMR
1516static int attr_to_dynamic_prop (const struct attribute *attr,
1517 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1518 struct dynamic_prop *prop, struct type *type);
63e43d3a 1519
c906108c
SS
1520/* memory allocation interface */
1521
7b5a2f43 1522static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1523
b60c80d6 1524static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1525
43f3e411 1526static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1527
8cf6f0b1
TT
1528static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1529 struct dwarf2_loclist_baton *baton,
ff39bb5e 1530 const struct attribute *attr);
8cf6f0b1 1531
ff39bb5e 1532static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1533 struct symbol *sym,
f1e6e072
TT
1534 struct dwarf2_cu *cu,
1535 int is_block);
4c2df51b 1536
d521ce57
TT
1537static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1538 const gdb_byte *info_ptr,
1539 struct abbrev_info *abbrev);
4bb7a0a7 1540
72bf9492
DJ
1541static hashval_t partial_die_hash (const void *item);
1542
1543static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1544
ae038cb0 1545static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1546 (sect_offset sect_off, unsigned int offset_in_dwz,
1547 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1548
9816fde3 1549static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1550 struct die_info *comp_unit_die,
1551 enum language pretend_language);
93311388 1552
ed2dc618 1553static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1554
dee91e82 1555static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1556
f792889a
DJ
1557static struct type *set_die_type (struct die_info *, struct type *,
1558 struct dwarf2_cu *);
1c379e20 1559
ed2dc618 1560static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1561
ed2dc618 1562static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1563
58f0c718 1564static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1565 enum language);
10b3939b 1566
95554aad
TT
1567static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1568 enum language);
10b3939b 1569
f4dc4d17
DE
1570static void process_full_type_unit (struct dwarf2_per_cu_data *,
1571 enum language);
1572
10b3939b
DJ
1573static void dwarf2_add_dependence (struct dwarf2_cu *,
1574 struct dwarf2_per_cu_data *);
1575
ae038cb0
DJ
1576static void dwarf2_mark (struct dwarf2_cu *);
1577
1578static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1579
b64f50a1 1580static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1581 struct dwarf2_per_cu_data *);
673bfd45 1582
f792889a 1583static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1584
95554aad
TT
1585static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1586 enum language pretend_language);
1587
ed2dc618 1588static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1589
b303c6f6
AB
1590/* Class, the destructor of which frees all allocated queue entries. This
1591 will only have work to do if an error was thrown while processing the
1592 dwarf. If no error was thrown then the queue entries should have all
1593 been processed, and freed, as we went along. */
1594
1595class dwarf2_queue_guard
1596{
1597public:
39856def
TT
1598 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1599 : m_per_objfile (per_objfile)
1600 {
1601 }
b303c6f6
AB
1602
1603 /* Free any entries remaining on the queue. There should only be
1604 entries left if we hit an error while processing the dwarf. */
1605 ~dwarf2_queue_guard ()
1606 {
39856def
TT
1607 /* Ensure that no memory is allocated by the queue. */
1608 std::queue<dwarf2_queue_item> empty;
1609 std::swap (m_per_objfile->queue, empty);
1610 }
b303c6f6 1611
39856def 1612 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1613
39856def
TT
1614private:
1615 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1616};
1617
39856def
TT
1618dwarf2_queue_item::~dwarf2_queue_item ()
1619{
1620 /* Anything still marked queued is likely to be in an
1621 inconsistent state, so discard it. */
1622 if (per_cu->queued)
1623 {
1624 if (per_cu->cu != NULL)
1625 free_one_cached_comp_unit (per_cu);
1626 per_cu->queued = 0;
1627 }
1628}
1629
d721ba37
PA
1630/* The return type of find_file_and_directory. Note, the enclosed
1631 string pointers are only valid while this object is valid. */
1632
1633struct file_and_directory
1634{
1635 /* The filename. This is never NULL. */
1636 const char *name;
1637
1638 /* The compilation directory. NULL if not known. If we needed to
1639 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1640 points directly to the DW_AT_comp_dir string attribute owned by
1641 the obstack that owns the DIE. */
1642 const char *comp_dir;
1643
1644 /* If we needed to build a new string for comp_dir, this is what
1645 owns the storage. */
1646 std::string comp_dir_storage;
1647};
1648
1649static file_and_directory find_file_and_directory (struct die_info *die,
1650 struct dwarf2_cu *cu);
9291a0cd 1651
b0b6a987 1652static htab_up allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1653
b0b6a987 1654static htab_up allocate_dwo_unit_table (struct objfile *objfile);
3019eac3 1655
57d63ce2 1656static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1657 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1658 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1659 ULONGEST signature, int is_debug_types);
a2ce51a0 1660
ed2dc618
SM
1661static struct dwp_file *get_dwp_file
1662 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1663
3019eac3 1664static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1665 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1666
1667static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1668 (struct signatured_type *, const char *, const char *);
3019eac3 1669
89e63ee4
DE
1670static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1671
263db9a1
TT
1672/* A unique pointer to a dwo_file. */
1673
51ac9db5 1674typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1675
ed2dc618 1676static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1677
1b80a9fa 1678static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1679
1680static void free_line_header_voidp (void *arg);
4390d890
DE
1681\f
1682/* Various complaints about symbol reading that don't abort the process. */
1683
1684static void
1685dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1686{
b98664d3 1687 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1688}
1689
1690static void
1691dwarf2_debug_line_missing_file_complaint (void)
1692{
b98664d3 1693 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1694}
1695
1696static void
1697dwarf2_debug_line_missing_end_sequence_complaint (void)
1698{
b98664d3 1699 complaint (_(".debug_line section has line "
4390d890
DE
1700 "program sequence without an end"));
1701}
1702
1703static void
1704dwarf2_complex_location_expr_complaint (void)
1705{
b98664d3 1706 complaint (_("location expression too complex"));
4390d890
DE
1707}
1708
1709static void
1710dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1711 int arg3)
1712{
b98664d3 1713 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1714 arg1, arg2, arg3);
1715}
1716
1717static void
1718dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1719{
b98664d3 1720 complaint (_("debug info runs off end of %s section"
4390d890 1721 " [in module %s]"),
96b79293
TT
1722 section->get_name (),
1723 section->get_file_name ());
4390d890 1724}
1b80a9fa 1725
4390d890
DE
1726static void
1727dwarf2_macro_malformed_definition_complaint (const char *arg1)
1728{
b98664d3 1729 complaint (_("macro debug info contains a "
4390d890
DE
1730 "malformed macro definition:\n`%s'"),
1731 arg1);
1732}
1733
1734static void
1735dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1736{
b98664d3 1737 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1738 arg1, arg2);
1739}
527f3840
JK
1740
1741/* Hash function for line_header_hash. */
1742
1743static hashval_t
1744line_header_hash (const struct line_header *ofs)
1745{
9c541725 1746 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1747}
1748
1749/* Hash function for htab_create_alloc_ex for line_header_hash. */
1750
1751static hashval_t
1752line_header_hash_voidp (const void *item)
1753{
9a3c8263 1754 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1755
1756 return line_header_hash (ofs);
1757}
1758
1759/* Equality function for line_header_hash. */
1760
1761static int
1762line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1763{
9a3c8263
SM
1764 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1765 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1766
9c541725 1767 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1768 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1769}
1770
4390d890 1771\f
9291a0cd 1772
330cdd98
PA
1773/* See declaration. */
1774
1775dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1776 const dwarf2_debug_sections *names,
1777 bool can_copy_)
1778 : objfile (objfile_),
1779 can_copy (can_copy_)
330cdd98
PA
1780{
1781 if (names == NULL)
1782 names = &dwarf2_elf_names;
1783
1784 bfd *obfd = objfile->obfd;
1785
1786 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1787 locate_sections (obfd, sec, *names);
1788}
1789
1790dwarf2_per_objfile::~dwarf2_per_objfile ()
1791{
1792 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1793 free_cached_comp_units ();
1794
b76e467d 1795 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1796 per_cu->imported_symtabs_free ();
fc8e7e75 1797
b2bdb8cf 1798 for (signatured_type *sig_type : all_type_units)
ae640021 1799 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1800
330cdd98
PA
1801 /* Everything else should be on the objfile obstack. */
1802}
1803
1804/* See declaration. */
1805
1806void
1807dwarf2_per_objfile::free_cached_comp_units ()
1808{
1809 dwarf2_per_cu_data *per_cu = read_in_chain;
1810 dwarf2_per_cu_data **last_chain = &read_in_chain;
1811 while (per_cu != NULL)
1812 {
1813 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1814
fcd3b13d 1815 delete per_cu->cu;
330cdd98
PA
1816 *last_chain = next_cu;
1817 per_cu = next_cu;
1818 }
1819}
1820
11ed8cad
TT
1821/* A helper class that calls free_cached_comp_units on
1822 destruction. */
1823
1824class free_cached_comp_units
1825{
1826public:
1827
1828 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1829 : m_per_objfile (per_objfile)
1830 {
1831 }
1832
1833 ~free_cached_comp_units ()
1834 {
1835 m_per_objfile->free_cached_comp_units ();
1836 }
1837
1838 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1839
1840private:
1841
1842 dwarf2_per_objfile *m_per_objfile;
1843};
1844
c906108c 1845/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1846 information and return true if we have enough to do something.
1847 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1848 ELF names are used. CAN_COPY is true for formats where symbol
1849 interposition is possible and so symbol values must follow copy
1850 relocation rules. */
c906108c
SS
1851
1852int
251d32d9 1853dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1854 const struct dwarf2_debug_sections *names,
1855 bool can_copy)
c906108c 1856{
97cbe998
SDJ
1857 if (objfile->flags & OBJF_READNEVER)
1858 return 0;
1859
ed2dc618
SM
1860 struct dwarf2_per_objfile *dwarf2_per_objfile
1861 = get_dwarf2_per_objfile (objfile);
1862
1863 if (dwarf2_per_objfile == NULL)
5bfd760d 1864 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1865 names,
1866 can_copy);
5bfd760d 1867
73869dc2 1868 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1869 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1870 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1871 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1872}
1873
251d32d9
TG
1874/* When loading sections, we look either for uncompressed section or for
1875 compressed section names. */
233a11ab
CS
1876
1877static int
251d32d9
TG
1878section_is_p (const char *section_name,
1879 const struct dwarf2_section_names *names)
233a11ab 1880{
251d32d9
TG
1881 if (names->normal != NULL
1882 && strcmp (section_name, names->normal) == 0)
1883 return 1;
1884 if (names->compressed != NULL
1885 && strcmp (section_name, names->compressed) == 0)
1886 return 1;
1887 return 0;
233a11ab
CS
1888}
1889
330cdd98 1890/* See declaration. */
c906108c 1891
330cdd98
PA
1892void
1893dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1894 const dwarf2_debug_sections &names)
c906108c 1895{
fd361982 1896 flagword aflag = bfd_section_flags (sectp);
251d32d9 1897
dc7650b8
JK
1898 if ((aflag & SEC_HAS_CONTENTS) == 0)
1899 {
1900 }
950b7495
KS
1901 else if (elf_section_data (sectp)->this_hdr.sh_size
1902 > bfd_get_file_size (abfd))
1903 {
1904 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1905 warning (_("Discarding section %s which has a section size (%s"
1906 ") larger than the file size [in module %s]"),
1907 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1908 bfd_get_filename (abfd));
1909 }
330cdd98 1910 else if (section_is_p (sectp->name, &names.info))
c906108c 1911 {
330cdd98 1912 this->info.s.section = sectp;
fd361982 1913 this->info.size = bfd_section_size (sectp);
c906108c 1914 }
330cdd98 1915 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1916 {
330cdd98 1917 this->abbrev.s.section = sectp;
fd361982 1918 this->abbrev.size = bfd_section_size (sectp);
c906108c 1919 }
330cdd98 1920 else if (section_is_p (sectp->name, &names.line))
c906108c 1921 {
330cdd98 1922 this->line.s.section = sectp;
fd361982 1923 this->line.size = bfd_section_size (sectp);
c906108c 1924 }
330cdd98 1925 else if (section_is_p (sectp->name, &names.loc))
c906108c 1926 {
330cdd98 1927 this->loc.s.section = sectp;
fd361982 1928 this->loc.size = bfd_section_size (sectp);
c906108c 1929 }
330cdd98 1930 else if (section_is_p (sectp->name, &names.loclists))
43988095 1931 {
330cdd98 1932 this->loclists.s.section = sectp;
fd361982 1933 this->loclists.size = bfd_section_size (sectp);
43988095 1934 }
330cdd98 1935 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1936 {
330cdd98 1937 this->macinfo.s.section = sectp;
fd361982 1938 this->macinfo.size = bfd_section_size (sectp);
c906108c 1939 }
330cdd98 1940 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1941 {
330cdd98 1942 this->macro.s.section = sectp;
fd361982 1943 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1944 }
330cdd98 1945 else if (section_is_p (sectp->name, &names.str))
c906108c 1946 {
330cdd98 1947 this->str.s.section = sectp;
fd361982 1948 this->str.size = bfd_section_size (sectp);
c906108c 1949 }
18a8505e
AT
1950 else if (section_is_p (sectp->name, &names.str_offsets))
1951 {
1952 this->str_offsets.s.section = sectp;
1953 this->str_offsets.size = bfd_section_size (sectp);
1954 }
330cdd98 1955 else if (section_is_p (sectp->name, &names.line_str))
43988095 1956 {
330cdd98 1957 this->line_str.s.section = sectp;
fd361982 1958 this->line_str.size = bfd_section_size (sectp);
43988095 1959 }
330cdd98 1960 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1961 {
330cdd98 1962 this->addr.s.section = sectp;
fd361982 1963 this->addr.size = bfd_section_size (sectp);
3019eac3 1964 }
330cdd98 1965 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1966 {
330cdd98 1967 this->frame.s.section = sectp;
fd361982 1968 this->frame.size = bfd_section_size (sectp);
b6af0555 1969 }
330cdd98 1970 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1971 {
330cdd98 1972 this->eh_frame.s.section = sectp;
fd361982 1973 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1974 }
330cdd98 1975 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1976 {
330cdd98 1977 this->ranges.s.section = sectp;
fd361982 1978 this->ranges.size = bfd_section_size (sectp);
af34e669 1979 }
330cdd98 1980 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1981 {
330cdd98 1982 this->rnglists.s.section = sectp;
fd361982 1983 this->rnglists.size = bfd_section_size (sectp);
43988095 1984 }
330cdd98 1985 else if (section_is_p (sectp->name, &names.types))
348e048f 1986 {
8b70b953
TT
1987 struct dwarf2_section_info type_section;
1988
1989 memset (&type_section, 0, sizeof (type_section));
049412e3 1990 type_section.s.section = sectp;
fd361982 1991 type_section.size = bfd_section_size (sectp);
8b70b953 1992
fd5866f6 1993 this->types.push_back (type_section);
348e048f 1994 }
330cdd98 1995 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1996 {
330cdd98 1997 this->gdb_index.s.section = sectp;
fd361982 1998 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1999 }
927aa2e7
JK
2000 else if (section_is_p (sectp->name, &names.debug_names))
2001 {
2002 this->debug_names.s.section = sectp;
fd361982 2003 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2004 }
2005 else if (section_is_p (sectp->name, &names.debug_aranges))
2006 {
2007 this->debug_aranges.s.section = sectp;
fd361982 2008 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2009 }
dce234bc 2010
fd361982
AM
2011 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2012 && bfd_section_vma (sectp) == 0)
330cdd98 2013 this->has_section_at_zero = true;
c906108c
SS
2014}
2015
dce234bc 2016/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2017 SECTION_NAME. */
af34e669 2018
dce234bc 2019void
3017a003
TG
2020dwarf2_get_section_info (struct objfile *objfile,
2021 enum dwarf2_section_enum sect,
d521ce57 2022 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2023 bfd_size_type *sizep)
2024{
5bfd760d 2025 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2026 struct dwarf2_section_info *info;
a3b2a86b
TT
2027
2028 /* We may see an objfile without any DWARF, in which case we just
2029 return nothing. */
2030 if (data == NULL)
2031 {
2032 *sectp = NULL;
2033 *bufp = NULL;
2034 *sizep = 0;
2035 return;
2036 }
3017a003
TG
2037 switch (sect)
2038 {
2039 case DWARF2_DEBUG_FRAME:
2040 info = &data->frame;
2041 break;
2042 case DWARF2_EH_FRAME:
2043 info = &data->eh_frame;
2044 break;
2045 default:
2046 gdb_assert_not_reached ("unexpected section");
2047 }
dce234bc 2048
96b79293 2049 info->read (objfile);
dce234bc 2050
96b79293 2051 *sectp = info->get_bfd_section ();
dce234bc
PP
2052 *bufp = info->buffer;
2053 *sizep = info->size;
2054}
2055
36586728
TT
2056/* A helper function to find the sections for a .dwz file. */
2057
2058static void
2059locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2060{
9a3c8263 2061 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2062
2063 /* Note that we only support the standard ELF names, because .dwz
2064 is ELF-only (at the time of writing). */
2065 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2066 {
049412e3 2067 dwz_file->abbrev.s.section = sectp;
fd361982 2068 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2071 {
049412e3 2072 dwz_file->info.s.section = sectp;
fd361982 2073 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2076 {
049412e3 2077 dwz_file->str.s.section = sectp;
fd361982 2078 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2081 {
049412e3 2082 dwz_file->line.s.section = sectp;
fd361982 2083 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2086 {
049412e3 2087 dwz_file->macro.s.section = sectp;
fd361982 2088 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2089 }
2ec9a5e0
TT
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2091 {
049412e3 2092 dwz_file->gdb_index.s.section = sectp;
fd361982 2093 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2094 }
927aa2e7
JK
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2096 {
2097 dwz_file->debug_names.s.section = sectp;
fd361982 2098 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2099 }
36586728
TT
2100}
2101
c4973306 2102/* See dwarf2read.h. */
36586728 2103
c4973306 2104struct dwz_file *
ed2dc618 2105dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2106{
36586728 2107 const char *filename;
acd13123 2108 bfd_size_type buildid_len_arg;
dc294be5
TT
2109 size_t buildid_len;
2110 bfd_byte *buildid;
36586728
TT
2111
2112 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2113 return dwarf2_per_objfile->dwz_file.get ();
36586728 2114
4db1a1dc 2115 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2116 gdb::unique_xmalloc_ptr<char> data
2117 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2118 &buildid_len_arg, &buildid));
4db1a1dc
TT
2119 if (data == NULL)
2120 {
2121 if (bfd_get_error () == bfd_error_no_error)
2122 return NULL;
2123 error (_("could not read '.gnu_debugaltlink' section: %s"),
2124 bfd_errmsg (bfd_get_error ()));
2125 }
791afaa2
TT
2126
2127 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2128
acd13123
TT
2129 buildid_len = (size_t) buildid_len_arg;
2130
791afaa2 2131 filename = data.get ();
d721ba37
PA
2132
2133 std::string abs_storage;
36586728
TT
2134 if (!IS_ABSOLUTE_PATH (filename))
2135 {
14278e1f
TT
2136 gdb::unique_xmalloc_ptr<char> abs
2137 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2138
14278e1f 2139 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2140 filename = abs_storage.c_str ();
36586728
TT
2141 }
2142
dc294be5
TT
2143 /* First try the file name given in the section. If that doesn't
2144 work, try to use the build-id instead. */
192b62ce 2145 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2146 if (dwz_bfd != NULL)
36586728 2147 {
192b62ce 2148 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2149 dwz_bfd.reset (nullptr);
36586728
TT
2150 }
2151
dc294be5
TT
2152 if (dwz_bfd == NULL)
2153 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2154
2155 if (dwz_bfd == NULL)
2156 error (_("could not find '.gnu_debugaltlink' file for %s"),
2157 objfile_name (dwarf2_per_objfile->objfile));
2158
7ff8cb8c
TT
2159 std::unique_ptr<struct dwz_file> result
2160 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2161
7ff8cb8c
TT
2162 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2163 result.get ());
36586728 2164
7ff8cb8c
TT
2165 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2166 result->dwz_bfd.get ());
2167 dwarf2_per_objfile->dwz_file = std::move (result);
2168 return dwarf2_per_objfile->dwz_file.get ();
36586728 2169}
9291a0cd 2170\f
7b9f3c50
DE
2171/* DWARF quick_symbols_functions support. */
2172
2173/* TUs can share .debug_line entries, and there can be a lot more TUs than
2174 unique line tables, so we maintain a separate table of all .debug_line
2175 derived entries to support the sharing.
2176 All the quick functions need is the list of file names. We discard the
2177 line_header when we're done and don't need to record it here. */
2178struct quick_file_names
2179{
094b34ac
DE
2180 /* The data used to construct the hash key. */
2181 struct stmt_list_hash hash;
7b9f3c50
DE
2182
2183 /* The number of entries in file_names, real_names. */
2184 unsigned int num_file_names;
2185
2186 /* The file names from the line table, after being run through
2187 file_full_name. */
2188 const char **file_names;
2189
2190 /* The file names from the line table after being run through
2191 gdb_realpath. These are computed lazily. */
2192 const char **real_names;
2193};
2194
2195/* When using the index (and thus not using psymtabs), each CU has an
2196 object of this type. This is used to hold information needed by
2197 the various "quick" methods. */
2198struct dwarf2_per_cu_quick_data
2199{
2200 /* The file table. This can be NULL if there was no file table
2201 or it's currently not read in.
2202 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2203 struct quick_file_names *file_names;
2204
2205 /* The corresponding symbol table. This is NULL if symbols for this
2206 CU have not yet been read. */
43f3e411 2207 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2208
2209 /* A temporary mark bit used when iterating over all CUs in
2210 expand_symtabs_matching. */
2211 unsigned int mark : 1;
2212
2213 /* True if we've tried to read the file table and found there isn't one.
2214 There will be no point in trying to read it again next time. */
2215 unsigned int no_file_data : 1;
2216};
2217
094b34ac
DE
2218/* Utility hash function for a stmt_list_hash. */
2219
2220static hashval_t
2221hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2222{
2223 hashval_t v = 0;
2224
2225 if (stmt_list_hash->dwo_unit != NULL)
2226 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2227 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2228 return v;
2229}
2230
2231/* Utility equality function for a stmt_list_hash. */
2232
2233static int
2234eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2235 const struct stmt_list_hash *rhs)
2236{
2237 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2238 return 0;
2239 if (lhs->dwo_unit != NULL
2240 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2241 return 0;
2242
9c541725 2243 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2244}
2245
7b9f3c50
DE
2246/* Hash function for a quick_file_names. */
2247
2248static hashval_t
2249hash_file_name_entry (const void *e)
2250{
9a3c8263
SM
2251 const struct quick_file_names *file_data
2252 = (const struct quick_file_names *) e;
7b9f3c50 2253
094b34ac 2254 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2255}
2256
2257/* Equality function for a quick_file_names. */
2258
2259static int
2260eq_file_name_entry (const void *a, const void *b)
2261{
9a3c8263
SM
2262 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2263 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2264
094b34ac 2265 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2266}
2267
2268/* Delete function for a quick_file_names. */
2269
2270static void
2271delete_file_name_entry (void *e)
2272{
9a3c8263 2273 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2274 int i;
2275
2276 for (i = 0; i < file_data->num_file_names; ++i)
2277 {
2278 xfree ((void*) file_data->file_names[i]);
2279 if (file_data->real_names)
2280 xfree ((void*) file_data->real_names[i]);
2281 }
2282
2283 /* The space for the struct itself lives on objfile_obstack,
2284 so we don't free it here. */
2285}
2286
2287/* Create a quick_file_names hash table. */
2288
5895093f 2289static htab_up
7b9f3c50
DE
2290create_quick_file_names_table (unsigned int nr_initial_entries)
2291{
5895093f
TT
2292 return htab_up (htab_create_alloc (nr_initial_entries,
2293 hash_file_name_entry, eq_file_name_entry,
2294 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2295}
9291a0cd 2296
918dd910
JK
2297/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2298 have to be created afterwards. You should call age_cached_comp_units after
2299 processing PER_CU->CU. dw2_setup must have been already called. */
2300
2301static void
58f0c718 2302load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2303{
3019eac3 2304 if (per_cu->is_debug_types)
e5fe5e75 2305 load_full_type_unit (per_cu);
918dd910 2306 else
58f0c718 2307 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2308
cc12ce38
DE
2309 if (per_cu->cu == NULL)
2310 return; /* Dummy CU. */
2dc860c0
DE
2311
2312 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2313}
2314
a0f42c21 2315/* Read in the symbols for PER_CU. */
2fdf6df6 2316
9291a0cd 2317static void
58f0c718 2318dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2319{
ed2dc618 2320 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2321
f4dc4d17
DE
2322 /* Skip type_unit_groups, reading the type units they contain
2323 is handled elsewhere. */
2324 if (IS_TYPE_UNIT_GROUP (per_cu))
2325 return;
2326
b303c6f6
AB
2327 /* The destructor of dwarf2_queue_guard frees any entries left on
2328 the queue. After this point we're guaranteed to leave this function
2329 with the dwarf queue empty. */
39856def 2330 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2331
95554aad 2332 if (dwarf2_per_objfile->using_index
43f3e411 2333 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2334 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2335 {
2336 queue_comp_unit (per_cu, language_minimal);
58f0c718 2337 load_cu (per_cu, skip_partial);
89e63ee4
DE
2338
2339 /* If we just loaded a CU from a DWO, and we're working with an index
2340 that may badly handle TUs, load all the TUs in that DWO as well.
2341 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2342 if (!per_cu->is_debug_types
cc12ce38 2343 && per_cu->cu != NULL
89e63ee4
DE
2344 && per_cu->cu->dwo_unit != NULL
2345 && dwarf2_per_objfile->index_table != NULL
2346 && dwarf2_per_objfile->index_table->version <= 7
2347 /* DWP files aren't supported yet. */
ed2dc618 2348 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2349 queue_and_load_all_dwo_tus (per_cu);
95554aad 2350 }
9291a0cd 2351
ed2dc618 2352 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2353
2354 /* Age the cache, releasing compilation units that have not
2355 been used recently. */
ed2dc618 2356 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2357}
2358
2359/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2360 the objfile from which this CU came. Returns the resulting symbol
2361 table. */
2fdf6df6 2362
43f3e411 2363static struct compunit_symtab *
58f0c718 2364dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2365{
ed2dc618
SM
2366 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2367
95554aad 2368 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2369 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2370 {
11ed8cad 2371 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2372 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2373 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2374 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2375 }
f194fefb 2376
43f3e411 2377 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2378}
2379
ff4c9fec 2380/* See declaration. */
f4dc4d17 2381
ff4c9fec
SM
2382dwarf2_per_cu_data *
2383dwarf2_per_objfile::get_cutu (int index)
2384{
b76e467d 2385 if (index >= this->all_comp_units.size ())
ff4c9fec 2386 {
b76e467d 2387 index -= this->all_comp_units.size ();
b2bdb8cf 2388 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2389 return &this->all_type_units[index]->per_cu;
2390 }
f4dc4d17 2391
ff4c9fec
SM
2392 return this->all_comp_units[index];
2393}
f4dc4d17 2394
ff4c9fec 2395/* See declaration. */
2fdf6df6 2396
ff4c9fec
SM
2397dwarf2_per_cu_data *
2398dwarf2_per_objfile::get_cu (int index)
1fd400ff 2399{
b76e467d 2400 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2401
ff4c9fec 2402 return this->all_comp_units[index];
f4dc4d17
DE
2403}
2404
ff4c9fec 2405/* See declaration. */
f4dc4d17 2406
ff4c9fec
SM
2407signatured_type *
2408dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2409{
b2bdb8cf 2410 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2411
ff4c9fec 2412 return this->all_type_units[index];
1fd400ff
TT
2413}
2414
4b514bc8
JK
2415/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2416 objfile_obstack, and constructed with the specified field
2417 values. */
2418
2419static dwarf2_per_cu_data *
ed2dc618 2420create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2421 struct dwarf2_section_info *section,
2422 int is_dwz,
2423 sect_offset sect_off, ULONGEST length)
2424{
ed2dc618 2425 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2426 dwarf2_per_cu_data *the_cu
2427 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2428 struct dwarf2_per_cu_data);
2429 the_cu->sect_off = sect_off;
2430 the_cu->length = length;
e3b94546 2431 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2432 the_cu->section = section;
2433 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2434 struct dwarf2_per_cu_quick_data);
2435 the_cu->is_dwz = is_dwz;
2436 return the_cu;
2437}
2438
2ec9a5e0
TT
2439/* A helper for create_cus_from_index that handles a given list of
2440 CUs. */
2fdf6df6 2441
74a0d9f6 2442static void
12359b5e 2443create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2444 const gdb_byte *cu_list, offset_type n_elements,
2445 struct dwarf2_section_info *section,
b76e467d 2446 int is_dwz)
9291a0cd 2447{
12359b5e 2448 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2449 {
74a0d9f6 2450 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2451
2452 sect_offset sect_off
2453 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2454 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2455 cu_list += 2 * 8;
2456
b76e467d 2457 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2458 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2459 sect_off, length);
b76e467d 2460 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2461 }
9291a0cd
TT
2462}
2463
2ec9a5e0 2464/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2465 the CU objects for this objfile. */
2ec9a5e0 2466
74a0d9f6 2467static void
12359b5e 2468create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2469 const gdb_byte *cu_list, offset_type cu_list_elements,
2470 const gdb_byte *dwz_list, offset_type dwz_elements)
2471{
b76e467d
SM
2472 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2473 dwarf2_per_objfile->all_comp_units.reserve
2474 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2475
12359b5e 2476 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2477 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2478
2479 if (dwz_elements == 0)
74a0d9f6 2480 return;
2ec9a5e0 2481
12359b5e
SM
2482 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2483 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2484 &dwz->info, 1);
2ec9a5e0
TT
2485}
2486
1fd400ff 2487/* Create the signatured type hash table from the index. */
673bfd45 2488
74a0d9f6 2489static void
12359b5e
SM
2490create_signatured_type_table_from_index
2491 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2492 struct dwarf2_section_info *section,
2493 const gdb_byte *bytes,
2494 offset_type elements)
1fd400ff 2495{
12359b5e 2496 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2497
b2bdb8cf
SM
2498 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2499 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2500
b0b6a987 2501 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 2502
12359b5e 2503 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2504 {
52dc124a 2505 struct signatured_type *sig_type;
9c541725 2506 ULONGEST signature;
1fd400ff 2507 void **slot;
9c541725 2508 cu_offset type_offset_in_tu;
1fd400ff 2509
74a0d9f6 2510 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2511 sect_offset sect_off
2512 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2513 type_offset_in_tu
2514 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2515 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2516 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2517 bytes += 3 * 8;
2518
52dc124a 2519 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2520 struct signatured_type);
52dc124a 2521 sig_type->signature = signature;
9c541725 2522 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2523 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2524 sig_type->per_cu.section = section;
9c541725 2525 sig_type->per_cu.sect_off = sect_off;
e3b94546 2526 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2527 sig_type->per_cu.v.quick
1fd400ff
TT
2528 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2529 struct dwarf2_per_cu_quick_data);
2530
b0b6a987 2531 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2532 *slot = sig_type;
1fd400ff 2533
b2bdb8cf 2534 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2535 }
2536
b0b6a987 2537 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2538}
2539
927aa2e7
JK
2540/* Create the signatured type hash table from .debug_names. */
2541
2542static void
2543create_signatured_type_table_from_debug_names
ed2dc618 2544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2545 const mapped_debug_names &map,
2546 struct dwarf2_section_info *section,
2547 struct dwarf2_section_info *abbrev_section)
2548{
ed2dc618
SM
2549 struct objfile *objfile = dwarf2_per_objfile->objfile;
2550
96b79293
TT
2551 section->read (objfile);
2552 abbrev_section->read (objfile);
927aa2e7 2553
b2bdb8cf
SM
2554 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2555 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2556
b0b6a987 2557 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
927aa2e7
JK
2558
2559 for (uint32_t i = 0; i < map.tu_count; ++i)
2560 {
2561 struct signatured_type *sig_type;
927aa2e7 2562 void **slot;
927aa2e7
JK
2563
2564 sect_offset sect_off
2565 = (sect_offset) (extract_unsigned_integer
2566 (map.tu_table_reordered + i * map.offset_size,
2567 map.offset_size,
2568 map.dwarf5_byte_order));
2569
2570 comp_unit_head cu_header;
ed2dc618
SM
2571 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2572 abbrev_section,
927aa2e7
JK
2573 section->buffer + to_underlying (sect_off),
2574 rcuh_kind::TYPE);
2575
2576 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2577 struct signatured_type);
2578 sig_type->signature = cu_header.signature;
2579 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2580 sig_type->per_cu.is_debug_types = 1;
2581 sig_type->per_cu.section = section;
2582 sig_type->per_cu.sect_off = sect_off;
e3b94546 2583 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2584 sig_type->per_cu.v.quick
2585 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2586 struct dwarf2_per_cu_quick_data);
2587
b0b6a987 2588 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2589 *slot = sig_type;
2590
b2bdb8cf 2591 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2592 }
2593
b0b6a987 2594 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2595}
2596
9291a0cd
TT
2597/* Read the address map data from the mapped index, and use it to
2598 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2599
9291a0cd 2600static void
ed2dc618
SM
2601create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2602 struct mapped_index *index)
9291a0cd 2603{
ed2dc618 2604 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2605 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2606 const gdb_byte *iter, *end;
9291a0cd 2607 struct addrmap *mutable_map;
9291a0cd
TT
2608 CORE_ADDR baseaddr;
2609
8268c778
PA
2610 auto_obstack temp_obstack;
2611
9291a0cd
TT
2612 mutable_map = addrmap_create_mutable (&temp_obstack);
2613
f00a2de2
PA
2614 iter = index->address_table.data ();
2615 end = iter + index->address_table.size ();
9291a0cd 2616
b3b3bada 2617 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2618
2619 while (iter < end)
2620 {
2621 ULONGEST hi, lo, cu_index;
2622 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2623 iter += 8;
2624 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2625 iter += 8;
2626 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2627 iter += 4;
f652bce2 2628
24a55014 2629 if (lo > hi)
f652bce2 2630 {
b98664d3 2631 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2632 hex_string (lo), hex_string (hi));
24a55014 2633 continue;
f652bce2 2634 }
24a55014 2635
b76e467d 2636 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2637 {
b98664d3 2638 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2639 (unsigned) cu_index);
24a55014 2640 continue;
f652bce2 2641 }
24a55014 2642
79748972
TT
2643 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2644 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2645 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2646 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2647 }
2648
d320c2b5 2649 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2650 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2651}
2652
927aa2e7
JK
2653/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2654 populate the objfile's psymtabs_addrmap. */
2655
2656static void
ed2dc618 2657create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2658 struct dwarf2_section_info *section)
2659{
ed2dc618 2660 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2661 bfd *abfd = objfile->obfd;
2662 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2663 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2664
2665 auto_obstack temp_obstack;
2666 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2667
2668 std::unordered_map<sect_offset,
2669 dwarf2_per_cu_data *,
2670 gdb::hash_enum<sect_offset>>
2671 debug_info_offset_to_per_cu;
b76e467d 2672 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2673 {
927aa2e7
JK
2674 const auto insertpair
2675 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2676 if (!insertpair.second)
2677 {
2678 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2679 "debug_info_offset %s, ignoring .debug_aranges."),
2680 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2681 return;
2682 }
2683 }
2684
96b79293 2685 section->read (objfile);
927aa2e7
JK
2686
2687 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2688
2689 const gdb_byte *addr = section->buffer;
2690
2691 while (addr < section->buffer + section->size)
2692 {
2693 const gdb_byte *const entry_addr = addr;
2694 unsigned int bytes_read;
2695
2696 const LONGEST entry_length = read_initial_length (abfd, addr,
2697 &bytes_read);
2698 addr += bytes_read;
2699
2700 const gdb_byte *const entry_end = addr + entry_length;
2701 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2702 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2703 if (addr + entry_length > section->buffer + section->size)
2704 {
47e3f474 2705 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2706 "length %s exceeds section length %s, "
2707 "ignoring .debug_aranges."),
47e3f474
TV
2708 objfile_name (objfile),
2709 plongest (entry_addr - section->buffer),
927aa2e7
JK
2710 plongest (bytes_read + entry_length),
2711 pulongest (section->size));
2712 return;
2713 }
2714
2715 /* The version number. */
2716 const uint16_t version = read_2_bytes (abfd, addr);
2717 addr += 2;
2718 if (version != 2)
2719 {
47e3f474 2720 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2721 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2722 objfile_name (objfile),
2723 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2724 return;
2725 }
2726
2727 const uint64_t debug_info_offset
2728 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2729 addr += offset_size;
2730 const auto per_cu_it
2731 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2732 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2733 {
47e3f474 2734 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2735 "debug_info_offset %s does not exists, "
2736 "ignoring .debug_aranges."),
47e3f474
TV
2737 objfile_name (objfile),
2738 plongest (entry_addr - section->buffer),
927aa2e7
JK
2739 pulongest (debug_info_offset));
2740 return;
2741 }
2742 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2743
2744 const uint8_t address_size = *addr++;
2745 if (address_size < 1 || address_size > 8)
2746 {
47e3f474 2747 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2748 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2749 objfile_name (objfile),
2750 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2751 return;
2752 }
2753
2754 const uint8_t segment_selector_size = *addr++;
2755 if (segment_selector_size != 0)
2756 {
47e3f474 2757 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2758 "segment_selector_size %u is not supported, "
2759 "ignoring .debug_aranges."),
47e3f474
TV
2760 objfile_name (objfile),
2761 plongest (entry_addr - section->buffer),
927aa2e7
JK
2762 segment_selector_size);
2763 return;
2764 }
2765
2766 /* Must pad to an alignment boundary that is twice the address
2767 size. It is undocumented by the DWARF standard but GCC does
2768 use it. */
2769 for (size_t padding = ((-(addr - section->buffer))
2770 & (2 * address_size - 1));
2771 padding > 0; padding--)
2772 if (*addr++ != 0)
2773 {
47e3f474 2774 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2775 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2776 objfile_name (objfile),
2777 plongest (entry_addr - section->buffer));
927aa2e7
JK
2778 return;
2779 }
2780
2781 for (;;)
2782 {
2783 if (addr + 2 * address_size > entry_end)
2784 {
47e3f474 2785 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2786 "address list is not properly terminated, "
2787 "ignoring .debug_aranges."),
47e3f474
TV
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer));
927aa2e7
JK
2790 return;
2791 }
2792 ULONGEST start = extract_unsigned_integer (addr, address_size,
2793 dwarf5_byte_order);
2794 addr += address_size;
2795 ULONGEST length = extract_unsigned_integer (addr, address_size,
2796 dwarf5_byte_order);
2797 addr += address_size;
2798 if (start == 0 && length == 0)
2799 break;
2800 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2801 {
2802 /* Symbol was eliminated due to a COMDAT group. */
2803 continue;
2804 }
2805 ULONGEST end = start + length;
79748972
TT
2806 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2807 - baseaddr);
2808 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2809 - baseaddr);
927aa2e7
JK
2810 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2811 }
2812 }
2813
d320c2b5 2814 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2815 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2816}
2817
9291a0cd
TT
2818/* Find a slot in the mapped index INDEX for the object named NAME.
2819 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2820 constant pool and return true. If NAME cannot be found, return
2821 false. */
2fdf6df6 2822
109483d9 2823static bool
9291a0cd
TT
2824find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2825 offset_type **vec_out)
2826{
0cf03b49 2827 offset_type hash;
9291a0cd 2828 offset_type slot, step;
559a7a62 2829 int (*cmp) (const char *, const char *);
9291a0cd 2830
791afaa2 2831 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2832 if (current_language->la_language == language_cplus
45280282
IB
2833 || current_language->la_language == language_fortran
2834 || current_language->la_language == language_d)
0cf03b49
JK
2835 {
2836 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2837 not contain any. */
a8719064 2838
72998fb3 2839 if (strchr (name, '(') != NULL)
0cf03b49 2840 {
109483d9 2841 without_params = cp_remove_params (name);
0cf03b49 2842
72998fb3 2843 if (without_params != NULL)
791afaa2 2844 name = without_params.get ();
0cf03b49
JK
2845 }
2846 }
2847
559a7a62 2848 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2849 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2850 simulate our NAME being searched is also lowercased. */
2851 hash = mapped_index_string_hash ((index->version == 4
2852 && case_sensitivity == case_sensitive_off
2853 ? 5 : index->version),
2854 name);
2855
f00a2de2
PA
2856 slot = hash & (index->symbol_table.size () - 1);
2857 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2858 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2859
2860 for (;;)
2861 {
9291a0cd 2862 const char *str;
f00a2de2
PA
2863
2864 const auto &bucket = index->symbol_table[slot];
2865 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2866 return false;
9291a0cd 2867
f00a2de2 2868 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2869 if (!cmp (name, str))
9291a0cd
TT
2870 {
2871 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2872 + MAYBE_SWAP (bucket.vec));
109483d9 2873 return true;
9291a0cd
TT
2874 }
2875
f00a2de2 2876 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2877 }
2878}
2879
4485a1c1
SM
2880/* A helper function that reads the .gdb_index from BUFFER and fills
2881 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2882 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2883 ok to use deprecated sections.
2884
2885 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2886 out parameters that are filled in with information about the CU and
2887 TU lists in the section.
2888
4485a1c1 2889 Returns true if all went well, false otherwise. */
2fdf6df6 2890
d33bc52e 2891static bool
4485a1c1
SM
2892read_gdb_index_from_buffer (struct objfile *objfile,
2893 const char *filename,
2894 bool deprecated_ok,
2895 gdb::array_view<const gdb_byte> buffer,
2896 struct mapped_index *map,
2897 const gdb_byte **cu_list,
2898 offset_type *cu_list_elements,
2899 const gdb_byte **types_list,
2900 offset_type *types_list_elements)
2901{
2902 const gdb_byte *addr = &buffer[0];
82430852 2903
9291a0cd 2904 /* Version check. */
4485a1c1 2905 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2906 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2907 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2908 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2909 indices. */
831adc1f 2910 if (version < 4)
481860b3
GB
2911 {
2912 static int warning_printed = 0;
2913 if (!warning_printed)
2914 {
2915 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2916 filename);
481860b3
GB
2917 warning_printed = 1;
2918 }
2919 return 0;
2920 }
2921 /* Index version 4 uses a different hash function than index version
2922 5 and later.
2923
2924 Versions earlier than 6 did not emit psymbols for inlined
2925 functions. Using these files will cause GDB not to be able to
2926 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2927 indices unless the user has done
2928 "set use-deprecated-index-sections on". */
2ec9a5e0 2929 if (version < 6 && !deprecated_ok)
481860b3
GB
2930 {
2931 static int warning_printed = 0;
2932 if (!warning_printed)
2933 {
e615022a
DE
2934 warning (_("\
2935Skipping deprecated .gdb_index section in %s.\n\
2936Do \"set use-deprecated-index-sections on\" before the file is read\n\
2937to use the section anyway."),
2ec9a5e0 2938 filename);
481860b3
GB
2939 warning_printed = 1;
2940 }
2941 return 0;
2942 }
796a7ff8 2943 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2944 of the TU (for symbols coming from TUs),
2945 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2946 Plus gold-generated indices can have duplicate entries for global symbols,
2947 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2948 These are just performance bugs, and we can't distinguish gdb-generated
2949 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2950
481860b3 2951 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2952 longer backward compatible. */
796a7ff8 2953 if (version > 8)
594e8718 2954 return 0;
9291a0cd 2955
559a7a62 2956 map->version = version;
9291a0cd 2957
4485a1c1 2958 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2959
4485a1c1 2960 int i = 0;
2ec9a5e0
TT
2961 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2962 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2963 / 8);
1fd400ff
TT
2964 ++i;
2965
2ec9a5e0
TT
2966 *types_list = addr + MAYBE_SWAP (metadata[i]);
2967 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2968 - MAYBE_SWAP (metadata[i]))
2969 / 8);
987d643c 2970 ++i;
1fd400ff 2971
f00a2de2
PA
2972 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2973 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2974 map->address_table
2975 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2976 ++i;
2977
f00a2de2
PA
2978 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2979 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2980 map->symbol_table
2981 = gdb::array_view<mapped_index::symbol_table_slot>
2982 ((mapped_index::symbol_table_slot *) symbol_table,
2983 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2984
f00a2de2 2985 ++i;
f9d83a0b 2986 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2987
2ec9a5e0
TT
2988 return 1;
2989}
2990
4485a1c1
SM
2991/* Callback types for dwarf2_read_gdb_index. */
2992
2993typedef gdb::function_view
2994 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2995 get_gdb_index_contents_ftype;
2996typedef gdb::function_view
2997 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2998 get_gdb_index_contents_dwz_ftype;
2999
927aa2e7 3000/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3001 elements of all the CUs and return 1. Otherwise, return 0. */
3002
3003static int
4485a1c1
SM
3004dwarf2_read_gdb_index
3005 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3006 get_gdb_index_contents_ftype get_gdb_index_contents,
3007 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3008{
2ec9a5e0
TT
3009 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3010 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3011 struct dwz_file *dwz;
12359b5e 3012 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3013
4485a1c1
SM
3014 gdb::array_view<const gdb_byte> main_index_contents
3015 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3016
3017 if (main_index_contents.empty ())
3018 return 0;
3019
3063847f 3020 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3021 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3022 use_deprecated_index_sections,
3023 main_index_contents, map.get (), &cu_list,
3024 &cu_list_elements, &types_list,
3025 &types_list_elements))
2ec9a5e0
TT
3026 return 0;
3027
0fefef59 3028 /* Don't use the index if it's empty. */
3063847f 3029 if (map->symbol_table.empty ())
0fefef59
DE
3030 return 0;
3031
2ec9a5e0
TT
3032 /* If there is a .dwz file, read it so we can get its CU list as
3033 well. */
ed2dc618 3034 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3035 if (dwz != NULL)
2ec9a5e0 3036 {
2ec9a5e0
TT
3037 struct mapped_index dwz_map;
3038 const gdb_byte *dwz_types_ignore;
3039 offset_type dwz_types_elements_ignore;
3040
4485a1c1
SM
3041 gdb::array_view<const gdb_byte> dwz_index_content
3042 = get_gdb_index_contents_dwz (objfile, dwz);
3043
3044 if (dwz_index_content.empty ())
3045 return 0;
3046
3047 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3048 bfd_get_filename (dwz->dwz_bfd.get ()),
3049 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3050 &dwz_list, &dwz_list_elements,
3051 &dwz_types_ignore,
3052 &dwz_types_elements_ignore))
2ec9a5e0
TT
3053 {
3054 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3055 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3056 return 0;
3057 }
3058 }
3059
12359b5e
SM
3060 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3061 dwz_list, dwz_list_elements);
1fd400ff 3062
8b70b953
TT
3063 if (types_list_elements)
3064 {
8b70b953
TT
3065 /* We can only handle a single .debug_types when we have an
3066 index. */
fd5866f6 3067 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3068 return 0;
3069
fd5866f6 3070 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3071
12359b5e
SM
3072 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3073 types_list, types_list_elements);
8b70b953 3074 }
9291a0cd 3075
3063847f 3076 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3077
3063847f 3078 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3079 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3080 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3081 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3082
3083 return 1;
3084}
3085
dee91e82 3086/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3087
dee91e82
DE
3088static void
3089dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3090 const gdb_byte *info_ptr,
3e225074 3091 struct die_info *comp_unit_die)
9291a0cd 3092{
dee91e82 3093 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3094 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3095 struct dwarf2_per_objfile *dwarf2_per_objfile
3096 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3097 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3098 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3099 struct attribute *attr;
7b9f3c50
DE
3100 void **slot;
3101 struct quick_file_names *qfn;
9291a0cd 3102
0186c6a7
DE
3103 gdb_assert (! this_cu->is_debug_types);
3104
07261596
TT
3105 /* Our callers never want to match partial units -- instead they
3106 will match the enclosing full CU. */
3107 if (comp_unit_die->tag == DW_TAG_partial_unit)
3108 {
3109 this_cu->v.quick->no_file_data = 1;
3110 return;
3111 }
3112
0186c6a7 3113 lh_cu = this_cu;
7b9f3c50 3114 slot = NULL;
dee91e82 3115
fff8551c 3116 line_header_up lh;
9c541725 3117 sect_offset line_offset {};
fff8551c 3118
dee91e82 3119 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3120 if (attr != nullptr)
9291a0cd 3121 {
7b9f3c50
DE
3122 struct quick_file_names find_entry;
3123
9c541725 3124 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3125
3126 /* We may have already read in this line header (TU line header sharing).
3127 If we have we're done. */
094b34ac 3128 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3129 find_entry.hash.line_sect_off = line_offset;
5895093f 3130 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3131 &find_entry, INSERT);
3132 if (*slot != NULL)
3133 {
9a3c8263 3134 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3135 return;
7b9f3c50
DE
3136 }
3137
3019eac3 3138 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3139 }
3140 if (lh == NULL)
3141 {
094b34ac 3142 lh_cu->v.quick->no_file_data = 1;
dee91e82 3143 return;
9291a0cd
TT
3144 }
3145
8d749320 3146 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3147 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3148 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3149 gdb_assert (slot != NULL);
3150 *slot = qfn;
9291a0cd 3151
d721ba37 3152 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3153
aa391654
TT
3154 int offset = 0;
3155 if (strcmp (fnd.name, "<unknown>") != 0)
3156 ++offset;
3157
7ba99d21 3158 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3159 qfn->file_names =
aa391654
TT
3160 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3161 if (offset != 0)
3162 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3163 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3164 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3165 fnd.comp_dir).release ();
7b9f3c50 3166 qfn->real_names = NULL;
9291a0cd 3167
094b34ac 3168 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3169}
3170
3171/* A helper for the "quick" functions which attempts to read the line
3172 table for THIS_CU. */
3173
3174static struct quick_file_names *
e4a48d9d 3175dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3176{
0186c6a7
DE
3177 /* This should never be called for TUs. */
3178 gdb_assert (! this_cu->is_debug_types);
3179 /* Nor type unit groups. */
3180 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3181
dee91e82
DE
3182 if (this_cu->v.quick->file_names != NULL)
3183 return this_cu->v.quick->file_names;
3184 /* If we know there is no line data, no point in looking again. */
3185 if (this_cu->v.quick->no_file_data)
3186 return NULL;
3187
c0ab21c2
TT
3188 cutu_reader reader (this_cu);
3189 if (!reader.dummy_p)
3e225074 3190 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3191
3192 if (this_cu->v.quick->no_file_data)
3193 return NULL;
3194 return this_cu->v.quick->file_names;
9291a0cd
TT
3195}
3196
3197/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3198 real path for a given file name from the line table. */
2fdf6df6 3199
9291a0cd 3200static const char *
7b9f3c50
DE
3201dw2_get_real_path (struct objfile *objfile,
3202 struct quick_file_names *qfn, int index)
9291a0cd 3203{
7b9f3c50
DE
3204 if (qfn->real_names == NULL)
3205 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3206 qfn->num_file_names, const char *);
9291a0cd 3207
7b9f3c50 3208 if (qfn->real_names[index] == NULL)
14278e1f 3209 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3210
7b9f3c50 3211 return qfn->real_names[index];
9291a0cd
TT
3212}
3213
3214static struct symtab *
3215dw2_find_last_source_symtab (struct objfile *objfile)
3216{
ed2dc618
SM
3217 struct dwarf2_per_objfile *dwarf2_per_objfile
3218 = get_dwarf2_per_objfile (objfile);
b76e467d 3219 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3220 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3221
43f3e411
DE
3222 if (cust == NULL)
3223 return NULL;
ed2dc618 3224
43f3e411 3225 return compunit_primary_filetab (cust);
9291a0cd
TT
3226}
3227
7b9f3c50
DE
3228/* Traversal function for dw2_forget_cached_source_info. */
3229
3230static int
3231dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3232{
7b9f3c50 3233 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3234
7b9f3c50 3235 if (file_data->real_names)
9291a0cd 3236 {
7b9f3c50 3237 int i;
9291a0cd 3238
7b9f3c50 3239 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3240 {
7b9f3c50
DE
3241 xfree ((void*) file_data->real_names[i]);
3242 file_data->real_names[i] = NULL;
9291a0cd
TT
3243 }
3244 }
7b9f3c50
DE
3245
3246 return 1;
3247}
3248
3249static void
3250dw2_forget_cached_source_info (struct objfile *objfile)
3251{
ed2dc618
SM
3252 struct dwarf2_per_objfile *dwarf2_per_objfile
3253 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3254
5895093f 3255 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3256 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3257}
3258
f8eba3c6
TT
3259/* Helper function for dw2_map_symtabs_matching_filename that expands
3260 the symtabs and calls the iterator. */
3261
3262static int
3263dw2_map_expand_apply (struct objfile *objfile,
3264 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3265 const char *name, const char *real_path,
14bc53a8 3266 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3267{
43f3e411 3268 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3269
3270 /* Don't visit already-expanded CUs. */
43f3e411 3271 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3272 return 0;
3273
3274 /* This may expand more than one symtab, and we want to iterate over
3275 all of them. */
58f0c718 3276 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3277
14bc53a8
PA
3278 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3279 last_made, callback);
f8eba3c6
TT
3280}
3281
3282/* Implementation of the map_symtabs_matching_filename method. */
3283
14bc53a8
PA
3284static bool
3285dw2_map_symtabs_matching_filename
3286 (struct objfile *objfile, const char *name, const char *real_path,
3287 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3288{
c011a4f4 3289 const char *name_basename = lbasename (name);
ed2dc618
SM
3290 struct dwarf2_per_objfile *dwarf2_per_objfile
3291 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3292
848e3e78
DE
3293 /* The rule is CUs specify all the files, including those used by
3294 any TU, so there's no need to scan TUs here. */
f4dc4d17 3295
b76e467d 3296 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3297 {
3d7bb9d9 3298 /* We only need to look at symtabs not already expanded. */
43f3e411 3299 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3300 continue;
3301
b76e467d 3302 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3303 if (file_data == NULL)
9291a0cd
TT
3304 continue;
3305
b76e467d 3306 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3307 {
7b9f3c50 3308 const char *this_name = file_data->file_names[j];
da235a7c 3309 const char *this_real_name;
9291a0cd 3310
af529f8f 3311 if (compare_filenames_for_search (this_name, name))
9291a0cd 3312 {
f5b95b50 3313 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3314 callback))
3315 return true;
288e77a7 3316 continue;
4aac40c8 3317 }
9291a0cd 3318
c011a4f4
DE
3319 /* Before we invoke realpath, which can get expensive when many
3320 files are involved, do a quick comparison of the basenames. */
3321 if (! basenames_may_differ
3322 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3323 continue;
3324
da235a7c
JK
3325 this_real_name = dw2_get_real_path (objfile, file_data, j);
3326 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3327 {
da235a7c 3328 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3329 callback))
3330 return true;
288e77a7 3331 continue;
da235a7c 3332 }
9291a0cd 3333
da235a7c
JK
3334 if (real_path != NULL)
3335 {
af529f8f
JK
3336 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3337 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3338 if (this_real_name != NULL
af529f8f 3339 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3340 {
f5b95b50 3341 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3342 callback))
3343 return true;
288e77a7 3344 continue;
9291a0cd
TT
3345 }
3346 }
3347 }
3348 }
3349
14bc53a8 3350 return false;
9291a0cd
TT
3351}
3352
da51c347
DE
3353/* Struct used to manage iterating over all CUs looking for a symbol. */
3354
3355struct dw2_symtab_iterator
9291a0cd 3356{
ed2dc618
SM
3357 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3358 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3359 /* If set, only look for symbols that match that block. Valid values are
3360 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3361 gdb::optional<block_enum> block_index;
da51c347
DE
3362 /* The kind of symbol we're looking for. */
3363 domain_enum domain;
3364 /* The list of CUs from the index entry of the symbol,
3365 or NULL if not found. */
3366 offset_type *vec;
3367 /* The next element in VEC to look at. */
3368 int next;
3369 /* The number of elements in VEC, or zero if there is no match. */
3370 int length;
8943b874
DE
3371 /* Have we seen a global version of the symbol?
3372 If so we can ignore all further global instances.
3373 This is to work around gold/15646, inefficient gold-generated
3374 indices. */
3375 int global_seen;
da51c347 3376};
9291a0cd 3377
2b79f376 3378/* Initialize the index symtab iterator ITER. */
2fdf6df6 3379
9291a0cd 3380static void
da51c347 3381dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3382 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3383 gdb::optional<block_enum> block_index,
da51c347
DE
3384 domain_enum domain,
3385 const char *name)
3386{
ed2dc618 3387 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3388 iter->block_index = block_index;
3389 iter->domain = domain;
3390 iter->next = 0;
8943b874 3391 iter->global_seen = 0;
da51c347 3392
3063847f 3393 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3394
3395 /* index is NULL if OBJF_READNOW. */
3396 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3397 iter->length = MAYBE_SWAP (*iter->vec);
3398 else
3399 {
3400 iter->vec = NULL;
3401 iter->length = 0;
3402 }
3403}
3404
3405/* Return the next matching CU or NULL if there are no more. */
3406
3407static struct dwarf2_per_cu_data *
3408dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3409{
ed2dc618
SM
3410 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3411
da51c347
DE
3412 for ( ; iter->next < iter->length; ++iter->next)
3413 {
3414 offset_type cu_index_and_attrs =
3415 MAYBE_SWAP (iter->vec[iter->next + 1]);
3416 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3417 gdb_index_symbol_kind symbol_kind =
3418 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3419 /* Only check the symbol attributes if they're present.
3420 Indices prior to version 7 don't record them,
3421 and indices >= 7 may elide them for certain symbols
3422 (gold does this). */
3423 int attrs_valid =
ed2dc618 3424 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3425 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3426
3190f0c6 3427 /* Don't crash on bad data. */
b76e467d 3428 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3429 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3430 {
b98664d3 3431 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3432 " [in module %s]"),
3433 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3434 continue;
3435 }
3436
ff4c9fec 3437 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3438
da51c347 3439 /* Skip if already read in. */
43f3e411 3440 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3441 continue;
3442
8943b874
DE
3443 /* Check static vs global. */
3444 if (attrs_valid)
3445 {
2b79f376
SM
3446 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3447
3448 if (iter->block_index.has_value ())
3449 {
3450 bool want_static = *iter->block_index == STATIC_BLOCK;
3451
3452 if (is_static != want_static)
3453 continue;
3454 }
3455
8943b874
DE
3456 /* Work around gold/15646. */
3457 if (!is_static && iter->global_seen)
3458 continue;
3459 if (!is_static)
3460 iter->global_seen = 1;
3461 }
da51c347
DE
3462
3463 /* Only check the symbol's kind if it has one. */
3464 if (attrs_valid)
3465 {
3466 switch (iter->domain)
3467 {
3468 case VAR_DOMAIN:
3469 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3471 /* Some types are also in VAR_DOMAIN. */
3472 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3473 continue;
3474 break;
3475 case STRUCT_DOMAIN:
3476 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3477 continue;
3478 break;
3479 case LABEL_DOMAIN:
3480 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3481 continue;
3482 break;
59c35742
AB
3483 case MODULE_DOMAIN:
3484 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3485 continue;
3486 break;
da51c347
DE
3487 default:
3488 break;
3489 }
3490 }
3491
3492 ++iter->next;
3493 return per_cu;
3494 }
3495
3496 return NULL;
3497}
3498
43f3e411 3499static struct compunit_symtab *
c7f839cb 3500dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3501 const char *name, domain_enum domain)
9291a0cd 3502{
43f3e411 3503 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3504 struct dwarf2_per_objfile *dwarf2_per_objfile
3505 = get_dwarf2_per_objfile (objfile);
9291a0cd 3506
b5ec771e
PA
3507 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3508
ed2dc618
SM
3509 struct dw2_symtab_iterator iter;
3510 struct dwarf2_per_cu_data *per_cu;
da51c347 3511
2b79f376 3512 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3513
ed2dc618
SM
3514 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3515 {
3516 struct symbol *sym, *with_opaque = NULL;
58f0c718 3517 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3518 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3519 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3520
ed2dc618
SM
3521 sym = block_find_symbol (block, name, domain,
3522 block_find_non_opaque_type_preferred,
3523 &with_opaque);
b2e2f908 3524
ed2dc618
SM
3525 /* Some caution must be observed with overloaded functions
3526 and methods, since the index will not contain any overload
3527 information (but NAME might contain it). */
da51c347 3528
ed2dc618
SM
3529 if (sym != NULL
3530 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3531 return stab;
3532 if (with_opaque != NULL
3533 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3534 stab_best = stab;
da51c347 3535
ed2dc618 3536 /* Keep looking through other CUs. */
9291a0cd 3537 }
9291a0cd 3538
da51c347 3539 return stab_best;
9291a0cd
TT
3540}
3541
3542static void
3543dw2_print_stats (struct objfile *objfile)
3544{
ed2dc618
SM
3545 struct dwarf2_per_objfile *dwarf2_per_objfile
3546 = get_dwarf2_per_objfile (objfile);
b76e467d 3547 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3548 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3549 int count = 0;
9291a0cd 3550
ed2dc618 3551 for (int i = 0; i < total; ++i)
9291a0cd 3552 {
ff4c9fec 3553 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3554
43f3e411 3555 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3556 ++count;
3557 }
e4a48d9d 3558 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3559 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3560}
3561
779bd270
DE
3562/* This dumps minimal information about the index.
3563 It is called via "mt print objfiles".
3564 One use is to verify .gdb_index has been loaded by the
3565 gdb.dwarf2/gdb-index.exp testcase. */
3566
9291a0cd
TT
3567static void
3568dw2_dump (struct objfile *objfile)
3569{
ed2dc618
SM
3570 struct dwarf2_per_objfile *dwarf2_per_objfile
3571 = get_dwarf2_per_objfile (objfile);
3572
779bd270
DE
3573 gdb_assert (dwarf2_per_objfile->using_index);
3574 printf_filtered (".gdb_index:");
3575 if (dwarf2_per_objfile->index_table != NULL)
3576 {
3577 printf_filtered (" version %d\n",
3578 dwarf2_per_objfile->index_table->version);
3579 }
3580 else
3581 printf_filtered (" faked for \"readnow\"\n");
3582 printf_filtered ("\n");
9291a0cd
TT
3583}
3584
9291a0cd
TT
3585static void
3586dw2_expand_symtabs_for_function (struct objfile *objfile,
3587 const char *func_name)
3588{
ed2dc618
SM
3589 struct dwarf2_per_objfile *dwarf2_per_objfile
3590 = get_dwarf2_per_objfile (objfile);
da51c347 3591
ed2dc618
SM
3592 struct dw2_symtab_iterator iter;
3593 struct dwarf2_per_cu_data *per_cu;
da51c347 3594
2b79f376 3595 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3596
ed2dc618 3597 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3598 dw2_instantiate_symtab (per_cu, false);
da51c347 3599
9291a0cd
TT
3600}
3601
3602static void
3603dw2_expand_all_symtabs (struct objfile *objfile)
3604{
ed2dc618
SM
3605 struct dwarf2_per_objfile *dwarf2_per_objfile
3606 = get_dwarf2_per_objfile (objfile);
b76e467d 3607 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3608 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3609
ed2dc618 3610 for (int i = 0; i < total_units; ++i)
9291a0cd 3611 {
ff4c9fec 3612 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3613
58f0c718
TT
3614 /* We don't want to directly expand a partial CU, because if we
3615 read it with the wrong language, then assertion failures can
3616 be triggered later on. See PR symtab/23010. So, tell
3617 dw2_instantiate_symtab to skip partial CUs -- any important
3618 partial CU will be read via DW_TAG_imported_unit anyway. */
3619 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3620 }
3621}
3622
3623static void
652a8996
JK
3624dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3625 const char *fullname)
9291a0cd 3626{
ed2dc618
SM
3627 struct dwarf2_per_objfile *dwarf2_per_objfile
3628 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3629
3630 /* We don't need to consider type units here.
3631 This is only called for examining code, e.g. expand_line_sal.
3632 There can be an order of magnitude (or more) more type units
3633 than comp units, and we avoid them if we can. */
3634
b76e467d 3635 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3636 {
3d7bb9d9 3637 /* We only need to look at symtabs not already expanded. */
43f3e411 3638 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3639 continue;
3640
b76e467d 3641 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3642 if (file_data == NULL)
9291a0cd
TT
3643 continue;
3644
b76e467d 3645 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3646 {
652a8996
JK
3647 const char *this_fullname = file_data->file_names[j];
3648
3649 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3650 {
58f0c718 3651 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3652 break;
3653 }
3654 }
3655 }
3656}
3657
9291a0cd 3658static void
199b4314
TT
3659dw2_map_matching_symbols
3660 (struct objfile *objfile,
b054970d 3661 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3662 int global,
3663 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3664 symbol_compare_ftype *ordered_compare)
9291a0cd 3665{
40658b94 3666 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3667 current language is Ada for a non-Ada objfile using GNU index. As Ada
3668 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3669}
3670
e1ef7d7a
PA
3671/* Starting from a search name, return the string that finds the upper
3672 bound of all strings that start with SEARCH_NAME in a sorted name
3673 list. Returns the empty string to indicate that the upper bound is
3674 the end of the list. */
3675
3676static std::string
3677make_sort_after_prefix_name (const char *search_name)
3678{
3679 /* When looking to complete "func", we find the upper bound of all
3680 symbols that start with "func" by looking for where we'd insert
3681 the closest string that would follow "func" in lexicographical
3682 order. Usually, that's "func"-with-last-character-incremented,
3683 i.e. "fund". Mind non-ASCII characters, though. Usually those
3684 will be UTF-8 multi-byte sequences, but we can't be certain.
3685 Especially mind the 0xff character, which is a valid character in
3686 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3687 rule out compilers allowing it in identifiers. Note that
3688 conveniently, strcmp/strcasecmp are specified to compare
3689 characters interpreted as unsigned char. So what we do is treat
3690 the whole string as a base 256 number composed of a sequence of
3691 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3692 to 0, and carries 1 to the following more-significant position.
3693 If the very first character in SEARCH_NAME ends up incremented
3694 and carries/overflows, then the upper bound is the end of the
3695 list. The string after the empty string is also the empty
3696 string.
3697
3698 Some examples of this operation:
3699
3700 SEARCH_NAME => "+1" RESULT
3701
3702 "abc" => "abd"
3703 "ab\xff" => "ac"
3704 "\xff" "a" "\xff" => "\xff" "b"
3705 "\xff" => ""
3706 "\xff\xff" => ""
3707 "" => ""
3708
3709 Then, with these symbols for example:
3710
3711 func
3712 func1
3713 fund
3714
3715 completing "func" looks for symbols between "func" and
3716 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3717 which finds "func" and "func1", but not "fund".
3718
3719 And with:
3720
3721 funcÿ (Latin1 'ÿ' [0xff])
3722 funcÿ1
3723 fund
3724
3725 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3726 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3727
3728 And with:
3729
3730 ÿÿ (Latin1 'ÿ' [0xff])
3731 ÿÿ1
3732
3733 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3734 the end of the list.
3735 */
3736 std::string after = search_name;
3737 while (!after.empty () && (unsigned char) after.back () == 0xff)
3738 after.pop_back ();
3739 if (!after.empty ())
3740 after.back () = (unsigned char) after.back () + 1;
3741 return after;
3742}
3743
5c58de74 3744/* See declaration. */
61d96d7e 3745
5c58de74
PA
3746std::pair<std::vector<name_component>::const_iterator,
3747 std::vector<name_component>::const_iterator>
44ed8f3e 3748mapped_index_base::find_name_components_bounds
3b00ef10 3749 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3750{
5c58de74
PA
3751 auto *name_cmp
3752 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3753
3b00ef10
TT
3754 const char *lang_name
3755 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3756
3f563c84
PA
3757 /* Comparison function object for lower_bound that matches against a
3758 given symbol name. */
3759 auto lookup_compare_lower = [&] (const name_component &elem,
3760 const char *name)
3761 {
5c58de74 3762 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3763 const char *elem_name = elem_qualified + elem.name_offset;
3764 return name_cmp (elem_name, name) < 0;
3765 };
3766
3767 /* Comparison function object for upper_bound that matches against a
3768 given symbol name. */
3769 auto lookup_compare_upper = [&] (const char *name,
3770 const name_component &elem)
3771 {
5c58de74 3772 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3773 const char *elem_name = elem_qualified + elem.name_offset;
3774 return name_cmp (name, elem_name) < 0;
3775 };
3776
5c58de74
PA
3777 auto begin = this->name_components.begin ();
3778 auto end = this->name_components.end ();
3f563c84
PA
3779
3780 /* Find the lower bound. */
3781 auto lower = [&] ()
3782 {
3b00ef10 3783 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3784 return begin;
3785 else
3b00ef10 3786 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3787 } ();
3788
3789 /* Find the upper bound. */
3790 auto upper = [&] ()
3791 {
5c58de74 3792 if (lookup_name_without_params.completion_mode ())
3f563c84 3793 {
e1ef7d7a
PA
3794 /* In completion mode, we want UPPER to point past all
3795 symbols names that have the same prefix. I.e., with
3796 these symbols, and completing "func":
3797
3798 function << lower bound
3799 function1
3800 other_function << upper bound
3801
3802 We find the upper bound by looking for the insertion
3803 point of "func"-with-last-character-incremented,
3804 i.e. "fund". */
3b00ef10 3805 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3806 if (after.empty ())
3f563c84 3807 return end;
e6b2f5ef
PA
3808 return std::lower_bound (lower, end, after.c_str (),
3809 lookup_compare_lower);
3f563c84
PA
3810 }
3811 else
3b00ef10 3812 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3813 } ();
3814
5c58de74
PA
3815 return {lower, upper};
3816}
3817
3818/* See declaration. */
3819
3820void
44ed8f3e 3821mapped_index_base::build_name_components ()
5c58de74
PA
3822{
3823 if (!this->name_components.empty ())
3824 return;
3825
3826 this->name_components_casing = case_sensitivity;
3827 auto *name_cmp
3828 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3829
3830 /* The code below only knows how to break apart components of C++
3831 symbol names (and other languages that use '::' as
3b00ef10 3832 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3833 auto count = this->symbol_name_count ();
3834 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3835 {
44ed8f3e 3836 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3837 continue;
3838
3839 const char *name = this->symbol_name_at (idx);
3840
3841 /* Add each name component to the name component table. */
3842 unsigned int previous_len = 0;
3b00ef10
TT
3843
3844 if (strstr (name, "::") != nullptr)
3845 {
3846 for (unsigned int current_len = cp_find_first_component (name);
3847 name[current_len] != '\0';
3848 current_len += cp_find_first_component (name + current_len))
3849 {
3850 gdb_assert (name[current_len] == ':');
3851 this->name_components.push_back ({previous_len, idx});
3852 /* Skip the '::'. */
3853 current_len += 2;
3854 previous_len = current_len;
3855 }
3856 }
3857 else
5c58de74 3858 {
3b00ef10
TT
3859 /* Handle the Ada encoded (aka mangled) form here. */
3860 for (const char *iter = strstr (name, "__");
3861 iter != nullptr;
3862 iter = strstr (iter, "__"))
3863 {
3864 this->name_components.push_back ({previous_len, idx});
3865 iter += 2;
3866 previous_len = iter - name;
3867 }
5c58de74 3868 }
3b00ef10 3869
5c58de74
PA
3870 this->name_components.push_back ({previous_len, idx});
3871 }
3872
3873 /* Sort name_components elements by name. */
3874 auto name_comp_compare = [&] (const name_component &left,
3875 const name_component &right)
3876 {
3877 const char *left_qualified = this->symbol_name_at (left.idx);
3878 const char *right_qualified = this->symbol_name_at (right.idx);
3879
3880 const char *left_name = left_qualified + left.name_offset;
3881 const char *right_name = right_qualified + right.name_offset;
3882
3883 return name_cmp (left_name, right_name) < 0;
3884 };
3885
3886 std::sort (this->name_components.begin (),
3887 this->name_components.end (),
3888 name_comp_compare);
3889}
3890
3891/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3892 mapped_index_base instead of the containing objfile. This is split
3893 to a separate function in order to be able to unit test the
3894 name_components matching using a mock mapped_index_base. For each
5c58de74 3895 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3896 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3897
3898static void
3899dw2_expand_symtabs_matching_symbol
44ed8f3e 3900 (mapped_index_base &index,
5c58de74
PA
3901 const lookup_name_info &lookup_name_in,
3902 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3903 enum search_domain kind,
3b00ef10 3904 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3905{
3906 lookup_name_info lookup_name_without_params
3907 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3908
3909 /* Build the symbol name component sorted vector, if we haven't
3910 yet. */
3911 index.build_name_components ();
3912
3f563c84
PA
3913 /* The same symbol may appear more than once in the range though.
3914 E.g., if we're looking for symbols that complete "w", and we have
3915 a symbol named "w1::w2", we'll find the two name components for
3916 that same symbol in the range. To be sure we only call the
3917 callback once per symbol, we first collect the symbol name
3918 indexes that matched in a temporary vector and ignore
3919 duplicates. */
3920 std::vector<offset_type> matches;
3f563c84 3921
3b00ef10
TT
3922 struct name_and_matcher
3923 {
3924 symbol_name_matcher_ftype *matcher;
3925 const std::string &name;
3926
3927 bool operator== (const name_and_matcher &other) const
3f563c84 3928 {
3b00ef10
TT
3929 return matcher == other.matcher && name == other.name;
3930 }
3931 };
3932
3933 /* A vector holding all the different symbol name matchers, for all
3934 languages. */
3935 std::vector<name_and_matcher> matchers;
3936
3937 for (int i = 0; i < nr_languages; i++)
3938 {
3939 enum language lang_e = (enum language) i;
3940
3941 const language_defn *lang = language_def (lang_e);
3942 symbol_name_matcher_ftype *name_matcher
3943 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3944
3b00ef10
TT
3945 name_and_matcher key {
3946 name_matcher,
3947 lookup_name_without_params.language_lookup_name (lang_e)
3948 };
3949
3950 /* Don't insert the same comparison routine more than once.
3951 Note that we do this linear walk. This is not a problem in
3952 practice because the number of supported languages is
3953 low. */
3954 if (std::find (matchers.begin (), matchers.end (), key)
3955 != matchers.end ())
9291a0cd 3956 continue;
3b00ef10
TT
3957 matchers.push_back (std::move (key));
3958
3959 auto bounds
3960 = index.find_name_components_bounds (lookup_name_without_params,
3961 lang_e);
3962
3963 /* Now for each symbol name in range, check to see if we have a name
3964 match, and if so, call the MATCH_CALLBACK callback. */
3965
3966 for (; bounds.first != bounds.second; ++bounds.first)
3967 {
3968 const char *qualified = index.symbol_name_at (bounds.first->idx);
3969
3970 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3971 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3972 continue;
9291a0cd 3973
3b00ef10
TT
3974 matches.push_back (bounds.first->idx);
3975 }
3f563c84
PA
3976 }
3977
3978 std::sort (matches.begin (), matches.end ());
3979
3980 /* Finally call the callback, once per match. */
3981 ULONGEST prev = -1;
3982 for (offset_type idx : matches)
3983 {
3984 if (prev != idx)
3985 {
3b00ef10
TT
3986 if (!match_callback (idx))
3987 break;
3f563c84
PA
3988 prev = idx;
3989 }
3990 }
3991
3992 /* Above we use a type wider than idx's for 'prev', since 0 and
3993 (offset_type)-1 are both possible values. */
3994 static_assert (sizeof (prev) > sizeof (offset_type), "");
3995}
3996
c62446b1
PA
3997#if GDB_SELF_TEST
3998
3999namespace selftests { namespace dw2_expand_symtabs_matching {
4000
a3c5fafd
PA
4001/* A mock .gdb_index/.debug_names-like name index table, enough to
4002 exercise dw2_expand_symtabs_matching_symbol, which works with the
4003 mapped_index_base interface. Builds an index from the symbol list
4004 passed as parameter to the constructor. */
4005class mock_mapped_index : public mapped_index_base
c62446b1
PA
4006{
4007public:
a3c5fafd
PA
4008 mock_mapped_index (gdb::array_view<const char *> symbols)
4009 : m_symbol_table (symbols)
c62446b1
PA
4010 {}
4011
a3c5fafd 4012 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4013
a3c5fafd 4014 /* Return the number of names in the symbol table. */
632e107b 4015 size_t symbol_name_count () const override
c62446b1 4016 {
a3c5fafd 4017 return m_symbol_table.size ();
c62446b1
PA
4018 }
4019
a3c5fafd 4020 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4021 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4022 {
4023 return m_symbol_table[idx];
4024 }
c62446b1 4025
a3c5fafd
PA
4026private:
4027 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4028};
4029
4030/* Convenience function that converts a NULL pointer to a "<null>"
4031 string, to pass to print routines. */
4032
4033static const char *
4034string_or_null (const char *str)
4035{
4036 return str != NULL ? str : "<null>";
4037}
4038
4039/* Check if a lookup_name_info built from
4040 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4041 index. EXPECTED_LIST is the list of expected matches, in expected
4042 matching order. If no match expected, then an empty list is
4043 specified. Returns true on success. On failure prints a warning
4044 indicating the file:line that failed, and returns false. */
4045
4046static bool
4047check_match (const char *file, int line,
4048 mock_mapped_index &mock_index,
4049 const char *name, symbol_name_match_type match_type,
4050 bool completion_mode,
4051 std::initializer_list<const char *> expected_list)
4052{
4053 lookup_name_info lookup_name (name, match_type, completion_mode);
4054
4055 bool matched = true;
4056
4057 auto mismatch = [&] (const char *expected_str,
4058 const char *got)
4059 {
4060 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4061 "expected=\"%s\", got=\"%s\"\n"),
4062 file, line,
4063 (match_type == symbol_name_match_type::FULL
4064 ? "FULL" : "WILD"),
4065 name, string_or_null (expected_str), string_or_null (got));
4066 matched = false;
4067 };
4068
4069 auto expected_it = expected_list.begin ();
4070 auto expected_end = expected_list.end ();
4071
a3c5fafd 4072 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4073 NULL, ALL_DOMAIN,
4074 [&] (offset_type idx)
4075 {
a3c5fafd 4076 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4077 const char *expected_str
4078 = expected_it == expected_end ? NULL : *expected_it++;
4079
4080 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4081 mismatch (expected_str, matched_name);
3b00ef10 4082 return true;
c62446b1
PA
4083 });
4084
4085 const char *expected_str
4086 = expected_it == expected_end ? NULL : *expected_it++;
4087 if (expected_str != NULL)
4088 mismatch (expected_str, NULL);
4089
4090 return matched;
4091}
4092
4093/* The symbols added to the mock mapped_index for testing (in
4094 canonical form). */
4095static const char *test_symbols[] = {
4096 "function",
4097 "std::bar",
4098 "std::zfunction",
4099 "std::zfunction2",
4100 "w1::w2",
4101 "ns::foo<char*>",
4102 "ns::foo<int>",
4103 "ns::foo<long>",
a20714ff
PA
4104 "ns2::tmpl<int>::foo2",
4105 "(anonymous namespace)::A::B::C",
c62446b1 4106
e1ef7d7a
PA
4107 /* These are used to check that the increment-last-char in the
4108 matching algorithm for completion doesn't match "t1_fund" when
4109 completing "t1_func". */
4110 "t1_func",
4111 "t1_func1",
4112 "t1_fund",
4113 "t1_fund1",
4114
4115 /* A UTF-8 name with multi-byte sequences to make sure that
4116 cp-name-parser understands this as a single identifier ("função"
4117 is "function" in PT). */
4118 u8"u8função",
4119
4120 /* \377 (0xff) is Latin1 'ÿ'. */
4121 "yfunc\377",
4122
4123 /* \377 (0xff) is Latin1 'ÿ'. */
4124 "\377",
4125 "\377\377123",
4126
c62446b1
PA
4127 /* A name with all sorts of complications. Starts with "z" to make
4128 it easier for the completion tests below. */
4129#define Z_SYM_NAME \
4130 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4131 "::tuple<(anonymous namespace)::ui*, " \
4132 "std::default_delete<(anonymous namespace)::ui>, void>"
4133
4134 Z_SYM_NAME
4135};
4136
a3c5fafd
PA
4137/* Returns true if the mapped_index_base::find_name_component_bounds
4138 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4139 in completion mode. */
5c58de74
PA
4140
4141static bool
a3c5fafd 4142check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4143 const char *search_name,
4144 gdb::array_view<const char *> expected_syms)
4145{
4146 lookup_name_info lookup_name (search_name,
4147 symbol_name_match_type::FULL, true);
4148
3b00ef10
TT
4149 auto bounds = index.find_name_components_bounds (lookup_name,
4150 language_cplus);
5c58de74
PA
4151
4152 size_t distance = std::distance (bounds.first, bounds.second);
4153 if (distance != expected_syms.size ())
4154 return false;
4155
4156 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4157 {
4158 auto nc_elem = bounds.first + exp_elem;
4159 const char *qualified = index.symbol_name_at (nc_elem->idx);
4160 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4161 return false;
4162 }
4163
4164 return true;
4165}
4166
4167/* Test the lower-level mapped_index::find_name_component_bounds
4168 method. */
4169
c62446b1 4170static void
5c58de74
PA
4171test_mapped_index_find_name_component_bounds ()
4172{
4173 mock_mapped_index mock_index (test_symbols);
4174
a3c5fafd 4175 mock_index.build_name_components ();
5c58de74
PA
4176
4177 /* Test the lower-level mapped_index::find_name_component_bounds
4178 method in completion mode. */
4179 {
4180 static const char *expected_syms[] = {
4181 "t1_func",
4182 "t1_func1",
5c58de74
PA
4183 };
4184
a3c5fafd 4185 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4186 "t1_func", expected_syms));
4187 }
4188
4189 /* Check that the increment-last-char in the name matching algorithm
4190 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4191 {
4192 static const char *expected_syms1[] = {
4193 "\377",
4194 "\377\377123",
4195 };
a3c5fafd 4196 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4197 "\377", expected_syms1));
4198
4199 static const char *expected_syms2[] = {
4200 "\377\377123",
4201 };
a3c5fafd 4202 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4203 "\377\377", expected_syms2));
4204 }
4205}
4206
4207/* Test dw2_expand_symtabs_matching_symbol. */
4208
4209static void
4210test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4211{
4212 mock_mapped_index mock_index (test_symbols);
4213
4214 /* We let all tests run until the end even if some fails, for debug
4215 convenience. */
4216 bool any_mismatch = false;
4217
4218 /* Create the expected symbols list (an initializer_list). Needed
4219 because lists have commas, and we need to pass them to CHECK,
4220 which is a macro. */
4221#define EXPECT(...) { __VA_ARGS__ }
4222
4223 /* Wrapper for check_match that passes down the current
4224 __FILE__/__LINE__. */
4225#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4226 any_mismatch |= !check_match (__FILE__, __LINE__, \
4227 mock_index, \
4228 NAME, MATCH_TYPE, COMPLETION_MODE, \
4229 EXPECTED_LIST)
4230
4231 /* Identity checks. */
4232 for (const char *sym : test_symbols)
4233 {
4234 /* Should be able to match all existing symbols. */
4235 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4236 EXPECT (sym));
4237
4238 /* Should be able to match all existing symbols with
4239 parameters. */
4240 std::string with_params = std::string (sym) + "(int)";
4241 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4242 EXPECT (sym));
4243
4244 /* Should be able to match all existing symbols with
4245 parameters and qualifiers. */
4246 with_params = std::string (sym) + " ( int ) const";
4247 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4248 EXPECT (sym));
4249
4250 /* This should really find sym, but cp-name-parser.y doesn't
4251 know about lvalue/rvalue qualifiers yet. */
4252 with_params = std::string (sym) + " ( int ) &&";
4253 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4254 {});
4255 }
4256
e1ef7d7a
PA
4257 /* Check that the name matching algorithm for completion doesn't get
4258 confused with Latin1 'ÿ' / 0xff. */
4259 {
4260 static const char str[] = "\377";
4261 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4262 EXPECT ("\377", "\377\377123"));
4263 }
4264
4265 /* Check that the increment-last-char in the matching algorithm for
4266 completion doesn't match "t1_fund" when completing "t1_func". */
4267 {
4268 static const char str[] = "t1_func";
4269 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4270 EXPECT ("t1_func", "t1_func1"));
4271 }
4272
c62446b1
PA
4273 /* Check that completion mode works at each prefix of the expected
4274 symbol name. */
4275 {
4276 static const char str[] = "function(int)";
4277 size_t len = strlen (str);
4278 std::string lookup;
4279
4280 for (size_t i = 1; i < len; i++)
4281 {
4282 lookup.assign (str, i);
4283 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4284 EXPECT ("function"));
4285 }
4286 }
4287
4288 /* While "w" is a prefix of both components, the match function
4289 should still only be called once. */
4290 {
4291 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4292 EXPECT ("w1::w2"));
a20714ff
PA
4293 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4294 EXPECT ("w1::w2"));
c62446b1
PA
4295 }
4296
4297 /* Same, with a "complicated" symbol. */
4298 {
4299 static const char str[] = Z_SYM_NAME;
4300 size_t len = strlen (str);
4301 std::string lookup;
4302
4303 for (size_t i = 1; i < len; i++)
4304 {
4305 lookup.assign (str, i);
4306 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4307 EXPECT (Z_SYM_NAME));
4308 }
4309 }
4310
4311 /* In FULL mode, an incomplete symbol doesn't match. */
4312 {
4313 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4314 {});
4315 }
4316
4317 /* A complete symbol with parameters matches any overload, since the
4318 index has no overload info. */
4319 {
4320 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4321 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4322 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4323 EXPECT ("std::zfunction", "std::zfunction2"));
4324 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4325 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4326 }
4327
4328 /* Check that whitespace is ignored appropriately. A symbol with a
4329 template argument list. */
4330 {
4331 static const char expected[] = "ns::foo<int>";
4332 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4333 EXPECT (expected));
a20714ff
PA
4334 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4335 EXPECT (expected));
c62446b1
PA
4336 }
4337
4338 /* Check that whitespace is ignored appropriately. A symbol with a
4339 template argument list that includes a pointer. */
4340 {
4341 static const char expected[] = "ns::foo<char*>";
4342 /* Try both completion and non-completion modes. */
4343 static const bool completion_mode[2] = {false, true};
4344 for (size_t i = 0; i < 2; i++)
4345 {
4346 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4347 completion_mode[i], EXPECT (expected));
a20714ff
PA
4348 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4349 completion_mode[i], EXPECT (expected));
c62446b1
PA
4350
4351 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4352 completion_mode[i], EXPECT (expected));
a20714ff
PA
4353 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4354 completion_mode[i], EXPECT (expected));
c62446b1
PA
4355 }
4356 }
4357
4358 {
4359 /* Check method qualifiers are ignored. */
4360 static const char expected[] = "ns::foo<char*>";
4361 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4362 symbol_name_match_type::FULL, true, EXPECT (expected));
4363 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4364 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4365 CHECK_MATCH ("foo < char * > ( int ) const",
4366 symbol_name_match_type::WILD, true, EXPECT (expected));
4367 CHECK_MATCH ("foo < char * > ( int ) &&",
4368 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4369 }
4370
4371 /* Test lookup names that don't match anything. */
4372 {
a20714ff
PA
4373 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4374 {});
4375
c62446b1
PA
4376 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4377 {});
4378 }
4379
a20714ff
PA
4380 /* Some wild matching tests, exercising "(anonymous namespace)",
4381 which should not be confused with a parameter list. */
4382 {
4383 static const char *syms[] = {
4384 "A::B::C",
4385 "B::C",
4386 "C",
4387 "A :: B :: C ( int )",
4388 "B :: C ( int )",
4389 "C ( int )",
4390 };
4391
4392 for (const char *s : syms)
4393 {
4394 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4395 EXPECT ("(anonymous namespace)::A::B::C"));
4396 }
4397 }
4398
4399 {
4400 static const char expected[] = "ns2::tmpl<int>::foo2";
4401 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4402 EXPECT (expected));
4403 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4404 EXPECT (expected));
4405 }
4406
c62446b1
PA
4407 SELF_CHECK (!any_mismatch);
4408
4409#undef EXPECT
4410#undef CHECK_MATCH
4411}
4412
5c58de74
PA
4413static void
4414run_test ()
4415{
4416 test_mapped_index_find_name_component_bounds ();
4417 test_dw2_expand_symtabs_matching_symbol ();
4418}
4419
c62446b1
PA
4420}} // namespace selftests::dw2_expand_symtabs_matching
4421
4422#endif /* GDB_SELF_TEST */
4423
4b514bc8
JK
4424/* If FILE_MATCHER is NULL or if PER_CU has
4425 dwarf2_per_cu_quick_data::MARK set (see
4426 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4427 EXPANSION_NOTIFY on it. */
4428
4429static void
4430dw2_expand_symtabs_matching_one
4431 (struct dwarf2_per_cu_data *per_cu,
4432 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4433 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4434{
4435 if (file_matcher == NULL || per_cu->v.quick->mark)
4436 {
4437 bool symtab_was_null
4438 = (per_cu->v.quick->compunit_symtab == NULL);
4439
58f0c718 4440 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4441
4442 if (expansion_notify != NULL
4443 && symtab_was_null
4444 && per_cu->v.quick->compunit_symtab != NULL)
4445 expansion_notify (per_cu->v.quick->compunit_symtab);
4446 }
4447}
4448
3f563c84
PA
4449/* Helper for dw2_expand_matching symtabs. Called on each symbol
4450 matched, to expand corresponding CUs that were marked. IDX is the
4451 index of the symbol name that matched. */
4452
4453static void
4454dw2_expand_marked_cus
ed2dc618 4455 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4456 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4457 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4458 search_domain kind)
4459{
3f563c84
PA
4460 offset_type *vec, vec_len, vec_idx;
4461 bool global_seen = false;
ed2dc618 4462 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4463
61920122 4464 vec = (offset_type *) (index.constant_pool
f00a2de2 4465 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4466 vec_len = MAYBE_SWAP (vec[0]);
4467 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4468 {
61920122
PA
4469 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4470 /* This value is only valid for index versions >= 7. */
4471 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4472 gdb_index_symbol_kind symbol_kind =
4473 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4474 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4475 /* Only check the symbol attributes if they're present.
4476 Indices prior to version 7 don't record them,
4477 and indices >= 7 may elide them for certain symbols
4478 (gold does this). */
4479 int attrs_valid =
4480 (index.version >= 7
4481 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4482
4483 /* Work around gold/15646. */
4484 if (attrs_valid)
9291a0cd 4485 {
61920122
PA
4486 if (!is_static && global_seen)
4487 continue;
4488 if (!is_static)
4489 global_seen = true;
4490 }
3190f0c6 4491
61920122
PA
4492 /* Only check the symbol's kind if it has one. */
4493 if (attrs_valid)
4494 {
4495 switch (kind)
8943b874 4496 {
61920122
PA
4497 case VARIABLES_DOMAIN:
4498 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4499 continue;
4500 break;
4501 case FUNCTIONS_DOMAIN:
4502 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4503 continue;
61920122
PA
4504 break;
4505 case TYPES_DOMAIN:
4506 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4507 continue;
4508 break;
59c35742
AB
4509 case MODULES_DOMAIN:
4510 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4511 continue;
4512 break;
61920122
PA
4513 default:
4514 break;
8943b874 4515 }
61920122 4516 }
8943b874 4517
61920122 4518 /* Don't crash on bad data. */
b76e467d 4519 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4520 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4521 {
b98664d3 4522 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4523 " [in module %s]"),
4524 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4525 continue;
4526 }
4527
ff4c9fec 4528 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4529 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4530 expansion_notify);
61920122
PA
4531 }
4532}
4533
4b514bc8
JK
4534/* If FILE_MATCHER is non-NULL, set all the
4535 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4536 that match FILE_MATCHER. */
4537
61920122 4538static void
4b514bc8 4539dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4540 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4541 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4542{
4b514bc8 4543 if (file_matcher == NULL)
61920122
PA
4544 return;
4545
4b514bc8
JK
4546 objfile *const objfile = dwarf2_per_objfile->objfile;
4547
4548 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4549 htab_eq_pointer,
4550 NULL, xcalloc, xfree));
4551 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4552 htab_eq_pointer,
4553 NULL, xcalloc, xfree));
61920122 4554
4b514bc8
JK
4555 /* The rule is CUs specify all the files, including those used by
4556 any TU, so there's no need to scan TUs here. */
61920122 4557
b76e467d 4558 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4559 {
927aa2e7
JK
4560 QUIT;
4561
4562 per_cu->v.quick->mark = 0;
4563
4564 /* We only need to look at symtabs not already expanded. */
4565 if (per_cu->v.quick->compunit_symtab)
4566 continue;
4567
b76e467d 4568 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4569 if (file_data == NULL)
4570 continue;
4571
4572 if (htab_find (visited_not_found.get (), file_data) != NULL)
4573 continue;
4574 else if (htab_find (visited_found.get (), file_data) != NULL)
4575 {
4576 per_cu->v.quick->mark = 1;
4577 continue;
4578 }
4579
b76e467d 4580 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4581 {
4582 const char *this_real_name;
4583
4584 if (file_matcher (file_data->file_names[j], false))
4585 {
4586 per_cu->v.quick->mark = 1;
4587 break;
4588 }
4589
4590 /* Before we invoke realpath, which can get expensive when many
4591 files are involved, do a quick comparison of the basenames. */
4592 if (!basenames_may_differ
4593 && !file_matcher (lbasename (file_data->file_names[j]),
4594 true))
4595 continue;
4596
4597 this_real_name = dw2_get_real_path (objfile, file_data, j);
4598 if (file_matcher (this_real_name, false))
4599 {
4600 per_cu->v.quick->mark = 1;
4601 break;
4602 }
4603 }
4604
b76e467d
SM
4605 void **slot = htab_find_slot (per_cu->v.quick->mark
4606 ? visited_found.get ()
4607 : visited_not_found.get (),
4608 file_data, INSERT);
927aa2e7
JK
4609 *slot = file_data;
4610 }
4611}
4612
4613static void
4614dw2_expand_symtabs_matching
4615 (struct objfile *objfile,
4616 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4617 const lookup_name_info &lookup_name,
4618 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4619 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4620 enum search_domain kind)
4621{
ed2dc618
SM
4622 struct dwarf2_per_objfile *dwarf2_per_objfile
4623 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4624
4625 /* index_table is NULL if OBJF_READNOW. */
4626 if (!dwarf2_per_objfile->index_table)
4627 return;
4628
ed2dc618 4629 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4630
4631 mapped_index &index = *dwarf2_per_objfile->index_table;
4632
4633 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4634 symbol_matcher,
4635 kind, [&] (offset_type idx)
4636 {
ed2dc618 4637 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4638 expansion_notify, kind);
3b00ef10 4639 return true;
927aa2e7
JK
4640 });
4641}
4642
4643/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4644 symtab. */
4645
4646static struct compunit_symtab *
4647recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4648 CORE_ADDR pc)
4649{
4650 int i;
4651
4652 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4653 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4654 return cust;
4655
4656 if (cust->includes == NULL)
4657 return NULL;
4658
4659 for (i = 0; cust->includes[i]; ++i)
4660 {
4661 struct compunit_symtab *s = cust->includes[i];
4662
4663 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4664 if (s != NULL)
4665 return s;
4666 }
4667
4668 return NULL;
4669}
4670
4671static struct compunit_symtab *
4672dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4673 struct bound_minimal_symbol msymbol,
4674 CORE_ADDR pc,
4675 struct obj_section *section,
4676 int warn_if_readin)
4677{
4678 struct dwarf2_per_cu_data *data;
4679 struct compunit_symtab *result;
4680
d320c2b5 4681 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4682 return NULL;
4683
b3b3bada 4684 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4685 data = (struct dwarf2_per_cu_data *) addrmap_find
4686 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4687 if (!data)
4688 return NULL;
4689
4690 if (warn_if_readin && data->v.quick->compunit_symtab)
4691 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4692 paddress (get_objfile_arch (objfile), pc));
4693
4694 result
58f0c718
TT
4695 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4696 false),
927aa2e7
JK
4697 pc);
4698 gdb_assert (result != NULL);
4699 return result;
4700}
4701
4702static void
4703dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4704 void *data, int need_fullname)
4705{
ed2dc618
SM
4706 struct dwarf2_per_objfile *dwarf2_per_objfile
4707 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4708
4709 if (!dwarf2_per_objfile->filenames_cache)
4710 {
4711 dwarf2_per_objfile->filenames_cache.emplace ();
4712
4713 htab_up visited (htab_create_alloc (10,
4714 htab_hash_pointer, htab_eq_pointer,
4715 NULL, xcalloc, xfree));
4716
4717 /* The rule is CUs specify all the files, including those used
4718 by any TU, so there's no need to scan TUs here. We can
4719 ignore file names coming from already-expanded CUs. */
4720
b76e467d 4721 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4722 {
927aa2e7
JK
4723 if (per_cu->v.quick->compunit_symtab)
4724 {
4725 void **slot = htab_find_slot (visited.get (),
4726 per_cu->v.quick->file_names,
4727 INSERT);
4728
4729 *slot = per_cu->v.quick->file_names;
4730 }
4731 }
4732
b76e467d 4733 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4734 {
927aa2e7
JK
4735 /* We only need to look at symtabs not already expanded. */
4736 if (per_cu->v.quick->compunit_symtab)
4737 continue;
4738
b76e467d 4739 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4740 if (file_data == NULL)
4741 continue;
4742
b76e467d 4743 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4744 if (*slot)
4745 {
4746 /* Already visited. */
4747 continue;
4748 }
4749 *slot = file_data;
4750
4751 for (int j = 0; j < file_data->num_file_names; ++j)
4752 {
4753 const char *filename = file_data->file_names[j];
4754 dwarf2_per_objfile->filenames_cache->seen (filename);
4755 }
4756 }
4757 }
4758
4759 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4760 {
4761 gdb::unique_xmalloc_ptr<char> this_real_name;
4762
4763 if (need_fullname)
4764 this_real_name = gdb_realpath (filename);
4765 (*fun) (filename, this_real_name.get (), data);
4766 });
4767}
4768
4769static int
4770dw2_has_symbols (struct objfile *objfile)
4771{
4772 return 1;
4773}
4774
4775const struct quick_symbol_functions dwarf2_gdb_index_functions =
4776{
4777 dw2_has_symbols,
4778 dw2_find_last_source_symtab,
4779 dw2_forget_cached_source_info,
4780 dw2_map_symtabs_matching_filename,
4781 dw2_lookup_symbol,
4782 dw2_print_stats,
4783 dw2_dump,
927aa2e7
JK
4784 dw2_expand_symtabs_for_function,
4785 dw2_expand_all_symtabs,
4786 dw2_expand_symtabs_with_fullname,
4787 dw2_map_matching_symbols,
4788 dw2_expand_symtabs_matching,
4789 dw2_find_pc_sect_compunit_symtab,
4790 NULL,
4791 dw2_map_symbol_filenames
4792};
4793
4794/* DWARF-5 debug_names reader. */
4795
4796/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4797static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4798
4799/* A helper function that reads the .debug_names section in SECTION
4800 and fills in MAP. FILENAME is the name of the file containing the
4801 section; it is used for error reporting.
4802
4803 Returns true if all went well, false otherwise. */
4804
4805static bool
4806read_debug_names_from_section (struct objfile *objfile,
4807 const char *filename,
4808 struct dwarf2_section_info *section,
4809 mapped_debug_names &map)
4810{
96b79293 4811 if (section->empty ())
927aa2e7
JK
4812 return false;
4813
4814 /* Older elfutils strip versions could keep the section in the main
4815 executable while splitting it for the separate debug info file. */
96b79293 4816 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4817 return false;
4818
96b79293 4819 section->read (objfile);
927aa2e7
JK
4820
4821 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4822
4823 const gdb_byte *addr = section->buffer;
4824
96b79293 4825 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4826
4827 unsigned int bytes_read;
4828 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4829 addr += bytes_read;
4830
4831 map.dwarf5_is_dwarf64 = bytes_read != 4;
4832 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4833 if (bytes_read + length != section->size)
4834 {
4835 /* There may be multiple per-CU indices. */
4836 warning (_("Section .debug_names in %s length %s does not match "
4837 "section length %s, ignoring .debug_names."),
4838 filename, plongest (bytes_read + length),
4839 pulongest (section->size));
4840 return false;
4841 }
4842
4843 /* The version number. */
4844 uint16_t version = read_2_bytes (abfd, addr);
4845 addr += 2;
4846 if (version != 5)
4847 {
4848 warning (_("Section .debug_names in %s has unsupported version %d, "
4849 "ignoring .debug_names."),
4850 filename, version);
4851 return false;
4852 }
4853
4854 /* Padding. */
4855 uint16_t padding = read_2_bytes (abfd, addr);
4856 addr += 2;
4857 if (padding != 0)
4858 {
4859 warning (_("Section .debug_names in %s has unsupported padding %d, "
4860 "ignoring .debug_names."),
4861 filename, padding);
4862 return false;
4863 }
4864
4865 /* comp_unit_count - The number of CUs in the CU list. */
4866 map.cu_count = read_4_bytes (abfd, addr);
4867 addr += 4;
4868
4869 /* local_type_unit_count - The number of TUs in the local TU
4870 list. */
4871 map.tu_count = read_4_bytes (abfd, addr);
4872 addr += 4;
4873
4874 /* foreign_type_unit_count - The number of TUs in the foreign TU
4875 list. */
4876 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4877 addr += 4;
4878 if (foreign_tu_count != 0)
4879 {
4880 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4881 "ignoring .debug_names."),
4882 filename, static_cast<unsigned long> (foreign_tu_count));
4883 return false;
4884 }
4885
4886 /* bucket_count - The number of hash buckets in the hash lookup
4887 table. */
4888 map.bucket_count = read_4_bytes (abfd, addr);
4889 addr += 4;
4890
4891 /* name_count - The number of unique names in the index. */
4892 map.name_count = read_4_bytes (abfd, addr);
4893 addr += 4;
4894
4895 /* abbrev_table_size - The size in bytes of the abbreviations
4896 table. */
4897 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4898 addr += 4;
4899
4900 /* augmentation_string_size - The size in bytes of the augmentation
4901 string. This value is rounded up to a multiple of 4. */
4902 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4903 addr += 4;
4904 map.augmentation_is_gdb = ((augmentation_string_size
4905 == sizeof (dwarf5_augmentation))
4906 && memcmp (addr, dwarf5_augmentation,
4907 sizeof (dwarf5_augmentation)) == 0);
4908 augmentation_string_size += (-augmentation_string_size) & 3;
4909 addr += augmentation_string_size;
4910
4911 /* List of CUs */
4912 map.cu_table_reordered = addr;
4913 addr += map.cu_count * map.offset_size;
4914
4915 /* List of Local TUs */
4916 map.tu_table_reordered = addr;
4917 addr += map.tu_count * map.offset_size;
4918
4919 /* Hash Lookup Table */
4920 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4921 addr += map.bucket_count * 4;
4922 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4923 addr += map.name_count * 4;
4924
4925 /* Name Table */
4926 map.name_table_string_offs_reordered = addr;
4927 addr += map.name_count * map.offset_size;
4928 map.name_table_entry_offs_reordered = addr;
4929 addr += map.name_count * map.offset_size;
4930
4931 const gdb_byte *abbrev_table_start = addr;
4932 for (;;)
4933 {
927aa2e7
JK
4934 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4935 addr += bytes_read;
4936 if (index_num == 0)
4937 break;
4938
4939 const auto insertpair
4940 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4941 if (!insertpair.second)
4942 {
4943 warning (_("Section .debug_names in %s has duplicate index %s, "
4944 "ignoring .debug_names."),
4945 filename, pulongest (index_num));
4946 return false;
4947 }
4948 mapped_debug_names::index_val &indexval = insertpair.first->second;
4949 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4950 addr += bytes_read;
4951
4952 for (;;)
4953 {
4954 mapped_debug_names::index_val::attr attr;
4955 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4956 addr += bytes_read;
4957 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4958 addr += bytes_read;
4959 if (attr.form == DW_FORM_implicit_const)
4960 {
4961 attr.implicit_const = read_signed_leb128 (abfd, addr,
4962 &bytes_read);
4963 addr += bytes_read;
4964 }
4965 if (attr.dw_idx == 0 && attr.form == 0)
4966 break;
4967 indexval.attr_vec.push_back (std::move (attr));
4968 }
4969 }
4970 if (addr != abbrev_table_start + abbrev_table_size)
4971 {
4972 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4973 "of size %s vs. written as %u, ignoring .debug_names."),
4974 filename, plongest (addr - abbrev_table_start),
4975 abbrev_table_size);
927aa2e7
JK
4976 return false;
4977 }
4978 map.entry_pool = addr;
4979
4980 return true;
4981}
4982
4983/* A helper for create_cus_from_debug_names that handles the MAP's CU
4984 list. */
4985
4986static void
ed2dc618 4987create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4988 const mapped_debug_names &map,
4989 dwarf2_section_info &section,
b76e467d 4990 bool is_dwz)
927aa2e7
JK
4991{
4992 sect_offset sect_off_prev;
4993 for (uint32_t i = 0; i <= map.cu_count; ++i)
4994 {
4995 sect_offset sect_off_next;
4996 if (i < map.cu_count)
4997 {
4998 sect_off_next
4999 = (sect_offset) (extract_unsigned_integer
5000 (map.cu_table_reordered + i * map.offset_size,
5001 map.offset_size,
5002 map.dwarf5_byte_order));
5003 }
5004 else
5005 sect_off_next = (sect_offset) section.size;
5006 if (i >= 1)
5007 {
5008 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5009 dwarf2_per_cu_data *per_cu
ed2dc618 5010 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5011 sect_off_prev, length);
b76e467d 5012 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5013 }
5014 sect_off_prev = sect_off_next;
5015 }
5016}
5017
5018/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5019 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5020
5021static void
ed2dc618 5022create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5023 const mapped_debug_names &map,
5024 const mapped_debug_names &dwz_map)
5025{
b76e467d
SM
5026 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5027 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5028
ed2dc618
SM
5029 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5030 dwarf2_per_objfile->info,
b76e467d 5031 false /* is_dwz */);
927aa2e7
JK
5032
5033 if (dwz_map.cu_count == 0)
5034 return;
5035
ed2dc618
SM
5036 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5037 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5038 true /* is_dwz */);
927aa2e7
JK
5039}
5040
5041/* Read .debug_names. If everything went ok, initialize the "quick"
5042 elements of all the CUs and return true. Otherwise, return false. */
5043
5044static bool
ed2dc618 5045dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5046{
22ca247e
TT
5047 std::unique_ptr<mapped_debug_names> map
5048 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5049 mapped_debug_names dwz_map (dwarf2_per_objfile);
5050 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5051
5052 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5053 &dwarf2_per_objfile->debug_names,
22ca247e 5054 *map))
927aa2e7
JK
5055 return false;
5056
5057 /* Don't use the index if it's empty. */
22ca247e 5058 if (map->name_count == 0)
927aa2e7
JK
5059 return false;
5060
5061 /* If there is a .dwz file, read it so we can get its CU list as
5062 well. */
ed2dc618 5063 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5064 if (dwz != NULL)
5065 {
5066 if (!read_debug_names_from_section (objfile,
00f93c44 5067 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5068 &dwz->debug_names, dwz_map))
5069 {
5070 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5071 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5072 return false;
5073 }
5074 }
5075
22ca247e 5076 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5077
22ca247e 5078 if (map->tu_count != 0)
927aa2e7
JK
5079 {
5080 /* We can only handle a single .debug_types when we have an
5081 index. */
fd5866f6 5082 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5083 return false;
5084
fd5866f6 5085 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5086
5087 create_signatured_type_table_from_debug_names
22ca247e 5088 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5089 }
5090
ed2dc618
SM
5091 create_addrmap_from_aranges (dwarf2_per_objfile,
5092 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5093
22ca247e 5094 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5095 dwarf2_per_objfile->using_index = 1;
5096 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5097 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5098
5099 return true;
5100}
5101
927aa2e7
JK
5102/* Type used to manage iterating over all CUs looking for a symbol for
5103 .debug_names. */
5104
5105class dw2_debug_names_iterator
5106{
5107public:
927aa2e7 5108 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5109 gdb::optional<block_enum> block_index,
5110 domain_enum domain,
927aa2e7 5111 const char *name)
2b79f376 5112 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5113 m_addr (find_vec_in_debug_names (map, name))
5114 {}
5115
5116 dw2_debug_names_iterator (const mapped_debug_names &map,
5117 search_domain search, uint32_t namei)
5118 : m_map (map),
5119 m_search (search),
5120 m_addr (find_vec_in_debug_names (map, namei))
5121 {}
5122
3b00ef10
TT
5123 dw2_debug_names_iterator (const mapped_debug_names &map,
5124 block_enum block_index, domain_enum domain,
5125 uint32_t namei)
5126 : m_map (map), m_block_index (block_index), m_domain (domain),
5127 m_addr (find_vec_in_debug_names (map, namei))
5128 {}
5129
927aa2e7
JK
5130 /* Return the next matching CU or NULL if there are no more. */
5131 dwarf2_per_cu_data *next ();
5132
5133private:
5134 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5135 const char *name);
5136 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5137 uint32_t namei);
5138
5139 /* The internalized form of .debug_names. */
5140 const mapped_debug_names &m_map;
5141
2b79f376
SM
5142 /* If set, only look for symbols that match that block. Valid values are
5143 GLOBAL_BLOCK and STATIC_BLOCK. */
5144 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5145
5146 /* The kind of symbol we're looking for. */
5147 const domain_enum m_domain = UNDEF_DOMAIN;
5148 const search_domain m_search = ALL_DOMAIN;
5149
5150 /* The list of CUs from the index entry of the symbol, or NULL if
5151 not found. */
5152 const gdb_byte *m_addr;
5153};
5154
5155const char *
5156mapped_debug_names::namei_to_name (uint32_t namei) const
5157{
5158 const ULONGEST namei_string_offs
5159 = extract_unsigned_integer ((name_table_string_offs_reordered
5160 + namei * offset_size),
5161 offset_size,
5162 dwarf5_byte_order);
5163 return read_indirect_string_at_offset
ed2dc618 5164 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5165}
5166
5167/* Find a slot in .debug_names for the object named NAME. If NAME is
5168 found, return pointer to its pool data. If NAME cannot be found,
5169 return NULL. */
5170
5171const gdb_byte *
5172dw2_debug_names_iterator::find_vec_in_debug_names
5173 (const mapped_debug_names &map, const char *name)
5174{
5175 int (*cmp) (const char *, const char *);
5176
54ee4252 5177 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5178 if (current_language->la_language == language_cplus
5179 || current_language->la_language == language_fortran
5180 || current_language->la_language == language_d)
5181 {
5182 /* NAME is already canonical. Drop any qualifiers as
5183 .debug_names does not contain any. */
5184
5185 if (strchr (name, '(') != NULL)
5186 {
54ee4252 5187 without_params = cp_remove_params (name);
927aa2e7 5188 if (without_params != NULL)
54ee4252 5189 name = without_params.get ();
927aa2e7
JK
5190 }
5191 }
5192
5193 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5194
5195 const uint32_t full_hash = dwarf5_djb_hash (name);
5196 uint32_t namei
5197 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5198 (map.bucket_table_reordered
5199 + (full_hash % map.bucket_count)), 4,
5200 map.dwarf5_byte_order);
5201 if (namei == 0)
5202 return NULL;
5203 --namei;
5204 if (namei >= map.name_count)
5205 {
b98664d3 5206 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5207 "[in module %s]"),
5208 namei, map.name_count,
ed2dc618 5209 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5210 return NULL;
5211 }
5212
5213 for (;;)
5214 {
5215 const uint32_t namei_full_hash
5216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5217 (map.hash_table_reordered + namei), 4,
5218 map.dwarf5_byte_order);
5219 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5220 return NULL;
5221
5222 if (full_hash == namei_full_hash)
5223 {
5224 const char *const namei_string = map.namei_to_name (namei);
5225
5226#if 0 /* An expensive sanity check. */
5227 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5228 {
b98664d3 5229 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5230 "[in module %s]"),
5231 namei, objfile_name (dwarf2_per_objfile->objfile));
5232 return NULL;
5233 }
5234#endif
5235
5236 if (cmp (namei_string, name) == 0)
5237 {
5238 const ULONGEST namei_entry_offs
5239 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5240 + namei * map.offset_size),
5241 map.offset_size, map.dwarf5_byte_order);
5242 return map.entry_pool + namei_entry_offs;
5243 }
5244 }
5245
5246 ++namei;
5247 if (namei >= map.name_count)
5248 return NULL;
5249 }
5250}
5251
5252const gdb_byte *
5253dw2_debug_names_iterator::find_vec_in_debug_names
5254 (const mapped_debug_names &map, uint32_t namei)
5255{
5256 if (namei >= map.name_count)
5257 {
b98664d3 5258 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5259 "[in module %s]"),
5260 namei, map.name_count,
ed2dc618 5261 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5262 return NULL;
5263 }
5264
5265 const ULONGEST namei_entry_offs
5266 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5267 + namei * map.offset_size),
5268 map.offset_size, map.dwarf5_byte_order);
5269 return map.entry_pool + namei_entry_offs;
5270}
5271
5272/* See dw2_debug_names_iterator. */
5273
5274dwarf2_per_cu_data *
5275dw2_debug_names_iterator::next ()
5276{
5277 if (m_addr == NULL)
5278 return NULL;
5279
ed2dc618
SM
5280 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5281 struct objfile *objfile = dwarf2_per_objfile->objfile;
5282 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5283
5284 again:
5285
5286 unsigned int bytes_read;
5287 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5288 m_addr += bytes_read;
5289 if (abbrev == 0)
5290 return NULL;
5291
5292 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5293 if (indexval_it == m_map.abbrev_map.cend ())
5294 {
b98664d3 5295 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5296 "[in module %s]"),
ed2dc618 5297 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5298 return NULL;
5299 }
5300 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5301 enum class symbol_linkage {
5302 unknown,
5303 static_,
5304 extern_,
23c13d42 5305 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5306 dwarf2_per_cu_data *per_cu = NULL;
5307 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5308 {
5309 ULONGEST ull;
5310 switch (attr.form)
5311 {
5312 case DW_FORM_implicit_const:
5313 ull = attr.implicit_const;
5314 break;
5315 case DW_FORM_flag_present:
5316 ull = 1;
5317 break;
5318 case DW_FORM_udata:
5319 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5320 m_addr += bytes_read;
5321 break;
5322 default:
b98664d3 5323 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5324 dwarf_form_name (attr.form),
ed2dc618 5325 objfile_name (objfile));
927aa2e7
JK
5326 return NULL;
5327 }
5328 switch (attr.dw_idx)
5329 {
5330 case DW_IDX_compile_unit:
5331 /* Don't crash on bad data. */
b76e467d 5332 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5333 {
b98664d3 5334 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5335 " [in module %s]"),
5336 pulongest (ull),
5337 objfile_name (dwarf2_per_objfile->objfile));
5338 continue;
5339 }
ff4c9fec 5340 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5341 break;
8af5c486
JK
5342 case DW_IDX_type_unit:
5343 /* Don't crash on bad data. */
b2bdb8cf 5344 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5345 {
b98664d3 5346 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5347 " [in module %s]"),
5348 pulongest (ull),
5349 objfile_name (dwarf2_per_objfile->objfile));
5350 continue;
5351 }
ff4c9fec 5352 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5353 break;
927aa2e7
JK
5354 case DW_IDX_GNU_internal:
5355 if (!m_map.augmentation_is_gdb)
5356 break;
23c13d42 5357 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5358 break;
5359 case DW_IDX_GNU_external:
5360 if (!m_map.augmentation_is_gdb)
5361 break;
23c13d42 5362 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5363 break;
5364 }
5365 }
5366
5367 /* Skip if already read in. */
5368 if (per_cu->v.quick->compunit_symtab)
5369 goto again;
5370
5371 /* Check static vs global. */
23c13d42 5372 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5373 {
2b79f376 5374 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5375 const bool symbol_is_static =
5376 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5377 if (want_static != symbol_is_static)
2b79f376 5378 goto again;
927aa2e7
JK
5379 }
5380
5381 /* Match dw2_symtab_iter_next, symbol_kind
5382 and debug_names::psymbol_tag. */
5383 switch (m_domain)
5384 {
5385 case VAR_DOMAIN:
5386 switch (indexval.dwarf_tag)
5387 {
5388 case DW_TAG_variable:
5389 case DW_TAG_subprogram:
5390 /* Some types are also in VAR_DOMAIN. */
5391 case DW_TAG_typedef:
5392 case DW_TAG_structure_type:
5393 break;
5394 default:
5395 goto again;
5396 }
5397 break;
5398 case STRUCT_DOMAIN:
5399 switch (indexval.dwarf_tag)
5400 {
5401 case DW_TAG_typedef:
5402 case DW_TAG_structure_type:
5403 break;
5404 default:
5405 goto again;
5406 }
5407 break;
5408 case LABEL_DOMAIN:
5409 switch (indexval.dwarf_tag)
5410 {
5411 case 0:
5412 case DW_TAG_variable:
5413 break;
5414 default:
5415 goto again;
5416 }
5417 break;
59c35742
AB
5418 case MODULE_DOMAIN:
5419 switch (indexval.dwarf_tag)
5420 {
5421 case DW_TAG_module:
5422 break;
5423 default:
5424 goto again;
5425 }
5426 break;
927aa2e7
JK
5427 default:
5428 break;
5429 }
5430
5431 /* Match dw2_expand_symtabs_matching, symbol_kind and
5432 debug_names::psymbol_tag. */
5433 switch (m_search)
4b514bc8 5434 {
927aa2e7
JK
5435 case VARIABLES_DOMAIN:
5436 switch (indexval.dwarf_tag)
4b514bc8 5437 {
927aa2e7
JK
5438 case DW_TAG_variable:
5439 break;
5440 default:
5441 goto again;
4b514bc8 5442 }
927aa2e7
JK
5443 break;
5444 case FUNCTIONS_DOMAIN:
5445 switch (indexval.dwarf_tag)
4b514bc8 5446 {
927aa2e7
JK
5447 case DW_TAG_subprogram:
5448 break;
5449 default:
5450 goto again;
4b514bc8 5451 }
927aa2e7
JK
5452 break;
5453 case TYPES_DOMAIN:
5454 switch (indexval.dwarf_tag)
5455 {
5456 case DW_TAG_typedef:
5457 case DW_TAG_structure_type:
5458 break;
5459 default:
5460 goto again;
5461 }
5462 break;
59c35742
AB
5463 case MODULES_DOMAIN:
5464 switch (indexval.dwarf_tag)
5465 {
5466 case DW_TAG_module:
5467 break;
5468 default:
5469 goto again;
5470 }
927aa2e7
JK
5471 default:
5472 break;
4b514bc8 5473 }
927aa2e7
JK
5474
5475 return per_cu;
4b514bc8 5476}
61920122 5477
927aa2e7 5478static struct compunit_symtab *
c7f839cb 5479dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5480 const char *name, domain_enum domain)
4b514bc8 5481{
ed2dc618
SM
5482 struct dwarf2_per_objfile *dwarf2_per_objfile
5483 = get_dwarf2_per_objfile (objfile);
61920122 5484
927aa2e7
JK
5485 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5486 if (!mapp)
61920122 5487 {
927aa2e7
JK
5488 /* index is NULL if OBJF_READNOW. */
5489 return NULL;
5490 }
5491 const auto &map = *mapp;
9291a0cd 5492
2b79f376 5493 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5494
927aa2e7
JK
5495 struct compunit_symtab *stab_best = NULL;
5496 struct dwarf2_per_cu_data *per_cu;
5497 while ((per_cu = iter.next ()) != NULL)
5498 {
5499 struct symbol *sym, *with_opaque = NULL;
58f0c718 5500 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5501 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5502 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5503
927aa2e7
JK
5504 sym = block_find_symbol (block, name, domain,
5505 block_find_non_opaque_type_preferred,
5506 &with_opaque);
9703b513 5507
927aa2e7
JK
5508 /* Some caution must be observed with overloaded functions and
5509 methods, since the index will not contain any overload
5510 information (but NAME might contain it). */
a3ec0bb1 5511
927aa2e7 5512 if (sym != NULL
987012b8 5513 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5514 return stab;
5515 if (with_opaque != NULL
987012b8 5516 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5517 stab_best = stab;
9703b513 5518
927aa2e7 5519 /* Keep looking through other CUs. */
9703b513
TT
5520 }
5521
927aa2e7 5522 return stab_best;
9703b513
TT
5523}
5524
927aa2e7
JK
5525/* This dumps minimal information about .debug_names. It is called
5526 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5527 uses this to verify that .debug_names has been loaded. */
9291a0cd 5528
927aa2e7
JK
5529static void
5530dw2_debug_names_dump (struct objfile *objfile)
5531{
ed2dc618
SM
5532 struct dwarf2_per_objfile *dwarf2_per_objfile
5533 = get_dwarf2_per_objfile (objfile);
5534
927aa2e7
JK
5535 gdb_assert (dwarf2_per_objfile->using_index);
5536 printf_filtered (".debug_names:");
5537 if (dwarf2_per_objfile->debug_names_table)
5538 printf_filtered (" exists\n");
5539 else
5540 printf_filtered (" faked for \"readnow\"\n");
5541 printf_filtered ("\n");
9291a0cd
TT
5542}
5543
9291a0cd 5544static void
927aa2e7
JK
5545dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5546 const char *func_name)
9291a0cd 5547{
ed2dc618
SM
5548 struct dwarf2_per_objfile *dwarf2_per_objfile
5549 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5550
927aa2e7
JK
5551 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5552 if (dwarf2_per_objfile->debug_names_table)
24c79950 5553 {
927aa2e7 5554 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5555
2b79f376 5556 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5557
927aa2e7
JK
5558 struct dwarf2_per_cu_data *per_cu;
5559 while ((per_cu = iter.next ()) != NULL)
58f0c718 5560 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5561 }
5562}
24c79950 5563
3b00ef10
TT
5564static void
5565dw2_debug_names_map_matching_symbols
5566 (struct objfile *objfile,
5567 const lookup_name_info &name, domain_enum domain,
5568 int global,
5569 gdb::function_view<symbol_found_callback_ftype> callback,
5570 symbol_compare_ftype *ordered_compare)
5571{
5572 struct dwarf2_per_objfile *dwarf2_per_objfile
5573 = get_dwarf2_per_objfile (objfile);
5574
5575 /* debug_names_table is NULL if OBJF_READNOW. */
5576 if (!dwarf2_per_objfile->debug_names_table)
5577 return;
5578
5579 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5580 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5581
5582 const char *match_name = name.ada ().lookup_name ().c_str ();
5583 auto matcher = [&] (const char *symname)
5584 {
5585 if (ordered_compare == nullptr)
5586 return true;
5587 return ordered_compare (symname, match_name) == 0;
5588 };
5589
5590 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5591 [&] (offset_type namei)
5592 {
5593 /* The name was matched, now expand corresponding CUs that were
5594 marked. */
5595 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5596
5597 struct dwarf2_per_cu_data *per_cu;
5598 while ((per_cu = iter.next ()) != NULL)
5599 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5600 return true;
5601 });
5602
5603 /* It's a shame we couldn't do this inside the
5604 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5605 that have already been expanded. Instead, this loop matches what
5606 the psymtab code does. */
5607 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5608 {
5609 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5610 if (cust != nullptr)
5611 {
5612 const struct block *block
5613 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5614 if (!iterate_over_symbols_terminated (block, name,
5615 domain, callback))
5616 break;
5617 }
5618 }
5619}
5620
927aa2e7
JK
5621static void
5622dw2_debug_names_expand_symtabs_matching
5623 (struct objfile *objfile,
5624 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5625 const lookup_name_info &lookup_name,
5626 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5627 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5628 enum search_domain kind)
5629{
ed2dc618
SM
5630 struct dwarf2_per_objfile *dwarf2_per_objfile
5631 = get_dwarf2_per_objfile (objfile);
9291a0cd 5632
927aa2e7
JK
5633 /* debug_names_table is NULL if OBJF_READNOW. */
5634 if (!dwarf2_per_objfile->debug_names_table)
5635 return;
9291a0cd 5636
ed2dc618 5637 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5638
44ed8f3e 5639 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5640
44ed8f3e
PA
5641 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5642 symbol_matcher,
5643 kind, [&] (offset_type namei)
927aa2e7 5644 {
927aa2e7
JK
5645 /* The name was matched, now expand corresponding CUs that were
5646 marked. */
5647 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5648
927aa2e7
JK
5649 struct dwarf2_per_cu_data *per_cu;
5650 while ((per_cu = iter.next ()) != NULL)
5651 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5652 expansion_notify);
3b00ef10 5653 return true;
44ed8f3e 5654 });
9291a0cd
TT
5655}
5656
927aa2e7 5657const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5658{
5659 dw2_has_symbols,
5660 dw2_find_last_source_symtab,
5661 dw2_forget_cached_source_info,
f8eba3c6 5662 dw2_map_symtabs_matching_filename,
927aa2e7 5663 dw2_debug_names_lookup_symbol,
9291a0cd 5664 dw2_print_stats,
927aa2e7 5665 dw2_debug_names_dump,
927aa2e7 5666 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5667 dw2_expand_all_symtabs,
652a8996 5668 dw2_expand_symtabs_with_fullname,
3b00ef10 5669 dw2_debug_names_map_matching_symbols,
927aa2e7 5670 dw2_debug_names_expand_symtabs_matching,
43f3e411 5671 dw2_find_pc_sect_compunit_symtab,
71a3c369 5672 NULL,
9291a0cd
TT
5673 dw2_map_symbol_filenames
5674};
5675
4485a1c1
SM
5676/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5677 to either a dwarf2_per_objfile or dwz_file object. */
5678
5679template <typename T>
5680static gdb::array_view<const gdb_byte>
5681get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5682{
5683 dwarf2_section_info *section = &section_owner->gdb_index;
5684
96b79293 5685 if (section->empty ())
4485a1c1
SM
5686 return {};
5687
5688 /* Older elfutils strip versions could keep the section in the main
5689 executable while splitting it for the separate debug info file. */
96b79293 5690 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5691 return {};
5692
96b79293 5693 section->read (obj);
4485a1c1 5694
8bebfcda
PA
5695 /* dwarf2_section_info::size is a bfd_size_type, while
5696 gdb::array_view works with size_t. On 32-bit hosts, with
5697 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5698 is 32-bit. So we need an explicit narrowing conversion here.
5699 This is fine, because it's impossible to allocate or mmap an
5700 array/buffer larger than what size_t can represent. */
5701 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5702}
5703
87d6a7aa
SM
5704/* Lookup the index cache for the contents of the index associated to
5705 DWARF2_OBJ. */
5706
5707static gdb::array_view<const gdb_byte>
5708get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5709{
5710 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5711 if (build_id == nullptr)
5712 return {};
5713
5714 return global_index_cache.lookup_gdb_index (build_id,
5715 &dwarf2_obj->index_cache_res);
5716}
5717
5718/* Same as the above, but for DWZ. */
5719
5720static gdb::array_view<const gdb_byte>
5721get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5722{
5723 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5724 if (build_id == nullptr)
5725 return {};
5726
5727 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5728}
5729
3c0aa29a 5730/* See symfile.h. */
9291a0cd 5731
3c0aa29a
PA
5732bool
5733dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5734{
ed2dc618
SM
5735 struct dwarf2_per_objfile *dwarf2_per_objfile
5736 = get_dwarf2_per_objfile (objfile);
5737
9291a0cd
TT
5738 /* If we're about to read full symbols, don't bother with the
5739 indices. In this case we also don't care if some other debug
5740 format is making psymtabs, because they are all about to be
5741 expanded anyway. */
5742 if ((objfile->flags & OBJF_READNOW))
5743 {
9291a0cd 5744 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5745 create_all_comp_units (dwarf2_per_objfile);
5746 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5747 dwarf2_per_objfile->quick_file_names_table
5748 = create_quick_file_names_table
5749 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5750
b76e467d 5751 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5752 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5753 {
ff4c9fec 5754 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5755
e254ef6a
DE
5756 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5757 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5758 }
5759
5760 /* Return 1 so that gdb sees the "quick" functions. However,
5761 these functions will be no-ops because we will have expanded
5762 all symtabs. */
3c0aa29a
PA
5763 *index_kind = dw_index_kind::GDB_INDEX;
5764 return true;
9291a0cd
TT
5765 }
5766
ed2dc618 5767 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5768 {
5769 *index_kind = dw_index_kind::DEBUG_NAMES;
5770 return true;
5771 }
927aa2e7 5772
4485a1c1
SM
5773 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5774 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5775 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5776 {
5777 *index_kind = dw_index_kind::GDB_INDEX;
5778 return true;
5779 }
9291a0cd 5780
87d6a7aa
SM
5781 /* ... otherwise, try to find the index in the index cache. */
5782 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5783 get_gdb_index_contents_from_cache,
5784 get_gdb_index_contents_from_cache_dwz))
5785 {
5786 global_index_cache.hit ();
5787 *index_kind = dw_index_kind::GDB_INDEX;
5788 return true;
5789 }
5790
5791 global_index_cache.miss ();
3c0aa29a 5792 return false;
9291a0cd
TT
5793}
5794
5795\f
5796
dce234bc
PP
5797/* Build a partial symbol table. */
5798
5799void
f29dff0a 5800dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5801{
ed2dc618
SM
5802 struct dwarf2_per_objfile *dwarf2_per_objfile
5803 = get_dwarf2_per_objfile (objfile);
c9bf0622 5804
6eee24ce 5805 init_psymbol_list (objfile, 1024);
c906108c 5806
a70b8144 5807 try
c9bf0622
TT
5808 {
5809 /* This isn't really ideal: all the data we allocate on the
5810 objfile's obstack is still uselessly kept around. However,
5811 freeing it seems unsafe. */
906768f9 5812 psymtab_discarder psymtabs (objfile);
ed2dc618 5813 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5814 psymtabs.keep ();
87d6a7aa
SM
5815
5816 /* (maybe) store an index in the cache. */
5817 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5818 }
230d2906 5819 catch (const gdb_exception_error &except)
492d29ea
PA
5820 {
5821 exception_print (gdb_stderr, except);
5822 }
c906108c 5823}
c906108c 5824
3b80fe9b
DE
5825/* Find the base address of the compilation unit for range lists and
5826 location lists. It will normally be specified by DW_AT_low_pc.
5827 In DWARF-3 draft 4, the base address could be overridden by
5828 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5829 compilation units with discontinuous ranges. */
5830
5831static void
5832dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5833{
5834 struct attribute *attr;
5835
5836 cu->base_known = 0;
5837 cu->base_address = 0;
5838
5839 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5840 if (attr != nullptr)
3b80fe9b 5841 {
cd6c91b4 5842 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5843 cu->base_known = 1;
5844 }
5845 else
5846 {
5847 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5848 if (attr != nullptr)
3b80fe9b 5849 {
cd6c91b4 5850 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5851 cu->base_known = 1;
5852 }
5853 }
5854}
5855
36586728
TT
5856/* Helper function that returns the proper abbrev section for
5857 THIS_CU. */
5858
5859static struct dwarf2_section_info *
5860get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5861{
5862 struct dwarf2_section_info *abbrev;
ed2dc618 5863 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5864
5865 if (this_cu->is_dwz)
ed2dc618 5866 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5867 else
5868 abbrev = &dwarf2_per_objfile->abbrev;
5869
5870 return abbrev;
5871}
5872
f4dc4d17
DE
5873/* Fetch the abbreviation table offset from a comp or type unit header. */
5874
5875static sect_offset
ed2dc618
SM
5876read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5877 struct dwarf2_section_info *section,
9c541725 5878 sect_offset sect_off)
f4dc4d17 5879{
96b79293 5880 bfd *abfd = section->get_bfd_owner ();
d521ce57 5881 const gdb_byte *info_ptr;
ac298888 5882 unsigned int initial_length_size, offset_size;
43988095 5883 uint16_t version;
f4dc4d17 5884
96b79293 5885 section->read (dwarf2_per_objfile->objfile);
9c541725 5886 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5887 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5888 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5889 info_ptr += initial_length_size;
5890
5891 version = read_2_bytes (abfd, info_ptr);
5892 info_ptr += 2;
5893 if (version >= 5)
5894 {
5895 /* Skip unit type and address size. */
5896 info_ptr += 2;
5897 }
5898
24aa364d 5899 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5900}
5901
aaa75496
JB
5902/* Allocate a new partial symtab for file named NAME and mark this new
5903 partial symtab as being an include of PST. */
5904
5905static void
891813be 5906dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5907 struct objfile *objfile)
5908{
891813be 5909 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 5910
fbd9ab74
JK
5911 if (!IS_ABSOLUTE_PATH (subpst->filename))
5912 {
5913 /* It shares objfile->objfile_obstack. */
5914 subpst->dirname = pst->dirname;
5915 }
5916
a9342b62 5917 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5918 subpst->dependencies[0] = pst;
5919 subpst->number_of_dependencies = 1;
5920
aaa75496
JB
5921 /* No private part is necessary for include psymtabs. This property
5922 can be used to differentiate between such include psymtabs and
10b3939b 5923 the regular ones. */
891813be 5924 subpst->per_cu_data = nullptr;
aaa75496
JB
5925}
5926
5927/* Read the Line Number Program data and extract the list of files
5928 included by the source file represented by PST. Build an include
d85a05f0 5929 partial symtab for each of these included files. */
aaa75496
JB
5930
5931static void
5932dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5933 struct die_info *die,
891813be 5934 dwarf2_psymtab *pst)
aaa75496 5935{
fff8551c 5936 line_header_up lh;
d85a05f0 5937 struct attribute *attr;
aaa75496 5938
d85a05f0 5939 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5940 if (attr != nullptr)
9c541725 5941 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5942 if (lh == NULL)
5943 return; /* No linetable, so no includes. */
5944
79748972
TT
5945 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5946 that we pass in the raw text_low here; that is ok because we're
5947 only decoding the line table to make include partial symtabs, and
5948 so the addresses aren't really used. */
4ae976d1 5949 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5950 pst->raw_text_low (), 1);
aaa75496
JB
5951}
5952
348e048f 5953static hashval_t
52dc124a 5954hash_signatured_type (const void *item)
348e048f 5955{
9a3c8263
SM
5956 const struct signatured_type *sig_type
5957 = (const struct signatured_type *) item;
9a619af0 5958
348e048f 5959 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5960 return sig_type->signature;
348e048f
DE
5961}
5962
5963static int
52dc124a 5964eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5965{
9a3c8263
SM
5966 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5967 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5968
348e048f
DE
5969 return lhs->signature == rhs->signature;
5970}
5971
1fd400ff
TT
5972/* Allocate a hash table for signatured types. */
5973
b0b6a987 5974static htab_up
673bfd45 5975allocate_signatured_type_table (struct objfile *objfile)
1fd400ff 5976{
b0b6a987
TT
5977 return htab_up (htab_create_alloc (41,
5978 hash_signatured_type,
5979 eq_signatured_type,
5980 NULL, xcalloc, xfree));
1fd400ff
TT
5981}
5982
d467dd73 5983/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5984
5985static int
d467dd73 5986add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5987{
9a3c8263 5988 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
5989 std::vector<signatured_type *> *all_type_units
5990 = (std::vector<signatured_type *> *) datum;
1fd400ff 5991
b2bdb8cf 5992 all_type_units->push_back (sigt);
1fd400ff
TT
5993
5994 return 1;
5995}
5996
78d4d2c5 5997/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5998 and fill them into TYPES_HTAB. It will process only type units,
5999 therefore DW_UT_type. */
c88ee1f0 6000
78d4d2c5 6001static void
ed2dc618
SM
6002create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6003 struct dwo_file *dwo_file,
b0b6a987 6004 dwarf2_section_info *section, htab_up &types_htab,
43988095 6005 rcuh_kind section_kind)
348e048f 6006{
3019eac3 6007 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6008 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6009 bfd *abfd;
6010 const gdb_byte *info_ptr, *end_ptr;
348e048f 6011
4bdcc0c1
DE
6012 abbrev_section = (dwo_file != NULL
6013 ? &dwo_file->sections.abbrev
6014 : &dwarf2_per_objfile->abbrev);
6015
b4f54984 6016 if (dwarf_read_debug)
43988095 6017 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6018 section->get_name (),
6019 abbrev_section->get_file_name ());
09406207 6020
96b79293 6021 section->read (objfile);
78d4d2c5 6022 info_ptr = section->buffer;
348e048f 6023
78d4d2c5
JK
6024 if (info_ptr == NULL)
6025 return;
348e048f 6026
78d4d2c5
JK
6027 /* We can't set abfd until now because the section may be empty or
6028 not present, in which case the bfd is unknown. */
96b79293 6029 abfd = section->get_bfd_owner ();
348e048f 6030
c0ab21c2
TT
6031 /* We don't use cutu_reader here because we don't need to read
6032 any dies: the signature is in the header. */
3019eac3 6033
78d4d2c5
JK
6034 end_ptr = info_ptr + section->size;
6035 while (info_ptr < end_ptr)
6036 {
78d4d2c5
JK
6037 struct signatured_type *sig_type;
6038 struct dwo_unit *dwo_tu;
6039 void **slot;
6040 const gdb_byte *ptr = info_ptr;
6041 struct comp_unit_head header;
6042 unsigned int length;
8b70b953 6043
9c541725 6044 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6045
a49dd8dd
JK
6046 /* Initialize it due to a false compiler warning. */
6047 header.signature = -1;
9c541725 6048 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6049
78d4d2c5
JK
6050 /* We need to read the type's signature in order to build the hash
6051 table, but we don't need anything else just yet. */
348e048f 6052
ed2dc618 6053 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6054 abbrev_section, ptr, section_kind);
348e048f 6055
4057dfde 6056 length = header.get_length ();
6caca83c 6057
78d4d2c5
JK
6058 /* Skip dummy type units. */
6059 if (ptr >= info_ptr + length
43988095
JK
6060 || peek_abbrev_code (abfd, ptr) == 0
6061 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6062 {
6063 info_ptr += length;
6064 continue;
6065 }
dee91e82 6066
78d4d2c5
JK
6067 if (types_htab == NULL)
6068 {
6069 if (dwo_file)
6070 types_htab = allocate_dwo_unit_table (objfile);
6071 else
6072 types_htab = allocate_signatured_type_table (objfile);
6073 }
8b70b953 6074
78d4d2c5
JK
6075 if (dwo_file)
6076 {
6077 sig_type = NULL;
6078 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6079 struct dwo_unit);
6080 dwo_tu->dwo_file = dwo_file;
43988095 6081 dwo_tu->signature = header.signature;
9c541725 6082 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6083 dwo_tu->section = section;
9c541725 6084 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6085 dwo_tu->length = length;
6086 }
6087 else
6088 {
6089 /* N.B.: type_offset is not usable if this type uses a DWO file.
6090 The real type_offset is in the DWO file. */
6091 dwo_tu = NULL;
6092 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6093 struct signatured_type);
43988095 6094 sig_type->signature = header.signature;
9c541725 6095 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6096 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6097 sig_type->per_cu.is_debug_types = 1;
6098 sig_type->per_cu.section = section;
9c541725 6099 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6100 sig_type->per_cu.length = length;
6101 }
6102
b0b6a987 6103 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6104 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6105 INSERT);
6106 gdb_assert (slot != NULL);
6107 if (*slot != NULL)
6108 {
9c541725 6109 sect_offset dup_sect_off;
0349ea22 6110
3019eac3
DE
6111 if (dwo_file)
6112 {
78d4d2c5
JK
6113 const struct dwo_unit *dup_tu
6114 = (const struct dwo_unit *) *slot;
6115
9c541725 6116 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6117 }
6118 else
6119 {
78d4d2c5
JK
6120 const struct signatured_type *dup_tu
6121 = (const struct signatured_type *) *slot;
6122
9c541725 6123 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6124 }
8b70b953 6125
b98664d3 6126 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6127 " the entry at offset %s, signature %s"),
6128 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6129 hex_string (header.signature));
78d4d2c5
JK
6130 }
6131 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6132
78d4d2c5 6133 if (dwarf_read_debug > 1)
9d8780f0
SM
6134 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6135 sect_offset_str (sect_off),
43988095 6136 hex_string (header.signature));
3019eac3 6137
78d4d2c5
JK
6138 info_ptr += length;
6139 }
6140}
3019eac3 6141
78d4d2c5
JK
6142/* Create the hash table of all entries in the .debug_types
6143 (or .debug_types.dwo) section(s).
6144 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6145 otherwise it is NULL.
b3c8eb43 6146
78d4d2c5 6147 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6148
78d4d2c5 6149 Note: This function processes DWO files only, not DWP files. */
348e048f 6150
78d4d2c5 6151static void
ed2dc618
SM
6152create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6153 struct dwo_file *dwo_file,
fd5866f6 6154 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6155 htab_up &types_htab)
78d4d2c5 6156{
fd5866f6
SM
6157 for (dwarf2_section_info &section : type_sections)
6158 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6159 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6160}
6161
6162/* Create the hash table of all entries in the .debug_types section,
6163 and initialize all_type_units.
6164 The result is zero if there is an error (e.g. missing .debug_types section),
6165 otherwise non-zero. */
6166
6167static int
ed2dc618 6168create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6169{
b0b6a987 6170 htab_up types_htab;
3019eac3 6171
ed2dc618
SM
6172 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6173 &dwarf2_per_objfile->info, types_htab,
43988095 6174 rcuh_kind::COMPILE);
ed2dc618
SM
6175 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6176 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6177 if (types_htab == NULL)
6178 {
6179 dwarf2_per_objfile->signatured_types = NULL;
6180 return 0;
6181 }
6182
b0b6a987 6183 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6184
b2bdb8cf 6185 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6186 dwarf2_per_objfile->all_type_units.reserve
6187 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6188
b0b6a987
TT
6189 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6190 add_signatured_type_cu_to_table,
b2bdb8cf 6191 &dwarf2_per_objfile->all_type_units);
1fd400ff 6192
348e048f
DE
6193 return 1;
6194}
6195
6aa5f3a6
DE
6196/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6197 If SLOT is non-NULL, it is the entry to use in the hash table.
6198 Otherwise we find one. */
6199
6200static struct signatured_type *
ed2dc618
SM
6201add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6202 void **slot)
6aa5f3a6
DE
6203{
6204 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6205
b2bdb8cf
SM
6206 if (dwarf2_per_objfile->all_type_units.size ()
6207 == dwarf2_per_objfile->all_type_units.capacity ())
6208 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6209
b2bdb8cf
SM
6210 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6211 struct signatured_type);
6212
6213 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6214 sig_type->signature = sig;
6215 sig_type->per_cu.is_debug_types = 1;
6216 if (dwarf2_per_objfile->using_index)
6217 {
6218 sig_type->per_cu.v.quick =
6219 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6220 struct dwarf2_per_cu_quick_data);
6221 }
6222
6223 if (slot == NULL)
6224 {
b0b6a987 6225 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6226 sig_type, INSERT);
6227 }
6228 gdb_assert (*slot == NULL);
6229 *slot = sig_type;
6230 /* The rest of sig_type must be filled in by the caller. */
6231 return sig_type;
6232}
6233
a2ce51a0
DE
6234/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6235 Fill in SIG_ENTRY with DWO_ENTRY. */
6236
6237static void
ed2dc618 6238fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6239 struct signatured_type *sig_entry,
6240 struct dwo_unit *dwo_entry)
6241{
7ee85ab1 6242 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6243 gdb_assert (! sig_entry->per_cu.queued);
6244 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6245 if (dwarf2_per_objfile->using_index)
6246 {
6247 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6248 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6249 }
6250 else
6251 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6252 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6253 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6254 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6255 gdb_assert (sig_entry->dwo_unit == NULL);
6256
6257 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6258 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6259 sig_entry->per_cu.length = dwo_entry->length;
6260 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6261 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6262 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6263 sig_entry->dwo_unit = dwo_entry;
6264}
6265
6266/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6267 If we haven't read the TU yet, create the signatured_type data structure
6268 for a TU to be read in directly from a DWO file, bypassing the stub.
6269 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6270 using .gdb_index, then when reading a CU we want to stay in the DWO file
6271 containing that CU. Otherwise we could end up reading several other DWO
6272 files (due to comdat folding) to process the transitive closure of all the
6273 mentioned TUs, and that can be slow. The current DWO file will have every
6274 type signature that it needs.
a2ce51a0
DE
6275 We only do this for .gdb_index because in the psymtab case we already have
6276 to read all the DWOs to build the type unit groups. */
6277
6278static struct signatured_type *
6279lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6280{
518817b3
SM
6281 struct dwarf2_per_objfile *dwarf2_per_objfile
6282 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6283 struct objfile *objfile = dwarf2_per_objfile->objfile;
6284 struct dwo_file *dwo_file;
6285 struct dwo_unit find_dwo_entry, *dwo_entry;
6286 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6287 void **slot;
a2ce51a0
DE
6288
6289 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6290
6aa5f3a6
DE
6291 /* If TU skeletons have been removed then we may not have read in any
6292 TUs yet. */
6293 if (dwarf2_per_objfile->signatured_types == NULL)
6294 {
6295 dwarf2_per_objfile->signatured_types
6296 = allocate_signatured_type_table (objfile);
6297 }
a2ce51a0
DE
6298
6299 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6300 Use the global signatured_types array to do our own comdat-folding
6301 of types. If this is the first time we're reading this TU, and
6302 the TU has an entry in .gdb_index, replace the recorded data from
6303 .gdb_index with this TU. */
a2ce51a0 6304
a2ce51a0 6305 find_sig_entry.signature = sig;
b0b6a987 6306 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6307 &find_sig_entry, INSERT);
9a3c8263 6308 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6309
6310 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6311 read. Don't reassign the global entry to point to this DWO if that's
6312 the case. Also note that if the TU is already being read, it may not
6313 have come from a DWO, the program may be a mix of Fission-compiled
6314 code and non-Fission-compiled code. */
6315
6316 /* Have we already tried to read this TU?
6317 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6318 needn't exist in the global table yet). */
6319 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6320 return sig_entry;
6321
6aa5f3a6
DE
6322 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6323 dwo_unit of the TU itself. */
6324 dwo_file = cu->dwo_unit->dwo_file;
6325
a2ce51a0
DE
6326 /* Ok, this is the first time we're reading this TU. */
6327 if (dwo_file->tus == NULL)
6328 return NULL;
6329 find_dwo_entry.signature = sig;
b0b6a987
TT
6330 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6331 &find_dwo_entry);
a2ce51a0
DE
6332 if (dwo_entry == NULL)
6333 return NULL;
6334
6aa5f3a6
DE
6335 /* If the global table doesn't have an entry for this TU, add one. */
6336 if (sig_entry == NULL)
ed2dc618 6337 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6338
ed2dc618 6339 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6340 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6341 return sig_entry;
6342}
6343
a2ce51a0
DE
6344/* Subroutine of lookup_signatured_type.
6345 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6346 then try the DWP file. If the TU stub (skeleton) has been removed then
6347 it won't be in .gdb_index. */
a2ce51a0
DE
6348
6349static struct signatured_type *
6350lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6351{
518817b3
SM
6352 struct dwarf2_per_objfile *dwarf2_per_objfile
6353 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6354 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6355 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6356 struct dwo_unit *dwo_entry;
6357 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6358 void **slot;
a2ce51a0
DE
6359
6360 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6361 gdb_assert (dwp_file != NULL);
6362
6aa5f3a6
DE
6363 /* If TU skeletons have been removed then we may not have read in any
6364 TUs yet. */
6365 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6366 {
6aa5f3a6
DE
6367 dwarf2_per_objfile->signatured_types
6368 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6369 }
6370
6aa5f3a6 6371 find_sig_entry.signature = sig;
b0b6a987 6372 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6373 &find_sig_entry, INSERT);
9a3c8263 6374 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6375
6376 /* Have we already tried to read this TU?
6377 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6378 needn't exist in the global table yet). */
6379 if (sig_entry != NULL)
6380 return sig_entry;
6381
a2ce51a0
DE
6382 if (dwp_file->tus == NULL)
6383 return NULL;
ed2dc618 6384 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6385 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6386 if (dwo_entry == NULL)
6387 return NULL;
6388
ed2dc618
SM
6389 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6390 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6391
a2ce51a0
DE
6392 return sig_entry;
6393}
6394
380bca97 6395/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6396 Returns NULL if signature SIG is not present in the table.
6397 It is up to the caller to complain about this. */
348e048f
DE
6398
6399static struct signatured_type *
a2ce51a0 6400lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6401{
518817b3
SM
6402 struct dwarf2_per_objfile *dwarf2_per_objfile
6403 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6404
a2ce51a0
DE
6405 if (cu->dwo_unit
6406 && dwarf2_per_objfile->using_index)
6407 {
6408 /* We're in a DWO/DWP file, and we're using .gdb_index.
6409 These cases require special processing. */
ed2dc618 6410 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6411 return lookup_dwo_signatured_type (cu, sig);
6412 else
6413 return lookup_dwp_signatured_type (cu, sig);
6414 }
6415 else
6416 {
6417 struct signatured_type find_entry, *entry;
348e048f 6418
a2ce51a0
DE
6419 if (dwarf2_per_objfile->signatured_types == NULL)
6420 return NULL;
6421 find_entry.signature = sig;
9a3c8263 6422 entry = ((struct signatured_type *)
b0b6a987
TT
6423 htab_find (dwarf2_per_objfile->signatured_types.get (),
6424 &find_entry));
a2ce51a0
DE
6425 return entry;
6426 }
348e048f 6427}
18a8505e
AT
6428
6429/* Return the address base of the compile unit, which, if exists, is stored
6430 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6431static gdb::optional<ULONGEST>
6432lookup_addr_base (struct die_info *comp_unit_die)
6433{
6434 struct attribute *attr;
6435 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6436 if (attr == nullptr)
6437 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6438 if (attr == nullptr)
6439 return gdb::optional<ULONGEST> ();
6440 return DW_UNSND (attr);
6441}
6442
6443/* Return range lists base of the compile unit, which, if exists, is stored
6444 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6445static ULONGEST
6446lookup_ranges_base (struct die_info *comp_unit_die)
6447{
6448 struct attribute *attr;
6449 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6450 if (attr == nullptr)
6451 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6452 if (attr == nullptr)
6453 return 0;
6454 return DW_UNSND (attr);
6455}
6456
42e7ad6c 6457/* Low level DIE reading support. */
348e048f 6458
d85a05f0
DJ
6459/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6460
6461static void
6462init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6463 struct dwarf2_cu *cu,
3019eac3 6464 struct dwarf2_section_info *section,
685af9cd
TT
6465 struct dwo_file *dwo_file,
6466 struct abbrev_table *abbrev_table)
d85a05f0 6467{
fceca515 6468 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6469 reader->abfd = section->get_bfd_owner ();
d85a05f0 6470 reader->cu = cu;
3019eac3 6471 reader->dwo_file = dwo_file;
dee91e82
DE
6472 reader->die_section = section;
6473 reader->buffer = section->buffer;
f664829e 6474 reader->buffer_end = section->buffer + section->size;
685af9cd 6475 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6476}
6477
c0ab21c2 6478/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6479 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6480 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6481 already.
6482
6483 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6484 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6485 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6486 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6487 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6488 STUB_COMP_DIR may be non-NULL.
3e225074 6489 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6490 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6491 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6492 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6493 kept around for at least as long as *RESULT_READER.
6494
b0c7bfa9
DE
6495 The result is non-zero if a valid (non-dummy) DIE was found. */
6496
6497static int
6498read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6499 struct dwo_unit *dwo_unit,
b0c7bfa9 6500 struct die_info *stub_comp_unit_die,
a2ce51a0 6501 const char *stub_comp_dir,
b0c7bfa9 6502 struct die_reader_specs *result_reader,
d521ce57 6503 const gdb_byte **result_info_ptr,
b0c7bfa9 6504 struct die_info **result_comp_unit_die,
685af9cd 6505 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6506{
ed2dc618 6507 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6508 struct objfile *objfile = dwarf2_per_objfile->objfile;
6509 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6510 bfd *abfd;
d521ce57 6511 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6512 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6513 int i,num_extra_attrs;
6514 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6515 struct die_info *comp_unit_die;
6516
b0aeadb3
DE
6517 /* At most one of these may be provided. */
6518 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6519
b0c7bfa9
DE
6520 /* These attributes aren't processed until later:
6521 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6522 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6523 referenced later. However, these attributes are found in the stub
6524 which we won't have later. In order to not impose this complication
6525 on the rest of the code, we read them here and copy them to the
6526 DWO CU/TU die. */
b0c7bfa9
DE
6527
6528 stmt_list = NULL;
6529 low_pc = NULL;
6530 high_pc = NULL;
6531 ranges = NULL;
6532 comp_dir = NULL;
6533
6534 if (stub_comp_unit_die != NULL)
6535 {
6536 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6537 DWO file. */
6538 if (! this_cu->is_debug_types)
6539 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6540 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6541 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6542 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6543 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6544
18a8505e 6545 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6546
18a8505e
AT
6547 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6548 here (if needed). We need the value before we can process
6549 DW_AT_ranges. */
6550 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6551 }
a2ce51a0
DE
6552 else if (stub_comp_dir != NULL)
6553 {
6554 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6555 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6556 comp_dir->name = DW_AT_comp_dir;
6557 comp_dir->form = DW_FORM_string;
6558 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6559 DW_STRING (comp_dir) = stub_comp_dir;
6560 }
b0c7bfa9
DE
6561
6562 /* Set up for reading the DWO CU/TU. */
6563 cu->dwo_unit = dwo_unit;
685af9cd 6564 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6565 section->read (objfile);
6566 abfd = section->get_bfd_owner ();
9c541725
PA
6567 begin_info_ptr = info_ptr = (section->buffer
6568 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6569 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6570
6571 if (this_cu->is_debug_types)
6572 {
b0c7bfa9
DE
6573 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6574
ed2dc618
SM
6575 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6576 &cu->header, section,
b0c7bfa9 6577 dwo_abbrev_section,
43988095 6578 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6579 /* This is not an assert because it can be caused by bad debug info. */
43988095 6580 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6581 {
6582 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6583 " TU at offset %s [in module %s]"),
a2ce51a0 6584 hex_string (sig_type->signature),
43988095 6585 hex_string (cu->header.signature),
9d8780f0 6586 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6587 bfd_get_filename (abfd));
6588 }
9c541725 6589 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6590 /* For DWOs coming from DWP files, we don't know the CU length
6591 nor the type's offset in the TU until now. */
4057dfde 6592 dwo_unit->length = cu->header.get_length ();
9c541725 6593 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6594
6595 /* Establish the type offset that can be used to lookup the type.
6596 For DWO files, we don't know it until now. */
9c541725
PA
6597 sig_type->type_offset_in_section
6598 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6599 }
6600 else
6601 {
ed2dc618
SM
6602 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6603 &cu->header, section,
b0c7bfa9 6604 dwo_abbrev_section,
43988095 6605 info_ptr, rcuh_kind::COMPILE);
9c541725 6606 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6607 /* For DWOs coming from DWP files, we don't know the CU length
6608 until now. */
4057dfde 6609 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6610 }
6611
685af9cd 6612 *result_dwo_abbrev_table
86de1d91
TT
6613 = abbrev_table::read (objfile, dwo_abbrev_section,
6614 cu->header.abbrev_sect_off);
685af9cd
TT
6615 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6616 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6617
6618 /* Read in the die, but leave space to copy over the attributes
6619 from the stub. This has the benefit of simplifying the rest of
6620 the code - all the work to maintain the illusion of a single
6621 DW_TAG_{compile,type}_unit DIE is done here. */
6622 num_extra_attrs = ((stmt_list != NULL)
6623 + (low_pc != NULL)
6624 + (high_pc != NULL)
6625 + (ranges != NULL)
6626 + (comp_dir != NULL));
6627 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6628 num_extra_attrs);
b0c7bfa9
DE
6629
6630 /* Copy over the attributes from the stub to the DIE we just read in. */
6631 comp_unit_die = *result_comp_unit_die;
6632 i = comp_unit_die->num_attrs;
6633 if (stmt_list != NULL)
6634 comp_unit_die->attrs[i++] = *stmt_list;
6635 if (low_pc != NULL)
6636 comp_unit_die->attrs[i++] = *low_pc;
6637 if (high_pc != NULL)
6638 comp_unit_die->attrs[i++] = *high_pc;
6639 if (ranges != NULL)
6640 comp_unit_die->attrs[i++] = *ranges;
6641 if (comp_dir != NULL)
6642 comp_unit_die->attrs[i++] = *comp_dir;
6643 comp_unit_die->num_attrs += num_extra_attrs;
6644
b4f54984 6645 if (dwarf_die_debug)
bf6af496
DE
6646 {
6647 fprintf_unfiltered (gdb_stdlog,
6648 "Read die from %s@0x%x of %s:\n",
96b79293 6649 section->get_name (),
bf6af496
DE
6650 (unsigned) (begin_info_ptr - section->buffer),
6651 bfd_get_filename (abfd));
b4f54984 6652 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6653 }
6654
b0c7bfa9
DE
6655 /* Skip dummy compilation units. */
6656 if (info_ptr >= begin_info_ptr + dwo_unit->length
6657 || peek_abbrev_code (abfd, info_ptr) == 0)
6658 return 0;
6659
6660 *result_info_ptr = info_ptr;
6661 return 1;
6662}
6663
a084a2a6
AT
6664/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6665 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6666 signature is part of the header. */
6667static gdb::optional<ULONGEST>
6668lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6669{
6670 if (cu->header.version >= 5)
6671 return cu->header.signature;
6672 struct attribute *attr;
6673 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6674 if (attr == nullptr)
6675 return gdb::optional<ULONGEST> ();
6676 return DW_UNSND (attr);
6677}
6678
c0ab21c2 6679/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6680 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6681 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6682
6683static struct dwo_unit *
6684lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6685 struct die_info *comp_unit_die,
6686 const char *dwo_name)
b0c7bfa9
DE
6687{
6688 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6689 struct dwo_unit *dwo_unit;
c0ab21c2 6690 const char *comp_dir;
b0c7bfa9 6691
a2ce51a0
DE
6692 gdb_assert (cu != NULL);
6693
b0c7bfa9 6694 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6695 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6696 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6697
6698 if (this_cu->is_debug_types)
6699 {
6700 struct signatured_type *sig_type;
6701
6702 /* Since this_cu is the first member of struct signatured_type,
6703 we can go from a pointer to one to a pointer to the other. */
6704 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6705 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6706 }
6707 else
6708 {
a084a2a6
AT
6709 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6710 if (!signature.has_value ())
b0c7bfa9
DE
6711 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6712 " [in module %s]"),
e3b94546 6713 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6714 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6715 *signature);
b0c7bfa9
DE
6716 }
6717
b0c7bfa9
DE
6718 return dwo_unit;
6719}
6720
c0ab21c2 6721/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6722 See it for a description of the parameters.
fcd3b13d 6723 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6724
c0ab21c2
TT
6725void
6726cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6727 int use_existing_cu)
a2ce51a0 6728{
a2ce51a0 6729 struct signatured_type *sig_type;
a2ce51a0 6730 struct die_reader_specs reader;
a2ce51a0
DE
6731
6732 /* Verify we can do the following downcast, and that we have the
6733 data we need. */
6734 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6735 sig_type = (struct signatured_type *) this_cu;
6736 gdb_assert (sig_type->dwo_unit != NULL);
6737
6aa5f3a6
DE
6738 if (use_existing_cu && this_cu->cu != NULL)
6739 {
6740 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6741 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6742 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6743 }
6744 else
6745 {
6746 /* If !use_existing_cu, this_cu->cu must be NULL. */
6747 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6748 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6749 }
6750
6751 /* A future optimization, if needed, would be to use an existing
6752 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6753 could share abbrev tables. */
a2ce51a0
DE
6754
6755 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6756 NULL /* stub_comp_unit_die */,
6757 sig_type->dwo_unit->dwo_file->comp_dir,
6758 &reader, &info_ptr,
3e225074 6759 &comp_unit_die,
c0ab21c2 6760 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6761 {
6762 /* Dummy die. */
c0ab21c2 6763 dummy_p = true;
a2ce51a0 6764 }
a2ce51a0
DE
6765}
6766
fd820528 6767/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6768 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6769
f4dc4d17
DE
6770 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6771 Otherwise the table specified in the comp unit header is read in and used.
6772 This is an optimization for when we already have the abbrev table.
6773
dee91e82 6774 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6775 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6776
c0ab21c2
TT
6777cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6778 struct abbrev_table *abbrev_table,
6751ebae 6779 int use_existing_cu,
c0ab21c2
TT
6780 bool skip_partial)
6781 : die_reader_specs {},
6751ebae 6782 m_this_cu (this_cu)
c906108c 6783{
ed2dc618 6784 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6785 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6786 struct dwarf2_section_info *section = this_cu->section;
96b79293 6787 bfd *abfd = section->get_bfd_owner ();
dee91e82 6788 struct dwarf2_cu *cu;
c0ab21c2 6789 const gdb_byte *begin_info_ptr;
dee91e82 6790 struct signatured_type *sig_type = NULL;
4bdcc0c1 6791 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6792 /* Non-zero if CU currently points to a DWO file and we need to
6793 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6794 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6795 int rereading_dwo_cu = 0;
c906108c 6796
b4f54984 6797 if (dwarf_die_debug)
9d8780f0 6798 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6799 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6800 sect_offset_str (this_cu->sect_off));
09406207 6801
a2ce51a0
DE
6802 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6803 file (instead of going through the stub), short-circuit all of this. */
6804 if (this_cu->reading_dwo_directly)
6805 {
6806 /* Narrow down the scope of possibilities to have to understand. */
6807 gdb_assert (this_cu->is_debug_types);
6808 gdb_assert (abbrev_table == NULL);
6751ebae 6809 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6810 return;
6811 }
6812
dee91e82 6813 /* This is cheap if the section is already read in. */
96b79293 6814 section->read (objfile);
dee91e82 6815
9c541725 6816 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6817
6818 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6819
6820 if (use_existing_cu && this_cu->cu != NULL)
6821 {
6822 cu = this_cu->cu;
42e7ad6c
DE
6823 /* If this CU is from a DWO file we need to start over, we need to
6824 refetch the attributes from the skeleton CU.
6825 This could be optimized by retrieving those attributes from when we
6826 were here the first time: the previous comp_unit_die was stored in
6827 comp_unit_obstack. But there's no data yet that we need this
6828 optimization. */
6829 if (cu->dwo_unit != NULL)
6830 rereading_dwo_cu = 1;
dee91e82
DE
6831 }
6832 else
6833 {
6834 /* If !use_existing_cu, this_cu->cu must be NULL. */
6835 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6836 m_new_cu.reset (new dwarf2_cu (this_cu));
6837 cu = m_new_cu.get ();
42e7ad6c 6838 }
dee91e82 6839
b0c7bfa9 6840 /* Get the header. */
9c541725 6841 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6842 {
6843 /* We already have the header, there's no need to read it in again. */
9c541725 6844 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6845 }
6846 else
6847 {
3019eac3 6848 if (this_cu->is_debug_types)
dee91e82 6849 {
ed2dc618
SM
6850 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6851 &cu->header, section,
4bdcc0c1 6852 abbrev_section, info_ptr,
43988095 6853 rcuh_kind::TYPE);
dee91e82 6854
42e7ad6c
DE
6855 /* Since per_cu is the first member of struct signatured_type,
6856 we can go from a pointer to one to a pointer to the other. */
6857 sig_type = (struct signatured_type *) this_cu;
43988095 6858 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6859 gdb_assert (sig_type->type_offset_in_tu
6860 == cu->header.type_cu_offset_in_tu);
6861 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6862
42e7ad6c
DE
6863 /* LENGTH has not been set yet for type units if we're
6864 using .gdb_index. */
4057dfde 6865 this_cu->length = cu->header.get_length ();
3019eac3
DE
6866
6867 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6868 sig_type->type_offset_in_section =
6869 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6870
6871 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6872 }
6873 else
6874 {
ed2dc618
SM
6875 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6876 &cu->header, section,
4bdcc0c1 6877 abbrev_section,
43988095
JK
6878 info_ptr,
6879 rcuh_kind::COMPILE);
dee91e82 6880
9c541725 6881 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6882 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6883 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6884 }
6885 }
10b3939b 6886
6caca83c 6887 /* Skip dummy compilation units. */
dee91e82 6888 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6889 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6890 {
6891 dummy_p = true;
6892 return;
6893 }
6caca83c 6894
433df2d4
DE
6895 /* If we don't have them yet, read the abbrevs for this compilation unit.
6896 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6897 done. */
f4dc4d17 6898 if (abbrev_table != NULL)
685af9cd
TT
6899 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6900 else
f4dc4d17 6901 {
c0ab21c2 6902 m_abbrev_table_holder
86de1d91
TT
6903 = abbrev_table::read (objfile, abbrev_section,
6904 cu->header.abbrev_sect_off);
c0ab21c2 6905 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6906 }
af703f96 6907
dee91e82 6908 /* Read the top level CU/TU die. */
c0ab21c2 6909 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6910 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6911
58f0c718 6912 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6913 {
6914 dummy_p = true;
6915 return;
6916 }
58f0c718 6917
b0c7bfa9 6918 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6919 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6920 table from the DWO file and pass the ownership over to us. It will be
6921 referenced from READER, so we must make sure to free it after we're done
6922 with READER.
6923
b0c7bfa9
DE
6924 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6925 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6926 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6927 if (dwo_name != nullptr)
3019eac3 6928 {
3019eac3 6929 struct dwo_unit *dwo_unit;
b0c7bfa9 6930 struct die_info *dwo_comp_unit_die;
3019eac3 6931
3e225074 6932 if (comp_unit_die->has_children)
6a506a2d 6933 {
b98664d3 6934 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6935 " has children (offset %s) [in module %s]"),
6936 sect_offset_str (this_cu->sect_off),
6937 bfd_get_filename (abfd));
6a506a2d 6938 }
c0ab21c2 6939 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6940 if (dwo_unit != NULL)
3019eac3 6941 {
6a506a2d 6942 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6943 comp_unit_die, NULL,
c0ab21c2 6944 this, &info_ptr,
3e225074 6945 &dwo_comp_unit_die,
c0ab21c2 6946 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6947 {
6948 /* Dummy die. */
c0ab21c2 6949 dummy_p = true;
6a506a2d
DE
6950 return;
6951 }
6952 comp_unit_die = dwo_comp_unit_die;
6953 }
6954 else
6955 {
6956 /* Yikes, we couldn't find the rest of the DIE, we only have
6957 the stub. A complaint has already been logged. There's
6958 not much more we can do except pass on the stub DIE to
6959 die_reader_func. We don't want to throw an error on bad
6960 debug info. */
3019eac3
DE
6961 }
6962 }
c0ab21c2 6963}
3019eac3 6964
6751ebae
TT
6965void
6966cutu_reader::keep ()
c0ab21c2 6967{
b0c7bfa9 6968 /* Done, clean up. */
6751ebae
TT
6969 gdb_assert (!dummy_p);
6970 if (m_new_cu != NULL)
348e048f 6971 {
c0ab21c2
TT
6972 struct dwarf2_per_objfile *dwarf2_per_objfile
6973 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6974 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6975 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6976 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6977 /* The chain owns it now. */
c0ab21c2 6978 m_new_cu.release ();
348e048f 6979 }
dee91e82
DE
6980}
6981
18a8505e
AT
6982/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6983 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6984 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6985
6986 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6987 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6988
6989 We fill in THIS_CU->length.
6990
dee91e82 6991 THIS_CU->cu is always freed when done.
3019eac3 6992 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6993 to care whether it refers to the "main" CU or the DWO CU.
6994
6995 When parent_cu is passed, it is used to provide a default value for
6996 str_offsets_base and addr_base from the parent. */
dee91e82 6997
c0ab21c2
TT
6998cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6999 struct dwarf2_cu *parent_cu,
7000 struct dwo_file *dwo_file)
7001 : die_reader_specs {},
7002 m_this_cu (this_cu)
dee91e82 7003{
ed2dc618 7004 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7005 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7006 struct dwarf2_section_info *section = this_cu->section;
96b79293 7007 bfd *abfd = section->get_bfd_owner ();
33e80786 7008 struct dwarf2_section_info *abbrev_section;
d521ce57 7009 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7010
b4f54984 7011 if (dwarf_die_debug)
9d8780f0 7012 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7013 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7014 sect_offset_str (this_cu->sect_off));
09406207 7015
dee91e82
DE
7016 gdb_assert (this_cu->cu == NULL);
7017
33e80786
DE
7018 abbrev_section = (dwo_file != NULL
7019 ? &dwo_file->sections.abbrev
7020 : get_abbrev_section_for_cu (this_cu));
7021
dee91e82 7022 /* This is cheap if the section is already read in. */
96b79293 7023 section->read (objfile);
dee91e82 7024
c0ab21c2 7025 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7026
9c541725 7027 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7028 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7029 &m_new_cu->header, section,
4bdcc0c1 7030 abbrev_section, info_ptr,
43988095
JK
7031 (this_cu->is_debug_types
7032 ? rcuh_kind::TYPE
7033 : rcuh_kind::COMPILE));
dee91e82 7034
18a8505e
AT
7035 if (parent_cu != nullptr)
7036 {
c0ab21c2
TT
7037 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7038 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7039 }
4057dfde 7040 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7041
7042 /* Skip dummy compilation units. */
7043 if (info_ptr >= begin_info_ptr + this_cu->length
7044 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7045 {
7046 dummy_p = true;
7047 return;
7048 }
72bf9492 7049
c0ab21c2 7050 m_abbrev_table_holder
86de1d91
TT
7051 = abbrev_table::read (objfile, abbrev_section,
7052 m_new_cu->header.abbrev_sect_off);
dee91e82 7053
c0ab21c2
TT
7054 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7055 m_abbrev_table_holder.get ());
3e225074 7056 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7057}
7058
0018ea6f
DE
7059\f
7060/* Type Unit Groups.
dee91e82 7061
0018ea6f
DE
7062 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7063 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7064 so that all types coming from the same compilation (.o file) are grouped
7065 together. A future step could be to put the types in the same symtab as
7066 the CU the types ultimately came from. */
ff013f42 7067
f4dc4d17
DE
7068static hashval_t
7069hash_type_unit_group (const void *item)
7070{
9a3c8263
SM
7071 const struct type_unit_group *tu_group
7072 = (const struct type_unit_group *) item;
f4dc4d17 7073
094b34ac 7074 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7075}
348e048f
DE
7076
7077static int
f4dc4d17 7078eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7079{
9a3c8263
SM
7080 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7081 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7082
094b34ac 7083 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7084}
348e048f 7085
f4dc4d17
DE
7086/* Allocate a hash table for type unit groups. */
7087
eaa5fa8b 7088static htab_up
ed2dc618 7089allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17 7090{
eaa5fa8b
TT
7091 return htab_up (htab_create_alloc (3,
7092 hash_type_unit_group,
7093 eq_type_unit_group,
7094 NULL, xcalloc, xfree));
f4dc4d17 7095}
dee91e82 7096
f4dc4d17
DE
7097/* Type units that don't have DW_AT_stmt_list are grouped into their own
7098 partial symtabs. We combine several TUs per psymtab to not let the size
7099 of any one psymtab grow too big. */
7100#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7101#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7102
094b34ac 7103/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7104 Create the type_unit_group object used to hold one or more TUs. */
7105
7106static struct type_unit_group *
094b34ac 7107create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7108{
518817b3
SM
7109 struct dwarf2_per_objfile *dwarf2_per_objfile
7110 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7111 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7112 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7113 struct type_unit_group *tu_group;
f4dc4d17
DE
7114
7115 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7116 struct type_unit_group);
094b34ac 7117 per_cu = &tu_group->per_cu;
518817b3 7118 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7119
094b34ac
DE
7120 if (dwarf2_per_objfile->using_index)
7121 {
7122 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7123 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7124 }
7125 else
7126 {
9c541725 7127 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7128 dwarf2_psymtab *pst;
528e1572 7129 std::string name;
094b34ac
DE
7130
7131 /* Give the symtab a useful name for debug purposes. */
7132 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7133 name = string_printf ("<type_units_%d>",
7134 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7135 else
528e1572 7136 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7137
528e1572 7138 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7139 pst->anonymous = true;
094b34ac 7140 }
f4dc4d17 7141
094b34ac 7142 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7143 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7144
7145 return tu_group;
7146}
7147
094b34ac
DE
7148/* Look up the type_unit_group for type unit CU, and create it if necessary.
7149 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7150
7151static struct type_unit_group *
ff39bb5e 7152get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7153{
518817b3
SM
7154 struct dwarf2_per_objfile *dwarf2_per_objfile
7155 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7156 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7157 struct type_unit_group *tu_group;
7158 void **slot;
7159 unsigned int line_offset;
7160 struct type_unit_group type_unit_group_for_lookup;
7161
7162 if (dwarf2_per_objfile->type_unit_groups == NULL)
7163 {
7164 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7165 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7166 }
7167
7168 /* Do we need to create a new group, or can we use an existing one? */
7169
7170 if (stmt_list)
7171 {
7172 line_offset = DW_UNSND (stmt_list);
7173 ++tu_stats->nr_symtab_sharers;
7174 }
7175 else
7176 {
7177 /* Ugh, no stmt_list. Rare, but we have to handle it.
7178 We can do various things here like create one group per TU or
7179 spread them over multiple groups to split up the expansion work.
7180 To avoid worst case scenarios (too many groups or too large groups)
7181 we, umm, group them in bunches. */
7182 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7183 | (tu_stats->nr_stmt_less_type_units
7184 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7185 ++tu_stats->nr_stmt_less_type_units;
7186 }
7187
094b34ac 7188 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7189 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7190 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7191 &type_unit_group_for_lookup, INSERT);
7192 if (*slot != NULL)
7193 {
9a3c8263 7194 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7195 gdb_assert (tu_group != NULL);
7196 }
7197 else
7198 {
9c541725 7199 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7200 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7201 *slot = tu_group;
7202 ++tu_stats->nr_symtabs;
7203 }
7204
7205 return tu_group;
7206}
0018ea6f
DE
7207\f
7208/* Partial symbol tables. */
7209
7210/* Create a psymtab named NAME and assign it to PER_CU.
7211
7212 The caller must fill in the following details:
7213 dirname, textlow, texthigh. */
7214
891813be 7215static dwarf2_psymtab *
0018ea6f
DE
7216create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7217{
e3b94546 7218 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7219 dwarf2_psymtab *pst;
0018ea6f 7220
891813be 7221 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7222
6d94535f 7223 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7224
7225 /* This is the glue that links PST into GDB's symbol API. */
891813be 7226 pst->per_cu_data = per_cu;
0018ea6f
DE
7227 per_cu->v.psymtab = pst;
7228
7229 return pst;
7230}
7231
c0ab21c2 7232/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7233
7234static void
7235process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7236 const gdb_byte *info_ptr,
0018ea6f 7237 struct die_info *comp_unit_die,
c0ab21c2 7238 enum language pretend_language)
0018ea6f
DE
7239{
7240 struct dwarf2_cu *cu = reader->cu;
518817b3 7241 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7243 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7244 CORE_ADDR baseaddr;
7245 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7246 dwarf2_psymtab *pst;
3a2b436a 7247 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7248 const char *filename;
0018ea6f 7249
0018ea6f
DE
7250 gdb_assert (! per_cu->is_debug_types);
7251
c0ab21c2 7252 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7253
0018ea6f 7254 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7255 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7256 if (filename == NULL)
0018ea6f 7257 filename = "";
0018ea6f
DE
7258
7259 pst = create_partial_symtab (per_cu, filename);
7260
7261 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7262 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7263
b3b3bada 7264 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7265
7266 dwarf2_find_base_address (comp_unit_die, cu);
7267
7268 /* Possibly set the default values of LOWPC and HIGHPC from
7269 `DW_AT_ranges'. */
3a2b436a
JK
7270 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7271 &best_highpc, cu, pst);
7272 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7273 {
7274 CORE_ADDR low
7275 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7276 - baseaddr);
7277 CORE_ADDR high
7278 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7279 - baseaddr - 1);
7280 /* Store the contiguous range if it is not empty; it can be
7281 empty for CUs with no code. */
d320c2b5
TT
7282 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7283 low, high, pst);
79748972 7284 }
0018ea6f
DE
7285
7286 /* Check if comp unit has_children.
7287 If so, read the rest of the partial symbols from this comp unit.
7288 If not, there's no more debug_info for this comp unit. */
3e225074 7289 if (comp_unit_die->has_children)
0018ea6f
DE
7290 {
7291 struct partial_die_info *first_die;
7292 CORE_ADDR lowpc, highpc;
7293
7294 lowpc = ((CORE_ADDR) -1);
7295 highpc = ((CORE_ADDR) 0);
7296
7297 first_die = load_partial_dies (reader, info_ptr, 1);
7298
7299 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7300 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7301
7302 /* If we didn't find a lowpc, set it to highpc to avoid
7303 complaints from `maint check'. */
7304 if (lowpc == ((CORE_ADDR) -1))
7305 lowpc = highpc;
7306
7307 /* If the compilation unit didn't have an explicit address range,
7308 then use the information extracted from its child dies. */
e385593e 7309 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7310 {
7311 best_lowpc = lowpc;
7312 best_highpc = highpc;
7313 }
7314 }
4ae976d1 7315 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7316 best_lowpc + baseaddr)
7317 - baseaddr);
4ae976d1 7318 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7319 best_highpc + baseaddr)
7320 - baseaddr);
0018ea6f 7321
8763cede 7322 end_psymtab_common (objfile, pst);
0018ea6f 7323
ae640021 7324 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7325 {
7326 int i;
ae640021 7327 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7328
7329 /* Fill in 'dependencies' here; we fill in 'users' in a
7330 post-pass. */
7331 pst->number_of_dependencies = len;
a9342b62
TT
7332 pst->dependencies
7333 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7334 for (i = 0; i < len; ++i)
7335 {
7336 pst->dependencies[i]
7337 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7338 }
0018ea6f 7339
ae640021 7340 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7341 }
7342
7343 /* Get the list of files included in the current compilation unit,
7344 and build a psymtab for each of them. */
7345 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7346
b4f54984 7347 if (dwarf_read_debug)
b926417a
TT
7348 fprintf_unfiltered (gdb_stdlog,
7349 "Psymtab for %s unit @%s: %s - %s"
7350 ", %d global, %d static syms\n",
7351 per_cu->is_debug_types ? "type" : "comp",
7352 sect_offset_str (per_cu->sect_off),
7353 paddress (gdbarch, pst->text_low (objfile)),
7354 paddress (gdbarch, pst->text_high (objfile)),
7355 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7356}
7357
7358/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7359 Process compilation unit THIS_CU for a psymtab. */
7360
7361static void
7362process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7363 bool want_partial_unit,
b93601f3 7364 enum language pretend_language)
0018ea6f
DE
7365{
7366 /* If this compilation unit was already read in, free the
7367 cached copy in order to read it in again. This is
7368 necessary because we skipped some symbols when we first
7369 read in the compilation unit (see load_partial_dies).
7370 This problem could be avoided, but the benefit is unclear. */
7371 if (this_cu->cu != NULL)
7372 free_one_cached_comp_unit (this_cu);
7373
6751ebae 7374 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2
TT
7375
7376 if (reader.dummy_p)
f1902523 7377 {
c0ab21c2 7378 /* Nothing. */
f1902523 7379 }
c0ab21c2 7380 else if (this_cu->is_debug_types)
3e225074
TT
7381 build_type_psymtabs_reader (&reader, reader.info_ptr,
7382 reader.comp_unit_die);
135f5437
TT
7383 else if (want_partial_unit
7384 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7385 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7386 reader.comp_unit_die,
c0ab21c2 7387 pretend_language);
0018ea6f
DE
7388
7389 /* Age out any secondary CUs. */
ed2dc618 7390 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7391}
f4dc4d17
DE
7392
7393/* Reader function for build_type_psymtabs. */
7394
7395static void
7396build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7397 const gdb_byte *info_ptr,
3e225074 7398 struct die_info *type_unit_die)
f4dc4d17 7399{
ed2dc618 7400 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7401 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7402 struct objfile *objfile = dwarf2_per_objfile->objfile;
7403 struct dwarf2_cu *cu = reader->cu;
7404 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7405 struct signatured_type *sig_type;
f4dc4d17
DE
7406 struct type_unit_group *tu_group;
7407 struct attribute *attr;
7408 struct partial_die_info *first_die;
7409 CORE_ADDR lowpc, highpc;
891813be 7410 dwarf2_psymtab *pst;
f4dc4d17 7411
0186c6a7
DE
7412 gdb_assert (per_cu->is_debug_types);
7413 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7414
3e225074 7415 if (! type_unit_die->has_children)
f4dc4d17
DE
7416 return;
7417
7418 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7419 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7420
df07e2c7 7421 if (tu_group->tus == nullptr)
a8b3b8e9 7422 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7423 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7424
7425 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7426 pst = create_partial_symtab (per_cu, "");
6d94535f 7427 pst->anonymous = true;
f4dc4d17
DE
7428
7429 first_die = load_partial_dies (reader, info_ptr, 1);
7430
7431 lowpc = (CORE_ADDR) -1;
7432 highpc = (CORE_ADDR) 0;
7433 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7434
8763cede 7435 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7436}
7437
73051182
DE
7438/* Struct used to sort TUs by their abbreviation table offset. */
7439
7440struct tu_abbrev_offset
7441{
b2bdb8cf
SM
7442 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7443 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7444 {}
7445
7446 signatured_type *sig_type;
73051182
DE
7447 sect_offset abbrev_offset;
7448};
7449
484cf504 7450/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7451
484cf504
TT
7452static bool
7453sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7454 const struct tu_abbrev_offset &b)
73051182 7455{
484cf504 7456 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7457}
7458
7459/* Efficiently read all the type units.
7460 This does the bulk of the work for build_type_psymtabs.
7461
7462 The efficiency is because we sort TUs by the abbrev table they use and
7463 only read each abbrev table once. In one program there are 200K TUs
7464 sharing 8K abbrev tables.
7465
7466 The main purpose of this function is to support building the
7467 dwarf2_per_objfile->type_unit_groups table.
7468 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7469 can collapse the search space by grouping them by stmt_list.
7470 The savings can be significant, in the same program from above the 200K TUs
7471 share 8K stmt_list tables.
7472
7473 FUNC is expected to call get_type_unit_group, which will create the
7474 struct type_unit_group if necessary and add it to
7475 dwarf2_per_objfile->type_unit_groups. */
7476
7477static void
ed2dc618 7478build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7479{
73051182 7480 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7481 abbrev_table_up abbrev_table;
73051182 7482 sect_offset abbrev_offset;
73051182
DE
7483
7484 /* It's up to the caller to not call us multiple times. */
7485 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7486
b2bdb8cf 7487 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7488 return;
7489
7490 /* TUs typically share abbrev tables, and there can be way more TUs than
7491 abbrev tables. Sort by abbrev table to reduce the number of times we
7492 read each abbrev table in.
7493 Alternatives are to punt or to maintain a cache of abbrev tables.
7494 This is simpler and efficient enough for now.
7495
7496 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7497 symtab to use). Typically TUs with the same abbrev offset have the same
7498 stmt_list value too so in practice this should work well.
7499
7500 The basic algorithm here is:
7501
7502 sort TUs by abbrev table
7503 for each TU with same abbrev table:
7504 read abbrev table if first user
7505 read TU top level DIE
7506 [IWBN if DWO skeletons had DW_AT_stmt_list]
7507 call FUNC */
7508
b4f54984 7509 if (dwarf_read_debug)
73051182
DE
7510 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7511
7512 /* Sort in a separate table to maintain the order of all_type_units
7513 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7514 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7515 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7516
7517 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7518 sorted_by_abbrev.emplace_back
7519 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7520 sig_type->per_cu.section,
7521 sig_type->per_cu.sect_off));
73051182 7522
484cf504
TT
7523 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7524 sort_tu_by_abbrev_offset);
73051182 7525
9c541725 7526 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7527
b2bdb8cf 7528 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7529 {
73051182
DE
7530 /* Switch to the next abbrev table if necessary. */
7531 if (abbrev_table == NULL
b2bdb8cf 7532 || tu.abbrev_offset != abbrev_offset)
73051182 7533 {
b2bdb8cf 7534 abbrev_offset = tu.abbrev_offset;
73051182 7535 abbrev_table =
86de1d91
TT
7536 abbrev_table::read (dwarf2_per_objfile->objfile,
7537 &dwarf2_per_objfile->abbrev,
7538 abbrev_offset);
73051182
DE
7539 ++tu_stats->nr_uniq_abbrev_tables;
7540 }
7541
c0ab21c2 7542 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7543 0, false);
c0ab21c2
TT
7544 if (!reader.dummy_p)
7545 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7546 reader.comp_unit_die);
73051182 7547 }
6aa5f3a6 7548}
73051182 7549
6aa5f3a6
DE
7550/* Print collected type unit statistics. */
7551
7552static void
ed2dc618 7553print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7554{
7555 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7556
7557 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7558 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7559 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7560 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7561 tu_stats->nr_uniq_abbrev_tables);
7562 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7563 tu_stats->nr_symtabs);
7564 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7565 tu_stats->nr_symtab_sharers);
7566 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7567 tu_stats->nr_stmt_less_type_units);
7568 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7569 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7570}
7571
f4dc4d17
DE
7572/* Traversal function for build_type_psymtabs. */
7573
7574static int
7575build_type_psymtab_dependencies (void **slot, void *info)
7576{
ed2dc618
SM
7577 struct dwarf2_per_objfile *dwarf2_per_objfile
7578 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7579 struct objfile *objfile = dwarf2_per_objfile->objfile;
7580 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7581 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7582 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7583 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7584 int i;
7585
7586 gdb_assert (len > 0);
0186c6a7 7587 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
7588
7589 pst->number_of_dependencies = len;
a9342b62 7590 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7591 for (i = 0; i < len; ++i)
f4dc4d17 7592 {
df07e2c7 7593 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7594 gdb_assert (iter->per_cu.is_debug_types);
7595 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7596 iter->type_unit_group = tu_group;
f4dc4d17
DE
7597 }
7598
df07e2c7
AB
7599 delete tu_group->tus;
7600 tu_group->tus = nullptr;
348e048f
DE
7601
7602 return 1;
7603}
7604
7605/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7606 Build partial symbol tables for the .debug_types comp-units. */
7607
7608static void
ed2dc618 7609build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7610{
ed2dc618 7611 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7612 return;
7613
ed2dc618 7614 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7615}
f4dc4d17 7616
6aa5f3a6
DE
7617/* Traversal function for process_skeletonless_type_unit.
7618 Read a TU in a DWO file and build partial symbols for it. */
7619
7620static int
7621process_skeletonless_type_unit (void **slot, void *info)
7622{
7623 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7624 struct dwarf2_per_objfile *dwarf2_per_objfile
7625 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7626 struct signatured_type find_entry, *entry;
7627
7628 /* If this TU doesn't exist in the global table, add it and read it in. */
7629
7630 if (dwarf2_per_objfile->signatured_types == NULL)
7631 {
7632 dwarf2_per_objfile->signatured_types
ed2dc618 7633 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
7634 }
7635
7636 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7637 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7638 &find_entry, INSERT);
6aa5f3a6
DE
7639 /* If we've already seen this type there's nothing to do. What's happening
7640 is we're doing our own version of comdat-folding here. */
7641 if (*slot != NULL)
7642 return 1;
7643
7644 /* This does the job that create_all_type_units would have done for
7645 this TU. */
ed2dc618
SM
7646 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7647 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7648 *slot = entry;
7649
7650 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7651 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7652 if (!reader.dummy_p)
7653 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7654 reader.comp_unit_die);
6aa5f3a6
DE
7655
7656 return 1;
7657}
7658
7659/* Traversal function for process_skeletonless_type_units. */
7660
7661static int
7662process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7663{
7664 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7665
7666 if (dwo_file->tus != NULL)
b0b6a987
TT
7667 htab_traverse_noresize (dwo_file->tus.get (),
7668 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7669
7670 return 1;
7671}
7672
7673/* Scan all TUs of DWO files, verifying we've processed them.
7674 This is needed in case a TU was emitted without its skeleton.
7675 Note: This can't be done until we know what all the DWO files are. */
7676
7677static void
ed2dc618 7678process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7679{
7680 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7681 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7682 && dwarf2_per_objfile->dwo_files != NULL)
7683 {
51ac9db5 7684 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7685 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7686 dwarf2_per_objfile);
6aa5f3a6 7687 }
348e048f
DE
7688}
7689
ed2dc618 7690/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7691
7692static void
ed2dc618 7693set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7694{
b76e467d 7695 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7696 {
891813be 7697 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7698
36586728
TT
7699 if (pst == NULL)
7700 continue;
7701
b76e467d 7702 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7703 {
7704 /* Set the 'user' field only if it is not already set. */
7705 if (pst->dependencies[j]->user == NULL)
7706 pst->dependencies[j]->user = pst;
7707 }
7708 }
7709}
7710
93311388
DE
7711/* Build the partial symbol table by doing a quick pass through the
7712 .debug_info and .debug_abbrev sections. */
72bf9492 7713
93311388 7714static void
ed2dc618 7715dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7716{
ed2dc618 7717 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7718
b4f54984 7719 if (dwarf_read_debug)
45cfd468
DE
7720 {
7721 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7722 objfile_name (objfile));
45cfd468
DE
7723 }
7724
98bfdba5
PA
7725 dwarf2_per_objfile->reading_partial_symbols = 1;
7726
96b79293 7727 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7728
93311388
DE
7729 /* Any cached compilation units will be linked by the per-objfile
7730 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7731 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7732
ed2dc618 7733 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7734
ed2dc618 7735 create_all_comp_units (dwarf2_per_objfile);
c906108c 7736
60606b2c
TT
7737 /* Create a temporary address map on a temporary obstack. We later
7738 copy this to the final obstack. */
8268c778 7739 auto_obstack temp_obstack;
791afaa2
TT
7740
7741 scoped_restore save_psymtabs_addrmap
d320c2b5 7742 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7743 addrmap_create_mutable (&temp_obstack));
72bf9492 7744
b76e467d 7745 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7746 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7747
6aa5f3a6 7748 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7749 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7750
7751 /* Now that all TUs have been processed we can fill in the dependencies. */
7752 if (dwarf2_per_objfile->type_unit_groups != NULL)
7753 {
eaa5fa8b 7754 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7755 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7756 }
7757
b4f54984 7758 if (dwarf_read_debug)
ed2dc618 7759 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7760
ed2dc618 7761 set_partial_user (dwarf2_per_objfile);
95554aad 7762
d320c2b5
TT
7763 objfile->partial_symtabs->psymtabs_addrmap
7764 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7765 objfile->partial_symtabs->obstack ());
791afaa2
TT
7766 /* At this point we want to keep the address map. */
7767 save_psymtabs_addrmap.release ();
ff013f42 7768
b4f54984 7769 if (dwarf_read_debug)
45cfd468 7770 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7771 objfile_name (objfile));
ae038cb0
DJ
7772}
7773
dee91e82
DE
7774/* Load the partial DIEs for a secondary CU into memory.
7775 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7776
dee91e82
DE
7777static void
7778load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7779{
6751ebae 7780 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7781
7782 if (!reader.dummy_p)
7783 {
7784 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7785 language_minimal);
7786
7787 /* Check if comp unit has_children.
7788 If so, read the rest of the partial symbols from this comp unit.
7789 If not, there's no more debug_info for this comp unit. */
3e225074 7790 if (reader.comp_unit_die->has_children)
c0ab21c2 7791 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7792
7793 reader.keep ();
c0ab21c2 7794 }
ae038cb0
DJ
7795}
7796
ae038cb0 7797static void
ed2dc618 7798read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7799 struct dwarf2_section_info *section,
f1902523 7800 struct dwarf2_section_info *abbrev_section,
b76e467d 7801 unsigned int is_dwz)
ae038cb0 7802{
d521ce57 7803 const gdb_byte *info_ptr;
ed2dc618 7804 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7805
b4f54984 7806 if (dwarf_read_debug)
bf6af496 7807 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7808 section->get_name (),
7809 section->get_file_name ());
bf6af496 7810
96b79293 7811 section->read (objfile);
ae038cb0 7812
36586728 7813 info_ptr = section->buffer;
6e70227d 7814
36586728 7815 while (info_ptr < section->buffer + section->size)
ae038cb0 7816 {
ae038cb0 7817 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7818
9c541725 7819 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7820
f1902523 7821 comp_unit_head cu_header;
ed2dc618
SM
7822 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7823 abbrev_section, info_ptr,
7824 rcuh_kind::COMPILE);
ae038cb0
DJ
7825
7826 /* Save the compilation unit for later lookup. */
f1902523
JK
7827 if (cu_header.unit_type != DW_UT_type)
7828 {
7829 this_cu = XOBNEW (&objfile->objfile_obstack,
7830 struct dwarf2_per_cu_data);
7831 memset (this_cu, 0, sizeof (*this_cu));
7832 }
7833 else
7834 {
7835 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7836 struct signatured_type);
7837 memset (sig_type, 0, sizeof (*sig_type));
7838 sig_type->signature = cu_header.signature;
7839 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7840 this_cu = &sig_type->per_cu;
7841 }
7842 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7843 this_cu->sect_off = sect_off;
f1902523 7844 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7845 this_cu->is_dwz = is_dwz;
e3b94546 7846 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7847 this_cu->section = section;
ae038cb0 7848
b76e467d 7849 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7850
7851 info_ptr = info_ptr + this_cu->length;
7852 }
36586728
TT
7853}
7854
7855/* Create a list of all compilation units in OBJFILE.
7856 This is only done for -readnow and building partial symtabs. */
7857
7858static void
ed2dc618 7859create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7860{
b76e467d 7861 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7862 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7863 &dwarf2_per_objfile->abbrev, 0);
36586728 7864
b76e467d 7865 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7866 if (dwz != NULL)
ed2dc618 7867 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7868 1);
c906108c
SS
7869}
7870
5734ee8b 7871/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7872 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7873 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7874 DW_AT_ranges). See the comments of add_partial_subprogram on how
7875 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7876
72bf9492
DJ
7877static void
7878scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7879 CORE_ADDR *highpc, int set_addrmap,
7880 struct dwarf2_cu *cu)
c906108c 7881{
72bf9492 7882 struct partial_die_info *pdi;
c906108c 7883
91c24f0a
DC
7884 /* Now, march along the PDI's, descending into ones which have
7885 interesting children but skipping the children of the other ones,
7886 until we reach the end of the compilation unit. */
c906108c 7887
72bf9492 7888 pdi = first_die;
91c24f0a 7889
72bf9492
DJ
7890 while (pdi != NULL)
7891 {
52356b79 7892 pdi->fixup (cu);
c906108c 7893
f55ee35c 7894 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7895 children, so we need to look at them. Ditto for anonymous
7896 enums. */
933c6fe4 7897
72bf9492 7898 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7899 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7900 || pdi->tag == DW_TAG_imported_unit
7901 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7902 {
72bf9492 7903 switch (pdi->tag)
c906108c
SS
7904 {
7905 case DW_TAG_subprogram:
b1dc1806 7906 case DW_TAG_inlined_subroutine:
cdc07690 7907 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7908 break;
72929c62 7909 case DW_TAG_constant:
c906108c
SS
7910 case DW_TAG_variable:
7911 case DW_TAG_typedef:
91c24f0a 7912 case DW_TAG_union_type:
72bf9492 7913 if (!pdi->is_declaration)
63d06c5c 7914 {
72bf9492 7915 add_partial_symbol (pdi, cu);
63d06c5c
DC
7916 }
7917 break;
c906108c 7918 case DW_TAG_class_type:
680b30c7 7919 case DW_TAG_interface_type:
c906108c 7920 case DW_TAG_structure_type:
72bf9492 7921 if (!pdi->is_declaration)
c906108c 7922 {
72bf9492 7923 add_partial_symbol (pdi, cu);
c906108c 7924 }
b7fee5a3
KS
7925 if ((cu->language == language_rust
7926 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7927 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7928 set_addrmap, cu);
c906108c 7929 break;
91c24f0a 7930 case DW_TAG_enumeration_type:
72bf9492
DJ
7931 if (!pdi->is_declaration)
7932 add_partial_enumeration (pdi, cu);
c906108c
SS
7933 break;
7934 case DW_TAG_base_type:
a02abb62 7935 case DW_TAG_subrange_type:
c906108c 7936 /* File scope base type definitions are added to the partial
c5aa993b 7937 symbol table. */
72bf9492 7938 add_partial_symbol (pdi, cu);
c906108c 7939 break;
d9fa45fe 7940 case DW_TAG_namespace:
cdc07690 7941 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7942 break;
5d7cb8df 7943 case DW_TAG_module:
59c35742
AB
7944 if (!pdi->is_declaration)
7945 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7946 break;
95554aad
TT
7947 case DW_TAG_imported_unit:
7948 {
7949 struct dwarf2_per_cu_data *per_cu;
7950
f4dc4d17
DE
7951 /* For now we don't handle imported units in type units. */
7952 if (cu->per_cu->is_debug_types)
7953 {
7954 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7955 " supported in type units [in module %s]"),
518817b3 7956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7957 }
7958
e3b94546
SM
7959 per_cu = dwarf2_find_containing_comp_unit
7960 (pdi->d.sect_off, pdi->is_dwz,
518817b3 7961 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
7962
7963 /* Go read the partial unit, if needed. */
7964 if (per_cu->v.psymtab == NULL)
135f5437 7965 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 7966
ae640021 7967 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7968 }
7969 break;
74921315
KS
7970 case DW_TAG_imported_declaration:
7971 add_partial_symbol (pdi, cu);
7972 break;
c906108c
SS
7973 default:
7974 break;
7975 }
7976 }
7977
72bf9492
DJ
7978 /* If the die has a sibling, skip to the sibling. */
7979
7980 pdi = pdi->die_sibling;
7981 }
7982}
7983
7984/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7985
72bf9492 7986 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7987 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7988 Enumerators are an exception; they use the scope of their parent
7989 enumeration type, i.e. the name of the enumeration type is not
7990 prepended to the enumerator.
91c24f0a 7991
72bf9492
DJ
7992 There are two complexities. One is DW_AT_specification; in this
7993 case "parent" means the parent of the target of the specification,
7994 instead of the direct parent of the DIE. The other is compilers
7995 which do not emit DW_TAG_namespace; in this case we try to guess
7996 the fully qualified name of structure types from their members'
7997 linkage names. This must be done using the DIE's children rather
7998 than the children of any DW_AT_specification target. We only need
7999 to do this for structures at the top level, i.e. if the target of
8000 any DW_AT_specification (if any; otherwise the DIE itself) does not
8001 have a parent. */
8002
8003/* Compute the scope prefix associated with PDI's parent, in
8004 compilation unit CU. The result will be allocated on CU's
8005 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8006 field. NULL is returned if no prefix is necessary. */
15d034d0 8007static const char *
72bf9492
DJ
8008partial_die_parent_scope (struct partial_die_info *pdi,
8009 struct dwarf2_cu *cu)
8010{
15d034d0 8011 const char *grandparent_scope;
72bf9492 8012 struct partial_die_info *parent, *real_pdi;
91c24f0a 8013
72bf9492
DJ
8014 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8015 then this means the parent of the specification DIE. */
8016
8017 real_pdi = pdi;
72bf9492 8018 while (real_pdi->has_specification)
fb816e8b 8019 {
122cf0f2
AB
8020 auto res = find_partial_die (real_pdi->spec_offset,
8021 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8022 real_pdi = res.pdi;
8023 cu = res.cu;
8024 }
72bf9492
DJ
8025
8026 parent = real_pdi->die_parent;
8027 if (parent == NULL)
8028 return NULL;
8029
8030 if (parent->scope_set)
8031 return parent->scope;
8032
52356b79 8033 parent->fixup (cu);
72bf9492 8034
10b3939b 8035 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8036
acebe513
UW
8037 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8038 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8039 Work around this problem here. */
8040 if (cu->language == language_cplus
6e70227d 8041 && parent->tag == DW_TAG_namespace
acebe513
UW
8042 && strcmp (parent->name, "::") == 0
8043 && grandparent_scope == NULL)
8044 {
8045 parent->scope = NULL;
8046 parent->scope_set = 1;
8047 return NULL;
8048 }
8049
0a4b0913 8050 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8051 if (pdi->tag == DW_TAG_enumerator)
8052 /* Enumerators should not get the name of the enumeration as a prefix. */
8053 parent->scope = grandparent_scope;
8054 else if (parent->tag == DW_TAG_namespace
f55ee35c 8055 || parent->tag == DW_TAG_module
72bf9492
DJ
8056 || parent->tag == DW_TAG_structure_type
8057 || parent->tag == DW_TAG_class_type
680b30c7 8058 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8059 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8060 || parent->tag == DW_TAG_enumeration_type
8061 || (cu->language == language_fortran
8062 && parent->tag == DW_TAG_subprogram
8063 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8064 {
8065 if (grandparent_scope == NULL)
8066 parent->scope = parent->name;
8067 else
3e43a32a
MS
8068 parent->scope = typename_concat (&cu->comp_unit_obstack,
8069 grandparent_scope,
f55ee35c 8070 parent->name, 0, cu);
72bf9492 8071 }
72bf9492
DJ
8072 else
8073 {
8074 /* FIXME drow/2004-04-01: What should we be doing with
8075 function-local names? For partial symbols, we should probably be
8076 ignoring them. */
fa9c3fa0
TT
8077 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8078 dwarf_tag_name (parent->tag),
8079 sect_offset_str (pdi->sect_off));
72bf9492 8080 parent->scope = grandparent_scope;
c906108c
SS
8081 }
8082
72bf9492
DJ
8083 parent->scope_set = 1;
8084 return parent->scope;
8085}
8086
8087/* Return the fully scoped name associated with PDI, from compilation unit
8088 CU. The result will be allocated with malloc. */
4568ecf9 8089
43816ebc 8090static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8091partial_die_full_name (struct partial_die_info *pdi,
8092 struct dwarf2_cu *cu)
8093{
15d034d0 8094 const char *parent_scope;
72bf9492 8095
98bfdba5
PA
8096 /* If this is a template instantiation, we can not work out the
8097 template arguments from partial DIEs. So, unfortunately, we have
8098 to go through the full DIEs. At least any work we do building
8099 types here will be reused if full symbols are loaded later. */
8100 if (pdi->has_template_arguments)
8101 {
52356b79 8102 pdi->fixup (cu);
98bfdba5
PA
8103
8104 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8105 {
8106 struct die_info *die;
8107 struct attribute attr;
8108 struct dwarf2_cu *ref_cu = cu;
8109
b64f50a1 8110 /* DW_FORM_ref_addr is using section offset. */
b4069958 8111 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8112 attr.form = DW_FORM_ref_addr;
9c541725 8113 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8114 die = follow_die_ref (NULL, &attr, &ref_cu);
8115
43816ebc 8116 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8117 }
8118 }
8119
72bf9492
DJ
8120 parent_scope = partial_die_parent_scope (pdi, cu);
8121 if (parent_scope == NULL)
8122 return NULL;
8123 else
43816ebc
TT
8124 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8125 pdi->name, 0, cu));
c906108c
SS
8126}
8127
8128static void
72bf9492 8129add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8130{
518817b3
SM
8131 struct dwarf2_per_objfile *dwarf2_per_objfile
8132 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8133 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8134 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8135 CORE_ADDR addr = 0;
15d034d0 8136 const char *actual_name = NULL;
e142c38c
DJ
8137 CORE_ADDR baseaddr;
8138
b3b3bada 8139 baseaddr = objfile->text_section_offset ();
c906108c 8140
43816ebc
TT
8141 gdb::unique_xmalloc_ptr<char> built_actual_name
8142 = partial_die_full_name (pdi, cu);
15d034d0 8143 if (built_actual_name != NULL)
43816ebc 8144 actual_name = built_actual_name.get ();
63d06c5c 8145
72bf9492
DJ
8146 if (actual_name == NULL)
8147 actual_name = pdi->name;
8148
c906108c
SS
8149 switch (pdi->tag)
8150 {
b1dc1806 8151 case DW_TAG_inlined_subroutine:
c906108c 8152 case DW_TAG_subprogram:
79748972
TT
8153 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8154 - baseaddr);
0a4b0913
AB
8155 if (pdi->is_external
8156 || cu->language == language_ada
8157 || (cu->language == language_fortran
8158 && pdi->die_parent != NULL
8159 && pdi->die_parent->tag == DW_TAG_subprogram))
8160 {
8161 /* Normally, only "external" DIEs are part of the global scope.
8162 But in Ada and Fortran, we want to be able to access nested
8163 procedures globally. So all Ada and Fortran subprograms are
8164 stored in the global scope. */
31edb802 8165 add_psymbol_to_list (actual_name,
15d034d0 8166 built_actual_name != NULL,
f47fb265 8167 VAR_DOMAIN, LOC_BLOCK,
79748972 8168 SECT_OFF_TEXT (objfile),
75aedd27 8169 psymbol_placement::GLOBAL,
79748972
TT
8170 addr,
8171 cu->language, objfile);
c906108c
SS
8172 }
8173 else
8174 {
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::STATIC,
1762568f 8180 addr, cu->language, objfile);
c906108c 8181 }
0c1b455e
TT
8182
8183 if (pdi->main_subprogram && actual_name != NULL)
8184 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8185 break;
72929c62 8186 case DW_TAG_constant:
31edb802 8187 add_psymbol_to_list (actual_name,
75aedd27
TT
8188 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8189 -1, (pdi->is_external
8190 ? psymbol_placement::GLOBAL
8191 : psymbol_placement::STATIC),
8192 0, cu->language, objfile);
72929c62 8193 break;
c906108c 8194 case DW_TAG_variable:
95554aad
TT
8195 if (pdi->d.locdesc)
8196 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8197
95554aad 8198 if (pdi->d.locdesc
caac4577
JG
8199 && addr == 0
8200 && !dwarf2_per_objfile->has_section_at_zero)
8201 {
8202 /* A global or static variable may also have been stripped
8203 out by the linker if unused, in which case its address
8204 will be nullified; do not add such variables into partial
8205 symbol table then. */
8206 }
8207 else if (pdi->is_external)
c906108c
SS
8208 {
8209 /* Global Variable.
8210 Don't enter into the minimal symbol tables as there is
8211 a minimal symbol table entry from the ELF symbols already.
8212 Enter into partial symbol table if it has a location
8213 descriptor or a type.
8214 If the location descriptor is missing, new_symbol will create
8215 a LOC_UNRESOLVED symbol, the address of the variable will then
8216 be determined from the minimal symbol table whenever the variable
8217 is referenced.
8218 The address for the partial symbol table entry is not
8219 used by GDB, but it comes in handy for debugging partial symbol
8220 table building. */
8221
95554aad 8222 if (pdi->d.locdesc || pdi->has_type)
31edb802 8223 add_psymbol_to_list (actual_name,
15d034d0 8224 built_actual_name != NULL,
f47fb265 8225 VAR_DOMAIN, LOC_STATIC,
79748972 8226 SECT_OFF_TEXT (objfile),
75aedd27 8227 psymbol_placement::GLOBAL,
79748972 8228 addr, cu->language, objfile);
c906108c
SS
8229 }
8230 else
8231 {
ff908ebf
AW
8232 int has_loc = pdi->d.locdesc != NULL;
8233
8234 /* Static Variable. Skip symbols whose value we cannot know (those
8235 without location descriptors or constant values). */
8236 if (!has_loc && !pdi->has_const_value)
43816ebc 8237 return;
ff908ebf 8238
31edb802 8239 add_psymbol_to_list (actual_name,
15d034d0 8240 built_actual_name != NULL,
f47fb265 8241 VAR_DOMAIN, LOC_STATIC,
79748972 8242 SECT_OFF_TEXT (objfile),
75aedd27 8243 psymbol_placement::STATIC,
79748972 8244 has_loc ? addr : 0,
f47fb265 8245 cu->language, objfile);
c906108c
SS
8246 }
8247 break;
8248 case DW_TAG_typedef:
8249 case DW_TAG_base_type:
a02abb62 8250 case DW_TAG_subrange_type:
31edb802 8251 add_psymbol_to_list (actual_name,
15d034d0 8252 built_actual_name != NULL,
79748972 8253 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8254 psymbol_placement::STATIC,
1762568f 8255 0, cu->language, objfile);
c906108c 8256 break;
74921315 8257 case DW_TAG_imported_declaration:
72bf9492 8258 case DW_TAG_namespace:
31edb802 8259 add_psymbol_to_list (actual_name,
15d034d0 8260 built_actual_name != NULL,
79748972 8261 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8262 psymbol_placement::GLOBAL,
1762568f 8263 0, cu->language, objfile);
72bf9492 8264 break;
530e8392 8265 case DW_TAG_module:
a5fd13a9
BH
8266 /* With Fortran 77 there might be a "BLOCK DATA" module
8267 available without any name. If so, we skip the module as it
8268 doesn't bring any value. */
8269 if (actual_name != nullptr)
31edb802 8270 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8271 built_actual_name != NULL,
8272 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8273 psymbol_placement::GLOBAL,
8274 0, cu->language, objfile);
530e8392 8275 break;
c906108c 8276 case DW_TAG_class_type:
680b30c7 8277 case DW_TAG_interface_type:
c906108c
SS
8278 case DW_TAG_structure_type:
8279 case DW_TAG_union_type:
8280 case DW_TAG_enumeration_type:
fa4028e9
JB
8281 /* Skip external references. The DWARF standard says in the section
8282 about "Structure, Union, and Class Type Entries": "An incomplete
8283 structure, union or class type is represented by a structure,
8284 union or class entry that does not have a byte size attribute
8285 and that has a DW_AT_declaration attribute." */
8286 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8287 return;
fa4028e9 8288
63d06c5c
DC
8289 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8290 static vs. global. */
31edb802 8291 add_psymbol_to_list (actual_name,
15d034d0 8292 built_actual_name != NULL,
79748972 8293 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8294 cu->language == language_cplus
75aedd27
TT
8295 ? psymbol_placement::GLOBAL
8296 : psymbol_placement::STATIC,
1762568f 8297 0, cu->language, objfile);
c906108c 8298
c906108c
SS
8299 break;
8300 case DW_TAG_enumerator:
31edb802 8301 add_psymbol_to_list (actual_name,
15d034d0 8302 built_actual_name != NULL,
79748972 8303 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8304 cu->language == language_cplus
75aedd27
TT
8305 ? psymbol_placement::GLOBAL
8306 : psymbol_placement::STATIC,
1762568f 8307 0, cu->language, objfile);
c906108c
SS
8308 break;
8309 default:
8310 break;
8311 }
8312}
8313
5c4e30ca
DC
8314/* Read a partial die corresponding to a namespace; also, add a symbol
8315 corresponding to that namespace to the symbol table. NAMESPACE is
8316 the name of the enclosing namespace. */
91c24f0a 8317
72bf9492
DJ
8318static void
8319add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8320 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8321 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8322{
72bf9492 8323 /* Add a symbol for the namespace. */
e7c27a73 8324
72bf9492 8325 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8326
8327 /* Now scan partial symbols in that namespace. */
8328
91c24f0a 8329 if (pdi->has_children)
cdc07690 8330 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8331}
8332
5d7cb8df
JK
8333/* Read a partial die corresponding to a Fortran module. */
8334
8335static void
8336add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8337 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8338{
530e8392
KB
8339 /* Add a symbol for the namespace. */
8340
8341 add_partial_symbol (pdi, cu);
8342
f55ee35c 8343 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8344
8345 if (pdi->has_children)
cdc07690 8346 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8347}
8348
b1dc1806
XR
8349/* Read a partial die corresponding to a subprogram or an inlined
8350 subprogram and create a partial symbol for that subprogram.
8351 When the CU language allows it, this routine also defines a partial
8352 symbol for each nested subprogram that this subprogram contains.
8353 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8354 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8355
cdc07690
YQ
8356 PDI may also be a lexical block, in which case we simply search
8357 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8358 Again, this is only performed when the CU language allows this
8359 type of definitions. */
8360
8361static void
8362add_partial_subprogram (struct partial_die_info *pdi,
8363 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8364 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8365{
b1dc1806 8366 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8367 {
8368 if (pdi->has_pc_info)
8369 {
8370 if (pdi->lowpc < *lowpc)
8371 *lowpc = pdi->lowpc;
8372 if (pdi->highpc > *highpc)
8373 *highpc = pdi->highpc;
cdc07690 8374 if (set_addrmap)
5734ee8b 8375 {
518817b3 8376 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8377 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8378 CORE_ADDR baseaddr;
b926417a
TT
8379 CORE_ADDR this_highpc;
8380 CORE_ADDR this_lowpc;
5734ee8b 8381
b3b3bada 8382 baseaddr = objfile->text_section_offset ();
b926417a
TT
8383 this_lowpc
8384 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8385 pdi->lowpc + baseaddr)
8386 - baseaddr);
8387 this_highpc
8388 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8389 pdi->highpc + baseaddr)
8390 - baseaddr);
d320c2b5 8391 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8392 this_lowpc, this_highpc - 1,
9291a0cd 8393 cu->per_cu->v.psymtab);
5734ee8b 8394 }
481860b3
GB
8395 }
8396
8397 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8398 {
bc30ff58 8399 if (!pdi->is_declaration)
e8d05480
JB
8400 /* Ignore subprogram DIEs that do not have a name, they are
8401 illegal. Do not emit a complaint at this point, we will
8402 do so when we convert this psymtab into a symtab. */
8403 if (pdi->name)
8404 add_partial_symbol (pdi, cu);
bc30ff58
JB
8405 }
8406 }
6e70227d 8407
bc30ff58
JB
8408 if (! pdi->has_children)
8409 return;
8410
0a4b0913 8411 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8412 {
8413 pdi = pdi->die_child;
8414 while (pdi != NULL)
8415 {
52356b79 8416 pdi->fixup (cu);
bc30ff58 8417 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8418 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8419 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8420 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8421 pdi = pdi->die_sibling;
8422 }
8423 }
8424}
8425
91c24f0a
DC
8426/* Read a partial die corresponding to an enumeration type. */
8427
72bf9492
DJ
8428static void
8429add_partial_enumeration (struct partial_die_info *enum_pdi,
8430 struct dwarf2_cu *cu)
91c24f0a 8431{
72bf9492 8432 struct partial_die_info *pdi;
91c24f0a
DC
8433
8434 if (enum_pdi->name != NULL)
72bf9492
DJ
8435 add_partial_symbol (enum_pdi, cu);
8436
8437 pdi = enum_pdi->die_child;
8438 while (pdi)
91c24f0a 8439 {
72bf9492 8440 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8441 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8442 else
72bf9492
DJ
8443 add_partial_symbol (pdi, cu);
8444 pdi = pdi->die_sibling;
91c24f0a 8445 }
91c24f0a
DC
8446}
8447
6caca83c
CC
8448/* Return the initial uleb128 in the die at INFO_PTR. */
8449
8450static unsigned int
d521ce57 8451peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8452{
8453 unsigned int bytes_read;
8454
8455 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8456}
8457
685af9cd
TT
8458/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8459 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8460
4bb7a0a7
DJ
8461 Return the corresponding abbrev, or NULL if the number is zero (indicating
8462 an empty DIE). In either case *BYTES_READ will be set to the length of
8463 the initial number. */
8464
8465static struct abbrev_info *
685af9cd
TT
8466peek_die_abbrev (const die_reader_specs &reader,
8467 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8468{
685af9cd 8469 dwarf2_cu *cu = reader.cu;
518817b3 8470 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8471 unsigned int abbrev_number
8472 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8473
8474 if (abbrev_number == 0)
8475 return NULL;
8476
685af9cd 8477 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8478 if (!abbrev)
8479 {
422b9917 8480 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8481 " at offset %s [in module %s]"),
422b9917 8482 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8483 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8484 }
8485
8486 return abbrev;
8487}
8488
93311388
DE
8489/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8490 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8491 DIE. Any children of the skipped DIEs will also be skipped. */
8492
d521ce57
TT
8493static const gdb_byte *
8494skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8495{
4bb7a0a7
DJ
8496 while (1)
8497 {
685af9cd
TT
8498 unsigned int bytes_read;
8499 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8500
4bb7a0a7
DJ
8501 if (abbrev == NULL)
8502 return info_ptr + bytes_read;
8503 else
dee91e82 8504 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8505 }
8506}
8507
93311388
DE
8508/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8509 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8510 abbrev corresponding to that skipped uleb128 should be passed in
8511 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8512 children. */
8513
d521ce57
TT
8514static const gdb_byte *
8515skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8516 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8517{
8518 unsigned int bytes_read;
8519 struct attribute attr;
dee91e82
DE
8520 bfd *abfd = reader->abfd;
8521 struct dwarf2_cu *cu = reader->cu;
d521ce57 8522 const gdb_byte *buffer = reader->buffer;
f664829e 8523 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8524 unsigned int form, i;
8525
8526 for (i = 0; i < abbrev->num_attrs; i++)
8527 {
8528 /* The only abbrev we care about is DW_AT_sibling. */
8529 if (abbrev->attrs[i].name == DW_AT_sibling)
8530 {
18a8505e
AT
8531 bool ignored;
8532 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8533 &ignored);
4bb7a0a7 8534 if (attr.form == DW_FORM_ref_addr)
b98664d3 8535 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8536 else
b9502d3f 8537 {
9c541725
PA
8538 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8539 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8540
8541 if (sibling_ptr < info_ptr)
b98664d3 8542 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
8543 else if (sibling_ptr > reader->buffer_end)
8544 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8545 else
8546 return sibling_ptr;
8547 }
4bb7a0a7
DJ
8548 }
8549
8550 /* If it isn't DW_AT_sibling, skip this attribute. */
8551 form = abbrev->attrs[i].form;
8552 skip_attribute:
8553 switch (form)
8554 {
4bb7a0a7 8555 case DW_FORM_ref_addr:
ae411497
TT
8556 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8557 and later it is offset sized. */
8558 if (cu->header.version == 2)
8559 info_ptr += cu->header.addr_size;
8560 else
8561 info_ptr += cu->header.offset_size;
8562 break;
36586728
TT
8563 case DW_FORM_GNU_ref_alt:
8564 info_ptr += cu->header.offset_size;
8565 break;
ae411497 8566 case DW_FORM_addr:
4bb7a0a7
DJ
8567 info_ptr += cu->header.addr_size;
8568 break;
8569 case DW_FORM_data1:
8570 case DW_FORM_ref1:
8571 case DW_FORM_flag:
8fe0f950 8572 case DW_FORM_strx1:
4bb7a0a7
DJ
8573 info_ptr += 1;
8574 break;
2dc7f7b3 8575 case DW_FORM_flag_present:
43988095 8576 case DW_FORM_implicit_const:
2dc7f7b3 8577 break;
4bb7a0a7
DJ
8578 case DW_FORM_data2:
8579 case DW_FORM_ref2:
8fe0f950 8580 case DW_FORM_strx2:
4bb7a0a7
DJ
8581 info_ptr += 2;
8582 break;
8fe0f950
AT
8583 case DW_FORM_strx3:
8584 info_ptr += 3;
8585 break;
4bb7a0a7
DJ
8586 case DW_FORM_data4:
8587 case DW_FORM_ref4:
8fe0f950 8588 case DW_FORM_strx4:
4bb7a0a7
DJ
8589 info_ptr += 4;
8590 break;
8591 case DW_FORM_data8:
8592 case DW_FORM_ref8:
55f1336d 8593 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8594 info_ptr += 8;
8595 break;
0224619f
JK
8596 case DW_FORM_data16:
8597 info_ptr += 16;
8598 break;
4bb7a0a7 8599 case DW_FORM_string:
9b1c24c8 8600 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8601 info_ptr += bytes_read;
8602 break;
2dc7f7b3 8603 case DW_FORM_sec_offset:
4bb7a0a7 8604 case DW_FORM_strp:
36586728 8605 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8606 info_ptr += cu->header.offset_size;
8607 break;
2dc7f7b3 8608 case DW_FORM_exprloc:
4bb7a0a7
DJ
8609 case DW_FORM_block:
8610 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8611 info_ptr += bytes_read;
8612 break;
8613 case DW_FORM_block1:
8614 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8615 break;
8616 case DW_FORM_block2:
8617 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8618 break;
8619 case DW_FORM_block4:
8620 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8621 break;
336d760d 8622 case DW_FORM_addrx:
cf532bd1 8623 case DW_FORM_strx:
4bb7a0a7
DJ
8624 case DW_FORM_sdata:
8625 case DW_FORM_udata:
8626 case DW_FORM_ref_udata:
3019eac3
DE
8627 case DW_FORM_GNU_addr_index:
8628 case DW_FORM_GNU_str_index:
18a8505e 8629 case DW_FORM_rnglistx:
d521ce57 8630 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8631 break;
8632 case DW_FORM_indirect:
8633 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8634 info_ptr += bytes_read;
8635 /* We need to continue parsing from here, so just go back to
8636 the top. */
8637 goto skip_attribute;
8638
8639 default:
3e43a32a
MS
8640 error (_("Dwarf Error: Cannot handle %s "
8641 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8642 dwarf_form_name (form),
8643 bfd_get_filename (abfd));
8644 }
8645 }
8646
8647 if (abbrev->has_children)
dee91e82 8648 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8649 else
8650 return info_ptr;
8651}
8652
93311388 8653/* Locate ORIG_PDI's sibling.
dee91e82 8654 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8655
d521ce57 8656static const gdb_byte *
dee91e82
DE
8657locate_pdi_sibling (const struct die_reader_specs *reader,
8658 struct partial_die_info *orig_pdi,
d521ce57 8659 const gdb_byte *info_ptr)
91c24f0a
DC
8660{
8661 /* Do we know the sibling already? */
72bf9492 8662
91c24f0a
DC
8663 if (orig_pdi->sibling)
8664 return orig_pdi->sibling;
8665
8666 /* Are there any children to deal with? */
8667
8668 if (!orig_pdi->has_children)
8669 return info_ptr;
8670
4bb7a0a7 8671 /* Skip the children the long way. */
91c24f0a 8672
dee91e82 8673 return skip_children (reader, info_ptr);
91c24f0a
DC
8674}
8675
257e7a09 8676/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8677 not NULL. */
c906108c 8678
891813be
TT
8679void
8680dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8681{
ed2dc618
SM
8682 struct dwarf2_per_objfile *dwarf2_per_objfile
8683 = get_dwarf2_per_objfile (objfile);
8684
077cbab2
TT
8685 gdb_assert (!readin);
8686 /* If this psymtab is constructed from a debug-only objfile, the
8687 has_section_at_zero flag will not necessarily be correct. We
8688 can get the correct value for this flag by looking at the data
8689 associated with the (presumably stripped) associated objfile. */
8690 if (objfile->separate_debug_objfile_backlink)
c906108c 8691 {
077cbab2
TT
8692 struct dwarf2_per_objfile *dpo_backlink
8693 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8694
077cbab2
TT
8695 dwarf2_per_objfile->has_section_at_zero
8696 = dpo_backlink->has_section_at_zero;
8697 }
98bfdba5 8698
077cbab2 8699 dwarf2_per_objfile->reading_partial_symbols = 0;
c906108c 8700
8566b89b 8701 expand_psymtab (objfile);
95554aad 8702
ed2dc618 8703 process_cu_includes (dwarf2_per_objfile);
c906108c 8704}
9cdd5dbd
DE
8705\f
8706/* Reading in full CUs. */
c906108c 8707
10b3939b
DJ
8708/* Add PER_CU to the queue. */
8709
8710static void
95554aad
TT
8711queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8712 enum language pretend_language)
10b3939b 8713{
10b3939b 8714 per_cu->queued = 1;
39856def 8715 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8716}
8717
89e63ee4
DE
8718/* If PER_CU is not yet queued, add it to the queue.
8719 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8720 dependency.
0907af0c 8721 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8722 meaning either PER_CU is already queued or it is already loaded.
8723
8724 N.B. There is an invariant here that if a CU is queued then it is loaded.
8725 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8726
8727static int
89e63ee4 8728maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8729 struct dwarf2_per_cu_data *per_cu,
8730 enum language pretend_language)
8731{
8732 /* We may arrive here during partial symbol reading, if we need full
8733 DIEs to process an unusual case (e.g. template arguments). Do
8734 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8735 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8736 {
8737 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8738 return 1;
8739 return 0;
8740 }
8741
8742 /* Mark the dependence relation so that we don't flush PER_CU
8743 too early. */
89e63ee4
DE
8744 if (dependent_cu != NULL)
8745 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8746
8747 /* If it's already on the queue, we have nothing to do. */
8748 if (per_cu->queued)
8749 return 0;
8750
8751 /* If the compilation unit is already loaded, just mark it as
8752 used. */
8753 if (per_cu->cu != NULL)
8754 {
8755 per_cu->cu->last_used = 0;
8756 return 0;
8757 }
8758
8759 /* Add it to the queue. */
8760 queue_comp_unit (per_cu, pretend_language);
8761
8762 return 1;
8763}
8764
10b3939b
DJ
8765/* Process the queue. */
8766
8767static void
ed2dc618 8768process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8769{
b4f54984 8770 if (dwarf_read_debug)
45cfd468
DE
8771 {
8772 fprintf_unfiltered (gdb_stdlog,
8773 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8774 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8775 }
8776
03dd20cc
DJ
8777 /* The queue starts out with one item, but following a DIE reference
8778 may load a new CU, adding it to the end of the queue. */
39856def 8779 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8780 {
39856def
TT
8781 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8782
cc12ce38 8783 if ((dwarf2_per_objfile->using_index
39856def
TT
8784 ? !item.per_cu->v.quick->compunit_symtab
8785 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8786 /* Skip dummy CUs. */
39856def 8787 && item.per_cu->cu != NULL)
f4dc4d17 8788 {
39856def 8789 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8790 unsigned int debug_print_threshold;
247f5c4f 8791 char buf[100];
f4dc4d17 8792
247f5c4f 8793 if (per_cu->is_debug_types)
f4dc4d17 8794 {
247f5c4f
DE
8795 struct signatured_type *sig_type =
8796 (struct signatured_type *) per_cu;
8797
9d8780f0 8798 sprintf (buf, "TU %s at offset %s",
73be47f5 8799 hex_string (sig_type->signature),
9d8780f0 8800 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8801 /* There can be 100s of TUs.
8802 Only print them in verbose mode. */
8803 debug_print_threshold = 2;
f4dc4d17 8804 }
247f5c4f 8805 else
73be47f5 8806 {
9d8780f0
SM
8807 sprintf (buf, "CU at offset %s",
8808 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8809 debug_print_threshold = 1;
8810 }
247f5c4f 8811
b4f54984 8812 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8813 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8814
8815 if (per_cu->is_debug_types)
39856def 8816 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8817 else
39856def 8818 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8819
b4f54984 8820 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8821 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8822 }
10b3939b 8823
39856def
TT
8824 item.per_cu->queued = 0;
8825 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8826 }
8827
b4f54984 8828 if (dwarf_read_debug)
45cfd468
DE
8829 {
8830 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8831 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8832 }
10b3939b
DJ
8833}
8834
10b3939b
DJ
8835/* Read in full symbols for PST, and anything it depends on. */
8836
8566b89b
TT
8837void
8838dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8839{
10b3939b 8840 struct dwarf2_per_cu_data *per_cu;
aaa75496 8841
8566b89b 8842 if (readin)
95554aad
TT
8843 return;
8844
0494dbec 8845 read_dependencies (objfile);
aaa75496 8846
8566b89b 8847 per_cu = per_cu_data;
10b3939b
DJ
8848
8849 if (per_cu == NULL)
aaa75496
JB
8850 {
8851 /* It's an include file, no symbols to read for it.
8852 Everything is in the parent symtab. */
8566b89b 8853 readin = true;
aaa75496
JB
8854 return;
8855 }
c906108c 8856
58f0c718 8857 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
8858}
8859
dee91e82
DE
8860/* Trivial hash function for die_info: the hash value of a DIE
8861 is its offset in .debug_info for this objfile. */
10b3939b 8862
dee91e82
DE
8863static hashval_t
8864die_hash (const void *item)
10b3939b 8865{
9a3c8263 8866 const struct die_info *die = (const struct die_info *) item;
6502dd73 8867
9c541725 8868 return to_underlying (die->sect_off);
dee91e82 8869}
63d06c5c 8870
dee91e82
DE
8871/* Trivial comparison function for die_info structures: two DIEs
8872 are equal if they have the same offset. */
98bfdba5 8873
dee91e82
DE
8874static int
8875die_eq (const void *item_lhs, const void *item_rhs)
8876{
9a3c8263
SM
8877 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8878 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8879
9c541725 8880 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8881}
c906108c 8882
c0ab21c2 8883/* Load the DIEs associated with PER_CU into memory. */
c906108c 8884
dee91e82 8885static void
c0ab21c2
TT
8886load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8887 bool skip_partial,
8888 enum language pretend_language)
dee91e82 8889{
c0ab21c2
TT
8890 gdb_assert (! this_cu->is_debug_types);
8891
6751ebae 8892 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8893 if (reader.dummy_p)
8894 return;
8895
8896 struct dwarf2_cu *cu = reader.cu;
8897 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8898
dee91e82
DE
8899 gdb_assert (cu->die_hash == NULL);
8900 cu->die_hash =
8901 htab_create_alloc_ex (cu->header.length / 12,
8902 die_hash,
8903 die_eq,
8904 NULL,
8905 &cu->comp_unit_obstack,
8906 hashtab_obstack_allocate,
8907 dummy_obstack_deallocate);
e142c38c 8908
3e225074 8909 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8910 reader.comp_unit_die->child
8911 = read_die_and_siblings (&reader, reader.info_ptr,
8912 &info_ptr, reader.comp_unit_die);
8913 cu->dies = reader.comp_unit_die;
dee91e82 8914 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8915
8916 /* We try not to read any attributes in this function, because not
9cdd5dbd 8917 all CUs needed for references have been loaded yet, and symbol
10b3939b 8918 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8919 or we won't be able to build types correctly.
8920 Similarly, if we do not read the producer, we can not apply
8921 producer-specific interpretation. */
c0ab21c2 8922 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8923
8924 reader.keep ();
10b3939b
DJ
8925}
8926
3da10d80
KS
8927/* Add a DIE to the delayed physname list. */
8928
8929static void
8930add_to_method_list (struct type *type, int fnfield_index, int index,
8931 const char *name, struct die_info *die,
8932 struct dwarf2_cu *cu)
8933{
8934 struct delayed_method_info mi;
8935 mi.type = type;
8936 mi.fnfield_index = fnfield_index;
8937 mi.index = index;
8938 mi.name = name;
8939 mi.die = die;
c89b44cd 8940 cu->method_list.push_back (mi);
3da10d80
KS
8941}
8942
3693fdb3
PA
8943/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8944 "const" / "volatile". If so, decrements LEN by the length of the
8945 modifier and return true. Otherwise return false. */
8946
8947template<size_t N>
8948static bool
8949check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8950{
8951 size_t mod_len = sizeof (mod) - 1;
8952 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8953 {
8954 len -= mod_len;
8955 return true;
8956 }
8957 return false;
8958}
8959
3da10d80
KS
8960/* Compute the physnames of any methods on the CU's method list.
8961
8962 The computation of method physnames is delayed in order to avoid the
8963 (bad) condition that one of the method's formal parameters is of an as yet
8964 incomplete type. */
8965
8966static void
8967compute_delayed_physnames (struct dwarf2_cu *cu)
8968{
3693fdb3 8969 /* Only C++ delays computing physnames. */
c89b44cd 8970 if (cu->method_list.empty ())
3693fdb3
PA
8971 return;
8972 gdb_assert (cu->language == language_cplus);
8973
52941706 8974 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8975 {
1d06ead6 8976 const char *physname;
3da10d80 8977 struct fn_fieldlist *fn_flp
c89b44cd
TT
8978 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8979 physname = dwarf2_physname (mi.name, mi.die, cu);
8980 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8981 = physname ? physname : "";
3693fdb3
PA
8982
8983 /* Since there's no tag to indicate whether a method is a
8984 const/volatile overload, extract that information out of the
8985 demangled name. */
8986 if (physname != NULL)
8987 {
8988 size_t len = strlen (physname);
8989
8990 while (1)
8991 {
8992 if (physname[len] == ')') /* shortcut */
8993 break;
8994 else if (check_modifier (physname, len, " const"))
c89b44cd 8995 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8996 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8997 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8998 else
8999 break;
9000 }
9001 }
3da10d80 9002 }
c89b44cd
TT
9003
9004 /* The list is no longer needed. */
9005 cu->method_list.clear ();
3da10d80
KS
9006}
9007
a766d390
DE
9008/* Go objects should be embedded in a DW_TAG_module DIE,
9009 and it's not clear if/how imported objects will appear.
9010 To keep Go support simple until that's worked out,
9011 go back through what we've read and create something usable.
9012 We could do this while processing each DIE, and feels kinda cleaner,
9013 but that way is more invasive.
9014 This is to, for example, allow the user to type "p var" or "b main"
9015 without having to specify the package name, and allow lookups
9016 of module.object to work in contexts that use the expression
9017 parser. */
9018
9019static void
9020fixup_go_packaging (struct dwarf2_cu *cu)
9021{
421d1616 9022 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9023 struct pending *list;
9024 int i;
9025
c24bdb02 9026 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9027 list != NULL;
9028 list = list->next)
a766d390
DE
9029 {
9030 for (i = 0; i < list->nsyms; ++i)
9031 {
9032 struct symbol *sym = list->symbol[i];
9033
c1b5c1eb 9034 if (sym->language () == language_go
a766d390
DE
9035 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9036 {
421d1616
TT
9037 gdb::unique_xmalloc_ptr<char> this_package_name
9038 (go_symbol_package_name (sym));
a766d390
DE
9039
9040 if (this_package_name == NULL)
9041 continue;
9042 if (package_name == NULL)
421d1616 9043 package_name = std::move (this_package_name);
a766d390
DE
9044 else
9045 {
518817b3
SM
9046 struct objfile *objfile
9047 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9048 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9049 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9050 (symbol_symtab (sym) != NULL
9051 ? symtab_to_filename_for_display
9052 (symbol_symtab (sym))
e3b94546 9053 : objfile_name (objfile)),
421d1616 9054 this_package_name.get (), package_name.get ());
a766d390
DE
9055 }
9056 }
9057 }
9058 }
9059
9060 if (package_name != NULL)
9061 {
518817b3 9062 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9063 const char *saved_package_name
421d1616 9064 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9065 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9066 saved_package_name);
a766d390
DE
9067 struct symbol *sym;
9068
e623cf5d 9069 sym = allocate_symbol (objfile);
d3ecddab 9070 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9071 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9072 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9073 e.g., "main" finds the "main" module and not C's main(). */
9074 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9075 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9076 SYMBOL_TYPE (sym) = type;
9077
c24bdb02 9078 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9079 }
9080}
9081
c9317f21
TT
9082/* Allocate a fully-qualified name consisting of the two parts on the
9083 obstack. */
9084
9085static const char *
9086rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9087{
9088 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9089}
9090
9091/* A helper that allocates a struct discriminant_info to attach to a
9092 union type. */
9093
9094static struct discriminant_info *
9095alloc_discriminant_info (struct type *type, int discriminant_index,
9096 int default_index)
9097{
9098 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9099 gdb_assert (discriminant_index == -1
9100 || (discriminant_index >= 0
9101 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9102 gdb_assert (default_index == -1
c7b15a66 9103 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9104
9105 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9106
9107 struct discriminant_info *disc
9108 = ((struct discriminant_info *)
9109 TYPE_ZALLOC (type,
9110 offsetof (struct discriminant_info, discriminants)
9111 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9112 disc->default_index = default_index;
9113 disc->discriminant_index = discriminant_index;
9114
9115 struct dynamic_prop prop;
9116 prop.kind = PROP_UNDEFINED;
9117 prop.data.baton = disc;
9118
9119 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9120
9121 return disc;
9122}
9123
9124/* Some versions of rustc emitted enums in an unusual way.
9125
9126 Ordinary enums were emitted as unions. The first element of each
9127 structure in the union was named "RUST$ENUM$DISR". This element
9128 held the discriminant.
9129
9130 These versions of Rust also implemented the "non-zero"
9131 optimization. When the enum had two values, and one is empty and
9132 the other holds a pointer that cannot be zero, the pointer is used
9133 as the discriminant, with a zero value meaning the empty variant.
9134 Here, the union's first member is of the form
9135 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9136 where the fieldnos are the indices of the fields that should be
9137 traversed in order to find the field (which may be several fields deep)
9138 and the variantname is the name of the variant of the case when the
9139 field is zero.
9140
9141 This function recognizes whether TYPE is of one of these forms,
9142 and, if so, smashes it to be a variant type. */
9143
9144static void
9145quirk_rust_enum (struct type *type, struct objfile *objfile)
9146{
9147 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9148
9149 /* We don't need to deal with empty enums. */
9150 if (TYPE_NFIELDS (type) == 0)
9151 return;
9152
9153#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9154 if (TYPE_NFIELDS (type) == 1
9155 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9156 {
9157 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9158
9159 /* Decode the field name to find the offset of the
9160 discriminant. */
9161 ULONGEST bit_offset = 0;
9162 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9163 while (name[0] >= '0' && name[0] <= '9')
9164 {
9165 char *tail;
9166 unsigned long index = strtoul (name, &tail, 10);
9167 name = tail;
9168 if (*name != '$'
9169 || index >= TYPE_NFIELDS (field_type)
9170 || (TYPE_FIELD_LOC_KIND (field_type, index)
9171 != FIELD_LOC_KIND_BITPOS))
9172 {
b98664d3 9173 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9174 "[in module %s]"),
9175 TYPE_FIELD_NAME (type, 0),
9176 objfile_name (objfile));
9177 return;
9178 }
9179 ++name;
9180
9181 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9182 field_type = TYPE_FIELD_TYPE (field_type, index);
9183 }
9184
9185 /* Make a union to hold the variants. */
9186 struct type *union_type = alloc_type (objfile);
9187 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9188 TYPE_NFIELDS (union_type) = 3;
9189 TYPE_FIELDS (union_type)
9190 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9191 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9192 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9193
9194 /* Put the discriminant must at index 0. */
9195 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9196 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9197 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9198 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9199
9200 /* The order of fields doesn't really matter, so put the real
9201 field at index 1 and the data-less field at index 2. */
9202 struct discriminant_info *disc
9203 = alloc_discriminant_info (union_type, 0, 1);
9204 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9205 TYPE_FIELD_NAME (union_type, 1)
9206 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9207 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9208 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9209 TYPE_FIELD_NAME (union_type, 1));
9210
9211 const char *dataless_name
9212 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9213 name);
9214 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9215 dataless_name);
9216 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9217 /* NAME points into the original discriminant name, which
9218 already has the correct lifetime. */
9219 TYPE_FIELD_NAME (union_type, 2) = name;
9220 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9221 disc->discriminants[2] = 0;
9222
9223 /* Smash this type to be a structure type. We have to do this
9224 because the type has already been recorded. */
9225 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9226 TYPE_NFIELDS (type) = 1;
9227 TYPE_FIELDS (type)
9228 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9229
9230 /* Install the variant part. */
9231 TYPE_FIELD_TYPE (type, 0) = union_type;
9232 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9233 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9234 }
77c2dba3
TT
9235 /* A union with a single anonymous field is probably an old-style
9236 univariant enum. */
9237 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9238 {
c9317f21
TT
9239 /* Smash this type to be a structure type. We have to do this
9240 because the type has already been recorded. */
9241 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9242
9243 /* Make a union to hold the variants. */
9244 struct type *union_type = alloc_type (objfile);
9245 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9246 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9247 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9248 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9249 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9250
9251 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9252 const char *variant_name
9253 = rust_last_path_segment (TYPE_NAME (field_type));
9254 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9255 TYPE_NAME (field_type)
9256 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9257 TYPE_NAME (type), variant_name);
c9317f21
TT
9258
9259 /* Install the union in the outer struct type. */
9260 TYPE_NFIELDS (type) = 1;
9261 TYPE_FIELDS (type)
9262 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9263 TYPE_FIELD_TYPE (type, 0) = union_type;
9264 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9265 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9266
9267 alloc_discriminant_info (union_type, -1, 0);
9268 }
9269 else
9270 {
9271 struct type *disr_type = nullptr;
9272 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9273 {
9274 disr_type = TYPE_FIELD_TYPE (type, i);
9275
a037790e
TT
9276 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9277 {
9278 /* All fields of a true enum will be structs. */
9279 return;
9280 }
9281 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9282 {
9283 /* Could be data-less variant, so keep going. */
a037790e 9284 disr_type = nullptr;
c9317f21
TT
9285 }
9286 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9287 "RUST$ENUM$DISR") != 0)
9288 {
9289 /* Not a Rust enum. */
9290 return;
9291 }
9292 else
9293 {
9294 /* Found one. */
9295 break;
9296 }
9297 }
9298
9299 /* If we got here without a discriminant, then it's probably
9300 just a union. */
9301 if (disr_type == nullptr)
9302 return;
9303
9304 /* Smash this type to be a structure type. We have to do this
9305 because the type has already been recorded. */
9306 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9307
9308 /* Make a union to hold the variants. */
9309 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9310 struct type *union_type = alloc_type (objfile);
9311 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9312 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9313 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9314 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9315 TYPE_FIELDS (union_type)
9316 = (struct field *) TYPE_ZALLOC (union_type,
9317 (TYPE_NFIELDS (union_type)
9318 * sizeof (struct field)));
9319
9320 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9321 TYPE_NFIELDS (type) * sizeof (struct field));
9322
9323 /* Install the discriminant at index 0 in the union. */
9324 TYPE_FIELD (union_type, 0) = *disr_field;
9325 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9326 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9327
9328 /* Install the union in the outer struct type. */
9329 TYPE_FIELD_TYPE (type, 0) = union_type;
9330 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9331 TYPE_NFIELDS (type) = 1;
9332
9333 /* Set the size and offset of the union type. */
9334 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9335
9336 /* We need a way to find the correct discriminant given a
9337 variant name. For convenience we build a map here. */
9338 struct type *enum_type = FIELD_TYPE (*disr_field);
9339 std::unordered_map<std::string, ULONGEST> discriminant_map;
9340 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9341 {
9342 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9343 {
9344 const char *name
9345 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9346 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9347 }
9348 }
9349
9350 int n_fields = TYPE_NFIELDS (union_type);
9351 struct discriminant_info *disc
9352 = alloc_discriminant_info (union_type, 0, -1);
9353 /* Skip the discriminant here. */
9354 for (int i = 1; i < n_fields; ++i)
9355 {
9356 /* Find the final word in the name of this variant's type.
9357 That name can be used to look up the correct
9358 discriminant. */
9359 const char *variant_name
9360 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9361 i)));
9362
9363 auto iter = discriminant_map.find (variant_name);
9364 if (iter != discriminant_map.end ())
9365 disc->discriminants[i] = iter->second;
9366
bedda9ac 9367 /* Remove the discriminant field, if it exists. */
c9317f21 9368 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9369 if (TYPE_NFIELDS (sub_type) > 0)
9370 {
9371 --TYPE_NFIELDS (sub_type);
9372 ++TYPE_FIELDS (sub_type);
9373 }
c9317f21
TT
9374 TYPE_FIELD_NAME (union_type, i) = variant_name;
9375 TYPE_NAME (sub_type)
9376 = rust_fully_qualify (&objfile->objfile_obstack,
9377 TYPE_NAME (type), variant_name);
9378 }
9379 }
9380}
9381
9382/* Rewrite some Rust unions to be structures with variants parts. */
9383
9384static void
9385rust_union_quirks (struct dwarf2_cu *cu)
9386{
9387 gdb_assert (cu->language == language_rust);
52941706
SM
9388 for (type *type_ : cu->rust_unions)
9389 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9390 /* We don't need this any more. */
9391 cu->rust_unions.clear ();
c9317f21
TT
9392}
9393
95554aad
TT
9394/* Return the symtab for PER_CU. This works properly regardless of
9395 whether we're using the index or psymtabs. */
9396
43f3e411
DE
9397static struct compunit_symtab *
9398get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9399{
ed2dc618 9400 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9401 ? per_cu->v.quick->compunit_symtab
9402 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9403}
9404
9405/* A helper function for computing the list of all symbol tables
9406 included by PER_CU. */
9407
9408static void
4c39bc03 9409recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9410 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9411 struct dwarf2_per_cu_data *per_cu,
43f3e411 9412 struct compunit_symtab *immediate_parent)
95554aad
TT
9413{
9414 void **slot;
43f3e411 9415 struct compunit_symtab *cust;
95554aad
TT
9416
9417 slot = htab_find_slot (all_children, per_cu, INSERT);
9418 if (*slot != NULL)
9419 {
9420 /* This inclusion and its children have been processed. */
9421 return;
9422 }
9423
9424 *slot = per_cu;
9425 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9426 cust = get_compunit_symtab (per_cu);
9427 if (cust != NULL)
ec94af83
DE
9428 {
9429 /* If this is a type unit only add its symbol table if we haven't
9430 seen it yet (type unit per_cu's can share symtabs). */
9431 if (per_cu->is_debug_types)
9432 {
43f3e411 9433 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9434 if (*slot == NULL)
9435 {
43f3e411 9436 *slot = cust;
4c39bc03 9437 result->push_back (cust);
43f3e411
DE
9438 if (cust->user == NULL)
9439 cust->user = immediate_parent;
ec94af83
DE
9440 }
9441 }
9442 else
f9125b6c 9443 {
4c39bc03 9444 result->push_back (cust);
43f3e411
DE
9445 if (cust->user == NULL)
9446 cust->user = immediate_parent;
f9125b6c 9447 }
ec94af83 9448 }
95554aad 9449
ae640021
AB
9450 if (!per_cu->imported_symtabs_empty ())
9451 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9452 {
9453 recursively_compute_inclusions (result, all_children,
9454 all_type_symtabs, ptr, cust);
9455 }
95554aad
TT
9456}
9457
43f3e411 9458/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9459 PER_CU. */
9460
9461static void
43f3e411 9462compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9463{
f4dc4d17
DE
9464 gdb_assert (! per_cu->is_debug_types);
9465
ae640021 9466 if (!per_cu->imported_symtabs_empty ())
95554aad 9467 {
ae640021 9468 int len;
4c39bc03 9469 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9470 htab_t all_children, all_type_symtabs;
43f3e411 9471 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9472
9473 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9474 if (cust == NULL)
95554aad
TT
9475 return;
9476
9477 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9478 NULL, xcalloc, xfree);
ec94af83
DE
9479 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9480 NULL, xcalloc, xfree);
95554aad 9481
ae640021 9482 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9483 {
9484 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9485 all_type_symtabs, ptr, cust);
ec94af83 9486 }
95554aad 9487
ec94af83 9488 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9489 len = result_symtabs.size ();
43f3e411 9490 cust->includes
ed2dc618 9491 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9492 struct compunit_symtab *, len + 1);
4c39bc03
TT
9493 memcpy (cust->includes, result_symtabs.data (),
9494 len * sizeof (compunit_symtab *));
43f3e411 9495 cust->includes[len] = NULL;
95554aad 9496
95554aad 9497 htab_delete (all_children);
ec94af83 9498 htab_delete (all_type_symtabs);
95554aad
TT
9499 }
9500}
9501
9502/* Compute the 'includes' field for the symtabs of all the CUs we just
9503 read. */
9504
9505static void
ed2dc618 9506process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9507{
71b73764 9508 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9509 {
9510 if (! iter->is_debug_types)
43f3e411 9511 compute_compunit_symtab_includes (iter);
f4dc4d17 9512 }
95554aad 9513
c5d0225d 9514 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9515}
9516
9cdd5dbd 9517/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9518 already been loaded into memory. */
9519
9520static void
95554aad
TT
9521process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9522 enum language pretend_language)
10b3939b 9523{
10b3939b 9524 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9525 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9526 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9527 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9528 CORE_ADDR lowpc, highpc;
43f3e411 9529 struct compunit_symtab *cust;
10b3939b 9530 CORE_ADDR baseaddr;
4359dff1 9531 struct block *static_block;
3e29f34a 9532 CORE_ADDR addr;
10b3939b 9533
b3b3bada 9534 baseaddr = objfile->text_section_offset ();
10b3939b 9535
c89b44cd
TT
9536 /* Clear the list here in case something was left over. */
9537 cu->method_list.clear ();
10b3939b 9538
95554aad
TT
9539 cu->language = pretend_language;
9540 cu->language_defn = language_def (cu->language);
9541
c906108c 9542 /* Do line number decoding in read_file_scope () */
10b3939b 9543 process_die (cu->dies, cu);
c906108c 9544
a766d390
DE
9545 /* For now fudge the Go package. */
9546 if (cu->language == language_go)
9547 fixup_go_packaging (cu);
9548
5f48f8f3 9549 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9550 should be complete, and it should now be safe to compute all of the
9551 physnames. */
9552 compute_delayed_physnames (cu);
3da10d80 9553
c9317f21
TT
9554 if (cu->language == language_rust)
9555 rust_union_quirks (cu);
9556
fae299cd
DC
9557 /* Some compilers don't define a DW_AT_high_pc attribute for the
9558 compilation unit. If the DW_AT_high_pc is missing, synthesize
9559 it, by scanning the DIE's below the compilation unit. */
10b3939b 9560 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9561
3e29f34a 9562 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9563 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9564
9565 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9566 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9567 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9568 addrmap to help ensure it has an accurate map of pc values belonging to
9569 this comp unit. */
9570 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9571
c24bdb02 9572 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9573 SECT_OFF_TEXT (objfile),
9574 0);
c906108c 9575
43f3e411 9576 if (cust != NULL)
c906108c 9577 {
df15bd07 9578 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9579
8be455d7
JK
9580 /* Set symtab language to language from DW_AT_language. If the
9581 compilation is from a C file generated by language preprocessors, do
9582 not set the language if it was already deduced by start_subfile. */
43f3e411 9583 if (!(cu->language == language_c
40e3ad0e 9584 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9585 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9586
9587 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9588 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9589 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9590 there were bugs in prologue debug info, fixed later in GCC-4.5
9591 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9592
9593 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9594 needed, it would be wrong due to missing DW_AT_producer there.
9595
9596 Still one can confuse GDB by using non-standard GCC compilation
9597 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9598 */
ab260dad 9599 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9600 cust->locations_valid = 1;
e0d00bc7
JK
9601
9602 if (gcc_4_minor >= 5)
43f3e411 9603 cust->epilogue_unwind_valid = 1;
96408a79 9604
43f3e411 9605 cust->call_site_htab = cu->call_site_htab;
c906108c 9606 }
9291a0cd
TT
9607
9608 if (dwarf2_per_objfile->using_index)
43f3e411 9609 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9610 else
9611 {
891813be 9612 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9613 pst->compunit_symtab = cust;
6d94535f 9614 pst->readin = true;
9291a0cd 9615 }
c906108c 9616
95554aad 9617 /* Push it for inclusion processing later. */
c5d0225d 9618 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9619
9620 /* Not needed any more. */
c24bdb02 9621 cu->reset_builder ();
f4dc4d17 9622}
45cfd468 9623
f4dc4d17
DE
9624/* Generate full symbol information for type unit PER_CU, whose DIEs have
9625 already been loaded into memory. */
9626
9627static void
9628process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9629 enum language pretend_language)
9630{
9631 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9632 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9633 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9634 struct compunit_symtab *cust;
0186c6a7
DE
9635 struct signatured_type *sig_type;
9636
9637 gdb_assert (per_cu->is_debug_types);
9638 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9639
c89b44cd
TT
9640 /* Clear the list here in case something was left over. */
9641 cu->method_list.clear ();
f4dc4d17 9642
f4dc4d17
DE
9643 cu->language = pretend_language;
9644 cu->language_defn = language_def (cu->language);
9645
9646 /* The symbol tables are set up in read_type_unit_scope. */
9647 process_die (cu->dies, cu);
9648
9649 /* For now fudge the Go package. */
9650 if (cu->language == language_go)
9651 fixup_go_packaging (cu);
9652
5f48f8f3 9653 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9654 should be complete, and it should now be safe to compute all of the
9655 physnames. */
9656 compute_delayed_physnames (cu);
f4dc4d17 9657
c9317f21
TT
9658 if (cu->language == language_rust)
9659 rust_union_quirks (cu);
9660
f4dc4d17
DE
9661 /* TUs share symbol tables.
9662 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9663 of it with end_expandable_symtab. Otherwise, complete the addition of
9664 this TU's symbols to the existing symtab. */
43f3e411 9665 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9666 {
c24bdb02
KS
9667 buildsym_compunit *builder = cu->get_builder ();
9668 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9669 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9670
43f3e411 9671 if (cust != NULL)
f4dc4d17
DE
9672 {
9673 /* Set symtab language to language from DW_AT_language. If the
9674 compilation is from a C file generated by language preprocessors,
9675 do not set the language if it was already deduced by
9676 start_subfile. */
43f3e411
DE
9677 if (!(cu->language == language_c
9678 && COMPUNIT_FILETABS (cust)->language != language_c))
9679 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9680 }
9681 }
9682 else
9683 {
c24bdb02 9684 cu->get_builder ()->augment_type_symtab ();
43f3e411 9685 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9686 }
9687
9688 if (dwarf2_per_objfile->using_index)
43f3e411 9689 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9690 else
9691 {
891813be 9692 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9693 pst->compunit_symtab = cust;
6d94535f 9694 pst->readin = true;
45cfd468 9695 }
804d2729
TT
9696
9697 /* Not needed any more. */
c24bdb02 9698 cu->reset_builder ();
c906108c
SS
9699}
9700
95554aad
TT
9701/* Process an imported unit DIE. */
9702
9703static void
9704process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9705{
9706 struct attribute *attr;
9707
f4dc4d17
DE
9708 /* For now we don't handle imported units in type units. */
9709 if (cu->per_cu->is_debug_types)
9710 {
9711 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9712 " supported in type units [in module %s]"),
518817b3 9713 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9714 }
9715
95554aad
TT
9716 attr = dwarf2_attr (die, DW_AT_import, cu);
9717 if (attr != NULL)
9718 {
9c541725
PA
9719 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9720 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9721 dwarf2_per_cu_data *per_cu
e3b94546 9722 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9723 cu->per_cu->dwarf2_per_objfile);
95554aad 9724
69d751e3 9725 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9726 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9727 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9728
ae640021 9729 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9730 }
9731}
9732
4c8aa72d
PA
9733/* RAII object that represents a process_die scope: i.e.,
9734 starts/finishes processing a DIE. */
9735class process_die_scope
adde2bff 9736{
4c8aa72d
PA
9737public:
9738 process_die_scope (die_info *die, dwarf2_cu *cu)
9739 : m_die (die), m_cu (cu)
9740 {
9741 /* We should only be processing DIEs not already in process. */
9742 gdb_assert (!m_die->in_process);
9743 m_die->in_process = true;
9744 }
8c3cb9fa 9745
4c8aa72d
PA
9746 ~process_die_scope ()
9747 {
9748 m_die->in_process = false;
9749
9750 /* If we're done processing the DIE for the CU that owns the line
9751 header, we don't need the line header anymore. */
9752 if (m_cu->line_header_die_owner == m_die)
9753 {
9754 delete m_cu->line_header;
9755 m_cu->line_header = NULL;
9756 m_cu->line_header_die_owner = NULL;
9757 }
9758 }
9759
9760private:
9761 die_info *m_die;
9762 dwarf2_cu *m_cu;
9763};
adde2bff 9764
c906108c
SS
9765/* Process a die and its children. */
9766
9767static void
e7c27a73 9768process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9769{
4c8aa72d 9770 process_die_scope scope (die, cu);
adde2bff 9771
c906108c
SS
9772 switch (die->tag)
9773 {
9774 case DW_TAG_padding:
9775 break;
9776 case DW_TAG_compile_unit:
95554aad 9777 case DW_TAG_partial_unit:
e7c27a73 9778 read_file_scope (die, cu);
c906108c 9779 break;
348e048f
DE
9780 case DW_TAG_type_unit:
9781 read_type_unit_scope (die, cu);
9782 break;
c906108c 9783 case DW_TAG_subprogram:
0a4b0913
AB
9784 /* Nested subprograms in Fortran get a prefix. */
9785 if (cu->language == language_fortran
9786 && die->parent != NULL
9787 && die->parent->tag == DW_TAG_subprogram)
9788 cu->processing_has_namespace_info = true;
9789 /* Fall through. */
c906108c 9790 case DW_TAG_inlined_subroutine:
edb3359d 9791 read_func_scope (die, cu);
c906108c
SS
9792 break;
9793 case DW_TAG_lexical_block:
14898363
L
9794 case DW_TAG_try_block:
9795 case DW_TAG_catch_block:
e7c27a73 9796 read_lexical_block_scope (die, cu);
c906108c 9797 break;
216f72a1 9798 case DW_TAG_call_site:
96408a79
SA
9799 case DW_TAG_GNU_call_site:
9800 read_call_site_scope (die, cu);
9801 break;
c906108c 9802 case DW_TAG_class_type:
680b30c7 9803 case DW_TAG_interface_type:
c906108c
SS
9804 case DW_TAG_structure_type:
9805 case DW_TAG_union_type:
134d01f1 9806 process_structure_scope (die, cu);
c906108c
SS
9807 break;
9808 case DW_TAG_enumeration_type:
134d01f1 9809 process_enumeration_scope (die, cu);
c906108c 9810 break;
134d01f1 9811
f792889a
DJ
9812 /* These dies have a type, but processing them does not create
9813 a symbol or recurse to process the children. Therefore we can
9814 read them on-demand through read_type_die. */
c906108c 9815 case DW_TAG_subroutine_type:
72019c9c 9816 case DW_TAG_set_type:
c906108c 9817 case DW_TAG_array_type:
c906108c 9818 case DW_TAG_pointer_type:
c906108c 9819 case DW_TAG_ptr_to_member_type:
c906108c 9820 case DW_TAG_reference_type:
4297a3f0 9821 case DW_TAG_rvalue_reference_type:
c906108c 9822 case DW_TAG_string_type:
c906108c 9823 break;
134d01f1 9824
c906108c 9825 case DW_TAG_base_type:
a02abb62 9826 case DW_TAG_subrange_type:
cb249c71 9827 case DW_TAG_typedef:
134d01f1
DJ
9828 /* Add a typedef symbol for the type definition, if it has a
9829 DW_AT_name. */
f792889a 9830 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9831 break;
c906108c 9832 case DW_TAG_common_block:
e7c27a73 9833 read_common_block (die, cu);
c906108c
SS
9834 break;
9835 case DW_TAG_common_inclusion:
9836 break;
d9fa45fe 9837 case DW_TAG_namespace:
9068261f 9838 cu->processing_has_namespace_info = true;
e7c27a73 9839 read_namespace (die, cu);
d9fa45fe 9840 break;
5d7cb8df 9841 case DW_TAG_module:
9068261f 9842 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9843 read_module (die, cu);
9844 break;
d9fa45fe 9845 case DW_TAG_imported_declaration:
9068261f 9846 cu->processing_has_namespace_info = true;
74921315
KS
9847 if (read_namespace_alias (die, cu))
9848 break;
86a73007
TT
9849 /* The declaration is not a global namespace alias. */
9850 /* Fall through. */
d9fa45fe 9851 case DW_TAG_imported_module:
9068261f 9852 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9853 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9854 || cu->language != language_fortran))
b98664d3 9855 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9856 dwarf_tag_name (die->tag));
9857 read_import_statement (die, cu);
d9fa45fe 9858 break;
95554aad
TT
9859
9860 case DW_TAG_imported_unit:
9861 process_imported_unit_die (die, cu);
9862 break;
9863
71a3c369
TT
9864 case DW_TAG_variable:
9865 read_variable (die, cu);
9866 break;
9867
c906108c 9868 default:
e7c27a73 9869 new_symbol (die, NULL, cu);
c906108c
SS
9870 break;
9871 }
9872}
ca69b9e6
DE
9873\f
9874/* DWARF name computation. */
c906108c 9875
94af9270
KS
9876/* A helper function for dwarf2_compute_name which determines whether DIE
9877 needs to have the name of the scope prepended to the name listed in the
9878 die. */
9879
9880static int
9881die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9882{
1c809c68
TT
9883 struct attribute *attr;
9884
94af9270
KS
9885 switch (die->tag)
9886 {
9887 case DW_TAG_namespace:
9888 case DW_TAG_typedef:
9889 case DW_TAG_class_type:
9890 case DW_TAG_interface_type:
9891 case DW_TAG_structure_type:
9892 case DW_TAG_union_type:
9893 case DW_TAG_enumeration_type:
9894 case DW_TAG_enumerator:
9895 case DW_TAG_subprogram:
08a76f8a 9896 case DW_TAG_inlined_subroutine:
94af9270 9897 case DW_TAG_member:
74921315 9898 case DW_TAG_imported_declaration:
94af9270
KS
9899 return 1;
9900
9901 case DW_TAG_variable:
c2b0a229 9902 case DW_TAG_constant:
94af9270
KS
9903 /* We only need to prefix "globally" visible variables. These include
9904 any variable marked with DW_AT_external or any variable that
9905 lives in a namespace. [Variables in anonymous namespaces
9906 require prefixing, but they are not DW_AT_external.] */
9907
9908 if (dwarf2_attr (die, DW_AT_specification, cu))
9909 {
9910 struct dwarf2_cu *spec_cu = cu;
9a619af0 9911
94af9270
KS
9912 return die_needs_namespace (die_specification (die, &spec_cu),
9913 spec_cu);
9914 }
9915
1c809c68 9916 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9917 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9918 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9919 return 0;
9920 /* A variable in a lexical block of some kind does not need a
9921 namespace, even though in C++ such variables may be external
9922 and have a mangled name. */
9923 if (die->parent->tag == DW_TAG_lexical_block
9924 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9925 || die->parent->tag == DW_TAG_catch_block
9926 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9927 return 0;
9928 return 1;
94af9270
KS
9929
9930 default:
9931 return 0;
9932 }
9933}
9934
73b9be8b
KS
9935/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9936 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9937 defined for the given DIE. */
9938
9939static struct attribute *
9940dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9941{
9942 struct attribute *attr;
9943
9944 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9945 if (attr == NULL)
9946 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9947
9948 return attr;
9949}
9950
9951/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9952 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9953 defined for the given DIE. */
9954
9955static const char *
9956dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9957{
9958 const char *linkage_name;
9959
9960 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9961 if (linkage_name == NULL)
9962 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9963
9964 return linkage_name;
9965}
9966
94af9270 9967/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9968 compute the physname for the object, which include a method's:
9c37b5ae 9969 - formal parameters (C++),
a766d390 9970 - receiver type (Go),
a766d390
DE
9971
9972 The term "physname" is a bit confusing.
9973 For C++, for example, it is the demangled name.
9974 For Go, for example, it's the mangled name.
94af9270 9975
af6b7be1
JB
9976 For Ada, return the DIE's linkage name rather than the fully qualified
9977 name. PHYSNAME is ignored..
9978
94af9270
KS
9979 The result is allocated on the objfile_obstack and canonicalized. */
9980
9981static const char *
15d034d0
TT
9982dwarf2_compute_name (const char *name,
9983 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9984 int physname)
9985{
518817b3 9986 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 9987
94af9270
KS
9988 if (name == NULL)
9989 name = dwarf2_name (die, cu);
9990
2ee7123e
DE
9991 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9992 but otherwise compute it by typename_concat inside GDB.
9993 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9994 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9995 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9996 will set the demangled name to the result of dwarf2_full_name, and it is
9997 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9998 if (cu->language == language_ada
9999 || (cu->language == language_fortran && physname))
10000 {
10001 /* For Ada unit, we prefer the linkage name over the name, as
10002 the former contains the exported name, which the user expects
10003 to be able to reference. Ideally, we want the user to be able
10004 to reference this entity using either natural or linkage name,
10005 but we haven't started looking at this enhancement yet. */
73b9be8b 10006 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10007
2ee7123e
DE
10008 if (linkage_name != NULL)
10009 return linkage_name;
f55ee35c
JK
10010 }
10011
94af9270
KS
10012 /* These are the only languages we know how to qualify names in. */
10013 if (name != NULL
9c37b5ae 10014 && (cu->language == language_cplus
c44af4eb
TT
10015 || cu->language == language_fortran || cu->language == language_d
10016 || cu->language == language_rust))
94af9270
KS
10017 {
10018 if (die_needs_namespace (die, cu))
10019 {
0d5cff50 10020 const char *prefix;
34a68019 10021 const char *canonical_name = NULL;
94af9270 10022
d7e74731
PA
10023 string_file buf;
10024
94af9270 10025 prefix = determine_prefix (die, cu);
94af9270
KS
10026 if (*prefix != '\0')
10027 {
43816ebc
TT
10028 gdb::unique_xmalloc_ptr<char> prefixed_name
10029 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10030
43816ebc 10031 buf.puts (prefixed_name.get ());
94af9270
KS
10032 }
10033 else
d7e74731 10034 buf.puts (name);
94af9270 10035
98bfdba5
PA
10036 /* Template parameters may be specified in the DIE's DW_AT_name, or
10037 as children with DW_TAG_template_type_param or
10038 DW_TAG_value_type_param. If the latter, add them to the name
10039 here. If the name already has template parameters, then
10040 skip this step; some versions of GCC emit both, and
10041 it is more efficient to use the pre-computed name.
10042
10043 Something to keep in mind about this process: it is very
10044 unlikely, or in some cases downright impossible, to produce
10045 something that will match the mangled name of a function.
10046 If the definition of the function has the same debug info,
10047 we should be able to match up with it anyway. But fallbacks
10048 using the minimal symbol, for instance to find a method
10049 implemented in a stripped copy of libstdc++, will not work.
10050 If we do not have debug info for the definition, we will have to
10051 match them up some other way.
10052
10053 When we do name matching there is a related problem with function
10054 templates; two instantiated function templates are allowed to
10055 differ only by their return types, which we do not add here. */
10056
10057 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10058 {
10059 struct attribute *attr;
10060 struct die_info *child;
10061 int first = 1;
10062
10063 die->building_fullname = 1;
10064
10065 for (child = die->child; child != NULL; child = child->sibling)
10066 {
10067 struct type *type;
12df843f 10068 LONGEST value;
d521ce57 10069 const gdb_byte *bytes;
98bfdba5
PA
10070 struct dwarf2_locexpr_baton *baton;
10071 struct value *v;
10072
10073 if (child->tag != DW_TAG_template_type_param
10074 && child->tag != DW_TAG_template_value_param)
10075 continue;
10076
10077 if (first)
10078 {
d7e74731 10079 buf.puts ("<");
98bfdba5
PA
10080 first = 0;
10081 }
10082 else
d7e74731 10083 buf.puts (", ");
98bfdba5
PA
10084
10085 attr = dwarf2_attr (child, DW_AT_type, cu);
10086 if (attr == NULL)
10087 {
b98664d3 10088 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10089 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10090 continue;
10091 }
10092 type = die_type (child, cu);
10093
10094 if (child->tag == DW_TAG_template_type_param)
10095 {
c1ec8cea
TT
10096 c_print_type (type, "", &buf, -1, 0, cu->language,
10097 &type_print_raw_options);
98bfdba5
PA
10098 continue;
10099 }
10100
10101 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10102 if (attr == NULL)
10103 {
b98664d3 10104 complaint (_("template parameter missing "
3e43a32a 10105 "DW_AT_const_value"));
d7e74731 10106 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10107 continue;
10108 }
10109
10110 dwarf2_const_value_attr (attr, type, name,
10111 &cu->comp_unit_obstack, cu,
10112 &value, &bytes, &baton);
10113
10114 if (TYPE_NOSIGN (type))
10115 /* GDB prints characters as NUMBER 'CHAR'. If that's
10116 changed, this can use value_print instead. */
d7e74731 10117 c_printchar (value, type, &buf);
98bfdba5
PA
10118 else
10119 {
10120 struct value_print_options opts;
10121
10122 if (baton != NULL)
10123 v = dwarf2_evaluate_loc_desc (type, NULL,
10124 baton->data,
10125 baton->size,
10126 baton->per_cu);
10127 else if (bytes != NULL)
10128 {
10129 v = allocate_value (type);
10130 memcpy (value_contents_writeable (v), bytes,
10131 TYPE_LENGTH (type));
10132 }
10133 else
10134 v = value_from_longest (type, value);
10135
3e43a32a
MS
10136 /* Specify decimal so that we do not depend on
10137 the radix. */
98bfdba5
PA
10138 get_formatted_print_options (&opts, 'd');
10139 opts.raw = 1;
d7e74731 10140 value_print (v, &buf, &opts);
98bfdba5 10141 release_value (v);
98bfdba5
PA
10142 }
10143 }
10144
10145 die->building_fullname = 0;
10146
10147 if (!first)
10148 {
10149 /* Close the argument list, with a space if necessary
10150 (nested templates). */
d7e74731
PA
10151 if (!buf.empty () && buf.string ().back () == '>')
10152 buf.puts (" >");
98bfdba5 10153 else
d7e74731 10154 buf.puts (">");
98bfdba5
PA
10155 }
10156 }
10157
9c37b5ae 10158 /* For C++ methods, append formal parameter type
94af9270 10159 information, if PHYSNAME. */
6e70227d 10160
94af9270 10161 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10162 && cu->language == language_cplus)
94af9270
KS
10163 {
10164 struct type *type = read_type_die (die, cu);
10165
d7e74731 10166 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10167 &type_print_raw_options);
94af9270 10168
9c37b5ae 10169 if (cu->language == language_cplus)
94af9270 10170 {
60430eff
DJ
10171 /* Assume that an artificial first parameter is
10172 "this", but do not crash if it is not. RealView
10173 marks unnamed (and thus unused) parameters as
10174 artificial; there is no way to differentiate
10175 the two cases. */
94af9270
KS
10176 if (TYPE_NFIELDS (type) > 0
10177 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10178 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10179 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10180 0))))
d7e74731 10181 buf.puts (" const");
94af9270
KS
10182 }
10183 }
10184
d7e74731 10185 const std::string &intermediate_name = buf.string ();
94af9270
KS
10186
10187 if (cu->language == language_cplus)
34a68019 10188 canonical_name
322a8516 10189 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10190 &objfile->per_bfd->storage_obstack);
10191
10192 /* If we only computed INTERMEDIATE_NAME, or if
10193 INTERMEDIATE_NAME is already canonical, then we need to
10194 copy it to the appropriate obstack. */
322a8516 10195 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10196 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10197 intermediate_name);
34a68019
TT
10198 else
10199 name = canonical_name;
94af9270
KS
10200 }
10201 }
10202
10203 return name;
10204}
10205
0114d602
DJ
10206/* Return the fully qualified name of DIE, based on its DW_AT_name.
10207 If scope qualifiers are appropriate they will be added. The result
34a68019 10208 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10209 not have a name. NAME may either be from a previous call to
10210 dwarf2_name or NULL.
10211
9c37b5ae 10212 The output string will be canonicalized (if C++). */
0114d602
DJ
10213
10214static const char *
15d034d0 10215dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10216{
94af9270
KS
10217 return dwarf2_compute_name (name, die, cu, 0);
10218}
0114d602 10219
94af9270
KS
10220/* Construct a physname for the given DIE in CU. NAME may either be
10221 from a previous call to dwarf2_name or NULL. The result will be
10222 allocated on the objfile_objstack or NULL if the DIE does not have a
10223 name.
0114d602 10224
9c37b5ae 10225 The output string will be canonicalized (if C++). */
0114d602 10226
94af9270 10227static const char *
15d034d0 10228dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10229{
518817b3 10230 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10231 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10232 int need_copy = 1;
10233
10234 /* In this case dwarf2_compute_name is just a shortcut not building anything
10235 on its own. */
10236 if (!die_needs_namespace (die, cu))
10237 return dwarf2_compute_name (name, die, cu, 1);
10238
73b9be8b 10239 mangled = dw2_linkage_name (die, cu);
900e11f9 10240
e98c9e7c
TT
10241 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10242 See https://github.com/rust-lang/rust/issues/32925. */
10243 if (cu->language == language_rust && mangled != NULL
10244 && strchr (mangled, '{') != NULL)
10245 mangled = NULL;
10246
900e11f9
JK
10247 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10248 has computed. */
791afaa2 10249 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10250 if (mangled != NULL)
900e11f9 10251 {
900e11f9 10252
59cc4834
JB
10253 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10254 {
10255 /* Do nothing (do not demangle the symbol name). */
10256 }
10257 else if (cu->language == language_go)
a766d390 10258 {
5e2db402
TT
10259 /* This is a lie, but we already lie to the caller new_symbol.
10260 new_symbol assumes we return the mangled name.
a766d390 10261 This just undoes that lie until things are cleaned up. */
a766d390
DE
10262 }
10263 else
10264 {
0eb876f5
JB
10265 /* Use DMGL_RET_DROP for C++ template functions to suppress
10266 their return type. It is easier for GDB users to search
10267 for such functions as `name(params)' than `long name(params)'.
10268 In such case the minimal symbol names do not match the full
10269 symbol names but for template functions there is never a need
10270 to look up their definition from their declaration so
10271 the only disadvantage remains the minimal symbol variant
10272 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10273 demangled.reset (gdb_demangle (mangled,
10274 (DMGL_PARAMS | DMGL_ANSI
10275 | DMGL_RET_DROP)));
a766d390 10276 }
900e11f9 10277 if (demangled)
791afaa2 10278 canon = demangled.get ();
900e11f9
JK
10279 else
10280 {
10281 canon = mangled;
10282 need_copy = 0;
10283 }
10284 }
10285
10286 if (canon == NULL || check_physname)
10287 {
10288 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10289
10290 if (canon != NULL && strcmp (physname, canon) != 0)
10291 {
10292 /* It may not mean a bug in GDB. The compiler could also
10293 compute DW_AT_linkage_name incorrectly. But in such case
10294 GDB would need to be bug-to-bug compatible. */
10295
b98664d3 10296 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10297 "(from linkage <%s>) - DIE at %s [in module %s]"),
10298 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10299 objfile_name (objfile));
900e11f9
JK
10300
10301 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10302 is available here - over computed PHYSNAME. It is safer
10303 against both buggy GDB and buggy compilers. */
10304
10305 retval = canon;
10306 }
10307 else
10308 {
10309 retval = physname;
10310 need_copy = 0;
10311 }
10312 }
10313 else
10314 retval = canon;
10315
10316 if (need_copy)
021887d8 10317 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 10318
900e11f9 10319 return retval;
0114d602
DJ
10320}
10321
74921315
KS
10322/* Inspect DIE in CU for a namespace alias. If one exists, record
10323 a new symbol for it.
10324
10325 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10326
10327static int
10328read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10329{
10330 struct attribute *attr;
10331
10332 /* If the die does not have a name, this is not a namespace
10333 alias. */
10334 attr = dwarf2_attr (die, DW_AT_name, cu);
10335 if (attr != NULL)
10336 {
10337 int num;
10338 struct die_info *d = die;
10339 struct dwarf2_cu *imported_cu = cu;
10340
10341 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10342 keep inspecting DIEs until we hit the underlying import. */
10343#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10344 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10345 {
10346 attr = dwarf2_attr (d, DW_AT_import, cu);
10347 if (attr == NULL)
10348 break;
10349
10350 d = follow_die_ref (d, attr, &imported_cu);
10351 if (d->tag != DW_TAG_imported_declaration)
10352 break;
10353 }
10354
10355 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10356 {
b98664d3 10357 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10358 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10359 return 0;
10360 }
10361
10362 if (attr != NULL)
10363 {
10364 struct type *type;
9c541725 10365 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10366
9c541725 10367 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10368 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10369 {
10370 /* This declaration is a global namespace alias. Add
10371 a symbol for it whose type is the aliased namespace. */
10372 new_symbol (die, type, cu);
10373 return 1;
10374 }
10375 }
10376 }
10377
10378 return 0;
10379}
10380
22cee43f 10381/* Return the using directives repository (global or local?) to use in the
804d2729 10382 current context for CU.
22cee43f
PMR
10383
10384 For Ada, imported declarations can materialize renamings, which *may* be
10385 global. However it is impossible (for now?) in DWARF to distinguish
10386 "external" imported declarations and "static" ones. As all imported
10387 declarations seem to be static in all other languages, make them all CU-wide
10388 global only in Ada. */
10389
10390static struct using_direct **
804d2729 10391using_directives (struct dwarf2_cu *cu)
22cee43f 10392{
c24bdb02
KS
10393 if (cu->language == language_ada
10394 && cu->get_builder ()->outermost_context_p ())
10395 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10396 else
c24bdb02 10397 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10398}
10399
27aa8d6a
SW
10400/* Read the import statement specified by the given die and record it. */
10401
10402static void
10403read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10404{
518817b3 10405 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10406 struct attribute *import_attr;
32019081 10407 struct die_info *imported_die, *child_die;
de4affc9 10408 struct dwarf2_cu *imported_cu;
27aa8d6a 10409 const char *imported_name;
794684b6 10410 const char *imported_name_prefix;
13387711
SW
10411 const char *canonical_name;
10412 const char *import_alias;
10413 const char *imported_declaration = NULL;
794684b6 10414 const char *import_prefix;
eb1e02fd 10415 std::vector<const char *> excludes;
13387711 10416
27aa8d6a
SW
10417 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10418 if (import_attr == NULL)
10419 {
b98664d3 10420 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10421 dwarf_tag_name (die->tag));
10422 return;
10423 }
10424
de4affc9
CC
10425 imported_cu = cu;
10426 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10427 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10428 if (imported_name == NULL)
10429 {
10430 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10431
10432 The import in the following code:
10433 namespace A
10434 {
10435 typedef int B;
10436 }
10437
10438 int main ()
10439 {
10440 using A::B;
10441 B b;
10442 return b;
10443 }
10444
10445 ...
10446 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10447 <52> DW_AT_decl_file : 1
10448 <53> DW_AT_decl_line : 6
10449 <54> DW_AT_import : <0x75>
10450 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10451 <59> DW_AT_name : B
10452 <5b> DW_AT_decl_file : 1
10453 <5c> DW_AT_decl_line : 2
10454 <5d> DW_AT_type : <0x6e>
10455 ...
10456 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10457 <76> DW_AT_byte_size : 4
10458 <77> DW_AT_encoding : 5 (signed)
10459
10460 imports the wrong die ( 0x75 instead of 0x58 ).
10461 This case will be ignored until the gcc bug is fixed. */
10462 return;
10463 }
10464
82856980
SW
10465 /* Figure out the local name after import. */
10466 import_alias = dwarf2_name (die, cu);
27aa8d6a 10467
794684b6
SW
10468 /* Figure out where the statement is being imported to. */
10469 import_prefix = determine_prefix (die, cu);
10470
10471 /* Figure out what the scope of the imported die is and prepend it
10472 to the name of the imported die. */
de4affc9 10473 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10474
f55ee35c
JK
10475 if (imported_die->tag != DW_TAG_namespace
10476 && imported_die->tag != DW_TAG_module)
794684b6 10477 {
13387711
SW
10478 imported_declaration = imported_name;
10479 canonical_name = imported_name_prefix;
794684b6 10480 }
13387711 10481 else if (strlen (imported_name_prefix) > 0)
12aaed36 10482 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10483 imported_name_prefix,
10484 (cu->language == language_d ? "." : "::"),
10485 imported_name, (char *) NULL);
13387711
SW
10486 else
10487 canonical_name = imported_name;
794684b6 10488
32019081
JK
10489 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10490 for (child_die = die->child; child_die && child_die->tag;
10491 child_die = sibling_die (child_die))
10492 {
10493 /* DWARF-4: A Fortran use statement with a “rename list” may be
10494 represented by an imported module entry with an import attribute
10495 referring to the module and owned entries corresponding to those
10496 entities that are renamed as part of being imported. */
10497
10498 if (child_die->tag != DW_TAG_imported_declaration)
10499 {
b98664d3 10500 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10501 "- DIE at %s [in module %s]"),
10502 sect_offset_str (child_die->sect_off),
10503 objfile_name (objfile));
32019081
JK
10504 continue;
10505 }
10506
10507 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10508 if (import_attr == NULL)
10509 {
b98664d3 10510 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10511 dwarf_tag_name (child_die->tag));
10512 continue;
10513 }
10514
10515 imported_cu = cu;
10516 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10517 &imported_cu);
10518 imported_name = dwarf2_name (imported_die, imported_cu);
10519 if (imported_name == NULL)
10520 {
b98664d3 10521 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10522 "imported name - DIE at %s [in module %s]"),
10523 sect_offset_str (child_die->sect_off),
10524 objfile_name (objfile));
32019081
JK
10525 continue;
10526 }
10527
eb1e02fd 10528 excludes.push_back (imported_name);
32019081
JK
10529
10530 process_die (child_die, cu);
10531 }
10532
804d2729 10533 add_using_directive (using_directives (cu),
22cee43f
PMR
10534 import_prefix,
10535 canonical_name,
10536 import_alias,
10537 imported_declaration,
10538 excludes,
10539 0,
10540 &objfile->objfile_obstack);
27aa8d6a
SW
10541}
10542
5230b05a
WT
10543/* ICC<14 does not output the required DW_AT_declaration on incomplete
10544 types, but gives them a size of zero. Starting with version 14,
10545 ICC is compatible with GCC. */
10546
9068261f 10547static bool
5230b05a
WT
10548producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10549{
10550 if (!cu->checked_producer)
10551 check_producer (cu);
10552
10553 return cu->producer_is_icc_lt_14;
10554}
10555
eb77c9df
AB
10556/* ICC generates a DW_AT_type for C void functions. This was observed on
10557 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10558 which says that void functions should not have a DW_AT_type. */
10559
10560static bool
10561producer_is_icc (struct dwarf2_cu *cu)
10562{
10563 if (!cu->checked_producer)
10564 check_producer (cu);
10565
10566 return cu->producer_is_icc;
10567}
10568
1b80a9fa
JK
10569/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10570 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10571 this, it was first present in GCC release 4.3.0. */
10572
9068261f 10573static bool
1b80a9fa
JK
10574producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10575{
10576 if (!cu->checked_producer)
10577 check_producer (cu);
10578
10579 return cu->producer_is_gcc_lt_4_3;
10580}
10581
d721ba37
PA
10582static file_and_directory
10583find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10584{
d721ba37
PA
10585 file_and_directory res;
10586
9291a0cd
TT
10587 /* Find the filename. Do not use dwarf2_name here, since the filename
10588 is not a source language identifier. */
d721ba37
PA
10589 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10590 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10591
d721ba37
PA
10592 if (res.comp_dir == NULL
10593 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10594 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10595 {
d721ba37
PA
10596 res.comp_dir_storage = ldirname (res.name);
10597 if (!res.comp_dir_storage.empty ())
10598 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10599 }
d721ba37 10600 if (res.comp_dir != NULL)
9291a0cd
TT
10601 {
10602 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10603 directory, get rid of it. */
d721ba37 10604 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10605
d721ba37
PA
10606 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10607 res.comp_dir = cp + 1;
9291a0cd
TT
10608 }
10609
d721ba37
PA
10610 if (res.name == NULL)
10611 res.name = "<unknown>";
10612
10613 return res;
9291a0cd
TT
10614}
10615
f4dc4d17
DE
10616/* Handle DW_AT_stmt_list for a compilation unit.
10617 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10618 COMP_DIR is the compilation directory. LOWPC is passed to
10619 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10620
10621static void
10622handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10623 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10624{
518817b3
SM
10625 struct dwarf2_per_objfile *dwarf2_per_objfile
10626 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10627 struct attribute *attr;
527f3840
JK
10628 struct line_header line_header_local;
10629 hashval_t line_header_local_hash;
527f3840
JK
10630 void **slot;
10631 int decode_mapping;
2ab95328 10632
f4dc4d17
DE
10633 gdb_assert (! cu->per_cu->is_debug_types);
10634
2ab95328 10635 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10636 if (attr == NULL)
10637 return;
10638
9c541725 10639 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10640
10641 /* The line header hash table is only created if needed (it exists to
10642 prevent redundant reading of the line table for partial_units).
10643 If we're given a partial_unit, we'll need it. If we're given a
10644 compile_unit, then use the line header hash table if it's already
10645 created, but don't create one just yet. */
10646
10647 if (dwarf2_per_objfile->line_header_hash == NULL
10648 && die->tag == DW_TAG_partial_unit)
2ab95328 10649 {
527f3840 10650 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10651 .reset (htab_create_alloc (127, line_header_hash_voidp,
10652 line_header_eq_voidp,
10653 free_line_header_voidp,
10654 xcalloc, xfree));
527f3840 10655 }
2ab95328 10656
9c541725 10657 line_header_local.sect_off = line_offset;
527f3840
JK
10658 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10659 line_header_local_hash = line_header_hash (&line_header_local);
10660 if (dwarf2_per_objfile->line_header_hash != NULL)
10661 {
d15acc42 10662 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10663 &line_header_local,
10664 line_header_local_hash, NO_INSERT);
10665
10666 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10667 is not present in *SLOT (since if there is something in *SLOT then
10668 it will be for a partial_unit). */
10669 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10670 {
527f3840 10671 gdb_assert (*slot != NULL);
9a3c8263 10672 cu->line_header = (struct line_header *) *slot;
527f3840 10673 return;
dee91e82 10674 }
2ab95328 10675 }
527f3840
JK
10676
10677 /* dwarf_decode_line_header does not yet provide sufficient information.
10678 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10679 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10680 if (lh == NULL)
527f3840 10681 return;
4c8aa72d
PA
10682
10683 cu->line_header = lh.release ();
10684 cu->line_header_die_owner = die;
527f3840
JK
10685
10686 if (dwarf2_per_objfile->line_header_hash == NULL)
10687 slot = NULL;
10688 else
10689 {
d15acc42 10690 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10691 &line_header_local,
10692 line_header_local_hash, INSERT);
10693 gdb_assert (slot != NULL);
10694 }
10695 if (slot != NULL && *slot == NULL)
10696 {
10697 /* This newly decoded line number information unit will be owned
10698 by line_header_hash hash table. */
10699 *slot = cu->line_header;
4c8aa72d 10700 cu->line_header_die_owner = NULL;
527f3840
JK
10701 }
10702 else
10703 {
10704 /* We cannot free any current entry in (*slot) as that struct line_header
10705 may be already used by multiple CUs. Create only temporary decoded
10706 line_header for this CU - it may happen at most once for each line
10707 number information unit. And if we're not using line_header_hash
10708 then this is what we want as well. */
10709 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10710 }
10711 decode_mapping = (die->tag != DW_TAG_partial_unit);
10712 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10713 decode_mapping);
fff8551c 10714
2ab95328
TT
10715}
10716
95554aad 10717/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10718
c906108c 10719static void
e7c27a73 10720read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10721{
518817b3
SM
10722 struct dwarf2_per_objfile *dwarf2_per_objfile
10723 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10724 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10725 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10726 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10727 CORE_ADDR highpc = ((CORE_ADDR) 0);
10728 struct attribute *attr;
c906108c 10729 struct die_info *child_die;
e142c38c 10730 CORE_ADDR baseaddr;
6e70227d 10731
380618d6 10732 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10733 baseaddr = objfile->text_section_offset ();
c906108c 10734
fae299cd 10735 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10736
10737 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10738 from finish_block. */
2acceee2 10739 if (lowpc == ((CORE_ADDR) -1))
c906108c 10740 lowpc = highpc;
3e29f34a 10741 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10742
d721ba37 10743 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10744
f4b8a18d
KW
10745 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10746 standardised yet. As a workaround for the language detection we fall
10747 back to the DW_AT_producer string. */
10748 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10749 cu->language = language_opencl;
10750
3019eac3
DE
10751 /* Similar hack for Go. */
10752 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10753 set_cu_language (DW_LANG_Go, cu);
10754
c24bdb02 10755 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10756
10757 /* Decode line number information if present. We do this before
10758 processing child DIEs, so that the line header table is available
10759 for DW_AT_decl_file. */
d721ba37 10760 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10761
10762 /* Process all dies in compilation unit. */
10763 if (die->child != NULL)
10764 {
10765 child_die = die->child;
10766 while (child_die && child_die->tag)
10767 {
10768 process_die (child_die, cu);
10769 child_die = sibling_die (child_die);
10770 }
10771 }
10772
10773 /* Decode macro information, if present. Dwarf 2 macro information
10774 refers to information in the line number info statement program
10775 header, so we can only read it if we've read the header
10776 successfully. */
0af92d60
JK
10777 attr = dwarf2_attr (die, DW_AT_macros, cu);
10778 if (attr == NULL)
10779 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10780 if (attr && cu->line_header)
10781 {
10782 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10783 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10784
43f3e411 10785 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10786 }
10787 else
10788 {
10789 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10790 if (attr && cu->line_header)
10791 {
10792 unsigned int macro_offset = DW_UNSND (attr);
10793
43f3e411 10794 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10795 }
10796 }
3019eac3
DE
10797}
10798
c24bdb02
KS
10799void
10800dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10801{
f4dc4d17
DE
10802 struct type_unit_group *tu_group;
10803 int first_time;
3019eac3 10804 struct attribute *attr;
9c541725 10805 unsigned int i;
0186c6a7 10806 struct signatured_type *sig_type;
3019eac3 10807
f4dc4d17 10808 gdb_assert (per_cu->is_debug_types);
0186c6a7 10809 sig_type = (struct signatured_type *) per_cu;
3019eac3 10810
c24bdb02 10811 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10812
f4dc4d17 10813 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10814 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10815 if (sig_type->type_unit_group == NULL)
c24bdb02 10816 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10817 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10818
10819 /* If we've already processed this stmt_list there's no real need to
10820 do it again, we could fake it and just recreate the part we need
10821 (file name,index -> symtab mapping). If data shows this optimization
10822 is useful we can do it then. */
43f3e411 10823 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10824
10825 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10826 debug info. */
fff8551c 10827 line_header_up lh;
f4dc4d17 10828 if (attr != NULL)
3019eac3 10829 {
9c541725 10830 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10831 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10832 }
10833 if (lh == NULL)
10834 {
10835 if (first_time)
c24bdb02 10836 start_symtab ("", NULL, 0);
f4dc4d17
DE
10837 else
10838 {
10839 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10840 gdb_assert (m_builder == nullptr);
804d2729 10841 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10842 m_builder.reset (new struct buildsym_compunit
10843 (COMPUNIT_OBJFILE (cust), "",
10844 COMPUNIT_DIRNAME (cust),
10845 compunit_language (cust),
10846 0, cust));
f4dc4d17 10847 }
f4dc4d17 10848 return;
3019eac3
DE
10849 }
10850
c24bdb02
KS
10851 line_header = lh.release ();
10852 line_header_die_owner = die;
3019eac3 10853
f4dc4d17
DE
10854 if (first_time)
10855 {
c24bdb02 10856 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10857
1fd60fc0
DE
10858 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10859 still initializing it, and our caller (a few levels up)
10860 process_full_type_unit still needs to know if this is the first
10861 time. */
10862
7ba99d21 10863 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 10864 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 10865 line_header->file_names_size ());
3019eac3 10866
7ba99d21
AT
10867 auto &file_names = line_header->file_names ();
10868 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10869 {
7ba99d21 10870 file_entry &fe = file_names[i];
c24bdb02
KS
10871 dwarf2_start_subfile (this, fe.name,
10872 fe.include_dir (line_header));
10873 buildsym_compunit *b = get_builder ();
10874 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10875 {
4c8aa72d
PA
10876 /* NOTE: start_subfile will recognize when it's been
10877 passed a file it has already seen. So we can't
10878 assume there's a simple mapping from
10879 cu->line_header->file_names to subfiles, plus
10880 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10881 b->get_current_subfile ()->symtab
10882 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10883 }
10884
c24bdb02 10885 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10886 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10887 }
10888 }
10889 else
3019eac3 10890 {
c24bdb02 10891 gdb_assert (m_builder == nullptr);
804d2729 10892 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10893 m_builder.reset (new struct buildsym_compunit
10894 (COMPUNIT_OBJFILE (cust), "",
10895 COMPUNIT_DIRNAME (cust),
10896 compunit_language (cust),
10897 0, cust));
f4dc4d17 10898
7ba99d21
AT
10899 auto &file_names = line_header->file_names ();
10900 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10901 {
7ba99d21 10902 file_entry &fe = file_names[i];
4c8aa72d 10903 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10904 }
3019eac3
DE
10905 }
10906
f4dc4d17
DE
10907 /* The main symtab is allocated last. Type units don't have DW_AT_name
10908 so they don't have a "real" (so to speak) symtab anyway.
10909 There is later code that will assign the main symtab to all symbols
10910 that don't have one. We need to handle the case of a symbol with a
10911 missing symtab (DW_AT_decl_file) anyway. */
10912}
3019eac3 10913
f4dc4d17
DE
10914/* Process DW_TAG_type_unit.
10915 For TUs we want to skip the first top level sibling if it's not the
10916 actual type being defined by this TU. In this case the first top
10917 level sibling is there to provide context only. */
3019eac3 10918
f4dc4d17
DE
10919static void
10920read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10921{
10922 struct die_info *child_die;
3019eac3 10923
f4dc4d17
DE
10924 prepare_one_comp_unit (cu, die, language_minimal);
10925
10926 /* Initialize (or reinitialize) the machinery for building symtabs.
10927 We do this before processing child DIEs, so that the line header table
10928 is available for DW_AT_decl_file. */
c24bdb02 10929 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10930
10931 if (die->child != NULL)
10932 {
10933 child_die = die->child;
10934 while (child_die && child_die->tag)
10935 {
10936 process_die (child_die, cu);
10937 child_die = sibling_die (child_die);
10938 }
10939 }
3019eac3
DE
10940}
10941\f
80626a55
DE
10942/* DWO/DWP files.
10943
10944 http://gcc.gnu.org/wiki/DebugFission
10945 http://gcc.gnu.org/wiki/DebugFissionDWP
10946
10947 To simplify handling of both DWO files ("object" files with the DWARF info)
10948 and DWP files (a file with the DWOs packaged up into one file), we treat
10949 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10950
10951static hashval_t
10952hash_dwo_file (const void *item)
10953{
9a3c8263 10954 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10955 hashval_t hash;
3019eac3 10956
a2ce51a0
DE
10957 hash = htab_hash_string (dwo_file->dwo_name);
10958 if (dwo_file->comp_dir != NULL)
10959 hash += htab_hash_string (dwo_file->comp_dir);
10960 return hash;
3019eac3
DE
10961}
10962
10963static int
10964eq_dwo_file (const void *item_lhs, const void *item_rhs)
10965{
9a3c8263
SM
10966 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10967 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10968
a2ce51a0
DE
10969 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10970 return 0;
10971 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10972 return lhs->comp_dir == rhs->comp_dir;
10973 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10974}
10975
10976/* Allocate a hash table for DWO files. */
10977
51ac9db5 10978static htab_up
ed2dc618 10979allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 10980{
51ac9db5
SM
10981 auto delete_dwo_file = [] (void *item)
10982 {
10983 struct dwo_file *dwo_file = (struct dwo_file *) item;
10984
10985 delete dwo_file;
10986 };
10987
bc68fb19
TT
10988 return htab_up (htab_create_alloc (41,
10989 hash_dwo_file,
10990 eq_dwo_file,
10991 delete_dwo_file,
10992 xcalloc, xfree));
3019eac3
DE
10993}
10994
80626a55
DE
10995/* Lookup DWO file DWO_NAME. */
10996
10997static void **
ed2dc618
SM
10998lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10999 const char *dwo_name,
11000 const char *comp_dir)
80626a55
DE
11001{
11002 struct dwo_file find_entry;
11003 void **slot;
11004
11005 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11006 dwarf2_per_objfile->dwo_files
11007 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11008
0ac5b59e
DE
11009 find_entry.dwo_name = dwo_name;
11010 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11011 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11012 INSERT);
80626a55
DE
11013
11014 return slot;
11015}
11016
3019eac3
DE
11017static hashval_t
11018hash_dwo_unit (const void *item)
11019{
9a3c8263 11020 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11021
11022 /* This drops the top 32 bits of the id, but is ok for a hash. */
11023 return dwo_unit->signature;
11024}
11025
11026static int
11027eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11028{
9a3c8263
SM
11029 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11030 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11031
11032 /* The signature is assumed to be unique within the DWO file.
11033 So while object file CU dwo_id's always have the value zero,
11034 that's OK, assuming each object file DWO file has only one CU,
11035 and that's the rule for now. */
11036 return lhs->signature == rhs->signature;
11037}
11038
11039/* Allocate a hash table for DWO CUs,TUs.
11040 There is one of these tables for each of CUs,TUs for each DWO file. */
11041
b0b6a987 11042static htab_up
3019eac3
DE
11043allocate_dwo_unit_table (struct objfile *objfile)
11044{
11045 /* Start out with a pretty small number.
11046 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11047 return htab_up (htab_create_alloc (3,
11048 hash_dwo_unit,
11049 eq_dwo_unit,
11050 NULL, xcalloc, xfree));
3019eac3
DE
11051}
11052
19c3d4c9 11053/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11054
11055static void
19c3d4c9
DE
11056create_dwo_cu_reader (const struct die_reader_specs *reader,
11057 const gdb_byte *info_ptr,
11058 struct die_info *comp_unit_die,
c0ab21c2
TT
11059 struct dwo_file *dwo_file,
11060 struct dwo_unit *dwo_unit)
3019eac3
DE
11061{
11062 struct dwarf2_cu *cu = reader->cu;
9c541725 11063 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11064 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11065
a084a2a6
AT
11066 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11067 if (!signature.has_value ())
3019eac3 11068 {
b98664d3 11069 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11070 " its dwo_id [in module %s]"),
9d8780f0 11071 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11072 return;
11073 }
11074
3019eac3 11075 dwo_unit->dwo_file = dwo_file;
a084a2a6 11076 dwo_unit->signature = *signature;
8a0459fd 11077 dwo_unit->section = section;
9c541725 11078 dwo_unit->sect_off = sect_off;
3019eac3
DE
11079 dwo_unit->length = cu->per_cu->length;
11080
b4f54984 11081 if (dwarf_read_debug)
9d8780f0
SM
11082 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11083 sect_offset_str (sect_off),
9c541725 11084 hex_string (dwo_unit->signature));
3019eac3
DE
11085}
11086
33c5cd75 11087/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11088 Note: This function processes DWO files only, not DWP files. */
3019eac3 11089
33c5cd75 11090static void
ed2dc618 11091create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11092 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11093 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11094{
11095 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11096 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11097
96b79293 11098 section.read (objfile);
33c5cd75 11099 info_ptr = section.buffer;
3019eac3
DE
11100
11101 if (info_ptr == NULL)
33c5cd75 11102 return;
3019eac3 11103
b4f54984 11104 if (dwarf_read_debug)
19c3d4c9
DE
11105 {
11106 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11107 section.get_name (),
11108 section.get_file_name ());
19c3d4c9 11109 }
3019eac3 11110
33c5cd75 11111 end_ptr = info_ptr + section.size;
3019eac3
DE
11112 while (info_ptr < end_ptr)
11113 {
11114 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11115 struct dwo_unit read_unit {};
33c5cd75
DB
11116 struct dwo_unit *dwo_unit;
11117 void **slot;
11118 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11119
11120 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11121 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11122 per_cu.is_debug_types = 0;
33c5cd75
DB
11123 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11124 per_cu.section = &section;
11125
c0ab21c2
TT
11126 cutu_reader reader (&per_cu, cu, &dwo_file);
11127 if (!reader.dummy_p)
11128 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11129 &dwo_file, &read_unit);
33c5cd75
DB
11130 info_ptr += per_cu.length;
11131
11132 // If the unit could not be parsed, skip it.
c0ab21c2 11133 if (read_unit.dwo_file == NULL)
33c5cd75 11134 continue;
3019eac3 11135
33c5cd75
DB
11136 if (cus_htab == NULL)
11137 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11138
33c5cd75 11139 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11140 *dwo_unit = read_unit;
b0b6a987 11141 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11142 gdb_assert (slot != NULL);
11143 if (*slot != NULL)
19c3d4c9 11144 {
33c5cd75
DB
11145 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11146 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11147
b98664d3 11148 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11149 " the entry at offset %s, signature %s"),
11150 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11151 hex_string (dwo_unit->signature));
19c3d4c9 11152 }
33c5cd75 11153 *slot = (void *)dwo_unit;
3019eac3 11154 }
3019eac3
DE
11155}
11156
80626a55
DE
11157/* DWP file .debug_{cu,tu}_index section format:
11158 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11159
d2415c6c
DE
11160 DWP Version 1:
11161
80626a55
DE
11162 Both index sections have the same format, and serve to map a 64-bit
11163 signature to a set of section numbers. Each section begins with a header,
11164 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11165 indexes, and a pool of 32-bit section numbers. The index sections will be
11166 aligned at 8-byte boundaries in the file.
11167
d2415c6c
DE
11168 The index section header consists of:
11169
11170 V, 32 bit version number
11171 -, 32 bits unused
11172 N, 32 bit number of compilation units or type units in the index
11173 M, 32 bit number of slots in the hash table
80626a55 11174
d2415c6c 11175 Numbers are recorded using the byte order of the application binary.
80626a55 11176
d2415c6c
DE
11177 The hash table begins at offset 16 in the section, and consists of an array
11178 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11179 order of the application binary). Unused slots in the hash table are 0.
11180 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11181
d2415c6c
DE
11182 The parallel table begins immediately after the hash table
11183 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11184 array of 32-bit indexes (using the byte order of the application binary),
11185 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11186 table contains a 32-bit index into the pool of section numbers. For unused
11187 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11188
73869dc2
DE
11189 The pool of section numbers begins immediately following the hash table
11190 (at offset 16 + 12 * M from the beginning of the section). The pool of
11191 section numbers consists of an array of 32-bit words (using the byte order
11192 of the application binary). Each item in the array is indexed starting
11193 from 0. The hash table entry provides the index of the first section
11194 number in the set. Additional section numbers in the set follow, and the
11195 set is terminated by a 0 entry (section number 0 is not used in ELF).
11196
11197 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11198 section must be the first entry in the set, and the .debug_abbrev.dwo must
11199 be the second entry. Other members of the set may follow in any order.
11200
11201 ---
11202
11203 DWP Version 2:
11204
11205 DWP Version 2 combines all the .debug_info, etc. sections into one,
11206 and the entries in the index tables are now offsets into these sections.
11207 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11208 section.
11209
11210 Index Section Contents:
11211 Header
11212 Hash Table of Signatures dwp_hash_table.hash_table
11213 Parallel Table of Indices dwp_hash_table.unit_table
11214 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11215 Table of Section Sizes dwp_hash_table.v2.sizes
11216
11217 The index section header consists of:
11218
11219 V, 32 bit version number
11220 L, 32 bit number of columns in the table of section offsets
11221 N, 32 bit number of compilation units or type units in the index
11222 M, 32 bit number of slots in the hash table
11223
11224 Numbers are recorded using the byte order of the application binary.
11225
11226 The hash table has the same format as version 1.
11227 The parallel table of indices has the same format as version 1,
11228 except that the entries are origin-1 indices into the table of sections
11229 offsets and the table of section sizes.
11230
11231 The table of offsets begins immediately following the parallel table
11232 (at offset 16 + 12 * M from the beginning of the section). The table is
11233 a two-dimensional array of 32-bit words (using the byte order of the
11234 application binary), with L columns and N+1 rows, in row-major order.
11235 Each row in the array is indexed starting from 0. The first row provides
11236 a key to the remaining rows: each column in this row provides an identifier
11237 for a debug section, and the offsets in the same column of subsequent rows
11238 refer to that section. The section identifiers are:
11239
11240 DW_SECT_INFO 1 .debug_info.dwo
11241 DW_SECT_TYPES 2 .debug_types.dwo
11242 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11243 DW_SECT_LINE 4 .debug_line.dwo
11244 DW_SECT_LOC 5 .debug_loc.dwo
11245 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11246 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11247 DW_SECT_MACRO 8 .debug_macro.dwo
11248
11249 The offsets provided by the CU and TU index sections are the base offsets
11250 for the contributions made by each CU or TU to the corresponding section
11251 in the package file. Each CU and TU header contains an abbrev_offset
11252 field, used to find the abbreviations table for that CU or TU within the
11253 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11254 be interpreted as relative to the base offset given in the index section.
11255 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11256 should be interpreted as relative to the base offset for .debug_line.dwo,
11257 and offsets into other debug sections obtained from DWARF attributes should
11258 also be interpreted as relative to the corresponding base offset.
11259
11260 The table of sizes begins immediately following the table of offsets.
11261 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11262 with L columns and N rows, in row-major order. Each row in the array is
11263 indexed starting from 1 (row 0 is shared by the two tables).
11264
11265 ---
11266
11267 Hash table lookup is handled the same in version 1 and 2:
11268
11269 We assume that N and M will not exceed 2^32 - 1.
11270 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11271
d2415c6c
DE
11272 Given a 64-bit compilation unit signature or a type signature S, an entry
11273 in the hash table is located as follows:
80626a55 11274
d2415c6c
DE
11275 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11276 the low-order k bits all set to 1.
80626a55 11277
d2415c6c 11278 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11279
d2415c6c
DE
11280 3) If the hash table entry at index H matches the signature, use that
11281 entry. If the hash table entry at index H is unused (all zeroes),
11282 terminate the search: the signature is not present in the table.
80626a55 11283
d2415c6c 11284 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11285
d2415c6c 11286 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11287 to stop at an unused slot or find the match. */
80626a55
DE
11288
11289/* Create a hash table to map DWO IDs to their CU/TU entry in
11290 .debug_{info,types}.dwo in DWP_FILE.
11291 Returns NULL if there isn't one.
11292 Note: This function processes DWP files only, not DWO files. */
11293
11294static struct dwp_hash_table *
ed2dc618
SM
11295create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11296 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11297{
11298 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11299 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11300 const gdb_byte *index_ptr, *index_end;
80626a55 11301 struct dwarf2_section_info *index;
73869dc2 11302 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11303 struct dwp_hash_table *htab;
11304
11305 if (is_debug_types)
11306 index = &dwp_file->sections.tu_index;
11307 else
11308 index = &dwp_file->sections.cu_index;
11309
96b79293 11310 if (index->empty ())
80626a55 11311 return NULL;
96b79293 11312 index->read (objfile);
80626a55
DE
11313
11314 index_ptr = index->buffer;
11315 index_end = index_ptr + index->size;
11316
11317 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11318 index_ptr += 4;
11319 if (version == 2)
11320 nr_columns = read_4_bytes (dbfd, index_ptr);
11321 else
11322 nr_columns = 0;
11323 index_ptr += 4;
80626a55
DE
11324 nr_units = read_4_bytes (dbfd, index_ptr);
11325 index_ptr += 4;
11326 nr_slots = read_4_bytes (dbfd, index_ptr);
11327 index_ptr += 4;
11328
73869dc2 11329 if (version != 1 && version != 2)
80626a55 11330 {
21aa081e 11331 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11332 " [in module %s]"),
21aa081e 11333 pulongest (version), dwp_file->name);
80626a55
DE
11334 }
11335 if (nr_slots != (nr_slots & -nr_slots))
11336 {
21aa081e 11337 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11338 " is not power of 2 [in module %s]"),
21aa081e 11339 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11340 }
11341
11342 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11343 htab->version = version;
11344 htab->nr_columns = nr_columns;
80626a55
DE
11345 htab->nr_units = nr_units;
11346 htab->nr_slots = nr_slots;
11347 htab->hash_table = index_ptr;
11348 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11349
11350 /* Exit early if the table is empty. */
11351 if (nr_slots == 0 || nr_units == 0
11352 || (version == 2 && nr_columns == 0))
11353 {
11354 /* All must be zero. */
11355 if (nr_slots != 0 || nr_units != 0
11356 || (version == 2 && nr_columns != 0))
11357 {
b98664d3 11358 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11359 " all zero [in modules %s]"),
11360 dwp_file->name);
11361 }
11362 return htab;
11363 }
11364
11365 if (version == 1)
11366 {
11367 htab->section_pool.v1.indices =
11368 htab->unit_table + sizeof (uint32_t) * nr_slots;
11369 /* It's harder to decide whether the section is too small in v1.
11370 V1 is deprecated anyway so we punt. */
11371 }
11372 else
11373 {
11374 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11375 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11376 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11377 /* Reverse map for error checking. */
11378 int ids_seen[DW_SECT_MAX + 1];
11379 int i;
11380
11381 if (nr_columns < 2)
11382 {
11383 error (_("Dwarf Error: bad DWP hash table, too few columns"
11384 " in section table [in module %s]"),
11385 dwp_file->name);
11386 }
11387 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11388 {
11389 error (_("Dwarf Error: bad DWP hash table, too many columns"
11390 " in section table [in module %s]"),
11391 dwp_file->name);
11392 }
04fd5eed
GB
11393 memset (ids, 255, sizeof_ids);
11394 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11395 for (i = 0; i < nr_columns; ++i)
11396 {
11397 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11398
11399 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11400 {
11401 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11402 " in section table [in module %s]"),
11403 id, dwp_file->name);
11404 }
11405 if (ids_seen[id] != -1)
11406 {
11407 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11408 " id %d in section table [in module %s]"),
11409 id, dwp_file->name);
11410 }
11411 ids_seen[id] = i;
11412 ids[i] = id;
11413 }
11414 /* Must have exactly one info or types section. */
11415 if (((ids_seen[DW_SECT_INFO] != -1)
11416 + (ids_seen[DW_SECT_TYPES] != -1))
11417 != 1)
11418 {
11419 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11420 " DWO info/types section [in module %s]"),
11421 dwp_file->name);
11422 }
11423 /* Must have an abbrev section. */
11424 if (ids_seen[DW_SECT_ABBREV] == -1)
11425 {
11426 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11427 " section [in module %s]"),
11428 dwp_file->name);
11429 }
11430 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11431 htab->section_pool.v2.sizes =
11432 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11433 * nr_units * nr_columns);
11434 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11435 * nr_units * nr_columns))
11436 > index_end)
11437 {
11438 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11439 " [in module %s]"),
11440 dwp_file->name);
11441 }
11442 }
80626a55
DE
11443
11444 return htab;
11445}
11446
11447/* Update SECTIONS with the data from SECTP.
11448
11449 This function is like the other "locate" section routines that are
11450 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11451 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11452
11453 The result is non-zero for success, or zero if an error was found. */
11454
11455static int
73869dc2
DE
11456locate_v1_virtual_dwo_sections (asection *sectp,
11457 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11458{
11459 const struct dwop_section_names *names = &dwop_section_names;
11460
11461 if (section_is_p (sectp->name, &names->abbrev_dwo))
11462 {
11463 /* There can be only one. */
049412e3 11464 if (sections->abbrev.s.section != NULL)
80626a55 11465 return 0;
049412e3 11466 sections->abbrev.s.section = sectp;
fd361982 11467 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11468 }
11469 else if (section_is_p (sectp->name, &names->info_dwo)
11470 || section_is_p (sectp->name, &names->types_dwo))
11471 {
11472 /* There can be only one. */
049412e3 11473 if (sections->info_or_types.s.section != NULL)
80626a55 11474 return 0;
049412e3 11475 sections->info_or_types.s.section = sectp;
fd361982 11476 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11477 }
11478 else if (section_is_p (sectp->name, &names->line_dwo))
11479 {
11480 /* There can be only one. */
049412e3 11481 if (sections->line.s.section != NULL)
80626a55 11482 return 0;
049412e3 11483 sections->line.s.section = sectp;
fd361982 11484 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11485 }
11486 else if (section_is_p (sectp->name, &names->loc_dwo))
11487 {
11488 /* There can be only one. */
049412e3 11489 if (sections->loc.s.section != NULL)
80626a55 11490 return 0;
049412e3 11491 sections->loc.s.section = sectp;
fd361982 11492 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11493 }
11494 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11495 {
11496 /* There can be only one. */
049412e3 11497 if (sections->macinfo.s.section != NULL)
80626a55 11498 return 0;
049412e3 11499 sections->macinfo.s.section = sectp;
fd361982 11500 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11501 }
11502 else if (section_is_p (sectp->name, &names->macro_dwo))
11503 {
11504 /* There can be only one. */
049412e3 11505 if (sections->macro.s.section != NULL)
80626a55 11506 return 0;
049412e3 11507 sections->macro.s.section = sectp;
fd361982 11508 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11509 }
11510 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11511 {
11512 /* There can be only one. */
049412e3 11513 if (sections->str_offsets.s.section != NULL)
80626a55 11514 return 0;
049412e3 11515 sections->str_offsets.s.section = sectp;
fd361982 11516 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11517 }
11518 else
11519 {
11520 /* No other kind of section is valid. */
11521 return 0;
11522 }
11523
11524 return 1;
11525}
11526
73869dc2
DE
11527/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11528 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11529 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11530 This is for DWP version 1 files. */
80626a55
DE
11531
11532static struct dwo_unit *
ed2dc618
SM
11533create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11534 struct dwp_file *dwp_file,
73869dc2
DE
11535 uint32_t unit_index,
11536 const char *comp_dir,
11537 ULONGEST signature, int is_debug_types)
80626a55
DE
11538{
11539 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11540 const struct dwp_hash_table *dwp_htab =
11541 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11542 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11543 const char *kind = is_debug_types ? "TU" : "CU";
11544 struct dwo_file *dwo_file;
11545 struct dwo_unit *dwo_unit;
73869dc2 11546 struct virtual_v1_dwo_sections sections;
80626a55 11547 void **dwo_file_slot;
80626a55
DE
11548 int i;
11549
73869dc2
DE
11550 gdb_assert (dwp_file->version == 1);
11551
b4f54984 11552 if (dwarf_read_debug)
80626a55 11553 {
73869dc2 11554 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11555 kind,
73869dc2 11556 pulongest (unit_index), hex_string (signature),
80626a55
DE
11557 dwp_file->name);
11558 }
11559
19ac8c2e 11560 /* Fetch the sections of this DWO unit.
80626a55
DE
11561 Put a limit on the number of sections we look for so that bad data
11562 doesn't cause us to loop forever. */
11563
73869dc2 11564#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11565 (1 /* .debug_info or .debug_types */ \
11566 + 1 /* .debug_abbrev */ \
11567 + 1 /* .debug_line */ \
11568 + 1 /* .debug_loc */ \
11569 + 1 /* .debug_str_offsets */ \
19ac8c2e 11570 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11571 + 1 /* trailing zero */)
11572
11573 memset (&sections, 0, sizeof (sections));
80626a55 11574
73869dc2 11575 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11576 {
11577 asection *sectp;
11578 uint32_t section_nr =
11579 read_4_bytes (dbfd,
73869dc2
DE
11580 dwp_htab->section_pool.v1.indices
11581 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11582
11583 if (section_nr == 0)
11584 break;
11585 if (section_nr >= dwp_file->num_sections)
11586 {
11587 error (_("Dwarf Error: bad DWP hash table, section number too large"
11588 " [in module %s]"),
11589 dwp_file->name);
11590 }
11591
11592 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11593 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11594 {
11595 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11596 " [in module %s]"),
11597 dwp_file->name);
11598 }
11599 }
11600
11601 if (i < 2
96b79293
TT
11602 || sections.info_or_types.empty ()
11603 || sections.abbrev.empty ())
80626a55
DE
11604 {
11605 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11606 " [in module %s]"),
11607 dwp_file->name);
11608 }
73869dc2 11609 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11610 {
11611 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11612 " [in module %s]"),
11613 dwp_file->name);
11614 }
11615
11616 /* It's easier for the rest of the code if we fake a struct dwo_file and
11617 have dwo_unit "live" in that. At least for now.
11618
11619 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11620 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11621 file, we can combine them back into a virtual DWO file to save space
11622 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11623 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11624
791afaa2
TT
11625 std::string virtual_dwo_name =
11626 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11627 sections.abbrev.get_id (),
11628 sections.line.get_id (),
11629 sections.loc.get_id (),
11630 sections.str_offsets.get_id ());
80626a55 11631 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11632 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11633 virtual_dwo_name.c_str (),
11634 comp_dir);
80626a55
DE
11635 /* Create one if necessary. */
11636 if (*dwo_file_slot == NULL)
11637 {
b4f54984 11638 if (dwarf_read_debug)
80626a55
DE
11639 {
11640 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11641 virtual_dwo_name.c_str ());
80626a55 11642 }
51ac9db5 11643 dwo_file = new struct dwo_file;
efba19b0
TT
11644 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11645 virtual_dwo_name);
0ac5b59e 11646 dwo_file->comp_dir = comp_dir;
80626a55
DE
11647 dwo_file->sections.abbrev = sections.abbrev;
11648 dwo_file->sections.line = sections.line;
11649 dwo_file->sections.loc = sections.loc;
11650 dwo_file->sections.macinfo = sections.macinfo;
11651 dwo_file->sections.macro = sections.macro;
11652 dwo_file->sections.str_offsets = sections.str_offsets;
11653 /* The "str" section is global to the entire DWP file. */
11654 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11655 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11656 there's no need to record it in dwo_file.
11657 Also, we can't simply record type sections in dwo_file because
11658 we record a pointer into the vector in dwo_unit. As we collect more
11659 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11660 for it, invalidating all copies of pointers into the previous
11661 contents. */
80626a55
DE
11662 *dwo_file_slot = dwo_file;
11663 }
11664 else
11665 {
b4f54984 11666 if (dwarf_read_debug)
80626a55
DE
11667 {
11668 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11669 virtual_dwo_name.c_str ());
80626a55 11670 }
9a3c8263 11671 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11672 }
80626a55
DE
11673
11674 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11675 dwo_unit->dwo_file = dwo_file;
11676 dwo_unit->signature = signature;
8d749320
SM
11677 dwo_unit->section =
11678 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11679 *dwo_unit->section = sections.info_or_types;
57d63ce2 11680 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11681
11682 return dwo_unit;
11683}
11684
73869dc2
DE
11685/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11686 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11687 piece within that section used by a TU/CU, return a virtual section
11688 of just that piece. */
11689
11690static struct dwarf2_section_info
ed2dc618
SM
11691create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11692 struct dwarf2_section_info *section,
73869dc2
DE
11693 bfd_size_type offset, bfd_size_type size)
11694{
11695 struct dwarf2_section_info result;
11696 asection *sectp;
11697
11698 gdb_assert (section != NULL);
11699 gdb_assert (!section->is_virtual);
11700
11701 memset (&result, 0, sizeof (result));
11702 result.s.containing_section = section;
dc4ccb6f 11703 result.is_virtual = true;
73869dc2
DE
11704
11705 if (size == 0)
11706 return result;
11707
96b79293 11708 sectp = section->get_bfd_section ();
73869dc2
DE
11709
11710 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11711 bounds of the real section. This is a pretty-rare event, so just
11712 flag an error (easier) instead of a warning and trying to cope. */
11713 if (sectp == NULL
fd361982 11714 || offset + size > bfd_section_size (sectp))
73869dc2 11715 {
73869dc2
DE
11716 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11717 " in section %s [in module %s]"),
fd361982 11718 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11719 objfile_name (dwarf2_per_objfile->objfile));
11720 }
11721
11722 result.virtual_offset = offset;
11723 result.size = size;
11724 return result;
11725}
11726
11727/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11728 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11729 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11730 This is for DWP version 2 files. */
11731
11732static struct dwo_unit *
ed2dc618
SM
11733create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11734 struct dwp_file *dwp_file,
73869dc2
DE
11735 uint32_t unit_index,
11736 const char *comp_dir,
11737 ULONGEST signature, int is_debug_types)
11738{
11739 struct objfile *objfile = dwarf2_per_objfile->objfile;
11740 const struct dwp_hash_table *dwp_htab =
11741 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11742 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11743 const char *kind = is_debug_types ? "TU" : "CU";
11744 struct dwo_file *dwo_file;
11745 struct dwo_unit *dwo_unit;
11746 struct virtual_v2_dwo_sections sections;
11747 void **dwo_file_slot;
73869dc2
DE
11748 int i;
11749
11750 gdb_assert (dwp_file->version == 2);
11751
b4f54984 11752 if (dwarf_read_debug)
73869dc2
DE
11753 {
11754 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11755 kind,
11756 pulongest (unit_index), hex_string (signature),
11757 dwp_file->name);
11758 }
11759
11760 /* Fetch the section offsets of this DWO unit. */
11761
11762 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11763
11764 for (i = 0; i < dwp_htab->nr_columns; ++i)
11765 {
11766 uint32_t offset = read_4_bytes (dbfd,
11767 dwp_htab->section_pool.v2.offsets
11768 + (((unit_index - 1) * dwp_htab->nr_columns
11769 + i)
11770 * sizeof (uint32_t)));
11771 uint32_t size = read_4_bytes (dbfd,
11772 dwp_htab->section_pool.v2.sizes
11773 + (((unit_index - 1) * dwp_htab->nr_columns
11774 + i)
11775 * sizeof (uint32_t)));
11776
11777 switch (dwp_htab->section_pool.v2.section_ids[i])
11778 {
11779 case DW_SECT_INFO:
11780 case DW_SECT_TYPES:
11781 sections.info_or_types_offset = offset;
11782 sections.info_or_types_size = size;
11783 break;
11784 case DW_SECT_ABBREV:
11785 sections.abbrev_offset = offset;
11786 sections.abbrev_size = size;
11787 break;
11788 case DW_SECT_LINE:
11789 sections.line_offset = offset;
11790 sections.line_size = size;
11791 break;
11792 case DW_SECT_LOC:
11793 sections.loc_offset = offset;
11794 sections.loc_size = size;
11795 break;
11796 case DW_SECT_STR_OFFSETS:
11797 sections.str_offsets_offset = offset;
11798 sections.str_offsets_size = size;
11799 break;
11800 case DW_SECT_MACINFO:
11801 sections.macinfo_offset = offset;
11802 sections.macinfo_size = size;
11803 break;
11804 case DW_SECT_MACRO:
11805 sections.macro_offset = offset;
11806 sections.macro_size = size;
11807 break;
11808 }
11809 }
11810
11811 /* It's easier for the rest of the code if we fake a struct dwo_file and
11812 have dwo_unit "live" in that. At least for now.
11813
11814 The DWP file can be made up of a random collection of CUs and TUs.
11815 However, for each CU + set of TUs that came from the same original DWO
11816 file, we can combine them back into a virtual DWO file to save space
11817 (fewer struct dwo_file objects to allocate). Remember that for really
11818 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11819
791afaa2
TT
11820 std::string virtual_dwo_name =
11821 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11822 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11823 (long) (sections.line_size ? sections.line_offset : 0),
11824 (long) (sections.loc_size ? sections.loc_offset : 0),
11825 (long) (sections.str_offsets_size
11826 ? sections.str_offsets_offset : 0));
73869dc2 11827 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11828 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11829 virtual_dwo_name.c_str (),
11830 comp_dir);
73869dc2
DE
11831 /* Create one if necessary. */
11832 if (*dwo_file_slot == NULL)
11833 {
b4f54984 11834 if (dwarf_read_debug)
73869dc2
DE
11835 {
11836 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11837 virtual_dwo_name.c_str ());
73869dc2 11838 }
51ac9db5 11839 dwo_file = new struct dwo_file;
efba19b0
TT
11840 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11841 virtual_dwo_name);
73869dc2
DE
11842 dwo_file->comp_dir = comp_dir;
11843 dwo_file->sections.abbrev =
ed2dc618 11844 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11845 sections.abbrev_offset, sections.abbrev_size);
11846 dwo_file->sections.line =
ed2dc618 11847 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11848 sections.line_offset, sections.line_size);
11849 dwo_file->sections.loc =
ed2dc618 11850 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11851 sections.loc_offset, sections.loc_size);
11852 dwo_file->sections.macinfo =
ed2dc618 11853 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11854 sections.macinfo_offset, sections.macinfo_size);
11855 dwo_file->sections.macro =
ed2dc618 11856 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11857 sections.macro_offset, sections.macro_size);
11858 dwo_file->sections.str_offsets =
ed2dc618
SM
11859 create_dwp_v2_section (dwarf2_per_objfile,
11860 &dwp_file->sections.str_offsets,
73869dc2
DE
11861 sections.str_offsets_offset,
11862 sections.str_offsets_size);
11863 /* The "str" section is global to the entire DWP file. */
11864 dwo_file->sections.str = dwp_file->sections.str;
11865 /* The info or types section is assigned below to dwo_unit,
11866 there's no need to record it in dwo_file.
11867 Also, we can't simply record type sections in dwo_file because
11868 we record a pointer into the vector in dwo_unit. As we collect more
11869 types we'll grow the vector and eventually have to reallocate space
11870 for it, invalidating all copies of pointers into the previous
11871 contents. */
11872 *dwo_file_slot = dwo_file;
11873 }
11874 else
11875 {
b4f54984 11876 if (dwarf_read_debug)
73869dc2
DE
11877 {
11878 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11879 virtual_dwo_name.c_str ());
73869dc2 11880 }
9a3c8263 11881 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11882 }
73869dc2
DE
11883
11884 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11885 dwo_unit->dwo_file = dwo_file;
11886 dwo_unit->signature = signature;
8d749320
SM
11887 dwo_unit->section =
11888 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11889 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11890 is_debug_types
73869dc2
DE
11891 ? &dwp_file->sections.types
11892 : &dwp_file->sections.info,
11893 sections.info_or_types_offset,
11894 sections.info_or_types_size);
11895 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11896
11897 return dwo_unit;
11898}
11899
57d63ce2
DE
11900/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11901 Returns NULL if the signature isn't found. */
80626a55
DE
11902
11903static struct dwo_unit *
ed2dc618
SM
11904lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11905 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11906 ULONGEST signature, int is_debug_types)
80626a55 11907{
57d63ce2
DE
11908 const struct dwp_hash_table *dwp_htab =
11909 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11910 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11911 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11912 uint32_t hash = signature & mask;
11913 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11914 unsigned int i;
11915 void **slot;
870f88f7 11916 struct dwo_unit find_dwo_cu;
80626a55
DE
11917
11918 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11919 find_dwo_cu.signature = signature;
19ac8c2e 11920 slot = htab_find_slot (is_debug_types
48b490f2
TT
11921 ? dwp_file->loaded_tus.get ()
11922 : dwp_file->loaded_cus.get (),
19ac8c2e 11923 &find_dwo_cu, INSERT);
80626a55
DE
11924
11925 if (*slot != NULL)
9a3c8263 11926 return (struct dwo_unit *) *slot;
80626a55
DE
11927
11928 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11929 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11930 {
11931 ULONGEST signature_in_table;
11932
11933 signature_in_table =
57d63ce2 11934 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11935 if (signature_in_table == signature)
11936 {
57d63ce2
DE
11937 uint32_t unit_index =
11938 read_4_bytes (dbfd,
11939 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11940
73869dc2
DE
11941 if (dwp_file->version == 1)
11942 {
ed2dc618
SM
11943 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11944 dwp_file, unit_index,
73869dc2
DE
11945 comp_dir, signature,
11946 is_debug_types);
11947 }
11948 else
11949 {
ed2dc618
SM
11950 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11951 dwp_file, unit_index,
73869dc2
DE
11952 comp_dir, signature,
11953 is_debug_types);
11954 }
9a3c8263 11955 return (struct dwo_unit *) *slot;
80626a55
DE
11956 }
11957 if (signature_in_table == 0)
11958 return NULL;
11959 hash = (hash + hash2) & mask;
11960 }
11961
11962 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11963 " [in module %s]"),
11964 dwp_file->name);
11965}
11966
ab5088bf 11967/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11968 Open the file specified by FILE_NAME and hand it off to BFD for
11969 preliminary analysis. Return a newly initialized bfd *, which
11970 includes a canonicalized copy of FILE_NAME.
80626a55 11971 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11972 SEARCH_CWD is true if the current directory is to be searched.
11973 It will be searched before debug-file-directory.
13aaf454
DE
11974 If successful, the file is added to the bfd include table of the
11975 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11976 If unable to find/open the file, return NULL.
3019eac3
DE
11977 NOTE: This function is derived from symfile_bfd_open. */
11978
192b62ce 11979static gdb_bfd_ref_ptr
ed2dc618
SM
11980try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11981 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11982{
24b9144d 11983 int desc;
9c02c129
DE
11984 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11985 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11986 to debug_file_directory. */
e0cc99a6 11987 const char *search_path;
9c02c129
DE
11988 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11989
e0cc99a6 11990 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
11991 if (search_cwd)
11992 {
11993 if (*debug_file_directory != '\0')
e0cc99a6
TT
11994 {
11995 search_path_holder.reset (concat (".", dirname_separator_string,
11996 debug_file_directory,
11997 (char *) NULL));
11998 search_path = search_path_holder.get ();
11999 }
6ac97d4c 12000 else
e0cc99a6 12001 search_path = ".";
6ac97d4c 12002 }
9c02c129 12003 else
e0cc99a6 12004 search_path = debug_file_directory;
3019eac3 12005
24b9144d 12006 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12007 if (is_dwp)
12008 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12009
12010 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12011 desc = openp (search_path, flags, file_name,
3019eac3
DE
12012 O_RDONLY | O_BINARY, &absolute_name);
12013 if (desc < 0)
12014 return NULL;
12015
e0cc99a6
TT
12016 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12017 gnutarget, desc));
9c02c129
DE
12018 if (sym_bfd == NULL)
12019 return NULL;
192b62ce 12020 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12021
192b62ce
TT
12022 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12023 return NULL;
3019eac3 12024
13aaf454
DE
12025 /* Success. Record the bfd as having been included by the objfile's bfd.
12026 This is important because things like demangled_names_hash lives in the
12027 objfile's per_bfd space and may have references to things like symbol
12028 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12029 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12030
3019eac3
DE
12031 return sym_bfd;
12032}
12033
ab5088bf 12034/* Try to open DWO file FILE_NAME.
3019eac3
DE
12035 COMP_DIR is the DW_AT_comp_dir attribute.
12036 The result is the bfd handle of the file.
12037 If there is a problem finding or opening the file, return NULL.
12038 Upon success, the canonicalized path of the file is stored in the bfd,
12039 same as symfile_bfd_open. */
12040
192b62ce 12041static gdb_bfd_ref_ptr
ed2dc618
SM
12042open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12043 const char *file_name, const char *comp_dir)
3019eac3 12044{
80626a55 12045 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12046 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12047 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12048
12049 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12050
12051 if (comp_dir != NULL)
12052 {
43816ebc
TT
12053 gdb::unique_xmalloc_ptr<char> path_to_try
12054 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12055
12056 /* NOTE: If comp_dir is a relative path, this will also try the
12057 search path, which seems useful. */
ed2dc618 12058 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12059 path_to_try.get (),
ed2dc618 12060 0 /*is_dwp*/,
192b62ce 12061 1 /*search_cwd*/));
3019eac3
DE
12062 if (abfd != NULL)
12063 return abfd;
12064 }
12065
12066 /* That didn't work, try debug-file-directory, which, despite its name,
12067 is a list of paths. */
12068
12069 if (*debug_file_directory == '\0')
12070 return NULL;
12071
ed2dc618
SM
12072 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12073 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12074}
12075
80626a55
DE
12076/* This function is mapped across the sections and remembers the offset and
12077 size of each of the DWO debugging sections we are interested in. */
12078
12079static void
12080dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12081{
9a3c8263 12082 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12083 const struct dwop_section_names *names = &dwop_section_names;
12084
12085 if (section_is_p (sectp->name, &names->abbrev_dwo))
12086 {
049412e3 12087 dwo_sections->abbrev.s.section = sectp;
fd361982 12088 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12089 }
12090 else if (section_is_p (sectp->name, &names->info_dwo))
12091 {
049412e3 12092 dwo_sections->info.s.section = sectp;
fd361982 12093 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12094 }
12095 else if (section_is_p (sectp->name, &names->line_dwo))
12096 {
049412e3 12097 dwo_sections->line.s.section = sectp;
fd361982 12098 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12099 }
12100 else if (section_is_p (sectp->name, &names->loc_dwo))
12101 {
049412e3 12102 dwo_sections->loc.s.section = sectp;
fd361982 12103 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12104 }
12105 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12106 {
049412e3 12107 dwo_sections->macinfo.s.section = sectp;
fd361982 12108 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12109 }
12110 else if (section_is_p (sectp->name, &names->macro_dwo))
12111 {
049412e3 12112 dwo_sections->macro.s.section = sectp;
fd361982 12113 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12114 }
12115 else if (section_is_p (sectp->name, &names->str_dwo))
12116 {
049412e3 12117 dwo_sections->str.s.section = sectp;
fd361982 12118 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12119 }
12120 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12121 {
049412e3 12122 dwo_sections->str_offsets.s.section = sectp;
fd361982 12123 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12124 }
12125 else if (section_is_p (sectp->name, &names->types_dwo))
12126 {
12127 struct dwarf2_section_info type_section;
12128
12129 memset (&type_section, 0, sizeof (type_section));
049412e3 12130 type_section.s.section = sectp;
fd361982 12131 type_section.size = bfd_section_size (sectp);
fd5866f6 12132 dwo_sections->types.push_back (type_section);
80626a55
DE
12133 }
12134}
12135
ab5088bf 12136/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12137 by PER_CU. This is for the non-DWP case.
80626a55 12138 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12139
12140static struct dwo_file *
0ac5b59e
DE
12141open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12142 const char *dwo_name, const char *comp_dir)
3019eac3 12143{
ed2dc618 12144 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12145
fb1eb2f9 12146 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12147 if (dbfd == NULL)
12148 {
b4f54984 12149 if (dwarf_read_debug)
80626a55
DE
12150 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12151 return NULL;
12152 }
263db9a1 12153
51ac9db5 12154 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12155 dwo_file->dwo_name = dwo_name;
12156 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12157 dwo_file->dbfd = std::move (dbfd);
3019eac3 12158
fb1eb2f9 12159 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12160 &dwo_file->sections);
3019eac3 12161
18a8505e
AT
12162 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12163 dwo_file->sections.info, dwo_file->cus);
3019eac3 12164
263db9a1 12165 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12166 dwo_file->sections.types, dwo_file->tus);
3019eac3 12167
b4f54984 12168 if (dwarf_read_debug)
80626a55
DE
12169 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12170
263db9a1 12171 return dwo_file.release ();
3019eac3
DE
12172}
12173
80626a55 12174/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12175 size of each of the DWP debugging sections common to version 1 and 2 that
12176 we are interested in. */
3019eac3 12177
80626a55 12178static void
73869dc2
DE
12179dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12180 void *dwp_file_ptr)
3019eac3 12181{
9a3c8263 12182 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12183 const struct dwop_section_names *names = &dwop_section_names;
12184 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12185
80626a55 12186 /* Record the ELF section number for later lookup: this is what the
73869dc2 12187 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12188 gdb_assert (elf_section_nr < dwp_file->num_sections);
12189 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12190
80626a55
DE
12191 /* Look for specific sections that we need. */
12192 if (section_is_p (sectp->name, &names->str_dwo))
12193 {
049412e3 12194 dwp_file->sections.str.s.section = sectp;
fd361982 12195 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12196 }
12197 else if (section_is_p (sectp->name, &names->cu_index))
12198 {
049412e3 12199 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12200 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12201 }
12202 else if (section_is_p (sectp->name, &names->tu_index))
12203 {
049412e3 12204 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12205 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12206 }
12207}
3019eac3 12208
73869dc2
DE
12209/* This function is mapped across the sections and remembers the offset and
12210 size of each of the DWP version 2 debugging sections that we are interested
12211 in. This is split into a separate function because we don't know if we
12212 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12213
12214static void
12215dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12216{
9a3c8263 12217 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12218 const struct dwop_section_names *names = &dwop_section_names;
12219 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12220
12221 /* Record the ELF section number for later lookup: this is what the
12222 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12223 gdb_assert (elf_section_nr < dwp_file->num_sections);
12224 dwp_file->elf_sections[elf_section_nr] = sectp;
12225
12226 /* Look for specific sections that we need. */
12227 if (section_is_p (sectp->name, &names->abbrev_dwo))
12228 {
049412e3 12229 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12230 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12231 }
12232 else if (section_is_p (sectp->name, &names->info_dwo))
12233 {
049412e3 12234 dwp_file->sections.info.s.section = sectp;
fd361982 12235 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12236 }
12237 else if (section_is_p (sectp->name, &names->line_dwo))
12238 {
049412e3 12239 dwp_file->sections.line.s.section = sectp;
fd361982 12240 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12241 }
12242 else if (section_is_p (sectp->name, &names->loc_dwo))
12243 {
049412e3 12244 dwp_file->sections.loc.s.section = sectp;
fd361982 12245 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12246 }
12247 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12248 {
049412e3 12249 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12250 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12251 }
12252 else if (section_is_p (sectp->name, &names->macro_dwo))
12253 {
049412e3 12254 dwp_file->sections.macro.s.section = sectp;
fd361982 12255 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12256 }
12257 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12258 {
049412e3 12259 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12260 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12261 }
12262 else if (section_is_p (sectp->name, &names->types_dwo))
12263 {
049412e3 12264 dwp_file->sections.types.s.section = sectp;
fd361982 12265 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12266 }
12267}
12268
80626a55 12269/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12270
80626a55
DE
12271static hashval_t
12272hash_dwp_loaded_cutus (const void *item)
12273{
9a3c8263 12274 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12275
80626a55
DE
12276 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12277 return dwo_unit->signature;
3019eac3
DE
12278}
12279
80626a55 12280/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12281
80626a55
DE
12282static int
12283eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12284{
9a3c8263
SM
12285 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12286 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12287
80626a55
DE
12288 return dua->signature == dub->signature;
12289}
3019eac3 12290
80626a55 12291/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12292
48b490f2 12293static htab_up
80626a55
DE
12294allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12295{
48b490f2
TT
12296 return htab_up (htab_create_alloc (3,
12297 hash_dwp_loaded_cutus,
12298 eq_dwp_loaded_cutus,
12299 NULL, xcalloc, xfree));
80626a55 12300}
3019eac3 12301
ab5088bf
DE
12302/* Try to open DWP file FILE_NAME.
12303 The result is the bfd handle of the file.
12304 If there is a problem finding or opening the file, return NULL.
12305 Upon success, the canonicalized path of the file is stored in the bfd,
12306 same as symfile_bfd_open. */
12307
192b62ce 12308static gdb_bfd_ref_ptr
ed2dc618
SM
12309open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12310 const char *file_name)
ab5088bf 12311{
ed2dc618
SM
12312 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12313 1 /*is_dwp*/,
192b62ce 12314 1 /*search_cwd*/));
6ac97d4c
DE
12315 if (abfd != NULL)
12316 return abfd;
12317
12318 /* Work around upstream bug 15652.
12319 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12320 [Whether that's a "bug" is debatable, but it is getting in our way.]
12321 We have no real idea where the dwp file is, because gdb's realpath-ing
12322 of the executable's path may have discarded the needed info.
12323 [IWBN if the dwp file name was recorded in the executable, akin to
12324 .gnu_debuglink, but that doesn't exist yet.]
12325 Strip the directory from FILE_NAME and search again. */
12326 if (*debug_file_directory != '\0')
12327 {
12328 /* Don't implicitly search the current directory here.
12329 If the user wants to search "." to handle this case,
12330 it must be added to debug-file-directory. */
ed2dc618
SM
12331 return try_open_dwop_file (dwarf2_per_objfile,
12332 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12333 0 /*search_cwd*/);
12334 }
12335
12336 return NULL;
ab5088bf
DE
12337}
12338
80626a55
DE
12339/* Initialize the use of the DWP file for the current objfile.
12340 By convention the name of the DWP file is ${objfile}.dwp.
12341 The result is NULL if it can't be found. */
a766d390 12342
400174b1 12343static std::unique_ptr<struct dwp_file>
ed2dc618 12344open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12345{
12346 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12347
82bf32bc
JK
12348 /* Try to find first .dwp for the binary file before any symbolic links
12349 resolving. */
6c447423
DE
12350
12351 /* If the objfile is a debug file, find the name of the real binary
12352 file and get the name of dwp file from there. */
d721ba37 12353 std::string dwp_name;
6c447423
DE
12354 if (objfile->separate_debug_objfile_backlink != NULL)
12355 {
12356 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12357 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12358
d721ba37 12359 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12360 }
12361 else
d721ba37
PA
12362 dwp_name = objfile->original_name;
12363
12364 dwp_name += ".dwp";
80626a55 12365
ed2dc618 12366 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12367 if (dbfd == NULL
12368 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12369 {
12370 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12371 dwp_name = objfile_name (objfile);
12372 dwp_name += ".dwp";
ed2dc618 12373 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12374 }
12375
80626a55
DE
12376 if (dbfd == NULL)
12377 {
b4f54984 12378 if (dwarf_read_debug)
d721ba37 12379 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12380 return std::unique_ptr<dwp_file> ();
3019eac3 12381 }
400174b1
TT
12382
12383 const char *name = bfd_get_filename (dbfd.get ());
12384 std::unique_ptr<struct dwp_file> dwp_file
12385 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12386
0a0f4c01 12387 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12388 dwp_file->elf_sections =
12389 OBSTACK_CALLOC (&objfile->objfile_obstack,
12390 dwp_file->num_sections, asection *);
12391
400174b1
TT
12392 bfd_map_over_sections (dwp_file->dbfd.get (),
12393 dwarf2_locate_common_dwp_sections,
12394 dwp_file.get ());
80626a55 12395
400174b1
TT
12396 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12397 0);
80626a55 12398
400174b1
TT
12399 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12400 1);
80626a55 12401
73869dc2 12402 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12403 if (dwp_file->cus && dwp_file->tus
12404 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12405 {
12406 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12407 pretty bizarre. We use pulongest here because that's the established
4d65956b 12408 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12409 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12410 " TU version %s [in DWP file %s]"),
12411 pulongest (dwp_file->cus->version),
d721ba37 12412 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12413 }
08302ed2
DE
12414
12415 if (dwp_file->cus)
12416 dwp_file->version = dwp_file->cus->version;
12417 else if (dwp_file->tus)
12418 dwp_file->version = dwp_file->tus->version;
12419 else
12420 dwp_file->version = 2;
73869dc2
DE
12421
12422 if (dwp_file->version == 2)
400174b1
TT
12423 bfd_map_over_sections (dwp_file->dbfd.get (),
12424 dwarf2_locate_v2_dwp_sections,
12425 dwp_file.get ());
73869dc2 12426
19ac8c2e
DE
12427 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12428 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 12429
b4f54984 12430 if (dwarf_read_debug)
80626a55
DE
12431 {
12432 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12433 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12434 " %s CUs, %s TUs\n",
12435 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12436 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12437 }
12438
12439 return dwp_file;
3019eac3 12440}
c906108c 12441
ab5088bf
DE
12442/* Wrapper around open_and_init_dwp_file, only open it once. */
12443
12444static struct dwp_file *
ed2dc618 12445get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12446{
12447 if (! dwarf2_per_objfile->dwp_checked)
12448 {
ed2dc618
SM
12449 dwarf2_per_objfile->dwp_file
12450 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12451 dwarf2_per_objfile->dwp_checked = 1;
12452 }
400174b1 12453 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12454}
12455
80626a55
DE
12456/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12457 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12458 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12459 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12460 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12461
12462 This is called, for example, when wanting to read a variable with a
12463 complex location. Therefore we don't want to do file i/o for every call.
12464 Therefore we don't want to look for a DWO file on every call.
12465 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12466 then we check if we've already seen DWO_NAME, and only THEN do we check
12467 for a DWO file.
12468
1c658ad5 12469 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12470 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12471
3019eac3 12472static struct dwo_unit *
80626a55
DE
12473lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12474 const char *dwo_name, const char *comp_dir,
12475 ULONGEST signature, int is_debug_types)
3019eac3 12476{
ed2dc618 12477 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12478 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12479 const char *kind = is_debug_types ? "TU" : "CU";
12480 void **dwo_file_slot;
3019eac3 12481 struct dwo_file *dwo_file;
80626a55 12482 struct dwp_file *dwp_file;
cb1df416 12483
6a506a2d
DE
12484 /* First see if there's a DWP file.
12485 If we have a DWP file but didn't find the DWO inside it, don't
12486 look for the original DWO file. It makes gdb behave differently
12487 depending on whether one is debugging in the build tree. */
cf2c3c16 12488
ed2dc618 12489 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12490 if (dwp_file != NULL)
cf2c3c16 12491 {
80626a55
DE
12492 const struct dwp_hash_table *dwp_htab =
12493 is_debug_types ? dwp_file->tus : dwp_file->cus;
12494
12495 if (dwp_htab != NULL)
12496 {
12497 struct dwo_unit *dwo_cutu =
ed2dc618 12498 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12499 signature, is_debug_types);
80626a55
DE
12500
12501 if (dwo_cutu != NULL)
12502 {
b4f54984 12503 if (dwarf_read_debug)
80626a55
DE
12504 {
12505 fprintf_unfiltered (gdb_stdlog,
12506 "Virtual DWO %s %s found: @%s\n",
12507 kind, hex_string (signature),
12508 host_address_to_string (dwo_cutu));
12509 }
12510 return dwo_cutu;
12511 }
12512 }
12513 }
6a506a2d 12514 else
80626a55 12515 {
6a506a2d 12516 /* No DWP file, look for the DWO file. */
80626a55 12517
ed2dc618
SM
12518 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12519 dwo_name, comp_dir);
6a506a2d 12520 if (*dwo_file_slot == NULL)
80626a55 12521 {
6a506a2d
DE
12522 /* Read in the file and build a table of the CUs/TUs it contains. */
12523 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12524 }
6a506a2d 12525 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12526 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12527
6a506a2d 12528 if (dwo_file != NULL)
19c3d4c9 12529 {
6a506a2d
DE
12530 struct dwo_unit *dwo_cutu = NULL;
12531
12532 if (is_debug_types && dwo_file->tus)
12533 {
12534 struct dwo_unit find_dwo_cutu;
12535
12536 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12537 find_dwo_cutu.signature = signature;
9a3c8263 12538 dwo_cutu
b0b6a987
TT
12539 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12540 &find_dwo_cutu);
6a506a2d 12541 }
33c5cd75 12542 else if (!is_debug_types && dwo_file->cus)
80626a55 12543 {
33c5cd75
DB
12544 struct dwo_unit find_dwo_cutu;
12545
12546 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12547 find_dwo_cutu.signature = signature;
b0b6a987 12548 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12549 &find_dwo_cutu);
6a506a2d
DE
12550 }
12551
12552 if (dwo_cutu != NULL)
12553 {
b4f54984 12554 if (dwarf_read_debug)
6a506a2d
DE
12555 {
12556 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12557 kind, dwo_name, hex_string (signature),
12558 host_address_to_string (dwo_cutu));
12559 }
12560 return dwo_cutu;
80626a55
DE
12561 }
12562 }
2e276125 12563 }
9cdd5dbd 12564
80626a55
DE
12565 /* We didn't find it. This could mean a dwo_id mismatch, or
12566 someone deleted the DWO/DWP file, or the search path isn't set up
12567 correctly to find the file. */
12568
b4f54984 12569 if (dwarf_read_debug)
80626a55
DE
12570 {
12571 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12572 kind, dwo_name, hex_string (signature));
12573 }
3019eac3 12574
6656a72d
DE
12575 /* This is a warning and not a complaint because it can be caused by
12576 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12577 {
12578 /* Print the name of the DWP file if we looked there, helps the user
12579 better diagnose the problem. */
791afaa2 12580 std::string dwp_text;
43942612
DE
12581
12582 if (dwp_file != NULL)
791afaa2
TT
12583 dwp_text = string_printf (" [in DWP file %s]",
12584 lbasename (dwp_file->name));
43942612 12585
9d8780f0 12586 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12587 " [in module %s]"),
12588 kind, dwo_name, hex_string (signature),
791afaa2 12589 dwp_text.c_str (),
43942612 12590 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12591 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12592 }
3019eac3 12593 return NULL;
5fb290d7
DJ
12594}
12595
80626a55
DE
12596/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12597 See lookup_dwo_cutu_unit for details. */
12598
12599static struct dwo_unit *
12600lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12601 const char *dwo_name, const char *comp_dir,
12602 ULONGEST signature)
12603{
12604 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12605}
12606
12607/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12608 See lookup_dwo_cutu_unit for details. */
12609
12610static struct dwo_unit *
12611lookup_dwo_type_unit (struct signatured_type *this_tu,
12612 const char *dwo_name, const char *comp_dir)
12613{
12614 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12615}
12616
89e63ee4
DE
12617/* Traversal function for queue_and_load_all_dwo_tus. */
12618
12619static int
12620queue_and_load_dwo_tu (void **slot, void *info)
12621{
12622 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12623 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12624 ULONGEST signature = dwo_unit->signature;
12625 struct signatured_type *sig_type =
12626 lookup_dwo_signatured_type (per_cu->cu, signature);
12627
12628 if (sig_type != NULL)
12629 {
12630 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12631
12632 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12633 a real dependency of PER_CU on SIG_TYPE. That is detected later
12634 while processing PER_CU. */
12635 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12636 load_full_type_unit (sig_cu);
ae640021 12637 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12638 }
12639
12640 return 1;
12641}
12642
12643/* Queue all TUs contained in the DWO of PER_CU to be read in.
12644 The DWO may have the only definition of the type, though it may not be
12645 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12646 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12647
12648static void
12649queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12650{
12651 struct dwo_unit *dwo_unit;
12652 struct dwo_file *dwo_file;
12653
12654 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12655 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12656 gdb_assert (per_cu->cu != NULL);
12657
12658 dwo_unit = per_cu->cu->dwo_unit;
12659 gdb_assert (dwo_unit != NULL);
12660
12661 dwo_file = dwo_unit->dwo_file;
12662 if (dwo_file->tus != NULL)
b0b6a987
TT
12663 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12664 per_cu);
89e63ee4
DE
12665}
12666
3019eac3 12667/* Read in various DIEs. */
348e048f 12668
d389af10 12669/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12670 Inherit only the children of the DW_AT_abstract_origin DIE not being
12671 already referenced by DW_AT_abstract_origin from the children of the
12672 current DIE. */
d389af10
JK
12673
12674static void
12675inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12676{
12677 struct die_info *child_die;
791afaa2 12678 sect_offset *offsetp;
d389af10
JK
12679 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12680 struct die_info *origin_die;
12681 /* Iterator of the ORIGIN_DIE children. */
12682 struct die_info *origin_child_die;
d389af10 12683 struct attribute *attr;
cd02d79d
PA
12684 struct dwarf2_cu *origin_cu;
12685 struct pending **origin_previous_list_in_scope;
d389af10
JK
12686
12687 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12688 if (!attr)
12689 return;
12690
cd02d79d
PA
12691 /* Note that following die references may follow to a die in a
12692 different cu. */
12693
12694 origin_cu = cu;
12695 origin_die = follow_die_ref (die, attr, &origin_cu);
12696
12697 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12698 symbols in. */
12699 origin_previous_list_in_scope = origin_cu->list_in_scope;
12700 origin_cu->list_in_scope = cu->list_in_scope;
12701
edb3359d
DJ
12702 if (die->tag != origin_die->tag
12703 && !(die->tag == DW_TAG_inlined_subroutine
12704 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12705 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12706 sect_offset_str (die->sect_off),
12707 sect_offset_str (origin_die->sect_off));
d389af10 12708
791afaa2 12709 std::vector<sect_offset> offsets;
d389af10 12710
3ea89b92
PMR
12711 for (child_die = die->child;
12712 child_die && child_die->tag;
12713 child_die = sibling_die (child_die))
12714 {
12715 struct die_info *child_origin_die;
12716 struct dwarf2_cu *child_origin_cu;
12717
12718 /* We are trying to process concrete instance entries:
216f72a1 12719 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12720 it's not relevant to our analysis here. i.e. detecting DIEs that are
12721 present in the abstract instance but not referenced in the concrete
12722 one. */
216f72a1
JK
12723 if (child_die->tag == DW_TAG_call_site
12724 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12725 continue;
12726
c38f313d
DJ
12727 /* For each CHILD_DIE, find the corresponding child of
12728 ORIGIN_DIE. If there is more than one layer of
12729 DW_AT_abstract_origin, follow them all; there shouldn't be,
12730 but GCC versions at least through 4.4 generate this (GCC PR
12731 40573). */
3ea89b92
PMR
12732 child_origin_die = child_die;
12733 child_origin_cu = cu;
c38f313d
DJ
12734 while (1)
12735 {
cd02d79d
PA
12736 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12737 child_origin_cu);
c38f313d
DJ
12738 if (attr == NULL)
12739 break;
cd02d79d
PA
12740 child_origin_die = follow_die_ref (child_origin_die, attr,
12741 &child_origin_cu);
c38f313d
DJ
12742 }
12743
d389af10
JK
12744 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12745 counterpart may exist. */
c38f313d 12746 if (child_origin_die != child_die)
d389af10 12747 {
edb3359d
DJ
12748 if (child_die->tag != child_origin_die->tag
12749 && !(child_die->tag == DW_TAG_inlined_subroutine
12750 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12751 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12752 "different tags"),
9d8780f0
SM
12753 sect_offset_str (child_die->sect_off),
12754 sect_offset_str (child_origin_die->sect_off));
c38f313d 12755 if (child_origin_die->parent != origin_die)
b98664d3 12756 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12757 "different parents"),
9d8780f0
SM
12758 sect_offset_str (child_die->sect_off),
12759 sect_offset_str (child_origin_die->sect_off));
c38f313d 12760 else
791afaa2 12761 offsets.push_back (child_origin_die->sect_off);
d389af10 12762 }
d389af10 12763 }
791afaa2
TT
12764 std::sort (offsets.begin (), offsets.end ());
12765 sect_offset *offsets_end = offsets.data () + offsets.size ();
12766 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12767 if (offsetp[-1] == *offsetp)
b98664d3 12768 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12769 "to DIE %s as their abstract origin"),
12770 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12771
791afaa2 12772 offsetp = offsets.data ();
d389af10
JK
12773 origin_child_die = origin_die->child;
12774 while (origin_child_die && origin_child_die->tag)
12775 {
12776 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12777 while (offsetp < offsets_end
9c541725 12778 && *offsetp < origin_child_die->sect_off)
d389af10 12779 offsetp++;
b64f50a1 12780 if (offsetp >= offsets_end
9c541725 12781 || *offsetp > origin_child_die->sect_off)
d389af10 12782 {
adde2bff
DE
12783 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12784 Check whether we're already processing ORIGIN_CHILD_DIE.
12785 This can happen with mutually referenced abstract_origins.
12786 PR 16581. */
12787 if (!origin_child_die->in_process)
12788 process_die (origin_child_die, origin_cu);
d389af10
JK
12789 }
12790 origin_child_die = sibling_die (origin_child_die);
12791 }
cd02d79d 12792 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12793
12794 if (cu != origin_cu)
12795 compute_delayed_physnames (origin_cu);
d389af10
JK
12796}
12797
c906108c 12798static void
e7c27a73 12799read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12800{
518817b3 12801 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12802 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12803 struct context_stack *newobj;
c906108c
SS
12804 CORE_ADDR lowpc;
12805 CORE_ADDR highpc;
12806 struct die_info *child_die;
edb3359d 12807 struct attribute *attr, *call_line, *call_file;
15d034d0 12808 const char *name;
e142c38c 12809 CORE_ADDR baseaddr;
801e3a5b 12810 struct block *block;
edb3359d 12811 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12812 std::vector<struct symbol *> template_args;
34eaf542 12813 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12814
12815 if (inlined_func)
12816 {
12817 /* If we do not have call site information, we can't show the
12818 caller of this inlined function. That's too confusing, so
12819 only use the scope for local variables. */
12820 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12821 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12822 if (call_line == NULL || call_file == NULL)
12823 {
12824 read_lexical_block_scope (die, cu);
12825 return;
12826 }
12827 }
c906108c 12828
b3b3bada 12829 baseaddr = objfile->text_section_offset ();
e142c38c 12830
94af9270 12831 name = dwarf2_name (die, cu);
c906108c 12832
e8d05480
JB
12833 /* Ignore functions with missing or empty names. These are actually
12834 illegal according to the DWARF standard. */
12835 if (name == NULL)
12836 {
b98664d3 12837 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12838 sect_offset_str (die->sect_off));
e8d05480
JB
12839 return;
12840 }
12841
12842 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12843 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12844 <= PC_BOUNDS_INVALID)
e8d05480 12845 {
ae4d0c03
PM
12846 attr = dwarf2_attr (die, DW_AT_external, cu);
12847 if (!attr || !DW_UNSND (attr))
b98664d3 12848 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12849 "for subprogram DIE at %s"),
12850 sect_offset_str (die->sect_off));
e8d05480
JB
12851 return;
12852 }
c906108c 12853
3e29f34a
MR
12854 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12855 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12856
34eaf542
TT
12857 /* If we have any template arguments, then we must allocate a
12858 different sort of symbol. */
12859 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12860 {
12861 if (child_die->tag == DW_TAG_template_type_param
12862 || child_die->tag == DW_TAG_template_value_param)
12863 {
e623cf5d 12864 templ_func = allocate_template_symbol (objfile);
cf724bc9 12865 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12866 break;
12867 }
12868 }
12869
c24bdb02 12870 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12871 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12872 (struct symbol *) templ_func);
4c2df51b 12873
81873cc8 12874 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12875 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12876 cu->language);
12877
4cecd739
DJ
12878 /* If there is a location expression for DW_AT_frame_base, record
12879 it. */
e142c38c 12880 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12881 if (attr != nullptr)
fe978cb0 12882 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12883
63e43d3a
PMR
12884 /* If there is a location for the static link, record it. */
12885 newobj->static_link = NULL;
12886 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12887 if (attr != nullptr)
63e43d3a 12888 {
224c3ddb
SM
12889 newobj->static_link
12890 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12891 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12892 cu->per_cu->addr_type ());
63e43d3a
PMR
12893 }
12894
c24bdb02 12895 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12896
639d11d3 12897 if (die->child != NULL)
c906108c 12898 {
639d11d3 12899 child_die = die->child;
c906108c
SS
12900 while (child_die && child_die->tag)
12901 {
34eaf542
TT
12902 if (child_die->tag == DW_TAG_template_type_param
12903 || child_die->tag == DW_TAG_template_value_param)
12904 {
12905 struct symbol *arg = new_symbol (child_die, NULL, cu);
12906
f1078f66 12907 if (arg != NULL)
2f4732b0 12908 template_args.push_back (arg);
34eaf542
TT
12909 }
12910 else
12911 process_die (child_die, cu);
c906108c
SS
12912 child_die = sibling_die (child_die);
12913 }
12914 }
12915
d389af10
JK
12916 inherit_abstract_dies (die, cu);
12917
4a811a97
UW
12918 /* If we have a DW_AT_specification, we might need to import using
12919 directives from the context of the specification DIE. See the
12920 comment in determine_prefix. */
12921 if (cu->language == language_cplus
12922 && dwarf2_attr (die, DW_AT_specification, cu))
12923 {
12924 struct dwarf2_cu *spec_cu = cu;
12925 struct die_info *spec_die = die_specification (die, &spec_cu);
12926
12927 while (spec_die)
12928 {
12929 child_die = spec_die->child;
12930 while (child_die && child_die->tag)
12931 {
12932 if (child_die->tag == DW_TAG_imported_module)
12933 process_die (child_die, spec_cu);
12934 child_die = sibling_die (child_die);
12935 }
12936
12937 /* In some cases, GCC generates specification DIEs that
12938 themselves contain DW_AT_specification attributes. */
12939 spec_die = die_specification (spec_die, &spec_cu);
12940 }
12941 }
12942
c24bdb02 12943 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12944 /* Make a block for the local symbols within. */
c24bdb02 12945 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12946 cstk.static_link, lowpc, highpc);
801e3a5b 12947
df8a16a1 12948 /* For C++, set the block's scope. */
45280282
IB
12949 if ((cu->language == language_cplus
12950 || cu->language == language_fortran
c44af4eb
TT
12951 || cu->language == language_d
12952 || cu->language == language_rust)
4d4ec4e5 12953 && cu->processing_has_namespace_info)
195a3f6c
TT
12954 block_set_scope (block, determine_prefix (die, cu),
12955 &objfile->objfile_obstack);
df8a16a1 12956
801e3a5b
JB
12957 /* If we have address ranges, record them. */
12958 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12959
a60f3166 12960 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12961
34eaf542 12962 /* Attach template arguments to function. */
2f4732b0 12963 if (!template_args.empty ())
34eaf542
TT
12964 {
12965 gdb_assert (templ_func != NULL);
12966
2f4732b0 12967 templ_func->n_template_arguments = template_args.size ();
34eaf542 12968 templ_func->template_arguments
8d749320
SM
12969 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12970 templ_func->n_template_arguments);
34eaf542 12971 memcpy (templ_func->template_arguments,
2f4732b0 12972 template_args.data (),
34eaf542 12973 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
12974
12975 /* Make sure that the symtab is set on the new symbols. Even
12976 though they don't appear in this symtab directly, other parts
12977 of gdb assume that symbols do, and this is reasonably
12978 true. */
8634679f 12979 for (symbol *sym : template_args)
3e1d3d8c 12980 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
12981 }
12982
208d8187
JB
12983 /* In C++, we can have functions nested inside functions (e.g., when
12984 a function declares a class that has methods). This means that
12985 when we finish processing a function scope, we may need to go
12986 back to building a containing block's symbol lists. */
c24bdb02
KS
12987 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12988 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 12989
921e78cf
JB
12990 /* If we've finished processing a top-level function, subsequent
12991 symbols go in the file symbol list. */
c24bdb02
KS
12992 if (cu->get_builder ()->outermost_context_p ())
12993 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
12994}
12995
12996/* Process all the DIES contained within a lexical block scope. Start
12997 a new scope, process the dies, and then close the scope. */
12998
12999static void
e7c27a73 13000read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13001{
518817b3 13002 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13003 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13004 CORE_ADDR lowpc, highpc;
13005 struct die_info *child_die;
e142c38c
DJ
13006 CORE_ADDR baseaddr;
13007
b3b3bada 13008 baseaddr = objfile->text_section_offset ();
c906108c
SS
13009
13010 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13011 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13012 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13013 be nasty. Might be easier to properly extend generic blocks to
af34e669 13014 describe ranges. */
e385593e
JK
13015 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13016 {
13017 case PC_BOUNDS_NOT_PRESENT:
13018 /* DW_TAG_lexical_block has no attributes, process its children as if
13019 there was no wrapping by that DW_TAG_lexical_block.
13020 GCC does no longer produces such DWARF since GCC r224161. */
13021 for (child_die = die->child;
13022 child_die != NULL && child_die->tag;
13023 child_die = sibling_die (child_die))
13024 process_die (child_die, cu);
13025 return;
13026 case PC_BOUNDS_INVALID:
13027 return;
13028 }
3e29f34a
MR
13029 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13030 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13031
c24bdb02 13032 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13033 if (die->child != NULL)
c906108c 13034 {
639d11d3 13035 child_die = die->child;
c906108c
SS
13036 while (child_die && child_die->tag)
13037 {
e7c27a73 13038 process_die (child_die, cu);
c906108c
SS
13039 child_die = sibling_die (child_die);
13040 }
13041 }
3ea89b92 13042 inherit_abstract_dies (die, cu);
c24bdb02 13043 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13044
c24bdb02
KS
13045 if (*cu->get_builder ()->get_local_symbols () != NULL
13046 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13047 {
801e3a5b 13048 struct block *block
c24bdb02 13049 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13050 cstk.start_addr, highpc);
801e3a5b
JB
13051
13052 /* Note that recording ranges after traversing children, as we
13053 do here, means that recording a parent's ranges entails
13054 walking across all its children's ranges as they appear in
13055 the address map, which is quadratic behavior.
13056
13057 It would be nicer to record the parent's ranges before
13058 traversing its children, simply overriding whatever you find
13059 there. But since we don't even decide whether to create a
13060 block until after we've traversed its children, that's hard
13061 to do. */
13062 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13063 }
c24bdb02
KS
13064 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13065 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13066}
13067
216f72a1 13068/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13069
13070static void
13071read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13072{
518817b3 13073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13074 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13075 CORE_ADDR pc, baseaddr;
13076 struct attribute *attr;
13077 struct call_site *call_site, call_site_local;
13078 void **slot;
13079 int nparams;
13080 struct die_info *child_die;
13081
b3b3bada 13082 baseaddr = objfile->text_section_offset ();
96408a79 13083
216f72a1
JK
13084 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13085 if (attr == NULL)
13086 {
13087 /* This was a pre-DWARF-5 GNU extension alias
13088 for DW_AT_call_return_pc. */
13089 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13090 }
96408a79
SA
13091 if (!attr)
13092 {
b98664d3 13093 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13094 "DIE %s [in module %s]"),
13095 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13096 return;
13097 }
cd6c91b4 13098 pc = attr->value_as_address () + baseaddr;
3e29f34a 13099 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13100
13101 if (cu->call_site_htab == NULL)
13102 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13103 NULL, &objfile->objfile_obstack,
13104 hashtab_obstack_allocate, NULL);
13105 call_site_local.pc = pc;
13106 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13107 if (*slot != NULL)
13108 {
b98664d3 13109 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13110 "DIE %s [in module %s]"),
13111 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13112 objfile_name (objfile));
96408a79
SA
13113 return;
13114 }
13115
13116 /* Count parameters at the caller. */
13117
13118 nparams = 0;
13119 for (child_die = die->child; child_die && child_die->tag;
13120 child_die = sibling_die (child_die))
13121 {
216f72a1
JK
13122 if (child_die->tag != DW_TAG_call_site_parameter
13123 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13124 {
b98664d3 13125 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13126 "DW_TAG_call_site child DIE %s [in module %s]"),
13127 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13128 objfile_name (objfile));
96408a79
SA
13129 continue;
13130 }
13131
13132 nparams++;
13133 }
13134
224c3ddb
SM
13135 call_site
13136 = ((struct call_site *)
13137 obstack_alloc (&objfile->objfile_obstack,
13138 sizeof (*call_site)
13139 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13140 *slot = call_site;
13141 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13142 call_site->pc = pc;
13143
216f72a1
JK
13144 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13145 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13146 {
13147 struct die_info *func_die;
13148
13149 /* Skip also over DW_TAG_inlined_subroutine. */
13150 for (func_die = die->parent;
13151 func_die && func_die->tag != DW_TAG_subprogram
13152 && func_die->tag != DW_TAG_subroutine_type;
13153 func_die = func_die->parent);
13154
216f72a1
JK
13155 /* DW_AT_call_all_calls is a superset
13156 of DW_AT_call_all_tail_calls. */
96408a79 13157 if (func_die
216f72a1 13158 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13159 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13160 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13161 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13162 {
13163 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13164 not complete. But keep CALL_SITE for look ups via call_site_htab,
13165 both the initial caller containing the real return address PC and
13166 the final callee containing the current PC of a chain of tail
13167 calls do not need to have the tail call list complete. But any
13168 function candidate for a virtual tail call frame searched via
13169 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13170 determined unambiguously. */
13171 }
13172 else
13173 {
13174 struct type *func_type = NULL;
13175
13176 if (func_die)
13177 func_type = get_die_type (func_die, cu);
13178 if (func_type != NULL)
13179 {
13180 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13181
13182 /* Enlist this call site to the function. */
13183 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13184 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13185 }
13186 else
b98664d3 13187 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13188 "DIE %s [in module %s]"),
13189 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13190 }
13191 }
13192
216f72a1
JK
13193 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13194 if (attr == NULL)
13195 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13196 if (attr == NULL)
13197 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13198 if (attr == NULL)
216f72a1
JK
13199 {
13200 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13201 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13202 }
96408a79 13203 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13204 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13205 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13206 else if (attr->form_is_block ())
96408a79
SA
13207 {
13208 struct dwarf2_locexpr_baton *dlbaton;
13209
8d749320 13210 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13211 dlbaton->data = DW_BLOCK (attr)->data;
13212 dlbaton->size = DW_BLOCK (attr)->size;
13213 dlbaton->per_cu = cu->per_cu;
13214
13215 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13216 }
cd6c91b4 13217 else if (attr->form_is_ref ())
96408a79 13218 {
96408a79
SA
13219 struct dwarf2_cu *target_cu = cu;
13220 struct die_info *target_die;
13221
ac9ec31b 13222 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13223 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13224 if (die_is_declaration (target_die, target_cu))
13225 {
7d45c7c3 13226 const char *target_physname;
9112db09
JK
13227
13228 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13229 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13230 if (target_physname == NULL)
9112db09 13231 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13232 if (target_physname == NULL)
b98664d3 13233 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13234 "physname, for referencing DIE %s [in module %s]"),
13235 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13236 else
7d455152 13237 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13238 }
13239 else
13240 {
13241 CORE_ADDR lowpc;
13242
13243 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13244 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13245 <= PC_BOUNDS_INVALID)
b98664d3 13246 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13247 "low pc, for referencing DIE %s [in module %s]"),
13248 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13249 else
3e29f34a
MR
13250 {
13251 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13252 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13253 }
96408a79
SA
13254 }
13255 }
13256 else
b98664d3 13257 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13258 "block nor reference, for DIE %s [in module %s]"),
13259 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13260
13261 call_site->per_cu = cu->per_cu;
13262
13263 for (child_die = die->child;
13264 child_die && child_die->tag;
13265 child_die = sibling_die (child_die))
13266 {
96408a79 13267 struct call_site_parameter *parameter;
1788b2d3 13268 struct attribute *loc, *origin;
96408a79 13269
216f72a1
JK
13270 if (child_die->tag != DW_TAG_call_site_parameter
13271 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13272 {
13273 /* Already printed the complaint above. */
13274 continue;
13275 }
13276
13277 gdb_assert (call_site->parameter_count < nparams);
13278 parameter = &call_site->parameter[call_site->parameter_count];
13279
1788b2d3
JK
13280 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13281 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13282 register is contained in DW_AT_call_value. */
96408a79 13283
24c5c679 13284 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13285 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13286 if (origin == NULL)
13287 {
13288 /* This was a pre-DWARF-5 GNU extension alias
13289 for DW_AT_call_parameter. */
13290 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13291 }
cd6c91b4 13292 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13293 {
1788b2d3 13294 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13295
13296 sect_offset sect_off
13297 = (sect_offset) dwarf2_get_ref_die_offset (origin);
4057dfde 13298 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13299 {
13300 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13301 binding can be done only inside one CU. Such referenced DIE
13302 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13303 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13304 "DW_TAG_call_site child DIE %s [in module %s]"),
13305 sect_offset_str (child_die->sect_off),
9c541725 13306 objfile_name (objfile));
d76b7dbc
JK
13307 continue;
13308 }
9c541725
PA
13309 parameter->u.param_cu_off
13310 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13311 }
4fc6c0d5 13312 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13313 {
b98664d3 13314 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13315 "DW_TAG_call_site child DIE %s [in module %s]"),
13316 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13317 continue;
13318 }
24c5c679 13319 else
96408a79 13320 {
24c5c679
JK
13321 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13322 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13323 if (parameter->u.dwarf_reg != -1)
13324 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13325 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13326 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13327 &parameter->u.fb_offset))
13328 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13329 else
13330 {
b98664d3 13331 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13332 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13333 "DW_TAG_call_site child DIE %s "
24c5c679 13334 "[in module %s]"),
9d8780f0 13335 sect_offset_str (child_die->sect_off),
9c541725 13336 objfile_name (objfile));
24c5c679
JK
13337 continue;
13338 }
96408a79
SA
13339 }
13340
216f72a1
JK
13341 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13342 if (attr == NULL)
13343 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13344 if (attr == NULL || !attr->form_is_block ())
96408a79 13345 {
b98664d3 13346 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13347 "DW_TAG_call_site child DIE %s [in module %s]"),
13348 sect_offset_str (child_die->sect_off),
9c541725 13349 objfile_name (objfile));
96408a79
SA
13350 continue;
13351 }
13352 parameter->value = DW_BLOCK (attr)->data;
13353 parameter->value_size = DW_BLOCK (attr)->size;
13354
13355 /* Parameters are not pre-cleared by memset above. */
13356 parameter->data_value = NULL;
13357 parameter->data_value_size = 0;
13358 call_site->parameter_count++;
13359
216f72a1
JK
13360 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13361 if (attr == NULL)
13362 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13363 if (attr != nullptr)
96408a79 13364 {
4fc6c0d5 13365 if (!attr->form_is_block ())
b98664d3 13366 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13367 "DW_TAG_call_site child DIE %s [in module %s]"),
13368 sect_offset_str (child_die->sect_off),
9c541725 13369 objfile_name (objfile));
96408a79
SA
13370 else
13371 {
13372 parameter->data_value = DW_BLOCK (attr)->data;
13373 parameter->data_value_size = DW_BLOCK (attr)->size;
13374 }
13375 }
13376 }
13377}
13378
71a3c369
TT
13379/* Helper function for read_variable. If DIE represents a virtual
13380 table, then return the type of the concrete object that is
13381 associated with the virtual table. Otherwise, return NULL. */
13382
13383static struct type *
13384rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13385{
13386 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13387 if (attr == NULL)
13388 return NULL;
13389
13390 /* Find the type DIE. */
13391 struct die_info *type_die = NULL;
13392 struct dwarf2_cu *type_cu = cu;
13393
cd6c91b4 13394 if (attr->form_is_ref ())
71a3c369
TT
13395 type_die = follow_die_ref (die, attr, &type_cu);
13396 if (type_die == NULL)
13397 return NULL;
13398
13399 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13400 return NULL;
13401 return die_containing_type (type_die, type_cu);
13402}
13403
13404/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13405
13406static void
13407read_variable (struct die_info *die, struct dwarf2_cu *cu)
13408{
13409 struct rust_vtable_symbol *storage = NULL;
13410
13411 if (cu->language == language_rust)
13412 {
13413 struct type *containing_type = rust_containing_type (die, cu);
13414
13415 if (containing_type != NULL)
13416 {
518817b3 13417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13418
468c0cbb 13419 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13420 initialize_objfile_symbol (storage);
13421 storage->concrete_type = containing_type;
cf724bc9 13422 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13423 }
13424 }
13425
e4a62c65
TV
13426 struct symbol *res = new_symbol (die, NULL, cu, storage);
13427 struct attribute *abstract_origin
13428 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13429 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13430 if (res == NULL && loc && abstract_origin)
13431 {
13432 /* We have a variable without a name, but with a location and an abstract
13433 origin. This may be a concrete instance of an abstract variable
13434 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13435 later. */
13436 struct dwarf2_cu *origin_cu = cu;
13437 struct die_info *origin_die
13438 = follow_die_ref (die, abstract_origin, &origin_cu);
13439 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13440 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13441 }
71a3c369
TT
13442}
13443
43988095
JK
13444/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13445 reading .debug_rnglists.
13446 Callback's type should be:
13447 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13448 Return true if the attributes are present and valid, otherwise,
13449 return false. */
13450
13451template <typename Callback>
13452static bool
13453dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13454 Callback &&callback)
13455{
ed2dc618 13456 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13457 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13458 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13459 bfd *obfd = objfile->obfd;
43988095
JK
13460 /* Base address selection entry. */
13461 CORE_ADDR base;
13462 int found_base;
43988095 13463 const gdb_byte *buffer;
43988095
JK
13464 CORE_ADDR baseaddr;
13465 bool overflow = false;
13466
13467 found_base = cu->base_known;
13468 base = cu->base_address;
13469
96b79293 13470 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13471 if (offset >= dwarf2_per_objfile->rnglists.size)
13472 {
b98664d3 13473 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13474 offset);
13475 return false;
13476 }
13477 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13478
b3b3bada 13479 baseaddr = objfile->text_section_offset ();
43988095
JK
13480
13481 while (1)
13482 {
7814882a
JK
13483 /* Initialize it due to a false compiler warning. */
13484 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13485 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13486 + dwarf2_per_objfile->rnglists.size);
13487 unsigned int bytes_read;
13488
13489 if (buffer == buf_end)
13490 {
13491 overflow = true;
13492 break;
13493 }
13494 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13495 switch (rlet)
13496 {
13497 case DW_RLE_end_of_list:
13498 break;
13499 case DW_RLE_base_address:
13500 if (buffer + cu->header.addr_size > buf_end)
13501 {
13502 overflow = true;
13503 break;
13504 }
c8a7a66f 13505 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13506 found_base = 1;
13507 buffer += bytes_read;
13508 break;
13509 case DW_RLE_start_length:
13510 if (buffer + cu->header.addr_size > buf_end)
13511 {
13512 overflow = true;
13513 break;
13514 }
c8a7a66f
TT
13515 range_beginning = cu->header.read_address (obfd, buffer,
13516 &bytes_read);
43988095
JK
13517 buffer += bytes_read;
13518 range_end = (range_beginning
13519 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13520 buffer += bytes_read;
13521 if (buffer > buf_end)
13522 {
13523 overflow = true;
13524 break;
13525 }
13526 break;
13527 case DW_RLE_offset_pair:
13528 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13529 buffer += bytes_read;
13530 if (buffer > buf_end)
13531 {
13532 overflow = true;
13533 break;
13534 }
13535 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13536 buffer += bytes_read;
13537 if (buffer > buf_end)
13538 {
13539 overflow = true;
13540 break;
13541 }
13542 break;
13543 case DW_RLE_start_end:
13544 if (buffer + 2 * cu->header.addr_size > buf_end)
13545 {
13546 overflow = true;
13547 break;
13548 }
c8a7a66f
TT
13549 range_beginning = cu->header.read_address (obfd, buffer,
13550 &bytes_read);
43988095 13551 buffer += bytes_read;
c8a7a66f 13552 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13553 buffer += bytes_read;
13554 break;
13555 default:
b98664d3 13556 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13557 return false;
13558 }
13559 if (rlet == DW_RLE_end_of_list || overflow)
13560 break;
13561 if (rlet == DW_RLE_base_address)
13562 continue;
13563
13564 if (!found_base)
13565 {
13566 /* We have no valid base address for the ranges
13567 data. */
b98664d3 13568 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13569 return false;
13570 }
13571
13572 if (range_beginning > range_end)
13573 {
13574 /* Inverted range entries are invalid. */
b98664d3 13575 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13576 return false;
13577 }
13578
13579 /* Empty range entries have no effect. */
13580 if (range_beginning == range_end)
13581 continue;
13582
13583 range_beginning += base;
13584 range_end += base;
13585
13586 /* A not-uncommon case of bad debug info.
13587 Don't pollute the addrmap with bad data. */
13588 if (range_beginning + baseaddr == 0
13589 && !dwarf2_per_objfile->has_section_at_zero)
13590 {
b98664d3 13591 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13592 " [in module %s]"), objfile_name (objfile));
13593 continue;
13594 }
13595
13596 callback (range_beginning, range_end);
13597 }
13598
13599 if (overflow)
13600 {
b98664d3 13601 complaint (_("Offset %d is not terminated "
43988095
JK
13602 "for DW_AT_ranges attribute"),
13603 offset);
13604 return false;
13605 }
13606
13607 return true;
13608}
13609
13610/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13611 Callback's type should be:
13612 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13613 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13614
43988095 13615template <typename Callback>
43039443 13616static int
5f46c5a5 13617dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13618 Callback &&callback)
43039443 13619{
ed2dc618 13620 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13621 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13622 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13623 struct comp_unit_head *cu_header = &cu->header;
13624 bfd *obfd = objfile->obfd;
13625 unsigned int addr_size = cu_header->addr_size;
13626 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13627 /* Base address selection entry. */
13628 CORE_ADDR base;
13629 int found_base;
13630 unsigned int dummy;
d521ce57 13631 const gdb_byte *buffer;
ff013f42 13632 CORE_ADDR baseaddr;
43039443 13633
43988095
JK
13634 if (cu_header->version >= 5)
13635 return dwarf2_rnglists_process (offset, cu, callback);
13636
d00adf39
DE
13637 found_base = cu->base_known;
13638 base = cu->base_address;
43039443 13639
96b79293 13640 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13641 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13642 {
b98664d3 13643 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13644 offset);
13645 return 0;
13646 }
dce234bc 13647 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13648
b3b3bada 13649 baseaddr = objfile->text_section_offset ();
ff013f42 13650
43039443
JK
13651 while (1)
13652 {
13653 CORE_ADDR range_beginning, range_end;
13654
c8a7a66f 13655 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13656 buffer += addr_size;
c8a7a66f 13657 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13658 buffer += addr_size;
13659 offset += 2 * addr_size;
13660
13661 /* An end of list marker is a pair of zero addresses. */
13662 if (range_beginning == 0 && range_end == 0)
13663 /* Found the end of list entry. */
13664 break;
13665
13666 /* Each base address selection entry is a pair of 2 values.
13667 The first is the largest possible address, the second is
13668 the base address. Check for a base address here. */
13669 if ((range_beginning & mask) == mask)
13670 {
28d2bfb9
AB
13671 /* If we found the largest possible address, then we already
13672 have the base address in range_end. */
13673 base = range_end;
43039443
JK
13674 found_base = 1;
13675 continue;
13676 }
13677
13678 if (!found_base)
13679 {
13680 /* We have no valid base address for the ranges
13681 data. */
b98664d3 13682 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13683 return 0;
13684 }
13685
9277c30c
UW
13686 if (range_beginning > range_end)
13687 {
13688 /* Inverted range entries are invalid. */
b98664d3 13689 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13690 return 0;
13691 }
13692
13693 /* Empty range entries have no effect. */
13694 if (range_beginning == range_end)
13695 continue;
13696
43039443
JK
13697 range_beginning += base;
13698 range_end += base;
13699
01093045
DE
13700 /* A not-uncommon case of bad debug info.
13701 Don't pollute the addrmap with bad data. */
13702 if (range_beginning + baseaddr == 0
13703 && !dwarf2_per_objfile->has_section_at_zero)
13704 {
b98664d3 13705 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13706 " [in module %s]"), objfile_name (objfile));
01093045
DE
13707 continue;
13708 }
13709
5f46c5a5
JK
13710 callback (range_beginning, range_end);
13711 }
13712
13713 return 1;
13714}
13715
13716/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13717 Return 1 if the attributes are present and valid, otherwise, return 0.
13718 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13719
13720static int
13721dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13722 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13723 dwarf2_psymtab *ranges_pst)
5f46c5a5 13724{
518817b3 13725 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13726 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13727 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13728 int low_set = 0;
13729 CORE_ADDR low = 0;
13730 CORE_ADDR high = 0;
13731 int retval;
13732
13733 retval = dwarf2_ranges_process (offset, cu,
13734 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13735 {
9277c30c 13736 if (ranges_pst != NULL)
3e29f34a
MR
13737 {
13738 CORE_ADDR lowpc;
13739 CORE_ADDR highpc;
13740
79748972
TT
13741 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13742 range_beginning + baseaddr)
13743 - baseaddr);
13744 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13745 range_end + baseaddr)
13746 - baseaddr);
d320c2b5
TT
13747 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13748 lowpc, highpc - 1, ranges_pst);
3e29f34a 13749 }
ff013f42 13750
43039443
JK
13751 /* FIXME: This is recording everything as a low-high
13752 segment of consecutive addresses. We should have a
13753 data structure for discontiguous block ranges
13754 instead. */
13755 if (! low_set)
13756 {
13757 low = range_beginning;
13758 high = range_end;
13759 low_set = 1;
13760 }
13761 else
13762 {
13763 if (range_beginning < low)
13764 low = range_beginning;
13765 if (range_end > high)
13766 high = range_end;
13767 }
5f46c5a5
JK
13768 });
13769 if (!retval)
13770 return 0;
43039443
JK
13771
13772 if (! low_set)
13773 /* If the first entry is an end-of-list marker, the range
13774 describes an empty scope, i.e. no instructions. */
13775 return 0;
13776
13777 if (low_return)
13778 *low_return = low;
13779 if (high_return)
13780 *high_return = high;
13781 return 1;
13782}
13783
3a2b436a
JK
13784/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13785 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13786 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13787
3a2b436a 13788static enum pc_bounds_kind
af34e669 13789dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13790 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13791 dwarf2_psymtab *pst)
c906108c 13792{
518817b3
SM
13793 struct dwarf2_per_objfile *dwarf2_per_objfile
13794 = cu->per_cu->dwarf2_per_objfile;
c906108c 13795 struct attribute *attr;
91da1414 13796 struct attribute *attr_high;
af34e669
DJ
13797 CORE_ADDR low = 0;
13798 CORE_ADDR high = 0;
e385593e 13799 enum pc_bounds_kind ret;
c906108c 13800
91da1414
MW
13801 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13802 if (attr_high)
af34e669 13803 {
e142c38c 13804 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13805 if (attr != nullptr)
91da1414 13806 {
cd6c91b4
TT
13807 low = attr->value_as_address ();
13808 high = attr_high->value_as_address ();
13809 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13810 high += low;
91da1414 13811 }
af34e669
DJ
13812 else
13813 /* Found high w/o low attribute. */
e385593e 13814 return PC_BOUNDS_INVALID;
af34e669
DJ
13815
13816 /* Found consecutive range of addresses. */
3a2b436a 13817 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13818 }
c906108c 13819 else
af34e669 13820 {
e142c38c 13821 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13822 if (attr != NULL)
13823 {
18a8505e 13824 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13825 We take advantage of the fact that DW_AT_ranges does not appear
13826 in DW_TAG_compile_unit of DWO files. */
13827 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13828 unsigned int ranges_offset = (DW_UNSND (attr)
13829 + (need_ranges_base
13830 ? cu->ranges_base
13831 : 0));
2e3cf129 13832
af34e669 13833 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13834 .debug_ranges section. */
2e3cf129 13835 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13836 return PC_BOUNDS_INVALID;
43039443 13837 /* Found discontinuous range of addresses. */
3a2b436a 13838 ret = PC_BOUNDS_RANGES;
af34e669 13839 }
e385593e
JK
13840 else
13841 return PC_BOUNDS_NOT_PRESENT;
af34e669 13842 }
c906108c 13843
48fbe735 13844 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13845 if (high <= low)
e385593e 13846 return PC_BOUNDS_INVALID;
c906108c
SS
13847
13848 /* When using the GNU linker, .gnu.linkonce. sections are used to
13849 eliminate duplicate copies of functions and vtables and such.
13850 The linker will arbitrarily choose one and discard the others.
13851 The AT_*_pc values for such functions refer to local labels in
13852 these sections. If the section from that file was discarded, the
13853 labels are not in the output, so the relocs get a value of 0.
13854 If this is a discarded function, mark the pc bounds as invalid,
13855 so that GDB will ignore it. */
72dca2f5 13856 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13857 return PC_BOUNDS_INVALID;
c906108c
SS
13858
13859 *lowpc = low;
96408a79
SA
13860 if (highpc)
13861 *highpc = high;
af34e669 13862 return ret;
c906108c
SS
13863}
13864
b084d499
JB
13865/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13866 its low and high PC addresses. Do nothing if these addresses could not
13867 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13868 and HIGHPC to the high address if greater than HIGHPC. */
13869
13870static void
13871dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13872 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13873 struct dwarf2_cu *cu)
13874{
13875 CORE_ADDR low, high;
13876 struct die_info *child = die->child;
13877
e385593e 13878 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13879 {
325fac50
PA
13880 *lowpc = std::min (*lowpc, low);
13881 *highpc = std::max (*highpc, high);
b084d499
JB
13882 }
13883
13884 /* If the language does not allow nested subprograms (either inside
13885 subprograms or lexical blocks), we're done. */
13886 if (cu->language != language_ada)
13887 return;
6e70227d 13888
b084d499
JB
13889 /* Check all the children of the given DIE. If it contains nested
13890 subprograms, then check their pc bounds. Likewise, we need to
13891 check lexical blocks as well, as they may also contain subprogram
13892 definitions. */
13893 while (child && child->tag)
13894 {
13895 if (child->tag == DW_TAG_subprogram
13896 || child->tag == DW_TAG_lexical_block)
13897 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13898 child = sibling_die (child);
13899 }
13900}
13901
fae299cd
DC
13902/* Get the low and high pc's represented by the scope DIE, and store
13903 them in *LOWPC and *HIGHPC. If the correct values can't be
13904 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13905
13906static void
13907get_scope_pc_bounds (struct die_info *die,
13908 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13909 struct dwarf2_cu *cu)
13910{
13911 CORE_ADDR best_low = (CORE_ADDR) -1;
13912 CORE_ADDR best_high = (CORE_ADDR) 0;
13913 CORE_ADDR current_low, current_high;
13914
3a2b436a 13915 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13916 >= PC_BOUNDS_RANGES)
fae299cd
DC
13917 {
13918 best_low = current_low;
13919 best_high = current_high;
13920 }
13921 else
13922 {
13923 struct die_info *child = die->child;
13924
13925 while (child && child->tag)
13926 {
13927 switch (child->tag) {
13928 case DW_TAG_subprogram:
b084d499 13929 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13930 break;
13931 case DW_TAG_namespace:
f55ee35c 13932 case DW_TAG_module:
fae299cd
DC
13933 /* FIXME: carlton/2004-01-16: Should we do this for
13934 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13935 that current GCC's always emit the DIEs corresponding
13936 to definitions of methods of classes as children of a
13937 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13938 the DIEs giving the declarations, which could be
13939 anywhere). But I don't see any reason why the
13940 standards says that they have to be there. */
13941 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13942
13943 if (current_low != ((CORE_ADDR) -1))
13944 {
325fac50
PA
13945 best_low = std::min (best_low, current_low);
13946 best_high = std::max (best_high, current_high);
fae299cd
DC
13947 }
13948 break;
13949 default:
0963b4bd 13950 /* Ignore. */
fae299cd
DC
13951 break;
13952 }
13953
13954 child = sibling_die (child);
13955 }
13956 }
13957
13958 *lowpc = best_low;
13959 *highpc = best_high;
13960}
13961
801e3a5b
JB
13962/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13963 in DIE. */
380bca97 13964
801e3a5b
JB
13965static void
13966dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13967 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13968{
518817b3 13969 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13970 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13971 struct attribute *attr;
91da1414 13972 struct attribute *attr_high;
801e3a5b 13973
91da1414
MW
13974 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13975 if (attr_high)
801e3a5b 13976 {
801e3a5b 13977 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13978 if (attr != nullptr)
801e3a5b 13979 {
cd6c91b4
TT
13980 CORE_ADDR low = attr->value_as_address ();
13981 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 13982
cd6c91b4 13983 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13984 high += low;
9a619af0 13985
3e29f34a
MR
13986 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13987 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 13988 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
13989 }
13990 }
13991
13992 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 13993 if (attr != nullptr)
801e3a5b 13994 {
18a8505e 13995 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13996 We take advantage of the fact that DW_AT_ranges does not appear
13997 in DW_TAG_compile_unit of DWO files. */
13998 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13999
14000 /* The value of the DW_AT_ranges attribute is the offset of the
14001 address range list in the .debug_ranges section. */
ab435259
DE
14002 unsigned long offset = (DW_UNSND (attr)
14003 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14004
2d5f09ec 14005 std::vector<blockrange> blockvec;
5f46c5a5
JK
14006 dwarf2_ranges_process (offset, cu,
14007 [&] (CORE_ADDR start, CORE_ADDR end)
14008 {
58fdfd2c
JK
14009 start += baseaddr;
14010 end += baseaddr;
5f46c5a5
JK
14011 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14012 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14013 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14014 blockvec.emplace_back (start, end);
5f46c5a5 14015 });
2d5f09ec
KB
14016
14017 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14018 }
14019}
14020
685b1105
JK
14021/* Check whether the producer field indicates either of GCC < 4.6, or the
14022 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14023
685b1105
JK
14024static void
14025check_producer (struct dwarf2_cu *cu)
60d5a603 14026{
38360086 14027 int major, minor;
60d5a603
JK
14028
14029 if (cu->producer == NULL)
14030 {
14031 /* For unknown compilers expect their behavior is DWARF version
14032 compliant.
14033
14034 GCC started to support .debug_types sections by -gdwarf-4 since
14035 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14036 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14037 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14038 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14039 }
b1ffba5a 14040 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14041 {
38360086
MW
14042 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14043 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14044 }
5230b05a 14045 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14046 {
14047 cu->producer_is_icc = true;
14048 cu->producer_is_icc_lt_14 = major < 14;
14049 }
c258c396
JD
14050 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14051 cu->producer_is_codewarrior = true;
685b1105
JK
14052 else
14053 {
14054 /* For other non-GCC compilers, expect their behavior is DWARF version
14055 compliant. */
60d5a603
JK
14056 }
14057
9068261f 14058 cu->checked_producer = true;
685b1105 14059}
ba919b58 14060
685b1105
JK
14061/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14062 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14063 during 4.6.0 experimental. */
14064
9068261f 14065static bool
685b1105
JK
14066producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14067{
14068 if (!cu->checked_producer)
14069 check_producer (cu);
14070
14071 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14072}
14073
c258c396
JD
14074
14075/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14076 with incorrect is_stmt attributes. */
14077
14078static bool
14079producer_is_codewarrior (struct dwarf2_cu *cu)
14080{
14081 if (!cu->checked_producer)
14082 check_producer (cu);
14083
14084 return cu->producer_is_codewarrior;
14085}
14086
405feb71 14087/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14088 DW_AT_accessibility. */
14089
14090static enum dwarf_access_attribute
14091dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14092{
14093 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14094 {
14095 /* The default DWARF 2 accessibility for members is public, the default
14096 accessibility for inheritance is private. */
14097
14098 if (die->tag != DW_TAG_inheritance)
14099 return DW_ACCESS_public;
14100 else
14101 return DW_ACCESS_private;
14102 }
14103 else
14104 {
14105 /* DWARF 3+ defines the default accessibility a different way. The same
14106 rules apply now for DW_TAG_inheritance as for the members and it only
14107 depends on the container kind. */
14108
14109 if (die->parent->tag == DW_TAG_class_type)
14110 return DW_ACCESS_private;
14111 else
14112 return DW_ACCESS_public;
14113 }
14114}
14115
74ac6d43
TT
14116/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14117 offset. If the attribute was not found return 0, otherwise return
14118 1. If it was found but could not properly be handled, set *OFFSET
14119 to 0. */
14120
14121static int
14122handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14123 LONGEST *offset)
14124{
14125 struct attribute *attr;
14126
14127 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14128 if (attr != NULL)
14129 {
14130 *offset = 0;
14131
14132 /* Note that we do not check for a section offset first here.
14133 This is because DW_AT_data_member_location is new in DWARF 4,
14134 so if we see it, we can assume that a constant form is really
14135 a constant and not a section offset. */
cd6c91b4 14136 if (attr->form_is_constant ())
74ac6d43 14137 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14138 else if (attr->form_is_section_offset ())
74ac6d43 14139 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14140 else if (attr->form_is_block ())
74ac6d43
TT
14141 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14142 else
14143 dwarf2_complex_location_expr_complaint ();
14144
14145 return 1;
14146 }
14147
14148 return 0;
14149}
14150
c906108c
SS
14151/* Add an aggregate field to the field list. */
14152
14153static void
107d2387 14154dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14155 struct dwarf2_cu *cu)
6e70227d 14156{
518817b3 14157 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14158 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14159 struct nextfield *new_field;
14160 struct attribute *attr;
14161 struct field *fp;
15d034d0 14162 const char *fieldname = "";
c906108c 14163
7d0ccb61
DJ
14164 if (die->tag == DW_TAG_inheritance)
14165 {
be2daae6
TT
14166 fip->baseclasses.emplace_back ();
14167 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14168 }
14169 else
14170 {
be2daae6
TT
14171 fip->fields.emplace_back ();
14172 new_field = &fip->fields.back ();
7d0ccb61 14173 }
be2daae6 14174
c906108c
SS
14175 fip->nfields++;
14176
e142c38c 14177 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14178 if (attr != nullptr)
c906108c 14179 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14180 else
14181 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14182 if (new_field->accessibility != DW_ACCESS_public)
14183 fip->non_public_fields = 1;
60d5a603 14184
e142c38c 14185 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14186 if (attr != nullptr)
c906108c 14187 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14188 else
14189 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14190
14191 fp = &new_field->field;
a9a9bd0f 14192
e142c38c 14193 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14194 {
74ac6d43
TT
14195 LONGEST offset;
14196
a9a9bd0f 14197 /* Data member other than a C++ static data member. */
6e70227d 14198
c906108c 14199 /* Get type of field. */
e7c27a73 14200 fp->type = die_type (die, cu);
c906108c 14201
d6a843b5 14202 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14203
c906108c 14204 /* Get bit size of field (zero if none). */
e142c38c 14205 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14206 if (attr != nullptr)
c906108c
SS
14207 {
14208 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14209 }
14210 else
14211 {
14212 FIELD_BITSIZE (*fp) = 0;
14213 }
14214
14215 /* Get bit offset of field. */
74ac6d43
TT
14216 if (handle_data_member_location (die, cu, &offset))
14217 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14218 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14219 if (attr != nullptr)
c906108c 14220 {
d5a22e77 14221 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14222 {
14223 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14224 additional bit offset from the MSB of the containing
14225 anonymous object to the MSB of the field. We don't
14226 have to do anything special since we don't need to
14227 know the size of the anonymous object. */
f41f5e61 14228 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14229 }
14230 else
14231 {
14232 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14233 MSB of the anonymous object, subtract off the number of
14234 bits from the MSB of the field to the MSB of the
14235 object, and then subtract off the number of bits of
14236 the field itself. The result is the bit offset of
14237 the LSB of the field. */
c906108c
SS
14238 int anonymous_size;
14239 int bit_offset = DW_UNSND (attr);
14240
e142c38c 14241 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14242 if (attr != nullptr)
c906108c
SS
14243 {
14244 /* The size of the anonymous object containing
14245 the bit field is explicit, so use the
14246 indicated size (in bytes). */
14247 anonymous_size = DW_UNSND (attr);
14248 }
14249 else
14250 {
14251 /* The size of the anonymous object containing
14252 the bit field must be inferred from the type
14253 attribute of the data member containing the
14254 bit field. */
14255 anonymous_size = TYPE_LENGTH (fp->type);
14256 }
f41f5e61
PA
14257 SET_FIELD_BITPOS (*fp,
14258 (FIELD_BITPOS (*fp)
14259 + anonymous_size * bits_per_byte
14260 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14261 }
14262 }
da5b30da
AA
14263 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14264 if (attr != NULL)
14265 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14266 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14267
14268 /* Get name of field. */
39cbfefa
DJ
14269 fieldname = dwarf2_name (die, cu);
14270 if (fieldname == NULL)
14271 fieldname = "";
d8151005
DJ
14272
14273 /* The name is already allocated along with this objfile, so we don't
14274 need to duplicate it for the type. */
14275 fp->name = fieldname;
c906108c
SS
14276
14277 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14278 pointer or virtual base class pointer) to private. */
e142c38c 14279 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14280 {
d48cc9dd 14281 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14282 new_field->accessibility = DW_ACCESS_private;
14283 fip->non_public_fields = 1;
14284 }
14285 }
a9a9bd0f 14286 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14287 {
a9a9bd0f
DC
14288 /* C++ static member. */
14289
14290 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14291 is a declaration, but all versions of G++ as of this writing
14292 (so through at least 3.2.1) incorrectly generate
14293 DW_TAG_variable tags. */
6e70227d 14294
ff355380 14295 const char *physname;
c906108c 14296
a9a9bd0f 14297 /* Get name of field. */
39cbfefa
DJ
14298 fieldname = dwarf2_name (die, cu);
14299 if (fieldname == NULL)
c906108c
SS
14300 return;
14301
254e6b9e 14302 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14303 if (attr
14304 /* Only create a symbol if this is an external value.
14305 new_symbol checks this and puts the value in the global symbol
14306 table, which we want. If it is not external, new_symbol
14307 will try to put the value in cu->list_in_scope which is wrong. */
14308 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14309 {
14310 /* A static const member, not much different than an enum as far as
14311 we're concerned, except that we can support more types. */
14312 new_symbol (die, NULL, cu);
14313 }
14314
2df3850c 14315 /* Get physical name. */
ff355380 14316 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14317
d8151005
DJ
14318 /* The name is already allocated along with this objfile, so we don't
14319 need to duplicate it for the type. */
14320 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14321 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14322 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14323 }
14324 else if (die->tag == DW_TAG_inheritance)
14325 {
74ac6d43 14326 LONGEST offset;
d4b96c9a 14327
74ac6d43
TT
14328 /* C++ base class field. */
14329 if (handle_data_member_location (die, cu, &offset))
14330 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14331 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14332 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14333 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14334 }
2ddeaf8a
TT
14335 else if (die->tag == DW_TAG_variant_part)
14336 {
14337 /* process_structure_scope will treat this DIE as a union. */
14338 process_structure_scope (die, cu);
14339
14340 /* The variant part is relative to the start of the enclosing
14341 structure. */
14342 SET_FIELD_BITPOS (*fp, 0);
14343 fp->type = get_die_type (die, cu);
14344 fp->artificial = 1;
14345 fp->name = "<<variant>>";
c8c81635
TT
14346
14347 /* Normally a DW_TAG_variant_part won't have a size, but our
14348 representation requires one, so set it to the maximum of the
489dbda6
TT
14349 child sizes, being sure to account for the offset at which
14350 each child is seen. */
c8c81635
TT
14351 if (TYPE_LENGTH (fp->type) == 0)
14352 {
14353 unsigned max = 0;
14354 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14355 {
14356 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14357 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14358 if (len > max)
14359 max = len;
14360 }
c8c81635
TT
14361 TYPE_LENGTH (fp->type) = max;
14362 }
2ddeaf8a
TT
14363 }
14364 else
14365 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14366}
14367
883fd55a
KS
14368/* Can the type given by DIE define another type? */
14369
14370static bool
14371type_can_define_types (const struct die_info *die)
14372{
14373 switch (die->tag)
14374 {
14375 case DW_TAG_typedef:
14376 case DW_TAG_class_type:
14377 case DW_TAG_structure_type:
14378 case DW_TAG_union_type:
14379 case DW_TAG_enumeration_type:
14380 return true;
14381
14382 default:
14383 return false;
14384 }
14385}
14386
14387/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14388
14389static void
883fd55a
KS
14390dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14391 struct dwarf2_cu *cu)
6e70227d 14392{
be2daae6
TT
14393 struct decl_field fp;
14394 memset (&fp, 0, sizeof (fp));
98751a41 14395
883fd55a 14396 gdb_assert (type_can_define_types (die));
98751a41 14397
883fd55a 14398 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14399 fp.name = dwarf2_name (die, cu);
14400 fp.type = read_type_die (die, cu);
98751a41 14401
c191a687
KS
14402 /* Save accessibility. */
14403 enum dwarf_access_attribute accessibility;
14404 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14405 if (attr != NULL)
14406 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14407 else
14408 accessibility = dwarf2_default_access_attribute (die, cu);
14409 switch (accessibility)
14410 {
14411 case DW_ACCESS_public:
14412 /* The assumed value if neither private nor protected. */
14413 break;
14414 case DW_ACCESS_private:
be2daae6 14415 fp.is_private = 1;
c191a687
KS
14416 break;
14417 case DW_ACCESS_protected:
be2daae6 14418 fp.is_protected = 1;
c191a687
KS
14419 break;
14420 default:
b98664d3 14421 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14422 }
14423
883fd55a 14424 if (die->tag == DW_TAG_typedef)
be2daae6 14425 fip->typedef_field_list.push_back (fp);
883fd55a 14426 else
be2daae6 14427 fip->nested_types_list.push_back (fp);
98751a41
JK
14428}
14429
c906108c
SS
14430/* Create the vector of fields, and attach it to the type. */
14431
14432static void
fba45db2 14433dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14434 struct dwarf2_cu *cu)
c906108c
SS
14435{
14436 int nfields = fip->nfields;
14437
14438 /* Record the field count, allocate space for the array of fields,
14439 and create blank accessibility bitfields if necessary. */
14440 TYPE_NFIELDS (type) = nfields;
14441 TYPE_FIELDS (type) = (struct field *)
be2daae6 14442 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14443
b4ba55a1 14444 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14445 {
14446 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14447
14448 TYPE_FIELD_PRIVATE_BITS (type) =
14449 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14450 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14451
14452 TYPE_FIELD_PROTECTED_BITS (type) =
14453 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14454 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14455
774b6a14
TT
14456 TYPE_FIELD_IGNORE_BITS (type) =
14457 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14458 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14459 }
14460
14461 /* If the type has baseclasses, allocate and clear a bit vector for
14462 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14463 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14464 {
be2daae6 14465 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14466 unsigned char *pointer;
c906108c
SS
14467
14468 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14469 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14470 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14471 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14472 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14473 }
14474
2ddeaf8a
TT
14475 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14476 {
14477 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14478
be2daae6 14479 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14480 {
be2daae6
TT
14481 struct nextfield &field = fip->fields[index];
14482
14483 if (field.variant.is_discriminant)
2ddeaf8a 14484 di->discriminant_index = index;
be2daae6 14485 else if (field.variant.default_branch)
2ddeaf8a
TT
14486 di->default_index = index;
14487 else
be2daae6 14488 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14489 }
14490 }
14491
be2daae6
TT
14492 /* Copy the saved-up fields into the field vector. */
14493 for (int i = 0; i < nfields; ++i)
c906108c 14494 {
be2daae6
TT
14495 struct nextfield &field
14496 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14497 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14498
be2daae6
TT
14499 TYPE_FIELD (type, i) = field.field;
14500 switch (field.accessibility)
c906108c 14501 {
c5aa993b 14502 case DW_ACCESS_private:
b4ba55a1 14503 if (cu->language != language_ada)
be2daae6 14504 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14505 break;
c906108c 14506
c5aa993b 14507 case DW_ACCESS_protected:
b4ba55a1 14508 if (cu->language != language_ada)
be2daae6 14509 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14510 break;
c906108c 14511
c5aa993b
JM
14512 case DW_ACCESS_public:
14513 break;
c906108c 14514
c5aa993b
JM
14515 default:
14516 /* Unknown accessibility. Complain and treat it as public. */
14517 {
b98664d3 14518 complaint (_("unsupported accessibility %d"),
be2daae6 14519 field.accessibility);
c5aa993b
JM
14520 }
14521 break;
c906108c 14522 }
be2daae6 14523 if (i < fip->baseclasses.size ())
c906108c 14524 {
be2daae6 14525 switch (field.virtuality)
c906108c 14526 {
c5aa993b
JM
14527 case DW_VIRTUALITY_virtual:
14528 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14529 if (cu->language == language_ada)
a73c6dcd 14530 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14531 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14532 break;
c906108c
SS
14533 }
14534 }
c906108c
SS
14535 }
14536}
14537
7d27a96d
TT
14538/* Return true if this member function is a constructor, false
14539 otherwise. */
14540
14541static int
14542dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14543{
14544 const char *fieldname;
fe978cb0 14545 const char *type_name;
7d27a96d
TT
14546 int len;
14547
14548 if (die->parent == NULL)
14549 return 0;
14550
14551 if (die->parent->tag != DW_TAG_structure_type
14552 && die->parent->tag != DW_TAG_union_type
14553 && die->parent->tag != DW_TAG_class_type)
14554 return 0;
14555
14556 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14557 type_name = dwarf2_name (die->parent, cu);
14558 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14559 return 0;
14560
14561 len = strlen (fieldname);
fe978cb0
PA
14562 return (strncmp (fieldname, type_name, len) == 0
14563 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14564}
14565
e35000a7
TBA
14566/* Check if the given VALUE is a recognized enum
14567 dwarf_defaulted_attribute constant according to DWARF5 spec,
14568 Table 7.24. */
14569
14570static bool
14571is_valid_DW_AT_defaulted (ULONGEST value)
14572{
14573 switch (value)
14574 {
14575 case DW_DEFAULTED_no:
14576 case DW_DEFAULTED_in_class:
14577 case DW_DEFAULTED_out_of_class:
14578 return true;
14579 }
14580
3142e908 14581 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14582 return false;
14583}
14584
c906108c
SS
14585/* Add a member function to the proper fieldlist. */
14586
14587static void
107d2387 14588dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14589 struct type *type, struct dwarf2_cu *cu)
c906108c 14590{
518817b3 14591 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14592 struct attribute *attr;
c906108c 14593 int i;
be2daae6 14594 struct fnfieldlist *flp = nullptr;
c906108c 14595 struct fn_field *fnp;
15d034d0 14596 const char *fieldname;
f792889a 14597 struct type *this_type;
60d5a603 14598 enum dwarf_access_attribute accessibility;
c906108c 14599
b4ba55a1 14600 if (cu->language == language_ada)
a73c6dcd 14601 error (_("unexpected member function in Ada type"));
b4ba55a1 14602
2df3850c 14603 /* Get name of member function. */
39cbfefa
DJ
14604 fieldname = dwarf2_name (die, cu);
14605 if (fieldname == NULL)
2df3850c 14606 return;
c906108c 14607
c906108c 14608 /* Look up member function name in fieldlist. */
be2daae6 14609 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14610 {
27bfe10e 14611 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14612 {
14613 flp = &fip->fnfieldlists[i];
14614 break;
14615 }
c906108c
SS
14616 }
14617
be2daae6
TT
14618 /* Create a new fnfieldlist if necessary. */
14619 if (flp == nullptr)
c906108c 14620 {
be2daae6
TT
14621 fip->fnfieldlists.emplace_back ();
14622 flp = &fip->fnfieldlists.back ();
c906108c 14623 flp->name = fieldname;
be2daae6 14624 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14625 }
14626
be2daae6
TT
14627 /* Create a new member function field and add it to the vector of
14628 fnfieldlists. */
14629 flp->fnfields.emplace_back ();
14630 fnp = &flp->fnfields.back ();
3da10d80
KS
14631
14632 /* Delay processing of the physname until later. */
9c37b5ae 14633 if (cu->language == language_cplus)
be2daae6
TT
14634 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14635 die, cu);
3da10d80
KS
14636 else
14637 {
1d06ead6 14638 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14639 fnp->physname = physname ? physname : "";
14640 }
14641
c906108c 14642 fnp->type = alloc_type (objfile);
f792889a
DJ
14643 this_type = read_type_die (die, cu);
14644 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14645 {
f792889a 14646 int nparams = TYPE_NFIELDS (this_type);
c906108c 14647
f792889a 14648 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14649 of the method itself (TYPE_CODE_METHOD). */
14650 smash_to_method_type (fnp->type, type,
f792889a
DJ
14651 TYPE_TARGET_TYPE (this_type),
14652 TYPE_FIELDS (this_type),
14653 TYPE_NFIELDS (this_type),
14654 TYPE_VARARGS (this_type));
c906108c
SS
14655
14656 /* Handle static member functions.
c5aa993b 14657 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14658 member functions. G++ helps GDB by marking the first
14659 parameter for non-static member functions (which is the this
14660 pointer) as artificial. We obtain this information from
14661 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14662 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14663 fnp->voffset = VOFFSET_STATIC;
14664 }
14665 else
b98664d3 14666 complaint (_("member function type missing for '%s'"),
3da10d80 14667 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14668
14669 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14670 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14671 fnp->fcontext = die_containing_type (die, cu);
c906108c 14672
3e43a32a
MS
14673 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14674 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14675
14676 /* Get accessibility. */
e142c38c 14677 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14678 if (attr != nullptr)
aead7601 14679 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14680 else
14681 accessibility = dwarf2_default_access_attribute (die, cu);
14682 switch (accessibility)
c906108c 14683 {
60d5a603
JK
14684 case DW_ACCESS_private:
14685 fnp->is_private = 1;
14686 break;
14687 case DW_ACCESS_protected:
14688 fnp->is_protected = 1;
14689 break;
c906108c
SS
14690 }
14691
b02dede2 14692 /* Check for artificial methods. */
e142c38c 14693 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14694 if (attr && DW_UNSND (attr) != 0)
14695 fnp->is_artificial = 1;
14696
e35000a7
TBA
14697 /* Check for defaulted methods. */
14698 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14699 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14700 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14701
14702 /* Check for deleted methods. */
14703 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14704 if (attr != nullptr && DW_UNSND (attr) != 0)
14705 fnp->is_deleted = 1;
14706
7d27a96d
TT
14707 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14708
0d564a31 14709 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14710 function. For older versions of GCC, this is an offset in the
14711 appropriate virtual table, as specified by DW_AT_containing_type.
14712 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14713 to the object address. */
14714
e142c38c 14715 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14716 if (attr != nullptr)
8e19ed76 14717 {
4fc6c0d5 14718 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14719 {
aec5aa8b
TT
14720 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14721 {
14722 /* Old-style GCC. */
14723 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14724 }
14725 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14726 || (DW_BLOCK (attr)->size > 1
14727 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14728 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14729 {
aec5aa8b
TT
14730 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14731 if ((fnp->voffset % cu->header.addr_size) != 0)
14732 dwarf2_complex_location_expr_complaint ();
14733 else
14734 fnp->voffset /= cu->header.addr_size;
14735 fnp->voffset += 2;
14736 }
14737 else
14738 dwarf2_complex_location_expr_complaint ();
14739
14740 if (!fnp->fcontext)
7e993ebf
KS
14741 {
14742 /* If there is no `this' field and no DW_AT_containing_type,
14743 we cannot actually find a base class context for the
14744 vtable! */
14745 if (TYPE_NFIELDS (this_type) == 0
14746 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14747 {
b98664d3 14748 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14749 "function \"%s\" (offset %s)"),
14750 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14751 }
14752 else
14753 {
14754 fnp->fcontext
14755 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14756 }
14757 }
aec5aa8b 14758 }
cd6c91b4 14759 else if (attr->form_is_section_offset ())
8e19ed76 14760 {
4d3c2250 14761 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14762 }
14763 else
14764 {
4d3c2250
KB
14765 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14766 fieldname);
8e19ed76 14767 }
0d564a31 14768 }
d48cc9dd
DJ
14769 else
14770 {
14771 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14772 if (attr && DW_UNSND (attr))
14773 {
14774 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14775 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14776 "but the vtable offset is not specified"),
9d8780f0 14777 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14778 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14779 TYPE_CPLUS_DYNAMIC (type) = 1;
14780 }
14781 }
c906108c
SS
14782}
14783
14784/* Create the vector of member function fields, and attach it to the type. */
14785
14786static void
fba45db2 14787dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14788 struct dwarf2_cu *cu)
c906108c 14789{
b4ba55a1 14790 if (cu->language == language_ada)
a73c6dcd 14791 error (_("unexpected member functions in Ada type"));
b4ba55a1 14792
c906108c
SS
14793 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14794 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14795 TYPE_ALLOC (type,
14796 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14797
be2daae6 14798 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14799 {
be2daae6 14800 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14801 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14802
be2daae6
TT
14803 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14804 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14805 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14806 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14807
14808 for (int k = 0; k < nf.fnfields.size (); ++k)
14809 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14810 }
14811
be2daae6 14812 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14813}
14814
1168df01
JB
14815/* Returns non-zero if NAME is the name of a vtable member in CU's
14816 language, zero otherwise. */
14817static int
14818is_vtable_name (const char *name, struct dwarf2_cu *cu)
14819{
14820 static const char vptr[] = "_vptr";
14821
9c37b5ae
TT
14822 /* Look for the C++ form of the vtable. */
14823 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14824 return 1;
14825
14826 return 0;
14827}
14828
c0dd20ea 14829/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14830 functions, with the ABI-specified layout. If TYPE describes
14831 such a structure, smash it into a member function type.
61049d3b
DJ
14832
14833 GCC shouldn't do this; it should just output pointer to member DIEs.
14834 This is GCC PR debug/28767. */
c0dd20ea 14835
0b92b5bb
TT
14836static void
14837quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14838{
09e2d7c7 14839 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14840
14841 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14842 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14843 return;
c0dd20ea
DJ
14844
14845 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14846 if (TYPE_FIELD_NAME (type, 0) == NULL
14847 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14848 || TYPE_FIELD_NAME (type, 1) == NULL
14849 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14850 return;
c0dd20ea
DJ
14851
14852 /* Find the type of the method. */
0b92b5bb 14853 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14854 if (pfn_type == NULL
14855 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14856 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14857 return;
c0dd20ea
DJ
14858
14859 /* Look for the "this" argument. */
14860 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14861 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14862 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14863 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14864 return;
c0dd20ea 14865
09e2d7c7 14866 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14867 new_type = alloc_type (objfile);
09e2d7c7 14868 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14869 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14870 TYPE_VARARGS (pfn_type));
0b92b5bb 14871 smash_to_methodptr_type (type, new_type);
c0dd20ea 14872}
1168df01 14873
2b4424c3
TT
14874/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14875 appropriate error checking and issuing complaints if there is a
14876 problem. */
14877
14878static ULONGEST
14879get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14880{
14881 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14882
14883 if (attr == nullptr)
14884 return 0;
14885
cd6c91b4 14886 if (!attr->form_is_constant ())
2b4424c3 14887 {
b98664d3 14888 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14889 " - DIE at %s [in module %s]"),
14890 sect_offset_str (die->sect_off),
14891 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14892 return 0;
14893 }
14894
14895 ULONGEST align;
14896 if (attr->form == DW_FORM_sdata)
14897 {
14898 LONGEST val = DW_SND (attr);
14899 if (val < 0)
14900 {
b98664d3 14901 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14902 " - DIE at %s [in module %s]"),
14903 sect_offset_str (die->sect_off),
14904 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14905 return 0;
14906 }
14907 align = val;
14908 }
14909 else
14910 align = DW_UNSND (attr);
14911
14912 if (align == 0)
14913 {
b98664d3 14914 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14915 " - DIE at %s [in module %s]"),
14916 sect_offset_str (die->sect_off),
14917 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14918 return 0;
14919 }
14920 if ((align & (align - 1)) != 0)
14921 {
b98664d3 14922 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14923 " - DIE at %s [in module %s]"),
14924 sect_offset_str (die->sect_off),
14925 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14926 return 0;
14927 }
14928
14929 return align;
14930}
14931
14932/* If the DIE has a DW_AT_alignment attribute, use its value to set
14933 the alignment for TYPE. */
14934
14935static void
14936maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14937 struct type *type)
14938{
14939 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14940 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14941 " - DIE at %s [in module %s]"),
14942 sect_offset_str (die->sect_off),
14943 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14944}
685b1105 14945
e35000a7
TBA
14946/* Check if the given VALUE is a valid enum dwarf_calling_convention
14947 constant for a type, according to DWARF5 spec, Table 5.5. */
14948
14949static bool
14950is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14951{
14952 switch (value)
14953 {
14954 case DW_CC_normal:
14955 case DW_CC_pass_by_reference:
14956 case DW_CC_pass_by_value:
14957 return true;
14958
14959 default:
14960 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14961 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14962 return false;
14963 }
14964}
14965
d0922fcf
TBA
14966/* Check if the given VALUE is a valid enum dwarf_calling_convention
14967 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14968 also according to GNU-specific values (see include/dwarf2.h). */
14969
14970static bool
14971is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14972{
14973 switch (value)
14974 {
14975 case DW_CC_normal:
14976 case DW_CC_program:
14977 case DW_CC_nocall:
14978 return true;
14979
14980 case DW_CC_GNU_renesas_sh:
14981 case DW_CC_GNU_borland_fastcall_i386:
14982 case DW_CC_GDB_IBM_OpenCL:
14983 return true;
14984
14985 default:
14986 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14987 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
14988 return false;
14989 }
14990}
14991
c906108c 14992/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14993 (definition) to create a type for the structure or union. Fill in
14994 the type's name and general properties; the members will not be
83655187
DE
14995 processed until process_structure_scope. A symbol table entry for
14996 the type will also not be done until process_structure_scope (assuming
14997 the type has a name).
c906108c 14998
c767944b
DJ
14999 NOTE: we need to call these functions regardless of whether or not the
15000 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15001 structure or union. This gets the type entered into our set of
83655187 15002 user defined types. */
c906108c 15003
f792889a 15004static struct type *
134d01f1 15005read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15006{
518817b3 15007 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15008 struct type *type;
15009 struct attribute *attr;
15d034d0 15010 const char *name;
c906108c 15011
348e048f
DE
15012 /* If the definition of this type lives in .debug_types, read that type.
15013 Don't follow DW_AT_specification though, that will take us back up
15014 the chain and we want to go down. */
45e58e77 15015 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15016 if (attr != nullptr)
348e048f 15017 {
ac9ec31b 15018 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15019
ac9ec31b 15020 /* The type's CU may not be the same as CU.
02142a6c 15021 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15022 return set_die_type (die, type, cu);
15023 }
15024
c0dd20ea 15025 type = alloc_type (objfile);
c906108c 15026 INIT_CPLUS_SPECIFIC (type);
93311388 15027
39cbfefa
DJ
15028 name = dwarf2_name (die, cu);
15029 if (name != NULL)
c906108c 15030 {
987504bb 15031 if (cu->language == language_cplus
c44af4eb
TT
15032 || cu->language == language_d
15033 || cu->language == language_rust)
63d06c5c 15034 {
15d034d0 15035 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15036
15037 /* dwarf2_full_name might have already finished building the DIE's
15038 type. If so, there is no need to continue. */
15039 if (get_die_type (die, cu) != NULL)
15040 return get_die_type (die, cu);
15041
e86ca25f 15042 TYPE_NAME (type) = full_name;
63d06c5c
DC
15043 }
15044 else
15045 {
d8151005
DJ
15046 /* The name is already allocated along with this objfile, so
15047 we don't need to duplicate it for the type. */
e86ca25f 15048 TYPE_NAME (type) = name;
63d06c5c 15049 }
c906108c
SS
15050 }
15051
15052 if (die->tag == DW_TAG_structure_type)
15053 {
15054 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15055 }
15056 else if (die->tag == DW_TAG_union_type)
15057 {
15058 TYPE_CODE (type) = TYPE_CODE_UNION;
15059 }
2ddeaf8a
TT
15060 else if (die->tag == DW_TAG_variant_part)
15061 {
15062 TYPE_CODE (type) = TYPE_CODE_UNION;
15063 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15064 }
c906108c
SS
15065 else
15066 {
4753d33b 15067 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15068 }
15069
0cc2414c
TT
15070 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15071 TYPE_DECLARED_CLASS (type) = 1;
15072
e35000a7
TBA
15073 /* Store the calling convention in the type if it's available in
15074 the die. Otherwise the calling convention remains set to
15075 the default value DW_CC_normal. */
15076 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15077 if (attr != nullptr
15078 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15079 {
15080 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15081 TYPE_CPLUS_CALLING_CONVENTION (type)
15082 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15083 }
15084
e142c38c 15085 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15086 if (attr != nullptr)
c906108c 15087 {
cd6c91b4 15088 if (attr->form_is_constant ())
155bfbd3
JB
15089 TYPE_LENGTH (type) = DW_UNSND (attr);
15090 else
15091 {
15092 /* For the moment, dynamic type sizes are not supported
15093 by GDB's struct type. The actual size is determined
15094 on-demand when resolving the type of a given object,
15095 so set the type's length to zero for now. Otherwise,
15096 we record an expression as the length, and that expression
15097 could lead to a very large value, which could eventually
15098 lead to us trying to allocate that much memory when creating
15099 a value of that type. */
15100 TYPE_LENGTH (type) = 0;
15101 }
c906108c
SS
15102 }
15103 else
15104 {
15105 TYPE_LENGTH (type) = 0;
15106 }
15107
2b4424c3
TT
15108 maybe_set_alignment (cu, die, type);
15109
5230b05a 15110 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15111 {
5230b05a
WT
15112 /* ICC<14 does not output the required DW_AT_declaration on
15113 incomplete types, but gives them a size of zero. */
422b1cb0 15114 TYPE_STUB (type) = 1;
685b1105
JK
15115 }
15116 else
15117 TYPE_STUB_SUPPORTED (type) = 1;
15118
dc718098 15119 if (die_is_declaration (die, cu))
876cecd0 15120 TYPE_STUB (type) = 1;
a6c727b2
DJ
15121 else if (attr == NULL && die->child == NULL
15122 && producer_is_realview (cu->producer))
15123 /* RealView does not output the required DW_AT_declaration
15124 on incomplete types. */
15125 TYPE_STUB (type) = 1;
dc718098 15126
c906108c
SS
15127 /* We need to add the type field to the die immediately so we don't
15128 infinitely recurse when dealing with pointers to the structure
0963b4bd 15129 type within the structure itself. */
1c379e20 15130 set_die_type (die, type, cu);
c906108c 15131
7e314c57
JK
15132 /* set_die_type should be already done. */
15133 set_descriptive_type (type, die, cu);
15134
c767944b
DJ
15135 return type;
15136}
15137
2ddeaf8a
TT
15138/* A helper for process_structure_scope that handles a single member
15139 DIE. */
15140
15141static void
15142handle_struct_member_die (struct die_info *child_die, struct type *type,
15143 struct field_info *fi,
15144 std::vector<struct symbol *> *template_args,
15145 struct dwarf2_cu *cu)
15146{
15147 if (child_die->tag == DW_TAG_member
15148 || child_die->tag == DW_TAG_variable
15149 || child_die->tag == DW_TAG_variant_part)
15150 {
15151 /* NOTE: carlton/2002-11-05: A C++ static data member
15152 should be a DW_TAG_member that is a declaration, but
15153 all versions of G++ as of this writing (so through at
15154 least 3.2.1) incorrectly generate DW_TAG_variable
15155 tags for them instead. */
15156 dwarf2_add_field (fi, child_die, cu);
15157 }
15158 else if (child_die->tag == DW_TAG_subprogram)
15159 {
15160 /* Rust doesn't have member functions in the C++ sense.
15161 However, it does emit ordinary functions as children
15162 of a struct DIE. */
15163 if (cu->language == language_rust)
15164 read_func_scope (child_die, cu);
15165 else
15166 {
15167 /* C++ member function. */
15168 dwarf2_add_member_fn (fi, child_die, type, cu);
15169 }
15170 }
15171 else if (child_die->tag == DW_TAG_inheritance)
15172 {
15173 /* C++ base class field. */
15174 dwarf2_add_field (fi, child_die, cu);
15175 }
15176 else if (type_can_define_types (child_die))
15177 dwarf2_add_type_defn (fi, child_die, cu);
15178 else if (child_die->tag == DW_TAG_template_type_param
15179 || child_die->tag == DW_TAG_template_value_param)
15180 {
15181 struct symbol *arg = new_symbol (child_die, NULL, cu);
15182
15183 if (arg != NULL)
15184 template_args->push_back (arg);
15185 }
15186 else if (child_die->tag == DW_TAG_variant)
15187 {
15188 /* In a variant we want to get the discriminant and also add a
15189 field for our sole member child. */
15190 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15191
bde09ab7 15192 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15193 variant_child != NULL;
15194 variant_child = sibling_die (variant_child))
15195 {
15196 if (variant_child->tag == DW_TAG_member)
15197 {
15198 handle_struct_member_die (variant_child, type, fi,
15199 template_args, cu);
15200 /* Only handle the one. */
15201 break;
15202 }
15203 }
15204
15205 /* We don't handle this but we might as well report it if we see
15206 it. */
15207 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15208 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15209 " - DIE at %s [in module %s]"),
15210 sect_offset_str (child_die->sect_off),
15211 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15212
15213 /* The first field was just added, so we can stash the
15214 discriminant there. */
be2daae6 15215 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15216 if (discr == NULL)
be2daae6 15217 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15218 else
be2daae6 15219 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15220 }
15221}
15222
c767944b
DJ
15223/* Finish creating a structure or union type, including filling in
15224 its members and creating a symbol for it. */
15225
15226static void
15227process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15228{
518817b3 15229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15230 struct die_info *child_die;
c767944b
DJ
15231 struct type *type;
15232
15233 type = get_die_type (die, cu);
15234 if (type == NULL)
15235 type = read_structure_type (die, cu);
15236
2ddeaf8a
TT
15237 /* When reading a DW_TAG_variant_part, we need to notice when we
15238 read the discriminant member, so we can record it later in the
15239 discriminant_info. */
15240 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15241 sect_offset discr_offset {};
3e1d3d8c 15242 bool has_template_parameters = false;
2ddeaf8a
TT
15243
15244 if (is_variant_part)
15245 {
15246 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15247 if (discr == NULL)
15248 {
15249 /* Maybe it's a univariant form, an extension we support.
15250 In this case arrange not to check the offset. */
15251 is_variant_part = false;
15252 }
cd6c91b4 15253 else if (discr->form_is_ref ())
2ddeaf8a
TT
15254 {
15255 struct dwarf2_cu *target_cu = cu;
15256 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15257
15258 discr_offset = target_die->sect_off;
15259 }
15260 else
15261 {
b98664d3 15262 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15263 " - DIE at %s [in module %s]"),
15264 sect_offset_str (die->sect_off),
15265 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15266 is_variant_part = false;
15267 }
15268 }
15269
e142c38c 15270 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15271 {
15272 struct field_info fi;
2f4732b0 15273 std::vector<struct symbol *> template_args;
c906108c 15274
639d11d3 15275 child_die = die->child;
c906108c
SS
15276
15277 while (child_die && child_die->tag)
15278 {
2ddeaf8a 15279 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15280
2ddeaf8a 15281 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15282 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15283
c906108c
SS
15284 child_die = sibling_die (child_die);
15285 }
15286
34eaf542 15287 /* Attach template arguments to type. */
2f4732b0 15288 if (!template_args.empty ())
34eaf542 15289 {
3e1d3d8c 15290 has_template_parameters = true;
34eaf542 15291 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15292 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15293 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15294 = XOBNEWVEC (&objfile->objfile_obstack,
15295 struct symbol *,
15296 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15297 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15298 template_args.data (),
34eaf542
TT
15299 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15300 * sizeof (struct symbol *)));
34eaf542
TT
15301 }
15302
c906108c
SS
15303 /* Attach fields and member functions to the type. */
15304 if (fi.nfields)
e7c27a73 15305 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15306 if (!fi.fnfieldlists.empty ())
c906108c 15307 {
e7c27a73 15308 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15309
c5aa993b 15310 /* Get the type which refers to the base class (possibly this
c906108c 15311 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15312 class from the DW_AT_containing_type attribute. This use of
15313 DW_AT_containing_type is a GNU extension. */
c906108c 15314
e142c38c 15315 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15316 {
e7c27a73 15317 struct type *t = die_containing_type (die, cu);
c906108c 15318
ae6ae975 15319 set_type_vptr_basetype (type, t);
c906108c
SS
15320 if (type == t)
15321 {
c906108c
SS
15322 int i;
15323
15324 /* Our own class provides vtbl ptr. */
15325 for (i = TYPE_NFIELDS (t) - 1;
15326 i >= TYPE_N_BASECLASSES (t);
15327 --i)
15328 {
0d5cff50 15329 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15330
1168df01 15331 if (is_vtable_name (fieldname, cu))
c906108c 15332 {
ae6ae975 15333 set_type_vptr_fieldno (type, i);
c906108c
SS
15334 break;
15335 }
15336 }
15337
15338 /* Complain if virtual function table field not found. */
15339 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15340 complaint (_("virtual function table pointer "
3e43a32a 15341 "not found when defining class '%s'"),
e86ca25f 15342 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15343 }
15344 else
15345 {
ae6ae975 15346 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15347 }
15348 }
f6235d4c 15349 else if (cu->producer
61012eef 15350 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15351 {
15352 /* The IBM XLC compiler does not provide direct indication
15353 of the containing type, but the vtable pointer is
15354 always named __vfp. */
15355
15356 int i;
15357
15358 for (i = TYPE_NFIELDS (type) - 1;
15359 i >= TYPE_N_BASECLASSES (type);
15360 --i)
15361 {
15362 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15363 {
ae6ae975
DE
15364 set_type_vptr_fieldno (type, i);
15365 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15366 break;
15367 }
15368 }
15369 }
c906108c 15370 }
98751a41
JK
15371
15372 /* Copy fi.typedef_field_list linked list elements content into the
15373 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15374 if (!fi.typedef_field_list.empty ())
98751a41 15375 {
be2daae6 15376 int count = fi.typedef_field_list.size ();
98751a41 15377
a0d7a4ff 15378 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15379 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15380 = ((struct decl_field *)
be2daae6
TT
15381 TYPE_ALLOC (type,
15382 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15383 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15384
be2daae6
TT
15385 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15386 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15387 }
c767944b 15388
883fd55a
KS
15389 /* Copy fi.nested_types_list linked list elements content into the
15390 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15391 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15392 {
be2daae6 15393 int count = fi.nested_types_list.size ();
883fd55a
KS
15394
15395 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15396 TYPE_NESTED_TYPES_ARRAY (type)
15397 = ((struct decl_field *)
be2daae6
TT
15398 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15399 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15400
be2daae6
TT
15401 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15402 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15403 }
c906108c 15404 }
63d06c5c 15405
bb5ed363 15406 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15407 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15408 cu->rust_unions.push_back (type);
0b92b5bb 15409
90aeadfc
DC
15410 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15411 snapshots) has been known to create a die giving a declaration
15412 for a class that has, as a child, a die giving a definition for a
15413 nested class. So we have to process our children even if the
15414 current die is a declaration. Normally, of course, a declaration
15415 won't have any children at all. */
134d01f1 15416
ca040673
DE
15417 child_die = die->child;
15418
90aeadfc
DC
15419 while (child_die != NULL && child_die->tag)
15420 {
15421 if (child_die->tag == DW_TAG_member
15422 || child_die->tag == DW_TAG_variable
34eaf542
TT
15423 || child_die->tag == DW_TAG_inheritance
15424 || child_die->tag == DW_TAG_template_value_param
15425 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15426 {
90aeadfc 15427 /* Do nothing. */
134d01f1 15428 }
90aeadfc
DC
15429 else
15430 process_die (child_die, cu);
134d01f1 15431
90aeadfc 15432 child_die = sibling_die (child_die);
134d01f1
DJ
15433 }
15434
fa4028e9
JB
15435 /* Do not consider external references. According to the DWARF standard,
15436 these DIEs are identified by the fact that they have no byte_size
15437 attribute, and a declaration attribute. */
15438 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15439 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15440 {
15441 struct symbol *sym = new_symbol (die, type, cu);
15442
15443 if (has_template_parameters)
15444 {
a776957c
TT
15445 struct symtab *symtab;
15446 if (sym != nullptr)
15447 symtab = symbol_symtab (sym);
15448 else if (cu->line_header != nullptr)
15449 {
15450 /* Any related symtab will do. */
15451 symtab
7ba99d21 15452 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15453 }
15454 else
15455 {
15456 symtab = nullptr;
15457 complaint (_("could not find suitable "
15458 "symtab for template parameter"
15459 " - DIE at %s [in module %s]"),
15460 sect_offset_str (die->sect_off),
15461 objfile_name (objfile));
15462 }
15463
15464 if (symtab != nullptr)
15465 {
15466 /* Make sure that the symtab is set on the new symbols.
15467 Even though they don't appear in this symtab directly,
15468 other parts of gdb assume that symbols do, and this is
15469 reasonably true. */
15470 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15471 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15472 }
3e1d3d8c
TT
15473 }
15474 }
134d01f1
DJ
15475}
15476
55426c9d
JB
15477/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15478 update TYPE using some information only available in DIE's children. */
15479
15480static void
15481update_enumeration_type_from_children (struct die_info *die,
15482 struct type *type,
15483 struct dwarf2_cu *cu)
15484{
60f7655a 15485 struct die_info *child_die;
55426c9d
JB
15486 int unsigned_enum = 1;
15487 int flag_enum = 1;
15488 ULONGEST mask = 0;
55426c9d 15489
8268c778 15490 auto_obstack obstack;
55426c9d 15491
60f7655a
DE
15492 for (child_die = die->child;
15493 child_die != NULL && child_die->tag;
15494 child_die = sibling_die (child_die))
55426c9d
JB
15495 {
15496 struct attribute *attr;
15497 LONGEST value;
15498 const gdb_byte *bytes;
15499 struct dwarf2_locexpr_baton *baton;
15500 const char *name;
60f7655a 15501
55426c9d
JB
15502 if (child_die->tag != DW_TAG_enumerator)
15503 continue;
15504
15505 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15506 if (attr == NULL)
15507 continue;
15508
15509 name = dwarf2_name (child_die, cu);
15510 if (name == NULL)
15511 name = "<anonymous enumerator>";
15512
15513 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15514 &value, &bytes, &baton);
15515 if (value < 0)
15516 {
15517 unsigned_enum = 0;
15518 flag_enum = 0;
15519 }
15520 else if ((mask & value) != 0)
15521 flag_enum = 0;
15522 else
15523 mask |= value;
15524
15525 /* If we already know that the enum type is neither unsigned, nor
15526 a flag type, no need to look at the rest of the enumerates. */
15527 if (!unsigned_enum && !flag_enum)
15528 break;
55426c9d
JB
15529 }
15530
15531 if (unsigned_enum)
15532 TYPE_UNSIGNED (type) = 1;
15533 if (flag_enum)
15534 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15535}
15536
134d01f1
DJ
15537/* Given a DW_AT_enumeration_type die, set its type. We do not
15538 complete the type's fields yet, or create any symbols. */
c906108c 15539
f792889a 15540static struct type *
134d01f1 15541read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15542{
518817b3 15543 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15544 struct type *type;
c906108c 15545 struct attribute *attr;
0114d602 15546 const char *name;
134d01f1 15547
348e048f
DE
15548 /* If the definition of this type lives in .debug_types, read that type.
15549 Don't follow DW_AT_specification though, that will take us back up
15550 the chain and we want to go down. */
45e58e77 15551 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15552 if (attr != nullptr)
348e048f 15553 {
ac9ec31b 15554 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15555
ac9ec31b 15556 /* The type's CU may not be the same as CU.
02142a6c 15557 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15558 return set_die_type (die, type, cu);
15559 }
15560
c906108c
SS
15561 type = alloc_type (objfile);
15562
15563 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15564 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15565 if (name != NULL)
e86ca25f 15566 TYPE_NAME (type) = name;
c906108c 15567
0626fc76
TT
15568 attr = dwarf2_attr (die, DW_AT_type, cu);
15569 if (attr != NULL)
15570 {
15571 struct type *underlying_type = die_type (die, cu);
15572
15573 TYPE_TARGET_TYPE (type) = underlying_type;
15574 }
15575
e142c38c 15576 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15577 if (attr != nullptr)
c906108c
SS
15578 {
15579 TYPE_LENGTH (type) = DW_UNSND (attr);
15580 }
15581 else
15582 {
15583 TYPE_LENGTH (type) = 0;
15584 }
15585
2b4424c3
TT
15586 maybe_set_alignment (cu, die, type);
15587
137033e9
JB
15588 /* The enumeration DIE can be incomplete. In Ada, any type can be
15589 declared as private in the package spec, and then defined only
15590 inside the package body. Such types are known as Taft Amendment
15591 Types. When another package uses such a type, an incomplete DIE
15592 may be generated by the compiler. */
02eb380e 15593 if (die_is_declaration (die, cu))
876cecd0 15594 TYPE_STUB (type) = 1;
02eb380e 15595
0626fc76
TT
15596 /* Finish the creation of this type by using the enum's children.
15597 We must call this even when the underlying type has been provided
15598 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15599 update_enumeration_type_from_children (die, type, cu);
15600
0626fc76
TT
15601 /* If this type has an underlying type that is not a stub, then we
15602 may use its attributes. We always use the "unsigned" attribute
15603 in this situation, because ordinarily we guess whether the type
15604 is unsigned -- but the guess can be wrong and the underlying type
15605 can tell us the reality. However, we defer to a local size
15606 attribute if one exists, because this lets the compiler override
15607 the underlying type if needed. */
15608 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15609 {
15610 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15611 if (TYPE_LENGTH (type) == 0)
15612 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15613 if (TYPE_RAW_ALIGN (type) == 0
15614 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15615 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15616 }
15617
3d567982
TT
15618 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15619
f792889a 15620 return set_die_type (die, type, cu);
134d01f1
DJ
15621}
15622
15623/* Given a pointer to a die which begins an enumeration, process all
15624 the dies that define the members of the enumeration, and create the
15625 symbol for the enumeration type.
15626
15627 NOTE: We reverse the order of the element list. */
15628
15629static void
15630process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15631{
f792889a 15632 struct type *this_type;
134d01f1 15633
f792889a
DJ
15634 this_type = get_die_type (die, cu);
15635 if (this_type == NULL)
15636 this_type = read_enumeration_type (die, cu);
9dc481d3 15637
639d11d3 15638 if (die->child != NULL)
c906108c 15639 {
9dc481d3
DE
15640 struct die_info *child_die;
15641 struct symbol *sym;
43816ebc 15642 std::vector<struct field> fields;
15d034d0 15643 const char *name;
9dc481d3 15644
639d11d3 15645 child_die = die->child;
c906108c
SS
15646 while (child_die && child_die->tag)
15647 {
15648 if (child_die->tag != DW_TAG_enumerator)
15649 {
e7c27a73 15650 process_die (child_die, cu);
c906108c
SS
15651 }
15652 else
15653 {
39cbfefa
DJ
15654 name = dwarf2_name (child_die, cu);
15655 if (name)
c906108c 15656 {
f792889a 15657 sym = new_symbol (child_die, this_type, cu);
c906108c 15658
43816ebc
TT
15659 fields.emplace_back ();
15660 struct field &field = fields.back ();
c906108c 15661
43816ebc
TT
15662 FIELD_NAME (field) = sym->linkage_name ();
15663 FIELD_TYPE (field) = NULL;
15664 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15665 FIELD_BITSIZE (field) = 0;
c906108c
SS
15666 }
15667 }
15668
15669 child_die = sibling_die (child_die);
15670 }
15671
43816ebc 15672 if (!fields.empty ())
c906108c 15673 {
43816ebc 15674 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15675 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15676 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15677 memcpy (TYPE_FIELDS (this_type), fields.data (),
15678 sizeof (struct field) * fields.size ());
c906108c 15679 }
c906108c 15680 }
134d01f1 15681
6c83ed52
TT
15682 /* If we are reading an enum from a .debug_types unit, and the enum
15683 is a declaration, and the enum is not the signatured type in the
15684 unit, then we do not want to add a symbol for it. Adding a
15685 symbol would in some cases obscure the true definition of the
15686 enum, giving users an incomplete type when the definition is
15687 actually available. Note that we do not want to do this for all
15688 enums which are just declarations, because C++0x allows forward
15689 enum declarations. */
3019eac3 15690 if (cu->per_cu->is_debug_types
6c83ed52
TT
15691 && die_is_declaration (die, cu))
15692 {
52dc124a 15693 struct signatured_type *sig_type;
6c83ed52 15694
c0f78cd4 15695 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15696 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15697 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15698 return;
15699 }
15700
f792889a 15701 new_symbol (die, this_type, cu);
c906108c
SS
15702}
15703
15704/* Extract all information from a DW_TAG_array_type DIE and put it in
15705 the DIE's type field. For now, this only handles one dimensional
15706 arrays. */
15707
f792889a 15708static struct type *
e7c27a73 15709read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15710{
518817b3 15711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15712 struct die_info *child_die;
7e314c57 15713 struct type *type;
c906108c 15714 struct type *element_type, *range_type, *index_type;
c906108c 15715 struct attribute *attr;
15d034d0 15716 const char *name;
a405673c 15717 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15718 unsigned int bit_stride = 0;
c906108c 15719
e7c27a73 15720 element_type = die_type (die, cu);
c906108c 15721
7e314c57
JK
15722 /* The die_type call above may have already set the type for this DIE. */
15723 type = get_die_type (die, cu);
15724 if (type)
15725 return type;
15726
dc53a7ad
JB
15727 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15728 if (attr != NULL)
a405673c
JB
15729 {
15730 int stride_ok;
09ba997f 15731 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15732
15733 byte_stride_prop
15734 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15735 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15736 prop_type);
a405673c
JB
15737 if (!stride_ok)
15738 {
b98664d3 15739 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15740 " - DIE at %s [in module %s]"),
15741 sect_offset_str (die->sect_off),
518817b3 15742 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15743 /* Ignore this attribute. We will likely not be able to print
15744 arrays of this type correctly, but there is little we can do
15745 to help if we cannot read the attribute's value. */
15746 byte_stride_prop = NULL;
15747 }
15748 }
dc53a7ad
JB
15749
15750 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15751 if (attr != NULL)
15752 bit_stride = DW_UNSND (attr);
15753
c906108c
SS
15754 /* Irix 6.2 native cc creates array types without children for
15755 arrays with unspecified length. */
639d11d3 15756 if (die->child == NULL)
c906108c 15757 {
46bf5051 15758 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15759 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15760 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15761 byte_stride_prop, bit_stride);
f792889a 15762 return set_die_type (die, type, cu);
c906108c
SS
15763 }
15764
791afaa2 15765 std::vector<struct type *> range_types;
639d11d3 15766 child_die = die->child;
c906108c
SS
15767 while (child_die && child_die->tag)
15768 {
15769 if (child_die->tag == DW_TAG_subrange_type)
15770 {
f792889a 15771 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15772
f792889a 15773 if (child_type != NULL)
a02abb62 15774 {
0963b4bd
MS
15775 /* The range type was succesfully read. Save it for the
15776 array type creation. */
791afaa2 15777 range_types.push_back (child_type);
a02abb62 15778 }
c906108c
SS
15779 }
15780 child_die = sibling_die (child_die);
15781 }
15782
15783 /* Dwarf2 dimensions are output from left to right, create the
15784 necessary array types in backwards order. */
7ca2d3a3 15785
c906108c 15786 type = element_type;
7ca2d3a3
DL
15787
15788 if (read_array_order (die, cu) == DW_ORD_col_major)
15789 {
15790 int i = 0;
9a619af0 15791
791afaa2 15792 while (i < range_types.size ())
dc53a7ad 15793 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15794 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15795 }
15796 else
15797 {
791afaa2 15798 size_t ndim = range_types.size ();
7ca2d3a3 15799 while (ndim-- > 0)
dc53a7ad 15800 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15801 byte_stride_prop, bit_stride);
7ca2d3a3 15802 }
c906108c 15803
f5f8a009
EZ
15804 /* Understand Dwarf2 support for vector types (like they occur on
15805 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15806 array type. This is not part of the Dwarf2/3 standard yet, but a
15807 custom vendor extension. The main difference between a regular
15808 array and the vector variant is that vectors are passed by value
15809 to functions. */
e142c38c 15810 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15811 if (attr != nullptr)
ea37ba09 15812 make_vector_type (type);
f5f8a009 15813
dbc98a8b
KW
15814 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15815 implementation may choose to implement triple vectors using this
15816 attribute. */
15817 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15818 if (attr != nullptr)
dbc98a8b
KW
15819 {
15820 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15821 TYPE_LENGTH (type) = DW_UNSND (attr);
15822 else
b98664d3 15823 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15824 "than the total size of elements"));
dbc98a8b
KW
15825 }
15826
39cbfefa
DJ
15827 name = dwarf2_name (die, cu);
15828 if (name)
15829 TYPE_NAME (type) = name;
6e70227d 15830
2b4424c3
TT
15831 maybe_set_alignment (cu, die, type);
15832
0963b4bd 15833 /* Install the type in the die. */
7e314c57
JK
15834 set_die_type (die, type, cu);
15835
15836 /* set_die_type should be already done. */
b4ba55a1
JB
15837 set_descriptive_type (type, die, cu);
15838
7e314c57 15839 return type;
c906108c
SS
15840}
15841
7ca2d3a3 15842static enum dwarf_array_dim_ordering
6e70227d 15843read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15844{
15845 struct attribute *attr;
15846
15847 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15848
435d3d88 15849 if (attr != nullptr)
aead7601 15850 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15851
0963b4bd
MS
15852 /* GNU F77 is a special case, as at 08/2004 array type info is the
15853 opposite order to the dwarf2 specification, but data is still
15854 laid out as per normal fortran.
7ca2d3a3 15855
0963b4bd
MS
15856 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15857 version checking. */
7ca2d3a3 15858
905e0470
PM
15859 if (cu->language == language_fortran
15860 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15861 {
15862 return DW_ORD_row_major;
15863 }
15864
6e70227d 15865 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15866 {
15867 case array_column_major:
15868 return DW_ORD_col_major;
15869 case array_row_major:
15870 default:
15871 return DW_ORD_row_major;
15872 };
15873}
15874
72019c9c 15875/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15876 the DIE's type field. */
72019c9c 15877
f792889a 15878static struct type *
72019c9c
GM
15879read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15880{
7e314c57
JK
15881 struct type *domain_type, *set_type;
15882 struct attribute *attr;
f792889a 15883
7e314c57
JK
15884 domain_type = die_type (die, cu);
15885
15886 /* The die_type call above may have already set the type for this DIE. */
15887 set_type = get_die_type (die, cu);
15888 if (set_type)
15889 return set_type;
15890
15891 set_type = create_set_type (NULL, domain_type);
15892
15893 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15894 if (attr != nullptr)
d09039dd 15895 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15896
2b4424c3
TT
15897 maybe_set_alignment (cu, die, set_type);
15898
f792889a 15899 return set_die_type (die, set_type, cu);
72019c9c 15900}
7ca2d3a3 15901
0971de02
TT
15902/* A helper for read_common_block that creates a locexpr baton.
15903 SYM is the symbol which we are marking as computed.
15904 COMMON_DIE is the DIE for the common block.
15905 COMMON_LOC is the location expression attribute for the common
15906 block itself.
15907 MEMBER_LOC is the location expression attribute for the particular
15908 member of the common block that we are processing.
15909 CU is the CU from which the above come. */
15910
15911static void
15912mark_common_block_symbol_computed (struct symbol *sym,
15913 struct die_info *common_die,
15914 struct attribute *common_loc,
15915 struct attribute *member_loc,
15916 struct dwarf2_cu *cu)
15917{
518817b3
SM
15918 struct dwarf2_per_objfile *dwarf2_per_objfile
15919 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15920 struct objfile *objfile = dwarf2_per_objfile->objfile;
15921 struct dwarf2_locexpr_baton *baton;
15922 gdb_byte *ptr;
15923 unsigned int cu_off;
15924 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15925 LONGEST offset = 0;
15926
15927 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15928 gdb_assert (common_loc->form_is_block ());
15929 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15930 || member_loc->form_is_constant ());
0971de02 15931
8d749320 15932 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15933 baton->per_cu = cu->per_cu;
15934 gdb_assert (baton->per_cu);
15935
15936 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15937
cd6c91b4 15938 if (member_loc->form_is_constant ())
0971de02
TT
15939 {
15940 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15941 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15942 }
15943 else
15944 baton->size += DW_BLOCK (member_loc)->size;
15945
224c3ddb 15946 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15947 baton->data = ptr;
15948
15949 *ptr++ = DW_OP_call4;
9c541725 15950 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15951 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15952 ptr += 4;
15953
cd6c91b4 15954 if (member_loc->form_is_constant ())
0971de02
TT
15955 {
15956 *ptr++ = DW_OP_addr;
15957 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15958 ptr += cu->header.addr_size;
15959 }
15960 else
15961 {
15962 /* We have to copy the data here, because DW_OP_call4 will only
15963 use a DW_AT_location attribute. */
15964 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15965 ptr += DW_BLOCK (member_loc)->size;
15966 }
15967
15968 *ptr++ = DW_OP_plus;
15969 gdb_assert (ptr - baton->data == baton->size);
15970
0971de02 15971 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15972 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15973}
15974
4357ac6c
TT
15975/* Create appropriate locally-scoped variables for all the
15976 DW_TAG_common_block entries. Also create a struct common_block
15977 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 15978 is used to separate the common blocks name namespace from regular
4357ac6c 15979 variable names. */
c906108c
SS
15980
15981static void
e7c27a73 15982read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15983{
0971de02
TT
15984 struct attribute *attr;
15985
15986 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 15987 if (attr != nullptr)
0971de02
TT
15988 {
15989 /* Support the .debug_loc offsets. */
4fc6c0d5 15990 if (attr->form_is_block ())
0971de02
TT
15991 {
15992 /* Ok. */
15993 }
cd6c91b4 15994 else if (attr->form_is_section_offset ())
0971de02
TT
15995 {
15996 dwarf2_complex_location_expr_complaint ();
15997 attr = NULL;
15998 }
15999 else
16000 {
16001 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16002 "common block member");
16003 attr = NULL;
16004 }
16005 }
16006
639d11d3 16007 if (die->child != NULL)
c906108c 16008 {
518817b3 16009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16010 struct die_info *child_die;
16011 size_t n_entries = 0, size;
16012 struct common_block *common_block;
16013 struct symbol *sym;
74ac6d43 16014
4357ac6c
TT
16015 for (child_die = die->child;
16016 child_die && child_die->tag;
16017 child_die = sibling_die (child_die))
16018 ++n_entries;
16019
16020 size = (sizeof (struct common_block)
16021 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16022 common_block
16023 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16024 size);
4357ac6c
TT
16025 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16026 common_block->n_entries = 0;
16027
16028 for (child_die = die->child;
16029 child_die && child_die->tag;
16030 child_die = sibling_die (child_die))
16031 {
16032 /* Create the symbol in the DW_TAG_common_block block in the current
16033 symbol scope. */
e7c27a73 16034 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16035 if (sym != NULL)
16036 {
16037 struct attribute *member_loc;
16038
16039 common_block->contents[common_block->n_entries++] = sym;
16040
16041 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16042 cu);
16043 if (member_loc)
16044 {
16045 /* GDB has handled this for a long time, but it is
16046 not specified by DWARF. It seems to have been
16047 emitted by gfortran at least as recently as:
16048 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16049 complaint (_("Variable in common block has "
0971de02 16050 "DW_AT_data_member_location "
9d8780f0
SM
16051 "- DIE at %s [in module %s]"),
16052 sect_offset_str (child_die->sect_off),
518817b3 16053 objfile_name (objfile));
0971de02 16054
cd6c91b4 16055 if (member_loc->form_is_section_offset ())
0971de02 16056 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16057 else if (member_loc->form_is_constant ()
4fc6c0d5 16058 || member_loc->form_is_block ())
0971de02 16059 {
435d3d88 16060 if (attr != nullptr)
0971de02
TT
16061 mark_common_block_symbol_computed (sym, die, attr,
16062 member_loc, cu);
16063 }
16064 else
16065 dwarf2_complex_location_expr_complaint ();
16066 }
16067 }
c906108c 16068 }
4357ac6c
TT
16069
16070 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16071 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16072 }
16073}
16074
0114d602 16075/* Create a type for a C++ namespace. */
d9fa45fe 16076
0114d602
DJ
16077static struct type *
16078read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16079{
518817b3 16080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16081 const char *previous_prefix, *name;
9219021c 16082 int is_anonymous;
0114d602
DJ
16083 struct type *type;
16084
16085 /* For extensions, reuse the type of the original namespace. */
16086 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16087 {
16088 struct die_info *ext_die;
16089 struct dwarf2_cu *ext_cu = cu;
9a619af0 16090
0114d602
DJ
16091 ext_die = dwarf2_extension (die, &ext_cu);
16092 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16093
16094 /* EXT_CU may not be the same as CU.
02142a6c 16095 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16096 return set_die_type (die, type, cu);
16097 }
9219021c 16098
e142c38c 16099 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16100
16101 /* Now build the name of the current namespace. */
16102
0114d602
DJ
16103 previous_prefix = determine_prefix (die, cu);
16104 if (previous_prefix[0] != '\0')
16105 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16106 previous_prefix, name, 0, cu);
0114d602
DJ
16107
16108 /* Create the type. */
19f392bc 16109 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16110
60531b24 16111 return set_die_type (die, type, cu);
0114d602
DJ
16112}
16113
22cee43f 16114/* Read a namespace scope. */
0114d602
DJ
16115
16116static void
16117read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16118{
518817b3 16119 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16120 int is_anonymous;
9219021c 16121
5c4e30ca
DC
16122 /* Add a symbol associated to this if we haven't seen the namespace
16123 before. Also, add a using directive if it's an anonymous
16124 namespace. */
9219021c 16125
f2f0e013 16126 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16127 {
16128 struct type *type;
16129
0114d602 16130 type = read_type_die (die, cu);
e7c27a73 16131 new_symbol (die, type, cu);
5c4e30ca 16132
e8e80198 16133 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16134 if (is_anonymous)
0114d602
DJ
16135 {
16136 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16137
eb1e02fd 16138 std::vector<const char *> excludes;
804d2729 16139 add_using_directive (using_directives (cu),
22cee43f 16140 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16141 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16142 }
5c4e30ca 16143 }
9219021c 16144
639d11d3 16145 if (die->child != NULL)
d9fa45fe 16146 {
639d11d3 16147 struct die_info *child_die = die->child;
6e70227d 16148
d9fa45fe
DC
16149 while (child_die && child_die->tag)
16150 {
e7c27a73 16151 process_die (child_die, cu);
d9fa45fe
DC
16152 child_die = sibling_die (child_die);
16153 }
16154 }
38d518c9
EZ
16155}
16156
f55ee35c
JK
16157/* Read a Fortran module as type. This DIE can be only a declaration used for
16158 imported module. Still we need that type as local Fortran "use ... only"
16159 declaration imports depend on the created type in determine_prefix. */
16160
16161static struct type *
16162read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16163{
518817b3 16164 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16165 const char *module_name;
f55ee35c
JK
16166 struct type *type;
16167
16168 module_name = dwarf2_name (die, cu);
19f392bc 16169 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16170
f55ee35c
JK
16171 return set_die_type (die, type, cu);
16172}
16173
5d7cb8df
JK
16174/* Read a Fortran module. */
16175
16176static void
16177read_module (struct die_info *die, struct dwarf2_cu *cu)
16178{
16179 struct die_info *child_die = die->child;
530e8392
KB
16180 struct type *type;
16181
16182 type = read_type_die (die, cu);
16183 new_symbol (die, type, cu);
5d7cb8df 16184
5d7cb8df
JK
16185 while (child_die && child_die->tag)
16186 {
16187 process_die (child_die, cu);
16188 child_die = sibling_die (child_die);
16189 }
16190}
16191
38d518c9
EZ
16192/* Return the name of the namespace represented by DIE. Set
16193 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16194 namespace. */
16195
16196static const char *
e142c38c 16197namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16198{
16199 struct die_info *current_die;
16200 const char *name = NULL;
16201
16202 /* Loop through the extensions until we find a name. */
16203
16204 for (current_die = die;
16205 current_die != NULL;
f2f0e013 16206 current_die = dwarf2_extension (die, &cu))
38d518c9 16207 {
96553a0c
DE
16208 /* We don't use dwarf2_name here so that we can detect the absence
16209 of a name -> anonymous namespace. */
7d45c7c3 16210 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16211
38d518c9
EZ
16212 if (name != NULL)
16213 break;
16214 }
16215
16216 /* Is it an anonymous namespace? */
16217
16218 *is_anonymous = (name == NULL);
16219 if (*is_anonymous)
2b1dbab0 16220 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16221
16222 return name;
d9fa45fe
DC
16223}
16224
c906108c
SS
16225/* Extract all information from a DW_TAG_pointer_type DIE and add to
16226 the user defined type vector. */
16227
f792889a 16228static struct type *
e7c27a73 16229read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16230{
518817b3
SM
16231 struct gdbarch *gdbarch
16232 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16233 struct comp_unit_head *cu_header = &cu->header;
c906108c 16234 struct type *type;
8b2dbe47
KB
16235 struct attribute *attr_byte_size;
16236 struct attribute *attr_address_class;
16237 int byte_size, addr_class;
7e314c57
JK
16238 struct type *target_type;
16239
16240 target_type = die_type (die, cu);
c906108c 16241
7e314c57
JK
16242 /* The die_type call above may have already set the type for this DIE. */
16243 type = get_die_type (die, cu);
16244 if (type)
16245 return type;
16246
16247 type = lookup_pointer_type (target_type);
8b2dbe47 16248
e142c38c 16249 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16250 if (attr_byte_size)
16251 byte_size = DW_UNSND (attr_byte_size);
c906108c 16252 else
8b2dbe47
KB
16253 byte_size = cu_header->addr_size;
16254
e142c38c 16255 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16256 if (attr_address_class)
16257 addr_class = DW_UNSND (attr_address_class);
16258 else
16259 addr_class = DW_ADDR_none;
16260
2b4424c3
TT
16261 ULONGEST alignment = get_alignment (cu, die);
16262
16263 /* If the pointer size, alignment, or address class is different
16264 than the default, create a type variant marked as such and set
16265 the length accordingly. */
16266 if (TYPE_LENGTH (type) != byte_size
16267 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16268 && alignment != TYPE_RAW_ALIGN (type))
16269 || addr_class != DW_ADDR_none)
c906108c 16270 {
5e2b427d 16271 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16272 {
16273 int type_flags;
16274
849957d9 16275 type_flags = gdbarch_address_class_type_flags
5e2b427d 16276 (gdbarch, byte_size, addr_class);
876cecd0
TT
16277 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16278 == 0);
8b2dbe47
KB
16279 type = make_type_with_address_space (type, type_flags);
16280 }
16281 else if (TYPE_LENGTH (type) != byte_size)
16282 {
b98664d3 16283 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16284 }
2b4424c3
TT
16285 else if (TYPE_RAW_ALIGN (type) != alignment)
16286 {
b98664d3 16287 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16288 " - DIE at %s [in module %s]"),
16289 sect_offset_str (die->sect_off),
16290 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16291 }
6e70227d 16292 else
9a619af0
MS
16293 {
16294 /* Should we also complain about unhandled address classes? */
16295 }
c906108c 16296 }
8b2dbe47
KB
16297
16298 TYPE_LENGTH (type) = byte_size;
2b4424c3 16299 set_type_align (type, alignment);
f792889a 16300 return set_die_type (die, type, cu);
c906108c
SS
16301}
16302
16303/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16304 the user defined type vector. */
16305
f792889a 16306static struct type *
e7c27a73 16307read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16308{
16309 struct type *type;
16310 struct type *to_type;
16311 struct type *domain;
16312
e7c27a73
DJ
16313 to_type = die_type (die, cu);
16314 domain = die_containing_type (die, cu);
0d5de010 16315
7e314c57
JK
16316 /* The calls above may have already set the type for this DIE. */
16317 type = get_die_type (die, cu);
16318 if (type)
16319 return type;
16320
0d5de010
DJ
16321 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16322 type = lookup_methodptr_type (to_type);
7078baeb
TT
16323 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16324 {
518817b3
SM
16325 struct type *new_type
16326 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16327
16328 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16329 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16330 TYPE_VARARGS (to_type));
16331 type = lookup_methodptr_type (new_type);
16332 }
0d5de010
DJ
16333 else
16334 type = lookup_memberptr_type (to_type, domain);
c906108c 16335
f792889a 16336 return set_die_type (die, type, cu);
c906108c
SS
16337}
16338
4297a3f0 16339/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16340 the user defined type vector. */
16341
f792889a 16342static struct type *
4297a3f0
AV
16343read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16344 enum type_code refcode)
c906108c 16345{
e7c27a73 16346 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16347 struct type *type, *target_type;
c906108c
SS
16348 struct attribute *attr;
16349
4297a3f0
AV
16350 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16351
7e314c57
JK
16352 target_type = die_type (die, cu);
16353
16354 /* The die_type call above may have already set the type for this DIE. */
16355 type = get_die_type (die, cu);
16356 if (type)
16357 return type;
16358
4297a3f0 16359 type = lookup_reference_type (target_type, refcode);
e142c38c 16360 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16361 if (attr != nullptr)
c906108c
SS
16362 {
16363 TYPE_LENGTH (type) = DW_UNSND (attr);
16364 }
16365 else
16366 {
107d2387 16367 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16368 }
2b4424c3 16369 maybe_set_alignment (cu, die, type);
f792889a 16370 return set_die_type (die, type, cu);
c906108c
SS
16371}
16372
cf363f18
MW
16373/* Add the given cv-qualifiers to the element type of the array. GCC
16374 outputs DWARF type qualifiers that apply to an array, not the
16375 element type. But GDB relies on the array element type to carry
16376 the cv-qualifiers. This mimics section 6.7.3 of the C99
16377 specification. */
16378
16379static struct type *
16380add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16381 struct type *base_type, int cnst, int voltl)
16382{
16383 struct type *el_type, *inner_array;
16384
16385 base_type = copy_type (base_type);
16386 inner_array = base_type;
16387
16388 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16389 {
16390 TYPE_TARGET_TYPE (inner_array) =
16391 copy_type (TYPE_TARGET_TYPE (inner_array));
16392 inner_array = TYPE_TARGET_TYPE (inner_array);
16393 }
16394
16395 el_type = TYPE_TARGET_TYPE (inner_array);
16396 cnst |= TYPE_CONST (el_type);
16397 voltl |= TYPE_VOLATILE (el_type);
16398 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16399
16400 return set_die_type (die, base_type, cu);
16401}
16402
f792889a 16403static struct type *
e7c27a73 16404read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16405{
f792889a 16406 struct type *base_type, *cv_type;
c906108c 16407
e7c27a73 16408 base_type = die_type (die, cu);
7e314c57
JK
16409
16410 /* The die_type call above may have already set the type for this DIE. */
16411 cv_type = get_die_type (die, cu);
16412 if (cv_type)
16413 return cv_type;
16414
2f608a3a
KW
16415 /* In case the const qualifier is applied to an array type, the element type
16416 is so qualified, not the array type (section 6.7.3 of C99). */
16417 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16418 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16419
f792889a
DJ
16420 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16421 return set_die_type (die, cv_type, cu);
c906108c
SS
16422}
16423
f792889a 16424static struct type *
e7c27a73 16425read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16426{
f792889a 16427 struct type *base_type, *cv_type;
c906108c 16428
e7c27a73 16429 base_type = die_type (die, cu);
7e314c57
JK
16430
16431 /* The die_type call above may have already set the type for this DIE. */
16432 cv_type = get_die_type (die, cu);
16433 if (cv_type)
16434 return cv_type;
16435
cf363f18
MW
16436 /* In case the volatile qualifier is applied to an array type, the
16437 element type is so qualified, not the array type (section 6.7.3
16438 of C99). */
16439 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16440 return add_array_cv_type (die, cu, base_type, 0, 1);
16441
f792889a
DJ
16442 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16443 return set_die_type (die, cv_type, cu);
c906108c
SS
16444}
16445
06d66ee9
TT
16446/* Handle DW_TAG_restrict_type. */
16447
16448static struct type *
16449read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16450{
16451 struct type *base_type, *cv_type;
16452
16453 base_type = die_type (die, cu);
16454
16455 /* The die_type call above may have already set the type for this DIE. */
16456 cv_type = get_die_type (die, cu);
16457 if (cv_type)
16458 return cv_type;
16459
16460 cv_type = make_restrict_type (base_type);
16461 return set_die_type (die, cv_type, cu);
16462}
16463
a2c2acaf
MW
16464/* Handle DW_TAG_atomic_type. */
16465
16466static struct type *
16467read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16468{
16469 struct type *base_type, *cv_type;
16470
16471 base_type = die_type (die, cu);
16472
16473 /* The die_type call above may have already set the type for this DIE. */
16474 cv_type = get_die_type (die, cu);
16475 if (cv_type)
16476 return cv_type;
16477
16478 cv_type = make_atomic_type (base_type);
16479 return set_die_type (die, cv_type, cu);
16480}
16481
c906108c
SS
16482/* Extract all information from a DW_TAG_string_type DIE and add to
16483 the user defined type vector. It isn't really a user defined type,
16484 but it behaves like one, with other DIE's using an AT_user_def_type
16485 attribute to reference it. */
16486
f792889a 16487static struct type *
e7c27a73 16488read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16489{
518817b3 16490 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16491 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16492 struct type *type, *range_type, *index_type, *char_type;
16493 struct attribute *attr;
216a7e6b
AB
16494 struct dynamic_prop prop;
16495 bool length_is_constant = true;
16496 LONGEST length;
16497
16498 /* There are a couple of places where bit sizes might be made use of
16499 when parsing a DW_TAG_string_type, however, no producer that we know
16500 of make use of these. Handling bit sizes that are a multiple of the
16501 byte size is easy enough, but what about other bit sizes? Lets deal
16502 with that problem when we have to. Warn about these attributes being
16503 unsupported, then parse the type and ignore them like we always
16504 have. */
16505 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16506 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16507 {
16508 static bool warning_printed = false;
16509 if (!warning_printed)
16510 {
16511 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16512 "currently supported on DW_TAG_string_type."));
16513 warning_printed = true;
16514 }
16515 }
c906108c 16516
e142c38c 16517 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16518 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16519 {
16520 /* The string length describes the location at which the length of
16521 the string can be found. The size of the length field can be
16522 specified with one of the attributes below. */
16523 struct type *prop_type;
16524 struct attribute *len
16525 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16526 if (len == nullptr)
16527 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16528 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16529 {
16530 /* Pass 0 as the default as we know this attribute is constant
16531 and the default value will not be returned. */
16532 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
09ba997f 16533 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16534 }
16535 else
16536 {
16537 /* If the size is not specified then we assume it is the size of
16538 an address on this target. */
09ba997f 16539 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16540 }
16541
16542 /* Convert the attribute into a dynamic property. */
16543 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16544 length = 1;
16545 else
16546 length_is_constant = false;
16547 }
16548 else if (attr != nullptr)
16549 {
16550 /* This DW_AT_string_length just contains the length with no
16551 indirection. There's no need to create a dynamic property in this
16552 case. Pass 0 for the default value as we know it will not be
16553 returned in this case. */
16554 length = dwarf2_get_attr_constant_value (attr, 0);
16555 }
16556 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16557 {
216a7e6b
AB
16558 /* We don't currently support non-constant byte sizes for strings. */
16559 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16560 }
16561 else
16562 {
216a7e6b
AB
16563 /* Use 1 as a fallback length if we have nothing else. */
16564 length = 1;
c906108c 16565 }
6ccb9162 16566
46bf5051 16567 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16568 if (length_is_constant)
16569 range_type = create_static_range_type (NULL, index_type, 1, length);
16570 else
16571 {
16572 struct dynamic_prop low_bound;
16573
16574 low_bound.kind = PROP_CONST;
16575 low_bound.data.const_val = 1;
16576 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16577 }
3b7538c0
UW
16578 char_type = language_string_char_type (cu->language_defn, gdbarch);
16579 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16580
f792889a 16581 return set_die_type (die, type, cu);
c906108c
SS
16582}
16583
4d804846
JB
16584/* Assuming that DIE corresponds to a function, returns nonzero
16585 if the function is prototyped. */
16586
16587static int
16588prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16589{
16590 struct attribute *attr;
16591
16592 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16593 if (attr && (DW_UNSND (attr) != 0))
16594 return 1;
16595
16596 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16597 is only meaningful for C, but the concept also extends to other
4d804846
JB
16598 languages that allow unprototyped functions (Eg: Objective C).
16599 For all other languages, assume that functions are always
16600 prototyped. */
16601 if (cu->language != language_c
16602 && cu->language != language_objc
16603 && cu->language != language_opencl)
16604 return 1;
16605
16606 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16607 prototyped and unprototyped functions; default to prototyped,
16608 since that is more common in modern code (and RealView warns
16609 about unprototyped functions). */
16610 if (producer_is_realview (cu->producer))
16611 return 1;
16612
16613 return 0;
16614}
16615
c906108c
SS
16616/* Handle DIES due to C code like:
16617
16618 struct foo
c5aa993b
JM
16619 {
16620 int (*funcp)(int a, long l);
16621 int b;
16622 };
c906108c 16623
0963b4bd 16624 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16625
f792889a 16626static struct type *
e7c27a73 16627read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16628{
518817b3 16629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16630 struct type *type; /* Type that this function returns. */
16631 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16632 struct attribute *attr;
16633
e7c27a73 16634 type = die_type (die, cu);
7e314c57
JK
16635
16636 /* The die_type call above may have already set the type for this DIE. */
16637 ftype = get_die_type (die, cu);
16638 if (ftype)
16639 return ftype;
16640
0c8b41f1 16641 ftype = lookup_function_type (type);
c906108c 16642
4d804846 16643 if (prototyped_function_p (die, cu))
a6c727b2 16644 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16645
c055b101
CV
16646 /* Store the calling convention in the type if it's available in
16647 the subroutine die. Otherwise set the calling convention to
16648 the default value DW_CC_normal. */
16649 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16650 if (attr != nullptr
16651 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16652 TYPE_CALLING_CONVENTION (ftype)
16653 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16654 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16655 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16656 else
16657 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16658
743649fd
MW
16659 /* Record whether the function returns normally to its caller or not
16660 if the DWARF producer set that information. */
16661 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16662 if (attr && (DW_UNSND (attr) != 0))
16663 TYPE_NO_RETURN (ftype) = 1;
16664
76c10ea2
GM
16665 /* We need to add the subroutine type to the die immediately so
16666 we don't infinitely recurse when dealing with parameters
0963b4bd 16667 declared as the same subroutine type. */
76c10ea2 16668 set_die_type (die, ftype, cu);
6e70227d 16669
639d11d3 16670 if (die->child != NULL)
c906108c 16671 {
bb5ed363 16672 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16673 struct die_info *child_die;
8072405b 16674 int nparams, iparams;
c906108c
SS
16675
16676 /* Count the number of parameters.
16677 FIXME: GDB currently ignores vararg functions, but knows about
16678 vararg member functions. */
8072405b 16679 nparams = 0;
639d11d3 16680 child_die = die->child;
c906108c
SS
16681 while (child_die && child_die->tag)
16682 {
16683 if (child_die->tag == DW_TAG_formal_parameter)
16684 nparams++;
16685 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16686 TYPE_VARARGS (ftype) = 1;
c906108c
SS
16687 child_die = sibling_die (child_die);
16688 }
16689
16690 /* Allocate storage for parameters and fill them in. */
16691 TYPE_NFIELDS (ftype) = nparams;
16692 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16693 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16694
8072405b
JK
16695 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16696 even if we error out during the parameters reading below. */
16697 for (iparams = 0; iparams < nparams; iparams++)
16698 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16699
16700 iparams = 0;
639d11d3 16701 child_die = die->child;
c906108c
SS
16702 while (child_die && child_die->tag)
16703 {
16704 if (child_die->tag == DW_TAG_formal_parameter)
16705 {
3ce3b1ba
PA
16706 struct type *arg_type;
16707
16708 /* DWARF version 2 has no clean way to discern C++
16709 static and non-static member functions. G++ helps
16710 GDB by marking the first parameter for non-static
16711 member functions (which is the this pointer) as
16712 artificial. We pass this information to
16713 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16714
16715 DWARF version 3 added DW_AT_object_pointer, which GCC
16716 4.5 does not yet generate. */
e142c38c 16717 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16718 if (attr != nullptr)
c906108c
SS
16719 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16720 else
9c37b5ae 16721 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16722 arg_type = die_type (child_die, cu);
16723
16724 /* RealView does not mark THIS as const, which the testsuite
16725 expects. GCC marks THIS as const in method definitions,
16726 but not in the class specifications (GCC PR 43053). */
16727 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16728 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16729 {
16730 int is_this = 0;
16731 struct dwarf2_cu *arg_cu = cu;
16732 const char *name = dwarf2_name (child_die, cu);
16733
16734 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16735 if (attr != nullptr)
3ce3b1ba
PA
16736 {
16737 /* If the compiler emits this, use it. */
16738 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16739 is_this = 1;
16740 }
16741 else if (name && strcmp (name, "this") == 0)
16742 /* Function definitions will have the argument names. */
16743 is_this = 1;
16744 else if (name == NULL && iparams == 0)
16745 /* Declarations may not have the names, so like
16746 elsewhere in GDB, assume an artificial first
16747 argument is "this". */
16748 is_this = 1;
16749
16750 if (is_this)
16751 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16752 arg_type, 0);
16753 }
16754
16755 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16756 iparams++;
16757 }
16758 child_die = sibling_die (child_die);
16759 }
16760 }
16761
76c10ea2 16762 return ftype;
c906108c
SS
16763}
16764
f792889a 16765static struct type *
e7c27a73 16766read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16767{
518817b3 16768 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16769 const char *name = NULL;
3c8e0968 16770 struct type *this_type, *target_type;
c906108c 16771
94af9270 16772 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16773 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16774 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16775 set_die_type (die, this_type, cu);
3c8e0968
DE
16776 target_type = die_type (die, cu);
16777 if (target_type != this_type)
16778 TYPE_TARGET_TYPE (this_type) = target_type;
16779 else
16780 {
16781 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16782 spec and cause infinite loops in GDB. */
b98664d3 16783 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16784 "- DIE at %s [in module %s]"),
16785 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16786 TYPE_TARGET_TYPE (this_type) = NULL;
16787 }
f792889a 16788 return this_type;
c906108c
SS
16789}
16790
9b790ce7
UW
16791/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16792 (which may be different from NAME) to the architecture back-end to allow
16793 it to guess the correct format if necessary. */
16794
16795static struct type *
16796dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16797 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16798{
16799 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16800 const struct floatformat **format;
16801 struct type *type;
16802
16803 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16804 if (format)
103a685e 16805 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16806 else
77b7c781 16807 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16808
16809 return type;
16810}
16811
eb77c9df
AB
16812/* Allocate an integer type of size BITS and name NAME. */
16813
16814static struct type *
16815dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16816 int bits, int unsigned_p, const char *name)
16817{
16818 struct type *type;
16819
16820 /* Versions of Intel's C Compiler generate an integer type called "void"
16821 instead of using DW_TAG_unspecified_type. This has been seen on
16822 at least versions 14, 17, and 18. */
35ee2dc2
AB
16823 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16824 && strcmp (name, "void") == 0)
eb77c9df
AB
16825 type = objfile_type (objfile)->builtin_void;
16826 else
16827 type = init_integer_type (objfile, bits, unsigned_p, name);
16828
16829 return type;
16830}
16831
8bdc1658
AB
16832/* Initialise and return a floating point type of size BITS suitable for
16833 use as a component of a complex number. The NAME_HINT is passed through
16834 when initialising the floating point type and is the name of the complex
16835 type.
16836
16837 As DWARF doesn't currently provide an explicit name for the components
16838 of a complex number, but it can be helpful to have these components
16839 named, we try to select a suitable name based on the size of the
16840 component. */
16841static struct type *
16842dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16843 struct objfile *objfile,
103a685e
TT
16844 int bits, const char *name_hint,
16845 enum bfd_endian byte_order)
8bdc1658
AB
16846{
16847 gdbarch *gdbarch = get_objfile_arch (objfile);
16848 struct type *tt = nullptr;
16849
35add35e
AB
16850 /* Try to find a suitable floating point builtin type of size BITS.
16851 We're going to use the name of this type as the name for the complex
16852 target type that we are about to create. */
1db455a7 16853 switch (cu->language)
8bdc1658 16854 {
1db455a7
AB
16855 case language_fortran:
16856 switch (bits)
16857 {
16858 case 32:
16859 tt = builtin_f_type (gdbarch)->builtin_real;
16860 break;
16861 case 64:
16862 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16863 break;
16864 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16865 case 128:
16866 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16867 break;
16868 }
8bdc1658 16869 break;
1db455a7
AB
16870 default:
16871 switch (bits)
16872 {
16873 case 32:
16874 tt = builtin_type (gdbarch)->builtin_float;
16875 break;
16876 case 64:
16877 tt = builtin_type (gdbarch)->builtin_double;
16878 break;
16879 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16880 case 128:
16881 tt = builtin_type (gdbarch)->builtin_long_double;
16882 break;
16883 }
8bdc1658
AB
16884 break;
16885 }
16886
35add35e
AB
16887 /* If the type we found doesn't match the size we were looking for, then
16888 pretend we didn't find a type at all, the complex target type we
16889 create will then be nameless. */
a12e5744 16890 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16891 tt = nullptr;
16892
8bdc1658 16893 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16894 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16895}
16896
c906108c
SS
16897/* Find a representation of a given base type and install
16898 it in the TYPE field of the die. */
16899
f792889a 16900static struct type *
e7c27a73 16901read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16902{
518817b3 16903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16904 struct type *type;
16905 struct attribute *attr;
19f392bc 16906 int encoding = 0, bits = 0;
15d034d0 16907 const char *name;
34877895 16908 gdbarch *arch;
c906108c 16909
e142c38c 16910 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16911 if (attr != nullptr)
34877895 16912 encoding = DW_UNSND (attr);
e142c38c 16913 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16914 if (attr != nullptr)
34877895 16915 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16916 name = dwarf2_name (die, cu);
6ccb9162 16917 if (!name)
34877895 16918 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16919
16920 arch = get_objfile_arch (objfile);
16921 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16922
34877895
PJ
16923 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16924 if (attr)
103a685e
TT
16925 {
16926 int endianity = DW_UNSND (attr);
16927
16928 switch (endianity)
16929 {
16930 case DW_END_big:
16931 byte_order = BFD_ENDIAN_BIG;
16932 break;
16933 case DW_END_little:
16934 byte_order = BFD_ENDIAN_LITTLE;
16935 break;
16936 default:
16937 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16938 break;
16939 }
16940 }
6ccb9162
UW
16941
16942 switch (encoding)
c906108c 16943 {
6ccb9162
UW
16944 case DW_ATE_address:
16945 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16946 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16947 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16948 break;
16949 case DW_ATE_boolean:
19f392bc 16950 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16951 break;
16952 case DW_ATE_complex_float:
103a685e
TT
16953 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16954 byte_order);
19f392bc 16955 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16956 break;
16957 case DW_ATE_decimal_float:
19f392bc 16958 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16959 break;
16960 case DW_ATE_float:
103a685e 16961 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16962 break;
16963 case DW_ATE_signed:
eb77c9df 16964 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16965 break;
16966 case DW_ATE_unsigned:
3b2b8fea
TT
16967 if (cu->language == language_fortran
16968 && name
61012eef 16969 && startswith (name, "character("))
19f392bc
UW
16970 type = init_character_type (objfile, bits, 1, name);
16971 else
eb77c9df 16972 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
16973 break;
16974 case DW_ATE_signed_char:
6e70227d 16975 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16976 || cu->language == language_pascal
16977 || cu->language == language_fortran)
19f392bc
UW
16978 type = init_character_type (objfile, bits, 0, name);
16979 else
eb77c9df 16980 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16981 break;
16982 case DW_ATE_unsigned_char:
868a0084 16983 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16984 || cu->language == language_pascal
c44af4eb
TT
16985 || cu->language == language_fortran
16986 || cu->language == language_rust)
19f392bc
UW
16987 type = init_character_type (objfile, bits, 1, name);
16988 else
eb77c9df 16989 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 16990 break;
75079b2b 16991 case DW_ATE_UTF:
53e710ac 16992 {
53e710ac
PA
16993 if (bits == 16)
16994 type = builtin_type (arch)->builtin_char16;
16995 else if (bits == 32)
16996 type = builtin_type (arch)->builtin_char32;
16997 else
16998 {
b98664d3 16999 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17000 bits);
eb77c9df 17001 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17002 }
17003 return set_die_type (die, type, cu);
17004 }
75079b2b
TT
17005 break;
17006
6ccb9162 17007 default:
b98664d3 17008 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17009 dwarf_type_encoding_name (encoding));
77b7c781 17010 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17011 break;
c906108c 17012 }
6ccb9162 17013
0114d602 17014 if (name && strcmp (name, "char") == 0)
876cecd0 17015 TYPE_NOSIGN (type) = 1;
0114d602 17016
2b4424c3
TT
17017 maybe_set_alignment (cu, die, type);
17018
103a685e 17019 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17020
f792889a 17021 return set_die_type (die, type, cu);
c906108c
SS
17022}
17023
80180f79
SA
17024/* Parse dwarf attribute if it's a block, reference or constant and put the
17025 resulting value of the attribute into struct bound_prop.
17026 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17027
17028static int
17029attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17030 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17031 struct type *default_type)
80180f79
SA
17032{
17033 struct dwarf2_property_baton *baton;
518817b3
SM
17034 struct obstack *obstack
17035 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17036
9a49df9d
AB
17037 gdb_assert (default_type != NULL);
17038
80180f79
SA
17039 if (attr == NULL || prop == NULL)
17040 return 0;
17041
4fc6c0d5 17042 if (attr->form_is_block ())
80180f79 17043 {
8d749320 17044 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17045 baton->property_type = default_type;
80180f79
SA
17046 baton->locexpr.per_cu = cu->per_cu;
17047 baton->locexpr.size = DW_BLOCK (attr)->size;
17048 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17049 switch (attr->name)
17050 {
17051 case DW_AT_string_length:
17052 baton->locexpr.is_reference = true;
17053 break;
17054 default:
17055 baton->locexpr.is_reference = false;
17056 break;
17057 }
80180f79
SA
17058 prop->data.baton = baton;
17059 prop->kind = PROP_LOCEXPR;
17060 gdb_assert (prop->data.baton != NULL);
17061 }
cd6c91b4 17062 else if (attr->form_is_ref ())
80180f79
SA
17063 {
17064 struct dwarf2_cu *target_cu = cu;
17065 struct die_info *target_die;
17066 struct attribute *target_attr;
17067
17068 target_die = follow_die_ref (die, attr, &target_cu);
17069 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17070 if (target_attr == NULL)
17071 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17072 target_cu);
80180f79
SA
17073 if (target_attr == NULL)
17074 return 0;
17075
df25ebbd 17076 switch (target_attr->name)
80180f79 17077 {
df25ebbd 17078 case DW_AT_location:
cd6c91b4 17079 if (target_attr->form_is_section_offset ())
df25ebbd 17080 {
8d749320 17081 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17082 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17083 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17084 prop->data.baton = baton;
17085 prop->kind = PROP_LOCLIST;
17086 gdb_assert (prop->data.baton != NULL);
17087 }
4fc6c0d5 17088 else if (target_attr->form_is_block ())
df25ebbd 17089 {
8d749320 17090 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17091 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17092 baton->locexpr.per_cu = cu->per_cu;
17093 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17094 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17095 baton->locexpr.is_reference = true;
df25ebbd
JB
17096 prop->data.baton = baton;
17097 prop->kind = PROP_LOCEXPR;
17098 gdb_assert (prop->data.baton != NULL);
17099 }
17100 else
17101 {
17102 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17103 "dynamic property");
17104 return 0;
17105 }
17106 break;
17107 case DW_AT_data_member_location:
17108 {
17109 LONGEST offset;
17110
17111 if (!handle_data_member_location (target_die, target_cu,
17112 &offset))
17113 return 0;
17114
8d749320 17115 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17116 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17117 target_cu);
df25ebbd
JB
17118 baton->offset_info.offset = offset;
17119 baton->offset_info.type = die_type (target_die, target_cu);
17120 prop->data.baton = baton;
17121 prop->kind = PROP_ADDR_OFFSET;
17122 break;
17123 }
80180f79
SA
17124 }
17125 }
cd6c91b4 17126 else if (attr->form_is_constant ())
80180f79
SA
17127 {
17128 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17129 prop->kind = PROP_CONST;
17130 }
17131 else
17132 {
17133 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17134 dwarf2_name (die, cu));
17135 return 0;
17136 }
17137
17138 return 1;
17139}
17140
09ba997f 17141/* See read.h. */
9a49df9d 17142
09ba997f
TT
17143struct type *
17144dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17145{
09ba997f 17146 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17147 struct type *int_type;
17148
17149 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17150#define TRY_TYPE(F) \
17151 int_type = (unsigned_p \
17152 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17153 : objfile_type (objfile)->builtin_ ## F); \
17154 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17155 return int_type
17156
17157 TRY_TYPE (char);
17158 TRY_TYPE (short);
17159 TRY_TYPE (int);
17160 TRY_TYPE (long);
17161 TRY_TYPE (long_long);
17162
17163#undef TRY_TYPE
17164
17165 gdb_assert_not_reached ("unable to find suitable integer type");
17166}
17167
09ba997f 17168/* See read.h. */
11a8b164 17169
09ba997f
TT
17170struct type *
17171dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17172{
09ba997f
TT
17173 int addr_size = this->addr_size ();
17174 return int_type (addr_size, unsigned_p);
11a8b164
AB
17175}
17176
b86352cf
AB
17177/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17178 present (which is valid) then compute the default type based on the
17179 compilation units address size. */
17180
17181static struct type *
17182read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17183{
17184 struct type *index_type = die_type (die, cu);
17185
17186 /* Dwarf-2 specifications explicitly allows to create subrange types
17187 without specifying a base type.
17188 In that case, the base type must be set to the type of
17189 the lower bound, upper bound or count, in that order, if any of these
17190 three attributes references an object that has a type.
17191 If no base type is found, the Dwarf-2 specifications say that
17192 a signed integer type of size equal to the size of an address should
17193 be used.
17194 For the following C code: `extern char gdb_int [];'
17195 GCC produces an empty range DIE.
17196 FIXME: muller/2010-05-28: Possible references to object for low bound,
17197 high bound or count are not yet handled by this code. */
17198 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17199 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17200
17201 return index_type;
17202}
17203
a02abb62
JB
17204/* Read the given DW_AT_subrange DIE. */
17205
f792889a 17206static struct type *
a02abb62
JB
17207read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17208{
4c9ad8c2 17209 struct type *base_type, *orig_base_type;
a02abb62
JB
17210 struct type *range_type;
17211 struct attribute *attr;
729efb13 17212 struct dynamic_prop low, high;
4fae6e18 17213 int low_default_is_valid;
c451ebe5 17214 int high_bound_is_count = 0;
15d034d0 17215 const char *name;
d359392f 17216 ULONGEST negative_mask;
e77813c8 17217
b86352cf
AB
17218 orig_base_type = read_subrange_index_type (die, cu);
17219
4c9ad8c2
TT
17220 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17221 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17222 creating the range type, but we use the result of check_typedef
17223 when examining properties of the type. */
17224 base_type = check_typedef (orig_base_type);
a02abb62 17225
7e314c57
JK
17226 /* The die_type call above may have already set the type for this DIE. */
17227 range_type = get_die_type (die, cu);
17228 if (range_type)
17229 return range_type;
17230
729efb13
SA
17231 low.kind = PROP_CONST;
17232 high.kind = PROP_CONST;
17233 high.data.const_val = 0;
17234
4fae6e18
JK
17235 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17236 omitting DW_AT_lower_bound. */
17237 switch (cu->language)
6e70227d 17238 {
4fae6e18
JK
17239 case language_c:
17240 case language_cplus:
729efb13 17241 low.data.const_val = 0;
4fae6e18
JK
17242 low_default_is_valid = 1;
17243 break;
17244 case language_fortran:
729efb13 17245 low.data.const_val = 1;
4fae6e18
JK
17246 low_default_is_valid = 1;
17247 break;
17248 case language_d:
4fae6e18 17249 case language_objc:
c44af4eb 17250 case language_rust:
729efb13 17251 low.data.const_val = 0;
4fae6e18
JK
17252 low_default_is_valid = (cu->header.version >= 4);
17253 break;
17254 case language_ada:
17255 case language_m2:
17256 case language_pascal:
729efb13 17257 low.data.const_val = 1;
4fae6e18
JK
17258 low_default_is_valid = (cu->header.version >= 4);
17259 break;
17260 default:
729efb13 17261 low.data.const_val = 0;
4fae6e18
JK
17262 low_default_is_valid = 0;
17263 break;
a02abb62
JB
17264 }
17265
e142c38c 17266 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17267 if (attr != nullptr)
9a49df9d 17268 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17269 else if (!low_default_is_valid)
b98664d3 17270 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17271 "- DIE at %s [in module %s]"),
17272 sect_offset_str (die->sect_off),
518817b3 17273 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17274
506f5c41
TV
17275 struct attribute *attr_ub, *attr_count;
17276 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17277 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17278 {
506f5c41 17279 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17280 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17281 {
c451ebe5
SA
17282 /* If bounds are constant do the final calculation here. */
17283 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17284 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17285 else
17286 high_bound_is_count = 1;
c2ff108b 17287 }
506f5c41
TV
17288 else
17289 {
17290 if (attr_ub != NULL)
17291 complaint (_("Unresolved DW_AT_upper_bound "
17292 "- DIE at %s [in module %s]"),
17293 sect_offset_str (die->sect_off),
17294 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17295 if (attr_count != NULL)
17296 complaint (_("Unresolved DW_AT_count "
17297 "- DIE at %s [in module %s]"),
17298 sect_offset_str (die->sect_off),
17299 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17300 }
e77813c8 17301 }
a02abb62 17302
4e962e74
TT
17303 LONGEST bias = 0;
17304 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17305 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17306 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17307
dbb9c2b1
JB
17308 /* Normally, the DWARF producers are expected to use a signed
17309 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17310 But this is unfortunately not always the case, as witnessed
17311 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17312 is used instead. To work around that ambiguity, we treat
17313 the bounds as signed, and thus sign-extend their values, when
17314 the base type is signed. */
6e70227d 17315 negative_mask =
d359392f 17316 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17317 if (low.kind == PROP_CONST
17318 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17319 low.data.const_val |= negative_mask;
17320 if (high.kind == PROP_CONST
17321 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17322 high.data.const_val |= negative_mask;
43bbcdc2 17323
5bbd8269
AB
17324 /* Check for bit and byte strides. */
17325 struct dynamic_prop byte_stride_prop;
17326 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17327 if (attr_byte_stride != nullptr)
17328 {
09ba997f 17329 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17330 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17331 prop_type);
17332 }
17333
17334 struct dynamic_prop bit_stride_prop;
17335 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17336 if (attr_bit_stride != nullptr)
17337 {
17338 /* It only makes sense to have either a bit or byte stride. */
17339 if (attr_byte_stride != nullptr)
17340 {
17341 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17342 "- DIE at %s [in module %s]"),
17343 sect_offset_str (die->sect_off),
17344 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17345 attr_bit_stride = nullptr;
17346 }
17347 else
17348 {
09ba997f 17349 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17350 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17351 prop_type);
17352 }
17353 }
17354
17355 if (attr_byte_stride != nullptr
17356 || attr_bit_stride != nullptr)
17357 {
17358 bool byte_stride_p = (attr_byte_stride != nullptr);
17359 struct dynamic_prop *stride
17360 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17361
17362 range_type
17363 = create_range_type_with_stride (NULL, orig_base_type, &low,
17364 &high, bias, stride, byte_stride_p);
17365 }
17366 else
17367 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17368
c451ebe5
SA
17369 if (high_bound_is_count)
17370 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17371
c2ff108b
JK
17372 /* Ada expects an empty array on no boundary attributes. */
17373 if (attr == NULL && cu->language != language_ada)
729efb13 17374 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17375
39cbfefa
DJ
17376 name = dwarf2_name (die, cu);
17377 if (name)
17378 TYPE_NAME (range_type) = name;
6e70227d 17379
e142c38c 17380 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17381 if (attr != nullptr)
a02abb62
JB
17382 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17383
2b4424c3
TT
17384 maybe_set_alignment (cu, die, range_type);
17385
7e314c57
JK
17386 set_die_type (die, range_type, cu);
17387
17388 /* set_die_type should be already done. */
b4ba55a1
JB
17389 set_descriptive_type (range_type, die, cu);
17390
7e314c57 17391 return range_type;
a02abb62 17392}
6e70227d 17393
f792889a 17394static struct type *
81a17f79
JB
17395read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17396{
17397 struct type *type;
81a17f79 17398
518817b3
SM
17399 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17400 NULL);
0114d602 17401 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17402
74a2f8ff 17403 /* In Ada, an unspecified type is typically used when the description
85102364 17404 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17405 such a type, we treat it as a stub, and try to resolve it later on,
17406 when needed. */
17407 if (cu->language == language_ada)
17408 TYPE_STUB (type) = 1;
17409
f792889a 17410 return set_die_type (die, type, cu);
81a17f79 17411}
a02abb62 17412
639d11d3
DC
17413/* Read a single die and all its descendents. Set the die's sibling
17414 field to NULL; set other fields in the die correctly, and set all
17415 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17416 location of the info_ptr after reading all of those dies. PARENT
17417 is the parent of the die in question. */
17418
17419static struct die_info *
dee91e82 17420read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17421 const gdb_byte *info_ptr,
17422 const gdb_byte **new_info_ptr,
dee91e82 17423 struct die_info *parent)
639d11d3
DC
17424{
17425 struct die_info *die;
d521ce57 17426 const gdb_byte *cur_ptr;
639d11d3 17427
3e225074 17428 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17429 if (die == NULL)
17430 {
17431 *new_info_ptr = cur_ptr;
17432 return NULL;
17433 }
93311388 17434 store_in_ref_table (die, reader->cu);
639d11d3 17435
3e225074 17436 if (die->has_children)
bf6af496 17437 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17438 else
17439 {
17440 die->child = NULL;
17441 *new_info_ptr = cur_ptr;
17442 }
17443
17444 die->sibling = NULL;
17445 die->parent = parent;
17446 return die;
17447}
17448
17449/* Read a die, all of its descendents, and all of its siblings; set
17450 all of the fields of all of the dies correctly. Arguments are as
17451 in read_die_and_children. */
17452
17453static struct die_info *
bf6af496 17454read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17455 const gdb_byte *info_ptr,
17456 const gdb_byte **new_info_ptr,
bf6af496 17457 struct die_info *parent)
639d11d3
DC
17458{
17459 struct die_info *first_die, *last_sibling;
d521ce57 17460 const gdb_byte *cur_ptr;
639d11d3 17461
c906108c 17462 cur_ptr = info_ptr;
639d11d3
DC
17463 first_die = last_sibling = NULL;
17464
17465 while (1)
c906108c 17466 {
639d11d3 17467 struct die_info *die
dee91e82 17468 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17469
1d325ec1 17470 if (die == NULL)
c906108c 17471 {
639d11d3
DC
17472 *new_info_ptr = cur_ptr;
17473 return first_die;
c906108c 17474 }
1d325ec1
DJ
17475
17476 if (!first_die)
17477 first_die = die;
c906108c 17478 else
1d325ec1
DJ
17479 last_sibling->sibling = die;
17480
17481 last_sibling = die;
c906108c 17482 }
c906108c
SS
17483}
17484
bf6af496
DE
17485/* Read a die, all of its descendents, and all of its siblings; set
17486 all of the fields of all of the dies correctly. Arguments are as
17487 in read_die_and_children.
17488 This the main entry point for reading a DIE and all its children. */
17489
17490static struct die_info *
17491read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17492 const gdb_byte *info_ptr,
17493 const gdb_byte **new_info_ptr,
bf6af496
DE
17494 struct die_info *parent)
17495{
17496 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17497 new_info_ptr, parent);
17498
b4f54984 17499 if (dwarf_die_debug)
bf6af496
DE
17500 {
17501 fprintf_unfiltered (gdb_stdlog,
17502 "Read die from %s@0x%x of %s:\n",
96b79293 17503 reader->die_section->get_name (),
bf6af496
DE
17504 (unsigned) (info_ptr - reader->die_section->buffer),
17505 bfd_get_filename (reader->abfd));
b4f54984 17506 dump_die (die, dwarf_die_debug);
bf6af496
DE
17507 }
17508
17509 return die;
17510}
17511
3019eac3
DE
17512/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17513 attributes.
17514 The caller is responsible for filling in the extra attributes
17515 and updating (*DIEP)->num_attrs.
17516 Set DIEP to point to a newly allocated die with its information,
3e225074 17517 except for its child, sibling, and parent fields. */
93311388 17518
d521ce57 17519static const gdb_byte *
3019eac3 17520read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17521 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17522 int num_extra_attrs)
93311388 17523{
b64f50a1 17524 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17525 struct abbrev_info *abbrev;
17526 struct die_info *die;
17527 struct dwarf2_cu *cu = reader->cu;
17528 bfd *abfd = reader->abfd;
17529
9c541725 17530 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17531 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17532 info_ptr += bytes_read;
17533 if (!abbrev_number)
17534 {
17535 *diep = NULL;
93311388
DE
17536 return info_ptr;
17537 }
17538
685af9cd 17539 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17540 if (!abbrev)
348e048f
DE
17541 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17542 abbrev_number,
17543 bfd_get_filename (abfd));
17544
3019eac3 17545 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17546 die->sect_off = sect_off;
93311388
DE
17547 die->tag = abbrev->tag;
17548 die->abbrev = abbrev_number;
3e225074 17549 die->has_children = abbrev->has_children;
93311388 17550
3019eac3
DE
17551 /* Make the result usable.
17552 The caller needs to update num_attrs after adding the extra
17553 attributes. */
93311388
DE
17554 die->num_attrs = abbrev->num_attrs;
17555
18a8505e 17556 std::vector<int> indexes_that_need_reprocess;
93311388 17557 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17558 {
17559 bool need_reprocess;
17560 info_ptr =
17561 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17562 info_ptr, &need_reprocess);
17563 if (need_reprocess)
17564 indexes_that_need_reprocess.push_back (i);
17565 }
17566
17567 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17568 if (attr != nullptr)
17569 cu->str_offsets_base = DW_UNSND (attr);
93311388 17570
18a8505e
AT
17571 auto maybe_addr_base = lookup_addr_base(die);
17572 if (maybe_addr_base.has_value ())
17573 cu->addr_base = *maybe_addr_base;
17574 for (int index : indexes_that_need_reprocess)
17575 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17576 *diep = die;
93311388
DE
17577 return info_ptr;
17578}
17579
3019eac3
DE
17580/* Read a die and all its attributes.
17581 Set DIEP to point to a newly allocated die with its information,
3e225074 17582 except for its child, sibling, and parent fields. */
3019eac3 17583
d521ce57 17584static const gdb_byte *
3019eac3 17585read_full_die (const struct die_reader_specs *reader,
3e225074 17586 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17587{
d521ce57 17588 const gdb_byte *result;
bf6af496 17589
3e225074 17590 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17591
b4f54984 17592 if (dwarf_die_debug)
bf6af496
DE
17593 {
17594 fprintf_unfiltered (gdb_stdlog,
17595 "Read die from %s@0x%x of %s:\n",
96b79293 17596 reader->die_section->get_name (),
bf6af496
DE
17597 (unsigned) (info_ptr - reader->die_section->buffer),
17598 bfd_get_filename (reader->abfd));
b4f54984 17599 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17600 }
17601
17602 return result;
3019eac3 17603}
433df2d4 17604\f
c906108c 17605
72bf9492
DJ
17606/* Returns nonzero if TAG represents a type that we might generate a partial
17607 symbol for. */
17608
17609static int
17610is_type_tag_for_partial (int tag)
17611{
17612 switch (tag)
17613 {
17614#if 0
17615 /* Some types that would be reasonable to generate partial symbols for,
17616 that we don't at present. */
17617 case DW_TAG_array_type:
17618 case DW_TAG_file_type:
17619 case DW_TAG_ptr_to_member_type:
17620 case DW_TAG_set_type:
17621 case DW_TAG_string_type:
17622 case DW_TAG_subroutine_type:
17623#endif
17624 case DW_TAG_base_type:
17625 case DW_TAG_class_type:
680b30c7 17626 case DW_TAG_interface_type:
72bf9492
DJ
17627 case DW_TAG_enumeration_type:
17628 case DW_TAG_structure_type:
17629 case DW_TAG_subrange_type:
17630 case DW_TAG_typedef:
17631 case DW_TAG_union_type:
17632 return 1;
17633 default:
17634 return 0;
17635 }
17636}
17637
17638/* Load all DIEs that are interesting for partial symbols into memory. */
17639
17640static struct partial_die_info *
dee91e82 17641load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17642 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17643{
dee91e82 17644 struct dwarf2_cu *cu = reader->cu;
518817b3 17645 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17646 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17647 unsigned int bytes_read;
5afb4e99 17648 unsigned int load_all = 0;
72bf9492
DJ
17649 int nesting_level = 1;
17650
17651 parent_die = NULL;
17652 last_die = NULL;
17653
7adf1e79
DE
17654 gdb_assert (cu->per_cu != NULL);
17655 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17656 load_all = 1;
17657
72bf9492
DJ
17658 cu->partial_dies
17659 = htab_create_alloc_ex (cu->header.length / 12,
17660 partial_die_hash,
17661 partial_die_eq,
17662 NULL,
17663 &cu->comp_unit_obstack,
17664 hashtab_obstack_allocate,
17665 dummy_obstack_deallocate);
17666
72bf9492
DJ
17667 while (1)
17668 {
685af9cd 17669 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17670
17671 /* A NULL abbrev means the end of a series of children. */
17672 if (abbrev == NULL)
17673 {
17674 if (--nesting_level == 0)
cd9983dd
YQ
17675 return first_die;
17676
72bf9492
DJ
17677 info_ptr += bytes_read;
17678 last_die = parent_die;
17679 parent_die = parent_die->die_parent;
17680 continue;
17681 }
17682
98bfdba5
PA
17683 /* Check for template arguments. We never save these; if
17684 they're seen, we just mark the parent, and go on our way. */
17685 if (parent_die != NULL
17686 && cu->language == language_cplus
17687 && (abbrev->tag == DW_TAG_template_type_param
17688 || abbrev->tag == DW_TAG_template_value_param))
17689 {
17690 parent_die->has_template_arguments = 1;
17691
17692 if (!load_all)
17693 {
17694 /* We don't need a partial DIE for the template argument. */
dee91e82 17695 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17696 continue;
17697 }
17698 }
17699
0d99eb77 17700 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17701 Skip their other children. */
17702 if (!load_all
17703 && cu->language == language_cplus
17704 && parent_die != NULL
17705 && parent_die->tag == DW_TAG_subprogram)
17706 {
dee91e82 17707 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17708 continue;
17709 }
17710
5afb4e99
DJ
17711 /* Check whether this DIE is interesting enough to save. Normally
17712 we would not be interested in members here, but there may be
17713 later variables referencing them via DW_AT_specification (for
17714 static members). */
17715 if (!load_all
17716 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17717 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17718 && abbrev->tag != DW_TAG_enumerator
17719 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17720 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17721 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17722 && abbrev->tag != DW_TAG_variable
5afb4e99 17723 && abbrev->tag != DW_TAG_namespace
f55ee35c 17724 && abbrev->tag != DW_TAG_module
95554aad 17725 && abbrev->tag != DW_TAG_member
74921315
KS
17726 && abbrev->tag != DW_TAG_imported_unit
17727 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17728 {
17729 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17730 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17731 continue;
17732 }
17733
6f06d47b
YQ
17734 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17735 abbrev);
cd9983dd 17736
48fbe735 17737 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17738
17739 /* This two-pass algorithm for processing partial symbols has a
17740 high cost in cache pressure. Thus, handle some simple cases
17741 here which cover the majority of C partial symbols. DIEs
17742 which neither have specification tags in them, nor could have
17743 specification tags elsewhere pointing at them, can simply be
17744 processed and discarded.
17745
17746 This segment is also optional; scan_partial_symbols and
17747 add_partial_symbol will handle these DIEs if we chain
17748 them in normally. When compilers which do not emit large
17749 quantities of duplicate debug information are more common,
17750 this code can probably be removed. */
17751
17752 /* Any complete simple types at the top level (pretty much all
17753 of them, for a language without namespaces), can be processed
17754 directly. */
17755 if (parent_die == NULL
cd9983dd
YQ
17756 && pdi.has_specification == 0
17757 && pdi.is_declaration == 0
17758 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17759 || pdi.tag == DW_TAG_base_type
17760 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17761 {
cd9983dd 17762 if (building_psymtab && pdi.name != NULL)
31edb802 17763 add_psymbol_to_list (pdi.name, false,
79748972 17764 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17765 psymbol_placement::STATIC,
1762568f 17766 0, cu->language, objfile);
cd9983dd 17767 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17768 continue;
17769 }
17770
d8228535
JK
17771 /* The exception for DW_TAG_typedef with has_children above is
17772 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17773 type_name_or_error will error on such types later.
d8228535
JK
17774
17775 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17776 it could not find the child DIEs referenced later, this is checked
17777 above. In correct DWARF DW_TAG_typedef should have no children. */
17778
cd9983dd 17779 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17780 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17781 "- DIE at %s [in module %s]"),
cd9983dd 17782 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17783
72bf9492
DJ
17784 /* If we're at the second level, and we're an enumerator, and
17785 our parent has no specification (meaning possibly lives in a
17786 namespace elsewhere), then we can add the partial symbol now
17787 instead of queueing it. */
cd9983dd 17788 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17789 && parent_die != NULL
17790 && parent_die->die_parent == NULL
17791 && parent_die->tag == DW_TAG_enumeration_type
17792 && parent_die->has_specification == 0)
17793 {
cd9983dd 17794 if (pdi.name == NULL)
b98664d3 17795 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17796 else if (building_psymtab)
31edb802 17797 add_psymbol_to_list (pdi.name, false,
79748972 17798 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17799 cu->language == language_cplus
75aedd27
TT
17800 ? psymbol_placement::GLOBAL
17801 : psymbol_placement::STATIC,
1762568f 17802 0, cu->language, objfile);
72bf9492 17803
cd9983dd 17804 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17805 continue;
17806 }
17807
cd9983dd 17808 struct partial_die_info *part_die
6f06d47b 17809 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17810
72bf9492
DJ
17811 /* We'll save this DIE so link it in. */
17812 part_die->die_parent = parent_die;
17813 part_die->die_sibling = NULL;
17814 part_die->die_child = NULL;
17815
17816 if (last_die && last_die == parent_die)
17817 last_die->die_child = part_die;
17818 else if (last_die)
17819 last_die->die_sibling = part_die;
17820
17821 last_die = part_die;
17822
17823 if (first_die == NULL)
17824 first_die = part_die;
17825
17826 /* Maybe add the DIE to the hash table. Not all DIEs that we
17827 find interesting need to be in the hash table, because we
17828 also have the parent/sibling/child chains; only those that we
17829 might refer to by offset later during partial symbol reading.
17830
17831 For now this means things that might have be the target of a
17832 DW_AT_specification, DW_AT_abstract_origin, or
17833 DW_AT_extension. DW_AT_extension will refer only to
17834 namespaces; DW_AT_abstract_origin refers to functions (and
17835 many things under the function DIE, but we do not recurse
17836 into function DIEs during partial symbol reading) and
17837 possibly variables as well; DW_AT_specification refers to
17838 declarations. Declarations ought to have the DW_AT_declaration
17839 flag. It happens that GCC forgets to put it in sometimes, but
17840 only for functions, not for types.
17841
17842 Adding more things than necessary to the hash table is harmless
17843 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17844 wasted time in find_partial_die, when we reread the compilation
17845 unit with load_all_dies set. */
72bf9492 17846
5afb4e99 17847 if (load_all
72929c62 17848 || abbrev->tag == DW_TAG_constant
5afb4e99 17849 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17850 || abbrev->tag == DW_TAG_variable
17851 || abbrev->tag == DW_TAG_namespace
17852 || part_die->is_declaration)
17853 {
17854 void **slot;
17855
17856 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17857 to_underlying (part_die->sect_off),
17858 INSERT);
72bf9492
DJ
17859 *slot = part_die;
17860 }
17861
72bf9492 17862 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17863 we have no reason to follow the children of structures; for other
98bfdba5
PA
17864 languages we have to, so that we can get at method physnames
17865 to infer fully qualified class names, for DW_AT_specification,
17866 and for C++ template arguments. For C++, we also look one level
17867 inside functions to find template arguments (if the name of the
17868 function does not already contain the template arguments).
bc30ff58 17869
0a4b0913
AB
17870 For Ada and Fortran, we need to scan the children of subprograms
17871 and lexical blocks as well because these languages allow the
17872 definition of nested entities that could be interesting for the
17873 debugger, such as nested subprograms for instance. */
72bf9492 17874 if (last_die->has_children
5afb4e99
DJ
17875 && (load_all
17876 || last_die->tag == DW_TAG_namespace
f55ee35c 17877 || last_die->tag == DW_TAG_module
72bf9492 17878 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17879 || (cu->language == language_cplus
17880 && last_die->tag == DW_TAG_subprogram
17881 && (last_die->name == NULL
17882 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17883 || (cu->language != language_c
17884 && (last_die->tag == DW_TAG_class_type
680b30c7 17885 || last_die->tag == DW_TAG_interface_type
72bf9492 17886 || last_die->tag == DW_TAG_structure_type
bc30ff58 17887 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17888 || ((cu->language == language_ada
17889 || cu->language == language_fortran)
bc30ff58
JB
17890 && (last_die->tag == DW_TAG_subprogram
17891 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17892 {
17893 nesting_level++;
17894 parent_die = last_die;
17895 continue;
17896 }
17897
17898 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17899 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17900
17901 /* Back to the top, do it again. */
17902 }
17903}
17904
6f06d47b
YQ
17905partial_die_info::partial_die_info (sect_offset sect_off_,
17906 struct abbrev_info *abbrev)
17907 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17908{
17909}
17910
35cc7ed7
YQ
17911/* Read a minimal amount of information into the minimal die structure.
17912 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17913
48fbe735
YQ
17914const gdb_byte *
17915partial_die_info::read (const struct die_reader_specs *reader,
17916 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17917{
dee91e82 17918 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17919 struct dwarf2_per_objfile *dwarf2_per_objfile
17920 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17921 unsigned int i;
c5aa993b 17922 int has_low_pc_attr = 0;
c906108c 17923 int has_high_pc_attr = 0;
91da1414 17924 int high_pc_relative = 0;
c906108c 17925
18a8505e 17926 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17927 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17928 {
18a8505e
AT
17929 bool need_reprocess;
17930 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17931 info_ptr, &need_reprocess);
17932 /* String and address offsets that need to do the reprocessing have
17933 already been read at this point, so there is no need to wait until
17934 the loop terminates to do the reprocessing. */
17935 if (need_reprocess)
17936 read_attribute_reprocess (reader, &attr_vec[i]);
17937 attribute &attr = attr_vec[i];
c906108c 17938 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17939 partial symbol table. */
c906108c
SS
17940 switch (attr.name)
17941 {
17942 case DW_AT_name:
48fbe735 17943 switch (tag)
71c25dea
TT
17944 {
17945 case DW_TAG_compile_unit:
95554aad 17946 case DW_TAG_partial_unit:
348e048f 17947 case DW_TAG_type_unit:
71c25dea
TT
17948 /* Compilation units have a DW_AT_name that is a filename, not
17949 a source language identifier. */
17950 case DW_TAG_enumeration_type:
17951 case DW_TAG_enumerator:
17952 /* These tags always have simple identifiers already; no need
17953 to canonicalize them. */
48fbe735 17954 name = DW_STRING (&attr);
71c25dea
TT
17955 break;
17956 default:
48fbe735
YQ
17957 {
17958 struct objfile *objfile = dwarf2_per_objfile->objfile;
17959
17960 name
17961 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17962 &objfile->per_bfd->storage_obstack);
17963 }
71c25dea
TT
17964 break;
17965 }
c906108c 17966 break;
31ef98ae 17967 case DW_AT_linkage_name:
c906108c 17968 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17969 /* Note that both forms of linkage name might appear. We
17970 assume they will be the same, and we only store the last
17971 one we see. */
48fbe735 17972 linkage_name = DW_STRING (&attr);
c906108c
SS
17973 break;
17974 case DW_AT_low_pc:
17975 has_low_pc_attr = 1;
cd6c91b4 17976 lowpc = attr.value_as_address ();
c906108c
SS
17977 break;
17978 case DW_AT_high_pc:
17979 has_high_pc_attr = 1;
cd6c91b4
TT
17980 highpc = attr.value_as_address ();
17981 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 17982 high_pc_relative = 1;
c906108c
SS
17983 break;
17984 case DW_AT_location:
0963b4bd 17985 /* Support the .debug_loc offsets. */
4fc6c0d5 17986 if (attr.form_is_block ())
8e19ed76 17987 {
48fbe735 17988 d.locdesc = DW_BLOCK (&attr);
8e19ed76 17989 }
cd6c91b4 17990 else if (attr.form_is_section_offset ())
8e19ed76 17991 {
4d3c2250 17992 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
17993 }
17994 else
17995 {
4d3c2250
KB
17996 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17997 "partial symbol information");
8e19ed76 17998 }
c906108c 17999 break;
c906108c 18000 case DW_AT_external:
48fbe735 18001 is_external = DW_UNSND (&attr);
c906108c
SS
18002 break;
18003 case DW_AT_declaration:
48fbe735 18004 is_declaration = DW_UNSND (&attr);
c906108c
SS
18005 break;
18006 case DW_AT_type:
48fbe735 18007 has_type = 1;
c906108c
SS
18008 break;
18009 case DW_AT_abstract_origin:
18010 case DW_AT_specification:
72bf9492 18011 case DW_AT_extension:
48fbe735
YQ
18012 has_specification = 1;
18013 spec_offset = dwarf2_get_ref_die_offset (&attr);
18014 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18015 || cu->per_cu->is_dwz);
c906108c
SS
18016 break;
18017 case DW_AT_sibling:
18018 /* Ignore absolute siblings, they might point outside of
18019 the current compile unit. */
18020 if (attr.form == DW_FORM_ref_addr)
b98664d3 18021 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18022 else
b9502d3f 18023 {
48fbe735 18024 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18025 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18026 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18027
18028 if (sibling_ptr < info_ptr)
b98664d3 18029 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18030 else if (sibling_ptr > reader->buffer_end)
18031 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18032 else
48fbe735 18033 sibling = sibling_ptr;
b9502d3f 18034 }
c906108c 18035 break;
fa4028e9 18036 case DW_AT_byte_size:
48fbe735 18037 has_byte_size = 1;
fa4028e9 18038 break;
ff908ebf 18039 case DW_AT_const_value:
48fbe735 18040 has_const_value = 1;
ff908ebf 18041 break;
68511cec
CES
18042 case DW_AT_calling_convention:
18043 /* DWARF doesn't provide a way to identify a program's source-level
18044 entry point. DW_AT_calling_convention attributes are only meant
18045 to describe functions' calling conventions.
18046
18047 However, because it's a necessary piece of information in
0c1b455e
TT
18048 Fortran, and before DWARF 4 DW_CC_program was the only
18049 piece of debugging information whose definition refers to
18050 a 'main program' at all, several compilers marked Fortran
18051 main programs with DW_CC_program --- even when those
18052 functions use the standard calling conventions.
18053
18054 Although DWARF now specifies a way to provide this
18055 information, we support this practice for backward
18056 compatibility. */
68511cec 18057 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18058 && cu->language == language_fortran)
48fbe735 18059 main_subprogram = 1;
68511cec 18060 break;
481860b3
GB
18061 case DW_AT_inline:
18062 if (DW_UNSND (&attr) == DW_INL_inlined
18063 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18064 may_be_inlined = 1;
481860b3 18065 break;
95554aad
TT
18066
18067 case DW_AT_import:
48fbe735 18068 if (tag == DW_TAG_imported_unit)
36586728 18069 {
48fbe735
YQ
18070 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18071 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18072 || cu->per_cu->is_dwz);
18073 }
95554aad
TT
18074 break;
18075
0c1b455e 18076 case DW_AT_main_subprogram:
48fbe735 18077 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18078 break;
18079
05caa1d2
TT
18080 case DW_AT_ranges:
18081 {
18082 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18083 but that requires a full DIE, so instead we just
18084 reimplement it. */
18085 int need_ranges_base = tag != DW_TAG_compile_unit;
18086 unsigned int ranges_offset = (DW_UNSND (&attr)
18087 + (need_ranges_base
18088 ? cu->ranges_base
18089 : 0));
18090
18091 /* Value of the DW_AT_ranges attribute is the offset in the
18092 .debug_ranges section. */
18093 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18094 nullptr))
18095 has_pc_info = 1;
18096 }
18097 break;
18098
c906108c
SS
18099 default:
18100 break;
18101 }
18102 }
18103
10d06d82
TT
18104 /* For Ada, if both the name and the linkage name appear, we prefer
18105 the latter. This lets "catch exception" work better, regardless
18106 of the order in which the name and linkage name were emitted.
18107 Really, though, this is just a workaround for the fact that gdb
18108 doesn't store both the name and the linkage name. */
18109 if (cu->language == language_ada && linkage_name != nullptr)
18110 name = linkage_name;
18111
91da1414 18112 if (high_pc_relative)
48fbe735 18113 highpc += lowpc;
91da1414 18114
9373cf26
JK
18115 if (has_low_pc_attr && has_high_pc_attr)
18116 {
18117 /* When using the GNU linker, .gnu.linkonce. sections are used to
18118 eliminate duplicate copies of functions and vtables and such.
18119 The linker will arbitrarily choose one and discard the others.
18120 The AT_*_pc values for such functions refer to local labels in
18121 these sections. If the section from that file was discarded, the
18122 labels are not in the output, so the relocs get a value of 0.
18123 If this is a discarded function, mark the pc bounds as invalid,
18124 so that GDB will ignore it. */
48fbe735 18125 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18126 {
48fbe735 18127 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18128 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18129
b98664d3 18130 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18131 "for DIE at %s [in module %s]"),
48fbe735
YQ
18132 paddress (gdbarch, lowpc),
18133 sect_offset_str (sect_off),
9d8780f0 18134 objfile_name (objfile));
9373cf26
JK
18135 }
18136 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18137 else if (lowpc >= highpc)
9373cf26 18138 {
48fbe735 18139 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18140 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18141
b98664d3 18142 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18143 "for DIE at %s [in module %s]"),
48fbe735
YQ
18144 paddress (gdbarch, lowpc),
18145 paddress (gdbarch, highpc),
18146 sect_offset_str (sect_off),
9c541725 18147 objfile_name (objfile));
9373cf26
JK
18148 }
18149 else
48fbe735 18150 has_pc_info = 1;
9373cf26 18151 }
85cbf3d3 18152
c906108c
SS
18153 return info_ptr;
18154}
18155
72bf9492
DJ
18156/* Find a cached partial DIE at OFFSET in CU. */
18157
d590ff25
YQ
18158struct partial_die_info *
18159dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18160{
18161 struct partial_die_info *lookup_die = NULL;
6f06d47b 18162 struct partial_die_info part_die (sect_off);
72bf9492 18163
9a3c8263 18164 lookup_die = ((struct partial_die_info *)
d590ff25 18165 htab_find_with_hash (partial_dies, &part_die,
9c541725 18166 to_underlying (sect_off)));
72bf9492 18167
72bf9492
DJ
18168 return lookup_die;
18169}
18170
348e048f
DE
18171/* Find a partial DIE at OFFSET, which may or may not be in CU,
18172 except in the case of .debug_types DIEs which do not reference
18173 outside their CU (they do however referencing other types via
55f1336d 18174 DW_FORM_ref_sig8). */
72bf9492 18175
122cf0f2 18176static const struct cu_partial_die_info
9c541725 18177find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18178{
518817b3
SM
18179 struct dwarf2_per_objfile *dwarf2_per_objfile
18180 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18181 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18182 struct dwarf2_per_cu_data *per_cu = NULL;
18183 struct partial_die_info *pd = NULL;
72bf9492 18184
36586728 18185 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18186 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18187 {
d590ff25 18188 pd = cu->find_partial_die (sect_off);
5afb4e99 18189 if (pd != NULL)
fb816e8b 18190 return { cu, pd };
0d99eb77
DE
18191 /* We missed recording what we needed.
18192 Load all dies and try again. */
18193 per_cu = cu->per_cu;
5afb4e99 18194 }
0d99eb77
DE
18195 else
18196 {
18197 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18198 if (cu->per_cu->is_debug_types)
0d99eb77 18199 {
9d8780f0
SM
18200 error (_("Dwarf Error: Type Unit at offset %s contains"
18201 " external reference to offset %s [in module %s].\n"),
18202 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18203 bfd_get_filename (objfile->obfd));
18204 }
9c541725 18205 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18206 dwarf2_per_objfile);
72bf9492 18207
0d99eb77
DE
18208 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18209 load_partial_comp_unit (per_cu);
ae038cb0 18210
0d99eb77 18211 per_cu->cu->last_used = 0;
d590ff25 18212 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18213 }
5afb4e99 18214
dee91e82
DE
18215 /* If we didn't find it, and not all dies have been loaded,
18216 load them all and try again. */
18217
5afb4e99
DJ
18218 if (pd == NULL && per_cu->load_all_dies == 0)
18219 {
5afb4e99 18220 per_cu->load_all_dies = 1;
fd820528
DE
18221
18222 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18223 THIS_CU->cu may already be in use. So we can't just free it and
18224 replace its DIEs with the ones we read in. Instead, we leave those
18225 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18226 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18227 set. */
dee91e82 18228 load_partial_comp_unit (per_cu);
5afb4e99 18229
d590ff25 18230 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18231 }
18232
18233 if (pd == NULL)
18234 internal_error (__FILE__, __LINE__,
9d8780f0 18235 _("could not find partial DIE %s "
3e43a32a 18236 "in cache [from module %s]\n"),
9d8780f0 18237 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18238 return { per_cu->cu, pd };
72bf9492
DJ
18239}
18240
abc72ce4
DE
18241/* See if we can figure out if the class lives in a namespace. We do
18242 this by looking for a member function; its demangled name will
18243 contain namespace info, if there is any. */
18244
18245static void
18246guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18247 struct dwarf2_cu *cu)
18248{
18249 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18250 what template types look like, because the demangler
18251 frequently doesn't give the same name as the debug info. We
18252 could fix this by only using the demangled name to get the
18253 prefix (but see comment in read_structure_type). */
18254
18255 struct partial_die_info *real_pdi;
18256 struct partial_die_info *child_pdi;
18257
18258 /* If this DIE (this DIE's specification, if any) has a parent, then
18259 we should not do this. We'll prepend the parent's fully qualified
18260 name when we create the partial symbol. */
18261
18262 real_pdi = struct_pdi;
18263 while (real_pdi->has_specification)
fb816e8b 18264 {
122cf0f2
AB
18265 auto res = find_partial_die (real_pdi->spec_offset,
18266 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18267 real_pdi = res.pdi;
18268 cu = res.cu;
18269 }
abc72ce4
DE
18270
18271 if (real_pdi->die_parent != NULL)
18272 return;
18273
18274 for (child_pdi = struct_pdi->die_child;
18275 child_pdi != NULL;
18276 child_pdi = child_pdi->die_sibling)
18277 {
18278 if (child_pdi->tag == DW_TAG_subprogram
18279 && child_pdi->linkage_name != NULL)
18280 {
43816ebc
TT
18281 gdb::unique_xmalloc_ptr<char> actual_class_name
18282 (language_class_name_from_physname (cu->language_defn,
18283 child_pdi->linkage_name));
abc72ce4
DE
18284 if (actual_class_name != NULL)
18285 {
518817b3 18286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18287 struct_pdi->name
021887d8 18288 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 18289 actual_class_name.get ());
abc72ce4
DE
18290 }
18291 break;
18292 }
18293 }
18294}
18295
52356b79
YQ
18296void
18297partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18298{
abc72ce4
DE
18299 /* Once we've fixed up a die, there's no point in doing so again.
18300 This also avoids a memory leak if we were to call
18301 guess_partial_die_structure_name multiple times. */
52356b79 18302 if (fixup_called)
abc72ce4
DE
18303 return;
18304
72bf9492
DJ
18305 /* If we found a reference attribute and the DIE has no name, try
18306 to find a name in the referred to DIE. */
18307
52356b79 18308 if (name == NULL && has_specification)
72bf9492
DJ
18309 {
18310 struct partial_die_info *spec_die;
72bf9492 18311
122cf0f2 18312 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18313 spec_die = res.pdi;
18314 cu = res.cu;
72bf9492 18315
52356b79 18316 spec_die->fixup (cu);
72bf9492
DJ
18317
18318 if (spec_die->name)
18319 {
52356b79 18320 name = spec_die->name;
72bf9492
DJ
18321
18322 /* Copy DW_AT_external attribute if it is set. */
18323 if (spec_die->is_external)
52356b79 18324 is_external = spec_die->is_external;
72bf9492
DJ
18325 }
18326 }
18327
18328 /* Set default names for some unnamed DIEs. */
72bf9492 18329
52356b79
YQ
18330 if (name == NULL && tag == DW_TAG_namespace)
18331 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18332
abc72ce4
DE
18333 /* If there is no parent die to provide a namespace, and there are
18334 children, see if we can determine the namespace from their linkage
122d1940 18335 name. */
abc72ce4 18336 if (cu->language == language_cplus
fd5866f6 18337 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18338 && die_parent == NULL
18339 && has_children
18340 && (tag == DW_TAG_class_type
18341 || tag == DW_TAG_structure_type
18342 || tag == DW_TAG_union_type))
18343 guess_partial_die_structure_name (this, cu);
abc72ce4 18344
53832f31
TT
18345 /* GCC might emit a nameless struct or union that has a linkage
18346 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18347 if (name == NULL
18348 && (tag == DW_TAG_class_type
18349 || tag == DW_TAG_interface_type
18350 || tag == DW_TAG_structure_type
18351 || tag == DW_TAG_union_type)
18352 && linkage_name != NULL)
53832f31 18353 {
43816ebc
TT
18354 gdb::unique_xmalloc_ptr<char> demangled
18355 (gdb_demangle (linkage_name, DMGL_TYPES));
18356 if (demangled != nullptr)
53832f31 18357 {
96408a79
SA
18358 const char *base;
18359
18360 /* Strip any leading namespaces/classes, keep only the base name.
18361 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18362 base = strrchr (demangled.get (), ':');
18363 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18364 base++;
18365 else
43816ebc 18366 base = demangled.get ();
96408a79 18367
518817b3 18368 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 18369 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
18370 }
18371 }
18372
52356b79 18373 fixup_called = 1;
72bf9492
DJ
18374}
18375
18a8505e
AT
18376/* Process the attributes that had to be skipped in the first round. These
18377 attributes are the ones that need str_offsets_base or addr_base attributes.
18378 They could not have been processed in the first round, because at the time
18379 the values of str_offsets_base or addr_base may not have been known. */
18380void read_attribute_reprocess (const struct die_reader_specs *reader,
18381 struct attribute *attr)
18382{
18383 struct dwarf2_cu *cu = reader->cu;
18384 switch (attr->form)
18385 {
18386 case DW_FORM_addrx:
18387 case DW_FORM_GNU_addr_index:
18388 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18389 break;
18390 case DW_FORM_strx:
18391 case DW_FORM_strx1:
18392 case DW_FORM_strx2:
18393 case DW_FORM_strx3:
18394 case DW_FORM_strx4:
18395 case DW_FORM_GNU_str_index:
18396 {
18397 unsigned int str_index = DW_UNSND (attr);
18398 if (reader->dwo_file != NULL)
18399 {
18400 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18401 DW_STRING_IS_CANONICAL (attr) = 0;
18402 }
18403 else
18404 {
18405 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18406 DW_STRING_IS_CANONICAL (attr) = 0;
18407 }
18408 break;
18409 }
18410 default:
18411 gdb_assert_not_reached (_("Unexpected DWARF form."));
18412 }
18413}
18414
a8329558 18415/* Read an attribute value described by an attribute form. */
c906108c 18416
d521ce57 18417static const gdb_byte *
dee91e82
DE
18418read_attribute_value (const struct die_reader_specs *reader,
18419 struct attribute *attr, unsigned form,
18a8505e
AT
18420 LONGEST implicit_const, const gdb_byte *info_ptr,
18421 bool *need_reprocess)
c906108c 18422{
dee91e82 18423 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18424 struct dwarf2_per_objfile *dwarf2_per_objfile
18425 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18426 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18427 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18428 bfd *abfd = reader->abfd;
e7c27a73 18429 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18430 unsigned int bytes_read;
18431 struct dwarf_block *blk;
18a8505e 18432 *need_reprocess = false;
c906108c 18433
aead7601 18434 attr->form = (enum dwarf_form) form;
a8329558 18435 switch (form)
c906108c 18436 {
c906108c 18437 case DW_FORM_ref_addr:
ae411497 18438 if (cu->header.version == 2)
c8a7a66f
TT
18439 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18440 &bytes_read);
ae411497 18441 else
8266302d
TT
18442 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18443 &bytes_read);
ae411497
TT
18444 info_ptr += bytes_read;
18445 break;
36586728 18446 case DW_FORM_GNU_ref_alt:
8266302d 18447 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18448 info_ptr += bytes_read;
18449 break;
ae411497 18450 case DW_FORM_addr:
c8a7a66f 18451 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18452 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18453 info_ptr += bytes_read;
c906108c
SS
18454 break;
18455 case DW_FORM_block2:
7b5a2f43 18456 blk = dwarf_alloc_block (cu);
c906108c
SS
18457 blk->size = read_2_bytes (abfd, info_ptr);
18458 info_ptr += 2;
18459 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18460 info_ptr += blk->size;
18461 DW_BLOCK (attr) = blk;
18462 break;
18463 case DW_FORM_block4:
7b5a2f43 18464 blk = dwarf_alloc_block (cu);
c906108c
SS
18465 blk->size = read_4_bytes (abfd, info_ptr);
18466 info_ptr += 4;
18467 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18468 info_ptr += blk->size;
18469 DW_BLOCK (attr) = blk;
18470 break;
18471 case DW_FORM_data2:
18472 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18473 info_ptr += 2;
18474 break;
18475 case DW_FORM_data4:
18476 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18477 info_ptr += 4;
18478 break;
18479 case DW_FORM_data8:
18480 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18481 info_ptr += 8;
18482 break;
0224619f
JK
18483 case DW_FORM_data16:
18484 blk = dwarf_alloc_block (cu);
18485 blk->size = 16;
18486 blk->data = read_n_bytes (abfd, info_ptr, 16);
18487 info_ptr += 16;
18488 DW_BLOCK (attr) = blk;
18489 break;
2dc7f7b3 18490 case DW_FORM_sec_offset:
8266302d 18491 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18492 info_ptr += bytes_read;
18493 break;
c906108c 18494 case DW_FORM_string:
9b1c24c8 18495 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18496 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18497 info_ptr += bytes_read;
18498 break;
4bdf3d34 18499 case DW_FORM_strp:
36586728
TT
18500 if (!cu->per_cu->is_dwz)
18501 {
ed2dc618
SM
18502 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18503 abfd, info_ptr, cu_header,
36586728
TT
18504 &bytes_read);
18505 DW_STRING_IS_CANONICAL (attr) = 0;
18506 info_ptr += bytes_read;
18507 break;
18508 }
18509 /* FALLTHROUGH */
43988095
JK
18510 case DW_FORM_line_strp:
18511 if (!cu->per_cu->is_dwz)
18512 {
ed2dc618
SM
18513 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18514 abfd, info_ptr,
43988095
JK
18515 cu_header, &bytes_read);
18516 DW_STRING_IS_CANONICAL (attr) = 0;
18517 info_ptr += bytes_read;
18518 break;
18519 }
18520 /* FALLTHROUGH */
36586728
TT
18521 case DW_FORM_GNU_strp_alt:
18522 {
ed2dc618 18523 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18524 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18525 &bytes_read);
36586728 18526
ed2dc618
SM
18527 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18528 dwz, str_offset);
36586728
TT
18529 DW_STRING_IS_CANONICAL (attr) = 0;
18530 info_ptr += bytes_read;
18531 }
4bdf3d34 18532 break;
2dc7f7b3 18533 case DW_FORM_exprloc:
c906108c 18534 case DW_FORM_block:
7b5a2f43 18535 blk = dwarf_alloc_block (cu);
c906108c
SS
18536 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18537 info_ptr += bytes_read;
18538 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18539 info_ptr += blk->size;
18540 DW_BLOCK (attr) = blk;
18541 break;
18542 case DW_FORM_block1:
7b5a2f43 18543 blk = dwarf_alloc_block (cu);
c906108c
SS
18544 blk->size = read_1_byte (abfd, info_ptr);
18545 info_ptr += 1;
18546 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18547 info_ptr += blk->size;
18548 DW_BLOCK (attr) = blk;
18549 break;
18550 case DW_FORM_data1:
18551 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18552 info_ptr += 1;
18553 break;
18554 case DW_FORM_flag:
18555 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18556 info_ptr += 1;
18557 break;
2dc7f7b3
TT
18558 case DW_FORM_flag_present:
18559 DW_UNSND (attr) = 1;
18560 break;
c906108c
SS
18561 case DW_FORM_sdata:
18562 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18563 info_ptr += bytes_read;
18564 break;
18565 case DW_FORM_udata:
18a8505e 18566 case DW_FORM_rnglistx:
c906108c
SS
18567 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18568 info_ptr += bytes_read;
18569 break;
18570 case DW_FORM_ref1:
9c541725 18571 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18572 + read_1_byte (abfd, info_ptr));
c906108c
SS
18573 info_ptr += 1;
18574 break;
18575 case DW_FORM_ref2:
9c541725 18576 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18577 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18578 info_ptr += 2;
18579 break;
18580 case DW_FORM_ref4:
9c541725 18581 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18582 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18583 info_ptr += 4;
18584 break;
613e1657 18585 case DW_FORM_ref8:
9c541725 18586 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18587 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18588 info_ptr += 8;
18589 break;
55f1336d 18590 case DW_FORM_ref_sig8:
ac9ec31b 18591 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18592 info_ptr += 8;
18593 break;
c906108c 18594 case DW_FORM_ref_udata:
9c541725 18595 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18596 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18597 info_ptr += bytes_read;
18598 break;
c906108c 18599 case DW_FORM_indirect:
a8329558
KW
18600 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18601 info_ptr += bytes_read;
43988095
JK
18602 if (form == DW_FORM_implicit_const)
18603 {
18604 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18605 info_ptr += bytes_read;
18606 }
18607 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18608 info_ptr, need_reprocess);
43988095
JK
18609 break;
18610 case DW_FORM_implicit_const:
18611 DW_SND (attr) = implicit_const;
a8329558 18612 break;
336d760d 18613 case DW_FORM_addrx:
3019eac3 18614 case DW_FORM_GNU_addr_index:
18a8505e
AT
18615 *need_reprocess = true;
18616 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18617 info_ptr += bytes_read;
18618 break;
cf532bd1 18619 case DW_FORM_strx:
15f18d14
AT
18620 case DW_FORM_strx1:
18621 case DW_FORM_strx2:
18622 case DW_FORM_strx3:
18623 case DW_FORM_strx4:
3019eac3 18624 case DW_FORM_GNU_str_index:
3019eac3 18625 {
15f18d14
AT
18626 ULONGEST str_index;
18627 if (form == DW_FORM_strx1)
18628 {
18629 str_index = read_1_byte (abfd, info_ptr);
18630 info_ptr += 1;
18631 }
18632 else if (form == DW_FORM_strx2)
18633 {
18634 str_index = read_2_bytes (abfd, info_ptr);
18635 info_ptr += 2;
18636 }
18637 else if (form == DW_FORM_strx3)
18638 {
18639 str_index = read_3_bytes (abfd, info_ptr);
18640 info_ptr += 3;
18641 }
18642 else if (form == DW_FORM_strx4)
18643 {
18644 str_index = read_4_bytes (abfd, info_ptr);
18645 info_ptr += 4;
18646 }
18647 else
18648 {
18649 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18650 info_ptr += bytes_read;
18651 }
18a8505e
AT
18652 *need_reprocess = true;
18653 DW_UNSND (attr) = str_index;
18654 }
3019eac3 18655 break;
c906108c 18656 default:
8a3fe4f8 18657 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18658 dwarf_form_name (form),
18659 bfd_get_filename (abfd));
c906108c 18660 }
28e94949 18661
36586728 18662 /* Super hack. */
cd6c91b4 18663 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18664 attr->form = DW_FORM_GNU_ref_alt;
18665
28e94949
JB
18666 /* We have seen instances where the compiler tried to emit a byte
18667 size attribute of -1 which ended up being encoded as an unsigned
18668 0xffffffff. Although 0xffffffff is technically a valid size value,
18669 an object of this size seems pretty unlikely so we can relatively
18670 safely treat these cases as if the size attribute was invalid and
18671 treat them as zero by default. */
18672 if (attr->name == DW_AT_byte_size
18673 && form == DW_FORM_data4
18674 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18675 {
18676 complaint
b98664d3 18677 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18678 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18679 DW_UNSND (attr) = 0;
18680 }
28e94949 18681
c906108c
SS
18682 return info_ptr;
18683}
18684
a8329558
KW
18685/* Read an attribute described by an abbreviated attribute. */
18686
d521ce57 18687static const gdb_byte *
dee91e82
DE
18688read_attribute (const struct die_reader_specs *reader,
18689 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18690 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18691{
18692 attr->name = abbrev->name;
43988095 18693 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18694 abbrev->implicit_const, info_ptr,
18695 need_reprocess);
a8329558
KW
18696}
18697
c764a876
DE
18698/* Cover function for read_initial_length.
18699 Returns the length of the object at BUF, and stores the size of the
18700 initial length in *BYTES_READ and stores the size that offsets will be in
18701 *OFFSET_SIZE.
18702 If the initial length size is not equivalent to that specified in
18703 CU_HEADER then issue a complaint.
18704 This is useful when reading non-comp-unit headers. */
dd373385 18705
c764a876 18706static LONGEST
d521ce57 18707read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18708 const struct comp_unit_head *cu_header,
18709 unsigned int *bytes_read,
18710 unsigned int *offset_size)
18711{
18712 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18713
18714 gdb_assert (cu_header->initial_length_size == 4
18715 || cu_header->initial_length_size == 8
18716 || cu_header->initial_length_size == 12);
18717
18718 if (cu_header->initial_length_size != *bytes_read)
b98664d3 18719 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18720
c764a876 18721 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18722 return length;
613e1657
KB
18723}
18724
43988095
JK
18725/* Return pointer to string at section SECT offset STR_OFFSET with error
18726 reporting strings FORM_NAME and SECT_NAME. */
18727
d521ce57 18728static const char *
ed2dc618
SM
18729read_indirect_string_at_offset_from (struct objfile *objfile,
18730 bfd *abfd, LONGEST str_offset,
43988095
JK
18731 struct dwarf2_section_info *sect,
18732 const char *form_name,
18733 const char *sect_name)
18734{
96b79293 18735 sect->read (objfile);
43988095
JK
18736 if (sect->buffer == NULL)
18737 error (_("%s used without %s section [in module %s]"),
18738 form_name, sect_name, bfd_get_filename (abfd));
18739 if (str_offset >= sect->size)
18740 error (_("%s pointing outside of %s section [in module %s]"),
18741 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18742 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18743 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18744 return NULL;
43988095
JK
18745 return (const char *) (sect->buffer + str_offset);
18746}
18747
18748/* Return pointer to string at .debug_str offset STR_OFFSET. */
18749
18750static const char *
ed2dc618
SM
18751read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18752 bfd *abfd, LONGEST str_offset)
43988095 18753{
ed2dc618
SM
18754 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18755 abfd, str_offset,
43988095
JK
18756 &dwarf2_per_objfile->str,
18757 "DW_FORM_strp", ".debug_str");
18758}
18759
18760/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18761
18762static const char *
ed2dc618
SM
18763read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18764 bfd *abfd, LONGEST str_offset)
43988095 18765{
ed2dc618
SM
18766 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18767 abfd, str_offset,
43988095
JK
18768 &dwarf2_per_objfile->line_str,
18769 "DW_FORM_line_strp",
18770 ".debug_line_str");
c906108c
SS
18771}
18772
36586728
TT
18773/* Read a string at offset STR_OFFSET in the .debug_str section from
18774 the .dwz file DWZ. Throw an error if the offset is too large. If
18775 the string consists of a single NUL byte, return NULL; otherwise
18776 return a pointer to the string. */
18777
d521ce57 18778static const char *
ed2dc618
SM
18779read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18780 LONGEST str_offset)
36586728 18781{
96b79293 18782 dwz->str.read (objfile);
36586728
TT
18783
18784 if (dwz->str.buffer == NULL)
18785 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18786 "section [in module %s]"),
00f93c44 18787 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18788 if (str_offset >= dwz->str.size)
18789 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18790 ".debug_str section [in module %s]"),
00f93c44 18791 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18792 gdb_assert (HOST_CHAR_BIT == 8);
18793 if (dwz->str.buffer[str_offset] == '\0')
18794 return NULL;
d521ce57 18795 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
18796}
18797
43988095
JK
18798/* Return pointer to string at .debug_str offset as read from BUF.
18799 BUF is assumed to be in a compilation unit described by CU_HEADER.
18800 Return *BYTES_READ_PTR count of bytes read from BUF. */
18801
d521ce57 18802static const char *
ed2dc618
SM
18803read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18804 const gdb_byte *buf,
cf2c3c16
TT
18805 const struct comp_unit_head *cu_header,
18806 unsigned int *bytes_read_ptr)
18807{
8266302d 18808 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18809
ed2dc618 18810 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
18811}
18812
43988095
JK
18813/* Return pointer to string at .debug_line_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
18817static const char *
ed2dc618
SM
18818read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18819 bfd *abfd, const gdb_byte *buf,
43988095
JK
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);
43988095 18824
ed2dc618
SM
18825 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18826 str_offset);
43988095
JK
18827}
18828
3019eac3 18829/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18830 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18831 ADDR_SIZE is the size of addresses from the CU header. */
18832
18833static CORE_ADDR
ed2dc618 18834read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18835 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18836 int addr_size)
3019eac3
DE
18837{
18838 struct objfile *objfile = dwarf2_per_objfile->objfile;
18839 bfd *abfd = objfile->obfd;
18840 const gdb_byte *info_ptr;
18a8505e 18841 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18842
96b79293 18843 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18844 if (dwarf2_per_objfile->addr.buffer == NULL)
18845 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18846 objfile_name (objfile));
18a8505e
AT
18847 if (addr_base_or_zero + addr_index * addr_size
18848 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18849 error (_("DW_FORM_addr_index pointing outside of "
18850 ".debug_addr section [in module %s]"),
4262abfb 18851 objfile_name (objfile));
3019eac3 18852 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18853 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18854 if (addr_size == 4)
18855 return bfd_get_32 (abfd, info_ptr);
18856 else
18857 return bfd_get_64 (abfd, info_ptr);
18858}
18859
18860/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18861
18862static CORE_ADDR
18863read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18864{
518817b3
SM
18865 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18866 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18867}
18868
18869/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18870
18871static CORE_ADDR
d521ce57 18872read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18873 unsigned int *bytes_read)
18874{
518817b3 18875 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18876 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18877
18878 return read_addr_index (cu, addr_index);
18879}
18880
3019eac3
DE
18881/* Given an index in .debug_addr, fetch the value.
18882 NOTE: This can be called during dwarf expression evaluation,
18883 long after the debug information has been read, and thus per_cu->cu
18884 may no longer exist. */
18885
18886CORE_ADDR
18887dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18888 unsigned int addr_index)
18889{
ed2dc618 18890 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18891 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18892 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18893 int addr_size;
18894
3019eac3
DE
18895 /* We need addr_base and addr_size.
18896 If we don't have PER_CU->cu, we have to get it.
18897 Nasty, but the alternative is storing the needed info in PER_CU,
18898 which at this point doesn't seem justified: it's not clear how frequently
18899 it would get used and it would increase the size of every PER_CU.
18900 Entry points like dwarf2_per_cu_addr_size do a similar thing
18901 so we're not in uncharted territory here.
18902 Alas we need to be a bit more complicated as addr_base is contained
18903 in the DIE.
18904
18905 We don't need to read the entire CU(/TU).
18906 We just need the header and top level die.
a1b64ce1 18907
3019eac3 18908 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18909 For now we skip this optimization. */
3019eac3
DE
18910
18911 if (cu != NULL)
18912 {
18913 addr_base = cu->addr_base;
18914 addr_size = cu->header.addr_size;
18915 }
18916 else
18917 {
6751ebae 18918 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18919 addr_base = reader.cu->addr_base;
18920 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18921 }
18922
ed2dc618
SM
18923 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18924 addr_size);
3019eac3
DE
18925}
18926
18a8505e
AT
18927/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18928 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18929 DWO file. */
3019eac3 18930
d521ce57 18931static const char *
18a8505e
AT
18932read_str_index (struct dwarf2_cu *cu,
18933 struct dwarf2_section_info *str_section,
18934 struct dwarf2_section_info *str_offsets_section,
18935 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18936{
518817b3
SM
18937 struct dwarf2_per_objfile *dwarf2_per_objfile
18938 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18939 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18940 const char *objf_name = objfile_name (objfile);
3019eac3 18941 bfd *abfd = objfile->obfd;
d521ce57 18942 const gdb_byte *info_ptr;
3019eac3 18943 ULONGEST str_offset;
cf532bd1 18944 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18945
96b79293
TT
18946 str_section->read (objfile);
18947 str_offsets_section->read (objfile);
73869dc2 18948 if (str_section->buffer == NULL)
18a8505e 18949 error (_("%s used without %s section"
9d8780f0 18950 " in CU at offset %s [in module %s]"),
96b79293 18951 form_name, str_section->get_name (),
18a8505e 18952 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18953 if (str_offsets_section->buffer == NULL)
18a8505e 18954 error (_("%s used without %s section"
9d8780f0 18955 " in CU at offset %s [in module %s]"),
96b79293 18956 form_name, str_section->get_name (),
18a8505e 18957 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18958 info_ptr = (str_offsets_section->buffer
18a8505e 18959 + str_offsets_base
3019eac3
DE
18960 + str_index * cu->header.offset_size);
18961 if (cu->header.offset_size == 4)
18962 str_offset = bfd_get_32 (abfd, info_ptr);
18963 else
18964 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18965 if (str_offset >= str_section->size)
57d63ce2 18966 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18967 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18968 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18969 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18970}
18971
18a8505e
AT
18972/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18973
18974static const char *
18975read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18976{
18977 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18978 ? reader->cu->header.addr_size : 0;
18979 return read_str_index (reader->cu,
18980 &reader->dwo_file->sections.str,
18981 &reader->dwo_file->sections.str_offsets,
18982 str_offsets_base, str_index);
18983}
18984
18985/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18986
18987static const char *
18988read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18989{
18990 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18991 const char *objf_name = objfile_name (objfile);
18992 static const char form_name[] = "DW_FORM_GNU_str_index";
18993 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18994
18995 if (!cu->str_offsets_base.has_value ())
18996 error (_("%s used in Fission stub without %s"
18997 " in CU at offset 0x%lx [in module %s]"),
18998 form_name, str_offsets_attr_name,
18999 (long) cu->header.offset_size, objf_name);
19000
19001 return read_str_index (cu,
19002 &cu->per_cu->dwarf2_per_objfile->str,
19003 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19004 *cu->str_offsets_base, str_index);
19005}
19006
3019eac3
DE
19007/* Return the length of an LEB128 number in BUF. */
19008
19009static int
19010leb128_size (const gdb_byte *buf)
19011{
19012 const gdb_byte *begin = buf;
19013 gdb_byte byte;
19014
19015 while (1)
19016 {
19017 byte = *buf++;
19018 if ((byte & 128) == 0)
19019 return buf - begin;
19020 }
19021}
19022
c906108c 19023static void
e142c38c 19024set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19025{
19026 switch (lang)
19027 {
19028 case DW_LANG_C89:
76bee0cc 19029 case DW_LANG_C99:
0cfd832f 19030 case DW_LANG_C11:
c906108c 19031 case DW_LANG_C:
d1be3247 19032 case DW_LANG_UPC:
e142c38c 19033 cu->language = language_c;
c906108c 19034 break;
9c37b5ae 19035 case DW_LANG_Java:
c906108c 19036 case DW_LANG_C_plus_plus:
0cfd832f
MW
19037 case DW_LANG_C_plus_plus_11:
19038 case DW_LANG_C_plus_plus_14:
e142c38c 19039 cu->language = language_cplus;
c906108c 19040 break;
6aecb9c2
JB
19041 case DW_LANG_D:
19042 cu->language = language_d;
19043 break;
c906108c
SS
19044 case DW_LANG_Fortran77:
19045 case DW_LANG_Fortran90:
b21b22e0 19046 case DW_LANG_Fortran95:
f7de9aab
MW
19047 case DW_LANG_Fortran03:
19048 case DW_LANG_Fortran08:
e142c38c 19049 cu->language = language_fortran;
c906108c 19050 break;
a766d390
DE
19051 case DW_LANG_Go:
19052 cu->language = language_go;
19053 break;
c906108c 19054 case DW_LANG_Mips_Assembler:
e142c38c 19055 cu->language = language_asm;
c906108c
SS
19056 break;
19057 case DW_LANG_Ada83:
8aaf0b47 19058 case DW_LANG_Ada95:
bc5f45f8
JB
19059 cu->language = language_ada;
19060 break;
72019c9c
GM
19061 case DW_LANG_Modula2:
19062 cu->language = language_m2;
19063 break;
fe8e67fd
PM
19064 case DW_LANG_Pascal83:
19065 cu->language = language_pascal;
19066 break;
22566fbd
DJ
19067 case DW_LANG_ObjC:
19068 cu->language = language_objc;
19069 break;
c44af4eb
TT
19070 case DW_LANG_Rust:
19071 case DW_LANG_Rust_old:
19072 cu->language = language_rust;
19073 break;
c906108c
SS
19074 case DW_LANG_Cobol74:
19075 case DW_LANG_Cobol85:
c906108c 19076 default:
e142c38c 19077 cu->language = language_minimal;
c906108c
SS
19078 break;
19079 }
e142c38c 19080 cu->language_defn = language_def (cu->language);
c906108c
SS
19081}
19082
19083/* Return the named attribute or NULL if not there. */
19084
19085static struct attribute *
e142c38c 19086dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19087{
a48e046c 19088 for (;;)
c906108c 19089 {
a48e046c
TT
19090 unsigned int i;
19091 struct attribute *spec = NULL;
19092
19093 for (i = 0; i < die->num_attrs; ++i)
19094 {
19095 if (die->attrs[i].name == name)
19096 return &die->attrs[i];
19097 if (die->attrs[i].name == DW_AT_specification
19098 || die->attrs[i].name == DW_AT_abstract_origin)
19099 spec = &die->attrs[i];
19100 }
19101
19102 if (!spec)
19103 break;
c906108c 19104
f2f0e013 19105 die = follow_die_ref (die, spec, &cu);
f2f0e013 19106 }
c5aa993b 19107
c906108c
SS
19108 return NULL;
19109}
19110
348e048f
DE
19111/* Return the named attribute or NULL if not there,
19112 but do not follow DW_AT_specification, etc.
19113 This is for use in contexts where we're reading .debug_types dies.
19114 Following DW_AT_specification, DW_AT_abstract_origin will take us
19115 back up the chain, and we want to go down. */
19116
19117static struct attribute *
45e58e77 19118dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19119{
19120 unsigned int i;
19121
19122 for (i = 0; i < die->num_attrs; ++i)
19123 if (die->attrs[i].name == name)
19124 return &die->attrs[i];
19125
19126 return NULL;
19127}
19128
7d45c7c3
KB
19129/* Return the string associated with a string-typed attribute, or NULL if it
19130 is either not found or is of an incorrect type. */
19131
19132static const char *
19133dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19134{
19135 struct attribute *attr;
19136 const char *str = NULL;
19137
19138 attr = dwarf2_attr (die, name, cu);
19139
19140 if (attr != NULL)
19141 {
43988095 19142 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19143 || attr->form == DW_FORM_string
cf532bd1 19144 || attr->form == DW_FORM_strx
8fe0f950
AT
19145 || attr->form == DW_FORM_strx1
19146 || attr->form == DW_FORM_strx2
19147 || attr->form == DW_FORM_strx3
19148 || attr->form == DW_FORM_strx4
b3340438 19149 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19150 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19151 str = DW_STRING (attr);
19152 else
b98664d3 19153 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19154 "DIE at %s in module %s"),
19155 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19156 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19157 }
19158
19159 return str;
19160}
19161
a084a2a6 19162/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19163 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19164static const char *
19165dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19166{
19167 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19168 if (dwo_name == nullptr)
19169 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19170 return dwo_name;
19171}
19172
05cf31d1
JB
19173/* Return non-zero iff the attribute NAME is defined for the given DIE,
19174 and holds a non-zero value. This function should only be used for
2dc7f7b3 19175 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19176
19177static int
19178dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19179{
19180 struct attribute *attr = dwarf2_attr (die, name, cu);
19181
19182 return (attr && DW_UNSND (attr));
19183}
19184
3ca72b44 19185static int
e142c38c 19186die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19187{
05cf31d1
JB
19188 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19189 which value is non-zero. However, we have to be careful with
19190 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19191 (via dwarf2_flag_true_p) follows this attribute. So we may
19192 end up accidently finding a declaration attribute that belongs
19193 to a different DIE referenced by the specification attribute,
19194 even though the given DIE does not have a declaration attribute. */
19195 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19196 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19197}
19198
63d06c5c 19199/* Return the die giving the specification for DIE, if there is
f2f0e013 19200 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19201 containing the return value on output. If there is no
19202 specification, but there is an abstract origin, that is
19203 returned. */
63d06c5c
DC
19204
19205static struct die_info *
f2f0e013 19206die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19207{
f2f0e013
DJ
19208 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19209 *spec_cu);
63d06c5c 19210
edb3359d
DJ
19211 if (spec_attr == NULL)
19212 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19213
63d06c5c
DC
19214 if (spec_attr == NULL)
19215 return NULL;
19216 else
f2f0e013 19217 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19218}
c906108c 19219
527f3840
JK
19220/* Stub for free_line_header to match void * callback types. */
19221
19222static void
19223free_line_header_voidp (void *arg)
19224{
9a3c8263 19225 struct line_header *lh = (struct line_header *) arg;
527f3840 19226
fff8551c 19227 delete lh;
527f3840
JK
19228}
19229
83769d0b 19230/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19231
19232static struct dwarf2_section_info *
19233get_debug_line_section (struct dwarf2_cu *cu)
19234{
19235 struct dwarf2_section_info *section;
518817b3
SM
19236 struct dwarf2_per_objfile *dwarf2_per_objfile
19237 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19238
19239 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19240 DWO file. */
19241 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19242 section = &cu->dwo_unit->dwo_file->sections.line;
19243 else if (cu->per_cu->is_dwz)
19244 {
ed2dc618 19245 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19246
19247 section = &dwz->line;
19248 }
19249 else
19250 section = &dwarf2_per_objfile->line;
19251
19252 return section;
19253}
19254
43988095
JK
19255/* Read directory or file name entry format, starting with byte of
19256 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19257 entries count and the entries themselves in the described entry
19258 format. */
19259
19260static void
ed2dc618
SM
19261read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19262 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19263 struct line_header *lh,
19264 const struct comp_unit_head *cu_header,
19265 void (*callback) (struct line_header *lh,
19266 const char *name,
ecfb656c 19267 dir_index d_index,
43988095
JK
19268 unsigned int mod_time,
19269 unsigned int length))
19270{
19271 gdb_byte format_count, formati;
19272 ULONGEST data_count, datai;
19273 const gdb_byte *buf = *bufp;
19274 const gdb_byte *format_header_data;
43988095
JK
19275 unsigned int bytes_read;
19276
19277 format_count = read_1_byte (abfd, buf);
19278 buf += 1;
19279 format_header_data = buf;
19280 for (formati = 0; formati < format_count; formati++)
19281 {
19282 read_unsigned_leb128 (abfd, buf, &bytes_read);
19283 buf += bytes_read;
19284 read_unsigned_leb128 (abfd, buf, &bytes_read);
19285 buf += bytes_read;
19286 }
19287
19288 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19289 buf += bytes_read;
19290 for (datai = 0; datai < data_count; datai++)
19291 {
19292 const gdb_byte *format = format_header_data;
19293 struct file_entry fe;
19294
43988095
JK
19295 for (formati = 0; formati < format_count; formati++)
19296 {
ecfb656c 19297 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19298 format += bytes_read;
43988095 19299
ecfb656c 19300 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19301 format += bytes_read;
ecfb656c
PA
19302
19303 gdb::optional<const char *> string;
19304 gdb::optional<unsigned int> uint;
19305
43988095
JK
19306 switch (form)
19307 {
19308 case DW_FORM_string:
ecfb656c 19309 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19310 buf += bytes_read;
19311 break;
19312
19313 case DW_FORM_line_strp:
ed2dc618
SM
19314 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19315 abfd, buf,
ecfb656c
PA
19316 cu_header,
19317 &bytes_read));
43988095
JK
19318 buf += bytes_read;
19319 break;
19320
19321 case DW_FORM_data1:
ecfb656c 19322 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19323 buf += 1;
19324 break;
19325
19326 case DW_FORM_data2:
ecfb656c 19327 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19328 buf += 2;
19329 break;
19330
19331 case DW_FORM_data4:
ecfb656c 19332 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19333 buf += 4;
19334 break;
19335
19336 case DW_FORM_data8:
ecfb656c 19337 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19338 buf += 8;
19339 break;
19340
7ba99d21
AT
19341 case DW_FORM_data16:
19342 /* This is used for MD5, but file_entry does not record MD5s. */
19343 buf += 16;
19344 break;
19345
43988095 19346 case DW_FORM_udata:
ecfb656c 19347 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19348 buf += bytes_read;
19349 break;
19350
19351 case DW_FORM_block:
19352 /* It is valid only for DW_LNCT_timestamp which is ignored by
19353 current GDB. */
19354 break;
19355 }
ecfb656c
PA
19356
19357 switch (content_type)
19358 {
19359 case DW_LNCT_path:
19360 if (string.has_value ())
19361 fe.name = *string;
19362 break;
19363 case DW_LNCT_directory_index:
19364 if (uint.has_value ())
19365 fe.d_index = (dir_index) *uint;
19366 break;
19367 case DW_LNCT_timestamp:
19368 if (uint.has_value ())
19369 fe.mod_time = *uint;
19370 break;
19371 case DW_LNCT_size:
19372 if (uint.has_value ())
19373 fe.length = *uint;
19374 break;
19375 case DW_LNCT_MD5:
19376 break;
19377 default:
b98664d3 19378 complaint (_("Unknown format content type %s"),
ecfb656c
PA
19379 pulongest (content_type));
19380 }
43988095
JK
19381 }
19382
ecfb656c 19383 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
19384 }
19385
19386 *bufp = buf;
19387}
19388
debd256d 19389/* Read the statement program header starting at OFFSET in
3019eac3 19390 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19391 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19392 Returns NULL if there is a problem reading the header, e.g., if it
19393 has a version we don't understand.
debd256d
JB
19394
19395 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19396 the returned object point into the dwarf line section buffer,
19397 and must not be freed. */
ae2de4f8 19398
fff8551c 19399static line_header_up
9c541725 19400dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19401{
d521ce57 19402 const gdb_byte *line_ptr;
c764a876 19403 unsigned int bytes_read, offset_size;
debd256d 19404 int i;
d521ce57 19405 const char *cur_dir, *cur_file;
3019eac3
DE
19406 struct dwarf2_section_info *section;
19407 bfd *abfd;
518817b3
SM
19408 struct dwarf2_per_objfile *dwarf2_per_objfile
19409 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19410
36586728 19411 section = get_debug_line_section (cu);
96b79293 19412 section->read (dwarf2_per_objfile->objfile);
3019eac3 19413 if (section->buffer == NULL)
debd256d 19414 {
3019eac3 19415 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19416 complaint (_("missing .debug_line.dwo section"));
3019eac3 19417 else
b98664d3 19418 complaint (_("missing .debug_line section"));
debd256d
JB
19419 return 0;
19420 }
19421
fceca515
DE
19422 /* We can't do this until we know the section is non-empty.
19423 Only then do we know we have such a section. */
96b79293 19424 abfd = section->get_bfd_owner ();
fceca515 19425
a738430d
MK
19426 /* Make sure that at least there's room for the total_length field.
19427 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 19428 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 19429 {
4d3c2250 19430 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19431 return 0;
19432 }
19433
fff8551c 19434 line_header_up lh (new line_header ());
debd256d 19435
9c541725 19436 lh->sect_off = sect_off;
527f3840
JK
19437 lh->offset_in_dwz = cu->per_cu->is_dwz;
19438
9c541725 19439 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 19440
a738430d 19441 /* Read in the header. */
6e70227d 19442 lh->total_length =
c764a876
DE
19443 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19444 &bytes_read, &offset_size);
debd256d 19445 line_ptr += bytes_read;
7ba99d21
AT
19446
19447 const gdb_byte *start_here = line_ptr;
19448
3019eac3 19449 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 19450 {
4d3c2250 19451 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19452 return 0;
19453 }
7ba99d21 19454 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
19455 lh->version = read_2_bytes (abfd, line_ptr);
19456 line_ptr += 2;
43988095 19457 if (lh->version > 5)
cd366ee8
DE
19458 {
19459 /* This is a version we don't understand. The format could have
19460 changed in ways we don't handle properly so just punt. */
b98664d3 19461 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
19462 return NULL;
19463 }
43988095
JK
19464 if (lh->version >= 5)
19465 {
19466 gdb_byte segment_selector_size;
19467
19468 /* Skip address size. */
19469 read_1_byte (abfd, line_ptr);
19470 line_ptr += 1;
19471
19472 segment_selector_size = read_1_byte (abfd, line_ptr);
19473 line_ptr += 1;
19474 if (segment_selector_size != 0)
19475 {
b98664d3 19476 complaint (_("unsupported segment selector size %u "
43988095
JK
19477 "in .debug_line section"),
19478 segment_selector_size);
19479 return NULL;
19480 }
19481 }
24aa364d 19482 lh->header_length = read_offset (abfd, line_ptr, offset_size);
c764a876 19483 line_ptr += offset_size;
7ba99d21 19484 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
19485 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19486 line_ptr += 1;
2dc7f7b3
TT
19487 if (lh->version >= 4)
19488 {
19489 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19490 line_ptr += 1;
19491 }
19492 else
19493 lh->maximum_ops_per_instruction = 1;
19494
19495 if (lh->maximum_ops_per_instruction == 0)
19496 {
19497 lh->maximum_ops_per_instruction = 1;
b98664d3 19498 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 19499 "in `.debug_line' section"));
2dc7f7b3
TT
19500 }
19501
debd256d
JB
19502 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19503 line_ptr += 1;
19504 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19505 line_ptr += 1;
19506 lh->line_range = read_1_byte (abfd, line_ptr);
19507 line_ptr += 1;
19508 lh->opcode_base = read_1_byte (abfd, line_ptr);
19509 line_ptr += 1;
fff8551c 19510 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
19511
19512 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19513 for (i = 1; i < lh->opcode_base; ++i)
19514 {
19515 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19516 line_ptr += 1;
19517 }
19518
43988095 19519 if (lh->version >= 5)
debd256d 19520 {
43988095 19521 /* Read directory table. */
ed2dc618
SM
19522 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19523 &cu->header,
b926417a 19524 [] (struct line_header *header, const char *name,
ecfb656c 19525 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19526 unsigned int length)
19527 {
b926417a 19528 header->add_include_dir (name);
fff8551c 19529 });
debd256d 19530
43988095 19531 /* Read file name table. */
ed2dc618
SM
19532 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19533 &cu->header,
b926417a 19534 [] (struct line_header *header, const char *name,
ecfb656c 19535 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19536 unsigned int length)
19537 {
b926417a 19538 header->add_file_name (name, d_index, mod_time, length);
fff8551c 19539 });
43988095
JK
19540 }
19541 else
debd256d 19542 {
43988095
JK
19543 /* Read directory table. */
19544 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19545 {
19546 line_ptr += bytes_read;
fff8551c 19547 lh->add_include_dir (cur_dir);
43988095 19548 }
debd256d
JB
19549 line_ptr += bytes_read;
19550
43988095
JK
19551 /* Read file name table. */
19552 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19553 {
ecfb656c
PA
19554 unsigned int mod_time, length;
19555 dir_index d_index;
43988095
JK
19556
19557 line_ptr += bytes_read;
ecfb656c 19558 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
19559 line_ptr += bytes_read;
19560 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19561 line_ptr += bytes_read;
19562 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19563 line_ptr += bytes_read;
19564
ecfb656c 19565 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
19566 }
19567 line_ptr += bytes_read;
debd256d 19568 }
debd256d 19569
3019eac3 19570 if (line_ptr > (section->buffer + section->size))
b98664d3 19571 complaint (_("line number info header doesn't "
3e43a32a 19572 "fit in `.debug_line' section"));
debd256d 19573
debd256d
JB
19574 return lh;
19575}
c906108c 19576
c6da4cef 19577/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19578 Return the file name of the psymtab for the given file_entry.
c6da4cef 19579 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19580 If space for the result is malloc'd, *NAME_HOLDER will be set.
19581 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19582
d521ce57 19583static const char *
7ba99d21 19584psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19585 const dwarf2_psymtab *pst,
c89b44cd
TT
19586 const char *comp_dir,
19587 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19588{
d521ce57
TT
19589 const char *include_name = fe.name;
19590 const char *include_name_to_compare = include_name;
72b9f47f 19591 const char *pst_filename;
c6da4cef
DE
19592 int file_is_pst;
19593
8c43009f 19594 const char *dir_name = fe.include_dir (lh);
c6da4cef 19595
c89b44cd 19596 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19597 if (!IS_ABSOLUTE_PATH (include_name)
19598 && (dir_name != NULL || comp_dir != NULL))
19599 {
19600 /* Avoid creating a duplicate psymtab for PST.
19601 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19602 Before we do the comparison, however, we need to account
19603 for DIR_NAME and COMP_DIR.
19604 First prepend dir_name (if non-NULL). If we still don't
19605 have an absolute path prepend comp_dir (if non-NULL).
19606 However, the directory we record in the include-file's
19607 psymtab does not contain COMP_DIR (to match the
19608 corresponding symtab(s)).
19609
19610 Example:
19611
19612 bash$ cd /tmp
19613 bash$ gcc -g ./hello.c
19614 include_name = "hello.c"
19615 dir_name = "."
19616 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19617 DW_AT_name = "./hello.c"
19618
19619 */
c6da4cef
DE
19620
19621 if (dir_name != NULL)
19622 {
c89b44cd
TT
19623 name_holder->reset (concat (dir_name, SLASH_STRING,
19624 include_name, (char *) NULL));
19625 include_name = name_holder->get ();
c6da4cef 19626 include_name_to_compare = include_name;
c6da4cef
DE
19627 }
19628 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19629 {
c89b44cd
TT
19630 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19631 include_name, (char *) NULL));
19632 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19633 }
19634 }
19635
19636 pst_filename = pst->filename;
c89b44cd 19637 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19638 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19639 {
c89b44cd
TT
19640 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19641 pst_filename, (char *) NULL));
19642 pst_filename = copied_name.get ();
c6da4cef
DE
19643 }
19644
1e3fad37 19645 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19646
c6da4cef
DE
19647 if (file_is_pst)
19648 return NULL;
19649 return include_name;
19650}
19651
d9b3de22
DE
19652/* State machine to track the state of the line number program. */
19653
6f77053d 19654class lnp_state_machine
d9b3de22 19655{
6f77053d
PA
19656public:
19657 /* Initialize a machine state for the start of a line number
19658 program. */
804d2729
TT
19659 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19660 bool record_lines_p);
6f77053d 19661
8c43009f
PA
19662 file_entry *current_file ()
19663 {
19664 /* lh->file_names is 0-based, but the file name numbers in the
19665 statement program are 1-based. */
6f77053d
PA
19666 return m_line_header->file_name_at (m_file);
19667 }
19668
19669 /* Record the line in the state machine. END_SEQUENCE is true if
19670 we're processing the end of a sequence. */
19671 void record_line (bool end_sequence);
19672
7ab6656f
OJ
19673 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19674 nop-out rest of the lines in this sequence. */
6f77053d
PA
19675 void check_line_address (struct dwarf2_cu *cu,
19676 const gdb_byte *line_ptr,
7ab6656f 19677 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19678
19679 void handle_set_discriminator (unsigned int discriminator)
19680 {
19681 m_discriminator = discriminator;
19682 m_line_has_non_zero_discriminator |= discriminator != 0;
19683 }
19684
19685 /* Handle DW_LNE_set_address. */
19686 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19687 {
19688 m_op_index = 0;
19689 address += baseaddr;
19690 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19691 }
19692
19693 /* Handle DW_LNS_advance_pc. */
19694 void handle_advance_pc (CORE_ADDR adjust);
19695
19696 /* Handle a special opcode. */
19697 void handle_special_opcode (unsigned char op_code);
19698
19699 /* Handle DW_LNS_advance_line. */
19700 void handle_advance_line (int line_delta)
19701 {
19702 advance_line (line_delta);
19703 }
19704
19705 /* Handle DW_LNS_set_file. */
19706 void handle_set_file (file_name_index file);
19707
19708 /* Handle DW_LNS_negate_stmt. */
19709 void handle_negate_stmt ()
19710 {
19711 m_is_stmt = !m_is_stmt;
19712 }
19713
19714 /* Handle DW_LNS_const_add_pc. */
19715 void handle_const_add_pc ();
19716
19717 /* Handle DW_LNS_fixed_advance_pc. */
19718 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19719 {
19720 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19721 m_op_index = 0;
19722 }
19723
19724 /* Handle DW_LNS_copy. */
19725 void handle_copy ()
19726 {
19727 record_line (false);
19728 m_discriminator = 0;
19729 }
19730
19731 /* Handle DW_LNE_end_sequence. */
19732 void handle_end_sequence ()
19733 {
804d2729 19734 m_currently_recording_lines = true;
6f77053d
PA
19735 }
19736
19737private:
19738 /* Advance the line by LINE_DELTA. */
19739 void advance_line (int line_delta)
19740 {
19741 m_line += line_delta;
19742
19743 if (line_delta != 0)
19744 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19745 }
19746
804d2729
TT
19747 struct dwarf2_cu *m_cu;
19748
6f77053d
PA
19749 gdbarch *m_gdbarch;
19750
19751 /* True if we're recording lines.
19752 Otherwise we're building partial symtabs and are just interested in
19753 finding include files mentioned by the line number program. */
19754 bool m_record_lines_p;
19755
8c43009f 19756 /* The line number header. */
6f77053d 19757 line_header *m_line_header;
8c43009f 19758
6f77053d
PA
19759 /* These are part of the standard DWARF line number state machine,
19760 and initialized according to the DWARF spec. */
d9b3de22 19761
6f77053d 19762 unsigned char m_op_index = 0;
7ba99d21
AT
19763 /* The line table index of the current file. */
19764 file_name_index m_file = 1;
6f77053d
PA
19765 unsigned int m_line = 1;
19766
19767 /* These are initialized in the constructor. */
19768
19769 CORE_ADDR m_address;
19770 bool m_is_stmt;
19771 unsigned int m_discriminator;
d9b3de22
DE
19772
19773 /* Additional bits of state we need to track. */
19774
19775 /* The last file that we called dwarf2_start_subfile for.
19776 This is only used for TLLs. */
6f77053d 19777 unsigned int m_last_file = 0;
d9b3de22 19778 /* The last file a line number was recorded for. */
6f77053d 19779 struct subfile *m_last_subfile = NULL;
d9b3de22 19780
804d2729
TT
19781 /* When true, record the lines we decode. */
19782 bool m_currently_recording_lines = false;
d9b3de22
DE
19783
19784 /* The last line number that was recorded, used to coalesce
19785 consecutive entries for the same line. This can happen, for
19786 example, when discriminators are present. PR 17276. */
6f77053d
PA
19787 unsigned int m_last_line = 0;
19788 bool m_line_has_non_zero_discriminator = false;
8c43009f 19789};
d9b3de22 19790
6f77053d
PA
19791void
19792lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19793{
19794 CORE_ADDR addr_adj = (((m_op_index + adjust)
19795 / m_line_header->maximum_ops_per_instruction)
19796 * m_line_header->minimum_instruction_length);
19797 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19798 m_op_index = ((m_op_index + adjust)
19799 % m_line_header->maximum_ops_per_instruction);
19800}
d9b3de22 19801
6f77053d
PA
19802void
19803lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19804{
6f77053d
PA
19805 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19806 CORE_ADDR addr_adj = (((m_op_index
19807 + (adj_opcode / m_line_header->line_range))
19808 / m_line_header->maximum_ops_per_instruction)
19809 * m_line_header->minimum_instruction_length);
19810 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19811 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19812 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19813
6f77053d
PA
19814 int line_delta = (m_line_header->line_base
19815 + (adj_opcode % m_line_header->line_range));
19816 advance_line (line_delta);
19817 record_line (false);
19818 m_discriminator = 0;
19819}
d9b3de22 19820
6f77053d
PA
19821void
19822lnp_state_machine::handle_set_file (file_name_index file)
19823{
19824 m_file = file;
19825
19826 const file_entry *fe = current_file ();
19827 if (fe == NULL)
19828 dwarf2_debug_line_missing_file_complaint ();
19829 else if (m_record_lines_p)
19830 {
19831 const char *dir = fe->include_dir (m_line_header);
19832
c24bdb02 19833 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19834 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19835 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19836 }
19837}
19838
19839void
19840lnp_state_machine::handle_const_add_pc ()
19841{
19842 CORE_ADDR adjust
19843 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19844
19845 CORE_ADDR addr_adj
19846 = (((m_op_index + adjust)
19847 / m_line_header->maximum_ops_per_instruction)
19848 * m_line_header->minimum_instruction_length);
19849
19850 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19851 m_op_index = ((m_op_index + adjust)
19852 % m_line_header->maximum_ops_per_instruction);
19853}
d9b3de22 19854
a05a36a5
DE
19855/* Return non-zero if we should add LINE to the line number table.
19856 LINE is the line to add, LAST_LINE is the last line that was added,
19857 LAST_SUBFILE is the subfile for LAST_LINE.
19858 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19859 had a non-zero discriminator.
19860
19861 We have to be careful in the presence of discriminators.
19862 E.g., for this line:
19863
19864 for (i = 0; i < 100000; i++);
19865
19866 clang can emit four line number entries for that one line,
19867 each with a different discriminator.
19868 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19869
19870 However, we want gdb to coalesce all four entries into one.
19871 Otherwise the user could stepi into the middle of the line and
19872 gdb would get confused about whether the pc really was in the
19873 middle of the line.
19874
19875 Things are further complicated by the fact that two consecutive
19876 line number entries for the same line is a heuristic used by gcc
19877 to denote the end of the prologue. So we can't just discard duplicate
19878 entries, we have to be selective about it. The heuristic we use is
19879 that we only collapse consecutive entries for the same line if at least
19880 one of those entries has a non-zero discriminator. PR 17276.
19881
19882 Note: Addresses in the line number state machine can never go backwards
19883 within one sequence, thus this coalescing is ok. */
19884
19885static int
804d2729
TT
19886dwarf_record_line_p (struct dwarf2_cu *cu,
19887 unsigned int line, unsigned int last_line,
a05a36a5
DE
19888 int line_has_non_zero_discriminator,
19889 struct subfile *last_subfile)
19890{
c24bdb02 19891 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19892 return 1;
19893 if (line != last_line)
19894 return 1;
19895 /* Same line for the same file that we've seen already.
19896 As a last check, for pr 17276, only record the line if the line
19897 has never had a non-zero discriminator. */
19898 if (!line_has_non_zero_discriminator)
19899 return 1;
19900 return 0;
19901}
19902
804d2729
TT
19903/* Use the CU's builder to record line number LINE beginning at
19904 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19905
19906static void
d9b3de22
DE
19907dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19908 unsigned int line, CORE_ADDR address,
804d2729 19909 struct dwarf2_cu *cu)
252a6764
DE
19910{
19911 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19912
27e0867f
DE
19913 if (dwarf_line_debug)
19914 {
19915 fprintf_unfiltered (gdb_stdlog,
19916 "Recording line %u, file %s, address %s\n",
19917 line, lbasename (subfile->name),
19918 paddress (gdbarch, address));
19919 }
19920
804d2729 19921 if (cu != nullptr)
c24bdb02 19922 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
19923}
19924
19925/* Subroutine of dwarf_decode_lines_1 to simplify it.
19926 Mark the end of a set of line number records.
d9b3de22 19927 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19928 If SUBFILE is NULL the request is ignored. */
19929
19930static void
19931dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19932 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19933{
27e0867f
DE
19934 if (subfile == NULL)
19935 return;
19936
19937 if (dwarf_line_debug)
19938 {
19939 fprintf_unfiltered (gdb_stdlog,
19940 "Finishing current line, file %s, address %s\n",
19941 lbasename (subfile->name),
19942 paddress (gdbarch, address));
19943 }
19944
804d2729 19945 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
19946}
19947
6f77053d
PA
19948void
19949lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19950{
d9b3de22
DE
19951 if (dwarf_line_debug)
19952 {
19953 fprintf_unfiltered (gdb_stdlog,
19954 "Processing actual line %u: file %u,"
94a72be7 19955 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19956 m_line, m_file,
6f77053d 19957 paddress (m_gdbarch, m_address),
94a72be7
AB
19958 m_is_stmt, m_discriminator,
19959 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19960 }
19961
6f77053d 19962 file_entry *fe = current_file ();
8c43009f
PA
19963
19964 if (fe == NULL)
d9b3de22
DE
19965 dwarf2_debug_line_missing_file_complaint ();
19966 /* For now we ignore lines not starting on an instruction boundary.
19967 But not when processing end_sequence for compatibility with the
19968 previous version of the code. */
6f77053d 19969 else if (m_op_index == 0 || end_sequence)
d9b3de22 19970 {
8c43009f 19971 fe->included_p = 1;
94a72be7
AB
19972 if (m_record_lines_p
19973 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 19974 {
c24bdb02 19975 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19976 || end_sequence)
d9b3de22 19977 {
804d2729
TT
19978 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19979 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19980 }
19981
19982 if (!end_sequence)
19983 {
804d2729 19984 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
19985 m_line_has_non_zero_discriminator,
19986 m_last_subfile))
d9b3de22 19987 {
c24bdb02 19988 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 19989 dwarf_record_line_1 (m_gdbarch,
c24bdb02 19990 builder->get_current_subfile (),
6f77053d 19991 m_line, m_address,
804d2729 19992 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 19993 }
c24bdb02 19994 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19995 m_last_line = m_line;
d9b3de22
DE
19996 }
19997 }
19998 }
19999}
20000
804d2729
TT
20001lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20002 line_header *lh, bool record_lines_p)
d9b3de22 20003{
804d2729 20004 m_cu = cu;
6f77053d
PA
20005 m_gdbarch = arch;
20006 m_record_lines_p = record_lines_p;
20007 m_line_header = lh;
d9b3de22 20008
804d2729 20009 m_currently_recording_lines = true;
d9b3de22 20010
d9b3de22
DE
20011 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20012 was a line entry for it so that the backend has a chance to adjust it
20013 and also record it in case it needs it. This is currently used by MIPS
20014 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20015 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20016 m_is_stmt = lh->default_is_stmt;
20017 m_discriminator = 0;
252a6764
DE
20018}
20019
6f77053d
PA
20020void
20021lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20022 const gdb_byte *line_ptr,
7ab6656f 20023 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20024{
7ab6656f
OJ
20025 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20026 the pc range of the CU. However, we restrict the test to only ADDRESS
20027 values of zero to preserve GDB's previous behaviour which is to handle
20028 the specific case of a function being GC'd by the linker. */
924c2928 20029
7ab6656f 20030 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20031 {
20032 /* This line table is for a function which has been
20033 GCd by the linker. Ignore it. PR gdb/12528 */
20034
518817b3 20035 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20036 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20037
b98664d3 20038 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20039 line_offset, objfile_name (objfile));
804d2729
TT
20040 m_currently_recording_lines = false;
20041 /* Note: m_currently_recording_lines is left as false until we see
20042 DW_LNE_end_sequence. */
924c2928
DE
20043 }
20044}
20045
f3f5162e 20046/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20047 Process the line number information in LH.
20048 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20049 program in order to set included_p for every referenced header. */
debd256d 20050
c906108c 20051static void
43f3e411
DE
20052dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20053 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20054{
d521ce57
TT
20055 const gdb_byte *line_ptr, *extended_end;
20056 const gdb_byte *line_end;
a8c50c1f 20057 unsigned int bytes_read, extended_len;
699ca60a 20058 unsigned char op_code, extended_op;
e142c38c 20059 CORE_ADDR baseaddr;
518817b3 20060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20061 bfd *abfd = objfile->obfd;
fbf65064 20062 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20063 /* True if we're recording line info (as opposed to building partial
20064 symtabs and just interested in finding include files mentioned by
20065 the line number program). */
20066 bool record_lines_p = !decode_for_pst_p;
e142c38c 20067
b3b3bada 20068 baseaddr = objfile->text_section_offset ();
c906108c 20069
debd256d
JB
20070 line_ptr = lh->statement_program_start;
20071 line_end = lh->statement_program_end;
c906108c
SS
20072
20073 /* Read the statement sequences until there's nothing left. */
20074 while (line_ptr < line_end)
20075 {
6f77053d
PA
20076 /* The DWARF line number program state machine. Reset the state
20077 machine at the start of each sequence. */
804d2729 20078 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20079 bool end_sequence = false;
d9b3de22 20080
8c43009f 20081 if (record_lines_p)
c906108c 20082 {
8c43009f
PA
20083 /* Start a subfile for the current file of the state
20084 machine. */
20085 const file_entry *fe = state_machine.current_file ();
20086
20087 if (fe != NULL)
804d2729 20088 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20089 }
20090
a738430d 20091 /* Decode the table. */
d9b3de22 20092 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20093 {
20094 op_code = read_1_byte (abfd, line_ptr);
20095 line_ptr += 1;
9aa1fe7e 20096
debd256d 20097 if (op_code >= lh->opcode_base)
6e70227d 20098 {
8e07a239 20099 /* Special opcode. */
6f77053d 20100 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20101 }
20102 else switch (op_code)
c906108c
SS
20103 {
20104 case DW_LNS_extended_op:
3e43a32a
MS
20105 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20106 &bytes_read);
473b7be6 20107 line_ptr += bytes_read;
a8c50c1f 20108 extended_end = line_ptr + extended_len;
c906108c
SS
20109 extended_op = read_1_byte (abfd, line_ptr);
20110 line_ptr += 1;
20111 switch (extended_op)
20112 {
20113 case DW_LNE_end_sequence:
6f77053d
PA
20114 state_machine.handle_end_sequence ();
20115 end_sequence = true;
c906108c
SS
20116 break;
20117 case DW_LNE_set_address:
d9b3de22
DE
20118 {
20119 CORE_ADDR address
c8a7a66f 20120 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20121 line_ptr += bytes_read;
6f77053d
PA
20122
20123 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20124 lowpc - baseaddr, address);
6f77053d 20125 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20126 }
c906108c
SS
20127 break;
20128 case DW_LNE_define_file:
debd256d 20129 {
d521ce57 20130 const char *cur_file;
ecfb656c
PA
20131 unsigned int mod_time, length;
20132 dir_index dindex;
6e70227d 20133
3e43a32a
MS
20134 cur_file = read_direct_string (abfd, line_ptr,
20135 &bytes_read);
debd256d 20136 line_ptr += bytes_read;
ecfb656c 20137 dindex = (dir_index)
debd256d
JB
20138 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20139 line_ptr += bytes_read;
20140 mod_time =
20141 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20142 line_ptr += bytes_read;
20143 length =
20144 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20145 line_ptr += bytes_read;
ecfb656c 20146 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20147 }
c906108c 20148 break;
d0c6ba3d 20149 case DW_LNE_set_discriminator:
6f77053d
PA
20150 {
20151 /* The discriminator is not interesting to the
20152 debugger; just ignore it. We still need to
20153 check its value though:
20154 if there are consecutive entries for the same
20155 (non-prologue) line we want to coalesce them.
20156 PR 17276. */
20157 unsigned int discr
20158 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20159 line_ptr += bytes_read;
20160
20161 state_machine.handle_set_discriminator (discr);
20162 }
d0c6ba3d 20163 break;
c906108c 20164 default:
b98664d3 20165 complaint (_("mangled .debug_line section"));
debd256d 20166 return;
c906108c 20167 }
a8c50c1f
DJ
20168 /* Make sure that we parsed the extended op correctly. If e.g.
20169 we expected a different address size than the producer used,
20170 we may have read the wrong number of bytes. */
20171 if (line_ptr != extended_end)
20172 {
b98664d3 20173 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20174 return;
20175 }
c906108c
SS
20176 break;
20177 case DW_LNS_copy:
6f77053d 20178 state_machine.handle_copy ();
c906108c
SS
20179 break;
20180 case DW_LNS_advance_pc:
2dc7f7b3
TT
20181 {
20182 CORE_ADDR adjust
20183 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20184 line_ptr += bytes_read;
6f77053d
PA
20185
20186 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20187 }
c906108c
SS
20188 break;
20189 case DW_LNS_advance_line:
a05a36a5
DE
20190 {
20191 int line_delta
20192 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20193 line_ptr += bytes_read;
6f77053d
PA
20194
20195 state_machine.handle_advance_line (line_delta);
a05a36a5 20196 }
c906108c
SS
20197 break;
20198 case DW_LNS_set_file:
d9b3de22 20199 {
6f77053d 20200 file_name_index file
ecfb656c
PA
20201 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20202 &bytes_read);
d9b3de22 20203 line_ptr += bytes_read;
8c43009f 20204
6f77053d 20205 state_machine.handle_set_file (file);
d9b3de22 20206 }
c906108c
SS
20207 break;
20208 case DW_LNS_set_column:
0ad93d4f 20209 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20210 line_ptr += bytes_read;
20211 break;
20212 case DW_LNS_negate_stmt:
6f77053d 20213 state_machine.handle_negate_stmt ();
c906108c
SS
20214 break;
20215 case DW_LNS_set_basic_block:
c906108c 20216 break;
c2c6d25f
JM
20217 /* Add to the address register of the state machine the
20218 address increment value corresponding to special opcode
a738430d
MK
20219 255. I.e., this value is scaled by the minimum
20220 instruction length since special opcode 255 would have
b021a221 20221 scaled the increment. */
c906108c 20222 case DW_LNS_const_add_pc:
6f77053d 20223 state_machine.handle_const_add_pc ();
c906108c
SS
20224 break;
20225 case DW_LNS_fixed_advance_pc:
3e29f34a 20226 {
6f77053d 20227 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20228 line_ptr += 2;
6f77053d
PA
20229
20230 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20231 }
c906108c 20232 break;
9aa1fe7e 20233 default:
a738430d
MK
20234 {
20235 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20236 int i;
a738430d 20237
debd256d 20238 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20239 {
20240 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20241 line_ptr += bytes_read;
20242 }
20243 }
c906108c
SS
20244 }
20245 }
d9b3de22
DE
20246
20247 if (!end_sequence)
20248 dwarf2_debug_line_missing_end_sequence_complaint ();
20249
20250 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20251 in which case we still finish recording the last line). */
6f77053d 20252 state_machine.record_line (true);
c906108c 20253 }
f3f5162e
DE
20254}
20255
20256/* Decode the Line Number Program (LNP) for the given line_header
20257 structure and CU. The actual information extracted and the type
20258 of structures created from the LNP depends on the value of PST.
20259
20260 1. If PST is NULL, then this procedure uses the data from the program
20261 to create all necessary symbol tables, and their linetables.
20262
20263 2. If PST is not NULL, this procedure reads the program to determine
20264 the list of files included by the unit represented by PST, and
20265 builds all the associated partial symbol tables.
20266
20267 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20268 It is used for relative paths in the line table.
20269 NOTE: When processing partial symtabs (pst != NULL),
20270 comp_dir == pst->dirname.
20271
20272 NOTE: It is important that psymtabs have the same file name (via strcmp)
20273 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20274 symtab we don't use it in the name of the psymtabs we create.
20275 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20276 A good testcase for this is mb-inline.exp.
20277
527f3840
JK
20278 LOWPC is the lowest address in CU (or 0 if not known).
20279
20280 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20281 for its PC<->lines mapping information. Otherwise only the filename
20282 table is read in. */
f3f5162e
DE
20283
20284static void
20285dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20286 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20287 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20288{
518817b3 20289 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20290 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20291
527f3840
JK
20292 if (decode_mapping)
20293 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20294
20295 if (decode_for_pst_p)
20296 {
aaa75496
JB
20297 /* Now that we're done scanning the Line Header Program, we can
20298 create the psymtab of each included file. */
7ba99d21
AT
20299 for (auto &file_entry : lh->file_names ())
20300 if (file_entry.included_p == 1)
aaa75496 20301 {
c89b44cd 20302 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20303 const char *include_name =
7ba99d21
AT
20304 psymtab_include_file_name (lh, file_entry, pst,
20305 comp_dir, &name_holder);
c6da4cef 20306 if (include_name != NULL)
aaa75496
JB
20307 dwarf2_create_include_psymtab (include_name, pst, objfile);
20308 }
20309 }
cb1df416
DJ
20310 else
20311 {
20312 /* Make sure a symtab is created for every file, even files
20313 which contain only variables (i.e. no code with associated
20314 line numbers). */
c24bdb02
KS
20315 buildsym_compunit *builder = cu->get_builder ();
20316 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20317
7ba99d21 20318 for (auto &fe : lh->file_names ())
cb1df416 20319 {
804d2729 20320 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20321 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20322 {
c24bdb02 20323 builder->get_current_subfile ()->symtab
804d2729 20324 = allocate_symtab (cust,
c24bdb02 20325 builder->get_current_subfile ()->name);
43f3e411 20326 }
c24bdb02 20327 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20328 }
20329 }
c906108c
SS
20330}
20331
20332/* Start a subfile for DWARF. FILENAME is the name of the file and
20333 DIRNAME the name of the source directory which contains FILENAME
4d663531 20334 or NULL if not known.
c906108c
SS
20335 This routine tries to keep line numbers from identical absolute and
20336 relative file names in a common subfile.
20337
20338 Using the `list' example from the GDB testsuite, which resides in
20339 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20340 of /srcdir/list0.c yields the following debugging information for list0.c:
20341
c5aa993b 20342 DW_AT_name: /srcdir/list0.c
4d663531 20343 DW_AT_comp_dir: /compdir
357e46e7 20344 files.files[0].name: list0.h
c5aa993b 20345 files.files[0].dir: /srcdir
357e46e7 20346 files.files[1].name: list0.c
c5aa993b 20347 files.files[1].dir: /srcdir
c906108c
SS
20348
20349 The line number information for list0.c has to end up in a single
4f1520fb
FR
20350 subfile, so that `break /srcdir/list0.c:1' works as expected.
20351 start_subfile will ensure that this happens provided that we pass the
20352 concatenation of files.files[1].dir and files.files[1].name as the
20353 subfile's name. */
c906108c
SS
20354
20355static void
804d2729
TT
20356dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20357 const char *dirname)
c906108c 20358{
43816ebc 20359 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20360
4d663531 20361 /* In order not to lose the line information directory,
4f1520fb
FR
20362 we concatenate it to the filename when it makes sense.
20363 Note that the Dwarf3 standard says (speaking of filenames in line
20364 information): ``The directory index is ignored for file names
20365 that represent full path names''. Thus ignoring dirname in the
20366 `else' branch below isn't an issue. */
c906108c 20367
d5166ae1 20368 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20369 {
43816ebc
TT
20370 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20371 filename = copy.get ();
d521ce57 20372 }
c906108c 20373
c24bdb02 20374 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20375}
20376
804d2729
TT
20377/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20378 buildsym_compunit constructor. */
f4dc4d17 20379
c24bdb02
KS
20380struct compunit_symtab *
20381dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20382 CORE_ADDR low_pc)
f4dc4d17 20383{
c24bdb02 20384 gdb_assert (m_builder == nullptr);
43f3e411 20385
c24bdb02
KS
20386 m_builder.reset (new struct buildsym_compunit
20387 (per_cu->dwarf2_per_objfile->objfile,
20388 name, comp_dir, language, low_pc));
93b8bea4 20389
c24bdb02 20390 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20391
c24bdb02
KS
20392 get_builder ()->record_debugformat ("DWARF 2");
20393 get_builder ()->record_producer (producer);
f4dc4d17 20394
c24bdb02 20395 processing_has_namespace_info = false;
43f3e411 20396
c24bdb02 20397 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20398}
20399
4c2df51b
DJ
20400static void
20401var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20402 struct dwarf2_cu *cu)
4c2df51b 20403{
518817b3 20404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20405 struct comp_unit_head *cu_header = &cu->header;
20406
4c2df51b
DJ
20407 /* NOTE drow/2003-01-30: There used to be a comment and some special
20408 code here to turn a symbol with DW_AT_external and a
20409 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20410 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20411 with some versions of binutils) where shared libraries could have
20412 relocations against symbols in their debug information - the
20413 minimal symbol would have the right address, but the debug info
20414 would not. It's no longer necessary, because we will explicitly
20415 apply relocations when we read in the debug information now. */
20416
20417 /* A DW_AT_location attribute with no contents indicates that a
20418 variable has been optimized away. */
4fc6c0d5 20419 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20420 {
f1e6e072 20421 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20422 return;
20423 }
20424
20425 /* Handle one degenerate form of location expression specially, to
20426 preserve GDB's previous behavior when section offsets are
336d760d
AT
20427 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20428 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20429
4fc6c0d5 20430 if (attr->form_is_block ()
3019eac3
DE
20431 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20432 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20433 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20434 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20435 && (DW_BLOCK (attr)->size
20436 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20437 {
891d2f0b 20438 unsigned int dummy;
4c2df51b 20439
3019eac3 20440 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20441 SET_SYMBOL_VALUE_ADDRESS
20442 (sym, cu->header.read_address (objfile->obfd,
20443 DW_BLOCK (attr)->data + 1,
20444 &dummy));
3019eac3 20445 else
38583298
TT
20446 SET_SYMBOL_VALUE_ADDRESS
20447 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20448 &dummy));
f1e6e072 20449 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20450 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20451 SET_SYMBOL_VALUE_ADDRESS
20452 (sym,
20453 SYMBOL_VALUE_ADDRESS (sym)
20454 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20455 return;
20456 }
20457
20458 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20459 expression evaluator, and use LOC_COMPUTED only when necessary
20460 (i.e. when the value of a register or memory location is
20461 referenced, or a thread-local block, etc.). Then again, it might
20462 not be worthwhile. I'm assuming that it isn't unless performance
20463 or memory numbers show me otherwise. */
20464
f1e6e072 20465 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20466
f1e6e072 20467 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20468 cu->has_loclist = true;
4c2df51b
DJ
20469}
20470
c906108c
SS
20471/* Given a pointer to a DWARF information entry, figure out if we need
20472 to make a symbol table entry for it, and if so, create a new entry
20473 and return a pointer to it.
20474 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20475 used the passed type.
20476 If SPACE is not NULL, use it to hold the new symbol. If it is
20477 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20478
20479static struct symbol *
5e2db402
TT
20480new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20481 struct symbol *space)
c906108c 20482{
518817b3
SM
20483 struct dwarf2_per_objfile *dwarf2_per_objfile
20484 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20485 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20486 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20487 struct symbol *sym = NULL;
15d034d0 20488 const char *name;
c906108c
SS
20489 struct attribute *attr = NULL;
20490 struct attribute *attr2 = NULL;
e142c38c 20491 CORE_ADDR baseaddr;
e37fd15a
SW
20492 struct pending **list_to_add = NULL;
20493
edb3359d 20494 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20495
b3b3bada 20496 baseaddr = objfile->text_section_offset ();
c906108c 20497
94af9270 20498 name = dwarf2_name (die, cu);
c906108c
SS
20499 if (name)
20500 {
94af9270 20501 const char *linkagename;
34eaf542 20502 int suppress_add = 0;
94af9270 20503
34eaf542
TT
20504 if (space)
20505 sym = space;
20506 else
e623cf5d 20507 sym = allocate_symbol (objfile);
c906108c 20508 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20509
20510 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20511 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20512 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20513 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20514
f55ee35c
JK
20515 /* Fortran does not have mangling standard and the mangling does differ
20516 between gfortran, iFort etc. */
20517 if (cu->language == language_fortran
468c0cbb
CB
20518 && symbol_get_demangled_name (sym) == NULL)
20519 symbol_set_demangled_name (sym,
cfc594ee 20520 dwarf2_full_name (name, die, cu),
29df156d 20521 NULL);
f55ee35c 20522
c906108c 20523 /* Default assumptions.
c5aa993b 20524 Use the passed type or decode it from the die. */
176620f1 20525 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20526 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20527 if (type != NULL)
20528 SYMBOL_TYPE (sym) = type;
20529 else
e7c27a73 20530 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20531 attr = dwarf2_attr (die,
20532 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20533 cu);
435d3d88 20534 if (attr != nullptr)
c906108c
SS
20535 {
20536 SYMBOL_LINE (sym) = DW_UNSND (attr);
20537 }
cb1df416 20538
edb3359d
DJ
20539 attr = dwarf2_attr (die,
20540 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20541 cu);
435d3d88 20542 if (attr != nullptr)
cb1df416 20543 {
ecfb656c 20544 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20545 struct file_entry *fe;
9a619af0 20546
ecfb656c
PA
20547 if (cu->line_header != NULL)
20548 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20549 else
20550 fe = NULL;
20551
20552 if (fe == NULL)
b98664d3 20553 complaint (_("file index out of range"));
8c43009f
PA
20554 else
20555 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20556 }
20557
c906108c
SS
20558 switch (die->tag)
20559 {
20560 case DW_TAG_label:
e142c38c 20561 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20562 if (attr != nullptr)
3e29f34a
MR
20563 {
20564 CORE_ADDR addr;
20565
cd6c91b4 20566 addr = attr->value_as_address ();
3e29f34a 20567 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20568 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20569 }
0f5238ed
TT
20570 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20571 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20572 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20573 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20574 break;
20575 case DW_TAG_subprogram:
20576 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20577 finish_block. */
f1e6e072 20578 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20579 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20580 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20581 || cu->language == language_ada
20582 || cu->language == language_fortran)
c906108c 20583 {
2cfa0c8d 20584 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20585 Ada and Fortran subprograms, whether marked external or
20586 not, are always stored as a global symbol, because we want
20587 to be able to access them globally. For instance, we want
20588 to be able to break on a nested subprogram without having
20589 to specify the context. */
c24bdb02 20590 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20591 }
20592 else
20593 {
e37fd15a 20594 list_to_add = cu->list_in_scope;
c906108c
SS
20595 }
20596 break;
edb3359d
DJ
20597 case DW_TAG_inlined_subroutine:
20598 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20599 finish_block. */
f1e6e072 20600 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20601 SYMBOL_INLINED (sym) = 1;
481860b3 20602 list_to_add = cu->list_in_scope;
edb3359d 20603 break;
34eaf542
TT
20604 case DW_TAG_template_value_param:
20605 suppress_add = 1;
20606 /* Fall through. */
72929c62 20607 case DW_TAG_constant:
c906108c 20608 case DW_TAG_variable:
254e6b9e 20609 case DW_TAG_member:
0963b4bd
MS
20610 /* Compilation with minimal debug info may result in
20611 variables with missing type entries. Change the
20612 misleading `void' type to something sensible. */
c906108c 20613 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20614 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20615
e142c38c 20616 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20617 /* In the case of DW_TAG_member, we should only be called for
20618 static const members. */
20619 if (die->tag == DW_TAG_member)
20620 {
3863f96c
DE
20621 /* dwarf2_add_field uses die_is_declaration,
20622 so we do the same. */
254e6b9e
DE
20623 gdb_assert (die_is_declaration (die, cu));
20624 gdb_assert (attr);
20625 }
435d3d88 20626 if (attr != nullptr)
c906108c 20627 {
e7c27a73 20628 dwarf2_const_value (attr, sym, cu);
e142c38c 20629 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20630 if (!suppress_add)
34eaf542
TT
20631 {
20632 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20633 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20634 else
e37fd15a 20635 list_to_add = cu->list_in_scope;
34eaf542 20636 }
c906108c
SS
20637 break;
20638 }
e142c38c 20639 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20640 if (attr != nullptr)
c906108c 20641 {
e7c27a73 20642 var_decode_location (attr, sym, cu);
e142c38c 20643 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20644
20645 /* Fortran explicitly imports any global symbols to the local
20646 scope by DW_TAG_common_block. */
20647 if (cu->language == language_fortran && die->parent
20648 && die->parent->tag == DW_TAG_common_block)
20649 attr2 = NULL;
20650
caac4577
JG
20651 if (SYMBOL_CLASS (sym) == LOC_STATIC
20652 && SYMBOL_VALUE_ADDRESS (sym) == 0
20653 && !dwarf2_per_objfile->has_section_at_zero)
20654 {
20655 /* When a static variable is eliminated by the linker,
20656 the corresponding debug information is not stripped
20657 out, but the variable address is set to null;
20658 do not add such variables into symbol table. */
20659 }
20660 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20661 {
4b610737
TT
20662 if (SYMBOL_CLASS (sym) == LOC_STATIC
20663 && (objfile->flags & OBJF_MAINLINE) == 0
20664 && dwarf2_per_objfile->can_copy)
20665 {
20666 /* A global static variable might be subject to
20667 copy relocation. We first check for a local
20668 minsym, though, because maybe the symbol was
20669 marked hidden, in which case this would not
20670 apply. */
20671 bound_minimal_symbol found
20672 = (lookup_minimal_symbol_linkage
987012b8 20673 (sym->linkage_name (), objfile));
4b610737
TT
20674 if (found.minsym != nullptr)
20675 sym->maybe_copied = 1;
20676 }
f55ee35c 20677
1c809c68
TT
20678 /* A variable with DW_AT_external is never static,
20679 but it may be block-scoped. */
804d2729 20680 list_to_add
c24bdb02
KS
20681 = ((cu->list_in_scope
20682 == cu->get_builder ()->get_file_symbols ())
20683 ? cu->get_builder ()->get_global_symbols ()
804d2729 20684 : cu->list_in_scope);
1c809c68 20685 }
c906108c 20686 else
e37fd15a 20687 list_to_add = cu->list_in_scope;
c906108c
SS
20688 }
20689 else
20690 {
20691 /* We do not know the address of this symbol.
c5aa993b
JM
20692 If it is an external symbol and we have type information
20693 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20694 The address of the variable will then be determined from
20695 the minimal symbol table whenever the variable is
20696 referenced. */
e142c38c 20697 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20698
20699 /* Fortran explicitly imports any global symbols to the local
20700 scope by DW_TAG_common_block. */
20701 if (cu->language == language_fortran && die->parent
20702 && die->parent->tag == DW_TAG_common_block)
20703 {
20704 /* SYMBOL_CLASS doesn't matter here because
20705 read_common_block is going to reset it. */
20706 if (!suppress_add)
20707 list_to_add = cu->list_in_scope;
20708 }
20709 else if (attr2 && (DW_UNSND (attr2) != 0)
20710 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20711 {
0fe7935b
DJ
20712 /* A variable with DW_AT_external is never static, but it
20713 may be block-scoped. */
804d2729 20714 list_to_add
c24bdb02
KS
20715 = ((cu->list_in_scope
20716 == cu->get_builder ()->get_file_symbols ())
20717 ? cu->get_builder ()->get_global_symbols ()
804d2729 20718 : cu->list_in_scope);
0fe7935b 20719
f1e6e072 20720 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20721 }
442ddf59
JK
20722 else if (!die_is_declaration (die, cu))
20723 {
20724 /* Use the default LOC_OPTIMIZED_OUT class. */
20725 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20726 if (!suppress_add)
20727 list_to_add = cu->list_in_scope;
442ddf59 20728 }
c906108c
SS
20729 }
20730 break;
20731 case DW_TAG_formal_parameter:
a60f3166
TT
20732 {
20733 /* If we are inside a function, mark this as an argument. If
20734 not, we might be looking at an argument to an inlined function
20735 when we do not have enough information to show inlined frames;
20736 pretend it's a local variable in that case so that the user can
20737 still see it. */
804d2729 20738 struct context_stack *curr
c24bdb02 20739 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20740 if (curr != nullptr && curr->name != nullptr)
20741 SYMBOL_IS_ARGUMENT (sym) = 1;
20742 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20743 if (attr != nullptr)
a60f3166
TT
20744 {
20745 var_decode_location (attr, sym, cu);
20746 }
20747 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20748 if (attr != nullptr)
a60f3166
TT
20749 {
20750 dwarf2_const_value (attr, sym, cu);
20751 }
f346a30d 20752
a60f3166
TT
20753 list_to_add = cu->list_in_scope;
20754 }
c906108c
SS
20755 break;
20756 case DW_TAG_unspecified_parameters:
20757 /* From varargs functions; gdb doesn't seem to have any
20758 interest in this information, so just ignore it for now.
20759 (FIXME?) */
20760 break;
34eaf542
TT
20761 case DW_TAG_template_type_param:
20762 suppress_add = 1;
20763 /* Fall through. */
c906108c 20764 case DW_TAG_class_type:
680b30c7 20765 case DW_TAG_interface_type:
c906108c
SS
20766 case DW_TAG_structure_type:
20767 case DW_TAG_union_type:
72019c9c 20768 case DW_TAG_set_type:
c906108c 20769 case DW_TAG_enumeration_type:
f1e6e072 20770 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20771 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20772
63d06c5c 20773 {
9c37b5ae 20774 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20775 really ever be static objects: otherwise, if you try
20776 to, say, break of a class's method and you're in a file
20777 which doesn't mention that class, it won't work unless
20778 the check for all static symbols in lookup_symbol_aux
20779 saves you. See the OtherFileClass tests in
20780 gdb.c++/namespace.exp. */
20781
e37fd15a 20782 if (!suppress_add)
34eaf542 20783 {
c24bdb02 20784 buildsym_compunit *builder = cu->get_builder ();
804d2729 20785 list_to_add
c24bdb02 20786 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20787 && cu->language == language_cplus
c24bdb02 20788 ? builder->get_global_symbols ()
804d2729 20789 : cu->list_in_scope);
63d06c5c 20790
64382290 20791 /* The semantics of C++ state that "struct foo {
9c37b5ae 20792 ... }" also defines a typedef for "foo". */
64382290 20793 if (cu->language == language_cplus
45280282 20794 || cu->language == language_ada
c44af4eb
TT
20795 || cu->language == language_d
20796 || cu->language == language_rust)
64382290
TT
20797 {
20798 /* The symbol's name is already allocated along
20799 with this objfile, so we don't need to
20800 duplicate it for the type. */
20801 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20802 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20803 }
63d06c5c
DC
20804 }
20805 }
c906108c
SS
20806 break;
20807 case DW_TAG_typedef:
f1e6e072 20808 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20809 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20810 list_to_add = cu->list_in_scope;
63d06c5c 20811 break;
c906108c 20812 case DW_TAG_base_type:
a02abb62 20813 case DW_TAG_subrange_type:
f1e6e072 20814 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20815 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20816 list_to_add = cu->list_in_scope;
c906108c
SS
20817 break;
20818 case DW_TAG_enumerator:
e142c38c 20819 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20820 if (attr != nullptr)
c906108c 20821 {
e7c27a73 20822 dwarf2_const_value (attr, sym, cu);
c906108c 20823 }
63d06c5c
DC
20824 {
20825 /* NOTE: carlton/2003-11-10: See comment above in the
20826 DW_TAG_class_type, etc. block. */
20827
804d2729 20828 list_to_add
c24bdb02 20829 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20830 && cu->language == language_cplus
c24bdb02 20831 ? cu->get_builder ()->get_global_symbols ()
804d2729 20832 : cu->list_in_scope);
63d06c5c 20833 }
c906108c 20834 break;
74921315 20835 case DW_TAG_imported_declaration:
5c4e30ca 20836 case DW_TAG_namespace:
f1e6e072 20837 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20838 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20839 break;
530e8392
KB
20840 case DW_TAG_module:
20841 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20842 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20843 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20844 break;
4357ac6c 20845 case DW_TAG_common_block:
f1e6e072 20846 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20847 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20848 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20849 break;
c906108c
SS
20850 default:
20851 /* Not a tag we recognize. Hopefully we aren't processing
20852 trash data, but since we must specifically ignore things
20853 we don't recognize, there is nothing else we should do at
0963b4bd 20854 this point. */
b98664d3 20855 complaint (_("unsupported tag: '%s'"),
4d3c2250 20856 dwarf_tag_name (die->tag));
c906108c
SS
20857 break;
20858 }
df8a16a1 20859
e37fd15a
SW
20860 if (suppress_add)
20861 {
20862 sym->hash_next = objfile->template_symbols;
20863 objfile->template_symbols = sym;
20864 list_to_add = NULL;
20865 }
20866
20867 if (list_to_add != NULL)
d3cb6808 20868 add_symbol_to_list (sym, list_to_add);
e37fd15a 20869
df8a16a1
DJ
20870 /* For the benefit of old versions of GCC, check for anonymous
20871 namespaces based on the demangled name. */
4d4ec4e5 20872 if (!cu->processing_has_namespace_info
94af9270 20873 && cu->language == language_cplus)
c24bdb02 20874 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20875 }
20876 return (sym);
20877}
20878
98bfdba5
PA
20879/* Given an attr with a DW_FORM_dataN value in host byte order,
20880 zero-extend it as appropriate for the symbol's type. The DWARF
20881 standard (v4) is not entirely clear about the meaning of using
20882 DW_FORM_dataN for a constant with a signed type, where the type is
20883 wider than the data. The conclusion of a discussion on the DWARF
20884 list was that this is unspecified. We choose to always zero-extend
20885 because that is the interpretation long in use by GCC. */
c906108c 20886
98bfdba5 20887static gdb_byte *
ff39bb5e 20888dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20889 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20890{
518817b3 20891 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20892 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20893 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20894 LONGEST l = DW_UNSND (attr);
20895
20896 if (bits < sizeof (*value) * 8)
20897 {
20898 l &= ((LONGEST) 1 << bits) - 1;
20899 *value = l;
20900 }
20901 else if (bits == sizeof (*value) * 8)
20902 *value = l;
20903 else
20904 {
224c3ddb 20905 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20906 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20907 return bytes;
20908 }
20909
20910 return NULL;
20911}
20912
20913/* Read a constant value from an attribute. Either set *VALUE, or if
20914 the value does not fit in *VALUE, set *BYTES - either already
20915 allocated on the objfile obstack, or newly allocated on OBSTACK,
20916 or, set *BATON, if we translated the constant to a location
20917 expression. */
20918
20919static void
ff39bb5e 20920dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20921 const char *name, struct obstack *obstack,
20922 struct dwarf2_cu *cu,
d521ce57 20923 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20924 struct dwarf2_locexpr_baton **baton)
20925{
518817b3 20926 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20927 struct comp_unit_head *cu_header = &cu->header;
c906108c 20928 struct dwarf_block *blk;
98bfdba5
PA
20929 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20930 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20931
20932 *value = 0;
20933 *bytes = NULL;
20934 *baton = NULL;
c906108c
SS
20935
20936 switch (attr->form)
20937 {
20938 case DW_FORM_addr:
336d760d 20939 case DW_FORM_addrx:
3019eac3 20940 case DW_FORM_GNU_addr_index:
ac56253d 20941 {
ac56253d
TT
20942 gdb_byte *data;
20943
98bfdba5
PA
20944 if (TYPE_LENGTH (type) != cu_header->addr_size)
20945 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20946 cu_header->addr_size,
98bfdba5 20947 TYPE_LENGTH (type));
ac56253d
TT
20948 /* Symbols of this form are reasonably rare, so we just
20949 piggyback on the existing location code rather than writing
20950 a new implementation of symbol_computed_ops. */
8d749320 20951 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20952 (*baton)->per_cu = cu->per_cu;
20953 gdb_assert ((*baton)->per_cu);
ac56253d 20954
98bfdba5 20955 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20956 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20957 (*baton)->data = data;
ac56253d
TT
20958
20959 data[0] = DW_OP_addr;
20960 store_unsigned_integer (&data[1], cu_header->addr_size,
20961 byte_order, DW_ADDR (attr));
20962 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20963 }
c906108c 20964 break;
4ac36638 20965 case DW_FORM_string:
93b5768b 20966 case DW_FORM_strp:
cf532bd1 20967 case DW_FORM_strx:
3019eac3 20968 case DW_FORM_GNU_str_index:
36586728 20969 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20970 /* DW_STRING is already allocated on the objfile obstack, point
20971 directly to it. */
d521ce57 20972 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20973 break;
c906108c
SS
20974 case DW_FORM_block1:
20975 case DW_FORM_block2:
20976 case DW_FORM_block4:
20977 case DW_FORM_block:
2dc7f7b3 20978 case DW_FORM_exprloc:
0224619f 20979 case DW_FORM_data16:
c906108c 20980 blk = DW_BLOCK (attr);
98bfdba5
PA
20981 if (TYPE_LENGTH (type) != blk->size)
20982 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20983 TYPE_LENGTH (type));
20984 *bytes = blk->data;
c906108c 20985 break;
2df3850c
JM
20986
20987 /* The DW_AT_const_value attributes are supposed to carry the
20988 symbol's value "represented as it would be on the target
20989 architecture." By the time we get here, it's already been
20990 converted to host endianness, so we just need to sign- or
20991 zero-extend it as appropriate. */
20992 case DW_FORM_data1:
3aef2284 20993 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20994 break;
c906108c 20995 case DW_FORM_data2:
3aef2284 20996 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20997 break;
c906108c 20998 case DW_FORM_data4:
3aef2284 20999 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21000 break;
c906108c 21001 case DW_FORM_data8:
3aef2284 21002 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21003 break;
21004
c906108c 21005 case DW_FORM_sdata:
663c44ac 21006 case DW_FORM_implicit_const:
98bfdba5 21007 *value = DW_SND (attr);
2df3850c
JM
21008 break;
21009
c906108c 21010 case DW_FORM_udata:
98bfdba5 21011 *value = DW_UNSND (attr);
c906108c 21012 break;
2df3850c 21013
c906108c 21014 default:
b98664d3 21015 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21016 dwarf_form_name (attr->form));
98bfdba5 21017 *value = 0;
c906108c
SS
21018 break;
21019 }
21020}
21021
2df3850c 21022
98bfdba5
PA
21023/* Copy constant value from an attribute to a symbol. */
21024
2df3850c 21025static void
ff39bb5e 21026dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21027 struct dwarf2_cu *cu)
2df3850c 21028{
518817b3 21029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21030 LONGEST value;
d521ce57 21031 const gdb_byte *bytes;
98bfdba5 21032 struct dwarf2_locexpr_baton *baton;
2df3850c 21033
98bfdba5 21034 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21035 sym->print_name (),
98bfdba5
PA
21036 &objfile->objfile_obstack, cu,
21037 &value, &bytes, &baton);
2df3850c 21038
98bfdba5
PA
21039 if (baton != NULL)
21040 {
98bfdba5 21041 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21042 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21043 }
21044 else if (bytes != NULL)
21045 {
21046 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21047 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21048 }
21049 else
21050 {
21051 SYMBOL_VALUE (sym) = value;
f1e6e072 21052 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21053 }
2df3850c
JM
21054}
21055
c906108c
SS
21056/* Return the type of the die in question using its DW_AT_type attribute. */
21057
21058static struct type *
e7c27a73 21059die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21060{
c906108c 21061 struct attribute *type_attr;
c906108c 21062
e142c38c 21063 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21064 if (!type_attr)
21065 {
518817b3 21066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21067 /* A missing DW_AT_type represents a void type. */
518817b3 21068 return objfile_type (objfile)->builtin_void;
c906108c 21069 }
348e048f 21070
673bfd45 21071 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21072}
21073
b4ba55a1
JB
21074/* True iff CU's producer generates GNAT Ada auxiliary information
21075 that allows to find parallel types through that information instead
21076 of having to do expensive parallel lookups by type name. */
21077
21078static int
21079need_gnat_info (struct dwarf2_cu *cu)
21080{
de4cb04a
JB
21081 /* Assume that the Ada compiler was GNAT, which always produces
21082 the auxiliary information. */
21083 return (cu->language == language_ada);
b4ba55a1
JB
21084}
21085
b4ba55a1
JB
21086/* Return the auxiliary type of the die in question using its
21087 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21088 attribute is not present. */
21089
21090static struct type *
21091die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21092{
b4ba55a1 21093 struct attribute *type_attr;
b4ba55a1
JB
21094
21095 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21096 if (!type_attr)
21097 return NULL;
21098
673bfd45 21099 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21100}
21101
21102/* If DIE has a descriptive_type attribute, then set the TYPE's
21103 descriptive type accordingly. */
21104
21105static void
21106set_descriptive_type (struct type *type, struct die_info *die,
21107 struct dwarf2_cu *cu)
21108{
21109 struct type *descriptive_type = die_descriptive_type (die, cu);
21110
21111 if (descriptive_type)
21112 {
21113 ALLOCATE_GNAT_AUX_TYPE (type);
21114 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21115 }
21116}
21117
c906108c
SS
21118/* Return the containing type of the die in question using its
21119 DW_AT_containing_type attribute. */
21120
21121static struct type *
e7c27a73 21122die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21123{
c906108c 21124 struct attribute *type_attr;
518817b3 21125 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21126
e142c38c 21127 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21128 if (!type_attr)
21129 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21130 "[in module %s]"), objfile_name (objfile));
33ac96f0 21131
673bfd45 21132 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21133}
21134
ac9ec31b
DE
21135/* Return an error marker type to use for the ill formed type in DIE/CU. */
21136
21137static struct type *
21138build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21139{
518817b3
SM
21140 struct dwarf2_per_objfile *dwarf2_per_objfile
21141 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21142 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21143 char *saved;
ac9ec31b 21144
528e1572
SM
21145 std::string message
21146 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21147 objfile_name (objfile),
21148 sect_offset_str (cu->header.sect_off),
21149 sect_offset_str (die->sect_off));
efba19b0 21150 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21151
19f392bc 21152 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21153}
21154
673bfd45 21155/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21156 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21157 DW_AT_containing_type.
673bfd45
DE
21158 If there is no type substitute an error marker. */
21159
c906108c 21160static struct type *
ff39bb5e 21161lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21162 struct dwarf2_cu *cu)
c906108c 21163{
518817b3
SM
21164 struct dwarf2_per_objfile *dwarf2_per_objfile
21165 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21166 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21167 struct type *this_type;
21168
ac9ec31b
DE
21169 gdb_assert (attr->name == DW_AT_type
21170 || attr->name == DW_AT_GNAT_descriptive_type
21171 || attr->name == DW_AT_containing_type);
21172
673bfd45
DE
21173 /* First see if we have it cached. */
21174
36586728
TT
21175 if (attr->form == DW_FORM_GNU_ref_alt)
21176 {
21177 struct dwarf2_per_cu_data *per_cu;
9c541725 21178 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21179
ed2dc618
SM
21180 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21181 dwarf2_per_objfile);
9c541725 21182 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21183 }
cd6c91b4 21184 else if (attr->form_is_ref ())
673bfd45 21185 {
9c541725 21186 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21187
9c541725 21188 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21189 }
55f1336d 21190 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21191 {
ac9ec31b 21192 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21193
ac9ec31b 21194 return get_signatured_type (die, signature, cu);
673bfd45
DE
21195 }
21196 else
21197 {
b98664d3 21198 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21199 " at %s [in module %s]"),
21200 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21201 objfile_name (objfile));
ac9ec31b 21202 return build_error_marker_type (cu, die);
673bfd45
DE
21203 }
21204
21205 /* If not cached we need to read it in. */
21206
21207 if (this_type == NULL)
21208 {
ac9ec31b 21209 struct die_info *type_die = NULL;
673bfd45
DE
21210 struct dwarf2_cu *type_cu = cu;
21211
cd6c91b4 21212 if (attr->form_is_ref ())
ac9ec31b
DE
21213 type_die = follow_die_ref (die, attr, &type_cu);
21214 if (type_die == NULL)
21215 return build_error_marker_type (cu, die);
21216 /* If we find the type now, it's probably because the type came
3019eac3
DE
21217 from an inter-CU reference and the type's CU got expanded before
21218 ours. */
ac9ec31b 21219 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21220 }
21221
21222 /* If we still don't have a type use an error marker. */
21223
21224 if (this_type == NULL)
ac9ec31b 21225 return build_error_marker_type (cu, die);
673bfd45 21226
f792889a 21227 return this_type;
c906108c
SS
21228}
21229
673bfd45
DE
21230/* Return the type in DIE, CU.
21231 Returns NULL for invalid types.
21232
02142a6c 21233 This first does a lookup in die_type_hash,
673bfd45
DE
21234 and only reads the die in if necessary.
21235
21236 NOTE: This can be called when reading in partial or full symbols. */
21237
f792889a 21238static struct type *
e7c27a73 21239read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21240{
f792889a
DJ
21241 struct type *this_type;
21242
21243 this_type = get_die_type (die, cu);
21244 if (this_type)
21245 return this_type;
21246
673bfd45
DE
21247 return read_type_die_1 (die, cu);
21248}
21249
21250/* Read the type in DIE, CU.
21251 Returns NULL for invalid types. */
21252
21253static struct type *
21254read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21255{
21256 struct type *this_type = NULL;
21257
c906108c
SS
21258 switch (die->tag)
21259 {
21260 case DW_TAG_class_type:
680b30c7 21261 case DW_TAG_interface_type:
c906108c
SS
21262 case DW_TAG_structure_type:
21263 case DW_TAG_union_type:
f792889a 21264 this_type = read_structure_type (die, cu);
c906108c
SS
21265 break;
21266 case DW_TAG_enumeration_type:
f792889a 21267 this_type = read_enumeration_type (die, cu);
c906108c
SS
21268 break;
21269 case DW_TAG_subprogram:
21270 case DW_TAG_subroutine_type:
edb3359d 21271 case DW_TAG_inlined_subroutine:
f792889a 21272 this_type = read_subroutine_type (die, cu);
c906108c
SS
21273 break;
21274 case DW_TAG_array_type:
f792889a 21275 this_type = read_array_type (die, cu);
c906108c 21276 break;
72019c9c 21277 case DW_TAG_set_type:
f792889a 21278 this_type = read_set_type (die, cu);
72019c9c 21279 break;
c906108c 21280 case DW_TAG_pointer_type:
f792889a 21281 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21282 break;
21283 case DW_TAG_ptr_to_member_type:
f792889a 21284 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21285 break;
21286 case DW_TAG_reference_type:
4297a3f0
AV
21287 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21288 break;
21289 case DW_TAG_rvalue_reference_type:
21290 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21291 break;
21292 case DW_TAG_const_type:
f792889a 21293 this_type = read_tag_const_type (die, cu);
c906108c
SS
21294 break;
21295 case DW_TAG_volatile_type:
f792889a 21296 this_type = read_tag_volatile_type (die, cu);
c906108c 21297 break;
06d66ee9
TT
21298 case DW_TAG_restrict_type:
21299 this_type = read_tag_restrict_type (die, cu);
21300 break;
c906108c 21301 case DW_TAG_string_type:
f792889a 21302 this_type = read_tag_string_type (die, cu);
c906108c
SS
21303 break;
21304 case DW_TAG_typedef:
f792889a 21305 this_type = read_typedef (die, cu);
c906108c 21306 break;
a02abb62 21307 case DW_TAG_subrange_type:
f792889a 21308 this_type = read_subrange_type (die, cu);
a02abb62 21309 break;
c906108c 21310 case DW_TAG_base_type:
f792889a 21311 this_type = read_base_type (die, cu);
c906108c 21312 break;
81a17f79 21313 case DW_TAG_unspecified_type:
f792889a 21314 this_type = read_unspecified_type (die, cu);
81a17f79 21315 break;
0114d602
DJ
21316 case DW_TAG_namespace:
21317 this_type = read_namespace_type (die, cu);
21318 break;
f55ee35c
JK
21319 case DW_TAG_module:
21320 this_type = read_module_type (die, cu);
21321 break;
a2c2acaf
MW
21322 case DW_TAG_atomic_type:
21323 this_type = read_tag_atomic_type (die, cu);
21324 break;
c906108c 21325 default:
b98664d3 21326 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21327 dwarf_tag_name (die->tag));
c906108c
SS
21328 break;
21329 }
63d06c5c 21330
f792889a 21331 return this_type;
63d06c5c
DC
21332}
21333
abc72ce4
DE
21334/* See if we can figure out if the class lives in a namespace. We do
21335 this by looking for a member function; its demangled name will
21336 contain namespace info, if there is any.
21337 Return the computed name or NULL.
21338 Space for the result is allocated on the objfile's obstack.
21339 This is the full-die version of guess_partial_die_structure_name.
21340 In this case we know DIE has no useful parent. */
21341
43816ebc 21342static const char *
abc72ce4
DE
21343guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21344{
21345 struct die_info *spec_die;
21346 struct dwarf2_cu *spec_cu;
21347 struct die_info *child;
518817b3 21348 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21349
21350 spec_cu = cu;
21351 spec_die = die_specification (die, &spec_cu);
21352 if (spec_die != NULL)
21353 {
21354 die = spec_die;
21355 cu = spec_cu;
21356 }
21357
21358 for (child = die->child;
21359 child != NULL;
21360 child = child->sibling)
21361 {
21362 if (child->tag == DW_TAG_subprogram)
21363 {
73b9be8b 21364 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21365
7d45c7c3 21366 if (linkage_name != NULL)
abc72ce4 21367 {
43816ebc
TT
21368 gdb::unique_xmalloc_ptr<char> actual_name
21369 (language_class_name_from_physname (cu->language_defn,
21370 linkage_name));
21371 const char *name = NULL;
abc72ce4
DE
21372
21373 if (actual_name != NULL)
21374 {
15d034d0 21375 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21376
21377 if (die_name != NULL
43816ebc 21378 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21379 {
21380 /* Strip off the class name from the full name.
21381 We want the prefix. */
21382 int die_name_len = strlen (die_name);
43816ebc
TT
21383 int actual_name_len = strlen (actual_name.get ());
21384 const char *ptr = actual_name.get ();
abc72ce4
DE
21385
21386 /* Test for '::' as a sanity check. */
21387 if (actual_name_len > die_name_len + 2
43816ebc 21388 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21389 name = obstack_strndup (
e3b94546 21390 &objfile->per_bfd->storage_obstack,
43816ebc 21391 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21392 }
21393 }
abc72ce4
DE
21394 return name;
21395 }
21396 }
21397 }
21398
21399 return NULL;
21400}
21401
96408a79
SA
21402/* GCC might emit a nameless typedef that has a linkage name. Determine the
21403 prefix part in such case. See
21404 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21405
a121b7c1 21406static const char *
96408a79
SA
21407anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21408{
21409 struct attribute *attr;
e6a959d6 21410 const char *base;
96408a79
SA
21411
21412 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21413 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21414 return NULL;
21415
7d45c7c3 21416 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21417 return NULL;
21418
73b9be8b 21419 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21420 if (attr == NULL || DW_STRING (attr) == NULL)
21421 return NULL;
21422
21423 /* dwarf2_name had to be already called. */
21424 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21425
21426 /* Strip the base name, keep any leading namespaces/classes. */
21427 base = strrchr (DW_STRING (attr), ':');
21428 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21429 return "";
21430
518817b3 21431 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21432 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21433 DW_STRING (attr),
21434 &base[-1] - DW_STRING (attr));
96408a79
SA
21435}
21436
fdde2d81 21437/* Return the name of the namespace/class that DIE is defined within,
0114d602 21438 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21439
0114d602
DJ
21440 For example, if we're within the method foo() in the following
21441 code:
21442
21443 namespace N {
21444 class C {
21445 void foo () {
21446 }
21447 };
21448 }
21449
21450 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21451
0d5cff50 21452static const char *
e142c38c 21453determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21454{
518817b3
SM
21455 struct dwarf2_per_objfile *dwarf2_per_objfile
21456 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21457 struct die_info *parent, *spec_die;
21458 struct dwarf2_cu *spec_cu;
21459 struct type *parent_type;
a121b7c1 21460 const char *retval;
63d06c5c 21461
9c37b5ae 21462 if (cu->language != language_cplus
c44af4eb
TT
21463 && cu->language != language_fortran && cu->language != language_d
21464 && cu->language != language_rust)
0114d602
DJ
21465 return "";
21466
96408a79
SA
21467 retval = anonymous_struct_prefix (die, cu);
21468 if (retval)
21469 return retval;
21470
0114d602
DJ
21471 /* We have to be careful in the presence of DW_AT_specification.
21472 For example, with GCC 3.4, given the code
21473
21474 namespace N {
21475 void foo() {
21476 // Definition of N::foo.
21477 }
21478 }
21479
21480 then we'll have a tree of DIEs like this:
21481
21482 1: DW_TAG_compile_unit
21483 2: DW_TAG_namespace // N
21484 3: DW_TAG_subprogram // declaration of N::foo
21485 4: DW_TAG_subprogram // definition of N::foo
21486 DW_AT_specification // refers to die #3
21487
21488 Thus, when processing die #4, we have to pretend that we're in
21489 the context of its DW_AT_specification, namely the contex of die
21490 #3. */
21491 spec_cu = cu;
21492 spec_die = die_specification (die, &spec_cu);
21493 if (spec_die == NULL)
21494 parent = die->parent;
21495 else
63d06c5c 21496 {
0114d602
DJ
21497 parent = spec_die->parent;
21498 cu = spec_cu;
63d06c5c 21499 }
0114d602
DJ
21500
21501 if (parent == NULL)
21502 return "";
98bfdba5
PA
21503 else if (parent->building_fullname)
21504 {
21505 const char *name;
21506 const char *parent_name;
21507
21508 /* It has been seen on RealView 2.2 built binaries,
21509 DW_TAG_template_type_param types actually _defined_ as
21510 children of the parent class:
21511
21512 enum E {};
21513 template class <class Enum> Class{};
21514 Class<enum E> class_e;
21515
21516 1: DW_TAG_class_type (Class)
21517 2: DW_TAG_enumeration_type (E)
21518 3: DW_TAG_enumerator (enum1:0)
21519 3: DW_TAG_enumerator (enum2:1)
21520 ...
21521 2: DW_TAG_template_type_param
21522 DW_AT_type DW_FORM_ref_udata (E)
21523
21524 Besides being broken debug info, it can put GDB into an
21525 infinite loop. Consider:
21526
21527 When we're building the full name for Class<E>, we'll start
21528 at Class, and go look over its template type parameters,
21529 finding E. We'll then try to build the full name of E, and
21530 reach here. We're now trying to build the full name of E,
21531 and look over the parent DIE for containing scope. In the
21532 broken case, if we followed the parent DIE of E, we'd again
21533 find Class, and once again go look at its template type
21534 arguments, etc., etc. Simply don't consider such parent die
21535 as source-level parent of this die (it can't be, the language
21536 doesn't allow it), and break the loop here. */
21537 name = dwarf2_name (die, cu);
21538 parent_name = dwarf2_name (parent, cu);
b98664d3 21539 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21540 name ? name : "<unknown>",
21541 parent_name ? parent_name : "<unknown>");
21542 return "";
21543 }
63d06c5c 21544 else
0114d602
DJ
21545 switch (parent->tag)
21546 {
63d06c5c 21547 case DW_TAG_namespace:
0114d602 21548 parent_type = read_type_die (parent, cu);
acebe513
UW
21549 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21550 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21551 Work around this problem here. */
21552 if (cu->language == language_cplus
e86ca25f 21553 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21554 return "";
0114d602 21555 /* We give a name to even anonymous namespaces. */
e86ca25f 21556 return TYPE_NAME (parent_type);
63d06c5c 21557 case DW_TAG_class_type:
680b30c7 21558 case DW_TAG_interface_type:
63d06c5c 21559 case DW_TAG_structure_type:
0114d602 21560 case DW_TAG_union_type:
f55ee35c 21561 case DW_TAG_module:
0114d602 21562 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21563 if (TYPE_NAME (parent_type) != NULL)
21564 return TYPE_NAME (parent_type);
0114d602
DJ
21565 else
21566 /* An anonymous structure is only allowed non-static data
21567 members; no typedefs, no member functions, et cetera.
21568 So it does not need a prefix. */
21569 return "";
abc72ce4 21570 case DW_TAG_compile_unit:
95554aad 21571 case DW_TAG_partial_unit:
abc72ce4
DE
21572 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21573 if (cu->language == language_cplus
fd5866f6 21574 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21575 && die->child != NULL
21576 && (die->tag == DW_TAG_class_type
21577 || die->tag == DW_TAG_structure_type
21578 || die->tag == DW_TAG_union_type))
21579 {
43816ebc 21580 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21581 if (name != NULL)
21582 return name;
21583 }
21584 return "";
0a4b0913
AB
21585 case DW_TAG_subprogram:
21586 /* Nested subroutines in Fortran get a prefix with the name
21587 of the parent's subroutine. */
21588 if (cu->language == language_fortran)
21589 {
21590 if ((die->tag == DW_TAG_subprogram)
21591 && (dwarf2_name (parent, cu) != NULL))
21592 return dwarf2_name (parent, cu);
21593 }
21594 return determine_prefix (parent, cu);
3d567982
TT
21595 case DW_TAG_enumeration_type:
21596 parent_type = read_type_die (parent, cu);
21597 if (TYPE_DECLARED_CLASS (parent_type))
21598 {
e86ca25f
TT
21599 if (TYPE_NAME (parent_type) != NULL)
21600 return TYPE_NAME (parent_type);
3d567982
TT
21601 return "";
21602 }
21603 /* Fall through. */
63d06c5c 21604 default:
8176b9b8 21605 return determine_prefix (parent, cu);
63d06c5c 21606 }
63d06c5c
DC
21607}
21608
3e43a32a
MS
21609/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21610 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21611 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21612 an obconcat, otherwise allocate storage for the result. The CU argument is
21613 used to determine the language and hence, the appropriate separator. */
987504bb 21614
f55ee35c 21615#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21616
21617static char *
f55ee35c
JK
21618typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21619 int physname, struct dwarf2_cu *cu)
63d06c5c 21620{
f55ee35c 21621 const char *lead = "";
5c315b68 21622 const char *sep;
63d06c5c 21623
3e43a32a
MS
21624 if (suffix == NULL || suffix[0] == '\0'
21625 || prefix == NULL || prefix[0] == '\0')
987504bb 21626 sep = "";
45280282
IB
21627 else if (cu->language == language_d)
21628 {
21629 /* For D, the 'main' function could be defined in any module, but it
21630 should never be prefixed. */
21631 if (strcmp (suffix, "D main") == 0)
21632 {
21633 prefix = "";
21634 sep = "";
21635 }
21636 else
21637 sep = ".";
21638 }
f55ee35c
JK
21639 else if (cu->language == language_fortran && physname)
21640 {
21641 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21642 DW_AT_MIPS_linkage_name is preferred and used instead. */
21643
21644 lead = "__";
21645 sep = "_MOD_";
21646 }
987504bb
JJ
21647 else
21648 sep = "::";
63d06c5c 21649
6dd47d34
DE
21650 if (prefix == NULL)
21651 prefix = "";
21652 if (suffix == NULL)
21653 suffix = "";
21654
987504bb
JJ
21655 if (obs == NULL)
21656 {
3e43a32a 21657 char *retval
224c3ddb
SM
21658 = ((char *)
21659 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21660
f55ee35c
JK
21661 strcpy (retval, lead);
21662 strcat (retval, prefix);
6dd47d34
DE
21663 strcat (retval, sep);
21664 strcat (retval, suffix);
63d06c5c
DC
21665 return retval;
21666 }
987504bb
JJ
21667 else
21668 {
21669 /* We have an obstack. */
f55ee35c 21670 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21671 }
63d06c5c
DC
21672}
21673
c906108c
SS
21674/* Return sibling of die, NULL if no sibling. */
21675
f9aca02d 21676static struct die_info *
fba45db2 21677sibling_die (struct die_info *die)
c906108c 21678{
639d11d3 21679 return die->sibling;
c906108c
SS
21680}
21681
71c25dea
TT
21682/* Get name of a die, return NULL if not found. */
21683
15d034d0
TT
21684static const char *
21685dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
21686 struct obstack *obstack)
21687{
21688 if (name && cu->language == language_cplus)
21689 {
2f408ecb 21690 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21691
2f408ecb 21692 if (!canon_name.empty ())
71c25dea 21693 {
2f408ecb 21694 if (canon_name != name)
efba19b0 21695 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
21696 }
21697 }
21698
21699 return name;
c906108c
SS
21700}
21701
96553a0c
DE
21702/* Get name of a die, return NULL if not found.
21703 Anonymous namespaces are converted to their magic string. */
9219021c 21704
15d034d0 21705static const char *
e142c38c 21706dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21707{
21708 struct attribute *attr;
518817b3 21709 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21710
e142c38c 21711 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21712 if ((!attr || !DW_STRING (attr))
96553a0c 21713 && die->tag != DW_TAG_namespace
53832f31
TT
21714 && die->tag != DW_TAG_class_type
21715 && die->tag != DW_TAG_interface_type
21716 && die->tag != DW_TAG_structure_type
21717 && die->tag != DW_TAG_union_type)
71c25dea
TT
21718 return NULL;
21719
21720 switch (die->tag)
21721 {
21722 case DW_TAG_compile_unit:
95554aad 21723 case DW_TAG_partial_unit:
71c25dea
TT
21724 /* Compilation units have a DW_AT_name that is a filename, not
21725 a source language identifier. */
21726 case DW_TAG_enumeration_type:
21727 case DW_TAG_enumerator:
21728 /* These tags always have simple identifiers already; no need
21729 to canonicalize them. */
21730 return DW_STRING (attr);
907af001 21731
96553a0c
DE
21732 case DW_TAG_namespace:
21733 if (attr != NULL && DW_STRING (attr) != NULL)
21734 return DW_STRING (attr);
21735 return CP_ANONYMOUS_NAMESPACE_STR;
21736
907af001
UW
21737 case DW_TAG_class_type:
21738 case DW_TAG_interface_type:
21739 case DW_TAG_structure_type:
21740 case DW_TAG_union_type:
21741 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21742 structures or unions. These were of the form "._%d" in GCC 4.1,
21743 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21744 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21745 if (attr && DW_STRING (attr)
61012eef
GB
21746 && (startswith (DW_STRING (attr), "._")
21747 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21748 return NULL;
53832f31
TT
21749
21750 /* GCC might emit a nameless typedef that has a linkage name. See
21751 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21752 if (!attr || DW_STRING (attr) == NULL)
21753 {
73b9be8b 21754 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21755 if (attr == NULL || DW_STRING (attr) == NULL)
21756 return NULL;
21757
df5c6c50
JK
21758 /* Avoid demangling DW_STRING (attr) the second time on a second
21759 call for the same DIE. */
21760 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21761 {
43816ebc
TT
21762 gdb::unique_xmalloc_ptr<char> demangled
21763 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21764
e6a959d6 21765 const char *base;
96408a79 21766
53832f31 21767 /* FIXME: we already did this for the partial symbol... */
34a68019 21768 DW_STRING (attr)
021887d8 21769 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 21770 demangled.get ());
53832f31 21771 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
21772
21773 /* Strip any leading namespaces/classes, keep only the base name.
21774 DW_AT_name for named DIEs does not contain the prefixes. */
21775 base = strrchr (DW_STRING (attr), ':');
21776 if (base && base > DW_STRING (attr) && base[-1] == ':')
21777 return &base[1];
21778 else
21779 return DW_STRING (attr);
53832f31
TT
21780 }
21781 }
907af001
UW
21782 break;
21783
71c25dea 21784 default:
907af001
UW
21785 break;
21786 }
21787
21788 if (!DW_STRING_IS_CANONICAL (attr))
21789 {
21790 DW_STRING (attr)
21791 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 21792 &objfile->per_bfd->storage_obstack);
907af001 21793 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21794 }
907af001 21795 return DW_STRING (attr);
9219021c
DC
21796}
21797
21798/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21799 is none. *EXT_CU is the CU containing DIE on input, and the CU
21800 containing the return value on output. */
9219021c
DC
21801
21802static struct die_info *
f2f0e013 21803dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21804{
21805 struct attribute *attr;
9219021c 21806
f2f0e013 21807 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21808 if (attr == NULL)
21809 return NULL;
21810
f2f0e013 21811 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21812}
21813
fa9c3fa0
TT
21814/* A convenience function that returns an "unknown" DWARF name,
21815 including the value of V. STR is the name of the entity being
21816 printed, e.g., "TAG". */
21817
21818static const char *
21819dwarf_unknown (const char *str, unsigned v)
21820{
21821 char *cell = get_print_cell ();
21822 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21823 return cell;
21824}
21825
c906108c
SS
21826/* Convert a DIE tag into its string name. */
21827
f39c6ffd 21828static const char *
aa1ee363 21829dwarf_tag_name (unsigned tag)
c906108c 21830{
f39c6ffd
TT
21831 const char *name = get_DW_TAG_name (tag);
21832
21833 if (name == NULL)
fa9c3fa0 21834 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
21835
21836 return name;
c906108c
SS
21837}
21838
21839/* Convert a DWARF attribute code into its string name. */
21840
f39c6ffd 21841static const char *
aa1ee363 21842dwarf_attr_name (unsigned attr)
c906108c 21843{
f39c6ffd
TT
21844 const char *name;
21845
c764a876 21846#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21847 if (attr == DW_AT_MIPS_fde)
21848 return "DW_AT_MIPS_fde";
21849#else
21850 if (attr == DW_AT_HP_block_index)
21851 return "DW_AT_HP_block_index";
c764a876 21852#endif
f39c6ffd
TT
21853
21854 name = get_DW_AT_name (attr);
21855
21856 if (name == NULL)
fa9c3fa0 21857 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
21858
21859 return name;
c906108c
SS
21860}
21861
21862/* Convert a DWARF value form code into its string name. */
21863
f39c6ffd 21864static const char *
aa1ee363 21865dwarf_form_name (unsigned form)
c906108c 21866{
f39c6ffd
TT
21867 const char *name = get_DW_FORM_name (form);
21868
21869 if (name == NULL)
fa9c3fa0 21870 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
21871
21872 return name;
c906108c
SS
21873}
21874
a121b7c1 21875static const char *
fba45db2 21876dwarf_bool_name (unsigned mybool)
c906108c
SS
21877{
21878 if (mybool)
21879 return "TRUE";
21880 else
21881 return "FALSE";
21882}
21883
21884/* Convert a DWARF type code into its string name. */
21885
f39c6ffd 21886static const char *
aa1ee363 21887dwarf_type_encoding_name (unsigned enc)
c906108c 21888{
f39c6ffd 21889 const char *name = get_DW_ATE_name (enc);
c906108c 21890
f39c6ffd 21891 if (name == NULL)
fa9c3fa0 21892 return dwarf_unknown ("ATE", enc);
c906108c 21893
f39c6ffd 21894 return name;
c906108c 21895}
c906108c 21896
f9aca02d 21897static void
d97bc12b 21898dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21899{
21900 unsigned int i;
21901
d97bc12b 21902 print_spaces (indent, f);
9d8780f0 21903 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21904 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21905 sect_offset_str (die->sect_off));
d97bc12b
DE
21906
21907 if (die->parent != NULL)
21908 {
21909 print_spaces (indent, f);
9d8780f0
SM
21910 fprintf_unfiltered (f, " parent at offset: %s\n",
21911 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21912 }
21913
21914 print_spaces (indent, f);
21915 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21916 dwarf_bool_name (die->child != NULL));
c906108c 21917
d97bc12b
DE
21918 print_spaces (indent, f);
21919 fprintf_unfiltered (f, " attributes:\n");
21920
c906108c
SS
21921 for (i = 0; i < die->num_attrs; ++i)
21922 {
d97bc12b
DE
21923 print_spaces (indent, f);
21924 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21925 dwarf_attr_name (die->attrs[i].name),
21926 dwarf_form_name (die->attrs[i].form));
d97bc12b 21927
c906108c
SS
21928 switch (die->attrs[i].form)
21929 {
c906108c 21930 case DW_FORM_addr:
336d760d 21931 case DW_FORM_addrx:
3019eac3 21932 case DW_FORM_GNU_addr_index:
d97bc12b 21933 fprintf_unfiltered (f, "address: ");
5af949e3 21934 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21935 break;
21936 case DW_FORM_block2:
21937 case DW_FORM_block4:
21938 case DW_FORM_block:
21939 case DW_FORM_block1:
56eb65bd
SP
21940 fprintf_unfiltered (f, "block: size %s",
21941 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21942 break;
2dc7f7b3 21943 case DW_FORM_exprloc:
56eb65bd
SP
21944 fprintf_unfiltered (f, "expression: size %s",
21945 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21946 break;
0224619f
JK
21947 case DW_FORM_data16:
21948 fprintf_unfiltered (f, "constant of 16 bytes");
21949 break;
4568ecf9
DE
21950 case DW_FORM_ref_addr:
21951 fprintf_unfiltered (f, "ref address: ");
21952 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21953 break;
36586728
TT
21954 case DW_FORM_GNU_ref_alt:
21955 fprintf_unfiltered (f, "alt ref address: ");
21956 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21957 break;
10b3939b
DJ
21958 case DW_FORM_ref1:
21959 case DW_FORM_ref2:
21960 case DW_FORM_ref4:
4568ecf9
DE
21961 case DW_FORM_ref8:
21962 case DW_FORM_ref_udata:
d97bc12b 21963 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21964 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21965 break;
c906108c
SS
21966 case DW_FORM_data1:
21967 case DW_FORM_data2:
21968 case DW_FORM_data4:
ce5d95e1 21969 case DW_FORM_data8:
c906108c
SS
21970 case DW_FORM_udata:
21971 case DW_FORM_sdata:
43bbcdc2
PH
21972 fprintf_unfiltered (f, "constant: %s",
21973 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21974 break;
2dc7f7b3
TT
21975 case DW_FORM_sec_offset:
21976 fprintf_unfiltered (f, "section offset: %s",
21977 pulongest (DW_UNSND (&die->attrs[i])));
21978 break;
55f1336d 21979 case DW_FORM_ref_sig8:
ac9ec31b
DE
21980 fprintf_unfiltered (f, "signature: %s",
21981 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21982 break;
c906108c 21983 case DW_FORM_string:
4bdf3d34 21984 case DW_FORM_strp:
43988095 21985 case DW_FORM_line_strp:
cf532bd1 21986 case DW_FORM_strx:
3019eac3 21987 case DW_FORM_GNU_str_index:
36586728 21988 case DW_FORM_GNU_strp_alt:
8285870a 21989 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21990 DW_STRING (&die->attrs[i])
8285870a
JK
21991 ? DW_STRING (&die->attrs[i]) : "",
21992 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21993 break;
21994 case DW_FORM_flag:
21995 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21996 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21997 else
d97bc12b 21998 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21999 break;
2dc7f7b3
TT
22000 case DW_FORM_flag_present:
22001 fprintf_unfiltered (f, "flag: TRUE");
22002 break;
a8329558 22003 case DW_FORM_indirect:
0963b4bd
MS
22004 /* The reader will have reduced the indirect form to
22005 the "base form" so this form should not occur. */
5f48f8f3 22006 fprintf_unfiltered (f,
3e43a32a 22007 "unexpected attribute form: DW_FORM_indirect");
a8329558 22008 break;
663c44ac
JK
22009 case DW_FORM_implicit_const:
22010 fprintf_unfiltered (f, "constant: %s",
22011 plongest (DW_SND (&die->attrs[i])));
22012 break;
c906108c 22013 default:
d97bc12b 22014 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22015 die->attrs[i].form);
d97bc12b 22016 break;
c906108c 22017 }
d97bc12b 22018 fprintf_unfiltered (f, "\n");
c906108c
SS
22019 }
22020}
22021
f9aca02d 22022static void
d97bc12b 22023dump_die_for_error (struct die_info *die)
c906108c 22024{
d97bc12b
DE
22025 dump_die_shallow (gdb_stderr, 0, die);
22026}
22027
22028static void
22029dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22030{
22031 int indent = level * 4;
22032
22033 gdb_assert (die != NULL);
22034
22035 if (level >= max_level)
22036 return;
22037
22038 dump_die_shallow (f, indent, die);
22039
22040 if (die->child != NULL)
c906108c 22041 {
d97bc12b
DE
22042 print_spaces (indent, f);
22043 fprintf_unfiltered (f, " Children:");
22044 if (level + 1 < max_level)
22045 {
22046 fprintf_unfiltered (f, "\n");
22047 dump_die_1 (f, level + 1, max_level, die->child);
22048 }
22049 else
22050 {
3e43a32a
MS
22051 fprintf_unfiltered (f,
22052 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22053 }
22054 }
22055
22056 if (die->sibling != NULL && level > 0)
22057 {
22058 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22059 }
22060}
22061
d97bc12b
DE
22062/* This is called from the pdie macro in gdbinit.in.
22063 It's not static so gcc will keep a copy callable from gdb. */
22064
22065void
22066dump_die (struct die_info *die, int max_level)
22067{
22068 dump_die_1 (gdb_stdlog, 0, max_level, die);
22069}
22070
f9aca02d 22071static void
51545339 22072store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22073{
51545339 22074 void **slot;
c906108c 22075
9c541725
PA
22076 slot = htab_find_slot_with_hash (cu->die_hash, die,
22077 to_underlying (die->sect_off),
b64f50a1 22078 INSERT);
51545339
DJ
22079
22080 *slot = die;
c906108c
SS
22081}
22082
b64f50a1
JK
22083/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22084 required kind. */
22085
22086static sect_offset
ff39bb5e 22087dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22088{
cd6c91b4 22089 if (attr->form_is_ref ())
9c541725 22090 return (sect_offset) DW_UNSND (attr);
93311388 22091
b98664d3 22092 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22093 dwarf_form_name (attr->form));
9c541725 22094 return {};
c906108c
SS
22095}
22096
43bbcdc2
PH
22097/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22098 * the value held by the attribute is not constant. */
a02abb62 22099
43bbcdc2 22100static LONGEST
ff39bb5e 22101dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22102{
663c44ac 22103 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22104 return DW_SND (attr);
22105 else if (attr->form == DW_FORM_udata
22106 || attr->form == DW_FORM_data1
22107 || attr->form == DW_FORM_data2
22108 || attr->form == DW_FORM_data4
22109 || attr->form == DW_FORM_data8)
22110 return DW_UNSND (attr);
22111 else
22112 {
cd6c91b4 22113 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22114 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22115 dwarf_form_name (attr->form));
22116 return default_value;
22117 }
22118}
22119
348e048f
DE
22120/* Follow reference or signature attribute ATTR of SRC_DIE.
22121 On entry *REF_CU is the CU of SRC_DIE.
22122 On exit *REF_CU is the CU of the result. */
22123
22124static struct die_info *
ff39bb5e 22125follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22126 struct dwarf2_cu **ref_cu)
22127{
22128 struct die_info *die;
22129
cd6c91b4 22130 if (attr->form_is_ref ())
348e048f 22131 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22132 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22133 die = follow_die_sig (src_die, attr, ref_cu);
22134 else
22135 {
22136 dump_die_for_error (src_die);
22137 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22138 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22139 }
22140
22141 return die;
03dd20cc
DJ
22142}
22143
5c631832 22144/* Follow reference OFFSET.
673bfd45
DE
22145 On entry *REF_CU is the CU of the source die referencing OFFSET.
22146 On exit *REF_CU is the CU of the result.
22147 Returns NULL if OFFSET is invalid. */
f504f079 22148
f9aca02d 22149static struct die_info *
9c541725 22150follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22151 struct dwarf2_cu **ref_cu)
c906108c 22152{
10b3939b 22153 struct die_info temp_die;
f2f0e013 22154 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22155 struct dwarf2_per_objfile *dwarf2_per_objfile
22156 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22157
348e048f
DE
22158 gdb_assert (cu->per_cu != NULL);
22159
98bfdba5
PA
22160 target_cu = cu;
22161
3019eac3 22162 if (cu->per_cu->is_debug_types)
348e048f
DE
22163 {
22164 /* .debug_types CUs cannot reference anything outside their CU.
22165 If they need to, they have to reference a signatured type via
55f1336d 22166 DW_FORM_ref_sig8. */
4057dfde 22167 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22168 return NULL;
348e048f 22169 }
36586728 22170 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22171 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22172 {
22173 struct dwarf2_per_cu_data *per_cu;
9a619af0 22174
9c541725 22175 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22176 dwarf2_per_objfile);
03dd20cc
DJ
22177
22178 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22179 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22180 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22181
10b3939b
DJ
22182 target_cu = per_cu->cu;
22183 }
98bfdba5
PA
22184 else if (cu->dies == NULL)
22185 {
22186 /* We're loading full DIEs during partial symbol reading. */
22187 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22188 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22189 }
c906108c 22190
f2f0e013 22191 *ref_cu = target_cu;
9c541725 22192 temp_die.sect_off = sect_off;
c24bdb02
KS
22193
22194 if (target_cu != cu)
22195 target_cu->ancestor = cu;
22196
9a3c8263 22197 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22198 &temp_die,
22199 to_underlying (sect_off));
5c631832 22200}
10b3939b 22201
5c631832
JK
22202/* Follow reference attribute ATTR of SRC_DIE.
22203 On entry *REF_CU is the CU of SRC_DIE.
22204 On exit *REF_CU is the CU of the result. */
22205
22206static struct die_info *
ff39bb5e 22207follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22208 struct dwarf2_cu **ref_cu)
22209{
9c541725 22210 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22211 struct dwarf2_cu *cu = *ref_cu;
22212 struct die_info *die;
22213
9c541725 22214 die = follow_die_offset (sect_off,
36586728
TT
22215 (attr->form == DW_FORM_GNU_ref_alt
22216 || cu->per_cu->is_dwz),
22217 ref_cu);
5c631832 22218 if (!die)
9d8780f0
SM
22219 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22220 "at %s [in module %s]"),
22221 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22222 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22223
5c631832
JK
22224 return die;
22225}
22226
9c541725 22227/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22228 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22229 dwarf2_locexpr_baton->data has lifetime of
22230 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22231
22232struct dwarf2_locexpr_baton
9c541725 22233dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22234 struct dwarf2_per_cu_data *per_cu,
22235 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22236 void *baton, bool resolve_abstract_p)
5c631832 22237{
918dd910 22238 struct dwarf2_cu *cu;
5c631832
JK
22239 struct die_info *die;
22240 struct attribute *attr;
22241 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22242 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22243 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22244
918dd910 22245 if (per_cu->cu == NULL)
58f0c718 22246 load_cu (per_cu, false);
918dd910 22247 cu = per_cu->cu;
cc12ce38
DE
22248 if (cu == NULL)
22249 {
22250 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22251 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22252 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22253 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22254 }
918dd910 22255
9c541725 22256 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22257 if (!die)
9d8780f0
SM
22258 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22259 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22260
22261 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22262 if (!attr && resolve_abstract_p
3360b6e7 22263 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22264 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22265 {
22266 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22267 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22269
3360b6e7
TV
22270 for (const auto &cand_off
22271 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22272 {
3360b6e7
TV
22273 struct dwarf2_cu *cand_cu = cu;
22274 struct die_info *cand
22275 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22276 if (!cand
22277 || !cand->parent
e4a62c65
TV
22278 || cand->parent->tag != DW_TAG_subprogram)
22279 continue;
22280
22281 CORE_ADDR pc_low, pc_high;
22282 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22283 if (pc_low == ((CORE_ADDR) -1))
22284 continue;
22285 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22286 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22287 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22288 continue;
22289
22290 die = cand;
22291 attr = dwarf2_attr (die, DW_AT_location, cu);
22292 break;
22293 }
22294 }
22295
5c631832
JK
22296 if (!attr)
22297 {
e103e986
JK
22298 /* DWARF: "If there is no such attribute, then there is no effect.".
22299 DATA is ignored if SIZE is 0. */
5c631832 22300
e103e986 22301 retval.data = NULL;
5c631832
JK
22302 retval.size = 0;
22303 }
cd6c91b4 22304 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22305 {
22306 struct dwarf2_loclist_baton loclist_baton;
22307 CORE_ADDR pc = (*get_frame_pc) (baton);
22308 size_t size;
22309
22310 fill_in_loclist_baton (cu, &loclist_baton, attr);
22311
22312 retval.data = dwarf2_find_location_expression (&loclist_baton,
22313 &size, pc);
22314 retval.size = size;
22315 }
5c631832
JK
22316 else
22317 {
4fc6c0d5 22318 if (!attr->form_is_block ())
9d8780f0 22319 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22320 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22321 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22322
22323 retval.data = DW_BLOCK (attr)->data;
22324 retval.size = DW_BLOCK (attr)->size;
22325 }
22326 retval.per_cu = cu->per_cu;
918dd910 22327
ed2dc618 22328 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22329
5c631832 22330 return retval;
348e048f
DE
22331}
22332
8b9737bf
TT
22333/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22334 offset. */
22335
22336struct dwarf2_locexpr_baton
22337dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22338 struct dwarf2_per_cu_data *per_cu,
22339 CORE_ADDR (*get_frame_pc) (void *baton),
22340 void *baton)
22341{
9c541725 22342 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22343
9c541725 22344 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22345}
22346
b6807d98
TT
22347/* Write a constant of a given type as target-ordered bytes into
22348 OBSTACK. */
22349
22350static const gdb_byte *
22351write_constant_as_bytes (struct obstack *obstack,
22352 enum bfd_endian byte_order,
22353 struct type *type,
22354 ULONGEST value,
22355 LONGEST *len)
22356{
22357 gdb_byte *result;
22358
22359 *len = TYPE_LENGTH (type);
224c3ddb 22360 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22361 store_unsigned_integer (result, *len, byte_order, value);
22362
22363 return result;
22364}
22365
22366/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22367 pointer to the constant bytes and set LEN to the length of the
22368 data. If memory is needed, allocate it on OBSTACK. If the DIE
22369 does not have a DW_AT_const_value, return NULL. */
22370
22371const gdb_byte *
9c541725 22372dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
22373 struct dwarf2_per_cu_data *per_cu,
22374 struct obstack *obstack,
22375 LONGEST *len)
22376{
22377 struct dwarf2_cu *cu;
22378 struct die_info *die;
22379 struct attribute *attr;
22380 const gdb_byte *result = NULL;
22381 struct type *type;
22382 LONGEST value;
22383 enum bfd_endian byte_order;
e3b94546 22384 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22385
b6807d98 22386 if (per_cu->cu == NULL)
58f0c718 22387 load_cu (per_cu, false);
b6807d98 22388 cu = per_cu->cu;
cc12ce38
DE
22389 if (cu == NULL)
22390 {
22391 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22392 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22393 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22394 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22395 }
b6807d98 22396
9c541725 22397 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22398 if (!die)
9d8780f0
SM
22399 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22400 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22401
22402 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22403 if (attr == NULL)
22404 return NULL;
22405
e3b94546 22406 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22407 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22408
22409 switch (attr->form)
22410 {
22411 case DW_FORM_addr:
336d760d 22412 case DW_FORM_addrx:
b6807d98
TT
22413 case DW_FORM_GNU_addr_index:
22414 {
22415 gdb_byte *tem;
22416
22417 *len = cu->header.addr_size;
224c3ddb 22418 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22419 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22420 result = tem;
22421 }
22422 break;
22423 case DW_FORM_string:
22424 case DW_FORM_strp:
cf532bd1 22425 case DW_FORM_strx:
b6807d98
TT
22426 case DW_FORM_GNU_str_index:
22427 case DW_FORM_GNU_strp_alt:
22428 /* DW_STRING is already allocated on the objfile obstack, point
22429 directly to it. */
22430 result = (const gdb_byte *) DW_STRING (attr);
22431 *len = strlen (DW_STRING (attr));
22432 break;
22433 case DW_FORM_block1:
22434 case DW_FORM_block2:
22435 case DW_FORM_block4:
22436 case DW_FORM_block:
22437 case DW_FORM_exprloc:
0224619f 22438 case DW_FORM_data16:
b6807d98
TT
22439 result = DW_BLOCK (attr)->data;
22440 *len = DW_BLOCK (attr)->size;
22441 break;
22442
22443 /* The DW_AT_const_value attributes are supposed to carry the
22444 symbol's value "represented as it would be on the target
22445 architecture." By the time we get here, it's already been
22446 converted to host endianness, so we just need to sign- or
22447 zero-extend it as appropriate. */
22448 case DW_FORM_data1:
22449 type = die_type (die, cu);
22450 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22451 if (result == NULL)
22452 result = write_constant_as_bytes (obstack, byte_order,
22453 type, value, len);
22454 break;
22455 case DW_FORM_data2:
22456 type = die_type (die, cu);
22457 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22458 if (result == NULL)
22459 result = write_constant_as_bytes (obstack, byte_order,
22460 type, value, len);
22461 break;
22462 case DW_FORM_data4:
22463 type = die_type (die, cu);
22464 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22465 if (result == NULL)
22466 result = write_constant_as_bytes (obstack, byte_order,
22467 type, value, len);
22468 break;
22469 case DW_FORM_data8:
22470 type = die_type (die, cu);
22471 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22472 if (result == NULL)
22473 result = write_constant_as_bytes (obstack, byte_order,
22474 type, value, len);
22475 break;
22476
22477 case DW_FORM_sdata:
663c44ac 22478 case DW_FORM_implicit_const:
b6807d98
TT
22479 type = die_type (die, cu);
22480 result = write_constant_as_bytes (obstack, byte_order,
22481 type, DW_SND (attr), len);
22482 break;
22483
22484 case DW_FORM_udata:
22485 type = die_type (die, cu);
22486 result = write_constant_as_bytes (obstack, byte_order,
22487 type, DW_UNSND (attr), len);
22488 break;
22489
22490 default:
b98664d3 22491 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22492 dwarf_form_name (attr->form));
22493 break;
22494 }
22495
22496 return result;
22497}
22498
7942e96e
AA
22499/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22500 valid type for this die is found. */
22501
22502struct type *
9c541725 22503dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
22504 struct dwarf2_per_cu_data *per_cu)
22505{
22506 struct dwarf2_cu *cu;
22507 struct die_info *die;
22508
7942e96e 22509 if (per_cu->cu == NULL)
58f0c718 22510 load_cu (per_cu, false);
7942e96e
AA
22511 cu = per_cu->cu;
22512 if (!cu)
22513 return NULL;
22514
9c541725 22515 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22516 if (!die)
22517 return NULL;
22518
22519 return die_type (die, cu);
22520}
22521
8a9b8146
TT
22522/* Return the type of the DIE at DIE_OFFSET in the CU named by
22523 PER_CU. */
22524
22525struct type *
b64f50a1 22526dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22527 struct dwarf2_per_cu_data *per_cu)
22528{
9c541725 22529 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22530 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22531}
22532
ac9ec31b 22533/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22534 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22535 On exit *REF_CU is the CU of the result.
22536 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22537
22538static struct die_info *
ac9ec31b
DE
22539follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22540 struct dwarf2_cu **ref_cu)
348e048f 22541{
348e048f 22542 struct die_info temp_die;
c24bdb02 22543 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22544 struct die_info *die;
22545
ac9ec31b
DE
22546 /* While it might be nice to assert sig_type->type == NULL here,
22547 we can get here for DW_AT_imported_declaration where we need
22548 the DIE not the type. */
348e048f
DE
22549
22550 /* If necessary, add it to the queue and load its DIEs. */
22551
95554aad 22552 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22553 read_signatured_type (sig_type);
348e048f 22554
348e048f 22555 sig_cu = sig_type->per_cu.cu;
69d751e3 22556 gdb_assert (sig_cu != NULL);
9c541725
PA
22557 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22558 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22559 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22560 to_underlying (temp_die.sect_off));
348e048f
DE
22561 if (die)
22562 {
ed2dc618 22563 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22564 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22565
796a7ff8
DE
22566 /* For .gdb_index version 7 keep track of included TUs.
22567 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22568 if (dwarf2_per_objfile->index_table != NULL
22569 && dwarf2_per_objfile->index_table->version <= 7)
22570 {
ae640021 22571 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22572 }
22573
348e048f 22574 *ref_cu = sig_cu;
c24bdb02
KS
22575 if (sig_cu != cu)
22576 sig_cu->ancestor = cu;
22577
348e048f
DE
22578 return die;
22579 }
22580
ac9ec31b
DE
22581 return NULL;
22582}
22583
22584/* Follow signatured type referenced by ATTR in SRC_DIE.
22585 On entry *REF_CU is the CU of SRC_DIE.
22586 On exit *REF_CU is the CU of the result.
22587 The result is the DIE of the type.
22588 If the referenced type cannot be found an error is thrown. */
22589
22590static struct die_info *
ff39bb5e 22591follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22592 struct dwarf2_cu **ref_cu)
22593{
22594 ULONGEST signature = DW_SIGNATURE (attr);
22595 struct signatured_type *sig_type;
22596 struct die_info *die;
22597
22598 gdb_assert (attr->form == DW_FORM_ref_sig8);
22599
a2ce51a0 22600 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22601 /* sig_type will be NULL if the signatured type is missing from
22602 the debug info. */
22603 if (sig_type == NULL)
22604 {
22605 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22606 " from DIE at %s [in module %s]"),
22607 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22608 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22609 }
22610
22611 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22612 if (die == NULL)
22613 {
22614 dump_die_for_error (src_die);
22615 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22616 " from DIE at %s [in module %s]"),
22617 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22618 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22619 }
22620
22621 return die;
22622}
22623
22624/* Get the type specified by SIGNATURE referenced in DIE/CU,
22625 reading in and processing the type unit if necessary. */
22626
22627static struct type *
22628get_signatured_type (struct die_info *die, ULONGEST signature,
22629 struct dwarf2_cu *cu)
22630{
518817b3
SM
22631 struct dwarf2_per_objfile *dwarf2_per_objfile
22632 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22633 struct signatured_type *sig_type;
22634 struct dwarf2_cu *type_cu;
22635 struct die_info *type_die;
22636 struct type *type;
22637
a2ce51a0 22638 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22639 /* sig_type will be NULL if the signatured type is missing from
22640 the debug info. */
22641 if (sig_type == NULL)
22642 {
b98664d3 22643 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22644 " from DIE at %s [in module %s]"),
22645 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22646 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22647 return build_error_marker_type (cu, die);
22648 }
22649
22650 /* If we already know the type we're done. */
22651 if (sig_type->type != NULL)
22652 return sig_type->type;
22653
22654 type_cu = cu;
22655 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22656 if (type_die != NULL)
22657 {
22658 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22659 is created. This is important, for example, because for c++ classes
22660 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22661 type = read_type_die (type_die, type_cu);
22662 if (type == NULL)
22663 {
b98664d3 22664 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22665 " referenced from DIE at %s [in module %s]"),
22666 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22667 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22668 type = build_error_marker_type (cu, die);
22669 }
22670 }
22671 else
22672 {
b98664d3 22673 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22674 " from DIE at %s [in module %s]"),
22675 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22676 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22677 type = build_error_marker_type (cu, die);
22678 }
22679 sig_type->type = type;
22680
22681 return type;
22682}
22683
22684/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22685 reading in and processing the type unit if necessary. */
22686
22687static struct type *
ff39bb5e 22688get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22689 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22690{
22691 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22692 if (attr->form_is_ref ())
ac9ec31b
DE
22693 {
22694 struct dwarf2_cu *type_cu = cu;
22695 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22696
22697 return read_type_die (type_die, type_cu);
22698 }
22699 else if (attr->form == DW_FORM_ref_sig8)
22700 {
22701 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22702 }
22703 else
22704 {
518817b3
SM
22705 struct dwarf2_per_objfile *dwarf2_per_objfile
22706 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22707
b98664d3 22708 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22709 " at %s [in module %s]"),
22710 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22711 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22712 return build_error_marker_type (cu, die);
22713 }
348e048f
DE
22714}
22715
e5fe5e75 22716/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22717
22718static void
e5fe5e75 22719load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22720{
52dc124a 22721 struct signatured_type *sig_type;
348e048f 22722
f4dc4d17
DE
22723 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22724 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22725
6721b2ec
DE
22726 /* We have the per_cu, but we need the signatured_type.
22727 Fortunately this is an easy translation. */
22728 gdb_assert (per_cu->is_debug_types);
22729 sig_type = (struct signatured_type *) per_cu;
348e048f 22730
6721b2ec 22731 gdb_assert (per_cu->cu == NULL);
348e048f 22732
52dc124a 22733 read_signatured_type (sig_type);
348e048f 22734
6721b2ec 22735 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22736}
22737
3019eac3
DE
22738/* Read in a signatured type and build its CU and DIEs.
22739 If the type is a stub for the real type in a DWO file,
22740 read in the real type from the DWO file as well. */
dee91e82
DE
22741
22742static void
22743read_signatured_type (struct signatured_type *sig_type)
22744{
22745 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22746
3019eac3 22747 gdb_assert (per_cu->is_debug_types);
dee91e82 22748 gdb_assert (per_cu->cu == NULL);
348e048f 22749
6751ebae 22750 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22751
22752 if (!reader.dummy_p)
22753 {
22754 struct dwarf2_cu *cu = reader.cu;
22755 const gdb_byte *info_ptr = reader.info_ptr;
22756
22757 gdb_assert (cu->die_hash == NULL);
22758 cu->die_hash =
22759 htab_create_alloc_ex (cu->header.length / 12,
22760 die_hash,
22761 die_eq,
22762 NULL,
22763 &cu->comp_unit_obstack,
22764 hashtab_obstack_allocate,
22765 dummy_obstack_deallocate);
22766
3e225074 22767 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22768 reader.comp_unit_die->child
22769 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22770 reader.comp_unit_die);
22771 cu->dies = reader.comp_unit_die;
22772 /* comp_unit_die is not stored in die_hash, no need. */
22773
22774 /* We try not to read any attributes in this function, because
22775 not all CUs needed for references have been loaded yet, and
22776 symbol table processing isn't initialized. But we have to
22777 set the CU language, or we won't be able to build types
22778 correctly. Similarly, if we do not read the producer, we can
22779 not apply producer-specific interpretation. */
22780 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22781
22782 reader.keep ();
c0ab21c2
TT
22783 }
22784
7ee85ab1 22785 sig_type->per_cu.tu_read = 1;
c906108c
SS
22786}
22787
c906108c
SS
22788/* Decode simple location descriptions.
22789 Given a pointer to a dwarf block that defines a location, compute
22790 the location and return the value.
22791
4cecd739
DJ
22792 NOTE drow/2003-11-18: This function is called in two situations
22793 now: for the address of static or global variables (partial symbols
22794 only) and for offsets into structures which are expected to be
22795 (more or less) constant. The partial symbol case should go away,
22796 and only the constant case should remain. That will let this
22797 function complain more accurately. A few special modes are allowed
22798 without complaint for global variables (for instance, global
22799 register values and thread-local values).
c906108c
SS
22800
22801 A location description containing no operations indicates that the
4cecd739 22802 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22803 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22804 callers will only want a very basic result and this can become a
21ae7a4d
JK
22805 complaint.
22806
22807 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22808
22809static CORE_ADDR
e7c27a73 22810decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22811{
518817b3 22812 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22813 size_t i;
22814 size_t size = blk->size;
d521ce57 22815 const gdb_byte *data = blk->data;
21ae7a4d
JK
22816 CORE_ADDR stack[64];
22817 int stacki;
22818 unsigned int bytes_read, unsnd;
22819 gdb_byte op;
c906108c 22820
21ae7a4d
JK
22821 i = 0;
22822 stacki = 0;
22823 stack[stacki] = 0;
22824 stack[++stacki] = 0;
22825
22826 while (i < size)
22827 {
22828 op = data[i++];
22829 switch (op)
22830 {
22831 case DW_OP_lit0:
22832 case DW_OP_lit1:
22833 case DW_OP_lit2:
22834 case DW_OP_lit3:
22835 case DW_OP_lit4:
22836 case DW_OP_lit5:
22837 case DW_OP_lit6:
22838 case DW_OP_lit7:
22839 case DW_OP_lit8:
22840 case DW_OP_lit9:
22841 case DW_OP_lit10:
22842 case DW_OP_lit11:
22843 case DW_OP_lit12:
22844 case DW_OP_lit13:
22845 case DW_OP_lit14:
22846 case DW_OP_lit15:
22847 case DW_OP_lit16:
22848 case DW_OP_lit17:
22849 case DW_OP_lit18:
22850 case DW_OP_lit19:
22851 case DW_OP_lit20:
22852 case DW_OP_lit21:
22853 case DW_OP_lit22:
22854 case DW_OP_lit23:
22855 case DW_OP_lit24:
22856 case DW_OP_lit25:
22857 case DW_OP_lit26:
22858 case DW_OP_lit27:
22859 case DW_OP_lit28:
22860 case DW_OP_lit29:
22861 case DW_OP_lit30:
22862 case DW_OP_lit31:
22863 stack[++stacki] = op - DW_OP_lit0;
22864 break;
f1bea926 22865
21ae7a4d
JK
22866 case DW_OP_reg0:
22867 case DW_OP_reg1:
22868 case DW_OP_reg2:
22869 case DW_OP_reg3:
22870 case DW_OP_reg4:
22871 case DW_OP_reg5:
22872 case DW_OP_reg6:
22873 case DW_OP_reg7:
22874 case DW_OP_reg8:
22875 case DW_OP_reg9:
22876 case DW_OP_reg10:
22877 case DW_OP_reg11:
22878 case DW_OP_reg12:
22879 case DW_OP_reg13:
22880 case DW_OP_reg14:
22881 case DW_OP_reg15:
22882 case DW_OP_reg16:
22883 case DW_OP_reg17:
22884 case DW_OP_reg18:
22885 case DW_OP_reg19:
22886 case DW_OP_reg20:
22887 case DW_OP_reg21:
22888 case DW_OP_reg22:
22889 case DW_OP_reg23:
22890 case DW_OP_reg24:
22891 case DW_OP_reg25:
22892 case DW_OP_reg26:
22893 case DW_OP_reg27:
22894 case DW_OP_reg28:
22895 case DW_OP_reg29:
22896 case DW_OP_reg30:
22897 case DW_OP_reg31:
22898 stack[++stacki] = op - DW_OP_reg0;
22899 if (i < size)
22900 dwarf2_complex_location_expr_complaint ();
22901 break;
c906108c 22902
21ae7a4d
JK
22903 case DW_OP_regx:
22904 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22905 i += bytes_read;
22906 stack[++stacki] = unsnd;
22907 if (i < size)
22908 dwarf2_complex_location_expr_complaint ();
22909 break;
c906108c 22910
21ae7a4d 22911 case DW_OP_addr:
c8a7a66f
TT
22912 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22913 &bytes_read);
21ae7a4d
JK
22914 i += bytes_read;
22915 break;
d53d4ac5 22916
21ae7a4d
JK
22917 case DW_OP_const1u:
22918 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22919 i += 1;
22920 break;
22921
22922 case DW_OP_const1s:
22923 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22924 i += 1;
22925 break;
22926
22927 case DW_OP_const2u:
22928 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22929 i += 2;
22930 break;
22931
22932 case DW_OP_const2s:
22933 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22934 i += 2;
22935 break;
d53d4ac5 22936
21ae7a4d
JK
22937 case DW_OP_const4u:
22938 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22939 i += 4;
22940 break;
22941
22942 case DW_OP_const4s:
22943 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22944 i += 4;
22945 break;
22946
585861ea
JK
22947 case DW_OP_const8u:
22948 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22949 i += 8;
22950 break;
22951
21ae7a4d
JK
22952 case DW_OP_constu:
22953 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22954 &bytes_read);
22955 i += bytes_read;
22956 break;
22957
22958 case DW_OP_consts:
22959 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22960 i += bytes_read;
22961 break;
22962
22963 case DW_OP_dup:
22964 stack[stacki + 1] = stack[stacki];
22965 stacki++;
22966 break;
22967
22968 case DW_OP_plus:
22969 stack[stacki - 1] += stack[stacki];
22970 stacki--;
22971 break;
22972
22973 case DW_OP_plus_uconst:
22974 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22975 &bytes_read);
22976 i += bytes_read;
22977 break;
22978
22979 case DW_OP_minus:
22980 stack[stacki - 1] -= stack[stacki];
22981 stacki--;
22982 break;
22983
22984 case DW_OP_deref:
22985 /* If we're not the last op, then we definitely can't encode
22986 this using GDB's address_class enum. This is valid for partial
22987 global symbols, although the variable's address will be bogus
22988 in the psymtab. */
22989 if (i < size)
22990 dwarf2_complex_location_expr_complaint ();
22991 break;
22992
22993 case DW_OP_GNU_push_tls_address:
4aa4e28b 22994 case DW_OP_form_tls_address:
21ae7a4d
JK
22995 /* The top of the stack has the offset from the beginning
22996 of the thread control block at which the variable is located. */
22997 /* Nothing should follow this operator, so the top of stack would
22998 be returned. */
22999 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23000 address will be bogus in the psymtab. Make it always at least
23001 non-zero to not look as a variable garbage collected by linker
23002 which have DW_OP_addr 0. */
21ae7a4d
JK
23003 if (i < size)
23004 dwarf2_complex_location_expr_complaint ();
585861ea 23005 stack[stacki]++;
21ae7a4d
JK
23006 break;
23007
23008 case DW_OP_GNU_uninit:
23009 break;
23010
336d760d 23011 case DW_OP_addrx:
3019eac3 23012 case DW_OP_GNU_addr_index:
49f6c839 23013 case DW_OP_GNU_const_index:
3019eac3
DE
23014 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23015 &bytes_read);
23016 i += bytes_read;
23017 break;
23018
21ae7a4d
JK
23019 default:
23020 {
f39c6ffd 23021 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23022
23023 if (name)
b98664d3 23024 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23025 name);
23026 else
b98664d3 23027 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23028 op);
23029 }
23030
23031 return (stack[stacki]);
d53d4ac5 23032 }
3c6e0cb3 23033
21ae7a4d
JK
23034 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23035 outside of the allocated space. Also enforce minimum>0. */
23036 if (stacki >= ARRAY_SIZE (stack) - 1)
23037 {
b98664d3 23038 complaint (_("location description stack overflow"));
21ae7a4d
JK
23039 return 0;
23040 }
23041
23042 if (stacki <= 0)
23043 {
b98664d3 23044 complaint (_("location description stack underflow"));
21ae7a4d
JK
23045 return 0;
23046 }
23047 }
23048 return (stack[stacki]);
c906108c
SS
23049}
23050
23051/* memory allocation interface */
23052
c906108c 23053static struct dwarf_block *
7b5a2f43 23054dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23055{
8d749320 23056 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23057}
23058
c906108c 23059static struct die_info *
b60c80d6 23060dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23061{
23062 struct die_info *die;
b60c80d6
DJ
23063 size_t size = sizeof (struct die_info);
23064
23065 if (num_attrs > 1)
23066 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23067
b60c80d6 23068 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23069 memset (die, 0, sizeof (struct die_info));
23070 return (die);
23071}
2e276125
JB
23072
23073\f
23074/* Macro support. */
23075
2e276125 23076static struct macro_source_file *
804d2729
TT
23077macro_start_file (struct dwarf2_cu *cu,
23078 int file, int line,
2e276125 23079 struct macro_source_file *current_file,
43f3e411 23080 struct line_header *lh)
2e276125 23081{
233d95b5 23082 /* File name relative to the compilation directory of this source file. */
03075812 23083 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
2e276125 23084
2e276125 23085 if (! current_file)
abc9d0dc 23086 {
fc474241
DE
23087 /* Note: We don't create a macro table for this compilation unit
23088 at all until we actually get a filename. */
c24bdb02 23089 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 23090
abc9d0dc
TT
23091 /* If we have no current file, then this must be the start_file
23092 directive for the compilation unit's main source file. */
03075812 23093 current_file = macro_set_main (macro_table, file_name.get ());
fc474241 23094 macro_define_special (macro_table);
abc9d0dc 23095 }
2e276125 23096 else
03075812 23097 current_file = macro_include (current_file, line, file_name.get ());
6e70227d 23098
2e276125
JB
23099 return current_file;
23100}
23101
2e276125
JB
23102static const char *
23103consume_improper_spaces (const char *p, const char *body)
23104{
23105 if (*p == ' ')
23106 {
b98664d3 23107 complaint (_("macro definition contains spaces "
3e43a32a 23108 "in formal argument list:\n`%s'"),
4d3c2250 23109 body);
2e276125
JB
23110
23111 while (*p == ' ')
23112 p++;
23113 }
23114
23115 return p;
23116}
23117
23118
23119static void
23120parse_macro_definition (struct macro_source_file *file, int line,
23121 const char *body)
23122{
23123 const char *p;
23124
23125 /* The body string takes one of two forms. For object-like macro
23126 definitions, it should be:
23127
23128 <macro name> " " <definition>
23129
23130 For function-like macro definitions, it should be:
23131
23132 <macro name> "() " <definition>
23133 or
23134 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23135
23136 Spaces may appear only where explicitly indicated, and in the
23137 <definition>.
23138
23139 The Dwarf 2 spec says that an object-like macro's name is always
23140 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23141 the space when the macro's definition is the empty string.
2e276125
JB
23142
23143 The Dwarf 2 spec says that there should be no spaces between the
23144 formal arguments in a function-like macro's formal argument list,
23145 but versions of GCC around March 2002 include spaces after the
23146 commas. */
23147
23148
23149 /* Find the extent of the macro name. The macro name is terminated
23150 by either a space or null character (for an object-like macro) or
23151 an opening paren (for a function-like macro). */
23152 for (p = body; *p; p++)
23153 if (*p == ' ' || *p == '(')
23154 break;
23155
23156 if (*p == ' ' || *p == '\0')
23157 {
23158 /* It's an object-like macro. */
23159 int name_len = p - body;
456e800a 23160 std::string name (body, name_len);
2e276125
JB
23161 const char *replacement;
23162
23163 if (*p == ' ')
23164 replacement = body + name_len + 1;
23165 else
23166 {
4d3c2250 23167 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23168 replacement = body + name_len;
23169 }
6e70227d 23170
456e800a 23171 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23172 }
23173 else if (*p == '(')
23174 {
23175 /* It's a function-like macro. */
456e800a 23176 std::string name (body, p - body);
2e276125
JB
23177 int argc = 0;
23178 int argv_size = 1;
8d749320 23179 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23180
23181 p++;
23182
23183 p = consume_improper_spaces (p, body);
23184
23185 /* Parse the formal argument list. */
23186 while (*p && *p != ')')
23187 {
23188 /* Find the extent of the current argument name. */
23189 const char *arg_start = p;
23190
23191 while (*p && *p != ',' && *p != ')' && *p != ' ')
23192 p++;
23193
23194 if (! *p || p == arg_start)
4d3c2250 23195 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23196 else
23197 {
23198 /* Make sure argv has room for the new argument. */
23199 if (argc >= argv_size)
23200 {
23201 argv_size *= 2;
224c3ddb 23202 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23203 }
23204
3f8a7804 23205 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23206 }
23207
23208 p = consume_improper_spaces (p, body);
23209
23210 /* Consume the comma, if present. */
23211 if (*p == ',')
23212 {
23213 p++;
23214
23215 p = consume_improper_spaces (p, body);
23216 }
23217 }
23218
23219 if (*p == ')')
23220 {
23221 p++;
23222
23223 if (*p == ' ')
23224 /* Perfectly formed definition, no complaints. */
456e800a 23225 macro_define_function (file, line, name.c_str (),
6e70227d 23226 argc, (const char **) argv,
2e276125
JB
23227 p + 1);
23228 else if (*p == '\0')
23229 {
23230 /* Complain, but do define it. */
4d3c2250 23231 dwarf2_macro_malformed_definition_complaint (body);
456e800a 23232 macro_define_function (file, line, name.c_str (),
6e70227d 23233 argc, (const char **) argv,
2e276125
JB
23234 p);
23235 }
23236 else
23237 /* Just complain. */
4d3c2250 23238 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23239 }
23240 else
23241 /* Just complain. */
4d3c2250 23242 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23243
2e276125
JB
23244 {
23245 int i;
23246
23247 for (i = 0; i < argc; i++)
23248 xfree (argv[i]);
23249 }
23250 xfree (argv);
23251 }
23252 else
4d3c2250 23253 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23254}
23255
cf2c3c16
TT
23256/* Skip some bytes from BYTES according to the form given in FORM.
23257 Returns the new pointer. */
2e276125 23258
d521ce57
TT
23259static const gdb_byte *
23260skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23261 enum dwarf_form form,
23262 unsigned int offset_size,
23263 struct dwarf2_section_info *section)
2e276125 23264{
cf2c3c16 23265 unsigned int bytes_read;
2e276125 23266
cf2c3c16 23267 switch (form)
2e276125 23268 {
cf2c3c16
TT
23269 case DW_FORM_data1:
23270 case DW_FORM_flag:
23271 ++bytes;
23272 break;
23273
23274 case DW_FORM_data2:
23275 bytes += 2;
23276 break;
23277
23278 case DW_FORM_data4:
23279 bytes += 4;
23280 break;
23281
23282 case DW_FORM_data8:
23283 bytes += 8;
23284 break;
23285
0224619f
JK
23286 case DW_FORM_data16:
23287 bytes += 16;
23288 break;
23289
cf2c3c16
TT
23290 case DW_FORM_string:
23291 read_direct_string (abfd, bytes, &bytes_read);
23292 bytes += bytes_read;
23293 break;
23294
23295 case DW_FORM_sec_offset:
23296 case DW_FORM_strp:
36586728 23297 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23298 bytes += offset_size;
23299 break;
23300
23301 case DW_FORM_block:
23302 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23303 bytes += bytes_read;
23304 break;
23305
23306 case DW_FORM_block1:
23307 bytes += 1 + read_1_byte (abfd, bytes);
23308 break;
23309 case DW_FORM_block2:
23310 bytes += 2 + read_2_bytes (abfd, bytes);
23311 break;
23312 case DW_FORM_block4:
23313 bytes += 4 + read_4_bytes (abfd, bytes);
23314 break;
23315
336d760d 23316 case DW_FORM_addrx:
cf2c3c16 23317 case DW_FORM_sdata:
cf532bd1 23318 case DW_FORM_strx:
cf2c3c16 23319 case DW_FORM_udata:
3019eac3
DE
23320 case DW_FORM_GNU_addr_index:
23321 case DW_FORM_GNU_str_index:
d521ce57 23322 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23323 if (bytes == NULL)
23324 {
23325 dwarf2_section_buffer_overflow_complaint (section);
23326 return NULL;
23327 }
cf2c3c16
TT
23328 break;
23329
663c44ac
JK
23330 case DW_FORM_implicit_const:
23331 break;
23332
cf2c3c16
TT
23333 default:
23334 {
b98664d3 23335 complaint (_("invalid form 0x%x in `%s'"),
96b79293 23336 form, section->get_name ());
cf2c3c16
TT
23337 return NULL;
23338 }
2e276125
JB
23339 }
23340
cf2c3c16
TT
23341 return bytes;
23342}
757a13d0 23343
cf2c3c16
TT
23344/* A helper for dwarf_decode_macros that handles skipping an unknown
23345 opcode. Returns an updated pointer to the macro data buffer; or,
23346 on error, issues a complaint and returns NULL. */
757a13d0 23347
d521ce57 23348static const gdb_byte *
cf2c3c16 23349skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23350 const gdb_byte **opcode_definitions,
23351 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23352 bfd *abfd,
23353 unsigned int offset_size,
23354 struct dwarf2_section_info *section)
23355{
23356 unsigned int bytes_read, i;
23357 unsigned long arg;
d521ce57 23358 const gdb_byte *defn;
2e276125 23359
cf2c3c16 23360 if (opcode_definitions[opcode] == NULL)
2e276125 23361 {
b98664d3 23362 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23363 opcode);
23364 return NULL;
23365 }
2e276125 23366
cf2c3c16
TT
23367 defn = opcode_definitions[opcode];
23368 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23369 defn += bytes_read;
2e276125 23370
cf2c3c16
TT
23371 for (i = 0; i < arg; ++i)
23372 {
aead7601
SM
23373 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23374 (enum dwarf_form) defn[i], offset_size,
f664829e 23375 section);
cf2c3c16
TT
23376 if (mac_ptr == NULL)
23377 {
23378 /* skip_form_bytes already issued the complaint. */
23379 return NULL;
23380 }
23381 }
757a13d0 23382
cf2c3c16
TT
23383 return mac_ptr;
23384}
757a13d0 23385
cf2c3c16
TT
23386/* A helper function which parses the header of a macro section.
23387 If the macro section is the extended (for now called "GNU") type,
23388 then this updates *OFFSET_SIZE. Returns a pointer to just after
23389 the header, or issues a complaint and returns NULL on error. */
757a13d0 23390
d521ce57
TT
23391static const gdb_byte *
23392dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 23393 bfd *abfd,
d521ce57 23394 const gdb_byte *mac_ptr,
cf2c3c16
TT
23395 unsigned int *offset_size,
23396 int section_is_gnu)
23397{
23398 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 23399
cf2c3c16
TT
23400 if (section_is_gnu)
23401 {
23402 unsigned int version, flags;
757a13d0 23403
cf2c3c16 23404 version = read_2_bytes (abfd, mac_ptr);
0af92d60 23405 if (version != 4 && version != 5)
cf2c3c16 23406 {
b98664d3 23407 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
23408 version);
23409 return NULL;
23410 }
23411 mac_ptr += 2;
757a13d0 23412
cf2c3c16
TT
23413 flags = read_1_byte (abfd, mac_ptr);
23414 ++mac_ptr;
23415 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 23416
cf2c3c16
TT
23417 if ((flags & 2) != 0)
23418 /* We don't need the line table offset. */
23419 mac_ptr += *offset_size;
757a13d0 23420
cf2c3c16
TT
23421 /* Vendor opcode descriptions. */
23422 if ((flags & 4) != 0)
23423 {
23424 unsigned int i, count;
757a13d0 23425
cf2c3c16
TT
23426 count = read_1_byte (abfd, mac_ptr);
23427 ++mac_ptr;
23428 for (i = 0; i < count; ++i)
23429 {
23430 unsigned int opcode, bytes_read;
23431 unsigned long arg;
23432
23433 opcode = read_1_byte (abfd, mac_ptr);
23434 ++mac_ptr;
23435 opcode_definitions[opcode] = mac_ptr;
23436 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23437 mac_ptr += bytes_read;
23438 mac_ptr += arg;
23439 }
757a13d0 23440 }
cf2c3c16 23441 }
757a13d0 23442
cf2c3c16
TT
23443 return mac_ptr;
23444}
757a13d0 23445
cf2c3c16 23446/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 23447 including DW_MACRO_import. */
cf2c3c16
TT
23448
23449static void
804d2729 23450dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 23451 bfd *abfd,
d521ce57 23452 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 23453 struct macro_source_file *current_file,
43f3e411 23454 struct line_header *lh,
cf2c3c16 23455 struct dwarf2_section_info *section,
36586728 23456 int section_is_gnu, int section_is_dwz,
cf2c3c16 23457 unsigned int offset_size,
8fc3fc34 23458 htab_t include_hash)
cf2c3c16 23459{
804d2729
TT
23460 struct dwarf2_per_objfile *dwarf2_per_objfile
23461 = cu->per_cu->dwarf2_per_objfile;
4d663531 23462 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
23463 enum dwarf_macro_record_type macinfo_type;
23464 int at_commandline;
d521ce57 23465 const gdb_byte *opcode_definitions[256];
757a13d0 23466
cf2c3c16
TT
23467 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23468 &offset_size, section_is_gnu);
23469 if (mac_ptr == NULL)
23470 {
23471 /* We already issued a complaint. */
23472 return;
23473 }
757a13d0
JK
23474
23475 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23476 GDB is still reading the definitions from command line. First
23477 DW_MACINFO_start_file will need to be ignored as it was already executed
23478 to create CURRENT_FILE for the main source holding also the command line
23479 definitions. On first met DW_MACINFO_start_file this flag is reset to
23480 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23481
23482 at_commandline = 1;
23483
23484 do
23485 {
23486 /* Do we at least have room for a macinfo type byte? */
23487 if (mac_ptr >= mac_end)
23488 {
f664829e 23489 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
23490 break;
23491 }
23492
aead7601 23493 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
23494 mac_ptr++;
23495
cf2c3c16
TT
23496 /* Note that we rely on the fact that the corresponding GNU and
23497 DWARF constants are the same. */
132448f8
SM
23498 DIAGNOSTIC_PUSH
23499 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
23500 switch (macinfo_type)
23501 {
23502 /* A zero macinfo type indicates the end of the macro
23503 information. */
23504 case 0:
23505 break;
2e276125 23506
0af92d60
JK
23507 case DW_MACRO_define:
23508 case DW_MACRO_undef:
23509 case DW_MACRO_define_strp:
23510 case DW_MACRO_undef_strp:
23511 case DW_MACRO_define_sup:
23512 case DW_MACRO_undef_sup:
2e276125 23513 {
891d2f0b 23514 unsigned int bytes_read;
2e276125 23515 int line;
d521ce57 23516 const char *body;
cf2c3c16 23517 int is_define;
2e276125 23518
cf2c3c16
TT
23519 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23520 mac_ptr += bytes_read;
23521
0af92d60
JK
23522 if (macinfo_type == DW_MACRO_define
23523 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
23524 {
23525 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23526 mac_ptr += bytes_read;
23527 }
23528 else
23529 {
23530 LONGEST str_offset;
23531
24aa364d 23532 str_offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16 23533 mac_ptr += offset_size;
2e276125 23534
0af92d60
JK
23535 if (macinfo_type == DW_MACRO_define_sup
23536 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 23537 || section_is_dwz)
36586728 23538 {
ed2dc618
SM
23539 struct dwz_file *dwz
23540 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 23541
ed2dc618
SM
23542 body = read_indirect_string_from_dwz (objfile,
23543 dwz, str_offset);
36586728
TT
23544 }
23545 else
ed2dc618
SM
23546 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23547 abfd, str_offset);
cf2c3c16
TT
23548 }
23549
0af92d60
JK
23550 is_define = (macinfo_type == DW_MACRO_define
23551 || macinfo_type == DW_MACRO_define_strp
23552 || macinfo_type == DW_MACRO_define_sup);
2e276125 23553 if (! current_file)
757a13d0
JK
23554 {
23555 /* DWARF violation as no main source is present. */
b98664d3 23556 complaint (_("debug info with no main source gives macro %s "
757a13d0 23557 "on line %d: %s"),
cf2c3c16
TT
23558 is_define ? _("definition") : _("undefinition"),
23559 line, body);
757a13d0
JK
23560 break;
23561 }
3e43a32a
MS
23562 if ((line == 0 && !at_commandline)
23563 || (line != 0 && at_commandline))
b98664d3 23564 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 23565 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 23566 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
23567 line == 0 ? _("zero") : _("non-zero"), line, body);
23568
955b06fa 23569 if (body == NULL)
7bede828 23570 {
955b06fa
SDJ
23571 /* Fedora's rpm-build's "debugedit" binary
23572 corrupted .debug_macro sections.
23573
23574 For more info, see
23575 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23576 complaint (_("debug info gives %s invalid macro %s "
23577 "without body (corrupted?) at line %d "
23578 "on file %s"),
23579 at_commandline ? _("command-line") : _("in-file"),
23580 is_define ? _("definition") : _("undefinition"),
23581 line, current_file->filename);
7bede828 23582 }
955b06fa
SDJ
23583 else if (is_define)
23584 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
23585 else
23586 {
0af92d60
JK
23587 gdb_assert (macinfo_type == DW_MACRO_undef
23588 || macinfo_type == DW_MACRO_undef_strp
23589 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
23590 macro_undef (current_file, line, body);
23591 }
2e276125
JB
23592 }
23593 break;
23594
0af92d60 23595 case DW_MACRO_start_file:
2e276125 23596 {
891d2f0b 23597 unsigned int bytes_read;
2e276125
JB
23598 int line, file;
23599
23600 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23601 mac_ptr += bytes_read;
23602 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23603 mac_ptr += bytes_read;
23604
3e43a32a
MS
23605 if ((line == 0 && !at_commandline)
23606 || (line != 0 && at_commandline))
b98664d3 23607 complaint (_("debug info gives source %d included "
757a13d0
JK
23608 "from %s at %s line %d"),
23609 file, at_commandline ? _("command-line") : _("file"),
23610 line == 0 ? _("zero") : _("non-zero"), line);
23611
23612 if (at_commandline)
23613 {
0af92d60 23614 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23615 pass one. */
757a13d0
JK
23616 at_commandline = 0;
23617 }
23618 else
804d2729
TT
23619 current_file = macro_start_file (cu, file, line, current_file,
23620 lh);
2e276125
JB
23621 }
23622 break;
23623
0af92d60 23624 case DW_MACRO_end_file:
2e276125 23625 if (! current_file)
b98664d3 23626 complaint (_("macro debug info has an unmatched "
3e43a32a 23627 "`close_file' directive"));
2e276125
JB
23628 else
23629 {
23630 current_file = current_file->included_by;
23631 if (! current_file)
23632 {
cf2c3c16 23633 enum dwarf_macro_record_type next_type;
2e276125
JB
23634
23635 /* GCC circa March 2002 doesn't produce the zero
23636 type byte marking the end of the compilation
23637 unit. Complain if it's not there, but exit no
23638 matter what. */
23639
23640 /* Do we at least have room for a macinfo type byte? */
23641 if (mac_ptr >= mac_end)
23642 {
f664829e 23643 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23644 return;
23645 }
23646
23647 /* We don't increment mac_ptr here, so this is just
23648 a look-ahead. */
aead7601
SM
23649 next_type
23650 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23651 mac_ptr);
2e276125 23652 if (next_type != 0)
b98664d3 23653 complaint (_("no terminating 0-type entry for "
3e43a32a 23654 "macros in `.debug_macinfo' section"));
2e276125
JB
23655
23656 return;
23657 }
23658 }
23659 break;
23660
0af92d60
JK
23661 case DW_MACRO_import:
23662 case DW_MACRO_import_sup:
cf2c3c16
TT
23663 {
23664 LONGEST offset;
8fc3fc34 23665 void **slot;
a036ba48
TT
23666 bfd *include_bfd = abfd;
23667 struct dwarf2_section_info *include_section = section;
d521ce57 23668 const gdb_byte *include_mac_end = mac_end;
a036ba48 23669 int is_dwz = section_is_dwz;
d521ce57 23670 const gdb_byte *new_mac_ptr;
cf2c3c16 23671
24aa364d 23672 offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16
TT
23673 mac_ptr += offset_size;
23674
0af92d60 23675 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 23676 {
ed2dc618 23677 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 23678
96b79293 23679 dwz->macro.read (objfile);
a036ba48 23680
a036ba48 23681 include_section = &dwz->macro;
96b79293 23682 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
23683 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23684 is_dwz = 1;
23685 }
23686
23687 new_mac_ptr = include_section->buffer + offset;
23688 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23689
8fc3fc34
TT
23690 if (*slot != NULL)
23691 {
23692 /* This has actually happened; see
23693 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 23694 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
23695 ".debug_macro section"));
23696 }
23697 else
23698 {
d521ce57 23699 *slot = (void *) new_mac_ptr;
36586728 23700
804d2729 23701 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 23702 include_mac_end, current_file, lh,
36586728 23703 section, section_is_gnu, is_dwz,
4d663531 23704 offset_size, include_hash);
8fc3fc34 23705
d521ce57 23706 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23707 }
cf2c3c16
TT
23708 }
23709 break;
23710
2e276125 23711 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23712 if (!section_is_gnu)
23713 {
23714 unsigned int bytes_read;
2e276125 23715
ac298888
TT
23716 /* This reads the constant, but since we don't recognize
23717 any vendor extensions, we ignore it. */
23718 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23719 mac_ptr += bytes_read;
23720 read_direct_string (abfd, mac_ptr, &bytes_read);
23721 mac_ptr += bytes_read;
2e276125 23722
cf2c3c16
TT
23723 /* We don't recognize any vendor extensions. */
23724 break;
23725 }
23726 /* FALLTHROUGH */
23727
23728 default:
23729 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23730 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23731 section);
23732 if (mac_ptr == NULL)
23733 return;
23734 break;
2e276125 23735 }
132448f8 23736 DIAGNOSTIC_POP
757a13d0 23737 } while (macinfo_type != 0);
2e276125 23738}
8e19ed76 23739
cf2c3c16 23740static void
09262596 23741dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 23742 int section_is_gnu)
cf2c3c16 23743{
518817b3
SM
23744 struct dwarf2_per_objfile *dwarf2_per_objfile
23745 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23746 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
23747 struct line_header *lh = cu->line_header;
23748 bfd *abfd;
d521ce57 23749 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23750 struct macro_source_file *current_file = 0;
23751 enum dwarf_macro_record_type macinfo_type;
23752 unsigned int offset_size = cu->header.offset_size;
d521ce57 23753 const gdb_byte *opcode_definitions[256];
8fc3fc34 23754 void **slot;
09262596
DE
23755 struct dwarf2_section_info *section;
23756 const char *section_name;
23757
23758 if (cu->dwo_unit != NULL)
23759 {
23760 if (section_is_gnu)
23761 {
23762 section = &cu->dwo_unit->dwo_file->sections.macro;
23763 section_name = ".debug_macro.dwo";
23764 }
23765 else
23766 {
23767 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23768 section_name = ".debug_macinfo.dwo";
23769 }
23770 }
23771 else
23772 {
23773 if (section_is_gnu)
23774 {
23775 section = &dwarf2_per_objfile->macro;
23776 section_name = ".debug_macro";
23777 }
23778 else
23779 {
23780 section = &dwarf2_per_objfile->macinfo;
23781 section_name = ".debug_macinfo";
23782 }
23783 }
cf2c3c16 23784
96b79293 23785 section->read (objfile);
cf2c3c16
TT
23786 if (section->buffer == NULL)
23787 {
b98664d3 23788 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
23789 return;
23790 }
96b79293 23791 abfd = section->get_bfd_owner ();
cf2c3c16
TT
23792
23793 /* First pass: Find the name of the base filename.
23794 This filename is needed in order to process all macros whose definition
23795 (or undefinition) comes from the command line. These macros are defined
23796 before the first DW_MACINFO_start_file entry, and yet still need to be
23797 associated to the base file.
23798
23799 To determine the base file name, we scan the macro definitions until we
23800 reach the first DW_MACINFO_start_file entry. We then initialize
23801 CURRENT_FILE accordingly so that any macro definition found before the
23802 first DW_MACINFO_start_file can still be associated to the base file. */
23803
23804 mac_ptr = section->buffer + offset;
23805 mac_end = section->buffer + section->size;
23806
23807 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23808 &offset_size, section_is_gnu);
23809 if (mac_ptr == NULL)
23810 {
23811 /* We already issued a complaint. */
23812 return;
23813 }
23814
23815 do
23816 {
23817 /* Do we at least have room for a macinfo type byte? */
23818 if (mac_ptr >= mac_end)
23819 {
23820 /* Complaint is printed during the second pass as GDB will probably
23821 stop the first pass earlier upon finding
23822 DW_MACINFO_start_file. */
23823 break;
23824 }
23825
aead7601 23826 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23827 mac_ptr++;
23828
23829 /* Note that we rely on the fact that the corresponding GNU and
23830 DWARF constants are the same. */
132448f8
SM
23831 DIAGNOSTIC_PUSH
23832 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
23833 switch (macinfo_type)
23834 {
23835 /* A zero macinfo type indicates the end of the macro
23836 information. */
23837 case 0:
23838 break;
23839
0af92d60
JK
23840 case DW_MACRO_define:
23841 case DW_MACRO_undef:
cf2c3c16
TT
23842 /* Only skip the data by MAC_PTR. */
23843 {
23844 unsigned int bytes_read;
23845
23846 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23847 mac_ptr += bytes_read;
23848 read_direct_string (abfd, mac_ptr, &bytes_read);
23849 mac_ptr += bytes_read;
23850 }
23851 break;
23852
0af92d60 23853 case DW_MACRO_start_file:
cf2c3c16
TT
23854 {
23855 unsigned int bytes_read;
23856 int line, file;
23857
23858 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23859 mac_ptr += bytes_read;
23860 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23861 mac_ptr += bytes_read;
23862
804d2729 23863 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
23864 }
23865 break;
23866
0af92d60 23867 case DW_MACRO_end_file:
cf2c3c16
TT
23868 /* No data to skip by MAC_PTR. */
23869 break;
23870
0af92d60
JK
23871 case DW_MACRO_define_strp:
23872 case DW_MACRO_undef_strp:
23873 case DW_MACRO_define_sup:
23874 case DW_MACRO_undef_sup:
cf2c3c16
TT
23875 {
23876 unsigned int bytes_read;
23877
23878 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23879 mac_ptr += bytes_read;
23880 mac_ptr += offset_size;
23881 }
23882 break;
23883
0af92d60
JK
23884 case DW_MACRO_import:
23885 case DW_MACRO_import_sup:
cf2c3c16 23886 /* Note that, according to the spec, a transparent include
0af92d60 23887 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23888 skip this opcode. */
23889 mac_ptr += offset_size;
23890 break;
23891
23892 case DW_MACINFO_vendor_ext:
23893 /* Only skip the data by MAC_PTR. */
23894 if (!section_is_gnu)
23895 {
23896 unsigned int bytes_read;
23897
23898 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23899 mac_ptr += bytes_read;
23900 read_direct_string (abfd, mac_ptr, &bytes_read);
23901 mac_ptr += bytes_read;
23902 }
23903 /* FALLTHROUGH */
23904
23905 default:
23906 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23907 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23908 section);
23909 if (mac_ptr == NULL)
23910 return;
23911 break;
23912 }
132448f8 23913 DIAGNOSTIC_POP
cf2c3c16
TT
23914 } while (macinfo_type != 0 && current_file == NULL);
23915
23916 /* Second pass: Process all entries.
23917
23918 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23919 command-line macro definitions/undefinitions. This flag is unset when we
23920 reach the first DW_MACINFO_start_file entry. */
23921
fc4007c9
TT
23922 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23923 htab_eq_pointer,
23924 NULL, xcalloc, xfree));
8fc3fc34 23925 mac_ptr = section->buffer + offset;
fc4007c9 23926 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23927 *slot = (void *) mac_ptr;
804d2729 23928 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 23929 current_file, lh, section,
fc4007c9
TT
23930 section_is_gnu, 0, offset_size,
23931 include_hash.get ());
cf2c3c16
TT
23932}
23933
3019eac3
DE
23934/* Return the .debug_loc section to use for CU.
23935 For DWO files use .debug_loc.dwo. */
23936
23937static struct dwarf2_section_info *
23938cu_debug_loc_section (struct dwarf2_cu *cu)
23939{
518817b3
SM
23940 struct dwarf2_per_objfile *dwarf2_per_objfile
23941 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23942
3019eac3 23943 if (cu->dwo_unit)
43988095
JK
23944 {
23945 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23946
43988095
JK
23947 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23948 }
23949 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23950 : &dwarf2_per_objfile->loc);
3019eac3
DE
23951}
23952
8cf6f0b1
TT
23953/* A helper function that fills in a dwarf2_loclist_baton. */
23954
23955static void
23956fill_in_loclist_baton (struct dwarf2_cu *cu,
23957 struct dwarf2_loclist_baton *baton,
ff39bb5e 23958 const struct attribute *attr)
8cf6f0b1 23959{
518817b3
SM
23960 struct dwarf2_per_objfile *dwarf2_per_objfile
23961 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23962 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23963
96b79293 23964 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23965
23966 baton->per_cu = cu->per_cu;
23967 gdb_assert (baton->per_cu);
23968 /* We don't know how long the location list is, but make sure we
23969 don't run off the edge of the section. */
3019eac3
DE
23970 baton->size = section->size - DW_UNSND (attr);
23971 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23972 baton->base_address = cu->base_address;
f664829e 23973 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23974}
23975
4c2df51b 23976static void
ff39bb5e 23977dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23978 struct dwarf2_cu *cu, int is_block)
4c2df51b 23979{
518817b3
SM
23980 struct dwarf2_per_objfile *dwarf2_per_objfile
23981 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23982 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23983 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23984
cd6c91b4 23985 if (attr->form_is_section_offset ()
3019eac3 23986 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23987 the section. If so, fall through to the complaint in the
23988 other branch. */
2c7d5afc 23989 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23990 {
0d53c4c4 23991 struct dwarf2_loclist_baton *baton;
4c2df51b 23992
8d749320 23993 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23994
8cf6f0b1 23995 fill_in_loclist_baton (cu, baton, attr);
be391dca 23996
d00adf39 23997 if (cu->base_known == 0)
b98664d3 23998 complaint (_("Location list used without "
3e43a32a 23999 "specifying the CU base address."));
4c2df51b 24000
f1e6e072
TT
24001 SYMBOL_ACLASS_INDEX (sym) = (is_block
24002 ? dwarf2_loclist_block_index
24003 : dwarf2_loclist_index);
0d53c4c4
DJ
24004 SYMBOL_LOCATION_BATON (sym) = baton;
24005 }
24006 else
24007 {
24008 struct dwarf2_locexpr_baton *baton;
24009
8d749320 24010 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24011 baton->per_cu = cu->per_cu;
24012 gdb_assert (baton->per_cu);
0d53c4c4 24013
4fc6c0d5 24014 if (attr->form_is_block ())
0d53c4c4
DJ
24015 {
24016 /* Note that we're just copying the block's data pointer
24017 here, not the actual data. We're still pointing into the
6502dd73
DJ
24018 info_buffer for SYM's objfile; right now we never release
24019 that buffer, but when we do clean up properly this may
24020 need to change. */
0d53c4c4
DJ
24021 baton->size = DW_BLOCK (attr)->size;
24022 baton->data = DW_BLOCK (attr)->data;
24023 }
24024 else
24025 {
24026 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24027 sym->natural_name ());
0d53c4c4 24028 baton->size = 0;
0d53c4c4 24029 }
6e70227d 24030
f1e6e072
TT
24031 SYMBOL_ACLASS_INDEX (sym) = (is_block
24032 ? dwarf2_locexpr_block_index
24033 : dwarf2_locexpr_index);
0d53c4c4
DJ
24034 SYMBOL_LOCATION_BATON (sym) = baton;
24035 }
4c2df51b 24036}
6502dd73 24037
09ba997f 24038/* See read.h. */
ae0d2f24
UW
24039
24040struct objfile *
09ba997f 24041dwarf2_per_cu_data::objfile () const
ae0d2f24 24042{
09ba997f 24043 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
24044
24045 /* Return the master objfile, so that we can report and look up the
24046 correct file containing this variable. */
24047 if (objfile->separate_debug_objfile_backlink)
24048 objfile = objfile->separate_debug_objfile_backlink;
24049
24050 return objfile;
24051}
24052
96408a79
SA
24053/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24054 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24055 CU_HEADERP first. */
24056
24057static const struct comp_unit_head *
24058per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 24059 const struct dwarf2_per_cu_data *per_cu)
96408a79 24060{
d521ce57 24061 const gdb_byte *info_ptr;
96408a79
SA
24062
24063 if (per_cu->cu)
24064 return &per_cu->cu->header;
24065
9c541725 24066 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24067
24068 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24069 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24070 rcuh_kind::COMPILE);
96408a79
SA
24071
24072 return cu_headerp;
24073}
24074
09ba997f 24075/* See read.h. */
ae0d2f24 24076
98714339 24077int
09ba997f 24078dwarf2_per_cu_data::addr_size () const
ae0d2f24 24079{
96408a79
SA
24080 struct comp_unit_head cu_header_local;
24081 const struct comp_unit_head *cu_headerp;
c471e790 24082
09ba997f 24083 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24084
24085 return cu_headerp->addr_size;
ae0d2f24
UW
24086}
24087
09ba997f 24088/* See read.h. */
9eae7c52
TT
24089
24090int
09ba997f 24091dwarf2_per_cu_data::offset_size () const
9eae7c52 24092{
96408a79
SA
24093 struct comp_unit_head cu_header_local;
24094 const struct comp_unit_head *cu_headerp;
9c6c53f7 24095
09ba997f 24096 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24097
24098 return cu_headerp->offset_size;
24099}
24100
09ba997f 24101/* See read.h. */
96408a79
SA
24102
24103int
09ba997f 24104dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
24105{
24106 struct comp_unit_head cu_header_local;
24107 const struct comp_unit_head *cu_headerp;
24108
09ba997f 24109 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24110
24111 if (cu_headerp->version == 2)
24112 return cu_headerp->addr_size;
24113 else
24114 return cu_headerp->offset_size;
181cebd4
JK
24115}
24116
09ba997f 24117/* See read.h. */
9aa1f1e3
TT
24118
24119CORE_ADDR
09ba997f 24120dwarf2_per_cu_data::text_offset () const
9aa1f1e3 24121{
09ba997f
TT
24122 struct objfile *objfile = dwarf2_per_objfile->objfile;
24123
24124 return objfile->text_section_offset ();
9aa1f1e3
TT
24125}
24126
09ba997f
TT
24127/* See read.h. */
24128
24129struct type *
24130dwarf2_per_cu_data::addr_type () const
9a49df9d 24131{
09ba997f 24132 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
24133 struct type *void_type = objfile_type (objfile)->builtin_void;
24134 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 24135 int addr_size = this->addr_size ();
9a49df9d
AB
24136
24137 if (TYPE_LENGTH (addr_type) == addr_size)
24138 return addr_type;
24139
09ba997f 24140 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
24141 return addr_type;
24142}
24143
348e048f
DE
24144/* Locate the .debug_info compilation unit from CU's objfile which contains
24145 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24146
24147static struct dwarf2_per_cu_data *
9c541725 24148dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24149 unsigned int offset_in_dwz,
ed2dc618 24150 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24151{
24152 struct dwarf2_per_cu_data *this_cu;
24153 int low, high;
24154
ae038cb0 24155 low = 0;
b76e467d 24156 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24157 while (high > low)
24158 {
36586728 24159 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24160 int mid = low + (high - low) / 2;
9a619af0 24161
36586728 24162 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 24163 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24164 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 24165 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
24166 high = mid;
24167 else
24168 low = mid + 1;
24169 }
24170 gdb_assert (low == high);
36586728 24171 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 24172 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24173 {
36586728 24174 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24175 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24176 "offset %s [in module %s]"),
24177 sect_offset_str (sect_off),
ed2dc618 24178 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24179
9c541725
PA
24180 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24181 <= sect_off);
ae038cb0
DJ
24182 return dwarf2_per_objfile->all_comp_units[low-1];
24183 }
24184 else
24185 {
b76e467d 24186 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24187 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24188 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24189 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24190 return this_cu;
24191 }
24192}
24193
23745b47 24194/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24195
fcd3b13d
SM
24196dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24197 : per_cu (per_cu_),
9068261f
AB
24198 mark (false),
24199 has_loclist (false),
24200 checked_producer (false),
24201 producer_is_gxx_lt_4_6 (false),
24202 producer_is_gcc_lt_4_3 (false),
eb77c9df 24203 producer_is_icc (false),
9068261f 24204 producer_is_icc_lt_14 (false),
c258c396 24205 producer_is_codewarrior (false),
9068261f 24206 processing_has_namespace_info (false)
93311388 24207{
fcd3b13d
SM
24208 per_cu->cu = this;
24209}
24210
24211/* Destroy a dwarf2_cu. */
24212
24213dwarf2_cu::~dwarf2_cu ()
24214{
24215 per_cu->cu = NULL;
9816fde3
JK
24216}
24217
24218/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24219
24220static void
95554aad
TT
24221prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24222 enum language pretend_language)
9816fde3
JK
24223{
24224 struct attribute *attr;
24225
24226 /* Set the language we're debugging. */
24227 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24228 if (attr != nullptr)
9816fde3
JK
24229 set_cu_language (DW_UNSND (attr), cu);
24230 else
9cded63f 24231 {
95554aad 24232 cu->language = pretend_language;
9cded63f
TT
24233 cu->language_defn = language_def (cu->language);
24234 }
dee91e82 24235
7d45c7c3 24236 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24237}
24238
ae038cb0
DJ
24239/* Increase the age counter on each cached compilation unit, and free
24240 any that are too old. */
24241
24242static void
ed2dc618 24243age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24244{
24245 struct dwarf2_per_cu_data *per_cu, **last_chain;
24246
24247 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24248 per_cu = dwarf2_per_objfile->read_in_chain;
24249 while (per_cu != NULL)
24250 {
24251 per_cu->cu->last_used ++;
b4f54984 24252 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24253 dwarf2_mark (per_cu->cu);
24254 per_cu = per_cu->cu->read_in_chain;
24255 }
24256
24257 per_cu = dwarf2_per_objfile->read_in_chain;
24258 last_chain = &dwarf2_per_objfile->read_in_chain;
24259 while (per_cu != NULL)
24260 {
24261 struct dwarf2_per_cu_data *next_cu;
24262
24263 next_cu = per_cu->cu->read_in_chain;
24264
24265 if (!per_cu->cu->mark)
24266 {
fcd3b13d 24267 delete per_cu->cu;
ae038cb0
DJ
24268 *last_chain = next_cu;
24269 }
24270 else
24271 last_chain = &per_cu->cu->read_in_chain;
24272
24273 per_cu = next_cu;
24274 }
24275}
24276
24277/* Remove a single compilation unit from the cache. */
24278
24279static void
dee91e82 24280free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24281{
24282 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24283 struct dwarf2_per_objfile *dwarf2_per_objfile
24284 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24285
24286 per_cu = dwarf2_per_objfile->read_in_chain;
24287 last_chain = &dwarf2_per_objfile->read_in_chain;
24288 while (per_cu != NULL)
24289 {
24290 struct dwarf2_per_cu_data *next_cu;
24291
24292 next_cu = per_cu->cu->read_in_chain;
24293
dee91e82 24294 if (per_cu == target_per_cu)
ae038cb0 24295 {
fcd3b13d 24296 delete per_cu->cu;
dee91e82 24297 per_cu->cu = NULL;
ae038cb0
DJ
24298 *last_chain = next_cu;
24299 break;
24300 }
24301 else
24302 last_chain = &per_cu->cu->read_in_chain;
24303
24304 per_cu = next_cu;
24305 }
24306}
24307
dee91e82
DE
24308/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24309 We store these in a hash table separate from the DIEs, and preserve them
24310 when the DIEs are flushed out of cache.
24311
24312 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24313 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24314 or the type may come from a DWO file. Furthermore, while it's more logical
24315 to use per_cu->section+offset, with Fission the section with the data is in
24316 the DWO file but we don't know that section at the point we need it.
24317 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24318 because we can enter the lookup routine, get_die_type_at_offset, from
24319 outside this file, and thus won't necessarily have PER_CU->cu.
24320 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24321
dee91e82 24322struct dwarf2_per_cu_offset_and_type
1c379e20 24323{
dee91e82 24324 const struct dwarf2_per_cu_data *per_cu;
9c541725 24325 sect_offset sect_off;
1c379e20
DJ
24326 struct type *type;
24327};
24328
dee91e82 24329/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24330
24331static hashval_t
dee91e82 24332per_cu_offset_and_type_hash (const void *item)
1c379e20 24333{
9a3c8263
SM
24334 const struct dwarf2_per_cu_offset_and_type *ofs
24335 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24336
9c541725 24337 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24338}
24339
dee91e82 24340/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24341
24342static int
dee91e82 24343per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24344{
9a3c8263
SM
24345 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24346 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24347 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24348 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24349
dee91e82 24350 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24351 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24352}
24353
24354/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24355 table if necessary. For convenience, return TYPE.
24356
24357 The DIEs reading must have careful ordering to:
85102364 24358 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24359 reading current DIE.
24360 * Not trying to dereference contents of still incompletely read in types
24361 while reading in other DIEs.
24362 * Enable referencing still incompletely read in types just by a pointer to
24363 the type without accessing its fields.
24364
24365 Therefore caller should follow these rules:
24366 * Try to fetch any prerequisite types we may need to build this DIE type
24367 before building the type and calling set_die_type.
e71ec853 24368 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24369 possible before fetching more types to complete the current type.
24370 * Make the type as complete as possible before fetching more types. */
1c379e20 24371
f792889a 24372static struct type *
1c379e20
DJ
24373set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24374{
518817b3
SM
24375 struct dwarf2_per_objfile *dwarf2_per_objfile
24376 = cu->per_cu->dwarf2_per_objfile;
dee91e82 24377 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 24378 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
24379 struct attribute *attr;
24380 struct dynamic_prop prop;
1c379e20 24381
b4ba55a1
JB
24382 /* For Ada types, make sure that the gnat-specific data is always
24383 initialized (if not already set). There are a few types where
24384 we should not be doing so, because the type-specific area is
24385 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24386 where the type-specific area is used to store the floatformat).
24387 But this is not a problem, because the gnat-specific information
24388 is actually not needed for these types. */
24389 if (need_gnat_info (cu)
24390 && TYPE_CODE (type) != TYPE_CODE_FUNC
24391 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
24392 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24393 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24394 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
24395 && !HAVE_GNAT_AUX_INFO (type))
24396 INIT_GNAT_SPECIFIC (type);
24397
3f2f83dd
KB
24398 /* Read DW_AT_allocated and set in type. */
24399 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 24400 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24401 {
09ba997f 24402 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24403 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24404 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
24405 }
24406 else if (attr != NULL)
24407 {
b98664d3 24408 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 24409 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24410 sect_offset_str (die->sect_off));
3f2f83dd
KB
24411 }
24412
24413 /* Read DW_AT_associated and set in type. */
24414 attr = dwarf2_attr (die, DW_AT_associated, 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_ASSOCIATED, prop, type);
3f2f83dd
KB
24420 }
24421 else if (attr != NULL)
24422 {
b98664d3 24423 complaint (_("DW_AT_associated 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
3cdcd0ce
JB
24428 /* Read DW_AT_data_location and set in type. */
24429 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 24430 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 24431 cu->per_cu->addr_type ()))
50a82047 24432 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 24433
dee91e82 24434 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
24435 dwarf2_per_objfile->die_type_hash
24436 = htab_up (htab_create_alloc (127,
24437 per_cu_offset_and_type_hash,
24438 per_cu_offset_and_type_eq,
24439 NULL, xcalloc, xfree));
1c379e20 24440
dee91e82 24441 ofs.per_cu = cu->per_cu;
9c541725 24442 ofs.sect_off = die->sect_off;
1c379e20 24443 ofs.type = type;
dee91e82 24444 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 24445 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24446 if (*slot)
b98664d3 24447 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24448 sect_offset_str (die->sect_off));
8d749320
SM
24449 *slot = XOBNEW (&objfile->objfile_obstack,
24450 struct dwarf2_per_cu_offset_and_type);
1c379e20 24451 **slot = ofs;
f792889a 24452 return type;
1c379e20
DJ
24453}
24454
9c541725 24455/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24456 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24457
24458static struct type *
9c541725 24459get_die_type_at_offset (sect_offset sect_off,
673bfd45 24460 struct dwarf2_per_cu_data *per_cu)
1c379e20 24461{
dee91e82 24462 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 24463 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 24464
dee91e82 24465 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24466 return NULL;
1c379e20 24467
dee91e82 24468 ofs.per_cu = per_cu;
9c541725 24469 ofs.sect_off = sect_off;
9a3c8263 24470 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 24471 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24472 if (slot)
24473 return slot->type;
24474 else
24475 return NULL;
24476}
24477
02142a6c 24478/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24479 or return NULL if DIE does not have a saved type. */
24480
24481static struct type *
24482get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24483{
9c541725 24484 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24485}
24486
10b3939b
DJ
24487/* Add a dependence relationship from CU to REF_PER_CU. */
24488
24489static void
24490dwarf2_add_dependence (struct dwarf2_cu *cu,
24491 struct dwarf2_per_cu_data *ref_per_cu)
24492{
24493 void **slot;
24494
24495 if (cu->dependencies == NULL)
24496 cu->dependencies
24497 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24498 NULL, &cu->comp_unit_obstack,
24499 hashtab_obstack_allocate,
24500 dummy_obstack_deallocate);
24501
24502 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24503 if (*slot == NULL)
24504 *slot = ref_per_cu;
24505}
1c379e20 24506
f504f079
DE
24507/* Subroutine of dwarf2_mark to pass to htab_traverse.
24508 Set the mark field in every compilation unit in the
ae038cb0
DJ
24509 cache that we must keep because we are keeping CU. */
24510
10b3939b
DJ
24511static int
24512dwarf2_mark_helper (void **slot, void *data)
24513{
24514 struct dwarf2_per_cu_data *per_cu;
24515
24516 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24517
24518 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24519 reading of the chain. As such dependencies remain valid it is not much
24520 useful to track and undo them during QUIT cleanups. */
24521 if (per_cu->cu == NULL)
24522 return 1;
24523
10b3939b
DJ
24524 if (per_cu->cu->mark)
24525 return 1;
9068261f 24526 per_cu->cu->mark = true;
10b3939b
DJ
24527
24528 if (per_cu->cu->dependencies != NULL)
24529 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24530
24531 return 1;
24532}
24533
f504f079
DE
24534/* Set the mark field in CU and in every other compilation unit in the
24535 cache that we must keep because we are keeping CU. */
24536
ae038cb0
DJ
24537static void
24538dwarf2_mark (struct dwarf2_cu *cu)
24539{
24540 if (cu->mark)
24541 return;
9068261f 24542 cu->mark = true;
10b3939b
DJ
24543 if (cu->dependencies != NULL)
24544 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24545}
24546
24547static void
24548dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24549{
24550 while (per_cu)
24551 {
9068261f 24552 per_cu->cu->mark = false;
ae038cb0
DJ
24553 per_cu = per_cu->cu->read_in_chain;
24554 }
72bf9492
DJ
24555}
24556
72bf9492
DJ
24557/* Trivial hash function for partial_die_info: the hash value of a DIE
24558 is its offset in .debug_info for this objfile. */
24559
24560static hashval_t
24561partial_die_hash (const void *item)
24562{
9a3c8263
SM
24563 const struct partial_die_info *part_die
24564 = (const struct partial_die_info *) item;
9a619af0 24565
9c541725 24566 return to_underlying (part_die->sect_off);
72bf9492
DJ
24567}
24568
24569/* Trivial comparison function for partial_die_info structures: two DIEs
24570 are equal if they have the same offset. */
24571
24572static int
24573partial_die_eq (const void *item_lhs, const void *item_rhs)
24574{
9a3c8263
SM
24575 const struct partial_die_info *part_die_lhs
24576 = (const struct partial_die_info *) item_lhs;
24577 const struct partial_die_info *part_die_rhs
24578 = (const struct partial_die_info *) item_rhs;
9a619af0 24579
9c541725 24580 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24581}
24582
3c3bb058
AB
24583struct cmd_list_element *set_dwarf_cmdlist;
24584struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24585
24586static void
981a3fb3 24587set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24588{
b4f54984 24589 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24590 gdb_stdout);
ae038cb0
DJ
24591}
24592
24593static void
981a3fb3 24594show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24595{
b4f54984 24596 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24597}
24598
9291a0cd 24599static void
cd4fb1b2
SM
24600show_check_physname (struct ui_file *file, int from_tty,
24601 struct cmd_list_element *c, const char *value)
9291a0cd 24602{
cd4fb1b2
SM
24603 fprintf_filtered (file,
24604 _("Whether to check \"physname\" is %s.\n"),
24605 value);
9291a0cd
TT
24606}
24607
6c265988 24608void _initialize_dwarf2_read ();
cd4fb1b2 24609void
6c265988 24610_initialize_dwarf2_read ()
9291a0cd 24611{
cd4fb1b2
SM
24612 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24613Set DWARF specific variables.\n\
590042fc 24614Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
24615 &set_dwarf_cmdlist, "maintenance set dwarf ",
24616 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24617
cd4fb1b2 24618 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
24619Show DWARF specific variables.\n\
24620Show DWARF variables such as the cache size."),
cd4fb1b2
SM
24621 &show_dwarf_cmdlist, "maintenance show dwarf ",
24622 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24623
cd4fb1b2
SM
24624 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24625 &dwarf_max_cache_age, _("\
24626Set the upper bound on the age of cached DWARF compilation units."), _("\
24627Show the upper bound on the age of cached DWARF compilation units."), _("\
24628A higher limit means that cached compilation units will be stored\n\
24629in memory longer, and more total memory will be used. Zero disables\n\
24630caching, which can slow down startup."),
24631 NULL,
24632 show_dwarf_max_cache_age,
24633 &set_dwarf_cmdlist,
24634 &show_dwarf_cmdlist);
156942c7 24635
cd4fb1b2
SM
24636 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24637Set debugging of the DWARF reader."), _("\
24638Show debugging of the DWARF reader."), _("\
24639When enabled (non-zero), debugging messages are printed during DWARF\n\
24640reading and symtab expansion. A value of 1 (one) provides basic\n\
24641information. A value greater than 1 provides more verbose information."),
24642 NULL,
24643 NULL,
24644 &setdebuglist, &showdebuglist);
9291a0cd 24645
cd4fb1b2
SM
24646 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24647Set debugging of the DWARF DIE reader."), _("\
24648Show debugging of the DWARF DIE reader."), _("\
24649When enabled (non-zero), DIEs are dumped after they are read in.\n\
24650The value is the maximum depth to print."),
24651 NULL,
24652 NULL,
24653 &setdebuglist, &showdebuglist);
9291a0cd 24654
cd4fb1b2
SM
24655 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24656Set debugging of the dwarf line reader."), _("\
24657Show debugging of the dwarf line reader."), _("\
24658When enabled (non-zero), line number entries are dumped as they are read in.\n\
24659A value of 1 (one) provides basic information.\n\
24660A value greater than 1 provides more verbose information."),
24661 NULL,
24662 NULL,
24663 &setdebuglist, &showdebuglist);
437afbb8 24664
cd4fb1b2
SM
24665 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24666Set cross-checking of \"physname\" code against demangler."), _("\
24667Show cross-checking of \"physname\" code against demangler."), _("\
24668When enabled, GDB's internal \"physname\" code is checked against\n\
24669the demangler."),
24670 NULL, show_check_physname,
24671 &setdebuglist, &showdebuglist);
900e11f9 24672
e615022a
DE
24673 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24674 no_class, &use_deprecated_index_sections, _("\
24675Set whether to use deprecated gdb_index sections."), _("\
24676Show whether to use deprecated gdb_index sections."), _("\
24677When enabled, deprecated .gdb_index sections are used anyway.\n\
24678Normally they are ignored either because of a missing feature or\n\
24679performance issue.\n\
24680Warning: This option must be enabled before gdb reads the file."),
24681 NULL,
24682 NULL,
24683 &setlist, &showlist);
24684
f1e6e072
TT
24685 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24686 &dwarf2_locexpr_funcs);
24687 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24688 &dwarf2_loclist_funcs);
24689
24690 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24691 &dwarf2_block_frame_base_locexpr_funcs);
24692 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24693 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24694
24695#if GDB_SELF_TEST
24696 selftests::register_test ("dw2_expand_symtabs_matching",
24697 selftests::dw2_expand_symtabs_matching::run_test);
24698#endif
6502dd73 24699}
This page took 5.307047 seconds and 4 git commands to generate.