Remove DWARF queue-related globals
[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"
82ca8957
TT
35#include "dwarf2/index-cache.h"
36#include "dwarf2/index-common.h"
f4382c45 37#include "dwarf2/leb.h"
4de283e4
TT
38#include "bfd.h"
39#include "elf-bfd.h"
40#include "symtab.h"
41#include "gdbtypes.h"
42#include "objfiles.h"
d55e5aa6 43#include "dwarf2.h"
4de283e4
TT
44#include "buildsym.h"
45#include "demangle.h"
46#include "gdb-demangle.h"
4de283e4
TT
47#include "filenames.h" /* for DOSish file names */
48#include "macrotab.h"
49#include "language.h"
50#include "complaints.h"
82ca8957
TT
51#include "dwarf2/expr.h"
52#include "dwarf2/loc.h"
4de283e4
TT
53#include "cp-support.h"
54#include "hashtab.h"
55#include "command.h"
d55e5aa6 56#include "gdbcmd.h"
4de283e4
TT
57#include "block.h"
58#include "addrmap.h"
59#include "typeprint.h"
60#include "psympriv.h"
4de283e4 61#include "c-lang.h"
d55e5aa6 62#include "go-lang.h"
4de283e4
TT
63#include "valprint.h"
64#include "gdbcore.h" /* for gnutarget */
65#include "gdb/gdb-index.h"
4de283e4
TT
66#include "gdb_bfd.h"
67#include "f-lang.h"
68#include "source.h"
4de283e4 69#include "build-id.h"
d55e5aa6 70#include "namespace.h"
268a13a5
TT
71#include "gdbsupport/function-view.h"
72#include "gdbsupport/gdb_optional.h"
73#include "gdbsupport/underlying.h"
268a13a5 74#include "gdbsupport/hash_enum.h"
4de283e4 75#include "filename-seen-cache.h"
b32b108a 76#include "producer.h"
4de283e4 77#include <fcntl.h>
4de283e4 78#include <algorithm>
4de283e4 79#include <unordered_map>
268a13a5 80#include "gdbsupport/selftest.h"
c9317f21 81#include "rust-lang.h"
268a13a5 82#include "gdbsupport/pathstuff.h"
437afbb8 83
73be47f5
DE
84/* When == 1, print basic high level tracing messages.
85 When > 1, be more verbose.
b4f54984
DE
86 This is in contrast to the low level DIE reading of dwarf_die_debug. */
87static unsigned int dwarf_read_debug = 0;
45cfd468 88
d97bc12b 89/* When non-zero, dump DIEs after they are read in. */
b4f54984 90static unsigned int dwarf_die_debug = 0;
d97bc12b 91
27e0867f
DE
92/* When non-zero, dump line number entries as they are read in. */
93static unsigned int dwarf_line_debug = 0;
94
491144b5
CB
95/* When true, cross-check physname against demangler. */
96static bool check_physname = false;
900e11f9 97
491144b5
CB
98/* When true, do not reject deprecated .gdb_index sections. */
99static bool use_deprecated_index_sections = false;
481860b3 100
5bfd760d 101static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 102
f1e6e072
TT
103/* The "aclass" indices for various kinds of computed DWARF symbols. */
104
105static int dwarf2_locexpr_index;
106static int dwarf2_loclist_index;
107static int dwarf2_locexpr_block_index;
108static int dwarf2_loclist_block_index;
109
3f563c84
PA
110/* An index into a (C++) symbol name component in a symbol name as
111 recorded in the mapped_index's symbol table. For each C++ symbol
112 in the symbol table, we record one entry for the start of each
113 component in the symbol in a table of name components, and then
114 sort the table, in order to be able to binary search symbol names,
115 ignoring leading namespaces, both completion and regular look up.
116 For example, for symbol "A::B::C", we'll have an entry that points
117 to "A::B::C", another that points to "B::C", and another for "C".
118 Note that function symbols in GDB index have no parameter
119 information, just the function/method names. You can convert a
120 name_component to a "const char *" using the
121 'mapped_index::symbol_name_at(offset_type)' method. */
122
123struct name_component
124{
125 /* Offset in the symbol name where the component starts. Stored as
126 a (32-bit) offset instead of a pointer to save memory and improve
127 locality on 64-bit architectures. */
128 offset_type name_offset;
129
130 /* The symbol's index in the symbol and constant pool tables of a
131 mapped_index. */
132 offset_type idx;
133};
134
44ed8f3e
PA
135/* Base class containing bits shared by both .gdb_index and
136 .debug_name indexes. */
137
138struct mapped_index_base
139{
22ca247e
TT
140 mapped_index_base () = default;
141 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
142
44ed8f3e
PA
143 /* The name_component table (a sorted vector). See name_component's
144 description above. */
145 std::vector<name_component> name_components;
146
147 /* How NAME_COMPONENTS is sorted. */
148 enum case_sensitivity name_components_casing;
149
150 /* Return the number of names in the symbol table. */
151 virtual size_t symbol_name_count () const = 0;
152
153 /* Get the name of the symbol at IDX in the symbol table. */
154 virtual const char *symbol_name_at (offset_type idx) const = 0;
155
156 /* Return whether the name at IDX in the symbol table should be
157 ignored. */
158 virtual bool symbol_name_slot_invalid (offset_type idx) const
159 {
160 return false;
161 }
162
163 /* Build the symbol name component sorted vector, if we haven't
164 yet. */
165 void build_name_components ();
166
167 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
168 possible matches for LN_NO_PARAMS in the name component
169 vector. */
170 std::pair<std::vector<name_component>::const_iterator,
171 std::vector<name_component>::const_iterator>
3b00ef10
TT
172 find_name_components_bounds (const lookup_name_info &ln_no_params,
173 enum language lang) const;
44ed8f3e
PA
174
175 /* Prevent deleting/destroying via a base class pointer. */
176protected:
177 ~mapped_index_base() = default;
178};
179
9291a0cd
TT
180/* A description of the mapped index. The file format is described in
181 a comment by the code that writes the index. */
fc898b42 182struct mapped_index final : public mapped_index_base
9291a0cd 183{
f00a2de2
PA
184 /* A slot/bucket in the symbol table hash. */
185 struct symbol_table_slot
186 {
187 const offset_type name;
188 const offset_type vec;
189 };
190
559a7a62 191 /* Index data format version. */
3063847f 192 int version = 0;
559a7a62 193
f00a2de2
PA
194 /* The address table data. */
195 gdb::array_view<const gdb_byte> address_table;
b11b1f88 196
3876f04e 197 /* The symbol table, implemented as a hash table. */
f00a2de2 198 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 199
9291a0cd 200 /* A pointer to the constant pool. */
3063847f 201 const char *constant_pool = nullptr;
3f563c84 202
44ed8f3e
PA
203 bool symbol_name_slot_invalid (offset_type idx) const override
204 {
205 const auto &bucket = this->symbol_table[idx];
9ab08412 206 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 207 }
5c58de74 208
3f563c84
PA
209 /* Convenience method to get at the name of the symbol at IDX in the
210 symbol table. */
44ed8f3e 211 const char *symbol_name_at (offset_type idx) const override
f00a2de2 212 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 213
44ed8f3e
PA
214 size_t symbol_name_count () const override
215 { return this->symbol_table.size (); }
9291a0cd
TT
216};
217
927aa2e7
JK
218/* A description of the mapped .debug_names.
219 Uninitialized map has CU_COUNT 0. */
fc898b42 220struct mapped_debug_names final : public mapped_index_base
927aa2e7 221{
ed2dc618
SM
222 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
223 : dwarf2_per_objfile (dwarf2_per_objfile_)
224 {}
225
226 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
227 bfd_endian dwarf5_byte_order;
228 bool dwarf5_is_dwarf64;
229 bool augmentation_is_gdb;
230 uint8_t offset_size;
231 uint32_t cu_count = 0;
232 uint32_t tu_count, bucket_count, name_count;
233 const gdb_byte *cu_table_reordered, *tu_table_reordered;
234 const uint32_t *bucket_table_reordered, *hash_table_reordered;
235 const gdb_byte *name_table_string_offs_reordered;
236 const gdb_byte *name_table_entry_offs_reordered;
237 const gdb_byte *entry_pool;
238
239 struct index_val
240 {
241 ULONGEST dwarf_tag;
242 struct attr
243 {
244 /* Attribute name DW_IDX_*. */
245 ULONGEST dw_idx;
246
247 /* Attribute form DW_FORM_*. */
248 ULONGEST form;
249
250 /* Value if FORM is DW_FORM_implicit_const. */
251 LONGEST implicit_const;
252 };
253 std::vector<attr> attr_vec;
254 };
255
256 std::unordered_map<ULONGEST, index_val> abbrev_map;
257
258 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
259
260 /* Implementation of the mapped_index_base virtual interface, for
261 the name_components cache. */
262
263 const char *symbol_name_at (offset_type idx) const override
264 { return namei_to_name (idx); }
265
266 size_t symbol_name_count () const override
267 { return this->name_count; }
927aa2e7
JK
268};
269
cd4fb1b2 270/* See dwarf2read.h. */
ed2dc618 271
cd4fb1b2 272dwarf2_per_objfile *
ed2dc618
SM
273get_dwarf2_per_objfile (struct objfile *objfile)
274{
5bfd760d 275 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 276}
c906108c 277
251d32d9 278/* Default names of the debugging sections. */
c906108c 279
233a11ab
CS
280/* Note that if the debugging section has been compressed, it might
281 have a name like .zdebug_info. */
282
9cdd5dbd
DE
283static const struct dwarf2_debug_sections dwarf2_elf_names =
284{
251d32d9
TG
285 { ".debug_info", ".zdebug_info" },
286 { ".debug_abbrev", ".zdebug_abbrev" },
287 { ".debug_line", ".zdebug_line" },
288 { ".debug_loc", ".zdebug_loc" },
43988095 289 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 290 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 291 { ".debug_macro", ".zdebug_macro" },
251d32d9 292 { ".debug_str", ".zdebug_str" },
18a8505e 293 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 294 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 295 { ".debug_ranges", ".zdebug_ranges" },
43988095 296 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 297 { ".debug_types", ".zdebug_types" },
3019eac3 298 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
299 { ".debug_frame", ".zdebug_frame" },
300 { ".eh_frame", NULL },
24d3216f 301 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
302 { ".debug_names", ".zdebug_names" },
303 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 304 23
251d32d9 305};
c906108c 306
80626a55 307/* List of DWO/DWP sections. */
3019eac3 308
80626a55 309static const struct dwop_section_names
3019eac3
DE
310{
311 struct dwarf2_section_names abbrev_dwo;
312 struct dwarf2_section_names info_dwo;
313 struct dwarf2_section_names line_dwo;
314 struct dwarf2_section_names loc_dwo;
43988095 315 struct dwarf2_section_names loclists_dwo;
09262596
DE
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
3019eac3
DE
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
80626a55
DE
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
3019eac3 323}
80626a55 324dwop_section_names =
3019eac3
DE
325{
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 330 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
331 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
332 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
333 { ".debug_str.dwo", ".zdebug_str.dwo" },
334 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
335 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
336 { ".debug_cu_index", ".zdebug_cu_index" },
337 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
338};
339
c906108c
SS
340/* local data types */
341
107d2387
AC
342/* The data in a compilation unit header, after target2host
343 translation, looks like this. */
c906108c 344struct comp_unit_head
a738430d 345{
c764a876 346 unsigned int length;
a738430d 347 short version;
a738430d
MK
348 unsigned char addr_size;
349 unsigned char signed_addr_p;
9c541725 350 sect_offset abbrev_sect_off;
57349743 351
a738430d
MK
352 /* Size of file offsets; either 4 or 8. */
353 unsigned int offset_size;
57349743 354
a738430d
MK
355 /* Size of the length field; either 4 or 12. */
356 unsigned int initial_length_size;
57349743 357
43988095
JK
358 enum dwarf_unit_type unit_type;
359
a738430d
MK
360 /* Offset to the first byte of this compilation unit header in the
361 .debug_info section, for resolving relative reference dies. */
9c541725 362 sect_offset sect_off;
57349743 363
d00adf39
DE
364 /* Offset to first die in this cu from the start of the cu.
365 This will be the first byte following the compilation unit header. */
9c541725 366 cu_offset first_die_cu_offset;
43988095 367
a084a2a6
AT
368
369 /* 64-bit signature of this unit. For type units, it denotes the signature of
370 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
371 Also used in DWARF 5, to denote the dwo id when the unit type is
372 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
373 ULONGEST signature;
374
375 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 376 cu_offset type_cu_offset_in_tu;
a738430d 377};
c906108c 378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
fcd3b13d
SM
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
c24bdb02
KS
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
fcd3b13d 427 CORE_ADDR base_address = 0;
d00adf39
DE
428
429 /* Non-zero if base_address has been set. */
fcd3b13d 430 int base_known = 0;
d00adf39 431
e142c38c 432 /* The language we are debugging. */
fcd3b13d
SM
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
e142c38c 435
fcd3b13d 436 const char *producer = nullptr;
b0f35d58 437
c24bdb02 438private:
804d2729
TT
439 /* The symtab builder for this CU. This is only non-NULL when full
440 symbols are being read. */
c24bdb02 441 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 442
c24bdb02 443public:
e142c38c
DJ
444 /* The generic symbol table building routines have separate lists for
445 file scope symbols and all all other scopes (local scopes). So
446 we need to select the right one to pass to add_symbol_to_list().
447 We do it by keeping a pointer to the correct list in list_in_scope.
448
449 FIXME: The original dwarf code just treated the file scope as the
450 first local scope, and all other local scopes as nested local
451 scopes, and worked fine. Check to see if we really need to
452 distinguish these in buildsym.c. */
fcd3b13d 453 struct pending **list_in_scope = nullptr;
e142c38c 454
b64f50a1
JK
455 /* Hash table holding all the loaded partial DIEs
456 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 457 htab_t partial_dies = nullptr;
72bf9492
DJ
458
459 /* Storage for things with the same lifetime as this read-in compilation
460 unit, including partial DIEs. */
fcd3b13d 461 auto_obstack comp_unit_obstack;
72bf9492 462
ae038cb0
DJ
463 /* When multiple dwarf2_cu structures are living in memory, this field
464 chains them all together, so that they can be released efficiently.
465 We will probably also want a generation counter so that most-recently-used
466 compilation units are cached... */
fcd3b13d 467 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 468
69d751e3 469 /* Backlink to our per_cu entry. */
ae038cb0
DJ
470 struct dwarf2_per_cu_data *per_cu;
471
472 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 473 int last_used = 0;
ae038cb0 474
b64f50a1
JK
475 /* A hash table of DIE cu_offset for following references with
476 die_info->offset.sect_off as hash. */
fcd3b13d 477 htab_t die_hash = nullptr;
10b3939b
DJ
478
479 /* Full DIEs if read in. */
fcd3b13d 480 struct die_info *dies = nullptr;
10b3939b
DJ
481
482 /* A set of pointers to dwarf2_per_cu_data objects for compilation
483 units referenced by this one. Only set during full symbol processing;
484 partial symbol tables do not have dependencies. */
fcd3b13d 485 htab_t dependencies = nullptr;
10b3939b 486
cb1df416 487 /* Header data from the line table, during full symbol processing. */
fcd3b13d 488 struct line_header *line_header = nullptr;
4c8aa72d
PA
489 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
490 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
491 this is the DW_TAG_compile_unit die for this CU. We'll hold on
492 to the line header as long as this DIE is being processed. See
493 process_die_scope. */
fcd3b13d 494 die_info *line_header_die_owner = nullptr;
cb1df416 495
3da10d80
KS
496 /* A list of methods which need to have physnames computed
497 after all type information has been read. */
c89b44cd 498 std::vector<delayed_method_info> method_list;
3da10d80 499
96408a79 500 /* To be copied to symtab->call_site_htab. */
fcd3b13d 501 htab_t call_site_htab = nullptr;
96408a79 502
034e5797
DE
503 /* Non-NULL if this CU came from a DWO file.
504 There is an invariant here that is important to remember:
505 Except for attributes copied from the top level DIE in the "main"
506 (or "stub") file in preparation for reading the DWO file
18a8505e 507 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
508 Either there isn't a DWO file (in which case this is NULL and the point
509 is moot), or there is and either we're not going to read it (in which
510 case this is NULL) or there is and we are reading it (in which case this
511 is non-NULL). */
fcd3b13d 512 struct dwo_unit *dwo_unit = nullptr;
3019eac3 513
18a8505e 514 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 515 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 516 gdb::optional<ULONGEST> addr_base;
3019eac3 517
18a8505e 518 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 519 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 520 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
521 be used without needing to know whether DWO files are in use or not.
522 N.B. This does not apply to DW_AT_ranges appearing in
523 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
524 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 525 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 526 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 527 ULONGEST ranges_base = 0;
2e3cf129 528
c9317f21
TT
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
18a8505e
AT
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
ae038cb0 543 /* Mark used when releasing cached dies. */
9068261f 544 bool mark : 1;
ae038cb0 545
8be455d7
JK
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 550 bool has_loclist : 1;
ba919b58 551
9068261f 552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
9068261f
AB
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 559 bool producer_is_icc : 1;
9068261f 560 bool producer_is_icc_lt_14 : 1;
c258c396 561 bool producer_is_codewarrior : 1;
4d4ec4e5 562
9068261f 563 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
9068261f 567 bool processing_has_namespace_info : 1;
d590ff25
YQ
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
e7c27a73
DJ
588};
589
094b34ac
DE
590/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593struct stmt_list_hash
594{
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 599 sect_offset line_sect_off;
094b34ac
DE
600};
601
f4dc4d17
DE
602/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605struct type_unit_group
606{
0186c6a7 607 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab.
611 This PER_CU is recognized by having no section. */
8a0459fd 612#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
613 struct dwarf2_per_cu_data per_cu;
614
0186c6a7
DE
615 /* The TUs that share this DW_AT_stmt_list entry.
616 This is added to while parsing type units to build partial symtabs,
617 and is deleted afterwards and not used again. */
a8b3b8e9 618 std::vector<signatured_type *> *tus;
f4dc4d17 619
43f3e411 620 /* The compunit symtab.
094b34ac 621 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
622 so we create an essentially anonymous symtab as the compunit symtab. */
623 struct compunit_symtab *compunit_symtab;
f4dc4d17 624
094b34ac
DE
625 /* The data used to construct the hash key. */
626 struct stmt_list_hash hash;
f4dc4d17
DE
627
628 /* The number of symtabs from the line header.
629 The value here must match line_header.num_file_names. */
630 unsigned int num_symtabs;
631
632 /* The symbol tables for this TU (obtained from the files listed in
633 DW_AT_stmt_list).
634 WARNING: The order of entries here must match the order of entries
635 in the line header. After the first TU using this type_unit_group, the
636 line header for the subsequent TUs is recreated from this. This is done
637 because we need to use the same symtabs for each TU using the same
638 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
639 there's no guarantee the line header doesn't have duplicate entries. */
640 struct symtab **symtabs;
641};
642
73869dc2 643/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
644
645struct dwo_sections
646{
647 struct dwarf2_section_info abbrev;
3019eac3
DE
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
43988095 650 struct dwarf2_section_info loclists;
09262596
DE
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
3019eac3
DE
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
80626a55
DE
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
fd5866f6 657 std::vector<dwarf2_section_info> types;
3019eac3
DE
658};
659
c88ee1f0 660/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
661
662struct dwo_unit
663{
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 673 struct dwarf2_section_info *section;
3019eac3 674
9c541725
PA
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
3019eac3
DE
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681};
682
73869dc2
DE
683/* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687enum dwp_v2_section_ids
688{
689 DW_SECT_MIN = 1
690};
691
80626a55 692/* Data for one DWO file.
57d63ce2
DE
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
3019eac3
DE
702
703struct dwo_file
704{
51ac9db5
SM
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
18a8505e 708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
51ac9db5 712 const char *dwo_name = nullptr;
0ac5b59e
DE
713
714 /* The DW_AT_comp_dir attribute. */
51ac9db5 715 const char *comp_dir = nullptr;
3019eac3 716
80626a55
DE
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 719 gdb_bfd_ref_ptr dbfd;
3019eac3 720
73869dc2
DE
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
51ac9db5 724 struct dwo_sections sections {};
3019eac3 725
33c5cd75
DB
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
51ac9db5 730 htab_t cus {};
3019eac3
DE
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
51ac9db5 734 htab_t tus {};
3019eac3
DE
735};
736
80626a55
DE
737/* These sections are what may appear in a DWP file. */
738
739struct dwp_sections
740{
73869dc2 741 /* These are used by both DWP version 1 and 2. */
80626a55
DE
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
73869dc2
DE
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
80626a55
DE
762};
763
73869dc2
DE
764/* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 766
73869dc2 767struct virtual_v1_dwo_sections
80626a55
DE
768{
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 776 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
777 struct dwarf2_section_info info_or_types;
778};
779
73869dc2
DE
780/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785struct virtual_v2_dwo_sections
786{
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809};
810
80626a55
DE
811/* Contents of DWP hash tables. */
812
813struct dwp_hash_table
814{
73869dc2 815 uint32_t version, nr_columns;
80626a55 816 uint32_t nr_units, nr_slots;
73869dc2
DE
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828#define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
80626a55
DE
840};
841
842/* Data for one DWP file. */
843
844struct dwp_file
845{
400174b1
TT
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
80626a55
DE
852 /* Name of the file. */
853 const char *name;
854
73869dc2 855 /* File format version. */
400174b1 856 int version = 0;
73869dc2 857
93417882 858 /* The bfd. */
400174b1 859 gdb_bfd_ref_ptr dbfd;
80626a55
DE
860
861 /* Section info for this file. */
400174b1 862 struct dwp_sections sections {};
80626a55 863
57d63ce2 864 /* Table of CUs in the file. */
400174b1 865 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
866
867 /* Table of TUs in the file. */
400174b1 868 const struct dwp_hash_table *tus = nullptr;
80626a55 869
19ac8c2e 870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
871 htab_t loaded_cus {};
872 htab_t loaded_tus {};
80626a55 873
73869dc2
DE
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
400174b1
TT
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
80626a55
DE
878};
879
0963b4bd
MS
880/* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
dee91e82 883 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
884
885struct die_reader_specs
886{
a32a8923 887 /* The bfd of die_section. */
93311388
DE
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
80626a55 893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
894 struct dwo_file *dwo_file;
895
dee91e82 896 /* The section the die comes from.
3019eac3 897 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
d521ce57 901 const gdb_byte *buffer;
f664829e
DE
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
a2ce51a0 905
685af9cd
TT
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
93311388
DE
908};
909
c0ab21c2
TT
910/* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913class cutu_reader : public die_reader_specs
914{
915public:
916
917 cutu_reader (struct dwarf2_per_cu_data *this_cu,
918 struct abbrev_table *abbrev_table,
919 int use_existing_cu, int keep,
920 bool skip_partial);
921
922 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
926 ~cutu_reader ();
927
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
932 bool dummy_p = false;
933
934private:
935 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
936 int use_existing_cu, int keep);
937
938 struct dwarf2_per_cu_data *m_this_cu;
939 int m_keep = 0;
940 std::unique_ptr<dwarf2_cu> m_new_cu;
941
942 /* The ordinary abbreviation table. */
943 abbrev_table_up m_abbrev_table_holder;
944
945 /* The DWO abbreviation table. */
946 abbrev_table_up m_dwo_abbrev_table;
947};
dee91e82 948
7ba99d21
AT
949/* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
950 later. */
951typedef int dir_index;
ecfb656c 952
7ba99d21
AT
953/* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
954 and later. */
955typedef int file_name_index;
ecfb656c 956
52059ffd
TT
957struct file_entry
958{
fff8551c
PA
959 file_entry () = default;
960
ecfb656c 961 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
962 unsigned int mod_time_, unsigned int length_)
963 : name (name_),
ecfb656c 964 d_index (d_index_),
fff8551c
PA
965 mod_time (mod_time_),
966 length (length_)
967 {}
968
ecfb656c
PA
969 /* Return the include directory at D_INDEX stored in LH. Returns
970 NULL if D_INDEX is out of bounds. */
8c43009f
PA
971 const char *include_dir (const line_header *lh) const;
972
fff8551c
PA
973 /* The file name. Note this is an observing pointer. The memory is
974 owned by debug_line_buffer. */
975 const char *name {};
976
8c43009f 977 /* The directory index (1-based). */
ecfb656c 978 dir_index d_index {};
fff8551c
PA
979
980 unsigned int mod_time {};
981
982 unsigned int length {};
983
984 /* True if referenced by the Line Number Program. */
985 bool included_p {};
986
83769d0b 987 /* The associated symbol table, if any. */
fff8551c 988 struct symtab *symtab {};
52059ffd
TT
989};
990
debd256d
JB
991/* The line number information for a compilation unit (found in the
992 .debug_line section) begins with a "statement program header",
993 which contains the following information. */
994struct line_header
995{
fff8551c
PA
996 line_header ()
997 : offset_in_dwz {}
998 {}
999
1000 /* Add an entry to the include directory table. */
1001 void add_include_dir (const char *include_dir);
1002
1003 /* Add an entry to the file name table. */
ecfb656c 1004 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1005 unsigned int mod_time, unsigned int length);
1006
7ba99d21
AT
1007 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1008 Returns NULL if INDEX is out of bounds. */
ecfb656c 1009 const char *include_dir_at (dir_index index) const
8c43009f 1010 {
7ba99d21
AT
1011 int vec_index;
1012 if (version >= 5)
1013 vec_index = index;
1014 else
1015 vec_index = index - 1;
1016 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
8c43009f 1017 return NULL;
7ba99d21 1018 return m_include_dirs[vec_index];
8c43009f
PA
1019 }
1020
7ba99d21 1021 bool is_valid_file_index (int file_index)
8c43009f 1022 {
7ba99d21
AT
1023 if (version >= 5)
1024 return 0 <= file_index && file_index < file_names_size ();
1025 return 1 <= file_index && file_index <= file_names_size ();
1026 }
ecfb656c 1027
7ba99d21
AT
1028 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1029 Returns NULL if INDEX is out of bounds. */
1030 file_entry *file_name_at (file_name_index index)
1031 {
1032 int vec_index;
1033 if (version >= 5)
1034 vec_index = index;
1035 else
1036 vec_index = index - 1;
1037 if (vec_index < 0 || vec_index >= m_file_names.size ())
fff8551c 1038 return NULL;
7ba99d21 1039 return &m_file_names[vec_index];
fff8551c
PA
1040 }
1041
7ba99d21
AT
1042 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1043 this method should only be used to iterate through all file entries in an
1044 index-agnostic manner. */
1045 std::vector<file_entry> &file_names ()
1046 { return m_file_names; }
1047
527f3840 1048 /* Offset of line number information in .debug_line section. */
9c541725 1049 sect_offset sect_off {};
527f3840
JK
1050
1051 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1052 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1053
1054 unsigned int total_length {};
1055 unsigned short version {};
1056 unsigned int header_length {};
1057 unsigned char minimum_instruction_length {};
1058 unsigned char maximum_ops_per_instruction {};
1059 unsigned char default_is_stmt {};
1060 int line_base {};
1061 unsigned char line_range {};
1062 unsigned char opcode_base {};
debd256d
JB
1063
1064 /* standard_opcode_lengths[i] is the number of operands for the
1065 standard opcode whose value is i. This means that
1066 standard_opcode_lengths[0] is unused, and the last meaningful
1067 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1068 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1069
7ba99d21
AT
1070 int file_names_size ()
1071 { return m_file_names.size(); }
debd256d
JB
1072
1073 /* The start and end of the statement program following this
6502dd73 1074 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1075 const gdb_byte *statement_program_start {}, *statement_program_end {};
7ba99d21
AT
1076
1077 private:
1078 /* The include_directories table. Note these are observing
1079 pointers. The memory is owned by debug_line_buffer. */
1080 std::vector<const char *> m_include_dirs;
1081
1082 /* The file_names table. This is private because the meaning of indexes
1083 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1084 before, and is 0 in DWARF 5 and later). So the client should use
1085 file_name_at method for access. */
1086 std::vector<file_entry> m_file_names;
debd256d 1087};
c906108c 1088
fff8551c
PA
1089typedef std::unique_ptr<line_header> line_header_up;
1090
8c43009f
PA
1091const char *
1092file_entry::include_dir (const line_header *lh) const
1093{
ecfb656c 1094 return lh->include_dir_at (d_index);
8c43009f
PA
1095}
1096
c906108c 1097/* When we construct a partial symbol table entry we only
0963b4bd 1098 need this much information. */
6f06d47b 1099struct partial_die_info : public allocate_on_obstack
c906108c 1100 {
6f06d47b
YQ
1101 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1102
1103 /* Disable assign but still keep copy ctor, which is needed
1104 load_partial_dies. */
1105 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1106
52356b79
YQ
1107 /* Adjust the partial die before generating a symbol for it. This
1108 function may set the is_external flag or change the DIE's
1109 name. */
1110 void fixup (struct dwarf2_cu *cu);
1111
48fbe735
YQ
1112 /* Read a minimal amount of information into the minimal die
1113 structure. */
1114 const gdb_byte *read (const struct die_reader_specs *reader,
1115 const struct abbrev_info &abbrev,
1116 const gdb_byte *info_ptr);
1117
72bf9492 1118 /* Offset of this DIE. */
6f06d47b 1119 const sect_offset sect_off;
72bf9492
DJ
1120
1121 /* DWARF-2 tag for this DIE. */
6f06d47b 1122 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1123
72bf9492 1124 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1125 const unsigned int has_children : 1;
1126
72bf9492
DJ
1127 unsigned int is_external : 1;
1128 unsigned int is_declaration : 1;
1129 unsigned int has_type : 1;
1130 unsigned int has_specification : 1;
1131 unsigned int has_pc_info : 1;
481860b3 1132 unsigned int may_be_inlined : 1;
72bf9492 1133
0c1b455e
TT
1134 /* This DIE has been marked DW_AT_main_subprogram. */
1135 unsigned int main_subprogram : 1;
1136
72bf9492
DJ
1137 /* Flag set if the SCOPE field of this structure has been
1138 computed. */
1139 unsigned int scope_set : 1;
1140
fa4028e9
JB
1141 /* Flag set if the DIE has a byte_size attribute. */
1142 unsigned int has_byte_size : 1;
1143
ff908ebf
AW
1144 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1145 unsigned int has_const_value : 1;
1146
98bfdba5
PA
1147 /* Flag set if any of the DIE's children are template arguments. */
1148 unsigned int has_template_arguments : 1;
1149
52356b79 1150 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1151 unsigned int fixup_called : 1;
1152
36586728
TT
1153 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1154 unsigned int is_dwz : 1;
1155
1156 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1157 unsigned int spec_is_dwz : 1;
1158
72bf9492 1159 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1160 sometimes a default name for unnamed DIEs. */
6f06d47b 1161 const char *name = nullptr;
72bf9492 1162
abc72ce4 1163 /* The linkage name, if present. */
6f06d47b 1164 const char *linkage_name = nullptr;
abc72ce4 1165
72bf9492
DJ
1166 /* The scope to prepend to our children. This is generally
1167 allocated on the comp_unit_obstack, so will disappear
1168 when this compilation unit leaves the cache. */
6f06d47b 1169 const char *scope = nullptr;
72bf9492 1170
95554aad
TT
1171 /* Some data associated with the partial DIE. The tag determines
1172 which field is live. */
1173 union
1174 {
1175 /* The location description associated with this DIE, if any. */
1176 struct dwarf_block *locdesc;
1177 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1178 sect_offset sect_off;
6f06d47b 1179 } d {};
72bf9492
DJ
1180
1181 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1182 CORE_ADDR lowpc = 0;
1183 CORE_ADDR highpc = 0;
72bf9492 1184
93311388 1185 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1186 DW_AT_sibling, if any. */
48fbe735
YQ
1187 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1188 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1189 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1190
1191 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1192 DW_AT_specification (or DW_AT_abstract_origin or
1193 DW_AT_extension). */
6f06d47b 1194 sect_offset spec_offset {};
72bf9492
DJ
1195
1196 /* Pointers to this DIE's parent, first child, and next sibling,
1197 if any. */
6f06d47b
YQ
1198 struct partial_die_info *die_parent = nullptr;
1199 struct partial_die_info *die_child = nullptr;
1200 struct partial_die_info *die_sibling = nullptr;
1201
1202 friend struct partial_die_info *
1203 dwarf2_cu::find_partial_die (sect_offset sect_off);
1204
1205 private:
1206 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1207 partial_die_info (sect_offset sect_off)
1208 : partial_die_info (sect_off, DW_TAG_padding, 0)
1209 {
1210 }
1211
1212 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1213 int has_children_)
1214 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1215 {
1216 is_external = 0;
1217 is_declaration = 0;
1218 has_type = 0;
1219 has_specification = 0;
1220 has_pc_info = 0;
1221 may_be_inlined = 0;
1222 main_subprogram = 0;
1223 scope_set = 0;
1224 has_byte_size = 0;
1225 has_const_value = 0;
1226 has_template_arguments = 0;
1227 fixup_called = 0;
1228 is_dwz = 0;
1229 spec_is_dwz = 0;
1230 }
c906108c
SS
1231 };
1232
0963b4bd 1233/* This data structure holds a complete die structure. */
c906108c
SS
1234struct die_info
1235 {
76815b17
DE
1236 /* DWARF-2 tag for this DIE. */
1237 ENUM_BITFIELD(dwarf_tag) tag : 16;
1238
1239 /* Number of attributes */
98bfdba5
PA
1240 unsigned char num_attrs;
1241
1242 /* True if we're presently building the full type name for the
1243 type derived from this DIE. */
1244 unsigned char building_fullname : 1;
76815b17 1245
adde2bff
DE
1246 /* True if this die is in process. PR 16581. */
1247 unsigned char in_process : 1;
1248
3e225074
TT
1249 /* True if this DIE has children. */
1250 unsigned char has_children : 1;
1251
76815b17
DE
1252 /* Abbrev number */
1253 unsigned int abbrev;
1254
93311388 1255 /* Offset in .debug_info or .debug_types section. */
9c541725 1256 sect_offset sect_off;
78ba4af6
JB
1257
1258 /* The dies in a compilation unit form an n-ary tree. PARENT
1259 points to this die's parent; CHILD points to the first child of
1260 this node; and all the children of a given node are chained
4950bc1c 1261 together via their SIBLING fields. */
639d11d3
DC
1262 struct die_info *child; /* Its first child, if any. */
1263 struct die_info *sibling; /* Its next sibling, if any. */
1264 struct die_info *parent; /* Its parent, if any. */
c906108c 1265
b60c80d6
DJ
1266 /* An array of attributes, with NUM_ATTRS elements. There may be
1267 zero, but it's not common and zero-sized arrays are not
1268 sufficiently portable C. */
1269 struct attribute attrs[1];
c906108c
SS
1270 };
1271
c906108c
SS
1272/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1273 but this would require a corresponding change in unpack_field_as_long
1274 and friends. */
1275static int bits_per_byte = 8;
1276
2ddeaf8a
TT
1277/* When reading a variant or variant part, we track a bit more
1278 information about the field, and store it in an object of this
1279 type. */
1280
1281struct variant_field
1282{
1283 /* If we see a DW_TAG_variant, then this will be the discriminant
1284 value. */
1285 ULONGEST discriminant_value;
1286 /* If we see a DW_TAG_variant, then this will be set if this is the
1287 default branch. */
1288 bool default_branch;
1289 /* While reading a DW_TAG_variant_part, this will be set if this
1290 field is the discriminant. */
1291 bool is_discriminant;
1292};
1293
52059ffd
TT
1294struct nextfield
1295{
be2daae6
TT
1296 int accessibility = 0;
1297 int virtuality = 0;
2ddeaf8a 1298 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1299 struct variant_field variant {};
1300 struct field field {};
52059ffd
TT
1301};
1302
1303struct fnfieldlist
1304{
be2daae6
TT
1305 const char *name = nullptr;
1306 std::vector<struct fn_field> fnfields;
52059ffd
TT
1307};
1308
c906108c
SS
1309/* The routines that read and process dies for a C struct or C++ class
1310 pass lists of data member fields and lists of member function fields
1311 in an instance of a field_info structure, as defined below. */
1312struct field_info
c5aa993b 1313 {
0963b4bd 1314 /* List of data member and baseclasses fields. */
be2daae6
TT
1315 std::vector<struct nextfield> fields;
1316 std::vector<struct nextfield> baseclasses;
c906108c 1317
7d0ccb61 1318 /* Number of fields (including baseclasses). */
be2daae6 1319 int nfields = 0;
c906108c 1320
85102364 1321 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1322 int non_public_fields = 0;
c906108c 1323
c5aa993b
JM
1324 /* Member function fieldlist array, contains name of possibly overloaded
1325 member function, number of overloaded member functions and a pointer
1326 to the head of the member function field chain. */
be2daae6 1327 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1328
1329 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1330 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1331 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1332
1333 /* Nested types defined by this class and the number of elements in this
1334 list. */
be2daae6 1335 std::vector<struct decl_field> nested_types_list;
c5aa993b 1336 };
c906108c 1337
ae038cb0
DJ
1338/* Loaded secondary compilation units are kept in memory until they
1339 have not been referenced for the processing of this many
1340 compilation units. Set this to zero to disable caching. Cache
1341 sizes of up to at least twenty will improve startup time for
1342 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1343static int dwarf_max_cache_age = 5;
920d2a44 1344static void
b4f54984
DE
1345show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1346 struct cmd_list_element *c, const char *value)
920d2a44 1347{
3e43a32a 1348 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1349 "DWARF compilation units is %s.\n"),
920d2a44
AC
1350 value);
1351}
4390d890 1352\f
c906108c
SS
1353/* local function prototypes */
1354
918dd910
JK
1355static void dwarf2_find_base_address (struct die_info *die,
1356 struct dwarf2_cu *cu);
1357
891813be 1358static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1359 (struct dwarf2_per_cu_data *per_cu, const char *name);
1360
f1902523
JK
1361static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1362 const gdb_byte *info_ptr,
3e225074 1363 struct die_info *type_unit_die);
f1902523 1364
ed2dc618
SM
1365static void dwarf2_build_psymtabs_hard
1366 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1367
72bf9492
DJ
1368static void scan_partial_symbols (struct partial_die_info *,
1369 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1370 int, struct dwarf2_cu *);
c906108c 1371
72bf9492
DJ
1372static void add_partial_symbol (struct partial_die_info *,
1373 struct dwarf2_cu *);
63d06c5c 1374
72bf9492
DJ
1375static void add_partial_namespace (struct partial_die_info *pdi,
1376 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1377 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1378
5d7cb8df 1379static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1380 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1381 struct dwarf2_cu *cu);
1382
72bf9492
DJ
1383static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1384 struct dwarf2_cu *cu);
91c24f0a 1385
bc30ff58
JB
1386static void add_partial_subprogram (struct partial_die_info *pdi,
1387 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1388 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1389
d521ce57 1390static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1391
dee91e82 1392static struct partial_die_info *load_partial_dies
d521ce57 1393 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1394
fb816e8b
TV
1395/* A pair of partial_die_info and compilation unit. */
1396struct cu_partial_die_info
1397{
1398 /* The compilation unit of the partial_die_info. */
1399 struct dwarf2_cu *cu;
1400 /* A partial_die_info. */
1401 struct partial_die_info *pdi;
122cf0f2
AB
1402
1403 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1404 : cu (cu),
1405 pdi (pdi)
405feb71 1406 { /* Nothing. */ }
122cf0f2
AB
1407
1408private:
1409 cu_partial_die_info () = delete;
fb816e8b
TV
1410};
1411
122cf0f2
AB
1412static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1413 struct dwarf2_cu *);
72bf9492 1414
d521ce57
TT
1415static const gdb_byte *read_attribute (const struct die_reader_specs *,
1416 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1417 const gdb_byte *, bool *need_reprocess);
1418
1419static void read_attribute_reprocess (const struct die_reader_specs *reader,
1420 struct attribute *attr);
1421
1422static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1423
d521ce57 1424static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1425 unsigned int *);
c906108c 1426
d521ce57 1427static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1428
1429static LONGEST read_checked_initial_length_and_offset
d521ce57 1430 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1431 unsigned int *, unsigned int *);
613e1657 1432
d521ce57
TT
1433static LONGEST read_offset (bfd *, const gdb_byte *,
1434 const struct comp_unit_head *,
c764a876
DE
1435 unsigned int *);
1436
d521ce57 1437static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1438
ed2dc618
SM
1439static sect_offset read_abbrev_offset
1440 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1441 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1442
d521ce57 1443static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1444
d521ce57 1445static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1446
ed2dc618
SM
1447static const char *read_indirect_string
1448 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1449 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1450
ed2dc618
SM
1451static const char *read_indirect_line_string
1452 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1453 const struct comp_unit_head *, unsigned int *);
36586728 1454
ed2dc618
SM
1455static const char *read_indirect_string_at_offset
1456 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1457 LONGEST str_offset);
927aa2e7 1458
ed2dc618
SM
1459static const char *read_indirect_string_from_dwz
1460 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1461
d521ce57
TT
1462static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1463 const gdb_byte *,
3019eac3
DE
1464 unsigned int *);
1465
18a8505e
AT
1466static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1467 ULONGEST str_index);
1468
1469static const char *read_stub_str_index (struct dwarf2_cu *cu,
1470 ULONGEST str_index);
3019eac3 1471
e142c38c 1472static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1473
e142c38c
DJ
1474static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1475 struct dwarf2_cu *);
c906108c 1476
348e048f 1477static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1478 unsigned int);
348e048f 1479
7d45c7c3
KB
1480static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1481 struct dwarf2_cu *cu);
1482
a084a2a6
AT
1483static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1484
05cf31d1
JB
1485static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1486 struct dwarf2_cu *cu);
1487
e142c38c 1488static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1489
e142c38c 1490static struct die_info *die_specification (struct die_info *die,
f2f0e013 1491 struct dwarf2_cu **);
63d06c5c 1492
9c541725 1493static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1494 struct dwarf2_cu *cu);
debd256d 1495
f3f5162e 1496static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1497 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1498 CORE_ADDR, int decode_mapping);
c906108c 1499
804d2729
TT
1500static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1501 const char *);
c906108c 1502
a14ed312 1503static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1504 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1505
ff39bb5e 1506static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1507 struct dwarf2_cu *);
c906108c 1508
ff39bb5e 1509static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1510 struct type *type,
1511 const char *name,
1512 struct obstack *obstack,
12df843f 1513 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1514 const gdb_byte **bytes,
98bfdba5 1515 struct dwarf2_locexpr_baton **baton);
2df3850c 1516
e7c27a73 1517static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1518
b4ba55a1
JB
1519static int need_gnat_info (struct dwarf2_cu *);
1520
3e43a32a
MS
1521static struct type *die_descriptive_type (struct die_info *,
1522 struct dwarf2_cu *);
b4ba55a1
JB
1523
1524static void set_descriptive_type (struct type *, struct die_info *,
1525 struct dwarf2_cu *);
1526
e7c27a73
DJ
1527static struct type *die_containing_type (struct die_info *,
1528 struct dwarf2_cu *);
c906108c 1529
ff39bb5e 1530static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1531 struct dwarf2_cu *);
c906108c 1532
f792889a 1533static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1534
673bfd45
DE
1535static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1536
0d5cff50 1537static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1538
6e70227d 1539static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1540 const char *suffix, int physname,
1541 struct dwarf2_cu *cu);
63d06c5c 1542
e7c27a73 1543static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1544
348e048f
DE
1545static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1546
e7c27a73 1547static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1548
e7c27a73 1549static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1550
96408a79
SA
1551static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1552
71a3c369
TT
1553static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1554
ff013f42 1555static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1556 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1557
3a2b436a 1558/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1559 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1560enum pc_bounds_kind
1561{
e385593e 1562 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1563 PC_BOUNDS_NOT_PRESENT,
1564
e385593e
JK
1565 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1566 were present but they do not form a valid range of PC addresses. */
1567 PC_BOUNDS_INVALID,
1568
3a2b436a
JK
1569 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1570 PC_BOUNDS_RANGES,
1571
1572 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1573 PC_BOUNDS_HIGH_LOW,
1574};
1575
1576static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1577 CORE_ADDR *, CORE_ADDR *,
1578 struct dwarf2_cu *,
891813be 1579 dwarf2_psymtab *);
c906108c 1580
fae299cd
DC
1581static void get_scope_pc_bounds (struct die_info *,
1582 CORE_ADDR *, CORE_ADDR *,
1583 struct dwarf2_cu *);
1584
801e3a5b
JB
1585static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1586 CORE_ADDR, struct dwarf2_cu *);
1587
a14ed312 1588static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1589 struct dwarf2_cu *);
c906108c 1590
a14ed312 1591static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1592 struct type *, struct dwarf2_cu *);
c906108c 1593
a14ed312 1594static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1595 struct die_info *, struct type *,
e7c27a73 1596 struct dwarf2_cu *);
c906108c 1597
a14ed312 1598static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1599 struct type *,
1600 struct dwarf2_cu *);
c906108c 1601
134d01f1 1602static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1603
e7c27a73 1604static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1605
e7c27a73 1606static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1607
5d7cb8df
JK
1608static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1609
804d2729 1610static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1611
27aa8d6a
SW
1612static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1613
74921315
KS
1614static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1615
f55ee35c
JK
1616static struct type *read_module_type (struct die_info *die,
1617 struct dwarf2_cu *cu);
1618
38d518c9 1619static const char *namespace_name (struct die_info *die,
e142c38c 1620 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1621
134d01f1 1622static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1623
e7c27a73 1624static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1625
6e70227d 1626static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1627 struct dwarf2_cu *);
1628
bf6af496 1629static struct die_info *read_die_and_siblings_1
d521ce57 1630 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1631 struct die_info *);
639d11d3 1632
dee91e82 1633static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1634 const gdb_byte *info_ptr,
1635 const gdb_byte **new_info_ptr,
639d11d3
DC
1636 struct die_info *parent);
1637
d521ce57
TT
1638static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1639 struct die_info **, const gdb_byte *,
3e225074 1640 int);
3019eac3 1641
d521ce57 1642static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1643 struct die_info **, const gdb_byte *);
93311388 1644
e7c27a73 1645static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1646
15d034d0
TT
1647static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1648 struct obstack *);
71c25dea 1649
15d034d0 1650static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1651
15d034d0 1652static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1653 struct die_info *die,
1654 struct dwarf2_cu *cu);
1655
ca69b9e6
DE
1656static const char *dwarf2_physname (const char *name, struct die_info *die,
1657 struct dwarf2_cu *cu);
1658
e142c38c 1659static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1660 struct dwarf2_cu **);
9219021c 1661
f39c6ffd 1662static const char *dwarf_tag_name (unsigned int);
c906108c 1663
f39c6ffd 1664static const char *dwarf_attr_name (unsigned int);
c906108c 1665
a084a2a6
AT
1666static const char *dwarf_unit_type_name (int unit_type);
1667
f39c6ffd 1668static const char *dwarf_form_name (unsigned int);
c906108c 1669
a121b7c1 1670static const char *dwarf_bool_name (unsigned int);
c906108c 1671
f39c6ffd 1672static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1673
f9aca02d 1674static struct die_info *sibling_die (struct die_info *);
c906108c 1675
d97bc12b
DE
1676static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1677
1678static void dump_die_for_error (struct die_info *);
1679
1680static void dump_die_1 (struct ui_file *, int level, int max_level,
1681 struct die_info *);
c906108c 1682
d97bc12b 1683/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1684
51545339 1685static void store_in_ref_table (struct die_info *,
10b3939b 1686 struct dwarf2_cu *);
c906108c 1687
ff39bb5e 1688static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1689
ff39bb5e 1690static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1691
348e048f 1692static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1693 const struct attribute *,
348e048f
DE
1694 struct dwarf2_cu **);
1695
10b3939b 1696static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1697 const struct attribute *,
f2f0e013 1698 struct dwarf2_cu **);
c906108c 1699
348e048f 1700static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1701 const struct attribute *,
348e048f
DE
1702 struct dwarf2_cu **);
1703
ac9ec31b
DE
1704static struct type *get_signatured_type (struct die_info *, ULONGEST,
1705 struct dwarf2_cu *);
1706
1707static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1708 const struct attribute *,
ac9ec31b
DE
1709 struct dwarf2_cu *);
1710
e5fe5e75 1711static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1712
52dc124a 1713static void read_signatured_type (struct signatured_type *);
348e048f 1714
63e43d3a
PMR
1715static int attr_to_dynamic_prop (const struct attribute *attr,
1716 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1717 struct dynamic_prop *prop, struct type *type);
63e43d3a 1718
c906108c
SS
1719/* memory allocation interface */
1720
7b5a2f43 1721static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1722
b60c80d6 1723static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1724
43f3e411 1725static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1726
8cf6f0b1
TT
1727static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1728 struct dwarf2_loclist_baton *baton,
ff39bb5e 1729 const struct attribute *attr);
8cf6f0b1 1730
ff39bb5e 1731static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1732 struct symbol *sym,
f1e6e072
TT
1733 struct dwarf2_cu *cu,
1734 int is_block);
4c2df51b 1735
d521ce57
TT
1736static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1737 const gdb_byte *info_ptr,
1738 struct abbrev_info *abbrev);
4bb7a0a7 1739
72bf9492
DJ
1740static hashval_t partial_die_hash (const void *item);
1741
1742static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1743
ae038cb0 1744static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1745 (sect_offset sect_off, unsigned int offset_in_dwz,
1746 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1747
9816fde3 1748static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1749 struct die_info *comp_unit_die,
1750 enum language pretend_language);
93311388 1751
ed2dc618 1752static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1753
dee91e82 1754static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1755
f792889a
DJ
1756static struct type *set_die_type (struct die_info *, struct type *,
1757 struct dwarf2_cu *);
1c379e20 1758
ed2dc618 1759static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1760
ed2dc618 1761static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1762
58f0c718 1763static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1764 enum language);
10b3939b 1765
95554aad
TT
1766static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1767 enum language);
10b3939b 1768
f4dc4d17
DE
1769static void process_full_type_unit (struct dwarf2_per_cu_data *,
1770 enum language);
1771
10b3939b
DJ
1772static void dwarf2_add_dependence (struct dwarf2_cu *,
1773 struct dwarf2_per_cu_data *);
1774
ae038cb0
DJ
1775static void dwarf2_mark (struct dwarf2_cu *);
1776
1777static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1778
b64f50a1 1779static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1780 struct dwarf2_per_cu_data *);
673bfd45 1781
f792889a 1782static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1783
95554aad
TT
1784static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1785 enum language pretend_language);
1786
ed2dc618 1787static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1788
9a49df9d
AB
1789static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1790static struct type *dwarf2_per_cu_addr_sized_int_type
1791 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1792static struct type *dwarf2_per_cu_int_type
1793 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1794 bool unsigned_p);
9a49df9d 1795
b303c6f6
AB
1796/* Class, the destructor of which frees all allocated queue entries. This
1797 will only have work to do if an error was thrown while processing the
1798 dwarf. If no error was thrown then the queue entries should have all
1799 been processed, and freed, as we went along. */
1800
1801class dwarf2_queue_guard
1802{
1803public:
39856def
TT
1804 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1805 : m_per_objfile (per_objfile)
1806 {
1807 }
b303c6f6
AB
1808
1809 /* Free any entries remaining on the queue. There should only be
1810 entries left if we hit an error while processing the dwarf. */
1811 ~dwarf2_queue_guard ()
1812 {
39856def
TT
1813 /* Ensure that no memory is allocated by the queue. */
1814 std::queue<dwarf2_queue_item> empty;
1815 std::swap (m_per_objfile->queue, empty);
1816 }
b303c6f6 1817
39856def 1818 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1819
39856def
TT
1820private:
1821 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1822};
1823
39856def
TT
1824dwarf2_queue_item::~dwarf2_queue_item ()
1825{
1826 /* Anything still marked queued is likely to be in an
1827 inconsistent state, so discard it. */
1828 if (per_cu->queued)
1829 {
1830 if (per_cu->cu != NULL)
1831 free_one_cached_comp_unit (per_cu);
1832 per_cu->queued = 0;
1833 }
1834}
1835
d721ba37
PA
1836/* The return type of find_file_and_directory. Note, the enclosed
1837 string pointers are only valid while this object is valid. */
1838
1839struct file_and_directory
1840{
1841 /* The filename. This is never NULL. */
1842 const char *name;
1843
1844 /* The compilation directory. NULL if not known. If we needed to
1845 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1846 points directly to the DW_AT_comp_dir string attribute owned by
1847 the obstack that owns the DIE. */
1848 const char *comp_dir;
1849
1850 /* If we needed to build a new string for comp_dir, this is what
1851 owns the storage. */
1852 std::string comp_dir_storage;
1853};
1854
1855static file_and_directory find_file_and_directory (struct die_info *die,
1856 struct dwarf2_cu *cu);
9291a0cd
TT
1857
1858static char *file_full_name (int file, struct line_header *lh,
1859 const char *comp_dir);
1860
43988095
JK
1861/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1862enum class rcuh_kind { COMPILE, TYPE };
1863
d521ce57 1864static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1865 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1866 struct comp_unit_head *header,
36586728 1867 struct dwarf2_section_info *section,
d521ce57 1868 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1869 rcuh_kind section_kind);
36586728 1870
673bfd45 1871static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1872
3019eac3
DE
1873static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1874
57d63ce2 1875static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1876 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1877 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1878 ULONGEST signature, int is_debug_types);
a2ce51a0 1879
ed2dc618
SM
1880static struct dwp_file *get_dwp_file
1881 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1882
3019eac3 1883static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1884 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1885
1886static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1887 (struct signatured_type *, const char *, const char *);
3019eac3 1888
89e63ee4
DE
1889static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1890
263db9a1
TT
1891/* A unique pointer to a dwo_file. */
1892
51ac9db5 1893typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1894
ed2dc618 1895static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1896
1b80a9fa 1897static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1898
1899static void free_line_header_voidp (void *arg);
4390d890
DE
1900\f
1901/* Various complaints about symbol reading that don't abort the process. */
1902
1903static void
1904dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1905{
b98664d3 1906 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1907}
1908
1909static void
1910dwarf2_debug_line_missing_file_complaint (void)
1911{
b98664d3 1912 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1913}
1914
1915static void
1916dwarf2_debug_line_missing_end_sequence_complaint (void)
1917{
b98664d3 1918 complaint (_(".debug_line section has line "
4390d890
DE
1919 "program sequence without an end"));
1920}
1921
1922static void
1923dwarf2_complex_location_expr_complaint (void)
1924{
b98664d3 1925 complaint (_("location expression too complex"));
4390d890
DE
1926}
1927
1928static void
1929dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1930 int arg3)
1931{
b98664d3 1932 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1933 arg1, arg2, arg3);
1934}
1935
1936static void
1937dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1938{
b98664d3 1939 complaint (_("debug info runs off end of %s section"
4390d890 1940 " [in module %s]"),
96b79293
TT
1941 section->get_name (),
1942 section->get_file_name ());
4390d890 1943}
1b80a9fa 1944
4390d890
DE
1945static void
1946dwarf2_macro_malformed_definition_complaint (const char *arg1)
1947{
b98664d3 1948 complaint (_("macro debug info contains a "
4390d890
DE
1949 "malformed macro definition:\n`%s'"),
1950 arg1);
1951}
1952
1953static void
1954dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1955{
b98664d3 1956 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1957 arg1, arg2);
1958}
527f3840
JK
1959
1960/* Hash function for line_header_hash. */
1961
1962static hashval_t
1963line_header_hash (const struct line_header *ofs)
1964{
9c541725 1965 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1966}
1967
1968/* Hash function for htab_create_alloc_ex for line_header_hash. */
1969
1970static hashval_t
1971line_header_hash_voidp (const void *item)
1972{
9a3c8263 1973 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1974
1975 return line_header_hash (ofs);
1976}
1977
1978/* Equality function for line_header_hash. */
1979
1980static int
1981line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1982{
9a3c8263
SM
1983 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1984 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1985
9c541725 1986 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1987 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1988}
1989
4390d890 1990\f
9291a0cd 1991
330cdd98
PA
1992/* See declaration. */
1993
1994dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1995 const dwarf2_debug_sections *names,
1996 bool can_copy_)
1997 : objfile (objfile_),
1998 can_copy (can_copy_)
330cdd98
PA
1999{
2000 if (names == NULL)
2001 names = &dwarf2_elf_names;
2002
2003 bfd *obfd = objfile->obfd;
2004
2005 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2006 locate_sections (obfd, sec, *names);
2007}
2008
2009dwarf2_per_objfile::~dwarf2_per_objfile ()
2010{
2011 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2012 free_cached_comp_units ();
2013
2014 if (quick_file_names_table)
2015 htab_delete (quick_file_names_table);
2016
2017 if (line_header_hash)
2018 htab_delete (line_header_hash);
2019
b76e467d 2020 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2021 per_cu->imported_symtabs_free ();
fc8e7e75 2022
b2bdb8cf 2023 for (signatured_type *sig_type : all_type_units)
ae640021 2024 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2025
330cdd98
PA
2026 /* Everything else should be on the objfile obstack. */
2027}
2028
2029/* See declaration. */
2030
2031void
2032dwarf2_per_objfile::free_cached_comp_units ()
2033{
2034 dwarf2_per_cu_data *per_cu = read_in_chain;
2035 dwarf2_per_cu_data **last_chain = &read_in_chain;
2036 while (per_cu != NULL)
2037 {
2038 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2039
fcd3b13d 2040 delete per_cu->cu;
330cdd98
PA
2041 *last_chain = next_cu;
2042 per_cu = next_cu;
2043 }
2044}
2045
11ed8cad
TT
2046/* A helper class that calls free_cached_comp_units on
2047 destruction. */
2048
2049class free_cached_comp_units
2050{
2051public:
2052
2053 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2054 : m_per_objfile (per_objfile)
2055 {
2056 }
2057
2058 ~free_cached_comp_units ()
2059 {
2060 m_per_objfile->free_cached_comp_units ();
2061 }
2062
2063 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2064
2065private:
2066
2067 dwarf2_per_objfile *m_per_objfile;
2068};
2069
c906108c 2070/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2071 information and return true if we have enough to do something.
2072 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2073 ELF names are used. CAN_COPY is true for formats where symbol
2074 interposition is possible and so symbol values must follow copy
2075 relocation rules. */
c906108c
SS
2076
2077int
251d32d9 2078dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2079 const struct dwarf2_debug_sections *names,
2080 bool can_copy)
c906108c 2081{
97cbe998
SDJ
2082 if (objfile->flags & OBJF_READNEVER)
2083 return 0;
2084
ed2dc618
SM
2085 struct dwarf2_per_objfile *dwarf2_per_objfile
2086 = get_dwarf2_per_objfile (objfile);
2087
2088 if (dwarf2_per_objfile == NULL)
5bfd760d 2089 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2090 names,
2091 can_copy);
5bfd760d 2092
73869dc2 2093 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2094 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2095 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2096 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2097}
2098
251d32d9
TG
2099/* When loading sections, we look either for uncompressed section or for
2100 compressed section names. */
233a11ab
CS
2101
2102static int
251d32d9
TG
2103section_is_p (const char *section_name,
2104 const struct dwarf2_section_names *names)
233a11ab 2105{
251d32d9
TG
2106 if (names->normal != NULL
2107 && strcmp (section_name, names->normal) == 0)
2108 return 1;
2109 if (names->compressed != NULL
2110 && strcmp (section_name, names->compressed) == 0)
2111 return 1;
2112 return 0;
233a11ab
CS
2113}
2114
330cdd98 2115/* See declaration. */
c906108c 2116
330cdd98
PA
2117void
2118dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2119 const dwarf2_debug_sections &names)
c906108c 2120{
fd361982 2121 flagword aflag = bfd_section_flags (sectp);
251d32d9 2122
dc7650b8
JK
2123 if ((aflag & SEC_HAS_CONTENTS) == 0)
2124 {
2125 }
950b7495
KS
2126 else if (elf_section_data (sectp)->this_hdr.sh_size
2127 > bfd_get_file_size (abfd))
2128 {
2129 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2130 warning (_("Discarding section %s which has a section size (%s"
2131 ") larger than the file size [in module %s]"),
2132 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2133 bfd_get_filename (abfd));
2134 }
330cdd98 2135 else if (section_is_p (sectp->name, &names.info))
c906108c 2136 {
330cdd98 2137 this->info.s.section = sectp;
fd361982 2138 this->info.size = bfd_section_size (sectp);
c906108c 2139 }
330cdd98 2140 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2141 {
330cdd98 2142 this->abbrev.s.section = sectp;
fd361982 2143 this->abbrev.size = bfd_section_size (sectp);
c906108c 2144 }
330cdd98 2145 else if (section_is_p (sectp->name, &names.line))
c906108c 2146 {
330cdd98 2147 this->line.s.section = sectp;
fd361982 2148 this->line.size = bfd_section_size (sectp);
c906108c 2149 }
330cdd98 2150 else if (section_is_p (sectp->name, &names.loc))
c906108c 2151 {
330cdd98 2152 this->loc.s.section = sectp;
fd361982 2153 this->loc.size = bfd_section_size (sectp);
c906108c 2154 }
330cdd98 2155 else if (section_is_p (sectp->name, &names.loclists))
43988095 2156 {
330cdd98 2157 this->loclists.s.section = sectp;
fd361982 2158 this->loclists.size = bfd_section_size (sectp);
43988095 2159 }
330cdd98 2160 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2161 {
330cdd98 2162 this->macinfo.s.section = sectp;
fd361982 2163 this->macinfo.size = bfd_section_size (sectp);
c906108c 2164 }
330cdd98 2165 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2166 {
330cdd98 2167 this->macro.s.section = sectp;
fd361982 2168 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2169 }
330cdd98 2170 else if (section_is_p (sectp->name, &names.str))
c906108c 2171 {
330cdd98 2172 this->str.s.section = sectp;
fd361982 2173 this->str.size = bfd_section_size (sectp);
c906108c 2174 }
18a8505e
AT
2175 else if (section_is_p (sectp->name, &names.str_offsets))
2176 {
2177 this->str_offsets.s.section = sectp;
2178 this->str_offsets.size = bfd_section_size (sectp);
2179 }
330cdd98 2180 else if (section_is_p (sectp->name, &names.line_str))
43988095 2181 {
330cdd98 2182 this->line_str.s.section = sectp;
fd361982 2183 this->line_str.size = bfd_section_size (sectp);
43988095 2184 }
330cdd98 2185 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2186 {
330cdd98 2187 this->addr.s.section = sectp;
fd361982 2188 this->addr.size = bfd_section_size (sectp);
3019eac3 2189 }
330cdd98 2190 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2191 {
330cdd98 2192 this->frame.s.section = sectp;
fd361982 2193 this->frame.size = bfd_section_size (sectp);
b6af0555 2194 }
330cdd98 2195 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2196 {
330cdd98 2197 this->eh_frame.s.section = sectp;
fd361982 2198 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2199 }
330cdd98 2200 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2201 {
330cdd98 2202 this->ranges.s.section = sectp;
fd361982 2203 this->ranges.size = bfd_section_size (sectp);
af34e669 2204 }
330cdd98 2205 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2206 {
330cdd98 2207 this->rnglists.s.section = sectp;
fd361982 2208 this->rnglists.size = bfd_section_size (sectp);
43988095 2209 }
330cdd98 2210 else if (section_is_p (sectp->name, &names.types))
348e048f 2211 {
8b70b953
TT
2212 struct dwarf2_section_info type_section;
2213
2214 memset (&type_section, 0, sizeof (type_section));
049412e3 2215 type_section.s.section = sectp;
fd361982 2216 type_section.size = bfd_section_size (sectp);
8b70b953 2217
fd5866f6 2218 this->types.push_back (type_section);
348e048f 2219 }
330cdd98 2220 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2221 {
330cdd98 2222 this->gdb_index.s.section = sectp;
fd361982 2223 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2224 }
927aa2e7
JK
2225 else if (section_is_p (sectp->name, &names.debug_names))
2226 {
2227 this->debug_names.s.section = sectp;
fd361982 2228 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2229 }
2230 else if (section_is_p (sectp->name, &names.debug_aranges))
2231 {
2232 this->debug_aranges.s.section = sectp;
fd361982 2233 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2234 }
dce234bc 2235
fd361982
AM
2236 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2237 && bfd_section_vma (sectp) == 0)
330cdd98 2238 this->has_section_at_zero = true;
c906108c
SS
2239}
2240
9e0ac564
TT
2241/* A helper function that returns the size of a section in a safe way.
2242 If you are positive that the section has been read before using the
2243 size, then it is safe to refer to the dwarf2_section_info object's
2244 "size" field directly. In other cases, you must call this
2245 function, because for compressed sections the size field is not set
2246 correctly until the section has been read. */
2247
2248static bfd_size_type
2249dwarf2_section_size (struct objfile *objfile,
2250 struct dwarf2_section_info *info)
2251{
2252 if (!info->readin)
96b79293 2253 info->read (objfile);
9e0ac564
TT
2254 return info->size;
2255}
2256
dce234bc 2257/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2258 SECTION_NAME. */
af34e669 2259
dce234bc 2260void
3017a003
TG
2261dwarf2_get_section_info (struct objfile *objfile,
2262 enum dwarf2_section_enum sect,
d521ce57 2263 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2264 bfd_size_type *sizep)
2265{
5bfd760d 2266 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2267 struct dwarf2_section_info *info;
a3b2a86b
TT
2268
2269 /* We may see an objfile without any DWARF, in which case we just
2270 return nothing. */
2271 if (data == NULL)
2272 {
2273 *sectp = NULL;
2274 *bufp = NULL;
2275 *sizep = 0;
2276 return;
2277 }
3017a003
TG
2278 switch (sect)
2279 {
2280 case DWARF2_DEBUG_FRAME:
2281 info = &data->frame;
2282 break;
2283 case DWARF2_EH_FRAME:
2284 info = &data->eh_frame;
2285 break;
2286 default:
2287 gdb_assert_not_reached ("unexpected section");
2288 }
dce234bc 2289
96b79293 2290 info->read (objfile);
dce234bc 2291
96b79293 2292 *sectp = info->get_bfd_section ();
dce234bc
PP
2293 *bufp = info->buffer;
2294 *sizep = info->size;
2295}
2296
36586728
TT
2297/* A helper function to find the sections for a .dwz file. */
2298
2299static void
2300locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2301{
9a3c8263 2302 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2303
2304 /* Note that we only support the standard ELF names, because .dwz
2305 is ELF-only (at the time of writing). */
2306 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2307 {
049412e3 2308 dwz_file->abbrev.s.section = sectp;
fd361982 2309 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2310 }
2311 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2312 {
049412e3 2313 dwz_file->info.s.section = sectp;
fd361982 2314 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2315 }
2316 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2317 {
049412e3 2318 dwz_file->str.s.section = sectp;
fd361982 2319 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2320 }
2321 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2322 {
049412e3 2323 dwz_file->line.s.section = sectp;
fd361982 2324 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2325 }
2326 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2327 {
049412e3 2328 dwz_file->macro.s.section = sectp;
fd361982 2329 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2330 }
2ec9a5e0
TT
2331 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2332 {
049412e3 2333 dwz_file->gdb_index.s.section = sectp;
fd361982 2334 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2335 }
927aa2e7
JK
2336 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2337 {
2338 dwz_file->debug_names.s.section = sectp;
fd361982 2339 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2340 }
36586728
TT
2341}
2342
c4973306 2343/* See dwarf2read.h. */
36586728 2344
c4973306 2345struct dwz_file *
ed2dc618 2346dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2347{
36586728 2348 const char *filename;
acd13123 2349 bfd_size_type buildid_len_arg;
dc294be5
TT
2350 size_t buildid_len;
2351 bfd_byte *buildid;
36586728
TT
2352
2353 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2354 return dwarf2_per_objfile->dwz_file.get ();
36586728 2355
4db1a1dc 2356 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2357 gdb::unique_xmalloc_ptr<char> data
2358 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2359 &buildid_len_arg, &buildid));
4db1a1dc
TT
2360 if (data == NULL)
2361 {
2362 if (bfd_get_error () == bfd_error_no_error)
2363 return NULL;
2364 error (_("could not read '.gnu_debugaltlink' section: %s"),
2365 bfd_errmsg (bfd_get_error ()));
2366 }
791afaa2
TT
2367
2368 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2369
acd13123
TT
2370 buildid_len = (size_t) buildid_len_arg;
2371
791afaa2 2372 filename = data.get ();
d721ba37
PA
2373
2374 std::string abs_storage;
36586728
TT
2375 if (!IS_ABSOLUTE_PATH (filename))
2376 {
14278e1f
TT
2377 gdb::unique_xmalloc_ptr<char> abs
2378 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2379
14278e1f 2380 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2381 filename = abs_storage.c_str ();
36586728
TT
2382 }
2383
dc294be5
TT
2384 /* First try the file name given in the section. If that doesn't
2385 work, try to use the build-id instead. */
192b62ce 2386 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2387 if (dwz_bfd != NULL)
36586728 2388 {
192b62ce 2389 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2390 dwz_bfd.reset (nullptr);
36586728
TT
2391 }
2392
dc294be5
TT
2393 if (dwz_bfd == NULL)
2394 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2395
2396 if (dwz_bfd == NULL)
2397 error (_("could not find '.gnu_debugaltlink' file for %s"),
2398 objfile_name (dwarf2_per_objfile->objfile));
2399
7ff8cb8c
TT
2400 std::unique_ptr<struct dwz_file> result
2401 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2402
7ff8cb8c
TT
2403 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2404 result.get ());
36586728 2405
7ff8cb8c
TT
2406 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2407 result->dwz_bfd.get ());
2408 dwarf2_per_objfile->dwz_file = std::move (result);
2409 return dwarf2_per_objfile->dwz_file.get ();
36586728 2410}
9291a0cd 2411\f
7b9f3c50
DE
2412/* DWARF quick_symbols_functions support. */
2413
2414/* TUs can share .debug_line entries, and there can be a lot more TUs than
2415 unique line tables, so we maintain a separate table of all .debug_line
2416 derived entries to support the sharing.
2417 All the quick functions need is the list of file names. We discard the
2418 line_header when we're done and don't need to record it here. */
2419struct quick_file_names
2420{
094b34ac
DE
2421 /* The data used to construct the hash key. */
2422 struct stmt_list_hash hash;
7b9f3c50
DE
2423
2424 /* The number of entries in file_names, real_names. */
2425 unsigned int num_file_names;
2426
2427 /* The file names from the line table, after being run through
2428 file_full_name. */
2429 const char **file_names;
2430
2431 /* The file names from the line table after being run through
2432 gdb_realpath. These are computed lazily. */
2433 const char **real_names;
2434};
2435
2436/* When using the index (and thus not using psymtabs), each CU has an
2437 object of this type. This is used to hold information needed by
2438 the various "quick" methods. */
2439struct dwarf2_per_cu_quick_data
2440{
2441 /* The file table. This can be NULL if there was no file table
2442 or it's currently not read in.
2443 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2444 struct quick_file_names *file_names;
2445
2446 /* The corresponding symbol table. This is NULL if symbols for this
2447 CU have not yet been read. */
43f3e411 2448 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2449
2450 /* A temporary mark bit used when iterating over all CUs in
2451 expand_symtabs_matching. */
2452 unsigned int mark : 1;
2453
2454 /* True if we've tried to read the file table and found there isn't one.
2455 There will be no point in trying to read it again next time. */
2456 unsigned int no_file_data : 1;
2457};
2458
094b34ac
DE
2459/* Utility hash function for a stmt_list_hash. */
2460
2461static hashval_t
2462hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2463{
2464 hashval_t v = 0;
2465
2466 if (stmt_list_hash->dwo_unit != NULL)
2467 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2468 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2469 return v;
2470}
2471
2472/* Utility equality function for a stmt_list_hash. */
2473
2474static int
2475eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2476 const struct stmt_list_hash *rhs)
2477{
2478 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2479 return 0;
2480 if (lhs->dwo_unit != NULL
2481 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2482 return 0;
2483
9c541725 2484 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2485}
2486
7b9f3c50
DE
2487/* Hash function for a quick_file_names. */
2488
2489static hashval_t
2490hash_file_name_entry (const void *e)
2491{
9a3c8263
SM
2492 const struct quick_file_names *file_data
2493 = (const struct quick_file_names *) e;
7b9f3c50 2494
094b34ac 2495 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2496}
2497
2498/* Equality function for a quick_file_names. */
2499
2500static int
2501eq_file_name_entry (const void *a, const void *b)
2502{
9a3c8263
SM
2503 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2504 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2505
094b34ac 2506 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2507}
2508
2509/* Delete function for a quick_file_names. */
2510
2511static void
2512delete_file_name_entry (void *e)
2513{
9a3c8263 2514 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2515 int i;
2516
2517 for (i = 0; i < file_data->num_file_names; ++i)
2518 {
2519 xfree ((void*) file_data->file_names[i]);
2520 if (file_data->real_names)
2521 xfree ((void*) file_data->real_names[i]);
2522 }
2523
2524 /* The space for the struct itself lives on objfile_obstack,
2525 so we don't free it here. */
2526}
2527
2528/* Create a quick_file_names hash table. */
2529
2530static htab_t
2531create_quick_file_names_table (unsigned int nr_initial_entries)
2532{
2533 return htab_create_alloc (nr_initial_entries,
2534 hash_file_name_entry, eq_file_name_entry,
2535 delete_file_name_entry, xcalloc, xfree);
2536}
9291a0cd 2537
918dd910
JK
2538/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2539 have to be created afterwards. You should call age_cached_comp_units after
2540 processing PER_CU->CU. dw2_setup must have been already called. */
2541
2542static void
58f0c718 2543load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2544{
3019eac3 2545 if (per_cu->is_debug_types)
e5fe5e75 2546 load_full_type_unit (per_cu);
918dd910 2547 else
58f0c718 2548 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2549
cc12ce38
DE
2550 if (per_cu->cu == NULL)
2551 return; /* Dummy CU. */
2dc860c0
DE
2552
2553 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2554}
2555
a0f42c21 2556/* Read in the symbols for PER_CU. */
2fdf6df6 2557
9291a0cd 2558static void
58f0c718 2559dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2560{
ed2dc618 2561 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2562
f4dc4d17
DE
2563 /* Skip type_unit_groups, reading the type units they contain
2564 is handled elsewhere. */
2565 if (IS_TYPE_UNIT_GROUP (per_cu))
2566 return;
2567
b303c6f6
AB
2568 /* The destructor of dwarf2_queue_guard frees any entries left on
2569 the queue. After this point we're guaranteed to leave this function
2570 with the dwarf queue empty. */
39856def 2571 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2572
95554aad 2573 if (dwarf2_per_objfile->using_index
43f3e411 2574 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2575 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2576 {
2577 queue_comp_unit (per_cu, language_minimal);
58f0c718 2578 load_cu (per_cu, skip_partial);
89e63ee4
DE
2579
2580 /* If we just loaded a CU from a DWO, and we're working with an index
2581 that may badly handle TUs, load all the TUs in that DWO as well.
2582 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2583 if (!per_cu->is_debug_types
cc12ce38 2584 && per_cu->cu != NULL
89e63ee4
DE
2585 && per_cu->cu->dwo_unit != NULL
2586 && dwarf2_per_objfile->index_table != NULL
2587 && dwarf2_per_objfile->index_table->version <= 7
2588 /* DWP files aren't supported yet. */
ed2dc618 2589 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2590 queue_and_load_all_dwo_tus (per_cu);
95554aad 2591 }
9291a0cd 2592
ed2dc618 2593 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2594
2595 /* Age the cache, releasing compilation units that have not
2596 been used recently. */
ed2dc618 2597 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2598}
2599
2600/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2601 the objfile from which this CU came. Returns the resulting symbol
2602 table. */
2fdf6df6 2603
43f3e411 2604static struct compunit_symtab *
58f0c718 2605dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2606{
ed2dc618
SM
2607 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2608
95554aad 2609 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2610 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2611 {
11ed8cad 2612 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2613 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2614 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2615 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2616 }
f194fefb 2617
43f3e411 2618 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2619}
2620
ff4c9fec 2621/* See declaration. */
f4dc4d17 2622
ff4c9fec
SM
2623dwarf2_per_cu_data *
2624dwarf2_per_objfile::get_cutu (int index)
2625{
b76e467d 2626 if (index >= this->all_comp_units.size ())
ff4c9fec 2627 {
b76e467d 2628 index -= this->all_comp_units.size ();
b2bdb8cf 2629 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2630 return &this->all_type_units[index]->per_cu;
2631 }
f4dc4d17 2632
ff4c9fec
SM
2633 return this->all_comp_units[index];
2634}
f4dc4d17 2635
ff4c9fec 2636/* See declaration. */
2fdf6df6 2637
ff4c9fec
SM
2638dwarf2_per_cu_data *
2639dwarf2_per_objfile::get_cu (int index)
1fd400ff 2640{
b76e467d 2641 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2642
ff4c9fec 2643 return this->all_comp_units[index];
f4dc4d17
DE
2644}
2645
ff4c9fec 2646/* See declaration. */
f4dc4d17 2647
ff4c9fec
SM
2648signatured_type *
2649dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2650{
b2bdb8cf 2651 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2652
ff4c9fec 2653 return this->all_type_units[index];
1fd400ff
TT
2654}
2655
4b514bc8
JK
2656/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2657 objfile_obstack, and constructed with the specified field
2658 values. */
2659
2660static dwarf2_per_cu_data *
ed2dc618 2661create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2662 struct dwarf2_section_info *section,
2663 int is_dwz,
2664 sect_offset sect_off, ULONGEST length)
2665{
ed2dc618 2666 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2667 dwarf2_per_cu_data *the_cu
2668 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2669 struct dwarf2_per_cu_data);
2670 the_cu->sect_off = sect_off;
2671 the_cu->length = length;
e3b94546 2672 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2673 the_cu->section = section;
2674 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2675 struct dwarf2_per_cu_quick_data);
2676 the_cu->is_dwz = is_dwz;
2677 return the_cu;
2678}
2679
2ec9a5e0
TT
2680/* A helper for create_cus_from_index that handles a given list of
2681 CUs. */
2fdf6df6 2682
74a0d9f6 2683static void
12359b5e 2684create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2685 const gdb_byte *cu_list, offset_type n_elements,
2686 struct dwarf2_section_info *section,
b76e467d 2687 int is_dwz)
9291a0cd 2688{
12359b5e 2689 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2690 {
74a0d9f6 2691 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2692
2693 sect_offset sect_off
2694 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2695 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2696 cu_list += 2 * 8;
2697
b76e467d 2698 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2699 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2700 sect_off, length);
b76e467d 2701 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2702 }
9291a0cd
TT
2703}
2704
2ec9a5e0 2705/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2706 the CU objects for this objfile. */
2ec9a5e0 2707
74a0d9f6 2708static void
12359b5e 2709create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2710 const gdb_byte *cu_list, offset_type cu_list_elements,
2711 const gdb_byte *dwz_list, offset_type dwz_elements)
2712{
b76e467d
SM
2713 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2714 dwarf2_per_objfile->all_comp_units.reserve
2715 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2716
12359b5e 2717 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2718 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2719
2720 if (dwz_elements == 0)
74a0d9f6 2721 return;
2ec9a5e0 2722
12359b5e
SM
2723 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2724 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2725 &dwz->info, 1);
2ec9a5e0
TT
2726}
2727
1fd400ff 2728/* Create the signatured type hash table from the index. */
673bfd45 2729
74a0d9f6 2730static void
12359b5e
SM
2731create_signatured_type_table_from_index
2732 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2733 struct dwarf2_section_info *section,
2734 const gdb_byte *bytes,
2735 offset_type elements)
1fd400ff 2736{
12359b5e 2737 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2738
b2bdb8cf
SM
2739 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2740 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2741
12359b5e 2742 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 2743
12359b5e 2744 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2745 {
52dc124a 2746 struct signatured_type *sig_type;
9c541725 2747 ULONGEST signature;
1fd400ff 2748 void **slot;
9c541725 2749 cu_offset type_offset_in_tu;
1fd400ff 2750
74a0d9f6 2751 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2752 sect_offset sect_off
2753 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2754 type_offset_in_tu
2755 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2756 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2757 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2758 bytes += 3 * 8;
2759
52dc124a 2760 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2761 struct signatured_type);
52dc124a 2762 sig_type->signature = signature;
9c541725 2763 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2764 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2765 sig_type->per_cu.section = section;
9c541725 2766 sig_type->per_cu.sect_off = sect_off;
e3b94546 2767 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2768 sig_type->per_cu.v.quick
1fd400ff
TT
2769 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2770 struct dwarf2_per_cu_quick_data);
2771
52dc124a
DE
2772 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2773 *slot = sig_type;
1fd400ff 2774
b2bdb8cf 2775 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2776 }
2777
673bfd45 2778 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2779}
2780
927aa2e7
JK
2781/* Create the signatured type hash table from .debug_names. */
2782
2783static void
2784create_signatured_type_table_from_debug_names
ed2dc618 2785 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2786 const mapped_debug_names &map,
2787 struct dwarf2_section_info *section,
2788 struct dwarf2_section_info *abbrev_section)
2789{
ed2dc618
SM
2790 struct objfile *objfile = dwarf2_per_objfile->objfile;
2791
96b79293
TT
2792 section->read (objfile);
2793 abbrev_section->read (objfile);
927aa2e7 2794
b2bdb8cf
SM
2795 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2796 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
2797
2798 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2799
2800 for (uint32_t i = 0; i < map.tu_count; ++i)
2801 {
2802 struct signatured_type *sig_type;
927aa2e7 2803 void **slot;
927aa2e7
JK
2804
2805 sect_offset sect_off
2806 = (sect_offset) (extract_unsigned_integer
2807 (map.tu_table_reordered + i * map.offset_size,
2808 map.offset_size,
2809 map.dwarf5_byte_order));
2810
2811 comp_unit_head cu_header;
ed2dc618
SM
2812 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2813 abbrev_section,
927aa2e7
JK
2814 section->buffer + to_underlying (sect_off),
2815 rcuh_kind::TYPE);
2816
2817 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2818 struct signatured_type);
2819 sig_type->signature = cu_header.signature;
2820 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2821 sig_type->per_cu.is_debug_types = 1;
2822 sig_type->per_cu.section = section;
2823 sig_type->per_cu.sect_off = sect_off;
e3b94546 2824 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2825 sig_type->per_cu.v.quick
2826 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2827 struct dwarf2_per_cu_quick_data);
2828
2829 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2830 *slot = sig_type;
2831
b2bdb8cf 2832 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2833 }
2834
2835 dwarf2_per_objfile->signatured_types = sig_types_hash;
2836}
2837
9291a0cd
TT
2838/* Read the address map data from the mapped index, and use it to
2839 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2840
9291a0cd 2841static void
ed2dc618
SM
2842create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2843 struct mapped_index *index)
9291a0cd 2844{
ed2dc618 2845 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2847 const gdb_byte *iter, *end;
9291a0cd 2848 struct addrmap *mutable_map;
9291a0cd
TT
2849 CORE_ADDR baseaddr;
2850
8268c778
PA
2851 auto_obstack temp_obstack;
2852
9291a0cd
TT
2853 mutable_map = addrmap_create_mutable (&temp_obstack);
2854
f00a2de2
PA
2855 iter = index->address_table.data ();
2856 end = iter + index->address_table.size ();
9291a0cd 2857
b3b3bada 2858 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2859
2860 while (iter < end)
2861 {
2862 ULONGEST hi, lo, cu_index;
2863 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2864 iter += 8;
2865 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2866 iter += 8;
2867 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2868 iter += 4;
f652bce2 2869
24a55014 2870 if (lo > hi)
f652bce2 2871 {
b98664d3 2872 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2873 hex_string (lo), hex_string (hi));
24a55014 2874 continue;
f652bce2 2875 }
24a55014 2876
b76e467d 2877 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2878 {
b98664d3 2879 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2880 (unsigned) cu_index);
24a55014 2881 continue;
f652bce2 2882 }
24a55014 2883
79748972
TT
2884 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2885 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2886 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2887 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2888 }
2889
d320c2b5 2890 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2891 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2892}
2893
927aa2e7
JK
2894/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2895 populate the objfile's psymtabs_addrmap. */
2896
2897static void
ed2dc618 2898create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2899 struct dwarf2_section_info *section)
2900{
ed2dc618 2901 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2902 bfd *abfd = objfile->obfd;
2903 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2904 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2905
2906 auto_obstack temp_obstack;
2907 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2908
2909 std::unordered_map<sect_offset,
2910 dwarf2_per_cu_data *,
2911 gdb::hash_enum<sect_offset>>
2912 debug_info_offset_to_per_cu;
b76e467d 2913 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2914 {
927aa2e7
JK
2915 const auto insertpair
2916 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2917 if (!insertpair.second)
2918 {
2919 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2920 "debug_info_offset %s, ignoring .debug_aranges."),
2921 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2922 return;
2923 }
2924 }
2925
96b79293 2926 section->read (objfile);
927aa2e7
JK
2927
2928 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2929
2930 const gdb_byte *addr = section->buffer;
2931
2932 while (addr < section->buffer + section->size)
2933 {
2934 const gdb_byte *const entry_addr = addr;
2935 unsigned int bytes_read;
2936
2937 const LONGEST entry_length = read_initial_length (abfd, addr,
2938 &bytes_read);
2939 addr += bytes_read;
2940
2941 const gdb_byte *const entry_end = addr + entry_length;
2942 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2943 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2944 if (addr + entry_length > section->buffer + section->size)
2945 {
47e3f474 2946 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2947 "length %s exceeds section length %s, "
2948 "ignoring .debug_aranges."),
47e3f474
TV
2949 objfile_name (objfile),
2950 plongest (entry_addr - section->buffer),
927aa2e7
JK
2951 plongest (bytes_read + entry_length),
2952 pulongest (section->size));
2953 return;
2954 }
2955
2956 /* The version number. */
2957 const uint16_t version = read_2_bytes (abfd, addr);
2958 addr += 2;
2959 if (version != 2)
2960 {
47e3f474 2961 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2962 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2963 objfile_name (objfile),
2964 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2965 return;
2966 }
2967
2968 const uint64_t debug_info_offset
2969 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2970 addr += offset_size;
2971 const auto per_cu_it
2972 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2973 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2974 {
47e3f474 2975 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2976 "debug_info_offset %s does not exists, "
2977 "ignoring .debug_aranges."),
47e3f474
TV
2978 objfile_name (objfile),
2979 plongest (entry_addr - section->buffer),
927aa2e7
JK
2980 pulongest (debug_info_offset));
2981 return;
2982 }
2983 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2984
2985 const uint8_t address_size = *addr++;
2986 if (address_size < 1 || address_size > 8)
2987 {
47e3f474 2988 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2989 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2990 objfile_name (objfile),
2991 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2992 return;
2993 }
2994
2995 const uint8_t segment_selector_size = *addr++;
2996 if (segment_selector_size != 0)
2997 {
47e3f474 2998 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2999 "segment_selector_size %u is not supported, "
3000 "ignoring .debug_aranges."),
47e3f474
TV
3001 objfile_name (objfile),
3002 plongest (entry_addr - section->buffer),
927aa2e7
JK
3003 segment_selector_size);
3004 return;
3005 }
3006
3007 /* Must pad to an alignment boundary that is twice the address
3008 size. It is undocumented by the DWARF standard but GCC does
3009 use it. */
3010 for (size_t padding = ((-(addr - section->buffer))
3011 & (2 * address_size - 1));
3012 padding > 0; padding--)
3013 if (*addr++ != 0)
3014 {
47e3f474 3015 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3016 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3017 objfile_name (objfile),
3018 plongest (entry_addr - section->buffer));
927aa2e7
JK
3019 return;
3020 }
3021
3022 for (;;)
3023 {
3024 if (addr + 2 * address_size > entry_end)
3025 {
47e3f474 3026 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3027 "address list is not properly terminated, "
3028 "ignoring .debug_aranges."),
47e3f474
TV
3029 objfile_name (objfile),
3030 plongest (entry_addr - section->buffer));
927aa2e7
JK
3031 return;
3032 }
3033 ULONGEST start = extract_unsigned_integer (addr, address_size,
3034 dwarf5_byte_order);
3035 addr += address_size;
3036 ULONGEST length = extract_unsigned_integer (addr, address_size,
3037 dwarf5_byte_order);
3038 addr += address_size;
3039 if (start == 0 && length == 0)
3040 break;
3041 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3042 {
3043 /* Symbol was eliminated due to a COMDAT group. */
3044 continue;
3045 }
3046 ULONGEST end = start + length;
79748972
TT
3047 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3048 - baseaddr);
3049 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3050 - baseaddr);
927aa2e7
JK
3051 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3052 }
3053 }
3054
d320c2b5 3055 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3056 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3057}
3058
9291a0cd
TT
3059/* Find a slot in the mapped index INDEX for the object named NAME.
3060 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3061 constant pool and return true. If NAME cannot be found, return
3062 false. */
2fdf6df6 3063
109483d9 3064static bool
9291a0cd
TT
3065find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3066 offset_type **vec_out)
3067{
0cf03b49 3068 offset_type hash;
9291a0cd 3069 offset_type slot, step;
559a7a62 3070 int (*cmp) (const char *, const char *);
9291a0cd 3071
791afaa2 3072 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3073 if (current_language->la_language == language_cplus
45280282
IB
3074 || current_language->la_language == language_fortran
3075 || current_language->la_language == language_d)
0cf03b49
JK
3076 {
3077 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3078 not contain any. */
a8719064 3079
72998fb3 3080 if (strchr (name, '(') != NULL)
0cf03b49 3081 {
109483d9 3082 without_params = cp_remove_params (name);
0cf03b49 3083
72998fb3 3084 if (without_params != NULL)
791afaa2 3085 name = without_params.get ();
0cf03b49
JK
3086 }
3087 }
3088
559a7a62 3089 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3090 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3091 simulate our NAME being searched is also lowercased. */
3092 hash = mapped_index_string_hash ((index->version == 4
3093 && case_sensitivity == case_sensitive_off
3094 ? 5 : index->version),
3095 name);
3096
f00a2de2
PA
3097 slot = hash & (index->symbol_table.size () - 1);
3098 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3099 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3100
3101 for (;;)
3102 {
9291a0cd 3103 const char *str;
f00a2de2
PA
3104
3105 const auto &bucket = index->symbol_table[slot];
3106 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3107 return false;
9291a0cd 3108
f00a2de2 3109 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3110 if (!cmp (name, str))
9291a0cd
TT
3111 {
3112 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3113 + MAYBE_SWAP (bucket.vec));
109483d9 3114 return true;
9291a0cd
TT
3115 }
3116
f00a2de2 3117 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3118 }
3119}
3120
4485a1c1
SM
3121/* A helper function that reads the .gdb_index from BUFFER and fills
3122 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3123 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3124 ok to use deprecated sections.
3125
3126 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3127 out parameters that are filled in with information about the CU and
3128 TU lists in the section.
3129
4485a1c1 3130 Returns true if all went well, false otherwise. */
2fdf6df6 3131
d33bc52e 3132static bool
4485a1c1
SM
3133read_gdb_index_from_buffer (struct objfile *objfile,
3134 const char *filename,
3135 bool deprecated_ok,
3136 gdb::array_view<const gdb_byte> buffer,
3137 struct mapped_index *map,
3138 const gdb_byte **cu_list,
3139 offset_type *cu_list_elements,
3140 const gdb_byte **types_list,
3141 offset_type *types_list_elements)
3142{
3143 const gdb_byte *addr = &buffer[0];
82430852 3144
9291a0cd 3145 /* Version check. */
4485a1c1 3146 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3147 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3148 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3149 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3150 indices. */
831adc1f 3151 if (version < 4)
481860b3
GB
3152 {
3153 static int warning_printed = 0;
3154 if (!warning_printed)
3155 {
3156 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3157 filename);
481860b3
GB
3158 warning_printed = 1;
3159 }
3160 return 0;
3161 }
3162 /* Index version 4 uses a different hash function than index version
3163 5 and later.
3164
3165 Versions earlier than 6 did not emit psymbols for inlined
3166 functions. Using these files will cause GDB not to be able to
3167 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3168 indices unless the user has done
3169 "set use-deprecated-index-sections on". */
2ec9a5e0 3170 if (version < 6 && !deprecated_ok)
481860b3
GB
3171 {
3172 static int warning_printed = 0;
3173 if (!warning_printed)
3174 {
e615022a
DE
3175 warning (_("\
3176Skipping deprecated .gdb_index section in %s.\n\
3177Do \"set use-deprecated-index-sections on\" before the file is read\n\
3178to use the section anyway."),
2ec9a5e0 3179 filename);
481860b3
GB
3180 warning_printed = 1;
3181 }
3182 return 0;
3183 }
796a7ff8 3184 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3185 of the TU (for symbols coming from TUs),
3186 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3187 Plus gold-generated indices can have duplicate entries for global symbols,
3188 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3189 These are just performance bugs, and we can't distinguish gdb-generated
3190 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3191
481860b3 3192 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3193 longer backward compatible. */
796a7ff8 3194 if (version > 8)
594e8718 3195 return 0;
9291a0cd 3196
559a7a62 3197 map->version = version;
9291a0cd 3198
4485a1c1 3199 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3200
4485a1c1 3201 int i = 0;
2ec9a5e0
TT
3202 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3203 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3204 / 8);
1fd400ff
TT
3205 ++i;
3206
2ec9a5e0
TT
3207 *types_list = addr + MAYBE_SWAP (metadata[i]);
3208 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3209 - MAYBE_SWAP (metadata[i]))
3210 / 8);
987d643c 3211 ++i;
1fd400ff 3212
f00a2de2
PA
3213 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3214 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3215 map->address_table
3216 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3217 ++i;
3218
f00a2de2
PA
3219 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3220 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3221 map->symbol_table
3222 = gdb::array_view<mapped_index::symbol_table_slot>
3223 ((mapped_index::symbol_table_slot *) symbol_table,
3224 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3225
f00a2de2 3226 ++i;
f9d83a0b 3227 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3228
2ec9a5e0
TT
3229 return 1;
3230}
3231
4485a1c1
SM
3232/* Callback types for dwarf2_read_gdb_index. */
3233
3234typedef gdb::function_view
3235 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3236 get_gdb_index_contents_ftype;
3237typedef gdb::function_view
3238 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3239 get_gdb_index_contents_dwz_ftype;
3240
927aa2e7 3241/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3242 elements of all the CUs and return 1. Otherwise, return 0. */
3243
3244static int
4485a1c1
SM
3245dwarf2_read_gdb_index
3246 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3247 get_gdb_index_contents_ftype get_gdb_index_contents,
3248 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3249{
2ec9a5e0
TT
3250 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3251 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3252 struct dwz_file *dwz;
12359b5e 3253 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3254
4485a1c1
SM
3255 gdb::array_view<const gdb_byte> main_index_contents
3256 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3257
3258 if (main_index_contents.empty ())
3259 return 0;
3260
3063847f 3261 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3262 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3263 use_deprecated_index_sections,
3264 main_index_contents, map.get (), &cu_list,
3265 &cu_list_elements, &types_list,
3266 &types_list_elements))
2ec9a5e0
TT
3267 return 0;
3268
0fefef59 3269 /* Don't use the index if it's empty. */
3063847f 3270 if (map->symbol_table.empty ())
0fefef59
DE
3271 return 0;
3272
2ec9a5e0
TT
3273 /* If there is a .dwz file, read it so we can get its CU list as
3274 well. */
ed2dc618 3275 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3276 if (dwz != NULL)
2ec9a5e0 3277 {
2ec9a5e0
TT
3278 struct mapped_index dwz_map;
3279 const gdb_byte *dwz_types_ignore;
3280 offset_type dwz_types_elements_ignore;
3281
4485a1c1
SM
3282 gdb::array_view<const gdb_byte> dwz_index_content
3283 = get_gdb_index_contents_dwz (objfile, dwz);
3284
3285 if (dwz_index_content.empty ())
3286 return 0;
3287
3288 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3289 bfd_get_filename (dwz->dwz_bfd.get ()),
3290 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3291 &dwz_list, &dwz_list_elements,
3292 &dwz_types_ignore,
3293 &dwz_types_elements_ignore))
2ec9a5e0
TT
3294 {
3295 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3296 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3297 return 0;
3298 }
3299 }
3300
12359b5e
SM
3301 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3302 dwz_list, dwz_list_elements);
1fd400ff 3303
8b70b953
TT
3304 if (types_list_elements)
3305 {
8b70b953
TT
3306 /* We can only handle a single .debug_types when we have an
3307 index. */
fd5866f6 3308 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3309 return 0;
3310
fd5866f6 3311 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3312
12359b5e
SM
3313 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3314 types_list, types_list_elements);
8b70b953 3315 }
9291a0cd 3316
3063847f 3317 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3318
3063847f 3319 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3320 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3321 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3322 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3323
3324 return 1;
3325}
3326
dee91e82 3327/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3328
dee91e82
DE
3329static void
3330dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3331 const gdb_byte *info_ptr,
3e225074 3332 struct die_info *comp_unit_die)
9291a0cd 3333{
dee91e82 3334 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3335 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3336 struct dwarf2_per_objfile *dwarf2_per_objfile
3337 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3338 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3339 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3340 struct attribute *attr;
7b9f3c50
DE
3341 void **slot;
3342 struct quick_file_names *qfn;
9291a0cd 3343
0186c6a7
DE
3344 gdb_assert (! this_cu->is_debug_types);
3345
07261596
TT
3346 /* Our callers never want to match partial units -- instead they
3347 will match the enclosing full CU. */
3348 if (comp_unit_die->tag == DW_TAG_partial_unit)
3349 {
3350 this_cu->v.quick->no_file_data = 1;
3351 return;
3352 }
3353
0186c6a7 3354 lh_cu = this_cu;
7b9f3c50 3355 slot = NULL;
dee91e82 3356
fff8551c 3357 line_header_up lh;
9c541725 3358 sect_offset line_offset {};
fff8551c 3359
dee91e82 3360 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3361 if (attr != nullptr)
9291a0cd 3362 {
7b9f3c50
DE
3363 struct quick_file_names find_entry;
3364
9c541725 3365 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3366
3367 /* We may have already read in this line header (TU line header sharing).
3368 If we have we're done. */
094b34ac 3369 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3370 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3371 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3372 &find_entry, INSERT);
3373 if (*slot != NULL)
3374 {
9a3c8263 3375 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3376 return;
7b9f3c50
DE
3377 }
3378
3019eac3 3379 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3380 }
3381 if (lh == NULL)
3382 {
094b34ac 3383 lh_cu->v.quick->no_file_data = 1;
dee91e82 3384 return;
9291a0cd
TT
3385 }
3386
8d749320 3387 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3388 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3389 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3390 gdb_assert (slot != NULL);
3391 *slot = qfn;
9291a0cd 3392
d721ba37 3393 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3394
aa391654
TT
3395 int offset = 0;
3396 if (strcmp (fnd.name, "<unknown>") != 0)
3397 ++offset;
3398
7ba99d21 3399 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3400 qfn->file_names =
aa391654
TT
3401 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3402 if (offset != 0)
3403 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3404 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3405 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3406 qfn->real_names = NULL;
9291a0cd 3407
094b34ac 3408 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3409}
3410
3411/* A helper for the "quick" functions which attempts to read the line
3412 table for THIS_CU. */
3413
3414static struct quick_file_names *
e4a48d9d 3415dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3416{
0186c6a7
DE
3417 /* This should never be called for TUs. */
3418 gdb_assert (! this_cu->is_debug_types);
3419 /* Nor type unit groups. */
3420 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3421
dee91e82
DE
3422 if (this_cu->v.quick->file_names != NULL)
3423 return this_cu->v.quick->file_names;
3424 /* If we know there is no line data, no point in looking again. */
3425 if (this_cu->v.quick->no_file_data)
3426 return NULL;
3427
c0ab21c2
TT
3428 cutu_reader reader (this_cu);
3429 if (!reader.dummy_p)
3e225074 3430 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3431
3432 if (this_cu->v.quick->no_file_data)
3433 return NULL;
3434 return this_cu->v.quick->file_names;
9291a0cd
TT
3435}
3436
3437/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3438 real path for a given file name from the line table. */
2fdf6df6 3439
9291a0cd 3440static const char *
7b9f3c50
DE
3441dw2_get_real_path (struct objfile *objfile,
3442 struct quick_file_names *qfn, int index)
9291a0cd 3443{
7b9f3c50
DE
3444 if (qfn->real_names == NULL)
3445 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3446 qfn->num_file_names, const char *);
9291a0cd 3447
7b9f3c50 3448 if (qfn->real_names[index] == NULL)
14278e1f 3449 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3450
7b9f3c50 3451 return qfn->real_names[index];
9291a0cd
TT
3452}
3453
3454static struct symtab *
3455dw2_find_last_source_symtab (struct objfile *objfile)
3456{
ed2dc618
SM
3457 struct dwarf2_per_objfile *dwarf2_per_objfile
3458 = get_dwarf2_per_objfile (objfile);
b76e467d 3459 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3460 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3461
43f3e411
DE
3462 if (cust == NULL)
3463 return NULL;
ed2dc618 3464
43f3e411 3465 return compunit_primary_filetab (cust);
9291a0cd
TT
3466}
3467
7b9f3c50
DE
3468/* Traversal function for dw2_forget_cached_source_info. */
3469
3470static int
3471dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3472{
7b9f3c50 3473 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3474
7b9f3c50 3475 if (file_data->real_names)
9291a0cd 3476 {
7b9f3c50 3477 int i;
9291a0cd 3478
7b9f3c50 3479 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3480 {
7b9f3c50
DE
3481 xfree ((void*) file_data->real_names[i]);
3482 file_data->real_names[i] = NULL;
9291a0cd
TT
3483 }
3484 }
7b9f3c50
DE
3485
3486 return 1;
3487}
3488
3489static void
3490dw2_forget_cached_source_info (struct objfile *objfile)
3491{
ed2dc618
SM
3492 struct dwarf2_per_objfile *dwarf2_per_objfile
3493 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3494
3495 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3496 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3497}
3498
f8eba3c6
TT
3499/* Helper function for dw2_map_symtabs_matching_filename that expands
3500 the symtabs and calls the iterator. */
3501
3502static int
3503dw2_map_expand_apply (struct objfile *objfile,
3504 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3505 const char *name, const char *real_path,
14bc53a8 3506 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3507{
43f3e411 3508 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3509
3510 /* Don't visit already-expanded CUs. */
43f3e411 3511 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3512 return 0;
3513
3514 /* This may expand more than one symtab, and we want to iterate over
3515 all of them. */
58f0c718 3516 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3517
14bc53a8
PA
3518 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3519 last_made, callback);
f8eba3c6
TT
3520}
3521
3522/* Implementation of the map_symtabs_matching_filename method. */
3523
14bc53a8
PA
3524static bool
3525dw2_map_symtabs_matching_filename
3526 (struct objfile *objfile, const char *name, const char *real_path,
3527 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3528{
c011a4f4 3529 const char *name_basename = lbasename (name);
ed2dc618
SM
3530 struct dwarf2_per_objfile *dwarf2_per_objfile
3531 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3532
848e3e78
DE
3533 /* The rule is CUs specify all the files, including those used by
3534 any TU, so there's no need to scan TUs here. */
f4dc4d17 3535
b76e467d 3536 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3537 {
3d7bb9d9 3538 /* We only need to look at symtabs not already expanded. */
43f3e411 3539 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3540 continue;
3541
b76e467d 3542 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3543 if (file_data == NULL)
9291a0cd
TT
3544 continue;
3545
b76e467d 3546 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3547 {
7b9f3c50 3548 const char *this_name = file_data->file_names[j];
da235a7c 3549 const char *this_real_name;
9291a0cd 3550
af529f8f 3551 if (compare_filenames_for_search (this_name, name))
9291a0cd 3552 {
f5b95b50 3553 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3554 callback))
3555 return true;
288e77a7 3556 continue;
4aac40c8 3557 }
9291a0cd 3558
c011a4f4
DE
3559 /* Before we invoke realpath, which can get expensive when many
3560 files are involved, do a quick comparison of the basenames. */
3561 if (! basenames_may_differ
3562 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3563 continue;
3564
da235a7c
JK
3565 this_real_name = dw2_get_real_path (objfile, file_data, j);
3566 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3567 {
da235a7c 3568 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3569 callback))
3570 return true;
288e77a7 3571 continue;
da235a7c 3572 }
9291a0cd 3573
da235a7c
JK
3574 if (real_path != NULL)
3575 {
af529f8f
JK
3576 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3577 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3578 if (this_real_name != NULL
af529f8f 3579 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3580 {
f5b95b50 3581 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3582 callback))
3583 return true;
288e77a7 3584 continue;
9291a0cd
TT
3585 }
3586 }
3587 }
3588 }
3589
14bc53a8 3590 return false;
9291a0cd
TT
3591}
3592
da51c347
DE
3593/* Struct used to manage iterating over all CUs looking for a symbol. */
3594
3595struct dw2_symtab_iterator
9291a0cd 3596{
ed2dc618
SM
3597 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3598 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3599 /* If set, only look for symbols that match that block. Valid values are
3600 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3601 gdb::optional<block_enum> block_index;
da51c347
DE
3602 /* The kind of symbol we're looking for. */
3603 domain_enum domain;
3604 /* The list of CUs from the index entry of the symbol,
3605 or NULL if not found. */
3606 offset_type *vec;
3607 /* The next element in VEC to look at. */
3608 int next;
3609 /* The number of elements in VEC, or zero if there is no match. */
3610 int length;
8943b874
DE
3611 /* Have we seen a global version of the symbol?
3612 If so we can ignore all further global instances.
3613 This is to work around gold/15646, inefficient gold-generated
3614 indices. */
3615 int global_seen;
da51c347 3616};
9291a0cd 3617
2b79f376 3618/* Initialize the index symtab iterator ITER. */
2fdf6df6 3619
9291a0cd 3620static void
da51c347 3621dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3622 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3623 gdb::optional<block_enum> block_index,
da51c347
DE
3624 domain_enum domain,
3625 const char *name)
3626{
ed2dc618 3627 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3628 iter->block_index = block_index;
3629 iter->domain = domain;
3630 iter->next = 0;
8943b874 3631 iter->global_seen = 0;
da51c347 3632
3063847f 3633 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3634
3635 /* index is NULL if OBJF_READNOW. */
3636 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3637 iter->length = MAYBE_SWAP (*iter->vec);
3638 else
3639 {
3640 iter->vec = NULL;
3641 iter->length = 0;
3642 }
3643}
3644
3645/* Return the next matching CU or NULL if there are no more. */
3646
3647static struct dwarf2_per_cu_data *
3648dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3649{
ed2dc618
SM
3650 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3651
da51c347
DE
3652 for ( ; iter->next < iter->length; ++iter->next)
3653 {
3654 offset_type cu_index_and_attrs =
3655 MAYBE_SWAP (iter->vec[iter->next + 1]);
3656 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3657 gdb_index_symbol_kind symbol_kind =
3658 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3659 /* Only check the symbol attributes if they're present.
3660 Indices prior to version 7 don't record them,
3661 and indices >= 7 may elide them for certain symbols
3662 (gold does this). */
3663 int attrs_valid =
ed2dc618 3664 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3665 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3666
3190f0c6 3667 /* Don't crash on bad data. */
b76e467d 3668 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3669 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3670 {
b98664d3 3671 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3672 " [in module %s]"),
3673 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3674 continue;
3675 }
3676
ff4c9fec 3677 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3678
da51c347 3679 /* Skip if already read in. */
43f3e411 3680 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3681 continue;
3682
8943b874
DE
3683 /* Check static vs global. */
3684 if (attrs_valid)
3685 {
2b79f376
SM
3686 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3687
3688 if (iter->block_index.has_value ())
3689 {
3690 bool want_static = *iter->block_index == STATIC_BLOCK;
3691
3692 if (is_static != want_static)
3693 continue;
3694 }
3695
8943b874
DE
3696 /* Work around gold/15646. */
3697 if (!is_static && iter->global_seen)
3698 continue;
3699 if (!is_static)
3700 iter->global_seen = 1;
3701 }
da51c347
DE
3702
3703 /* Only check the symbol's kind if it has one. */
3704 if (attrs_valid)
3705 {
3706 switch (iter->domain)
3707 {
3708 case VAR_DOMAIN:
3709 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3710 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3711 /* Some types are also in VAR_DOMAIN. */
3712 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3713 continue;
3714 break;
3715 case STRUCT_DOMAIN:
3716 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3717 continue;
3718 break;
3719 case LABEL_DOMAIN:
3720 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3721 continue;
3722 break;
59c35742
AB
3723 case MODULE_DOMAIN:
3724 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3725 continue;
3726 break;
da51c347
DE
3727 default:
3728 break;
3729 }
3730 }
3731
3732 ++iter->next;
3733 return per_cu;
3734 }
3735
3736 return NULL;
3737}
3738
43f3e411 3739static struct compunit_symtab *
c7f839cb 3740dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3741 const char *name, domain_enum domain)
9291a0cd 3742{
43f3e411 3743 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3744 struct dwarf2_per_objfile *dwarf2_per_objfile
3745 = get_dwarf2_per_objfile (objfile);
9291a0cd 3746
b5ec771e
PA
3747 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3748
ed2dc618
SM
3749 struct dw2_symtab_iterator iter;
3750 struct dwarf2_per_cu_data *per_cu;
da51c347 3751
2b79f376 3752 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3753
ed2dc618
SM
3754 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3755 {
3756 struct symbol *sym, *with_opaque = NULL;
58f0c718 3757 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3758 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3759 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3760
ed2dc618
SM
3761 sym = block_find_symbol (block, name, domain,
3762 block_find_non_opaque_type_preferred,
3763 &with_opaque);
b2e2f908 3764
ed2dc618
SM
3765 /* Some caution must be observed with overloaded functions
3766 and methods, since the index will not contain any overload
3767 information (but NAME might contain it). */
da51c347 3768
ed2dc618
SM
3769 if (sym != NULL
3770 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3771 return stab;
3772 if (with_opaque != NULL
3773 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3774 stab_best = stab;
da51c347 3775
ed2dc618 3776 /* Keep looking through other CUs. */
9291a0cd 3777 }
9291a0cd 3778
da51c347 3779 return stab_best;
9291a0cd
TT
3780}
3781
3782static void
3783dw2_print_stats (struct objfile *objfile)
3784{
ed2dc618
SM
3785 struct dwarf2_per_objfile *dwarf2_per_objfile
3786 = get_dwarf2_per_objfile (objfile);
b76e467d 3787 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3788 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3789 int count = 0;
9291a0cd 3790
ed2dc618 3791 for (int i = 0; i < total; ++i)
9291a0cd 3792 {
ff4c9fec 3793 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3794
43f3e411 3795 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3796 ++count;
3797 }
e4a48d9d 3798 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3799 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3800}
3801
779bd270
DE
3802/* This dumps minimal information about the index.
3803 It is called via "mt print objfiles".
3804 One use is to verify .gdb_index has been loaded by the
3805 gdb.dwarf2/gdb-index.exp testcase. */
3806
9291a0cd
TT
3807static void
3808dw2_dump (struct objfile *objfile)
3809{
ed2dc618
SM
3810 struct dwarf2_per_objfile *dwarf2_per_objfile
3811 = get_dwarf2_per_objfile (objfile);
3812
779bd270
DE
3813 gdb_assert (dwarf2_per_objfile->using_index);
3814 printf_filtered (".gdb_index:");
3815 if (dwarf2_per_objfile->index_table != NULL)
3816 {
3817 printf_filtered (" version %d\n",
3818 dwarf2_per_objfile->index_table->version);
3819 }
3820 else
3821 printf_filtered (" faked for \"readnow\"\n");
3822 printf_filtered ("\n");
9291a0cd
TT
3823}
3824
9291a0cd
TT
3825static void
3826dw2_expand_symtabs_for_function (struct objfile *objfile,
3827 const char *func_name)
3828{
ed2dc618
SM
3829 struct dwarf2_per_objfile *dwarf2_per_objfile
3830 = get_dwarf2_per_objfile (objfile);
da51c347 3831
ed2dc618
SM
3832 struct dw2_symtab_iterator iter;
3833 struct dwarf2_per_cu_data *per_cu;
da51c347 3834
2b79f376 3835 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3836
ed2dc618 3837 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3838 dw2_instantiate_symtab (per_cu, false);
da51c347 3839
9291a0cd
TT
3840}
3841
3842static void
3843dw2_expand_all_symtabs (struct objfile *objfile)
3844{
ed2dc618
SM
3845 struct dwarf2_per_objfile *dwarf2_per_objfile
3846 = get_dwarf2_per_objfile (objfile);
b76e467d 3847 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3848 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3849
ed2dc618 3850 for (int i = 0; i < total_units; ++i)
9291a0cd 3851 {
ff4c9fec 3852 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3853
58f0c718
TT
3854 /* We don't want to directly expand a partial CU, because if we
3855 read it with the wrong language, then assertion failures can
3856 be triggered later on. See PR symtab/23010. So, tell
3857 dw2_instantiate_symtab to skip partial CUs -- any important
3858 partial CU will be read via DW_TAG_imported_unit anyway. */
3859 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3860 }
3861}
3862
3863static void
652a8996
JK
3864dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3865 const char *fullname)
9291a0cd 3866{
ed2dc618
SM
3867 struct dwarf2_per_objfile *dwarf2_per_objfile
3868 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3869
3870 /* We don't need to consider type units here.
3871 This is only called for examining code, e.g. expand_line_sal.
3872 There can be an order of magnitude (or more) more type units
3873 than comp units, and we avoid them if we can. */
3874
b76e467d 3875 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3876 {
3d7bb9d9 3877 /* We only need to look at symtabs not already expanded. */
43f3e411 3878 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3879 continue;
3880
b76e467d 3881 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3882 if (file_data == NULL)
9291a0cd
TT
3883 continue;
3884
b76e467d 3885 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3886 {
652a8996
JK
3887 const char *this_fullname = file_data->file_names[j];
3888
3889 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3890 {
58f0c718 3891 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3892 break;
3893 }
3894 }
3895 }
3896}
3897
9291a0cd 3898static void
199b4314
TT
3899dw2_map_matching_symbols
3900 (struct objfile *objfile,
b054970d 3901 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3902 int global,
3903 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3904 symbol_compare_ftype *ordered_compare)
9291a0cd 3905{
40658b94 3906 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3907 current language is Ada for a non-Ada objfile using GNU index. As Ada
3908 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3909}
3910
e1ef7d7a
PA
3911/* Starting from a search name, return the string that finds the upper
3912 bound of all strings that start with SEARCH_NAME in a sorted name
3913 list. Returns the empty string to indicate that the upper bound is
3914 the end of the list. */
3915
3916static std::string
3917make_sort_after_prefix_name (const char *search_name)
3918{
3919 /* When looking to complete "func", we find the upper bound of all
3920 symbols that start with "func" by looking for where we'd insert
3921 the closest string that would follow "func" in lexicographical
3922 order. Usually, that's "func"-with-last-character-incremented,
3923 i.e. "fund". Mind non-ASCII characters, though. Usually those
3924 will be UTF-8 multi-byte sequences, but we can't be certain.
3925 Especially mind the 0xff character, which is a valid character in
3926 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3927 rule out compilers allowing it in identifiers. Note that
3928 conveniently, strcmp/strcasecmp are specified to compare
3929 characters interpreted as unsigned char. So what we do is treat
3930 the whole string as a base 256 number composed of a sequence of
3931 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3932 to 0, and carries 1 to the following more-significant position.
3933 If the very first character in SEARCH_NAME ends up incremented
3934 and carries/overflows, then the upper bound is the end of the
3935 list. The string after the empty string is also the empty
3936 string.
3937
3938 Some examples of this operation:
3939
3940 SEARCH_NAME => "+1" RESULT
3941
3942 "abc" => "abd"
3943 "ab\xff" => "ac"
3944 "\xff" "a" "\xff" => "\xff" "b"
3945 "\xff" => ""
3946 "\xff\xff" => ""
3947 "" => ""
3948
3949 Then, with these symbols for example:
3950
3951 func
3952 func1
3953 fund
3954
3955 completing "func" looks for symbols between "func" and
3956 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3957 which finds "func" and "func1", but not "fund".
3958
3959 And with:
3960
3961 funcÿ (Latin1 'ÿ' [0xff])
3962 funcÿ1
3963 fund
3964
3965 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3966 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3967
3968 And with:
3969
3970 ÿÿ (Latin1 'ÿ' [0xff])
3971 ÿÿ1
3972
3973 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3974 the end of the list.
3975 */
3976 std::string after = search_name;
3977 while (!after.empty () && (unsigned char) after.back () == 0xff)
3978 after.pop_back ();
3979 if (!after.empty ())
3980 after.back () = (unsigned char) after.back () + 1;
3981 return after;
3982}
3983
5c58de74 3984/* See declaration. */
61d96d7e 3985
5c58de74
PA
3986std::pair<std::vector<name_component>::const_iterator,
3987 std::vector<name_component>::const_iterator>
44ed8f3e 3988mapped_index_base::find_name_components_bounds
3b00ef10 3989 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3990{
5c58de74
PA
3991 auto *name_cmp
3992 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3993
3b00ef10
TT
3994 const char *lang_name
3995 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3996
3f563c84
PA
3997 /* Comparison function object for lower_bound that matches against a
3998 given symbol name. */
3999 auto lookup_compare_lower = [&] (const name_component &elem,
4000 const char *name)
4001 {
5c58de74 4002 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4003 const char *elem_name = elem_qualified + elem.name_offset;
4004 return name_cmp (elem_name, name) < 0;
4005 };
4006
4007 /* Comparison function object for upper_bound that matches against a
4008 given symbol name. */
4009 auto lookup_compare_upper = [&] (const char *name,
4010 const name_component &elem)
4011 {
5c58de74 4012 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4013 const char *elem_name = elem_qualified + elem.name_offset;
4014 return name_cmp (name, elem_name) < 0;
4015 };
4016
5c58de74
PA
4017 auto begin = this->name_components.begin ();
4018 auto end = this->name_components.end ();
3f563c84
PA
4019
4020 /* Find the lower bound. */
4021 auto lower = [&] ()
4022 {
3b00ef10 4023 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4024 return begin;
4025 else
3b00ef10 4026 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4027 } ();
4028
4029 /* Find the upper bound. */
4030 auto upper = [&] ()
4031 {
5c58de74 4032 if (lookup_name_without_params.completion_mode ())
3f563c84 4033 {
e1ef7d7a
PA
4034 /* In completion mode, we want UPPER to point past all
4035 symbols names that have the same prefix. I.e., with
4036 these symbols, and completing "func":
4037
4038 function << lower bound
4039 function1
4040 other_function << upper bound
4041
4042 We find the upper bound by looking for the insertion
4043 point of "func"-with-last-character-incremented,
4044 i.e. "fund". */
3b00ef10 4045 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4046 if (after.empty ())
3f563c84 4047 return end;
e6b2f5ef
PA
4048 return std::lower_bound (lower, end, after.c_str (),
4049 lookup_compare_lower);
3f563c84
PA
4050 }
4051 else
3b00ef10 4052 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4053 } ();
4054
5c58de74
PA
4055 return {lower, upper};
4056}
4057
4058/* See declaration. */
4059
4060void
44ed8f3e 4061mapped_index_base::build_name_components ()
5c58de74
PA
4062{
4063 if (!this->name_components.empty ())
4064 return;
4065
4066 this->name_components_casing = case_sensitivity;
4067 auto *name_cmp
4068 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4069
4070 /* The code below only knows how to break apart components of C++
4071 symbol names (and other languages that use '::' as
3b00ef10 4072 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4073 auto count = this->symbol_name_count ();
4074 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4075 {
44ed8f3e 4076 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4077 continue;
4078
4079 const char *name = this->symbol_name_at (idx);
4080
4081 /* Add each name component to the name component table. */
4082 unsigned int previous_len = 0;
3b00ef10
TT
4083
4084 if (strstr (name, "::") != nullptr)
4085 {
4086 for (unsigned int current_len = cp_find_first_component (name);
4087 name[current_len] != '\0';
4088 current_len += cp_find_first_component (name + current_len))
4089 {
4090 gdb_assert (name[current_len] == ':');
4091 this->name_components.push_back ({previous_len, idx});
4092 /* Skip the '::'. */
4093 current_len += 2;
4094 previous_len = current_len;
4095 }
4096 }
4097 else
5c58de74 4098 {
3b00ef10
TT
4099 /* Handle the Ada encoded (aka mangled) form here. */
4100 for (const char *iter = strstr (name, "__");
4101 iter != nullptr;
4102 iter = strstr (iter, "__"))
4103 {
4104 this->name_components.push_back ({previous_len, idx});
4105 iter += 2;
4106 previous_len = iter - name;
4107 }
5c58de74 4108 }
3b00ef10 4109
5c58de74
PA
4110 this->name_components.push_back ({previous_len, idx});
4111 }
4112
4113 /* Sort name_components elements by name. */
4114 auto name_comp_compare = [&] (const name_component &left,
4115 const name_component &right)
4116 {
4117 const char *left_qualified = this->symbol_name_at (left.idx);
4118 const char *right_qualified = this->symbol_name_at (right.idx);
4119
4120 const char *left_name = left_qualified + left.name_offset;
4121 const char *right_name = right_qualified + right.name_offset;
4122
4123 return name_cmp (left_name, right_name) < 0;
4124 };
4125
4126 std::sort (this->name_components.begin (),
4127 this->name_components.end (),
4128 name_comp_compare);
4129}
4130
4131/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4132 mapped_index_base instead of the containing objfile. This is split
4133 to a separate function in order to be able to unit test the
4134 name_components matching using a mock mapped_index_base. For each
5c58de74 4135 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4136 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4137
4138static void
4139dw2_expand_symtabs_matching_symbol
44ed8f3e 4140 (mapped_index_base &index,
5c58de74
PA
4141 const lookup_name_info &lookup_name_in,
4142 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4143 enum search_domain kind,
3b00ef10 4144 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4145{
4146 lookup_name_info lookup_name_without_params
4147 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4148
4149 /* Build the symbol name component sorted vector, if we haven't
4150 yet. */
4151 index.build_name_components ();
4152
3f563c84
PA
4153 /* The same symbol may appear more than once in the range though.
4154 E.g., if we're looking for symbols that complete "w", and we have
4155 a symbol named "w1::w2", we'll find the two name components for
4156 that same symbol in the range. To be sure we only call the
4157 callback once per symbol, we first collect the symbol name
4158 indexes that matched in a temporary vector and ignore
4159 duplicates. */
4160 std::vector<offset_type> matches;
3f563c84 4161
3b00ef10
TT
4162 struct name_and_matcher
4163 {
4164 symbol_name_matcher_ftype *matcher;
4165 const std::string &name;
4166
4167 bool operator== (const name_and_matcher &other) const
3f563c84 4168 {
3b00ef10
TT
4169 return matcher == other.matcher && name == other.name;
4170 }
4171 };
4172
4173 /* A vector holding all the different symbol name matchers, for all
4174 languages. */
4175 std::vector<name_and_matcher> matchers;
4176
4177 for (int i = 0; i < nr_languages; i++)
4178 {
4179 enum language lang_e = (enum language) i;
4180
4181 const language_defn *lang = language_def (lang_e);
4182 symbol_name_matcher_ftype *name_matcher
4183 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4184
3b00ef10
TT
4185 name_and_matcher key {
4186 name_matcher,
4187 lookup_name_without_params.language_lookup_name (lang_e)
4188 };
4189
4190 /* Don't insert the same comparison routine more than once.
4191 Note that we do this linear walk. This is not a problem in
4192 practice because the number of supported languages is
4193 low. */
4194 if (std::find (matchers.begin (), matchers.end (), key)
4195 != matchers.end ())
9291a0cd 4196 continue;
3b00ef10
TT
4197 matchers.push_back (std::move (key));
4198
4199 auto bounds
4200 = index.find_name_components_bounds (lookup_name_without_params,
4201 lang_e);
4202
4203 /* Now for each symbol name in range, check to see if we have a name
4204 match, and if so, call the MATCH_CALLBACK callback. */
4205
4206 for (; bounds.first != bounds.second; ++bounds.first)
4207 {
4208 const char *qualified = index.symbol_name_at (bounds.first->idx);
4209
4210 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4211 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4212 continue;
9291a0cd 4213
3b00ef10
TT
4214 matches.push_back (bounds.first->idx);
4215 }
3f563c84
PA
4216 }
4217
4218 std::sort (matches.begin (), matches.end ());
4219
4220 /* Finally call the callback, once per match. */
4221 ULONGEST prev = -1;
4222 for (offset_type idx : matches)
4223 {
4224 if (prev != idx)
4225 {
3b00ef10
TT
4226 if (!match_callback (idx))
4227 break;
3f563c84
PA
4228 prev = idx;
4229 }
4230 }
4231
4232 /* Above we use a type wider than idx's for 'prev', since 0 and
4233 (offset_type)-1 are both possible values. */
4234 static_assert (sizeof (prev) > sizeof (offset_type), "");
4235}
4236
c62446b1
PA
4237#if GDB_SELF_TEST
4238
4239namespace selftests { namespace dw2_expand_symtabs_matching {
4240
a3c5fafd
PA
4241/* A mock .gdb_index/.debug_names-like name index table, enough to
4242 exercise dw2_expand_symtabs_matching_symbol, which works with the
4243 mapped_index_base interface. Builds an index from the symbol list
4244 passed as parameter to the constructor. */
4245class mock_mapped_index : public mapped_index_base
c62446b1
PA
4246{
4247public:
a3c5fafd
PA
4248 mock_mapped_index (gdb::array_view<const char *> symbols)
4249 : m_symbol_table (symbols)
c62446b1
PA
4250 {}
4251
a3c5fafd 4252 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4253
a3c5fafd 4254 /* Return the number of names in the symbol table. */
632e107b 4255 size_t symbol_name_count () const override
c62446b1 4256 {
a3c5fafd 4257 return m_symbol_table.size ();
c62446b1
PA
4258 }
4259
a3c5fafd 4260 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4261 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4262 {
4263 return m_symbol_table[idx];
4264 }
c62446b1 4265
a3c5fafd
PA
4266private:
4267 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4268};
4269
4270/* Convenience function that converts a NULL pointer to a "<null>"
4271 string, to pass to print routines. */
4272
4273static const char *
4274string_or_null (const char *str)
4275{
4276 return str != NULL ? str : "<null>";
4277}
4278
4279/* Check if a lookup_name_info built from
4280 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4281 index. EXPECTED_LIST is the list of expected matches, in expected
4282 matching order. If no match expected, then an empty list is
4283 specified. Returns true on success. On failure prints a warning
4284 indicating the file:line that failed, and returns false. */
4285
4286static bool
4287check_match (const char *file, int line,
4288 mock_mapped_index &mock_index,
4289 const char *name, symbol_name_match_type match_type,
4290 bool completion_mode,
4291 std::initializer_list<const char *> expected_list)
4292{
4293 lookup_name_info lookup_name (name, match_type, completion_mode);
4294
4295 bool matched = true;
4296
4297 auto mismatch = [&] (const char *expected_str,
4298 const char *got)
4299 {
4300 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4301 "expected=\"%s\", got=\"%s\"\n"),
4302 file, line,
4303 (match_type == symbol_name_match_type::FULL
4304 ? "FULL" : "WILD"),
4305 name, string_or_null (expected_str), string_or_null (got));
4306 matched = false;
4307 };
4308
4309 auto expected_it = expected_list.begin ();
4310 auto expected_end = expected_list.end ();
4311
a3c5fafd 4312 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4313 NULL, ALL_DOMAIN,
4314 [&] (offset_type idx)
4315 {
a3c5fafd 4316 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4317 const char *expected_str
4318 = expected_it == expected_end ? NULL : *expected_it++;
4319
4320 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4321 mismatch (expected_str, matched_name);
3b00ef10 4322 return true;
c62446b1
PA
4323 });
4324
4325 const char *expected_str
4326 = expected_it == expected_end ? NULL : *expected_it++;
4327 if (expected_str != NULL)
4328 mismatch (expected_str, NULL);
4329
4330 return matched;
4331}
4332
4333/* The symbols added to the mock mapped_index for testing (in
4334 canonical form). */
4335static const char *test_symbols[] = {
4336 "function",
4337 "std::bar",
4338 "std::zfunction",
4339 "std::zfunction2",
4340 "w1::w2",
4341 "ns::foo<char*>",
4342 "ns::foo<int>",
4343 "ns::foo<long>",
a20714ff
PA
4344 "ns2::tmpl<int>::foo2",
4345 "(anonymous namespace)::A::B::C",
c62446b1 4346
e1ef7d7a
PA
4347 /* These are used to check that the increment-last-char in the
4348 matching algorithm for completion doesn't match "t1_fund" when
4349 completing "t1_func". */
4350 "t1_func",
4351 "t1_func1",
4352 "t1_fund",
4353 "t1_fund1",
4354
4355 /* A UTF-8 name with multi-byte sequences to make sure that
4356 cp-name-parser understands this as a single identifier ("função"
4357 is "function" in PT). */
4358 u8"u8função",
4359
4360 /* \377 (0xff) is Latin1 'ÿ'. */
4361 "yfunc\377",
4362
4363 /* \377 (0xff) is Latin1 'ÿ'. */
4364 "\377",
4365 "\377\377123",
4366
c62446b1
PA
4367 /* A name with all sorts of complications. Starts with "z" to make
4368 it easier for the completion tests below. */
4369#define Z_SYM_NAME \
4370 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4371 "::tuple<(anonymous namespace)::ui*, " \
4372 "std::default_delete<(anonymous namespace)::ui>, void>"
4373
4374 Z_SYM_NAME
4375};
4376
a3c5fafd
PA
4377/* Returns true if the mapped_index_base::find_name_component_bounds
4378 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4379 in completion mode. */
5c58de74
PA
4380
4381static bool
a3c5fafd 4382check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4383 const char *search_name,
4384 gdb::array_view<const char *> expected_syms)
4385{
4386 lookup_name_info lookup_name (search_name,
4387 symbol_name_match_type::FULL, true);
4388
3b00ef10
TT
4389 auto bounds = index.find_name_components_bounds (lookup_name,
4390 language_cplus);
5c58de74
PA
4391
4392 size_t distance = std::distance (bounds.first, bounds.second);
4393 if (distance != expected_syms.size ())
4394 return false;
4395
4396 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4397 {
4398 auto nc_elem = bounds.first + exp_elem;
4399 const char *qualified = index.symbol_name_at (nc_elem->idx);
4400 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4401 return false;
4402 }
4403
4404 return true;
4405}
4406
4407/* Test the lower-level mapped_index::find_name_component_bounds
4408 method. */
4409
c62446b1 4410static void
5c58de74
PA
4411test_mapped_index_find_name_component_bounds ()
4412{
4413 mock_mapped_index mock_index (test_symbols);
4414
a3c5fafd 4415 mock_index.build_name_components ();
5c58de74
PA
4416
4417 /* Test the lower-level mapped_index::find_name_component_bounds
4418 method in completion mode. */
4419 {
4420 static const char *expected_syms[] = {
4421 "t1_func",
4422 "t1_func1",
5c58de74
PA
4423 };
4424
a3c5fafd 4425 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4426 "t1_func", expected_syms));
4427 }
4428
4429 /* Check that the increment-last-char in the name matching algorithm
4430 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4431 {
4432 static const char *expected_syms1[] = {
4433 "\377",
4434 "\377\377123",
4435 };
a3c5fafd 4436 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4437 "\377", expected_syms1));
4438
4439 static const char *expected_syms2[] = {
4440 "\377\377123",
4441 };
a3c5fafd 4442 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4443 "\377\377", expected_syms2));
4444 }
4445}
4446
4447/* Test dw2_expand_symtabs_matching_symbol. */
4448
4449static void
4450test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4451{
4452 mock_mapped_index mock_index (test_symbols);
4453
4454 /* We let all tests run until the end even if some fails, for debug
4455 convenience. */
4456 bool any_mismatch = false;
4457
4458 /* Create the expected symbols list (an initializer_list). Needed
4459 because lists have commas, and we need to pass them to CHECK,
4460 which is a macro. */
4461#define EXPECT(...) { __VA_ARGS__ }
4462
4463 /* Wrapper for check_match that passes down the current
4464 __FILE__/__LINE__. */
4465#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4466 any_mismatch |= !check_match (__FILE__, __LINE__, \
4467 mock_index, \
4468 NAME, MATCH_TYPE, COMPLETION_MODE, \
4469 EXPECTED_LIST)
4470
4471 /* Identity checks. */
4472 for (const char *sym : test_symbols)
4473 {
4474 /* Should be able to match all existing symbols. */
4475 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4476 EXPECT (sym));
4477
4478 /* Should be able to match all existing symbols with
4479 parameters. */
4480 std::string with_params = std::string (sym) + "(int)";
4481 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4482 EXPECT (sym));
4483
4484 /* Should be able to match all existing symbols with
4485 parameters and qualifiers. */
4486 with_params = std::string (sym) + " ( int ) const";
4487 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4488 EXPECT (sym));
4489
4490 /* This should really find sym, but cp-name-parser.y doesn't
4491 know about lvalue/rvalue qualifiers yet. */
4492 with_params = std::string (sym) + " ( int ) &&";
4493 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4494 {});
4495 }
4496
e1ef7d7a
PA
4497 /* Check that the name matching algorithm for completion doesn't get
4498 confused with Latin1 'ÿ' / 0xff. */
4499 {
4500 static const char str[] = "\377";
4501 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4502 EXPECT ("\377", "\377\377123"));
4503 }
4504
4505 /* Check that the increment-last-char in the matching algorithm for
4506 completion doesn't match "t1_fund" when completing "t1_func". */
4507 {
4508 static const char str[] = "t1_func";
4509 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4510 EXPECT ("t1_func", "t1_func1"));
4511 }
4512
c62446b1
PA
4513 /* Check that completion mode works at each prefix of the expected
4514 symbol name. */
4515 {
4516 static const char str[] = "function(int)";
4517 size_t len = strlen (str);
4518 std::string lookup;
4519
4520 for (size_t i = 1; i < len; i++)
4521 {
4522 lookup.assign (str, i);
4523 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4524 EXPECT ("function"));
4525 }
4526 }
4527
4528 /* While "w" is a prefix of both components, the match function
4529 should still only be called once. */
4530 {
4531 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4532 EXPECT ("w1::w2"));
a20714ff
PA
4533 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4534 EXPECT ("w1::w2"));
c62446b1
PA
4535 }
4536
4537 /* Same, with a "complicated" symbol. */
4538 {
4539 static const char str[] = Z_SYM_NAME;
4540 size_t len = strlen (str);
4541 std::string lookup;
4542
4543 for (size_t i = 1; i < len; i++)
4544 {
4545 lookup.assign (str, i);
4546 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4547 EXPECT (Z_SYM_NAME));
4548 }
4549 }
4550
4551 /* In FULL mode, an incomplete symbol doesn't match. */
4552 {
4553 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4554 {});
4555 }
4556
4557 /* A complete symbol with parameters matches any overload, since the
4558 index has no overload info. */
4559 {
4560 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4561 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4562 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4563 EXPECT ("std::zfunction", "std::zfunction2"));
4564 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4565 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4566 }
4567
4568 /* Check that whitespace is ignored appropriately. A symbol with a
4569 template argument list. */
4570 {
4571 static const char expected[] = "ns::foo<int>";
4572 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4573 EXPECT (expected));
a20714ff
PA
4574 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4575 EXPECT (expected));
c62446b1
PA
4576 }
4577
4578 /* Check that whitespace is ignored appropriately. A symbol with a
4579 template argument list that includes a pointer. */
4580 {
4581 static const char expected[] = "ns::foo<char*>";
4582 /* Try both completion and non-completion modes. */
4583 static const bool completion_mode[2] = {false, true};
4584 for (size_t i = 0; i < 2; i++)
4585 {
4586 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4587 completion_mode[i], EXPECT (expected));
a20714ff
PA
4588 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4589 completion_mode[i], EXPECT (expected));
c62446b1
PA
4590
4591 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4592 completion_mode[i], EXPECT (expected));
a20714ff
PA
4593 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4594 completion_mode[i], EXPECT (expected));
c62446b1
PA
4595 }
4596 }
4597
4598 {
4599 /* Check method qualifiers are ignored. */
4600 static const char expected[] = "ns::foo<char*>";
4601 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4602 symbol_name_match_type::FULL, true, EXPECT (expected));
4603 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4604 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4605 CHECK_MATCH ("foo < char * > ( int ) const",
4606 symbol_name_match_type::WILD, true, EXPECT (expected));
4607 CHECK_MATCH ("foo < char * > ( int ) &&",
4608 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4609 }
4610
4611 /* Test lookup names that don't match anything. */
4612 {
a20714ff
PA
4613 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4614 {});
4615
c62446b1
PA
4616 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4617 {});
4618 }
4619
a20714ff
PA
4620 /* Some wild matching tests, exercising "(anonymous namespace)",
4621 which should not be confused with a parameter list. */
4622 {
4623 static const char *syms[] = {
4624 "A::B::C",
4625 "B::C",
4626 "C",
4627 "A :: B :: C ( int )",
4628 "B :: C ( int )",
4629 "C ( int )",
4630 };
4631
4632 for (const char *s : syms)
4633 {
4634 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4635 EXPECT ("(anonymous namespace)::A::B::C"));
4636 }
4637 }
4638
4639 {
4640 static const char expected[] = "ns2::tmpl<int>::foo2";
4641 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4642 EXPECT (expected));
4643 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4644 EXPECT (expected));
4645 }
4646
c62446b1
PA
4647 SELF_CHECK (!any_mismatch);
4648
4649#undef EXPECT
4650#undef CHECK_MATCH
4651}
4652
5c58de74
PA
4653static void
4654run_test ()
4655{
4656 test_mapped_index_find_name_component_bounds ();
4657 test_dw2_expand_symtabs_matching_symbol ();
4658}
4659
c62446b1
PA
4660}} // namespace selftests::dw2_expand_symtabs_matching
4661
4662#endif /* GDB_SELF_TEST */
4663
4b514bc8
JK
4664/* If FILE_MATCHER is NULL or if PER_CU has
4665 dwarf2_per_cu_quick_data::MARK set (see
4666 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4667 EXPANSION_NOTIFY on it. */
4668
4669static void
4670dw2_expand_symtabs_matching_one
4671 (struct dwarf2_per_cu_data *per_cu,
4672 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4673 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4674{
4675 if (file_matcher == NULL || per_cu->v.quick->mark)
4676 {
4677 bool symtab_was_null
4678 = (per_cu->v.quick->compunit_symtab == NULL);
4679
58f0c718 4680 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4681
4682 if (expansion_notify != NULL
4683 && symtab_was_null
4684 && per_cu->v.quick->compunit_symtab != NULL)
4685 expansion_notify (per_cu->v.quick->compunit_symtab);
4686 }
4687}
4688
3f563c84
PA
4689/* Helper for dw2_expand_matching symtabs. Called on each symbol
4690 matched, to expand corresponding CUs that were marked. IDX is the
4691 index of the symbol name that matched. */
4692
4693static void
4694dw2_expand_marked_cus
ed2dc618 4695 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4696 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4697 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4698 search_domain kind)
4699{
3f563c84
PA
4700 offset_type *vec, vec_len, vec_idx;
4701 bool global_seen = false;
ed2dc618 4702 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4703
61920122 4704 vec = (offset_type *) (index.constant_pool
f00a2de2 4705 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4706 vec_len = MAYBE_SWAP (vec[0]);
4707 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4708 {
61920122
PA
4709 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4710 /* This value is only valid for index versions >= 7. */
4711 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4712 gdb_index_symbol_kind symbol_kind =
4713 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4714 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4715 /* Only check the symbol attributes if they're present.
4716 Indices prior to version 7 don't record them,
4717 and indices >= 7 may elide them for certain symbols
4718 (gold does this). */
4719 int attrs_valid =
4720 (index.version >= 7
4721 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4722
4723 /* Work around gold/15646. */
4724 if (attrs_valid)
9291a0cd 4725 {
61920122
PA
4726 if (!is_static && global_seen)
4727 continue;
4728 if (!is_static)
4729 global_seen = true;
4730 }
3190f0c6 4731
61920122
PA
4732 /* Only check the symbol's kind if it has one. */
4733 if (attrs_valid)
4734 {
4735 switch (kind)
8943b874 4736 {
61920122
PA
4737 case VARIABLES_DOMAIN:
4738 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4739 continue;
4740 break;
4741 case FUNCTIONS_DOMAIN:
4742 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4743 continue;
61920122
PA
4744 break;
4745 case TYPES_DOMAIN:
4746 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4747 continue;
4748 break;
59c35742
AB
4749 case MODULES_DOMAIN:
4750 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4751 continue;
4752 break;
61920122
PA
4753 default:
4754 break;
8943b874 4755 }
61920122 4756 }
8943b874 4757
61920122 4758 /* Don't crash on bad data. */
b76e467d 4759 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4760 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4761 {
b98664d3 4762 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4763 " [in module %s]"),
4764 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4765 continue;
4766 }
4767
ff4c9fec 4768 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4769 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4770 expansion_notify);
61920122
PA
4771 }
4772}
4773
4b514bc8
JK
4774/* If FILE_MATCHER is non-NULL, set all the
4775 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4776 that match FILE_MATCHER. */
4777
61920122 4778static void
4b514bc8 4779dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4780 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4781 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4782{
4b514bc8 4783 if (file_matcher == NULL)
61920122
PA
4784 return;
4785
4b514bc8
JK
4786 objfile *const objfile = dwarf2_per_objfile->objfile;
4787
4788 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4789 htab_eq_pointer,
4790 NULL, xcalloc, xfree));
4791 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4792 htab_eq_pointer,
4793 NULL, xcalloc, xfree));
61920122 4794
4b514bc8
JK
4795 /* The rule is CUs specify all the files, including those used by
4796 any TU, so there's no need to scan TUs here. */
61920122 4797
b76e467d 4798 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4799 {
927aa2e7
JK
4800 QUIT;
4801
4802 per_cu->v.quick->mark = 0;
4803
4804 /* We only need to look at symtabs not already expanded. */
4805 if (per_cu->v.quick->compunit_symtab)
4806 continue;
4807
b76e467d 4808 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4809 if (file_data == NULL)
4810 continue;
4811
4812 if (htab_find (visited_not_found.get (), file_data) != NULL)
4813 continue;
4814 else if (htab_find (visited_found.get (), file_data) != NULL)
4815 {
4816 per_cu->v.quick->mark = 1;
4817 continue;
4818 }
4819
b76e467d 4820 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4821 {
4822 const char *this_real_name;
4823
4824 if (file_matcher (file_data->file_names[j], false))
4825 {
4826 per_cu->v.quick->mark = 1;
4827 break;
4828 }
4829
4830 /* Before we invoke realpath, which can get expensive when many
4831 files are involved, do a quick comparison of the basenames. */
4832 if (!basenames_may_differ
4833 && !file_matcher (lbasename (file_data->file_names[j]),
4834 true))
4835 continue;
4836
4837 this_real_name = dw2_get_real_path (objfile, file_data, j);
4838 if (file_matcher (this_real_name, false))
4839 {
4840 per_cu->v.quick->mark = 1;
4841 break;
4842 }
4843 }
4844
b76e467d
SM
4845 void **slot = htab_find_slot (per_cu->v.quick->mark
4846 ? visited_found.get ()
4847 : visited_not_found.get (),
4848 file_data, INSERT);
927aa2e7
JK
4849 *slot = file_data;
4850 }
4851}
4852
4853static void
4854dw2_expand_symtabs_matching
4855 (struct objfile *objfile,
4856 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4857 const lookup_name_info &lookup_name,
4858 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4859 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4860 enum search_domain kind)
4861{
ed2dc618
SM
4862 struct dwarf2_per_objfile *dwarf2_per_objfile
4863 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4864
4865 /* index_table is NULL if OBJF_READNOW. */
4866 if (!dwarf2_per_objfile->index_table)
4867 return;
4868
ed2dc618 4869 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4870
4871 mapped_index &index = *dwarf2_per_objfile->index_table;
4872
4873 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4874 symbol_matcher,
4875 kind, [&] (offset_type idx)
4876 {
ed2dc618 4877 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4878 expansion_notify, kind);
3b00ef10 4879 return true;
927aa2e7
JK
4880 });
4881}
4882
4883/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4884 symtab. */
4885
4886static struct compunit_symtab *
4887recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4888 CORE_ADDR pc)
4889{
4890 int i;
4891
4892 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4893 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4894 return cust;
4895
4896 if (cust->includes == NULL)
4897 return NULL;
4898
4899 for (i = 0; cust->includes[i]; ++i)
4900 {
4901 struct compunit_symtab *s = cust->includes[i];
4902
4903 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4904 if (s != NULL)
4905 return s;
4906 }
4907
4908 return NULL;
4909}
4910
4911static struct compunit_symtab *
4912dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4913 struct bound_minimal_symbol msymbol,
4914 CORE_ADDR pc,
4915 struct obj_section *section,
4916 int warn_if_readin)
4917{
4918 struct dwarf2_per_cu_data *data;
4919 struct compunit_symtab *result;
4920
d320c2b5 4921 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4922 return NULL;
4923
b3b3bada 4924 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4925 data = (struct dwarf2_per_cu_data *) addrmap_find
4926 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4927 if (!data)
4928 return NULL;
4929
4930 if (warn_if_readin && data->v.quick->compunit_symtab)
4931 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4932 paddress (get_objfile_arch (objfile), pc));
4933
4934 result
58f0c718
TT
4935 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4936 false),
927aa2e7
JK
4937 pc);
4938 gdb_assert (result != NULL);
4939 return result;
4940}
4941
4942static void
4943dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4944 void *data, int need_fullname)
4945{
ed2dc618
SM
4946 struct dwarf2_per_objfile *dwarf2_per_objfile
4947 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4948
4949 if (!dwarf2_per_objfile->filenames_cache)
4950 {
4951 dwarf2_per_objfile->filenames_cache.emplace ();
4952
4953 htab_up visited (htab_create_alloc (10,
4954 htab_hash_pointer, htab_eq_pointer,
4955 NULL, xcalloc, xfree));
4956
4957 /* The rule is CUs specify all the files, including those used
4958 by any TU, so there's no need to scan TUs here. We can
4959 ignore file names coming from already-expanded CUs. */
4960
b76e467d 4961 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4962 {
927aa2e7
JK
4963 if (per_cu->v.quick->compunit_symtab)
4964 {
4965 void **slot = htab_find_slot (visited.get (),
4966 per_cu->v.quick->file_names,
4967 INSERT);
4968
4969 *slot = per_cu->v.quick->file_names;
4970 }
4971 }
4972
b76e467d 4973 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4974 {
927aa2e7
JK
4975 /* We only need to look at symtabs not already expanded. */
4976 if (per_cu->v.quick->compunit_symtab)
4977 continue;
4978
b76e467d 4979 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4980 if (file_data == NULL)
4981 continue;
4982
b76e467d 4983 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4984 if (*slot)
4985 {
4986 /* Already visited. */
4987 continue;
4988 }
4989 *slot = file_data;
4990
4991 for (int j = 0; j < file_data->num_file_names; ++j)
4992 {
4993 const char *filename = file_data->file_names[j];
4994 dwarf2_per_objfile->filenames_cache->seen (filename);
4995 }
4996 }
4997 }
4998
4999 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5000 {
5001 gdb::unique_xmalloc_ptr<char> this_real_name;
5002
5003 if (need_fullname)
5004 this_real_name = gdb_realpath (filename);
5005 (*fun) (filename, this_real_name.get (), data);
5006 });
5007}
5008
5009static int
5010dw2_has_symbols (struct objfile *objfile)
5011{
5012 return 1;
5013}
5014
5015const struct quick_symbol_functions dwarf2_gdb_index_functions =
5016{
5017 dw2_has_symbols,
5018 dw2_find_last_source_symtab,
5019 dw2_forget_cached_source_info,
5020 dw2_map_symtabs_matching_filename,
5021 dw2_lookup_symbol,
5022 dw2_print_stats,
5023 dw2_dump,
927aa2e7
JK
5024 dw2_expand_symtabs_for_function,
5025 dw2_expand_all_symtabs,
5026 dw2_expand_symtabs_with_fullname,
5027 dw2_map_matching_symbols,
5028 dw2_expand_symtabs_matching,
5029 dw2_find_pc_sect_compunit_symtab,
5030 NULL,
5031 dw2_map_symbol_filenames
5032};
5033
5034/* DWARF-5 debug_names reader. */
5035
5036/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5037static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5038
5039/* A helper function that reads the .debug_names section in SECTION
5040 and fills in MAP. FILENAME is the name of the file containing the
5041 section; it is used for error reporting.
5042
5043 Returns true if all went well, false otherwise. */
5044
5045static bool
5046read_debug_names_from_section (struct objfile *objfile,
5047 const char *filename,
5048 struct dwarf2_section_info *section,
5049 mapped_debug_names &map)
5050{
96b79293 5051 if (section->empty ())
927aa2e7
JK
5052 return false;
5053
5054 /* Older elfutils strip versions could keep the section in the main
5055 executable while splitting it for the separate debug info file. */
96b79293 5056 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5057 return false;
5058
96b79293 5059 section->read (objfile);
927aa2e7
JK
5060
5061 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5062
5063 const gdb_byte *addr = section->buffer;
5064
96b79293 5065 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5066
5067 unsigned int bytes_read;
5068 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5069 addr += bytes_read;
5070
5071 map.dwarf5_is_dwarf64 = bytes_read != 4;
5072 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5073 if (bytes_read + length != section->size)
5074 {
5075 /* There may be multiple per-CU indices. */
5076 warning (_("Section .debug_names in %s length %s does not match "
5077 "section length %s, ignoring .debug_names."),
5078 filename, plongest (bytes_read + length),
5079 pulongest (section->size));
5080 return false;
5081 }
5082
5083 /* The version number. */
5084 uint16_t version = read_2_bytes (abfd, addr);
5085 addr += 2;
5086 if (version != 5)
5087 {
5088 warning (_("Section .debug_names in %s has unsupported version %d, "
5089 "ignoring .debug_names."),
5090 filename, version);
5091 return false;
5092 }
5093
5094 /* Padding. */
5095 uint16_t padding = read_2_bytes (abfd, addr);
5096 addr += 2;
5097 if (padding != 0)
5098 {
5099 warning (_("Section .debug_names in %s has unsupported padding %d, "
5100 "ignoring .debug_names."),
5101 filename, padding);
5102 return false;
5103 }
5104
5105 /* comp_unit_count - The number of CUs in the CU list. */
5106 map.cu_count = read_4_bytes (abfd, addr);
5107 addr += 4;
5108
5109 /* local_type_unit_count - The number of TUs in the local TU
5110 list. */
5111 map.tu_count = read_4_bytes (abfd, addr);
5112 addr += 4;
5113
5114 /* foreign_type_unit_count - The number of TUs in the foreign TU
5115 list. */
5116 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5117 addr += 4;
5118 if (foreign_tu_count != 0)
5119 {
5120 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5121 "ignoring .debug_names."),
5122 filename, static_cast<unsigned long> (foreign_tu_count));
5123 return false;
5124 }
5125
5126 /* bucket_count - The number of hash buckets in the hash lookup
5127 table. */
5128 map.bucket_count = read_4_bytes (abfd, addr);
5129 addr += 4;
5130
5131 /* name_count - The number of unique names in the index. */
5132 map.name_count = read_4_bytes (abfd, addr);
5133 addr += 4;
5134
5135 /* abbrev_table_size - The size in bytes of the abbreviations
5136 table. */
5137 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5138 addr += 4;
5139
5140 /* augmentation_string_size - The size in bytes of the augmentation
5141 string. This value is rounded up to a multiple of 4. */
5142 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5143 addr += 4;
5144 map.augmentation_is_gdb = ((augmentation_string_size
5145 == sizeof (dwarf5_augmentation))
5146 && memcmp (addr, dwarf5_augmentation,
5147 sizeof (dwarf5_augmentation)) == 0);
5148 augmentation_string_size += (-augmentation_string_size) & 3;
5149 addr += augmentation_string_size;
5150
5151 /* List of CUs */
5152 map.cu_table_reordered = addr;
5153 addr += map.cu_count * map.offset_size;
5154
5155 /* List of Local TUs */
5156 map.tu_table_reordered = addr;
5157 addr += map.tu_count * map.offset_size;
5158
5159 /* Hash Lookup Table */
5160 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5161 addr += map.bucket_count * 4;
5162 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5163 addr += map.name_count * 4;
5164
5165 /* Name Table */
5166 map.name_table_string_offs_reordered = addr;
5167 addr += map.name_count * map.offset_size;
5168 map.name_table_entry_offs_reordered = addr;
5169 addr += map.name_count * map.offset_size;
5170
5171 const gdb_byte *abbrev_table_start = addr;
5172 for (;;)
5173 {
927aa2e7
JK
5174 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5175 addr += bytes_read;
5176 if (index_num == 0)
5177 break;
5178
5179 const auto insertpair
5180 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5181 if (!insertpair.second)
5182 {
5183 warning (_("Section .debug_names in %s has duplicate index %s, "
5184 "ignoring .debug_names."),
5185 filename, pulongest (index_num));
5186 return false;
5187 }
5188 mapped_debug_names::index_val &indexval = insertpair.first->second;
5189 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5190 addr += bytes_read;
5191
5192 for (;;)
5193 {
5194 mapped_debug_names::index_val::attr attr;
5195 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5196 addr += bytes_read;
5197 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5198 addr += bytes_read;
5199 if (attr.form == DW_FORM_implicit_const)
5200 {
5201 attr.implicit_const = read_signed_leb128 (abfd, addr,
5202 &bytes_read);
5203 addr += bytes_read;
5204 }
5205 if (attr.dw_idx == 0 && attr.form == 0)
5206 break;
5207 indexval.attr_vec.push_back (std::move (attr));
5208 }
5209 }
5210 if (addr != abbrev_table_start + abbrev_table_size)
5211 {
5212 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5213 "of size %s vs. written as %u, ignoring .debug_names."),
5214 filename, plongest (addr - abbrev_table_start),
5215 abbrev_table_size);
927aa2e7
JK
5216 return false;
5217 }
5218 map.entry_pool = addr;
5219
5220 return true;
5221}
5222
5223/* A helper for create_cus_from_debug_names that handles the MAP's CU
5224 list. */
5225
5226static void
ed2dc618 5227create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5228 const mapped_debug_names &map,
5229 dwarf2_section_info &section,
b76e467d 5230 bool is_dwz)
927aa2e7
JK
5231{
5232 sect_offset sect_off_prev;
5233 for (uint32_t i = 0; i <= map.cu_count; ++i)
5234 {
5235 sect_offset sect_off_next;
5236 if (i < map.cu_count)
5237 {
5238 sect_off_next
5239 = (sect_offset) (extract_unsigned_integer
5240 (map.cu_table_reordered + i * map.offset_size,
5241 map.offset_size,
5242 map.dwarf5_byte_order));
5243 }
5244 else
5245 sect_off_next = (sect_offset) section.size;
5246 if (i >= 1)
5247 {
5248 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5249 dwarf2_per_cu_data *per_cu
ed2dc618 5250 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5251 sect_off_prev, length);
b76e467d 5252 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5253 }
5254 sect_off_prev = sect_off_next;
5255 }
5256}
5257
5258/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5259 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5260
5261static void
ed2dc618 5262create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5263 const mapped_debug_names &map,
5264 const mapped_debug_names &dwz_map)
5265{
b76e467d
SM
5266 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5267 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5268
ed2dc618
SM
5269 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5270 dwarf2_per_objfile->info,
b76e467d 5271 false /* is_dwz */);
927aa2e7
JK
5272
5273 if (dwz_map.cu_count == 0)
5274 return;
5275
ed2dc618
SM
5276 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5277 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5278 true /* is_dwz */);
927aa2e7
JK
5279}
5280
5281/* Read .debug_names. If everything went ok, initialize the "quick"
5282 elements of all the CUs and return true. Otherwise, return false. */
5283
5284static bool
ed2dc618 5285dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5286{
22ca247e
TT
5287 std::unique_ptr<mapped_debug_names> map
5288 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5289 mapped_debug_names dwz_map (dwarf2_per_objfile);
5290 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5291
5292 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5293 &dwarf2_per_objfile->debug_names,
22ca247e 5294 *map))
927aa2e7
JK
5295 return false;
5296
5297 /* Don't use the index if it's empty. */
22ca247e 5298 if (map->name_count == 0)
927aa2e7
JK
5299 return false;
5300
5301 /* If there is a .dwz file, read it so we can get its CU list as
5302 well. */
ed2dc618 5303 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5304 if (dwz != NULL)
5305 {
5306 if (!read_debug_names_from_section (objfile,
00f93c44 5307 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5308 &dwz->debug_names, dwz_map))
5309 {
5310 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5311 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5312 return false;
5313 }
5314 }
5315
22ca247e 5316 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5317
22ca247e 5318 if (map->tu_count != 0)
927aa2e7
JK
5319 {
5320 /* We can only handle a single .debug_types when we have an
5321 index. */
fd5866f6 5322 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5323 return false;
5324
fd5866f6 5325 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5326
5327 create_signatured_type_table_from_debug_names
22ca247e 5328 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5329 }
5330
ed2dc618
SM
5331 create_addrmap_from_aranges (dwarf2_per_objfile,
5332 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5333
22ca247e 5334 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5335 dwarf2_per_objfile->using_index = 1;
5336 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5337 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5338
5339 return true;
5340}
5341
927aa2e7
JK
5342/* Type used to manage iterating over all CUs looking for a symbol for
5343 .debug_names. */
5344
5345class dw2_debug_names_iterator
5346{
5347public:
927aa2e7 5348 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5349 gdb::optional<block_enum> block_index,
5350 domain_enum domain,
927aa2e7 5351 const char *name)
2b79f376 5352 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5353 m_addr (find_vec_in_debug_names (map, name))
5354 {}
5355
5356 dw2_debug_names_iterator (const mapped_debug_names &map,
5357 search_domain search, uint32_t namei)
5358 : m_map (map),
5359 m_search (search),
5360 m_addr (find_vec_in_debug_names (map, namei))
5361 {}
5362
3b00ef10
TT
5363 dw2_debug_names_iterator (const mapped_debug_names &map,
5364 block_enum block_index, domain_enum domain,
5365 uint32_t namei)
5366 : m_map (map), m_block_index (block_index), m_domain (domain),
5367 m_addr (find_vec_in_debug_names (map, namei))
5368 {}
5369
927aa2e7
JK
5370 /* Return the next matching CU or NULL if there are no more. */
5371 dwarf2_per_cu_data *next ();
5372
5373private:
5374 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5375 const char *name);
5376 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5377 uint32_t namei);
5378
5379 /* The internalized form of .debug_names. */
5380 const mapped_debug_names &m_map;
5381
2b79f376
SM
5382 /* If set, only look for symbols that match that block. Valid values are
5383 GLOBAL_BLOCK and STATIC_BLOCK. */
5384 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5385
5386 /* The kind of symbol we're looking for. */
5387 const domain_enum m_domain = UNDEF_DOMAIN;
5388 const search_domain m_search = ALL_DOMAIN;
5389
5390 /* The list of CUs from the index entry of the symbol, or NULL if
5391 not found. */
5392 const gdb_byte *m_addr;
5393};
5394
5395const char *
5396mapped_debug_names::namei_to_name (uint32_t namei) const
5397{
5398 const ULONGEST namei_string_offs
5399 = extract_unsigned_integer ((name_table_string_offs_reordered
5400 + namei * offset_size),
5401 offset_size,
5402 dwarf5_byte_order);
5403 return read_indirect_string_at_offset
ed2dc618 5404 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5405}
5406
5407/* Find a slot in .debug_names for the object named NAME. If NAME is
5408 found, return pointer to its pool data. If NAME cannot be found,
5409 return NULL. */
5410
5411const gdb_byte *
5412dw2_debug_names_iterator::find_vec_in_debug_names
5413 (const mapped_debug_names &map, const char *name)
5414{
5415 int (*cmp) (const char *, const char *);
5416
54ee4252 5417 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5418 if (current_language->la_language == language_cplus
5419 || current_language->la_language == language_fortran
5420 || current_language->la_language == language_d)
5421 {
5422 /* NAME is already canonical. Drop any qualifiers as
5423 .debug_names does not contain any. */
5424
5425 if (strchr (name, '(') != NULL)
5426 {
54ee4252 5427 without_params = cp_remove_params (name);
927aa2e7 5428 if (without_params != NULL)
54ee4252 5429 name = without_params.get ();
927aa2e7
JK
5430 }
5431 }
5432
5433 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5434
5435 const uint32_t full_hash = dwarf5_djb_hash (name);
5436 uint32_t namei
5437 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5438 (map.bucket_table_reordered
5439 + (full_hash % map.bucket_count)), 4,
5440 map.dwarf5_byte_order);
5441 if (namei == 0)
5442 return NULL;
5443 --namei;
5444 if (namei >= map.name_count)
5445 {
b98664d3 5446 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5447 "[in module %s]"),
5448 namei, map.name_count,
ed2dc618 5449 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5450 return NULL;
5451 }
5452
5453 for (;;)
5454 {
5455 const uint32_t namei_full_hash
5456 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5457 (map.hash_table_reordered + namei), 4,
5458 map.dwarf5_byte_order);
5459 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5460 return NULL;
5461
5462 if (full_hash == namei_full_hash)
5463 {
5464 const char *const namei_string = map.namei_to_name (namei);
5465
5466#if 0 /* An expensive sanity check. */
5467 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5468 {
b98664d3 5469 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5470 "[in module %s]"),
5471 namei, objfile_name (dwarf2_per_objfile->objfile));
5472 return NULL;
5473 }
5474#endif
5475
5476 if (cmp (namei_string, name) == 0)
5477 {
5478 const ULONGEST namei_entry_offs
5479 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5480 + namei * map.offset_size),
5481 map.offset_size, map.dwarf5_byte_order);
5482 return map.entry_pool + namei_entry_offs;
5483 }
5484 }
5485
5486 ++namei;
5487 if (namei >= map.name_count)
5488 return NULL;
5489 }
5490}
5491
5492const gdb_byte *
5493dw2_debug_names_iterator::find_vec_in_debug_names
5494 (const mapped_debug_names &map, uint32_t namei)
5495{
5496 if (namei >= map.name_count)
5497 {
b98664d3 5498 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5499 "[in module %s]"),
5500 namei, map.name_count,
ed2dc618 5501 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5502 return NULL;
5503 }
5504
5505 const ULONGEST namei_entry_offs
5506 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5507 + namei * map.offset_size),
5508 map.offset_size, map.dwarf5_byte_order);
5509 return map.entry_pool + namei_entry_offs;
5510}
5511
5512/* See dw2_debug_names_iterator. */
5513
5514dwarf2_per_cu_data *
5515dw2_debug_names_iterator::next ()
5516{
5517 if (m_addr == NULL)
5518 return NULL;
5519
ed2dc618
SM
5520 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5521 struct objfile *objfile = dwarf2_per_objfile->objfile;
5522 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5523
5524 again:
5525
5526 unsigned int bytes_read;
5527 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5528 m_addr += bytes_read;
5529 if (abbrev == 0)
5530 return NULL;
5531
5532 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5533 if (indexval_it == m_map.abbrev_map.cend ())
5534 {
b98664d3 5535 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5536 "[in module %s]"),
ed2dc618 5537 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5538 return NULL;
5539 }
5540 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5541 enum class symbol_linkage {
5542 unknown,
5543 static_,
5544 extern_,
23c13d42 5545 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5546 dwarf2_per_cu_data *per_cu = NULL;
5547 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5548 {
5549 ULONGEST ull;
5550 switch (attr.form)
5551 {
5552 case DW_FORM_implicit_const:
5553 ull = attr.implicit_const;
5554 break;
5555 case DW_FORM_flag_present:
5556 ull = 1;
5557 break;
5558 case DW_FORM_udata:
5559 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5560 m_addr += bytes_read;
5561 break;
5562 default:
b98664d3 5563 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5564 dwarf_form_name (attr.form),
ed2dc618 5565 objfile_name (objfile));
927aa2e7
JK
5566 return NULL;
5567 }
5568 switch (attr.dw_idx)
5569 {
5570 case DW_IDX_compile_unit:
5571 /* Don't crash on bad data. */
b76e467d 5572 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5573 {
b98664d3 5574 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5575 " [in module %s]"),
5576 pulongest (ull),
5577 objfile_name (dwarf2_per_objfile->objfile));
5578 continue;
5579 }
ff4c9fec 5580 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5581 break;
8af5c486
JK
5582 case DW_IDX_type_unit:
5583 /* Don't crash on bad data. */
b2bdb8cf 5584 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5585 {
b98664d3 5586 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5587 " [in module %s]"),
5588 pulongest (ull),
5589 objfile_name (dwarf2_per_objfile->objfile));
5590 continue;
5591 }
ff4c9fec 5592 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5593 break;
927aa2e7
JK
5594 case DW_IDX_GNU_internal:
5595 if (!m_map.augmentation_is_gdb)
5596 break;
23c13d42 5597 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5598 break;
5599 case DW_IDX_GNU_external:
5600 if (!m_map.augmentation_is_gdb)
5601 break;
23c13d42 5602 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5603 break;
5604 }
5605 }
5606
5607 /* Skip if already read in. */
5608 if (per_cu->v.quick->compunit_symtab)
5609 goto again;
5610
5611 /* Check static vs global. */
23c13d42 5612 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5613 {
2b79f376 5614 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5615 const bool symbol_is_static =
5616 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5617 if (want_static != symbol_is_static)
2b79f376 5618 goto again;
927aa2e7
JK
5619 }
5620
5621 /* Match dw2_symtab_iter_next, symbol_kind
5622 and debug_names::psymbol_tag. */
5623 switch (m_domain)
5624 {
5625 case VAR_DOMAIN:
5626 switch (indexval.dwarf_tag)
5627 {
5628 case DW_TAG_variable:
5629 case DW_TAG_subprogram:
5630 /* Some types are also in VAR_DOMAIN. */
5631 case DW_TAG_typedef:
5632 case DW_TAG_structure_type:
5633 break;
5634 default:
5635 goto again;
5636 }
5637 break;
5638 case STRUCT_DOMAIN:
5639 switch (indexval.dwarf_tag)
5640 {
5641 case DW_TAG_typedef:
5642 case DW_TAG_structure_type:
5643 break;
5644 default:
5645 goto again;
5646 }
5647 break;
5648 case LABEL_DOMAIN:
5649 switch (indexval.dwarf_tag)
5650 {
5651 case 0:
5652 case DW_TAG_variable:
5653 break;
5654 default:
5655 goto again;
5656 }
5657 break;
59c35742
AB
5658 case MODULE_DOMAIN:
5659 switch (indexval.dwarf_tag)
5660 {
5661 case DW_TAG_module:
5662 break;
5663 default:
5664 goto again;
5665 }
5666 break;
927aa2e7
JK
5667 default:
5668 break;
5669 }
5670
5671 /* Match dw2_expand_symtabs_matching, symbol_kind and
5672 debug_names::psymbol_tag. */
5673 switch (m_search)
4b514bc8 5674 {
927aa2e7
JK
5675 case VARIABLES_DOMAIN:
5676 switch (indexval.dwarf_tag)
4b514bc8 5677 {
927aa2e7
JK
5678 case DW_TAG_variable:
5679 break;
5680 default:
5681 goto again;
4b514bc8 5682 }
927aa2e7
JK
5683 break;
5684 case FUNCTIONS_DOMAIN:
5685 switch (indexval.dwarf_tag)
4b514bc8 5686 {
927aa2e7
JK
5687 case DW_TAG_subprogram:
5688 break;
5689 default:
5690 goto again;
4b514bc8 5691 }
927aa2e7
JK
5692 break;
5693 case TYPES_DOMAIN:
5694 switch (indexval.dwarf_tag)
5695 {
5696 case DW_TAG_typedef:
5697 case DW_TAG_structure_type:
5698 break;
5699 default:
5700 goto again;
5701 }
5702 break;
59c35742
AB
5703 case MODULES_DOMAIN:
5704 switch (indexval.dwarf_tag)
5705 {
5706 case DW_TAG_module:
5707 break;
5708 default:
5709 goto again;
5710 }
927aa2e7
JK
5711 default:
5712 break;
4b514bc8 5713 }
927aa2e7
JK
5714
5715 return per_cu;
4b514bc8 5716}
61920122 5717
927aa2e7 5718static struct compunit_symtab *
c7f839cb 5719dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5720 const char *name, domain_enum domain)
4b514bc8 5721{
ed2dc618
SM
5722 struct dwarf2_per_objfile *dwarf2_per_objfile
5723 = get_dwarf2_per_objfile (objfile);
61920122 5724
927aa2e7
JK
5725 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5726 if (!mapp)
61920122 5727 {
927aa2e7
JK
5728 /* index is NULL if OBJF_READNOW. */
5729 return NULL;
5730 }
5731 const auto &map = *mapp;
9291a0cd 5732
2b79f376 5733 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5734
927aa2e7
JK
5735 struct compunit_symtab *stab_best = NULL;
5736 struct dwarf2_per_cu_data *per_cu;
5737 while ((per_cu = iter.next ()) != NULL)
5738 {
5739 struct symbol *sym, *with_opaque = NULL;
58f0c718 5740 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5741 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5742 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5743
927aa2e7
JK
5744 sym = block_find_symbol (block, name, domain,
5745 block_find_non_opaque_type_preferred,
5746 &with_opaque);
9703b513 5747
927aa2e7
JK
5748 /* Some caution must be observed with overloaded functions and
5749 methods, since the index will not contain any overload
5750 information (but NAME might contain it). */
a3ec0bb1 5751
927aa2e7 5752 if (sym != NULL
987012b8 5753 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5754 return stab;
5755 if (with_opaque != NULL
987012b8 5756 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5757 stab_best = stab;
9703b513 5758
927aa2e7 5759 /* Keep looking through other CUs. */
9703b513
TT
5760 }
5761
927aa2e7 5762 return stab_best;
9703b513
TT
5763}
5764
927aa2e7
JK
5765/* This dumps minimal information about .debug_names. It is called
5766 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5767 uses this to verify that .debug_names has been loaded. */
9291a0cd 5768
927aa2e7
JK
5769static void
5770dw2_debug_names_dump (struct objfile *objfile)
5771{
ed2dc618
SM
5772 struct dwarf2_per_objfile *dwarf2_per_objfile
5773 = get_dwarf2_per_objfile (objfile);
5774
927aa2e7
JK
5775 gdb_assert (dwarf2_per_objfile->using_index);
5776 printf_filtered (".debug_names:");
5777 if (dwarf2_per_objfile->debug_names_table)
5778 printf_filtered (" exists\n");
5779 else
5780 printf_filtered (" faked for \"readnow\"\n");
5781 printf_filtered ("\n");
9291a0cd
TT
5782}
5783
9291a0cd 5784static void
927aa2e7
JK
5785dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5786 const char *func_name)
9291a0cd 5787{
ed2dc618
SM
5788 struct dwarf2_per_objfile *dwarf2_per_objfile
5789 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5790
927aa2e7
JK
5791 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5792 if (dwarf2_per_objfile->debug_names_table)
24c79950 5793 {
927aa2e7 5794 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5795
2b79f376 5796 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5797
927aa2e7
JK
5798 struct dwarf2_per_cu_data *per_cu;
5799 while ((per_cu = iter.next ()) != NULL)
58f0c718 5800 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5801 }
5802}
24c79950 5803
3b00ef10
TT
5804static void
5805dw2_debug_names_map_matching_symbols
5806 (struct objfile *objfile,
5807 const lookup_name_info &name, domain_enum domain,
5808 int global,
5809 gdb::function_view<symbol_found_callback_ftype> callback,
5810 symbol_compare_ftype *ordered_compare)
5811{
5812 struct dwarf2_per_objfile *dwarf2_per_objfile
5813 = get_dwarf2_per_objfile (objfile);
5814
5815 /* debug_names_table is NULL if OBJF_READNOW. */
5816 if (!dwarf2_per_objfile->debug_names_table)
5817 return;
5818
5819 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5820 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5821
5822 const char *match_name = name.ada ().lookup_name ().c_str ();
5823 auto matcher = [&] (const char *symname)
5824 {
5825 if (ordered_compare == nullptr)
5826 return true;
5827 return ordered_compare (symname, match_name) == 0;
5828 };
5829
5830 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5831 [&] (offset_type namei)
5832 {
5833 /* The name was matched, now expand corresponding CUs that were
5834 marked. */
5835 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5836
5837 struct dwarf2_per_cu_data *per_cu;
5838 while ((per_cu = iter.next ()) != NULL)
5839 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5840 return true;
5841 });
5842
5843 /* It's a shame we couldn't do this inside the
5844 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5845 that have already been expanded. Instead, this loop matches what
5846 the psymtab code does. */
5847 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5848 {
5849 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5850 if (cust != nullptr)
5851 {
5852 const struct block *block
5853 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5854 if (!iterate_over_symbols_terminated (block, name,
5855 domain, callback))
5856 break;
5857 }
5858 }
5859}
5860
927aa2e7
JK
5861static void
5862dw2_debug_names_expand_symtabs_matching
5863 (struct objfile *objfile,
5864 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5865 const lookup_name_info &lookup_name,
5866 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5867 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5868 enum search_domain kind)
5869{
ed2dc618
SM
5870 struct dwarf2_per_objfile *dwarf2_per_objfile
5871 = get_dwarf2_per_objfile (objfile);
9291a0cd 5872
927aa2e7
JK
5873 /* debug_names_table is NULL if OBJF_READNOW. */
5874 if (!dwarf2_per_objfile->debug_names_table)
5875 return;
9291a0cd 5876
ed2dc618 5877 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5878
44ed8f3e 5879 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5880
44ed8f3e
PA
5881 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5882 symbol_matcher,
5883 kind, [&] (offset_type namei)
927aa2e7 5884 {
927aa2e7
JK
5885 /* The name was matched, now expand corresponding CUs that were
5886 marked. */
5887 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5888
927aa2e7
JK
5889 struct dwarf2_per_cu_data *per_cu;
5890 while ((per_cu = iter.next ()) != NULL)
5891 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5892 expansion_notify);
3b00ef10 5893 return true;
44ed8f3e 5894 });
9291a0cd
TT
5895}
5896
927aa2e7 5897const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5898{
5899 dw2_has_symbols,
5900 dw2_find_last_source_symtab,
5901 dw2_forget_cached_source_info,
f8eba3c6 5902 dw2_map_symtabs_matching_filename,
927aa2e7 5903 dw2_debug_names_lookup_symbol,
9291a0cd 5904 dw2_print_stats,
927aa2e7 5905 dw2_debug_names_dump,
927aa2e7 5906 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5907 dw2_expand_all_symtabs,
652a8996 5908 dw2_expand_symtabs_with_fullname,
3b00ef10 5909 dw2_debug_names_map_matching_symbols,
927aa2e7 5910 dw2_debug_names_expand_symtabs_matching,
43f3e411 5911 dw2_find_pc_sect_compunit_symtab,
71a3c369 5912 NULL,
9291a0cd
TT
5913 dw2_map_symbol_filenames
5914};
5915
4485a1c1
SM
5916/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5917 to either a dwarf2_per_objfile or dwz_file object. */
5918
5919template <typename T>
5920static gdb::array_view<const gdb_byte>
5921get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5922{
5923 dwarf2_section_info *section = &section_owner->gdb_index;
5924
96b79293 5925 if (section->empty ())
4485a1c1
SM
5926 return {};
5927
5928 /* Older elfutils strip versions could keep the section in the main
5929 executable while splitting it for the separate debug info file. */
96b79293 5930 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5931 return {};
5932
96b79293 5933 section->read (obj);
4485a1c1 5934
8bebfcda
PA
5935 /* dwarf2_section_info::size is a bfd_size_type, while
5936 gdb::array_view works with size_t. On 32-bit hosts, with
5937 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5938 is 32-bit. So we need an explicit narrowing conversion here.
5939 This is fine, because it's impossible to allocate or mmap an
5940 array/buffer larger than what size_t can represent. */
5941 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5942}
5943
87d6a7aa
SM
5944/* Lookup the index cache for the contents of the index associated to
5945 DWARF2_OBJ. */
5946
5947static gdb::array_view<const gdb_byte>
5948get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5949{
5950 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5951 if (build_id == nullptr)
5952 return {};
5953
5954 return global_index_cache.lookup_gdb_index (build_id,
5955 &dwarf2_obj->index_cache_res);
5956}
5957
5958/* Same as the above, but for DWZ. */
5959
5960static gdb::array_view<const gdb_byte>
5961get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5962{
5963 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5964 if (build_id == nullptr)
5965 return {};
5966
5967 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5968}
5969
3c0aa29a 5970/* See symfile.h. */
9291a0cd 5971
3c0aa29a
PA
5972bool
5973dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5974{
ed2dc618
SM
5975 struct dwarf2_per_objfile *dwarf2_per_objfile
5976 = get_dwarf2_per_objfile (objfile);
5977
9291a0cd
TT
5978 /* If we're about to read full symbols, don't bother with the
5979 indices. In this case we also don't care if some other debug
5980 format is making psymtabs, because they are all about to be
5981 expanded anyway. */
5982 if ((objfile->flags & OBJF_READNOW))
5983 {
9291a0cd 5984 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5985 create_all_comp_units (dwarf2_per_objfile);
5986 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5987 dwarf2_per_objfile->quick_file_names_table
5988 = create_quick_file_names_table
5989 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5990
b76e467d 5991 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5992 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5993 {
ff4c9fec 5994 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5995
e254ef6a
DE
5996 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5997 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5998 }
5999
6000 /* Return 1 so that gdb sees the "quick" functions. However,
6001 these functions will be no-ops because we will have expanded
6002 all symtabs. */
3c0aa29a
PA
6003 *index_kind = dw_index_kind::GDB_INDEX;
6004 return true;
9291a0cd
TT
6005 }
6006
ed2dc618 6007 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6008 {
6009 *index_kind = dw_index_kind::DEBUG_NAMES;
6010 return true;
6011 }
927aa2e7 6012
4485a1c1
SM
6013 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6014 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6015 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6016 {
6017 *index_kind = dw_index_kind::GDB_INDEX;
6018 return true;
6019 }
9291a0cd 6020
87d6a7aa
SM
6021 /* ... otherwise, try to find the index in the index cache. */
6022 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6023 get_gdb_index_contents_from_cache,
6024 get_gdb_index_contents_from_cache_dwz))
6025 {
6026 global_index_cache.hit ();
6027 *index_kind = dw_index_kind::GDB_INDEX;
6028 return true;
6029 }
6030
6031 global_index_cache.miss ();
3c0aa29a 6032 return false;
9291a0cd
TT
6033}
6034
6035\f
6036
dce234bc
PP
6037/* Build a partial symbol table. */
6038
6039void
f29dff0a 6040dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6041{
ed2dc618
SM
6042 struct dwarf2_per_objfile *dwarf2_per_objfile
6043 = get_dwarf2_per_objfile (objfile);
c9bf0622 6044
6eee24ce 6045 init_psymbol_list (objfile, 1024);
c906108c 6046
a70b8144 6047 try
c9bf0622
TT
6048 {
6049 /* This isn't really ideal: all the data we allocate on the
6050 objfile's obstack is still uselessly kept around. However,
6051 freeing it seems unsafe. */
906768f9 6052 psymtab_discarder psymtabs (objfile);
ed2dc618 6053 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6054 psymtabs.keep ();
87d6a7aa
SM
6055
6056 /* (maybe) store an index in the cache. */
6057 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6058 }
230d2906 6059 catch (const gdb_exception_error &except)
492d29ea
PA
6060 {
6061 exception_print (gdb_stderr, except);
6062 }
c906108c 6063}
c906108c 6064
1ce1cefd
DE
6065/* Return the total length of the CU described by HEADER. */
6066
6067static unsigned int
6068get_cu_length (const struct comp_unit_head *header)
6069{
6070 return header->initial_length_size + header->length;
6071}
6072
9c541725 6073/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6074
9c541725
PA
6075static inline bool
6076offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6077{
9c541725
PA
6078 sect_offset bottom = cu_header->sect_off;
6079 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6080
9c541725 6081 return sect_off >= bottom && sect_off < top;
45452591
DE
6082}
6083
3b80fe9b
DE
6084/* Find the base address of the compilation unit for range lists and
6085 location lists. It will normally be specified by DW_AT_low_pc.
6086 In DWARF-3 draft 4, the base address could be overridden by
6087 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6088 compilation units with discontinuous ranges. */
6089
6090static void
6091dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6092{
6093 struct attribute *attr;
6094
6095 cu->base_known = 0;
6096 cu->base_address = 0;
6097
6098 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6099 if (attr != nullptr)
3b80fe9b 6100 {
cd6c91b4 6101 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6102 cu->base_known = 1;
6103 }
6104 else
6105 {
6106 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6107 if (attr != nullptr)
3b80fe9b 6108 {
cd6c91b4 6109 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6110 cu->base_known = 1;
6111 }
6112 }
6113}
6114
93311388 6115/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6116 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6117 NOTE: This leaves members offset, first_die_offset to be filled in
6118 by the caller. */
107d2387 6119
d521ce57 6120static const gdb_byte *
107d2387 6121read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6122 const gdb_byte *info_ptr,
6123 struct dwarf2_section_info *section,
6124 rcuh_kind section_kind)
107d2387
AC
6125{
6126 int signed_addr;
891d2f0b 6127 unsigned int bytes_read;
96b79293
TT
6128 const char *filename = section->get_file_name ();
6129 bfd *abfd = section->get_bfd_owner ();
c764a876
DE
6130
6131 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6132 cu_header->initial_length_size = bytes_read;
6133 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6134 info_ptr += bytes_read;
107d2387 6135 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6136 if (cu_header->version < 2 || cu_header->version > 5)
6137 error (_("Dwarf Error: wrong version in compilation unit header "
6138 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6139 cu_header->version, filename);
107d2387 6140 info_ptr += 2;
43988095
JK
6141 if (cu_header->version < 5)
6142 switch (section_kind)
6143 {
6144 case rcuh_kind::COMPILE:
6145 cu_header->unit_type = DW_UT_compile;
6146 break;
6147 case rcuh_kind::TYPE:
6148 cu_header->unit_type = DW_UT_type;
6149 break;
6150 default:
6151 internal_error (__FILE__, __LINE__,
6152 _("read_comp_unit_head: invalid section_kind"));
6153 }
6154 else
6155 {
6156 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6157 (read_1_byte (abfd, info_ptr));
6158 info_ptr += 1;
6159 switch (cu_header->unit_type)
6160 {
6161 case DW_UT_compile:
a084a2a6
AT
6162 case DW_UT_partial:
6163 case DW_UT_skeleton:
6164 case DW_UT_split_compile:
43988095
JK
6165 if (section_kind != rcuh_kind::COMPILE)
6166 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6167 "(is %s, should be %s) [in module %s]"),
6168 dwarf_unit_type_name (cu_header->unit_type),
6169 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6170 break;
6171 case DW_UT_type:
a084a2a6 6172 case DW_UT_split_type:
43988095
JK
6173 section_kind = rcuh_kind::TYPE;
6174 break;
6175 default:
6176 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6177 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6178 "[in module %s]"), cu_header->unit_type,
6179 dwarf_unit_type_name (DW_UT_compile),
6180 dwarf_unit_type_name (DW_UT_skeleton),
6181 dwarf_unit_type_name (DW_UT_split_compile),
6182 dwarf_unit_type_name (DW_UT_type),
6183 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6184 }
6185
6186 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6187 info_ptr += 1;
6188 }
9c541725
PA
6189 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6190 cu_header,
6191 &bytes_read);
613e1657 6192 info_ptr += bytes_read;
43988095
JK
6193 if (cu_header->version < 5)
6194 {
6195 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6196 info_ptr += 1;
6197 }
107d2387
AC
6198 signed_addr = bfd_get_sign_extend_vma (abfd);
6199 if (signed_addr < 0)
8e65ff28 6200 internal_error (__FILE__, __LINE__,
e2e0b3e5 6201 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6202 cu_header->signed_addr_p = signed_addr;
c764a876 6203
a084a2a6
AT
6204 bool header_has_signature = section_kind == rcuh_kind::TYPE
6205 || cu_header->unit_type == DW_UT_skeleton
6206 || cu_header->unit_type == DW_UT_split_compile;
43988095 6207
a084a2a6
AT
6208 if (header_has_signature)
6209 {
43988095
JK
6210 cu_header->signature = read_8_bytes (abfd, info_ptr);
6211 info_ptr += 8;
a084a2a6 6212 }
43988095 6213
a084a2a6
AT
6214 if (section_kind == rcuh_kind::TYPE)
6215 {
6216 LONGEST type_offset;
43988095
JK
6217 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6218 info_ptr += bytes_read;
9c541725
PA
6219 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6220 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6221 error (_("Dwarf Error: Too big type_offset in compilation unit "
6222 "header (is %s) [in module %s]"), plongest (type_offset),
6223 filename);
6224 }
6225
107d2387
AC
6226 return info_ptr;
6227}
6228
36586728
TT
6229/* Helper function that returns the proper abbrev section for
6230 THIS_CU. */
6231
6232static struct dwarf2_section_info *
6233get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6234{
6235 struct dwarf2_section_info *abbrev;
ed2dc618 6236 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6237
6238 if (this_cu->is_dwz)
ed2dc618 6239 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6240 else
6241 abbrev = &dwarf2_per_objfile->abbrev;
6242
6243 return abbrev;
6244}
6245
9ff913ba
DE
6246/* Subroutine of read_and_check_comp_unit_head and
6247 read_and_check_type_unit_head to simplify them.
6248 Perform various error checking on the header. */
6249
6250static void
ed2dc618
SM
6251error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6252 struct comp_unit_head *header,
4bdcc0c1
DE
6253 struct dwarf2_section_info *section,
6254 struct dwarf2_section_info *abbrev_section)
9ff913ba 6255{
96b79293 6256 const char *filename = section->get_file_name ();
9ff913ba 6257
9c541725 6258 if (to_underlying (header->abbrev_sect_off)
36586728 6259 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6260 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6261 "(offset %s + 6) [in module %s]"),
6262 sect_offset_str (header->abbrev_sect_off),
6263 sect_offset_str (header->sect_off),
9ff913ba
DE
6264 filename);
6265
9c541725 6266 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6267 avoid potential 32-bit overflow. */
9c541725 6268 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6269 > section->size)
9c541725 6270 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6271 "(offset %s + 0) [in module %s]"),
6272 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6273 filename);
6274}
6275
6276/* Read in a CU/TU header and perform some basic error checking.
6277 The contents of the header are stored in HEADER.
6278 The result is a pointer to the start of the first DIE. */
adabb602 6279
d521ce57 6280static const gdb_byte *
ed2dc618
SM
6281read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6282 struct comp_unit_head *header,
9ff913ba 6283 struct dwarf2_section_info *section,
4bdcc0c1 6284 struct dwarf2_section_info *abbrev_section,
d521ce57 6285 const gdb_byte *info_ptr,
43988095 6286 rcuh_kind section_kind)
72bf9492 6287{
d521ce57 6288 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6289
9c541725 6290 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6291
43988095 6292 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6293
9c541725 6294 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6295
ed2dc618
SM
6296 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6297 abbrev_section);
9ff913ba
DE
6298
6299 return info_ptr;
348e048f
DE
6300}
6301
f4dc4d17
DE
6302/* Fetch the abbreviation table offset from a comp or type unit header. */
6303
6304static sect_offset
ed2dc618
SM
6305read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6306 struct dwarf2_section_info *section,
9c541725 6307 sect_offset sect_off)
f4dc4d17 6308{
96b79293 6309 bfd *abfd = section->get_bfd_owner ();
d521ce57 6310 const gdb_byte *info_ptr;
ac298888 6311 unsigned int initial_length_size, offset_size;
43988095 6312 uint16_t version;
f4dc4d17 6313
96b79293 6314 section->read (dwarf2_per_objfile->objfile);
9c541725 6315 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6316 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6317 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6318 info_ptr += initial_length_size;
6319
6320 version = read_2_bytes (abfd, info_ptr);
6321 info_ptr += 2;
6322 if (version >= 5)
6323 {
6324 /* Skip unit type and address size. */
6325 info_ptr += 2;
6326 }
6327
9c541725 6328 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6329}
6330
aaa75496
JB
6331/* Allocate a new partial symtab for file named NAME and mark this new
6332 partial symtab as being an include of PST. */
6333
6334static void
891813be 6335dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6336 struct objfile *objfile)
6337{
891813be 6338 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 6339
fbd9ab74
JK
6340 if (!IS_ABSOLUTE_PATH (subpst->filename))
6341 {
6342 /* It shares objfile->objfile_obstack. */
6343 subpst->dirname = pst->dirname;
6344 }
6345
a9342b62 6346 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6347 subpst->dependencies[0] = pst;
6348 subpst->number_of_dependencies = 1;
6349
aaa75496
JB
6350 /* No private part is necessary for include psymtabs. This property
6351 can be used to differentiate between such include psymtabs and
10b3939b 6352 the regular ones. */
891813be 6353 subpst->per_cu_data = nullptr;
aaa75496
JB
6354}
6355
6356/* Read the Line Number Program data and extract the list of files
6357 included by the source file represented by PST. Build an include
d85a05f0 6358 partial symtab for each of these included files. */
aaa75496
JB
6359
6360static void
6361dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6362 struct die_info *die,
891813be 6363 dwarf2_psymtab *pst)
aaa75496 6364{
fff8551c 6365 line_header_up lh;
d85a05f0 6366 struct attribute *attr;
aaa75496 6367
d85a05f0 6368 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6369 if (attr != nullptr)
9c541725 6370 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6371 if (lh == NULL)
6372 return; /* No linetable, so no includes. */
6373
79748972
TT
6374 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6375 that we pass in the raw text_low here; that is ok because we're
6376 only decoding the line table to make include partial symtabs, and
6377 so the addresses aren't really used. */
4ae976d1 6378 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6379 pst->raw_text_low (), 1);
aaa75496
JB
6380}
6381
348e048f 6382static hashval_t
52dc124a 6383hash_signatured_type (const void *item)
348e048f 6384{
9a3c8263
SM
6385 const struct signatured_type *sig_type
6386 = (const struct signatured_type *) item;
9a619af0 6387
348e048f 6388 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6389 return sig_type->signature;
348e048f
DE
6390}
6391
6392static int
52dc124a 6393eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6394{
9a3c8263
SM
6395 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6396 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6397
348e048f
DE
6398 return lhs->signature == rhs->signature;
6399}
6400
1fd400ff
TT
6401/* Allocate a hash table for signatured types. */
6402
6403static htab_t
673bfd45 6404allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6405{
6406 return htab_create_alloc_ex (41,
52dc124a
DE
6407 hash_signatured_type,
6408 eq_signatured_type,
1fd400ff
TT
6409 NULL,
6410 &objfile->objfile_obstack,
6411 hashtab_obstack_allocate,
6412 dummy_obstack_deallocate);
6413}
6414
d467dd73 6415/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6416
6417static int
d467dd73 6418add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6419{
9a3c8263 6420 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6421 std::vector<signatured_type *> *all_type_units
6422 = (std::vector<signatured_type *> *) datum;
1fd400ff 6423
b2bdb8cf 6424 all_type_units->push_back (sigt);
1fd400ff
TT
6425
6426 return 1;
6427}
6428
78d4d2c5 6429/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6430 and fill them into TYPES_HTAB. It will process only type units,
6431 therefore DW_UT_type. */
c88ee1f0 6432
78d4d2c5 6433static void
ed2dc618
SM
6434create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6435 struct dwo_file *dwo_file,
43988095
JK
6436 dwarf2_section_info *section, htab_t &types_htab,
6437 rcuh_kind section_kind)
348e048f 6438{
3019eac3 6439 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6440 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6441 bfd *abfd;
6442 const gdb_byte *info_ptr, *end_ptr;
348e048f 6443
4bdcc0c1
DE
6444 abbrev_section = (dwo_file != NULL
6445 ? &dwo_file->sections.abbrev
6446 : &dwarf2_per_objfile->abbrev);
6447
b4f54984 6448 if (dwarf_read_debug)
43988095 6449 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6450 section->get_name (),
6451 abbrev_section->get_file_name ());
09406207 6452
96b79293 6453 section->read (objfile);
78d4d2c5 6454 info_ptr = section->buffer;
348e048f 6455
78d4d2c5
JK
6456 if (info_ptr == NULL)
6457 return;
348e048f 6458
78d4d2c5
JK
6459 /* We can't set abfd until now because the section may be empty or
6460 not present, in which case the bfd is unknown. */
96b79293 6461 abfd = section->get_bfd_owner ();
348e048f 6462
c0ab21c2
TT
6463 /* We don't use cutu_reader here because we don't need to read
6464 any dies: the signature is in the header. */
3019eac3 6465
78d4d2c5
JK
6466 end_ptr = info_ptr + section->size;
6467 while (info_ptr < end_ptr)
6468 {
78d4d2c5
JK
6469 struct signatured_type *sig_type;
6470 struct dwo_unit *dwo_tu;
6471 void **slot;
6472 const gdb_byte *ptr = info_ptr;
6473 struct comp_unit_head header;
6474 unsigned int length;
8b70b953 6475
9c541725 6476 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6477
a49dd8dd
JK
6478 /* Initialize it due to a false compiler warning. */
6479 header.signature = -1;
9c541725 6480 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6481
78d4d2c5
JK
6482 /* We need to read the type's signature in order to build the hash
6483 table, but we don't need anything else just yet. */
348e048f 6484
ed2dc618 6485 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6486 abbrev_section, ptr, section_kind);
348e048f 6487
78d4d2c5 6488 length = get_cu_length (&header);
6caca83c 6489
78d4d2c5
JK
6490 /* Skip dummy type units. */
6491 if (ptr >= info_ptr + length
43988095
JK
6492 || peek_abbrev_code (abfd, ptr) == 0
6493 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6494 {
6495 info_ptr += length;
6496 continue;
6497 }
dee91e82 6498
78d4d2c5
JK
6499 if (types_htab == NULL)
6500 {
6501 if (dwo_file)
6502 types_htab = allocate_dwo_unit_table (objfile);
6503 else
6504 types_htab = allocate_signatured_type_table (objfile);
6505 }
8b70b953 6506
78d4d2c5
JK
6507 if (dwo_file)
6508 {
6509 sig_type = NULL;
6510 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6511 struct dwo_unit);
6512 dwo_tu->dwo_file = dwo_file;
43988095 6513 dwo_tu->signature = header.signature;
9c541725 6514 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6515 dwo_tu->section = section;
9c541725 6516 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6517 dwo_tu->length = length;
6518 }
6519 else
6520 {
6521 /* N.B.: type_offset is not usable if this type uses a DWO file.
6522 The real type_offset is in the DWO file. */
6523 dwo_tu = NULL;
6524 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6525 struct signatured_type);
43988095 6526 sig_type->signature = header.signature;
9c541725 6527 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6528 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6529 sig_type->per_cu.is_debug_types = 1;
6530 sig_type->per_cu.section = section;
9c541725 6531 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6532 sig_type->per_cu.length = length;
6533 }
6534
6535 slot = htab_find_slot (types_htab,
6536 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6537 INSERT);
6538 gdb_assert (slot != NULL);
6539 if (*slot != NULL)
6540 {
9c541725 6541 sect_offset dup_sect_off;
0349ea22 6542
3019eac3
DE
6543 if (dwo_file)
6544 {
78d4d2c5
JK
6545 const struct dwo_unit *dup_tu
6546 = (const struct dwo_unit *) *slot;
6547
9c541725 6548 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6549 }
6550 else
6551 {
78d4d2c5
JK
6552 const struct signatured_type *dup_tu
6553 = (const struct signatured_type *) *slot;
6554
9c541725 6555 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6556 }
8b70b953 6557
b98664d3 6558 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6559 " the entry at offset %s, signature %s"),
6560 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6561 hex_string (header.signature));
78d4d2c5
JK
6562 }
6563 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6564
78d4d2c5 6565 if (dwarf_read_debug > 1)
9d8780f0
SM
6566 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6567 sect_offset_str (sect_off),
43988095 6568 hex_string (header.signature));
3019eac3 6569
78d4d2c5
JK
6570 info_ptr += length;
6571 }
6572}
3019eac3 6573
78d4d2c5
JK
6574/* Create the hash table of all entries in the .debug_types
6575 (or .debug_types.dwo) section(s).
6576 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6577 otherwise it is NULL.
b3c8eb43 6578
78d4d2c5 6579 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6580
78d4d2c5 6581 Note: This function processes DWO files only, not DWP files. */
348e048f 6582
78d4d2c5 6583static void
ed2dc618
SM
6584create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6585 struct dwo_file *dwo_file,
fd5866f6 6586 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6587 htab_t &types_htab)
6588{
fd5866f6
SM
6589 for (dwarf2_section_info &section : type_sections)
6590 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6591 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6592}
6593
6594/* Create the hash table of all entries in the .debug_types section,
6595 and initialize all_type_units.
6596 The result is zero if there is an error (e.g. missing .debug_types section),
6597 otherwise non-zero. */
6598
6599static int
ed2dc618 6600create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6601{
78d4d2c5 6602 htab_t types_htab = NULL;
3019eac3 6603
ed2dc618
SM
6604 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6605 &dwarf2_per_objfile->info, types_htab,
43988095 6606 rcuh_kind::COMPILE);
ed2dc618
SM
6607 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6608 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6609 if (types_htab == NULL)
6610 {
6611 dwarf2_per_objfile->signatured_types = NULL;
6612 return 0;
6613 }
6614
348e048f
DE
6615 dwarf2_per_objfile->signatured_types = types_htab;
6616
b2bdb8cf
SM
6617 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6618 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6619
6620 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6621 &dwarf2_per_objfile->all_type_units);
1fd400ff 6622
348e048f
DE
6623 return 1;
6624}
6625
6aa5f3a6
DE
6626/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6627 If SLOT is non-NULL, it is the entry to use in the hash table.
6628 Otherwise we find one. */
6629
6630static struct signatured_type *
ed2dc618
SM
6631add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6632 void **slot)
6aa5f3a6
DE
6633{
6634 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6635
b2bdb8cf
SM
6636 if (dwarf2_per_objfile->all_type_units.size ()
6637 == dwarf2_per_objfile->all_type_units.capacity ())
6638 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6639
b2bdb8cf
SM
6640 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6641 struct signatured_type);
6642
6643 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6644 sig_type->signature = sig;
6645 sig_type->per_cu.is_debug_types = 1;
6646 if (dwarf2_per_objfile->using_index)
6647 {
6648 sig_type->per_cu.v.quick =
6649 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6650 struct dwarf2_per_cu_quick_data);
6651 }
6652
6653 if (slot == NULL)
6654 {
6655 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6656 sig_type, INSERT);
6657 }
6658 gdb_assert (*slot == NULL);
6659 *slot = sig_type;
6660 /* The rest of sig_type must be filled in by the caller. */
6661 return sig_type;
6662}
6663
a2ce51a0
DE
6664/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6665 Fill in SIG_ENTRY with DWO_ENTRY. */
6666
6667static void
ed2dc618 6668fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6669 struct signatured_type *sig_entry,
6670 struct dwo_unit *dwo_entry)
6671{
7ee85ab1 6672 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6673 gdb_assert (! sig_entry->per_cu.queued);
6674 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6675 if (dwarf2_per_objfile->using_index)
6676 {
6677 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6678 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6679 }
6680 else
6681 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6682 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6683 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6684 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6685 gdb_assert (sig_entry->dwo_unit == NULL);
6686
6687 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6688 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6689 sig_entry->per_cu.length = dwo_entry->length;
6690 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6691 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6692 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6693 sig_entry->dwo_unit = dwo_entry;
6694}
6695
6696/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6697 If we haven't read the TU yet, create the signatured_type data structure
6698 for a TU to be read in directly from a DWO file, bypassing the stub.
6699 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6700 using .gdb_index, then when reading a CU we want to stay in the DWO file
6701 containing that CU. Otherwise we could end up reading several other DWO
6702 files (due to comdat folding) to process the transitive closure of all the
6703 mentioned TUs, and that can be slow. The current DWO file will have every
6704 type signature that it needs.
a2ce51a0
DE
6705 We only do this for .gdb_index because in the psymtab case we already have
6706 to read all the DWOs to build the type unit groups. */
6707
6708static struct signatured_type *
6709lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6710{
518817b3
SM
6711 struct dwarf2_per_objfile *dwarf2_per_objfile
6712 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6713 struct objfile *objfile = dwarf2_per_objfile->objfile;
6714 struct dwo_file *dwo_file;
6715 struct dwo_unit find_dwo_entry, *dwo_entry;
6716 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6717 void **slot;
a2ce51a0
DE
6718
6719 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6720
6aa5f3a6
DE
6721 /* If TU skeletons have been removed then we may not have read in any
6722 TUs yet. */
6723 if (dwarf2_per_objfile->signatured_types == NULL)
6724 {
6725 dwarf2_per_objfile->signatured_types
6726 = allocate_signatured_type_table (objfile);
6727 }
a2ce51a0
DE
6728
6729 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6730 Use the global signatured_types array to do our own comdat-folding
6731 of types. If this is the first time we're reading this TU, and
6732 the TU has an entry in .gdb_index, replace the recorded data from
6733 .gdb_index with this TU. */
a2ce51a0 6734
a2ce51a0 6735 find_sig_entry.signature = sig;
6aa5f3a6
DE
6736 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6737 &find_sig_entry, INSERT);
9a3c8263 6738 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6739
6740 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6741 read. Don't reassign the global entry to point to this DWO if that's
6742 the case. Also note that if the TU is already being read, it may not
6743 have come from a DWO, the program may be a mix of Fission-compiled
6744 code and non-Fission-compiled code. */
6745
6746 /* Have we already tried to read this TU?
6747 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6748 needn't exist in the global table yet). */
6749 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6750 return sig_entry;
6751
6aa5f3a6
DE
6752 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6753 dwo_unit of the TU itself. */
6754 dwo_file = cu->dwo_unit->dwo_file;
6755
a2ce51a0
DE
6756 /* Ok, this is the first time we're reading this TU. */
6757 if (dwo_file->tus == NULL)
6758 return NULL;
6759 find_dwo_entry.signature = sig;
9a3c8263 6760 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6761 if (dwo_entry == NULL)
6762 return NULL;
6763
6aa5f3a6
DE
6764 /* If the global table doesn't have an entry for this TU, add one. */
6765 if (sig_entry == NULL)
ed2dc618 6766 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6767
ed2dc618 6768 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6769 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6770 return sig_entry;
6771}
6772
a2ce51a0
DE
6773/* Subroutine of lookup_signatured_type.
6774 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6775 then try the DWP file. If the TU stub (skeleton) has been removed then
6776 it won't be in .gdb_index. */
a2ce51a0
DE
6777
6778static struct signatured_type *
6779lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6780{
518817b3
SM
6781 struct dwarf2_per_objfile *dwarf2_per_objfile
6782 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6783 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6784 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6785 struct dwo_unit *dwo_entry;
6786 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6787 void **slot;
a2ce51a0
DE
6788
6789 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6790 gdb_assert (dwp_file != NULL);
6791
6aa5f3a6
DE
6792 /* If TU skeletons have been removed then we may not have read in any
6793 TUs yet. */
6794 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6795 {
6aa5f3a6
DE
6796 dwarf2_per_objfile->signatured_types
6797 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6798 }
6799
6aa5f3a6
DE
6800 find_sig_entry.signature = sig;
6801 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6802 &find_sig_entry, INSERT);
9a3c8263 6803 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6804
6805 /* Have we already tried to read this TU?
6806 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6807 needn't exist in the global table yet). */
6808 if (sig_entry != NULL)
6809 return sig_entry;
6810
a2ce51a0
DE
6811 if (dwp_file->tus == NULL)
6812 return NULL;
ed2dc618 6813 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6814 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6815 if (dwo_entry == NULL)
6816 return NULL;
6817
ed2dc618
SM
6818 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6819 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6820
a2ce51a0
DE
6821 return sig_entry;
6822}
6823
380bca97 6824/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6825 Returns NULL if signature SIG is not present in the table.
6826 It is up to the caller to complain about this. */
348e048f
DE
6827
6828static struct signatured_type *
a2ce51a0 6829lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6830{
518817b3
SM
6831 struct dwarf2_per_objfile *dwarf2_per_objfile
6832 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6833
a2ce51a0
DE
6834 if (cu->dwo_unit
6835 && dwarf2_per_objfile->using_index)
6836 {
6837 /* We're in a DWO/DWP file, and we're using .gdb_index.
6838 These cases require special processing. */
ed2dc618 6839 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6840 return lookup_dwo_signatured_type (cu, sig);
6841 else
6842 return lookup_dwp_signatured_type (cu, sig);
6843 }
6844 else
6845 {
6846 struct signatured_type find_entry, *entry;
348e048f 6847
a2ce51a0
DE
6848 if (dwarf2_per_objfile->signatured_types == NULL)
6849 return NULL;
6850 find_entry.signature = sig;
9a3c8263
SM
6851 entry = ((struct signatured_type *)
6852 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
6853 return entry;
6854 }
348e048f 6855}
18a8505e
AT
6856
6857/* Return the address base of the compile unit, which, if exists, is stored
6858 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6859static gdb::optional<ULONGEST>
6860lookup_addr_base (struct die_info *comp_unit_die)
6861{
6862 struct attribute *attr;
6863 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6864 if (attr == nullptr)
6865 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6866 if (attr == nullptr)
6867 return gdb::optional<ULONGEST> ();
6868 return DW_UNSND (attr);
6869}
6870
6871/* Return range lists base of the compile unit, which, if exists, is stored
6872 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6873static ULONGEST
6874lookup_ranges_base (struct die_info *comp_unit_die)
6875{
6876 struct attribute *attr;
6877 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6878 if (attr == nullptr)
6879 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6880 if (attr == nullptr)
6881 return 0;
6882 return DW_UNSND (attr);
6883}
6884
42e7ad6c 6885/* Low level DIE reading support. */
348e048f 6886
d85a05f0
DJ
6887/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6888
6889static void
6890init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6891 struct dwarf2_cu *cu,
3019eac3 6892 struct dwarf2_section_info *section,
685af9cd
TT
6893 struct dwo_file *dwo_file,
6894 struct abbrev_table *abbrev_table)
d85a05f0 6895{
fceca515 6896 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6897 reader->abfd = section->get_bfd_owner ();
d85a05f0 6898 reader->cu = cu;
3019eac3 6899 reader->dwo_file = dwo_file;
dee91e82
DE
6900 reader->die_section = section;
6901 reader->buffer = section->buffer;
f664829e 6902 reader->buffer_end = section->buffer + section->size;
685af9cd 6903 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6904}
6905
c0ab21c2 6906/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6907 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6908 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6909 already.
6910
6911 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6912 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6913 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6914 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6915 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6916 STUB_COMP_DIR may be non-NULL.
3e225074 6917 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6918 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6919 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6920 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6921 kept around for at least as long as *RESULT_READER.
6922
b0c7bfa9
DE
6923 The result is non-zero if a valid (non-dummy) DIE was found. */
6924
6925static int
6926read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6927 struct dwo_unit *dwo_unit,
b0c7bfa9 6928 struct die_info *stub_comp_unit_die,
a2ce51a0 6929 const char *stub_comp_dir,
b0c7bfa9 6930 struct die_reader_specs *result_reader,
d521ce57 6931 const gdb_byte **result_info_ptr,
b0c7bfa9 6932 struct die_info **result_comp_unit_die,
685af9cd 6933 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6934{
ed2dc618 6935 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6936 struct objfile *objfile = dwarf2_per_objfile->objfile;
6937 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6938 bfd *abfd;
d521ce57 6939 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6940 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6941 int i,num_extra_attrs;
6942 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6943 struct die_info *comp_unit_die;
6944
b0aeadb3
DE
6945 /* At most one of these may be provided. */
6946 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6947
b0c7bfa9
DE
6948 /* These attributes aren't processed until later:
6949 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6950 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6951 referenced later. However, these attributes are found in the stub
6952 which we won't have later. In order to not impose this complication
6953 on the rest of the code, we read them here and copy them to the
6954 DWO CU/TU die. */
b0c7bfa9
DE
6955
6956 stmt_list = NULL;
6957 low_pc = NULL;
6958 high_pc = NULL;
6959 ranges = NULL;
6960 comp_dir = NULL;
6961
6962 if (stub_comp_unit_die != NULL)
6963 {
6964 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6965 DWO file. */
6966 if (! this_cu->is_debug_types)
6967 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6968 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6969 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6970 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6971 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6972
18a8505e 6973 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6974
18a8505e
AT
6975 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6976 here (if needed). We need the value before we can process
6977 DW_AT_ranges. */
6978 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6979 }
a2ce51a0
DE
6980 else if (stub_comp_dir != NULL)
6981 {
6982 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6983 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6984 comp_dir->name = DW_AT_comp_dir;
6985 comp_dir->form = DW_FORM_string;
6986 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6987 DW_STRING (comp_dir) = stub_comp_dir;
6988 }
b0c7bfa9
DE
6989
6990 /* Set up for reading the DWO CU/TU. */
6991 cu->dwo_unit = dwo_unit;
685af9cd 6992 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6993 section->read (objfile);
6994 abfd = section->get_bfd_owner ();
9c541725
PA
6995 begin_info_ptr = info_ptr = (section->buffer
6996 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6997 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6998
6999 if (this_cu->is_debug_types)
7000 {
b0c7bfa9
DE
7001 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7002
ed2dc618
SM
7003 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7004 &cu->header, section,
b0c7bfa9 7005 dwo_abbrev_section,
43988095 7006 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7007 /* This is not an assert because it can be caused by bad debug info. */
43988095 7008 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7009 {
7010 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7011 " TU at offset %s [in module %s]"),
a2ce51a0 7012 hex_string (sig_type->signature),
43988095 7013 hex_string (cu->header.signature),
9d8780f0 7014 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7015 bfd_get_filename (abfd));
7016 }
9c541725 7017 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7018 /* For DWOs coming from DWP files, we don't know the CU length
7019 nor the type's offset in the TU until now. */
7020 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7021 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7022
7023 /* Establish the type offset that can be used to lookup the type.
7024 For DWO files, we don't know it until now. */
9c541725
PA
7025 sig_type->type_offset_in_section
7026 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7027 }
7028 else
7029 {
ed2dc618
SM
7030 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7031 &cu->header, section,
b0c7bfa9 7032 dwo_abbrev_section,
43988095 7033 info_ptr, rcuh_kind::COMPILE);
9c541725 7034 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7035 /* For DWOs coming from DWP files, we don't know the CU length
7036 until now. */
7037 dwo_unit->length = get_cu_length (&cu->header);
7038 }
7039
685af9cd 7040 *result_dwo_abbrev_table
3054dd54 7041 = abbrev_table_read_table (objfile, dwo_abbrev_section,
685af9cd
TT
7042 cu->header.abbrev_sect_off);
7043 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7044 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7045
7046 /* Read in the die, but leave space to copy over the attributes
7047 from the stub. This has the benefit of simplifying the rest of
7048 the code - all the work to maintain the illusion of a single
7049 DW_TAG_{compile,type}_unit DIE is done here. */
7050 num_extra_attrs = ((stmt_list != NULL)
7051 + (low_pc != NULL)
7052 + (high_pc != NULL)
7053 + (ranges != NULL)
7054 + (comp_dir != NULL));
7055 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 7056 num_extra_attrs);
b0c7bfa9
DE
7057
7058 /* Copy over the attributes from the stub to the DIE we just read in. */
7059 comp_unit_die = *result_comp_unit_die;
7060 i = comp_unit_die->num_attrs;
7061 if (stmt_list != NULL)
7062 comp_unit_die->attrs[i++] = *stmt_list;
7063 if (low_pc != NULL)
7064 comp_unit_die->attrs[i++] = *low_pc;
7065 if (high_pc != NULL)
7066 comp_unit_die->attrs[i++] = *high_pc;
7067 if (ranges != NULL)
7068 comp_unit_die->attrs[i++] = *ranges;
7069 if (comp_dir != NULL)
7070 comp_unit_die->attrs[i++] = *comp_dir;
7071 comp_unit_die->num_attrs += num_extra_attrs;
7072
b4f54984 7073 if (dwarf_die_debug)
bf6af496
DE
7074 {
7075 fprintf_unfiltered (gdb_stdlog,
7076 "Read die from %s@0x%x of %s:\n",
96b79293 7077 section->get_name (),
bf6af496
DE
7078 (unsigned) (begin_info_ptr - section->buffer),
7079 bfd_get_filename (abfd));
b4f54984 7080 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7081 }
7082
b0c7bfa9
DE
7083 /* Skip dummy compilation units. */
7084 if (info_ptr >= begin_info_ptr + dwo_unit->length
7085 || peek_abbrev_code (abfd, info_ptr) == 0)
7086 return 0;
7087
7088 *result_info_ptr = info_ptr;
7089 return 1;
7090}
7091
a084a2a6
AT
7092/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7093 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7094 signature is part of the header. */
7095static gdb::optional<ULONGEST>
7096lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7097{
7098 if (cu->header.version >= 5)
7099 return cu->header.signature;
7100 struct attribute *attr;
7101 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7102 if (attr == nullptr)
7103 return gdb::optional<ULONGEST> ();
7104 return DW_UNSND (attr);
7105}
7106
c0ab21c2 7107/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7108 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7109 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7110
7111static struct dwo_unit *
7112lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
7113 struct die_info *comp_unit_die,
7114 const char *dwo_name)
b0c7bfa9
DE
7115{
7116 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7117 struct dwo_unit *dwo_unit;
c0ab21c2 7118 const char *comp_dir;
b0c7bfa9 7119
a2ce51a0
DE
7120 gdb_assert (cu != NULL);
7121
b0c7bfa9 7122 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7123 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7124 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7125
7126 if (this_cu->is_debug_types)
7127 {
7128 struct signatured_type *sig_type;
7129
7130 /* Since this_cu is the first member of struct signatured_type,
7131 we can go from a pointer to one to a pointer to the other. */
7132 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7133 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7134 }
7135 else
7136 {
a084a2a6
AT
7137 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7138 if (!signature.has_value ())
b0c7bfa9
DE
7139 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7140 " [in module %s]"),
e3b94546 7141 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7142 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7143 *signature);
b0c7bfa9
DE
7144 }
7145
b0c7bfa9
DE
7146 return dwo_unit;
7147}
7148
c0ab21c2 7149/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7150 See it for a description of the parameters.
fcd3b13d 7151 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7152
c0ab21c2
TT
7153void
7154cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7155 int use_existing_cu, int keep)
a2ce51a0 7156{
a2ce51a0 7157 struct signatured_type *sig_type;
a2ce51a0 7158 struct die_reader_specs reader;
a2ce51a0
DE
7159
7160 /* Verify we can do the following downcast, and that we have the
7161 data we need. */
7162 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7163 sig_type = (struct signatured_type *) this_cu;
7164 gdb_assert (sig_type->dwo_unit != NULL);
7165
6aa5f3a6
DE
7166 if (use_existing_cu && this_cu->cu != NULL)
7167 {
7168 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7169 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7170 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7171 }
7172 else
7173 {
7174 /* If !use_existing_cu, this_cu->cu must be NULL. */
7175 gdb_assert (this_cu->cu == NULL);
c0ab21c2 7176 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7177 }
7178
7179 /* A future optimization, if needed, would be to use an existing
7180 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7181 could share abbrev tables. */
a2ce51a0
DE
7182
7183 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7184 NULL /* stub_comp_unit_die */,
7185 sig_type->dwo_unit->dwo_file->comp_dir,
7186 &reader, &info_ptr,
3e225074 7187 &comp_unit_die,
c0ab21c2 7188 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7189 {
7190 /* Dummy die. */
c0ab21c2 7191 dummy_p = true;
a2ce51a0 7192 }
a2ce51a0
DE
7193}
7194
fd820528 7195/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7196 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7197
f4dc4d17
DE
7198 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7199 Otherwise the table specified in the comp unit header is read in and used.
7200 This is an optimization for when we already have the abbrev table.
7201
dee91e82
DE
7202 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7203 Otherwise, a new CU is allocated with xmalloc.
7204
7205 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
c0ab21c2
TT
7206 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7207 end. */
aaa75496 7208
c0ab21c2
TT
7209cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7210 struct abbrev_table *abbrev_table,
7211 int use_existing_cu, int keep,
7212 bool skip_partial)
7213 : die_reader_specs {},
7214 m_this_cu (this_cu),
7215 m_keep (keep)
c906108c 7216{
ed2dc618 7217 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7218 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7219 struct dwarf2_section_info *section = this_cu->section;
96b79293 7220 bfd *abfd = section->get_bfd_owner ();
dee91e82 7221 struct dwarf2_cu *cu;
c0ab21c2 7222 const gdb_byte *begin_info_ptr;
dee91e82 7223 struct signatured_type *sig_type = NULL;
4bdcc0c1 7224 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7225 /* Non-zero if CU currently points to a DWO file and we need to
7226 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7227 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7228 int rereading_dwo_cu = 0;
c906108c 7229
b4f54984 7230 if (dwarf_die_debug)
9d8780f0 7231 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7232 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7233 sect_offset_str (this_cu->sect_off));
09406207 7234
dee91e82
DE
7235 if (use_existing_cu)
7236 gdb_assert (keep);
23745b47 7237
a2ce51a0
DE
7238 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7239 file (instead of going through the stub), short-circuit all of this. */
7240 if (this_cu->reading_dwo_directly)
7241 {
7242 /* Narrow down the scope of possibilities to have to understand. */
7243 gdb_assert (this_cu->is_debug_types);
7244 gdb_assert (abbrev_table == NULL);
c0ab21c2 7245 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
a2ce51a0
DE
7246 return;
7247 }
7248
dee91e82 7249 /* This is cheap if the section is already read in. */
96b79293 7250 section->read (objfile);
dee91e82 7251
9c541725 7252 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7253
7254 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7255
7256 if (use_existing_cu && this_cu->cu != NULL)
7257 {
7258 cu = this_cu->cu;
42e7ad6c
DE
7259 /* If this CU is from a DWO file we need to start over, we need to
7260 refetch the attributes from the skeleton CU.
7261 This could be optimized by retrieving those attributes from when we
7262 were here the first time: the previous comp_unit_die was stored in
7263 comp_unit_obstack. But there's no data yet that we need this
7264 optimization. */
7265 if (cu->dwo_unit != NULL)
7266 rereading_dwo_cu = 1;
dee91e82
DE
7267 }
7268 else
7269 {
7270 /* If !use_existing_cu, this_cu->cu must be NULL. */
7271 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
7272 m_new_cu.reset (new dwarf2_cu (this_cu));
7273 cu = m_new_cu.get ();
42e7ad6c 7274 }
dee91e82 7275
b0c7bfa9 7276 /* Get the header. */
9c541725 7277 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7278 {
7279 /* We already have the header, there's no need to read it in again. */
9c541725 7280 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7281 }
7282 else
7283 {
3019eac3 7284 if (this_cu->is_debug_types)
dee91e82 7285 {
ed2dc618
SM
7286 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7287 &cu->header, section,
4bdcc0c1 7288 abbrev_section, info_ptr,
43988095 7289 rcuh_kind::TYPE);
dee91e82 7290
42e7ad6c
DE
7291 /* Since per_cu is the first member of struct signatured_type,
7292 we can go from a pointer to one to a pointer to the other. */
7293 sig_type = (struct signatured_type *) this_cu;
43988095 7294 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7295 gdb_assert (sig_type->type_offset_in_tu
7296 == cu->header.type_cu_offset_in_tu);
7297 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7298
42e7ad6c
DE
7299 /* LENGTH has not been set yet for type units if we're
7300 using .gdb_index. */
1ce1cefd 7301 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7302
7303 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7304 sig_type->type_offset_in_section =
7305 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7306
7307 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7308 }
7309 else
7310 {
ed2dc618
SM
7311 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7312 &cu->header, section,
4bdcc0c1 7313 abbrev_section,
43988095
JK
7314 info_ptr,
7315 rcuh_kind::COMPILE);
dee91e82 7316
9c541725 7317 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7318 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7319 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7320 }
7321 }
10b3939b 7322
6caca83c 7323 /* Skip dummy compilation units. */
dee91e82 7324 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7325 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7326 {
7327 dummy_p = true;
7328 return;
7329 }
6caca83c 7330
433df2d4
DE
7331 /* If we don't have them yet, read the abbrevs for this compilation unit.
7332 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7333 done. */
f4dc4d17 7334 if (abbrev_table != NULL)
685af9cd
TT
7335 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7336 else
f4dc4d17 7337 {
c0ab21c2 7338 m_abbrev_table_holder
3054dd54 7339 = abbrev_table_read_table (objfile, abbrev_section,
685af9cd 7340 cu->header.abbrev_sect_off);
c0ab21c2 7341 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7342 }
af703f96 7343
dee91e82 7344 /* Read the top level CU/TU die. */
c0ab21c2 7345 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7346 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7347
58f0c718 7348 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7349 {
7350 dummy_p = true;
7351 return;
7352 }
58f0c718 7353
b0c7bfa9 7354 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7355 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7356 table from the DWO file and pass the ownership over to us. It will be
7357 referenced from READER, so we must make sure to free it after we're done
7358 with READER.
7359
b0c7bfa9
DE
7360 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7361 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7362 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7363 if (dwo_name != nullptr)
3019eac3 7364 {
3019eac3 7365 struct dwo_unit *dwo_unit;
b0c7bfa9 7366 struct die_info *dwo_comp_unit_die;
3019eac3 7367
3e225074 7368 if (comp_unit_die->has_children)
6a506a2d 7369 {
b98664d3 7370 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7371 " has children (offset %s) [in module %s]"),
7372 sect_offset_str (this_cu->sect_off),
7373 bfd_get_filename (abfd));
6a506a2d 7374 }
c0ab21c2 7375 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7376 if (dwo_unit != NULL)
3019eac3 7377 {
6a506a2d 7378 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7379 comp_unit_die, NULL,
c0ab21c2 7380 this, &info_ptr,
3e225074 7381 &dwo_comp_unit_die,
c0ab21c2 7382 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7383 {
7384 /* Dummy die. */
c0ab21c2 7385 dummy_p = true;
6a506a2d
DE
7386 return;
7387 }
7388 comp_unit_die = dwo_comp_unit_die;
7389 }
7390 else
7391 {
7392 /* Yikes, we couldn't find the rest of the DIE, we only have
7393 the stub. A complaint has already been logged. There's
7394 not much more we can do except pass on the stub DIE to
7395 die_reader_func. We don't want to throw an error on bad
7396 debug info. */
3019eac3
DE
7397 }
7398 }
c0ab21c2 7399}
3019eac3 7400
c0ab21c2
TT
7401cutu_reader::~cutu_reader ()
7402{
b0c7bfa9 7403 /* Done, clean up. */
c0ab21c2 7404 if (m_new_cu != NULL && m_keep && !dummy_p)
348e048f 7405 {
c0ab21c2
TT
7406 struct dwarf2_per_objfile *dwarf2_per_objfile
7407 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7408 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7409 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7410 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7411 /* The chain owns it now. */
c0ab21c2 7412 m_new_cu.release ();
348e048f 7413 }
dee91e82
DE
7414}
7415
18a8505e
AT
7416/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7417 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7418 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7419
7420 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7421 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7422
7423 We fill in THIS_CU->length.
7424
dee91e82 7425 THIS_CU->cu is always freed when done.
3019eac3 7426 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7427 to care whether it refers to the "main" CU or the DWO CU.
7428
7429 When parent_cu is passed, it is used to provide a default value for
7430 str_offsets_base and addr_base from the parent. */
dee91e82 7431
c0ab21c2
TT
7432cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7433 struct dwarf2_cu *parent_cu,
7434 struct dwo_file *dwo_file)
7435 : die_reader_specs {},
7436 m_this_cu (this_cu)
dee91e82 7437{
ed2dc618 7438 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7439 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7440 struct dwarf2_section_info *section = this_cu->section;
96b79293 7441 bfd *abfd = section->get_bfd_owner ();
33e80786 7442 struct dwarf2_section_info *abbrev_section;
d521ce57 7443 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7444
b4f54984 7445 if (dwarf_die_debug)
9d8780f0 7446 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7447 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7448 sect_offset_str (this_cu->sect_off));
09406207 7449
dee91e82
DE
7450 gdb_assert (this_cu->cu == NULL);
7451
33e80786
DE
7452 abbrev_section = (dwo_file != NULL
7453 ? &dwo_file->sections.abbrev
7454 : get_abbrev_section_for_cu (this_cu));
7455
dee91e82 7456 /* This is cheap if the section is already read in. */
96b79293 7457 section->read (objfile);
dee91e82 7458
c0ab21c2 7459 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7460
9c541725 7461 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7462 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7463 &m_new_cu->header, section,
4bdcc0c1 7464 abbrev_section, info_ptr,
43988095
JK
7465 (this_cu->is_debug_types
7466 ? rcuh_kind::TYPE
7467 : rcuh_kind::COMPILE));
dee91e82 7468
18a8505e
AT
7469 if (parent_cu != nullptr)
7470 {
c0ab21c2
TT
7471 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7472 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7473 }
c0ab21c2 7474 this_cu->length = get_cu_length (&m_new_cu->header);
dee91e82
DE
7475
7476 /* Skip dummy compilation units. */
7477 if (info_ptr >= begin_info_ptr + this_cu->length
7478 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7479 {
7480 dummy_p = true;
7481 return;
7482 }
72bf9492 7483
c0ab21c2 7484 m_abbrev_table_holder
3054dd54 7485 = abbrev_table_read_table (objfile, abbrev_section,
c0ab21c2 7486 m_new_cu->header.abbrev_sect_off);
dee91e82 7487
c0ab21c2
TT
7488 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7489 m_abbrev_table_holder.get ());
3e225074 7490 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7491}
7492
0018ea6f
DE
7493\f
7494/* Type Unit Groups.
dee91e82 7495
0018ea6f
DE
7496 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7497 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7498 so that all types coming from the same compilation (.o file) are grouped
7499 together. A future step could be to put the types in the same symtab as
7500 the CU the types ultimately came from. */
ff013f42 7501
f4dc4d17
DE
7502static hashval_t
7503hash_type_unit_group (const void *item)
7504{
9a3c8263
SM
7505 const struct type_unit_group *tu_group
7506 = (const struct type_unit_group *) item;
f4dc4d17 7507
094b34ac 7508 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7509}
348e048f
DE
7510
7511static int
f4dc4d17 7512eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7513{
9a3c8263
SM
7514 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7515 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7516
094b34ac 7517 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7518}
348e048f 7519
f4dc4d17
DE
7520/* Allocate a hash table for type unit groups. */
7521
7522static htab_t
ed2dc618 7523allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7524{
7525 return htab_create_alloc_ex (3,
7526 hash_type_unit_group,
7527 eq_type_unit_group,
7528 NULL,
ed2dc618 7529 &objfile->objfile_obstack,
f4dc4d17
DE
7530 hashtab_obstack_allocate,
7531 dummy_obstack_deallocate);
7532}
dee91e82 7533
f4dc4d17
DE
7534/* Type units that don't have DW_AT_stmt_list are grouped into their own
7535 partial symtabs. We combine several TUs per psymtab to not let the size
7536 of any one psymtab grow too big. */
7537#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7538#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7539
094b34ac 7540/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7541 Create the type_unit_group object used to hold one or more TUs. */
7542
7543static struct type_unit_group *
094b34ac 7544create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7545{
518817b3
SM
7546 struct dwarf2_per_objfile *dwarf2_per_objfile
7547 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7548 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7549 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7550 struct type_unit_group *tu_group;
f4dc4d17
DE
7551
7552 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7553 struct type_unit_group);
094b34ac 7554 per_cu = &tu_group->per_cu;
518817b3 7555 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7556
094b34ac
DE
7557 if (dwarf2_per_objfile->using_index)
7558 {
7559 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7560 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7561 }
7562 else
7563 {
9c541725 7564 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7565 dwarf2_psymtab *pst;
528e1572 7566 std::string name;
094b34ac
DE
7567
7568 /* Give the symtab a useful name for debug purposes. */
7569 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7570 name = string_printf ("<type_units_%d>",
7571 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7572 else
528e1572 7573 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7574
528e1572 7575 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7576 pst->anonymous = true;
094b34ac 7577 }
f4dc4d17 7578
094b34ac 7579 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7580 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7581
7582 return tu_group;
7583}
7584
094b34ac
DE
7585/* Look up the type_unit_group for type unit CU, and create it if necessary.
7586 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7587
7588static struct type_unit_group *
ff39bb5e 7589get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7590{
518817b3
SM
7591 struct dwarf2_per_objfile *dwarf2_per_objfile
7592 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7593 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7594 struct type_unit_group *tu_group;
7595 void **slot;
7596 unsigned int line_offset;
7597 struct type_unit_group type_unit_group_for_lookup;
7598
7599 if (dwarf2_per_objfile->type_unit_groups == NULL)
7600 {
7601 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7602 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7603 }
7604
7605 /* Do we need to create a new group, or can we use an existing one? */
7606
7607 if (stmt_list)
7608 {
7609 line_offset = DW_UNSND (stmt_list);
7610 ++tu_stats->nr_symtab_sharers;
7611 }
7612 else
7613 {
7614 /* Ugh, no stmt_list. Rare, but we have to handle it.
7615 We can do various things here like create one group per TU or
7616 spread them over multiple groups to split up the expansion work.
7617 To avoid worst case scenarios (too many groups or too large groups)
7618 we, umm, group them in bunches. */
7619 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7620 | (tu_stats->nr_stmt_less_type_units
7621 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7622 ++tu_stats->nr_stmt_less_type_units;
7623 }
7624
094b34ac 7625 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7626 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7627 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7628 &type_unit_group_for_lookup, INSERT);
7629 if (*slot != NULL)
7630 {
9a3c8263 7631 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7632 gdb_assert (tu_group != NULL);
7633 }
7634 else
7635 {
9c541725 7636 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7637 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7638 *slot = tu_group;
7639 ++tu_stats->nr_symtabs;
7640 }
7641
7642 return tu_group;
7643}
0018ea6f
DE
7644\f
7645/* Partial symbol tables. */
7646
7647/* Create a psymtab named NAME and assign it to PER_CU.
7648
7649 The caller must fill in the following details:
7650 dirname, textlow, texthigh. */
7651
891813be 7652static dwarf2_psymtab *
0018ea6f
DE
7653create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7654{
e3b94546 7655 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7656 dwarf2_psymtab *pst;
0018ea6f 7657
891813be 7658 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7659
6d94535f 7660 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7661
7662 /* This is the glue that links PST into GDB's symbol API. */
891813be 7663 pst->per_cu_data = per_cu;
0018ea6f
DE
7664 per_cu->v.psymtab = pst;
7665
7666 return pst;
7667}
7668
c0ab21c2 7669/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7670
7671static void
7672process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7673 const gdb_byte *info_ptr,
0018ea6f 7674 struct die_info *comp_unit_die,
c0ab21c2
TT
7675 int want_partial_unit,
7676 enum language pretend_language)
0018ea6f
DE
7677{
7678 struct dwarf2_cu *cu = reader->cu;
518817b3 7679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7680 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7681 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7682 CORE_ADDR baseaddr;
7683 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7684 dwarf2_psymtab *pst;
3a2b436a 7685 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7686 const char *filename;
0018ea6f 7687
c0ab21c2 7688 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
0018ea6f
DE
7689 return;
7690
7691 gdb_assert (! per_cu->is_debug_types);
7692
c0ab21c2 7693 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7694
0018ea6f 7695 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7696 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7697 if (filename == NULL)
0018ea6f 7698 filename = "";
0018ea6f
DE
7699
7700 pst = create_partial_symtab (per_cu, filename);
7701
7702 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7703 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7704
b3b3bada 7705 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7706
7707 dwarf2_find_base_address (comp_unit_die, cu);
7708
7709 /* Possibly set the default values of LOWPC and HIGHPC from
7710 `DW_AT_ranges'. */
3a2b436a
JK
7711 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7712 &best_highpc, cu, pst);
7713 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7714 {
7715 CORE_ADDR low
7716 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7717 - baseaddr);
7718 CORE_ADDR high
7719 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7720 - baseaddr - 1);
7721 /* Store the contiguous range if it is not empty; it can be
7722 empty for CUs with no code. */
d320c2b5
TT
7723 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7724 low, high, pst);
79748972 7725 }
0018ea6f
DE
7726
7727 /* Check if comp unit has_children.
7728 If so, read the rest of the partial symbols from this comp unit.
7729 If not, there's no more debug_info for this comp unit. */
3e225074 7730 if (comp_unit_die->has_children)
0018ea6f
DE
7731 {
7732 struct partial_die_info *first_die;
7733 CORE_ADDR lowpc, highpc;
7734
7735 lowpc = ((CORE_ADDR) -1);
7736 highpc = ((CORE_ADDR) 0);
7737
7738 first_die = load_partial_dies (reader, info_ptr, 1);
7739
7740 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7741 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7742
7743 /* If we didn't find a lowpc, set it to highpc to avoid
7744 complaints from `maint check'. */
7745 if (lowpc == ((CORE_ADDR) -1))
7746 lowpc = highpc;
7747
7748 /* If the compilation unit didn't have an explicit address range,
7749 then use the information extracted from its child dies. */
e385593e 7750 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7751 {
7752 best_lowpc = lowpc;
7753 best_highpc = highpc;
7754 }
7755 }
4ae976d1 7756 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7757 best_lowpc + baseaddr)
7758 - baseaddr);
4ae976d1 7759 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7760 best_highpc + baseaddr)
7761 - baseaddr);
0018ea6f 7762
8763cede 7763 end_psymtab_common (objfile, pst);
0018ea6f 7764
ae640021 7765 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7766 {
7767 int i;
ae640021 7768 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7769
7770 /* Fill in 'dependencies' here; we fill in 'users' in a
7771 post-pass. */
7772 pst->number_of_dependencies = len;
a9342b62
TT
7773 pst->dependencies
7774 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7775 for (i = 0; i < len; ++i)
7776 {
7777 pst->dependencies[i]
7778 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7779 }
0018ea6f 7780
ae640021 7781 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7782 }
7783
7784 /* Get the list of files included in the current compilation unit,
7785 and build a psymtab for each of them. */
7786 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7787
b4f54984 7788 if (dwarf_read_debug)
b926417a
TT
7789 fprintf_unfiltered (gdb_stdlog,
7790 "Psymtab for %s unit @%s: %s - %s"
7791 ", %d global, %d static syms\n",
7792 per_cu->is_debug_types ? "type" : "comp",
7793 sect_offset_str (per_cu->sect_off),
7794 paddress (gdbarch, pst->text_low (objfile)),
7795 paddress (gdbarch, pst->text_high (objfile)),
7796 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7797}
7798
7799/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7800 Process compilation unit THIS_CU for a psymtab. */
7801
7802static void
7803process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
7804 int want_partial_unit,
7805 enum language pretend_language)
0018ea6f
DE
7806{
7807 /* If this compilation unit was already read in, free the
7808 cached copy in order to read it in again. This is
7809 necessary because we skipped some symbols when we first
7810 read in the compilation unit (see load_partial_dies).
7811 This problem could be avoided, but the benefit is unclear. */
7812 if (this_cu->cu != NULL)
7813 free_one_cached_comp_unit (this_cu);
7814
c0ab21c2
TT
7815 cutu_reader reader (this_cu, NULL, 0, 0, false);
7816
7817 if (reader.dummy_p)
f1902523 7818 {
c0ab21c2 7819 /* Nothing. */
f1902523 7820 }
c0ab21c2 7821 else if (this_cu->is_debug_types)
3e225074
TT
7822 build_type_psymtabs_reader (&reader, reader.info_ptr,
7823 reader.comp_unit_die);
c0ab21c2
TT
7824 else
7825 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7826 reader.comp_unit_die,
c0ab21c2
TT
7827 want_partial_unit,
7828 pretend_language);
0018ea6f
DE
7829
7830 /* Age out any secondary CUs. */
ed2dc618 7831 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7832}
f4dc4d17
DE
7833
7834/* Reader function for build_type_psymtabs. */
7835
7836static void
7837build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7838 const gdb_byte *info_ptr,
3e225074 7839 struct die_info *type_unit_die)
f4dc4d17 7840{
ed2dc618 7841 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7842 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7843 struct objfile *objfile = dwarf2_per_objfile->objfile;
7844 struct dwarf2_cu *cu = reader->cu;
7845 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7846 struct signatured_type *sig_type;
f4dc4d17
DE
7847 struct type_unit_group *tu_group;
7848 struct attribute *attr;
7849 struct partial_die_info *first_die;
7850 CORE_ADDR lowpc, highpc;
891813be 7851 dwarf2_psymtab *pst;
f4dc4d17 7852
0186c6a7
DE
7853 gdb_assert (per_cu->is_debug_types);
7854 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7855
3e225074 7856 if (! type_unit_die->has_children)
f4dc4d17
DE
7857 return;
7858
7859 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7860 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7861
df07e2c7 7862 if (tu_group->tus == nullptr)
a8b3b8e9 7863 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7864 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7865
7866 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7867 pst = create_partial_symtab (per_cu, "");
6d94535f 7868 pst->anonymous = true;
f4dc4d17
DE
7869
7870 first_die = load_partial_dies (reader, info_ptr, 1);
7871
7872 lowpc = (CORE_ADDR) -1;
7873 highpc = (CORE_ADDR) 0;
7874 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7875
8763cede 7876 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7877}
7878
73051182
DE
7879/* Struct used to sort TUs by their abbreviation table offset. */
7880
7881struct tu_abbrev_offset
7882{
b2bdb8cf
SM
7883 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7884 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7885 {}
7886
7887 signatured_type *sig_type;
73051182
DE
7888 sect_offset abbrev_offset;
7889};
7890
484cf504 7891/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7892
484cf504
TT
7893static bool
7894sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7895 const struct tu_abbrev_offset &b)
73051182 7896{
484cf504 7897 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7898}
7899
7900/* Efficiently read all the type units.
7901 This does the bulk of the work for build_type_psymtabs.
7902
7903 The efficiency is because we sort TUs by the abbrev table they use and
7904 only read each abbrev table once. In one program there are 200K TUs
7905 sharing 8K abbrev tables.
7906
7907 The main purpose of this function is to support building the
7908 dwarf2_per_objfile->type_unit_groups table.
7909 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7910 can collapse the search space by grouping them by stmt_list.
7911 The savings can be significant, in the same program from above the 200K TUs
7912 share 8K stmt_list tables.
7913
7914 FUNC is expected to call get_type_unit_group, which will create the
7915 struct type_unit_group if necessary and add it to
7916 dwarf2_per_objfile->type_unit_groups. */
7917
7918static void
ed2dc618 7919build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7920{
73051182 7921 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7922 abbrev_table_up abbrev_table;
73051182 7923 sect_offset abbrev_offset;
73051182
DE
7924
7925 /* It's up to the caller to not call us multiple times. */
7926 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7927
b2bdb8cf 7928 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7929 return;
7930
7931 /* TUs typically share abbrev tables, and there can be way more TUs than
7932 abbrev tables. Sort by abbrev table to reduce the number of times we
7933 read each abbrev table in.
7934 Alternatives are to punt or to maintain a cache of abbrev tables.
7935 This is simpler and efficient enough for now.
7936
7937 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7938 symtab to use). Typically TUs with the same abbrev offset have the same
7939 stmt_list value too so in practice this should work well.
7940
7941 The basic algorithm here is:
7942
7943 sort TUs by abbrev table
7944 for each TU with same abbrev table:
7945 read abbrev table if first user
7946 read TU top level DIE
7947 [IWBN if DWO skeletons had DW_AT_stmt_list]
7948 call FUNC */
7949
b4f54984 7950 if (dwarf_read_debug)
73051182
DE
7951 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7952
7953 /* Sort in a separate table to maintain the order of all_type_units
7954 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7955 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7956 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7957
7958 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7959 sorted_by_abbrev.emplace_back
7960 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7961 sig_type->per_cu.section,
7962 sig_type->per_cu.sect_off));
73051182 7963
484cf504
TT
7964 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7965 sort_tu_by_abbrev_offset);
73051182 7966
9c541725 7967 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7968
b2bdb8cf 7969 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7970 {
73051182
DE
7971 /* Switch to the next abbrev table if necessary. */
7972 if (abbrev_table == NULL
b2bdb8cf 7973 || tu.abbrev_offset != abbrev_offset)
73051182 7974 {
b2bdb8cf 7975 abbrev_offset = tu.abbrev_offset;
73051182 7976 abbrev_table =
3054dd54 7977 abbrev_table_read_table (dwarf2_per_objfile->objfile,
ed2dc618 7978 &dwarf2_per_objfile->abbrev,
73051182
DE
7979 abbrev_offset);
7980 ++tu_stats->nr_uniq_abbrev_tables;
7981 }
7982
c0ab21c2
TT
7983 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7984 0, 0, false);
7985 if (!reader.dummy_p)
7986 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7987 reader.comp_unit_die);
73051182 7988 }
6aa5f3a6 7989}
73051182 7990
6aa5f3a6
DE
7991/* Print collected type unit statistics. */
7992
7993static void
ed2dc618 7994print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7995{
7996 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7997
7998 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7999 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8000 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8001 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8002 tu_stats->nr_uniq_abbrev_tables);
8003 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8004 tu_stats->nr_symtabs);
8005 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8006 tu_stats->nr_symtab_sharers);
8007 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8008 tu_stats->nr_stmt_less_type_units);
8009 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8010 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8011}
8012
f4dc4d17
DE
8013/* Traversal function for build_type_psymtabs. */
8014
8015static int
8016build_type_psymtab_dependencies (void **slot, void *info)
8017{
ed2dc618
SM
8018 struct dwarf2_per_objfile *dwarf2_per_objfile
8019 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8020 struct objfile *objfile = dwarf2_per_objfile->objfile;
8021 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8022 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 8023 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 8024 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8025 int i;
8026
8027 gdb_assert (len > 0);
0186c6a7 8028 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8029
8030 pst->number_of_dependencies = len;
a9342b62 8031 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8032 for (i = 0; i < len; ++i)
f4dc4d17 8033 {
df07e2c7 8034 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8035 gdb_assert (iter->per_cu.is_debug_types);
8036 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8037 iter->type_unit_group = tu_group;
f4dc4d17
DE
8038 }
8039
df07e2c7
AB
8040 delete tu_group->tus;
8041 tu_group->tus = nullptr;
348e048f
DE
8042
8043 return 1;
8044}
8045
8046/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8047 Build partial symbol tables for the .debug_types comp-units. */
8048
8049static void
ed2dc618 8050build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8051{
ed2dc618 8052 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8053 return;
8054
ed2dc618 8055 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8056}
f4dc4d17 8057
6aa5f3a6
DE
8058/* Traversal function for process_skeletonless_type_unit.
8059 Read a TU in a DWO file and build partial symbols for it. */
8060
8061static int
8062process_skeletonless_type_unit (void **slot, void *info)
8063{
8064 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8065 struct dwarf2_per_objfile *dwarf2_per_objfile
8066 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8067 struct signatured_type find_entry, *entry;
8068
8069 /* If this TU doesn't exist in the global table, add it and read it in. */
8070
8071 if (dwarf2_per_objfile->signatured_types == NULL)
8072 {
8073 dwarf2_per_objfile->signatured_types
ed2dc618 8074 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8075 }
8076
8077 find_entry.signature = dwo_unit->signature;
8078 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8079 INSERT);
8080 /* If we've already seen this type there's nothing to do. What's happening
8081 is we're doing our own version of comdat-folding here. */
8082 if (*slot != NULL)
8083 return 1;
8084
8085 /* This does the job that create_all_type_units would have done for
8086 this TU. */
ed2dc618
SM
8087 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8088 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8089 *slot = entry;
8090
8091 /* This does the job that build_type_psymtabs_1 would have done. */
c0ab21c2
TT
8092 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8093 if (!reader.dummy_p)
8094 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 8095 reader.comp_unit_die);
6aa5f3a6
DE
8096
8097 return 1;
8098}
8099
8100/* Traversal function for process_skeletonless_type_units. */
8101
8102static int
8103process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8104{
8105 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8106
8107 if (dwo_file->tus != NULL)
8108 {
8109 htab_traverse_noresize (dwo_file->tus,
8110 process_skeletonless_type_unit, info);
8111 }
8112
8113 return 1;
8114}
8115
8116/* Scan all TUs of DWO files, verifying we've processed them.
8117 This is needed in case a TU was emitted without its skeleton.
8118 Note: This can't be done until we know what all the DWO files are. */
8119
8120static void
ed2dc618 8121process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8122{
8123 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8124 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8125 && dwarf2_per_objfile->dwo_files != NULL)
8126 {
51ac9db5 8127 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8128 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8129 dwarf2_per_objfile);
6aa5f3a6 8130 }
348e048f
DE
8131}
8132
ed2dc618 8133/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8134
8135static void
ed2dc618 8136set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8137{
b76e467d 8138 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8139 {
891813be 8140 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8141
36586728
TT
8142 if (pst == NULL)
8143 continue;
8144
b76e467d 8145 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8146 {
8147 /* Set the 'user' field only if it is not already set. */
8148 if (pst->dependencies[j]->user == NULL)
8149 pst->dependencies[j]->user = pst;
8150 }
8151 }
8152}
8153
93311388
DE
8154/* Build the partial symbol table by doing a quick pass through the
8155 .debug_info and .debug_abbrev sections. */
72bf9492 8156
93311388 8157static void
ed2dc618 8158dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8159{
ed2dc618 8160 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8161
b4f54984 8162 if (dwarf_read_debug)
45cfd468
DE
8163 {
8164 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8165 objfile_name (objfile));
45cfd468
DE
8166 }
8167
98bfdba5
PA
8168 dwarf2_per_objfile->reading_partial_symbols = 1;
8169
96b79293 8170 dwarf2_per_objfile->info.read (objfile);
91c24f0a 8171
93311388
DE
8172 /* Any cached compilation units will be linked by the per-objfile
8173 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8174 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8175
ed2dc618 8176 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8177
ed2dc618 8178 create_all_comp_units (dwarf2_per_objfile);
c906108c 8179
60606b2c
TT
8180 /* Create a temporary address map on a temporary obstack. We later
8181 copy this to the final obstack. */
8268c778 8182 auto_obstack temp_obstack;
791afaa2
TT
8183
8184 scoped_restore save_psymtabs_addrmap
d320c2b5 8185 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8186 addrmap_create_mutable (&temp_obstack));
72bf9492 8187
b76e467d
SM
8188 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8189 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8190
6aa5f3a6 8191 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8192 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8193
8194 /* Now that all TUs have been processed we can fill in the dependencies. */
8195 if (dwarf2_per_objfile->type_unit_groups != NULL)
8196 {
8197 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8198 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8199 }
8200
b4f54984 8201 if (dwarf_read_debug)
ed2dc618 8202 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8203
ed2dc618 8204 set_partial_user (dwarf2_per_objfile);
95554aad 8205
d320c2b5
TT
8206 objfile->partial_symtabs->psymtabs_addrmap
8207 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8208 objfile->partial_symtabs->obstack ());
791afaa2
TT
8209 /* At this point we want to keep the address map. */
8210 save_psymtabs_addrmap.release ();
ff013f42 8211
b4f54984 8212 if (dwarf_read_debug)
45cfd468 8213 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8214 objfile_name (objfile));
ae038cb0
DJ
8215}
8216
dee91e82
DE
8217/* Load the partial DIEs for a secondary CU into memory.
8218 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8219
dee91e82
DE
8220static void
8221load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8222{
c0ab21c2
TT
8223 cutu_reader reader (this_cu, NULL, 1, 1, false);
8224
8225 if (!reader.dummy_p)
8226 {
8227 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8228 language_minimal);
8229
8230 /* Check if comp unit has_children.
8231 If so, read the rest of the partial symbols from this comp unit.
8232 If not, there's no more debug_info for this comp unit. */
3e225074 8233 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8234 load_partial_dies (&reader, reader.info_ptr, 0);
8235 }
ae038cb0
DJ
8236}
8237
ae038cb0 8238static void
ed2dc618 8239read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8240 struct dwarf2_section_info *section,
f1902523 8241 struct dwarf2_section_info *abbrev_section,
b76e467d 8242 unsigned int is_dwz)
ae038cb0 8243{
d521ce57 8244 const gdb_byte *info_ptr;
ed2dc618 8245 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8246
b4f54984 8247 if (dwarf_read_debug)
bf6af496 8248 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8249 section->get_name (),
8250 section->get_file_name ());
bf6af496 8251
96b79293 8252 section->read (objfile);
ae038cb0 8253
36586728 8254 info_ptr = section->buffer;
6e70227d 8255
36586728 8256 while (info_ptr < section->buffer + section->size)
ae038cb0 8257 {
ae038cb0 8258 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8259
9c541725 8260 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8261
f1902523 8262 comp_unit_head cu_header;
ed2dc618
SM
8263 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8264 abbrev_section, info_ptr,
8265 rcuh_kind::COMPILE);
ae038cb0
DJ
8266
8267 /* Save the compilation unit for later lookup. */
f1902523
JK
8268 if (cu_header.unit_type != DW_UT_type)
8269 {
8270 this_cu = XOBNEW (&objfile->objfile_obstack,
8271 struct dwarf2_per_cu_data);
8272 memset (this_cu, 0, sizeof (*this_cu));
8273 }
8274 else
8275 {
8276 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8277 struct signatured_type);
8278 memset (sig_type, 0, sizeof (*sig_type));
8279 sig_type->signature = cu_header.signature;
8280 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8281 this_cu = &sig_type->per_cu;
8282 }
8283 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8284 this_cu->sect_off = sect_off;
f1902523 8285 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8286 this_cu->is_dwz = is_dwz;
e3b94546 8287 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8288 this_cu->section = section;
ae038cb0 8289
b76e467d 8290 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8291
8292 info_ptr = info_ptr + this_cu->length;
8293 }
36586728
TT
8294}
8295
8296/* Create a list of all compilation units in OBJFILE.
8297 This is only done for -readnow and building partial symtabs. */
8298
8299static void
ed2dc618 8300create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8301{
b76e467d 8302 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8303 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8304 &dwarf2_per_objfile->abbrev, 0);
36586728 8305
b76e467d 8306 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8307 if (dwz != NULL)
ed2dc618 8308 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8309 1);
c906108c
SS
8310}
8311
5734ee8b 8312/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8313 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8314 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8315 DW_AT_ranges). See the comments of add_partial_subprogram on how
8316 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8317
72bf9492
DJ
8318static void
8319scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8320 CORE_ADDR *highpc, int set_addrmap,
8321 struct dwarf2_cu *cu)
c906108c 8322{
72bf9492 8323 struct partial_die_info *pdi;
c906108c 8324
91c24f0a
DC
8325 /* Now, march along the PDI's, descending into ones which have
8326 interesting children but skipping the children of the other ones,
8327 until we reach the end of the compilation unit. */
c906108c 8328
72bf9492 8329 pdi = first_die;
91c24f0a 8330
72bf9492
DJ
8331 while (pdi != NULL)
8332 {
52356b79 8333 pdi->fixup (cu);
c906108c 8334
f55ee35c 8335 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8336 children, so we need to look at them. Ditto for anonymous
8337 enums. */
933c6fe4 8338
72bf9492 8339 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8340 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8341 || pdi->tag == DW_TAG_imported_unit
8342 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8343 {
72bf9492 8344 switch (pdi->tag)
c906108c
SS
8345 {
8346 case DW_TAG_subprogram:
b1dc1806 8347 case DW_TAG_inlined_subroutine:
cdc07690 8348 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8349 break;
72929c62 8350 case DW_TAG_constant:
c906108c
SS
8351 case DW_TAG_variable:
8352 case DW_TAG_typedef:
91c24f0a 8353 case DW_TAG_union_type:
72bf9492 8354 if (!pdi->is_declaration)
63d06c5c 8355 {
72bf9492 8356 add_partial_symbol (pdi, cu);
63d06c5c
DC
8357 }
8358 break;
c906108c 8359 case DW_TAG_class_type:
680b30c7 8360 case DW_TAG_interface_type:
c906108c 8361 case DW_TAG_structure_type:
72bf9492 8362 if (!pdi->is_declaration)
c906108c 8363 {
72bf9492 8364 add_partial_symbol (pdi, cu);
c906108c 8365 }
b7fee5a3
KS
8366 if ((cu->language == language_rust
8367 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8368 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8369 set_addrmap, cu);
c906108c 8370 break;
91c24f0a 8371 case DW_TAG_enumeration_type:
72bf9492
DJ
8372 if (!pdi->is_declaration)
8373 add_partial_enumeration (pdi, cu);
c906108c
SS
8374 break;
8375 case DW_TAG_base_type:
a02abb62 8376 case DW_TAG_subrange_type:
c906108c 8377 /* File scope base type definitions are added to the partial
c5aa993b 8378 symbol table. */
72bf9492 8379 add_partial_symbol (pdi, cu);
c906108c 8380 break;
d9fa45fe 8381 case DW_TAG_namespace:
cdc07690 8382 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8383 break;
5d7cb8df 8384 case DW_TAG_module:
59c35742
AB
8385 if (!pdi->is_declaration)
8386 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8387 break;
95554aad
TT
8388 case DW_TAG_imported_unit:
8389 {
8390 struct dwarf2_per_cu_data *per_cu;
8391
f4dc4d17
DE
8392 /* For now we don't handle imported units in type units. */
8393 if (cu->per_cu->is_debug_types)
8394 {
8395 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8396 " supported in type units [in module %s]"),
518817b3 8397 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8398 }
8399
e3b94546
SM
8400 per_cu = dwarf2_find_containing_comp_unit
8401 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8402 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8403
8404 /* Go read the partial unit, if needed. */
8405 if (per_cu->v.psymtab == NULL)
b93601f3 8406 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8407
ae640021 8408 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8409 }
8410 break;
74921315
KS
8411 case DW_TAG_imported_declaration:
8412 add_partial_symbol (pdi, cu);
8413 break;
c906108c
SS
8414 default:
8415 break;
8416 }
8417 }
8418
72bf9492
DJ
8419 /* If the die has a sibling, skip to the sibling. */
8420
8421 pdi = pdi->die_sibling;
8422 }
8423}
8424
8425/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8426
72bf9492 8427 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8428 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8429 Enumerators are an exception; they use the scope of their parent
8430 enumeration type, i.e. the name of the enumeration type is not
8431 prepended to the enumerator.
91c24f0a 8432
72bf9492
DJ
8433 There are two complexities. One is DW_AT_specification; in this
8434 case "parent" means the parent of the target of the specification,
8435 instead of the direct parent of the DIE. The other is compilers
8436 which do not emit DW_TAG_namespace; in this case we try to guess
8437 the fully qualified name of structure types from their members'
8438 linkage names. This must be done using the DIE's children rather
8439 than the children of any DW_AT_specification target. We only need
8440 to do this for structures at the top level, i.e. if the target of
8441 any DW_AT_specification (if any; otherwise the DIE itself) does not
8442 have a parent. */
8443
8444/* Compute the scope prefix associated with PDI's parent, in
8445 compilation unit CU. The result will be allocated on CU's
8446 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8447 field. NULL is returned if no prefix is necessary. */
15d034d0 8448static const char *
72bf9492
DJ
8449partial_die_parent_scope (struct partial_die_info *pdi,
8450 struct dwarf2_cu *cu)
8451{
15d034d0 8452 const char *grandparent_scope;
72bf9492 8453 struct partial_die_info *parent, *real_pdi;
91c24f0a 8454
72bf9492
DJ
8455 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8456 then this means the parent of the specification DIE. */
8457
8458 real_pdi = pdi;
72bf9492 8459 while (real_pdi->has_specification)
fb816e8b 8460 {
122cf0f2
AB
8461 auto res = find_partial_die (real_pdi->spec_offset,
8462 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8463 real_pdi = res.pdi;
8464 cu = res.cu;
8465 }
72bf9492
DJ
8466
8467 parent = real_pdi->die_parent;
8468 if (parent == NULL)
8469 return NULL;
8470
8471 if (parent->scope_set)
8472 return parent->scope;
8473
52356b79 8474 parent->fixup (cu);
72bf9492 8475
10b3939b 8476 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8477
acebe513
UW
8478 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8479 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8480 Work around this problem here. */
8481 if (cu->language == language_cplus
6e70227d 8482 && parent->tag == DW_TAG_namespace
acebe513
UW
8483 && strcmp (parent->name, "::") == 0
8484 && grandparent_scope == NULL)
8485 {
8486 parent->scope = NULL;
8487 parent->scope_set = 1;
8488 return NULL;
8489 }
8490
0a4b0913 8491 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8492 if (pdi->tag == DW_TAG_enumerator)
8493 /* Enumerators should not get the name of the enumeration as a prefix. */
8494 parent->scope = grandparent_scope;
8495 else if (parent->tag == DW_TAG_namespace
f55ee35c 8496 || parent->tag == DW_TAG_module
72bf9492
DJ
8497 || parent->tag == DW_TAG_structure_type
8498 || parent->tag == DW_TAG_class_type
680b30c7 8499 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8500 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8501 || parent->tag == DW_TAG_enumeration_type
8502 || (cu->language == language_fortran
8503 && parent->tag == DW_TAG_subprogram
8504 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8505 {
8506 if (grandparent_scope == NULL)
8507 parent->scope = parent->name;
8508 else
3e43a32a
MS
8509 parent->scope = typename_concat (&cu->comp_unit_obstack,
8510 grandparent_scope,
f55ee35c 8511 parent->name, 0, cu);
72bf9492 8512 }
72bf9492
DJ
8513 else
8514 {
8515 /* FIXME drow/2004-04-01: What should we be doing with
8516 function-local names? For partial symbols, we should probably be
8517 ignoring them. */
fa9c3fa0
TT
8518 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8519 dwarf_tag_name (parent->tag),
8520 sect_offset_str (pdi->sect_off));
72bf9492 8521 parent->scope = grandparent_scope;
c906108c
SS
8522 }
8523
72bf9492
DJ
8524 parent->scope_set = 1;
8525 return parent->scope;
8526}
8527
8528/* Return the fully scoped name associated with PDI, from compilation unit
8529 CU. The result will be allocated with malloc. */
4568ecf9 8530
43816ebc 8531static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8532partial_die_full_name (struct partial_die_info *pdi,
8533 struct dwarf2_cu *cu)
8534{
15d034d0 8535 const char *parent_scope;
72bf9492 8536
98bfdba5
PA
8537 /* If this is a template instantiation, we can not work out the
8538 template arguments from partial DIEs. So, unfortunately, we have
8539 to go through the full DIEs. At least any work we do building
8540 types here will be reused if full symbols are loaded later. */
8541 if (pdi->has_template_arguments)
8542 {
52356b79 8543 pdi->fixup (cu);
98bfdba5
PA
8544
8545 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8546 {
8547 struct die_info *die;
8548 struct attribute attr;
8549 struct dwarf2_cu *ref_cu = cu;
8550
b64f50a1 8551 /* DW_FORM_ref_addr is using section offset. */
b4069958 8552 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8553 attr.form = DW_FORM_ref_addr;
9c541725 8554 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8555 die = follow_die_ref (NULL, &attr, &ref_cu);
8556
43816ebc 8557 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8558 }
8559 }
8560
72bf9492
DJ
8561 parent_scope = partial_die_parent_scope (pdi, cu);
8562 if (parent_scope == NULL)
8563 return NULL;
8564 else
43816ebc
TT
8565 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8566 pdi->name, 0, cu));
c906108c
SS
8567}
8568
8569static void
72bf9492 8570add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8571{
518817b3
SM
8572 struct dwarf2_per_objfile *dwarf2_per_objfile
8573 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8574 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8576 CORE_ADDR addr = 0;
15d034d0 8577 const char *actual_name = NULL;
e142c38c
DJ
8578 CORE_ADDR baseaddr;
8579
b3b3bada 8580 baseaddr = objfile->text_section_offset ();
c906108c 8581
43816ebc
TT
8582 gdb::unique_xmalloc_ptr<char> built_actual_name
8583 = partial_die_full_name (pdi, cu);
15d034d0 8584 if (built_actual_name != NULL)
43816ebc 8585 actual_name = built_actual_name.get ();
63d06c5c 8586
72bf9492
DJ
8587 if (actual_name == NULL)
8588 actual_name = pdi->name;
8589
c906108c
SS
8590 switch (pdi->tag)
8591 {
b1dc1806 8592 case DW_TAG_inlined_subroutine:
c906108c 8593 case DW_TAG_subprogram:
79748972
TT
8594 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8595 - baseaddr);
0a4b0913
AB
8596 if (pdi->is_external
8597 || cu->language == language_ada
8598 || (cu->language == language_fortran
8599 && pdi->die_parent != NULL
8600 && pdi->die_parent->tag == DW_TAG_subprogram))
8601 {
8602 /* Normally, only "external" DIEs are part of the global scope.
8603 But in Ada and Fortran, we want to be able to access nested
8604 procedures globally. So all Ada and Fortran subprograms are
8605 stored in the global scope. */
31edb802 8606 add_psymbol_to_list (actual_name,
15d034d0 8607 built_actual_name != NULL,
f47fb265 8608 VAR_DOMAIN, LOC_BLOCK,
79748972 8609 SECT_OFF_TEXT (objfile),
75aedd27 8610 psymbol_placement::GLOBAL,
79748972
TT
8611 addr,
8612 cu->language, objfile);
c906108c
SS
8613 }
8614 else
8615 {
31edb802 8616 add_psymbol_to_list (actual_name,
15d034d0 8617 built_actual_name != NULL,
f47fb265 8618 VAR_DOMAIN, LOC_BLOCK,
79748972 8619 SECT_OFF_TEXT (objfile),
75aedd27 8620 psymbol_placement::STATIC,
1762568f 8621 addr, cu->language, objfile);
c906108c 8622 }
0c1b455e
TT
8623
8624 if (pdi->main_subprogram && actual_name != NULL)
8625 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8626 break;
72929c62 8627 case DW_TAG_constant:
31edb802 8628 add_psymbol_to_list (actual_name,
75aedd27
TT
8629 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8630 -1, (pdi->is_external
8631 ? psymbol_placement::GLOBAL
8632 : psymbol_placement::STATIC),
8633 0, cu->language, objfile);
72929c62 8634 break;
c906108c 8635 case DW_TAG_variable:
95554aad
TT
8636 if (pdi->d.locdesc)
8637 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8638
95554aad 8639 if (pdi->d.locdesc
caac4577
JG
8640 && addr == 0
8641 && !dwarf2_per_objfile->has_section_at_zero)
8642 {
8643 /* A global or static variable may also have been stripped
8644 out by the linker if unused, in which case its address
8645 will be nullified; do not add such variables into partial
8646 symbol table then. */
8647 }
8648 else if (pdi->is_external)
c906108c
SS
8649 {
8650 /* Global Variable.
8651 Don't enter into the minimal symbol tables as there is
8652 a minimal symbol table entry from the ELF symbols already.
8653 Enter into partial symbol table if it has a location
8654 descriptor or a type.
8655 If the location descriptor is missing, new_symbol will create
8656 a LOC_UNRESOLVED symbol, the address of the variable will then
8657 be determined from the minimal symbol table whenever the variable
8658 is referenced.
8659 The address for the partial symbol table entry is not
8660 used by GDB, but it comes in handy for debugging partial symbol
8661 table building. */
8662
95554aad 8663 if (pdi->d.locdesc || pdi->has_type)
31edb802 8664 add_psymbol_to_list (actual_name,
15d034d0 8665 built_actual_name != NULL,
f47fb265 8666 VAR_DOMAIN, LOC_STATIC,
79748972 8667 SECT_OFF_TEXT (objfile),
75aedd27 8668 psymbol_placement::GLOBAL,
79748972 8669 addr, cu->language, objfile);
c906108c
SS
8670 }
8671 else
8672 {
ff908ebf
AW
8673 int has_loc = pdi->d.locdesc != NULL;
8674
8675 /* Static Variable. Skip symbols whose value we cannot know (those
8676 without location descriptors or constant values). */
8677 if (!has_loc && !pdi->has_const_value)
43816ebc 8678 return;
ff908ebf 8679
31edb802 8680 add_psymbol_to_list (actual_name,
15d034d0 8681 built_actual_name != NULL,
f47fb265 8682 VAR_DOMAIN, LOC_STATIC,
79748972 8683 SECT_OFF_TEXT (objfile),
75aedd27 8684 psymbol_placement::STATIC,
79748972 8685 has_loc ? addr : 0,
f47fb265 8686 cu->language, objfile);
c906108c
SS
8687 }
8688 break;
8689 case DW_TAG_typedef:
8690 case DW_TAG_base_type:
a02abb62 8691 case DW_TAG_subrange_type:
31edb802 8692 add_psymbol_to_list (actual_name,
15d034d0 8693 built_actual_name != NULL,
79748972 8694 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8695 psymbol_placement::STATIC,
1762568f 8696 0, cu->language, objfile);
c906108c 8697 break;
74921315 8698 case DW_TAG_imported_declaration:
72bf9492 8699 case DW_TAG_namespace:
31edb802 8700 add_psymbol_to_list (actual_name,
15d034d0 8701 built_actual_name != NULL,
79748972 8702 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8703 psymbol_placement::GLOBAL,
1762568f 8704 0, cu->language, objfile);
72bf9492 8705 break;
530e8392 8706 case DW_TAG_module:
a5fd13a9
BH
8707 /* With Fortran 77 there might be a "BLOCK DATA" module
8708 available without any name. If so, we skip the module as it
8709 doesn't bring any value. */
8710 if (actual_name != nullptr)
31edb802 8711 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8712 built_actual_name != NULL,
8713 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8714 psymbol_placement::GLOBAL,
8715 0, cu->language, objfile);
530e8392 8716 break;
c906108c 8717 case DW_TAG_class_type:
680b30c7 8718 case DW_TAG_interface_type:
c906108c
SS
8719 case DW_TAG_structure_type:
8720 case DW_TAG_union_type:
8721 case DW_TAG_enumeration_type:
fa4028e9
JB
8722 /* Skip external references. The DWARF standard says in the section
8723 about "Structure, Union, and Class Type Entries": "An incomplete
8724 structure, union or class type is represented by a structure,
8725 union or class entry that does not have a byte size attribute
8726 and that has a DW_AT_declaration attribute." */
8727 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8728 return;
fa4028e9 8729
63d06c5c
DC
8730 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8731 static vs. global. */
31edb802 8732 add_psymbol_to_list (actual_name,
15d034d0 8733 built_actual_name != NULL,
79748972 8734 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8735 cu->language == language_cplus
75aedd27
TT
8736 ? psymbol_placement::GLOBAL
8737 : psymbol_placement::STATIC,
1762568f 8738 0, cu->language, objfile);
c906108c 8739
c906108c
SS
8740 break;
8741 case DW_TAG_enumerator:
31edb802 8742 add_psymbol_to_list (actual_name,
15d034d0 8743 built_actual_name != NULL,
79748972 8744 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8745 cu->language == language_cplus
75aedd27
TT
8746 ? psymbol_placement::GLOBAL
8747 : psymbol_placement::STATIC,
1762568f 8748 0, cu->language, objfile);
c906108c
SS
8749 break;
8750 default:
8751 break;
8752 }
8753}
8754
5c4e30ca
DC
8755/* Read a partial die corresponding to a namespace; also, add a symbol
8756 corresponding to that namespace to the symbol table. NAMESPACE is
8757 the name of the enclosing namespace. */
91c24f0a 8758
72bf9492
DJ
8759static void
8760add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8761 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8762 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8763{
72bf9492 8764 /* Add a symbol for the namespace. */
e7c27a73 8765
72bf9492 8766 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8767
8768 /* Now scan partial symbols in that namespace. */
8769
91c24f0a 8770 if (pdi->has_children)
cdc07690 8771 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8772}
8773
5d7cb8df
JK
8774/* Read a partial die corresponding to a Fortran module. */
8775
8776static void
8777add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8778 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8779{
530e8392
KB
8780 /* Add a symbol for the namespace. */
8781
8782 add_partial_symbol (pdi, cu);
8783
f55ee35c 8784 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8785
8786 if (pdi->has_children)
cdc07690 8787 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8788}
8789
b1dc1806
XR
8790/* Read a partial die corresponding to a subprogram or an inlined
8791 subprogram and create a partial symbol for that subprogram.
8792 When the CU language allows it, this routine also defines a partial
8793 symbol for each nested subprogram that this subprogram contains.
8794 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8795 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8796
cdc07690
YQ
8797 PDI may also be a lexical block, in which case we simply search
8798 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8799 Again, this is only performed when the CU language allows this
8800 type of definitions. */
8801
8802static void
8803add_partial_subprogram (struct partial_die_info *pdi,
8804 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8805 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8806{
b1dc1806 8807 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8808 {
8809 if (pdi->has_pc_info)
8810 {
8811 if (pdi->lowpc < *lowpc)
8812 *lowpc = pdi->lowpc;
8813 if (pdi->highpc > *highpc)
8814 *highpc = pdi->highpc;
cdc07690 8815 if (set_addrmap)
5734ee8b 8816 {
518817b3 8817 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8818 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8819 CORE_ADDR baseaddr;
b926417a
TT
8820 CORE_ADDR this_highpc;
8821 CORE_ADDR this_lowpc;
5734ee8b 8822
b3b3bada 8823 baseaddr = objfile->text_section_offset ();
b926417a
TT
8824 this_lowpc
8825 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8826 pdi->lowpc + baseaddr)
8827 - baseaddr);
8828 this_highpc
8829 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8830 pdi->highpc + baseaddr)
8831 - baseaddr);
d320c2b5 8832 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8833 this_lowpc, this_highpc - 1,
9291a0cd 8834 cu->per_cu->v.psymtab);
5734ee8b 8835 }
481860b3
GB
8836 }
8837
8838 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8839 {
bc30ff58 8840 if (!pdi->is_declaration)
e8d05480
JB
8841 /* Ignore subprogram DIEs that do not have a name, they are
8842 illegal. Do not emit a complaint at this point, we will
8843 do so when we convert this psymtab into a symtab. */
8844 if (pdi->name)
8845 add_partial_symbol (pdi, cu);
bc30ff58
JB
8846 }
8847 }
6e70227d 8848
bc30ff58
JB
8849 if (! pdi->has_children)
8850 return;
8851
0a4b0913 8852 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8853 {
8854 pdi = pdi->die_child;
8855 while (pdi != NULL)
8856 {
52356b79 8857 pdi->fixup (cu);
bc30ff58 8858 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8859 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8860 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8861 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8862 pdi = pdi->die_sibling;
8863 }
8864 }
8865}
8866
91c24f0a
DC
8867/* Read a partial die corresponding to an enumeration type. */
8868
72bf9492
DJ
8869static void
8870add_partial_enumeration (struct partial_die_info *enum_pdi,
8871 struct dwarf2_cu *cu)
91c24f0a 8872{
72bf9492 8873 struct partial_die_info *pdi;
91c24f0a
DC
8874
8875 if (enum_pdi->name != NULL)
72bf9492
DJ
8876 add_partial_symbol (enum_pdi, cu);
8877
8878 pdi = enum_pdi->die_child;
8879 while (pdi)
91c24f0a 8880 {
72bf9492 8881 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8882 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8883 else
72bf9492
DJ
8884 add_partial_symbol (pdi, cu);
8885 pdi = pdi->die_sibling;
91c24f0a 8886 }
91c24f0a
DC
8887}
8888
6caca83c
CC
8889/* Return the initial uleb128 in the die at INFO_PTR. */
8890
8891static unsigned int
d521ce57 8892peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8893{
8894 unsigned int bytes_read;
8895
8896 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8897}
8898
685af9cd
TT
8899/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8900 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8901
4bb7a0a7
DJ
8902 Return the corresponding abbrev, or NULL if the number is zero (indicating
8903 an empty DIE). In either case *BYTES_READ will be set to the length of
8904 the initial number. */
8905
8906static struct abbrev_info *
685af9cd
TT
8907peek_die_abbrev (const die_reader_specs &reader,
8908 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8909{
685af9cd 8910 dwarf2_cu *cu = reader.cu;
518817b3 8911 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8912 unsigned int abbrev_number
8913 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8914
8915 if (abbrev_number == 0)
8916 return NULL;
8917
685af9cd 8918 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8919 if (!abbrev)
8920 {
422b9917 8921 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8922 " at offset %s [in module %s]"),
422b9917 8923 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8924 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8925 }
8926
8927 return abbrev;
8928}
8929
93311388
DE
8930/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8931 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8932 DIE. Any children of the skipped DIEs will also be skipped. */
8933
d521ce57
TT
8934static const gdb_byte *
8935skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8936{
4bb7a0a7
DJ
8937 while (1)
8938 {
685af9cd
TT
8939 unsigned int bytes_read;
8940 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8941
4bb7a0a7
DJ
8942 if (abbrev == NULL)
8943 return info_ptr + bytes_read;
8944 else
dee91e82 8945 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8946 }
8947}
8948
93311388
DE
8949/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8950 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8951 abbrev corresponding to that skipped uleb128 should be passed in
8952 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8953 children. */
8954
d521ce57
TT
8955static const gdb_byte *
8956skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8957 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8958{
8959 unsigned int bytes_read;
8960 struct attribute attr;
dee91e82
DE
8961 bfd *abfd = reader->abfd;
8962 struct dwarf2_cu *cu = reader->cu;
d521ce57 8963 const gdb_byte *buffer = reader->buffer;
f664829e 8964 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8965 unsigned int form, i;
8966
8967 for (i = 0; i < abbrev->num_attrs; i++)
8968 {
8969 /* The only abbrev we care about is DW_AT_sibling. */
8970 if (abbrev->attrs[i].name == DW_AT_sibling)
8971 {
18a8505e
AT
8972 bool ignored;
8973 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8974 &ignored);
4bb7a0a7 8975 if (attr.form == DW_FORM_ref_addr)
b98664d3 8976 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8977 else
b9502d3f 8978 {
9c541725
PA
8979 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8980 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8981
8982 if (sibling_ptr < info_ptr)
b98664d3 8983 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
8984 else if (sibling_ptr > reader->buffer_end)
8985 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8986 else
8987 return sibling_ptr;
8988 }
4bb7a0a7
DJ
8989 }
8990
8991 /* If it isn't DW_AT_sibling, skip this attribute. */
8992 form = abbrev->attrs[i].form;
8993 skip_attribute:
8994 switch (form)
8995 {
4bb7a0a7 8996 case DW_FORM_ref_addr:
ae411497
TT
8997 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8998 and later it is offset sized. */
8999 if (cu->header.version == 2)
9000 info_ptr += cu->header.addr_size;
9001 else
9002 info_ptr += cu->header.offset_size;
9003 break;
36586728
TT
9004 case DW_FORM_GNU_ref_alt:
9005 info_ptr += cu->header.offset_size;
9006 break;
ae411497 9007 case DW_FORM_addr:
4bb7a0a7
DJ
9008 info_ptr += cu->header.addr_size;
9009 break;
9010 case DW_FORM_data1:
9011 case DW_FORM_ref1:
9012 case DW_FORM_flag:
8fe0f950 9013 case DW_FORM_strx1:
4bb7a0a7
DJ
9014 info_ptr += 1;
9015 break;
2dc7f7b3 9016 case DW_FORM_flag_present:
43988095 9017 case DW_FORM_implicit_const:
2dc7f7b3 9018 break;
4bb7a0a7
DJ
9019 case DW_FORM_data2:
9020 case DW_FORM_ref2:
8fe0f950 9021 case DW_FORM_strx2:
4bb7a0a7
DJ
9022 info_ptr += 2;
9023 break;
8fe0f950
AT
9024 case DW_FORM_strx3:
9025 info_ptr += 3;
9026 break;
4bb7a0a7
DJ
9027 case DW_FORM_data4:
9028 case DW_FORM_ref4:
8fe0f950 9029 case DW_FORM_strx4:
4bb7a0a7
DJ
9030 info_ptr += 4;
9031 break;
9032 case DW_FORM_data8:
9033 case DW_FORM_ref8:
55f1336d 9034 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9035 info_ptr += 8;
9036 break;
0224619f
JK
9037 case DW_FORM_data16:
9038 info_ptr += 16;
9039 break;
4bb7a0a7 9040 case DW_FORM_string:
9b1c24c8 9041 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9042 info_ptr += bytes_read;
9043 break;
2dc7f7b3 9044 case DW_FORM_sec_offset:
4bb7a0a7 9045 case DW_FORM_strp:
36586728 9046 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9047 info_ptr += cu->header.offset_size;
9048 break;
2dc7f7b3 9049 case DW_FORM_exprloc:
4bb7a0a7
DJ
9050 case DW_FORM_block:
9051 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9052 info_ptr += bytes_read;
9053 break;
9054 case DW_FORM_block1:
9055 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9056 break;
9057 case DW_FORM_block2:
9058 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9059 break;
9060 case DW_FORM_block4:
9061 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9062 break;
336d760d 9063 case DW_FORM_addrx:
cf532bd1 9064 case DW_FORM_strx:
4bb7a0a7
DJ
9065 case DW_FORM_sdata:
9066 case DW_FORM_udata:
9067 case DW_FORM_ref_udata:
3019eac3
DE
9068 case DW_FORM_GNU_addr_index:
9069 case DW_FORM_GNU_str_index:
18a8505e 9070 case DW_FORM_rnglistx:
d521ce57 9071 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9072 break;
9073 case DW_FORM_indirect:
9074 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9075 info_ptr += bytes_read;
9076 /* We need to continue parsing from here, so just go back to
9077 the top. */
9078 goto skip_attribute;
9079
9080 default:
3e43a32a
MS
9081 error (_("Dwarf Error: Cannot handle %s "
9082 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9083 dwarf_form_name (form),
9084 bfd_get_filename (abfd));
9085 }
9086 }
9087
9088 if (abbrev->has_children)
dee91e82 9089 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9090 else
9091 return info_ptr;
9092}
9093
93311388 9094/* Locate ORIG_PDI's sibling.
dee91e82 9095 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9096
d521ce57 9097static const gdb_byte *
dee91e82
DE
9098locate_pdi_sibling (const struct die_reader_specs *reader,
9099 struct partial_die_info *orig_pdi,
d521ce57 9100 const gdb_byte *info_ptr)
91c24f0a
DC
9101{
9102 /* Do we know the sibling already? */
72bf9492 9103
91c24f0a
DC
9104 if (orig_pdi->sibling)
9105 return orig_pdi->sibling;
9106
9107 /* Are there any children to deal with? */
9108
9109 if (!orig_pdi->has_children)
9110 return info_ptr;
9111
4bb7a0a7 9112 /* Skip the children the long way. */
91c24f0a 9113
dee91e82 9114 return skip_children (reader, info_ptr);
91c24f0a
DC
9115}
9116
257e7a09 9117/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9118 not NULL. */
c906108c 9119
891813be
TT
9120void
9121dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9122{
ed2dc618
SM
9123 struct dwarf2_per_objfile *dwarf2_per_objfile
9124 = get_dwarf2_per_objfile (objfile);
9125
077cbab2
TT
9126 gdb_assert (!readin);
9127 /* If this psymtab is constructed from a debug-only objfile, the
9128 has_section_at_zero flag will not necessarily be correct. We
9129 can get the correct value for this flag by looking at the data
9130 associated with the (presumably stripped) associated objfile. */
9131 if (objfile->separate_debug_objfile_backlink)
c906108c 9132 {
077cbab2
TT
9133 struct dwarf2_per_objfile *dpo_backlink
9134 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9135
077cbab2
TT
9136 dwarf2_per_objfile->has_section_at_zero
9137 = dpo_backlink->has_section_at_zero;
9138 }
98bfdba5 9139
077cbab2 9140 dwarf2_per_objfile->reading_partial_symbols = 0;
c906108c 9141
8566b89b 9142 expand_psymtab (objfile);
95554aad 9143
ed2dc618 9144 process_cu_includes (dwarf2_per_objfile);
c906108c 9145}
9cdd5dbd
DE
9146\f
9147/* Reading in full CUs. */
c906108c 9148
10b3939b
DJ
9149/* Add PER_CU to the queue. */
9150
9151static void
95554aad
TT
9152queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9153 enum language pretend_language)
10b3939b 9154{
10b3939b 9155 per_cu->queued = 1;
39856def 9156 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
9157}
9158
89e63ee4
DE
9159/* If PER_CU is not yet queued, add it to the queue.
9160 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9161 dependency.
0907af0c 9162 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9163 meaning either PER_CU is already queued or it is already loaded.
9164
9165 N.B. There is an invariant here that if a CU is queued then it is loaded.
9166 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9167
9168static int
89e63ee4 9169maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9170 struct dwarf2_per_cu_data *per_cu,
9171 enum language pretend_language)
9172{
9173 /* We may arrive here during partial symbol reading, if we need full
9174 DIEs to process an unusual case (e.g. template arguments). Do
9175 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9176 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9177 {
9178 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9179 return 1;
9180 return 0;
9181 }
9182
9183 /* Mark the dependence relation so that we don't flush PER_CU
9184 too early. */
89e63ee4
DE
9185 if (dependent_cu != NULL)
9186 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9187
9188 /* If it's already on the queue, we have nothing to do. */
9189 if (per_cu->queued)
9190 return 0;
9191
9192 /* If the compilation unit is already loaded, just mark it as
9193 used. */
9194 if (per_cu->cu != NULL)
9195 {
9196 per_cu->cu->last_used = 0;
9197 return 0;
9198 }
9199
9200 /* Add it to the queue. */
9201 queue_comp_unit (per_cu, pretend_language);
9202
9203 return 1;
9204}
9205
10b3939b
DJ
9206/* Process the queue. */
9207
9208static void
ed2dc618 9209process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 9210{
b4f54984 9211 if (dwarf_read_debug)
45cfd468
DE
9212 {
9213 fprintf_unfiltered (gdb_stdlog,
9214 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9215 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9216 }
9217
03dd20cc
DJ
9218 /* The queue starts out with one item, but following a DIE reference
9219 may load a new CU, adding it to the end of the queue. */
39856def 9220 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 9221 {
39856def
TT
9222 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
9223
cc12ce38 9224 if ((dwarf2_per_objfile->using_index
39856def
TT
9225 ? !item.per_cu->v.quick->compunit_symtab
9226 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 9227 /* Skip dummy CUs. */
39856def 9228 && item.per_cu->cu != NULL)
f4dc4d17 9229 {
39856def 9230 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 9231 unsigned int debug_print_threshold;
247f5c4f 9232 char buf[100];
f4dc4d17 9233
247f5c4f 9234 if (per_cu->is_debug_types)
f4dc4d17 9235 {
247f5c4f
DE
9236 struct signatured_type *sig_type =
9237 (struct signatured_type *) per_cu;
9238
9d8780f0 9239 sprintf (buf, "TU %s at offset %s",
73be47f5 9240 hex_string (sig_type->signature),
9d8780f0 9241 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9242 /* There can be 100s of TUs.
9243 Only print them in verbose mode. */
9244 debug_print_threshold = 2;
f4dc4d17 9245 }
247f5c4f 9246 else
73be47f5 9247 {
9d8780f0
SM
9248 sprintf (buf, "CU at offset %s",
9249 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9250 debug_print_threshold = 1;
9251 }
247f5c4f 9252
b4f54984 9253 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9254 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9255
9256 if (per_cu->is_debug_types)
39856def 9257 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 9258 else
39856def 9259 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 9260
b4f54984 9261 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9262 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9263 }
10b3939b 9264
39856def
TT
9265 item.per_cu->queued = 0;
9266 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
9267 }
9268
b4f54984 9269 if (dwarf_read_debug)
45cfd468
DE
9270 {
9271 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9272 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9273 }
10b3939b
DJ
9274}
9275
10b3939b
DJ
9276/* Read in full symbols for PST, and anything it depends on. */
9277
8566b89b
TT
9278void
9279dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9280{
10b3939b 9281 struct dwarf2_per_cu_data *per_cu;
aaa75496 9282
8566b89b 9283 if (readin)
95554aad
TT
9284 return;
9285
0494dbec 9286 read_dependencies (objfile);
aaa75496 9287
8566b89b 9288 per_cu = per_cu_data;
10b3939b
DJ
9289
9290 if (per_cu == NULL)
aaa75496
JB
9291 {
9292 /* It's an include file, no symbols to read for it.
9293 Everything is in the parent symtab. */
8566b89b 9294 readin = true;
aaa75496
JB
9295 return;
9296 }
c906108c 9297
58f0c718 9298 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9299}
9300
dee91e82
DE
9301/* Trivial hash function for die_info: the hash value of a DIE
9302 is its offset in .debug_info for this objfile. */
10b3939b 9303
dee91e82
DE
9304static hashval_t
9305die_hash (const void *item)
10b3939b 9306{
9a3c8263 9307 const struct die_info *die = (const struct die_info *) item;
6502dd73 9308
9c541725 9309 return to_underlying (die->sect_off);
dee91e82 9310}
63d06c5c 9311
dee91e82
DE
9312/* Trivial comparison function for die_info structures: two DIEs
9313 are equal if they have the same offset. */
98bfdba5 9314
dee91e82
DE
9315static int
9316die_eq (const void *item_lhs, const void *item_rhs)
9317{
9a3c8263
SM
9318 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9319 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9320
9c541725 9321 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9322}
c906108c 9323
c0ab21c2 9324/* Load the DIEs associated with PER_CU into memory. */
c906108c 9325
dee91e82 9326static void
c0ab21c2
TT
9327load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9328 bool skip_partial,
9329 enum language pretend_language)
dee91e82 9330{
c0ab21c2
TT
9331 gdb_assert (! this_cu->is_debug_types);
9332
9333 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9334 if (reader.dummy_p)
9335 return;
9336
9337 struct dwarf2_cu *cu = reader.cu;
9338 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9339
dee91e82
DE
9340 gdb_assert (cu->die_hash == NULL);
9341 cu->die_hash =
9342 htab_create_alloc_ex (cu->header.length / 12,
9343 die_hash,
9344 die_eq,
9345 NULL,
9346 &cu->comp_unit_obstack,
9347 hashtab_obstack_allocate,
9348 dummy_obstack_deallocate);
e142c38c 9349
3e225074 9350 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9351 reader.comp_unit_die->child
9352 = read_die_and_siblings (&reader, reader.info_ptr,
9353 &info_ptr, reader.comp_unit_die);
9354 cu->dies = reader.comp_unit_die;
dee91e82 9355 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9356
9357 /* We try not to read any attributes in this function, because not
9cdd5dbd 9358 all CUs needed for references have been loaded yet, and symbol
10b3939b 9359 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9360 or we won't be able to build types correctly.
9361 Similarly, if we do not read the producer, we can not apply
9362 producer-specific interpretation. */
c0ab21c2 9363 prepare_one_comp_unit (cu, cu->dies, pretend_language);
10b3939b
DJ
9364}
9365
3da10d80
KS
9366/* Add a DIE to the delayed physname list. */
9367
9368static void
9369add_to_method_list (struct type *type, int fnfield_index, int index,
9370 const char *name, struct die_info *die,
9371 struct dwarf2_cu *cu)
9372{
9373 struct delayed_method_info mi;
9374 mi.type = type;
9375 mi.fnfield_index = fnfield_index;
9376 mi.index = index;
9377 mi.name = name;
9378 mi.die = die;
c89b44cd 9379 cu->method_list.push_back (mi);
3da10d80
KS
9380}
9381
3693fdb3
PA
9382/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9383 "const" / "volatile". If so, decrements LEN by the length of the
9384 modifier and return true. Otherwise return false. */
9385
9386template<size_t N>
9387static bool
9388check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9389{
9390 size_t mod_len = sizeof (mod) - 1;
9391 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9392 {
9393 len -= mod_len;
9394 return true;
9395 }
9396 return false;
9397}
9398
3da10d80
KS
9399/* Compute the physnames of any methods on the CU's method list.
9400
9401 The computation of method physnames is delayed in order to avoid the
9402 (bad) condition that one of the method's formal parameters is of an as yet
9403 incomplete type. */
9404
9405static void
9406compute_delayed_physnames (struct dwarf2_cu *cu)
9407{
3693fdb3 9408 /* Only C++ delays computing physnames. */
c89b44cd 9409 if (cu->method_list.empty ())
3693fdb3
PA
9410 return;
9411 gdb_assert (cu->language == language_cplus);
9412
52941706 9413 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9414 {
1d06ead6 9415 const char *physname;
3da10d80 9416 struct fn_fieldlist *fn_flp
c89b44cd
TT
9417 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9418 physname = dwarf2_physname (mi.name, mi.die, cu);
9419 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9420 = physname ? physname : "";
3693fdb3
PA
9421
9422 /* Since there's no tag to indicate whether a method is a
9423 const/volatile overload, extract that information out of the
9424 demangled name. */
9425 if (physname != NULL)
9426 {
9427 size_t len = strlen (physname);
9428
9429 while (1)
9430 {
9431 if (physname[len] == ')') /* shortcut */
9432 break;
9433 else if (check_modifier (physname, len, " const"))
c89b44cd 9434 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9435 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9436 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9437 else
9438 break;
9439 }
9440 }
3da10d80 9441 }
c89b44cd
TT
9442
9443 /* The list is no longer needed. */
9444 cu->method_list.clear ();
3da10d80
KS
9445}
9446
a766d390
DE
9447/* Go objects should be embedded in a DW_TAG_module DIE,
9448 and it's not clear if/how imported objects will appear.
9449 To keep Go support simple until that's worked out,
9450 go back through what we've read and create something usable.
9451 We could do this while processing each DIE, and feels kinda cleaner,
9452 but that way is more invasive.
9453 This is to, for example, allow the user to type "p var" or "b main"
9454 without having to specify the package name, and allow lookups
9455 of module.object to work in contexts that use the expression
9456 parser. */
9457
9458static void
9459fixup_go_packaging (struct dwarf2_cu *cu)
9460{
421d1616 9461 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9462 struct pending *list;
9463 int i;
9464
c24bdb02 9465 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9466 list != NULL;
9467 list = list->next)
a766d390
DE
9468 {
9469 for (i = 0; i < list->nsyms; ++i)
9470 {
9471 struct symbol *sym = list->symbol[i];
9472
c1b5c1eb 9473 if (sym->language () == language_go
a766d390
DE
9474 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9475 {
421d1616
TT
9476 gdb::unique_xmalloc_ptr<char> this_package_name
9477 (go_symbol_package_name (sym));
a766d390
DE
9478
9479 if (this_package_name == NULL)
9480 continue;
9481 if (package_name == NULL)
421d1616 9482 package_name = std::move (this_package_name);
a766d390
DE
9483 else
9484 {
518817b3
SM
9485 struct objfile *objfile
9486 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9487 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9488 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9489 (symbol_symtab (sym) != NULL
9490 ? symtab_to_filename_for_display
9491 (symbol_symtab (sym))
e3b94546 9492 : objfile_name (objfile)),
421d1616 9493 this_package_name.get (), package_name.get ());
a766d390
DE
9494 }
9495 }
9496 }
9497 }
9498
9499 if (package_name != NULL)
9500 {
518817b3 9501 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9502 const char *saved_package_name
421d1616 9503 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9504 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9505 saved_package_name);
a766d390
DE
9506 struct symbol *sym;
9507
e623cf5d 9508 sym = allocate_symbol (objfile);
d3ecddab 9509 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9510 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9511 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9512 e.g., "main" finds the "main" module and not C's main(). */
9513 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9514 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9515 SYMBOL_TYPE (sym) = type;
9516
c24bdb02 9517 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9518 }
9519}
9520
c9317f21
TT
9521/* Allocate a fully-qualified name consisting of the two parts on the
9522 obstack. */
9523
9524static const char *
9525rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9526{
9527 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9528}
9529
9530/* A helper that allocates a struct discriminant_info to attach to a
9531 union type. */
9532
9533static struct discriminant_info *
9534alloc_discriminant_info (struct type *type, int discriminant_index,
9535 int default_index)
9536{
9537 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9538 gdb_assert (discriminant_index == -1
9539 || (discriminant_index >= 0
9540 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9541 gdb_assert (default_index == -1
c7b15a66 9542 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9543
9544 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9545
9546 struct discriminant_info *disc
9547 = ((struct discriminant_info *)
9548 TYPE_ZALLOC (type,
9549 offsetof (struct discriminant_info, discriminants)
9550 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9551 disc->default_index = default_index;
9552 disc->discriminant_index = discriminant_index;
9553
9554 struct dynamic_prop prop;
9555 prop.kind = PROP_UNDEFINED;
9556 prop.data.baton = disc;
9557
9558 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9559
9560 return disc;
9561}
9562
9563/* Some versions of rustc emitted enums in an unusual way.
9564
9565 Ordinary enums were emitted as unions. The first element of each
9566 structure in the union was named "RUST$ENUM$DISR". This element
9567 held the discriminant.
9568
9569 These versions of Rust also implemented the "non-zero"
9570 optimization. When the enum had two values, and one is empty and
9571 the other holds a pointer that cannot be zero, the pointer is used
9572 as the discriminant, with a zero value meaning the empty variant.
9573 Here, the union's first member is of the form
9574 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9575 where the fieldnos are the indices of the fields that should be
9576 traversed in order to find the field (which may be several fields deep)
9577 and the variantname is the name of the variant of the case when the
9578 field is zero.
9579
9580 This function recognizes whether TYPE is of one of these forms,
9581 and, if so, smashes it to be a variant type. */
9582
9583static void
9584quirk_rust_enum (struct type *type, struct objfile *objfile)
9585{
9586 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9587
9588 /* We don't need to deal with empty enums. */
9589 if (TYPE_NFIELDS (type) == 0)
9590 return;
9591
9592#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9593 if (TYPE_NFIELDS (type) == 1
9594 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9595 {
9596 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9597
9598 /* Decode the field name to find the offset of the
9599 discriminant. */
9600 ULONGEST bit_offset = 0;
9601 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9602 while (name[0] >= '0' && name[0] <= '9')
9603 {
9604 char *tail;
9605 unsigned long index = strtoul (name, &tail, 10);
9606 name = tail;
9607 if (*name != '$'
9608 || index >= TYPE_NFIELDS (field_type)
9609 || (TYPE_FIELD_LOC_KIND (field_type, index)
9610 != FIELD_LOC_KIND_BITPOS))
9611 {
b98664d3 9612 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9613 "[in module %s]"),
9614 TYPE_FIELD_NAME (type, 0),
9615 objfile_name (objfile));
9616 return;
9617 }
9618 ++name;
9619
9620 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9621 field_type = TYPE_FIELD_TYPE (field_type, index);
9622 }
9623
9624 /* Make a union to hold the variants. */
9625 struct type *union_type = alloc_type (objfile);
9626 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9627 TYPE_NFIELDS (union_type) = 3;
9628 TYPE_FIELDS (union_type)
9629 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9630 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9631 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9632
9633 /* Put the discriminant must at index 0. */
9634 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9635 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9636 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9637 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9638
9639 /* The order of fields doesn't really matter, so put the real
9640 field at index 1 and the data-less field at index 2. */
9641 struct discriminant_info *disc
9642 = alloc_discriminant_info (union_type, 0, 1);
9643 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9644 TYPE_FIELD_NAME (union_type, 1)
9645 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9646 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9647 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9648 TYPE_FIELD_NAME (union_type, 1));
9649
9650 const char *dataless_name
9651 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9652 name);
9653 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9654 dataless_name);
9655 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9656 /* NAME points into the original discriminant name, which
9657 already has the correct lifetime. */
9658 TYPE_FIELD_NAME (union_type, 2) = name;
9659 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9660 disc->discriminants[2] = 0;
9661
9662 /* Smash this type to be a structure type. We have to do this
9663 because the type has already been recorded. */
9664 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9665 TYPE_NFIELDS (type) = 1;
9666 TYPE_FIELDS (type)
9667 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9668
9669 /* Install the variant part. */
9670 TYPE_FIELD_TYPE (type, 0) = union_type;
9671 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9672 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9673 }
77c2dba3
TT
9674 /* A union with a single anonymous field is probably an old-style
9675 univariant enum. */
9676 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9677 {
c9317f21
TT
9678 /* Smash this type to be a structure type. We have to do this
9679 because the type has already been recorded. */
9680 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9681
9682 /* Make a union to hold the variants. */
9683 struct type *union_type = alloc_type (objfile);
9684 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9685 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9686 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9687 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9688 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9689
9690 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9691 const char *variant_name
9692 = rust_last_path_segment (TYPE_NAME (field_type));
9693 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9694 TYPE_NAME (field_type)
9695 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9696 TYPE_NAME (type), variant_name);
c9317f21
TT
9697
9698 /* Install the union in the outer struct type. */
9699 TYPE_NFIELDS (type) = 1;
9700 TYPE_FIELDS (type)
9701 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9702 TYPE_FIELD_TYPE (type, 0) = union_type;
9703 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9704 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9705
9706 alloc_discriminant_info (union_type, -1, 0);
9707 }
9708 else
9709 {
9710 struct type *disr_type = nullptr;
9711 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9712 {
9713 disr_type = TYPE_FIELD_TYPE (type, i);
9714
a037790e
TT
9715 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9716 {
9717 /* All fields of a true enum will be structs. */
9718 return;
9719 }
9720 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9721 {
9722 /* Could be data-less variant, so keep going. */
a037790e 9723 disr_type = nullptr;
c9317f21
TT
9724 }
9725 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9726 "RUST$ENUM$DISR") != 0)
9727 {
9728 /* Not a Rust enum. */
9729 return;
9730 }
9731 else
9732 {
9733 /* Found one. */
9734 break;
9735 }
9736 }
9737
9738 /* If we got here without a discriminant, then it's probably
9739 just a union. */
9740 if (disr_type == nullptr)
9741 return;
9742
9743 /* Smash this type to be a structure type. We have to do this
9744 because the type has already been recorded. */
9745 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9746
9747 /* Make a union to hold the variants. */
9748 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9749 struct type *union_type = alloc_type (objfile);
9750 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9751 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9752 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9753 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9754 TYPE_FIELDS (union_type)
9755 = (struct field *) TYPE_ZALLOC (union_type,
9756 (TYPE_NFIELDS (union_type)
9757 * sizeof (struct field)));
9758
9759 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9760 TYPE_NFIELDS (type) * sizeof (struct field));
9761
9762 /* Install the discriminant at index 0 in the union. */
9763 TYPE_FIELD (union_type, 0) = *disr_field;
9764 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9765 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9766
9767 /* Install the union in the outer struct type. */
9768 TYPE_FIELD_TYPE (type, 0) = union_type;
9769 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9770 TYPE_NFIELDS (type) = 1;
9771
9772 /* Set the size and offset of the union type. */
9773 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9774
9775 /* We need a way to find the correct discriminant given a
9776 variant name. For convenience we build a map here. */
9777 struct type *enum_type = FIELD_TYPE (*disr_field);
9778 std::unordered_map<std::string, ULONGEST> discriminant_map;
9779 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9780 {
9781 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9782 {
9783 const char *name
9784 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9785 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9786 }
9787 }
9788
9789 int n_fields = TYPE_NFIELDS (union_type);
9790 struct discriminant_info *disc
9791 = alloc_discriminant_info (union_type, 0, -1);
9792 /* Skip the discriminant here. */
9793 for (int i = 1; i < n_fields; ++i)
9794 {
9795 /* Find the final word in the name of this variant's type.
9796 That name can be used to look up the correct
9797 discriminant. */
9798 const char *variant_name
9799 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9800 i)));
9801
9802 auto iter = discriminant_map.find (variant_name);
9803 if (iter != discriminant_map.end ())
9804 disc->discriminants[i] = iter->second;
9805
bedda9ac 9806 /* Remove the discriminant field, if it exists. */
c9317f21 9807 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9808 if (TYPE_NFIELDS (sub_type) > 0)
9809 {
9810 --TYPE_NFIELDS (sub_type);
9811 ++TYPE_FIELDS (sub_type);
9812 }
c9317f21
TT
9813 TYPE_FIELD_NAME (union_type, i) = variant_name;
9814 TYPE_NAME (sub_type)
9815 = rust_fully_qualify (&objfile->objfile_obstack,
9816 TYPE_NAME (type), variant_name);
9817 }
9818 }
9819}
9820
9821/* Rewrite some Rust unions to be structures with variants parts. */
9822
9823static void
9824rust_union_quirks (struct dwarf2_cu *cu)
9825{
9826 gdb_assert (cu->language == language_rust);
52941706
SM
9827 for (type *type_ : cu->rust_unions)
9828 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9829 /* We don't need this any more. */
9830 cu->rust_unions.clear ();
c9317f21
TT
9831}
9832
95554aad
TT
9833/* Return the symtab for PER_CU. This works properly regardless of
9834 whether we're using the index or psymtabs. */
9835
43f3e411
DE
9836static struct compunit_symtab *
9837get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9838{
ed2dc618 9839 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9840 ? per_cu->v.quick->compunit_symtab
9841 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9842}
9843
9844/* A helper function for computing the list of all symbol tables
9845 included by PER_CU. */
9846
9847static void
4c39bc03 9848recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9849 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9850 struct dwarf2_per_cu_data *per_cu,
43f3e411 9851 struct compunit_symtab *immediate_parent)
95554aad
TT
9852{
9853 void **slot;
43f3e411 9854 struct compunit_symtab *cust;
95554aad
TT
9855
9856 slot = htab_find_slot (all_children, per_cu, INSERT);
9857 if (*slot != NULL)
9858 {
9859 /* This inclusion and its children have been processed. */
9860 return;
9861 }
9862
9863 *slot = per_cu;
9864 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9865 cust = get_compunit_symtab (per_cu);
9866 if (cust != NULL)
ec94af83
DE
9867 {
9868 /* If this is a type unit only add its symbol table if we haven't
9869 seen it yet (type unit per_cu's can share symtabs). */
9870 if (per_cu->is_debug_types)
9871 {
43f3e411 9872 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9873 if (*slot == NULL)
9874 {
43f3e411 9875 *slot = cust;
4c39bc03 9876 result->push_back (cust);
43f3e411
DE
9877 if (cust->user == NULL)
9878 cust->user = immediate_parent;
ec94af83
DE
9879 }
9880 }
9881 else
f9125b6c 9882 {
4c39bc03 9883 result->push_back (cust);
43f3e411
DE
9884 if (cust->user == NULL)
9885 cust->user = immediate_parent;
f9125b6c 9886 }
ec94af83 9887 }
95554aad 9888
ae640021
AB
9889 if (!per_cu->imported_symtabs_empty ())
9890 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9891 {
9892 recursively_compute_inclusions (result, all_children,
9893 all_type_symtabs, ptr, cust);
9894 }
95554aad
TT
9895}
9896
43f3e411 9897/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9898 PER_CU. */
9899
9900static void
43f3e411 9901compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9902{
f4dc4d17
DE
9903 gdb_assert (! per_cu->is_debug_types);
9904
ae640021 9905 if (!per_cu->imported_symtabs_empty ())
95554aad 9906 {
ae640021 9907 int len;
4c39bc03 9908 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9909 htab_t all_children, all_type_symtabs;
43f3e411 9910 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9911
9912 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9913 if (cust == NULL)
95554aad
TT
9914 return;
9915
9916 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9917 NULL, xcalloc, xfree);
ec94af83
DE
9918 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9919 NULL, xcalloc, xfree);
95554aad 9920
ae640021 9921 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9922 {
9923 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9924 all_type_symtabs, ptr, cust);
ec94af83 9925 }
95554aad 9926
ec94af83 9927 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9928 len = result_symtabs.size ();
43f3e411 9929 cust->includes
ed2dc618 9930 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9931 struct compunit_symtab *, len + 1);
4c39bc03
TT
9932 memcpy (cust->includes, result_symtabs.data (),
9933 len * sizeof (compunit_symtab *));
43f3e411 9934 cust->includes[len] = NULL;
95554aad 9935
95554aad 9936 htab_delete (all_children);
ec94af83 9937 htab_delete (all_type_symtabs);
95554aad
TT
9938 }
9939}
9940
9941/* Compute the 'includes' field for the symtabs of all the CUs we just
9942 read. */
9943
9944static void
ed2dc618 9945process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9946{
71b73764 9947 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9948 {
9949 if (! iter->is_debug_types)
43f3e411 9950 compute_compunit_symtab_includes (iter);
f4dc4d17 9951 }
95554aad 9952
c5d0225d 9953 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9954}
9955
9cdd5dbd 9956/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9957 already been loaded into memory. */
9958
9959static void
95554aad
TT
9960process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9961 enum language pretend_language)
10b3939b 9962{
10b3939b 9963 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9964 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9965 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9966 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9967 CORE_ADDR lowpc, highpc;
43f3e411 9968 struct compunit_symtab *cust;
10b3939b 9969 CORE_ADDR baseaddr;
4359dff1 9970 struct block *static_block;
3e29f34a 9971 CORE_ADDR addr;
10b3939b 9972
b3b3bada 9973 baseaddr = objfile->text_section_offset ();
10b3939b 9974
c89b44cd
TT
9975 /* Clear the list here in case something was left over. */
9976 cu->method_list.clear ();
10b3939b 9977
95554aad
TT
9978 cu->language = pretend_language;
9979 cu->language_defn = language_def (cu->language);
9980
c906108c 9981 /* Do line number decoding in read_file_scope () */
10b3939b 9982 process_die (cu->dies, cu);
c906108c 9983
a766d390
DE
9984 /* For now fudge the Go package. */
9985 if (cu->language == language_go)
9986 fixup_go_packaging (cu);
9987
5f48f8f3 9988 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9989 should be complete, and it should now be safe to compute all of the
9990 physnames. */
9991 compute_delayed_physnames (cu);
3da10d80 9992
c9317f21
TT
9993 if (cu->language == language_rust)
9994 rust_union_quirks (cu);
9995
fae299cd
DC
9996 /* Some compilers don't define a DW_AT_high_pc attribute for the
9997 compilation unit. If the DW_AT_high_pc is missing, synthesize
9998 it, by scanning the DIE's below the compilation unit. */
10b3939b 9999 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10000
3e29f34a 10001 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10002 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10003
10004 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10005 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10006 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10007 addrmap to help ensure it has an accurate map of pc values belonging to
10008 this comp unit. */
10009 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10010
c24bdb02 10011 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10012 SECT_OFF_TEXT (objfile),
10013 0);
c906108c 10014
43f3e411 10015 if (cust != NULL)
c906108c 10016 {
df15bd07 10017 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10018
8be455d7
JK
10019 /* Set symtab language to language from DW_AT_language. If the
10020 compilation is from a C file generated by language preprocessors, do
10021 not set the language if it was already deduced by start_subfile. */
43f3e411 10022 if (!(cu->language == language_c
40e3ad0e 10023 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10024 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10025
10026 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10027 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10028 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10029 there were bugs in prologue debug info, fixed later in GCC-4.5
10030 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10031
10032 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10033 needed, it would be wrong due to missing DW_AT_producer there.
10034
10035 Still one can confuse GDB by using non-standard GCC compilation
10036 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10037 */
ab260dad 10038 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10039 cust->locations_valid = 1;
e0d00bc7
JK
10040
10041 if (gcc_4_minor >= 5)
43f3e411 10042 cust->epilogue_unwind_valid = 1;
96408a79 10043
43f3e411 10044 cust->call_site_htab = cu->call_site_htab;
c906108c 10045 }
9291a0cd
TT
10046
10047 if (dwarf2_per_objfile->using_index)
43f3e411 10048 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10049 else
10050 {
891813be 10051 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10052 pst->compunit_symtab = cust;
6d94535f 10053 pst->readin = true;
9291a0cd 10054 }
c906108c 10055
95554aad 10056 /* Push it for inclusion processing later. */
c5d0225d 10057 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10058
10059 /* Not needed any more. */
c24bdb02 10060 cu->reset_builder ();
f4dc4d17 10061}
45cfd468 10062
f4dc4d17
DE
10063/* Generate full symbol information for type unit PER_CU, whose DIEs have
10064 already been loaded into memory. */
10065
10066static void
10067process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10068 enum language pretend_language)
10069{
10070 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10071 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10072 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10073 struct compunit_symtab *cust;
0186c6a7
DE
10074 struct signatured_type *sig_type;
10075
10076 gdb_assert (per_cu->is_debug_types);
10077 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10078
c89b44cd
TT
10079 /* Clear the list here in case something was left over. */
10080 cu->method_list.clear ();
f4dc4d17 10081
f4dc4d17
DE
10082 cu->language = pretend_language;
10083 cu->language_defn = language_def (cu->language);
10084
10085 /* The symbol tables are set up in read_type_unit_scope. */
10086 process_die (cu->dies, cu);
10087
10088 /* For now fudge the Go package. */
10089 if (cu->language == language_go)
10090 fixup_go_packaging (cu);
10091
5f48f8f3 10092 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10093 should be complete, and it should now be safe to compute all of the
10094 physnames. */
10095 compute_delayed_physnames (cu);
f4dc4d17 10096
c9317f21
TT
10097 if (cu->language == language_rust)
10098 rust_union_quirks (cu);
10099
f4dc4d17
DE
10100 /* TUs share symbol tables.
10101 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10102 of it with end_expandable_symtab. Otherwise, complete the addition of
10103 this TU's symbols to the existing symtab. */
43f3e411 10104 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10105 {
c24bdb02
KS
10106 buildsym_compunit *builder = cu->get_builder ();
10107 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10108 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10109
43f3e411 10110 if (cust != NULL)
f4dc4d17
DE
10111 {
10112 /* Set symtab language to language from DW_AT_language. If the
10113 compilation is from a C file generated by language preprocessors,
10114 do not set the language if it was already deduced by
10115 start_subfile. */
43f3e411
DE
10116 if (!(cu->language == language_c
10117 && COMPUNIT_FILETABS (cust)->language != language_c))
10118 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10119 }
10120 }
10121 else
10122 {
c24bdb02 10123 cu->get_builder ()->augment_type_symtab ();
43f3e411 10124 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10125 }
10126
10127 if (dwarf2_per_objfile->using_index)
43f3e411 10128 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10129 else
10130 {
891813be 10131 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10132 pst->compunit_symtab = cust;
6d94535f 10133 pst->readin = true;
45cfd468 10134 }
804d2729
TT
10135
10136 /* Not needed any more. */
c24bdb02 10137 cu->reset_builder ();
c906108c
SS
10138}
10139
95554aad
TT
10140/* Process an imported unit DIE. */
10141
10142static void
10143process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10144{
10145 struct attribute *attr;
10146
f4dc4d17
DE
10147 /* For now we don't handle imported units in type units. */
10148 if (cu->per_cu->is_debug_types)
10149 {
10150 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10151 " supported in type units [in module %s]"),
518817b3 10152 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10153 }
10154
95554aad
TT
10155 attr = dwarf2_attr (die, DW_AT_import, cu);
10156 if (attr != NULL)
10157 {
9c541725
PA
10158 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10159 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10160 dwarf2_per_cu_data *per_cu
e3b94546 10161 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10162 cu->per_cu->dwarf2_per_objfile);
95554aad 10163
69d751e3 10164 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10165 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10166 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10167
ae640021 10168 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10169 }
10170}
10171
4c8aa72d
PA
10172/* RAII object that represents a process_die scope: i.e.,
10173 starts/finishes processing a DIE. */
10174class process_die_scope
adde2bff 10175{
4c8aa72d
PA
10176public:
10177 process_die_scope (die_info *die, dwarf2_cu *cu)
10178 : m_die (die), m_cu (cu)
10179 {
10180 /* We should only be processing DIEs not already in process. */
10181 gdb_assert (!m_die->in_process);
10182 m_die->in_process = true;
10183 }
8c3cb9fa 10184
4c8aa72d
PA
10185 ~process_die_scope ()
10186 {
10187 m_die->in_process = false;
10188
10189 /* If we're done processing the DIE for the CU that owns the line
10190 header, we don't need the line header anymore. */
10191 if (m_cu->line_header_die_owner == m_die)
10192 {
10193 delete m_cu->line_header;
10194 m_cu->line_header = NULL;
10195 m_cu->line_header_die_owner = NULL;
10196 }
10197 }
10198
10199private:
10200 die_info *m_die;
10201 dwarf2_cu *m_cu;
10202};
adde2bff 10203
c906108c
SS
10204/* Process a die and its children. */
10205
10206static void
e7c27a73 10207process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10208{
4c8aa72d 10209 process_die_scope scope (die, cu);
adde2bff 10210
c906108c
SS
10211 switch (die->tag)
10212 {
10213 case DW_TAG_padding:
10214 break;
10215 case DW_TAG_compile_unit:
95554aad 10216 case DW_TAG_partial_unit:
e7c27a73 10217 read_file_scope (die, cu);
c906108c 10218 break;
348e048f
DE
10219 case DW_TAG_type_unit:
10220 read_type_unit_scope (die, cu);
10221 break;
c906108c 10222 case DW_TAG_subprogram:
0a4b0913
AB
10223 /* Nested subprograms in Fortran get a prefix. */
10224 if (cu->language == language_fortran
10225 && die->parent != NULL
10226 && die->parent->tag == DW_TAG_subprogram)
10227 cu->processing_has_namespace_info = true;
10228 /* Fall through. */
c906108c 10229 case DW_TAG_inlined_subroutine:
edb3359d 10230 read_func_scope (die, cu);
c906108c
SS
10231 break;
10232 case DW_TAG_lexical_block:
14898363
L
10233 case DW_TAG_try_block:
10234 case DW_TAG_catch_block:
e7c27a73 10235 read_lexical_block_scope (die, cu);
c906108c 10236 break;
216f72a1 10237 case DW_TAG_call_site:
96408a79
SA
10238 case DW_TAG_GNU_call_site:
10239 read_call_site_scope (die, cu);
10240 break;
c906108c 10241 case DW_TAG_class_type:
680b30c7 10242 case DW_TAG_interface_type:
c906108c
SS
10243 case DW_TAG_structure_type:
10244 case DW_TAG_union_type:
134d01f1 10245 process_structure_scope (die, cu);
c906108c
SS
10246 break;
10247 case DW_TAG_enumeration_type:
134d01f1 10248 process_enumeration_scope (die, cu);
c906108c 10249 break;
134d01f1 10250
f792889a
DJ
10251 /* These dies have a type, but processing them does not create
10252 a symbol or recurse to process the children. Therefore we can
10253 read them on-demand through read_type_die. */
c906108c 10254 case DW_TAG_subroutine_type:
72019c9c 10255 case DW_TAG_set_type:
c906108c 10256 case DW_TAG_array_type:
c906108c 10257 case DW_TAG_pointer_type:
c906108c 10258 case DW_TAG_ptr_to_member_type:
c906108c 10259 case DW_TAG_reference_type:
4297a3f0 10260 case DW_TAG_rvalue_reference_type:
c906108c 10261 case DW_TAG_string_type:
c906108c 10262 break;
134d01f1 10263
c906108c 10264 case DW_TAG_base_type:
a02abb62 10265 case DW_TAG_subrange_type:
cb249c71 10266 case DW_TAG_typedef:
134d01f1
DJ
10267 /* Add a typedef symbol for the type definition, if it has a
10268 DW_AT_name. */
f792889a 10269 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10270 break;
c906108c 10271 case DW_TAG_common_block:
e7c27a73 10272 read_common_block (die, cu);
c906108c
SS
10273 break;
10274 case DW_TAG_common_inclusion:
10275 break;
d9fa45fe 10276 case DW_TAG_namespace:
9068261f 10277 cu->processing_has_namespace_info = true;
e7c27a73 10278 read_namespace (die, cu);
d9fa45fe 10279 break;
5d7cb8df 10280 case DW_TAG_module:
9068261f 10281 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10282 read_module (die, cu);
10283 break;
d9fa45fe 10284 case DW_TAG_imported_declaration:
9068261f 10285 cu->processing_has_namespace_info = true;
74921315
KS
10286 if (read_namespace_alias (die, cu))
10287 break;
86a73007
TT
10288 /* The declaration is not a global namespace alias. */
10289 /* Fall through. */
d9fa45fe 10290 case DW_TAG_imported_module:
9068261f 10291 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10292 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10293 || cu->language != language_fortran))
b98664d3 10294 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10295 dwarf_tag_name (die->tag));
10296 read_import_statement (die, cu);
d9fa45fe 10297 break;
95554aad
TT
10298
10299 case DW_TAG_imported_unit:
10300 process_imported_unit_die (die, cu);
10301 break;
10302
71a3c369
TT
10303 case DW_TAG_variable:
10304 read_variable (die, cu);
10305 break;
10306
c906108c 10307 default:
e7c27a73 10308 new_symbol (die, NULL, cu);
c906108c
SS
10309 break;
10310 }
10311}
ca69b9e6
DE
10312\f
10313/* DWARF name computation. */
c906108c 10314
94af9270
KS
10315/* A helper function for dwarf2_compute_name which determines whether DIE
10316 needs to have the name of the scope prepended to the name listed in the
10317 die. */
10318
10319static int
10320die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10321{
1c809c68
TT
10322 struct attribute *attr;
10323
94af9270
KS
10324 switch (die->tag)
10325 {
10326 case DW_TAG_namespace:
10327 case DW_TAG_typedef:
10328 case DW_TAG_class_type:
10329 case DW_TAG_interface_type:
10330 case DW_TAG_structure_type:
10331 case DW_TAG_union_type:
10332 case DW_TAG_enumeration_type:
10333 case DW_TAG_enumerator:
10334 case DW_TAG_subprogram:
08a76f8a 10335 case DW_TAG_inlined_subroutine:
94af9270 10336 case DW_TAG_member:
74921315 10337 case DW_TAG_imported_declaration:
94af9270
KS
10338 return 1;
10339
10340 case DW_TAG_variable:
c2b0a229 10341 case DW_TAG_constant:
94af9270
KS
10342 /* We only need to prefix "globally" visible variables. These include
10343 any variable marked with DW_AT_external or any variable that
10344 lives in a namespace. [Variables in anonymous namespaces
10345 require prefixing, but they are not DW_AT_external.] */
10346
10347 if (dwarf2_attr (die, DW_AT_specification, cu))
10348 {
10349 struct dwarf2_cu *spec_cu = cu;
9a619af0 10350
94af9270
KS
10351 return die_needs_namespace (die_specification (die, &spec_cu),
10352 spec_cu);
10353 }
10354
1c809c68 10355 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10356 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10357 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10358 return 0;
10359 /* A variable in a lexical block of some kind does not need a
10360 namespace, even though in C++ such variables may be external
10361 and have a mangled name. */
10362 if (die->parent->tag == DW_TAG_lexical_block
10363 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10364 || die->parent->tag == DW_TAG_catch_block
10365 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10366 return 0;
10367 return 1;
94af9270
KS
10368
10369 default:
10370 return 0;
10371 }
10372}
10373
73b9be8b
KS
10374/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10375 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10376 defined for the given DIE. */
10377
10378static struct attribute *
10379dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10380{
10381 struct attribute *attr;
10382
10383 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10384 if (attr == NULL)
10385 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10386
10387 return attr;
10388}
10389
10390/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10391 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10392 defined for the given DIE. */
10393
10394static const char *
10395dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10396{
10397 const char *linkage_name;
10398
10399 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10400 if (linkage_name == NULL)
10401 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10402
10403 return linkage_name;
10404}
10405
94af9270 10406/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10407 compute the physname for the object, which include a method's:
9c37b5ae 10408 - formal parameters (C++),
a766d390 10409 - receiver type (Go),
a766d390
DE
10410
10411 The term "physname" is a bit confusing.
10412 For C++, for example, it is the demangled name.
10413 For Go, for example, it's the mangled name.
94af9270 10414
af6b7be1
JB
10415 For Ada, return the DIE's linkage name rather than the fully qualified
10416 name. PHYSNAME is ignored..
10417
94af9270
KS
10418 The result is allocated on the objfile_obstack and canonicalized. */
10419
10420static const char *
15d034d0
TT
10421dwarf2_compute_name (const char *name,
10422 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10423 int physname)
10424{
518817b3 10425 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10426
94af9270
KS
10427 if (name == NULL)
10428 name = dwarf2_name (die, cu);
10429
2ee7123e
DE
10430 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10431 but otherwise compute it by typename_concat inside GDB.
10432 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10433 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10434 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10435 will set the demangled name to the result of dwarf2_full_name, and it is
10436 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10437 if (cu->language == language_ada
10438 || (cu->language == language_fortran && physname))
10439 {
10440 /* For Ada unit, we prefer the linkage name over the name, as
10441 the former contains the exported name, which the user expects
10442 to be able to reference. Ideally, we want the user to be able
10443 to reference this entity using either natural or linkage name,
10444 but we haven't started looking at this enhancement yet. */
73b9be8b 10445 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10446
2ee7123e
DE
10447 if (linkage_name != NULL)
10448 return linkage_name;
f55ee35c
JK
10449 }
10450
94af9270
KS
10451 /* These are the only languages we know how to qualify names in. */
10452 if (name != NULL
9c37b5ae 10453 && (cu->language == language_cplus
c44af4eb
TT
10454 || cu->language == language_fortran || cu->language == language_d
10455 || cu->language == language_rust))
94af9270
KS
10456 {
10457 if (die_needs_namespace (die, cu))
10458 {
0d5cff50 10459 const char *prefix;
34a68019 10460 const char *canonical_name = NULL;
94af9270 10461
d7e74731
PA
10462 string_file buf;
10463
94af9270 10464 prefix = determine_prefix (die, cu);
94af9270
KS
10465 if (*prefix != '\0')
10466 {
43816ebc
TT
10467 gdb::unique_xmalloc_ptr<char> prefixed_name
10468 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10469
43816ebc 10470 buf.puts (prefixed_name.get ());
94af9270
KS
10471 }
10472 else
d7e74731 10473 buf.puts (name);
94af9270 10474
98bfdba5
PA
10475 /* Template parameters may be specified in the DIE's DW_AT_name, or
10476 as children with DW_TAG_template_type_param or
10477 DW_TAG_value_type_param. If the latter, add them to the name
10478 here. If the name already has template parameters, then
10479 skip this step; some versions of GCC emit both, and
10480 it is more efficient to use the pre-computed name.
10481
10482 Something to keep in mind about this process: it is very
10483 unlikely, or in some cases downright impossible, to produce
10484 something that will match the mangled name of a function.
10485 If the definition of the function has the same debug info,
10486 we should be able to match up with it anyway. But fallbacks
10487 using the minimal symbol, for instance to find a method
10488 implemented in a stripped copy of libstdc++, will not work.
10489 If we do not have debug info for the definition, we will have to
10490 match them up some other way.
10491
10492 When we do name matching there is a related problem with function
10493 templates; two instantiated function templates are allowed to
10494 differ only by their return types, which we do not add here. */
10495
10496 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10497 {
10498 struct attribute *attr;
10499 struct die_info *child;
10500 int first = 1;
10501
10502 die->building_fullname = 1;
10503
10504 for (child = die->child; child != NULL; child = child->sibling)
10505 {
10506 struct type *type;
12df843f 10507 LONGEST value;
d521ce57 10508 const gdb_byte *bytes;
98bfdba5
PA
10509 struct dwarf2_locexpr_baton *baton;
10510 struct value *v;
10511
10512 if (child->tag != DW_TAG_template_type_param
10513 && child->tag != DW_TAG_template_value_param)
10514 continue;
10515
10516 if (first)
10517 {
d7e74731 10518 buf.puts ("<");
98bfdba5
PA
10519 first = 0;
10520 }
10521 else
d7e74731 10522 buf.puts (", ");
98bfdba5
PA
10523
10524 attr = dwarf2_attr (child, DW_AT_type, cu);
10525 if (attr == NULL)
10526 {
b98664d3 10527 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10528 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10529 continue;
10530 }
10531 type = die_type (child, cu);
10532
10533 if (child->tag == DW_TAG_template_type_param)
10534 {
c1ec8cea
TT
10535 c_print_type (type, "", &buf, -1, 0, cu->language,
10536 &type_print_raw_options);
98bfdba5
PA
10537 continue;
10538 }
10539
10540 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10541 if (attr == NULL)
10542 {
b98664d3 10543 complaint (_("template parameter missing "
3e43a32a 10544 "DW_AT_const_value"));
d7e74731 10545 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10546 continue;
10547 }
10548
10549 dwarf2_const_value_attr (attr, type, name,
10550 &cu->comp_unit_obstack, cu,
10551 &value, &bytes, &baton);
10552
10553 if (TYPE_NOSIGN (type))
10554 /* GDB prints characters as NUMBER 'CHAR'. If that's
10555 changed, this can use value_print instead. */
d7e74731 10556 c_printchar (value, type, &buf);
98bfdba5
PA
10557 else
10558 {
10559 struct value_print_options opts;
10560
10561 if (baton != NULL)
10562 v = dwarf2_evaluate_loc_desc (type, NULL,
10563 baton->data,
10564 baton->size,
10565 baton->per_cu);
10566 else if (bytes != NULL)
10567 {
10568 v = allocate_value (type);
10569 memcpy (value_contents_writeable (v), bytes,
10570 TYPE_LENGTH (type));
10571 }
10572 else
10573 v = value_from_longest (type, value);
10574
3e43a32a
MS
10575 /* Specify decimal so that we do not depend on
10576 the radix. */
98bfdba5
PA
10577 get_formatted_print_options (&opts, 'd');
10578 opts.raw = 1;
d7e74731 10579 value_print (v, &buf, &opts);
98bfdba5 10580 release_value (v);
98bfdba5
PA
10581 }
10582 }
10583
10584 die->building_fullname = 0;
10585
10586 if (!first)
10587 {
10588 /* Close the argument list, with a space if necessary
10589 (nested templates). */
d7e74731
PA
10590 if (!buf.empty () && buf.string ().back () == '>')
10591 buf.puts (" >");
98bfdba5 10592 else
d7e74731 10593 buf.puts (">");
98bfdba5
PA
10594 }
10595 }
10596
9c37b5ae 10597 /* For C++ methods, append formal parameter type
94af9270 10598 information, if PHYSNAME. */
6e70227d 10599
94af9270 10600 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10601 && cu->language == language_cplus)
94af9270
KS
10602 {
10603 struct type *type = read_type_die (die, cu);
10604
d7e74731 10605 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10606 &type_print_raw_options);
94af9270 10607
9c37b5ae 10608 if (cu->language == language_cplus)
94af9270 10609 {
60430eff
DJ
10610 /* Assume that an artificial first parameter is
10611 "this", but do not crash if it is not. RealView
10612 marks unnamed (and thus unused) parameters as
10613 artificial; there is no way to differentiate
10614 the two cases. */
94af9270
KS
10615 if (TYPE_NFIELDS (type) > 0
10616 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10617 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10618 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10619 0))))
d7e74731 10620 buf.puts (" const");
94af9270
KS
10621 }
10622 }
10623
d7e74731 10624 const std::string &intermediate_name = buf.string ();
94af9270
KS
10625
10626 if (cu->language == language_cplus)
34a68019 10627 canonical_name
322a8516 10628 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10629 &objfile->per_bfd->storage_obstack);
10630
10631 /* If we only computed INTERMEDIATE_NAME, or if
10632 INTERMEDIATE_NAME is already canonical, then we need to
10633 copy it to the appropriate obstack. */
322a8516 10634 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10635 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10636 intermediate_name);
34a68019
TT
10637 else
10638 name = canonical_name;
94af9270
KS
10639 }
10640 }
10641
10642 return name;
10643}
10644
0114d602
DJ
10645/* Return the fully qualified name of DIE, based on its DW_AT_name.
10646 If scope qualifiers are appropriate they will be added. The result
34a68019 10647 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10648 not have a name. NAME may either be from a previous call to
10649 dwarf2_name or NULL.
10650
9c37b5ae 10651 The output string will be canonicalized (if C++). */
0114d602
DJ
10652
10653static const char *
15d034d0 10654dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10655{
94af9270
KS
10656 return dwarf2_compute_name (name, die, cu, 0);
10657}
0114d602 10658
94af9270
KS
10659/* Construct a physname for the given DIE in CU. NAME may either be
10660 from a previous call to dwarf2_name or NULL. The result will be
10661 allocated on the objfile_objstack or NULL if the DIE does not have a
10662 name.
0114d602 10663
9c37b5ae 10664 The output string will be canonicalized (if C++). */
0114d602 10665
94af9270 10666static const char *
15d034d0 10667dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10668{
518817b3 10669 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10670 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10671 int need_copy = 1;
10672
10673 /* In this case dwarf2_compute_name is just a shortcut not building anything
10674 on its own. */
10675 if (!die_needs_namespace (die, cu))
10676 return dwarf2_compute_name (name, die, cu, 1);
10677
73b9be8b 10678 mangled = dw2_linkage_name (die, cu);
900e11f9 10679
e98c9e7c
TT
10680 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10681 See https://github.com/rust-lang/rust/issues/32925. */
10682 if (cu->language == language_rust && mangled != NULL
10683 && strchr (mangled, '{') != NULL)
10684 mangled = NULL;
10685
900e11f9
JK
10686 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10687 has computed. */
791afaa2 10688 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10689 if (mangled != NULL)
900e11f9 10690 {
900e11f9 10691
59cc4834
JB
10692 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10693 {
10694 /* Do nothing (do not demangle the symbol name). */
10695 }
10696 else if (cu->language == language_go)
a766d390 10697 {
5e2db402
TT
10698 /* This is a lie, but we already lie to the caller new_symbol.
10699 new_symbol assumes we return the mangled name.
a766d390 10700 This just undoes that lie until things are cleaned up. */
a766d390
DE
10701 }
10702 else
10703 {
0eb876f5
JB
10704 /* Use DMGL_RET_DROP for C++ template functions to suppress
10705 their return type. It is easier for GDB users to search
10706 for such functions as `name(params)' than `long name(params)'.
10707 In such case the minimal symbol names do not match the full
10708 symbol names but for template functions there is never a need
10709 to look up their definition from their declaration so
10710 the only disadvantage remains the minimal symbol variant
10711 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10712 demangled.reset (gdb_demangle (mangled,
10713 (DMGL_PARAMS | DMGL_ANSI
10714 | DMGL_RET_DROP)));
a766d390 10715 }
900e11f9 10716 if (demangled)
791afaa2 10717 canon = demangled.get ();
900e11f9
JK
10718 else
10719 {
10720 canon = mangled;
10721 need_copy = 0;
10722 }
10723 }
10724
10725 if (canon == NULL || check_physname)
10726 {
10727 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10728
10729 if (canon != NULL && strcmp (physname, canon) != 0)
10730 {
10731 /* It may not mean a bug in GDB. The compiler could also
10732 compute DW_AT_linkage_name incorrectly. But in such case
10733 GDB would need to be bug-to-bug compatible. */
10734
b98664d3 10735 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10736 "(from linkage <%s>) - DIE at %s [in module %s]"),
10737 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10738 objfile_name (objfile));
900e11f9
JK
10739
10740 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10741 is available here - over computed PHYSNAME. It is safer
10742 against both buggy GDB and buggy compilers. */
10743
10744 retval = canon;
10745 }
10746 else
10747 {
10748 retval = physname;
10749 need_copy = 0;
10750 }
10751 }
10752 else
10753 retval = canon;
10754
10755 if (need_copy)
021887d8 10756 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 10757
900e11f9 10758 return retval;
0114d602
DJ
10759}
10760
74921315
KS
10761/* Inspect DIE in CU for a namespace alias. If one exists, record
10762 a new symbol for it.
10763
10764 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10765
10766static int
10767read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10768{
10769 struct attribute *attr;
10770
10771 /* If the die does not have a name, this is not a namespace
10772 alias. */
10773 attr = dwarf2_attr (die, DW_AT_name, cu);
10774 if (attr != NULL)
10775 {
10776 int num;
10777 struct die_info *d = die;
10778 struct dwarf2_cu *imported_cu = cu;
10779
10780 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10781 keep inspecting DIEs until we hit the underlying import. */
10782#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10783 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10784 {
10785 attr = dwarf2_attr (d, DW_AT_import, cu);
10786 if (attr == NULL)
10787 break;
10788
10789 d = follow_die_ref (d, attr, &imported_cu);
10790 if (d->tag != DW_TAG_imported_declaration)
10791 break;
10792 }
10793
10794 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10795 {
b98664d3 10796 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10797 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10798 return 0;
10799 }
10800
10801 if (attr != NULL)
10802 {
10803 struct type *type;
9c541725 10804 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10805
9c541725 10806 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10807 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10808 {
10809 /* This declaration is a global namespace alias. Add
10810 a symbol for it whose type is the aliased namespace. */
10811 new_symbol (die, type, cu);
10812 return 1;
10813 }
10814 }
10815 }
10816
10817 return 0;
10818}
10819
22cee43f 10820/* Return the using directives repository (global or local?) to use in the
804d2729 10821 current context for CU.
22cee43f
PMR
10822
10823 For Ada, imported declarations can materialize renamings, which *may* be
10824 global. However it is impossible (for now?) in DWARF to distinguish
10825 "external" imported declarations and "static" ones. As all imported
10826 declarations seem to be static in all other languages, make them all CU-wide
10827 global only in Ada. */
10828
10829static struct using_direct **
804d2729 10830using_directives (struct dwarf2_cu *cu)
22cee43f 10831{
c24bdb02
KS
10832 if (cu->language == language_ada
10833 && cu->get_builder ()->outermost_context_p ())
10834 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10835 else
c24bdb02 10836 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10837}
10838
27aa8d6a
SW
10839/* Read the import statement specified by the given die and record it. */
10840
10841static void
10842read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10843{
518817b3 10844 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10845 struct attribute *import_attr;
32019081 10846 struct die_info *imported_die, *child_die;
de4affc9 10847 struct dwarf2_cu *imported_cu;
27aa8d6a 10848 const char *imported_name;
794684b6 10849 const char *imported_name_prefix;
13387711
SW
10850 const char *canonical_name;
10851 const char *import_alias;
10852 const char *imported_declaration = NULL;
794684b6 10853 const char *import_prefix;
eb1e02fd 10854 std::vector<const char *> excludes;
13387711 10855
27aa8d6a
SW
10856 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10857 if (import_attr == NULL)
10858 {
b98664d3 10859 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10860 dwarf_tag_name (die->tag));
10861 return;
10862 }
10863
de4affc9
CC
10864 imported_cu = cu;
10865 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10866 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10867 if (imported_name == NULL)
10868 {
10869 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10870
10871 The import in the following code:
10872 namespace A
10873 {
10874 typedef int B;
10875 }
10876
10877 int main ()
10878 {
10879 using A::B;
10880 B b;
10881 return b;
10882 }
10883
10884 ...
10885 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10886 <52> DW_AT_decl_file : 1
10887 <53> DW_AT_decl_line : 6
10888 <54> DW_AT_import : <0x75>
10889 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10890 <59> DW_AT_name : B
10891 <5b> DW_AT_decl_file : 1
10892 <5c> DW_AT_decl_line : 2
10893 <5d> DW_AT_type : <0x6e>
10894 ...
10895 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10896 <76> DW_AT_byte_size : 4
10897 <77> DW_AT_encoding : 5 (signed)
10898
10899 imports the wrong die ( 0x75 instead of 0x58 ).
10900 This case will be ignored until the gcc bug is fixed. */
10901 return;
10902 }
10903
82856980
SW
10904 /* Figure out the local name after import. */
10905 import_alias = dwarf2_name (die, cu);
27aa8d6a 10906
794684b6
SW
10907 /* Figure out where the statement is being imported to. */
10908 import_prefix = determine_prefix (die, cu);
10909
10910 /* Figure out what the scope of the imported die is and prepend it
10911 to the name of the imported die. */
de4affc9 10912 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10913
f55ee35c
JK
10914 if (imported_die->tag != DW_TAG_namespace
10915 && imported_die->tag != DW_TAG_module)
794684b6 10916 {
13387711
SW
10917 imported_declaration = imported_name;
10918 canonical_name = imported_name_prefix;
794684b6 10919 }
13387711 10920 else if (strlen (imported_name_prefix) > 0)
12aaed36 10921 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10922 imported_name_prefix,
10923 (cu->language == language_d ? "." : "::"),
10924 imported_name, (char *) NULL);
13387711
SW
10925 else
10926 canonical_name = imported_name;
794684b6 10927
32019081
JK
10928 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10929 for (child_die = die->child; child_die && child_die->tag;
10930 child_die = sibling_die (child_die))
10931 {
10932 /* DWARF-4: A Fortran use statement with a “rename list” may be
10933 represented by an imported module entry with an import attribute
10934 referring to the module and owned entries corresponding to those
10935 entities that are renamed as part of being imported. */
10936
10937 if (child_die->tag != DW_TAG_imported_declaration)
10938 {
b98664d3 10939 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10940 "- DIE at %s [in module %s]"),
10941 sect_offset_str (child_die->sect_off),
10942 objfile_name (objfile));
32019081
JK
10943 continue;
10944 }
10945
10946 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10947 if (import_attr == NULL)
10948 {
b98664d3 10949 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10950 dwarf_tag_name (child_die->tag));
10951 continue;
10952 }
10953
10954 imported_cu = cu;
10955 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10956 &imported_cu);
10957 imported_name = dwarf2_name (imported_die, imported_cu);
10958 if (imported_name == NULL)
10959 {
b98664d3 10960 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10961 "imported name - DIE at %s [in module %s]"),
10962 sect_offset_str (child_die->sect_off),
10963 objfile_name (objfile));
32019081
JK
10964 continue;
10965 }
10966
eb1e02fd 10967 excludes.push_back (imported_name);
32019081
JK
10968
10969 process_die (child_die, cu);
10970 }
10971
804d2729 10972 add_using_directive (using_directives (cu),
22cee43f
PMR
10973 import_prefix,
10974 canonical_name,
10975 import_alias,
10976 imported_declaration,
10977 excludes,
10978 0,
10979 &objfile->objfile_obstack);
27aa8d6a
SW
10980}
10981
5230b05a
WT
10982/* ICC<14 does not output the required DW_AT_declaration on incomplete
10983 types, but gives them a size of zero. Starting with version 14,
10984 ICC is compatible with GCC. */
10985
9068261f 10986static bool
5230b05a
WT
10987producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10988{
10989 if (!cu->checked_producer)
10990 check_producer (cu);
10991
10992 return cu->producer_is_icc_lt_14;
10993}
10994
eb77c9df
AB
10995/* ICC generates a DW_AT_type for C void functions. This was observed on
10996 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10997 which says that void functions should not have a DW_AT_type. */
10998
10999static bool
11000producer_is_icc (struct dwarf2_cu *cu)
11001{
11002 if (!cu->checked_producer)
11003 check_producer (cu);
11004
11005 return cu->producer_is_icc;
11006}
11007
1b80a9fa
JK
11008/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11009 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11010 this, it was first present in GCC release 4.3.0. */
11011
9068261f 11012static bool
1b80a9fa
JK
11013producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11014{
11015 if (!cu->checked_producer)
11016 check_producer (cu);
11017
11018 return cu->producer_is_gcc_lt_4_3;
11019}
11020
d721ba37
PA
11021static file_and_directory
11022find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11023{
d721ba37
PA
11024 file_and_directory res;
11025
9291a0cd
TT
11026 /* Find the filename. Do not use dwarf2_name here, since the filename
11027 is not a source language identifier. */
d721ba37
PA
11028 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11029 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11030
d721ba37
PA
11031 if (res.comp_dir == NULL
11032 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11033 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11034 {
d721ba37
PA
11035 res.comp_dir_storage = ldirname (res.name);
11036 if (!res.comp_dir_storage.empty ())
11037 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11038 }
d721ba37 11039 if (res.comp_dir != NULL)
9291a0cd
TT
11040 {
11041 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11042 directory, get rid of it. */
d721ba37 11043 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11044
d721ba37
PA
11045 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11046 res.comp_dir = cp + 1;
9291a0cd
TT
11047 }
11048
d721ba37
PA
11049 if (res.name == NULL)
11050 res.name = "<unknown>";
11051
11052 return res;
9291a0cd
TT
11053}
11054
f4dc4d17
DE
11055/* Handle DW_AT_stmt_list for a compilation unit.
11056 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11057 COMP_DIR is the compilation directory. LOWPC is passed to
11058 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11059
11060static void
11061handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11062 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11063{
518817b3
SM
11064 struct dwarf2_per_objfile *dwarf2_per_objfile
11065 = cu->per_cu->dwarf2_per_objfile;
527f3840 11066 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11067 struct attribute *attr;
527f3840
JK
11068 struct line_header line_header_local;
11069 hashval_t line_header_local_hash;
527f3840
JK
11070 void **slot;
11071 int decode_mapping;
2ab95328 11072
f4dc4d17
DE
11073 gdb_assert (! cu->per_cu->is_debug_types);
11074
2ab95328 11075 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11076 if (attr == NULL)
11077 return;
11078
9c541725 11079 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11080
11081 /* The line header hash table is only created if needed (it exists to
11082 prevent redundant reading of the line table for partial_units).
11083 If we're given a partial_unit, we'll need it. If we're given a
11084 compile_unit, then use the line header hash table if it's already
11085 created, but don't create one just yet. */
11086
11087 if (dwarf2_per_objfile->line_header_hash == NULL
11088 && die->tag == DW_TAG_partial_unit)
2ab95328 11089 {
527f3840
JK
11090 dwarf2_per_objfile->line_header_hash
11091 = htab_create_alloc_ex (127, line_header_hash_voidp,
11092 line_header_eq_voidp,
11093 free_line_header_voidp,
11094 &objfile->objfile_obstack,
11095 hashtab_obstack_allocate,
11096 dummy_obstack_deallocate);
11097 }
2ab95328 11098
9c541725 11099 line_header_local.sect_off = line_offset;
527f3840
JK
11100 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11101 line_header_local_hash = line_header_hash (&line_header_local);
11102 if (dwarf2_per_objfile->line_header_hash != NULL)
11103 {
11104 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11105 &line_header_local,
11106 line_header_local_hash, NO_INSERT);
11107
11108 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11109 is not present in *SLOT (since if there is something in *SLOT then
11110 it will be for a partial_unit). */
11111 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11112 {
527f3840 11113 gdb_assert (*slot != NULL);
9a3c8263 11114 cu->line_header = (struct line_header *) *slot;
527f3840 11115 return;
dee91e82 11116 }
2ab95328 11117 }
527f3840
JK
11118
11119 /* dwarf_decode_line_header does not yet provide sufficient information.
11120 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11121 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11122 if (lh == NULL)
527f3840 11123 return;
4c8aa72d
PA
11124
11125 cu->line_header = lh.release ();
11126 cu->line_header_die_owner = die;
527f3840
JK
11127
11128 if (dwarf2_per_objfile->line_header_hash == NULL)
11129 slot = NULL;
11130 else
11131 {
11132 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11133 &line_header_local,
11134 line_header_local_hash, INSERT);
11135 gdb_assert (slot != NULL);
11136 }
11137 if (slot != NULL && *slot == NULL)
11138 {
11139 /* This newly decoded line number information unit will be owned
11140 by line_header_hash hash table. */
11141 *slot = cu->line_header;
4c8aa72d 11142 cu->line_header_die_owner = NULL;
527f3840
JK
11143 }
11144 else
11145 {
11146 /* We cannot free any current entry in (*slot) as that struct line_header
11147 may be already used by multiple CUs. Create only temporary decoded
11148 line_header for this CU - it may happen at most once for each line
11149 number information unit. And if we're not using line_header_hash
11150 then this is what we want as well. */
11151 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11152 }
11153 decode_mapping = (die->tag != DW_TAG_partial_unit);
11154 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11155 decode_mapping);
fff8551c 11156
2ab95328
TT
11157}
11158
95554aad 11159/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11160
c906108c 11161static void
e7c27a73 11162read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11163{
518817b3
SM
11164 struct dwarf2_per_objfile *dwarf2_per_objfile
11165 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11166 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11167 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11168 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11169 CORE_ADDR highpc = ((CORE_ADDR) 0);
11170 struct attribute *attr;
c906108c 11171 struct die_info *child_die;
e142c38c 11172 CORE_ADDR baseaddr;
6e70227d 11173
380618d6 11174 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11175 baseaddr = objfile->text_section_offset ();
c906108c 11176
fae299cd 11177 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11178
11179 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11180 from finish_block. */
2acceee2 11181 if (lowpc == ((CORE_ADDR) -1))
c906108c 11182 lowpc = highpc;
3e29f34a 11183 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11184
d721ba37 11185 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11186
f4b8a18d
KW
11187 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11188 standardised yet. As a workaround for the language detection we fall
11189 back to the DW_AT_producer string. */
11190 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11191 cu->language = language_opencl;
11192
3019eac3
DE
11193 /* Similar hack for Go. */
11194 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11195 set_cu_language (DW_LANG_Go, cu);
11196
c24bdb02 11197 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11198
11199 /* Decode line number information if present. We do this before
11200 processing child DIEs, so that the line header table is available
11201 for DW_AT_decl_file. */
d721ba37 11202 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11203
11204 /* Process all dies in compilation unit. */
11205 if (die->child != NULL)
11206 {
11207 child_die = die->child;
11208 while (child_die && child_die->tag)
11209 {
11210 process_die (child_die, cu);
11211 child_die = sibling_die (child_die);
11212 }
11213 }
11214
11215 /* Decode macro information, if present. Dwarf 2 macro information
11216 refers to information in the line number info statement program
11217 header, so we can only read it if we've read the header
11218 successfully. */
0af92d60
JK
11219 attr = dwarf2_attr (die, DW_AT_macros, cu);
11220 if (attr == NULL)
11221 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11222 if (attr && cu->line_header)
11223 {
11224 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11225 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11226
43f3e411 11227 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11228 }
11229 else
11230 {
11231 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11232 if (attr && cu->line_header)
11233 {
11234 unsigned int macro_offset = DW_UNSND (attr);
11235
43f3e411 11236 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11237 }
11238 }
3019eac3
DE
11239}
11240
c24bdb02
KS
11241void
11242dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11243{
f4dc4d17
DE
11244 struct type_unit_group *tu_group;
11245 int first_time;
3019eac3 11246 struct attribute *attr;
9c541725 11247 unsigned int i;
0186c6a7 11248 struct signatured_type *sig_type;
3019eac3 11249
f4dc4d17 11250 gdb_assert (per_cu->is_debug_types);
0186c6a7 11251 sig_type = (struct signatured_type *) per_cu;
3019eac3 11252
c24bdb02 11253 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11254
f4dc4d17 11255 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11256 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11257 if (sig_type->type_unit_group == NULL)
c24bdb02 11258 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11259 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11260
11261 /* If we've already processed this stmt_list there's no real need to
11262 do it again, we could fake it and just recreate the part we need
11263 (file name,index -> symtab mapping). If data shows this optimization
11264 is useful we can do it then. */
43f3e411 11265 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11266
11267 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11268 debug info. */
fff8551c 11269 line_header_up lh;
f4dc4d17 11270 if (attr != NULL)
3019eac3 11271 {
9c541725 11272 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11273 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11274 }
11275 if (lh == NULL)
11276 {
11277 if (first_time)
c24bdb02 11278 start_symtab ("", NULL, 0);
f4dc4d17
DE
11279 else
11280 {
11281 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11282 gdb_assert (m_builder == nullptr);
804d2729 11283 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11284 m_builder.reset (new struct buildsym_compunit
11285 (COMPUNIT_OBJFILE (cust), "",
11286 COMPUNIT_DIRNAME (cust),
11287 compunit_language (cust),
11288 0, cust));
f4dc4d17 11289 }
f4dc4d17 11290 return;
3019eac3
DE
11291 }
11292
c24bdb02
KS
11293 line_header = lh.release ();
11294 line_header_die_owner = die;
3019eac3 11295
f4dc4d17
DE
11296 if (first_time)
11297 {
c24bdb02 11298 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11299
1fd60fc0
DE
11300 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11301 still initializing it, and our caller (a few levels up)
11302 process_full_type_unit still needs to know if this is the first
11303 time. */
11304
7ba99d21 11305 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11306 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11307 line_header->file_names_size ());
3019eac3 11308
7ba99d21
AT
11309 auto &file_names = line_header->file_names ();
11310 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11311 {
7ba99d21 11312 file_entry &fe = file_names[i];
c24bdb02
KS
11313 dwarf2_start_subfile (this, fe.name,
11314 fe.include_dir (line_header));
11315 buildsym_compunit *b = get_builder ();
11316 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11317 {
4c8aa72d
PA
11318 /* NOTE: start_subfile will recognize when it's been
11319 passed a file it has already seen. So we can't
11320 assume there's a simple mapping from
11321 cu->line_header->file_names to subfiles, plus
11322 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11323 b->get_current_subfile ()->symtab
11324 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11325 }
11326
c24bdb02 11327 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11328 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11329 }
11330 }
11331 else
3019eac3 11332 {
c24bdb02 11333 gdb_assert (m_builder == nullptr);
804d2729 11334 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11335 m_builder.reset (new struct buildsym_compunit
11336 (COMPUNIT_OBJFILE (cust), "",
11337 COMPUNIT_DIRNAME (cust),
11338 compunit_language (cust),
11339 0, cust));
f4dc4d17 11340
7ba99d21
AT
11341 auto &file_names = line_header->file_names ();
11342 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11343 {
7ba99d21 11344 file_entry &fe = file_names[i];
4c8aa72d 11345 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11346 }
3019eac3
DE
11347 }
11348
f4dc4d17
DE
11349 /* The main symtab is allocated last. Type units don't have DW_AT_name
11350 so they don't have a "real" (so to speak) symtab anyway.
11351 There is later code that will assign the main symtab to all symbols
11352 that don't have one. We need to handle the case of a symbol with a
11353 missing symtab (DW_AT_decl_file) anyway. */
11354}
3019eac3 11355
f4dc4d17
DE
11356/* Process DW_TAG_type_unit.
11357 For TUs we want to skip the first top level sibling if it's not the
11358 actual type being defined by this TU. In this case the first top
11359 level sibling is there to provide context only. */
3019eac3 11360
f4dc4d17
DE
11361static void
11362read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11363{
11364 struct die_info *child_die;
3019eac3 11365
f4dc4d17
DE
11366 prepare_one_comp_unit (cu, die, language_minimal);
11367
11368 /* Initialize (or reinitialize) the machinery for building symtabs.
11369 We do this before processing child DIEs, so that the line header table
11370 is available for DW_AT_decl_file. */
c24bdb02 11371 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11372
11373 if (die->child != NULL)
11374 {
11375 child_die = die->child;
11376 while (child_die && child_die->tag)
11377 {
11378 process_die (child_die, cu);
11379 child_die = sibling_die (child_die);
11380 }
11381 }
3019eac3
DE
11382}
11383\f
80626a55
DE
11384/* DWO/DWP files.
11385
11386 http://gcc.gnu.org/wiki/DebugFission
11387 http://gcc.gnu.org/wiki/DebugFissionDWP
11388
11389 To simplify handling of both DWO files ("object" files with the DWARF info)
11390 and DWP files (a file with the DWOs packaged up into one file), we treat
11391 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11392
11393static hashval_t
11394hash_dwo_file (const void *item)
11395{
9a3c8263 11396 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11397 hashval_t hash;
3019eac3 11398
a2ce51a0
DE
11399 hash = htab_hash_string (dwo_file->dwo_name);
11400 if (dwo_file->comp_dir != NULL)
11401 hash += htab_hash_string (dwo_file->comp_dir);
11402 return hash;
3019eac3
DE
11403}
11404
11405static int
11406eq_dwo_file (const void *item_lhs, const void *item_rhs)
11407{
9a3c8263
SM
11408 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11409 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11410
a2ce51a0
DE
11411 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11412 return 0;
11413 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11414 return lhs->comp_dir == rhs->comp_dir;
11415 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11416}
11417
11418/* Allocate a hash table for DWO files. */
11419
51ac9db5 11420static htab_up
ed2dc618 11421allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11422{
51ac9db5
SM
11423 auto delete_dwo_file = [] (void *item)
11424 {
11425 struct dwo_file *dwo_file = (struct dwo_file *) item;
11426
11427 delete dwo_file;
11428 };
11429
11430 return htab_up (htab_create_alloc_ex (41,
11431 hash_dwo_file,
11432 eq_dwo_file,
11433 delete_dwo_file,
11434 &objfile->objfile_obstack,
11435 hashtab_obstack_allocate,
11436 dummy_obstack_deallocate));
3019eac3
DE
11437}
11438
80626a55
DE
11439/* Lookup DWO file DWO_NAME. */
11440
11441static void **
ed2dc618
SM
11442lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11443 const char *dwo_name,
11444 const char *comp_dir)
80626a55
DE
11445{
11446 struct dwo_file find_entry;
11447 void **slot;
11448
11449 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11450 dwarf2_per_objfile->dwo_files
11451 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11452
0ac5b59e
DE
11453 find_entry.dwo_name = dwo_name;
11454 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11455 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11456 INSERT);
80626a55
DE
11457
11458 return slot;
11459}
11460
3019eac3
DE
11461static hashval_t
11462hash_dwo_unit (const void *item)
11463{
9a3c8263 11464 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11465
11466 /* This drops the top 32 bits of the id, but is ok for a hash. */
11467 return dwo_unit->signature;
11468}
11469
11470static int
11471eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11472{
9a3c8263
SM
11473 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11474 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11475
11476 /* The signature is assumed to be unique within the DWO file.
11477 So while object file CU dwo_id's always have the value zero,
11478 that's OK, assuming each object file DWO file has only one CU,
11479 and that's the rule for now. */
11480 return lhs->signature == rhs->signature;
11481}
11482
11483/* Allocate a hash table for DWO CUs,TUs.
11484 There is one of these tables for each of CUs,TUs for each DWO file. */
11485
11486static htab_t
11487allocate_dwo_unit_table (struct objfile *objfile)
11488{
11489 /* Start out with a pretty small number.
11490 Generally DWO files contain only one CU and maybe some TUs. */
11491 return htab_create_alloc_ex (3,
11492 hash_dwo_unit,
11493 eq_dwo_unit,
11494 NULL,
11495 &objfile->objfile_obstack,
11496 hashtab_obstack_allocate,
11497 dummy_obstack_deallocate);
11498}
11499
19c3d4c9 11500/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11501
11502static void
19c3d4c9
DE
11503create_dwo_cu_reader (const struct die_reader_specs *reader,
11504 const gdb_byte *info_ptr,
11505 struct die_info *comp_unit_die,
c0ab21c2
TT
11506 struct dwo_file *dwo_file,
11507 struct dwo_unit *dwo_unit)
3019eac3
DE
11508{
11509 struct dwarf2_cu *cu = reader->cu;
9c541725 11510 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11511 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11512
a084a2a6
AT
11513 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11514 if (!signature.has_value ())
3019eac3 11515 {
b98664d3 11516 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11517 " its dwo_id [in module %s]"),
9d8780f0 11518 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11519 return;
11520 }
11521
3019eac3 11522 dwo_unit->dwo_file = dwo_file;
a084a2a6 11523 dwo_unit->signature = *signature;
8a0459fd 11524 dwo_unit->section = section;
9c541725 11525 dwo_unit->sect_off = sect_off;
3019eac3
DE
11526 dwo_unit->length = cu->per_cu->length;
11527
b4f54984 11528 if (dwarf_read_debug)
9d8780f0
SM
11529 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11530 sect_offset_str (sect_off),
9c541725 11531 hex_string (dwo_unit->signature));
3019eac3
DE
11532}
11533
33c5cd75 11534/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11535 Note: This function processes DWO files only, not DWP files. */
3019eac3 11536
33c5cd75 11537static void
ed2dc618 11538create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
11539 dwarf2_cu *cu, struct dwo_file &dwo_file,
11540 dwarf2_section_info &section, htab_t &cus_htab)
3019eac3
DE
11541{
11542 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11543 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11544
96b79293 11545 section.read (objfile);
33c5cd75 11546 info_ptr = section.buffer;
3019eac3
DE
11547
11548 if (info_ptr == NULL)
33c5cd75 11549 return;
3019eac3 11550
b4f54984 11551 if (dwarf_read_debug)
19c3d4c9
DE
11552 {
11553 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11554 section.get_name (),
11555 section.get_file_name ());
19c3d4c9 11556 }
3019eac3 11557
33c5cd75 11558 end_ptr = info_ptr + section.size;
3019eac3
DE
11559 while (info_ptr < end_ptr)
11560 {
11561 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11562 struct dwo_unit read_unit {};
33c5cd75
DB
11563 struct dwo_unit *dwo_unit;
11564 void **slot;
11565 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11566
11567 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11568 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11569 per_cu.is_debug_types = 0;
33c5cd75
DB
11570 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11571 per_cu.section = &section;
11572
c0ab21c2
TT
11573 cutu_reader reader (&per_cu, cu, &dwo_file);
11574 if (!reader.dummy_p)
11575 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11576 &dwo_file, &read_unit);
33c5cd75
DB
11577 info_ptr += per_cu.length;
11578
11579 // If the unit could not be parsed, skip it.
c0ab21c2 11580 if (read_unit.dwo_file == NULL)
33c5cd75 11581 continue;
3019eac3 11582
33c5cd75
DB
11583 if (cus_htab == NULL)
11584 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11585
33c5cd75 11586 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11587 *dwo_unit = read_unit;
33c5cd75
DB
11588 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11589 gdb_assert (slot != NULL);
11590 if (*slot != NULL)
19c3d4c9 11591 {
33c5cd75
DB
11592 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11593 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11594
b98664d3 11595 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11596 " the entry at offset %s, signature %s"),
11597 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11598 hex_string (dwo_unit->signature));
19c3d4c9 11599 }
33c5cd75 11600 *slot = (void *)dwo_unit;
3019eac3 11601 }
3019eac3
DE
11602}
11603
80626a55
DE
11604/* DWP file .debug_{cu,tu}_index section format:
11605 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11606
d2415c6c
DE
11607 DWP Version 1:
11608
80626a55
DE
11609 Both index sections have the same format, and serve to map a 64-bit
11610 signature to a set of section numbers. Each section begins with a header,
11611 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11612 indexes, and a pool of 32-bit section numbers. The index sections will be
11613 aligned at 8-byte boundaries in the file.
11614
d2415c6c
DE
11615 The index section header consists of:
11616
11617 V, 32 bit version number
11618 -, 32 bits unused
11619 N, 32 bit number of compilation units or type units in the index
11620 M, 32 bit number of slots in the hash table
80626a55 11621
d2415c6c 11622 Numbers are recorded using the byte order of the application binary.
80626a55 11623
d2415c6c
DE
11624 The hash table begins at offset 16 in the section, and consists of an array
11625 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11626 order of the application binary). Unused slots in the hash table are 0.
11627 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11628
d2415c6c
DE
11629 The parallel table begins immediately after the hash table
11630 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11631 array of 32-bit indexes (using the byte order of the application binary),
11632 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11633 table contains a 32-bit index into the pool of section numbers. For unused
11634 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11635
73869dc2
DE
11636 The pool of section numbers begins immediately following the hash table
11637 (at offset 16 + 12 * M from the beginning of the section). The pool of
11638 section numbers consists of an array of 32-bit words (using the byte order
11639 of the application binary). Each item in the array is indexed starting
11640 from 0. The hash table entry provides the index of the first section
11641 number in the set. Additional section numbers in the set follow, and the
11642 set is terminated by a 0 entry (section number 0 is not used in ELF).
11643
11644 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11645 section must be the first entry in the set, and the .debug_abbrev.dwo must
11646 be the second entry. Other members of the set may follow in any order.
11647
11648 ---
11649
11650 DWP Version 2:
11651
11652 DWP Version 2 combines all the .debug_info, etc. sections into one,
11653 and the entries in the index tables are now offsets into these sections.
11654 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11655 section.
11656
11657 Index Section Contents:
11658 Header
11659 Hash Table of Signatures dwp_hash_table.hash_table
11660 Parallel Table of Indices dwp_hash_table.unit_table
11661 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11662 Table of Section Sizes dwp_hash_table.v2.sizes
11663
11664 The index section header consists of:
11665
11666 V, 32 bit version number
11667 L, 32 bit number of columns in the table of section offsets
11668 N, 32 bit number of compilation units or type units in the index
11669 M, 32 bit number of slots in the hash table
11670
11671 Numbers are recorded using the byte order of the application binary.
11672
11673 The hash table has the same format as version 1.
11674 The parallel table of indices has the same format as version 1,
11675 except that the entries are origin-1 indices into the table of sections
11676 offsets and the table of section sizes.
11677
11678 The table of offsets begins immediately following the parallel table
11679 (at offset 16 + 12 * M from the beginning of the section). The table is
11680 a two-dimensional array of 32-bit words (using the byte order of the
11681 application binary), with L columns and N+1 rows, in row-major order.
11682 Each row in the array is indexed starting from 0. The first row provides
11683 a key to the remaining rows: each column in this row provides an identifier
11684 for a debug section, and the offsets in the same column of subsequent rows
11685 refer to that section. The section identifiers are:
11686
11687 DW_SECT_INFO 1 .debug_info.dwo
11688 DW_SECT_TYPES 2 .debug_types.dwo
11689 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11690 DW_SECT_LINE 4 .debug_line.dwo
11691 DW_SECT_LOC 5 .debug_loc.dwo
11692 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11693 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11694 DW_SECT_MACRO 8 .debug_macro.dwo
11695
11696 The offsets provided by the CU and TU index sections are the base offsets
11697 for the contributions made by each CU or TU to the corresponding section
11698 in the package file. Each CU and TU header contains an abbrev_offset
11699 field, used to find the abbreviations table for that CU or TU within the
11700 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11701 be interpreted as relative to the base offset given in the index section.
11702 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11703 should be interpreted as relative to the base offset for .debug_line.dwo,
11704 and offsets into other debug sections obtained from DWARF attributes should
11705 also be interpreted as relative to the corresponding base offset.
11706
11707 The table of sizes begins immediately following the table of offsets.
11708 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11709 with L columns and N rows, in row-major order. Each row in the array is
11710 indexed starting from 1 (row 0 is shared by the two tables).
11711
11712 ---
11713
11714 Hash table lookup is handled the same in version 1 and 2:
11715
11716 We assume that N and M will not exceed 2^32 - 1.
11717 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11718
d2415c6c
DE
11719 Given a 64-bit compilation unit signature or a type signature S, an entry
11720 in the hash table is located as follows:
80626a55 11721
d2415c6c
DE
11722 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11723 the low-order k bits all set to 1.
80626a55 11724
d2415c6c 11725 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11726
d2415c6c
DE
11727 3) If the hash table entry at index H matches the signature, use that
11728 entry. If the hash table entry at index H is unused (all zeroes),
11729 terminate the search: the signature is not present in the table.
80626a55 11730
d2415c6c 11731 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11732
d2415c6c 11733 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11734 to stop at an unused slot or find the match. */
80626a55
DE
11735
11736/* Create a hash table to map DWO IDs to their CU/TU entry in
11737 .debug_{info,types}.dwo in DWP_FILE.
11738 Returns NULL if there isn't one.
11739 Note: This function processes DWP files only, not DWO files. */
11740
11741static struct dwp_hash_table *
ed2dc618
SM
11742create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11743 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11744{
11745 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11746 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11747 const gdb_byte *index_ptr, *index_end;
80626a55 11748 struct dwarf2_section_info *index;
73869dc2 11749 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11750 struct dwp_hash_table *htab;
11751
11752 if (is_debug_types)
11753 index = &dwp_file->sections.tu_index;
11754 else
11755 index = &dwp_file->sections.cu_index;
11756
96b79293 11757 if (index->empty ())
80626a55 11758 return NULL;
96b79293 11759 index->read (objfile);
80626a55
DE
11760
11761 index_ptr = index->buffer;
11762 index_end = index_ptr + index->size;
11763
11764 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11765 index_ptr += 4;
11766 if (version == 2)
11767 nr_columns = read_4_bytes (dbfd, index_ptr);
11768 else
11769 nr_columns = 0;
11770 index_ptr += 4;
80626a55
DE
11771 nr_units = read_4_bytes (dbfd, index_ptr);
11772 index_ptr += 4;
11773 nr_slots = read_4_bytes (dbfd, index_ptr);
11774 index_ptr += 4;
11775
73869dc2 11776 if (version != 1 && version != 2)
80626a55 11777 {
21aa081e 11778 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11779 " [in module %s]"),
21aa081e 11780 pulongest (version), dwp_file->name);
80626a55
DE
11781 }
11782 if (nr_slots != (nr_slots & -nr_slots))
11783 {
21aa081e 11784 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11785 " is not power of 2 [in module %s]"),
21aa081e 11786 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11787 }
11788
11789 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11790 htab->version = version;
11791 htab->nr_columns = nr_columns;
80626a55
DE
11792 htab->nr_units = nr_units;
11793 htab->nr_slots = nr_slots;
11794 htab->hash_table = index_ptr;
11795 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11796
11797 /* Exit early if the table is empty. */
11798 if (nr_slots == 0 || nr_units == 0
11799 || (version == 2 && nr_columns == 0))
11800 {
11801 /* All must be zero. */
11802 if (nr_slots != 0 || nr_units != 0
11803 || (version == 2 && nr_columns != 0))
11804 {
b98664d3 11805 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11806 " all zero [in modules %s]"),
11807 dwp_file->name);
11808 }
11809 return htab;
11810 }
11811
11812 if (version == 1)
11813 {
11814 htab->section_pool.v1.indices =
11815 htab->unit_table + sizeof (uint32_t) * nr_slots;
11816 /* It's harder to decide whether the section is too small in v1.
11817 V1 is deprecated anyway so we punt. */
11818 }
11819 else
11820 {
11821 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11822 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11823 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11824 /* Reverse map for error checking. */
11825 int ids_seen[DW_SECT_MAX + 1];
11826 int i;
11827
11828 if (nr_columns < 2)
11829 {
11830 error (_("Dwarf Error: bad DWP hash table, too few columns"
11831 " in section table [in module %s]"),
11832 dwp_file->name);
11833 }
11834 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11835 {
11836 error (_("Dwarf Error: bad DWP hash table, too many columns"
11837 " in section table [in module %s]"),
11838 dwp_file->name);
11839 }
04fd5eed
GB
11840 memset (ids, 255, sizeof_ids);
11841 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11842 for (i = 0; i < nr_columns; ++i)
11843 {
11844 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11845
11846 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11847 {
11848 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11849 " in section table [in module %s]"),
11850 id, dwp_file->name);
11851 }
11852 if (ids_seen[id] != -1)
11853 {
11854 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11855 " id %d in section table [in module %s]"),
11856 id, dwp_file->name);
11857 }
11858 ids_seen[id] = i;
11859 ids[i] = id;
11860 }
11861 /* Must have exactly one info or types section. */
11862 if (((ids_seen[DW_SECT_INFO] != -1)
11863 + (ids_seen[DW_SECT_TYPES] != -1))
11864 != 1)
11865 {
11866 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11867 " DWO info/types section [in module %s]"),
11868 dwp_file->name);
11869 }
11870 /* Must have an abbrev section. */
11871 if (ids_seen[DW_SECT_ABBREV] == -1)
11872 {
11873 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11874 " section [in module %s]"),
11875 dwp_file->name);
11876 }
11877 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11878 htab->section_pool.v2.sizes =
11879 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11880 * nr_units * nr_columns);
11881 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11882 * nr_units * nr_columns))
11883 > index_end)
11884 {
11885 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11886 " [in module %s]"),
11887 dwp_file->name);
11888 }
11889 }
80626a55
DE
11890
11891 return htab;
11892}
11893
11894/* Update SECTIONS with the data from SECTP.
11895
11896 This function is like the other "locate" section routines that are
11897 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11898 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11899
11900 The result is non-zero for success, or zero if an error was found. */
11901
11902static int
73869dc2
DE
11903locate_v1_virtual_dwo_sections (asection *sectp,
11904 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11905{
11906 const struct dwop_section_names *names = &dwop_section_names;
11907
11908 if (section_is_p (sectp->name, &names->abbrev_dwo))
11909 {
11910 /* There can be only one. */
049412e3 11911 if (sections->abbrev.s.section != NULL)
80626a55 11912 return 0;
049412e3 11913 sections->abbrev.s.section = sectp;
fd361982 11914 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11915 }
11916 else if (section_is_p (sectp->name, &names->info_dwo)
11917 || section_is_p (sectp->name, &names->types_dwo))
11918 {
11919 /* There can be only one. */
049412e3 11920 if (sections->info_or_types.s.section != NULL)
80626a55 11921 return 0;
049412e3 11922 sections->info_or_types.s.section = sectp;
fd361982 11923 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11924 }
11925 else if (section_is_p (sectp->name, &names->line_dwo))
11926 {
11927 /* There can be only one. */
049412e3 11928 if (sections->line.s.section != NULL)
80626a55 11929 return 0;
049412e3 11930 sections->line.s.section = sectp;
fd361982 11931 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11932 }
11933 else if (section_is_p (sectp->name, &names->loc_dwo))
11934 {
11935 /* There can be only one. */
049412e3 11936 if (sections->loc.s.section != NULL)
80626a55 11937 return 0;
049412e3 11938 sections->loc.s.section = sectp;
fd361982 11939 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11940 }
11941 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11942 {
11943 /* There can be only one. */
049412e3 11944 if (sections->macinfo.s.section != NULL)
80626a55 11945 return 0;
049412e3 11946 sections->macinfo.s.section = sectp;
fd361982 11947 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11948 }
11949 else if (section_is_p (sectp->name, &names->macro_dwo))
11950 {
11951 /* There can be only one. */
049412e3 11952 if (sections->macro.s.section != NULL)
80626a55 11953 return 0;
049412e3 11954 sections->macro.s.section = sectp;
fd361982 11955 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11956 }
11957 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11958 {
11959 /* There can be only one. */
049412e3 11960 if (sections->str_offsets.s.section != NULL)
80626a55 11961 return 0;
049412e3 11962 sections->str_offsets.s.section = sectp;
fd361982 11963 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11964 }
11965 else
11966 {
11967 /* No other kind of section is valid. */
11968 return 0;
11969 }
11970
11971 return 1;
11972}
11973
73869dc2
DE
11974/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11975 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11976 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11977 This is for DWP version 1 files. */
80626a55
DE
11978
11979static struct dwo_unit *
ed2dc618
SM
11980create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11981 struct dwp_file *dwp_file,
73869dc2
DE
11982 uint32_t unit_index,
11983 const char *comp_dir,
11984 ULONGEST signature, int is_debug_types)
80626a55
DE
11985{
11986 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11987 const struct dwp_hash_table *dwp_htab =
11988 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11989 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11990 const char *kind = is_debug_types ? "TU" : "CU";
11991 struct dwo_file *dwo_file;
11992 struct dwo_unit *dwo_unit;
73869dc2 11993 struct virtual_v1_dwo_sections sections;
80626a55 11994 void **dwo_file_slot;
80626a55
DE
11995 int i;
11996
73869dc2
DE
11997 gdb_assert (dwp_file->version == 1);
11998
b4f54984 11999 if (dwarf_read_debug)
80626a55 12000 {
73869dc2 12001 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12002 kind,
73869dc2 12003 pulongest (unit_index), hex_string (signature),
80626a55
DE
12004 dwp_file->name);
12005 }
12006
19ac8c2e 12007 /* Fetch the sections of this DWO unit.
80626a55
DE
12008 Put a limit on the number of sections we look for so that bad data
12009 doesn't cause us to loop forever. */
12010
73869dc2 12011#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12012 (1 /* .debug_info or .debug_types */ \
12013 + 1 /* .debug_abbrev */ \
12014 + 1 /* .debug_line */ \
12015 + 1 /* .debug_loc */ \
12016 + 1 /* .debug_str_offsets */ \
19ac8c2e 12017 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12018 + 1 /* trailing zero */)
12019
12020 memset (&sections, 0, sizeof (sections));
80626a55 12021
73869dc2 12022 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12023 {
12024 asection *sectp;
12025 uint32_t section_nr =
12026 read_4_bytes (dbfd,
73869dc2
DE
12027 dwp_htab->section_pool.v1.indices
12028 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12029
12030 if (section_nr == 0)
12031 break;
12032 if (section_nr >= dwp_file->num_sections)
12033 {
12034 error (_("Dwarf Error: bad DWP hash table, section number too large"
12035 " [in module %s]"),
12036 dwp_file->name);
12037 }
12038
12039 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12040 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12041 {
12042 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12043 " [in module %s]"),
12044 dwp_file->name);
12045 }
12046 }
12047
12048 if (i < 2
96b79293
TT
12049 || sections.info_or_types.empty ()
12050 || sections.abbrev.empty ())
80626a55
DE
12051 {
12052 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12053 " [in module %s]"),
12054 dwp_file->name);
12055 }
73869dc2 12056 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12057 {
12058 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12059 " [in module %s]"),
12060 dwp_file->name);
12061 }
12062
12063 /* It's easier for the rest of the code if we fake a struct dwo_file and
12064 have dwo_unit "live" in that. At least for now.
12065
12066 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12067 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12068 file, we can combine them back into a virtual DWO file to save space
12069 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12070 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12071
791afaa2
TT
12072 std::string virtual_dwo_name =
12073 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12074 sections.abbrev.get_id (),
12075 sections.line.get_id (),
12076 sections.loc.get_id (),
12077 sections.str_offsets.get_id ());
80626a55 12078 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12079 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12080 virtual_dwo_name.c_str (),
12081 comp_dir);
80626a55
DE
12082 /* Create one if necessary. */
12083 if (*dwo_file_slot == NULL)
12084 {
b4f54984 12085 if (dwarf_read_debug)
80626a55
DE
12086 {
12087 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12088 virtual_dwo_name.c_str ());
80626a55 12089 }
51ac9db5 12090 dwo_file = new struct dwo_file;
efba19b0
TT
12091 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12092 virtual_dwo_name);
0ac5b59e 12093 dwo_file->comp_dir = comp_dir;
80626a55
DE
12094 dwo_file->sections.abbrev = sections.abbrev;
12095 dwo_file->sections.line = sections.line;
12096 dwo_file->sections.loc = sections.loc;
12097 dwo_file->sections.macinfo = sections.macinfo;
12098 dwo_file->sections.macro = sections.macro;
12099 dwo_file->sections.str_offsets = sections.str_offsets;
12100 /* The "str" section is global to the entire DWP file. */
12101 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12102 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12103 there's no need to record it in dwo_file.
12104 Also, we can't simply record type sections in dwo_file because
12105 we record a pointer into the vector in dwo_unit. As we collect more
12106 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12107 for it, invalidating all copies of pointers into the previous
12108 contents. */
80626a55
DE
12109 *dwo_file_slot = dwo_file;
12110 }
12111 else
12112 {
b4f54984 12113 if (dwarf_read_debug)
80626a55
DE
12114 {
12115 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12116 virtual_dwo_name.c_str ());
80626a55 12117 }
9a3c8263 12118 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12119 }
80626a55
DE
12120
12121 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12122 dwo_unit->dwo_file = dwo_file;
12123 dwo_unit->signature = signature;
8d749320
SM
12124 dwo_unit->section =
12125 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12126 *dwo_unit->section = sections.info_or_types;
57d63ce2 12127 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12128
12129 return dwo_unit;
12130}
12131
73869dc2
DE
12132/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12133 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12134 piece within that section used by a TU/CU, return a virtual section
12135 of just that piece. */
12136
12137static struct dwarf2_section_info
ed2dc618
SM
12138create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12139 struct dwarf2_section_info *section,
73869dc2
DE
12140 bfd_size_type offset, bfd_size_type size)
12141{
12142 struct dwarf2_section_info result;
12143 asection *sectp;
12144
12145 gdb_assert (section != NULL);
12146 gdb_assert (!section->is_virtual);
12147
12148 memset (&result, 0, sizeof (result));
12149 result.s.containing_section = section;
dc4ccb6f 12150 result.is_virtual = true;
73869dc2
DE
12151
12152 if (size == 0)
12153 return result;
12154
96b79293 12155 sectp = section->get_bfd_section ();
73869dc2
DE
12156
12157 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12158 bounds of the real section. This is a pretty-rare event, so just
12159 flag an error (easier) instead of a warning and trying to cope. */
12160 if (sectp == NULL
fd361982 12161 || offset + size > bfd_section_size (sectp))
73869dc2 12162 {
73869dc2
DE
12163 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12164 " in section %s [in module %s]"),
fd361982 12165 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12166 objfile_name (dwarf2_per_objfile->objfile));
12167 }
12168
12169 result.virtual_offset = offset;
12170 result.size = size;
12171 return result;
12172}
12173
12174/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12175 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12176 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12177 This is for DWP version 2 files. */
12178
12179static struct dwo_unit *
ed2dc618
SM
12180create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12181 struct dwp_file *dwp_file,
73869dc2
DE
12182 uint32_t unit_index,
12183 const char *comp_dir,
12184 ULONGEST signature, int is_debug_types)
12185{
12186 struct objfile *objfile = dwarf2_per_objfile->objfile;
12187 const struct dwp_hash_table *dwp_htab =
12188 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12189 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12190 const char *kind = is_debug_types ? "TU" : "CU";
12191 struct dwo_file *dwo_file;
12192 struct dwo_unit *dwo_unit;
12193 struct virtual_v2_dwo_sections sections;
12194 void **dwo_file_slot;
73869dc2
DE
12195 int i;
12196
12197 gdb_assert (dwp_file->version == 2);
12198
b4f54984 12199 if (dwarf_read_debug)
73869dc2
DE
12200 {
12201 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12202 kind,
12203 pulongest (unit_index), hex_string (signature),
12204 dwp_file->name);
12205 }
12206
12207 /* Fetch the section offsets of this DWO unit. */
12208
12209 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12210
12211 for (i = 0; i < dwp_htab->nr_columns; ++i)
12212 {
12213 uint32_t offset = read_4_bytes (dbfd,
12214 dwp_htab->section_pool.v2.offsets
12215 + (((unit_index - 1) * dwp_htab->nr_columns
12216 + i)
12217 * sizeof (uint32_t)));
12218 uint32_t size = read_4_bytes (dbfd,
12219 dwp_htab->section_pool.v2.sizes
12220 + (((unit_index - 1) * dwp_htab->nr_columns
12221 + i)
12222 * sizeof (uint32_t)));
12223
12224 switch (dwp_htab->section_pool.v2.section_ids[i])
12225 {
12226 case DW_SECT_INFO:
12227 case DW_SECT_TYPES:
12228 sections.info_or_types_offset = offset;
12229 sections.info_or_types_size = size;
12230 break;
12231 case DW_SECT_ABBREV:
12232 sections.abbrev_offset = offset;
12233 sections.abbrev_size = size;
12234 break;
12235 case DW_SECT_LINE:
12236 sections.line_offset = offset;
12237 sections.line_size = size;
12238 break;
12239 case DW_SECT_LOC:
12240 sections.loc_offset = offset;
12241 sections.loc_size = size;
12242 break;
12243 case DW_SECT_STR_OFFSETS:
12244 sections.str_offsets_offset = offset;
12245 sections.str_offsets_size = size;
12246 break;
12247 case DW_SECT_MACINFO:
12248 sections.macinfo_offset = offset;
12249 sections.macinfo_size = size;
12250 break;
12251 case DW_SECT_MACRO:
12252 sections.macro_offset = offset;
12253 sections.macro_size = size;
12254 break;
12255 }
12256 }
12257
12258 /* It's easier for the rest of the code if we fake a struct dwo_file and
12259 have dwo_unit "live" in that. At least for now.
12260
12261 The DWP file can be made up of a random collection of CUs and TUs.
12262 However, for each CU + set of TUs that came from the same original DWO
12263 file, we can combine them back into a virtual DWO file to save space
12264 (fewer struct dwo_file objects to allocate). Remember that for really
12265 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12266
791afaa2
TT
12267 std::string virtual_dwo_name =
12268 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12269 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12270 (long) (sections.line_size ? sections.line_offset : 0),
12271 (long) (sections.loc_size ? sections.loc_offset : 0),
12272 (long) (sections.str_offsets_size
12273 ? sections.str_offsets_offset : 0));
73869dc2 12274 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12275 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12276 virtual_dwo_name.c_str (),
12277 comp_dir);
73869dc2
DE
12278 /* Create one if necessary. */
12279 if (*dwo_file_slot == NULL)
12280 {
b4f54984 12281 if (dwarf_read_debug)
73869dc2
DE
12282 {
12283 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12284 virtual_dwo_name.c_str ());
73869dc2 12285 }
51ac9db5 12286 dwo_file = new struct dwo_file;
efba19b0
TT
12287 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12288 virtual_dwo_name);
73869dc2
DE
12289 dwo_file->comp_dir = comp_dir;
12290 dwo_file->sections.abbrev =
ed2dc618 12291 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12292 sections.abbrev_offset, sections.abbrev_size);
12293 dwo_file->sections.line =
ed2dc618 12294 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12295 sections.line_offset, sections.line_size);
12296 dwo_file->sections.loc =
ed2dc618 12297 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12298 sections.loc_offset, sections.loc_size);
12299 dwo_file->sections.macinfo =
ed2dc618 12300 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12301 sections.macinfo_offset, sections.macinfo_size);
12302 dwo_file->sections.macro =
ed2dc618 12303 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12304 sections.macro_offset, sections.macro_size);
12305 dwo_file->sections.str_offsets =
ed2dc618
SM
12306 create_dwp_v2_section (dwarf2_per_objfile,
12307 &dwp_file->sections.str_offsets,
73869dc2
DE
12308 sections.str_offsets_offset,
12309 sections.str_offsets_size);
12310 /* The "str" section is global to the entire DWP file. */
12311 dwo_file->sections.str = dwp_file->sections.str;
12312 /* The info or types section is assigned below to dwo_unit,
12313 there's no need to record it in dwo_file.
12314 Also, we can't simply record type sections in dwo_file because
12315 we record a pointer into the vector in dwo_unit. As we collect more
12316 types we'll grow the vector and eventually have to reallocate space
12317 for it, invalidating all copies of pointers into the previous
12318 contents. */
12319 *dwo_file_slot = dwo_file;
12320 }
12321 else
12322 {
b4f54984 12323 if (dwarf_read_debug)
73869dc2
DE
12324 {
12325 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12326 virtual_dwo_name.c_str ());
73869dc2 12327 }
9a3c8263 12328 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12329 }
73869dc2
DE
12330
12331 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12332 dwo_unit->dwo_file = dwo_file;
12333 dwo_unit->signature = signature;
8d749320
SM
12334 dwo_unit->section =
12335 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12336 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12337 is_debug_types
73869dc2
DE
12338 ? &dwp_file->sections.types
12339 : &dwp_file->sections.info,
12340 sections.info_or_types_offset,
12341 sections.info_or_types_size);
12342 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12343
12344 return dwo_unit;
12345}
12346
57d63ce2
DE
12347/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12348 Returns NULL if the signature isn't found. */
80626a55
DE
12349
12350static struct dwo_unit *
ed2dc618
SM
12351lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12352 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12353 ULONGEST signature, int is_debug_types)
80626a55 12354{
57d63ce2
DE
12355 const struct dwp_hash_table *dwp_htab =
12356 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12357 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12358 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12359 uint32_t hash = signature & mask;
12360 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12361 unsigned int i;
12362 void **slot;
870f88f7 12363 struct dwo_unit find_dwo_cu;
80626a55
DE
12364
12365 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12366 find_dwo_cu.signature = signature;
19ac8c2e
DE
12367 slot = htab_find_slot (is_debug_types
12368 ? dwp_file->loaded_tus
12369 : dwp_file->loaded_cus,
12370 &find_dwo_cu, INSERT);
80626a55
DE
12371
12372 if (*slot != NULL)
9a3c8263 12373 return (struct dwo_unit *) *slot;
80626a55
DE
12374
12375 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12376 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12377 {
12378 ULONGEST signature_in_table;
12379
12380 signature_in_table =
57d63ce2 12381 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12382 if (signature_in_table == signature)
12383 {
57d63ce2
DE
12384 uint32_t unit_index =
12385 read_4_bytes (dbfd,
12386 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12387
73869dc2
DE
12388 if (dwp_file->version == 1)
12389 {
ed2dc618
SM
12390 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12391 dwp_file, unit_index,
73869dc2
DE
12392 comp_dir, signature,
12393 is_debug_types);
12394 }
12395 else
12396 {
ed2dc618
SM
12397 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12398 dwp_file, unit_index,
73869dc2
DE
12399 comp_dir, signature,
12400 is_debug_types);
12401 }
9a3c8263 12402 return (struct dwo_unit *) *slot;
80626a55
DE
12403 }
12404 if (signature_in_table == 0)
12405 return NULL;
12406 hash = (hash + hash2) & mask;
12407 }
12408
12409 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12410 " [in module %s]"),
12411 dwp_file->name);
12412}
12413
ab5088bf 12414/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12415 Open the file specified by FILE_NAME and hand it off to BFD for
12416 preliminary analysis. Return a newly initialized bfd *, which
12417 includes a canonicalized copy of FILE_NAME.
80626a55 12418 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12419 SEARCH_CWD is true if the current directory is to be searched.
12420 It will be searched before debug-file-directory.
13aaf454
DE
12421 If successful, the file is added to the bfd include table of the
12422 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12423 If unable to find/open the file, return NULL.
3019eac3
DE
12424 NOTE: This function is derived from symfile_bfd_open. */
12425
192b62ce 12426static gdb_bfd_ref_ptr
ed2dc618
SM
12427try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12428 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12429{
24b9144d 12430 int desc;
9c02c129
DE
12431 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12432 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12433 to debug_file_directory. */
e0cc99a6 12434 const char *search_path;
9c02c129
DE
12435 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12436
e0cc99a6 12437 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12438 if (search_cwd)
12439 {
12440 if (*debug_file_directory != '\0')
e0cc99a6
TT
12441 {
12442 search_path_holder.reset (concat (".", dirname_separator_string,
12443 debug_file_directory,
12444 (char *) NULL));
12445 search_path = search_path_holder.get ();
12446 }
6ac97d4c 12447 else
e0cc99a6 12448 search_path = ".";
6ac97d4c 12449 }
9c02c129 12450 else
e0cc99a6 12451 search_path = debug_file_directory;
3019eac3 12452
24b9144d 12453 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12454 if (is_dwp)
12455 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12456
12457 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12458 desc = openp (search_path, flags, file_name,
3019eac3
DE
12459 O_RDONLY | O_BINARY, &absolute_name);
12460 if (desc < 0)
12461 return NULL;
12462
e0cc99a6
TT
12463 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12464 gnutarget, desc));
9c02c129
DE
12465 if (sym_bfd == NULL)
12466 return NULL;
192b62ce 12467 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12468
192b62ce
TT
12469 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12470 return NULL;
3019eac3 12471
13aaf454
DE
12472 /* Success. Record the bfd as having been included by the objfile's bfd.
12473 This is important because things like demangled_names_hash lives in the
12474 objfile's per_bfd space and may have references to things like symbol
12475 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12476 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12477
3019eac3
DE
12478 return sym_bfd;
12479}
12480
ab5088bf 12481/* Try to open DWO file FILE_NAME.
3019eac3
DE
12482 COMP_DIR is the DW_AT_comp_dir attribute.
12483 The result is the bfd handle of the file.
12484 If there is a problem finding or opening the file, return NULL.
12485 Upon success, the canonicalized path of the file is stored in the bfd,
12486 same as symfile_bfd_open. */
12487
192b62ce 12488static gdb_bfd_ref_ptr
ed2dc618
SM
12489open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12490 const char *file_name, const char *comp_dir)
3019eac3 12491{
80626a55 12492 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12493 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12494 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12495
12496 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12497
12498 if (comp_dir != NULL)
12499 {
43816ebc
TT
12500 gdb::unique_xmalloc_ptr<char> path_to_try
12501 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12502
12503 /* NOTE: If comp_dir is a relative path, this will also try the
12504 search path, which seems useful. */
ed2dc618 12505 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12506 path_to_try.get (),
ed2dc618 12507 0 /*is_dwp*/,
192b62ce 12508 1 /*search_cwd*/));
3019eac3
DE
12509 if (abfd != NULL)
12510 return abfd;
12511 }
12512
12513 /* That didn't work, try debug-file-directory, which, despite its name,
12514 is a list of paths. */
12515
12516 if (*debug_file_directory == '\0')
12517 return NULL;
12518
ed2dc618
SM
12519 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12520 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12521}
12522
80626a55
DE
12523/* This function is mapped across the sections and remembers the offset and
12524 size of each of the DWO debugging sections we are interested in. */
12525
12526static void
12527dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12528{
9a3c8263 12529 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12530 const struct dwop_section_names *names = &dwop_section_names;
12531
12532 if (section_is_p (sectp->name, &names->abbrev_dwo))
12533 {
049412e3 12534 dwo_sections->abbrev.s.section = sectp;
fd361982 12535 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12536 }
12537 else if (section_is_p (sectp->name, &names->info_dwo))
12538 {
049412e3 12539 dwo_sections->info.s.section = sectp;
fd361982 12540 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12541 }
12542 else if (section_is_p (sectp->name, &names->line_dwo))
12543 {
049412e3 12544 dwo_sections->line.s.section = sectp;
fd361982 12545 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12546 }
12547 else if (section_is_p (sectp->name, &names->loc_dwo))
12548 {
049412e3 12549 dwo_sections->loc.s.section = sectp;
fd361982 12550 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12551 }
12552 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12553 {
049412e3 12554 dwo_sections->macinfo.s.section = sectp;
fd361982 12555 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12556 }
12557 else if (section_is_p (sectp->name, &names->macro_dwo))
12558 {
049412e3 12559 dwo_sections->macro.s.section = sectp;
fd361982 12560 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12561 }
12562 else if (section_is_p (sectp->name, &names->str_dwo))
12563 {
049412e3 12564 dwo_sections->str.s.section = sectp;
fd361982 12565 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12566 }
12567 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12568 {
049412e3 12569 dwo_sections->str_offsets.s.section = sectp;
fd361982 12570 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12571 }
12572 else if (section_is_p (sectp->name, &names->types_dwo))
12573 {
12574 struct dwarf2_section_info type_section;
12575
12576 memset (&type_section, 0, sizeof (type_section));
049412e3 12577 type_section.s.section = sectp;
fd361982 12578 type_section.size = bfd_section_size (sectp);
fd5866f6 12579 dwo_sections->types.push_back (type_section);
80626a55
DE
12580 }
12581}
12582
ab5088bf 12583/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12584 by PER_CU. This is for the non-DWP case.
80626a55 12585 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12586
12587static struct dwo_file *
0ac5b59e
DE
12588open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12589 const char *dwo_name, const char *comp_dir)
3019eac3 12590{
ed2dc618 12591 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12592
fb1eb2f9 12593 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12594 if (dbfd == NULL)
12595 {
b4f54984 12596 if (dwarf_read_debug)
80626a55
DE
12597 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12598 return NULL;
12599 }
263db9a1 12600
51ac9db5 12601 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12602 dwo_file->dwo_name = dwo_name;
12603 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12604 dwo_file->dbfd = std::move (dbfd);
3019eac3 12605
fb1eb2f9 12606 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12607 &dwo_file->sections);
3019eac3 12608
18a8505e
AT
12609 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12610 dwo_file->sections.info, dwo_file->cus);
3019eac3 12611
263db9a1 12612 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12613 dwo_file->sections.types, dwo_file->tus);
3019eac3 12614
b4f54984 12615 if (dwarf_read_debug)
80626a55
DE
12616 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12617
263db9a1 12618 return dwo_file.release ();
3019eac3
DE
12619}
12620
80626a55 12621/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12622 size of each of the DWP debugging sections common to version 1 and 2 that
12623 we are interested in. */
3019eac3 12624
80626a55 12625static void
73869dc2
DE
12626dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12627 void *dwp_file_ptr)
3019eac3 12628{
9a3c8263 12629 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12630 const struct dwop_section_names *names = &dwop_section_names;
12631 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12632
80626a55 12633 /* Record the ELF section number for later lookup: this is what the
73869dc2 12634 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12635 gdb_assert (elf_section_nr < dwp_file->num_sections);
12636 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12637
80626a55
DE
12638 /* Look for specific sections that we need. */
12639 if (section_is_p (sectp->name, &names->str_dwo))
12640 {
049412e3 12641 dwp_file->sections.str.s.section = sectp;
fd361982 12642 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12643 }
12644 else if (section_is_p (sectp->name, &names->cu_index))
12645 {
049412e3 12646 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12647 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12648 }
12649 else if (section_is_p (sectp->name, &names->tu_index))
12650 {
049412e3 12651 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12652 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12653 }
12654}
3019eac3 12655
73869dc2
DE
12656/* This function is mapped across the sections and remembers the offset and
12657 size of each of the DWP version 2 debugging sections that we are interested
12658 in. This is split into a separate function because we don't know if we
12659 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12660
12661static void
12662dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12663{
9a3c8263 12664 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12665 const struct dwop_section_names *names = &dwop_section_names;
12666 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12667
12668 /* Record the ELF section number for later lookup: this is what the
12669 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12670 gdb_assert (elf_section_nr < dwp_file->num_sections);
12671 dwp_file->elf_sections[elf_section_nr] = sectp;
12672
12673 /* Look for specific sections that we need. */
12674 if (section_is_p (sectp->name, &names->abbrev_dwo))
12675 {
049412e3 12676 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12677 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12678 }
12679 else if (section_is_p (sectp->name, &names->info_dwo))
12680 {
049412e3 12681 dwp_file->sections.info.s.section = sectp;
fd361982 12682 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12683 }
12684 else if (section_is_p (sectp->name, &names->line_dwo))
12685 {
049412e3 12686 dwp_file->sections.line.s.section = sectp;
fd361982 12687 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12688 }
12689 else if (section_is_p (sectp->name, &names->loc_dwo))
12690 {
049412e3 12691 dwp_file->sections.loc.s.section = sectp;
fd361982 12692 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12693 }
12694 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12695 {
049412e3 12696 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12697 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12698 }
12699 else if (section_is_p (sectp->name, &names->macro_dwo))
12700 {
049412e3 12701 dwp_file->sections.macro.s.section = sectp;
fd361982 12702 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12703 }
12704 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12705 {
049412e3 12706 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12707 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12708 }
12709 else if (section_is_p (sectp->name, &names->types_dwo))
12710 {
049412e3 12711 dwp_file->sections.types.s.section = sectp;
fd361982 12712 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12713 }
12714}
12715
80626a55 12716/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12717
80626a55
DE
12718static hashval_t
12719hash_dwp_loaded_cutus (const void *item)
12720{
9a3c8263 12721 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12722
80626a55
DE
12723 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12724 return dwo_unit->signature;
3019eac3
DE
12725}
12726
80626a55 12727/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12728
80626a55
DE
12729static int
12730eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12731{
9a3c8263
SM
12732 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12733 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12734
80626a55
DE
12735 return dua->signature == dub->signature;
12736}
3019eac3 12737
80626a55 12738/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12739
80626a55
DE
12740static htab_t
12741allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12742{
12743 return htab_create_alloc_ex (3,
12744 hash_dwp_loaded_cutus,
12745 eq_dwp_loaded_cutus,
12746 NULL,
12747 &objfile->objfile_obstack,
12748 hashtab_obstack_allocate,
12749 dummy_obstack_deallocate);
12750}
3019eac3 12751
ab5088bf
DE
12752/* Try to open DWP file FILE_NAME.
12753 The result is the bfd handle of the file.
12754 If there is a problem finding or opening the file, return NULL.
12755 Upon success, the canonicalized path of the file is stored in the bfd,
12756 same as symfile_bfd_open. */
12757
192b62ce 12758static gdb_bfd_ref_ptr
ed2dc618
SM
12759open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12760 const char *file_name)
ab5088bf 12761{
ed2dc618
SM
12762 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12763 1 /*is_dwp*/,
192b62ce 12764 1 /*search_cwd*/));
6ac97d4c
DE
12765 if (abfd != NULL)
12766 return abfd;
12767
12768 /* Work around upstream bug 15652.
12769 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12770 [Whether that's a "bug" is debatable, but it is getting in our way.]
12771 We have no real idea where the dwp file is, because gdb's realpath-ing
12772 of the executable's path may have discarded the needed info.
12773 [IWBN if the dwp file name was recorded in the executable, akin to
12774 .gnu_debuglink, but that doesn't exist yet.]
12775 Strip the directory from FILE_NAME and search again. */
12776 if (*debug_file_directory != '\0')
12777 {
12778 /* Don't implicitly search the current directory here.
12779 If the user wants to search "." to handle this case,
12780 it must be added to debug-file-directory. */
ed2dc618
SM
12781 return try_open_dwop_file (dwarf2_per_objfile,
12782 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12783 0 /*search_cwd*/);
12784 }
12785
12786 return NULL;
ab5088bf
DE
12787}
12788
80626a55
DE
12789/* Initialize the use of the DWP file for the current objfile.
12790 By convention the name of the DWP file is ${objfile}.dwp.
12791 The result is NULL if it can't be found. */
a766d390 12792
400174b1 12793static std::unique_ptr<struct dwp_file>
ed2dc618 12794open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12795{
12796 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12797
82bf32bc
JK
12798 /* Try to find first .dwp for the binary file before any symbolic links
12799 resolving. */
6c447423
DE
12800
12801 /* If the objfile is a debug file, find the name of the real binary
12802 file and get the name of dwp file from there. */
d721ba37 12803 std::string dwp_name;
6c447423
DE
12804 if (objfile->separate_debug_objfile_backlink != NULL)
12805 {
12806 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12807 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12808
d721ba37 12809 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12810 }
12811 else
d721ba37
PA
12812 dwp_name = objfile->original_name;
12813
12814 dwp_name += ".dwp";
80626a55 12815
ed2dc618 12816 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12817 if (dbfd == NULL
12818 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12819 {
12820 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12821 dwp_name = objfile_name (objfile);
12822 dwp_name += ".dwp";
ed2dc618 12823 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12824 }
12825
80626a55
DE
12826 if (dbfd == NULL)
12827 {
b4f54984 12828 if (dwarf_read_debug)
d721ba37 12829 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12830 return std::unique_ptr<dwp_file> ();
3019eac3 12831 }
400174b1
TT
12832
12833 const char *name = bfd_get_filename (dbfd.get ());
12834 std::unique_ptr<struct dwp_file> dwp_file
12835 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12836
0a0f4c01 12837 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12838 dwp_file->elf_sections =
12839 OBSTACK_CALLOC (&objfile->objfile_obstack,
12840 dwp_file->num_sections, asection *);
12841
400174b1
TT
12842 bfd_map_over_sections (dwp_file->dbfd.get (),
12843 dwarf2_locate_common_dwp_sections,
12844 dwp_file.get ());
80626a55 12845
400174b1
TT
12846 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12847 0);
80626a55 12848
400174b1
TT
12849 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12850 1);
80626a55 12851
73869dc2 12852 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12853 if (dwp_file->cus && dwp_file->tus
12854 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12855 {
12856 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12857 pretty bizarre. We use pulongest here because that's the established
4d65956b 12858 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12859 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12860 " TU version %s [in DWP file %s]"),
12861 pulongest (dwp_file->cus->version),
d721ba37 12862 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12863 }
08302ed2
DE
12864
12865 if (dwp_file->cus)
12866 dwp_file->version = dwp_file->cus->version;
12867 else if (dwp_file->tus)
12868 dwp_file->version = dwp_file->tus->version;
12869 else
12870 dwp_file->version = 2;
73869dc2
DE
12871
12872 if (dwp_file->version == 2)
400174b1
TT
12873 bfd_map_over_sections (dwp_file->dbfd.get (),
12874 dwarf2_locate_v2_dwp_sections,
12875 dwp_file.get ());
73869dc2 12876
19ac8c2e
DE
12877 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12878 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 12879
b4f54984 12880 if (dwarf_read_debug)
80626a55
DE
12881 {
12882 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12883 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12884 " %s CUs, %s TUs\n",
12885 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12886 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12887 }
12888
12889 return dwp_file;
3019eac3 12890}
c906108c 12891
ab5088bf
DE
12892/* Wrapper around open_and_init_dwp_file, only open it once. */
12893
12894static struct dwp_file *
ed2dc618 12895get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12896{
12897 if (! dwarf2_per_objfile->dwp_checked)
12898 {
ed2dc618
SM
12899 dwarf2_per_objfile->dwp_file
12900 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12901 dwarf2_per_objfile->dwp_checked = 1;
12902 }
400174b1 12903 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12904}
12905
80626a55
DE
12906/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12907 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12908 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12909 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12910 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12911
12912 This is called, for example, when wanting to read a variable with a
12913 complex location. Therefore we don't want to do file i/o for every call.
12914 Therefore we don't want to look for a DWO file on every call.
12915 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12916 then we check if we've already seen DWO_NAME, and only THEN do we check
12917 for a DWO file.
12918
1c658ad5 12919 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12920 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12921
3019eac3 12922static struct dwo_unit *
80626a55
DE
12923lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12924 const char *dwo_name, const char *comp_dir,
12925 ULONGEST signature, int is_debug_types)
3019eac3 12926{
ed2dc618 12927 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12928 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12929 const char *kind = is_debug_types ? "TU" : "CU";
12930 void **dwo_file_slot;
3019eac3 12931 struct dwo_file *dwo_file;
80626a55 12932 struct dwp_file *dwp_file;
cb1df416 12933
6a506a2d
DE
12934 /* First see if there's a DWP file.
12935 If we have a DWP file but didn't find the DWO inside it, don't
12936 look for the original DWO file. It makes gdb behave differently
12937 depending on whether one is debugging in the build tree. */
cf2c3c16 12938
ed2dc618 12939 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12940 if (dwp_file != NULL)
cf2c3c16 12941 {
80626a55
DE
12942 const struct dwp_hash_table *dwp_htab =
12943 is_debug_types ? dwp_file->tus : dwp_file->cus;
12944
12945 if (dwp_htab != NULL)
12946 {
12947 struct dwo_unit *dwo_cutu =
ed2dc618 12948 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12949 signature, is_debug_types);
80626a55
DE
12950
12951 if (dwo_cutu != NULL)
12952 {
b4f54984 12953 if (dwarf_read_debug)
80626a55
DE
12954 {
12955 fprintf_unfiltered (gdb_stdlog,
12956 "Virtual DWO %s %s found: @%s\n",
12957 kind, hex_string (signature),
12958 host_address_to_string (dwo_cutu));
12959 }
12960 return dwo_cutu;
12961 }
12962 }
12963 }
6a506a2d 12964 else
80626a55 12965 {
6a506a2d 12966 /* No DWP file, look for the DWO file. */
80626a55 12967
ed2dc618
SM
12968 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12969 dwo_name, comp_dir);
6a506a2d 12970 if (*dwo_file_slot == NULL)
80626a55 12971 {
6a506a2d
DE
12972 /* Read in the file and build a table of the CUs/TUs it contains. */
12973 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12974 }
6a506a2d 12975 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12976 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12977
6a506a2d 12978 if (dwo_file != NULL)
19c3d4c9 12979 {
6a506a2d
DE
12980 struct dwo_unit *dwo_cutu = NULL;
12981
12982 if (is_debug_types && dwo_file->tus)
12983 {
12984 struct dwo_unit find_dwo_cutu;
12985
12986 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12987 find_dwo_cutu.signature = signature;
9a3c8263
SM
12988 dwo_cutu
12989 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 12990 }
33c5cd75 12991 else if (!is_debug_types && dwo_file->cus)
80626a55 12992 {
33c5cd75
DB
12993 struct dwo_unit find_dwo_cutu;
12994
12995 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12996 find_dwo_cutu.signature = signature;
12997 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
12998 &find_dwo_cutu);
6a506a2d
DE
12999 }
13000
13001 if (dwo_cutu != NULL)
13002 {
b4f54984 13003 if (dwarf_read_debug)
6a506a2d
DE
13004 {
13005 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13006 kind, dwo_name, hex_string (signature),
13007 host_address_to_string (dwo_cutu));
13008 }
13009 return dwo_cutu;
80626a55
DE
13010 }
13011 }
2e276125 13012 }
9cdd5dbd 13013
80626a55
DE
13014 /* We didn't find it. This could mean a dwo_id mismatch, or
13015 someone deleted the DWO/DWP file, or the search path isn't set up
13016 correctly to find the file. */
13017
b4f54984 13018 if (dwarf_read_debug)
80626a55
DE
13019 {
13020 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13021 kind, dwo_name, hex_string (signature));
13022 }
3019eac3 13023
6656a72d
DE
13024 /* This is a warning and not a complaint because it can be caused by
13025 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13026 {
13027 /* Print the name of the DWP file if we looked there, helps the user
13028 better diagnose the problem. */
791afaa2 13029 std::string dwp_text;
43942612
DE
13030
13031 if (dwp_file != NULL)
791afaa2
TT
13032 dwp_text = string_printf (" [in DWP file %s]",
13033 lbasename (dwp_file->name));
43942612 13034
9d8780f0 13035 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13036 " [in module %s]"),
13037 kind, dwo_name, hex_string (signature),
791afaa2 13038 dwp_text.c_str (),
43942612 13039 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13040 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13041 }
3019eac3 13042 return NULL;
5fb290d7
DJ
13043}
13044
80626a55
DE
13045/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13046 See lookup_dwo_cutu_unit for details. */
13047
13048static struct dwo_unit *
13049lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13050 const char *dwo_name, const char *comp_dir,
13051 ULONGEST signature)
13052{
13053 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13054}
13055
13056/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13057 See lookup_dwo_cutu_unit for details. */
13058
13059static struct dwo_unit *
13060lookup_dwo_type_unit (struct signatured_type *this_tu,
13061 const char *dwo_name, const char *comp_dir)
13062{
13063 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13064}
13065
89e63ee4
DE
13066/* Traversal function for queue_and_load_all_dwo_tus. */
13067
13068static int
13069queue_and_load_dwo_tu (void **slot, void *info)
13070{
13071 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13072 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13073 ULONGEST signature = dwo_unit->signature;
13074 struct signatured_type *sig_type =
13075 lookup_dwo_signatured_type (per_cu->cu, signature);
13076
13077 if (sig_type != NULL)
13078 {
13079 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13080
13081 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13082 a real dependency of PER_CU on SIG_TYPE. That is detected later
13083 while processing PER_CU. */
13084 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13085 load_full_type_unit (sig_cu);
ae640021 13086 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13087 }
13088
13089 return 1;
13090}
13091
13092/* Queue all TUs contained in the DWO of PER_CU to be read in.
13093 The DWO may have the only definition of the type, though it may not be
13094 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13095 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13096
13097static void
13098queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13099{
13100 struct dwo_unit *dwo_unit;
13101 struct dwo_file *dwo_file;
13102
13103 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13104 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13105 gdb_assert (per_cu->cu != NULL);
13106
13107 dwo_unit = per_cu->cu->dwo_unit;
13108 gdb_assert (dwo_unit != NULL);
13109
13110 dwo_file = dwo_unit->dwo_file;
13111 if (dwo_file->tus != NULL)
13112 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13113}
13114
3019eac3 13115/* Read in various DIEs. */
348e048f 13116
d389af10 13117/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13118 Inherit only the children of the DW_AT_abstract_origin DIE not being
13119 already referenced by DW_AT_abstract_origin from the children of the
13120 current DIE. */
d389af10
JK
13121
13122static void
13123inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13124{
13125 struct die_info *child_die;
791afaa2 13126 sect_offset *offsetp;
d389af10
JK
13127 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13128 struct die_info *origin_die;
13129 /* Iterator of the ORIGIN_DIE children. */
13130 struct die_info *origin_child_die;
d389af10 13131 struct attribute *attr;
cd02d79d
PA
13132 struct dwarf2_cu *origin_cu;
13133 struct pending **origin_previous_list_in_scope;
d389af10
JK
13134
13135 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13136 if (!attr)
13137 return;
13138
cd02d79d
PA
13139 /* Note that following die references may follow to a die in a
13140 different cu. */
13141
13142 origin_cu = cu;
13143 origin_die = follow_die_ref (die, attr, &origin_cu);
13144
13145 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13146 symbols in. */
13147 origin_previous_list_in_scope = origin_cu->list_in_scope;
13148 origin_cu->list_in_scope = cu->list_in_scope;
13149
edb3359d
DJ
13150 if (die->tag != origin_die->tag
13151 && !(die->tag == DW_TAG_inlined_subroutine
13152 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13153 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13154 sect_offset_str (die->sect_off),
13155 sect_offset_str (origin_die->sect_off));
d389af10 13156
791afaa2 13157 std::vector<sect_offset> offsets;
d389af10 13158
3ea89b92
PMR
13159 for (child_die = die->child;
13160 child_die && child_die->tag;
13161 child_die = sibling_die (child_die))
13162 {
13163 struct die_info *child_origin_die;
13164 struct dwarf2_cu *child_origin_cu;
13165
13166 /* We are trying to process concrete instance entries:
216f72a1 13167 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13168 it's not relevant to our analysis here. i.e. detecting DIEs that are
13169 present in the abstract instance but not referenced in the concrete
13170 one. */
216f72a1
JK
13171 if (child_die->tag == DW_TAG_call_site
13172 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13173 continue;
13174
c38f313d
DJ
13175 /* For each CHILD_DIE, find the corresponding child of
13176 ORIGIN_DIE. If there is more than one layer of
13177 DW_AT_abstract_origin, follow them all; there shouldn't be,
13178 but GCC versions at least through 4.4 generate this (GCC PR
13179 40573). */
3ea89b92
PMR
13180 child_origin_die = child_die;
13181 child_origin_cu = cu;
c38f313d
DJ
13182 while (1)
13183 {
cd02d79d
PA
13184 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13185 child_origin_cu);
c38f313d
DJ
13186 if (attr == NULL)
13187 break;
cd02d79d
PA
13188 child_origin_die = follow_die_ref (child_origin_die, attr,
13189 &child_origin_cu);
c38f313d
DJ
13190 }
13191
d389af10
JK
13192 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13193 counterpart may exist. */
c38f313d 13194 if (child_origin_die != child_die)
d389af10 13195 {
edb3359d
DJ
13196 if (child_die->tag != child_origin_die->tag
13197 && !(child_die->tag == DW_TAG_inlined_subroutine
13198 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13199 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13200 "different tags"),
9d8780f0
SM
13201 sect_offset_str (child_die->sect_off),
13202 sect_offset_str (child_origin_die->sect_off));
c38f313d 13203 if (child_origin_die->parent != origin_die)
b98664d3 13204 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13205 "different parents"),
9d8780f0
SM
13206 sect_offset_str (child_die->sect_off),
13207 sect_offset_str (child_origin_die->sect_off));
c38f313d 13208 else
791afaa2 13209 offsets.push_back (child_origin_die->sect_off);
d389af10 13210 }
d389af10 13211 }
791afaa2
TT
13212 std::sort (offsets.begin (), offsets.end ());
13213 sect_offset *offsets_end = offsets.data () + offsets.size ();
13214 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13215 if (offsetp[-1] == *offsetp)
b98664d3 13216 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13217 "to DIE %s as their abstract origin"),
13218 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13219
791afaa2 13220 offsetp = offsets.data ();
d389af10
JK
13221 origin_child_die = origin_die->child;
13222 while (origin_child_die && origin_child_die->tag)
13223 {
13224 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13225 while (offsetp < offsets_end
9c541725 13226 && *offsetp < origin_child_die->sect_off)
d389af10 13227 offsetp++;
b64f50a1 13228 if (offsetp >= offsets_end
9c541725 13229 || *offsetp > origin_child_die->sect_off)
d389af10 13230 {
adde2bff
DE
13231 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13232 Check whether we're already processing ORIGIN_CHILD_DIE.
13233 This can happen with mutually referenced abstract_origins.
13234 PR 16581. */
13235 if (!origin_child_die->in_process)
13236 process_die (origin_child_die, origin_cu);
d389af10
JK
13237 }
13238 origin_child_die = sibling_die (origin_child_die);
13239 }
cd02d79d 13240 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13241
13242 if (cu != origin_cu)
13243 compute_delayed_physnames (origin_cu);
d389af10
JK
13244}
13245
c906108c 13246static void
e7c27a73 13247read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13248{
518817b3 13249 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13250 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13251 struct context_stack *newobj;
c906108c
SS
13252 CORE_ADDR lowpc;
13253 CORE_ADDR highpc;
13254 struct die_info *child_die;
edb3359d 13255 struct attribute *attr, *call_line, *call_file;
15d034d0 13256 const char *name;
e142c38c 13257 CORE_ADDR baseaddr;
801e3a5b 13258 struct block *block;
edb3359d 13259 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13260 std::vector<struct symbol *> template_args;
34eaf542 13261 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13262
13263 if (inlined_func)
13264 {
13265 /* If we do not have call site information, we can't show the
13266 caller of this inlined function. That's too confusing, so
13267 only use the scope for local variables. */
13268 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13269 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13270 if (call_line == NULL || call_file == NULL)
13271 {
13272 read_lexical_block_scope (die, cu);
13273 return;
13274 }
13275 }
c906108c 13276
b3b3bada 13277 baseaddr = objfile->text_section_offset ();
e142c38c 13278
94af9270 13279 name = dwarf2_name (die, cu);
c906108c 13280
e8d05480
JB
13281 /* Ignore functions with missing or empty names. These are actually
13282 illegal according to the DWARF standard. */
13283 if (name == NULL)
13284 {
b98664d3 13285 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13286 sect_offset_str (die->sect_off));
e8d05480
JB
13287 return;
13288 }
13289
13290 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13291 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13292 <= PC_BOUNDS_INVALID)
e8d05480 13293 {
ae4d0c03
PM
13294 attr = dwarf2_attr (die, DW_AT_external, cu);
13295 if (!attr || !DW_UNSND (attr))
b98664d3 13296 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13297 "for subprogram DIE at %s"),
13298 sect_offset_str (die->sect_off));
e8d05480
JB
13299 return;
13300 }
c906108c 13301
3e29f34a
MR
13302 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13303 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13304
34eaf542
TT
13305 /* If we have any template arguments, then we must allocate a
13306 different sort of symbol. */
13307 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13308 {
13309 if (child_die->tag == DW_TAG_template_type_param
13310 || child_die->tag == DW_TAG_template_value_param)
13311 {
e623cf5d 13312 templ_func = allocate_template_symbol (objfile);
cf724bc9 13313 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13314 break;
13315 }
13316 }
13317
c24bdb02 13318 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13319 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13320 (struct symbol *) templ_func);
4c2df51b 13321
81873cc8 13322 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13323 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13324 cu->language);
13325
4cecd739
DJ
13326 /* If there is a location expression for DW_AT_frame_base, record
13327 it. */
e142c38c 13328 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13329 if (attr != nullptr)
fe978cb0 13330 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13331
63e43d3a
PMR
13332 /* If there is a location for the static link, record it. */
13333 newobj->static_link = NULL;
13334 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13335 if (attr != nullptr)
63e43d3a 13336 {
224c3ddb
SM
13337 newobj->static_link
13338 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13339 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13340 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13341 }
13342
c24bdb02 13343 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13344
639d11d3 13345 if (die->child != NULL)
c906108c 13346 {
639d11d3 13347 child_die = die->child;
c906108c
SS
13348 while (child_die && child_die->tag)
13349 {
34eaf542
TT
13350 if (child_die->tag == DW_TAG_template_type_param
13351 || child_die->tag == DW_TAG_template_value_param)
13352 {
13353 struct symbol *arg = new_symbol (child_die, NULL, cu);
13354
f1078f66 13355 if (arg != NULL)
2f4732b0 13356 template_args.push_back (arg);
34eaf542
TT
13357 }
13358 else
13359 process_die (child_die, cu);
c906108c
SS
13360 child_die = sibling_die (child_die);
13361 }
13362 }
13363
d389af10
JK
13364 inherit_abstract_dies (die, cu);
13365
4a811a97
UW
13366 /* If we have a DW_AT_specification, we might need to import using
13367 directives from the context of the specification DIE. See the
13368 comment in determine_prefix. */
13369 if (cu->language == language_cplus
13370 && dwarf2_attr (die, DW_AT_specification, cu))
13371 {
13372 struct dwarf2_cu *spec_cu = cu;
13373 struct die_info *spec_die = die_specification (die, &spec_cu);
13374
13375 while (spec_die)
13376 {
13377 child_die = spec_die->child;
13378 while (child_die && child_die->tag)
13379 {
13380 if (child_die->tag == DW_TAG_imported_module)
13381 process_die (child_die, spec_cu);
13382 child_die = sibling_die (child_die);
13383 }
13384
13385 /* In some cases, GCC generates specification DIEs that
13386 themselves contain DW_AT_specification attributes. */
13387 spec_die = die_specification (spec_die, &spec_cu);
13388 }
13389 }
13390
c24bdb02 13391 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13392 /* Make a block for the local symbols within. */
c24bdb02 13393 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13394 cstk.static_link, lowpc, highpc);
801e3a5b 13395
df8a16a1 13396 /* For C++, set the block's scope. */
45280282
IB
13397 if ((cu->language == language_cplus
13398 || cu->language == language_fortran
c44af4eb
TT
13399 || cu->language == language_d
13400 || cu->language == language_rust)
4d4ec4e5 13401 && cu->processing_has_namespace_info)
195a3f6c
TT
13402 block_set_scope (block, determine_prefix (die, cu),
13403 &objfile->objfile_obstack);
df8a16a1 13404
801e3a5b
JB
13405 /* If we have address ranges, record them. */
13406 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13407
a60f3166 13408 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13409
34eaf542 13410 /* Attach template arguments to function. */
2f4732b0 13411 if (!template_args.empty ())
34eaf542
TT
13412 {
13413 gdb_assert (templ_func != NULL);
13414
2f4732b0 13415 templ_func->n_template_arguments = template_args.size ();
34eaf542 13416 templ_func->template_arguments
8d749320
SM
13417 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13418 templ_func->n_template_arguments);
34eaf542 13419 memcpy (templ_func->template_arguments,
2f4732b0 13420 template_args.data (),
34eaf542 13421 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13422
13423 /* Make sure that the symtab is set on the new symbols. Even
13424 though they don't appear in this symtab directly, other parts
13425 of gdb assume that symbols do, and this is reasonably
13426 true. */
8634679f 13427 for (symbol *sym : template_args)
3e1d3d8c 13428 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13429 }
13430
208d8187
JB
13431 /* In C++, we can have functions nested inside functions (e.g., when
13432 a function declares a class that has methods). This means that
13433 when we finish processing a function scope, we may need to go
13434 back to building a containing block's symbol lists. */
c24bdb02
KS
13435 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13436 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13437
921e78cf
JB
13438 /* If we've finished processing a top-level function, subsequent
13439 symbols go in the file symbol list. */
c24bdb02
KS
13440 if (cu->get_builder ()->outermost_context_p ())
13441 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13442}
13443
13444/* Process all the DIES contained within a lexical block scope. Start
13445 a new scope, process the dies, and then close the scope. */
13446
13447static void
e7c27a73 13448read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13449{
518817b3 13450 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13451 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13452 CORE_ADDR lowpc, highpc;
13453 struct die_info *child_die;
e142c38c
DJ
13454 CORE_ADDR baseaddr;
13455
b3b3bada 13456 baseaddr = objfile->text_section_offset ();
c906108c
SS
13457
13458 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13459 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13460 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13461 be nasty. Might be easier to properly extend generic blocks to
af34e669 13462 describe ranges. */
e385593e
JK
13463 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13464 {
13465 case PC_BOUNDS_NOT_PRESENT:
13466 /* DW_TAG_lexical_block has no attributes, process its children as if
13467 there was no wrapping by that DW_TAG_lexical_block.
13468 GCC does no longer produces such DWARF since GCC r224161. */
13469 for (child_die = die->child;
13470 child_die != NULL && child_die->tag;
13471 child_die = sibling_die (child_die))
13472 process_die (child_die, cu);
13473 return;
13474 case PC_BOUNDS_INVALID:
13475 return;
13476 }
3e29f34a
MR
13477 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13478 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13479
c24bdb02 13480 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13481 if (die->child != NULL)
c906108c 13482 {
639d11d3 13483 child_die = die->child;
c906108c
SS
13484 while (child_die && child_die->tag)
13485 {
e7c27a73 13486 process_die (child_die, cu);
c906108c
SS
13487 child_die = sibling_die (child_die);
13488 }
13489 }
3ea89b92 13490 inherit_abstract_dies (die, cu);
c24bdb02 13491 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13492
c24bdb02
KS
13493 if (*cu->get_builder ()->get_local_symbols () != NULL
13494 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13495 {
801e3a5b 13496 struct block *block
c24bdb02 13497 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13498 cstk.start_addr, highpc);
801e3a5b
JB
13499
13500 /* Note that recording ranges after traversing children, as we
13501 do here, means that recording a parent's ranges entails
13502 walking across all its children's ranges as they appear in
13503 the address map, which is quadratic behavior.
13504
13505 It would be nicer to record the parent's ranges before
13506 traversing its children, simply overriding whatever you find
13507 there. But since we don't even decide whether to create a
13508 block until after we've traversed its children, that's hard
13509 to do. */
13510 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13511 }
c24bdb02
KS
13512 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13513 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13514}
13515
216f72a1 13516/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13517
13518static void
13519read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13520{
518817b3 13521 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13522 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13523 CORE_ADDR pc, baseaddr;
13524 struct attribute *attr;
13525 struct call_site *call_site, call_site_local;
13526 void **slot;
13527 int nparams;
13528 struct die_info *child_die;
13529
b3b3bada 13530 baseaddr = objfile->text_section_offset ();
96408a79 13531
216f72a1
JK
13532 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13533 if (attr == NULL)
13534 {
13535 /* This was a pre-DWARF-5 GNU extension alias
13536 for DW_AT_call_return_pc. */
13537 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13538 }
96408a79
SA
13539 if (!attr)
13540 {
b98664d3 13541 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13542 "DIE %s [in module %s]"),
13543 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13544 return;
13545 }
cd6c91b4 13546 pc = attr->value_as_address () + baseaddr;
3e29f34a 13547 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13548
13549 if (cu->call_site_htab == NULL)
13550 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13551 NULL, &objfile->objfile_obstack,
13552 hashtab_obstack_allocate, NULL);
13553 call_site_local.pc = pc;
13554 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13555 if (*slot != NULL)
13556 {
b98664d3 13557 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13558 "DIE %s [in module %s]"),
13559 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13560 objfile_name (objfile));
96408a79
SA
13561 return;
13562 }
13563
13564 /* Count parameters at the caller. */
13565
13566 nparams = 0;
13567 for (child_die = die->child; child_die && child_die->tag;
13568 child_die = sibling_die (child_die))
13569 {
216f72a1
JK
13570 if (child_die->tag != DW_TAG_call_site_parameter
13571 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13572 {
b98664d3 13573 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13574 "DW_TAG_call_site child DIE %s [in module %s]"),
13575 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13576 objfile_name (objfile));
96408a79
SA
13577 continue;
13578 }
13579
13580 nparams++;
13581 }
13582
224c3ddb
SM
13583 call_site
13584 = ((struct call_site *)
13585 obstack_alloc (&objfile->objfile_obstack,
13586 sizeof (*call_site)
13587 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13588 *slot = call_site;
13589 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13590 call_site->pc = pc;
13591
216f72a1
JK
13592 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13593 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13594 {
13595 struct die_info *func_die;
13596
13597 /* Skip also over DW_TAG_inlined_subroutine. */
13598 for (func_die = die->parent;
13599 func_die && func_die->tag != DW_TAG_subprogram
13600 && func_die->tag != DW_TAG_subroutine_type;
13601 func_die = func_die->parent);
13602
216f72a1
JK
13603 /* DW_AT_call_all_calls is a superset
13604 of DW_AT_call_all_tail_calls. */
96408a79 13605 if (func_die
216f72a1 13606 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13607 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13608 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13609 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13610 {
13611 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13612 not complete. But keep CALL_SITE for look ups via call_site_htab,
13613 both the initial caller containing the real return address PC and
13614 the final callee containing the current PC of a chain of tail
13615 calls do not need to have the tail call list complete. But any
13616 function candidate for a virtual tail call frame searched via
13617 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13618 determined unambiguously. */
13619 }
13620 else
13621 {
13622 struct type *func_type = NULL;
13623
13624 if (func_die)
13625 func_type = get_die_type (func_die, cu);
13626 if (func_type != NULL)
13627 {
13628 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13629
13630 /* Enlist this call site to the function. */
13631 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13632 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13633 }
13634 else
b98664d3 13635 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13636 "DIE %s [in module %s]"),
13637 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13638 }
13639 }
13640
216f72a1
JK
13641 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13642 if (attr == NULL)
13643 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13644 if (attr == NULL)
13645 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13646 if (attr == NULL)
216f72a1
JK
13647 {
13648 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13649 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13650 }
96408a79 13651 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13652 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13653 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13654 else if (attr->form_is_block ())
96408a79
SA
13655 {
13656 struct dwarf2_locexpr_baton *dlbaton;
13657
8d749320 13658 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13659 dlbaton->data = DW_BLOCK (attr)->data;
13660 dlbaton->size = DW_BLOCK (attr)->size;
13661 dlbaton->per_cu = cu->per_cu;
13662
13663 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13664 }
cd6c91b4 13665 else if (attr->form_is_ref ())
96408a79 13666 {
96408a79
SA
13667 struct dwarf2_cu *target_cu = cu;
13668 struct die_info *target_die;
13669
ac9ec31b 13670 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13671 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13672 if (die_is_declaration (target_die, target_cu))
13673 {
7d45c7c3 13674 const char *target_physname;
9112db09
JK
13675
13676 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13677 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13678 if (target_physname == NULL)
9112db09 13679 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13680 if (target_physname == NULL)
b98664d3 13681 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13682 "physname, for referencing DIE %s [in module %s]"),
13683 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13684 else
7d455152 13685 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13686 }
13687 else
13688 {
13689 CORE_ADDR lowpc;
13690
13691 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13692 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13693 <= PC_BOUNDS_INVALID)
b98664d3 13694 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13695 "low pc, for referencing DIE %s [in module %s]"),
13696 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13697 else
3e29f34a
MR
13698 {
13699 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13700 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13701 }
96408a79
SA
13702 }
13703 }
13704 else
b98664d3 13705 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13706 "block nor reference, for DIE %s [in module %s]"),
13707 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13708
13709 call_site->per_cu = cu->per_cu;
13710
13711 for (child_die = die->child;
13712 child_die && child_die->tag;
13713 child_die = sibling_die (child_die))
13714 {
96408a79 13715 struct call_site_parameter *parameter;
1788b2d3 13716 struct attribute *loc, *origin;
96408a79 13717
216f72a1
JK
13718 if (child_die->tag != DW_TAG_call_site_parameter
13719 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13720 {
13721 /* Already printed the complaint above. */
13722 continue;
13723 }
13724
13725 gdb_assert (call_site->parameter_count < nparams);
13726 parameter = &call_site->parameter[call_site->parameter_count];
13727
1788b2d3
JK
13728 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13729 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13730 register is contained in DW_AT_call_value. */
96408a79 13731
24c5c679 13732 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13733 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13734 if (origin == NULL)
13735 {
13736 /* This was a pre-DWARF-5 GNU extension alias
13737 for DW_AT_call_parameter. */
13738 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13739 }
cd6c91b4 13740 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13741 {
1788b2d3 13742 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13743
13744 sect_offset sect_off
13745 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13746 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
13747 {
13748 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13749 binding can be done only inside one CU. Such referenced DIE
13750 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13751 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13752 "DW_TAG_call_site child DIE %s [in module %s]"),
13753 sect_offset_str (child_die->sect_off),
9c541725 13754 objfile_name (objfile));
d76b7dbc
JK
13755 continue;
13756 }
9c541725
PA
13757 parameter->u.param_cu_off
13758 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13759 }
4fc6c0d5 13760 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13761 {
b98664d3 13762 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13763 "DW_TAG_call_site child DIE %s [in module %s]"),
13764 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13765 continue;
13766 }
24c5c679 13767 else
96408a79 13768 {
24c5c679
JK
13769 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13770 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13771 if (parameter->u.dwarf_reg != -1)
13772 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13773 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13774 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13775 &parameter->u.fb_offset))
13776 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13777 else
13778 {
b98664d3 13779 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13780 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13781 "DW_TAG_call_site child DIE %s "
24c5c679 13782 "[in module %s]"),
9d8780f0 13783 sect_offset_str (child_die->sect_off),
9c541725 13784 objfile_name (objfile));
24c5c679
JK
13785 continue;
13786 }
96408a79
SA
13787 }
13788
216f72a1
JK
13789 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13790 if (attr == NULL)
13791 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13792 if (attr == NULL || !attr->form_is_block ())
96408a79 13793 {
b98664d3 13794 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13795 "DW_TAG_call_site child DIE %s [in module %s]"),
13796 sect_offset_str (child_die->sect_off),
9c541725 13797 objfile_name (objfile));
96408a79
SA
13798 continue;
13799 }
13800 parameter->value = DW_BLOCK (attr)->data;
13801 parameter->value_size = DW_BLOCK (attr)->size;
13802
13803 /* Parameters are not pre-cleared by memset above. */
13804 parameter->data_value = NULL;
13805 parameter->data_value_size = 0;
13806 call_site->parameter_count++;
13807
216f72a1
JK
13808 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13809 if (attr == NULL)
13810 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13811 if (attr != nullptr)
96408a79 13812 {
4fc6c0d5 13813 if (!attr->form_is_block ())
b98664d3 13814 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13815 "DW_TAG_call_site child DIE %s [in module %s]"),
13816 sect_offset_str (child_die->sect_off),
9c541725 13817 objfile_name (objfile));
96408a79
SA
13818 else
13819 {
13820 parameter->data_value = DW_BLOCK (attr)->data;
13821 parameter->data_value_size = DW_BLOCK (attr)->size;
13822 }
13823 }
13824 }
13825}
13826
71a3c369
TT
13827/* Helper function for read_variable. If DIE represents a virtual
13828 table, then return the type of the concrete object that is
13829 associated with the virtual table. Otherwise, return NULL. */
13830
13831static struct type *
13832rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13833{
13834 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13835 if (attr == NULL)
13836 return NULL;
13837
13838 /* Find the type DIE. */
13839 struct die_info *type_die = NULL;
13840 struct dwarf2_cu *type_cu = cu;
13841
cd6c91b4 13842 if (attr->form_is_ref ())
71a3c369
TT
13843 type_die = follow_die_ref (die, attr, &type_cu);
13844 if (type_die == NULL)
13845 return NULL;
13846
13847 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13848 return NULL;
13849 return die_containing_type (type_die, type_cu);
13850}
13851
13852/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13853
13854static void
13855read_variable (struct die_info *die, struct dwarf2_cu *cu)
13856{
13857 struct rust_vtable_symbol *storage = NULL;
13858
13859 if (cu->language == language_rust)
13860 {
13861 struct type *containing_type = rust_containing_type (die, cu);
13862
13863 if (containing_type != NULL)
13864 {
518817b3 13865 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13866
468c0cbb 13867 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13868 initialize_objfile_symbol (storage);
13869 storage->concrete_type = containing_type;
cf724bc9 13870 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13871 }
13872 }
13873
e4a62c65
TV
13874 struct symbol *res = new_symbol (die, NULL, cu, storage);
13875 struct attribute *abstract_origin
13876 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13877 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13878 if (res == NULL && loc && abstract_origin)
13879 {
13880 /* We have a variable without a name, but with a location and an abstract
13881 origin. This may be a concrete instance of an abstract variable
13882 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13883 later. */
13884 struct dwarf2_cu *origin_cu = cu;
13885 struct die_info *origin_die
13886 = follow_die_ref (die, abstract_origin, &origin_cu);
13887 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13888 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13889 }
71a3c369
TT
13890}
13891
43988095
JK
13892/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13893 reading .debug_rnglists.
13894 Callback's type should be:
13895 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13896 Return true if the attributes are present and valid, otherwise,
13897 return false. */
13898
13899template <typename Callback>
13900static bool
13901dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13902 Callback &&callback)
13903{
ed2dc618 13904 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13905 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13906 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13907 bfd *obfd = objfile->obfd;
43988095
JK
13908 /* Base address selection entry. */
13909 CORE_ADDR base;
13910 int found_base;
43988095 13911 const gdb_byte *buffer;
43988095
JK
13912 CORE_ADDR baseaddr;
13913 bool overflow = false;
13914
13915 found_base = cu->base_known;
13916 base = cu->base_address;
13917
96b79293 13918 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13919 if (offset >= dwarf2_per_objfile->rnglists.size)
13920 {
b98664d3 13921 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13922 offset);
13923 return false;
13924 }
13925 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13926
b3b3bada 13927 baseaddr = objfile->text_section_offset ();
43988095
JK
13928
13929 while (1)
13930 {
7814882a
JK
13931 /* Initialize it due to a false compiler warning. */
13932 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13933 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13934 + dwarf2_per_objfile->rnglists.size);
13935 unsigned int bytes_read;
13936
13937 if (buffer == buf_end)
13938 {
13939 overflow = true;
13940 break;
13941 }
13942 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13943 switch (rlet)
13944 {
13945 case DW_RLE_end_of_list:
13946 break;
13947 case DW_RLE_base_address:
13948 if (buffer + cu->header.addr_size > buf_end)
13949 {
13950 overflow = true;
13951 break;
13952 }
13953 base = read_address (obfd, buffer, cu, &bytes_read);
13954 found_base = 1;
13955 buffer += bytes_read;
13956 break;
13957 case DW_RLE_start_length:
13958 if (buffer + cu->header.addr_size > buf_end)
13959 {
13960 overflow = true;
13961 break;
13962 }
13963 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13964 buffer += bytes_read;
13965 range_end = (range_beginning
13966 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13967 buffer += bytes_read;
13968 if (buffer > buf_end)
13969 {
13970 overflow = true;
13971 break;
13972 }
13973 break;
13974 case DW_RLE_offset_pair:
13975 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13976 buffer += bytes_read;
13977 if (buffer > buf_end)
13978 {
13979 overflow = true;
13980 break;
13981 }
13982 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13983 buffer += bytes_read;
13984 if (buffer > buf_end)
13985 {
13986 overflow = true;
13987 break;
13988 }
13989 break;
13990 case DW_RLE_start_end:
13991 if (buffer + 2 * cu->header.addr_size > buf_end)
13992 {
13993 overflow = true;
13994 break;
13995 }
13996 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13997 buffer += bytes_read;
13998 range_end = read_address (obfd, buffer, cu, &bytes_read);
13999 buffer += bytes_read;
14000 break;
14001 default:
b98664d3 14002 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14003 return false;
14004 }
14005 if (rlet == DW_RLE_end_of_list || overflow)
14006 break;
14007 if (rlet == DW_RLE_base_address)
14008 continue;
14009
14010 if (!found_base)
14011 {
14012 /* We have no valid base address for the ranges
14013 data. */
b98664d3 14014 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14015 return false;
14016 }
14017
14018 if (range_beginning > range_end)
14019 {
14020 /* Inverted range entries are invalid. */
b98664d3 14021 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14022 return false;
14023 }
14024
14025 /* Empty range entries have no effect. */
14026 if (range_beginning == range_end)
14027 continue;
14028
14029 range_beginning += base;
14030 range_end += base;
14031
14032 /* A not-uncommon case of bad debug info.
14033 Don't pollute the addrmap with bad data. */
14034 if (range_beginning + baseaddr == 0
14035 && !dwarf2_per_objfile->has_section_at_zero)
14036 {
b98664d3 14037 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14038 " [in module %s]"), objfile_name (objfile));
14039 continue;
14040 }
14041
14042 callback (range_beginning, range_end);
14043 }
14044
14045 if (overflow)
14046 {
b98664d3 14047 complaint (_("Offset %d is not terminated "
43988095
JK
14048 "for DW_AT_ranges attribute"),
14049 offset);
14050 return false;
14051 }
14052
14053 return true;
14054}
14055
14056/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14057 Callback's type should be:
14058 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14059 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14060
43988095 14061template <typename Callback>
43039443 14062static int
5f46c5a5 14063dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14064 Callback &&callback)
43039443 14065{
ed2dc618 14066 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14067 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14068 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14069 struct comp_unit_head *cu_header = &cu->header;
14070 bfd *obfd = objfile->obfd;
14071 unsigned int addr_size = cu_header->addr_size;
14072 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14073 /* Base address selection entry. */
14074 CORE_ADDR base;
14075 int found_base;
14076 unsigned int dummy;
d521ce57 14077 const gdb_byte *buffer;
ff013f42 14078 CORE_ADDR baseaddr;
43039443 14079
43988095
JK
14080 if (cu_header->version >= 5)
14081 return dwarf2_rnglists_process (offset, cu, callback);
14082
d00adf39
DE
14083 found_base = cu->base_known;
14084 base = cu->base_address;
43039443 14085
96b79293 14086 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 14087 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14088 {
b98664d3 14089 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14090 offset);
14091 return 0;
14092 }
dce234bc 14093 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14094
b3b3bada 14095 baseaddr = objfile->text_section_offset ();
ff013f42 14096
43039443
JK
14097 while (1)
14098 {
14099 CORE_ADDR range_beginning, range_end;
14100
14101 range_beginning = read_address (obfd, buffer, cu, &dummy);
14102 buffer += addr_size;
14103 range_end = read_address (obfd, buffer, cu, &dummy);
14104 buffer += addr_size;
14105 offset += 2 * addr_size;
14106
14107 /* An end of list marker is a pair of zero addresses. */
14108 if (range_beginning == 0 && range_end == 0)
14109 /* Found the end of list entry. */
14110 break;
14111
14112 /* Each base address selection entry is a pair of 2 values.
14113 The first is the largest possible address, the second is
14114 the base address. Check for a base address here. */
14115 if ((range_beginning & mask) == mask)
14116 {
28d2bfb9
AB
14117 /* If we found the largest possible address, then we already
14118 have the base address in range_end. */
14119 base = range_end;
43039443
JK
14120 found_base = 1;
14121 continue;
14122 }
14123
14124 if (!found_base)
14125 {
14126 /* We have no valid base address for the ranges
14127 data. */
b98664d3 14128 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14129 return 0;
14130 }
14131
9277c30c
UW
14132 if (range_beginning > range_end)
14133 {
14134 /* Inverted range entries are invalid. */
b98664d3 14135 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14136 return 0;
14137 }
14138
14139 /* Empty range entries have no effect. */
14140 if (range_beginning == range_end)
14141 continue;
14142
43039443
JK
14143 range_beginning += base;
14144 range_end += base;
14145
01093045
DE
14146 /* A not-uncommon case of bad debug info.
14147 Don't pollute the addrmap with bad data. */
14148 if (range_beginning + baseaddr == 0
14149 && !dwarf2_per_objfile->has_section_at_zero)
14150 {
b98664d3 14151 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14152 " [in module %s]"), objfile_name (objfile));
01093045
DE
14153 continue;
14154 }
14155
5f46c5a5
JK
14156 callback (range_beginning, range_end);
14157 }
14158
14159 return 1;
14160}
14161
14162/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14163 Return 1 if the attributes are present and valid, otherwise, return 0.
14164 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14165
14166static int
14167dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14168 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14169 dwarf2_psymtab *ranges_pst)
5f46c5a5 14170{
518817b3 14171 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 14172 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 14173 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14174 int low_set = 0;
14175 CORE_ADDR low = 0;
14176 CORE_ADDR high = 0;
14177 int retval;
14178
14179 retval = dwarf2_ranges_process (offset, cu,
14180 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14181 {
9277c30c 14182 if (ranges_pst != NULL)
3e29f34a
MR
14183 {
14184 CORE_ADDR lowpc;
14185 CORE_ADDR highpc;
14186
79748972
TT
14187 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14188 range_beginning + baseaddr)
14189 - baseaddr);
14190 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14191 range_end + baseaddr)
14192 - baseaddr);
d320c2b5
TT
14193 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14194 lowpc, highpc - 1, ranges_pst);
3e29f34a 14195 }
ff013f42 14196
43039443
JK
14197 /* FIXME: This is recording everything as a low-high
14198 segment of consecutive addresses. We should have a
14199 data structure for discontiguous block ranges
14200 instead. */
14201 if (! low_set)
14202 {
14203 low = range_beginning;
14204 high = range_end;
14205 low_set = 1;
14206 }
14207 else
14208 {
14209 if (range_beginning < low)
14210 low = range_beginning;
14211 if (range_end > high)
14212 high = range_end;
14213 }
5f46c5a5
JK
14214 });
14215 if (!retval)
14216 return 0;
43039443
JK
14217
14218 if (! low_set)
14219 /* If the first entry is an end-of-list marker, the range
14220 describes an empty scope, i.e. no instructions. */
14221 return 0;
14222
14223 if (low_return)
14224 *low_return = low;
14225 if (high_return)
14226 *high_return = high;
14227 return 1;
14228}
14229
3a2b436a
JK
14230/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14231 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14232 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14233
3a2b436a 14234static enum pc_bounds_kind
af34e669 14235dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14236 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14237 dwarf2_psymtab *pst)
c906108c 14238{
518817b3
SM
14239 struct dwarf2_per_objfile *dwarf2_per_objfile
14240 = cu->per_cu->dwarf2_per_objfile;
c906108c 14241 struct attribute *attr;
91da1414 14242 struct attribute *attr_high;
af34e669
DJ
14243 CORE_ADDR low = 0;
14244 CORE_ADDR high = 0;
e385593e 14245 enum pc_bounds_kind ret;
c906108c 14246
91da1414
MW
14247 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14248 if (attr_high)
af34e669 14249 {
e142c38c 14250 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14251 if (attr != nullptr)
91da1414 14252 {
cd6c91b4
TT
14253 low = attr->value_as_address ();
14254 high = attr_high->value_as_address ();
14255 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14256 high += low;
91da1414 14257 }
af34e669
DJ
14258 else
14259 /* Found high w/o low attribute. */
e385593e 14260 return PC_BOUNDS_INVALID;
af34e669
DJ
14261
14262 /* Found consecutive range of addresses. */
3a2b436a 14263 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14264 }
c906108c 14265 else
af34e669 14266 {
e142c38c 14267 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14268 if (attr != NULL)
14269 {
18a8505e 14270 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14271 We take advantage of the fact that DW_AT_ranges does not appear
14272 in DW_TAG_compile_unit of DWO files. */
14273 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14274 unsigned int ranges_offset = (DW_UNSND (attr)
14275 + (need_ranges_base
14276 ? cu->ranges_base
14277 : 0));
2e3cf129 14278
af34e669 14279 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14280 .debug_ranges section. */
2e3cf129 14281 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14282 return PC_BOUNDS_INVALID;
43039443 14283 /* Found discontinuous range of addresses. */
3a2b436a 14284 ret = PC_BOUNDS_RANGES;
af34e669 14285 }
e385593e
JK
14286 else
14287 return PC_BOUNDS_NOT_PRESENT;
af34e669 14288 }
c906108c 14289
48fbe735 14290 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14291 if (high <= low)
e385593e 14292 return PC_BOUNDS_INVALID;
c906108c
SS
14293
14294 /* When using the GNU linker, .gnu.linkonce. sections are used to
14295 eliminate duplicate copies of functions and vtables and such.
14296 The linker will arbitrarily choose one and discard the others.
14297 The AT_*_pc values for such functions refer to local labels in
14298 these sections. If the section from that file was discarded, the
14299 labels are not in the output, so the relocs get a value of 0.
14300 If this is a discarded function, mark the pc bounds as invalid,
14301 so that GDB will ignore it. */
72dca2f5 14302 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14303 return PC_BOUNDS_INVALID;
c906108c
SS
14304
14305 *lowpc = low;
96408a79
SA
14306 if (highpc)
14307 *highpc = high;
af34e669 14308 return ret;
c906108c
SS
14309}
14310
b084d499
JB
14311/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14312 its low and high PC addresses. Do nothing if these addresses could not
14313 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14314 and HIGHPC to the high address if greater than HIGHPC. */
14315
14316static void
14317dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14318 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14319 struct dwarf2_cu *cu)
14320{
14321 CORE_ADDR low, high;
14322 struct die_info *child = die->child;
14323
e385593e 14324 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14325 {
325fac50
PA
14326 *lowpc = std::min (*lowpc, low);
14327 *highpc = std::max (*highpc, high);
b084d499
JB
14328 }
14329
14330 /* If the language does not allow nested subprograms (either inside
14331 subprograms or lexical blocks), we're done. */
14332 if (cu->language != language_ada)
14333 return;
6e70227d 14334
b084d499
JB
14335 /* Check all the children of the given DIE. If it contains nested
14336 subprograms, then check their pc bounds. Likewise, we need to
14337 check lexical blocks as well, as they may also contain subprogram
14338 definitions. */
14339 while (child && child->tag)
14340 {
14341 if (child->tag == DW_TAG_subprogram
14342 || child->tag == DW_TAG_lexical_block)
14343 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14344 child = sibling_die (child);
14345 }
14346}
14347
fae299cd
DC
14348/* Get the low and high pc's represented by the scope DIE, and store
14349 them in *LOWPC and *HIGHPC. If the correct values can't be
14350 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14351
14352static void
14353get_scope_pc_bounds (struct die_info *die,
14354 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14355 struct dwarf2_cu *cu)
14356{
14357 CORE_ADDR best_low = (CORE_ADDR) -1;
14358 CORE_ADDR best_high = (CORE_ADDR) 0;
14359 CORE_ADDR current_low, current_high;
14360
3a2b436a 14361 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14362 >= PC_BOUNDS_RANGES)
fae299cd
DC
14363 {
14364 best_low = current_low;
14365 best_high = current_high;
14366 }
14367 else
14368 {
14369 struct die_info *child = die->child;
14370
14371 while (child && child->tag)
14372 {
14373 switch (child->tag) {
14374 case DW_TAG_subprogram:
b084d499 14375 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14376 break;
14377 case DW_TAG_namespace:
f55ee35c 14378 case DW_TAG_module:
fae299cd
DC
14379 /* FIXME: carlton/2004-01-16: Should we do this for
14380 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14381 that current GCC's always emit the DIEs corresponding
14382 to definitions of methods of classes as children of a
14383 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14384 the DIEs giving the declarations, which could be
14385 anywhere). But I don't see any reason why the
14386 standards says that they have to be there. */
14387 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14388
14389 if (current_low != ((CORE_ADDR) -1))
14390 {
325fac50
PA
14391 best_low = std::min (best_low, current_low);
14392 best_high = std::max (best_high, current_high);
fae299cd
DC
14393 }
14394 break;
14395 default:
0963b4bd 14396 /* Ignore. */
fae299cd
DC
14397 break;
14398 }
14399
14400 child = sibling_die (child);
14401 }
14402 }
14403
14404 *lowpc = best_low;
14405 *highpc = best_high;
14406}
14407
801e3a5b
JB
14408/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14409 in DIE. */
380bca97 14410
801e3a5b
JB
14411static void
14412dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14413 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14414{
518817b3 14415 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14416 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14417 struct attribute *attr;
91da1414 14418 struct attribute *attr_high;
801e3a5b 14419
91da1414
MW
14420 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14421 if (attr_high)
801e3a5b 14422 {
801e3a5b 14423 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14424 if (attr != nullptr)
801e3a5b 14425 {
cd6c91b4
TT
14426 CORE_ADDR low = attr->value_as_address ();
14427 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14428
cd6c91b4 14429 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14430 high += low;
9a619af0 14431
3e29f34a
MR
14432 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14433 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14434 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14435 }
14436 }
14437
14438 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14439 if (attr != nullptr)
801e3a5b 14440 {
18a8505e 14441 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14442 We take advantage of the fact that DW_AT_ranges does not appear
14443 in DW_TAG_compile_unit of DWO files. */
14444 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14445
14446 /* The value of the DW_AT_ranges attribute is the offset of the
14447 address range list in the .debug_ranges section. */
ab435259
DE
14448 unsigned long offset = (DW_UNSND (attr)
14449 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14450
2d5f09ec 14451 std::vector<blockrange> blockvec;
5f46c5a5
JK
14452 dwarf2_ranges_process (offset, cu,
14453 [&] (CORE_ADDR start, CORE_ADDR end)
14454 {
58fdfd2c
JK
14455 start += baseaddr;
14456 end += baseaddr;
5f46c5a5
JK
14457 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14458 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14459 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14460 blockvec.emplace_back (start, end);
5f46c5a5 14461 });
2d5f09ec
KB
14462
14463 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14464 }
14465}
14466
685b1105
JK
14467/* Check whether the producer field indicates either of GCC < 4.6, or the
14468 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14469
685b1105
JK
14470static void
14471check_producer (struct dwarf2_cu *cu)
60d5a603 14472{
38360086 14473 int major, minor;
60d5a603
JK
14474
14475 if (cu->producer == NULL)
14476 {
14477 /* For unknown compilers expect their behavior is DWARF version
14478 compliant.
14479
14480 GCC started to support .debug_types sections by -gdwarf-4 since
14481 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14482 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14483 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14484 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14485 }
b1ffba5a 14486 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14487 {
38360086
MW
14488 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14489 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14490 }
5230b05a 14491 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14492 {
14493 cu->producer_is_icc = true;
14494 cu->producer_is_icc_lt_14 = major < 14;
14495 }
c258c396
JD
14496 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14497 cu->producer_is_codewarrior = true;
685b1105
JK
14498 else
14499 {
14500 /* For other non-GCC compilers, expect their behavior is DWARF version
14501 compliant. */
60d5a603
JK
14502 }
14503
9068261f 14504 cu->checked_producer = true;
685b1105 14505}
ba919b58 14506
685b1105
JK
14507/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14508 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14509 during 4.6.0 experimental. */
14510
9068261f 14511static bool
685b1105
JK
14512producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14513{
14514 if (!cu->checked_producer)
14515 check_producer (cu);
14516
14517 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14518}
14519
c258c396
JD
14520
14521/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14522 with incorrect is_stmt attributes. */
14523
14524static bool
14525producer_is_codewarrior (struct dwarf2_cu *cu)
14526{
14527 if (!cu->checked_producer)
14528 check_producer (cu);
14529
14530 return cu->producer_is_codewarrior;
14531}
14532
405feb71 14533/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14534 DW_AT_accessibility. */
14535
14536static enum dwarf_access_attribute
14537dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14538{
14539 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14540 {
14541 /* The default DWARF 2 accessibility for members is public, the default
14542 accessibility for inheritance is private. */
14543
14544 if (die->tag != DW_TAG_inheritance)
14545 return DW_ACCESS_public;
14546 else
14547 return DW_ACCESS_private;
14548 }
14549 else
14550 {
14551 /* DWARF 3+ defines the default accessibility a different way. The same
14552 rules apply now for DW_TAG_inheritance as for the members and it only
14553 depends on the container kind. */
14554
14555 if (die->parent->tag == DW_TAG_class_type)
14556 return DW_ACCESS_private;
14557 else
14558 return DW_ACCESS_public;
14559 }
14560}
14561
74ac6d43
TT
14562/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14563 offset. If the attribute was not found return 0, otherwise return
14564 1. If it was found but could not properly be handled, set *OFFSET
14565 to 0. */
14566
14567static int
14568handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14569 LONGEST *offset)
14570{
14571 struct attribute *attr;
14572
14573 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14574 if (attr != NULL)
14575 {
14576 *offset = 0;
14577
14578 /* Note that we do not check for a section offset first here.
14579 This is because DW_AT_data_member_location is new in DWARF 4,
14580 so if we see it, we can assume that a constant form is really
14581 a constant and not a section offset. */
cd6c91b4 14582 if (attr->form_is_constant ())
74ac6d43 14583 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14584 else if (attr->form_is_section_offset ())
74ac6d43 14585 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14586 else if (attr->form_is_block ())
74ac6d43
TT
14587 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14588 else
14589 dwarf2_complex_location_expr_complaint ();
14590
14591 return 1;
14592 }
14593
14594 return 0;
14595}
14596
c906108c
SS
14597/* Add an aggregate field to the field list. */
14598
14599static void
107d2387 14600dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14601 struct dwarf2_cu *cu)
6e70227d 14602{
518817b3 14603 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14605 struct nextfield *new_field;
14606 struct attribute *attr;
14607 struct field *fp;
15d034d0 14608 const char *fieldname = "";
c906108c 14609
7d0ccb61
DJ
14610 if (die->tag == DW_TAG_inheritance)
14611 {
be2daae6
TT
14612 fip->baseclasses.emplace_back ();
14613 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14614 }
14615 else
14616 {
be2daae6
TT
14617 fip->fields.emplace_back ();
14618 new_field = &fip->fields.back ();
7d0ccb61 14619 }
be2daae6 14620
c906108c
SS
14621 fip->nfields++;
14622
e142c38c 14623 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14624 if (attr != nullptr)
c906108c 14625 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14626 else
14627 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14628 if (new_field->accessibility != DW_ACCESS_public)
14629 fip->non_public_fields = 1;
60d5a603 14630
e142c38c 14631 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14632 if (attr != nullptr)
c906108c 14633 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14634 else
14635 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14636
14637 fp = &new_field->field;
a9a9bd0f 14638
e142c38c 14639 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14640 {
74ac6d43
TT
14641 LONGEST offset;
14642
a9a9bd0f 14643 /* Data member other than a C++ static data member. */
6e70227d 14644
c906108c 14645 /* Get type of field. */
e7c27a73 14646 fp->type = die_type (die, cu);
c906108c 14647
d6a843b5 14648 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14649
c906108c 14650 /* Get bit size of field (zero if none). */
e142c38c 14651 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14652 if (attr != nullptr)
c906108c
SS
14653 {
14654 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14655 }
14656 else
14657 {
14658 FIELD_BITSIZE (*fp) = 0;
14659 }
14660
14661 /* Get bit offset of field. */
74ac6d43
TT
14662 if (handle_data_member_location (die, cu, &offset))
14663 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14664 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14665 if (attr != nullptr)
c906108c 14666 {
d5a22e77 14667 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14668 {
14669 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14670 additional bit offset from the MSB of the containing
14671 anonymous object to the MSB of the field. We don't
14672 have to do anything special since we don't need to
14673 know the size of the anonymous object. */
f41f5e61 14674 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14675 }
14676 else
14677 {
14678 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14679 MSB of the anonymous object, subtract off the number of
14680 bits from the MSB of the field to the MSB of the
14681 object, and then subtract off the number of bits of
14682 the field itself. The result is the bit offset of
14683 the LSB of the field. */
c906108c
SS
14684 int anonymous_size;
14685 int bit_offset = DW_UNSND (attr);
14686
e142c38c 14687 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14688 if (attr != nullptr)
c906108c
SS
14689 {
14690 /* The size of the anonymous object containing
14691 the bit field is explicit, so use the
14692 indicated size (in bytes). */
14693 anonymous_size = DW_UNSND (attr);
14694 }
14695 else
14696 {
14697 /* The size of the anonymous object containing
14698 the bit field must be inferred from the type
14699 attribute of the data member containing the
14700 bit field. */
14701 anonymous_size = TYPE_LENGTH (fp->type);
14702 }
f41f5e61
PA
14703 SET_FIELD_BITPOS (*fp,
14704 (FIELD_BITPOS (*fp)
14705 + anonymous_size * bits_per_byte
14706 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14707 }
14708 }
da5b30da
AA
14709 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14710 if (attr != NULL)
14711 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14712 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14713
14714 /* Get name of field. */
39cbfefa
DJ
14715 fieldname = dwarf2_name (die, cu);
14716 if (fieldname == NULL)
14717 fieldname = "";
d8151005
DJ
14718
14719 /* The name is already allocated along with this objfile, so we don't
14720 need to duplicate it for the type. */
14721 fp->name = fieldname;
c906108c
SS
14722
14723 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14724 pointer or virtual base class pointer) to private. */
e142c38c 14725 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14726 {
d48cc9dd 14727 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14728 new_field->accessibility = DW_ACCESS_private;
14729 fip->non_public_fields = 1;
14730 }
14731 }
a9a9bd0f 14732 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14733 {
a9a9bd0f
DC
14734 /* C++ static member. */
14735
14736 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14737 is a declaration, but all versions of G++ as of this writing
14738 (so through at least 3.2.1) incorrectly generate
14739 DW_TAG_variable tags. */
6e70227d 14740
ff355380 14741 const char *physname;
c906108c 14742
a9a9bd0f 14743 /* Get name of field. */
39cbfefa
DJ
14744 fieldname = dwarf2_name (die, cu);
14745 if (fieldname == NULL)
c906108c
SS
14746 return;
14747
254e6b9e 14748 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14749 if (attr
14750 /* Only create a symbol if this is an external value.
14751 new_symbol checks this and puts the value in the global symbol
14752 table, which we want. If it is not external, new_symbol
14753 will try to put the value in cu->list_in_scope which is wrong. */
14754 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14755 {
14756 /* A static const member, not much different than an enum as far as
14757 we're concerned, except that we can support more types. */
14758 new_symbol (die, NULL, cu);
14759 }
14760
2df3850c 14761 /* Get physical name. */
ff355380 14762 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14763
d8151005
DJ
14764 /* The name is already allocated along with this objfile, so we don't
14765 need to duplicate it for the type. */
14766 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14767 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14768 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14769 }
14770 else if (die->tag == DW_TAG_inheritance)
14771 {
74ac6d43 14772 LONGEST offset;
d4b96c9a 14773
74ac6d43
TT
14774 /* C++ base class field. */
14775 if (handle_data_member_location (die, cu, &offset))
14776 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14777 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14778 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14779 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14780 }
2ddeaf8a
TT
14781 else if (die->tag == DW_TAG_variant_part)
14782 {
14783 /* process_structure_scope will treat this DIE as a union. */
14784 process_structure_scope (die, cu);
14785
14786 /* The variant part is relative to the start of the enclosing
14787 structure. */
14788 SET_FIELD_BITPOS (*fp, 0);
14789 fp->type = get_die_type (die, cu);
14790 fp->artificial = 1;
14791 fp->name = "<<variant>>";
c8c81635
TT
14792
14793 /* Normally a DW_TAG_variant_part won't have a size, but our
14794 representation requires one, so set it to the maximum of the
489dbda6
TT
14795 child sizes, being sure to account for the offset at which
14796 each child is seen. */
c8c81635
TT
14797 if (TYPE_LENGTH (fp->type) == 0)
14798 {
14799 unsigned max = 0;
14800 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14801 {
14802 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14803 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14804 if (len > max)
14805 max = len;
14806 }
c8c81635
TT
14807 TYPE_LENGTH (fp->type) = max;
14808 }
2ddeaf8a
TT
14809 }
14810 else
14811 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14812}
14813
883fd55a
KS
14814/* Can the type given by DIE define another type? */
14815
14816static bool
14817type_can_define_types (const struct die_info *die)
14818{
14819 switch (die->tag)
14820 {
14821 case DW_TAG_typedef:
14822 case DW_TAG_class_type:
14823 case DW_TAG_structure_type:
14824 case DW_TAG_union_type:
14825 case DW_TAG_enumeration_type:
14826 return true;
14827
14828 default:
14829 return false;
14830 }
14831}
14832
14833/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14834
14835static void
883fd55a
KS
14836dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14837 struct dwarf2_cu *cu)
6e70227d 14838{
be2daae6
TT
14839 struct decl_field fp;
14840 memset (&fp, 0, sizeof (fp));
98751a41 14841
883fd55a 14842 gdb_assert (type_can_define_types (die));
98751a41 14843
883fd55a 14844 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14845 fp.name = dwarf2_name (die, cu);
14846 fp.type = read_type_die (die, cu);
98751a41 14847
c191a687
KS
14848 /* Save accessibility. */
14849 enum dwarf_access_attribute accessibility;
14850 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14851 if (attr != NULL)
14852 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14853 else
14854 accessibility = dwarf2_default_access_attribute (die, cu);
14855 switch (accessibility)
14856 {
14857 case DW_ACCESS_public:
14858 /* The assumed value if neither private nor protected. */
14859 break;
14860 case DW_ACCESS_private:
be2daae6 14861 fp.is_private = 1;
c191a687
KS
14862 break;
14863 case DW_ACCESS_protected:
be2daae6 14864 fp.is_protected = 1;
c191a687
KS
14865 break;
14866 default:
b98664d3 14867 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14868 }
14869
883fd55a 14870 if (die->tag == DW_TAG_typedef)
be2daae6 14871 fip->typedef_field_list.push_back (fp);
883fd55a 14872 else
be2daae6 14873 fip->nested_types_list.push_back (fp);
98751a41
JK
14874}
14875
c906108c
SS
14876/* Create the vector of fields, and attach it to the type. */
14877
14878static void
fba45db2 14879dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14880 struct dwarf2_cu *cu)
c906108c
SS
14881{
14882 int nfields = fip->nfields;
14883
14884 /* Record the field count, allocate space for the array of fields,
14885 and create blank accessibility bitfields if necessary. */
14886 TYPE_NFIELDS (type) = nfields;
14887 TYPE_FIELDS (type) = (struct field *)
be2daae6 14888 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14889
b4ba55a1 14890 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14891 {
14892 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14893
14894 TYPE_FIELD_PRIVATE_BITS (type) =
14895 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14896 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14897
14898 TYPE_FIELD_PROTECTED_BITS (type) =
14899 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14900 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14901
774b6a14
TT
14902 TYPE_FIELD_IGNORE_BITS (type) =
14903 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14904 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14905 }
14906
14907 /* If the type has baseclasses, allocate and clear a bit vector for
14908 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14909 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14910 {
be2daae6 14911 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14912 unsigned char *pointer;
c906108c
SS
14913
14914 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14915 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14916 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14917 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14918 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14919 }
14920
2ddeaf8a
TT
14921 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14922 {
14923 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14924
be2daae6 14925 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14926 {
be2daae6
TT
14927 struct nextfield &field = fip->fields[index];
14928
14929 if (field.variant.is_discriminant)
2ddeaf8a 14930 di->discriminant_index = index;
be2daae6 14931 else if (field.variant.default_branch)
2ddeaf8a
TT
14932 di->default_index = index;
14933 else
be2daae6 14934 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14935 }
14936 }
14937
be2daae6
TT
14938 /* Copy the saved-up fields into the field vector. */
14939 for (int i = 0; i < nfields; ++i)
c906108c 14940 {
be2daae6
TT
14941 struct nextfield &field
14942 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14943 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14944
be2daae6
TT
14945 TYPE_FIELD (type, i) = field.field;
14946 switch (field.accessibility)
c906108c 14947 {
c5aa993b 14948 case DW_ACCESS_private:
b4ba55a1 14949 if (cu->language != language_ada)
be2daae6 14950 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14951 break;
c906108c 14952
c5aa993b 14953 case DW_ACCESS_protected:
b4ba55a1 14954 if (cu->language != language_ada)
be2daae6 14955 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14956 break;
c906108c 14957
c5aa993b
JM
14958 case DW_ACCESS_public:
14959 break;
c906108c 14960
c5aa993b
JM
14961 default:
14962 /* Unknown accessibility. Complain and treat it as public. */
14963 {
b98664d3 14964 complaint (_("unsupported accessibility %d"),
be2daae6 14965 field.accessibility);
c5aa993b
JM
14966 }
14967 break;
c906108c 14968 }
be2daae6 14969 if (i < fip->baseclasses.size ())
c906108c 14970 {
be2daae6 14971 switch (field.virtuality)
c906108c 14972 {
c5aa993b
JM
14973 case DW_VIRTUALITY_virtual:
14974 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14975 if (cu->language == language_ada)
a73c6dcd 14976 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14977 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14978 break;
c906108c
SS
14979 }
14980 }
c906108c
SS
14981 }
14982}
14983
7d27a96d
TT
14984/* Return true if this member function is a constructor, false
14985 otherwise. */
14986
14987static int
14988dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14989{
14990 const char *fieldname;
fe978cb0 14991 const char *type_name;
7d27a96d
TT
14992 int len;
14993
14994 if (die->parent == NULL)
14995 return 0;
14996
14997 if (die->parent->tag != DW_TAG_structure_type
14998 && die->parent->tag != DW_TAG_union_type
14999 && die->parent->tag != DW_TAG_class_type)
15000 return 0;
15001
15002 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15003 type_name = dwarf2_name (die->parent, cu);
15004 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15005 return 0;
15006
15007 len = strlen (fieldname);
fe978cb0
PA
15008 return (strncmp (fieldname, type_name, len) == 0
15009 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15010}
15011
e35000a7
TBA
15012/* Check if the given VALUE is a recognized enum
15013 dwarf_defaulted_attribute constant according to DWARF5 spec,
15014 Table 7.24. */
15015
15016static bool
15017is_valid_DW_AT_defaulted (ULONGEST value)
15018{
15019 switch (value)
15020 {
15021 case DW_DEFAULTED_no:
15022 case DW_DEFAULTED_in_class:
15023 case DW_DEFAULTED_out_of_class:
15024 return true;
15025 }
15026
3142e908 15027 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15028 return false;
15029}
15030
c906108c
SS
15031/* Add a member function to the proper fieldlist. */
15032
15033static void
107d2387 15034dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15035 struct type *type, struct dwarf2_cu *cu)
c906108c 15036{
518817b3 15037 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15038 struct attribute *attr;
c906108c 15039 int i;
be2daae6 15040 struct fnfieldlist *flp = nullptr;
c906108c 15041 struct fn_field *fnp;
15d034d0 15042 const char *fieldname;
f792889a 15043 struct type *this_type;
60d5a603 15044 enum dwarf_access_attribute accessibility;
c906108c 15045
b4ba55a1 15046 if (cu->language == language_ada)
a73c6dcd 15047 error (_("unexpected member function in Ada type"));
b4ba55a1 15048
2df3850c 15049 /* Get name of member function. */
39cbfefa
DJ
15050 fieldname = dwarf2_name (die, cu);
15051 if (fieldname == NULL)
2df3850c 15052 return;
c906108c 15053
c906108c 15054 /* Look up member function name in fieldlist. */
be2daae6 15055 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15056 {
27bfe10e 15057 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15058 {
15059 flp = &fip->fnfieldlists[i];
15060 break;
15061 }
c906108c
SS
15062 }
15063
be2daae6
TT
15064 /* Create a new fnfieldlist if necessary. */
15065 if (flp == nullptr)
c906108c 15066 {
be2daae6
TT
15067 fip->fnfieldlists.emplace_back ();
15068 flp = &fip->fnfieldlists.back ();
c906108c 15069 flp->name = fieldname;
be2daae6 15070 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15071 }
15072
be2daae6
TT
15073 /* Create a new member function field and add it to the vector of
15074 fnfieldlists. */
15075 flp->fnfields.emplace_back ();
15076 fnp = &flp->fnfields.back ();
3da10d80
KS
15077
15078 /* Delay processing of the physname until later. */
9c37b5ae 15079 if (cu->language == language_cplus)
be2daae6
TT
15080 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15081 die, cu);
3da10d80
KS
15082 else
15083 {
1d06ead6 15084 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15085 fnp->physname = physname ? physname : "";
15086 }
15087
c906108c 15088 fnp->type = alloc_type (objfile);
f792889a
DJ
15089 this_type = read_type_die (die, cu);
15090 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15091 {
f792889a 15092 int nparams = TYPE_NFIELDS (this_type);
c906108c 15093
f792889a 15094 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15095 of the method itself (TYPE_CODE_METHOD). */
15096 smash_to_method_type (fnp->type, type,
f792889a
DJ
15097 TYPE_TARGET_TYPE (this_type),
15098 TYPE_FIELDS (this_type),
15099 TYPE_NFIELDS (this_type),
15100 TYPE_VARARGS (this_type));
c906108c
SS
15101
15102 /* Handle static member functions.
c5aa993b 15103 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15104 member functions. G++ helps GDB by marking the first
15105 parameter for non-static member functions (which is the this
15106 pointer) as artificial. We obtain this information from
15107 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15108 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15109 fnp->voffset = VOFFSET_STATIC;
15110 }
15111 else
b98664d3 15112 complaint (_("member function type missing for '%s'"),
3da10d80 15113 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15114
15115 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15116 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15117 fnp->fcontext = die_containing_type (die, cu);
c906108c 15118
3e43a32a
MS
15119 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15120 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15121
15122 /* Get accessibility. */
e142c38c 15123 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15124 if (attr != nullptr)
aead7601 15125 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15126 else
15127 accessibility = dwarf2_default_access_attribute (die, cu);
15128 switch (accessibility)
c906108c 15129 {
60d5a603
JK
15130 case DW_ACCESS_private:
15131 fnp->is_private = 1;
15132 break;
15133 case DW_ACCESS_protected:
15134 fnp->is_protected = 1;
15135 break;
c906108c
SS
15136 }
15137
b02dede2 15138 /* Check for artificial methods. */
e142c38c 15139 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15140 if (attr && DW_UNSND (attr) != 0)
15141 fnp->is_artificial = 1;
15142
e35000a7
TBA
15143 /* Check for defaulted methods. */
15144 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15145 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15146 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15147
15148 /* Check for deleted methods. */
15149 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15150 if (attr != nullptr && DW_UNSND (attr) != 0)
15151 fnp->is_deleted = 1;
15152
7d27a96d
TT
15153 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15154
0d564a31 15155 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15156 function. For older versions of GCC, this is an offset in the
15157 appropriate virtual table, as specified by DW_AT_containing_type.
15158 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15159 to the object address. */
15160
e142c38c 15161 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15162 if (attr != nullptr)
8e19ed76 15163 {
4fc6c0d5 15164 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15165 {
aec5aa8b
TT
15166 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15167 {
15168 /* Old-style GCC. */
15169 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15170 }
15171 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15172 || (DW_BLOCK (attr)->size > 1
15173 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15174 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15175 {
aec5aa8b
TT
15176 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15177 if ((fnp->voffset % cu->header.addr_size) != 0)
15178 dwarf2_complex_location_expr_complaint ();
15179 else
15180 fnp->voffset /= cu->header.addr_size;
15181 fnp->voffset += 2;
15182 }
15183 else
15184 dwarf2_complex_location_expr_complaint ();
15185
15186 if (!fnp->fcontext)
7e993ebf
KS
15187 {
15188 /* If there is no `this' field and no DW_AT_containing_type,
15189 we cannot actually find a base class context for the
15190 vtable! */
15191 if (TYPE_NFIELDS (this_type) == 0
15192 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15193 {
b98664d3 15194 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15195 "function \"%s\" (offset %s)"),
15196 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15197 }
15198 else
15199 {
15200 fnp->fcontext
15201 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15202 }
15203 }
aec5aa8b 15204 }
cd6c91b4 15205 else if (attr->form_is_section_offset ())
8e19ed76 15206 {
4d3c2250 15207 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15208 }
15209 else
15210 {
4d3c2250
KB
15211 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15212 fieldname);
8e19ed76 15213 }
0d564a31 15214 }
d48cc9dd
DJ
15215 else
15216 {
15217 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15218 if (attr && DW_UNSND (attr))
15219 {
15220 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15221 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15222 "but the vtable offset is not specified"),
9d8780f0 15223 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15224 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15225 TYPE_CPLUS_DYNAMIC (type) = 1;
15226 }
15227 }
c906108c
SS
15228}
15229
15230/* Create the vector of member function fields, and attach it to the type. */
15231
15232static void
fba45db2 15233dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15234 struct dwarf2_cu *cu)
c906108c 15235{
b4ba55a1 15236 if (cu->language == language_ada)
a73c6dcd 15237 error (_("unexpected member functions in Ada type"));
b4ba55a1 15238
c906108c
SS
15239 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15240 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15241 TYPE_ALLOC (type,
15242 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15243
be2daae6 15244 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15245 {
be2daae6 15246 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15247 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15248
be2daae6
TT
15249 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15250 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15251 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15252 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15253
15254 for (int k = 0; k < nf.fnfields.size (); ++k)
15255 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15256 }
15257
be2daae6 15258 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15259}
15260
1168df01
JB
15261/* Returns non-zero if NAME is the name of a vtable member in CU's
15262 language, zero otherwise. */
15263static int
15264is_vtable_name (const char *name, struct dwarf2_cu *cu)
15265{
15266 static const char vptr[] = "_vptr";
15267
9c37b5ae
TT
15268 /* Look for the C++ form of the vtable. */
15269 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15270 return 1;
15271
15272 return 0;
15273}
15274
c0dd20ea 15275/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15276 functions, with the ABI-specified layout. If TYPE describes
15277 such a structure, smash it into a member function type.
61049d3b
DJ
15278
15279 GCC shouldn't do this; it should just output pointer to member DIEs.
15280 This is GCC PR debug/28767. */
c0dd20ea 15281
0b92b5bb
TT
15282static void
15283quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15284{
09e2d7c7 15285 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15286
15287 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15288 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15289 return;
c0dd20ea
DJ
15290
15291 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15292 if (TYPE_FIELD_NAME (type, 0) == NULL
15293 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15294 || TYPE_FIELD_NAME (type, 1) == NULL
15295 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15296 return;
c0dd20ea
DJ
15297
15298 /* Find the type of the method. */
0b92b5bb 15299 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15300 if (pfn_type == NULL
15301 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15302 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15303 return;
c0dd20ea
DJ
15304
15305 /* Look for the "this" argument. */
15306 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15307 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15308 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15309 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15310 return;
c0dd20ea 15311
09e2d7c7 15312 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15313 new_type = alloc_type (objfile);
09e2d7c7 15314 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15315 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15316 TYPE_VARARGS (pfn_type));
0b92b5bb 15317 smash_to_methodptr_type (type, new_type);
c0dd20ea 15318}
1168df01 15319
2b4424c3
TT
15320/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15321 appropriate error checking and issuing complaints if there is a
15322 problem. */
15323
15324static ULONGEST
15325get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15326{
15327 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15328
15329 if (attr == nullptr)
15330 return 0;
15331
cd6c91b4 15332 if (!attr->form_is_constant ())
2b4424c3 15333 {
b98664d3 15334 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15335 " - DIE at %s [in module %s]"),
15336 sect_offset_str (die->sect_off),
15337 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15338 return 0;
15339 }
15340
15341 ULONGEST align;
15342 if (attr->form == DW_FORM_sdata)
15343 {
15344 LONGEST val = DW_SND (attr);
15345 if (val < 0)
15346 {
b98664d3 15347 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15348 " - DIE at %s [in module %s]"),
15349 sect_offset_str (die->sect_off),
15350 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15351 return 0;
15352 }
15353 align = val;
15354 }
15355 else
15356 align = DW_UNSND (attr);
15357
15358 if (align == 0)
15359 {
b98664d3 15360 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15361 " - DIE at %s [in module %s]"),
15362 sect_offset_str (die->sect_off),
15363 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15364 return 0;
15365 }
15366 if ((align & (align - 1)) != 0)
15367 {
b98664d3 15368 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15369 " - DIE at %s [in module %s]"),
15370 sect_offset_str (die->sect_off),
15371 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15372 return 0;
15373 }
15374
15375 return align;
15376}
15377
15378/* If the DIE has a DW_AT_alignment attribute, use its value to set
15379 the alignment for TYPE. */
15380
15381static void
15382maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15383 struct type *type)
15384{
15385 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15386 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15387 " - DIE at %s [in module %s]"),
15388 sect_offset_str (die->sect_off),
15389 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15390}
685b1105 15391
e35000a7
TBA
15392/* Check if the given VALUE is a valid enum dwarf_calling_convention
15393 constant for a type, according to DWARF5 spec, Table 5.5. */
15394
15395static bool
15396is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15397{
15398 switch (value)
15399 {
15400 case DW_CC_normal:
15401 case DW_CC_pass_by_reference:
15402 case DW_CC_pass_by_value:
15403 return true;
15404
15405 default:
15406 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15407 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15408 return false;
15409 }
15410}
15411
d0922fcf
TBA
15412/* Check if the given VALUE is a valid enum dwarf_calling_convention
15413 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15414 also according to GNU-specific values (see include/dwarf2.h). */
15415
15416static bool
15417is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15418{
15419 switch (value)
15420 {
15421 case DW_CC_normal:
15422 case DW_CC_program:
15423 case DW_CC_nocall:
15424 return true;
15425
15426 case DW_CC_GNU_renesas_sh:
15427 case DW_CC_GNU_borland_fastcall_i386:
15428 case DW_CC_GDB_IBM_OpenCL:
15429 return true;
15430
15431 default:
15432 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15433 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15434 return false;
15435 }
15436}
15437
c906108c 15438/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15439 (definition) to create a type for the structure or union. Fill in
15440 the type's name and general properties; the members will not be
83655187
DE
15441 processed until process_structure_scope. A symbol table entry for
15442 the type will also not be done until process_structure_scope (assuming
15443 the type has a name).
c906108c 15444
c767944b
DJ
15445 NOTE: we need to call these functions regardless of whether or not the
15446 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15447 structure or union. This gets the type entered into our set of
83655187 15448 user defined types. */
c906108c 15449
f792889a 15450static struct type *
134d01f1 15451read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15452{
518817b3 15453 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15454 struct type *type;
15455 struct attribute *attr;
15d034d0 15456 const char *name;
c906108c 15457
348e048f
DE
15458 /* If the definition of this type lives in .debug_types, read that type.
15459 Don't follow DW_AT_specification though, that will take us back up
15460 the chain and we want to go down. */
45e58e77 15461 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15462 if (attr != nullptr)
348e048f 15463 {
ac9ec31b 15464 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15465
ac9ec31b 15466 /* The type's CU may not be the same as CU.
02142a6c 15467 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15468 return set_die_type (die, type, cu);
15469 }
15470
c0dd20ea 15471 type = alloc_type (objfile);
c906108c 15472 INIT_CPLUS_SPECIFIC (type);
93311388 15473
39cbfefa
DJ
15474 name = dwarf2_name (die, cu);
15475 if (name != NULL)
c906108c 15476 {
987504bb 15477 if (cu->language == language_cplus
c44af4eb
TT
15478 || cu->language == language_d
15479 || cu->language == language_rust)
63d06c5c 15480 {
15d034d0 15481 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15482
15483 /* dwarf2_full_name might have already finished building the DIE's
15484 type. If so, there is no need to continue. */
15485 if (get_die_type (die, cu) != NULL)
15486 return get_die_type (die, cu);
15487
e86ca25f 15488 TYPE_NAME (type) = full_name;
63d06c5c
DC
15489 }
15490 else
15491 {
d8151005
DJ
15492 /* The name is already allocated along with this objfile, so
15493 we don't need to duplicate it for the type. */
e86ca25f 15494 TYPE_NAME (type) = name;
63d06c5c 15495 }
c906108c
SS
15496 }
15497
15498 if (die->tag == DW_TAG_structure_type)
15499 {
15500 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15501 }
15502 else if (die->tag == DW_TAG_union_type)
15503 {
15504 TYPE_CODE (type) = TYPE_CODE_UNION;
15505 }
2ddeaf8a
TT
15506 else if (die->tag == DW_TAG_variant_part)
15507 {
15508 TYPE_CODE (type) = TYPE_CODE_UNION;
15509 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15510 }
c906108c
SS
15511 else
15512 {
4753d33b 15513 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15514 }
15515
0cc2414c
TT
15516 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15517 TYPE_DECLARED_CLASS (type) = 1;
15518
e35000a7
TBA
15519 /* Store the calling convention in the type if it's available in
15520 the die. Otherwise the calling convention remains set to
15521 the default value DW_CC_normal. */
15522 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15523 if (attr != nullptr
15524 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15525 {
15526 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15527 TYPE_CPLUS_CALLING_CONVENTION (type)
15528 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15529 }
15530
e142c38c 15531 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15532 if (attr != nullptr)
c906108c 15533 {
cd6c91b4 15534 if (attr->form_is_constant ())
155bfbd3
JB
15535 TYPE_LENGTH (type) = DW_UNSND (attr);
15536 else
15537 {
15538 /* For the moment, dynamic type sizes are not supported
15539 by GDB's struct type. The actual size is determined
15540 on-demand when resolving the type of a given object,
15541 so set the type's length to zero for now. Otherwise,
15542 we record an expression as the length, and that expression
15543 could lead to a very large value, which could eventually
15544 lead to us trying to allocate that much memory when creating
15545 a value of that type. */
15546 TYPE_LENGTH (type) = 0;
15547 }
c906108c
SS
15548 }
15549 else
15550 {
15551 TYPE_LENGTH (type) = 0;
15552 }
15553
2b4424c3
TT
15554 maybe_set_alignment (cu, die, type);
15555
5230b05a 15556 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15557 {
5230b05a
WT
15558 /* ICC<14 does not output the required DW_AT_declaration on
15559 incomplete types, but gives them a size of zero. */
422b1cb0 15560 TYPE_STUB (type) = 1;
685b1105
JK
15561 }
15562 else
15563 TYPE_STUB_SUPPORTED (type) = 1;
15564
dc718098 15565 if (die_is_declaration (die, cu))
876cecd0 15566 TYPE_STUB (type) = 1;
a6c727b2
DJ
15567 else if (attr == NULL && die->child == NULL
15568 && producer_is_realview (cu->producer))
15569 /* RealView does not output the required DW_AT_declaration
15570 on incomplete types. */
15571 TYPE_STUB (type) = 1;
dc718098 15572
c906108c
SS
15573 /* We need to add the type field to the die immediately so we don't
15574 infinitely recurse when dealing with pointers to the structure
0963b4bd 15575 type within the structure itself. */
1c379e20 15576 set_die_type (die, type, cu);
c906108c 15577
7e314c57
JK
15578 /* set_die_type should be already done. */
15579 set_descriptive_type (type, die, cu);
15580
c767944b
DJ
15581 return type;
15582}
15583
2ddeaf8a
TT
15584/* A helper for process_structure_scope that handles a single member
15585 DIE. */
15586
15587static void
15588handle_struct_member_die (struct die_info *child_die, struct type *type,
15589 struct field_info *fi,
15590 std::vector<struct symbol *> *template_args,
15591 struct dwarf2_cu *cu)
15592{
15593 if (child_die->tag == DW_TAG_member
15594 || child_die->tag == DW_TAG_variable
15595 || child_die->tag == DW_TAG_variant_part)
15596 {
15597 /* NOTE: carlton/2002-11-05: A C++ static data member
15598 should be a DW_TAG_member that is a declaration, but
15599 all versions of G++ as of this writing (so through at
15600 least 3.2.1) incorrectly generate DW_TAG_variable
15601 tags for them instead. */
15602 dwarf2_add_field (fi, child_die, cu);
15603 }
15604 else if (child_die->tag == DW_TAG_subprogram)
15605 {
15606 /* Rust doesn't have member functions in the C++ sense.
15607 However, it does emit ordinary functions as children
15608 of a struct DIE. */
15609 if (cu->language == language_rust)
15610 read_func_scope (child_die, cu);
15611 else
15612 {
15613 /* C++ member function. */
15614 dwarf2_add_member_fn (fi, child_die, type, cu);
15615 }
15616 }
15617 else if (child_die->tag == DW_TAG_inheritance)
15618 {
15619 /* C++ base class field. */
15620 dwarf2_add_field (fi, child_die, cu);
15621 }
15622 else if (type_can_define_types (child_die))
15623 dwarf2_add_type_defn (fi, child_die, cu);
15624 else if (child_die->tag == DW_TAG_template_type_param
15625 || child_die->tag == DW_TAG_template_value_param)
15626 {
15627 struct symbol *arg = new_symbol (child_die, NULL, cu);
15628
15629 if (arg != NULL)
15630 template_args->push_back (arg);
15631 }
15632 else if (child_die->tag == DW_TAG_variant)
15633 {
15634 /* In a variant we want to get the discriminant and also add a
15635 field for our sole member child. */
15636 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15637
bde09ab7 15638 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15639 variant_child != NULL;
15640 variant_child = sibling_die (variant_child))
15641 {
15642 if (variant_child->tag == DW_TAG_member)
15643 {
15644 handle_struct_member_die (variant_child, type, fi,
15645 template_args, cu);
15646 /* Only handle the one. */
15647 break;
15648 }
15649 }
15650
15651 /* We don't handle this but we might as well report it if we see
15652 it. */
15653 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15654 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15655 " - DIE at %s [in module %s]"),
15656 sect_offset_str (child_die->sect_off),
15657 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15658
15659 /* The first field was just added, so we can stash the
15660 discriminant there. */
be2daae6 15661 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15662 if (discr == NULL)
be2daae6 15663 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15664 else
be2daae6 15665 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15666 }
15667}
15668
c767944b
DJ
15669/* Finish creating a structure or union type, including filling in
15670 its members and creating a symbol for it. */
15671
15672static void
15673process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15674{
518817b3 15675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15676 struct die_info *child_die;
c767944b
DJ
15677 struct type *type;
15678
15679 type = get_die_type (die, cu);
15680 if (type == NULL)
15681 type = read_structure_type (die, cu);
15682
2ddeaf8a
TT
15683 /* When reading a DW_TAG_variant_part, we need to notice when we
15684 read the discriminant member, so we can record it later in the
15685 discriminant_info. */
15686 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15687 sect_offset discr_offset {};
3e1d3d8c 15688 bool has_template_parameters = false;
2ddeaf8a
TT
15689
15690 if (is_variant_part)
15691 {
15692 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15693 if (discr == NULL)
15694 {
15695 /* Maybe it's a univariant form, an extension we support.
15696 In this case arrange not to check the offset. */
15697 is_variant_part = false;
15698 }
cd6c91b4 15699 else if (discr->form_is_ref ())
2ddeaf8a
TT
15700 {
15701 struct dwarf2_cu *target_cu = cu;
15702 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15703
15704 discr_offset = target_die->sect_off;
15705 }
15706 else
15707 {
b98664d3 15708 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15709 " - DIE at %s [in module %s]"),
15710 sect_offset_str (die->sect_off),
15711 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15712 is_variant_part = false;
15713 }
15714 }
15715
e142c38c 15716 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15717 {
15718 struct field_info fi;
2f4732b0 15719 std::vector<struct symbol *> template_args;
c906108c 15720
639d11d3 15721 child_die = die->child;
c906108c
SS
15722
15723 while (child_die && child_die->tag)
15724 {
2ddeaf8a 15725 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15726
2ddeaf8a 15727 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15728 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15729
c906108c
SS
15730 child_die = sibling_die (child_die);
15731 }
15732
34eaf542 15733 /* Attach template arguments to type. */
2f4732b0 15734 if (!template_args.empty ())
34eaf542 15735 {
3e1d3d8c 15736 has_template_parameters = true;
34eaf542 15737 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15738 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15739 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15740 = XOBNEWVEC (&objfile->objfile_obstack,
15741 struct symbol *,
15742 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15743 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15744 template_args.data (),
34eaf542
TT
15745 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15746 * sizeof (struct symbol *)));
34eaf542
TT
15747 }
15748
c906108c
SS
15749 /* Attach fields and member functions to the type. */
15750 if (fi.nfields)
e7c27a73 15751 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15752 if (!fi.fnfieldlists.empty ())
c906108c 15753 {
e7c27a73 15754 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15755
c5aa993b 15756 /* Get the type which refers to the base class (possibly this
c906108c 15757 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15758 class from the DW_AT_containing_type attribute. This use of
15759 DW_AT_containing_type is a GNU extension. */
c906108c 15760
e142c38c 15761 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15762 {
e7c27a73 15763 struct type *t = die_containing_type (die, cu);
c906108c 15764
ae6ae975 15765 set_type_vptr_basetype (type, t);
c906108c
SS
15766 if (type == t)
15767 {
c906108c
SS
15768 int i;
15769
15770 /* Our own class provides vtbl ptr. */
15771 for (i = TYPE_NFIELDS (t) - 1;
15772 i >= TYPE_N_BASECLASSES (t);
15773 --i)
15774 {
0d5cff50 15775 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15776
1168df01 15777 if (is_vtable_name (fieldname, cu))
c906108c 15778 {
ae6ae975 15779 set_type_vptr_fieldno (type, i);
c906108c
SS
15780 break;
15781 }
15782 }
15783
15784 /* Complain if virtual function table field not found. */
15785 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15786 complaint (_("virtual function table pointer "
3e43a32a 15787 "not found when defining class '%s'"),
e86ca25f 15788 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15789 }
15790 else
15791 {
ae6ae975 15792 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15793 }
15794 }
f6235d4c 15795 else if (cu->producer
61012eef 15796 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15797 {
15798 /* The IBM XLC compiler does not provide direct indication
15799 of the containing type, but the vtable pointer is
15800 always named __vfp. */
15801
15802 int i;
15803
15804 for (i = TYPE_NFIELDS (type) - 1;
15805 i >= TYPE_N_BASECLASSES (type);
15806 --i)
15807 {
15808 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15809 {
ae6ae975
DE
15810 set_type_vptr_fieldno (type, i);
15811 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15812 break;
15813 }
15814 }
15815 }
c906108c 15816 }
98751a41
JK
15817
15818 /* Copy fi.typedef_field_list linked list elements content into the
15819 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15820 if (!fi.typedef_field_list.empty ())
98751a41 15821 {
be2daae6 15822 int count = fi.typedef_field_list.size ();
98751a41 15823
a0d7a4ff 15824 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15825 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15826 = ((struct decl_field *)
be2daae6
TT
15827 TYPE_ALLOC (type,
15828 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15829 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15830
be2daae6
TT
15831 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15832 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15833 }
c767944b 15834
883fd55a
KS
15835 /* Copy fi.nested_types_list linked list elements content into the
15836 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15837 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15838 {
be2daae6 15839 int count = fi.nested_types_list.size ();
883fd55a
KS
15840
15841 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15842 TYPE_NESTED_TYPES_ARRAY (type)
15843 = ((struct decl_field *)
be2daae6
TT
15844 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15845 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15846
be2daae6
TT
15847 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15848 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15849 }
c906108c 15850 }
63d06c5c 15851
bb5ed363 15852 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15853 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15854 cu->rust_unions.push_back (type);
0b92b5bb 15855
90aeadfc
DC
15856 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15857 snapshots) has been known to create a die giving a declaration
15858 for a class that has, as a child, a die giving a definition for a
15859 nested class. So we have to process our children even if the
15860 current die is a declaration. Normally, of course, a declaration
15861 won't have any children at all. */
134d01f1 15862
ca040673
DE
15863 child_die = die->child;
15864
90aeadfc
DC
15865 while (child_die != NULL && child_die->tag)
15866 {
15867 if (child_die->tag == DW_TAG_member
15868 || child_die->tag == DW_TAG_variable
34eaf542
TT
15869 || child_die->tag == DW_TAG_inheritance
15870 || child_die->tag == DW_TAG_template_value_param
15871 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15872 {
90aeadfc 15873 /* Do nothing. */
134d01f1 15874 }
90aeadfc
DC
15875 else
15876 process_die (child_die, cu);
134d01f1 15877
90aeadfc 15878 child_die = sibling_die (child_die);
134d01f1
DJ
15879 }
15880
fa4028e9
JB
15881 /* Do not consider external references. According to the DWARF standard,
15882 these DIEs are identified by the fact that they have no byte_size
15883 attribute, and a declaration attribute. */
15884 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15885 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15886 {
15887 struct symbol *sym = new_symbol (die, type, cu);
15888
15889 if (has_template_parameters)
15890 {
a776957c
TT
15891 struct symtab *symtab;
15892 if (sym != nullptr)
15893 symtab = symbol_symtab (sym);
15894 else if (cu->line_header != nullptr)
15895 {
15896 /* Any related symtab will do. */
15897 symtab
7ba99d21 15898 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15899 }
15900 else
15901 {
15902 symtab = nullptr;
15903 complaint (_("could not find suitable "
15904 "symtab for template parameter"
15905 " - DIE at %s [in module %s]"),
15906 sect_offset_str (die->sect_off),
15907 objfile_name (objfile));
15908 }
15909
15910 if (symtab != nullptr)
15911 {
15912 /* Make sure that the symtab is set on the new symbols.
15913 Even though they don't appear in this symtab directly,
15914 other parts of gdb assume that symbols do, and this is
15915 reasonably true. */
15916 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15917 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15918 }
3e1d3d8c
TT
15919 }
15920 }
134d01f1
DJ
15921}
15922
55426c9d
JB
15923/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15924 update TYPE using some information only available in DIE's children. */
15925
15926static void
15927update_enumeration_type_from_children (struct die_info *die,
15928 struct type *type,
15929 struct dwarf2_cu *cu)
15930{
60f7655a 15931 struct die_info *child_die;
55426c9d
JB
15932 int unsigned_enum = 1;
15933 int flag_enum = 1;
15934 ULONGEST mask = 0;
55426c9d 15935
8268c778 15936 auto_obstack obstack;
55426c9d 15937
60f7655a
DE
15938 for (child_die = die->child;
15939 child_die != NULL && child_die->tag;
15940 child_die = sibling_die (child_die))
55426c9d
JB
15941 {
15942 struct attribute *attr;
15943 LONGEST value;
15944 const gdb_byte *bytes;
15945 struct dwarf2_locexpr_baton *baton;
15946 const char *name;
60f7655a 15947
55426c9d
JB
15948 if (child_die->tag != DW_TAG_enumerator)
15949 continue;
15950
15951 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15952 if (attr == NULL)
15953 continue;
15954
15955 name = dwarf2_name (child_die, cu);
15956 if (name == NULL)
15957 name = "<anonymous enumerator>";
15958
15959 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15960 &value, &bytes, &baton);
15961 if (value < 0)
15962 {
15963 unsigned_enum = 0;
15964 flag_enum = 0;
15965 }
15966 else if ((mask & value) != 0)
15967 flag_enum = 0;
15968 else
15969 mask |= value;
15970
15971 /* If we already know that the enum type is neither unsigned, nor
15972 a flag type, no need to look at the rest of the enumerates. */
15973 if (!unsigned_enum && !flag_enum)
15974 break;
55426c9d
JB
15975 }
15976
15977 if (unsigned_enum)
15978 TYPE_UNSIGNED (type) = 1;
15979 if (flag_enum)
15980 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15981}
15982
134d01f1
DJ
15983/* Given a DW_AT_enumeration_type die, set its type. We do not
15984 complete the type's fields yet, or create any symbols. */
c906108c 15985
f792889a 15986static struct type *
134d01f1 15987read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15988{
518817b3 15989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15990 struct type *type;
c906108c 15991 struct attribute *attr;
0114d602 15992 const char *name;
134d01f1 15993
348e048f
DE
15994 /* If the definition of this type lives in .debug_types, read that type.
15995 Don't follow DW_AT_specification though, that will take us back up
15996 the chain and we want to go down. */
45e58e77 15997 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15998 if (attr != nullptr)
348e048f 15999 {
ac9ec31b 16000 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16001
ac9ec31b 16002 /* The type's CU may not be the same as CU.
02142a6c 16003 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16004 return set_die_type (die, type, cu);
16005 }
16006
c906108c
SS
16007 type = alloc_type (objfile);
16008
16009 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16010 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16011 if (name != NULL)
e86ca25f 16012 TYPE_NAME (type) = name;
c906108c 16013
0626fc76
TT
16014 attr = dwarf2_attr (die, DW_AT_type, cu);
16015 if (attr != NULL)
16016 {
16017 struct type *underlying_type = die_type (die, cu);
16018
16019 TYPE_TARGET_TYPE (type) = underlying_type;
16020 }
16021
e142c38c 16022 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16023 if (attr != nullptr)
c906108c
SS
16024 {
16025 TYPE_LENGTH (type) = DW_UNSND (attr);
16026 }
16027 else
16028 {
16029 TYPE_LENGTH (type) = 0;
16030 }
16031
2b4424c3
TT
16032 maybe_set_alignment (cu, die, type);
16033
137033e9
JB
16034 /* The enumeration DIE can be incomplete. In Ada, any type can be
16035 declared as private in the package spec, and then defined only
16036 inside the package body. Such types are known as Taft Amendment
16037 Types. When another package uses such a type, an incomplete DIE
16038 may be generated by the compiler. */
02eb380e 16039 if (die_is_declaration (die, cu))
876cecd0 16040 TYPE_STUB (type) = 1;
02eb380e 16041
0626fc76
TT
16042 /* Finish the creation of this type by using the enum's children.
16043 We must call this even when the underlying type has been provided
16044 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16045 update_enumeration_type_from_children (die, type, cu);
16046
0626fc76
TT
16047 /* If this type has an underlying type that is not a stub, then we
16048 may use its attributes. We always use the "unsigned" attribute
16049 in this situation, because ordinarily we guess whether the type
16050 is unsigned -- but the guess can be wrong and the underlying type
16051 can tell us the reality. However, we defer to a local size
16052 attribute if one exists, because this lets the compiler override
16053 the underlying type if needed. */
16054 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16055 {
16056 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16057 if (TYPE_LENGTH (type) == 0)
16058 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16059 if (TYPE_RAW_ALIGN (type) == 0
16060 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16061 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16062 }
16063
3d567982
TT
16064 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16065
f792889a 16066 return set_die_type (die, type, cu);
134d01f1
DJ
16067}
16068
16069/* Given a pointer to a die which begins an enumeration, process all
16070 the dies that define the members of the enumeration, and create the
16071 symbol for the enumeration type.
16072
16073 NOTE: We reverse the order of the element list. */
16074
16075static void
16076process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16077{
f792889a 16078 struct type *this_type;
134d01f1 16079
f792889a
DJ
16080 this_type = get_die_type (die, cu);
16081 if (this_type == NULL)
16082 this_type = read_enumeration_type (die, cu);
9dc481d3 16083
639d11d3 16084 if (die->child != NULL)
c906108c 16085 {
9dc481d3
DE
16086 struct die_info *child_die;
16087 struct symbol *sym;
43816ebc 16088 std::vector<struct field> fields;
15d034d0 16089 const char *name;
9dc481d3 16090
639d11d3 16091 child_die = die->child;
c906108c
SS
16092 while (child_die && child_die->tag)
16093 {
16094 if (child_die->tag != DW_TAG_enumerator)
16095 {
e7c27a73 16096 process_die (child_die, cu);
c906108c
SS
16097 }
16098 else
16099 {
39cbfefa
DJ
16100 name = dwarf2_name (child_die, cu);
16101 if (name)
c906108c 16102 {
f792889a 16103 sym = new_symbol (child_die, this_type, cu);
c906108c 16104
43816ebc
TT
16105 fields.emplace_back ();
16106 struct field &field = fields.back ();
c906108c 16107
43816ebc
TT
16108 FIELD_NAME (field) = sym->linkage_name ();
16109 FIELD_TYPE (field) = NULL;
16110 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16111 FIELD_BITSIZE (field) = 0;
c906108c
SS
16112 }
16113 }
16114
16115 child_die = sibling_die (child_die);
16116 }
16117
43816ebc 16118 if (!fields.empty ())
c906108c 16119 {
43816ebc 16120 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16121 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16122 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16123 memcpy (TYPE_FIELDS (this_type), fields.data (),
16124 sizeof (struct field) * fields.size ());
c906108c 16125 }
c906108c 16126 }
134d01f1 16127
6c83ed52
TT
16128 /* If we are reading an enum from a .debug_types unit, and the enum
16129 is a declaration, and the enum is not the signatured type in the
16130 unit, then we do not want to add a symbol for it. Adding a
16131 symbol would in some cases obscure the true definition of the
16132 enum, giving users an incomplete type when the definition is
16133 actually available. Note that we do not want to do this for all
16134 enums which are just declarations, because C++0x allows forward
16135 enum declarations. */
3019eac3 16136 if (cu->per_cu->is_debug_types
6c83ed52
TT
16137 && die_is_declaration (die, cu))
16138 {
52dc124a 16139 struct signatured_type *sig_type;
6c83ed52 16140
c0f78cd4 16141 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16142 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16143 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16144 return;
16145 }
16146
f792889a 16147 new_symbol (die, this_type, cu);
c906108c
SS
16148}
16149
16150/* Extract all information from a DW_TAG_array_type DIE and put it in
16151 the DIE's type field. For now, this only handles one dimensional
16152 arrays. */
16153
f792889a 16154static struct type *
e7c27a73 16155read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16156{
518817b3 16157 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16158 struct die_info *child_die;
7e314c57 16159 struct type *type;
c906108c 16160 struct type *element_type, *range_type, *index_type;
c906108c 16161 struct attribute *attr;
15d034d0 16162 const char *name;
a405673c 16163 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16164 unsigned int bit_stride = 0;
c906108c 16165
e7c27a73 16166 element_type = die_type (die, cu);
c906108c 16167
7e314c57
JK
16168 /* The die_type call above may have already set the type for this DIE. */
16169 type = get_die_type (die, cu);
16170 if (type)
16171 return type;
16172
dc53a7ad
JB
16173 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16174 if (attr != NULL)
a405673c
JB
16175 {
16176 int stride_ok;
9a49df9d
AB
16177 struct type *prop_type
16178 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16179
16180 byte_stride_prop
16181 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16182 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16183 prop_type);
a405673c
JB
16184 if (!stride_ok)
16185 {
b98664d3 16186 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16187 " - DIE at %s [in module %s]"),
16188 sect_offset_str (die->sect_off),
518817b3 16189 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16190 /* Ignore this attribute. We will likely not be able to print
16191 arrays of this type correctly, but there is little we can do
16192 to help if we cannot read the attribute's value. */
16193 byte_stride_prop = NULL;
16194 }
16195 }
dc53a7ad
JB
16196
16197 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16198 if (attr != NULL)
16199 bit_stride = DW_UNSND (attr);
16200
c906108c
SS
16201 /* Irix 6.2 native cc creates array types without children for
16202 arrays with unspecified length. */
639d11d3 16203 if (die->child == NULL)
c906108c 16204 {
46bf5051 16205 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16206 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16207 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16208 byte_stride_prop, bit_stride);
f792889a 16209 return set_die_type (die, type, cu);
c906108c
SS
16210 }
16211
791afaa2 16212 std::vector<struct type *> range_types;
639d11d3 16213 child_die = die->child;
c906108c
SS
16214 while (child_die && child_die->tag)
16215 {
16216 if (child_die->tag == DW_TAG_subrange_type)
16217 {
f792889a 16218 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16219
f792889a 16220 if (child_type != NULL)
a02abb62 16221 {
0963b4bd
MS
16222 /* The range type was succesfully read. Save it for the
16223 array type creation. */
791afaa2 16224 range_types.push_back (child_type);
a02abb62 16225 }
c906108c
SS
16226 }
16227 child_die = sibling_die (child_die);
16228 }
16229
16230 /* Dwarf2 dimensions are output from left to right, create the
16231 necessary array types in backwards order. */
7ca2d3a3 16232
c906108c 16233 type = element_type;
7ca2d3a3
DL
16234
16235 if (read_array_order (die, cu) == DW_ORD_col_major)
16236 {
16237 int i = 0;
9a619af0 16238
791afaa2 16239 while (i < range_types.size ())
dc53a7ad 16240 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16241 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16242 }
16243 else
16244 {
791afaa2 16245 size_t ndim = range_types.size ();
7ca2d3a3 16246 while (ndim-- > 0)
dc53a7ad 16247 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16248 byte_stride_prop, bit_stride);
7ca2d3a3 16249 }
c906108c 16250
f5f8a009
EZ
16251 /* Understand Dwarf2 support for vector types (like they occur on
16252 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16253 array type. This is not part of the Dwarf2/3 standard yet, but a
16254 custom vendor extension. The main difference between a regular
16255 array and the vector variant is that vectors are passed by value
16256 to functions. */
e142c38c 16257 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16258 if (attr != nullptr)
ea37ba09 16259 make_vector_type (type);
f5f8a009 16260
dbc98a8b
KW
16261 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16262 implementation may choose to implement triple vectors using this
16263 attribute. */
16264 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16265 if (attr != nullptr)
dbc98a8b
KW
16266 {
16267 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16268 TYPE_LENGTH (type) = DW_UNSND (attr);
16269 else
b98664d3 16270 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16271 "than the total size of elements"));
dbc98a8b
KW
16272 }
16273
39cbfefa
DJ
16274 name = dwarf2_name (die, cu);
16275 if (name)
16276 TYPE_NAME (type) = name;
6e70227d 16277
2b4424c3
TT
16278 maybe_set_alignment (cu, die, type);
16279
0963b4bd 16280 /* Install the type in the die. */
7e314c57
JK
16281 set_die_type (die, type, cu);
16282
16283 /* set_die_type should be already done. */
b4ba55a1
JB
16284 set_descriptive_type (type, die, cu);
16285
7e314c57 16286 return type;
c906108c
SS
16287}
16288
7ca2d3a3 16289static enum dwarf_array_dim_ordering
6e70227d 16290read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16291{
16292 struct attribute *attr;
16293
16294 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16295
435d3d88 16296 if (attr != nullptr)
aead7601 16297 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16298
0963b4bd
MS
16299 /* GNU F77 is a special case, as at 08/2004 array type info is the
16300 opposite order to the dwarf2 specification, but data is still
16301 laid out as per normal fortran.
7ca2d3a3 16302
0963b4bd
MS
16303 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16304 version checking. */
7ca2d3a3 16305
905e0470
PM
16306 if (cu->language == language_fortran
16307 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16308 {
16309 return DW_ORD_row_major;
16310 }
16311
6e70227d 16312 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16313 {
16314 case array_column_major:
16315 return DW_ORD_col_major;
16316 case array_row_major:
16317 default:
16318 return DW_ORD_row_major;
16319 };
16320}
16321
72019c9c 16322/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16323 the DIE's type field. */
72019c9c 16324
f792889a 16325static struct type *
72019c9c
GM
16326read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16327{
7e314c57
JK
16328 struct type *domain_type, *set_type;
16329 struct attribute *attr;
f792889a 16330
7e314c57
JK
16331 domain_type = die_type (die, cu);
16332
16333 /* The die_type call above may have already set the type for this DIE. */
16334 set_type = get_die_type (die, cu);
16335 if (set_type)
16336 return set_type;
16337
16338 set_type = create_set_type (NULL, domain_type);
16339
16340 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16341 if (attr != nullptr)
d09039dd 16342 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16343
2b4424c3
TT
16344 maybe_set_alignment (cu, die, set_type);
16345
f792889a 16346 return set_die_type (die, set_type, cu);
72019c9c 16347}
7ca2d3a3 16348
0971de02
TT
16349/* A helper for read_common_block that creates a locexpr baton.
16350 SYM is the symbol which we are marking as computed.
16351 COMMON_DIE is the DIE for the common block.
16352 COMMON_LOC is the location expression attribute for the common
16353 block itself.
16354 MEMBER_LOC is the location expression attribute for the particular
16355 member of the common block that we are processing.
16356 CU is the CU from which the above come. */
16357
16358static void
16359mark_common_block_symbol_computed (struct symbol *sym,
16360 struct die_info *common_die,
16361 struct attribute *common_loc,
16362 struct attribute *member_loc,
16363 struct dwarf2_cu *cu)
16364{
518817b3
SM
16365 struct dwarf2_per_objfile *dwarf2_per_objfile
16366 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16367 struct objfile *objfile = dwarf2_per_objfile->objfile;
16368 struct dwarf2_locexpr_baton *baton;
16369 gdb_byte *ptr;
16370 unsigned int cu_off;
16371 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16372 LONGEST offset = 0;
16373
16374 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16375 gdb_assert (common_loc->form_is_block ());
16376 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16377 || member_loc->form_is_constant ());
0971de02 16378
8d749320 16379 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16380 baton->per_cu = cu->per_cu;
16381 gdb_assert (baton->per_cu);
16382
16383 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16384
cd6c91b4 16385 if (member_loc->form_is_constant ())
0971de02
TT
16386 {
16387 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16388 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16389 }
16390 else
16391 baton->size += DW_BLOCK (member_loc)->size;
16392
224c3ddb 16393 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16394 baton->data = ptr;
16395
16396 *ptr++ = DW_OP_call4;
9c541725 16397 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16398 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16399 ptr += 4;
16400
cd6c91b4 16401 if (member_loc->form_is_constant ())
0971de02
TT
16402 {
16403 *ptr++ = DW_OP_addr;
16404 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16405 ptr += cu->header.addr_size;
16406 }
16407 else
16408 {
16409 /* We have to copy the data here, because DW_OP_call4 will only
16410 use a DW_AT_location attribute. */
16411 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16412 ptr += DW_BLOCK (member_loc)->size;
16413 }
16414
16415 *ptr++ = DW_OP_plus;
16416 gdb_assert (ptr - baton->data == baton->size);
16417
0971de02 16418 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16419 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16420}
16421
4357ac6c
TT
16422/* Create appropriate locally-scoped variables for all the
16423 DW_TAG_common_block entries. Also create a struct common_block
16424 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16425 is used to separate the common blocks name namespace from regular
4357ac6c 16426 variable names. */
c906108c
SS
16427
16428static void
e7c27a73 16429read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16430{
0971de02
TT
16431 struct attribute *attr;
16432
16433 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16434 if (attr != nullptr)
0971de02
TT
16435 {
16436 /* Support the .debug_loc offsets. */
4fc6c0d5 16437 if (attr->form_is_block ())
0971de02
TT
16438 {
16439 /* Ok. */
16440 }
cd6c91b4 16441 else if (attr->form_is_section_offset ())
0971de02
TT
16442 {
16443 dwarf2_complex_location_expr_complaint ();
16444 attr = NULL;
16445 }
16446 else
16447 {
16448 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16449 "common block member");
16450 attr = NULL;
16451 }
16452 }
16453
639d11d3 16454 if (die->child != NULL)
c906108c 16455 {
518817b3 16456 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16457 struct die_info *child_die;
16458 size_t n_entries = 0, size;
16459 struct common_block *common_block;
16460 struct symbol *sym;
74ac6d43 16461
4357ac6c
TT
16462 for (child_die = die->child;
16463 child_die && child_die->tag;
16464 child_die = sibling_die (child_die))
16465 ++n_entries;
16466
16467 size = (sizeof (struct common_block)
16468 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16469 common_block
16470 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16471 size);
4357ac6c
TT
16472 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16473 common_block->n_entries = 0;
16474
16475 for (child_die = die->child;
16476 child_die && child_die->tag;
16477 child_die = sibling_die (child_die))
16478 {
16479 /* Create the symbol in the DW_TAG_common_block block in the current
16480 symbol scope. */
e7c27a73 16481 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16482 if (sym != NULL)
16483 {
16484 struct attribute *member_loc;
16485
16486 common_block->contents[common_block->n_entries++] = sym;
16487
16488 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16489 cu);
16490 if (member_loc)
16491 {
16492 /* GDB has handled this for a long time, but it is
16493 not specified by DWARF. It seems to have been
16494 emitted by gfortran at least as recently as:
16495 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16496 complaint (_("Variable in common block has "
0971de02 16497 "DW_AT_data_member_location "
9d8780f0
SM
16498 "- DIE at %s [in module %s]"),
16499 sect_offset_str (child_die->sect_off),
518817b3 16500 objfile_name (objfile));
0971de02 16501
cd6c91b4 16502 if (member_loc->form_is_section_offset ())
0971de02 16503 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16504 else if (member_loc->form_is_constant ()
4fc6c0d5 16505 || member_loc->form_is_block ())
0971de02 16506 {
435d3d88 16507 if (attr != nullptr)
0971de02
TT
16508 mark_common_block_symbol_computed (sym, die, attr,
16509 member_loc, cu);
16510 }
16511 else
16512 dwarf2_complex_location_expr_complaint ();
16513 }
16514 }
c906108c 16515 }
4357ac6c
TT
16516
16517 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16518 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16519 }
16520}
16521
0114d602 16522/* Create a type for a C++ namespace. */
d9fa45fe 16523
0114d602
DJ
16524static struct type *
16525read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16526{
518817b3 16527 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16528 const char *previous_prefix, *name;
9219021c 16529 int is_anonymous;
0114d602
DJ
16530 struct type *type;
16531
16532 /* For extensions, reuse the type of the original namespace. */
16533 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16534 {
16535 struct die_info *ext_die;
16536 struct dwarf2_cu *ext_cu = cu;
9a619af0 16537
0114d602
DJ
16538 ext_die = dwarf2_extension (die, &ext_cu);
16539 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16540
16541 /* EXT_CU may not be the same as CU.
02142a6c 16542 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16543 return set_die_type (die, type, cu);
16544 }
9219021c 16545
e142c38c 16546 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16547
16548 /* Now build the name of the current namespace. */
16549
0114d602
DJ
16550 previous_prefix = determine_prefix (die, cu);
16551 if (previous_prefix[0] != '\0')
16552 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16553 previous_prefix, name, 0, cu);
0114d602
DJ
16554
16555 /* Create the type. */
19f392bc 16556 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16557
60531b24 16558 return set_die_type (die, type, cu);
0114d602
DJ
16559}
16560
22cee43f 16561/* Read a namespace scope. */
0114d602
DJ
16562
16563static void
16564read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16565{
518817b3 16566 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16567 int is_anonymous;
9219021c 16568
5c4e30ca
DC
16569 /* Add a symbol associated to this if we haven't seen the namespace
16570 before. Also, add a using directive if it's an anonymous
16571 namespace. */
9219021c 16572
f2f0e013 16573 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16574 {
16575 struct type *type;
16576
0114d602 16577 type = read_type_die (die, cu);
e7c27a73 16578 new_symbol (die, type, cu);
5c4e30ca 16579
e8e80198 16580 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16581 if (is_anonymous)
0114d602
DJ
16582 {
16583 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16584
eb1e02fd 16585 std::vector<const char *> excludes;
804d2729 16586 add_using_directive (using_directives (cu),
22cee43f 16587 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16588 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16589 }
5c4e30ca 16590 }
9219021c 16591
639d11d3 16592 if (die->child != NULL)
d9fa45fe 16593 {
639d11d3 16594 struct die_info *child_die = die->child;
6e70227d 16595
d9fa45fe
DC
16596 while (child_die && child_die->tag)
16597 {
e7c27a73 16598 process_die (child_die, cu);
d9fa45fe
DC
16599 child_die = sibling_die (child_die);
16600 }
16601 }
38d518c9
EZ
16602}
16603
f55ee35c
JK
16604/* Read a Fortran module as type. This DIE can be only a declaration used for
16605 imported module. Still we need that type as local Fortran "use ... only"
16606 declaration imports depend on the created type in determine_prefix. */
16607
16608static struct type *
16609read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16610{
518817b3 16611 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16612 const char *module_name;
f55ee35c
JK
16613 struct type *type;
16614
16615 module_name = dwarf2_name (die, cu);
19f392bc 16616 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16617
f55ee35c
JK
16618 return set_die_type (die, type, cu);
16619}
16620
5d7cb8df
JK
16621/* Read a Fortran module. */
16622
16623static void
16624read_module (struct die_info *die, struct dwarf2_cu *cu)
16625{
16626 struct die_info *child_die = die->child;
530e8392
KB
16627 struct type *type;
16628
16629 type = read_type_die (die, cu);
16630 new_symbol (die, type, cu);
5d7cb8df 16631
5d7cb8df
JK
16632 while (child_die && child_die->tag)
16633 {
16634 process_die (child_die, cu);
16635 child_die = sibling_die (child_die);
16636 }
16637}
16638
38d518c9
EZ
16639/* Return the name of the namespace represented by DIE. Set
16640 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16641 namespace. */
16642
16643static const char *
e142c38c 16644namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16645{
16646 struct die_info *current_die;
16647 const char *name = NULL;
16648
16649 /* Loop through the extensions until we find a name. */
16650
16651 for (current_die = die;
16652 current_die != NULL;
f2f0e013 16653 current_die = dwarf2_extension (die, &cu))
38d518c9 16654 {
96553a0c
DE
16655 /* We don't use dwarf2_name here so that we can detect the absence
16656 of a name -> anonymous namespace. */
7d45c7c3 16657 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16658
38d518c9
EZ
16659 if (name != NULL)
16660 break;
16661 }
16662
16663 /* Is it an anonymous namespace? */
16664
16665 *is_anonymous = (name == NULL);
16666 if (*is_anonymous)
2b1dbab0 16667 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16668
16669 return name;
d9fa45fe
DC
16670}
16671
c906108c
SS
16672/* Extract all information from a DW_TAG_pointer_type DIE and add to
16673 the user defined type vector. */
16674
f792889a 16675static struct type *
e7c27a73 16676read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16677{
518817b3
SM
16678 struct gdbarch *gdbarch
16679 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16680 struct comp_unit_head *cu_header = &cu->header;
c906108c 16681 struct type *type;
8b2dbe47
KB
16682 struct attribute *attr_byte_size;
16683 struct attribute *attr_address_class;
16684 int byte_size, addr_class;
7e314c57
JK
16685 struct type *target_type;
16686
16687 target_type = die_type (die, cu);
c906108c 16688
7e314c57
JK
16689 /* The die_type call above may have already set the type for this DIE. */
16690 type = get_die_type (die, cu);
16691 if (type)
16692 return type;
16693
16694 type = lookup_pointer_type (target_type);
8b2dbe47 16695
e142c38c 16696 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16697 if (attr_byte_size)
16698 byte_size = DW_UNSND (attr_byte_size);
c906108c 16699 else
8b2dbe47
KB
16700 byte_size = cu_header->addr_size;
16701
e142c38c 16702 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16703 if (attr_address_class)
16704 addr_class = DW_UNSND (attr_address_class);
16705 else
16706 addr_class = DW_ADDR_none;
16707
2b4424c3
TT
16708 ULONGEST alignment = get_alignment (cu, die);
16709
16710 /* If the pointer size, alignment, or address class is different
16711 than the default, create a type variant marked as such and set
16712 the length accordingly. */
16713 if (TYPE_LENGTH (type) != byte_size
16714 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16715 && alignment != TYPE_RAW_ALIGN (type))
16716 || addr_class != DW_ADDR_none)
c906108c 16717 {
5e2b427d 16718 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16719 {
16720 int type_flags;
16721
849957d9 16722 type_flags = gdbarch_address_class_type_flags
5e2b427d 16723 (gdbarch, byte_size, addr_class);
876cecd0
TT
16724 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16725 == 0);
8b2dbe47
KB
16726 type = make_type_with_address_space (type, type_flags);
16727 }
16728 else if (TYPE_LENGTH (type) != byte_size)
16729 {
b98664d3 16730 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16731 }
2b4424c3
TT
16732 else if (TYPE_RAW_ALIGN (type) != alignment)
16733 {
b98664d3 16734 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16735 " - DIE at %s [in module %s]"),
16736 sect_offset_str (die->sect_off),
16737 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16738 }
6e70227d 16739 else
9a619af0
MS
16740 {
16741 /* Should we also complain about unhandled address classes? */
16742 }
c906108c 16743 }
8b2dbe47
KB
16744
16745 TYPE_LENGTH (type) = byte_size;
2b4424c3 16746 set_type_align (type, alignment);
f792889a 16747 return set_die_type (die, type, cu);
c906108c
SS
16748}
16749
16750/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16751 the user defined type vector. */
16752
f792889a 16753static struct type *
e7c27a73 16754read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16755{
16756 struct type *type;
16757 struct type *to_type;
16758 struct type *domain;
16759
e7c27a73
DJ
16760 to_type = die_type (die, cu);
16761 domain = die_containing_type (die, cu);
0d5de010 16762
7e314c57
JK
16763 /* The calls above may have already set the type for this DIE. */
16764 type = get_die_type (die, cu);
16765 if (type)
16766 return type;
16767
0d5de010
DJ
16768 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16769 type = lookup_methodptr_type (to_type);
7078baeb
TT
16770 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16771 {
518817b3
SM
16772 struct type *new_type
16773 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16774
16775 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16776 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16777 TYPE_VARARGS (to_type));
16778 type = lookup_methodptr_type (new_type);
16779 }
0d5de010
DJ
16780 else
16781 type = lookup_memberptr_type (to_type, domain);
c906108c 16782
f792889a 16783 return set_die_type (die, type, cu);
c906108c
SS
16784}
16785
4297a3f0 16786/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16787 the user defined type vector. */
16788
f792889a 16789static struct type *
4297a3f0
AV
16790read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16791 enum type_code refcode)
c906108c 16792{
e7c27a73 16793 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16794 struct type *type, *target_type;
c906108c
SS
16795 struct attribute *attr;
16796
4297a3f0
AV
16797 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16798
7e314c57
JK
16799 target_type = die_type (die, cu);
16800
16801 /* The die_type call above may have already set the type for this DIE. */
16802 type = get_die_type (die, cu);
16803 if (type)
16804 return type;
16805
4297a3f0 16806 type = lookup_reference_type (target_type, refcode);
e142c38c 16807 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16808 if (attr != nullptr)
c906108c
SS
16809 {
16810 TYPE_LENGTH (type) = DW_UNSND (attr);
16811 }
16812 else
16813 {
107d2387 16814 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16815 }
2b4424c3 16816 maybe_set_alignment (cu, die, type);
f792889a 16817 return set_die_type (die, type, cu);
c906108c
SS
16818}
16819
cf363f18
MW
16820/* Add the given cv-qualifiers to the element type of the array. GCC
16821 outputs DWARF type qualifiers that apply to an array, not the
16822 element type. But GDB relies on the array element type to carry
16823 the cv-qualifiers. This mimics section 6.7.3 of the C99
16824 specification. */
16825
16826static struct type *
16827add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16828 struct type *base_type, int cnst, int voltl)
16829{
16830 struct type *el_type, *inner_array;
16831
16832 base_type = copy_type (base_type);
16833 inner_array = base_type;
16834
16835 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16836 {
16837 TYPE_TARGET_TYPE (inner_array) =
16838 copy_type (TYPE_TARGET_TYPE (inner_array));
16839 inner_array = TYPE_TARGET_TYPE (inner_array);
16840 }
16841
16842 el_type = TYPE_TARGET_TYPE (inner_array);
16843 cnst |= TYPE_CONST (el_type);
16844 voltl |= TYPE_VOLATILE (el_type);
16845 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16846
16847 return set_die_type (die, base_type, cu);
16848}
16849
f792889a 16850static struct type *
e7c27a73 16851read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16852{
f792889a 16853 struct type *base_type, *cv_type;
c906108c 16854
e7c27a73 16855 base_type = die_type (die, cu);
7e314c57
JK
16856
16857 /* The die_type call above may have already set the type for this DIE. */
16858 cv_type = get_die_type (die, cu);
16859 if (cv_type)
16860 return cv_type;
16861
2f608a3a
KW
16862 /* In case the const qualifier is applied to an array type, the element type
16863 is so qualified, not the array type (section 6.7.3 of C99). */
16864 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16865 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16866
f792889a
DJ
16867 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16868 return set_die_type (die, cv_type, cu);
c906108c
SS
16869}
16870
f792889a 16871static struct type *
e7c27a73 16872read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16873{
f792889a 16874 struct type *base_type, *cv_type;
c906108c 16875
e7c27a73 16876 base_type = die_type (die, cu);
7e314c57
JK
16877
16878 /* The die_type call above may have already set the type for this DIE. */
16879 cv_type = get_die_type (die, cu);
16880 if (cv_type)
16881 return cv_type;
16882
cf363f18
MW
16883 /* In case the volatile qualifier is applied to an array type, the
16884 element type is so qualified, not the array type (section 6.7.3
16885 of C99). */
16886 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16887 return add_array_cv_type (die, cu, base_type, 0, 1);
16888
f792889a
DJ
16889 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16890 return set_die_type (die, cv_type, cu);
c906108c
SS
16891}
16892
06d66ee9
TT
16893/* Handle DW_TAG_restrict_type. */
16894
16895static struct type *
16896read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16897{
16898 struct type *base_type, *cv_type;
16899
16900 base_type = die_type (die, cu);
16901
16902 /* The die_type call above may have already set the type for this DIE. */
16903 cv_type = get_die_type (die, cu);
16904 if (cv_type)
16905 return cv_type;
16906
16907 cv_type = make_restrict_type (base_type);
16908 return set_die_type (die, cv_type, cu);
16909}
16910
a2c2acaf
MW
16911/* Handle DW_TAG_atomic_type. */
16912
16913static struct type *
16914read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16915{
16916 struct type *base_type, *cv_type;
16917
16918 base_type = die_type (die, cu);
16919
16920 /* The die_type call above may have already set the type for this DIE. */
16921 cv_type = get_die_type (die, cu);
16922 if (cv_type)
16923 return cv_type;
16924
16925 cv_type = make_atomic_type (base_type);
16926 return set_die_type (die, cv_type, cu);
16927}
16928
c906108c
SS
16929/* Extract all information from a DW_TAG_string_type DIE and add to
16930 the user defined type vector. It isn't really a user defined type,
16931 but it behaves like one, with other DIE's using an AT_user_def_type
16932 attribute to reference it. */
16933
f792889a 16934static struct type *
e7c27a73 16935read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16936{
518817b3 16937 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16938 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16939 struct type *type, *range_type, *index_type, *char_type;
16940 struct attribute *attr;
216a7e6b
AB
16941 struct dynamic_prop prop;
16942 bool length_is_constant = true;
16943 LONGEST length;
16944
16945 /* There are a couple of places where bit sizes might be made use of
16946 when parsing a DW_TAG_string_type, however, no producer that we know
16947 of make use of these. Handling bit sizes that are a multiple of the
16948 byte size is easy enough, but what about other bit sizes? Lets deal
16949 with that problem when we have to. Warn about these attributes being
16950 unsupported, then parse the type and ignore them like we always
16951 have. */
16952 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16953 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16954 {
16955 static bool warning_printed = false;
16956 if (!warning_printed)
16957 {
16958 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16959 "currently supported on DW_TAG_string_type."));
16960 warning_printed = true;
16961 }
16962 }
c906108c 16963
e142c38c 16964 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16965 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16966 {
16967 /* The string length describes the location at which the length of
16968 the string can be found. The size of the length field can be
16969 specified with one of the attributes below. */
16970 struct type *prop_type;
16971 struct attribute *len
16972 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16973 if (len == nullptr)
16974 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16975 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16976 {
16977 /* Pass 0 as the default as we know this attribute is constant
16978 and the default value will not be returned. */
16979 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16980 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
16981 }
16982 else
16983 {
16984 /* If the size is not specified then we assume it is the size of
16985 an address on this target. */
16986 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
16987 }
16988
16989 /* Convert the attribute into a dynamic property. */
16990 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16991 length = 1;
16992 else
16993 length_is_constant = false;
16994 }
16995 else if (attr != nullptr)
16996 {
16997 /* This DW_AT_string_length just contains the length with no
16998 indirection. There's no need to create a dynamic property in this
16999 case. Pass 0 for the default value as we know it will not be
17000 returned in this case. */
17001 length = dwarf2_get_attr_constant_value (attr, 0);
17002 }
17003 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17004 {
216a7e6b
AB
17005 /* We don't currently support non-constant byte sizes for strings. */
17006 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17007 }
17008 else
17009 {
216a7e6b
AB
17010 /* Use 1 as a fallback length if we have nothing else. */
17011 length = 1;
c906108c 17012 }
6ccb9162 17013
46bf5051 17014 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17015 if (length_is_constant)
17016 range_type = create_static_range_type (NULL, index_type, 1, length);
17017 else
17018 {
17019 struct dynamic_prop low_bound;
17020
17021 low_bound.kind = PROP_CONST;
17022 low_bound.data.const_val = 1;
17023 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17024 }
3b7538c0
UW
17025 char_type = language_string_char_type (cu->language_defn, gdbarch);
17026 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17027
f792889a 17028 return set_die_type (die, type, cu);
c906108c
SS
17029}
17030
4d804846
JB
17031/* Assuming that DIE corresponds to a function, returns nonzero
17032 if the function is prototyped. */
17033
17034static int
17035prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17036{
17037 struct attribute *attr;
17038
17039 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17040 if (attr && (DW_UNSND (attr) != 0))
17041 return 1;
17042
17043 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17044 is only meaningful for C, but the concept also extends to other
4d804846
JB
17045 languages that allow unprototyped functions (Eg: Objective C).
17046 For all other languages, assume that functions are always
17047 prototyped. */
17048 if (cu->language != language_c
17049 && cu->language != language_objc
17050 && cu->language != language_opencl)
17051 return 1;
17052
17053 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17054 prototyped and unprototyped functions; default to prototyped,
17055 since that is more common in modern code (and RealView warns
17056 about unprototyped functions). */
17057 if (producer_is_realview (cu->producer))
17058 return 1;
17059
17060 return 0;
17061}
17062
c906108c
SS
17063/* Handle DIES due to C code like:
17064
17065 struct foo
c5aa993b
JM
17066 {
17067 int (*funcp)(int a, long l);
17068 int b;
17069 };
c906108c 17070
0963b4bd 17071 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17072
f792889a 17073static struct type *
e7c27a73 17074read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17075{
518817b3 17076 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17077 struct type *type; /* Type that this function returns. */
17078 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17079 struct attribute *attr;
17080
e7c27a73 17081 type = die_type (die, cu);
7e314c57
JK
17082
17083 /* The die_type call above may have already set the type for this DIE. */
17084 ftype = get_die_type (die, cu);
17085 if (ftype)
17086 return ftype;
17087
0c8b41f1 17088 ftype = lookup_function_type (type);
c906108c 17089
4d804846 17090 if (prototyped_function_p (die, cu))
a6c727b2 17091 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17092
c055b101
CV
17093 /* Store the calling convention in the type if it's available in
17094 the subroutine die. Otherwise set the calling convention to
17095 the default value DW_CC_normal. */
17096 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17097 if (attr != nullptr
17098 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17099 TYPE_CALLING_CONVENTION (ftype)
17100 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17101 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17102 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17103 else
17104 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17105
743649fd
MW
17106 /* Record whether the function returns normally to its caller or not
17107 if the DWARF producer set that information. */
17108 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17109 if (attr && (DW_UNSND (attr) != 0))
17110 TYPE_NO_RETURN (ftype) = 1;
17111
76c10ea2
GM
17112 /* We need to add the subroutine type to the die immediately so
17113 we don't infinitely recurse when dealing with parameters
0963b4bd 17114 declared as the same subroutine type. */
76c10ea2 17115 set_die_type (die, ftype, cu);
6e70227d 17116
639d11d3 17117 if (die->child != NULL)
c906108c 17118 {
bb5ed363 17119 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17120 struct die_info *child_die;
8072405b 17121 int nparams, iparams;
c906108c
SS
17122
17123 /* Count the number of parameters.
17124 FIXME: GDB currently ignores vararg functions, but knows about
17125 vararg member functions. */
8072405b 17126 nparams = 0;
639d11d3 17127 child_die = die->child;
c906108c
SS
17128 while (child_die && child_die->tag)
17129 {
17130 if (child_die->tag == DW_TAG_formal_parameter)
17131 nparams++;
17132 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17133 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17134 child_die = sibling_die (child_die);
17135 }
17136
17137 /* Allocate storage for parameters and fill them in. */
17138 TYPE_NFIELDS (ftype) = nparams;
17139 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17140 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17141
8072405b
JK
17142 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17143 even if we error out during the parameters reading below. */
17144 for (iparams = 0; iparams < nparams; iparams++)
17145 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17146
17147 iparams = 0;
639d11d3 17148 child_die = die->child;
c906108c
SS
17149 while (child_die && child_die->tag)
17150 {
17151 if (child_die->tag == DW_TAG_formal_parameter)
17152 {
3ce3b1ba
PA
17153 struct type *arg_type;
17154
17155 /* DWARF version 2 has no clean way to discern C++
17156 static and non-static member functions. G++ helps
17157 GDB by marking the first parameter for non-static
17158 member functions (which is the this pointer) as
17159 artificial. We pass this information to
17160 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17161
17162 DWARF version 3 added DW_AT_object_pointer, which GCC
17163 4.5 does not yet generate. */
e142c38c 17164 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17165 if (attr != nullptr)
c906108c
SS
17166 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17167 else
9c37b5ae 17168 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17169 arg_type = die_type (child_die, cu);
17170
17171 /* RealView does not mark THIS as const, which the testsuite
17172 expects. GCC marks THIS as const in method definitions,
17173 but not in the class specifications (GCC PR 43053). */
17174 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17175 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17176 {
17177 int is_this = 0;
17178 struct dwarf2_cu *arg_cu = cu;
17179 const char *name = dwarf2_name (child_die, cu);
17180
17181 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17182 if (attr != nullptr)
3ce3b1ba
PA
17183 {
17184 /* If the compiler emits this, use it. */
17185 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17186 is_this = 1;
17187 }
17188 else if (name && strcmp (name, "this") == 0)
17189 /* Function definitions will have the argument names. */
17190 is_this = 1;
17191 else if (name == NULL && iparams == 0)
17192 /* Declarations may not have the names, so like
17193 elsewhere in GDB, assume an artificial first
17194 argument is "this". */
17195 is_this = 1;
17196
17197 if (is_this)
17198 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17199 arg_type, 0);
17200 }
17201
17202 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17203 iparams++;
17204 }
17205 child_die = sibling_die (child_die);
17206 }
17207 }
17208
76c10ea2 17209 return ftype;
c906108c
SS
17210}
17211
f792889a 17212static struct type *
e7c27a73 17213read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17214{
518817b3 17215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17216 const char *name = NULL;
3c8e0968 17217 struct type *this_type, *target_type;
c906108c 17218
94af9270 17219 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17220 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17221 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17222 set_die_type (die, this_type, cu);
3c8e0968
DE
17223 target_type = die_type (die, cu);
17224 if (target_type != this_type)
17225 TYPE_TARGET_TYPE (this_type) = target_type;
17226 else
17227 {
17228 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17229 spec and cause infinite loops in GDB. */
b98664d3 17230 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17231 "- DIE at %s [in module %s]"),
17232 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17233 TYPE_TARGET_TYPE (this_type) = NULL;
17234 }
f792889a 17235 return this_type;
c906108c
SS
17236}
17237
9b790ce7
UW
17238/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17239 (which may be different from NAME) to the architecture back-end to allow
17240 it to guess the correct format if necessary. */
17241
17242static struct type *
17243dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17244 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17245{
17246 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17247 const struct floatformat **format;
17248 struct type *type;
17249
17250 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17251 if (format)
103a685e 17252 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17253 else
77b7c781 17254 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17255
17256 return type;
17257}
17258
eb77c9df
AB
17259/* Allocate an integer type of size BITS and name NAME. */
17260
17261static struct type *
17262dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17263 int bits, int unsigned_p, const char *name)
17264{
17265 struct type *type;
17266
17267 /* Versions of Intel's C Compiler generate an integer type called "void"
17268 instead of using DW_TAG_unspecified_type. This has been seen on
17269 at least versions 14, 17, and 18. */
35ee2dc2
AB
17270 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17271 && strcmp (name, "void") == 0)
eb77c9df
AB
17272 type = objfile_type (objfile)->builtin_void;
17273 else
17274 type = init_integer_type (objfile, bits, unsigned_p, name);
17275
17276 return type;
17277}
17278
8bdc1658
AB
17279/* Initialise and return a floating point type of size BITS suitable for
17280 use as a component of a complex number. The NAME_HINT is passed through
17281 when initialising the floating point type and is the name of the complex
17282 type.
17283
17284 As DWARF doesn't currently provide an explicit name for the components
17285 of a complex number, but it can be helpful to have these components
17286 named, we try to select a suitable name based on the size of the
17287 component. */
17288static struct type *
17289dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17290 struct objfile *objfile,
103a685e
TT
17291 int bits, const char *name_hint,
17292 enum bfd_endian byte_order)
8bdc1658
AB
17293{
17294 gdbarch *gdbarch = get_objfile_arch (objfile);
17295 struct type *tt = nullptr;
17296
35add35e
AB
17297 /* Try to find a suitable floating point builtin type of size BITS.
17298 We're going to use the name of this type as the name for the complex
17299 target type that we are about to create. */
1db455a7 17300 switch (cu->language)
8bdc1658 17301 {
1db455a7
AB
17302 case language_fortran:
17303 switch (bits)
17304 {
17305 case 32:
17306 tt = builtin_f_type (gdbarch)->builtin_real;
17307 break;
17308 case 64:
17309 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17310 break;
17311 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17312 case 128:
17313 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17314 break;
17315 }
8bdc1658 17316 break;
1db455a7
AB
17317 default:
17318 switch (bits)
17319 {
17320 case 32:
17321 tt = builtin_type (gdbarch)->builtin_float;
17322 break;
17323 case 64:
17324 tt = builtin_type (gdbarch)->builtin_double;
17325 break;
17326 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17327 case 128:
17328 tt = builtin_type (gdbarch)->builtin_long_double;
17329 break;
17330 }
8bdc1658
AB
17331 break;
17332 }
17333
35add35e
AB
17334 /* If the type we found doesn't match the size we were looking for, then
17335 pretend we didn't find a type at all, the complex target type we
17336 create will then be nameless. */
a12e5744 17337 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17338 tt = nullptr;
17339
8bdc1658 17340 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17341 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17342}
17343
c906108c
SS
17344/* Find a representation of a given base type and install
17345 it in the TYPE field of the die. */
17346
f792889a 17347static struct type *
e7c27a73 17348read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17349{
518817b3 17350 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17351 struct type *type;
17352 struct attribute *attr;
19f392bc 17353 int encoding = 0, bits = 0;
15d034d0 17354 const char *name;
34877895 17355 gdbarch *arch;
c906108c 17356
e142c38c 17357 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17358 if (attr != nullptr)
34877895 17359 encoding = DW_UNSND (attr);
e142c38c 17360 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17361 if (attr != nullptr)
34877895 17362 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17363 name = dwarf2_name (die, cu);
6ccb9162 17364 if (!name)
34877895 17365 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17366
17367 arch = get_objfile_arch (objfile);
17368 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17369
34877895
PJ
17370 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17371 if (attr)
103a685e
TT
17372 {
17373 int endianity = DW_UNSND (attr);
17374
17375 switch (endianity)
17376 {
17377 case DW_END_big:
17378 byte_order = BFD_ENDIAN_BIG;
17379 break;
17380 case DW_END_little:
17381 byte_order = BFD_ENDIAN_LITTLE;
17382 break;
17383 default:
17384 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17385 break;
17386 }
17387 }
6ccb9162
UW
17388
17389 switch (encoding)
c906108c 17390 {
6ccb9162
UW
17391 case DW_ATE_address:
17392 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17393 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17394 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17395 break;
17396 case DW_ATE_boolean:
19f392bc 17397 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17398 break;
17399 case DW_ATE_complex_float:
103a685e
TT
17400 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17401 byte_order);
19f392bc 17402 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17403 break;
17404 case DW_ATE_decimal_float:
19f392bc 17405 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17406 break;
17407 case DW_ATE_float:
103a685e 17408 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17409 break;
17410 case DW_ATE_signed:
eb77c9df 17411 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17412 break;
17413 case DW_ATE_unsigned:
3b2b8fea
TT
17414 if (cu->language == language_fortran
17415 && name
61012eef 17416 && startswith (name, "character("))
19f392bc
UW
17417 type = init_character_type (objfile, bits, 1, name);
17418 else
eb77c9df 17419 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17420 break;
17421 case DW_ATE_signed_char:
6e70227d 17422 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17423 || cu->language == language_pascal
17424 || cu->language == language_fortran)
19f392bc
UW
17425 type = init_character_type (objfile, bits, 0, name);
17426 else
eb77c9df 17427 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17428 break;
17429 case DW_ATE_unsigned_char:
868a0084 17430 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17431 || cu->language == language_pascal
c44af4eb
TT
17432 || cu->language == language_fortran
17433 || cu->language == language_rust)
19f392bc
UW
17434 type = init_character_type (objfile, bits, 1, name);
17435 else
eb77c9df 17436 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17437 break;
75079b2b 17438 case DW_ATE_UTF:
53e710ac 17439 {
53e710ac
PA
17440 if (bits == 16)
17441 type = builtin_type (arch)->builtin_char16;
17442 else if (bits == 32)
17443 type = builtin_type (arch)->builtin_char32;
17444 else
17445 {
b98664d3 17446 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17447 bits);
eb77c9df 17448 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17449 }
17450 return set_die_type (die, type, cu);
17451 }
75079b2b
TT
17452 break;
17453
6ccb9162 17454 default:
b98664d3 17455 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17456 dwarf_type_encoding_name (encoding));
77b7c781 17457 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17458 break;
c906108c 17459 }
6ccb9162 17460
0114d602 17461 if (name && strcmp (name, "char") == 0)
876cecd0 17462 TYPE_NOSIGN (type) = 1;
0114d602 17463
2b4424c3
TT
17464 maybe_set_alignment (cu, die, type);
17465
103a685e 17466 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17467
f792889a 17468 return set_die_type (die, type, cu);
c906108c
SS
17469}
17470
80180f79
SA
17471/* Parse dwarf attribute if it's a block, reference or constant and put the
17472 resulting value of the attribute into struct bound_prop.
17473 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17474
17475static int
17476attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17477 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17478 struct type *default_type)
80180f79
SA
17479{
17480 struct dwarf2_property_baton *baton;
518817b3
SM
17481 struct obstack *obstack
17482 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17483
9a49df9d
AB
17484 gdb_assert (default_type != NULL);
17485
80180f79
SA
17486 if (attr == NULL || prop == NULL)
17487 return 0;
17488
4fc6c0d5 17489 if (attr->form_is_block ())
80180f79 17490 {
8d749320 17491 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17492 baton->property_type = default_type;
80180f79
SA
17493 baton->locexpr.per_cu = cu->per_cu;
17494 baton->locexpr.size = DW_BLOCK (attr)->size;
17495 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17496 switch (attr->name)
17497 {
17498 case DW_AT_string_length:
17499 baton->locexpr.is_reference = true;
17500 break;
17501 default:
17502 baton->locexpr.is_reference = false;
17503 break;
17504 }
80180f79
SA
17505 prop->data.baton = baton;
17506 prop->kind = PROP_LOCEXPR;
17507 gdb_assert (prop->data.baton != NULL);
17508 }
cd6c91b4 17509 else if (attr->form_is_ref ())
80180f79
SA
17510 {
17511 struct dwarf2_cu *target_cu = cu;
17512 struct die_info *target_die;
17513 struct attribute *target_attr;
17514
17515 target_die = follow_die_ref (die, attr, &target_cu);
17516 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17517 if (target_attr == NULL)
17518 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17519 target_cu);
80180f79
SA
17520 if (target_attr == NULL)
17521 return 0;
17522
df25ebbd 17523 switch (target_attr->name)
80180f79 17524 {
df25ebbd 17525 case DW_AT_location:
cd6c91b4 17526 if (target_attr->form_is_section_offset ())
df25ebbd 17527 {
8d749320 17528 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17529 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17530 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17531 prop->data.baton = baton;
17532 prop->kind = PROP_LOCLIST;
17533 gdb_assert (prop->data.baton != NULL);
17534 }
4fc6c0d5 17535 else if (target_attr->form_is_block ())
df25ebbd 17536 {
8d749320 17537 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17538 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17539 baton->locexpr.per_cu = cu->per_cu;
17540 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17541 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17542 baton->locexpr.is_reference = true;
df25ebbd
JB
17543 prop->data.baton = baton;
17544 prop->kind = PROP_LOCEXPR;
17545 gdb_assert (prop->data.baton != NULL);
17546 }
17547 else
17548 {
17549 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17550 "dynamic property");
17551 return 0;
17552 }
17553 break;
17554 case DW_AT_data_member_location:
17555 {
17556 LONGEST offset;
17557
17558 if (!handle_data_member_location (target_die, target_cu,
17559 &offset))
17560 return 0;
17561
8d749320 17562 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17563 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17564 target_cu);
df25ebbd
JB
17565 baton->offset_info.offset = offset;
17566 baton->offset_info.type = die_type (target_die, target_cu);
17567 prop->data.baton = baton;
17568 prop->kind = PROP_ADDR_OFFSET;
17569 break;
17570 }
80180f79
SA
17571 }
17572 }
cd6c91b4 17573 else if (attr->form_is_constant ())
80180f79
SA
17574 {
17575 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17576 prop->kind = PROP_CONST;
17577 }
17578 else
17579 {
17580 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17581 dwarf2_name (die, cu));
17582 return 0;
17583 }
17584
17585 return 1;
17586}
17587
11a8b164
AB
17588/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17589 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
17590
17591static struct type *
11a8b164
AB
17592dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17593 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
17594{
17595 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
17596 struct type *int_type;
17597
17598 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17599#define TRY_TYPE(F) \
17600 int_type = (unsigned_p \
17601 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17602 : objfile_type (objfile)->builtin_ ## F); \
17603 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17604 return int_type
17605
17606 TRY_TYPE (char);
17607 TRY_TYPE (short);
17608 TRY_TYPE (int);
17609 TRY_TYPE (long);
17610 TRY_TYPE (long_long);
17611
17612#undef TRY_TYPE
17613
17614 gdb_assert_not_reached ("unable to find suitable integer type");
17615}
17616
11a8b164
AB
17617/* Find an integer type the same size as the address size given in the
17618 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17619 is unsigned or not. */
17620
17621static struct type *
17622dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17623 bool unsigned_p)
17624{
17625 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17626 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17627}
17628
b86352cf
AB
17629/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17630 present (which is valid) then compute the default type based on the
17631 compilation units address size. */
17632
17633static struct type *
17634read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17635{
17636 struct type *index_type = die_type (die, cu);
17637
17638 /* Dwarf-2 specifications explicitly allows to create subrange types
17639 without specifying a base type.
17640 In that case, the base type must be set to the type of
17641 the lower bound, upper bound or count, in that order, if any of these
17642 three attributes references an object that has a type.
17643 If no base type is found, the Dwarf-2 specifications say that
17644 a signed integer type of size equal to the size of an address should
17645 be used.
17646 For the following C code: `extern char gdb_int [];'
17647 GCC produces an empty range DIE.
17648 FIXME: muller/2010-05-28: Possible references to object for low bound,
17649 high bound or count are not yet handled by this code. */
17650 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17651 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17652
17653 return index_type;
17654}
17655
a02abb62
JB
17656/* Read the given DW_AT_subrange DIE. */
17657
f792889a 17658static struct type *
a02abb62
JB
17659read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17660{
4c9ad8c2 17661 struct type *base_type, *orig_base_type;
a02abb62
JB
17662 struct type *range_type;
17663 struct attribute *attr;
729efb13 17664 struct dynamic_prop low, high;
4fae6e18 17665 int low_default_is_valid;
c451ebe5 17666 int high_bound_is_count = 0;
15d034d0 17667 const char *name;
d359392f 17668 ULONGEST negative_mask;
e77813c8 17669
b86352cf
AB
17670 orig_base_type = read_subrange_index_type (die, cu);
17671
4c9ad8c2
TT
17672 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17673 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17674 creating the range type, but we use the result of check_typedef
17675 when examining properties of the type. */
17676 base_type = check_typedef (orig_base_type);
a02abb62 17677
7e314c57
JK
17678 /* The die_type call above may have already set the type for this DIE. */
17679 range_type = get_die_type (die, cu);
17680 if (range_type)
17681 return range_type;
17682
729efb13
SA
17683 low.kind = PROP_CONST;
17684 high.kind = PROP_CONST;
17685 high.data.const_val = 0;
17686
4fae6e18
JK
17687 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17688 omitting DW_AT_lower_bound. */
17689 switch (cu->language)
6e70227d 17690 {
4fae6e18
JK
17691 case language_c:
17692 case language_cplus:
729efb13 17693 low.data.const_val = 0;
4fae6e18
JK
17694 low_default_is_valid = 1;
17695 break;
17696 case language_fortran:
729efb13 17697 low.data.const_val = 1;
4fae6e18
JK
17698 low_default_is_valid = 1;
17699 break;
17700 case language_d:
4fae6e18 17701 case language_objc:
c44af4eb 17702 case language_rust:
729efb13 17703 low.data.const_val = 0;
4fae6e18
JK
17704 low_default_is_valid = (cu->header.version >= 4);
17705 break;
17706 case language_ada:
17707 case language_m2:
17708 case language_pascal:
729efb13 17709 low.data.const_val = 1;
4fae6e18
JK
17710 low_default_is_valid = (cu->header.version >= 4);
17711 break;
17712 default:
729efb13 17713 low.data.const_val = 0;
4fae6e18
JK
17714 low_default_is_valid = 0;
17715 break;
a02abb62
JB
17716 }
17717
e142c38c 17718 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17719 if (attr != nullptr)
9a49df9d 17720 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17721 else if (!low_default_is_valid)
b98664d3 17722 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17723 "- DIE at %s [in module %s]"),
17724 sect_offset_str (die->sect_off),
518817b3 17725 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17726
506f5c41
TV
17727 struct attribute *attr_ub, *attr_count;
17728 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17729 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17730 {
506f5c41 17731 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17732 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17733 {
c451ebe5
SA
17734 /* If bounds are constant do the final calculation here. */
17735 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17736 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17737 else
17738 high_bound_is_count = 1;
c2ff108b 17739 }
506f5c41
TV
17740 else
17741 {
17742 if (attr_ub != NULL)
17743 complaint (_("Unresolved DW_AT_upper_bound "
17744 "- DIE at %s [in module %s]"),
17745 sect_offset_str (die->sect_off),
17746 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17747 if (attr_count != NULL)
17748 complaint (_("Unresolved DW_AT_count "
17749 "- DIE at %s [in module %s]"),
17750 sect_offset_str (die->sect_off),
17751 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17752 }
e77813c8 17753 }
a02abb62 17754
4e962e74
TT
17755 LONGEST bias = 0;
17756 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17757 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17758 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17759
dbb9c2b1
JB
17760 /* Normally, the DWARF producers are expected to use a signed
17761 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17762 But this is unfortunately not always the case, as witnessed
17763 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17764 is used instead. To work around that ambiguity, we treat
17765 the bounds as signed, and thus sign-extend their values, when
17766 the base type is signed. */
6e70227d 17767 negative_mask =
d359392f 17768 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17769 if (low.kind == PROP_CONST
17770 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17771 low.data.const_val |= negative_mask;
17772 if (high.kind == PROP_CONST
17773 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17774 high.data.const_val |= negative_mask;
43bbcdc2 17775
5bbd8269
AB
17776 /* Check for bit and byte strides. */
17777 struct dynamic_prop byte_stride_prop;
17778 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17779 if (attr_byte_stride != nullptr)
17780 {
17781 struct type *prop_type
17782 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17783 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17784 prop_type);
17785 }
17786
17787 struct dynamic_prop bit_stride_prop;
17788 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17789 if (attr_bit_stride != nullptr)
17790 {
17791 /* It only makes sense to have either a bit or byte stride. */
17792 if (attr_byte_stride != nullptr)
17793 {
17794 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17795 "- DIE at %s [in module %s]"),
17796 sect_offset_str (die->sect_off),
17797 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17798 attr_bit_stride = nullptr;
17799 }
17800 else
17801 {
17802 struct type *prop_type
17803 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17804 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17805 prop_type);
17806 }
17807 }
17808
17809 if (attr_byte_stride != nullptr
17810 || attr_bit_stride != nullptr)
17811 {
17812 bool byte_stride_p = (attr_byte_stride != nullptr);
17813 struct dynamic_prop *stride
17814 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17815
17816 range_type
17817 = create_range_type_with_stride (NULL, orig_base_type, &low,
17818 &high, bias, stride, byte_stride_p);
17819 }
17820 else
17821 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17822
c451ebe5
SA
17823 if (high_bound_is_count)
17824 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17825
c2ff108b
JK
17826 /* Ada expects an empty array on no boundary attributes. */
17827 if (attr == NULL && cu->language != language_ada)
729efb13 17828 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17829
39cbfefa
DJ
17830 name = dwarf2_name (die, cu);
17831 if (name)
17832 TYPE_NAME (range_type) = name;
6e70227d 17833
e142c38c 17834 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17835 if (attr != nullptr)
a02abb62
JB
17836 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17837
2b4424c3
TT
17838 maybe_set_alignment (cu, die, range_type);
17839
7e314c57
JK
17840 set_die_type (die, range_type, cu);
17841
17842 /* set_die_type should be already done. */
b4ba55a1
JB
17843 set_descriptive_type (range_type, die, cu);
17844
7e314c57 17845 return range_type;
a02abb62 17846}
6e70227d 17847
f792889a 17848static struct type *
81a17f79
JB
17849read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17850{
17851 struct type *type;
81a17f79 17852
518817b3
SM
17853 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17854 NULL);
0114d602 17855 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17856
74a2f8ff 17857 /* In Ada, an unspecified type is typically used when the description
85102364 17858 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17859 such a type, we treat it as a stub, and try to resolve it later on,
17860 when needed. */
17861 if (cu->language == language_ada)
17862 TYPE_STUB (type) = 1;
17863
f792889a 17864 return set_die_type (die, type, cu);
81a17f79 17865}
a02abb62 17866
639d11d3
DC
17867/* Read a single die and all its descendents. Set the die's sibling
17868 field to NULL; set other fields in the die correctly, and set all
17869 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17870 location of the info_ptr after reading all of those dies. PARENT
17871 is the parent of the die in question. */
17872
17873static struct die_info *
dee91e82 17874read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17875 const gdb_byte *info_ptr,
17876 const gdb_byte **new_info_ptr,
dee91e82 17877 struct die_info *parent)
639d11d3
DC
17878{
17879 struct die_info *die;
d521ce57 17880 const gdb_byte *cur_ptr;
639d11d3 17881
3e225074 17882 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17883 if (die == NULL)
17884 {
17885 *new_info_ptr = cur_ptr;
17886 return NULL;
17887 }
93311388 17888 store_in_ref_table (die, reader->cu);
639d11d3 17889
3e225074 17890 if (die->has_children)
bf6af496 17891 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17892 else
17893 {
17894 die->child = NULL;
17895 *new_info_ptr = cur_ptr;
17896 }
17897
17898 die->sibling = NULL;
17899 die->parent = parent;
17900 return die;
17901}
17902
17903/* Read a die, all of its descendents, and all of its siblings; set
17904 all of the fields of all of the dies correctly. Arguments are as
17905 in read_die_and_children. */
17906
17907static struct die_info *
bf6af496 17908read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17909 const gdb_byte *info_ptr,
17910 const gdb_byte **new_info_ptr,
bf6af496 17911 struct die_info *parent)
639d11d3
DC
17912{
17913 struct die_info *first_die, *last_sibling;
d521ce57 17914 const gdb_byte *cur_ptr;
639d11d3 17915
c906108c 17916 cur_ptr = info_ptr;
639d11d3
DC
17917 first_die = last_sibling = NULL;
17918
17919 while (1)
c906108c 17920 {
639d11d3 17921 struct die_info *die
dee91e82 17922 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17923
1d325ec1 17924 if (die == NULL)
c906108c 17925 {
639d11d3
DC
17926 *new_info_ptr = cur_ptr;
17927 return first_die;
c906108c 17928 }
1d325ec1
DJ
17929
17930 if (!first_die)
17931 first_die = die;
c906108c 17932 else
1d325ec1
DJ
17933 last_sibling->sibling = die;
17934
17935 last_sibling = die;
c906108c 17936 }
c906108c
SS
17937}
17938
bf6af496
DE
17939/* Read a die, all of its descendents, and all of its siblings; set
17940 all of the fields of all of the dies correctly. Arguments are as
17941 in read_die_and_children.
17942 This the main entry point for reading a DIE and all its children. */
17943
17944static struct die_info *
17945read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17946 const gdb_byte *info_ptr,
17947 const gdb_byte **new_info_ptr,
bf6af496
DE
17948 struct die_info *parent)
17949{
17950 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17951 new_info_ptr, parent);
17952
b4f54984 17953 if (dwarf_die_debug)
bf6af496
DE
17954 {
17955 fprintf_unfiltered (gdb_stdlog,
17956 "Read die from %s@0x%x of %s:\n",
96b79293 17957 reader->die_section->get_name (),
bf6af496
DE
17958 (unsigned) (info_ptr - reader->die_section->buffer),
17959 bfd_get_filename (reader->abfd));
b4f54984 17960 dump_die (die, dwarf_die_debug);
bf6af496
DE
17961 }
17962
17963 return die;
17964}
17965
3019eac3
DE
17966/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17967 attributes.
17968 The caller is responsible for filling in the extra attributes
17969 and updating (*DIEP)->num_attrs.
17970 Set DIEP to point to a newly allocated die with its information,
3e225074 17971 except for its child, sibling, and parent fields. */
93311388 17972
d521ce57 17973static const gdb_byte *
3019eac3 17974read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17975 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17976 int num_extra_attrs)
93311388 17977{
b64f50a1 17978 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17979 struct abbrev_info *abbrev;
17980 struct die_info *die;
17981 struct dwarf2_cu *cu = reader->cu;
17982 bfd *abfd = reader->abfd;
17983
9c541725 17984 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17985 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17986 info_ptr += bytes_read;
17987 if (!abbrev_number)
17988 {
17989 *diep = NULL;
93311388
DE
17990 return info_ptr;
17991 }
17992
685af9cd 17993 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17994 if (!abbrev)
348e048f
DE
17995 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17996 abbrev_number,
17997 bfd_get_filename (abfd));
17998
3019eac3 17999 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18000 die->sect_off = sect_off;
93311388
DE
18001 die->tag = abbrev->tag;
18002 die->abbrev = abbrev_number;
3e225074 18003 die->has_children = abbrev->has_children;
93311388 18004
3019eac3
DE
18005 /* Make the result usable.
18006 The caller needs to update num_attrs after adding the extra
18007 attributes. */
93311388
DE
18008 die->num_attrs = abbrev->num_attrs;
18009
18a8505e 18010 std::vector<int> indexes_that_need_reprocess;
93311388 18011 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18012 {
18013 bool need_reprocess;
18014 info_ptr =
18015 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18016 info_ptr, &need_reprocess);
18017 if (need_reprocess)
18018 indexes_that_need_reprocess.push_back (i);
18019 }
18020
18021 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18022 if (attr != nullptr)
18023 cu->str_offsets_base = DW_UNSND (attr);
93311388 18024
18a8505e
AT
18025 auto maybe_addr_base = lookup_addr_base(die);
18026 if (maybe_addr_base.has_value ())
18027 cu->addr_base = *maybe_addr_base;
18028 for (int index : indexes_that_need_reprocess)
18029 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18030 *diep = die;
93311388
DE
18031 return info_ptr;
18032}
18033
3019eac3
DE
18034/* Read a die and all its attributes.
18035 Set DIEP to point to a newly allocated die with its information,
3e225074 18036 except for its child, sibling, and parent fields. */
3019eac3 18037
d521ce57 18038static const gdb_byte *
3019eac3 18039read_full_die (const struct die_reader_specs *reader,
3e225074 18040 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18041{
d521ce57 18042 const gdb_byte *result;
bf6af496 18043
3e225074 18044 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18045
b4f54984 18046 if (dwarf_die_debug)
bf6af496
DE
18047 {
18048 fprintf_unfiltered (gdb_stdlog,
18049 "Read die from %s@0x%x of %s:\n",
96b79293 18050 reader->die_section->get_name (),
bf6af496
DE
18051 (unsigned) (info_ptr - reader->die_section->buffer),
18052 bfd_get_filename (reader->abfd));
b4f54984 18053 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18054 }
18055
18056 return result;
3019eac3 18057}
433df2d4 18058\f
c906108c 18059
72bf9492
DJ
18060/* Returns nonzero if TAG represents a type that we might generate a partial
18061 symbol for. */
18062
18063static int
18064is_type_tag_for_partial (int tag)
18065{
18066 switch (tag)
18067 {
18068#if 0
18069 /* Some types that would be reasonable to generate partial symbols for,
18070 that we don't at present. */
18071 case DW_TAG_array_type:
18072 case DW_TAG_file_type:
18073 case DW_TAG_ptr_to_member_type:
18074 case DW_TAG_set_type:
18075 case DW_TAG_string_type:
18076 case DW_TAG_subroutine_type:
18077#endif
18078 case DW_TAG_base_type:
18079 case DW_TAG_class_type:
680b30c7 18080 case DW_TAG_interface_type:
72bf9492
DJ
18081 case DW_TAG_enumeration_type:
18082 case DW_TAG_structure_type:
18083 case DW_TAG_subrange_type:
18084 case DW_TAG_typedef:
18085 case DW_TAG_union_type:
18086 return 1;
18087 default:
18088 return 0;
18089 }
18090}
18091
18092/* Load all DIEs that are interesting for partial symbols into memory. */
18093
18094static struct partial_die_info *
dee91e82 18095load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18096 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18097{
dee91e82 18098 struct dwarf2_cu *cu = reader->cu;
518817b3 18099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18100 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18101 unsigned int bytes_read;
5afb4e99 18102 unsigned int load_all = 0;
72bf9492
DJ
18103 int nesting_level = 1;
18104
18105 parent_die = NULL;
18106 last_die = NULL;
18107
7adf1e79
DE
18108 gdb_assert (cu->per_cu != NULL);
18109 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18110 load_all = 1;
18111
72bf9492
DJ
18112 cu->partial_dies
18113 = htab_create_alloc_ex (cu->header.length / 12,
18114 partial_die_hash,
18115 partial_die_eq,
18116 NULL,
18117 &cu->comp_unit_obstack,
18118 hashtab_obstack_allocate,
18119 dummy_obstack_deallocate);
18120
72bf9492
DJ
18121 while (1)
18122 {
685af9cd 18123 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18124
18125 /* A NULL abbrev means the end of a series of children. */
18126 if (abbrev == NULL)
18127 {
18128 if (--nesting_level == 0)
cd9983dd
YQ
18129 return first_die;
18130
72bf9492
DJ
18131 info_ptr += bytes_read;
18132 last_die = parent_die;
18133 parent_die = parent_die->die_parent;
18134 continue;
18135 }
18136
98bfdba5
PA
18137 /* Check for template arguments. We never save these; if
18138 they're seen, we just mark the parent, and go on our way. */
18139 if (parent_die != NULL
18140 && cu->language == language_cplus
18141 && (abbrev->tag == DW_TAG_template_type_param
18142 || abbrev->tag == DW_TAG_template_value_param))
18143 {
18144 parent_die->has_template_arguments = 1;
18145
18146 if (!load_all)
18147 {
18148 /* We don't need a partial DIE for the template argument. */
dee91e82 18149 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18150 continue;
18151 }
18152 }
18153
0d99eb77 18154 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18155 Skip their other children. */
18156 if (!load_all
18157 && cu->language == language_cplus
18158 && parent_die != NULL
18159 && parent_die->tag == DW_TAG_subprogram)
18160 {
dee91e82 18161 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18162 continue;
18163 }
18164
5afb4e99
DJ
18165 /* Check whether this DIE is interesting enough to save. Normally
18166 we would not be interested in members here, but there may be
18167 later variables referencing them via DW_AT_specification (for
18168 static members). */
18169 if (!load_all
18170 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18171 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18172 && abbrev->tag != DW_TAG_enumerator
18173 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18174 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18175 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18176 && abbrev->tag != DW_TAG_variable
5afb4e99 18177 && abbrev->tag != DW_TAG_namespace
f55ee35c 18178 && abbrev->tag != DW_TAG_module
95554aad 18179 && abbrev->tag != DW_TAG_member
74921315
KS
18180 && abbrev->tag != DW_TAG_imported_unit
18181 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18182 {
18183 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18184 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18185 continue;
18186 }
18187
6f06d47b
YQ
18188 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18189 abbrev);
cd9983dd 18190
48fbe735 18191 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18192
18193 /* This two-pass algorithm for processing partial symbols has a
18194 high cost in cache pressure. Thus, handle some simple cases
18195 here which cover the majority of C partial symbols. DIEs
18196 which neither have specification tags in them, nor could have
18197 specification tags elsewhere pointing at them, can simply be
18198 processed and discarded.
18199
18200 This segment is also optional; scan_partial_symbols and
18201 add_partial_symbol will handle these DIEs if we chain
18202 them in normally. When compilers which do not emit large
18203 quantities of duplicate debug information are more common,
18204 this code can probably be removed. */
18205
18206 /* Any complete simple types at the top level (pretty much all
18207 of them, for a language without namespaces), can be processed
18208 directly. */
18209 if (parent_die == NULL
cd9983dd
YQ
18210 && pdi.has_specification == 0
18211 && pdi.is_declaration == 0
18212 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18213 || pdi.tag == DW_TAG_base_type
18214 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18215 {
cd9983dd 18216 if (building_psymtab && pdi.name != NULL)
31edb802 18217 add_psymbol_to_list (pdi.name, false,
79748972 18218 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18219 psymbol_placement::STATIC,
1762568f 18220 0, cu->language, objfile);
cd9983dd 18221 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18222 continue;
18223 }
18224
d8228535
JK
18225 /* The exception for DW_TAG_typedef with has_children above is
18226 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18227 type_name_or_error will error on such types later.
d8228535
JK
18228
18229 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18230 it could not find the child DIEs referenced later, this is checked
18231 above. In correct DWARF DW_TAG_typedef should have no children. */
18232
cd9983dd 18233 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18234 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18235 "- DIE at %s [in module %s]"),
cd9983dd 18236 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18237
72bf9492
DJ
18238 /* If we're at the second level, and we're an enumerator, and
18239 our parent has no specification (meaning possibly lives in a
18240 namespace elsewhere), then we can add the partial symbol now
18241 instead of queueing it. */
cd9983dd 18242 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18243 && parent_die != NULL
18244 && parent_die->die_parent == NULL
18245 && parent_die->tag == DW_TAG_enumeration_type
18246 && parent_die->has_specification == 0)
18247 {
cd9983dd 18248 if (pdi.name == NULL)
b98664d3 18249 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18250 else if (building_psymtab)
31edb802 18251 add_psymbol_to_list (pdi.name, false,
79748972 18252 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18253 cu->language == language_cplus
75aedd27
TT
18254 ? psymbol_placement::GLOBAL
18255 : psymbol_placement::STATIC,
1762568f 18256 0, cu->language, objfile);
72bf9492 18257
cd9983dd 18258 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18259 continue;
18260 }
18261
cd9983dd 18262 struct partial_die_info *part_die
6f06d47b 18263 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18264
72bf9492
DJ
18265 /* We'll save this DIE so link it in. */
18266 part_die->die_parent = parent_die;
18267 part_die->die_sibling = NULL;
18268 part_die->die_child = NULL;
18269
18270 if (last_die && last_die == parent_die)
18271 last_die->die_child = part_die;
18272 else if (last_die)
18273 last_die->die_sibling = part_die;
18274
18275 last_die = part_die;
18276
18277 if (first_die == NULL)
18278 first_die = part_die;
18279
18280 /* Maybe add the DIE to the hash table. Not all DIEs that we
18281 find interesting need to be in the hash table, because we
18282 also have the parent/sibling/child chains; only those that we
18283 might refer to by offset later during partial symbol reading.
18284
18285 For now this means things that might have be the target of a
18286 DW_AT_specification, DW_AT_abstract_origin, or
18287 DW_AT_extension. DW_AT_extension will refer only to
18288 namespaces; DW_AT_abstract_origin refers to functions (and
18289 many things under the function DIE, but we do not recurse
18290 into function DIEs during partial symbol reading) and
18291 possibly variables as well; DW_AT_specification refers to
18292 declarations. Declarations ought to have the DW_AT_declaration
18293 flag. It happens that GCC forgets to put it in sometimes, but
18294 only for functions, not for types.
18295
18296 Adding more things than necessary to the hash table is harmless
18297 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18298 wasted time in find_partial_die, when we reread the compilation
18299 unit with load_all_dies set. */
72bf9492 18300
5afb4e99 18301 if (load_all
72929c62 18302 || abbrev->tag == DW_TAG_constant
5afb4e99 18303 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18304 || abbrev->tag == DW_TAG_variable
18305 || abbrev->tag == DW_TAG_namespace
18306 || part_die->is_declaration)
18307 {
18308 void **slot;
18309
18310 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18311 to_underlying (part_die->sect_off),
18312 INSERT);
72bf9492
DJ
18313 *slot = part_die;
18314 }
18315
72bf9492 18316 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18317 we have no reason to follow the children of structures; for other
98bfdba5
PA
18318 languages we have to, so that we can get at method physnames
18319 to infer fully qualified class names, for DW_AT_specification,
18320 and for C++ template arguments. For C++, we also look one level
18321 inside functions to find template arguments (if the name of the
18322 function does not already contain the template arguments).
bc30ff58 18323
0a4b0913
AB
18324 For Ada and Fortran, we need to scan the children of subprograms
18325 and lexical blocks as well because these languages allow the
18326 definition of nested entities that could be interesting for the
18327 debugger, such as nested subprograms for instance. */
72bf9492 18328 if (last_die->has_children
5afb4e99
DJ
18329 && (load_all
18330 || last_die->tag == DW_TAG_namespace
f55ee35c 18331 || last_die->tag == DW_TAG_module
72bf9492 18332 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18333 || (cu->language == language_cplus
18334 && last_die->tag == DW_TAG_subprogram
18335 && (last_die->name == NULL
18336 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18337 || (cu->language != language_c
18338 && (last_die->tag == DW_TAG_class_type
680b30c7 18339 || last_die->tag == DW_TAG_interface_type
72bf9492 18340 || last_die->tag == DW_TAG_structure_type
bc30ff58 18341 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18342 || ((cu->language == language_ada
18343 || cu->language == language_fortran)
bc30ff58
JB
18344 && (last_die->tag == DW_TAG_subprogram
18345 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18346 {
18347 nesting_level++;
18348 parent_die = last_die;
18349 continue;
18350 }
18351
18352 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18353 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18354
18355 /* Back to the top, do it again. */
18356 }
18357}
18358
6f06d47b
YQ
18359partial_die_info::partial_die_info (sect_offset sect_off_,
18360 struct abbrev_info *abbrev)
18361 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18362{
18363}
18364
35cc7ed7
YQ
18365/* Read a minimal amount of information into the minimal die structure.
18366 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18367
48fbe735
YQ
18368const gdb_byte *
18369partial_die_info::read (const struct die_reader_specs *reader,
18370 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18371{
dee91e82 18372 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18373 struct dwarf2_per_objfile *dwarf2_per_objfile
18374 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18375 unsigned int i;
c5aa993b 18376 int has_low_pc_attr = 0;
c906108c 18377 int has_high_pc_attr = 0;
91da1414 18378 int high_pc_relative = 0;
c906108c 18379
18a8505e 18380 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 18381 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18382 {
18a8505e
AT
18383 bool need_reprocess;
18384 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18385 info_ptr, &need_reprocess);
18386 /* String and address offsets that need to do the reprocessing have
18387 already been read at this point, so there is no need to wait until
18388 the loop terminates to do the reprocessing. */
18389 if (need_reprocess)
18390 read_attribute_reprocess (reader, &attr_vec[i]);
18391 attribute &attr = attr_vec[i];
c906108c 18392 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18393 partial symbol table. */
c906108c
SS
18394 switch (attr.name)
18395 {
18396 case DW_AT_name:
48fbe735 18397 switch (tag)
71c25dea
TT
18398 {
18399 case DW_TAG_compile_unit:
95554aad 18400 case DW_TAG_partial_unit:
348e048f 18401 case DW_TAG_type_unit:
71c25dea
TT
18402 /* Compilation units have a DW_AT_name that is a filename, not
18403 a source language identifier. */
18404 case DW_TAG_enumeration_type:
18405 case DW_TAG_enumerator:
18406 /* These tags always have simple identifiers already; no need
18407 to canonicalize them. */
48fbe735 18408 name = DW_STRING (&attr);
71c25dea
TT
18409 break;
18410 default:
48fbe735
YQ
18411 {
18412 struct objfile *objfile = dwarf2_per_objfile->objfile;
18413
18414 name
18415 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18416 &objfile->per_bfd->storage_obstack);
18417 }
71c25dea
TT
18418 break;
18419 }
c906108c 18420 break;
31ef98ae 18421 case DW_AT_linkage_name:
c906108c 18422 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18423 /* Note that both forms of linkage name might appear. We
18424 assume they will be the same, and we only store the last
18425 one we see. */
48fbe735 18426 linkage_name = DW_STRING (&attr);
c906108c
SS
18427 break;
18428 case DW_AT_low_pc:
18429 has_low_pc_attr = 1;
cd6c91b4 18430 lowpc = attr.value_as_address ();
c906108c
SS
18431 break;
18432 case DW_AT_high_pc:
18433 has_high_pc_attr = 1;
cd6c91b4
TT
18434 highpc = attr.value_as_address ();
18435 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18436 high_pc_relative = 1;
c906108c
SS
18437 break;
18438 case DW_AT_location:
0963b4bd 18439 /* Support the .debug_loc offsets. */
4fc6c0d5 18440 if (attr.form_is_block ())
8e19ed76 18441 {
48fbe735 18442 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18443 }
cd6c91b4 18444 else if (attr.form_is_section_offset ())
8e19ed76 18445 {
4d3c2250 18446 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18447 }
18448 else
18449 {
4d3c2250
KB
18450 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18451 "partial symbol information");
8e19ed76 18452 }
c906108c 18453 break;
c906108c 18454 case DW_AT_external:
48fbe735 18455 is_external = DW_UNSND (&attr);
c906108c
SS
18456 break;
18457 case DW_AT_declaration:
48fbe735 18458 is_declaration = DW_UNSND (&attr);
c906108c
SS
18459 break;
18460 case DW_AT_type:
48fbe735 18461 has_type = 1;
c906108c
SS
18462 break;
18463 case DW_AT_abstract_origin:
18464 case DW_AT_specification:
72bf9492 18465 case DW_AT_extension:
48fbe735
YQ
18466 has_specification = 1;
18467 spec_offset = dwarf2_get_ref_die_offset (&attr);
18468 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18469 || cu->per_cu->is_dwz);
c906108c
SS
18470 break;
18471 case DW_AT_sibling:
18472 /* Ignore absolute siblings, they might point outside of
18473 the current compile unit. */
18474 if (attr.form == DW_FORM_ref_addr)
b98664d3 18475 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18476 else
b9502d3f 18477 {
48fbe735 18478 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18479 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18480 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18481
18482 if (sibling_ptr < info_ptr)
b98664d3 18483 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18484 else if (sibling_ptr > reader->buffer_end)
18485 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18486 else
48fbe735 18487 sibling = sibling_ptr;
b9502d3f 18488 }
c906108c 18489 break;
fa4028e9 18490 case DW_AT_byte_size:
48fbe735 18491 has_byte_size = 1;
fa4028e9 18492 break;
ff908ebf 18493 case DW_AT_const_value:
48fbe735 18494 has_const_value = 1;
ff908ebf 18495 break;
68511cec
CES
18496 case DW_AT_calling_convention:
18497 /* DWARF doesn't provide a way to identify a program's source-level
18498 entry point. DW_AT_calling_convention attributes are only meant
18499 to describe functions' calling conventions.
18500
18501 However, because it's a necessary piece of information in
0c1b455e
TT
18502 Fortran, and before DWARF 4 DW_CC_program was the only
18503 piece of debugging information whose definition refers to
18504 a 'main program' at all, several compilers marked Fortran
18505 main programs with DW_CC_program --- even when those
18506 functions use the standard calling conventions.
18507
18508 Although DWARF now specifies a way to provide this
18509 information, we support this practice for backward
18510 compatibility. */
68511cec 18511 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18512 && cu->language == language_fortran)
48fbe735 18513 main_subprogram = 1;
68511cec 18514 break;
481860b3
GB
18515 case DW_AT_inline:
18516 if (DW_UNSND (&attr) == DW_INL_inlined
18517 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18518 may_be_inlined = 1;
481860b3 18519 break;
95554aad
TT
18520
18521 case DW_AT_import:
48fbe735 18522 if (tag == DW_TAG_imported_unit)
36586728 18523 {
48fbe735
YQ
18524 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18525 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18526 || cu->per_cu->is_dwz);
18527 }
95554aad
TT
18528 break;
18529
0c1b455e 18530 case DW_AT_main_subprogram:
48fbe735 18531 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18532 break;
18533
05caa1d2
TT
18534 case DW_AT_ranges:
18535 {
18536 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18537 but that requires a full DIE, so instead we just
18538 reimplement it. */
18539 int need_ranges_base = tag != DW_TAG_compile_unit;
18540 unsigned int ranges_offset = (DW_UNSND (&attr)
18541 + (need_ranges_base
18542 ? cu->ranges_base
18543 : 0));
18544
18545 /* Value of the DW_AT_ranges attribute is the offset in the
18546 .debug_ranges section. */
18547 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18548 nullptr))
18549 has_pc_info = 1;
18550 }
18551 break;
18552
c906108c
SS
18553 default:
18554 break;
18555 }
18556 }
18557
10d06d82
TT
18558 /* For Ada, if both the name and the linkage name appear, we prefer
18559 the latter. This lets "catch exception" work better, regardless
18560 of the order in which the name and linkage name were emitted.
18561 Really, though, this is just a workaround for the fact that gdb
18562 doesn't store both the name and the linkage name. */
18563 if (cu->language == language_ada && linkage_name != nullptr)
18564 name = linkage_name;
18565
91da1414 18566 if (high_pc_relative)
48fbe735 18567 highpc += lowpc;
91da1414 18568
9373cf26
JK
18569 if (has_low_pc_attr && has_high_pc_attr)
18570 {
18571 /* When using the GNU linker, .gnu.linkonce. sections are used to
18572 eliminate duplicate copies of functions and vtables and such.
18573 The linker will arbitrarily choose one and discard the others.
18574 The AT_*_pc values for such functions refer to local labels in
18575 these sections. If the section from that file was discarded, the
18576 labels are not in the output, so the relocs get a value of 0.
18577 If this is a discarded function, mark the pc bounds as invalid,
18578 so that GDB will ignore it. */
48fbe735 18579 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18580 {
48fbe735 18581 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18582 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18583
b98664d3 18584 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18585 "for DIE at %s [in module %s]"),
48fbe735
YQ
18586 paddress (gdbarch, lowpc),
18587 sect_offset_str (sect_off),
9d8780f0 18588 objfile_name (objfile));
9373cf26
JK
18589 }
18590 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18591 else if (lowpc >= highpc)
9373cf26 18592 {
48fbe735 18593 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18594 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18595
b98664d3 18596 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18597 "for DIE at %s [in module %s]"),
48fbe735
YQ
18598 paddress (gdbarch, lowpc),
18599 paddress (gdbarch, highpc),
18600 sect_offset_str (sect_off),
9c541725 18601 objfile_name (objfile));
9373cf26
JK
18602 }
18603 else
48fbe735 18604 has_pc_info = 1;
9373cf26 18605 }
85cbf3d3 18606
c906108c
SS
18607 return info_ptr;
18608}
18609
72bf9492
DJ
18610/* Find a cached partial DIE at OFFSET in CU. */
18611
d590ff25
YQ
18612struct partial_die_info *
18613dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18614{
18615 struct partial_die_info *lookup_die = NULL;
6f06d47b 18616 struct partial_die_info part_die (sect_off);
72bf9492 18617
9a3c8263 18618 lookup_die = ((struct partial_die_info *)
d590ff25 18619 htab_find_with_hash (partial_dies, &part_die,
9c541725 18620 to_underlying (sect_off)));
72bf9492 18621
72bf9492
DJ
18622 return lookup_die;
18623}
18624
348e048f
DE
18625/* Find a partial DIE at OFFSET, which may or may not be in CU,
18626 except in the case of .debug_types DIEs which do not reference
18627 outside their CU (they do however referencing other types via
55f1336d 18628 DW_FORM_ref_sig8). */
72bf9492 18629
122cf0f2 18630static const struct cu_partial_die_info
9c541725 18631find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18632{
518817b3
SM
18633 struct dwarf2_per_objfile *dwarf2_per_objfile
18634 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18635 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18636 struct dwarf2_per_cu_data *per_cu = NULL;
18637 struct partial_die_info *pd = NULL;
72bf9492 18638
36586728 18639 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18640 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18641 {
d590ff25 18642 pd = cu->find_partial_die (sect_off);
5afb4e99 18643 if (pd != NULL)
fb816e8b 18644 return { cu, pd };
0d99eb77
DE
18645 /* We missed recording what we needed.
18646 Load all dies and try again. */
18647 per_cu = cu->per_cu;
5afb4e99 18648 }
0d99eb77
DE
18649 else
18650 {
18651 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18652 if (cu->per_cu->is_debug_types)
0d99eb77 18653 {
9d8780f0
SM
18654 error (_("Dwarf Error: Type Unit at offset %s contains"
18655 " external reference to offset %s [in module %s].\n"),
18656 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18657 bfd_get_filename (objfile->obfd));
18658 }
9c541725 18659 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18660 dwarf2_per_objfile);
72bf9492 18661
0d99eb77
DE
18662 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18663 load_partial_comp_unit (per_cu);
ae038cb0 18664
0d99eb77 18665 per_cu->cu->last_used = 0;
d590ff25 18666 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18667 }
5afb4e99 18668
dee91e82
DE
18669 /* If we didn't find it, and not all dies have been loaded,
18670 load them all and try again. */
18671
5afb4e99
DJ
18672 if (pd == NULL && per_cu->load_all_dies == 0)
18673 {
5afb4e99 18674 per_cu->load_all_dies = 1;
fd820528
DE
18675
18676 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18677 THIS_CU->cu may already be in use. So we can't just free it and
18678 replace its DIEs with the ones we read in. Instead, we leave those
18679 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18680 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18681 set. */
dee91e82 18682 load_partial_comp_unit (per_cu);
5afb4e99 18683
d590ff25 18684 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18685 }
18686
18687 if (pd == NULL)
18688 internal_error (__FILE__, __LINE__,
9d8780f0 18689 _("could not find partial DIE %s "
3e43a32a 18690 "in cache [from module %s]\n"),
9d8780f0 18691 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18692 return { per_cu->cu, pd };
72bf9492
DJ
18693}
18694
abc72ce4
DE
18695/* See if we can figure out if the class lives in a namespace. We do
18696 this by looking for a member function; its demangled name will
18697 contain namespace info, if there is any. */
18698
18699static void
18700guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18701 struct dwarf2_cu *cu)
18702{
18703 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18704 what template types look like, because the demangler
18705 frequently doesn't give the same name as the debug info. We
18706 could fix this by only using the demangled name to get the
18707 prefix (but see comment in read_structure_type). */
18708
18709 struct partial_die_info *real_pdi;
18710 struct partial_die_info *child_pdi;
18711
18712 /* If this DIE (this DIE's specification, if any) has a parent, then
18713 we should not do this. We'll prepend the parent's fully qualified
18714 name when we create the partial symbol. */
18715
18716 real_pdi = struct_pdi;
18717 while (real_pdi->has_specification)
fb816e8b 18718 {
122cf0f2
AB
18719 auto res = find_partial_die (real_pdi->spec_offset,
18720 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18721 real_pdi = res.pdi;
18722 cu = res.cu;
18723 }
abc72ce4
DE
18724
18725 if (real_pdi->die_parent != NULL)
18726 return;
18727
18728 for (child_pdi = struct_pdi->die_child;
18729 child_pdi != NULL;
18730 child_pdi = child_pdi->die_sibling)
18731 {
18732 if (child_pdi->tag == DW_TAG_subprogram
18733 && child_pdi->linkage_name != NULL)
18734 {
43816ebc
TT
18735 gdb::unique_xmalloc_ptr<char> actual_class_name
18736 (language_class_name_from_physname (cu->language_defn,
18737 child_pdi->linkage_name));
abc72ce4
DE
18738 if (actual_class_name != NULL)
18739 {
518817b3 18740 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18741 struct_pdi->name
021887d8 18742 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 18743 actual_class_name.get ());
abc72ce4
DE
18744 }
18745 break;
18746 }
18747 }
18748}
18749
52356b79
YQ
18750void
18751partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18752{
abc72ce4
DE
18753 /* Once we've fixed up a die, there's no point in doing so again.
18754 This also avoids a memory leak if we were to call
18755 guess_partial_die_structure_name multiple times. */
52356b79 18756 if (fixup_called)
abc72ce4
DE
18757 return;
18758
72bf9492
DJ
18759 /* If we found a reference attribute and the DIE has no name, try
18760 to find a name in the referred to DIE. */
18761
52356b79 18762 if (name == NULL && has_specification)
72bf9492
DJ
18763 {
18764 struct partial_die_info *spec_die;
72bf9492 18765
122cf0f2 18766 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18767 spec_die = res.pdi;
18768 cu = res.cu;
72bf9492 18769
52356b79 18770 spec_die->fixup (cu);
72bf9492
DJ
18771
18772 if (spec_die->name)
18773 {
52356b79 18774 name = spec_die->name;
72bf9492
DJ
18775
18776 /* Copy DW_AT_external attribute if it is set. */
18777 if (spec_die->is_external)
52356b79 18778 is_external = spec_die->is_external;
72bf9492
DJ
18779 }
18780 }
18781
18782 /* Set default names for some unnamed DIEs. */
72bf9492 18783
52356b79
YQ
18784 if (name == NULL && tag == DW_TAG_namespace)
18785 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18786
abc72ce4
DE
18787 /* If there is no parent die to provide a namespace, and there are
18788 children, see if we can determine the namespace from their linkage
122d1940 18789 name. */
abc72ce4 18790 if (cu->language == language_cplus
fd5866f6 18791 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18792 && die_parent == NULL
18793 && has_children
18794 && (tag == DW_TAG_class_type
18795 || tag == DW_TAG_structure_type
18796 || tag == DW_TAG_union_type))
18797 guess_partial_die_structure_name (this, cu);
abc72ce4 18798
53832f31
TT
18799 /* GCC might emit a nameless struct or union that has a linkage
18800 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18801 if (name == NULL
18802 && (tag == DW_TAG_class_type
18803 || tag == DW_TAG_interface_type
18804 || tag == DW_TAG_structure_type
18805 || tag == DW_TAG_union_type)
18806 && linkage_name != NULL)
53832f31 18807 {
43816ebc
TT
18808 gdb::unique_xmalloc_ptr<char> demangled
18809 (gdb_demangle (linkage_name, DMGL_TYPES));
18810 if (demangled != nullptr)
53832f31 18811 {
96408a79
SA
18812 const char *base;
18813
18814 /* Strip any leading namespaces/classes, keep only the base name.
18815 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18816 base = strrchr (demangled.get (), ':');
18817 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18818 base++;
18819 else
43816ebc 18820 base = demangled.get ();
96408a79 18821
518817b3 18822 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 18823 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
18824 }
18825 }
18826
52356b79 18827 fixup_called = 1;
72bf9492
DJ
18828}
18829
18a8505e
AT
18830/* Process the attributes that had to be skipped in the first round. These
18831 attributes are the ones that need str_offsets_base or addr_base attributes.
18832 They could not have been processed in the first round, because at the time
18833 the values of str_offsets_base or addr_base may not have been known. */
18834void read_attribute_reprocess (const struct die_reader_specs *reader,
18835 struct attribute *attr)
18836{
18837 struct dwarf2_cu *cu = reader->cu;
18838 switch (attr->form)
18839 {
18840 case DW_FORM_addrx:
18841 case DW_FORM_GNU_addr_index:
18842 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18843 break;
18844 case DW_FORM_strx:
18845 case DW_FORM_strx1:
18846 case DW_FORM_strx2:
18847 case DW_FORM_strx3:
18848 case DW_FORM_strx4:
18849 case DW_FORM_GNU_str_index:
18850 {
18851 unsigned int str_index = DW_UNSND (attr);
18852 if (reader->dwo_file != NULL)
18853 {
18854 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18855 DW_STRING_IS_CANONICAL (attr) = 0;
18856 }
18857 else
18858 {
18859 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18860 DW_STRING_IS_CANONICAL (attr) = 0;
18861 }
18862 break;
18863 }
18864 default:
18865 gdb_assert_not_reached (_("Unexpected DWARF form."));
18866 }
18867}
18868
a8329558 18869/* Read an attribute value described by an attribute form. */
c906108c 18870
d521ce57 18871static const gdb_byte *
dee91e82
DE
18872read_attribute_value (const struct die_reader_specs *reader,
18873 struct attribute *attr, unsigned form,
18a8505e
AT
18874 LONGEST implicit_const, const gdb_byte *info_ptr,
18875 bool *need_reprocess)
c906108c 18876{
dee91e82 18877 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18878 struct dwarf2_per_objfile *dwarf2_per_objfile
18879 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18880 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18881 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18882 bfd *abfd = reader->abfd;
e7c27a73 18883 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18884 unsigned int bytes_read;
18885 struct dwarf_block *blk;
18a8505e 18886 *need_reprocess = false;
c906108c 18887
aead7601 18888 attr->form = (enum dwarf_form) form;
a8329558 18889 switch (form)
c906108c 18890 {
c906108c 18891 case DW_FORM_ref_addr:
ae411497 18892 if (cu->header.version == 2)
4568ecf9 18893 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18894 else
4568ecf9
DE
18895 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18896 &cu->header, &bytes_read);
ae411497
TT
18897 info_ptr += bytes_read;
18898 break;
36586728
TT
18899 case DW_FORM_GNU_ref_alt:
18900 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18901 info_ptr += bytes_read;
18902 break;
ae411497 18903 case DW_FORM_addr:
e7c27a73 18904 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18905 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18906 info_ptr += bytes_read;
c906108c
SS
18907 break;
18908 case DW_FORM_block2:
7b5a2f43 18909 blk = dwarf_alloc_block (cu);
c906108c
SS
18910 blk->size = read_2_bytes (abfd, info_ptr);
18911 info_ptr += 2;
18912 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18913 info_ptr += blk->size;
18914 DW_BLOCK (attr) = blk;
18915 break;
18916 case DW_FORM_block4:
7b5a2f43 18917 blk = dwarf_alloc_block (cu);
c906108c
SS
18918 blk->size = read_4_bytes (abfd, info_ptr);
18919 info_ptr += 4;
18920 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18921 info_ptr += blk->size;
18922 DW_BLOCK (attr) = blk;
18923 break;
18924 case DW_FORM_data2:
18925 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18926 info_ptr += 2;
18927 break;
18928 case DW_FORM_data4:
18929 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18930 info_ptr += 4;
18931 break;
18932 case DW_FORM_data8:
18933 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18934 info_ptr += 8;
18935 break;
0224619f
JK
18936 case DW_FORM_data16:
18937 blk = dwarf_alloc_block (cu);
18938 blk->size = 16;
18939 blk->data = read_n_bytes (abfd, info_ptr, 16);
18940 info_ptr += 16;
18941 DW_BLOCK (attr) = blk;
18942 break;
2dc7f7b3
TT
18943 case DW_FORM_sec_offset:
18944 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18945 info_ptr += bytes_read;
18946 break;
c906108c 18947 case DW_FORM_string:
9b1c24c8 18948 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18949 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18950 info_ptr += bytes_read;
18951 break;
4bdf3d34 18952 case DW_FORM_strp:
36586728
TT
18953 if (!cu->per_cu->is_dwz)
18954 {
ed2dc618
SM
18955 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18956 abfd, info_ptr, cu_header,
36586728
TT
18957 &bytes_read);
18958 DW_STRING_IS_CANONICAL (attr) = 0;
18959 info_ptr += bytes_read;
18960 break;
18961 }
18962 /* FALLTHROUGH */
43988095
JK
18963 case DW_FORM_line_strp:
18964 if (!cu->per_cu->is_dwz)
18965 {
ed2dc618
SM
18966 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18967 abfd, info_ptr,
43988095
JK
18968 cu_header, &bytes_read);
18969 DW_STRING_IS_CANONICAL (attr) = 0;
18970 info_ptr += bytes_read;
18971 break;
18972 }
18973 /* FALLTHROUGH */
36586728
TT
18974 case DW_FORM_GNU_strp_alt:
18975 {
ed2dc618 18976 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
18977 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18978 &bytes_read);
18979
ed2dc618
SM
18980 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18981 dwz, str_offset);
36586728
TT
18982 DW_STRING_IS_CANONICAL (attr) = 0;
18983 info_ptr += bytes_read;
18984 }
4bdf3d34 18985 break;
2dc7f7b3 18986 case DW_FORM_exprloc:
c906108c 18987 case DW_FORM_block:
7b5a2f43 18988 blk = dwarf_alloc_block (cu);
c906108c
SS
18989 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18990 info_ptr += bytes_read;
18991 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18992 info_ptr += blk->size;
18993 DW_BLOCK (attr) = blk;
18994 break;
18995 case DW_FORM_block1:
7b5a2f43 18996 blk = dwarf_alloc_block (cu);
c906108c
SS
18997 blk->size = read_1_byte (abfd, info_ptr);
18998 info_ptr += 1;
18999 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19000 info_ptr += blk->size;
19001 DW_BLOCK (attr) = blk;
19002 break;
19003 case DW_FORM_data1:
19004 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19005 info_ptr += 1;
19006 break;
19007 case DW_FORM_flag:
19008 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19009 info_ptr += 1;
19010 break;
2dc7f7b3
TT
19011 case DW_FORM_flag_present:
19012 DW_UNSND (attr) = 1;
19013 break;
c906108c
SS
19014 case DW_FORM_sdata:
19015 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19016 info_ptr += bytes_read;
19017 break;
19018 case DW_FORM_udata:
18a8505e 19019 case DW_FORM_rnglistx:
c906108c
SS
19020 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19021 info_ptr += bytes_read;
19022 break;
19023 case DW_FORM_ref1:
9c541725 19024 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19025 + read_1_byte (abfd, info_ptr));
c906108c
SS
19026 info_ptr += 1;
19027 break;
19028 case DW_FORM_ref2:
9c541725 19029 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19030 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19031 info_ptr += 2;
19032 break;
19033 case DW_FORM_ref4:
9c541725 19034 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19035 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19036 info_ptr += 4;
19037 break;
613e1657 19038 case DW_FORM_ref8:
9c541725 19039 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19040 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19041 info_ptr += 8;
19042 break;
55f1336d 19043 case DW_FORM_ref_sig8:
ac9ec31b 19044 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19045 info_ptr += 8;
19046 break;
c906108c 19047 case DW_FORM_ref_udata:
9c541725 19048 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19049 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19050 info_ptr += bytes_read;
19051 break;
c906108c 19052 case DW_FORM_indirect:
a8329558
KW
19053 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19054 info_ptr += bytes_read;
43988095
JK
19055 if (form == DW_FORM_implicit_const)
19056 {
19057 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19058 info_ptr += bytes_read;
19059 }
19060 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19061 info_ptr, need_reprocess);
43988095
JK
19062 break;
19063 case DW_FORM_implicit_const:
19064 DW_SND (attr) = implicit_const;
a8329558 19065 break;
336d760d 19066 case DW_FORM_addrx:
3019eac3 19067 case DW_FORM_GNU_addr_index:
18a8505e
AT
19068 *need_reprocess = true;
19069 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19070 info_ptr += bytes_read;
19071 break;
cf532bd1 19072 case DW_FORM_strx:
15f18d14
AT
19073 case DW_FORM_strx1:
19074 case DW_FORM_strx2:
19075 case DW_FORM_strx3:
19076 case DW_FORM_strx4:
3019eac3 19077 case DW_FORM_GNU_str_index:
3019eac3 19078 {
15f18d14
AT
19079 ULONGEST str_index;
19080 if (form == DW_FORM_strx1)
19081 {
19082 str_index = read_1_byte (abfd, info_ptr);
19083 info_ptr += 1;
19084 }
19085 else if (form == DW_FORM_strx2)
19086 {
19087 str_index = read_2_bytes (abfd, info_ptr);
19088 info_ptr += 2;
19089 }
19090 else if (form == DW_FORM_strx3)
19091 {
19092 str_index = read_3_bytes (abfd, info_ptr);
19093 info_ptr += 3;
19094 }
19095 else if (form == DW_FORM_strx4)
19096 {
19097 str_index = read_4_bytes (abfd, info_ptr);
19098 info_ptr += 4;
19099 }
19100 else
19101 {
19102 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19103 info_ptr += bytes_read;
19104 }
18a8505e
AT
19105 *need_reprocess = true;
19106 DW_UNSND (attr) = str_index;
19107 }
3019eac3 19108 break;
c906108c 19109 default:
8a3fe4f8 19110 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19111 dwarf_form_name (form),
19112 bfd_get_filename (abfd));
c906108c 19113 }
28e94949 19114
36586728 19115 /* Super hack. */
cd6c91b4 19116 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19117 attr->form = DW_FORM_GNU_ref_alt;
19118
28e94949
JB
19119 /* We have seen instances where the compiler tried to emit a byte
19120 size attribute of -1 which ended up being encoded as an unsigned
19121 0xffffffff. Although 0xffffffff is technically a valid size value,
19122 an object of this size seems pretty unlikely so we can relatively
19123 safely treat these cases as if the size attribute was invalid and
19124 treat them as zero by default. */
19125 if (attr->name == DW_AT_byte_size
19126 && form == DW_FORM_data4
19127 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19128 {
19129 complaint
b98664d3 19130 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19131 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19132 DW_UNSND (attr) = 0;
19133 }
28e94949 19134
c906108c
SS
19135 return info_ptr;
19136}
19137
a8329558
KW
19138/* Read an attribute described by an abbreviated attribute. */
19139
d521ce57 19140static const gdb_byte *
dee91e82
DE
19141read_attribute (const struct die_reader_specs *reader,
19142 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19143 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19144{
19145 attr->name = abbrev->name;
43988095 19146 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19147 abbrev->implicit_const, info_ptr,
19148 need_reprocess);
a8329558
KW
19149}
19150
c906108c 19151static CORE_ADDR
d521ce57 19152read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19153 unsigned int *bytes_read)
c906108c 19154{
e7c27a73 19155 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19156 CORE_ADDR retval = 0;
19157
107d2387 19158 if (cu_header->signed_addr_p)
c906108c 19159 {
107d2387
AC
19160 switch (cu_header->addr_size)
19161 {
19162 case 2:
fe1b8b76 19163 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19164 break;
19165 case 4:
fe1b8b76 19166 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19167 break;
19168 case 8:
fe1b8b76 19169 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19170 break;
19171 default:
8e65ff28 19172 internal_error (__FILE__, __LINE__,
e2e0b3e5 19173 _("read_address: bad switch, signed [in module %s]"),
659b0389 19174 bfd_get_filename (abfd));
107d2387
AC
19175 }
19176 }
19177 else
19178 {
19179 switch (cu_header->addr_size)
19180 {
19181 case 2:
fe1b8b76 19182 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19183 break;
19184 case 4:
fe1b8b76 19185 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19186 break;
19187 case 8:
fe1b8b76 19188 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19189 break;
19190 default:
8e65ff28 19191 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19192 _("read_address: bad switch, "
19193 "unsigned [in module %s]"),
659b0389 19194 bfd_get_filename (abfd));
107d2387 19195 }
c906108c 19196 }
64367e0a 19197
107d2387
AC
19198 *bytes_read = cu_header->addr_size;
19199 return retval;
c906108c
SS
19200}
19201
f7ef9339 19202/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19203 specification allows the initial length to take up either 4 bytes
19204 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19205 bytes describe the length and all offsets will be 8 bytes in length
19206 instead of 4.
19207
f7ef9339
KB
19208 An older, non-standard 64-bit format is also handled by this
19209 function. The older format in question stores the initial length
19210 as an 8-byte quantity without an escape value. Lengths greater
19211 than 2^32 aren't very common which means that the initial 4 bytes
19212 is almost always zero. Since a length value of zero doesn't make
19213 sense for the 32-bit format, this initial zero can be considered to
19214 be an escape value which indicates the presence of the older 64-bit
19215 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19216 greater than 4GB. If it becomes necessary to handle lengths
19217 somewhat larger than 4GB, we could allow other small values (such
19218 as the non-sensical values of 1, 2, and 3) to also be used as
19219 escape values indicating the presence of the old format.
f7ef9339 19220
917c78fc
MK
19221 The value returned via bytes_read should be used to increment the
19222 relevant pointer after calling read_initial_length().
c764a876 19223
613e1657
KB
19224 [ Note: read_initial_length() and read_offset() are based on the
19225 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19226 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19227 from:
19228
f7ef9339 19229 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19230
613e1657
KB
19231 This document is only a draft and is subject to change. (So beware.)
19232
f7ef9339 19233 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19234 determined empirically by examining 64-bit ELF files produced by
19235 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19236
19237 - Kevin, July 16, 2002
613e1657
KB
19238 ] */
19239
19240static LONGEST
d521ce57 19241read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19242{
fe1b8b76 19243 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19244
dd373385 19245 if (length == 0xffffffff)
613e1657 19246 {
fe1b8b76 19247 length = bfd_get_64 (abfd, buf + 4);
613e1657 19248 *bytes_read = 12;
613e1657 19249 }
dd373385 19250 else if (length == 0)
f7ef9339 19251 {
dd373385 19252 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19253 length = bfd_get_64 (abfd, buf);
f7ef9339 19254 *bytes_read = 8;
f7ef9339 19255 }
613e1657
KB
19256 else
19257 {
19258 *bytes_read = 4;
613e1657
KB
19259 }
19260
c764a876
DE
19261 return length;
19262}
dd373385 19263
c764a876
DE
19264/* Cover function for read_initial_length.
19265 Returns the length of the object at BUF, and stores the size of the
19266 initial length in *BYTES_READ and stores the size that offsets will be in
19267 *OFFSET_SIZE.
19268 If the initial length size is not equivalent to that specified in
19269 CU_HEADER then issue a complaint.
19270 This is useful when reading non-comp-unit headers. */
dd373385 19271
c764a876 19272static LONGEST
d521ce57 19273read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19274 const struct comp_unit_head *cu_header,
19275 unsigned int *bytes_read,
19276 unsigned int *offset_size)
19277{
19278 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19279
19280 gdb_assert (cu_header->initial_length_size == 4
19281 || cu_header->initial_length_size == 8
19282 || cu_header->initial_length_size == 12);
19283
19284 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19285 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19286
c764a876 19287 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19288 return length;
613e1657
KB
19289}
19290
19291/* Read an offset from the data stream. The size of the offset is
917c78fc 19292 given by cu_header->offset_size. */
613e1657
KB
19293
19294static LONGEST
d521ce57
TT
19295read_offset (bfd *abfd, const gdb_byte *buf,
19296 const struct comp_unit_head *cu_header,
891d2f0b 19297 unsigned int *bytes_read)
c764a876
DE
19298{
19299 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19300
c764a876
DE
19301 *bytes_read = cu_header->offset_size;
19302 return offset;
19303}
19304
19305/* Read an offset from the data stream. */
19306
19307static LONGEST
d521ce57 19308read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19309{
19310 LONGEST retval = 0;
19311
c764a876 19312 switch (offset_size)
613e1657
KB
19313 {
19314 case 4:
fe1b8b76 19315 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19316 break;
19317 case 8:
fe1b8b76 19318 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19319 break;
19320 default:
8e65ff28 19321 internal_error (__FILE__, __LINE__,
c764a876 19322 _("read_offset_1: bad switch [in module %s]"),
659b0389 19323 bfd_get_filename (abfd));
613e1657
KB
19324 }
19325
917c78fc 19326 return retval;
613e1657
KB
19327}
19328
d521ce57
TT
19329static const gdb_byte *
19330read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19331{
19332 /* If the size of a host char is 8 bits, we can return a pointer
19333 to the buffer, otherwise we have to copy the data to a buffer
19334 allocated on the temporary obstack. */
4bdf3d34 19335 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19336 return buf;
c906108c
SS
19337}
19338
d521ce57
TT
19339static const char *
19340read_direct_string (bfd *abfd, const gdb_byte *buf,
19341 unsigned int *bytes_read_ptr)
c906108c
SS
19342{
19343 /* If the size of a host char is 8 bits, we can return a pointer
19344 to the string, otherwise we have to copy the string to a buffer
19345 allocated on the temporary obstack. */
4bdf3d34 19346 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19347 if (*buf == '\0')
19348 {
19349 *bytes_read_ptr = 1;
19350 return NULL;
19351 }
d521ce57
TT
19352 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19353 return (const char *) buf;
4bdf3d34
JJ
19354}
19355
43988095
JK
19356/* Return pointer to string at section SECT offset STR_OFFSET with error
19357 reporting strings FORM_NAME and SECT_NAME. */
19358
d521ce57 19359static const char *
ed2dc618
SM
19360read_indirect_string_at_offset_from (struct objfile *objfile,
19361 bfd *abfd, LONGEST str_offset,
43988095
JK
19362 struct dwarf2_section_info *sect,
19363 const char *form_name,
19364 const char *sect_name)
19365{
96b79293 19366 sect->read (objfile);
43988095
JK
19367 if (sect->buffer == NULL)
19368 error (_("%s used without %s section [in module %s]"),
19369 form_name, sect_name, bfd_get_filename (abfd));
19370 if (str_offset >= sect->size)
19371 error (_("%s pointing outside of %s section [in module %s]"),
19372 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19373 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19374 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19375 return NULL;
43988095
JK
19376 return (const char *) (sect->buffer + str_offset);
19377}
19378
19379/* Return pointer to string at .debug_str offset STR_OFFSET. */
19380
19381static const char *
ed2dc618
SM
19382read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19383 bfd *abfd, LONGEST str_offset)
43988095 19384{
ed2dc618
SM
19385 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19386 abfd, str_offset,
43988095
JK
19387 &dwarf2_per_objfile->str,
19388 "DW_FORM_strp", ".debug_str");
19389}
19390
19391/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19392
19393static const char *
ed2dc618
SM
19394read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19395 bfd *abfd, LONGEST str_offset)
43988095 19396{
ed2dc618
SM
19397 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19398 abfd, str_offset,
43988095
JK
19399 &dwarf2_per_objfile->line_str,
19400 "DW_FORM_line_strp",
19401 ".debug_line_str");
c906108c
SS
19402}
19403
36586728
TT
19404/* Read a string at offset STR_OFFSET in the .debug_str section from
19405 the .dwz file DWZ. Throw an error if the offset is too large. If
19406 the string consists of a single NUL byte, return NULL; otherwise
19407 return a pointer to the string. */
19408
d521ce57 19409static const char *
ed2dc618
SM
19410read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19411 LONGEST str_offset)
36586728 19412{
96b79293 19413 dwz->str.read (objfile);
36586728
TT
19414
19415 if (dwz->str.buffer == NULL)
19416 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19417 "section [in module %s]"),
00f93c44 19418 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19419 if (str_offset >= dwz->str.size)
19420 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19421 ".debug_str section [in module %s]"),
00f93c44 19422 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19423 gdb_assert (HOST_CHAR_BIT == 8);
19424 if (dwz->str.buffer[str_offset] == '\0')
19425 return NULL;
d521ce57 19426 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19427}
19428
43988095
JK
19429/* Return pointer to string at .debug_str offset as read from BUF.
19430 BUF is assumed to be in a compilation unit described by CU_HEADER.
19431 Return *BYTES_READ_PTR count of bytes read from BUF. */
19432
d521ce57 19433static const char *
ed2dc618
SM
19434read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19435 const gdb_byte *buf,
cf2c3c16
TT
19436 const struct comp_unit_head *cu_header,
19437 unsigned int *bytes_read_ptr)
19438{
19439 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19440
ed2dc618 19441 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19442}
19443
43988095
JK
19444/* Return pointer to string at .debug_line_str offset as read from BUF.
19445 BUF is assumed to be in a compilation unit described by CU_HEADER.
19446 Return *BYTES_READ_PTR count of bytes read from BUF. */
19447
19448static const char *
ed2dc618
SM
19449read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19450 bfd *abfd, const gdb_byte *buf,
43988095
JK
19451 const struct comp_unit_head *cu_header,
19452 unsigned int *bytes_read_ptr)
19453{
19454 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19455
ed2dc618
SM
19456 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19457 str_offset);
43988095
JK
19458}
19459
3019eac3 19460/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19461 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19462 ADDR_SIZE is the size of addresses from the CU header. */
19463
19464static CORE_ADDR
ed2dc618 19465read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19466 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19467 int addr_size)
3019eac3
DE
19468{
19469 struct objfile *objfile = dwarf2_per_objfile->objfile;
19470 bfd *abfd = objfile->obfd;
19471 const gdb_byte *info_ptr;
18a8505e 19472 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19473
96b79293 19474 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
19475 if (dwarf2_per_objfile->addr.buffer == NULL)
19476 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19477 objfile_name (objfile));
18a8505e
AT
19478 if (addr_base_or_zero + addr_index * addr_size
19479 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
19480 error (_("DW_FORM_addr_index pointing outside of "
19481 ".debug_addr section [in module %s]"),
4262abfb 19482 objfile_name (objfile));
3019eac3 19483 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 19484 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19485 if (addr_size == 4)
19486 return bfd_get_32 (abfd, info_ptr);
19487 else
19488 return bfd_get_64 (abfd, info_ptr);
19489}
19490
19491/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19492
19493static CORE_ADDR
19494read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19495{
518817b3
SM
19496 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19497 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19498}
19499
19500/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19501
19502static CORE_ADDR
d521ce57 19503read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19504 unsigned int *bytes_read)
19505{
518817b3 19506 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19507 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19508
19509 return read_addr_index (cu, addr_index);
19510}
19511
3019eac3
DE
19512/* Given an index in .debug_addr, fetch the value.
19513 NOTE: This can be called during dwarf expression evaluation,
19514 long after the debug information has been read, and thus per_cu->cu
19515 may no longer exist. */
19516
19517CORE_ADDR
19518dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19519 unsigned int addr_index)
19520{
ed2dc618 19521 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19522 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19523 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19524 int addr_size;
19525
3019eac3
DE
19526 /* We need addr_base and addr_size.
19527 If we don't have PER_CU->cu, we have to get it.
19528 Nasty, but the alternative is storing the needed info in PER_CU,
19529 which at this point doesn't seem justified: it's not clear how frequently
19530 it would get used and it would increase the size of every PER_CU.
19531 Entry points like dwarf2_per_cu_addr_size do a similar thing
19532 so we're not in uncharted territory here.
19533 Alas we need to be a bit more complicated as addr_base is contained
19534 in the DIE.
19535
19536 We don't need to read the entire CU(/TU).
19537 We just need the header and top level die.
a1b64ce1 19538
3019eac3 19539 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19540 For now we skip this optimization. */
3019eac3
DE
19541
19542 if (cu != NULL)
19543 {
19544 addr_base = cu->addr_base;
19545 addr_size = cu->header.addr_size;
19546 }
19547 else
19548 {
c0ab21c2
TT
19549 cutu_reader reader (per_cu, NULL, 0, 0, false);
19550 addr_base = reader.cu->addr_base;
19551 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19552 }
19553
ed2dc618
SM
19554 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19555 addr_size);
3019eac3
DE
19556}
19557
18a8505e
AT
19558/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19559 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19560 DWO file. */
3019eac3 19561
d521ce57 19562static const char *
18a8505e
AT
19563read_str_index (struct dwarf2_cu *cu,
19564 struct dwarf2_section_info *str_section,
19565 struct dwarf2_section_info *str_offsets_section,
19566 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19567{
518817b3
SM
19568 struct dwarf2_per_objfile *dwarf2_per_objfile
19569 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19570 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19571 const char *objf_name = objfile_name (objfile);
3019eac3 19572 bfd *abfd = objfile->obfd;
d521ce57 19573 const gdb_byte *info_ptr;
3019eac3 19574 ULONGEST str_offset;
cf532bd1 19575 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19576
96b79293
TT
19577 str_section->read (objfile);
19578 str_offsets_section->read (objfile);
73869dc2 19579 if (str_section->buffer == NULL)
18a8505e 19580 error (_("%s used without %s section"
9d8780f0 19581 " in CU at offset %s [in module %s]"),
96b79293 19582 form_name, str_section->get_name (),
18a8505e 19583 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19584 if (str_offsets_section->buffer == NULL)
18a8505e 19585 error (_("%s used without %s section"
9d8780f0 19586 " in CU at offset %s [in module %s]"),
96b79293 19587 form_name, str_section->get_name (),
18a8505e 19588 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19589 info_ptr = (str_offsets_section->buffer
18a8505e 19590 + str_offsets_base
3019eac3
DE
19591 + str_index * cu->header.offset_size);
19592 if (cu->header.offset_size == 4)
19593 str_offset = bfd_get_32 (abfd, info_ptr);
19594 else
19595 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19596 if (str_offset >= str_section->size)
57d63ce2 19597 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19598 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19599 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19600 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19601}
19602
18a8505e
AT
19603/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19604
19605static const char *
19606read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19607{
19608 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19609 ? reader->cu->header.addr_size : 0;
19610 return read_str_index (reader->cu,
19611 &reader->dwo_file->sections.str,
19612 &reader->dwo_file->sections.str_offsets,
19613 str_offsets_base, str_index);
19614}
19615
19616/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19617
19618static const char *
19619read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19620{
19621 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19622 const char *objf_name = objfile_name (objfile);
19623 static const char form_name[] = "DW_FORM_GNU_str_index";
19624 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19625
19626 if (!cu->str_offsets_base.has_value ())
19627 error (_("%s used in Fission stub without %s"
19628 " in CU at offset 0x%lx [in module %s]"),
19629 form_name, str_offsets_attr_name,
19630 (long) cu->header.offset_size, objf_name);
19631
19632 return read_str_index (cu,
19633 &cu->per_cu->dwarf2_per_objfile->str,
19634 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19635 *cu->str_offsets_base, str_index);
19636}
19637
3019eac3
DE
19638/* Return the length of an LEB128 number in BUF. */
19639
19640static int
19641leb128_size (const gdb_byte *buf)
19642{
19643 const gdb_byte *begin = buf;
19644 gdb_byte byte;
19645
19646 while (1)
19647 {
19648 byte = *buf++;
19649 if ((byte & 128) == 0)
19650 return buf - begin;
19651 }
19652}
19653
c906108c 19654static void
e142c38c 19655set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19656{
19657 switch (lang)
19658 {
19659 case DW_LANG_C89:
76bee0cc 19660 case DW_LANG_C99:
0cfd832f 19661 case DW_LANG_C11:
c906108c 19662 case DW_LANG_C:
d1be3247 19663 case DW_LANG_UPC:
e142c38c 19664 cu->language = language_c;
c906108c 19665 break;
9c37b5ae 19666 case DW_LANG_Java:
c906108c 19667 case DW_LANG_C_plus_plus:
0cfd832f
MW
19668 case DW_LANG_C_plus_plus_11:
19669 case DW_LANG_C_plus_plus_14:
e142c38c 19670 cu->language = language_cplus;
c906108c 19671 break;
6aecb9c2
JB
19672 case DW_LANG_D:
19673 cu->language = language_d;
19674 break;
c906108c
SS
19675 case DW_LANG_Fortran77:
19676 case DW_LANG_Fortran90:
b21b22e0 19677 case DW_LANG_Fortran95:
f7de9aab
MW
19678 case DW_LANG_Fortran03:
19679 case DW_LANG_Fortran08:
e142c38c 19680 cu->language = language_fortran;
c906108c 19681 break;
a766d390
DE
19682 case DW_LANG_Go:
19683 cu->language = language_go;
19684 break;
c906108c 19685 case DW_LANG_Mips_Assembler:
e142c38c 19686 cu->language = language_asm;
c906108c
SS
19687 break;
19688 case DW_LANG_Ada83:
8aaf0b47 19689 case DW_LANG_Ada95:
bc5f45f8
JB
19690 cu->language = language_ada;
19691 break;
72019c9c
GM
19692 case DW_LANG_Modula2:
19693 cu->language = language_m2;
19694 break;
fe8e67fd
PM
19695 case DW_LANG_Pascal83:
19696 cu->language = language_pascal;
19697 break;
22566fbd
DJ
19698 case DW_LANG_ObjC:
19699 cu->language = language_objc;
19700 break;
c44af4eb
TT
19701 case DW_LANG_Rust:
19702 case DW_LANG_Rust_old:
19703 cu->language = language_rust;
19704 break;
c906108c
SS
19705 case DW_LANG_Cobol74:
19706 case DW_LANG_Cobol85:
c906108c 19707 default:
e142c38c 19708 cu->language = language_minimal;
c906108c
SS
19709 break;
19710 }
e142c38c 19711 cu->language_defn = language_def (cu->language);
c906108c
SS
19712}
19713
19714/* Return the named attribute or NULL if not there. */
19715
19716static struct attribute *
e142c38c 19717dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19718{
a48e046c 19719 for (;;)
c906108c 19720 {
a48e046c
TT
19721 unsigned int i;
19722 struct attribute *spec = NULL;
19723
19724 for (i = 0; i < die->num_attrs; ++i)
19725 {
19726 if (die->attrs[i].name == name)
19727 return &die->attrs[i];
19728 if (die->attrs[i].name == DW_AT_specification
19729 || die->attrs[i].name == DW_AT_abstract_origin)
19730 spec = &die->attrs[i];
19731 }
19732
19733 if (!spec)
19734 break;
c906108c 19735
f2f0e013 19736 die = follow_die_ref (die, spec, &cu);
f2f0e013 19737 }
c5aa993b 19738
c906108c
SS
19739 return NULL;
19740}
19741
348e048f
DE
19742/* Return the named attribute or NULL if not there,
19743 but do not follow DW_AT_specification, etc.
19744 This is for use in contexts where we're reading .debug_types dies.
19745 Following DW_AT_specification, DW_AT_abstract_origin will take us
19746 back up the chain, and we want to go down. */
19747
19748static struct attribute *
45e58e77 19749dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19750{
19751 unsigned int i;
19752
19753 for (i = 0; i < die->num_attrs; ++i)
19754 if (die->attrs[i].name == name)
19755 return &die->attrs[i];
19756
19757 return NULL;
19758}
19759
7d45c7c3
KB
19760/* Return the string associated with a string-typed attribute, or NULL if it
19761 is either not found or is of an incorrect type. */
19762
19763static const char *
19764dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19765{
19766 struct attribute *attr;
19767 const char *str = NULL;
19768
19769 attr = dwarf2_attr (die, name, cu);
19770
19771 if (attr != NULL)
19772 {
43988095 19773 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19774 || attr->form == DW_FORM_string
cf532bd1 19775 || attr->form == DW_FORM_strx
8fe0f950
AT
19776 || attr->form == DW_FORM_strx1
19777 || attr->form == DW_FORM_strx2
19778 || attr->form == DW_FORM_strx3
19779 || attr->form == DW_FORM_strx4
b3340438 19780 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19781 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19782 str = DW_STRING (attr);
19783 else
b98664d3 19784 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19785 "DIE at %s in module %s"),
19786 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19787 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19788 }
19789
19790 return str;
19791}
19792
a084a2a6 19793/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19794 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19795static const char *
19796dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19797{
19798 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19799 if (dwo_name == nullptr)
19800 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19801 return dwo_name;
19802}
19803
05cf31d1
JB
19804/* Return non-zero iff the attribute NAME is defined for the given DIE,
19805 and holds a non-zero value. This function should only be used for
2dc7f7b3 19806 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19807
19808static int
19809dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19810{
19811 struct attribute *attr = dwarf2_attr (die, name, cu);
19812
19813 return (attr && DW_UNSND (attr));
19814}
19815
3ca72b44 19816static int
e142c38c 19817die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19818{
05cf31d1
JB
19819 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19820 which value is non-zero. However, we have to be careful with
19821 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19822 (via dwarf2_flag_true_p) follows this attribute. So we may
19823 end up accidently finding a declaration attribute that belongs
19824 to a different DIE referenced by the specification attribute,
19825 even though the given DIE does not have a declaration attribute. */
19826 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19827 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19828}
19829
63d06c5c 19830/* Return the die giving the specification for DIE, if there is
f2f0e013 19831 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19832 containing the return value on output. If there is no
19833 specification, but there is an abstract origin, that is
19834 returned. */
63d06c5c
DC
19835
19836static struct die_info *
f2f0e013 19837die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19838{
f2f0e013
DJ
19839 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19840 *spec_cu);
63d06c5c 19841
edb3359d
DJ
19842 if (spec_attr == NULL)
19843 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19844
63d06c5c
DC
19845 if (spec_attr == NULL)
19846 return NULL;
19847 else
f2f0e013 19848 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19849}
c906108c 19850
527f3840
JK
19851/* Stub for free_line_header to match void * callback types. */
19852
19853static void
19854free_line_header_voidp (void *arg)
19855{
9a3c8263 19856 struct line_header *lh = (struct line_header *) arg;
527f3840 19857
fff8551c 19858 delete lh;
527f3840
JK
19859}
19860
fff8551c
PA
19861void
19862line_header::add_include_dir (const char *include_dir)
c906108c 19863{
27e0867f 19864 if (dwarf_line_debug >= 2)
7ba99d21
AT
19865 {
19866 size_t new_size;
19867 if (version >= 5)
19868 new_size = m_include_dirs.size ();
19869 else
19870 new_size = m_include_dirs.size () + 1;
19871 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19872 new_size, include_dir);
19873 }
19874 m_include_dirs.push_back (include_dir);
debd256d 19875}
6e70227d 19876
fff8551c
PA
19877void
19878line_header::add_file_name (const char *name,
ecfb656c 19879 dir_index d_index,
fff8551c
PA
19880 unsigned int mod_time,
19881 unsigned int length)
debd256d 19882{
27e0867f 19883 if (dwarf_line_debug >= 2)
7ba99d21
AT
19884 {
19885 size_t new_size;
19886 if (version >= 5)
19887 new_size = file_names_size ();
19888 else
19889 new_size = file_names_size () + 1;
19890 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
19891 new_size, name);
19892 }
19893 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19894}
6e70227d 19895
83769d0b 19896/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19897
19898static struct dwarf2_section_info *
19899get_debug_line_section (struct dwarf2_cu *cu)
19900{
19901 struct dwarf2_section_info *section;
518817b3
SM
19902 struct dwarf2_per_objfile *dwarf2_per_objfile
19903 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19904
19905 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19906 DWO file. */
19907 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19908 section = &cu->dwo_unit->dwo_file->sections.line;
19909 else if (cu->per_cu->is_dwz)
19910 {
ed2dc618 19911 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19912
19913 section = &dwz->line;
19914 }
19915 else
19916 section = &dwarf2_per_objfile->line;
19917
19918 return section;
19919}
19920
43988095
JK
19921/* Read directory or file name entry format, starting with byte of
19922 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19923 entries count and the entries themselves in the described entry
19924 format. */
19925
19926static void
ed2dc618
SM
19927read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19928 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19929 struct line_header *lh,
19930 const struct comp_unit_head *cu_header,
19931 void (*callback) (struct line_header *lh,
19932 const char *name,
ecfb656c 19933 dir_index d_index,
43988095
JK
19934 unsigned int mod_time,
19935 unsigned int length))
19936{
19937 gdb_byte format_count, formati;
19938 ULONGEST data_count, datai;
19939 const gdb_byte *buf = *bufp;
19940 const gdb_byte *format_header_data;
43988095
JK
19941 unsigned int bytes_read;
19942
19943 format_count = read_1_byte (abfd, buf);
19944 buf += 1;
19945 format_header_data = buf;
19946 for (formati = 0; formati < format_count; formati++)
19947 {
19948 read_unsigned_leb128 (abfd, buf, &bytes_read);
19949 buf += bytes_read;
19950 read_unsigned_leb128 (abfd, buf, &bytes_read);
19951 buf += bytes_read;
19952 }
19953
19954 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19955 buf += bytes_read;
19956 for (datai = 0; datai < data_count; datai++)
19957 {
19958 const gdb_byte *format = format_header_data;
19959 struct file_entry fe;
19960
43988095
JK
19961 for (formati = 0; formati < format_count; formati++)
19962 {
ecfb656c 19963 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19964 format += bytes_read;
43988095 19965
ecfb656c 19966 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19967 format += bytes_read;
ecfb656c
PA
19968
19969 gdb::optional<const char *> string;
19970 gdb::optional<unsigned int> uint;
19971
43988095
JK
19972 switch (form)
19973 {
19974 case DW_FORM_string:
ecfb656c 19975 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19976 buf += bytes_read;
19977 break;
19978
19979 case DW_FORM_line_strp:
ed2dc618
SM
19980 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19981 abfd, buf,
ecfb656c
PA
19982 cu_header,
19983 &bytes_read));
43988095
JK
19984 buf += bytes_read;
19985 break;
19986
19987 case DW_FORM_data1:
ecfb656c 19988 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19989 buf += 1;
19990 break;
19991
19992 case DW_FORM_data2:
ecfb656c 19993 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19994 buf += 2;
19995 break;
19996
19997 case DW_FORM_data4:
ecfb656c 19998 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19999 buf += 4;
20000 break;
20001
20002 case DW_FORM_data8:
ecfb656c 20003 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20004 buf += 8;
20005 break;
20006
7ba99d21
AT
20007 case DW_FORM_data16:
20008 /* This is used for MD5, but file_entry does not record MD5s. */
20009 buf += 16;
20010 break;
20011
43988095 20012 case DW_FORM_udata:
ecfb656c 20013 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20014 buf += bytes_read;
20015 break;
20016
20017 case DW_FORM_block:
20018 /* It is valid only for DW_LNCT_timestamp which is ignored by
20019 current GDB. */
20020 break;
20021 }
ecfb656c
PA
20022
20023 switch (content_type)
20024 {
20025 case DW_LNCT_path:
20026 if (string.has_value ())
20027 fe.name = *string;
20028 break;
20029 case DW_LNCT_directory_index:
20030 if (uint.has_value ())
20031 fe.d_index = (dir_index) *uint;
20032 break;
20033 case DW_LNCT_timestamp:
20034 if (uint.has_value ())
20035 fe.mod_time = *uint;
20036 break;
20037 case DW_LNCT_size:
20038 if (uint.has_value ())
20039 fe.length = *uint;
20040 break;
20041 case DW_LNCT_MD5:
20042 break;
20043 default:
b98664d3 20044 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20045 pulongest (content_type));
20046 }
43988095
JK
20047 }
20048
ecfb656c 20049 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20050 }
20051
20052 *bufp = buf;
20053}
20054
debd256d 20055/* Read the statement program header starting at OFFSET in
3019eac3 20056 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20057 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20058 Returns NULL if there is a problem reading the header, e.g., if it
20059 has a version we don't understand.
debd256d
JB
20060
20061 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20062 the returned object point into the dwarf line section buffer,
20063 and must not be freed. */
ae2de4f8 20064
fff8551c 20065static line_header_up
9c541725 20066dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20067{
d521ce57 20068 const gdb_byte *line_ptr;
c764a876 20069 unsigned int bytes_read, offset_size;
debd256d 20070 int i;
d521ce57 20071 const char *cur_dir, *cur_file;
3019eac3
DE
20072 struct dwarf2_section_info *section;
20073 bfd *abfd;
518817b3
SM
20074 struct dwarf2_per_objfile *dwarf2_per_objfile
20075 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20076
36586728 20077 section = get_debug_line_section (cu);
96b79293 20078 section->read (dwarf2_per_objfile->objfile);
3019eac3 20079 if (section->buffer == NULL)
debd256d 20080 {
3019eac3 20081 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20082 complaint (_("missing .debug_line.dwo section"));
3019eac3 20083 else
b98664d3 20084 complaint (_("missing .debug_line section"));
debd256d
JB
20085 return 0;
20086 }
20087
fceca515
DE
20088 /* We can't do this until we know the section is non-empty.
20089 Only then do we know we have such a section. */
96b79293 20090 abfd = section->get_bfd_owner ();
fceca515 20091
a738430d
MK
20092 /* Make sure that at least there's room for the total_length field.
20093 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20094 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20095 {
4d3c2250 20096 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20097 return 0;
20098 }
20099
fff8551c 20100 line_header_up lh (new line_header ());
debd256d 20101
9c541725 20102 lh->sect_off = sect_off;
527f3840
JK
20103 lh->offset_in_dwz = cu->per_cu->is_dwz;
20104
9c541725 20105 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20106
a738430d 20107 /* Read in the header. */
6e70227d 20108 lh->total_length =
c764a876
DE
20109 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20110 &bytes_read, &offset_size);
debd256d 20111 line_ptr += bytes_read;
7ba99d21
AT
20112
20113 const gdb_byte *start_here = line_ptr;
20114
3019eac3 20115 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20116 {
4d3c2250 20117 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20118 return 0;
20119 }
7ba99d21 20120 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20121 lh->version = read_2_bytes (abfd, line_ptr);
20122 line_ptr += 2;
43988095 20123 if (lh->version > 5)
cd366ee8
DE
20124 {
20125 /* This is a version we don't understand. The format could have
20126 changed in ways we don't handle properly so just punt. */
b98664d3 20127 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20128 return NULL;
20129 }
43988095
JK
20130 if (lh->version >= 5)
20131 {
20132 gdb_byte segment_selector_size;
20133
20134 /* Skip address size. */
20135 read_1_byte (abfd, line_ptr);
20136 line_ptr += 1;
20137
20138 segment_selector_size = read_1_byte (abfd, line_ptr);
20139 line_ptr += 1;
20140 if (segment_selector_size != 0)
20141 {
b98664d3 20142 complaint (_("unsupported segment selector size %u "
43988095
JK
20143 "in .debug_line section"),
20144 segment_selector_size);
20145 return NULL;
20146 }
20147 }
c764a876
DE
20148 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20149 line_ptr += offset_size;
7ba99d21 20150 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20151 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20152 line_ptr += 1;
2dc7f7b3
TT
20153 if (lh->version >= 4)
20154 {
20155 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20156 line_ptr += 1;
20157 }
20158 else
20159 lh->maximum_ops_per_instruction = 1;
20160
20161 if (lh->maximum_ops_per_instruction == 0)
20162 {
20163 lh->maximum_ops_per_instruction = 1;
b98664d3 20164 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20165 "in `.debug_line' section"));
2dc7f7b3
TT
20166 }
20167
debd256d
JB
20168 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20169 line_ptr += 1;
20170 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20171 line_ptr += 1;
20172 lh->line_range = read_1_byte (abfd, line_ptr);
20173 line_ptr += 1;
20174 lh->opcode_base = read_1_byte (abfd, line_ptr);
20175 line_ptr += 1;
fff8551c 20176 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20177
20178 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20179 for (i = 1; i < lh->opcode_base; ++i)
20180 {
20181 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20182 line_ptr += 1;
20183 }
20184
43988095 20185 if (lh->version >= 5)
debd256d 20186 {
43988095 20187 /* Read directory table. */
ed2dc618
SM
20188 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20189 &cu->header,
b926417a 20190 [] (struct line_header *header, const char *name,
ecfb656c 20191 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20192 unsigned int length)
20193 {
b926417a 20194 header->add_include_dir (name);
fff8551c 20195 });
debd256d 20196
43988095 20197 /* Read file name table. */
ed2dc618
SM
20198 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20199 &cu->header,
b926417a 20200 [] (struct line_header *header, const char *name,
ecfb656c 20201 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20202 unsigned int length)
20203 {
b926417a 20204 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20205 });
43988095
JK
20206 }
20207 else
debd256d 20208 {
43988095
JK
20209 /* Read directory table. */
20210 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20211 {
20212 line_ptr += bytes_read;
fff8551c 20213 lh->add_include_dir (cur_dir);
43988095 20214 }
debd256d
JB
20215 line_ptr += bytes_read;
20216
43988095
JK
20217 /* Read file name table. */
20218 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20219 {
ecfb656c
PA
20220 unsigned int mod_time, length;
20221 dir_index d_index;
43988095
JK
20222
20223 line_ptr += bytes_read;
ecfb656c 20224 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20225 line_ptr += bytes_read;
20226 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20227 line_ptr += bytes_read;
20228 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20229 line_ptr += bytes_read;
20230
ecfb656c 20231 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20232 }
20233 line_ptr += bytes_read;
debd256d 20234 }
debd256d 20235
3019eac3 20236 if (line_ptr > (section->buffer + section->size))
b98664d3 20237 complaint (_("line number info header doesn't "
3e43a32a 20238 "fit in `.debug_line' section"));
debd256d 20239
debd256d
JB
20240 return lh;
20241}
c906108c 20242
c6da4cef 20243/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20244 Return the file name of the psymtab for the given file_entry.
c6da4cef 20245 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20246 If space for the result is malloc'd, *NAME_HOLDER will be set.
20247 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20248
d521ce57 20249static const char *
7ba99d21 20250psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20251 const dwarf2_psymtab *pst,
c89b44cd
TT
20252 const char *comp_dir,
20253 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20254{
d521ce57
TT
20255 const char *include_name = fe.name;
20256 const char *include_name_to_compare = include_name;
72b9f47f 20257 const char *pst_filename;
c6da4cef
DE
20258 int file_is_pst;
20259
8c43009f 20260 const char *dir_name = fe.include_dir (lh);
c6da4cef 20261
c89b44cd 20262 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20263 if (!IS_ABSOLUTE_PATH (include_name)
20264 && (dir_name != NULL || comp_dir != NULL))
20265 {
20266 /* Avoid creating a duplicate psymtab for PST.
20267 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20268 Before we do the comparison, however, we need to account
20269 for DIR_NAME and COMP_DIR.
20270 First prepend dir_name (if non-NULL). If we still don't
20271 have an absolute path prepend comp_dir (if non-NULL).
20272 However, the directory we record in the include-file's
20273 psymtab does not contain COMP_DIR (to match the
20274 corresponding symtab(s)).
20275
20276 Example:
20277
20278 bash$ cd /tmp
20279 bash$ gcc -g ./hello.c
20280 include_name = "hello.c"
20281 dir_name = "."
20282 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20283 DW_AT_name = "./hello.c"
20284
20285 */
c6da4cef
DE
20286
20287 if (dir_name != NULL)
20288 {
c89b44cd
TT
20289 name_holder->reset (concat (dir_name, SLASH_STRING,
20290 include_name, (char *) NULL));
20291 include_name = name_holder->get ();
c6da4cef 20292 include_name_to_compare = include_name;
c6da4cef
DE
20293 }
20294 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20295 {
c89b44cd
TT
20296 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20297 include_name, (char *) NULL));
20298 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20299 }
20300 }
20301
20302 pst_filename = pst->filename;
c89b44cd 20303 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20304 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20305 {
c89b44cd
TT
20306 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20307 pst_filename, (char *) NULL));
20308 pst_filename = copied_name.get ();
c6da4cef
DE
20309 }
20310
1e3fad37 20311 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20312
c6da4cef
DE
20313 if (file_is_pst)
20314 return NULL;
20315 return include_name;
20316}
20317
d9b3de22
DE
20318/* State machine to track the state of the line number program. */
20319
6f77053d 20320class lnp_state_machine
d9b3de22 20321{
6f77053d
PA
20322public:
20323 /* Initialize a machine state for the start of a line number
20324 program. */
804d2729
TT
20325 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20326 bool record_lines_p);
6f77053d 20327
8c43009f
PA
20328 file_entry *current_file ()
20329 {
20330 /* lh->file_names is 0-based, but the file name numbers in the
20331 statement program are 1-based. */
6f77053d
PA
20332 return m_line_header->file_name_at (m_file);
20333 }
20334
20335 /* Record the line in the state machine. END_SEQUENCE is true if
20336 we're processing the end of a sequence. */
20337 void record_line (bool end_sequence);
20338
7ab6656f
OJ
20339 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20340 nop-out rest of the lines in this sequence. */
6f77053d
PA
20341 void check_line_address (struct dwarf2_cu *cu,
20342 const gdb_byte *line_ptr,
7ab6656f 20343 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20344
20345 void handle_set_discriminator (unsigned int discriminator)
20346 {
20347 m_discriminator = discriminator;
20348 m_line_has_non_zero_discriminator |= discriminator != 0;
20349 }
20350
20351 /* Handle DW_LNE_set_address. */
20352 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20353 {
20354 m_op_index = 0;
20355 address += baseaddr;
20356 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20357 }
20358
20359 /* Handle DW_LNS_advance_pc. */
20360 void handle_advance_pc (CORE_ADDR adjust);
20361
20362 /* Handle a special opcode. */
20363 void handle_special_opcode (unsigned char op_code);
20364
20365 /* Handle DW_LNS_advance_line. */
20366 void handle_advance_line (int line_delta)
20367 {
20368 advance_line (line_delta);
20369 }
20370
20371 /* Handle DW_LNS_set_file. */
20372 void handle_set_file (file_name_index file);
20373
20374 /* Handle DW_LNS_negate_stmt. */
20375 void handle_negate_stmt ()
20376 {
20377 m_is_stmt = !m_is_stmt;
20378 }
20379
20380 /* Handle DW_LNS_const_add_pc. */
20381 void handle_const_add_pc ();
20382
20383 /* Handle DW_LNS_fixed_advance_pc. */
20384 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20385 {
20386 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20387 m_op_index = 0;
20388 }
20389
20390 /* Handle DW_LNS_copy. */
20391 void handle_copy ()
20392 {
20393 record_line (false);
20394 m_discriminator = 0;
20395 }
20396
20397 /* Handle DW_LNE_end_sequence. */
20398 void handle_end_sequence ()
20399 {
804d2729 20400 m_currently_recording_lines = true;
6f77053d
PA
20401 }
20402
20403private:
20404 /* Advance the line by LINE_DELTA. */
20405 void advance_line (int line_delta)
20406 {
20407 m_line += line_delta;
20408
20409 if (line_delta != 0)
20410 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20411 }
20412
804d2729
TT
20413 struct dwarf2_cu *m_cu;
20414
6f77053d
PA
20415 gdbarch *m_gdbarch;
20416
20417 /* True if we're recording lines.
20418 Otherwise we're building partial symtabs and are just interested in
20419 finding include files mentioned by the line number program. */
20420 bool m_record_lines_p;
20421
8c43009f 20422 /* The line number header. */
6f77053d 20423 line_header *m_line_header;
8c43009f 20424
6f77053d
PA
20425 /* These are part of the standard DWARF line number state machine,
20426 and initialized according to the DWARF spec. */
d9b3de22 20427
6f77053d 20428 unsigned char m_op_index = 0;
7ba99d21
AT
20429 /* The line table index of the current file. */
20430 file_name_index m_file = 1;
6f77053d
PA
20431 unsigned int m_line = 1;
20432
20433 /* These are initialized in the constructor. */
20434
20435 CORE_ADDR m_address;
20436 bool m_is_stmt;
20437 unsigned int m_discriminator;
d9b3de22
DE
20438
20439 /* Additional bits of state we need to track. */
20440
20441 /* The last file that we called dwarf2_start_subfile for.
20442 This is only used for TLLs. */
6f77053d 20443 unsigned int m_last_file = 0;
d9b3de22 20444 /* The last file a line number was recorded for. */
6f77053d 20445 struct subfile *m_last_subfile = NULL;
d9b3de22 20446
804d2729
TT
20447 /* When true, record the lines we decode. */
20448 bool m_currently_recording_lines = false;
d9b3de22
DE
20449
20450 /* The last line number that was recorded, used to coalesce
20451 consecutive entries for the same line. This can happen, for
20452 example, when discriminators are present. PR 17276. */
6f77053d
PA
20453 unsigned int m_last_line = 0;
20454 bool m_line_has_non_zero_discriminator = false;
8c43009f 20455};
d9b3de22 20456
6f77053d
PA
20457void
20458lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20459{
20460 CORE_ADDR addr_adj = (((m_op_index + adjust)
20461 / m_line_header->maximum_ops_per_instruction)
20462 * m_line_header->minimum_instruction_length);
20463 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20464 m_op_index = ((m_op_index + adjust)
20465 % m_line_header->maximum_ops_per_instruction);
20466}
d9b3de22 20467
6f77053d
PA
20468void
20469lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20470{
6f77053d
PA
20471 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20472 CORE_ADDR addr_adj = (((m_op_index
20473 + (adj_opcode / m_line_header->line_range))
20474 / m_line_header->maximum_ops_per_instruction)
20475 * m_line_header->minimum_instruction_length);
20476 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20477 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20478 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20479
6f77053d
PA
20480 int line_delta = (m_line_header->line_base
20481 + (adj_opcode % m_line_header->line_range));
20482 advance_line (line_delta);
20483 record_line (false);
20484 m_discriminator = 0;
20485}
d9b3de22 20486
6f77053d
PA
20487void
20488lnp_state_machine::handle_set_file (file_name_index file)
20489{
20490 m_file = file;
20491
20492 const file_entry *fe = current_file ();
20493 if (fe == NULL)
20494 dwarf2_debug_line_missing_file_complaint ();
20495 else if (m_record_lines_p)
20496 {
20497 const char *dir = fe->include_dir (m_line_header);
20498
c24bdb02 20499 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20500 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20501 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20502 }
20503}
20504
20505void
20506lnp_state_machine::handle_const_add_pc ()
20507{
20508 CORE_ADDR adjust
20509 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20510
20511 CORE_ADDR addr_adj
20512 = (((m_op_index + adjust)
20513 / m_line_header->maximum_ops_per_instruction)
20514 * m_line_header->minimum_instruction_length);
20515
20516 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20517 m_op_index = ((m_op_index + adjust)
20518 % m_line_header->maximum_ops_per_instruction);
20519}
d9b3de22 20520
a05a36a5
DE
20521/* Return non-zero if we should add LINE to the line number table.
20522 LINE is the line to add, LAST_LINE is the last line that was added,
20523 LAST_SUBFILE is the subfile for LAST_LINE.
20524 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20525 had a non-zero discriminator.
20526
20527 We have to be careful in the presence of discriminators.
20528 E.g., for this line:
20529
20530 for (i = 0; i < 100000; i++);
20531
20532 clang can emit four line number entries for that one line,
20533 each with a different discriminator.
20534 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20535
20536 However, we want gdb to coalesce all four entries into one.
20537 Otherwise the user could stepi into the middle of the line and
20538 gdb would get confused about whether the pc really was in the
20539 middle of the line.
20540
20541 Things are further complicated by the fact that two consecutive
20542 line number entries for the same line is a heuristic used by gcc
20543 to denote the end of the prologue. So we can't just discard duplicate
20544 entries, we have to be selective about it. The heuristic we use is
20545 that we only collapse consecutive entries for the same line if at least
20546 one of those entries has a non-zero discriminator. PR 17276.
20547
20548 Note: Addresses in the line number state machine can never go backwards
20549 within one sequence, thus this coalescing is ok. */
20550
20551static int
804d2729
TT
20552dwarf_record_line_p (struct dwarf2_cu *cu,
20553 unsigned int line, unsigned int last_line,
a05a36a5
DE
20554 int line_has_non_zero_discriminator,
20555 struct subfile *last_subfile)
20556{
c24bdb02 20557 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20558 return 1;
20559 if (line != last_line)
20560 return 1;
20561 /* Same line for the same file that we've seen already.
20562 As a last check, for pr 17276, only record the line if the line
20563 has never had a non-zero discriminator. */
20564 if (!line_has_non_zero_discriminator)
20565 return 1;
20566 return 0;
20567}
20568
804d2729
TT
20569/* Use the CU's builder to record line number LINE beginning at
20570 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20571
20572static void
d9b3de22
DE
20573dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20574 unsigned int line, CORE_ADDR address,
804d2729 20575 struct dwarf2_cu *cu)
252a6764
DE
20576{
20577 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20578
27e0867f
DE
20579 if (dwarf_line_debug)
20580 {
20581 fprintf_unfiltered (gdb_stdlog,
20582 "Recording line %u, file %s, address %s\n",
20583 line, lbasename (subfile->name),
20584 paddress (gdbarch, address));
20585 }
20586
804d2729 20587 if (cu != nullptr)
c24bdb02 20588 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20589}
20590
20591/* Subroutine of dwarf_decode_lines_1 to simplify it.
20592 Mark the end of a set of line number records.
d9b3de22 20593 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20594 If SUBFILE is NULL the request is ignored. */
20595
20596static void
20597dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20598 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20599{
27e0867f
DE
20600 if (subfile == NULL)
20601 return;
20602
20603 if (dwarf_line_debug)
20604 {
20605 fprintf_unfiltered (gdb_stdlog,
20606 "Finishing current line, file %s, address %s\n",
20607 lbasename (subfile->name),
20608 paddress (gdbarch, address));
20609 }
20610
804d2729 20611 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20612}
20613
6f77053d
PA
20614void
20615lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20616{
d9b3de22
DE
20617 if (dwarf_line_debug)
20618 {
20619 fprintf_unfiltered (gdb_stdlog,
20620 "Processing actual line %u: file %u,"
94a72be7 20621 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20622 m_line, m_file,
6f77053d 20623 paddress (m_gdbarch, m_address),
94a72be7
AB
20624 m_is_stmt, m_discriminator,
20625 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20626 }
20627
6f77053d 20628 file_entry *fe = current_file ();
8c43009f
PA
20629
20630 if (fe == NULL)
d9b3de22
DE
20631 dwarf2_debug_line_missing_file_complaint ();
20632 /* For now we ignore lines not starting on an instruction boundary.
20633 But not when processing end_sequence for compatibility with the
20634 previous version of the code. */
6f77053d 20635 else if (m_op_index == 0 || end_sequence)
d9b3de22 20636 {
8c43009f 20637 fe->included_p = 1;
94a72be7
AB
20638 if (m_record_lines_p
20639 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 20640 {
c24bdb02 20641 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20642 || end_sequence)
d9b3de22 20643 {
804d2729
TT
20644 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20645 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20646 }
20647
20648 if (!end_sequence)
20649 {
804d2729 20650 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20651 m_line_has_non_zero_discriminator,
20652 m_last_subfile))
d9b3de22 20653 {
c24bdb02 20654 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20655 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20656 builder->get_current_subfile (),
6f77053d 20657 m_line, m_address,
804d2729 20658 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20659 }
c24bdb02 20660 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20661 m_last_line = m_line;
d9b3de22
DE
20662 }
20663 }
20664 }
20665}
20666
804d2729
TT
20667lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20668 line_header *lh, bool record_lines_p)
d9b3de22 20669{
804d2729 20670 m_cu = cu;
6f77053d
PA
20671 m_gdbarch = arch;
20672 m_record_lines_p = record_lines_p;
20673 m_line_header = lh;
d9b3de22 20674
804d2729 20675 m_currently_recording_lines = true;
d9b3de22 20676
d9b3de22
DE
20677 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20678 was a line entry for it so that the backend has a chance to adjust it
20679 and also record it in case it needs it. This is currently used by MIPS
20680 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20681 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20682 m_is_stmt = lh->default_is_stmt;
20683 m_discriminator = 0;
252a6764
DE
20684}
20685
6f77053d
PA
20686void
20687lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20688 const gdb_byte *line_ptr,
7ab6656f 20689 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20690{
7ab6656f
OJ
20691 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20692 the pc range of the CU. However, we restrict the test to only ADDRESS
20693 values of zero to preserve GDB's previous behaviour which is to handle
20694 the specific case of a function being GC'd by the linker. */
924c2928 20695
7ab6656f 20696 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20697 {
20698 /* This line table is for a function which has been
20699 GCd by the linker. Ignore it. PR gdb/12528 */
20700
518817b3 20701 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20702 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20703
b98664d3 20704 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20705 line_offset, objfile_name (objfile));
804d2729
TT
20706 m_currently_recording_lines = false;
20707 /* Note: m_currently_recording_lines is left as false until we see
20708 DW_LNE_end_sequence. */
924c2928
DE
20709 }
20710}
20711
f3f5162e 20712/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20713 Process the line number information in LH.
20714 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20715 program in order to set included_p for every referenced header. */
debd256d 20716
c906108c 20717static void
43f3e411
DE
20718dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20719 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20720{
d521ce57
TT
20721 const gdb_byte *line_ptr, *extended_end;
20722 const gdb_byte *line_end;
a8c50c1f 20723 unsigned int bytes_read, extended_len;
699ca60a 20724 unsigned char op_code, extended_op;
e142c38c 20725 CORE_ADDR baseaddr;
518817b3 20726 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20727 bfd *abfd = objfile->obfd;
fbf65064 20728 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20729 /* True if we're recording line info (as opposed to building partial
20730 symtabs and just interested in finding include files mentioned by
20731 the line number program). */
20732 bool record_lines_p = !decode_for_pst_p;
e142c38c 20733
b3b3bada 20734 baseaddr = objfile->text_section_offset ();
c906108c 20735
debd256d
JB
20736 line_ptr = lh->statement_program_start;
20737 line_end = lh->statement_program_end;
c906108c
SS
20738
20739 /* Read the statement sequences until there's nothing left. */
20740 while (line_ptr < line_end)
20741 {
6f77053d
PA
20742 /* The DWARF line number program state machine. Reset the state
20743 machine at the start of each sequence. */
804d2729 20744 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20745 bool end_sequence = false;
d9b3de22 20746
8c43009f 20747 if (record_lines_p)
c906108c 20748 {
8c43009f
PA
20749 /* Start a subfile for the current file of the state
20750 machine. */
20751 const file_entry *fe = state_machine.current_file ();
20752
20753 if (fe != NULL)
804d2729 20754 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20755 }
20756
a738430d 20757 /* Decode the table. */
d9b3de22 20758 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20759 {
20760 op_code = read_1_byte (abfd, line_ptr);
20761 line_ptr += 1;
9aa1fe7e 20762
debd256d 20763 if (op_code >= lh->opcode_base)
6e70227d 20764 {
8e07a239 20765 /* Special opcode. */
6f77053d 20766 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20767 }
20768 else switch (op_code)
c906108c
SS
20769 {
20770 case DW_LNS_extended_op:
3e43a32a
MS
20771 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20772 &bytes_read);
473b7be6 20773 line_ptr += bytes_read;
a8c50c1f 20774 extended_end = line_ptr + extended_len;
c906108c
SS
20775 extended_op = read_1_byte (abfd, line_ptr);
20776 line_ptr += 1;
20777 switch (extended_op)
20778 {
20779 case DW_LNE_end_sequence:
6f77053d
PA
20780 state_machine.handle_end_sequence ();
20781 end_sequence = true;
c906108c
SS
20782 break;
20783 case DW_LNE_set_address:
d9b3de22
DE
20784 {
20785 CORE_ADDR address
20786 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20787 line_ptr += bytes_read;
6f77053d
PA
20788
20789 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20790 lowpc - baseaddr, address);
6f77053d 20791 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20792 }
c906108c
SS
20793 break;
20794 case DW_LNE_define_file:
debd256d 20795 {
d521ce57 20796 const char *cur_file;
ecfb656c
PA
20797 unsigned int mod_time, length;
20798 dir_index dindex;
6e70227d 20799
3e43a32a
MS
20800 cur_file = read_direct_string (abfd, line_ptr,
20801 &bytes_read);
debd256d 20802 line_ptr += bytes_read;
ecfb656c 20803 dindex = (dir_index)
debd256d
JB
20804 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20805 line_ptr += bytes_read;
20806 mod_time =
20807 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20808 line_ptr += bytes_read;
20809 length =
20810 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20811 line_ptr += bytes_read;
ecfb656c 20812 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20813 }
c906108c 20814 break;
d0c6ba3d 20815 case DW_LNE_set_discriminator:
6f77053d
PA
20816 {
20817 /* The discriminator is not interesting to the
20818 debugger; just ignore it. We still need to
20819 check its value though:
20820 if there are consecutive entries for the same
20821 (non-prologue) line we want to coalesce them.
20822 PR 17276. */
20823 unsigned int discr
20824 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20825 line_ptr += bytes_read;
20826
20827 state_machine.handle_set_discriminator (discr);
20828 }
d0c6ba3d 20829 break;
c906108c 20830 default:
b98664d3 20831 complaint (_("mangled .debug_line section"));
debd256d 20832 return;
c906108c 20833 }
a8c50c1f
DJ
20834 /* Make sure that we parsed the extended op correctly. If e.g.
20835 we expected a different address size than the producer used,
20836 we may have read the wrong number of bytes. */
20837 if (line_ptr != extended_end)
20838 {
b98664d3 20839 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20840 return;
20841 }
c906108c
SS
20842 break;
20843 case DW_LNS_copy:
6f77053d 20844 state_machine.handle_copy ();
c906108c
SS
20845 break;
20846 case DW_LNS_advance_pc:
2dc7f7b3
TT
20847 {
20848 CORE_ADDR adjust
20849 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20850 line_ptr += bytes_read;
6f77053d
PA
20851
20852 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20853 }
c906108c
SS
20854 break;
20855 case DW_LNS_advance_line:
a05a36a5
DE
20856 {
20857 int line_delta
20858 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20859 line_ptr += bytes_read;
6f77053d
PA
20860
20861 state_machine.handle_advance_line (line_delta);
a05a36a5 20862 }
c906108c
SS
20863 break;
20864 case DW_LNS_set_file:
d9b3de22 20865 {
6f77053d 20866 file_name_index file
ecfb656c
PA
20867 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20868 &bytes_read);
d9b3de22 20869 line_ptr += bytes_read;
8c43009f 20870
6f77053d 20871 state_machine.handle_set_file (file);
d9b3de22 20872 }
c906108c
SS
20873 break;
20874 case DW_LNS_set_column:
0ad93d4f 20875 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20876 line_ptr += bytes_read;
20877 break;
20878 case DW_LNS_negate_stmt:
6f77053d 20879 state_machine.handle_negate_stmt ();
c906108c
SS
20880 break;
20881 case DW_LNS_set_basic_block:
c906108c 20882 break;
c2c6d25f
JM
20883 /* Add to the address register of the state machine the
20884 address increment value corresponding to special opcode
a738430d
MK
20885 255. I.e., this value is scaled by the minimum
20886 instruction length since special opcode 255 would have
b021a221 20887 scaled the increment. */
c906108c 20888 case DW_LNS_const_add_pc:
6f77053d 20889 state_machine.handle_const_add_pc ();
c906108c
SS
20890 break;
20891 case DW_LNS_fixed_advance_pc:
3e29f34a 20892 {
6f77053d 20893 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20894 line_ptr += 2;
6f77053d
PA
20895
20896 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20897 }
c906108c 20898 break;
9aa1fe7e 20899 default:
a738430d
MK
20900 {
20901 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20902 int i;
a738430d 20903
debd256d 20904 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20905 {
20906 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20907 line_ptr += bytes_read;
20908 }
20909 }
c906108c
SS
20910 }
20911 }
d9b3de22
DE
20912
20913 if (!end_sequence)
20914 dwarf2_debug_line_missing_end_sequence_complaint ();
20915
20916 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20917 in which case we still finish recording the last line). */
6f77053d 20918 state_machine.record_line (true);
c906108c 20919 }
f3f5162e
DE
20920}
20921
20922/* Decode the Line Number Program (LNP) for the given line_header
20923 structure and CU. The actual information extracted and the type
20924 of structures created from the LNP depends on the value of PST.
20925
20926 1. If PST is NULL, then this procedure uses the data from the program
20927 to create all necessary symbol tables, and their linetables.
20928
20929 2. If PST is not NULL, this procedure reads the program to determine
20930 the list of files included by the unit represented by PST, and
20931 builds all the associated partial symbol tables.
20932
20933 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20934 It is used for relative paths in the line table.
20935 NOTE: When processing partial symtabs (pst != NULL),
20936 comp_dir == pst->dirname.
20937
20938 NOTE: It is important that psymtabs have the same file name (via strcmp)
20939 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20940 symtab we don't use it in the name of the psymtabs we create.
20941 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20942 A good testcase for this is mb-inline.exp.
20943
527f3840
JK
20944 LOWPC is the lowest address in CU (or 0 if not known).
20945
20946 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20947 for its PC<->lines mapping information. Otherwise only the filename
20948 table is read in. */
f3f5162e
DE
20949
20950static void
20951dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20952 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20953 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20954{
518817b3 20955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20956 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20957
527f3840
JK
20958 if (decode_mapping)
20959 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20960
20961 if (decode_for_pst_p)
20962 {
aaa75496
JB
20963 /* Now that we're done scanning the Line Header Program, we can
20964 create the psymtab of each included file. */
7ba99d21
AT
20965 for (auto &file_entry : lh->file_names ())
20966 if (file_entry.included_p == 1)
aaa75496 20967 {
c89b44cd 20968 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20969 const char *include_name =
7ba99d21
AT
20970 psymtab_include_file_name (lh, file_entry, pst,
20971 comp_dir, &name_holder);
c6da4cef 20972 if (include_name != NULL)
aaa75496
JB
20973 dwarf2_create_include_psymtab (include_name, pst, objfile);
20974 }
20975 }
cb1df416
DJ
20976 else
20977 {
20978 /* Make sure a symtab is created for every file, even files
20979 which contain only variables (i.e. no code with associated
20980 line numbers). */
c24bdb02
KS
20981 buildsym_compunit *builder = cu->get_builder ();
20982 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20983
7ba99d21 20984 for (auto &fe : lh->file_names ())
cb1df416 20985 {
804d2729 20986 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20987 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20988 {
c24bdb02 20989 builder->get_current_subfile ()->symtab
804d2729 20990 = allocate_symtab (cust,
c24bdb02 20991 builder->get_current_subfile ()->name);
43f3e411 20992 }
c24bdb02 20993 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20994 }
20995 }
c906108c
SS
20996}
20997
20998/* Start a subfile for DWARF. FILENAME is the name of the file and
20999 DIRNAME the name of the source directory which contains FILENAME
4d663531 21000 or NULL if not known.
c906108c
SS
21001 This routine tries to keep line numbers from identical absolute and
21002 relative file names in a common subfile.
21003
21004 Using the `list' example from the GDB testsuite, which resides in
21005 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21006 of /srcdir/list0.c yields the following debugging information for list0.c:
21007
c5aa993b 21008 DW_AT_name: /srcdir/list0.c
4d663531 21009 DW_AT_comp_dir: /compdir
357e46e7 21010 files.files[0].name: list0.h
c5aa993b 21011 files.files[0].dir: /srcdir
357e46e7 21012 files.files[1].name: list0.c
c5aa993b 21013 files.files[1].dir: /srcdir
c906108c
SS
21014
21015 The line number information for list0.c has to end up in a single
4f1520fb
FR
21016 subfile, so that `break /srcdir/list0.c:1' works as expected.
21017 start_subfile will ensure that this happens provided that we pass the
21018 concatenation of files.files[1].dir and files.files[1].name as the
21019 subfile's name. */
c906108c
SS
21020
21021static void
804d2729
TT
21022dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21023 const char *dirname)
c906108c 21024{
43816ebc 21025 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21026
4d663531 21027 /* In order not to lose the line information directory,
4f1520fb
FR
21028 we concatenate it to the filename when it makes sense.
21029 Note that the Dwarf3 standard says (speaking of filenames in line
21030 information): ``The directory index is ignored for file names
21031 that represent full path names''. Thus ignoring dirname in the
21032 `else' branch below isn't an issue. */
c906108c 21033
d5166ae1 21034 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21035 {
43816ebc
TT
21036 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21037 filename = copy.get ();
d521ce57 21038 }
c906108c 21039
c24bdb02 21040 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21041}
21042
804d2729
TT
21043/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21044 buildsym_compunit constructor. */
f4dc4d17 21045
c24bdb02
KS
21046struct compunit_symtab *
21047dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21048 CORE_ADDR low_pc)
f4dc4d17 21049{
c24bdb02 21050 gdb_assert (m_builder == nullptr);
43f3e411 21051
c24bdb02
KS
21052 m_builder.reset (new struct buildsym_compunit
21053 (per_cu->dwarf2_per_objfile->objfile,
21054 name, comp_dir, language, low_pc));
93b8bea4 21055
c24bdb02 21056 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21057
c24bdb02
KS
21058 get_builder ()->record_debugformat ("DWARF 2");
21059 get_builder ()->record_producer (producer);
f4dc4d17 21060
c24bdb02 21061 processing_has_namespace_info = false;
43f3e411 21062
c24bdb02 21063 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21064}
21065
4c2df51b
DJ
21066static void
21067var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21068 struct dwarf2_cu *cu)
4c2df51b 21069{
518817b3 21070 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21071 struct comp_unit_head *cu_header = &cu->header;
21072
4c2df51b
DJ
21073 /* NOTE drow/2003-01-30: There used to be a comment and some special
21074 code here to turn a symbol with DW_AT_external and a
21075 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21076 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21077 with some versions of binutils) where shared libraries could have
21078 relocations against symbols in their debug information - the
21079 minimal symbol would have the right address, but the debug info
21080 would not. It's no longer necessary, because we will explicitly
21081 apply relocations when we read in the debug information now. */
21082
21083 /* A DW_AT_location attribute with no contents indicates that a
21084 variable has been optimized away. */
4fc6c0d5 21085 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 21086 {
f1e6e072 21087 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21088 return;
21089 }
21090
21091 /* Handle one degenerate form of location expression specially, to
21092 preserve GDB's previous behavior when section offsets are
336d760d
AT
21093 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21094 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21095
4fc6c0d5 21096 if (attr->form_is_block ()
3019eac3
DE
21097 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21098 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21099 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21100 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21101 && (DW_BLOCK (attr)->size
21102 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21103 {
891d2f0b 21104 unsigned int dummy;
4c2df51b 21105
3019eac3 21106 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21107 SET_SYMBOL_VALUE_ADDRESS (sym,
21108 read_address (objfile->obfd,
21109 DW_BLOCK (attr)->data + 1,
21110 cu, &dummy));
3019eac3 21111 else
38583298
TT
21112 SET_SYMBOL_VALUE_ADDRESS
21113 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21114 &dummy));
f1e6e072 21115 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21116 fixup_symbol_section (sym, objfile);
6a053cb1
TT
21117 SET_SYMBOL_VALUE_ADDRESS
21118 (sym,
21119 SYMBOL_VALUE_ADDRESS (sym)
21120 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
21121 return;
21122 }
21123
21124 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21125 expression evaluator, and use LOC_COMPUTED only when necessary
21126 (i.e. when the value of a register or memory location is
21127 referenced, or a thread-local block, etc.). Then again, it might
21128 not be worthwhile. I'm assuming that it isn't unless performance
21129 or memory numbers show me otherwise. */
21130
f1e6e072 21131 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21132
f1e6e072 21133 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21134 cu->has_loclist = true;
4c2df51b
DJ
21135}
21136
c906108c
SS
21137/* Given a pointer to a DWARF information entry, figure out if we need
21138 to make a symbol table entry for it, and if so, create a new entry
21139 and return a pointer to it.
21140 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21141 used the passed type.
21142 If SPACE is not NULL, use it to hold the new symbol. If it is
21143 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21144
21145static struct symbol *
5e2db402
TT
21146new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21147 struct symbol *space)
c906108c 21148{
518817b3
SM
21149 struct dwarf2_per_objfile *dwarf2_per_objfile
21150 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21151 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21152 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21153 struct symbol *sym = NULL;
15d034d0 21154 const char *name;
c906108c
SS
21155 struct attribute *attr = NULL;
21156 struct attribute *attr2 = NULL;
e142c38c 21157 CORE_ADDR baseaddr;
e37fd15a
SW
21158 struct pending **list_to_add = NULL;
21159
edb3359d 21160 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21161
b3b3bada 21162 baseaddr = objfile->text_section_offset ();
c906108c 21163
94af9270 21164 name = dwarf2_name (die, cu);
c906108c
SS
21165 if (name)
21166 {
94af9270 21167 const char *linkagename;
34eaf542 21168 int suppress_add = 0;
94af9270 21169
34eaf542
TT
21170 if (space)
21171 sym = space;
21172 else
e623cf5d 21173 sym = allocate_symbol (objfile);
c906108c 21174 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21175
21176 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21177 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21178 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21179 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21180
f55ee35c
JK
21181 /* Fortran does not have mangling standard and the mangling does differ
21182 between gfortran, iFort etc. */
21183 if (cu->language == language_fortran
468c0cbb
CB
21184 && symbol_get_demangled_name (sym) == NULL)
21185 symbol_set_demangled_name (sym,
cfc594ee 21186 dwarf2_full_name (name, die, cu),
29df156d 21187 NULL);
f55ee35c 21188
c906108c 21189 /* Default assumptions.
c5aa993b 21190 Use the passed type or decode it from the die. */
176620f1 21191 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21192 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21193 if (type != NULL)
21194 SYMBOL_TYPE (sym) = type;
21195 else
e7c27a73 21196 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21197 attr = dwarf2_attr (die,
21198 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21199 cu);
435d3d88 21200 if (attr != nullptr)
c906108c
SS
21201 {
21202 SYMBOL_LINE (sym) = DW_UNSND (attr);
21203 }
cb1df416 21204
edb3359d
DJ
21205 attr = dwarf2_attr (die,
21206 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21207 cu);
435d3d88 21208 if (attr != nullptr)
cb1df416 21209 {
ecfb656c 21210 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21211 struct file_entry *fe;
9a619af0 21212
ecfb656c
PA
21213 if (cu->line_header != NULL)
21214 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21215 else
21216 fe = NULL;
21217
21218 if (fe == NULL)
b98664d3 21219 complaint (_("file index out of range"));
8c43009f
PA
21220 else
21221 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21222 }
21223
c906108c
SS
21224 switch (die->tag)
21225 {
21226 case DW_TAG_label:
e142c38c 21227 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21228 if (attr != nullptr)
3e29f34a
MR
21229 {
21230 CORE_ADDR addr;
21231
cd6c91b4 21232 addr = attr->value_as_address ();
3e29f34a 21233 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21234 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21235 }
0f5238ed
TT
21236 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21237 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21238 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21239 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21240 break;
21241 case DW_TAG_subprogram:
21242 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21243 finish_block. */
f1e6e072 21244 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21245 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21246 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21247 || cu->language == language_ada
21248 || cu->language == language_fortran)
c906108c 21249 {
2cfa0c8d 21250 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21251 Ada and Fortran subprograms, whether marked external or
21252 not, are always stored as a global symbol, because we want
21253 to be able to access them globally. For instance, we want
21254 to be able to break on a nested subprogram without having
21255 to specify the context. */
c24bdb02 21256 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21257 }
21258 else
21259 {
e37fd15a 21260 list_to_add = cu->list_in_scope;
c906108c
SS
21261 }
21262 break;
edb3359d
DJ
21263 case DW_TAG_inlined_subroutine:
21264 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21265 finish_block. */
f1e6e072 21266 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21267 SYMBOL_INLINED (sym) = 1;
481860b3 21268 list_to_add = cu->list_in_scope;
edb3359d 21269 break;
34eaf542
TT
21270 case DW_TAG_template_value_param:
21271 suppress_add = 1;
21272 /* Fall through. */
72929c62 21273 case DW_TAG_constant:
c906108c 21274 case DW_TAG_variable:
254e6b9e 21275 case DW_TAG_member:
0963b4bd
MS
21276 /* Compilation with minimal debug info may result in
21277 variables with missing type entries. Change the
21278 misleading `void' type to something sensible. */
c906108c 21279 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21280 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21281
e142c38c 21282 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21283 /* In the case of DW_TAG_member, we should only be called for
21284 static const members. */
21285 if (die->tag == DW_TAG_member)
21286 {
3863f96c
DE
21287 /* dwarf2_add_field uses die_is_declaration,
21288 so we do the same. */
254e6b9e
DE
21289 gdb_assert (die_is_declaration (die, cu));
21290 gdb_assert (attr);
21291 }
435d3d88 21292 if (attr != nullptr)
c906108c 21293 {
e7c27a73 21294 dwarf2_const_value (attr, sym, cu);
e142c38c 21295 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21296 if (!suppress_add)
34eaf542
TT
21297 {
21298 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21299 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21300 else
e37fd15a 21301 list_to_add = cu->list_in_scope;
34eaf542 21302 }
c906108c
SS
21303 break;
21304 }
e142c38c 21305 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21306 if (attr != nullptr)
c906108c 21307 {
e7c27a73 21308 var_decode_location (attr, sym, cu);
e142c38c 21309 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21310
21311 /* Fortran explicitly imports any global symbols to the local
21312 scope by DW_TAG_common_block. */
21313 if (cu->language == language_fortran && die->parent
21314 && die->parent->tag == DW_TAG_common_block)
21315 attr2 = NULL;
21316
caac4577
JG
21317 if (SYMBOL_CLASS (sym) == LOC_STATIC
21318 && SYMBOL_VALUE_ADDRESS (sym) == 0
21319 && !dwarf2_per_objfile->has_section_at_zero)
21320 {
21321 /* When a static variable is eliminated by the linker,
21322 the corresponding debug information is not stripped
21323 out, but the variable address is set to null;
21324 do not add such variables into symbol table. */
21325 }
21326 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21327 {
4b610737
TT
21328 if (SYMBOL_CLASS (sym) == LOC_STATIC
21329 && (objfile->flags & OBJF_MAINLINE) == 0
21330 && dwarf2_per_objfile->can_copy)
21331 {
21332 /* A global static variable might be subject to
21333 copy relocation. We first check for a local
21334 minsym, though, because maybe the symbol was
21335 marked hidden, in which case this would not
21336 apply. */
21337 bound_minimal_symbol found
21338 = (lookup_minimal_symbol_linkage
987012b8 21339 (sym->linkage_name (), objfile));
4b610737
TT
21340 if (found.minsym != nullptr)
21341 sym->maybe_copied = 1;
21342 }
f55ee35c 21343
1c809c68
TT
21344 /* A variable with DW_AT_external is never static,
21345 but it may be block-scoped. */
804d2729 21346 list_to_add
c24bdb02
KS
21347 = ((cu->list_in_scope
21348 == cu->get_builder ()->get_file_symbols ())
21349 ? cu->get_builder ()->get_global_symbols ()
804d2729 21350 : cu->list_in_scope);
1c809c68 21351 }
c906108c 21352 else
e37fd15a 21353 list_to_add = cu->list_in_scope;
c906108c
SS
21354 }
21355 else
21356 {
21357 /* We do not know the address of this symbol.
c5aa993b
JM
21358 If it is an external symbol and we have type information
21359 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21360 The address of the variable will then be determined from
21361 the minimal symbol table whenever the variable is
21362 referenced. */
e142c38c 21363 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21364
21365 /* Fortran explicitly imports any global symbols to the local
21366 scope by DW_TAG_common_block. */
21367 if (cu->language == language_fortran && die->parent
21368 && die->parent->tag == DW_TAG_common_block)
21369 {
21370 /* SYMBOL_CLASS doesn't matter here because
21371 read_common_block is going to reset it. */
21372 if (!suppress_add)
21373 list_to_add = cu->list_in_scope;
21374 }
21375 else if (attr2 && (DW_UNSND (attr2) != 0)
21376 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21377 {
0fe7935b
DJ
21378 /* A variable with DW_AT_external is never static, but it
21379 may be block-scoped. */
804d2729 21380 list_to_add
c24bdb02
KS
21381 = ((cu->list_in_scope
21382 == cu->get_builder ()->get_file_symbols ())
21383 ? cu->get_builder ()->get_global_symbols ()
804d2729 21384 : cu->list_in_scope);
0fe7935b 21385
f1e6e072 21386 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21387 }
442ddf59
JK
21388 else if (!die_is_declaration (die, cu))
21389 {
21390 /* Use the default LOC_OPTIMIZED_OUT class. */
21391 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21392 if (!suppress_add)
21393 list_to_add = cu->list_in_scope;
442ddf59 21394 }
c906108c
SS
21395 }
21396 break;
21397 case DW_TAG_formal_parameter:
a60f3166
TT
21398 {
21399 /* If we are inside a function, mark this as an argument. If
21400 not, we might be looking at an argument to an inlined function
21401 when we do not have enough information to show inlined frames;
21402 pretend it's a local variable in that case so that the user can
21403 still see it. */
804d2729 21404 struct context_stack *curr
c24bdb02 21405 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21406 if (curr != nullptr && curr->name != nullptr)
21407 SYMBOL_IS_ARGUMENT (sym) = 1;
21408 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21409 if (attr != nullptr)
a60f3166
TT
21410 {
21411 var_decode_location (attr, sym, cu);
21412 }
21413 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21414 if (attr != nullptr)
a60f3166
TT
21415 {
21416 dwarf2_const_value (attr, sym, cu);
21417 }
f346a30d 21418
a60f3166
TT
21419 list_to_add = cu->list_in_scope;
21420 }
c906108c
SS
21421 break;
21422 case DW_TAG_unspecified_parameters:
21423 /* From varargs functions; gdb doesn't seem to have any
21424 interest in this information, so just ignore it for now.
21425 (FIXME?) */
21426 break;
34eaf542
TT
21427 case DW_TAG_template_type_param:
21428 suppress_add = 1;
21429 /* Fall through. */
c906108c 21430 case DW_TAG_class_type:
680b30c7 21431 case DW_TAG_interface_type:
c906108c
SS
21432 case DW_TAG_structure_type:
21433 case DW_TAG_union_type:
72019c9c 21434 case DW_TAG_set_type:
c906108c 21435 case DW_TAG_enumeration_type:
f1e6e072 21436 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21437 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21438
63d06c5c 21439 {
9c37b5ae 21440 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21441 really ever be static objects: otherwise, if you try
21442 to, say, break of a class's method and you're in a file
21443 which doesn't mention that class, it won't work unless
21444 the check for all static symbols in lookup_symbol_aux
21445 saves you. See the OtherFileClass tests in
21446 gdb.c++/namespace.exp. */
21447
e37fd15a 21448 if (!suppress_add)
34eaf542 21449 {
c24bdb02 21450 buildsym_compunit *builder = cu->get_builder ();
804d2729 21451 list_to_add
c24bdb02 21452 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21453 && cu->language == language_cplus
c24bdb02 21454 ? builder->get_global_symbols ()
804d2729 21455 : cu->list_in_scope);
63d06c5c 21456
64382290 21457 /* The semantics of C++ state that "struct foo {
9c37b5ae 21458 ... }" also defines a typedef for "foo". */
64382290 21459 if (cu->language == language_cplus
45280282 21460 || cu->language == language_ada
c44af4eb
TT
21461 || cu->language == language_d
21462 || cu->language == language_rust)
64382290
TT
21463 {
21464 /* The symbol's name is already allocated along
21465 with this objfile, so we don't need to
21466 duplicate it for the type. */
21467 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 21468 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 21469 }
63d06c5c
DC
21470 }
21471 }
c906108c
SS
21472 break;
21473 case DW_TAG_typedef:
f1e6e072 21474 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21475 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21476 list_to_add = cu->list_in_scope;
63d06c5c 21477 break;
c906108c 21478 case DW_TAG_base_type:
a02abb62 21479 case DW_TAG_subrange_type:
f1e6e072 21480 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21481 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21482 list_to_add = cu->list_in_scope;
c906108c
SS
21483 break;
21484 case DW_TAG_enumerator:
e142c38c 21485 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21486 if (attr != nullptr)
c906108c 21487 {
e7c27a73 21488 dwarf2_const_value (attr, sym, cu);
c906108c 21489 }
63d06c5c
DC
21490 {
21491 /* NOTE: carlton/2003-11-10: See comment above in the
21492 DW_TAG_class_type, etc. block. */
21493
804d2729 21494 list_to_add
c24bdb02 21495 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21496 && cu->language == language_cplus
c24bdb02 21497 ? cu->get_builder ()->get_global_symbols ()
804d2729 21498 : cu->list_in_scope);
63d06c5c 21499 }
c906108c 21500 break;
74921315 21501 case DW_TAG_imported_declaration:
5c4e30ca 21502 case DW_TAG_namespace:
f1e6e072 21503 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21504 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21505 break;
530e8392
KB
21506 case DW_TAG_module:
21507 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21508 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21509 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21510 break;
4357ac6c 21511 case DW_TAG_common_block:
f1e6e072 21512 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21513 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21514 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21515 break;
c906108c
SS
21516 default:
21517 /* Not a tag we recognize. Hopefully we aren't processing
21518 trash data, but since we must specifically ignore things
21519 we don't recognize, there is nothing else we should do at
0963b4bd 21520 this point. */
b98664d3 21521 complaint (_("unsupported tag: '%s'"),
4d3c2250 21522 dwarf_tag_name (die->tag));
c906108c
SS
21523 break;
21524 }
df8a16a1 21525
e37fd15a
SW
21526 if (suppress_add)
21527 {
21528 sym->hash_next = objfile->template_symbols;
21529 objfile->template_symbols = sym;
21530 list_to_add = NULL;
21531 }
21532
21533 if (list_to_add != NULL)
d3cb6808 21534 add_symbol_to_list (sym, list_to_add);
e37fd15a 21535
df8a16a1
DJ
21536 /* For the benefit of old versions of GCC, check for anonymous
21537 namespaces based on the demangled name. */
4d4ec4e5 21538 if (!cu->processing_has_namespace_info
94af9270 21539 && cu->language == language_cplus)
c24bdb02 21540 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21541 }
21542 return (sym);
21543}
21544
98bfdba5
PA
21545/* Given an attr with a DW_FORM_dataN value in host byte order,
21546 zero-extend it as appropriate for the symbol's type. The DWARF
21547 standard (v4) is not entirely clear about the meaning of using
21548 DW_FORM_dataN for a constant with a signed type, where the type is
21549 wider than the data. The conclusion of a discussion on the DWARF
21550 list was that this is unspecified. We choose to always zero-extend
21551 because that is the interpretation long in use by GCC. */
c906108c 21552
98bfdba5 21553static gdb_byte *
ff39bb5e 21554dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21555 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21556{
518817b3 21557 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21558 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21559 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21560 LONGEST l = DW_UNSND (attr);
21561
21562 if (bits < sizeof (*value) * 8)
21563 {
21564 l &= ((LONGEST) 1 << bits) - 1;
21565 *value = l;
21566 }
21567 else if (bits == sizeof (*value) * 8)
21568 *value = l;
21569 else
21570 {
224c3ddb 21571 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21572 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21573 return bytes;
21574 }
21575
21576 return NULL;
21577}
21578
21579/* Read a constant value from an attribute. Either set *VALUE, or if
21580 the value does not fit in *VALUE, set *BYTES - either already
21581 allocated on the objfile obstack, or newly allocated on OBSTACK,
21582 or, set *BATON, if we translated the constant to a location
21583 expression. */
21584
21585static void
ff39bb5e 21586dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21587 const char *name, struct obstack *obstack,
21588 struct dwarf2_cu *cu,
d521ce57 21589 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21590 struct dwarf2_locexpr_baton **baton)
21591{
518817b3 21592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21593 struct comp_unit_head *cu_header = &cu->header;
c906108c 21594 struct dwarf_block *blk;
98bfdba5
PA
21595 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21596 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21597
21598 *value = 0;
21599 *bytes = NULL;
21600 *baton = NULL;
c906108c
SS
21601
21602 switch (attr->form)
21603 {
21604 case DW_FORM_addr:
336d760d 21605 case DW_FORM_addrx:
3019eac3 21606 case DW_FORM_GNU_addr_index:
ac56253d 21607 {
ac56253d
TT
21608 gdb_byte *data;
21609
98bfdba5
PA
21610 if (TYPE_LENGTH (type) != cu_header->addr_size)
21611 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21612 cu_header->addr_size,
98bfdba5 21613 TYPE_LENGTH (type));
ac56253d
TT
21614 /* Symbols of this form are reasonably rare, so we just
21615 piggyback on the existing location code rather than writing
21616 a new implementation of symbol_computed_ops. */
8d749320 21617 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21618 (*baton)->per_cu = cu->per_cu;
21619 gdb_assert ((*baton)->per_cu);
ac56253d 21620
98bfdba5 21621 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21622 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21623 (*baton)->data = data;
ac56253d
TT
21624
21625 data[0] = DW_OP_addr;
21626 store_unsigned_integer (&data[1], cu_header->addr_size,
21627 byte_order, DW_ADDR (attr));
21628 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21629 }
c906108c 21630 break;
4ac36638 21631 case DW_FORM_string:
93b5768b 21632 case DW_FORM_strp:
cf532bd1 21633 case DW_FORM_strx:
3019eac3 21634 case DW_FORM_GNU_str_index:
36586728 21635 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21636 /* DW_STRING is already allocated on the objfile obstack, point
21637 directly to it. */
d521ce57 21638 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21639 break;
c906108c
SS
21640 case DW_FORM_block1:
21641 case DW_FORM_block2:
21642 case DW_FORM_block4:
21643 case DW_FORM_block:
2dc7f7b3 21644 case DW_FORM_exprloc:
0224619f 21645 case DW_FORM_data16:
c906108c 21646 blk = DW_BLOCK (attr);
98bfdba5
PA
21647 if (TYPE_LENGTH (type) != blk->size)
21648 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21649 TYPE_LENGTH (type));
21650 *bytes = blk->data;
c906108c 21651 break;
2df3850c
JM
21652
21653 /* The DW_AT_const_value attributes are supposed to carry the
21654 symbol's value "represented as it would be on the target
21655 architecture." By the time we get here, it's already been
21656 converted to host endianness, so we just need to sign- or
21657 zero-extend it as appropriate. */
21658 case DW_FORM_data1:
3aef2284 21659 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21660 break;
c906108c 21661 case DW_FORM_data2:
3aef2284 21662 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21663 break;
c906108c 21664 case DW_FORM_data4:
3aef2284 21665 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21666 break;
c906108c 21667 case DW_FORM_data8:
3aef2284 21668 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21669 break;
21670
c906108c 21671 case DW_FORM_sdata:
663c44ac 21672 case DW_FORM_implicit_const:
98bfdba5 21673 *value = DW_SND (attr);
2df3850c
JM
21674 break;
21675
c906108c 21676 case DW_FORM_udata:
98bfdba5 21677 *value = DW_UNSND (attr);
c906108c 21678 break;
2df3850c 21679
c906108c 21680 default:
b98664d3 21681 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21682 dwarf_form_name (attr->form));
98bfdba5 21683 *value = 0;
c906108c
SS
21684 break;
21685 }
21686}
21687
2df3850c 21688
98bfdba5
PA
21689/* Copy constant value from an attribute to a symbol. */
21690
2df3850c 21691static void
ff39bb5e 21692dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21693 struct dwarf2_cu *cu)
2df3850c 21694{
518817b3 21695 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21696 LONGEST value;
d521ce57 21697 const gdb_byte *bytes;
98bfdba5 21698 struct dwarf2_locexpr_baton *baton;
2df3850c 21699
98bfdba5 21700 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21701 sym->print_name (),
98bfdba5
PA
21702 &objfile->objfile_obstack, cu,
21703 &value, &bytes, &baton);
2df3850c 21704
98bfdba5
PA
21705 if (baton != NULL)
21706 {
98bfdba5 21707 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21708 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21709 }
21710 else if (bytes != NULL)
21711 {
21712 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21713 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21714 }
21715 else
21716 {
21717 SYMBOL_VALUE (sym) = value;
f1e6e072 21718 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21719 }
2df3850c
JM
21720}
21721
c906108c
SS
21722/* Return the type of the die in question using its DW_AT_type attribute. */
21723
21724static struct type *
e7c27a73 21725die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21726{
c906108c 21727 struct attribute *type_attr;
c906108c 21728
e142c38c 21729 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21730 if (!type_attr)
21731 {
518817b3 21732 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21733 /* A missing DW_AT_type represents a void type. */
518817b3 21734 return objfile_type (objfile)->builtin_void;
c906108c 21735 }
348e048f 21736
673bfd45 21737 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21738}
21739
b4ba55a1
JB
21740/* True iff CU's producer generates GNAT Ada auxiliary information
21741 that allows to find parallel types through that information instead
21742 of having to do expensive parallel lookups by type name. */
21743
21744static int
21745need_gnat_info (struct dwarf2_cu *cu)
21746{
de4cb04a
JB
21747 /* Assume that the Ada compiler was GNAT, which always produces
21748 the auxiliary information. */
21749 return (cu->language == language_ada);
b4ba55a1
JB
21750}
21751
b4ba55a1
JB
21752/* Return the auxiliary type of the die in question using its
21753 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21754 attribute is not present. */
21755
21756static struct type *
21757die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21758{
b4ba55a1 21759 struct attribute *type_attr;
b4ba55a1
JB
21760
21761 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21762 if (!type_attr)
21763 return NULL;
21764
673bfd45 21765 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21766}
21767
21768/* If DIE has a descriptive_type attribute, then set the TYPE's
21769 descriptive type accordingly. */
21770
21771static void
21772set_descriptive_type (struct type *type, struct die_info *die,
21773 struct dwarf2_cu *cu)
21774{
21775 struct type *descriptive_type = die_descriptive_type (die, cu);
21776
21777 if (descriptive_type)
21778 {
21779 ALLOCATE_GNAT_AUX_TYPE (type);
21780 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21781 }
21782}
21783
c906108c
SS
21784/* Return the containing type of the die in question using its
21785 DW_AT_containing_type attribute. */
21786
21787static struct type *
e7c27a73 21788die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21789{
c906108c 21790 struct attribute *type_attr;
518817b3 21791 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21792
e142c38c 21793 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21794 if (!type_attr)
21795 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21796 "[in module %s]"), objfile_name (objfile));
33ac96f0 21797
673bfd45 21798 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21799}
21800
ac9ec31b
DE
21801/* Return an error marker type to use for the ill formed type in DIE/CU. */
21802
21803static struct type *
21804build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21805{
518817b3
SM
21806 struct dwarf2_per_objfile *dwarf2_per_objfile
21807 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21808 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21809 char *saved;
ac9ec31b 21810
528e1572
SM
21811 std::string message
21812 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21813 objfile_name (objfile),
21814 sect_offset_str (cu->header.sect_off),
21815 sect_offset_str (die->sect_off));
efba19b0 21816 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21817
19f392bc 21818 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21819}
21820
673bfd45 21821/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21822 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21823 DW_AT_containing_type.
673bfd45
DE
21824 If there is no type substitute an error marker. */
21825
c906108c 21826static struct type *
ff39bb5e 21827lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21828 struct dwarf2_cu *cu)
c906108c 21829{
518817b3
SM
21830 struct dwarf2_per_objfile *dwarf2_per_objfile
21831 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21832 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21833 struct type *this_type;
21834
ac9ec31b
DE
21835 gdb_assert (attr->name == DW_AT_type
21836 || attr->name == DW_AT_GNAT_descriptive_type
21837 || attr->name == DW_AT_containing_type);
21838
673bfd45
DE
21839 /* First see if we have it cached. */
21840
36586728
TT
21841 if (attr->form == DW_FORM_GNU_ref_alt)
21842 {
21843 struct dwarf2_per_cu_data *per_cu;
9c541725 21844 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21845
ed2dc618
SM
21846 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21847 dwarf2_per_objfile);
9c541725 21848 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21849 }
cd6c91b4 21850 else if (attr->form_is_ref ())
673bfd45 21851 {
9c541725 21852 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21853
9c541725 21854 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21855 }
55f1336d 21856 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21857 {
ac9ec31b 21858 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21859
ac9ec31b 21860 return get_signatured_type (die, signature, cu);
673bfd45
DE
21861 }
21862 else
21863 {
b98664d3 21864 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21865 " at %s [in module %s]"),
21866 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21867 objfile_name (objfile));
ac9ec31b 21868 return build_error_marker_type (cu, die);
673bfd45
DE
21869 }
21870
21871 /* If not cached we need to read it in. */
21872
21873 if (this_type == NULL)
21874 {
ac9ec31b 21875 struct die_info *type_die = NULL;
673bfd45
DE
21876 struct dwarf2_cu *type_cu = cu;
21877
cd6c91b4 21878 if (attr->form_is_ref ())
ac9ec31b
DE
21879 type_die = follow_die_ref (die, attr, &type_cu);
21880 if (type_die == NULL)
21881 return build_error_marker_type (cu, die);
21882 /* If we find the type now, it's probably because the type came
3019eac3
DE
21883 from an inter-CU reference and the type's CU got expanded before
21884 ours. */
ac9ec31b 21885 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21886 }
21887
21888 /* If we still don't have a type use an error marker. */
21889
21890 if (this_type == NULL)
ac9ec31b 21891 return build_error_marker_type (cu, die);
673bfd45 21892
f792889a 21893 return this_type;
c906108c
SS
21894}
21895
673bfd45
DE
21896/* Return the type in DIE, CU.
21897 Returns NULL for invalid types.
21898
02142a6c 21899 This first does a lookup in die_type_hash,
673bfd45
DE
21900 and only reads the die in if necessary.
21901
21902 NOTE: This can be called when reading in partial or full symbols. */
21903
f792889a 21904static struct type *
e7c27a73 21905read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21906{
f792889a
DJ
21907 struct type *this_type;
21908
21909 this_type = get_die_type (die, cu);
21910 if (this_type)
21911 return this_type;
21912
673bfd45
DE
21913 return read_type_die_1 (die, cu);
21914}
21915
21916/* Read the type in DIE, CU.
21917 Returns NULL for invalid types. */
21918
21919static struct type *
21920read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21921{
21922 struct type *this_type = NULL;
21923
c906108c
SS
21924 switch (die->tag)
21925 {
21926 case DW_TAG_class_type:
680b30c7 21927 case DW_TAG_interface_type:
c906108c
SS
21928 case DW_TAG_structure_type:
21929 case DW_TAG_union_type:
f792889a 21930 this_type = read_structure_type (die, cu);
c906108c
SS
21931 break;
21932 case DW_TAG_enumeration_type:
f792889a 21933 this_type = read_enumeration_type (die, cu);
c906108c
SS
21934 break;
21935 case DW_TAG_subprogram:
21936 case DW_TAG_subroutine_type:
edb3359d 21937 case DW_TAG_inlined_subroutine:
f792889a 21938 this_type = read_subroutine_type (die, cu);
c906108c
SS
21939 break;
21940 case DW_TAG_array_type:
f792889a 21941 this_type = read_array_type (die, cu);
c906108c 21942 break;
72019c9c 21943 case DW_TAG_set_type:
f792889a 21944 this_type = read_set_type (die, cu);
72019c9c 21945 break;
c906108c 21946 case DW_TAG_pointer_type:
f792889a 21947 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21948 break;
21949 case DW_TAG_ptr_to_member_type:
f792889a 21950 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21951 break;
21952 case DW_TAG_reference_type:
4297a3f0
AV
21953 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21954 break;
21955 case DW_TAG_rvalue_reference_type:
21956 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21957 break;
21958 case DW_TAG_const_type:
f792889a 21959 this_type = read_tag_const_type (die, cu);
c906108c
SS
21960 break;
21961 case DW_TAG_volatile_type:
f792889a 21962 this_type = read_tag_volatile_type (die, cu);
c906108c 21963 break;
06d66ee9
TT
21964 case DW_TAG_restrict_type:
21965 this_type = read_tag_restrict_type (die, cu);
21966 break;
c906108c 21967 case DW_TAG_string_type:
f792889a 21968 this_type = read_tag_string_type (die, cu);
c906108c
SS
21969 break;
21970 case DW_TAG_typedef:
f792889a 21971 this_type = read_typedef (die, cu);
c906108c 21972 break;
a02abb62 21973 case DW_TAG_subrange_type:
f792889a 21974 this_type = read_subrange_type (die, cu);
a02abb62 21975 break;
c906108c 21976 case DW_TAG_base_type:
f792889a 21977 this_type = read_base_type (die, cu);
c906108c 21978 break;
81a17f79 21979 case DW_TAG_unspecified_type:
f792889a 21980 this_type = read_unspecified_type (die, cu);
81a17f79 21981 break;
0114d602
DJ
21982 case DW_TAG_namespace:
21983 this_type = read_namespace_type (die, cu);
21984 break;
f55ee35c
JK
21985 case DW_TAG_module:
21986 this_type = read_module_type (die, cu);
21987 break;
a2c2acaf
MW
21988 case DW_TAG_atomic_type:
21989 this_type = read_tag_atomic_type (die, cu);
21990 break;
c906108c 21991 default:
b98664d3 21992 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21993 dwarf_tag_name (die->tag));
c906108c
SS
21994 break;
21995 }
63d06c5c 21996
f792889a 21997 return this_type;
63d06c5c
DC
21998}
21999
abc72ce4
DE
22000/* See if we can figure out if the class lives in a namespace. We do
22001 this by looking for a member function; its demangled name will
22002 contain namespace info, if there is any.
22003 Return the computed name or NULL.
22004 Space for the result is allocated on the objfile's obstack.
22005 This is the full-die version of guess_partial_die_structure_name.
22006 In this case we know DIE has no useful parent. */
22007
43816ebc 22008static const char *
abc72ce4
DE
22009guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22010{
22011 struct die_info *spec_die;
22012 struct dwarf2_cu *spec_cu;
22013 struct die_info *child;
518817b3 22014 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22015
22016 spec_cu = cu;
22017 spec_die = die_specification (die, &spec_cu);
22018 if (spec_die != NULL)
22019 {
22020 die = spec_die;
22021 cu = spec_cu;
22022 }
22023
22024 for (child = die->child;
22025 child != NULL;
22026 child = child->sibling)
22027 {
22028 if (child->tag == DW_TAG_subprogram)
22029 {
73b9be8b 22030 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22031
7d45c7c3 22032 if (linkage_name != NULL)
abc72ce4 22033 {
43816ebc
TT
22034 gdb::unique_xmalloc_ptr<char> actual_name
22035 (language_class_name_from_physname (cu->language_defn,
22036 linkage_name));
22037 const char *name = NULL;
abc72ce4
DE
22038
22039 if (actual_name != NULL)
22040 {
15d034d0 22041 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22042
22043 if (die_name != NULL
43816ebc 22044 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22045 {
22046 /* Strip off the class name from the full name.
22047 We want the prefix. */
22048 int die_name_len = strlen (die_name);
43816ebc
TT
22049 int actual_name_len = strlen (actual_name.get ());
22050 const char *ptr = actual_name.get ();
abc72ce4
DE
22051
22052 /* Test for '::' as a sanity check. */
22053 if (actual_name_len > die_name_len + 2
43816ebc 22054 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22055 name = obstack_strndup (
e3b94546 22056 &objfile->per_bfd->storage_obstack,
43816ebc 22057 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22058 }
22059 }
abc72ce4
DE
22060 return name;
22061 }
22062 }
22063 }
22064
22065 return NULL;
22066}
22067
96408a79
SA
22068/* GCC might emit a nameless typedef that has a linkage name. Determine the
22069 prefix part in such case. See
22070 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22071
a121b7c1 22072static const char *
96408a79
SA
22073anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22074{
22075 struct attribute *attr;
e6a959d6 22076 const char *base;
96408a79
SA
22077
22078 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22079 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22080 return NULL;
22081
7d45c7c3 22082 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22083 return NULL;
22084
73b9be8b 22085 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22086 if (attr == NULL || DW_STRING (attr) == NULL)
22087 return NULL;
22088
22089 /* dwarf2_name had to be already called. */
22090 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22091
22092 /* Strip the base name, keep any leading namespaces/classes. */
22093 base = strrchr (DW_STRING (attr), ':');
22094 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22095 return "";
22096
518817b3 22097 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22098 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22099 DW_STRING (attr),
22100 &base[-1] - DW_STRING (attr));
96408a79
SA
22101}
22102
fdde2d81 22103/* Return the name of the namespace/class that DIE is defined within,
0114d602 22104 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22105
0114d602
DJ
22106 For example, if we're within the method foo() in the following
22107 code:
22108
22109 namespace N {
22110 class C {
22111 void foo () {
22112 }
22113 };
22114 }
22115
22116 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22117
0d5cff50 22118static const char *
e142c38c 22119determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22120{
518817b3
SM
22121 struct dwarf2_per_objfile *dwarf2_per_objfile
22122 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22123 struct die_info *parent, *spec_die;
22124 struct dwarf2_cu *spec_cu;
22125 struct type *parent_type;
a121b7c1 22126 const char *retval;
63d06c5c 22127
9c37b5ae 22128 if (cu->language != language_cplus
c44af4eb
TT
22129 && cu->language != language_fortran && cu->language != language_d
22130 && cu->language != language_rust)
0114d602
DJ
22131 return "";
22132
96408a79
SA
22133 retval = anonymous_struct_prefix (die, cu);
22134 if (retval)
22135 return retval;
22136
0114d602
DJ
22137 /* We have to be careful in the presence of DW_AT_specification.
22138 For example, with GCC 3.4, given the code
22139
22140 namespace N {
22141 void foo() {
22142 // Definition of N::foo.
22143 }
22144 }
22145
22146 then we'll have a tree of DIEs like this:
22147
22148 1: DW_TAG_compile_unit
22149 2: DW_TAG_namespace // N
22150 3: DW_TAG_subprogram // declaration of N::foo
22151 4: DW_TAG_subprogram // definition of N::foo
22152 DW_AT_specification // refers to die #3
22153
22154 Thus, when processing die #4, we have to pretend that we're in
22155 the context of its DW_AT_specification, namely the contex of die
22156 #3. */
22157 spec_cu = cu;
22158 spec_die = die_specification (die, &spec_cu);
22159 if (spec_die == NULL)
22160 parent = die->parent;
22161 else
63d06c5c 22162 {
0114d602
DJ
22163 parent = spec_die->parent;
22164 cu = spec_cu;
63d06c5c 22165 }
0114d602
DJ
22166
22167 if (parent == NULL)
22168 return "";
98bfdba5
PA
22169 else if (parent->building_fullname)
22170 {
22171 const char *name;
22172 const char *parent_name;
22173
22174 /* It has been seen on RealView 2.2 built binaries,
22175 DW_TAG_template_type_param types actually _defined_ as
22176 children of the parent class:
22177
22178 enum E {};
22179 template class <class Enum> Class{};
22180 Class<enum E> class_e;
22181
22182 1: DW_TAG_class_type (Class)
22183 2: DW_TAG_enumeration_type (E)
22184 3: DW_TAG_enumerator (enum1:0)
22185 3: DW_TAG_enumerator (enum2:1)
22186 ...
22187 2: DW_TAG_template_type_param
22188 DW_AT_type DW_FORM_ref_udata (E)
22189
22190 Besides being broken debug info, it can put GDB into an
22191 infinite loop. Consider:
22192
22193 When we're building the full name for Class<E>, we'll start
22194 at Class, and go look over its template type parameters,
22195 finding E. We'll then try to build the full name of E, and
22196 reach here. We're now trying to build the full name of E,
22197 and look over the parent DIE for containing scope. In the
22198 broken case, if we followed the parent DIE of E, we'd again
22199 find Class, and once again go look at its template type
22200 arguments, etc., etc. Simply don't consider such parent die
22201 as source-level parent of this die (it can't be, the language
22202 doesn't allow it), and break the loop here. */
22203 name = dwarf2_name (die, cu);
22204 parent_name = dwarf2_name (parent, cu);
b98664d3 22205 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22206 name ? name : "<unknown>",
22207 parent_name ? parent_name : "<unknown>");
22208 return "";
22209 }
63d06c5c 22210 else
0114d602
DJ
22211 switch (parent->tag)
22212 {
63d06c5c 22213 case DW_TAG_namespace:
0114d602 22214 parent_type = read_type_die (parent, cu);
acebe513
UW
22215 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22216 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22217 Work around this problem here. */
22218 if (cu->language == language_cplus
e86ca25f 22219 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22220 return "";
0114d602 22221 /* We give a name to even anonymous namespaces. */
e86ca25f 22222 return TYPE_NAME (parent_type);
63d06c5c 22223 case DW_TAG_class_type:
680b30c7 22224 case DW_TAG_interface_type:
63d06c5c 22225 case DW_TAG_structure_type:
0114d602 22226 case DW_TAG_union_type:
f55ee35c 22227 case DW_TAG_module:
0114d602 22228 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22229 if (TYPE_NAME (parent_type) != NULL)
22230 return TYPE_NAME (parent_type);
0114d602
DJ
22231 else
22232 /* An anonymous structure is only allowed non-static data
22233 members; no typedefs, no member functions, et cetera.
22234 So it does not need a prefix. */
22235 return "";
abc72ce4 22236 case DW_TAG_compile_unit:
95554aad 22237 case DW_TAG_partial_unit:
abc72ce4
DE
22238 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22239 if (cu->language == language_cplus
fd5866f6 22240 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22241 && die->child != NULL
22242 && (die->tag == DW_TAG_class_type
22243 || die->tag == DW_TAG_structure_type
22244 || die->tag == DW_TAG_union_type))
22245 {
43816ebc 22246 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22247 if (name != NULL)
22248 return name;
22249 }
22250 return "";
0a4b0913
AB
22251 case DW_TAG_subprogram:
22252 /* Nested subroutines in Fortran get a prefix with the name
22253 of the parent's subroutine. */
22254 if (cu->language == language_fortran)
22255 {
22256 if ((die->tag == DW_TAG_subprogram)
22257 && (dwarf2_name (parent, cu) != NULL))
22258 return dwarf2_name (parent, cu);
22259 }
22260 return determine_prefix (parent, cu);
3d567982
TT
22261 case DW_TAG_enumeration_type:
22262 parent_type = read_type_die (parent, cu);
22263 if (TYPE_DECLARED_CLASS (parent_type))
22264 {
e86ca25f
TT
22265 if (TYPE_NAME (parent_type) != NULL)
22266 return TYPE_NAME (parent_type);
3d567982
TT
22267 return "";
22268 }
22269 /* Fall through. */
63d06c5c 22270 default:
8176b9b8 22271 return determine_prefix (parent, cu);
63d06c5c 22272 }
63d06c5c
DC
22273}
22274
3e43a32a
MS
22275/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22276 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22277 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22278 an obconcat, otherwise allocate storage for the result. The CU argument is
22279 used to determine the language and hence, the appropriate separator. */
987504bb 22280
f55ee35c 22281#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22282
22283static char *
f55ee35c
JK
22284typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22285 int physname, struct dwarf2_cu *cu)
63d06c5c 22286{
f55ee35c 22287 const char *lead = "";
5c315b68 22288 const char *sep;
63d06c5c 22289
3e43a32a
MS
22290 if (suffix == NULL || suffix[0] == '\0'
22291 || prefix == NULL || prefix[0] == '\0')
987504bb 22292 sep = "";
45280282
IB
22293 else if (cu->language == language_d)
22294 {
22295 /* For D, the 'main' function could be defined in any module, but it
22296 should never be prefixed. */
22297 if (strcmp (suffix, "D main") == 0)
22298 {
22299 prefix = "";
22300 sep = "";
22301 }
22302 else
22303 sep = ".";
22304 }
f55ee35c
JK
22305 else if (cu->language == language_fortran && physname)
22306 {
22307 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22308 DW_AT_MIPS_linkage_name is preferred and used instead. */
22309
22310 lead = "__";
22311 sep = "_MOD_";
22312 }
987504bb
JJ
22313 else
22314 sep = "::";
63d06c5c 22315
6dd47d34
DE
22316 if (prefix == NULL)
22317 prefix = "";
22318 if (suffix == NULL)
22319 suffix = "";
22320
987504bb
JJ
22321 if (obs == NULL)
22322 {
3e43a32a 22323 char *retval
224c3ddb
SM
22324 = ((char *)
22325 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22326
f55ee35c
JK
22327 strcpy (retval, lead);
22328 strcat (retval, prefix);
6dd47d34
DE
22329 strcat (retval, sep);
22330 strcat (retval, suffix);
63d06c5c
DC
22331 return retval;
22332 }
987504bb
JJ
22333 else
22334 {
22335 /* We have an obstack. */
f55ee35c 22336 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22337 }
63d06c5c
DC
22338}
22339
c906108c
SS
22340/* Return sibling of die, NULL if no sibling. */
22341
f9aca02d 22342static struct die_info *
fba45db2 22343sibling_die (struct die_info *die)
c906108c 22344{
639d11d3 22345 return die->sibling;
c906108c
SS
22346}
22347
71c25dea
TT
22348/* Get name of a die, return NULL if not found. */
22349
15d034d0
TT
22350static const char *
22351dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22352 struct obstack *obstack)
22353{
22354 if (name && cu->language == language_cplus)
22355 {
2f408ecb 22356 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22357
2f408ecb 22358 if (!canon_name.empty ())
71c25dea 22359 {
2f408ecb 22360 if (canon_name != name)
efba19b0 22361 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22362 }
22363 }
22364
22365 return name;
c906108c
SS
22366}
22367
96553a0c
DE
22368/* Get name of a die, return NULL if not found.
22369 Anonymous namespaces are converted to their magic string. */
9219021c 22370
15d034d0 22371static const char *
e142c38c 22372dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22373{
22374 struct attribute *attr;
518817b3 22375 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22376
e142c38c 22377 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22378 if ((!attr || !DW_STRING (attr))
96553a0c 22379 && die->tag != DW_TAG_namespace
53832f31
TT
22380 && die->tag != DW_TAG_class_type
22381 && die->tag != DW_TAG_interface_type
22382 && die->tag != DW_TAG_structure_type
22383 && die->tag != DW_TAG_union_type)
71c25dea
TT
22384 return NULL;
22385
22386 switch (die->tag)
22387 {
22388 case DW_TAG_compile_unit:
95554aad 22389 case DW_TAG_partial_unit:
71c25dea
TT
22390 /* Compilation units have a DW_AT_name that is a filename, not
22391 a source language identifier. */
22392 case DW_TAG_enumeration_type:
22393 case DW_TAG_enumerator:
22394 /* These tags always have simple identifiers already; no need
22395 to canonicalize them. */
22396 return DW_STRING (attr);
907af001 22397
96553a0c
DE
22398 case DW_TAG_namespace:
22399 if (attr != NULL && DW_STRING (attr) != NULL)
22400 return DW_STRING (attr);
22401 return CP_ANONYMOUS_NAMESPACE_STR;
22402
907af001
UW
22403 case DW_TAG_class_type:
22404 case DW_TAG_interface_type:
22405 case DW_TAG_structure_type:
22406 case DW_TAG_union_type:
22407 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22408 structures or unions. These were of the form "._%d" in GCC 4.1,
22409 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22410 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22411 if (attr && DW_STRING (attr)
61012eef
GB
22412 && (startswith (DW_STRING (attr), "._")
22413 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22414 return NULL;
53832f31
TT
22415
22416 /* GCC might emit a nameless typedef that has a linkage name. See
22417 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22418 if (!attr || DW_STRING (attr) == NULL)
22419 {
73b9be8b 22420 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22421 if (attr == NULL || DW_STRING (attr) == NULL)
22422 return NULL;
22423
df5c6c50
JK
22424 /* Avoid demangling DW_STRING (attr) the second time on a second
22425 call for the same DIE. */
22426 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22427 {
43816ebc
TT
22428 gdb::unique_xmalloc_ptr<char> demangled
22429 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22430
e6a959d6 22431 const char *base;
96408a79 22432
53832f31 22433 /* FIXME: we already did this for the partial symbol... */
34a68019 22434 DW_STRING (attr)
021887d8 22435 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 22436 demangled.get ());
53832f31 22437 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
22438
22439 /* Strip any leading namespaces/classes, keep only the base name.
22440 DW_AT_name for named DIEs does not contain the prefixes. */
22441 base = strrchr (DW_STRING (attr), ':');
22442 if (base && base > DW_STRING (attr) && base[-1] == ':')
22443 return &base[1];
22444 else
22445 return DW_STRING (attr);
53832f31
TT
22446 }
22447 }
907af001
UW
22448 break;
22449
71c25dea 22450 default:
907af001
UW
22451 break;
22452 }
22453
22454 if (!DW_STRING_IS_CANONICAL (attr))
22455 {
22456 DW_STRING (attr)
22457 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22458 &objfile->per_bfd->storage_obstack);
907af001 22459 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22460 }
907af001 22461 return DW_STRING (attr);
9219021c
DC
22462}
22463
22464/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22465 is none. *EXT_CU is the CU containing DIE on input, and the CU
22466 containing the return value on output. */
9219021c
DC
22467
22468static struct die_info *
f2f0e013 22469dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22470{
22471 struct attribute *attr;
9219021c 22472
f2f0e013 22473 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22474 if (attr == NULL)
22475 return NULL;
22476
f2f0e013 22477 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22478}
22479
fa9c3fa0
TT
22480/* A convenience function that returns an "unknown" DWARF name,
22481 including the value of V. STR is the name of the entity being
22482 printed, e.g., "TAG". */
22483
22484static const char *
22485dwarf_unknown (const char *str, unsigned v)
22486{
22487 char *cell = get_print_cell ();
22488 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22489 return cell;
22490}
22491
c906108c
SS
22492/* Convert a DIE tag into its string name. */
22493
f39c6ffd 22494static const char *
aa1ee363 22495dwarf_tag_name (unsigned tag)
c906108c 22496{
f39c6ffd
TT
22497 const char *name = get_DW_TAG_name (tag);
22498
22499 if (name == NULL)
fa9c3fa0 22500 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22501
22502 return name;
c906108c
SS
22503}
22504
22505/* Convert a DWARF attribute code into its string name. */
22506
f39c6ffd 22507static const char *
aa1ee363 22508dwarf_attr_name (unsigned attr)
c906108c 22509{
f39c6ffd
TT
22510 const char *name;
22511
c764a876 22512#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22513 if (attr == DW_AT_MIPS_fde)
22514 return "DW_AT_MIPS_fde";
22515#else
22516 if (attr == DW_AT_HP_block_index)
22517 return "DW_AT_HP_block_index";
c764a876 22518#endif
f39c6ffd
TT
22519
22520 name = get_DW_AT_name (attr);
22521
22522 if (name == NULL)
fa9c3fa0 22523 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22524
22525 return name;
c906108c
SS
22526}
22527
a084a2a6
AT
22528/* Convert a unit type to corresponding DW_UT name. */
22529
22530static const char *
22531dwarf_unit_type_name (int unit_type) {
22532 switch (unit_type)
22533 {
22534 case 0x01:
22535 return "DW_UT_compile (0x01)";
22536 case 0x02:
22537 return "DW_UT_type (0x02)";
22538 case 0x03:
22539 return "DW_UT_partial (0x03)";
22540 case 0x04:
22541 return "DW_UT_skeleton (0x04)";
22542 case 0x05:
22543 return "DW_UT_split_compile (0x05)";
22544 case 0x06:
22545 return "DW_UT_split_type (0x06)";
22546 case 0x80:
22547 return "DW_UT_lo_user (0x80)";
22548 case 0xff:
22549 return "DW_UT_hi_user (0xff)";
22550 default:
22551 return nullptr;
22552 }
22553}
22554
c906108c
SS
22555/* Convert a DWARF value form code into its string name. */
22556
f39c6ffd 22557static const char *
aa1ee363 22558dwarf_form_name (unsigned form)
c906108c 22559{
f39c6ffd
TT
22560 const char *name = get_DW_FORM_name (form);
22561
22562 if (name == NULL)
fa9c3fa0 22563 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22564
22565 return name;
c906108c
SS
22566}
22567
a121b7c1 22568static const char *
fba45db2 22569dwarf_bool_name (unsigned mybool)
c906108c
SS
22570{
22571 if (mybool)
22572 return "TRUE";
22573 else
22574 return "FALSE";
22575}
22576
22577/* Convert a DWARF type code into its string name. */
22578
f39c6ffd 22579static const char *
aa1ee363 22580dwarf_type_encoding_name (unsigned enc)
c906108c 22581{
f39c6ffd 22582 const char *name = get_DW_ATE_name (enc);
c906108c 22583
f39c6ffd 22584 if (name == NULL)
fa9c3fa0 22585 return dwarf_unknown ("ATE", enc);
c906108c 22586
f39c6ffd 22587 return name;
c906108c 22588}
c906108c 22589
f9aca02d 22590static void
d97bc12b 22591dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22592{
22593 unsigned int i;
22594
d97bc12b 22595 print_spaces (indent, f);
9d8780f0 22596 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22597 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22598 sect_offset_str (die->sect_off));
d97bc12b
DE
22599
22600 if (die->parent != NULL)
22601 {
22602 print_spaces (indent, f);
9d8780f0
SM
22603 fprintf_unfiltered (f, " parent at offset: %s\n",
22604 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22605 }
22606
22607 print_spaces (indent, f);
22608 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22609 dwarf_bool_name (die->child != NULL));
c906108c 22610
d97bc12b
DE
22611 print_spaces (indent, f);
22612 fprintf_unfiltered (f, " attributes:\n");
22613
c906108c
SS
22614 for (i = 0; i < die->num_attrs; ++i)
22615 {
d97bc12b
DE
22616 print_spaces (indent, f);
22617 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22618 dwarf_attr_name (die->attrs[i].name),
22619 dwarf_form_name (die->attrs[i].form));
d97bc12b 22620
c906108c
SS
22621 switch (die->attrs[i].form)
22622 {
c906108c 22623 case DW_FORM_addr:
336d760d 22624 case DW_FORM_addrx:
3019eac3 22625 case DW_FORM_GNU_addr_index:
d97bc12b 22626 fprintf_unfiltered (f, "address: ");
5af949e3 22627 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22628 break;
22629 case DW_FORM_block2:
22630 case DW_FORM_block4:
22631 case DW_FORM_block:
22632 case DW_FORM_block1:
56eb65bd
SP
22633 fprintf_unfiltered (f, "block: size %s",
22634 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22635 break;
2dc7f7b3 22636 case DW_FORM_exprloc:
56eb65bd
SP
22637 fprintf_unfiltered (f, "expression: size %s",
22638 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22639 break;
0224619f
JK
22640 case DW_FORM_data16:
22641 fprintf_unfiltered (f, "constant of 16 bytes");
22642 break;
4568ecf9
DE
22643 case DW_FORM_ref_addr:
22644 fprintf_unfiltered (f, "ref address: ");
22645 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22646 break;
36586728
TT
22647 case DW_FORM_GNU_ref_alt:
22648 fprintf_unfiltered (f, "alt ref address: ");
22649 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22650 break;
10b3939b
DJ
22651 case DW_FORM_ref1:
22652 case DW_FORM_ref2:
22653 case DW_FORM_ref4:
4568ecf9
DE
22654 case DW_FORM_ref8:
22655 case DW_FORM_ref_udata:
d97bc12b 22656 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22657 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22658 break;
c906108c
SS
22659 case DW_FORM_data1:
22660 case DW_FORM_data2:
22661 case DW_FORM_data4:
ce5d95e1 22662 case DW_FORM_data8:
c906108c
SS
22663 case DW_FORM_udata:
22664 case DW_FORM_sdata:
43bbcdc2
PH
22665 fprintf_unfiltered (f, "constant: %s",
22666 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22667 break;
2dc7f7b3
TT
22668 case DW_FORM_sec_offset:
22669 fprintf_unfiltered (f, "section offset: %s",
22670 pulongest (DW_UNSND (&die->attrs[i])));
22671 break;
55f1336d 22672 case DW_FORM_ref_sig8:
ac9ec31b
DE
22673 fprintf_unfiltered (f, "signature: %s",
22674 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22675 break;
c906108c 22676 case DW_FORM_string:
4bdf3d34 22677 case DW_FORM_strp:
43988095 22678 case DW_FORM_line_strp:
cf532bd1 22679 case DW_FORM_strx:
3019eac3 22680 case DW_FORM_GNU_str_index:
36586728 22681 case DW_FORM_GNU_strp_alt:
8285870a 22682 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22683 DW_STRING (&die->attrs[i])
8285870a
JK
22684 ? DW_STRING (&die->attrs[i]) : "",
22685 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22686 break;
22687 case DW_FORM_flag:
22688 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22689 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22690 else
d97bc12b 22691 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22692 break;
2dc7f7b3
TT
22693 case DW_FORM_flag_present:
22694 fprintf_unfiltered (f, "flag: TRUE");
22695 break;
a8329558 22696 case DW_FORM_indirect:
0963b4bd
MS
22697 /* The reader will have reduced the indirect form to
22698 the "base form" so this form should not occur. */
5f48f8f3 22699 fprintf_unfiltered (f,
3e43a32a 22700 "unexpected attribute form: DW_FORM_indirect");
a8329558 22701 break;
663c44ac
JK
22702 case DW_FORM_implicit_const:
22703 fprintf_unfiltered (f, "constant: %s",
22704 plongest (DW_SND (&die->attrs[i])));
22705 break;
c906108c 22706 default:
d97bc12b 22707 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22708 die->attrs[i].form);
d97bc12b 22709 break;
c906108c 22710 }
d97bc12b 22711 fprintf_unfiltered (f, "\n");
c906108c
SS
22712 }
22713}
22714
f9aca02d 22715static void
d97bc12b 22716dump_die_for_error (struct die_info *die)
c906108c 22717{
d97bc12b
DE
22718 dump_die_shallow (gdb_stderr, 0, die);
22719}
22720
22721static void
22722dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22723{
22724 int indent = level * 4;
22725
22726 gdb_assert (die != NULL);
22727
22728 if (level >= max_level)
22729 return;
22730
22731 dump_die_shallow (f, indent, die);
22732
22733 if (die->child != NULL)
c906108c 22734 {
d97bc12b
DE
22735 print_spaces (indent, f);
22736 fprintf_unfiltered (f, " Children:");
22737 if (level + 1 < max_level)
22738 {
22739 fprintf_unfiltered (f, "\n");
22740 dump_die_1 (f, level + 1, max_level, die->child);
22741 }
22742 else
22743 {
3e43a32a
MS
22744 fprintf_unfiltered (f,
22745 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22746 }
22747 }
22748
22749 if (die->sibling != NULL && level > 0)
22750 {
22751 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22752 }
22753}
22754
d97bc12b
DE
22755/* This is called from the pdie macro in gdbinit.in.
22756 It's not static so gcc will keep a copy callable from gdb. */
22757
22758void
22759dump_die (struct die_info *die, int max_level)
22760{
22761 dump_die_1 (gdb_stdlog, 0, max_level, die);
22762}
22763
f9aca02d 22764static void
51545339 22765store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22766{
51545339 22767 void **slot;
c906108c 22768
9c541725
PA
22769 slot = htab_find_slot_with_hash (cu->die_hash, die,
22770 to_underlying (die->sect_off),
b64f50a1 22771 INSERT);
51545339
DJ
22772
22773 *slot = die;
c906108c
SS
22774}
22775
b64f50a1
JK
22776/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22777 required kind. */
22778
22779static sect_offset
ff39bb5e 22780dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22781{
cd6c91b4 22782 if (attr->form_is_ref ())
9c541725 22783 return (sect_offset) DW_UNSND (attr);
93311388 22784
b98664d3 22785 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22786 dwarf_form_name (attr->form));
9c541725 22787 return {};
c906108c
SS
22788}
22789
43bbcdc2
PH
22790/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22791 * the value held by the attribute is not constant. */
a02abb62 22792
43bbcdc2 22793static LONGEST
ff39bb5e 22794dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22795{
663c44ac 22796 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22797 return DW_SND (attr);
22798 else if (attr->form == DW_FORM_udata
22799 || attr->form == DW_FORM_data1
22800 || attr->form == DW_FORM_data2
22801 || attr->form == DW_FORM_data4
22802 || attr->form == DW_FORM_data8)
22803 return DW_UNSND (attr);
22804 else
22805 {
cd6c91b4 22806 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22807 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22808 dwarf_form_name (attr->form));
22809 return default_value;
22810 }
22811}
22812
348e048f
DE
22813/* Follow reference or signature attribute ATTR of SRC_DIE.
22814 On entry *REF_CU is the CU of SRC_DIE.
22815 On exit *REF_CU is the CU of the result. */
22816
22817static struct die_info *
ff39bb5e 22818follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22819 struct dwarf2_cu **ref_cu)
22820{
22821 struct die_info *die;
22822
cd6c91b4 22823 if (attr->form_is_ref ())
348e048f 22824 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22825 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22826 die = follow_die_sig (src_die, attr, ref_cu);
22827 else
22828 {
22829 dump_die_for_error (src_die);
22830 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22831 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22832 }
22833
22834 return die;
03dd20cc
DJ
22835}
22836
5c631832 22837/* Follow reference OFFSET.
673bfd45
DE
22838 On entry *REF_CU is the CU of the source die referencing OFFSET.
22839 On exit *REF_CU is the CU of the result.
22840 Returns NULL if OFFSET is invalid. */
f504f079 22841
f9aca02d 22842static struct die_info *
9c541725 22843follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22844 struct dwarf2_cu **ref_cu)
c906108c 22845{
10b3939b 22846 struct die_info temp_die;
f2f0e013 22847 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22848 struct dwarf2_per_objfile *dwarf2_per_objfile
22849 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22850
348e048f
DE
22851 gdb_assert (cu->per_cu != NULL);
22852
98bfdba5
PA
22853 target_cu = cu;
22854
3019eac3 22855 if (cu->per_cu->is_debug_types)
348e048f
DE
22856 {
22857 /* .debug_types CUs cannot reference anything outside their CU.
22858 If they need to, they have to reference a signatured type via
55f1336d 22859 DW_FORM_ref_sig8. */
9c541725 22860 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22861 return NULL;
348e048f 22862 }
36586728 22863 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22864 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22865 {
22866 struct dwarf2_per_cu_data *per_cu;
9a619af0 22867
9c541725 22868 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22869 dwarf2_per_objfile);
03dd20cc
DJ
22870
22871 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22872 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22873 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22874
10b3939b
DJ
22875 target_cu = per_cu->cu;
22876 }
98bfdba5
PA
22877 else if (cu->dies == NULL)
22878 {
22879 /* We're loading full DIEs during partial symbol reading. */
22880 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22881 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22882 }
c906108c 22883
f2f0e013 22884 *ref_cu = target_cu;
9c541725 22885 temp_die.sect_off = sect_off;
c24bdb02
KS
22886
22887 if (target_cu != cu)
22888 target_cu->ancestor = cu;
22889
9a3c8263 22890 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22891 &temp_die,
22892 to_underlying (sect_off));
5c631832 22893}
10b3939b 22894
5c631832
JK
22895/* Follow reference attribute ATTR of SRC_DIE.
22896 On entry *REF_CU is the CU of SRC_DIE.
22897 On exit *REF_CU is the CU of the result. */
22898
22899static struct die_info *
ff39bb5e 22900follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22901 struct dwarf2_cu **ref_cu)
22902{
9c541725 22903 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22904 struct dwarf2_cu *cu = *ref_cu;
22905 struct die_info *die;
22906
9c541725 22907 die = follow_die_offset (sect_off,
36586728
TT
22908 (attr->form == DW_FORM_GNU_ref_alt
22909 || cu->per_cu->is_dwz),
22910 ref_cu);
5c631832 22911 if (!die)
9d8780f0
SM
22912 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22913 "at %s [in module %s]"),
22914 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22915 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22916
5c631832
JK
22917 return die;
22918}
22919
9c541725 22920/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22921 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22922 dwarf2_locexpr_baton->data has lifetime of
22923 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22924
22925struct dwarf2_locexpr_baton
9c541725 22926dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22927 struct dwarf2_per_cu_data *per_cu,
22928 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22929 void *baton, bool resolve_abstract_p)
5c631832 22930{
918dd910 22931 struct dwarf2_cu *cu;
5c631832
JK
22932 struct die_info *die;
22933 struct attribute *attr;
22934 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22935 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22936 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22937
918dd910 22938 if (per_cu->cu == NULL)
58f0c718 22939 load_cu (per_cu, false);
918dd910 22940 cu = per_cu->cu;
cc12ce38
DE
22941 if (cu == NULL)
22942 {
22943 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22944 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22945 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22946 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22947 }
918dd910 22948
9c541725 22949 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22950 if (!die)
9d8780f0
SM
22951 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22952 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22953
22954 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22955 if (!attr && resolve_abstract_p
3360b6e7 22956 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22957 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22958 {
22959 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22960 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22961 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22962
3360b6e7
TV
22963 for (const auto &cand_off
22964 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22965 {
3360b6e7
TV
22966 struct dwarf2_cu *cand_cu = cu;
22967 struct die_info *cand
22968 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22969 if (!cand
22970 || !cand->parent
e4a62c65
TV
22971 || cand->parent->tag != DW_TAG_subprogram)
22972 continue;
22973
22974 CORE_ADDR pc_low, pc_high;
22975 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22976 if (pc_low == ((CORE_ADDR) -1))
22977 continue;
22978 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22979 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22980 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22981 continue;
22982
22983 die = cand;
22984 attr = dwarf2_attr (die, DW_AT_location, cu);
22985 break;
22986 }
22987 }
22988
5c631832
JK
22989 if (!attr)
22990 {
e103e986
JK
22991 /* DWARF: "If there is no such attribute, then there is no effect.".
22992 DATA is ignored if SIZE is 0. */
5c631832 22993
e103e986 22994 retval.data = NULL;
5c631832
JK
22995 retval.size = 0;
22996 }
cd6c91b4 22997 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22998 {
22999 struct dwarf2_loclist_baton loclist_baton;
23000 CORE_ADDR pc = (*get_frame_pc) (baton);
23001 size_t size;
23002
23003 fill_in_loclist_baton (cu, &loclist_baton, attr);
23004
23005 retval.data = dwarf2_find_location_expression (&loclist_baton,
23006 &size, pc);
23007 retval.size = size;
23008 }
5c631832
JK
23009 else
23010 {
4fc6c0d5 23011 if (!attr->form_is_block ())
9d8780f0 23012 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23013 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23014 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23015
23016 retval.data = DW_BLOCK (attr)->data;
23017 retval.size = DW_BLOCK (attr)->size;
23018 }
23019 retval.per_cu = cu->per_cu;
918dd910 23020
ed2dc618 23021 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23022
5c631832 23023 return retval;
348e048f
DE
23024}
23025
8b9737bf
TT
23026/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23027 offset. */
23028
23029struct dwarf2_locexpr_baton
23030dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23031 struct dwarf2_per_cu_data *per_cu,
23032 CORE_ADDR (*get_frame_pc) (void *baton),
23033 void *baton)
23034{
9c541725 23035 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23036
9c541725 23037 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23038}
23039
b6807d98
TT
23040/* Write a constant of a given type as target-ordered bytes into
23041 OBSTACK. */
23042
23043static const gdb_byte *
23044write_constant_as_bytes (struct obstack *obstack,
23045 enum bfd_endian byte_order,
23046 struct type *type,
23047 ULONGEST value,
23048 LONGEST *len)
23049{
23050 gdb_byte *result;
23051
23052 *len = TYPE_LENGTH (type);
224c3ddb 23053 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23054 store_unsigned_integer (result, *len, byte_order, value);
23055
23056 return result;
23057}
23058
23059/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23060 pointer to the constant bytes and set LEN to the length of the
23061 data. If memory is needed, allocate it on OBSTACK. If the DIE
23062 does not have a DW_AT_const_value, return NULL. */
23063
23064const gdb_byte *
9c541725 23065dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23066 struct dwarf2_per_cu_data *per_cu,
23067 struct obstack *obstack,
23068 LONGEST *len)
23069{
23070 struct dwarf2_cu *cu;
23071 struct die_info *die;
23072 struct attribute *attr;
23073 const gdb_byte *result = NULL;
23074 struct type *type;
23075 LONGEST value;
23076 enum bfd_endian byte_order;
e3b94546 23077 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23078
b6807d98 23079 if (per_cu->cu == NULL)
58f0c718 23080 load_cu (per_cu, false);
b6807d98 23081 cu = per_cu->cu;
cc12ce38
DE
23082 if (cu == NULL)
23083 {
23084 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23085 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23086 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23087 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23088 }
b6807d98 23089
9c541725 23090 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23091 if (!die)
9d8780f0
SM
23092 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23093 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23094
23095 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23096 if (attr == NULL)
23097 return NULL;
23098
e3b94546 23099 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23100 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23101
23102 switch (attr->form)
23103 {
23104 case DW_FORM_addr:
336d760d 23105 case DW_FORM_addrx:
b6807d98
TT
23106 case DW_FORM_GNU_addr_index:
23107 {
23108 gdb_byte *tem;
23109
23110 *len = cu->header.addr_size;
224c3ddb 23111 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23112 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23113 result = tem;
23114 }
23115 break;
23116 case DW_FORM_string:
23117 case DW_FORM_strp:
cf532bd1 23118 case DW_FORM_strx:
b6807d98
TT
23119 case DW_FORM_GNU_str_index:
23120 case DW_FORM_GNU_strp_alt:
23121 /* DW_STRING is already allocated on the objfile obstack, point
23122 directly to it. */
23123 result = (const gdb_byte *) DW_STRING (attr);
23124 *len = strlen (DW_STRING (attr));
23125 break;
23126 case DW_FORM_block1:
23127 case DW_FORM_block2:
23128 case DW_FORM_block4:
23129 case DW_FORM_block:
23130 case DW_FORM_exprloc:
0224619f 23131 case DW_FORM_data16:
b6807d98
TT
23132 result = DW_BLOCK (attr)->data;
23133 *len = DW_BLOCK (attr)->size;
23134 break;
23135
23136 /* The DW_AT_const_value attributes are supposed to carry the
23137 symbol's value "represented as it would be on the target
23138 architecture." By the time we get here, it's already been
23139 converted to host endianness, so we just need to sign- or
23140 zero-extend it as appropriate. */
23141 case DW_FORM_data1:
23142 type = die_type (die, cu);
23143 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23144 if (result == NULL)
23145 result = write_constant_as_bytes (obstack, byte_order,
23146 type, value, len);
23147 break;
23148 case DW_FORM_data2:
23149 type = die_type (die, cu);
23150 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23151 if (result == NULL)
23152 result = write_constant_as_bytes (obstack, byte_order,
23153 type, value, len);
23154 break;
23155 case DW_FORM_data4:
23156 type = die_type (die, cu);
23157 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23158 if (result == NULL)
23159 result = write_constant_as_bytes (obstack, byte_order,
23160 type, value, len);
23161 break;
23162 case DW_FORM_data8:
23163 type = die_type (die, cu);
23164 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23165 if (result == NULL)
23166 result = write_constant_as_bytes (obstack, byte_order,
23167 type, value, len);
23168 break;
23169
23170 case DW_FORM_sdata:
663c44ac 23171 case DW_FORM_implicit_const:
b6807d98
TT
23172 type = die_type (die, cu);
23173 result = write_constant_as_bytes (obstack, byte_order,
23174 type, DW_SND (attr), len);
23175 break;
23176
23177 case DW_FORM_udata:
23178 type = die_type (die, cu);
23179 result = write_constant_as_bytes (obstack, byte_order,
23180 type, DW_UNSND (attr), len);
23181 break;
23182
23183 default:
b98664d3 23184 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23185 dwarf_form_name (attr->form));
23186 break;
23187 }
23188
23189 return result;
23190}
23191
7942e96e
AA
23192/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23193 valid type for this die is found. */
23194
23195struct type *
9c541725 23196dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23197 struct dwarf2_per_cu_data *per_cu)
23198{
23199 struct dwarf2_cu *cu;
23200 struct die_info *die;
23201
7942e96e 23202 if (per_cu->cu == NULL)
58f0c718 23203 load_cu (per_cu, false);
7942e96e
AA
23204 cu = per_cu->cu;
23205 if (!cu)
23206 return NULL;
23207
9c541725 23208 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23209 if (!die)
23210 return NULL;
23211
23212 return die_type (die, cu);
23213}
23214
8a9b8146
TT
23215/* Return the type of the DIE at DIE_OFFSET in the CU named by
23216 PER_CU. */
23217
23218struct type *
b64f50a1 23219dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23220 struct dwarf2_per_cu_data *per_cu)
23221{
9c541725 23222 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23223 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23224}
23225
ac9ec31b 23226/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23227 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23228 On exit *REF_CU is the CU of the result.
23229 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23230
23231static struct die_info *
ac9ec31b
DE
23232follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23233 struct dwarf2_cu **ref_cu)
348e048f 23234{
348e048f 23235 struct die_info temp_die;
c24bdb02 23236 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23237 struct die_info *die;
23238
ac9ec31b
DE
23239 /* While it might be nice to assert sig_type->type == NULL here,
23240 we can get here for DW_AT_imported_declaration where we need
23241 the DIE not the type. */
348e048f
DE
23242
23243 /* If necessary, add it to the queue and load its DIEs. */
23244
95554aad 23245 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23246 read_signatured_type (sig_type);
348e048f 23247
348e048f 23248 sig_cu = sig_type->per_cu.cu;
69d751e3 23249 gdb_assert (sig_cu != NULL);
9c541725
PA
23250 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23251 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23252 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23253 to_underlying (temp_die.sect_off));
348e048f
DE
23254 if (die)
23255 {
ed2dc618 23256 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23257 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23258
796a7ff8
DE
23259 /* For .gdb_index version 7 keep track of included TUs.
23260 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23261 if (dwarf2_per_objfile->index_table != NULL
23262 && dwarf2_per_objfile->index_table->version <= 7)
23263 {
ae640021 23264 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23265 }
23266
348e048f 23267 *ref_cu = sig_cu;
c24bdb02
KS
23268 if (sig_cu != cu)
23269 sig_cu->ancestor = cu;
23270
348e048f
DE
23271 return die;
23272 }
23273
ac9ec31b
DE
23274 return NULL;
23275}
23276
23277/* Follow signatured type referenced by ATTR in SRC_DIE.
23278 On entry *REF_CU is the CU of SRC_DIE.
23279 On exit *REF_CU is the CU of the result.
23280 The result is the DIE of the type.
23281 If the referenced type cannot be found an error is thrown. */
23282
23283static struct die_info *
ff39bb5e 23284follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23285 struct dwarf2_cu **ref_cu)
23286{
23287 ULONGEST signature = DW_SIGNATURE (attr);
23288 struct signatured_type *sig_type;
23289 struct die_info *die;
23290
23291 gdb_assert (attr->form == DW_FORM_ref_sig8);
23292
a2ce51a0 23293 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23294 /* sig_type will be NULL if the signatured type is missing from
23295 the debug info. */
23296 if (sig_type == NULL)
23297 {
23298 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23299 " from DIE at %s [in module %s]"),
23300 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23301 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23302 }
23303
23304 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23305 if (die == NULL)
23306 {
23307 dump_die_for_error (src_die);
23308 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23309 " from DIE at %s [in module %s]"),
23310 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23311 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23312 }
23313
23314 return die;
23315}
23316
23317/* Get the type specified by SIGNATURE referenced in DIE/CU,
23318 reading in and processing the type unit if necessary. */
23319
23320static struct type *
23321get_signatured_type (struct die_info *die, ULONGEST signature,
23322 struct dwarf2_cu *cu)
23323{
518817b3
SM
23324 struct dwarf2_per_objfile *dwarf2_per_objfile
23325 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23326 struct signatured_type *sig_type;
23327 struct dwarf2_cu *type_cu;
23328 struct die_info *type_die;
23329 struct type *type;
23330
a2ce51a0 23331 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23332 /* sig_type will be NULL if the signatured type is missing from
23333 the debug info. */
23334 if (sig_type == NULL)
23335 {
b98664d3 23336 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23337 " from DIE at %s [in module %s]"),
23338 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23339 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23340 return build_error_marker_type (cu, die);
23341 }
23342
23343 /* If we already know the type we're done. */
23344 if (sig_type->type != NULL)
23345 return sig_type->type;
23346
23347 type_cu = cu;
23348 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23349 if (type_die != NULL)
23350 {
23351 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23352 is created. This is important, for example, because for c++ classes
23353 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23354 type = read_type_die (type_die, type_cu);
23355 if (type == NULL)
23356 {
b98664d3 23357 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23358 " referenced from DIE at %s [in module %s]"),
23359 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23360 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23361 type = build_error_marker_type (cu, die);
23362 }
23363 }
23364 else
23365 {
b98664d3 23366 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23367 " from DIE at %s [in module %s]"),
23368 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23369 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23370 type = build_error_marker_type (cu, die);
23371 }
23372 sig_type->type = type;
23373
23374 return type;
23375}
23376
23377/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23378 reading in and processing the type unit if necessary. */
23379
23380static struct type *
ff39bb5e 23381get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23382 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23383{
23384 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23385 if (attr->form_is_ref ())
ac9ec31b
DE
23386 {
23387 struct dwarf2_cu *type_cu = cu;
23388 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23389
23390 return read_type_die (type_die, type_cu);
23391 }
23392 else if (attr->form == DW_FORM_ref_sig8)
23393 {
23394 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23395 }
23396 else
23397 {
518817b3
SM
23398 struct dwarf2_per_objfile *dwarf2_per_objfile
23399 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23400
b98664d3 23401 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23402 " at %s [in module %s]"),
23403 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23404 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23405 return build_error_marker_type (cu, die);
23406 }
348e048f
DE
23407}
23408
e5fe5e75 23409/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23410
23411static void
e5fe5e75 23412load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23413{
52dc124a 23414 struct signatured_type *sig_type;
348e048f 23415
f4dc4d17
DE
23416 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23417 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23418
6721b2ec
DE
23419 /* We have the per_cu, but we need the signatured_type.
23420 Fortunately this is an easy translation. */
23421 gdb_assert (per_cu->is_debug_types);
23422 sig_type = (struct signatured_type *) per_cu;
348e048f 23423
6721b2ec 23424 gdb_assert (per_cu->cu == NULL);
348e048f 23425
52dc124a 23426 read_signatured_type (sig_type);
348e048f 23427
6721b2ec 23428 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23429}
23430
3019eac3
DE
23431/* Read in a signatured type and build its CU and DIEs.
23432 If the type is a stub for the real type in a DWO file,
23433 read in the real type from the DWO file as well. */
dee91e82
DE
23434
23435static void
23436read_signatured_type (struct signatured_type *sig_type)
23437{
23438 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23439
3019eac3 23440 gdb_assert (per_cu->is_debug_types);
dee91e82 23441 gdb_assert (per_cu->cu == NULL);
348e048f 23442
c0ab21c2
TT
23443 cutu_reader reader (per_cu, NULL, 0, 1, false);
23444
23445 if (!reader.dummy_p)
23446 {
23447 struct dwarf2_cu *cu = reader.cu;
23448 const gdb_byte *info_ptr = reader.info_ptr;
23449
23450 gdb_assert (cu->die_hash == NULL);
23451 cu->die_hash =
23452 htab_create_alloc_ex (cu->header.length / 12,
23453 die_hash,
23454 die_eq,
23455 NULL,
23456 &cu->comp_unit_obstack,
23457 hashtab_obstack_allocate,
23458 dummy_obstack_deallocate);
23459
3e225074 23460 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23461 reader.comp_unit_die->child
23462 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23463 reader.comp_unit_die);
23464 cu->dies = reader.comp_unit_die;
23465 /* comp_unit_die is not stored in die_hash, no need. */
23466
23467 /* We try not to read any attributes in this function, because
23468 not all CUs needed for references have been loaded yet, and
23469 symbol table processing isn't initialized. But we have to
23470 set the CU language, or we won't be able to build types
23471 correctly. Similarly, if we do not read the producer, we can
23472 not apply producer-specific interpretation. */
23473 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23474 }
23475
7ee85ab1 23476 sig_type->per_cu.tu_read = 1;
c906108c
SS
23477}
23478
c906108c
SS
23479/* Decode simple location descriptions.
23480 Given a pointer to a dwarf block that defines a location, compute
23481 the location and return the value.
23482
4cecd739
DJ
23483 NOTE drow/2003-11-18: This function is called in two situations
23484 now: for the address of static or global variables (partial symbols
23485 only) and for offsets into structures which are expected to be
23486 (more or less) constant. The partial symbol case should go away,
23487 and only the constant case should remain. That will let this
23488 function complain more accurately. A few special modes are allowed
23489 without complaint for global variables (for instance, global
23490 register values and thread-local values).
c906108c
SS
23491
23492 A location description containing no operations indicates that the
4cecd739 23493 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23494 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23495 callers will only want a very basic result and this can become a
21ae7a4d
JK
23496 complaint.
23497
23498 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23499
23500static CORE_ADDR
e7c27a73 23501decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23502{
518817b3 23503 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23504 size_t i;
23505 size_t size = blk->size;
d521ce57 23506 const gdb_byte *data = blk->data;
21ae7a4d
JK
23507 CORE_ADDR stack[64];
23508 int stacki;
23509 unsigned int bytes_read, unsnd;
23510 gdb_byte op;
c906108c 23511
21ae7a4d
JK
23512 i = 0;
23513 stacki = 0;
23514 stack[stacki] = 0;
23515 stack[++stacki] = 0;
23516
23517 while (i < size)
23518 {
23519 op = data[i++];
23520 switch (op)
23521 {
23522 case DW_OP_lit0:
23523 case DW_OP_lit1:
23524 case DW_OP_lit2:
23525 case DW_OP_lit3:
23526 case DW_OP_lit4:
23527 case DW_OP_lit5:
23528 case DW_OP_lit6:
23529 case DW_OP_lit7:
23530 case DW_OP_lit8:
23531 case DW_OP_lit9:
23532 case DW_OP_lit10:
23533 case DW_OP_lit11:
23534 case DW_OP_lit12:
23535 case DW_OP_lit13:
23536 case DW_OP_lit14:
23537 case DW_OP_lit15:
23538 case DW_OP_lit16:
23539 case DW_OP_lit17:
23540 case DW_OP_lit18:
23541 case DW_OP_lit19:
23542 case DW_OP_lit20:
23543 case DW_OP_lit21:
23544 case DW_OP_lit22:
23545 case DW_OP_lit23:
23546 case DW_OP_lit24:
23547 case DW_OP_lit25:
23548 case DW_OP_lit26:
23549 case DW_OP_lit27:
23550 case DW_OP_lit28:
23551 case DW_OP_lit29:
23552 case DW_OP_lit30:
23553 case DW_OP_lit31:
23554 stack[++stacki] = op - DW_OP_lit0;
23555 break;
f1bea926 23556
21ae7a4d
JK
23557 case DW_OP_reg0:
23558 case DW_OP_reg1:
23559 case DW_OP_reg2:
23560 case DW_OP_reg3:
23561 case DW_OP_reg4:
23562 case DW_OP_reg5:
23563 case DW_OP_reg6:
23564 case DW_OP_reg7:
23565 case DW_OP_reg8:
23566 case DW_OP_reg9:
23567 case DW_OP_reg10:
23568 case DW_OP_reg11:
23569 case DW_OP_reg12:
23570 case DW_OP_reg13:
23571 case DW_OP_reg14:
23572 case DW_OP_reg15:
23573 case DW_OP_reg16:
23574 case DW_OP_reg17:
23575 case DW_OP_reg18:
23576 case DW_OP_reg19:
23577 case DW_OP_reg20:
23578 case DW_OP_reg21:
23579 case DW_OP_reg22:
23580 case DW_OP_reg23:
23581 case DW_OP_reg24:
23582 case DW_OP_reg25:
23583 case DW_OP_reg26:
23584 case DW_OP_reg27:
23585 case DW_OP_reg28:
23586 case DW_OP_reg29:
23587 case DW_OP_reg30:
23588 case DW_OP_reg31:
23589 stack[++stacki] = op - DW_OP_reg0;
23590 if (i < size)
23591 dwarf2_complex_location_expr_complaint ();
23592 break;
c906108c 23593
21ae7a4d
JK
23594 case DW_OP_regx:
23595 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23596 i += bytes_read;
23597 stack[++stacki] = unsnd;
23598 if (i < size)
23599 dwarf2_complex_location_expr_complaint ();
23600 break;
c906108c 23601
21ae7a4d
JK
23602 case DW_OP_addr:
23603 stack[++stacki] = read_address (objfile->obfd, &data[i],
23604 cu, &bytes_read);
23605 i += bytes_read;
23606 break;
d53d4ac5 23607
21ae7a4d
JK
23608 case DW_OP_const1u:
23609 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23610 i += 1;
23611 break;
23612
23613 case DW_OP_const1s:
23614 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23615 i += 1;
23616 break;
23617
23618 case DW_OP_const2u:
23619 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23620 i += 2;
23621 break;
23622
23623 case DW_OP_const2s:
23624 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23625 i += 2;
23626 break;
d53d4ac5 23627
21ae7a4d
JK
23628 case DW_OP_const4u:
23629 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23630 i += 4;
23631 break;
23632
23633 case DW_OP_const4s:
23634 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23635 i += 4;
23636 break;
23637
585861ea
JK
23638 case DW_OP_const8u:
23639 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23640 i += 8;
23641 break;
23642
21ae7a4d
JK
23643 case DW_OP_constu:
23644 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23645 &bytes_read);
23646 i += bytes_read;
23647 break;
23648
23649 case DW_OP_consts:
23650 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23651 i += bytes_read;
23652 break;
23653
23654 case DW_OP_dup:
23655 stack[stacki + 1] = stack[stacki];
23656 stacki++;
23657 break;
23658
23659 case DW_OP_plus:
23660 stack[stacki - 1] += stack[stacki];
23661 stacki--;
23662 break;
23663
23664 case DW_OP_plus_uconst:
23665 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23666 &bytes_read);
23667 i += bytes_read;
23668 break;
23669
23670 case DW_OP_minus:
23671 stack[stacki - 1] -= stack[stacki];
23672 stacki--;
23673 break;
23674
23675 case DW_OP_deref:
23676 /* If we're not the last op, then we definitely can't encode
23677 this using GDB's address_class enum. This is valid for partial
23678 global symbols, although the variable's address will be bogus
23679 in the psymtab. */
23680 if (i < size)
23681 dwarf2_complex_location_expr_complaint ();
23682 break;
23683
23684 case DW_OP_GNU_push_tls_address:
4aa4e28b 23685 case DW_OP_form_tls_address:
21ae7a4d
JK
23686 /* The top of the stack has the offset from the beginning
23687 of the thread control block at which the variable is located. */
23688 /* Nothing should follow this operator, so the top of stack would
23689 be returned. */
23690 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23691 address will be bogus in the psymtab. Make it always at least
23692 non-zero to not look as a variable garbage collected by linker
23693 which have DW_OP_addr 0. */
21ae7a4d
JK
23694 if (i < size)
23695 dwarf2_complex_location_expr_complaint ();
585861ea 23696 stack[stacki]++;
21ae7a4d
JK
23697 break;
23698
23699 case DW_OP_GNU_uninit:
23700 break;
23701
336d760d 23702 case DW_OP_addrx:
3019eac3 23703 case DW_OP_GNU_addr_index:
49f6c839 23704 case DW_OP_GNU_const_index:
3019eac3
DE
23705 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23706 &bytes_read);
23707 i += bytes_read;
23708 break;
23709
21ae7a4d
JK
23710 default:
23711 {
f39c6ffd 23712 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23713
23714 if (name)
b98664d3 23715 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23716 name);
23717 else
b98664d3 23718 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23719 op);
23720 }
23721
23722 return (stack[stacki]);
d53d4ac5 23723 }
3c6e0cb3 23724
21ae7a4d
JK
23725 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23726 outside of the allocated space. Also enforce minimum>0. */
23727 if (stacki >= ARRAY_SIZE (stack) - 1)
23728 {
b98664d3 23729 complaint (_("location description stack overflow"));
21ae7a4d
JK
23730 return 0;
23731 }
23732
23733 if (stacki <= 0)
23734 {
b98664d3 23735 complaint (_("location description stack underflow"));
21ae7a4d
JK
23736 return 0;
23737 }
23738 }
23739 return (stack[stacki]);
c906108c
SS
23740}
23741
23742/* memory allocation interface */
23743
c906108c 23744static struct dwarf_block *
7b5a2f43 23745dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23746{
8d749320 23747 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23748}
23749
c906108c 23750static struct die_info *
b60c80d6 23751dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23752{
23753 struct die_info *die;
b60c80d6
DJ
23754 size_t size = sizeof (struct die_info);
23755
23756 if (num_attrs > 1)
23757 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23758
b60c80d6 23759 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23760 memset (die, 0, sizeof (struct die_info));
23761 return (die);
23762}
2e276125
JB
23763
23764\f
23765/* Macro support. */
23766
233d95b5
JK
23767/* Return file name relative to the compilation directory of file number I in
23768 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23769 responsible for freeing it. */
233d95b5 23770
2e276125 23771static char *
233d95b5 23772file_file_name (int file, struct line_header *lh)
2e276125 23773{
6a83a1e6
EZ
23774 /* Is the file number a valid index into the line header's file name
23775 table? Remember that file numbers start with one, not zero. */
7ba99d21 23776 if (lh->is_valid_file_index (file))
6a83a1e6 23777 {
7ba99d21 23778 const file_entry *fe = lh->file_name_at (file);
6e70227d 23779
7ba99d21 23780 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 23781 {
7ba99d21 23782 const char *dir = fe->include_dir (lh);
8c43009f 23783 if (dir != NULL)
7ba99d21 23784 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 23785 }
7ba99d21 23786 return xstrdup (fe->name);
6a83a1e6 23787 }
2e276125
JB
23788 else
23789 {
6a83a1e6
EZ
23790 /* The compiler produced a bogus file number. We can at least
23791 record the macro definitions made in the file, even if we
23792 won't be able to find the file by name. */
23793 char fake_name[80];
9a619af0 23794
8c042590
PM
23795 xsnprintf (fake_name, sizeof (fake_name),
23796 "<bad macro file number %d>", file);
2e276125 23797
b98664d3 23798 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 23799 file);
2e276125 23800
6a83a1e6 23801 return xstrdup (fake_name);
2e276125
JB
23802 }
23803}
23804
233d95b5
JK
23805/* Return the full name of file number I in *LH's file name table.
23806 Use COMP_DIR as the name of the current directory of the
23807 compilation. The result is allocated using xmalloc; the caller is
23808 responsible for freeing it. */
23809static char *
23810file_full_name (int file, struct line_header *lh, const char *comp_dir)
23811{
23812 /* Is the file number a valid index into the line header's file name
23813 table? Remember that file numbers start with one, not zero. */
7ba99d21 23814 if (lh->is_valid_file_index (file))
233d95b5
JK
23815 {
23816 char *relative = file_file_name (file, lh);
23817
23818 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23819 return relative;
b36cec19
PA
23820 return reconcat (relative, comp_dir, SLASH_STRING,
23821 relative, (char *) NULL);
233d95b5
JK
23822 }
23823 else
23824 return file_file_name (file, lh);
23825}
23826
2e276125
JB
23827
23828static struct macro_source_file *
804d2729
TT
23829macro_start_file (struct dwarf2_cu *cu,
23830 int file, int line,
2e276125 23831 struct macro_source_file *current_file,
43f3e411 23832 struct line_header *lh)
2e276125 23833{
233d95b5
JK
23834 /* File name relative to the compilation directory of this source file. */
23835 char *file_name = file_file_name (file, lh);
2e276125 23836
2e276125 23837 if (! current_file)
abc9d0dc 23838 {
fc474241
DE
23839 /* Note: We don't create a macro table for this compilation unit
23840 at all until we actually get a filename. */
c24bdb02 23841 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 23842
abc9d0dc
TT
23843 /* If we have no current file, then this must be the start_file
23844 directive for the compilation unit's main source file. */
fc474241
DE
23845 current_file = macro_set_main (macro_table, file_name);
23846 macro_define_special (macro_table);
abc9d0dc 23847 }
2e276125 23848 else
233d95b5 23849 current_file = macro_include (current_file, line, file_name);
2e276125 23850
233d95b5 23851 xfree (file_name);
6e70227d 23852
2e276125
JB
23853 return current_file;
23854}
23855
2e276125
JB
23856static const char *
23857consume_improper_spaces (const char *p, const char *body)
23858{
23859 if (*p == ' ')
23860 {
b98664d3 23861 complaint (_("macro definition contains spaces "
3e43a32a 23862 "in formal argument list:\n`%s'"),
4d3c2250 23863 body);
2e276125
JB
23864
23865 while (*p == ' ')
23866 p++;
23867 }
23868
23869 return p;
23870}
23871
23872
23873static void
23874parse_macro_definition (struct macro_source_file *file, int line,
23875 const char *body)
23876{
23877 const char *p;
23878
23879 /* The body string takes one of two forms. For object-like macro
23880 definitions, it should be:
23881
23882 <macro name> " " <definition>
23883
23884 For function-like macro definitions, it should be:
23885
23886 <macro name> "() " <definition>
23887 or
23888 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23889
23890 Spaces may appear only where explicitly indicated, and in the
23891 <definition>.
23892
23893 The Dwarf 2 spec says that an object-like macro's name is always
23894 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23895 the space when the macro's definition is the empty string.
2e276125
JB
23896
23897 The Dwarf 2 spec says that there should be no spaces between the
23898 formal arguments in a function-like macro's formal argument list,
23899 but versions of GCC around March 2002 include spaces after the
23900 commas. */
23901
23902
23903 /* Find the extent of the macro name. The macro name is terminated
23904 by either a space or null character (for an object-like macro) or
23905 an opening paren (for a function-like macro). */
23906 for (p = body; *p; p++)
23907 if (*p == ' ' || *p == '(')
23908 break;
23909
23910 if (*p == ' ' || *p == '\0')
23911 {
23912 /* It's an object-like macro. */
23913 int name_len = p - body;
456e800a 23914 std::string name (body, name_len);
2e276125
JB
23915 const char *replacement;
23916
23917 if (*p == ' ')
23918 replacement = body + name_len + 1;
23919 else
23920 {
4d3c2250 23921 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23922 replacement = body + name_len;
23923 }
6e70227d 23924
456e800a 23925 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23926 }
23927 else if (*p == '(')
23928 {
23929 /* It's a function-like macro. */
456e800a 23930 std::string name (body, p - body);
2e276125
JB
23931 int argc = 0;
23932 int argv_size = 1;
8d749320 23933 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23934
23935 p++;
23936
23937 p = consume_improper_spaces (p, body);
23938
23939 /* Parse the formal argument list. */
23940 while (*p && *p != ')')
23941 {
23942 /* Find the extent of the current argument name. */
23943 const char *arg_start = p;
23944
23945 while (*p && *p != ',' && *p != ')' && *p != ' ')
23946 p++;
23947
23948 if (! *p || p == arg_start)
4d3c2250 23949 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23950 else
23951 {
23952 /* Make sure argv has room for the new argument. */
23953 if (argc >= argv_size)
23954 {
23955 argv_size *= 2;
224c3ddb 23956 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23957 }
23958
3f8a7804 23959 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23960 }
23961
23962 p = consume_improper_spaces (p, body);
23963
23964 /* Consume the comma, if present. */
23965 if (*p == ',')
23966 {
23967 p++;
23968
23969 p = consume_improper_spaces (p, body);
23970 }
23971 }
23972
23973 if (*p == ')')
23974 {
23975 p++;
23976
23977 if (*p == ' ')
23978 /* Perfectly formed definition, no complaints. */
456e800a 23979 macro_define_function (file, line, name.c_str (),
6e70227d 23980 argc, (const char **) argv,
2e276125
JB
23981 p + 1);
23982 else if (*p == '\0')
23983 {
23984 /* Complain, but do define it. */
4d3c2250 23985 dwarf2_macro_malformed_definition_complaint (body);
456e800a 23986 macro_define_function (file, line, name.c_str (),
6e70227d 23987 argc, (const char **) argv,
2e276125
JB
23988 p);
23989 }
23990 else
23991 /* Just complain. */
4d3c2250 23992 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23993 }
23994 else
23995 /* Just complain. */
4d3c2250 23996 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23997
2e276125
JB
23998 {
23999 int i;
24000
24001 for (i = 0; i < argc; i++)
24002 xfree (argv[i]);
24003 }
24004 xfree (argv);
24005 }
24006 else
4d3c2250 24007 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24008}
24009
cf2c3c16
TT
24010/* Skip some bytes from BYTES according to the form given in FORM.
24011 Returns the new pointer. */
2e276125 24012
d521ce57
TT
24013static const gdb_byte *
24014skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24015 enum dwarf_form form,
24016 unsigned int offset_size,
24017 struct dwarf2_section_info *section)
2e276125 24018{
cf2c3c16 24019 unsigned int bytes_read;
2e276125 24020
cf2c3c16 24021 switch (form)
2e276125 24022 {
cf2c3c16
TT
24023 case DW_FORM_data1:
24024 case DW_FORM_flag:
24025 ++bytes;
24026 break;
24027
24028 case DW_FORM_data2:
24029 bytes += 2;
24030 break;
24031
24032 case DW_FORM_data4:
24033 bytes += 4;
24034 break;
24035
24036 case DW_FORM_data8:
24037 bytes += 8;
24038 break;
24039
0224619f
JK
24040 case DW_FORM_data16:
24041 bytes += 16;
24042 break;
24043
cf2c3c16
TT
24044 case DW_FORM_string:
24045 read_direct_string (abfd, bytes, &bytes_read);
24046 bytes += bytes_read;
24047 break;
24048
24049 case DW_FORM_sec_offset:
24050 case DW_FORM_strp:
36586728 24051 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24052 bytes += offset_size;
24053 break;
24054
24055 case DW_FORM_block:
24056 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24057 bytes += bytes_read;
24058 break;
24059
24060 case DW_FORM_block1:
24061 bytes += 1 + read_1_byte (abfd, bytes);
24062 break;
24063 case DW_FORM_block2:
24064 bytes += 2 + read_2_bytes (abfd, bytes);
24065 break;
24066 case DW_FORM_block4:
24067 bytes += 4 + read_4_bytes (abfd, bytes);
24068 break;
24069
336d760d 24070 case DW_FORM_addrx:
cf2c3c16 24071 case DW_FORM_sdata:
cf532bd1 24072 case DW_FORM_strx:
cf2c3c16 24073 case DW_FORM_udata:
3019eac3
DE
24074 case DW_FORM_GNU_addr_index:
24075 case DW_FORM_GNU_str_index:
d521ce57 24076 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24077 if (bytes == NULL)
24078 {
24079 dwarf2_section_buffer_overflow_complaint (section);
24080 return NULL;
24081 }
cf2c3c16
TT
24082 break;
24083
663c44ac
JK
24084 case DW_FORM_implicit_const:
24085 break;
24086
cf2c3c16
TT
24087 default:
24088 {
b98664d3 24089 complaint (_("invalid form 0x%x in `%s'"),
96b79293 24090 form, section->get_name ());
cf2c3c16
TT
24091 return NULL;
24092 }
2e276125
JB
24093 }
24094
cf2c3c16
TT
24095 return bytes;
24096}
757a13d0 24097
cf2c3c16
TT
24098/* A helper for dwarf_decode_macros that handles skipping an unknown
24099 opcode. Returns an updated pointer to the macro data buffer; or,
24100 on error, issues a complaint and returns NULL. */
757a13d0 24101
d521ce57 24102static const gdb_byte *
cf2c3c16 24103skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24104 const gdb_byte **opcode_definitions,
24105 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24106 bfd *abfd,
24107 unsigned int offset_size,
24108 struct dwarf2_section_info *section)
24109{
24110 unsigned int bytes_read, i;
24111 unsigned long arg;
d521ce57 24112 const gdb_byte *defn;
2e276125 24113
cf2c3c16 24114 if (opcode_definitions[opcode] == NULL)
2e276125 24115 {
b98664d3 24116 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24117 opcode);
24118 return NULL;
24119 }
2e276125 24120
cf2c3c16
TT
24121 defn = opcode_definitions[opcode];
24122 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24123 defn += bytes_read;
2e276125 24124
cf2c3c16
TT
24125 for (i = 0; i < arg; ++i)
24126 {
aead7601
SM
24127 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24128 (enum dwarf_form) defn[i], offset_size,
f664829e 24129 section);
cf2c3c16
TT
24130 if (mac_ptr == NULL)
24131 {
24132 /* skip_form_bytes already issued the complaint. */
24133 return NULL;
24134 }
24135 }
757a13d0 24136
cf2c3c16
TT
24137 return mac_ptr;
24138}
757a13d0 24139
cf2c3c16
TT
24140/* A helper function which parses the header of a macro section.
24141 If the macro section is the extended (for now called "GNU") type,
24142 then this updates *OFFSET_SIZE. Returns a pointer to just after
24143 the header, or issues a complaint and returns NULL on error. */
757a13d0 24144
d521ce57
TT
24145static const gdb_byte *
24146dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24147 bfd *abfd,
d521ce57 24148 const gdb_byte *mac_ptr,
cf2c3c16
TT
24149 unsigned int *offset_size,
24150 int section_is_gnu)
24151{
24152 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24153
cf2c3c16
TT
24154 if (section_is_gnu)
24155 {
24156 unsigned int version, flags;
757a13d0 24157
cf2c3c16 24158 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24159 if (version != 4 && version != 5)
cf2c3c16 24160 {
b98664d3 24161 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24162 version);
24163 return NULL;
24164 }
24165 mac_ptr += 2;
757a13d0 24166
cf2c3c16
TT
24167 flags = read_1_byte (abfd, mac_ptr);
24168 ++mac_ptr;
24169 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24170
cf2c3c16
TT
24171 if ((flags & 2) != 0)
24172 /* We don't need the line table offset. */
24173 mac_ptr += *offset_size;
757a13d0 24174
cf2c3c16
TT
24175 /* Vendor opcode descriptions. */
24176 if ((flags & 4) != 0)
24177 {
24178 unsigned int i, count;
757a13d0 24179
cf2c3c16
TT
24180 count = read_1_byte (abfd, mac_ptr);
24181 ++mac_ptr;
24182 for (i = 0; i < count; ++i)
24183 {
24184 unsigned int opcode, bytes_read;
24185 unsigned long arg;
24186
24187 opcode = read_1_byte (abfd, mac_ptr);
24188 ++mac_ptr;
24189 opcode_definitions[opcode] = mac_ptr;
24190 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24191 mac_ptr += bytes_read;
24192 mac_ptr += arg;
24193 }
757a13d0 24194 }
cf2c3c16 24195 }
757a13d0 24196
cf2c3c16
TT
24197 return mac_ptr;
24198}
757a13d0 24199
cf2c3c16 24200/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24201 including DW_MACRO_import. */
cf2c3c16
TT
24202
24203static void
804d2729 24204dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24205 bfd *abfd,
d521ce57 24206 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24207 struct macro_source_file *current_file,
43f3e411 24208 struct line_header *lh,
cf2c3c16 24209 struct dwarf2_section_info *section,
36586728 24210 int section_is_gnu, int section_is_dwz,
cf2c3c16 24211 unsigned int offset_size,
8fc3fc34 24212 htab_t include_hash)
cf2c3c16 24213{
804d2729
TT
24214 struct dwarf2_per_objfile *dwarf2_per_objfile
24215 = cu->per_cu->dwarf2_per_objfile;
4d663531 24216 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24217 enum dwarf_macro_record_type macinfo_type;
24218 int at_commandline;
d521ce57 24219 const gdb_byte *opcode_definitions[256];
757a13d0 24220
cf2c3c16
TT
24221 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24222 &offset_size, section_is_gnu);
24223 if (mac_ptr == NULL)
24224 {
24225 /* We already issued a complaint. */
24226 return;
24227 }
757a13d0
JK
24228
24229 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24230 GDB is still reading the definitions from command line. First
24231 DW_MACINFO_start_file will need to be ignored as it was already executed
24232 to create CURRENT_FILE for the main source holding also the command line
24233 definitions. On first met DW_MACINFO_start_file this flag is reset to
24234 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24235
24236 at_commandline = 1;
24237
24238 do
24239 {
24240 /* Do we at least have room for a macinfo type byte? */
24241 if (mac_ptr >= mac_end)
24242 {
f664829e 24243 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24244 break;
24245 }
24246
aead7601 24247 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24248 mac_ptr++;
24249
cf2c3c16
TT
24250 /* Note that we rely on the fact that the corresponding GNU and
24251 DWARF constants are the same. */
132448f8
SM
24252 DIAGNOSTIC_PUSH
24253 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24254 switch (macinfo_type)
24255 {
24256 /* A zero macinfo type indicates the end of the macro
24257 information. */
24258 case 0:
24259 break;
2e276125 24260
0af92d60
JK
24261 case DW_MACRO_define:
24262 case DW_MACRO_undef:
24263 case DW_MACRO_define_strp:
24264 case DW_MACRO_undef_strp:
24265 case DW_MACRO_define_sup:
24266 case DW_MACRO_undef_sup:
2e276125 24267 {
891d2f0b 24268 unsigned int bytes_read;
2e276125 24269 int line;
d521ce57 24270 const char *body;
cf2c3c16 24271 int is_define;
2e276125 24272
cf2c3c16
TT
24273 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24274 mac_ptr += bytes_read;
24275
0af92d60
JK
24276 if (macinfo_type == DW_MACRO_define
24277 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24278 {
24279 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24280 mac_ptr += bytes_read;
24281 }
24282 else
24283 {
24284 LONGEST str_offset;
24285
24286 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24287 mac_ptr += offset_size;
2e276125 24288
0af92d60
JK
24289 if (macinfo_type == DW_MACRO_define_sup
24290 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24291 || section_is_dwz)
36586728 24292 {
ed2dc618
SM
24293 struct dwz_file *dwz
24294 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24295
ed2dc618
SM
24296 body = read_indirect_string_from_dwz (objfile,
24297 dwz, str_offset);
36586728
TT
24298 }
24299 else
ed2dc618
SM
24300 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24301 abfd, str_offset);
cf2c3c16
TT
24302 }
24303
0af92d60
JK
24304 is_define = (macinfo_type == DW_MACRO_define
24305 || macinfo_type == DW_MACRO_define_strp
24306 || macinfo_type == DW_MACRO_define_sup);
2e276125 24307 if (! current_file)
757a13d0
JK
24308 {
24309 /* DWARF violation as no main source is present. */
b98664d3 24310 complaint (_("debug info with no main source gives macro %s "
757a13d0 24311 "on line %d: %s"),
cf2c3c16
TT
24312 is_define ? _("definition") : _("undefinition"),
24313 line, body);
757a13d0
JK
24314 break;
24315 }
3e43a32a
MS
24316 if ((line == 0 && !at_commandline)
24317 || (line != 0 && at_commandline))
b98664d3 24318 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24319 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24320 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24321 line == 0 ? _("zero") : _("non-zero"), line, body);
24322
955b06fa 24323 if (body == NULL)
7bede828 24324 {
955b06fa
SDJ
24325 /* Fedora's rpm-build's "debugedit" binary
24326 corrupted .debug_macro sections.
24327
24328 For more info, see
24329 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24330 complaint (_("debug info gives %s invalid macro %s "
24331 "without body (corrupted?) at line %d "
24332 "on file %s"),
24333 at_commandline ? _("command-line") : _("in-file"),
24334 is_define ? _("definition") : _("undefinition"),
24335 line, current_file->filename);
7bede828 24336 }
955b06fa
SDJ
24337 else if (is_define)
24338 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24339 else
24340 {
0af92d60
JK
24341 gdb_assert (macinfo_type == DW_MACRO_undef
24342 || macinfo_type == DW_MACRO_undef_strp
24343 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24344 macro_undef (current_file, line, body);
24345 }
2e276125
JB
24346 }
24347 break;
24348
0af92d60 24349 case DW_MACRO_start_file:
2e276125 24350 {
891d2f0b 24351 unsigned int bytes_read;
2e276125
JB
24352 int line, file;
24353
24354 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24355 mac_ptr += bytes_read;
24356 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24357 mac_ptr += bytes_read;
24358
3e43a32a
MS
24359 if ((line == 0 && !at_commandline)
24360 || (line != 0 && at_commandline))
b98664d3 24361 complaint (_("debug info gives source %d included "
757a13d0
JK
24362 "from %s at %s line %d"),
24363 file, at_commandline ? _("command-line") : _("file"),
24364 line == 0 ? _("zero") : _("non-zero"), line);
24365
24366 if (at_commandline)
24367 {
0af92d60 24368 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24369 pass one. */
757a13d0
JK
24370 at_commandline = 0;
24371 }
24372 else
804d2729
TT
24373 current_file = macro_start_file (cu, file, line, current_file,
24374 lh);
2e276125
JB
24375 }
24376 break;
24377
0af92d60 24378 case DW_MACRO_end_file:
2e276125 24379 if (! current_file)
b98664d3 24380 complaint (_("macro debug info has an unmatched "
3e43a32a 24381 "`close_file' directive"));
2e276125
JB
24382 else
24383 {
24384 current_file = current_file->included_by;
24385 if (! current_file)
24386 {
cf2c3c16 24387 enum dwarf_macro_record_type next_type;
2e276125
JB
24388
24389 /* GCC circa March 2002 doesn't produce the zero
24390 type byte marking the end of the compilation
24391 unit. Complain if it's not there, but exit no
24392 matter what. */
24393
24394 /* Do we at least have room for a macinfo type byte? */
24395 if (mac_ptr >= mac_end)
24396 {
f664829e 24397 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24398 return;
24399 }
24400
24401 /* We don't increment mac_ptr here, so this is just
24402 a look-ahead. */
aead7601
SM
24403 next_type
24404 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24405 mac_ptr);
2e276125 24406 if (next_type != 0)
b98664d3 24407 complaint (_("no terminating 0-type entry for "
3e43a32a 24408 "macros in `.debug_macinfo' section"));
2e276125
JB
24409
24410 return;
24411 }
24412 }
24413 break;
24414
0af92d60
JK
24415 case DW_MACRO_import:
24416 case DW_MACRO_import_sup:
cf2c3c16
TT
24417 {
24418 LONGEST offset;
8fc3fc34 24419 void **slot;
a036ba48
TT
24420 bfd *include_bfd = abfd;
24421 struct dwarf2_section_info *include_section = section;
d521ce57 24422 const gdb_byte *include_mac_end = mac_end;
a036ba48 24423 int is_dwz = section_is_dwz;
d521ce57 24424 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24425
24426 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24427 mac_ptr += offset_size;
24428
0af92d60 24429 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24430 {
ed2dc618 24431 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24432
96b79293 24433 dwz->macro.read (objfile);
a036ba48 24434
a036ba48 24435 include_section = &dwz->macro;
96b79293 24436 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
24437 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24438 is_dwz = 1;
24439 }
24440
24441 new_mac_ptr = include_section->buffer + offset;
24442 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24443
8fc3fc34
TT
24444 if (*slot != NULL)
24445 {
24446 /* This has actually happened; see
24447 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24448 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24449 ".debug_macro section"));
24450 }
24451 else
24452 {
d521ce57 24453 *slot = (void *) new_mac_ptr;
36586728 24454
804d2729 24455 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24456 include_mac_end, current_file, lh,
36586728 24457 section, section_is_gnu, is_dwz,
4d663531 24458 offset_size, include_hash);
8fc3fc34 24459
d521ce57 24460 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24461 }
cf2c3c16
TT
24462 }
24463 break;
24464
2e276125 24465 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24466 if (!section_is_gnu)
24467 {
24468 unsigned int bytes_read;
2e276125 24469
ac298888
TT
24470 /* This reads the constant, but since we don't recognize
24471 any vendor extensions, we ignore it. */
24472 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24473 mac_ptr += bytes_read;
24474 read_direct_string (abfd, mac_ptr, &bytes_read);
24475 mac_ptr += bytes_read;
2e276125 24476
cf2c3c16
TT
24477 /* We don't recognize any vendor extensions. */
24478 break;
24479 }
24480 /* FALLTHROUGH */
24481
24482 default:
24483 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24484 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24485 section);
24486 if (mac_ptr == NULL)
24487 return;
24488 break;
2e276125 24489 }
132448f8 24490 DIAGNOSTIC_POP
757a13d0 24491 } while (macinfo_type != 0);
2e276125 24492}
8e19ed76 24493
cf2c3c16 24494static void
09262596 24495dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24496 int section_is_gnu)
cf2c3c16 24497{
518817b3
SM
24498 struct dwarf2_per_objfile *dwarf2_per_objfile
24499 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24500 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24501 struct line_header *lh = cu->line_header;
24502 bfd *abfd;
d521ce57 24503 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24504 struct macro_source_file *current_file = 0;
24505 enum dwarf_macro_record_type macinfo_type;
24506 unsigned int offset_size = cu->header.offset_size;
d521ce57 24507 const gdb_byte *opcode_definitions[256];
8fc3fc34 24508 void **slot;
09262596
DE
24509 struct dwarf2_section_info *section;
24510 const char *section_name;
24511
24512 if (cu->dwo_unit != NULL)
24513 {
24514 if (section_is_gnu)
24515 {
24516 section = &cu->dwo_unit->dwo_file->sections.macro;
24517 section_name = ".debug_macro.dwo";
24518 }
24519 else
24520 {
24521 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24522 section_name = ".debug_macinfo.dwo";
24523 }
24524 }
24525 else
24526 {
24527 if (section_is_gnu)
24528 {
24529 section = &dwarf2_per_objfile->macro;
24530 section_name = ".debug_macro";
24531 }
24532 else
24533 {
24534 section = &dwarf2_per_objfile->macinfo;
24535 section_name = ".debug_macinfo";
24536 }
24537 }
cf2c3c16 24538
96b79293 24539 section->read (objfile);
cf2c3c16
TT
24540 if (section->buffer == NULL)
24541 {
b98664d3 24542 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24543 return;
24544 }
96b79293 24545 abfd = section->get_bfd_owner ();
cf2c3c16
TT
24546
24547 /* First pass: Find the name of the base filename.
24548 This filename is needed in order to process all macros whose definition
24549 (or undefinition) comes from the command line. These macros are defined
24550 before the first DW_MACINFO_start_file entry, and yet still need to be
24551 associated to the base file.
24552
24553 To determine the base file name, we scan the macro definitions until we
24554 reach the first DW_MACINFO_start_file entry. We then initialize
24555 CURRENT_FILE accordingly so that any macro definition found before the
24556 first DW_MACINFO_start_file can still be associated to the base file. */
24557
24558 mac_ptr = section->buffer + offset;
24559 mac_end = section->buffer + section->size;
24560
24561 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24562 &offset_size, section_is_gnu);
24563 if (mac_ptr == NULL)
24564 {
24565 /* We already issued a complaint. */
24566 return;
24567 }
24568
24569 do
24570 {
24571 /* Do we at least have room for a macinfo type byte? */
24572 if (mac_ptr >= mac_end)
24573 {
24574 /* Complaint is printed during the second pass as GDB will probably
24575 stop the first pass earlier upon finding
24576 DW_MACINFO_start_file. */
24577 break;
24578 }
24579
aead7601 24580 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24581 mac_ptr++;
24582
24583 /* Note that we rely on the fact that the corresponding GNU and
24584 DWARF constants are the same. */
132448f8
SM
24585 DIAGNOSTIC_PUSH
24586 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24587 switch (macinfo_type)
24588 {
24589 /* A zero macinfo type indicates the end of the macro
24590 information. */
24591 case 0:
24592 break;
24593
0af92d60
JK
24594 case DW_MACRO_define:
24595 case DW_MACRO_undef:
cf2c3c16
TT
24596 /* Only skip the data by MAC_PTR. */
24597 {
24598 unsigned int bytes_read;
24599
24600 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24601 mac_ptr += bytes_read;
24602 read_direct_string (abfd, mac_ptr, &bytes_read);
24603 mac_ptr += bytes_read;
24604 }
24605 break;
24606
0af92d60 24607 case DW_MACRO_start_file:
cf2c3c16
TT
24608 {
24609 unsigned int bytes_read;
24610 int line, file;
24611
24612 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24613 mac_ptr += bytes_read;
24614 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24615 mac_ptr += bytes_read;
24616
804d2729 24617 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
24618 }
24619 break;
24620
0af92d60 24621 case DW_MACRO_end_file:
cf2c3c16
TT
24622 /* No data to skip by MAC_PTR. */
24623 break;
24624
0af92d60
JK
24625 case DW_MACRO_define_strp:
24626 case DW_MACRO_undef_strp:
24627 case DW_MACRO_define_sup:
24628 case DW_MACRO_undef_sup:
cf2c3c16
TT
24629 {
24630 unsigned int bytes_read;
24631
24632 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24633 mac_ptr += bytes_read;
24634 mac_ptr += offset_size;
24635 }
24636 break;
24637
0af92d60
JK
24638 case DW_MACRO_import:
24639 case DW_MACRO_import_sup:
cf2c3c16 24640 /* Note that, according to the spec, a transparent include
0af92d60 24641 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24642 skip this opcode. */
24643 mac_ptr += offset_size;
24644 break;
24645
24646 case DW_MACINFO_vendor_ext:
24647 /* Only skip the data by MAC_PTR. */
24648 if (!section_is_gnu)
24649 {
24650 unsigned int bytes_read;
24651
24652 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24653 mac_ptr += bytes_read;
24654 read_direct_string (abfd, mac_ptr, &bytes_read);
24655 mac_ptr += bytes_read;
24656 }
24657 /* FALLTHROUGH */
24658
24659 default:
24660 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24661 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24662 section);
24663 if (mac_ptr == NULL)
24664 return;
24665 break;
24666 }
132448f8 24667 DIAGNOSTIC_POP
cf2c3c16
TT
24668 } while (macinfo_type != 0 && current_file == NULL);
24669
24670 /* Second pass: Process all entries.
24671
24672 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24673 command-line macro definitions/undefinitions. This flag is unset when we
24674 reach the first DW_MACINFO_start_file entry. */
24675
fc4007c9
TT
24676 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24677 htab_eq_pointer,
24678 NULL, xcalloc, xfree));
8fc3fc34 24679 mac_ptr = section->buffer + offset;
fc4007c9 24680 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24681 *slot = (void *) mac_ptr;
804d2729 24682 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 24683 current_file, lh, section,
fc4007c9
TT
24684 section_is_gnu, 0, offset_size,
24685 include_hash.get ());
cf2c3c16
TT
24686}
24687
3019eac3
DE
24688/* Return the .debug_loc section to use for CU.
24689 For DWO files use .debug_loc.dwo. */
24690
24691static struct dwarf2_section_info *
24692cu_debug_loc_section (struct dwarf2_cu *cu)
24693{
518817b3
SM
24694 struct dwarf2_per_objfile *dwarf2_per_objfile
24695 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24696
3019eac3 24697 if (cu->dwo_unit)
43988095
JK
24698 {
24699 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24700
43988095
JK
24701 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24702 }
24703 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24704 : &dwarf2_per_objfile->loc);
3019eac3
DE
24705}
24706
8cf6f0b1
TT
24707/* A helper function that fills in a dwarf2_loclist_baton. */
24708
24709static void
24710fill_in_loclist_baton (struct dwarf2_cu *cu,
24711 struct dwarf2_loclist_baton *baton,
ff39bb5e 24712 const struct attribute *attr)
8cf6f0b1 24713{
518817b3
SM
24714 struct dwarf2_per_objfile *dwarf2_per_objfile
24715 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24716 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24717
96b79293 24718 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
24719
24720 baton->per_cu = cu->per_cu;
24721 gdb_assert (baton->per_cu);
24722 /* We don't know how long the location list is, but make sure we
24723 don't run off the edge of the section. */
3019eac3
DE
24724 baton->size = section->size - DW_UNSND (attr);
24725 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24726 baton->base_address = cu->base_address;
f664829e 24727 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24728}
24729
4c2df51b 24730static void
ff39bb5e 24731dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24732 struct dwarf2_cu *cu, int is_block)
4c2df51b 24733{
518817b3
SM
24734 struct dwarf2_per_objfile *dwarf2_per_objfile
24735 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24736 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24737 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24738
cd6c91b4 24739 if (attr->form_is_section_offset ()
3019eac3 24740 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24741 the section. If so, fall through to the complaint in the
24742 other branch. */
3019eac3 24743 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24744 {
0d53c4c4 24745 struct dwarf2_loclist_baton *baton;
4c2df51b 24746
8d749320 24747 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24748
8cf6f0b1 24749 fill_in_loclist_baton (cu, baton, attr);
be391dca 24750
d00adf39 24751 if (cu->base_known == 0)
b98664d3 24752 complaint (_("Location list used without "
3e43a32a 24753 "specifying the CU base address."));
4c2df51b 24754
f1e6e072
TT
24755 SYMBOL_ACLASS_INDEX (sym) = (is_block
24756 ? dwarf2_loclist_block_index
24757 : dwarf2_loclist_index);
0d53c4c4
DJ
24758 SYMBOL_LOCATION_BATON (sym) = baton;
24759 }
24760 else
24761 {
24762 struct dwarf2_locexpr_baton *baton;
24763
8d749320 24764 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24765 baton->per_cu = cu->per_cu;
24766 gdb_assert (baton->per_cu);
0d53c4c4 24767
4fc6c0d5 24768 if (attr->form_is_block ())
0d53c4c4
DJ
24769 {
24770 /* Note that we're just copying the block's data pointer
24771 here, not the actual data. We're still pointing into the
6502dd73
DJ
24772 info_buffer for SYM's objfile; right now we never release
24773 that buffer, but when we do clean up properly this may
24774 need to change. */
0d53c4c4
DJ
24775 baton->size = DW_BLOCK (attr)->size;
24776 baton->data = DW_BLOCK (attr)->data;
24777 }
24778 else
24779 {
24780 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24781 sym->natural_name ());
0d53c4c4 24782 baton->size = 0;
0d53c4c4 24783 }
6e70227d 24784
f1e6e072
TT
24785 SYMBOL_ACLASS_INDEX (sym) = (is_block
24786 ? dwarf2_locexpr_block_index
24787 : dwarf2_locexpr_index);
0d53c4c4
DJ
24788 SYMBOL_LOCATION_BATON (sym) = baton;
24789 }
4c2df51b 24790}
6502dd73 24791
9aa1f1e3
TT
24792/* Return the OBJFILE associated with the compilation unit CU. If CU
24793 came from a separate debuginfo file, then the master objfile is
24794 returned. */
ae0d2f24
UW
24795
24796struct objfile *
24797dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24798{
e3b94546 24799 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24800
24801 /* Return the master objfile, so that we can report and look up the
24802 correct file containing this variable. */
24803 if (objfile->separate_debug_objfile_backlink)
24804 objfile = objfile->separate_debug_objfile_backlink;
24805
24806 return objfile;
24807}
24808
96408a79
SA
24809/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24810 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24811 CU_HEADERP first. */
24812
24813static const struct comp_unit_head *
24814per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24815 struct dwarf2_per_cu_data *per_cu)
24816{
d521ce57 24817 const gdb_byte *info_ptr;
96408a79
SA
24818
24819 if (per_cu->cu)
24820 return &per_cu->cu->header;
24821
9c541725 24822 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24823
24824 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24825 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24826 rcuh_kind::COMPILE);
96408a79
SA
24827
24828 return cu_headerp;
24829}
24830
ae0d2f24
UW
24831/* Return the address size given in the compilation unit header for CU. */
24832
98714339 24833int
ae0d2f24
UW
24834dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24835{
96408a79
SA
24836 struct comp_unit_head cu_header_local;
24837 const struct comp_unit_head *cu_headerp;
c471e790 24838
96408a79
SA
24839 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24840
24841 return cu_headerp->addr_size;
ae0d2f24
UW
24842}
24843
9eae7c52
TT
24844/* Return the offset size given in the compilation unit header for CU. */
24845
24846int
24847dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24848{
96408a79
SA
24849 struct comp_unit_head cu_header_local;
24850 const struct comp_unit_head *cu_headerp;
9c6c53f7 24851
96408a79
SA
24852 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24853
24854 return cu_headerp->offset_size;
24855}
24856
24857/* See its dwarf2loc.h declaration. */
24858
24859int
24860dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24861{
24862 struct comp_unit_head cu_header_local;
24863 const struct comp_unit_head *cu_headerp;
24864
24865 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24866
24867 if (cu_headerp->version == 2)
24868 return cu_headerp->addr_size;
24869 else
24870 return cu_headerp->offset_size;
181cebd4
JK
24871}
24872
9aa1f1e3
TT
24873/* Return the text offset of the CU. The returned offset comes from
24874 this CU's objfile. If this objfile came from a separate debuginfo
24875 file, then the offset may be different from the corresponding
24876 offset in the parent objfile. */
24877
24878CORE_ADDR
24879dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24880{
b3b3bada 24881 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
9aa1f1e3
TT
24882}
24883
9a49df9d
AB
24884/* Return a type that is a generic pointer type, the size of which matches
24885 the address size given in the compilation unit header for PER_CU. */
24886static struct type *
24887dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
24888{
24889 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24890 struct type *void_type = objfile_type (objfile)->builtin_void;
24891 struct type *addr_type = lookup_pointer_type (void_type);
24892 int addr_size = dwarf2_per_cu_addr_size (per_cu);
24893
24894 if (TYPE_LENGTH (addr_type) == addr_size)
24895 return addr_type;
24896
24897 addr_type
24898 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
24899 return addr_type;
24900}
24901
43988095
JK
24902/* Return DWARF version number of PER_CU. */
24903
24904short
24905dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24906{
24907 return per_cu->dwarf_version;
24908}
24909
348e048f
DE
24910/* Locate the .debug_info compilation unit from CU's objfile which contains
24911 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24912
24913static struct dwarf2_per_cu_data *
9c541725 24914dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24915 unsigned int offset_in_dwz,
ed2dc618 24916 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24917{
24918 struct dwarf2_per_cu_data *this_cu;
24919 int low, high;
24920
ae038cb0 24921 low = 0;
b76e467d 24922 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24923 while (high > low)
24924 {
36586728 24925 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24926 int mid = low + (high - low) / 2;
9a619af0 24927
36586728 24928 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 24929 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24930 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 24931 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
24932 high = mid;
24933 else
24934 low = mid + 1;
24935 }
24936 gdb_assert (low == high);
36586728 24937 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 24938 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24939 {
36586728 24940 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24941 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24942 "offset %s [in module %s]"),
24943 sect_offset_str (sect_off),
ed2dc618 24944 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24945
9c541725
PA
24946 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24947 <= sect_off);
ae038cb0
DJ
24948 return dwarf2_per_objfile->all_comp_units[low-1];
24949 }
24950 else
24951 {
b76e467d 24952 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24953 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24954 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24955 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24956 return this_cu;
24957 }
24958}
24959
23745b47 24960/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24961
fcd3b13d
SM
24962dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24963 : per_cu (per_cu_),
9068261f
AB
24964 mark (false),
24965 has_loclist (false),
24966 checked_producer (false),
24967 producer_is_gxx_lt_4_6 (false),
24968 producer_is_gcc_lt_4_3 (false),
eb77c9df 24969 producer_is_icc (false),
9068261f 24970 producer_is_icc_lt_14 (false),
c258c396 24971 producer_is_codewarrior (false),
9068261f 24972 processing_has_namespace_info (false)
93311388 24973{
fcd3b13d
SM
24974 per_cu->cu = this;
24975}
24976
24977/* Destroy a dwarf2_cu. */
24978
24979dwarf2_cu::~dwarf2_cu ()
24980{
24981 per_cu->cu = NULL;
9816fde3
JK
24982}
24983
24984/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24985
24986static void
95554aad
TT
24987prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24988 enum language pretend_language)
9816fde3
JK
24989{
24990 struct attribute *attr;
24991
24992 /* Set the language we're debugging. */
24993 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24994 if (attr != nullptr)
9816fde3
JK
24995 set_cu_language (DW_UNSND (attr), cu);
24996 else
9cded63f 24997 {
95554aad 24998 cu->language = pretend_language;
9cded63f
TT
24999 cu->language_defn = language_def (cu->language);
25000 }
dee91e82 25001
7d45c7c3 25002 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25003}
25004
ae038cb0
DJ
25005/* Increase the age counter on each cached compilation unit, and free
25006 any that are too old. */
25007
25008static void
ed2dc618 25009age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25010{
25011 struct dwarf2_per_cu_data *per_cu, **last_chain;
25012
25013 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25014 per_cu = dwarf2_per_objfile->read_in_chain;
25015 while (per_cu != NULL)
25016 {
25017 per_cu->cu->last_used ++;
b4f54984 25018 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25019 dwarf2_mark (per_cu->cu);
25020 per_cu = per_cu->cu->read_in_chain;
25021 }
25022
25023 per_cu = dwarf2_per_objfile->read_in_chain;
25024 last_chain = &dwarf2_per_objfile->read_in_chain;
25025 while (per_cu != NULL)
25026 {
25027 struct dwarf2_per_cu_data *next_cu;
25028
25029 next_cu = per_cu->cu->read_in_chain;
25030
25031 if (!per_cu->cu->mark)
25032 {
fcd3b13d 25033 delete per_cu->cu;
ae038cb0
DJ
25034 *last_chain = next_cu;
25035 }
25036 else
25037 last_chain = &per_cu->cu->read_in_chain;
25038
25039 per_cu = next_cu;
25040 }
25041}
25042
25043/* Remove a single compilation unit from the cache. */
25044
25045static void
dee91e82 25046free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25047{
25048 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25049 struct dwarf2_per_objfile *dwarf2_per_objfile
25050 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25051
25052 per_cu = dwarf2_per_objfile->read_in_chain;
25053 last_chain = &dwarf2_per_objfile->read_in_chain;
25054 while (per_cu != NULL)
25055 {
25056 struct dwarf2_per_cu_data *next_cu;
25057
25058 next_cu = per_cu->cu->read_in_chain;
25059
dee91e82 25060 if (per_cu == target_per_cu)
ae038cb0 25061 {
fcd3b13d 25062 delete per_cu->cu;
dee91e82 25063 per_cu->cu = NULL;
ae038cb0
DJ
25064 *last_chain = next_cu;
25065 break;
25066 }
25067 else
25068 last_chain = &per_cu->cu->read_in_chain;
25069
25070 per_cu = next_cu;
25071 }
25072}
25073
dee91e82
DE
25074/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25075 We store these in a hash table separate from the DIEs, and preserve them
25076 when the DIEs are flushed out of cache.
25077
25078 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25079 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25080 or the type may come from a DWO file. Furthermore, while it's more logical
25081 to use per_cu->section+offset, with Fission the section with the data is in
25082 the DWO file but we don't know that section at the point we need it.
25083 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25084 because we can enter the lookup routine, get_die_type_at_offset, from
25085 outside this file, and thus won't necessarily have PER_CU->cu.
25086 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25087
dee91e82 25088struct dwarf2_per_cu_offset_and_type
1c379e20 25089{
dee91e82 25090 const struct dwarf2_per_cu_data *per_cu;
9c541725 25091 sect_offset sect_off;
1c379e20
DJ
25092 struct type *type;
25093};
25094
dee91e82 25095/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25096
25097static hashval_t
dee91e82 25098per_cu_offset_and_type_hash (const void *item)
1c379e20 25099{
9a3c8263
SM
25100 const struct dwarf2_per_cu_offset_and_type *ofs
25101 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25102
9c541725 25103 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25104}
25105
dee91e82 25106/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25107
25108static int
dee91e82 25109per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25110{
9a3c8263
SM
25111 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25112 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25113 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25114 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25115
dee91e82 25116 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25117 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25118}
25119
25120/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25121 table if necessary. For convenience, return TYPE.
25122
25123 The DIEs reading must have careful ordering to:
85102364 25124 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25125 reading current DIE.
25126 * Not trying to dereference contents of still incompletely read in types
25127 while reading in other DIEs.
25128 * Enable referencing still incompletely read in types just by a pointer to
25129 the type without accessing its fields.
25130
25131 Therefore caller should follow these rules:
25132 * Try to fetch any prerequisite types we may need to build this DIE type
25133 before building the type and calling set_die_type.
e71ec853 25134 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25135 possible before fetching more types to complete the current type.
25136 * Make the type as complete as possible before fetching more types. */
1c379e20 25137
f792889a 25138static struct type *
1c379e20
DJ
25139set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25140{
518817b3
SM
25141 struct dwarf2_per_objfile *dwarf2_per_objfile
25142 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25143 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25144 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25145 struct attribute *attr;
25146 struct dynamic_prop prop;
1c379e20 25147
b4ba55a1
JB
25148 /* For Ada types, make sure that the gnat-specific data is always
25149 initialized (if not already set). There are a few types where
25150 we should not be doing so, because the type-specific area is
25151 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25152 where the type-specific area is used to store the floatformat).
25153 But this is not a problem, because the gnat-specific information
25154 is actually not needed for these types. */
25155 if (need_gnat_info (cu)
25156 && TYPE_CODE (type) != TYPE_CODE_FUNC
25157 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25158 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25159 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25160 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25161 && !HAVE_GNAT_AUX_INFO (type))
25162 INIT_GNAT_SPECIFIC (type);
25163
3f2f83dd
KB
25164 /* Read DW_AT_allocated and set in type. */
25165 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 25166 if (attr != NULL && attr->form_is_block ())
3f2f83dd 25167 {
9a49df9d
AB
25168 struct type *prop_type
25169 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25170 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25171 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25172 }
25173 else if (attr != NULL)
25174 {
b98664d3 25175 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25176 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25177 sect_offset_str (die->sect_off));
3f2f83dd
KB
25178 }
25179
25180 /* Read DW_AT_associated and set in type. */
25181 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 25182 if (attr != NULL && attr->form_is_block ())
3f2f83dd 25183 {
9a49df9d
AB
25184 struct type *prop_type
25185 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25186 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25187 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25188 }
25189 else if (attr != NULL)
25190 {
b98664d3 25191 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25192 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25193 sect_offset_str (die->sect_off));
3f2f83dd
KB
25194 }
25195
3cdcd0ce
JB
25196 /* Read DW_AT_data_location and set in type. */
25197 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25198 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25199 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25200 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25201
dee91e82 25202 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25203 {
dee91e82
DE
25204 dwarf2_per_objfile->die_type_hash =
25205 htab_create_alloc_ex (127,
25206 per_cu_offset_and_type_hash,
25207 per_cu_offset_and_type_eq,
25208 NULL,
25209 &objfile->objfile_obstack,
25210 hashtab_obstack_allocate,
25211 dummy_obstack_deallocate);
f792889a 25212 }
1c379e20 25213
dee91e82 25214 ofs.per_cu = cu->per_cu;
9c541725 25215 ofs.sect_off = die->sect_off;
1c379e20 25216 ofs.type = type;
dee91e82
DE
25217 slot = (struct dwarf2_per_cu_offset_and_type **)
25218 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25219 if (*slot)
b98664d3 25220 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25221 sect_offset_str (die->sect_off));
8d749320
SM
25222 *slot = XOBNEW (&objfile->objfile_obstack,
25223 struct dwarf2_per_cu_offset_and_type);
1c379e20 25224 **slot = ofs;
f792889a 25225 return type;
1c379e20
DJ
25226}
25227
9c541725 25228/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25229 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25230
25231static struct type *
9c541725 25232get_die_type_at_offset (sect_offset sect_off,
673bfd45 25233 struct dwarf2_per_cu_data *per_cu)
1c379e20 25234{
dee91e82 25235 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25236 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25237
dee91e82 25238 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25239 return NULL;
1c379e20 25240
dee91e82 25241 ofs.per_cu = per_cu;
9c541725 25242 ofs.sect_off = sect_off;
9a3c8263
SM
25243 slot = ((struct dwarf2_per_cu_offset_and_type *)
25244 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25245 if (slot)
25246 return slot->type;
25247 else
25248 return NULL;
25249}
25250
02142a6c 25251/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25252 or return NULL if DIE does not have a saved type. */
25253
25254static struct type *
25255get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25256{
9c541725 25257 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25258}
25259
10b3939b
DJ
25260/* Add a dependence relationship from CU to REF_PER_CU. */
25261
25262static void
25263dwarf2_add_dependence (struct dwarf2_cu *cu,
25264 struct dwarf2_per_cu_data *ref_per_cu)
25265{
25266 void **slot;
25267
25268 if (cu->dependencies == NULL)
25269 cu->dependencies
25270 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25271 NULL, &cu->comp_unit_obstack,
25272 hashtab_obstack_allocate,
25273 dummy_obstack_deallocate);
25274
25275 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25276 if (*slot == NULL)
25277 *slot = ref_per_cu;
25278}
1c379e20 25279
f504f079
DE
25280/* Subroutine of dwarf2_mark to pass to htab_traverse.
25281 Set the mark field in every compilation unit in the
ae038cb0
DJ
25282 cache that we must keep because we are keeping CU. */
25283
10b3939b
DJ
25284static int
25285dwarf2_mark_helper (void **slot, void *data)
25286{
25287 struct dwarf2_per_cu_data *per_cu;
25288
25289 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25290
25291 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25292 reading of the chain. As such dependencies remain valid it is not much
25293 useful to track and undo them during QUIT cleanups. */
25294 if (per_cu->cu == NULL)
25295 return 1;
25296
10b3939b
DJ
25297 if (per_cu->cu->mark)
25298 return 1;
9068261f 25299 per_cu->cu->mark = true;
10b3939b
DJ
25300
25301 if (per_cu->cu->dependencies != NULL)
25302 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25303
25304 return 1;
25305}
25306
f504f079
DE
25307/* Set the mark field in CU and in every other compilation unit in the
25308 cache that we must keep because we are keeping CU. */
25309
ae038cb0
DJ
25310static void
25311dwarf2_mark (struct dwarf2_cu *cu)
25312{
25313 if (cu->mark)
25314 return;
9068261f 25315 cu->mark = true;
10b3939b
DJ
25316 if (cu->dependencies != NULL)
25317 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25318}
25319
25320static void
25321dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25322{
25323 while (per_cu)
25324 {
9068261f 25325 per_cu->cu->mark = false;
ae038cb0
DJ
25326 per_cu = per_cu->cu->read_in_chain;
25327 }
72bf9492
DJ
25328}
25329
72bf9492
DJ
25330/* Trivial hash function for partial_die_info: the hash value of a DIE
25331 is its offset in .debug_info for this objfile. */
25332
25333static hashval_t
25334partial_die_hash (const void *item)
25335{
9a3c8263
SM
25336 const struct partial_die_info *part_die
25337 = (const struct partial_die_info *) item;
9a619af0 25338
9c541725 25339 return to_underlying (part_die->sect_off);
72bf9492
DJ
25340}
25341
25342/* Trivial comparison function for partial_die_info structures: two DIEs
25343 are equal if they have the same offset. */
25344
25345static int
25346partial_die_eq (const void *item_lhs, const void *item_rhs)
25347{
9a3c8263
SM
25348 const struct partial_die_info *part_die_lhs
25349 = (const struct partial_die_info *) item_lhs;
25350 const struct partial_die_info *part_die_rhs
25351 = (const struct partial_die_info *) item_rhs;
9a619af0 25352
9c541725 25353 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25354}
25355
3c3bb058
AB
25356struct cmd_list_element *set_dwarf_cmdlist;
25357struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25358
25359static void
981a3fb3 25360set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25361{
b4f54984 25362 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25363 gdb_stdout);
ae038cb0
DJ
25364}
25365
25366static void
981a3fb3 25367show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25368{
b4f54984 25369 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25370}
25371
491144b5 25372bool dwarf_always_disassemble;
437afbb8 25373
437afbb8 25374static void
cd4fb1b2
SM
25375show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25376 struct cmd_list_element *c, const char *value)
9291a0cd 25377{
cd4fb1b2
SM
25378 fprintf_filtered (file,
25379 _("Whether to always disassemble "
25380 "DWARF expressions is %s.\n"),
25381 value);
9291a0cd
TT
25382}
25383
9291a0cd 25384static void
cd4fb1b2
SM
25385show_check_physname (struct ui_file *file, int from_tty,
25386 struct cmd_list_element *c, const char *value)
9291a0cd 25387{
cd4fb1b2
SM
25388 fprintf_filtered (file,
25389 _("Whether to check \"physname\" is %s.\n"),
25390 value);
9291a0cd
TT
25391}
25392
6c265988 25393void _initialize_dwarf2_read ();
cd4fb1b2 25394void
6c265988 25395_initialize_dwarf2_read ()
9291a0cd 25396{
cd4fb1b2
SM
25397 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25398Set DWARF specific variables.\n\
590042fc 25399Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25400 &set_dwarf_cmdlist, "maintenance set dwarf ",
25401 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25402
cd4fb1b2 25403 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25404Show DWARF specific variables.\n\
25405Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25406 &show_dwarf_cmdlist, "maintenance show dwarf ",
25407 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25408
cd4fb1b2
SM
25409 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25410 &dwarf_max_cache_age, _("\
25411Set the upper bound on the age of cached DWARF compilation units."), _("\
25412Show the upper bound on the age of cached DWARF compilation units."), _("\
25413A higher limit means that cached compilation units will be stored\n\
25414in memory longer, and more total memory will be used. Zero disables\n\
25415caching, which can slow down startup."),
25416 NULL,
25417 show_dwarf_max_cache_age,
25418 &set_dwarf_cmdlist,
25419 &show_dwarf_cmdlist);
156942c7 25420
cd4fb1b2
SM
25421 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25422 &dwarf_always_disassemble, _("\
25423Set whether `info address' always disassembles DWARF expressions."), _("\
25424Show whether `info address' always disassembles DWARF expressions."), _("\
25425When enabled, DWARF expressions are always printed in an assembly-like\n\
25426syntax. When disabled, expressions will be printed in a more\n\
25427conversational style, when possible."),
25428 NULL,
25429 show_dwarf_always_disassemble,
25430 &set_dwarf_cmdlist,
25431 &show_dwarf_cmdlist);
9291a0cd 25432
cd4fb1b2
SM
25433 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25434Set debugging of the DWARF reader."), _("\
25435Show debugging of the DWARF reader."), _("\
25436When enabled (non-zero), debugging messages are printed during DWARF\n\
25437reading and symtab expansion. A value of 1 (one) provides basic\n\
25438information. A value greater than 1 provides more verbose information."),
25439 NULL,
25440 NULL,
25441 &setdebuglist, &showdebuglist);
9291a0cd 25442
cd4fb1b2
SM
25443 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25444Set debugging of the DWARF DIE reader."), _("\
25445Show debugging of the DWARF DIE reader."), _("\
25446When enabled (non-zero), DIEs are dumped after they are read in.\n\
25447The value is the maximum depth to print."),
25448 NULL,
25449 NULL,
25450 &setdebuglist, &showdebuglist);
9291a0cd 25451
cd4fb1b2
SM
25452 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25453Set debugging of the dwarf line reader."), _("\
25454Show debugging of the dwarf line reader."), _("\
25455When enabled (non-zero), line number entries are dumped as they are read in.\n\
25456A value of 1 (one) provides basic information.\n\
25457A value greater than 1 provides more verbose information."),
25458 NULL,
25459 NULL,
25460 &setdebuglist, &showdebuglist);
437afbb8 25461
cd4fb1b2
SM
25462 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25463Set cross-checking of \"physname\" code against demangler."), _("\
25464Show cross-checking of \"physname\" code against demangler."), _("\
25465When enabled, GDB's internal \"physname\" code is checked against\n\
25466the demangler."),
25467 NULL, show_check_physname,
25468 &setdebuglist, &showdebuglist);
900e11f9 25469
e615022a
DE
25470 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25471 no_class, &use_deprecated_index_sections, _("\
25472Set whether to use deprecated gdb_index sections."), _("\
25473Show whether to use deprecated gdb_index sections."), _("\
25474When enabled, deprecated .gdb_index sections are used anyway.\n\
25475Normally they are ignored either because of a missing feature or\n\
25476performance issue.\n\
25477Warning: This option must be enabled before gdb reads the file."),
25478 NULL,
25479 NULL,
25480 &setlist, &showlist);
25481
f1e6e072
TT
25482 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25483 &dwarf2_locexpr_funcs);
25484 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25485 &dwarf2_loclist_funcs);
25486
25487 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25488 &dwarf2_block_frame_base_locexpr_funcs);
25489 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25490 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25491
25492#if GDB_SELF_TEST
25493 selftests::register_test ("dw2_expand_symtabs_matching",
25494 selftests::dw2_expand_symtabs_matching::run_test);
25495#endif
6502dd73 25496}
This page took 7.058705 seconds and 4 git commands to generate.