Change attr_form_is_block to be a method
[deliverable/binutils-gdb.git] / gdb / dwarf2read.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"
4de283e4 32#include "dwarf2read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
d55e5aa6
TT
35#include "dwarf-index-cache.h"
36#include "dwarf-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"
d55e5aa6
TT
51#include "dwarf2expr.h"
52#include "dwarf2loc.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
DE
905
906 /* The value of the DW_AT_comp_dir attribute. */
907 const char *comp_dir;
685af9cd
TT
908
909 /* The abbreviation table to use when reading the DIEs. */
910 struct abbrev_table *abbrev_table;
93311388
DE
911};
912
c0ab21c2
TT
913/* A subclass of die_reader_specs that holds storage and has complex
914 constructor and destructor behavior. */
915
916class cutu_reader : public die_reader_specs
917{
918public:
919
920 cutu_reader (struct dwarf2_per_cu_data *this_cu,
921 struct abbrev_table *abbrev_table,
922 int use_existing_cu, int keep,
923 bool skip_partial);
924
925 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
926 struct dwarf2_cu *parent_cu = nullptr,
927 struct dwo_file *dwo_file = nullptr);
928
929 ~cutu_reader ();
930
931 DISABLE_COPY_AND_ASSIGN (cutu_reader);
932
933 const gdb_byte *info_ptr = nullptr;
934 struct die_info *comp_unit_die = nullptr;
935 int has_children = 0;
936 bool dummy_p = false;
937
938private:
939 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
940 int use_existing_cu, int keep);
941
942 struct dwarf2_per_cu_data *m_this_cu;
943 int m_keep = 0;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951};
dee91e82 952
7ba99d21
AT
953/* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
954 later. */
955typedef int dir_index;
ecfb656c 956
7ba99d21
AT
957/* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
958 and later. */
959typedef int file_name_index;
ecfb656c 960
52059ffd
TT
961struct file_entry
962{
fff8551c
PA
963 file_entry () = default;
964
ecfb656c 965 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
966 unsigned int mod_time_, unsigned int length_)
967 : name (name_),
ecfb656c 968 d_index (d_index_),
fff8551c
PA
969 mod_time (mod_time_),
970 length (length_)
971 {}
972
ecfb656c
PA
973 /* Return the include directory at D_INDEX stored in LH. Returns
974 NULL if D_INDEX is out of bounds. */
8c43009f
PA
975 const char *include_dir (const line_header *lh) const;
976
fff8551c
PA
977 /* The file name. Note this is an observing pointer. The memory is
978 owned by debug_line_buffer. */
979 const char *name {};
980
8c43009f 981 /* The directory index (1-based). */
ecfb656c 982 dir_index d_index {};
fff8551c
PA
983
984 unsigned int mod_time {};
985
986 unsigned int length {};
987
988 /* True if referenced by the Line Number Program. */
989 bool included_p {};
990
83769d0b 991 /* The associated symbol table, if any. */
fff8551c 992 struct symtab *symtab {};
52059ffd
TT
993};
994
debd256d
JB
995/* The line number information for a compilation unit (found in the
996 .debug_line section) begins with a "statement program header",
997 which contains the following information. */
998struct line_header
999{
fff8551c
PA
1000 line_header ()
1001 : offset_in_dwz {}
1002 {}
1003
1004 /* Add an entry to the include directory table. */
1005 void add_include_dir (const char *include_dir);
1006
1007 /* Add an entry to the file name table. */
ecfb656c 1008 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1009 unsigned int mod_time, unsigned int length);
1010
7ba99d21
AT
1011 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1012 Returns NULL if INDEX is out of bounds. */
ecfb656c 1013 const char *include_dir_at (dir_index index) const
8c43009f 1014 {
7ba99d21
AT
1015 int vec_index;
1016 if (version >= 5)
1017 vec_index = index;
1018 else
1019 vec_index = index - 1;
1020 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
8c43009f 1021 return NULL;
7ba99d21 1022 return m_include_dirs[vec_index];
8c43009f
PA
1023 }
1024
7ba99d21 1025 bool is_valid_file_index (int file_index)
8c43009f 1026 {
7ba99d21
AT
1027 if (version >= 5)
1028 return 0 <= file_index && file_index < file_names_size ();
1029 return 1 <= file_index && file_index <= file_names_size ();
1030 }
ecfb656c 1031
7ba99d21
AT
1032 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1033 Returns NULL if INDEX is out of bounds. */
1034 file_entry *file_name_at (file_name_index index)
1035 {
1036 int vec_index;
1037 if (version >= 5)
1038 vec_index = index;
1039 else
1040 vec_index = index - 1;
1041 if (vec_index < 0 || vec_index >= m_file_names.size ())
fff8551c 1042 return NULL;
7ba99d21 1043 return &m_file_names[vec_index];
fff8551c
PA
1044 }
1045
7ba99d21
AT
1046 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1047 this method should only be used to iterate through all file entries in an
1048 index-agnostic manner. */
1049 std::vector<file_entry> &file_names ()
1050 { return m_file_names; }
1051
527f3840 1052 /* Offset of line number information in .debug_line section. */
9c541725 1053 sect_offset sect_off {};
527f3840
JK
1054
1055 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1056 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1057
1058 unsigned int total_length {};
1059 unsigned short version {};
1060 unsigned int header_length {};
1061 unsigned char minimum_instruction_length {};
1062 unsigned char maximum_ops_per_instruction {};
1063 unsigned char default_is_stmt {};
1064 int line_base {};
1065 unsigned char line_range {};
1066 unsigned char opcode_base {};
debd256d
JB
1067
1068 /* standard_opcode_lengths[i] is the number of operands for the
1069 standard opcode whose value is i. This means that
1070 standard_opcode_lengths[0] is unused, and the last meaningful
1071 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1072 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1073
7ba99d21
AT
1074 int file_names_size ()
1075 { return m_file_names.size(); }
debd256d
JB
1076
1077 /* The start and end of the statement program following this
6502dd73 1078 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1079 const gdb_byte *statement_program_start {}, *statement_program_end {};
7ba99d21
AT
1080
1081 private:
1082 /* The include_directories table. Note these are observing
1083 pointers. The memory is owned by debug_line_buffer. */
1084 std::vector<const char *> m_include_dirs;
1085
1086 /* The file_names table. This is private because the meaning of indexes
1087 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1088 before, and is 0 in DWARF 5 and later). So the client should use
1089 file_name_at method for access. */
1090 std::vector<file_entry> m_file_names;
debd256d 1091};
c906108c 1092
fff8551c
PA
1093typedef std::unique_ptr<line_header> line_header_up;
1094
8c43009f
PA
1095const char *
1096file_entry::include_dir (const line_header *lh) const
1097{
ecfb656c 1098 return lh->include_dir_at (d_index);
8c43009f
PA
1099}
1100
c906108c 1101/* When we construct a partial symbol table entry we only
0963b4bd 1102 need this much information. */
6f06d47b 1103struct partial_die_info : public allocate_on_obstack
c906108c 1104 {
6f06d47b
YQ
1105 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1106
1107 /* Disable assign but still keep copy ctor, which is needed
1108 load_partial_dies. */
1109 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1110
52356b79
YQ
1111 /* Adjust the partial die before generating a symbol for it. This
1112 function may set the is_external flag or change the DIE's
1113 name. */
1114 void fixup (struct dwarf2_cu *cu);
1115
48fbe735
YQ
1116 /* Read a minimal amount of information into the minimal die
1117 structure. */
1118 const gdb_byte *read (const struct die_reader_specs *reader,
1119 const struct abbrev_info &abbrev,
1120 const gdb_byte *info_ptr);
1121
72bf9492 1122 /* Offset of this DIE. */
6f06d47b 1123 const sect_offset sect_off;
72bf9492
DJ
1124
1125 /* DWARF-2 tag for this DIE. */
6f06d47b 1126 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1127
72bf9492 1128 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1129 const unsigned int has_children : 1;
1130
72bf9492
DJ
1131 unsigned int is_external : 1;
1132 unsigned int is_declaration : 1;
1133 unsigned int has_type : 1;
1134 unsigned int has_specification : 1;
1135 unsigned int has_pc_info : 1;
481860b3 1136 unsigned int may_be_inlined : 1;
72bf9492 1137
0c1b455e
TT
1138 /* This DIE has been marked DW_AT_main_subprogram. */
1139 unsigned int main_subprogram : 1;
1140
72bf9492
DJ
1141 /* Flag set if the SCOPE field of this structure has been
1142 computed. */
1143 unsigned int scope_set : 1;
1144
fa4028e9
JB
1145 /* Flag set if the DIE has a byte_size attribute. */
1146 unsigned int has_byte_size : 1;
1147
ff908ebf
AW
1148 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1149 unsigned int has_const_value : 1;
1150
98bfdba5
PA
1151 /* Flag set if any of the DIE's children are template arguments. */
1152 unsigned int has_template_arguments : 1;
1153
52356b79 1154 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1155 unsigned int fixup_called : 1;
1156
36586728
TT
1157 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1158 unsigned int is_dwz : 1;
1159
1160 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1161 unsigned int spec_is_dwz : 1;
1162
72bf9492 1163 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1164 sometimes a default name for unnamed DIEs. */
6f06d47b 1165 const char *name = nullptr;
72bf9492 1166
abc72ce4 1167 /* The linkage name, if present. */
6f06d47b 1168 const char *linkage_name = nullptr;
abc72ce4 1169
72bf9492
DJ
1170 /* The scope to prepend to our children. This is generally
1171 allocated on the comp_unit_obstack, so will disappear
1172 when this compilation unit leaves the cache. */
6f06d47b 1173 const char *scope = nullptr;
72bf9492 1174
95554aad
TT
1175 /* Some data associated with the partial DIE. The tag determines
1176 which field is live. */
1177 union
1178 {
1179 /* The location description associated with this DIE, if any. */
1180 struct dwarf_block *locdesc;
1181 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1182 sect_offset sect_off;
6f06d47b 1183 } d {};
72bf9492
DJ
1184
1185 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1186 CORE_ADDR lowpc = 0;
1187 CORE_ADDR highpc = 0;
72bf9492 1188
93311388 1189 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1190 DW_AT_sibling, if any. */
48fbe735
YQ
1191 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1192 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1193 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1194
1195 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1196 DW_AT_specification (or DW_AT_abstract_origin or
1197 DW_AT_extension). */
6f06d47b 1198 sect_offset spec_offset {};
72bf9492
DJ
1199
1200 /* Pointers to this DIE's parent, first child, and next sibling,
1201 if any. */
6f06d47b
YQ
1202 struct partial_die_info *die_parent = nullptr;
1203 struct partial_die_info *die_child = nullptr;
1204 struct partial_die_info *die_sibling = nullptr;
1205
1206 friend struct partial_die_info *
1207 dwarf2_cu::find_partial_die (sect_offset sect_off);
1208
1209 private:
1210 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1211 partial_die_info (sect_offset sect_off)
1212 : partial_die_info (sect_off, DW_TAG_padding, 0)
1213 {
1214 }
1215
1216 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1217 int has_children_)
1218 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1219 {
1220 is_external = 0;
1221 is_declaration = 0;
1222 has_type = 0;
1223 has_specification = 0;
1224 has_pc_info = 0;
1225 may_be_inlined = 0;
1226 main_subprogram = 0;
1227 scope_set = 0;
1228 has_byte_size = 0;
1229 has_const_value = 0;
1230 has_template_arguments = 0;
1231 fixup_called = 0;
1232 is_dwz = 0;
1233 spec_is_dwz = 0;
1234 }
c906108c
SS
1235 };
1236
0963b4bd 1237/* This data structure holds a complete die structure. */
c906108c
SS
1238struct die_info
1239 {
76815b17
DE
1240 /* DWARF-2 tag for this DIE. */
1241 ENUM_BITFIELD(dwarf_tag) tag : 16;
1242
1243 /* Number of attributes */
98bfdba5
PA
1244 unsigned char num_attrs;
1245
1246 /* True if we're presently building the full type name for the
1247 type derived from this DIE. */
1248 unsigned char building_fullname : 1;
76815b17 1249
adde2bff
DE
1250 /* True if this die is in process. PR 16581. */
1251 unsigned char in_process : 1;
1252
76815b17
DE
1253 /* Abbrev number */
1254 unsigned int abbrev;
1255
93311388 1256 /* Offset in .debug_info or .debug_types section. */
9c541725 1257 sect_offset sect_off;
78ba4af6
JB
1258
1259 /* The dies in a compilation unit form an n-ary tree. PARENT
1260 points to this die's parent; CHILD points to the first child of
1261 this node; and all the children of a given node are chained
4950bc1c 1262 together via their SIBLING fields. */
639d11d3
DC
1263 struct die_info *child; /* Its first child, if any. */
1264 struct die_info *sibling; /* Its next sibling, if any. */
1265 struct die_info *parent; /* Its parent, if any. */
c906108c 1266
b60c80d6
DJ
1267 /* An array of attributes, with NUM_ATTRS elements. There may be
1268 zero, but it's not common and zero-sized arrays are not
1269 sufficiently portable C. */
1270 struct attribute attrs[1];
c906108c
SS
1271 };
1272
c906108c
SS
1273/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1274 but this would require a corresponding change in unpack_field_as_long
1275 and friends. */
1276static int bits_per_byte = 8;
1277
2ddeaf8a
TT
1278/* When reading a variant or variant part, we track a bit more
1279 information about the field, and store it in an object of this
1280 type. */
1281
1282struct variant_field
1283{
1284 /* If we see a DW_TAG_variant, then this will be the discriminant
1285 value. */
1286 ULONGEST discriminant_value;
1287 /* If we see a DW_TAG_variant, then this will be set if this is the
1288 default branch. */
1289 bool default_branch;
1290 /* While reading a DW_TAG_variant_part, this will be set if this
1291 field is the discriminant. */
1292 bool is_discriminant;
1293};
1294
52059ffd
TT
1295struct nextfield
1296{
be2daae6
TT
1297 int accessibility = 0;
1298 int virtuality = 0;
2ddeaf8a 1299 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1300 struct variant_field variant {};
1301 struct field field {};
52059ffd
TT
1302};
1303
1304struct fnfieldlist
1305{
be2daae6
TT
1306 const char *name = nullptr;
1307 std::vector<struct fn_field> fnfields;
52059ffd
TT
1308};
1309
c906108c
SS
1310/* The routines that read and process dies for a C struct or C++ class
1311 pass lists of data member fields and lists of member function fields
1312 in an instance of a field_info structure, as defined below. */
1313struct field_info
c5aa993b 1314 {
0963b4bd 1315 /* List of data member and baseclasses fields. */
be2daae6
TT
1316 std::vector<struct nextfield> fields;
1317 std::vector<struct nextfield> baseclasses;
c906108c 1318
7d0ccb61 1319 /* Number of fields (including baseclasses). */
be2daae6 1320 int nfields = 0;
c906108c 1321
85102364 1322 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1323 int non_public_fields = 0;
c906108c 1324
c5aa993b
JM
1325 /* Member function fieldlist array, contains name of possibly overloaded
1326 member function, number of overloaded member functions and a pointer
1327 to the head of the member function field chain. */
be2daae6 1328 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1329
1330 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1331 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1332 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1333
1334 /* Nested types defined by this class and the number of elements in this
1335 list. */
be2daae6 1336 std::vector<struct decl_field> nested_types_list;
c5aa993b 1337 };
c906108c 1338
10b3939b
DJ
1339/* One item on the queue of compilation units to read in full symbols
1340 for. */
1341struct dwarf2_queue_item
1342{
1343 struct dwarf2_per_cu_data *per_cu;
95554aad 1344 enum language pretend_language;
10b3939b
DJ
1345 struct dwarf2_queue_item *next;
1346};
1347
1348/* The current queue. */
1349static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1350
ae038cb0
DJ
1351/* Loaded secondary compilation units are kept in memory until they
1352 have not been referenced for the processing of this many
1353 compilation units. Set this to zero to disable caching. Cache
1354 sizes of up to at least twenty will improve startup time for
1355 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1356static int dwarf_max_cache_age = 5;
920d2a44 1357static void
b4f54984
DE
1358show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1359 struct cmd_list_element *c, const char *value)
920d2a44 1360{
3e43a32a 1361 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1362 "DWARF compilation units is %s.\n"),
920d2a44
AC
1363 value);
1364}
4390d890 1365\f
c906108c
SS
1366/* local function prototypes */
1367
918dd910
JK
1368static void dwarf2_find_base_address (struct die_info *die,
1369 struct dwarf2_cu *cu);
1370
891813be 1371static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1372 (struct dwarf2_per_cu_data *per_cu, const char *name);
1373
f1902523
JK
1374static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1375 const gdb_byte *info_ptr,
1376 struct die_info *type_unit_die,
c0ab21c2 1377 int has_children);
f1902523 1378
ed2dc618
SM
1379static void dwarf2_build_psymtabs_hard
1380 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1381
72bf9492
DJ
1382static void scan_partial_symbols (struct partial_die_info *,
1383 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1384 int, struct dwarf2_cu *);
c906108c 1385
72bf9492
DJ
1386static void add_partial_symbol (struct partial_die_info *,
1387 struct dwarf2_cu *);
63d06c5c 1388
72bf9492
DJ
1389static void add_partial_namespace (struct partial_die_info *pdi,
1390 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1391 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1392
5d7cb8df 1393static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1394 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1395 struct dwarf2_cu *cu);
1396
72bf9492
DJ
1397static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1398 struct dwarf2_cu *cu);
91c24f0a 1399
bc30ff58
JB
1400static void add_partial_subprogram (struct partial_die_info *pdi,
1401 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1402 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1403
d521ce57 1404static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1405
dee91e82 1406static struct partial_die_info *load_partial_dies
d521ce57 1407 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1408
fb816e8b
TV
1409/* A pair of partial_die_info and compilation unit. */
1410struct cu_partial_die_info
1411{
1412 /* The compilation unit of the partial_die_info. */
1413 struct dwarf2_cu *cu;
1414 /* A partial_die_info. */
1415 struct partial_die_info *pdi;
122cf0f2
AB
1416
1417 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1418 : cu (cu),
1419 pdi (pdi)
405feb71 1420 { /* Nothing. */ }
122cf0f2
AB
1421
1422private:
1423 cu_partial_die_info () = delete;
fb816e8b
TV
1424};
1425
122cf0f2
AB
1426static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1427 struct dwarf2_cu *);
72bf9492 1428
d521ce57
TT
1429static const gdb_byte *read_attribute (const struct die_reader_specs *,
1430 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1431 const gdb_byte *, bool *need_reprocess);
1432
1433static void read_attribute_reprocess (const struct die_reader_specs *reader,
1434 struct attribute *attr);
1435
1436static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1437
d521ce57 1438static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1439 unsigned int *);
c906108c 1440
d521ce57 1441static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1442
1443static LONGEST read_checked_initial_length_and_offset
d521ce57 1444 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1445 unsigned int *, unsigned int *);
613e1657 1446
d521ce57
TT
1447static LONGEST read_offset (bfd *, const gdb_byte *,
1448 const struct comp_unit_head *,
c764a876
DE
1449 unsigned int *);
1450
d521ce57 1451static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1452
ed2dc618
SM
1453static sect_offset read_abbrev_offset
1454 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1455 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1456
d521ce57 1457static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1458
d521ce57 1459static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1460
ed2dc618
SM
1461static const char *read_indirect_string
1462 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1463 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1464
ed2dc618
SM
1465static const char *read_indirect_line_string
1466 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1467 const struct comp_unit_head *, unsigned int *);
36586728 1468
ed2dc618
SM
1469static const char *read_indirect_string_at_offset
1470 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1471 LONGEST str_offset);
927aa2e7 1472
ed2dc618
SM
1473static const char *read_indirect_string_from_dwz
1474 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1475
d521ce57
TT
1476static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1477 const gdb_byte *,
3019eac3
DE
1478 unsigned int *);
1479
18a8505e
AT
1480static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1481 ULONGEST str_index);
1482
1483static const char *read_stub_str_index (struct dwarf2_cu *cu,
1484 ULONGEST str_index);
3019eac3 1485
e142c38c 1486static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1487
e142c38c
DJ
1488static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1489 struct dwarf2_cu *);
c906108c 1490
348e048f 1491static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1492 unsigned int);
348e048f 1493
7d45c7c3
KB
1494static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1495 struct dwarf2_cu *cu);
1496
a084a2a6
AT
1497static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1498
05cf31d1
JB
1499static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1500 struct dwarf2_cu *cu);
1501
e142c38c 1502static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1503
e142c38c 1504static struct die_info *die_specification (struct die_info *die,
f2f0e013 1505 struct dwarf2_cu **);
63d06c5c 1506
9c541725 1507static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1508 struct dwarf2_cu *cu);
debd256d 1509
f3f5162e 1510static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1511 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1512 CORE_ADDR, int decode_mapping);
c906108c 1513
804d2729
TT
1514static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1515 const char *);
c906108c 1516
a14ed312 1517static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1518 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1519
ff39bb5e 1520static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1521 struct dwarf2_cu *);
c906108c 1522
ff39bb5e 1523static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1524 struct type *type,
1525 const char *name,
1526 struct obstack *obstack,
12df843f 1527 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1528 const gdb_byte **bytes,
98bfdba5 1529 struct dwarf2_locexpr_baton **baton);
2df3850c 1530
e7c27a73 1531static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1532
b4ba55a1
JB
1533static int need_gnat_info (struct dwarf2_cu *);
1534
3e43a32a
MS
1535static struct type *die_descriptive_type (struct die_info *,
1536 struct dwarf2_cu *);
b4ba55a1
JB
1537
1538static void set_descriptive_type (struct type *, struct die_info *,
1539 struct dwarf2_cu *);
1540
e7c27a73
DJ
1541static struct type *die_containing_type (struct die_info *,
1542 struct dwarf2_cu *);
c906108c 1543
ff39bb5e 1544static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1545 struct dwarf2_cu *);
c906108c 1546
f792889a 1547static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1548
673bfd45
DE
1549static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1550
0d5cff50 1551static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1552
6e70227d 1553static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1554 const char *suffix, int physname,
1555 struct dwarf2_cu *cu);
63d06c5c 1556
e7c27a73 1557static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1558
348e048f
DE
1559static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1560
e7c27a73 1561static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1562
e7c27a73 1563static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1564
96408a79
SA
1565static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1566
71a3c369
TT
1567static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1568
ff013f42 1569static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1570 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1571
3a2b436a 1572/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1573 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1574enum pc_bounds_kind
1575{
e385593e 1576 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1577 PC_BOUNDS_NOT_PRESENT,
1578
e385593e
JK
1579 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1580 were present but they do not form a valid range of PC addresses. */
1581 PC_BOUNDS_INVALID,
1582
3a2b436a
JK
1583 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1584 PC_BOUNDS_RANGES,
1585
1586 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1587 PC_BOUNDS_HIGH_LOW,
1588};
1589
1590static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1591 CORE_ADDR *, CORE_ADDR *,
1592 struct dwarf2_cu *,
891813be 1593 dwarf2_psymtab *);
c906108c 1594
fae299cd
DC
1595static void get_scope_pc_bounds (struct die_info *,
1596 CORE_ADDR *, CORE_ADDR *,
1597 struct dwarf2_cu *);
1598
801e3a5b
JB
1599static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1600 CORE_ADDR, struct dwarf2_cu *);
1601
a14ed312 1602static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1603 struct dwarf2_cu *);
c906108c 1604
a14ed312 1605static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1606 struct type *, struct dwarf2_cu *);
c906108c 1607
a14ed312 1608static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1609 struct die_info *, struct type *,
e7c27a73 1610 struct dwarf2_cu *);
c906108c 1611
a14ed312 1612static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1613 struct type *,
1614 struct dwarf2_cu *);
c906108c 1615
134d01f1 1616static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1617
e7c27a73 1618static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1619
e7c27a73 1620static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1621
5d7cb8df
JK
1622static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1623
804d2729 1624static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1625
27aa8d6a
SW
1626static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1627
74921315
KS
1628static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1629
f55ee35c
JK
1630static struct type *read_module_type (struct die_info *die,
1631 struct dwarf2_cu *cu);
1632
38d518c9 1633static const char *namespace_name (struct die_info *die,
e142c38c 1634 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1635
134d01f1 1636static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1637
e7c27a73 1638static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1639
6e70227d 1640static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1641 struct dwarf2_cu *);
1642
bf6af496 1643static struct die_info *read_die_and_siblings_1
d521ce57 1644 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1645 struct die_info *);
639d11d3 1646
dee91e82 1647static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1648 const gdb_byte *info_ptr,
1649 const gdb_byte **new_info_ptr,
639d11d3
DC
1650 struct die_info *parent);
1651
d521ce57
TT
1652static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1653 struct die_info **, const gdb_byte *,
1654 int *, int);
3019eac3 1655
d521ce57
TT
1656static const gdb_byte *read_full_die (const struct die_reader_specs *,
1657 struct die_info **, const gdb_byte *,
1658 int *);
93311388 1659
e7c27a73 1660static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1661
15d034d0
TT
1662static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1663 struct obstack *);
71c25dea 1664
15d034d0 1665static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1666
15d034d0 1667static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1668 struct die_info *die,
1669 struct dwarf2_cu *cu);
1670
ca69b9e6
DE
1671static const char *dwarf2_physname (const char *name, struct die_info *die,
1672 struct dwarf2_cu *cu);
1673
e142c38c 1674static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1675 struct dwarf2_cu **);
9219021c 1676
f39c6ffd 1677static const char *dwarf_tag_name (unsigned int);
c906108c 1678
f39c6ffd 1679static const char *dwarf_attr_name (unsigned int);
c906108c 1680
a084a2a6
AT
1681static const char *dwarf_unit_type_name (int unit_type);
1682
f39c6ffd 1683static const char *dwarf_form_name (unsigned int);
c906108c 1684
a121b7c1 1685static const char *dwarf_bool_name (unsigned int);
c906108c 1686
f39c6ffd 1687static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1688
f9aca02d 1689static struct die_info *sibling_die (struct die_info *);
c906108c 1690
d97bc12b
DE
1691static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1692
1693static void dump_die_for_error (struct die_info *);
1694
1695static void dump_die_1 (struct ui_file *, int level, int max_level,
1696 struct die_info *);
c906108c 1697
d97bc12b 1698/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1699
51545339 1700static void store_in_ref_table (struct die_info *,
10b3939b 1701 struct dwarf2_cu *);
c906108c 1702
ff39bb5e 1703static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1704
ff39bb5e 1705static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1706
348e048f 1707static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1708 const struct attribute *,
348e048f
DE
1709 struct dwarf2_cu **);
1710
10b3939b 1711static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1712 const struct attribute *,
f2f0e013 1713 struct dwarf2_cu **);
c906108c 1714
348e048f 1715static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1716 const struct attribute *,
348e048f
DE
1717 struct dwarf2_cu **);
1718
ac9ec31b
DE
1719static struct type *get_signatured_type (struct die_info *, ULONGEST,
1720 struct dwarf2_cu *);
1721
1722static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1723 const struct attribute *,
ac9ec31b
DE
1724 struct dwarf2_cu *);
1725
e5fe5e75 1726static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1727
52dc124a 1728static void read_signatured_type (struct signatured_type *);
348e048f 1729
63e43d3a
PMR
1730static int attr_to_dynamic_prop (const struct attribute *attr,
1731 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1732 struct dynamic_prop *prop, struct type *type);
63e43d3a 1733
c906108c
SS
1734/* memory allocation interface */
1735
7b5a2f43 1736static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1737
b60c80d6 1738static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1739
43f3e411 1740static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1741
8cf6f0b1
TT
1742static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1743 struct dwarf2_loclist_baton *baton,
ff39bb5e 1744 const struct attribute *attr);
8cf6f0b1 1745
ff39bb5e 1746static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1747 struct symbol *sym,
f1e6e072
TT
1748 struct dwarf2_cu *cu,
1749 int is_block);
4c2df51b 1750
d521ce57
TT
1751static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1752 const gdb_byte *info_ptr,
1753 struct abbrev_info *abbrev);
4bb7a0a7 1754
72bf9492
DJ
1755static hashval_t partial_die_hash (const void *item);
1756
1757static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1758
ae038cb0 1759static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1760 (sect_offset sect_off, unsigned int offset_in_dwz,
1761 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1762
9816fde3 1763static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1764 struct die_info *comp_unit_die,
1765 enum language pretend_language);
93311388 1766
ed2dc618 1767static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1768
dee91e82 1769static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1770
f792889a
DJ
1771static struct type *set_die_type (struct die_info *, struct type *,
1772 struct dwarf2_cu *);
1c379e20 1773
ed2dc618 1774static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1775
ed2dc618 1776static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1777
58f0c718 1778static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1779 enum language);
10b3939b 1780
95554aad
TT
1781static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1782 enum language);
10b3939b 1783
f4dc4d17
DE
1784static void process_full_type_unit (struct dwarf2_per_cu_data *,
1785 enum language);
1786
10b3939b
DJ
1787static void dwarf2_add_dependence (struct dwarf2_cu *,
1788 struct dwarf2_per_cu_data *);
1789
ae038cb0
DJ
1790static void dwarf2_mark (struct dwarf2_cu *);
1791
1792static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1793
b64f50a1 1794static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1795 struct dwarf2_per_cu_data *);
673bfd45 1796
f792889a 1797static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1798
95554aad
TT
1799static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1800 enum language pretend_language);
1801
ed2dc618 1802static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1803
9a49df9d
AB
1804static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1805static struct type *dwarf2_per_cu_addr_sized_int_type
1806 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1807static struct type *dwarf2_per_cu_int_type
1808 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1809 bool unsigned_p);
9a49df9d 1810
b303c6f6
AB
1811/* Class, the destructor of which frees all allocated queue entries. This
1812 will only have work to do if an error was thrown while processing the
1813 dwarf. If no error was thrown then the queue entries should have all
1814 been processed, and freed, as we went along. */
1815
1816class dwarf2_queue_guard
1817{
1818public:
1819 dwarf2_queue_guard () = default;
1820
1821 /* Free any entries remaining on the queue. There should only be
1822 entries left if we hit an error while processing the dwarf. */
1823 ~dwarf2_queue_guard ()
1824 {
1825 struct dwarf2_queue_item *item, *last;
1826
1827 item = dwarf2_queue;
1828 while (item)
1829 {
1830 /* Anything still marked queued is likely to be in an
1831 inconsistent state, so discard it. */
1832 if (item->per_cu->queued)
1833 {
1834 if (item->per_cu->cu != NULL)
1835 free_one_cached_comp_unit (item->per_cu);
1836 item->per_cu->queued = 0;
1837 }
1838
1839 last = item;
1840 item = item->next;
1841 xfree (last);
1842 }
1843
1844 dwarf2_queue = dwarf2_queue_tail = NULL;
1845 }
1846};
1847
d721ba37
PA
1848/* The return type of find_file_and_directory. Note, the enclosed
1849 string pointers are only valid while this object is valid. */
1850
1851struct file_and_directory
1852{
1853 /* The filename. This is never NULL. */
1854 const char *name;
1855
1856 /* The compilation directory. NULL if not known. If we needed to
1857 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1858 points directly to the DW_AT_comp_dir string attribute owned by
1859 the obstack that owns the DIE. */
1860 const char *comp_dir;
1861
1862 /* If we needed to build a new string for comp_dir, this is what
1863 owns the storage. */
1864 std::string comp_dir_storage;
1865};
1866
1867static file_and_directory find_file_and_directory (struct die_info *die,
1868 struct dwarf2_cu *cu);
9291a0cd
TT
1869
1870static char *file_full_name (int file, struct line_header *lh,
1871 const char *comp_dir);
1872
43988095
JK
1873/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1874enum class rcuh_kind { COMPILE, TYPE };
1875
d521ce57 1876static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1877 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1878 struct comp_unit_head *header,
36586728 1879 struct dwarf2_section_info *section,
d521ce57 1880 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1881 rcuh_kind section_kind);
36586728 1882
673bfd45 1883static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1884
3019eac3
DE
1885static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1886
57d63ce2 1887static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1888 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1889 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1890 ULONGEST signature, int is_debug_types);
a2ce51a0 1891
ed2dc618
SM
1892static struct dwp_file *get_dwp_file
1893 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1894
3019eac3 1895static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1896 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1897
1898static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1899 (struct signatured_type *, const char *, const char *);
3019eac3 1900
89e63ee4
DE
1901static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1902
263db9a1
TT
1903/* A unique pointer to a dwo_file. */
1904
51ac9db5 1905typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1906
ed2dc618 1907static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1908
1b80a9fa 1909static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1910
1911static void free_line_header_voidp (void *arg);
4390d890
DE
1912\f
1913/* Various complaints about symbol reading that don't abort the process. */
1914
1915static void
1916dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1917{
b98664d3 1918 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1919}
1920
1921static void
1922dwarf2_debug_line_missing_file_complaint (void)
1923{
b98664d3 1924 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1925}
1926
1927static void
1928dwarf2_debug_line_missing_end_sequence_complaint (void)
1929{
b98664d3 1930 complaint (_(".debug_line section has line "
4390d890
DE
1931 "program sequence without an end"));
1932}
1933
1934static void
1935dwarf2_complex_location_expr_complaint (void)
1936{
b98664d3 1937 complaint (_("location expression too complex"));
4390d890
DE
1938}
1939
1940static void
1941dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1942 int arg3)
1943{
b98664d3 1944 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1945 arg1, arg2, arg3);
1946}
1947
1948static void
1949dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1950{
b98664d3 1951 complaint (_("debug info runs off end of %s section"
4390d890 1952 " [in module %s]"),
96b79293
TT
1953 section->get_name (),
1954 section->get_file_name ());
4390d890 1955}
1b80a9fa 1956
4390d890
DE
1957static void
1958dwarf2_macro_malformed_definition_complaint (const char *arg1)
1959{
b98664d3 1960 complaint (_("macro debug info contains a "
4390d890
DE
1961 "malformed macro definition:\n`%s'"),
1962 arg1);
1963}
1964
1965static void
1966dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1967{
b98664d3 1968 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1969 arg1, arg2);
1970}
527f3840
JK
1971
1972/* Hash function for line_header_hash. */
1973
1974static hashval_t
1975line_header_hash (const struct line_header *ofs)
1976{
9c541725 1977 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1978}
1979
1980/* Hash function for htab_create_alloc_ex for line_header_hash. */
1981
1982static hashval_t
1983line_header_hash_voidp (const void *item)
1984{
9a3c8263 1985 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1986
1987 return line_header_hash (ofs);
1988}
1989
1990/* Equality function for line_header_hash. */
1991
1992static int
1993line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1994{
9a3c8263
SM
1995 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1996 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1997
9c541725 1998 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1999 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2000}
2001
4390d890 2002\f
9291a0cd 2003
330cdd98
PA
2004/* See declaration. */
2005
2006dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2007 const dwarf2_debug_sections *names,
2008 bool can_copy_)
2009 : objfile (objfile_),
2010 can_copy (can_copy_)
330cdd98
PA
2011{
2012 if (names == NULL)
2013 names = &dwarf2_elf_names;
2014
2015 bfd *obfd = objfile->obfd;
2016
2017 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2018 locate_sections (obfd, sec, *names);
2019}
2020
2021dwarf2_per_objfile::~dwarf2_per_objfile ()
2022{
2023 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2024 free_cached_comp_units ();
2025
2026 if (quick_file_names_table)
2027 htab_delete (quick_file_names_table);
2028
2029 if (line_header_hash)
2030 htab_delete (line_header_hash);
2031
b76e467d 2032 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2033 per_cu->imported_symtabs_free ();
fc8e7e75 2034
b2bdb8cf 2035 for (signatured_type *sig_type : all_type_units)
ae640021 2036 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2037
330cdd98
PA
2038 /* Everything else should be on the objfile obstack. */
2039}
2040
2041/* See declaration. */
2042
2043void
2044dwarf2_per_objfile::free_cached_comp_units ()
2045{
2046 dwarf2_per_cu_data *per_cu = read_in_chain;
2047 dwarf2_per_cu_data **last_chain = &read_in_chain;
2048 while (per_cu != NULL)
2049 {
2050 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2051
fcd3b13d 2052 delete per_cu->cu;
330cdd98
PA
2053 *last_chain = next_cu;
2054 per_cu = next_cu;
2055 }
2056}
2057
11ed8cad
TT
2058/* A helper class that calls free_cached_comp_units on
2059 destruction. */
2060
2061class free_cached_comp_units
2062{
2063public:
2064
2065 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2066 : m_per_objfile (per_objfile)
2067 {
2068 }
2069
2070 ~free_cached_comp_units ()
2071 {
2072 m_per_objfile->free_cached_comp_units ();
2073 }
2074
2075 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2076
2077private:
2078
2079 dwarf2_per_objfile *m_per_objfile;
2080};
2081
c906108c 2082/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2083 information and return true if we have enough to do something.
2084 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2085 ELF names are used. CAN_COPY is true for formats where symbol
2086 interposition is possible and so symbol values must follow copy
2087 relocation rules. */
c906108c
SS
2088
2089int
251d32d9 2090dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2091 const struct dwarf2_debug_sections *names,
2092 bool can_copy)
c906108c 2093{
97cbe998
SDJ
2094 if (objfile->flags & OBJF_READNEVER)
2095 return 0;
2096
ed2dc618
SM
2097 struct dwarf2_per_objfile *dwarf2_per_objfile
2098 = get_dwarf2_per_objfile (objfile);
2099
2100 if (dwarf2_per_objfile == NULL)
5bfd760d 2101 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2102 names,
2103 can_copy);
5bfd760d 2104
73869dc2 2105 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2106 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2107 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2108 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2109}
2110
251d32d9
TG
2111/* When loading sections, we look either for uncompressed section or for
2112 compressed section names. */
233a11ab
CS
2113
2114static int
251d32d9
TG
2115section_is_p (const char *section_name,
2116 const struct dwarf2_section_names *names)
233a11ab 2117{
251d32d9
TG
2118 if (names->normal != NULL
2119 && strcmp (section_name, names->normal) == 0)
2120 return 1;
2121 if (names->compressed != NULL
2122 && strcmp (section_name, names->compressed) == 0)
2123 return 1;
2124 return 0;
233a11ab
CS
2125}
2126
330cdd98 2127/* See declaration. */
c906108c 2128
330cdd98
PA
2129void
2130dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2131 const dwarf2_debug_sections &names)
c906108c 2132{
fd361982 2133 flagword aflag = bfd_section_flags (sectp);
251d32d9 2134
dc7650b8
JK
2135 if ((aflag & SEC_HAS_CONTENTS) == 0)
2136 {
2137 }
950b7495
KS
2138 else if (elf_section_data (sectp)->this_hdr.sh_size
2139 > bfd_get_file_size (abfd))
2140 {
2141 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2142 warning (_("Discarding section %s which has a section size (%s"
2143 ") larger than the file size [in module %s]"),
2144 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2145 bfd_get_filename (abfd));
2146 }
330cdd98 2147 else if (section_is_p (sectp->name, &names.info))
c906108c 2148 {
330cdd98 2149 this->info.s.section = sectp;
fd361982 2150 this->info.size = bfd_section_size (sectp);
c906108c 2151 }
330cdd98 2152 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2153 {
330cdd98 2154 this->abbrev.s.section = sectp;
fd361982 2155 this->abbrev.size = bfd_section_size (sectp);
c906108c 2156 }
330cdd98 2157 else if (section_is_p (sectp->name, &names.line))
c906108c 2158 {
330cdd98 2159 this->line.s.section = sectp;
fd361982 2160 this->line.size = bfd_section_size (sectp);
c906108c 2161 }
330cdd98 2162 else if (section_is_p (sectp->name, &names.loc))
c906108c 2163 {
330cdd98 2164 this->loc.s.section = sectp;
fd361982 2165 this->loc.size = bfd_section_size (sectp);
c906108c 2166 }
330cdd98 2167 else if (section_is_p (sectp->name, &names.loclists))
43988095 2168 {
330cdd98 2169 this->loclists.s.section = sectp;
fd361982 2170 this->loclists.size = bfd_section_size (sectp);
43988095 2171 }
330cdd98 2172 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2173 {
330cdd98 2174 this->macinfo.s.section = sectp;
fd361982 2175 this->macinfo.size = bfd_section_size (sectp);
c906108c 2176 }
330cdd98 2177 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2178 {
330cdd98 2179 this->macro.s.section = sectp;
fd361982 2180 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2181 }
330cdd98 2182 else if (section_is_p (sectp->name, &names.str))
c906108c 2183 {
330cdd98 2184 this->str.s.section = sectp;
fd361982 2185 this->str.size = bfd_section_size (sectp);
c906108c 2186 }
18a8505e
AT
2187 else if (section_is_p (sectp->name, &names.str_offsets))
2188 {
2189 this->str_offsets.s.section = sectp;
2190 this->str_offsets.size = bfd_section_size (sectp);
2191 }
330cdd98 2192 else if (section_is_p (sectp->name, &names.line_str))
43988095 2193 {
330cdd98 2194 this->line_str.s.section = sectp;
fd361982 2195 this->line_str.size = bfd_section_size (sectp);
43988095 2196 }
330cdd98 2197 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2198 {
330cdd98 2199 this->addr.s.section = sectp;
fd361982 2200 this->addr.size = bfd_section_size (sectp);
3019eac3 2201 }
330cdd98 2202 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2203 {
330cdd98 2204 this->frame.s.section = sectp;
fd361982 2205 this->frame.size = bfd_section_size (sectp);
b6af0555 2206 }
330cdd98 2207 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2208 {
330cdd98 2209 this->eh_frame.s.section = sectp;
fd361982 2210 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2211 }
330cdd98 2212 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2213 {
330cdd98 2214 this->ranges.s.section = sectp;
fd361982 2215 this->ranges.size = bfd_section_size (sectp);
af34e669 2216 }
330cdd98 2217 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2218 {
330cdd98 2219 this->rnglists.s.section = sectp;
fd361982 2220 this->rnglists.size = bfd_section_size (sectp);
43988095 2221 }
330cdd98 2222 else if (section_is_p (sectp->name, &names.types))
348e048f 2223 {
8b70b953
TT
2224 struct dwarf2_section_info type_section;
2225
2226 memset (&type_section, 0, sizeof (type_section));
049412e3 2227 type_section.s.section = sectp;
fd361982 2228 type_section.size = bfd_section_size (sectp);
8b70b953 2229
fd5866f6 2230 this->types.push_back (type_section);
348e048f 2231 }
330cdd98 2232 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2233 {
330cdd98 2234 this->gdb_index.s.section = sectp;
fd361982 2235 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2236 }
927aa2e7
JK
2237 else if (section_is_p (sectp->name, &names.debug_names))
2238 {
2239 this->debug_names.s.section = sectp;
fd361982 2240 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2241 }
2242 else if (section_is_p (sectp->name, &names.debug_aranges))
2243 {
2244 this->debug_aranges.s.section = sectp;
fd361982 2245 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2246 }
dce234bc 2247
fd361982
AM
2248 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2249 && bfd_section_vma (sectp) == 0)
330cdd98 2250 this->has_section_at_zero = true;
c906108c
SS
2251}
2252
9e0ac564
TT
2253/* A helper function that returns the size of a section in a safe way.
2254 If you are positive that the section has been read before using the
2255 size, then it is safe to refer to the dwarf2_section_info object's
2256 "size" field directly. In other cases, you must call this
2257 function, because for compressed sections the size field is not set
2258 correctly until the section has been read. */
2259
2260static bfd_size_type
2261dwarf2_section_size (struct objfile *objfile,
2262 struct dwarf2_section_info *info)
2263{
2264 if (!info->readin)
96b79293 2265 info->read (objfile);
9e0ac564
TT
2266 return info->size;
2267}
2268
dce234bc 2269/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2270 SECTION_NAME. */
af34e669 2271
dce234bc 2272void
3017a003
TG
2273dwarf2_get_section_info (struct objfile *objfile,
2274 enum dwarf2_section_enum sect,
d521ce57 2275 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2276 bfd_size_type *sizep)
2277{
5bfd760d 2278 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2279 struct dwarf2_section_info *info;
a3b2a86b
TT
2280
2281 /* We may see an objfile without any DWARF, in which case we just
2282 return nothing. */
2283 if (data == NULL)
2284 {
2285 *sectp = NULL;
2286 *bufp = NULL;
2287 *sizep = 0;
2288 return;
2289 }
3017a003
TG
2290 switch (sect)
2291 {
2292 case DWARF2_DEBUG_FRAME:
2293 info = &data->frame;
2294 break;
2295 case DWARF2_EH_FRAME:
2296 info = &data->eh_frame;
2297 break;
2298 default:
2299 gdb_assert_not_reached ("unexpected section");
2300 }
dce234bc 2301
96b79293 2302 info->read (objfile);
dce234bc 2303
96b79293 2304 *sectp = info->get_bfd_section ();
dce234bc
PP
2305 *bufp = info->buffer;
2306 *sizep = info->size;
2307}
2308
36586728
TT
2309/* A helper function to find the sections for a .dwz file. */
2310
2311static void
2312locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2313{
9a3c8263 2314 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2315
2316 /* Note that we only support the standard ELF names, because .dwz
2317 is ELF-only (at the time of writing). */
2318 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2319 {
049412e3 2320 dwz_file->abbrev.s.section = sectp;
fd361982 2321 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2322 }
2323 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2324 {
049412e3 2325 dwz_file->info.s.section = sectp;
fd361982 2326 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2327 }
2328 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2329 {
049412e3 2330 dwz_file->str.s.section = sectp;
fd361982 2331 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2332 }
2333 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2334 {
049412e3 2335 dwz_file->line.s.section = sectp;
fd361982 2336 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2337 }
2338 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2339 {
049412e3 2340 dwz_file->macro.s.section = sectp;
fd361982 2341 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2342 }
2ec9a5e0
TT
2343 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2344 {
049412e3 2345 dwz_file->gdb_index.s.section = sectp;
fd361982 2346 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2347 }
927aa2e7
JK
2348 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2349 {
2350 dwz_file->debug_names.s.section = sectp;
fd361982 2351 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2352 }
36586728
TT
2353}
2354
c4973306 2355/* See dwarf2read.h. */
36586728 2356
c4973306 2357struct dwz_file *
ed2dc618 2358dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2359{
36586728 2360 const char *filename;
acd13123 2361 bfd_size_type buildid_len_arg;
dc294be5
TT
2362 size_t buildid_len;
2363 bfd_byte *buildid;
36586728
TT
2364
2365 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2366 return dwarf2_per_objfile->dwz_file.get ();
36586728 2367
4db1a1dc 2368 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2369 gdb::unique_xmalloc_ptr<char> data
2370 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2371 &buildid_len_arg, &buildid));
4db1a1dc
TT
2372 if (data == NULL)
2373 {
2374 if (bfd_get_error () == bfd_error_no_error)
2375 return NULL;
2376 error (_("could not read '.gnu_debugaltlink' section: %s"),
2377 bfd_errmsg (bfd_get_error ()));
2378 }
791afaa2
TT
2379
2380 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2381
acd13123
TT
2382 buildid_len = (size_t) buildid_len_arg;
2383
791afaa2 2384 filename = data.get ();
d721ba37
PA
2385
2386 std::string abs_storage;
36586728
TT
2387 if (!IS_ABSOLUTE_PATH (filename))
2388 {
14278e1f
TT
2389 gdb::unique_xmalloc_ptr<char> abs
2390 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2391
14278e1f 2392 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2393 filename = abs_storage.c_str ();
36586728
TT
2394 }
2395
dc294be5
TT
2396 /* First try the file name given in the section. If that doesn't
2397 work, try to use the build-id instead. */
192b62ce 2398 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2399 if (dwz_bfd != NULL)
36586728 2400 {
192b62ce 2401 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2402 dwz_bfd.reset (nullptr);
36586728
TT
2403 }
2404
dc294be5
TT
2405 if (dwz_bfd == NULL)
2406 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2407
2408 if (dwz_bfd == NULL)
2409 error (_("could not find '.gnu_debugaltlink' file for %s"),
2410 objfile_name (dwarf2_per_objfile->objfile));
2411
7ff8cb8c
TT
2412 std::unique_ptr<struct dwz_file> result
2413 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2414
7ff8cb8c
TT
2415 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2416 result.get ());
36586728 2417
7ff8cb8c
TT
2418 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2419 result->dwz_bfd.get ());
2420 dwarf2_per_objfile->dwz_file = std::move (result);
2421 return dwarf2_per_objfile->dwz_file.get ();
36586728 2422}
9291a0cd 2423\f
7b9f3c50
DE
2424/* DWARF quick_symbols_functions support. */
2425
2426/* TUs can share .debug_line entries, and there can be a lot more TUs than
2427 unique line tables, so we maintain a separate table of all .debug_line
2428 derived entries to support the sharing.
2429 All the quick functions need is the list of file names. We discard the
2430 line_header when we're done and don't need to record it here. */
2431struct quick_file_names
2432{
094b34ac
DE
2433 /* The data used to construct the hash key. */
2434 struct stmt_list_hash hash;
7b9f3c50
DE
2435
2436 /* The number of entries in file_names, real_names. */
2437 unsigned int num_file_names;
2438
2439 /* The file names from the line table, after being run through
2440 file_full_name. */
2441 const char **file_names;
2442
2443 /* The file names from the line table after being run through
2444 gdb_realpath. These are computed lazily. */
2445 const char **real_names;
2446};
2447
2448/* When using the index (and thus not using psymtabs), each CU has an
2449 object of this type. This is used to hold information needed by
2450 the various "quick" methods. */
2451struct dwarf2_per_cu_quick_data
2452{
2453 /* The file table. This can be NULL if there was no file table
2454 or it's currently not read in.
2455 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2456 struct quick_file_names *file_names;
2457
2458 /* The corresponding symbol table. This is NULL if symbols for this
2459 CU have not yet been read. */
43f3e411 2460 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2461
2462 /* A temporary mark bit used when iterating over all CUs in
2463 expand_symtabs_matching. */
2464 unsigned int mark : 1;
2465
2466 /* True if we've tried to read the file table and found there isn't one.
2467 There will be no point in trying to read it again next time. */
2468 unsigned int no_file_data : 1;
2469};
2470
094b34ac
DE
2471/* Utility hash function for a stmt_list_hash. */
2472
2473static hashval_t
2474hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2475{
2476 hashval_t v = 0;
2477
2478 if (stmt_list_hash->dwo_unit != NULL)
2479 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2480 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2481 return v;
2482}
2483
2484/* Utility equality function for a stmt_list_hash. */
2485
2486static int
2487eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2488 const struct stmt_list_hash *rhs)
2489{
2490 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2491 return 0;
2492 if (lhs->dwo_unit != NULL
2493 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2494 return 0;
2495
9c541725 2496 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2497}
2498
7b9f3c50
DE
2499/* Hash function for a quick_file_names. */
2500
2501static hashval_t
2502hash_file_name_entry (const void *e)
2503{
9a3c8263
SM
2504 const struct quick_file_names *file_data
2505 = (const struct quick_file_names *) e;
7b9f3c50 2506
094b34ac 2507 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2508}
2509
2510/* Equality function for a quick_file_names. */
2511
2512static int
2513eq_file_name_entry (const void *a, const void *b)
2514{
9a3c8263
SM
2515 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2516 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2517
094b34ac 2518 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2519}
2520
2521/* Delete function for a quick_file_names. */
2522
2523static void
2524delete_file_name_entry (void *e)
2525{
9a3c8263 2526 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2527 int i;
2528
2529 for (i = 0; i < file_data->num_file_names; ++i)
2530 {
2531 xfree ((void*) file_data->file_names[i]);
2532 if (file_data->real_names)
2533 xfree ((void*) file_data->real_names[i]);
2534 }
2535
2536 /* The space for the struct itself lives on objfile_obstack,
2537 so we don't free it here. */
2538}
2539
2540/* Create a quick_file_names hash table. */
2541
2542static htab_t
2543create_quick_file_names_table (unsigned int nr_initial_entries)
2544{
2545 return htab_create_alloc (nr_initial_entries,
2546 hash_file_name_entry, eq_file_name_entry,
2547 delete_file_name_entry, xcalloc, xfree);
2548}
9291a0cd 2549
918dd910
JK
2550/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2551 have to be created afterwards. You should call age_cached_comp_units after
2552 processing PER_CU->CU. dw2_setup must have been already called. */
2553
2554static void
58f0c718 2555load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2556{
3019eac3 2557 if (per_cu->is_debug_types)
e5fe5e75 2558 load_full_type_unit (per_cu);
918dd910 2559 else
58f0c718 2560 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2561
cc12ce38
DE
2562 if (per_cu->cu == NULL)
2563 return; /* Dummy CU. */
2dc860c0
DE
2564
2565 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2566}
2567
a0f42c21 2568/* Read in the symbols for PER_CU. */
2fdf6df6 2569
9291a0cd 2570static void
58f0c718 2571dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2572{
ed2dc618 2573 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2574
f4dc4d17
DE
2575 /* Skip type_unit_groups, reading the type units they contain
2576 is handled elsewhere. */
2577 if (IS_TYPE_UNIT_GROUP (per_cu))
2578 return;
2579
b303c6f6
AB
2580 /* The destructor of dwarf2_queue_guard frees any entries left on
2581 the queue. After this point we're guaranteed to leave this function
2582 with the dwarf queue empty. */
2583 dwarf2_queue_guard q_guard;
9291a0cd 2584
95554aad 2585 if (dwarf2_per_objfile->using_index
43f3e411 2586 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2587 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2588 {
2589 queue_comp_unit (per_cu, language_minimal);
58f0c718 2590 load_cu (per_cu, skip_partial);
89e63ee4
DE
2591
2592 /* If we just loaded a CU from a DWO, and we're working with an index
2593 that may badly handle TUs, load all the TUs in that DWO as well.
2594 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2595 if (!per_cu->is_debug_types
cc12ce38 2596 && per_cu->cu != NULL
89e63ee4
DE
2597 && per_cu->cu->dwo_unit != NULL
2598 && dwarf2_per_objfile->index_table != NULL
2599 && dwarf2_per_objfile->index_table->version <= 7
2600 /* DWP files aren't supported yet. */
ed2dc618 2601 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2602 queue_and_load_all_dwo_tus (per_cu);
95554aad 2603 }
9291a0cd 2604
ed2dc618 2605 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2606
2607 /* Age the cache, releasing compilation units that have not
2608 been used recently. */
ed2dc618 2609 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2610}
2611
2612/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2613 the objfile from which this CU came. Returns the resulting symbol
2614 table. */
2fdf6df6 2615
43f3e411 2616static struct compunit_symtab *
58f0c718 2617dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2618{
ed2dc618
SM
2619 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2620
95554aad 2621 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2622 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2623 {
11ed8cad 2624 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2625 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2626 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2627 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2628 }
f194fefb 2629
43f3e411 2630 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2631}
2632
ff4c9fec 2633/* See declaration. */
f4dc4d17 2634
ff4c9fec
SM
2635dwarf2_per_cu_data *
2636dwarf2_per_objfile::get_cutu (int index)
2637{
b76e467d 2638 if (index >= this->all_comp_units.size ())
ff4c9fec 2639 {
b76e467d 2640 index -= this->all_comp_units.size ();
b2bdb8cf 2641 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2642 return &this->all_type_units[index]->per_cu;
2643 }
f4dc4d17 2644
ff4c9fec
SM
2645 return this->all_comp_units[index];
2646}
f4dc4d17 2647
ff4c9fec 2648/* See declaration. */
2fdf6df6 2649
ff4c9fec
SM
2650dwarf2_per_cu_data *
2651dwarf2_per_objfile::get_cu (int index)
1fd400ff 2652{
b76e467d 2653 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2654
ff4c9fec 2655 return this->all_comp_units[index];
f4dc4d17
DE
2656}
2657
ff4c9fec 2658/* See declaration. */
f4dc4d17 2659
ff4c9fec
SM
2660signatured_type *
2661dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2662{
b2bdb8cf 2663 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2664
ff4c9fec 2665 return this->all_type_units[index];
1fd400ff
TT
2666}
2667
4b514bc8
JK
2668/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2669 objfile_obstack, and constructed with the specified field
2670 values. */
2671
2672static dwarf2_per_cu_data *
ed2dc618 2673create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2674 struct dwarf2_section_info *section,
2675 int is_dwz,
2676 sect_offset sect_off, ULONGEST length)
2677{
ed2dc618 2678 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2679 dwarf2_per_cu_data *the_cu
2680 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2681 struct dwarf2_per_cu_data);
2682 the_cu->sect_off = sect_off;
2683 the_cu->length = length;
e3b94546 2684 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2685 the_cu->section = section;
2686 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2687 struct dwarf2_per_cu_quick_data);
2688 the_cu->is_dwz = is_dwz;
2689 return the_cu;
2690}
2691
2ec9a5e0
TT
2692/* A helper for create_cus_from_index that handles a given list of
2693 CUs. */
2fdf6df6 2694
74a0d9f6 2695static void
12359b5e 2696create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2697 const gdb_byte *cu_list, offset_type n_elements,
2698 struct dwarf2_section_info *section,
b76e467d 2699 int is_dwz)
9291a0cd 2700{
12359b5e 2701 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2702 {
74a0d9f6 2703 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2704
2705 sect_offset sect_off
2706 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2707 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2708 cu_list += 2 * 8;
2709
b76e467d 2710 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2711 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2712 sect_off, length);
b76e467d 2713 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2714 }
9291a0cd
TT
2715}
2716
2ec9a5e0 2717/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2718 the CU objects for this objfile. */
2ec9a5e0 2719
74a0d9f6 2720static void
12359b5e 2721create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2722 const gdb_byte *cu_list, offset_type cu_list_elements,
2723 const gdb_byte *dwz_list, offset_type dwz_elements)
2724{
b76e467d
SM
2725 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2726 dwarf2_per_objfile->all_comp_units.reserve
2727 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2728
12359b5e 2729 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2730 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2731
2732 if (dwz_elements == 0)
74a0d9f6 2733 return;
2ec9a5e0 2734
12359b5e
SM
2735 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2736 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2737 &dwz->info, 1);
2ec9a5e0
TT
2738}
2739
1fd400ff 2740/* Create the signatured type hash table from the index. */
673bfd45 2741
74a0d9f6 2742static void
12359b5e
SM
2743create_signatured_type_table_from_index
2744 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2745 struct dwarf2_section_info *section,
2746 const gdb_byte *bytes,
2747 offset_type elements)
1fd400ff 2748{
12359b5e 2749 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2750
b2bdb8cf
SM
2751 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2752 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2753
12359b5e 2754 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 2755
12359b5e 2756 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2757 {
52dc124a 2758 struct signatured_type *sig_type;
9c541725 2759 ULONGEST signature;
1fd400ff 2760 void **slot;
9c541725 2761 cu_offset type_offset_in_tu;
1fd400ff 2762
74a0d9f6 2763 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2764 sect_offset sect_off
2765 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2766 type_offset_in_tu
2767 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2768 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2769 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2770 bytes += 3 * 8;
2771
52dc124a 2772 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2773 struct signatured_type);
52dc124a 2774 sig_type->signature = signature;
9c541725 2775 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2776 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2777 sig_type->per_cu.section = section;
9c541725 2778 sig_type->per_cu.sect_off = sect_off;
e3b94546 2779 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2780 sig_type->per_cu.v.quick
1fd400ff
TT
2781 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2782 struct dwarf2_per_cu_quick_data);
2783
52dc124a
DE
2784 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2785 *slot = sig_type;
1fd400ff 2786
b2bdb8cf 2787 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2788 }
2789
673bfd45 2790 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2791}
2792
927aa2e7
JK
2793/* Create the signatured type hash table from .debug_names. */
2794
2795static void
2796create_signatured_type_table_from_debug_names
ed2dc618 2797 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2798 const mapped_debug_names &map,
2799 struct dwarf2_section_info *section,
2800 struct dwarf2_section_info *abbrev_section)
2801{
ed2dc618
SM
2802 struct objfile *objfile = dwarf2_per_objfile->objfile;
2803
96b79293
TT
2804 section->read (objfile);
2805 abbrev_section->read (objfile);
927aa2e7 2806
b2bdb8cf
SM
2807 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2808 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
2809
2810 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2811
2812 for (uint32_t i = 0; i < map.tu_count; ++i)
2813 {
2814 struct signatured_type *sig_type;
927aa2e7 2815 void **slot;
927aa2e7
JK
2816
2817 sect_offset sect_off
2818 = (sect_offset) (extract_unsigned_integer
2819 (map.tu_table_reordered + i * map.offset_size,
2820 map.offset_size,
2821 map.dwarf5_byte_order));
2822
2823 comp_unit_head cu_header;
ed2dc618
SM
2824 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2825 abbrev_section,
927aa2e7
JK
2826 section->buffer + to_underlying (sect_off),
2827 rcuh_kind::TYPE);
2828
2829 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2830 struct signatured_type);
2831 sig_type->signature = cu_header.signature;
2832 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2833 sig_type->per_cu.is_debug_types = 1;
2834 sig_type->per_cu.section = section;
2835 sig_type->per_cu.sect_off = sect_off;
e3b94546 2836 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2837 sig_type->per_cu.v.quick
2838 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2839 struct dwarf2_per_cu_quick_data);
2840
2841 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2842 *slot = sig_type;
2843
b2bdb8cf 2844 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2845 }
2846
2847 dwarf2_per_objfile->signatured_types = sig_types_hash;
2848}
2849
9291a0cd
TT
2850/* Read the address map data from the mapped index, and use it to
2851 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2852
9291a0cd 2853static void
ed2dc618
SM
2854create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2855 struct mapped_index *index)
9291a0cd 2856{
ed2dc618 2857 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2858 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2859 const gdb_byte *iter, *end;
9291a0cd 2860 struct addrmap *mutable_map;
9291a0cd
TT
2861 CORE_ADDR baseaddr;
2862
8268c778
PA
2863 auto_obstack temp_obstack;
2864
9291a0cd
TT
2865 mutable_map = addrmap_create_mutable (&temp_obstack);
2866
f00a2de2
PA
2867 iter = index->address_table.data ();
2868 end = iter + index->address_table.size ();
9291a0cd 2869
b3b3bada 2870 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2871
2872 while (iter < end)
2873 {
2874 ULONGEST hi, lo, cu_index;
2875 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2876 iter += 8;
2877 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2878 iter += 8;
2879 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2880 iter += 4;
f652bce2 2881
24a55014 2882 if (lo > hi)
f652bce2 2883 {
b98664d3 2884 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2885 hex_string (lo), hex_string (hi));
24a55014 2886 continue;
f652bce2 2887 }
24a55014 2888
b76e467d 2889 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2890 {
b98664d3 2891 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2892 (unsigned) cu_index);
24a55014 2893 continue;
f652bce2 2894 }
24a55014 2895
79748972
TT
2896 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2897 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2898 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2899 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2900 }
2901
d320c2b5 2902 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2903 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2904}
2905
927aa2e7
JK
2906/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2907 populate the objfile's psymtabs_addrmap. */
2908
2909static void
ed2dc618 2910create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2911 struct dwarf2_section_info *section)
2912{
ed2dc618 2913 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2914 bfd *abfd = objfile->obfd;
2915 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2916 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2917
2918 auto_obstack temp_obstack;
2919 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2920
2921 std::unordered_map<sect_offset,
2922 dwarf2_per_cu_data *,
2923 gdb::hash_enum<sect_offset>>
2924 debug_info_offset_to_per_cu;
b76e467d 2925 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2926 {
927aa2e7
JK
2927 const auto insertpair
2928 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2929 if (!insertpair.second)
2930 {
2931 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2932 "debug_info_offset %s, ignoring .debug_aranges."),
2933 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2934 return;
2935 }
2936 }
2937
96b79293 2938 section->read (objfile);
927aa2e7
JK
2939
2940 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2941
2942 const gdb_byte *addr = section->buffer;
2943
2944 while (addr < section->buffer + section->size)
2945 {
2946 const gdb_byte *const entry_addr = addr;
2947 unsigned int bytes_read;
2948
2949 const LONGEST entry_length = read_initial_length (abfd, addr,
2950 &bytes_read);
2951 addr += bytes_read;
2952
2953 const gdb_byte *const entry_end = addr + entry_length;
2954 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2955 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2956 if (addr + entry_length > section->buffer + section->size)
2957 {
47e3f474 2958 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2959 "length %s exceeds section length %s, "
2960 "ignoring .debug_aranges."),
47e3f474
TV
2961 objfile_name (objfile),
2962 plongest (entry_addr - section->buffer),
927aa2e7
JK
2963 plongest (bytes_read + entry_length),
2964 pulongest (section->size));
2965 return;
2966 }
2967
2968 /* The version number. */
2969 const uint16_t version = read_2_bytes (abfd, addr);
2970 addr += 2;
2971 if (version != 2)
2972 {
47e3f474 2973 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2974 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2975 objfile_name (objfile),
2976 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2977 return;
2978 }
2979
2980 const uint64_t debug_info_offset
2981 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2982 addr += offset_size;
2983 const auto per_cu_it
2984 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2985 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2986 {
47e3f474 2987 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2988 "debug_info_offset %s does not exists, "
2989 "ignoring .debug_aranges."),
47e3f474
TV
2990 objfile_name (objfile),
2991 plongest (entry_addr - section->buffer),
927aa2e7
JK
2992 pulongest (debug_info_offset));
2993 return;
2994 }
2995 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2996
2997 const uint8_t address_size = *addr++;
2998 if (address_size < 1 || address_size > 8)
2999 {
47e3f474 3000 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3001 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3002 objfile_name (objfile),
3003 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3004 return;
3005 }
3006
3007 const uint8_t segment_selector_size = *addr++;
3008 if (segment_selector_size != 0)
3009 {
47e3f474 3010 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3011 "segment_selector_size %u is not supported, "
3012 "ignoring .debug_aranges."),
47e3f474
TV
3013 objfile_name (objfile),
3014 plongest (entry_addr - section->buffer),
927aa2e7
JK
3015 segment_selector_size);
3016 return;
3017 }
3018
3019 /* Must pad to an alignment boundary that is twice the address
3020 size. It is undocumented by the DWARF standard but GCC does
3021 use it. */
3022 for (size_t padding = ((-(addr - section->buffer))
3023 & (2 * address_size - 1));
3024 padding > 0; padding--)
3025 if (*addr++ != 0)
3026 {
47e3f474 3027 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3028 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3029 objfile_name (objfile),
3030 plongest (entry_addr - section->buffer));
927aa2e7
JK
3031 return;
3032 }
3033
3034 for (;;)
3035 {
3036 if (addr + 2 * address_size > entry_end)
3037 {
47e3f474 3038 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3039 "address list is not properly terminated, "
3040 "ignoring .debug_aranges."),
47e3f474
TV
3041 objfile_name (objfile),
3042 plongest (entry_addr - section->buffer));
927aa2e7
JK
3043 return;
3044 }
3045 ULONGEST start = extract_unsigned_integer (addr, address_size,
3046 dwarf5_byte_order);
3047 addr += address_size;
3048 ULONGEST length = extract_unsigned_integer (addr, address_size,
3049 dwarf5_byte_order);
3050 addr += address_size;
3051 if (start == 0 && length == 0)
3052 break;
3053 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3054 {
3055 /* Symbol was eliminated due to a COMDAT group. */
3056 continue;
3057 }
3058 ULONGEST end = start + length;
79748972
TT
3059 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3060 - baseaddr);
3061 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3062 - baseaddr);
927aa2e7
JK
3063 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3064 }
3065 }
3066
d320c2b5 3067 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3068 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3069}
3070
9291a0cd
TT
3071/* Find a slot in the mapped index INDEX for the object named NAME.
3072 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3073 constant pool and return true. If NAME cannot be found, return
3074 false. */
2fdf6df6 3075
109483d9 3076static bool
9291a0cd
TT
3077find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3078 offset_type **vec_out)
3079{
0cf03b49 3080 offset_type hash;
9291a0cd 3081 offset_type slot, step;
559a7a62 3082 int (*cmp) (const char *, const char *);
9291a0cd 3083
791afaa2 3084 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3085 if (current_language->la_language == language_cplus
45280282
IB
3086 || current_language->la_language == language_fortran
3087 || current_language->la_language == language_d)
0cf03b49
JK
3088 {
3089 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3090 not contain any. */
a8719064 3091
72998fb3 3092 if (strchr (name, '(') != NULL)
0cf03b49 3093 {
109483d9 3094 without_params = cp_remove_params (name);
0cf03b49 3095
72998fb3 3096 if (without_params != NULL)
791afaa2 3097 name = without_params.get ();
0cf03b49
JK
3098 }
3099 }
3100
559a7a62 3101 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3102 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3103 simulate our NAME being searched is also lowercased. */
3104 hash = mapped_index_string_hash ((index->version == 4
3105 && case_sensitivity == case_sensitive_off
3106 ? 5 : index->version),
3107 name);
3108
f00a2de2
PA
3109 slot = hash & (index->symbol_table.size () - 1);
3110 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3111 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3112
3113 for (;;)
3114 {
9291a0cd 3115 const char *str;
f00a2de2
PA
3116
3117 const auto &bucket = index->symbol_table[slot];
3118 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3119 return false;
9291a0cd 3120
f00a2de2 3121 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3122 if (!cmp (name, str))
9291a0cd
TT
3123 {
3124 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3125 + MAYBE_SWAP (bucket.vec));
109483d9 3126 return true;
9291a0cd
TT
3127 }
3128
f00a2de2 3129 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3130 }
3131}
3132
4485a1c1
SM
3133/* A helper function that reads the .gdb_index from BUFFER and fills
3134 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3135 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3136 ok to use deprecated sections.
3137
3138 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3139 out parameters that are filled in with information about the CU and
3140 TU lists in the section.
3141
4485a1c1 3142 Returns true if all went well, false otherwise. */
2fdf6df6 3143
d33bc52e 3144static bool
4485a1c1
SM
3145read_gdb_index_from_buffer (struct objfile *objfile,
3146 const char *filename,
3147 bool deprecated_ok,
3148 gdb::array_view<const gdb_byte> buffer,
3149 struct mapped_index *map,
3150 const gdb_byte **cu_list,
3151 offset_type *cu_list_elements,
3152 const gdb_byte **types_list,
3153 offset_type *types_list_elements)
3154{
3155 const gdb_byte *addr = &buffer[0];
82430852 3156
9291a0cd 3157 /* Version check. */
4485a1c1 3158 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3159 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3160 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3161 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3162 indices. */
831adc1f 3163 if (version < 4)
481860b3
GB
3164 {
3165 static int warning_printed = 0;
3166 if (!warning_printed)
3167 {
3168 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3169 filename);
481860b3
GB
3170 warning_printed = 1;
3171 }
3172 return 0;
3173 }
3174 /* Index version 4 uses a different hash function than index version
3175 5 and later.
3176
3177 Versions earlier than 6 did not emit psymbols for inlined
3178 functions. Using these files will cause GDB not to be able to
3179 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3180 indices unless the user has done
3181 "set use-deprecated-index-sections on". */
2ec9a5e0 3182 if (version < 6 && !deprecated_ok)
481860b3
GB
3183 {
3184 static int warning_printed = 0;
3185 if (!warning_printed)
3186 {
e615022a
DE
3187 warning (_("\
3188Skipping deprecated .gdb_index section in %s.\n\
3189Do \"set use-deprecated-index-sections on\" before the file is read\n\
3190to use the section anyway."),
2ec9a5e0 3191 filename);
481860b3
GB
3192 warning_printed = 1;
3193 }
3194 return 0;
3195 }
796a7ff8 3196 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3197 of the TU (for symbols coming from TUs),
3198 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3199 Plus gold-generated indices can have duplicate entries for global symbols,
3200 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3201 These are just performance bugs, and we can't distinguish gdb-generated
3202 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3203
481860b3 3204 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3205 longer backward compatible. */
796a7ff8 3206 if (version > 8)
594e8718 3207 return 0;
9291a0cd 3208
559a7a62 3209 map->version = version;
9291a0cd 3210
4485a1c1 3211 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3212
4485a1c1 3213 int i = 0;
2ec9a5e0
TT
3214 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3215 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3216 / 8);
1fd400ff
TT
3217 ++i;
3218
2ec9a5e0
TT
3219 *types_list = addr + MAYBE_SWAP (metadata[i]);
3220 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3221 - MAYBE_SWAP (metadata[i]))
3222 / 8);
987d643c 3223 ++i;
1fd400ff 3224
f00a2de2
PA
3225 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3226 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3227 map->address_table
3228 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3229 ++i;
3230
f00a2de2
PA
3231 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3232 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3233 map->symbol_table
3234 = gdb::array_view<mapped_index::symbol_table_slot>
3235 ((mapped_index::symbol_table_slot *) symbol_table,
3236 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3237
f00a2de2 3238 ++i;
f9d83a0b 3239 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3240
2ec9a5e0
TT
3241 return 1;
3242}
3243
4485a1c1
SM
3244/* Callback types for dwarf2_read_gdb_index. */
3245
3246typedef gdb::function_view
3247 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3248 get_gdb_index_contents_ftype;
3249typedef gdb::function_view
3250 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3251 get_gdb_index_contents_dwz_ftype;
3252
927aa2e7 3253/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3254 elements of all the CUs and return 1. Otherwise, return 0. */
3255
3256static int
4485a1c1
SM
3257dwarf2_read_gdb_index
3258 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3259 get_gdb_index_contents_ftype get_gdb_index_contents,
3260 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3261{
2ec9a5e0
TT
3262 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3263 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3264 struct dwz_file *dwz;
12359b5e 3265 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3266
4485a1c1
SM
3267 gdb::array_view<const gdb_byte> main_index_contents
3268 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3269
3270 if (main_index_contents.empty ())
3271 return 0;
3272
3063847f 3273 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3274 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3275 use_deprecated_index_sections,
3276 main_index_contents, map.get (), &cu_list,
3277 &cu_list_elements, &types_list,
3278 &types_list_elements))
2ec9a5e0
TT
3279 return 0;
3280
0fefef59 3281 /* Don't use the index if it's empty. */
3063847f 3282 if (map->symbol_table.empty ())
0fefef59
DE
3283 return 0;
3284
2ec9a5e0
TT
3285 /* If there is a .dwz file, read it so we can get its CU list as
3286 well. */
ed2dc618 3287 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3288 if (dwz != NULL)
2ec9a5e0 3289 {
2ec9a5e0
TT
3290 struct mapped_index dwz_map;
3291 const gdb_byte *dwz_types_ignore;
3292 offset_type dwz_types_elements_ignore;
3293
4485a1c1
SM
3294 gdb::array_view<const gdb_byte> dwz_index_content
3295 = get_gdb_index_contents_dwz (objfile, dwz);
3296
3297 if (dwz_index_content.empty ())
3298 return 0;
3299
3300 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3301 bfd_get_filename (dwz->dwz_bfd.get ()),
3302 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3303 &dwz_list, &dwz_list_elements,
3304 &dwz_types_ignore,
3305 &dwz_types_elements_ignore))
2ec9a5e0
TT
3306 {
3307 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3308 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3309 return 0;
3310 }
3311 }
3312
12359b5e
SM
3313 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3314 dwz_list, dwz_list_elements);
1fd400ff 3315
8b70b953
TT
3316 if (types_list_elements)
3317 {
8b70b953
TT
3318 /* We can only handle a single .debug_types when we have an
3319 index. */
fd5866f6 3320 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3321 return 0;
3322
fd5866f6 3323 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3324
12359b5e
SM
3325 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3326 types_list, types_list_elements);
8b70b953 3327 }
9291a0cd 3328
3063847f 3329 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3330
3063847f 3331 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3332 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3333 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3334 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3335
3336 return 1;
3337}
3338
dee91e82 3339/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3340
dee91e82
DE
3341static void
3342dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3343 const gdb_byte *info_ptr,
dee91e82 3344 struct die_info *comp_unit_die,
c0ab21c2 3345 int has_children)
9291a0cd 3346{
dee91e82 3347 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3348 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3349 struct dwarf2_per_objfile *dwarf2_per_objfile
3350 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3351 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3352 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3353 struct attribute *attr;
7b9f3c50
DE
3354 void **slot;
3355 struct quick_file_names *qfn;
9291a0cd 3356
0186c6a7
DE
3357 gdb_assert (! this_cu->is_debug_types);
3358
07261596
TT
3359 /* Our callers never want to match partial units -- instead they
3360 will match the enclosing full CU. */
3361 if (comp_unit_die->tag == DW_TAG_partial_unit)
3362 {
3363 this_cu->v.quick->no_file_data = 1;
3364 return;
3365 }
3366
0186c6a7 3367 lh_cu = this_cu;
7b9f3c50 3368 slot = NULL;
dee91e82 3369
fff8551c 3370 line_header_up lh;
9c541725 3371 sect_offset line_offset {};
fff8551c 3372
dee91e82 3373 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3374 if (attr != nullptr)
9291a0cd 3375 {
7b9f3c50
DE
3376 struct quick_file_names find_entry;
3377
9c541725 3378 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3379
3380 /* We may have already read in this line header (TU line header sharing).
3381 If we have we're done. */
094b34ac 3382 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3383 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3384 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3385 &find_entry, INSERT);
3386 if (*slot != NULL)
3387 {
9a3c8263 3388 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3389 return;
7b9f3c50
DE
3390 }
3391
3019eac3 3392 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3393 }
3394 if (lh == NULL)
3395 {
094b34ac 3396 lh_cu->v.quick->no_file_data = 1;
dee91e82 3397 return;
9291a0cd
TT
3398 }
3399
8d749320 3400 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3401 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3402 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3403 gdb_assert (slot != NULL);
3404 *slot = qfn;
9291a0cd 3405
d721ba37 3406 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3407
aa391654
TT
3408 int offset = 0;
3409 if (strcmp (fnd.name, "<unknown>") != 0)
3410 ++offset;
3411
7ba99d21 3412 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3413 qfn->file_names =
aa391654
TT
3414 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3415 if (offset != 0)
3416 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3417 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3418 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3419 qfn->real_names = NULL;
9291a0cd 3420
094b34ac 3421 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3422}
3423
3424/* A helper for the "quick" functions which attempts to read the line
3425 table for THIS_CU. */
3426
3427static struct quick_file_names *
e4a48d9d 3428dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3429{
0186c6a7
DE
3430 /* This should never be called for TUs. */
3431 gdb_assert (! this_cu->is_debug_types);
3432 /* Nor type unit groups. */
3433 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3434
dee91e82
DE
3435 if (this_cu->v.quick->file_names != NULL)
3436 return this_cu->v.quick->file_names;
3437 /* If we know there is no line data, no point in looking again. */
3438 if (this_cu->v.quick->no_file_data)
3439 return NULL;
3440
c0ab21c2
TT
3441 cutu_reader reader (this_cu);
3442 if (!reader.dummy_p)
3443 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3444 reader.has_children);
dee91e82
DE
3445
3446 if (this_cu->v.quick->no_file_data)
3447 return NULL;
3448 return this_cu->v.quick->file_names;
9291a0cd
TT
3449}
3450
3451/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3452 real path for a given file name from the line table. */
2fdf6df6 3453
9291a0cd 3454static const char *
7b9f3c50
DE
3455dw2_get_real_path (struct objfile *objfile,
3456 struct quick_file_names *qfn, int index)
9291a0cd 3457{
7b9f3c50
DE
3458 if (qfn->real_names == NULL)
3459 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3460 qfn->num_file_names, const char *);
9291a0cd 3461
7b9f3c50 3462 if (qfn->real_names[index] == NULL)
14278e1f 3463 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3464
7b9f3c50 3465 return qfn->real_names[index];
9291a0cd
TT
3466}
3467
3468static struct symtab *
3469dw2_find_last_source_symtab (struct objfile *objfile)
3470{
ed2dc618
SM
3471 struct dwarf2_per_objfile *dwarf2_per_objfile
3472 = get_dwarf2_per_objfile (objfile);
b76e467d 3473 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3474 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3475
43f3e411
DE
3476 if (cust == NULL)
3477 return NULL;
ed2dc618 3478
43f3e411 3479 return compunit_primary_filetab (cust);
9291a0cd
TT
3480}
3481
7b9f3c50
DE
3482/* Traversal function for dw2_forget_cached_source_info. */
3483
3484static int
3485dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3486{
7b9f3c50 3487 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3488
7b9f3c50 3489 if (file_data->real_names)
9291a0cd 3490 {
7b9f3c50 3491 int i;
9291a0cd 3492
7b9f3c50 3493 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3494 {
7b9f3c50
DE
3495 xfree ((void*) file_data->real_names[i]);
3496 file_data->real_names[i] = NULL;
9291a0cd
TT
3497 }
3498 }
7b9f3c50
DE
3499
3500 return 1;
3501}
3502
3503static void
3504dw2_forget_cached_source_info (struct objfile *objfile)
3505{
ed2dc618
SM
3506 struct dwarf2_per_objfile *dwarf2_per_objfile
3507 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3508
3509 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3510 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3511}
3512
f8eba3c6
TT
3513/* Helper function for dw2_map_symtabs_matching_filename that expands
3514 the symtabs and calls the iterator. */
3515
3516static int
3517dw2_map_expand_apply (struct objfile *objfile,
3518 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3519 const char *name, const char *real_path,
14bc53a8 3520 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3521{
43f3e411 3522 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3523
3524 /* Don't visit already-expanded CUs. */
43f3e411 3525 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3526 return 0;
3527
3528 /* This may expand more than one symtab, and we want to iterate over
3529 all of them. */
58f0c718 3530 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3531
14bc53a8
PA
3532 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3533 last_made, callback);
f8eba3c6
TT
3534}
3535
3536/* Implementation of the map_symtabs_matching_filename method. */
3537
14bc53a8
PA
3538static bool
3539dw2_map_symtabs_matching_filename
3540 (struct objfile *objfile, const char *name, const char *real_path,
3541 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3542{
c011a4f4 3543 const char *name_basename = lbasename (name);
ed2dc618
SM
3544 struct dwarf2_per_objfile *dwarf2_per_objfile
3545 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3546
848e3e78
DE
3547 /* The rule is CUs specify all the files, including those used by
3548 any TU, so there's no need to scan TUs here. */
f4dc4d17 3549
b76e467d 3550 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3551 {
3d7bb9d9 3552 /* We only need to look at symtabs not already expanded. */
43f3e411 3553 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3554 continue;
3555
b76e467d 3556 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3557 if (file_data == NULL)
9291a0cd
TT
3558 continue;
3559
b76e467d 3560 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3561 {
7b9f3c50 3562 const char *this_name = file_data->file_names[j];
da235a7c 3563 const char *this_real_name;
9291a0cd 3564
af529f8f 3565 if (compare_filenames_for_search (this_name, name))
9291a0cd 3566 {
f5b95b50 3567 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3568 callback))
3569 return true;
288e77a7 3570 continue;
4aac40c8 3571 }
9291a0cd 3572
c011a4f4
DE
3573 /* Before we invoke realpath, which can get expensive when many
3574 files are involved, do a quick comparison of the basenames. */
3575 if (! basenames_may_differ
3576 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3577 continue;
3578
da235a7c
JK
3579 this_real_name = dw2_get_real_path (objfile, file_data, j);
3580 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3581 {
da235a7c 3582 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3583 callback))
3584 return true;
288e77a7 3585 continue;
da235a7c 3586 }
9291a0cd 3587
da235a7c
JK
3588 if (real_path != NULL)
3589 {
af529f8f
JK
3590 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3591 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3592 if (this_real_name != NULL
af529f8f 3593 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3594 {
f5b95b50 3595 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3596 callback))
3597 return true;
288e77a7 3598 continue;
9291a0cd
TT
3599 }
3600 }
3601 }
3602 }
3603
14bc53a8 3604 return false;
9291a0cd
TT
3605}
3606
da51c347
DE
3607/* Struct used to manage iterating over all CUs looking for a symbol. */
3608
3609struct dw2_symtab_iterator
9291a0cd 3610{
ed2dc618
SM
3611 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3612 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3613 /* If set, only look for symbols that match that block. Valid values are
3614 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3615 gdb::optional<block_enum> block_index;
da51c347
DE
3616 /* The kind of symbol we're looking for. */
3617 domain_enum domain;
3618 /* The list of CUs from the index entry of the symbol,
3619 or NULL if not found. */
3620 offset_type *vec;
3621 /* The next element in VEC to look at. */
3622 int next;
3623 /* The number of elements in VEC, or zero if there is no match. */
3624 int length;
8943b874
DE
3625 /* Have we seen a global version of the symbol?
3626 If so we can ignore all further global instances.
3627 This is to work around gold/15646, inefficient gold-generated
3628 indices. */
3629 int global_seen;
da51c347 3630};
9291a0cd 3631
2b79f376 3632/* Initialize the index symtab iterator ITER. */
2fdf6df6 3633
9291a0cd 3634static void
da51c347 3635dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3636 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3637 gdb::optional<block_enum> block_index,
da51c347
DE
3638 domain_enum domain,
3639 const char *name)
3640{
ed2dc618 3641 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3642 iter->block_index = block_index;
3643 iter->domain = domain;
3644 iter->next = 0;
8943b874 3645 iter->global_seen = 0;
da51c347 3646
3063847f 3647 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3648
3649 /* index is NULL if OBJF_READNOW. */
3650 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3651 iter->length = MAYBE_SWAP (*iter->vec);
3652 else
3653 {
3654 iter->vec = NULL;
3655 iter->length = 0;
3656 }
3657}
3658
3659/* Return the next matching CU or NULL if there are no more. */
3660
3661static struct dwarf2_per_cu_data *
3662dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3663{
ed2dc618
SM
3664 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3665
da51c347
DE
3666 for ( ; iter->next < iter->length; ++iter->next)
3667 {
3668 offset_type cu_index_and_attrs =
3669 MAYBE_SWAP (iter->vec[iter->next + 1]);
3670 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3671 gdb_index_symbol_kind symbol_kind =
3672 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3673 /* Only check the symbol attributes if they're present.
3674 Indices prior to version 7 don't record them,
3675 and indices >= 7 may elide them for certain symbols
3676 (gold does this). */
3677 int attrs_valid =
ed2dc618 3678 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3679 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3680
3190f0c6 3681 /* Don't crash on bad data. */
b76e467d 3682 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3683 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3684 {
b98664d3 3685 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3686 " [in module %s]"),
3687 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3688 continue;
3689 }
3690
ff4c9fec 3691 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3692
da51c347 3693 /* Skip if already read in. */
43f3e411 3694 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3695 continue;
3696
8943b874
DE
3697 /* Check static vs global. */
3698 if (attrs_valid)
3699 {
2b79f376
SM
3700 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3701
3702 if (iter->block_index.has_value ())
3703 {
3704 bool want_static = *iter->block_index == STATIC_BLOCK;
3705
3706 if (is_static != want_static)
3707 continue;
3708 }
3709
8943b874
DE
3710 /* Work around gold/15646. */
3711 if (!is_static && iter->global_seen)
3712 continue;
3713 if (!is_static)
3714 iter->global_seen = 1;
3715 }
da51c347
DE
3716
3717 /* Only check the symbol's kind if it has one. */
3718 if (attrs_valid)
3719 {
3720 switch (iter->domain)
3721 {
3722 case VAR_DOMAIN:
3723 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3724 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3725 /* Some types are also in VAR_DOMAIN. */
3726 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3727 continue;
3728 break;
3729 case STRUCT_DOMAIN:
3730 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3731 continue;
3732 break;
3733 case LABEL_DOMAIN:
3734 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3735 continue;
3736 break;
59c35742
AB
3737 case MODULE_DOMAIN:
3738 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3739 continue;
3740 break;
da51c347
DE
3741 default:
3742 break;
3743 }
3744 }
3745
3746 ++iter->next;
3747 return per_cu;
3748 }
3749
3750 return NULL;
3751}
3752
43f3e411 3753static struct compunit_symtab *
c7f839cb 3754dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3755 const char *name, domain_enum domain)
9291a0cd 3756{
43f3e411 3757 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3758 struct dwarf2_per_objfile *dwarf2_per_objfile
3759 = get_dwarf2_per_objfile (objfile);
9291a0cd 3760
b5ec771e
PA
3761 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3762
ed2dc618
SM
3763 struct dw2_symtab_iterator iter;
3764 struct dwarf2_per_cu_data *per_cu;
da51c347 3765
2b79f376 3766 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3767
ed2dc618
SM
3768 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3769 {
3770 struct symbol *sym, *with_opaque = NULL;
58f0c718 3771 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3772 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3773 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3774
ed2dc618
SM
3775 sym = block_find_symbol (block, name, domain,
3776 block_find_non_opaque_type_preferred,
3777 &with_opaque);
b2e2f908 3778
ed2dc618
SM
3779 /* Some caution must be observed with overloaded functions
3780 and methods, since the index will not contain any overload
3781 information (but NAME might contain it). */
da51c347 3782
ed2dc618
SM
3783 if (sym != NULL
3784 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3785 return stab;
3786 if (with_opaque != NULL
3787 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3788 stab_best = stab;
da51c347 3789
ed2dc618 3790 /* Keep looking through other CUs. */
9291a0cd 3791 }
9291a0cd 3792
da51c347 3793 return stab_best;
9291a0cd
TT
3794}
3795
3796static void
3797dw2_print_stats (struct objfile *objfile)
3798{
ed2dc618
SM
3799 struct dwarf2_per_objfile *dwarf2_per_objfile
3800 = get_dwarf2_per_objfile (objfile);
b76e467d 3801 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3802 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3803 int count = 0;
9291a0cd 3804
ed2dc618 3805 for (int i = 0; i < total; ++i)
9291a0cd 3806 {
ff4c9fec 3807 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3808
43f3e411 3809 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3810 ++count;
3811 }
e4a48d9d 3812 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3813 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3814}
3815
779bd270
DE
3816/* This dumps minimal information about the index.
3817 It is called via "mt print objfiles".
3818 One use is to verify .gdb_index has been loaded by the
3819 gdb.dwarf2/gdb-index.exp testcase. */
3820
9291a0cd
TT
3821static void
3822dw2_dump (struct objfile *objfile)
3823{
ed2dc618
SM
3824 struct dwarf2_per_objfile *dwarf2_per_objfile
3825 = get_dwarf2_per_objfile (objfile);
3826
779bd270
DE
3827 gdb_assert (dwarf2_per_objfile->using_index);
3828 printf_filtered (".gdb_index:");
3829 if (dwarf2_per_objfile->index_table != NULL)
3830 {
3831 printf_filtered (" version %d\n",
3832 dwarf2_per_objfile->index_table->version);
3833 }
3834 else
3835 printf_filtered (" faked for \"readnow\"\n");
3836 printf_filtered ("\n");
9291a0cd
TT
3837}
3838
9291a0cd
TT
3839static void
3840dw2_expand_symtabs_for_function (struct objfile *objfile,
3841 const char *func_name)
3842{
ed2dc618
SM
3843 struct dwarf2_per_objfile *dwarf2_per_objfile
3844 = get_dwarf2_per_objfile (objfile);
da51c347 3845
ed2dc618
SM
3846 struct dw2_symtab_iterator iter;
3847 struct dwarf2_per_cu_data *per_cu;
da51c347 3848
2b79f376 3849 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3850
ed2dc618 3851 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3852 dw2_instantiate_symtab (per_cu, false);
da51c347 3853
9291a0cd
TT
3854}
3855
3856static void
3857dw2_expand_all_symtabs (struct objfile *objfile)
3858{
ed2dc618
SM
3859 struct dwarf2_per_objfile *dwarf2_per_objfile
3860 = get_dwarf2_per_objfile (objfile);
b76e467d 3861 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3862 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3863
ed2dc618 3864 for (int i = 0; i < total_units; ++i)
9291a0cd 3865 {
ff4c9fec 3866 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3867
58f0c718
TT
3868 /* We don't want to directly expand a partial CU, because if we
3869 read it with the wrong language, then assertion failures can
3870 be triggered later on. See PR symtab/23010. So, tell
3871 dw2_instantiate_symtab to skip partial CUs -- any important
3872 partial CU will be read via DW_TAG_imported_unit anyway. */
3873 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3874 }
3875}
3876
3877static void
652a8996
JK
3878dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3879 const char *fullname)
9291a0cd 3880{
ed2dc618
SM
3881 struct dwarf2_per_objfile *dwarf2_per_objfile
3882 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3883
3884 /* We don't need to consider type units here.
3885 This is only called for examining code, e.g. expand_line_sal.
3886 There can be an order of magnitude (or more) more type units
3887 than comp units, and we avoid them if we can. */
3888
b76e467d 3889 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3890 {
3d7bb9d9 3891 /* We only need to look at symtabs not already expanded. */
43f3e411 3892 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3893 continue;
3894
b76e467d 3895 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3896 if (file_data == NULL)
9291a0cd
TT
3897 continue;
3898
b76e467d 3899 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3900 {
652a8996
JK
3901 const char *this_fullname = file_data->file_names[j];
3902
3903 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3904 {
58f0c718 3905 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3906 break;
3907 }
3908 }
3909 }
3910}
3911
9291a0cd 3912static void
199b4314
TT
3913dw2_map_matching_symbols
3914 (struct objfile *objfile,
b054970d 3915 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3916 int global,
3917 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3918 symbol_compare_ftype *ordered_compare)
9291a0cd 3919{
40658b94 3920 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3921 current language is Ada for a non-Ada objfile using GNU index. As Ada
3922 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3923}
3924
e1ef7d7a
PA
3925/* Starting from a search name, return the string that finds the upper
3926 bound of all strings that start with SEARCH_NAME in a sorted name
3927 list. Returns the empty string to indicate that the upper bound is
3928 the end of the list. */
3929
3930static std::string
3931make_sort_after_prefix_name (const char *search_name)
3932{
3933 /* When looking to complete "func", we find the upper bound of all
3934 symbols that start with "func" by looking for where we'd insert
3935 the closest string that would follow "func" in lexicographical
3936 order. Usually, that's "func"-with-last-character-incremented,
3937 i.e. "fund". Mind non-ASCII characters, though. Usually those
3938 will be UTF-8 multi-byte sequences, but we can't be certain.
3939 Especially mind the 0xff character, which is a valid character in
3940 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3941 rule out compilers allowing it in identifiers. Note that
3942 conveniently, strcmp/strcasecmp are specified to compare
3943 characters interpreted as unsigned char. So what we do is treat
3944 the whole string as a base 256 number composed of a sequence of
3945 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3946 to 0, and carries 1 to the following more-significant position.
3947 If the very first character in SEARCH_NAME ends up incremented
3948 and carries/overflows, then the upper bound is the end of the
3949 list. The string after the empty string is also the empty
3950 string.
3951
3952 Some examples of this operation:
3953
3954 SEARCH_NAME => "+1" RESULT
3955
3956 "abc" => "abd"
3957 "ab\xff" => "ac"
3958 "\xff" "a" "\xff" => "\xff" "b"
3959 "\xff" => ""
3960 "\xff\xff" => ""
3961 "" => ""
3962
3963 Then, with these symbols for example:
3964
3965 func
3966 func1
3967 fund
3968
3969 completing "func" looks for symbols between "func" and
3970 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3971 which finds "func" and "func1", but not "fund".
3972
3973 And with:
3974
3975 funcÿ (Latin1 'ÿ' [0xff])
3976 funcÿ1
3977 fund
3978
3979 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3980 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3981
3982 And with:
3983
3984 ÿÿ (Latin1 'ÿ' [0xff])
3985 ÿÿ1
3986
3987 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3988 the end of the list.
3989 */
3990 std::string after = search_name;
3991 while (!after.empty () && (unsigned char) after.back () == 0xff)
3992 after.pop_back ();
3993 if (!after.empty ())
3994 after.back () = (unsigned char) after.back () + 1;
3995 return after;
3996}
3997
5c58de74 3998/* See declaration. */
61d96d7e 3999
5c58de74
PA
4000std::pair<std::vector<name_component>::const_iterator,
4001 std::vector<name_component>::const_iterator>
44ed8f3e 4002mapped_index_base::find_name_components_bounds
3b00ef10 4003 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4004{
5c58de74
PA
4005 auto *name_cmp
4006 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4007
3b00ef10
TT
4008 const char *lang_name
4009 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4010
3f563c84
PA
4011 /* Comparison function object for lower_bound that matches against a
4012 given symbol name. */
4013 auto lookup_compare_lower = [&] (const name_component &elem,
4014 const char *name)
4015 {
5c58de74 4016 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4017 const char *elem_name = elem_qualified + elem.name_offset;
4018 return name_cmp (elem_name, name) < 0;
4019 };
4020
4021 /* Comparison function object for upper_bound that matches against a
4022 given symbol name. */
4023 auto lookup_compare_upper = [&] (const char *name,
4024 const name_component &elem)
4025 {
5c58de74 4026 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4027 const char *elem_name = elem_qualified + elem.name_offset;
4028 return name_cmp (name, elem_name) < 0;
4029 };
4030
5c58de74
PA
4031 auto begin = this->name_components.begin ();
4032 auto end = this->name_components.end ();
3f563c84
PA
4033
4034 /* Find the lower bound. */
4035 auto lower = [&] ()
4036 {
3b00ef10 4037 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4038 return begin;
4039 else
3b00ef10 4040 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4041 } ();
4042
4043 /* Find the upper bound. */
4044 auto upper = [&] ()
4045 {
5c58de74 4046 if (lookup_name_without_params.completion_mode ())
3f563c84 4047 {
e1ef7d7a
PA
4048 /* In completion mode, we want UPPER to point past all
4049 symbols names that have the same prefix. I.e., with
4050 these symbols, and completing "func":
4051
4052 function << lower bound
4053 function1
4054 other_function << upper bound
4055
4056 We find the upper bound by looking for the insertion
4057 point of "func"-with-last-character-incremented,
4058 i.e. "fund". */
3b00ef10 4059 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4060 if (after.empty ())
3f563c84 4061 return end;
e6b2f5ef
PA
4062 return std::lower_bound (lower, end, after.c_str (),
4063 lookup_compare_lower);
3f563c84
PA
4064 }
4065 else
3b00ef10 4066 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4067 } ();
4068
5c58de74
PA
4069 return {lower, upper};
4070}
4071
4072/* See declaration. */
4073
4074void
44ed8f3e 4075mapped_index_base::build_name_components ()
5c58de74
PA
4076{
4077 if (!this->name_components.empty ())
4078 return;
4079
4080 this->name_components_casing = case_sensitivity;
4081 auto *name_cmp
4082 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4083
4084 /* The code below only knows how to break apart components of C++
4085 symbol names (and other languages that use '::' as
3b00ef10 4086 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4087 auto count = this->symbol_name_count ();
4088 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4089 {
44ed8f3e 4090 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4091 continue;
4092
4093 const char *name = this->symbol_name_at (idx);
4094
4095 /* Add each name component to the name component table. */
4096 unsigned int previous_len = 0;
3b00ef10
TT
4097
4098 if (strstr (name, "::") != nullptr)
4099 {
4100 for (unsigned int current_len = cp_find_first_component (name);
4101 name[current_len] != '\0';
4102 current_len += cp_find_first_component (name + current_len))
4103 {
4104 gdb_assert (name[current_len] == ':');
4105 this->name_components.push_back ({previous_len, idx});
4106 /* Skip the '::'. */
4107 current_len += 2;
4108 previous_len = current_len;
4109 }
4110 }
4111 else
5c58de74 4112 {
3b00ef10
TT
4113 /* Handle the Ada encoded (aka mangled) form here. */
4114 for (const char *iter = strstr (name, "__");
4115 iter != nullptr;
4116 iter = strstr (iter, "__"))
4117 {
4118 this->name_components.push_back ({previous_len, idx});
4119 iter += 2;
4120 previous_len = iter - name;
4121 }
5c58de74 4122 }
3b00ef10 4123
5c58de74
PA
4124 this->name_components.push_back ({previous_len, idx});
4125 }
4126
4127 /* Sort name_components elements by name. */
4128 auto name_comp_compare = [&] (const name_component &left,
4129 const name_component &right)
4130 {
4131 const char *left_qualified = this->symbol_name_at (left.idx);
4132 const char *right_qualified = this->symbol_name_at (right.idx);
4133
4134 const char *left_name = left_qualified + left.name_offset;
4135 const char *right_name = right_qualified + right.name_offset;
4136
4137 return name_cmp (left_name, right_name) < 0;
4138 };
4139
4140 std::sort (this->name_components.begin (),
4141 this->name_components.end (),
4142 name_comp_compare);
4143}
4144
4145/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4146 mapped_index_base instead of the containing objfile. This is split
4147 to a separate function in order to be able to unit test the
4148 name_components matching using a mock mapped_index_base. For each
5c58de74 4149 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4150 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4151
4152static void
4153dw2_expand_symtabs_matching_symbol
44ed8f3e 4154 (mapped_index_base &index,
5c58de74
PA
4155 const lookup_name_info &lookup_name_in,
4156 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4157 enum search_domain kind,
3b00ef10 4158 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4159{
4160 lookup_name_info lookup_name_without_params
4161 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4162
4163 /* Build the symbol name component sorted vector, if we haven't
4164 yet. */
4165 index.build_name_components ();
4166
3f563c84
PA
4167 /* The same symbol may appear more than once in the range though.
4168 E.g., if we're looking for symbols that complete "w", and we have
4169 a symbol named "w1::w2", we'll find the two name components for
4170 that same symbol in the range. To be sure we only call the
4171 callback once per symbol, we first collect the symbol name
4172 indexes that matched in a temporary vector and ignore
4173 duplicates. */
4174 std::vector<offset_type> matches;
3f563c84 4175
3b00ef10
TT
4176 struct name_and_matcher
4177 {
4178 symbol_name_matcher_ftype *matcher;
4179 const std::string &name;
4180
4181 bool operator== (const name_and_matcher &other) const
3f563c84 4182 {
3b00ef10
TT
4183 return matcher == other.matcher && name == other.name;
4184 }
4185 };
4186
4187 /* A vector holding all the different symbol name matchers, for all
4188 languages. */
4189 std::vector<name_and_matcher> matchers;
4190
4191 for (int i = 0; i < nr_languages; i++)
4192 {
4193 enum language lang_e = (enum language) i;
4194
4195 const language_defn *lang = language_def (lang_e);
4196 symbol_name_matcher_ftype *name_matcher
4197 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4198
3b00ef10
TT
4199 name_and_matcher key {
4200 name_matcher,
4201 lookup_name_without_params.language_lookup_name (lang_e)
4202 };
4203
4204 /* Don't insert the same comparison routine more than once.
4205 Note that we do this linear walk. This is not a problem in
4206 practice because the number of supported languages is
4207 low. */
4208 if (std::find (matchers.begin (), matchers.end (), key)
4209 != matchers.end ())
9291a0cd 4210 continue;
3b00ef10
TT
4211 matchers.push_back (std::move (key));
4212
4213 auto bounds
4214 = index.find_name_components_bounds (lookup_name_without_params,
4215 lang_e);
4216
4217 /* Now for each symbol name in range, check to see if we have a name
4218 match, and if so, call the MATCH_CALLBACK callback. */
4219
4220 for (; bounds.first != bounds.second; ++bounds.first)
4221 {
4222 const char *qualified = index.symbol_name_at (bounds.first->idx);
4223
4224 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4225 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4226 continue;
9291a0cd 4227
3b00ef10
TT
4228 matches.push_back (bounds.first->idx);
4229 }
3f563c84
PA
4230 }
4231
4232 std::sort (matches.begin (), matches.end ());
4233
4234 /* Finally call the callback, once per match. */
4235 ULONGEST prev = -1;
4236 for (offset_type idx : matches)
4237 {
4238 if (prev != idx)
4239 {
3b00ef10
TT
4240 if (!match_callback (idx))
4241 break;
3f563c84
PA
4242 prev = idx;
4243 }
4244 }
4245
4246 /* Above we use a type wider than idx's for 'prev', since 0 and
4247 (offset_type)-1 are both possible values. */
4248 static_assert (sizeof (prev) > sizeof (offset_type), "");
4249}
4250
c62446b1
PA
4251#if GDB_SELF_TEST
4252
4253namespace selftests { namespace dw2_expand_symtabs_matching {
4254
a3c5fafd
PA
4255/* A mock .gdb_index/.debug_names-like name index table, enough to
4256 exercise dw2_expand_symtabs_matching_symbol, which works with the
4257 mapped_index_base interface. Builds an index from the symbol list
4258 passed as parameter to the constructor. */
4259class mock_mapped_index : public mapped_index_base
c62446b1
PA
4260{
4261public:
a3c5fafd
PA
4262 mock_mapped_index (gdb::array_view<const char *> symbols)
4263 : m_symbol_table (symbols)
c62446b1
PA
4264 {}
4265
a3c5fafd 4266 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4267
a3c5fafd 4268 /* Return the number of names in the symbol table. */
632e107b 4269 size_t symbol_name_count () const override
c62446b1 4270 {
a3c5fafd 4271 return m_symbol_table.size ();
c62446b1
PA
4272 }
4273
a3c5fafd 4274 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4275 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4276 {
4277 return m_symbol_table[idx];
4278 }
c62446b1 4279
a3c5fafd
PA
4280private:
4281 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4282};
4283
4284/* Convenience function that converts a NULL pointer to a "<null>"
4285 string, to pass to print routines. */
4286
4287static const char *
4288string_or_null (const char *str)
4289{
4290 return str != NULL ? str : "<null>";
4291}
4292
4293/* Check if a lookup_name_info built from
4294 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4295 index. EXPECTED_LIST is the list of expected matches, in expected
4296 matching order. If no match expected, then an empty list is
4297 specified. Returns true on success. On failure prints a warning
4298 indicating the file:line that failed, and returns false. */
4299
4300static bool
4301check_match (const char *file, int line,
4302 mock_mapped_index &mock_index,
4303 const char *name, symbol_name_match_type match_type,
4304 bool completion_mode,
4305 std::initializer_list<const char *> expected_list)
4306{
4307 lookup_name_info lookup_name (name, match_type, completion_mode);
4308
4309 bool matched = true;
4310
4311 auto mismatch = [&] (const char *expected_str,
4312 const char *got)
4313 {
4314 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4315 "expected=\"%s\", got=\"%s\"\n"),
4316 file, line,
4317 (match_type == symbol_name_match_type::FULL
4318 ? "FULL" : "WILD"),
4319 name, string_or_null (expected_str), string_or_null (got));
4320 matched = false;
4321 };
4322
4323 auto expected_it = expected_list.begin ();
4324 auto expected_end = expected_list.end ();
4325
a3c5fafd 4326 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4327 NULL, ALL_DOMAIN,
4328 [&] (offset_type idx)
4329 {
a3c5fafd 4330 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4331 const char *expected_str
4332 = expected_it == expected_end ? NULL : *expected_it++;
4333
4334 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4335 mismatch (expected_str, matched_name);
3b00ef10 4336 return true;
c62446b1
PA
4337 });
4338
4339 const char *expected_str
4340 = expected_it == expected_end ? NULL : *expected_it++;
4341 if (expected_str != NULL)
4342 mismatch (expected_str, NULL);
4343
4344 return matched;
4345}
4346
4347/* The symbols added to the mock mapped_index for testing (in
4348 canonical form). */
4349static const char *test_symbols[] = {
4350 "function",
4351 "std::bar",
4352 "std::zfunction",
4353 "std::zfunction2",
4354 "w1::w2",
4355 "ns::foo<char*>",
4356 "ns::foo<int>",
4357 "ns::foo<long>",
a20714ff
PA
4358 "ns2::tmpl<int>::foo2",
4359 "(anonymous namespace)::A::B::C",
c62446b1 4360
e1ef7d7a
PA
4361 /* These are used to check that the increment-last-char in the
4362 matching algorithm for completion doesn't match "t1_fund" when
4363 completing "t1_func". */
4364 "t1_func",
4365 "t1_func1",
4366 "t1_fund",
4367 "t1_fund1",
4368
4369 /* A UTF-8 name with multi-byte sequences to make sure that
4370 cp-name-parser understands this as a single identifier ("função"
4371 is "function" in PT). */
4372 u8"u8função",
4373
4374 /* \377 (0xff) is Latin1 'ÿ'. */
4375 "yfunc\377",
4376
4377 /* \377 (0xff) is Latin1 'ÿ'. */
4378 "\377",
4379 "\377\377123",
4380
c62446b1
PA
4381 /* A name with all sorts of complications. Starts with "z" to make
4382 it easier for the completion tests below. */
4383#define Z_SYM_NAME \
4384 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4385 "::tuple<(anonymous namespace)::ui*, " \
4386 "std::default_delete<(anonymous namespace)::ui>, void>"
4387
4388 Z_SYM_NAME
4389};
4390
a3c5fafd
PA
4391/* Returns true if the mapped_index_base::find_name_component_bounds
4392 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4393 in completion mode. */
5c58de74
PA
4394
4395static bool
a3c5fafd 4396check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4397 const char *search_name,
4398 gdb::array_view<const char *> expected_syms)
4399{
4400 lookup_name_info lookup_name (search_name,
4401 symbol_name_match_type::FULL, true);
4402
3b00ef10
TT
4403 auto bounds = index.find_name_components_bounds (lookup_name,
4404 language_cplus);
5c58de74
PA
4405
4406 size_t distance = std::distance (bounds.first, bounds.second);
4407 if (distance != expected_syms.size ())
4408 return false;
4409
4410 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4411 {
4412 auto nc_elem = bounds.first + exp_elem;
4413 const char *qualified = index.symbol_name_at (nc_elem->idx);
4414 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4415 return false;
4416 }
4417
4418 return true;
4419}
4420
4421/* Test the lower-level mapped_index::find_name_component_bounds
4422 method. */
4423
c62446b1 4424static void
5c58de74
PA
4425test_mapped_index_find_name_component_bounds ()
4426{
4427 mock_mapped_index mock_index (test_symbols);
4428
a3c5fafd 4429 mock_index.build_name_components ();
5c58de74
PA
4430
4431 /* Test the lower-level mapped_index::find_name_component_bounds
4432 method in completion mode. */
4433 {
4434 static const char *expected_syms[] = {
4435 "t1_func",
4436 "t1_func1",
5c58de74
PA
4437 };
4438
a3c5fafd 4439 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4440 "t1_func", expected_syms));
4441 }
4442
4443 /* Check that the increment-last-char in the name matching algorithm
4444 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4445 {
4446 static const char *expected_syms1[] = {
4447 "\377",
4448 "\377\377123",
4449 };
a3c5fafd 4450 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4451 "\377", expected_syms1));
4452
4453 static const char *expected_syms2[] = {
4454 "\377\377123",
4455 };
a3c5fafd 4456 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4457 "\377\377", expected_syms2));
4458 }
4459}
4460
4461/* Test dw2_expand_symtabs_matching_symbol. */
4462
4463static void
4464test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4465{
4466 mock_mapped_index mock_index (test_symbols);
4467
4468 /* We let all tests run until the end even if some fails, for debug
4469 convenience. */
4470 bool any_mismatch = false;
4471
4472 /* Create the expected symbols list (an initializer_list). Needed
4473 because lists have commas, and we need to pass them to CHECK,
4474 which is a macro. */
4475#define EXPECT(...) { __VA_ARGS__ }
4476
4477 /* Wrapper for check_match that passes down the current
4478 __FILE__/__LINE__. */
4479#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4480 any_mismatch |= !check_match (__FILE__, __LINE__, \
4481 mock_index, \
4482 NAME, MATCH_TYPE, COMPLETION_MODE, \
4483 EXPECTED_LIST)
4484
4485 /* Identity checks. */
4486 for (const char *sym : test_symbols)
4487 {
4488 /* Should be able to match all existing symbols. */
4489 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4490 EXPECT (sym));
4491
4492 /* Should be able to match all existing symbols with
4493 parameters. */
4494 std::string with_params = std::string (sym) + "(int)";
4495 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4496 EXPECT (sym));
4497
4498 /* Should be able to match all existing symbols with
4499 parameters and qualifiers. */
4500 with_params = std::string (sym) + " ( int ) const";
4501 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4502 EXPECT (sym));
4503
4504 /* This should really find sym, but cp-name-parser.y doesn't
4505 know about lvalue/rvalue qualifiers yet. */
4506 with_params = std::string (sym) + " ( int ) &&";
4507 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4508 {});
4509 }
4510
e1ef7d7a
PA
4511 /* Check that the name matching algorithm for completion doesn't get
4512 confused with Latin1 'ÿ' / 0xff. */
4513 {
4514 static const char str[] = "\377";
4515 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4516 EXPECT ("\377", "\377\377123"));
4517 }
4518
4519 /* Check that the increment-last-char in the matching algorithm for
4520 completion doesn't match "t1_fund" when completing "t1_func". */
4521 {
4522 static const char str[] = "t1_func";
4523 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4524 EXPECT ("t1_func", "t1_func1"));
4525 }
4526
c62446b1
PA
4527 /* Check that completion mode works at each prefix of the expected
4528 symbol name. */
4529 {
4530 static const char str[] = "function(int)";
4531 size_t len = strlen (str);
4532 std::string lookup;
4533
4534 for (size_t i = 1; i < len; i++)
4535 {
4536 lookup.assign (str, i);
4537 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4538 EXPECT ("function"));
4539 }
4540 }
4541
4542 /* While "w" is a prefix of both components, the match function
4543 should still only be called once. */
4544 {
4545 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4546 EXPECT ("w1::w2"));
a20714ff
PA
4547 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4548 EXPECT ("w1::w2"));
c62446b1
PA
4549 }
4550
4551 /* Same, with a "complicated" symbol. */
4552 {
4553 static const char str[] = Z_SYM_NAME;
4554 size_t len = strlen (str);
4555 std::string lookup;
4556
4557 for (size_t i = 1; i < len; i++)
4558 {
4559 lookup.assign (str, i);
4560 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4561 EXPECT (Z_SYM_NAME));
4562 }
4563 }
4564
4565 /* In FULL mode, an incomplete symbol doesn't match. */
4566 {
4567 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4568 {});
4569 }
4570
4571 /* A complete symbol with parameters matches any overload, since the
4572 index has no overload info. */
4573 {
4574 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4575 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4576 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4577 EXPECT ("std::zfunction", "std::zfunction2"));
4578 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4579 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4580 }
4581
4582 /* Check that whitespace is ignored appropriately. A symbol with a
4583 template argument list. */
4584 {
4585 static const char expected[] = "ns::foo<int>";
4586 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4587 EXPECT (expected));
a20714ff
PA
4588 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4589 EXPECT (expected));
c62446b1
PA
4590 }
4591
4592 /* Check that whitespace is ignored appropriately. A symbol with a
4593 template argument list that includes a pointer. */
4594 {
4595 static const char expected[] = "ns::foo<char*>";
4596 /* Try both completion and non-completion modes. */
4597 static const bool completion_mode[2] = {false, true};
4598 for (size_t i = 0; i < 2; i++)
4599 {
4600 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4601 completion_mode[i], EXPECT (expected));
a20714ff
PA
4602 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4603 completion_mode[i], EXPECT (expected));
c62446b1
PA
4604
4605 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4606 completion_mode[i], EXPECT (expected));
a20714ff
PA
4607 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4608 completion_mode[i], EXPECT (expected));
c62446b1
PA
4609 }
4610 }
4611
4612 {
4613 /* Check method qualifiers are ignored. */
4614 static const char expected[] = "ns::foo<char*>";
4615 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4616 symbol_name_match_type::FULL, true, EXPECT (expected));
4617 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4618 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4619 CHECK_MATCH ("foo < char * > ( int ) const",
4620 symbol_name_match_type::WILD, true, EXPECT (expected));
4621 CHECK_MATCH ("foo < char * > ( int ) &&",
4622 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4623 }
4624
4625 /* Test lookup names that don't match anything. */
4626 {
a20714ff
PA
4627 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4628 {});
4629
c62446b1
PA
4630 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4631 {});
4632 }
4633
a20714ff
PA
4634 /* Some wild matching tests, exercising "(anonymous namespace)",
4635 which should not be confused with a parameter list. */
4636 {
4637 static const char *syms[] = {
4638 "A::B::C",
4639 "B::C",
4640 "C",
4641 "A :: B :: C ( int )",
4642 "B :: C ( int )",
4643 "C ( int )",
4644 };
4645
4646 for (const char *s : syms)
4647 {
4648 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4649 EXPECT ("(anonymous namespace)::A::B::C"));
4650 }
4651 }
4652
4653 {
4654 static const char expected[] = "ns2::tmpl<int>::foo2";
4655 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4656 EXPECT (expected));
4657 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4658 EXPECT (expected));
4659 }
4660
c62446b1
PA
4661 SELF_CHECK (!any_mismatch);
4662
4663#undef EXPECT
4664#undef CHECK_MATCH
4665}
4666
5c58de74
PA
4667static void
4668run_test ()
4669{
4670 test_mapped_index_find_name_component_bounds ();
4671 test_dw2_expand_symtabs_matching_symbol ();
4672}
4673
c62446b1
PA
4674}} // namespace selftests::dw2_expand_symtabs_matching
4675
4676#endif /* GDB_SELF_TEST */
4677
4b514bc8
JK
4678/* If FILE_MATCHER is NULL or if PER_CU has
4679 dwarf2_per_cu_quick_data::MARK set (see
4680 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4681 EXPANSION_NOTIFY on it. */
4682
4683static void
4684dw2_expand_symtabs_matching_one
4685 (struct dwarf2_per_cu_data *per_cu,
4686 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4687 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4688{
4689 if (file_matcher == NULL || per_cu->v.quick->mark)
4690 {
4691 bool symtab_was_null
4692 = (per_cu->v.quick->compunit_symtab == NULL);
4693
58f0c718 4694 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4695
4696 if (expansion_notify != NULL
4697 && symtab_was_null
4698 && per_cu->v.quick->compunit_symtab != NULL)
4699 expansion_notify (per_cu->v.quick->compunit_symtab);
4700 }
4701}
4702
3f563c84
PA
4703/* Helper for dw2_expand_matching symtabs. Called on each symbol
4704 matched, to expand corresponding CUs that were marked. IDX is the
4705 index of the symbol name that matched. */
4706
4707static void
4708dw2_expand_marked_cus
ed2dc618 4709 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4710 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4711 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4712 search_domain kind)
4713{
3f563c84
PA
4714 offset_type *vec, vec_len, vec_idx;
4715 bool global_seen = false;
ed2dc618 4716 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4717
61920122 4718 vec = (offset_type *) (index.constant_pool
f00a2de2 4719 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4720 vec_len = MAYBE_SWAP (vec[0]);
4721 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4722 {
61920122
PA
4723 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4724 /* This value is only valid for index versions >= 7. */
4725 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4726 gdb_index_symbol_kind symbol_kind =
4727 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4728 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4729 /* Only check the symbol attributes if they're present.
4730 Indices prior to version 7 don't record them,
4731 and indices >= 7 may elide them for certain symbols
4732 (gold does this). */
4733 int attrs_valid =
4734 (index.version >= 7
4735 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4736
4737 /* Work around gold/15646. */
4738 if (attrs_valid)
9291a0cd 4739 {
61920122
PA
4740 if (!is_static && global_seen)
4741 continue;
4742 if (!is_static)
4743 global_seen = true;
4744 }
3190f0c6 4745
61920122
PA
4746 /* Only check the symbol's kind if it has one. */
4747 if (attrs_valid)
4748 {
4749 switch (kind)
8943b874 4750 {
61920122
PA
4751 case VARIABLES_DOMAIN:
4752 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4753 continue;
4754 break;
4755 case FUNCTIONS_DOMAIN:
4756 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4757 continue;
61920122
PA
4758 break;
4759 case TYPES_DOMAIN:
4760 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4761 continue;
4762 break;
59c35742
AB
4763 case MODULES_DOMAIN:
4764 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4765 continue;
4766 break;
61920122
PA
4767 default:
4768 break;
8943b874 4769 }
61920122 4770 }
8943b874 4771
61920122 4772 /* Don't crash on bad data. */
b76e467d 4773 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4774 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4775 {
b98664d3 4776 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4777 " [in module %s]"),
4778 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4779 continue;
4780 }
4781
ff4c9fec 4782 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4783 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4784 expansion_notify);
61920122
PA
4785 }
4786}
4787
4b514bc8
JK
4788/* If FILE_MATCHER is non-NULL, set all the
4789 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4790 that match FILE_MATCHER. */
4791
61920122 4792static void
4b514bc8 4793dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4794 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4795 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4796{
4b514bc8 4797 if (file_matcher == NULL)
61920122
PA
4798 return;
4799
4b514bc8
JK
4800 objfile *const objfile = dwarf2_per_objfile->objfile;
4801
4802 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4803 htab_eq_pointer,
4804 NULL, xcalloc, xfree));
4805 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4806 htab_eq_pointer,
4807 NULL, xcalloc, xfree));
61920122 4808
4b514bc8
JK
4809 /* The rule is CUs specify all the files, including those used by
4810 any TU, so there's no need to scan TUs here. */
61920122 4811
b76e467d 4812 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4813 {
927aa2e7
JK
4814 QUIT;
4815
4816 per_cu->v.quick->mark = 0;
4817
4818 /* We only need to look at symtabs not already expanded. */
4819 if (per_cu->v.quick->compunit_symtab)
4820 continue;
4821
b76e467d 4822 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4823 if (file_data == NULL)
4824 continue;
4825
4826 if (htab_find (visited_not_found.get (), file_data) != NULL)
4827 continue;
4828 else if (htab_find (visited_found.get (), file_data) != NULL)
4829 {
4830 per_cu->v.quick->mark = 1;
4831 continue;
4832 }
4833
b76e467d 4834 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4835 {
4836 const char *this_real_name;
4837
4838 if (file_matcher (file_data->file_names[j], false))
4839 {
4840 per_cu->v.quick->mark = 1;
4841 break;
4842 }
4843
4844 /* Before we invoke realpath, which can get expensive when many
4845 files are involved, do a quick comparison of the basenames. */
4846 if (!basenames_may_differ
4847 && !file_matcher (lbasename (file_data->file_names[j]),
4848 true))
4849 continue;
4850
4851 this_real_name = dw2_get_real_path (objfile, file_data, j);
4852 if (file_matcher (this_real_name, false))
4853 {
4854 per_cu->v.quick->mark = 1;
4855 break;
4856 }
4857 }
4858
b76e467d
SM
4859 void **slot = htab_find_slot (per_cu->v.quick->mark
4860 ? visited_found.get ()
4861 : visited_not_found.get (),
4862 file_data, INSERT);
927aa2e7
JK
4863 *slot = file_data;
4864 }
4865}
4866
4867static void
4868dw2_expand_symtabs_matching
4869 (struct objfile *objfile,
4870 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4871 const lookup_name_info &lookup_name,
4872 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4873 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4874 enum search_domain kind)
4875{
ed2dc618
SM
4876 struct dwarf2_per_objfile *dwarf2_per_objfile
4877 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4878
4879 /* index_table is NULL if OBJF_READNOW. */
4880 if (!dwarf2_per_objfile->index_table)
4881 return;
4882
ed2dc618 4883 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4884
4885 mapped_index &index = *dwarf2_per_objfile->index_table;
4886
4887 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4888 symbol_matcher,
4889 kind, [&] (offset_type idx)
4890 {
ed2dc618 4891 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4892 expansion_notify, kind);
3b00ef10 4893 return true;
927aa2e7
JK
4894 });
4895}
4896
4897/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4898 symtab. */
4899
4900static struct compunit_symtab *
4901recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4902 CORE_ADDR pc)
4903{
4904 int i;
4905
4906 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4907 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4908 return cust;
4909
4910 if (cust->includes == NULL)
4911 return NULL;
4912
4913 for (i = 0; cust->includes[i]; ++i)
4914 {
4915 struct compunit_symtab *s = cust->includes[i];
4916
4917 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4918 if (s != NULL)
4919 return s;
4920 }
4921
4922 return NULL;
4923}
4924
4925static struct compunit_symtab *
4926dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4927 struct bound_minimal_symbol msymbol,
4928 CORE_ADDR pc,
4929 struct obj_section *section,
4930 int warn_if_readin)
4931{
4932 struct dwarf2_per_cu_data *data;
4933 struct compunit_symtab *result;
4934
d320c2b5 4935 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4936 return NULL;
4937
b3b3bada 4938 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4939 data = (struct dwarf2_per_cu_data *) addrmap_find
4940 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4941 if (!data)
4942 return NULL;
4943
4944 if (warn_if_readin && data->v.quick->compunit_symtab)
4945 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4946 paddress (get_objfile_arch (objfile), pc));
4947
4948 result
58f0c718
TT
4949 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4950 false),
927aa2e7
JK
4951 pc);
4952 gdb_assert (result != NULL);
4953 return result;
4954}
4955
4956static void
4957dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4958 void *data, int need_fullname)
4959{
ed2dc618
SM
4960 struct dwarf2_per_objfile *dwarf2_per_objfile
4961 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4962
4963 if (!dwarf2_per_objfile->filenames_cache)
4964 {
4965 dwarf2_per_objfile->filenames_cache.emplace ();
4966
4967 htab_up visited (htab_create_alloc (10,
4968 htab_hash_pointer, htab_eq_pointer,
4969 NULL, xcalloc, xfree));
4970
4971 /* The rule is CUs specify all the files, including those used
4972 by any TU, so there's no need to scan TUs here. We can
4973 ignore file names coming from already-expanded CUs. */
4974
b76e467d 4975 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4976 {
927aa2e7
JK
4977 if (per_cu->v.quick->compunit_symtab)
4978 {
4979 void **slot = htab_find_slot (visited.get (),
4980 per_cu->v.quick->file_names,
4981 INSERT);
4982
4983 *slot = per_cu->v.quick->file_names;
4984 }
4985 }
4986
b76e467d 4987 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4988 {
927aa2e7
JK
4989 /* We only need to look at symtabs not already expanded. */
4990 if (per_cu->v.quick->compunit_symtab)
4991 continue;
4992
b76e467d 4993 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4994 if (file_data == NULL)
4995 continue;
4996
b76e467d 4997 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4998 if (*slot)
4999 {
5000 /* Already visited. */
5001 continue;
5002 }
5003 *slot = file_data;
5004
5005 for (int j = 0; j < file_data->num_file_names; ++j)
5006 {
5007 const char *filename = file_data->file_names[j];
5008 dwarf2_per_objfile->filenames_cache->seen (filename);
5009 }
5010 }
5011 }
5012
5013 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5014 {
5015 gdb::unique_xmalloc_ptr<char> this_real_name;
5016
5017 if (need_fullname)
5018 this_real_name = gdb_realpath (filename);
5019 (*fun) (filename, this_real_name.get (), data);
5020 });
5021}
5022
5023static int
5024dw2_has_symbols (struct objfile *objfile)
5025{
5026 return 1;
5027}
5028
5029const struct quick_symbol_functions dwarf2_gdb_index_functions =
5030{
5031 dw2_has_symbols,
5032 dw2_find_last_source_symtab,
5033 dw2_forget_cached_source_info,
5034 dw2_map_symtabs_matching_filename,
5035 dw2_lookup_symbol,
5036 dw2_print_stats,
5037 dw2_dump,
927aa2e7
JK
5038 dw2_expand_symtabs_for_function,
5039 dw2_expand_all_symtabs,
5040 dw2_expand_symtabs_with_fullname,
5041 dw2_map_matching_symbols,
5042 dw2_expand_symtabs_matching,
5043 dw2_find_pc_sect_compunit_symtab,
5044 NULL,
5045 dw2_map_symbol_filenames
5046};
5047
5048/* DWARF-5 debug_names reader. */
5049
5050/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5051static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5052
5053/* A helper function that reads the .debug_names section in SECTION
5054 and fills in MAP. FILENAME is the name of the file containing the
5055 section; it is used for error reporting.
5056
5057 Returns true if all went well, false otherwise. */
5058
5059static bool
5060read_debug_names_from_section (struct objfile *objfile,
5061 const char *filename,
5062 struct dwarf2_section_info *section,
5063 mapped_debug_names &map)
5064{
96b79293 5065 if (section->empty ())
927aa2e7
JK
5066 return false;
5067
5068 /* Older elfutils strip versions could keep the section in the main
5069 executable while splitting it for the separate debug info file. */
96b79293 5070 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5071 return false;
5072
96b79293 5073 section->read (objfile);
927aa2e7
JK
5074
5075 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5076
5077 const gdb_byte *addr = section->buffer;
5078
96b79293 5079 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5080
5081 unsigned int bytes_read;
5082 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5083 addr += bytes_read;
5084
5085 map.dwarf5_is_dwarf64 = bytes_read != 4;
5086 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5087 if (bytes_read + length != section->size)
5088 {
5089 /* There may be multiple per-CU indices. */
5090 warning (_("Section .debug_names in %s length %s does not match "
5091 "section length %s, ignoring .debug_names."),
5092 filename, plongest (bytes_read + length),
5093 pulongest (section->size));
5094 return false;
5095 }
5096
5097 /* The version number. */
5098 uint16_t version = read_2_bytes (abfd, addr);
5099 addr += 2;
5100 if (version != 5)
5101 {
5102 warning (_("Section .debug_names in %s has unsupported version %d, "
5103 "ignoring .debug_names."),
5104 filename, version);
5105 return false;
5106 }
5107
5108 /* Padding. */
5109 uint16_t padding = read_2_bytes (abfd, addr);
5110 addr += 2;
5111 if (padding != 0)
5112 {
5113 warning (_("Section .debug_names in %s has unsupported padding %d, "
5114 "ignoring .debug_names."),
5115 filename, padding);
5116 return false;
5117 }
5118
5119 /* comp_unit_count - The number of CUs in the CU list. */
5120 map.cu_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* local_type_unit_count - The number of TUs in the local TU
5124 list. */
5125 map.tu_count = read_4_bytes (abfd, addr);
5126 addr += 4;
5127
5128 /* foreign_type_unit_count - The number of TUs in the foreign TU
5129 list. */
5130 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5131 addr += 4;
5132 if (foreign_tu_count != 0)
5133 {
5134 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5135 "ignoring .debug_names."),
5136 filename, static_cast<unsigned long> (foreign_tu_count));
5137 return false;
5138 }
5139
5140 /* bucket_count - The number of hash buckets in the hash lookup
5141 table. */
5142 map.bucket_count = read_4_bytes (abfd, addr);
5143 addr += 4;
5144
5145 /* name_count - The number of unique names in the index. */
5146 map.name_count = read_4_bytes (abfd, addr);
5147 addr += 4;
5148
5149 /* abbrev_table_size - The size in bytes of the abbreviations
5150 table. */
5151 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5152 addr += 4;
5153
5154 /* augmentation_string_size - The size in bytes of the augmentation
5155 string. This value is rounded up to a multiple of 4. */
5156 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5157 addr += 4;
5158 map.augmentation_is_gdb = ((augmentation_string_size
5159 == sizeof (dwarf5_augmentation))
5160 && memcmp (addr, dwarf5_augmentation,
5161 sizeof (dwarf5_augmentation)) == 0);
5162 augmentation_string_size += (-augmentation_string_size) & 3;
5163 addr += augmentation_string_size;
5164
5165 /* List of CUs */
5166 map.cu_table_reordered = addr;
5167 addr += map.cu_count * map.offset_size;
5168
5169 /* List of Local TUs */
5170 map.tu_table_reordered = addr;
5171 addr += map.tu_count * map.offset_size;
5172
5173 /* Hash Lookup Table */
5174 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5175 addr += map.bucket_count * 4;
5176 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5177 addr += map.name_count * 4;
5178
5179 /* Name Table */
5180 map.name_table_string_offs_reordered = addr;
5181 addr += map.name_count * map.offset_size;
5182 map.name_table_entry_offs_reordered = addr;
5183 addr += map.name_count * map.offset_size;
5184
5185 const gdb_byte *abbrev_table_start = addr;
5186 for (;;)
5187 {
927aa2e7
JK
5188 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5189 addr += bytes_read;
5190 if (index_num == 0)
5191 break;
5192
5193 const auto insertpair
5194 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5195 if (!insertpair.second)
5196 {
5197 warning (_("Section .debug_names in %s has duplicate index %s, "
5198 "ignoring .debug_names."),
5199 filename, pulongest (index_num));
5200 return false;
5201 }
5202 mapped_debug_names::index_val &indexval = insertpair.first->second;
5203 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5204 addr += bytes_read;
5205
5206 for (;;)
5207 {
5208 mapped_debug_names::index_val::attr attr;
5209 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5210 addr += bytes_read;
5211 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5212 addr += bytes_read;
5213 if (attr.form == DW_FORM_implicit_const)
5214 {
5215 attr.implicit_const = read_signed_leb128 (abfd, addr,
5216 &bytes_read);
5217 addr += bytes_read;
5218 }
5219 if (attr.dw_idx == 0 && attr.form == 0)
5220 break;
5221 indexval.attr_vec.push_back (std::move (attr));
5222 }
5223 }
5224 if (addr != abbrev_table_start + abbrev_table_size)
5225 {
5226 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5227 "of size %s vs. written as %u, ignoring .debug_names."),
5228 filename, plongest (addr - abbrev_table_start),
5229 abbrev_table_size);
927aa2e7
JK
5230 return false;
5231 }
5232 map.entry_pool = addr;
5233
5234 return true;
5235}
5236
5237/* A helper for create_cus_from_debug_names that handles the MAP's CU
5238 list. */
5239
5240static void
ed2dc618 5241create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5242 const mapped_debug_names &map,
5243 dwarf2_section_info &section,
b76e467d 5244 bool is_dwz)
927aa2e7
JK
5245{
5246 sect_offset sect_off_prev;
5247 for (uint32_t i = 0; i <= map.cu_count; ++i)
5248 {
5249 sect_offset sect_off_next;
5250 if (i < map.cu_count)
5251 {
5252 sect_off_next
5253 = (sect_offset) (extract_unsigned_integer
5254 (map.cu_table_reordered + i * map.offset_size,
5255 map.offset_size,
5256 map.dwarf5_byte_order));
5257 }
5258 else
5259 sect_off_next = (sect_offset) section.size;
5260 if (i >= 1)
5261 {
5262 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5263 dwarf2_per_cu_data *per_cu
ed2dc618 5264 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5265 sect_off_prev, length);
b76e467d 5266 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5267 }
5268 sect_off_prev = sect_off_next;
5269 }
5270}
5271
5272/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5273 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5274
5275static void
ed2dc618 5276create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5277 const mapped_debug_names &map,
5278 const mapped_debug_names &dwz_map)
5279{
b76e467d
SM
5280 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5281 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5282
ed2dc618
SM
5283 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5284 dwarf2_per_objfile->info,
b76e467d 5285 false /* is_dwz */);
927aa2e7
JK
5286
5287 if (dwz_map.cu_count == 0)
5288 return;
5289
ed2dc618
SM
5290 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5291 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5292 true /* is_dwz */);
927aa2e7
JK
5293}
5294
5295/* Read .debug_names. If everything went ok, initialize the "quick"
5296 elements of all the CUs and return true. Otherwise, return false. */
5297
5298static bool
ed2dc618 5299dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5300{
22ca247e
TT
5301 std::unique_ptr<mapped_debug_names> map
5302 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5303 mapped_debug_names dwz_map (dwarf2_per_objfile);
5304 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5305
5306 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5307 &dwarf2_per_objfile->debug_names,
22ca247e 5308 *map))
927aa2e7
JK
5309 return false;
5310
5311 /* Don't use the index if it's empty. */
22ca247e 5312 if (map->name_count == 0)
927aa2e7
JK
5313 return false;
5314
5315 /* If there is a .dwz file, read it so we can get its CU list as
5316 well. */
ed2dc618 5317 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5318 if (dwz != NULL)
5319 {
5320 if (!read_debug_names_from_section (objfile,
00f93c44 5321 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5322 &dwz->debug_names, dwz_map))
5323 {
5324 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5325 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5326 return false;
5327 }
5328 }
5329
22ca247e 5330 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5331
22ca247e 5332 if (map->tu_count != 0)
927aa2e7
JK
5333 {
5334 /* We can only handle a single .debug_types when we have an
5335 index. */
fd5866f6 5336 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5337 return false;
5338
fd5866f6 5339 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5340
5341 create_signatured_type_table_from_debug_names
22ca247e 5342 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5343 }
5344
ed2dc618
SM
5345 create_addrmap_from_aranges (dwarf2_per_objfile,
5346 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5347
22ca247e 5348 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5349 dwarf2_per_objfile->using_index = 1;
5350 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5351 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5352
5353 return true;
5354}
5355
927aa2e7
JK
5356/* Type used to manage iterating over all CUs looking for a symbol for
5357 .debug_names. */
5358
5359class dw2_debug_names_iterator
5360{
5361public:
927aa2e7 5362 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5363 gdb::optional<block_enum> block_index,
5364 domain_enum domain,
927aa2e7 5365 const char *name)
2b79f376 5366 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5367 m_addr (find_vec_in_debug_names (map, name))
5368 {}
5369
5370 dw2_debug_names_iterator (const mapped_debug_names &map,
5371 search_domain search, uint32_t namei)
5372 : m_map (map),
5373 m_search (search),
5374 m_addr (find_vec_in_debug_names (map, namei))
5375 {}
5376
3b00ef10
TT
5377 dw2_debug_names_iterator (const mapped_debug_names &map,
5378 block_enum block_index, domain_enum domain,
5379 uint32_t namei)
5380 : m_map (map), m_block_index (block_index), m_domain (domain),
5381 m_addr (find_vec_in_debug_names (map, namei))
5382 {}
5383
927aa2e7
JK
5384 /* Return the next matching CU or NULL if there are no more. */
5385 dwarf2_per_cu_data *next ();
5386
5387private:
5388 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5389 const char *name);
5390 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5391 uint32_t namei);
5392
5393 /* The internalized form of .debug_names. */
5394 const mapped_debug_names &m_map;
5395
2b79f376
SM
5396 /* If set, only look for symbols that match that block. Valid values are
5397 GLOBAL_BLOCK and STATIC_BLOCK. */
5398 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5399
5400 /* The kind of symbol we're looking for. */
5401 const domain_enum m_domain = UNDEF_DOMAIN;
5402 const search_domain m_search = ALL_DOMAIN;
5403
5404 /* The list of CUs from the index entry of the symbol, or NULL if
5405 not found. */
5406 const gdb_byte *m_addr;
5407};
5408
5409const char *
5410mapped_debug_names::namei_to_name (uint32_t namei) const
5411{
5412 const ULONGEST namei_string_offs
5413 = extract_unsigned_integer ((name_table_string_offs_reordered
5414 + namei * offset_size),
5415 offset_size,
5416 dwarf5_byte_order);
5417 return read_indirect_string_at_offset
ed2dc618 5418 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5419}
5420
5421/* Find a slot in .debug_names for the object named NAME. If NAME is
5422 found, return pointer to its pool data. If NAME cannot be found,
5423 return NULL. */
5424
5425const gdb_byte *
5426dw2_debug_names_iterator::find_vec_in_debug_names
5427 (const mapped_debug_names &map, const char *name)
5428{
5429 int (*cmp) (const char *, const char *);
5430
54ee4252 5431 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5432 if (current_language->la_language == language_cplus
5433 || current_language->la_language == language_fortran
5434 || current_language->la_language == language_d)
5435 {
5436 /* NAME is already canonical. Drop any qualifiers as
5437 .debug_names does not contain any. */
5438
5439 if (strchr (name, '(') != NULL)
5440 {
54ee4252 5441 without_params = cp_remove_params (name);
927aa2e7 5442 if (without_params != NULL)
54ee4252 5443 name = without_params.get ();
927aa2e7
JK
5444 }
5445 }
5446
5447 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5448
5449 const uint32_t full_hash = dwarf5_djb_hash (name);
5450 uint32_t namei
5451 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5452 (map.bucket_table_reordered
5453 + (full_hash % map.bucket_count)), 4,
5454 map.dwarf5_byte_order);
5455 if (namei == 0)
5456 return NULL;
5457 --namei;
5458 if (namei >= map.name_count)
5459 {
b98664d3 5460 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5461 "[in module %s]"),
5462 namei, map.name_count,
ed2dc618 5463 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5464 return NULL;
5465 }
5466
5467 for (;;)
5468 {
5469 const uint32_t namei_full_hash
5470 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5471 (map.hash_table_reordered + namei), 4,
5472 map.dwarf5_byte_order);
5473 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5474 return NULL;
5475
5476 if (full_hash == namei_full_hash)
5477 {
5478 const char *const namei_string = map.namei_to_name (namei);
5479
5480#if 0 /* An expensive sanity check. */
5481 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5482 {
b98664d3 5483 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5484 "[in module %s]"),
5485 namei, objfile_name (dwarf2_per_objfile->objfile));
5486 return NULL;
5487 }
5488#endif
5489
5490 if (cmp (namei_string, name) == 0)
5491 {
5492 const ULONGEST namei_entry_offs
5493 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5494 + namei * map.offset_size),
5495 map.offset_size, map.dwarf5_byte_order);
5496 return map.entry_pool + namei_entry_offs;
5497 }
5498 }
5499
5500 ++namei;
5501 if (namei >= map.name_count)
5502 return NULL;
5503 }
5504}
5505
5506const gdb_byte *
5507dw2_debug_names_iterator::find_vec_in_debug_names
5508 (const mapped_debug_names &map, uint32_t namei)
5509{
5510 if (namei >= map.name_count)
5511 {
b98664d3 5512 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5513 "[in module %s]"),
5514 namei, map.name_count,
ed2dc618 5515 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5516 return NULL;
5517 }
5518
5519 const ULONGEST namei_entry_offs
5520 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5521 + namei * map.offset_size),
5522 map.offset_size, map.dwarf5_byte_order);
5523 return map.entry_pool + namei_entry_offs;
5524}
5525
5526/* See dw2_debug_names_iterator. */
5527
5528dwarf2_per_cu_data *
5529dw2_debug_names_iterator::next ()
5530{
5531 if (m_addr == NULL)
5532 return NULL;
5533
ed2dc618
SM
5534 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5535 struct objfile *objfile = dwarf2_per_objfile->objfile;
5536 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5537
5538 again:
5539
5540 unsigned int bytes_read;
5541 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5542 m_addr += bytes_read;
5543 if (abbrev == 0)
5544 return NULL;
5545
5546 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5547 if (indexval_it == m_map.abbrev_map.cend ())
5548 {
b98664d3 5549 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5550 "[in module %s]"),
ed2dc618 5551 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5552 return NULL;
5553 }
5554 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5555 enum class symbol_linkage {
5556 unknown,
5557 static_,
5558 extern_,
23c13d42 5559 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5560 dwarf2_per_cu_data *per_cu = NULL;
5561 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5562 {
5563 ULONGEST ull;
5564 switch (attr.form)
5565 {
5566 case DW_FORM_implicit_const:
5567 ull = attr.implicit_const;
5568 break;
5569 case DW_FORM_flag_present:
5570 ull = 1;
5571 break;
5572 case DW_FORM_udata:
5573 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5574 m_addr += bytes_read;
5575 break;
5576 default:
b98664d3 5577 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5578 dwarf_form_name (attr.form),
ed2dc618 5579 objfile_name (objfile));
927aa2e7
JK
5580 return NULL;
5581 }
5582 switch (attr.dw_idx)
5583 {
5584 case DW_IDX_compile_unit:
5585 /* Don't crash on bad data. */
b76e467d 5586 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5587 {
b98664d3 5588 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5589 " [in module %s]"),
5590 pulongest (ull),
5591 objfile_name (dwarf2_per_objfile->objfile));
5592 continue;
5593 }
ff4c9fec 5594 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5595 break;
8af5c486
JK
5596 case DW_IDX_type_unit:
5597 /* Don't crash on bad data. */
b2bdb8cf 5598 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5599 {
b98664d3 5600 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5601 " [in module %s]"),
5602 pulongest (ull),
5603 objfile_name (dwarf2_per_objfile->objfile));
5604 continue;
5605 }
ff4c9fec 5606 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5607 break;
927aa2e7
JK
5608 case DW_IDX_GNU_internal:
5609 if (!m_map.augmentation_is_gdb)
5610 break;
23c13d42 5611 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5612 break;
5613 case DW_IDX_GNU_external:
5614 if (!m_map.augmentation_is_gdb)
5615 break;
23c13d42 5616 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5617 break;
5618 }
5619 }
5620
5621 /* Skip if already read in. */
5622 if (per_cu->v.quick->compunit_symtab)
5623 goto again;
5624
5625 /* Check static vs global. */
23c13d42 5626 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5627 {
2b79f376 5628 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5629 const bool symbol_is_static =
5630 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5631 if (want_static != symbol_is_static)
2b79f376 5632 goto again;
927aa2e7
JK
5633 }
5634
5635 /* Match dw2_symtab_iter_next, symbol_kind
5636 and debug_names::psymbol_tag. */
5637 switch (m_domain)
5638 {
5639 case VAR_DOMAIN:
5640 switch (indexval.dwarf_tag)
5641 {
5642 case DW_TAG_variable:
5643 case DW_TAG_subprogram:
5644 /* Some types are also in VAR_DOMAIN. */
5645 case DW_TAG_typedef:
5646 case DW_TAG_structure_type:
5647 break;
5648 default:
5649 goto again;
5650 }
5651 break;
5652 case STRUCT_DOMAIN:
5653 switch (indexval.dwarf_tag)
5654 {
5655 case DW_TAG_typedef:
5656 case DW_TAG_structure_type:
5657 break;
5658 default:
5659 goto again;
5660 }
5661 break;
5662 case LABEL_DOMAIN:
5663 switch (indexval.dwarf_tag)
5664 {
5665 case 0:
5666 case DW_TAG_variable:
5667 break;
5668 default:
5669 goto again;
5670 }
5671 break;
59c35742
AB
5672 case MODULE_DOMAIN:
5673 switch (indexval.dwarf_tag)
5674 {
5675 case DW_TAG_module:
5676 break;
5677 default:
5678 goto again;
5679 }
5680 break;
927aa2e7
JK
5681 default:
5682 break;
5683 }
5684
5685 /* Match dw2_expand_symtabs_matching, symbol_kind and
5686 debug_names::psymbol_tag. */
5687 switch (m_search)
4b514bc8 5688 {
927aa2e7
JK
5689 case VARIABLES_DOMAIN:
5690 switch (indexval.dwarf_tag)
4b514bc8 5691 {
927aa2e7
JK
5692 case DW_TAG_variable:
5693 break;
5694 default:
5695 goto again;
4b514bc8 5696 }
927aa2e7
JK
5697 break;
5698 case FUNCTIONS_DOMAIN:
5699 switch (indexval.dwarf_tag)
4b514bc8 5700 {
927aa2e7
JK
5701 case DW_TAG_subprogram:
5702 break;
5703 default:
5704 goto again;
4b514bc8 5705 }
927aa2e7
JK
5706 break;
5707 case TYPES_DOMAIN:
5708 switch (indexval.dwarf_tag)
5709 {
5710 case DW_TAG_typedef:
5711 case DW_TAG_structure_type:
5712 break;
5713 default:
5714 goto again;
5715 }
5716 break;
59c35742
AB
5717 case MODULES_DOMAIN:
5718 switch (indexval.dwarf_tag)
5719 {
5720 case DW_TAG_module:
5721 break;
5722 default:
5723 goto again;
5724 }
927aa2e7
JK
5725 default:
5726 break;
4b514bc8 5727 }
927aa2e7
JK
5728
5729 return per_cu;
4b514bc8 5730}
61920122 5731
927aa2e7 5732static struct compunit_symtab *
c7f839cb 5733dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5734 const char *name, domain_enum domain)
4b514bc8 5735{
ed2dc618
SM
5736 struct dwarf2_per_objfile *dwarf2_per_objfile
5737 = get_dwarf2_per_objfile (objfile);
61920122 5738
927aa2e7
JK
5739 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5740 if (!mapp)
61920122 5741 {
927aa2e7
JK
5742 /* index is NULL if OBJF_READNOW. */
5743 return NULL;
5744 }
5745 const auto &map = *mapp;
9291a0cd 5746
2b79f376 5747 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5748
927aa2e7
JK
5749 struct compunit_symtab *stab_best = NULL;
5750 struct dwarf2_per_cu_data *per_cu;
5751 while ((per_cu = iter.next ()) != NULL)
5752 {
5753 struct symbol *sym, *with_opaque = NULL;
58f0c718 5754 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5755 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5756 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5757
927aa2e7
JK
5758 sym = block_find_symbol (block, name, domain,
5759 block_find_non_opaque_type_preferred,
5760 &with_opaque);
9703b513 5761
927aa2e7
JK
5762 /* Some caution must be observed with overloaded functions and
5763 methods, since the index will not contain any overload
5764 information (but NAME might contain it). */
a3ec0bb1 5765
927aa2e7 5766 if (sym != NULL
987012b8 5767 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5768 return stab;
5769 if (with_opaque != NULL
987012b8 5770 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5771 stab_best = stab;
9703b513 5772
927aa2e7 5773 /* Keep looking through other CUs. */
9703b513
TT
5774 }
5775
927aa2e7 5776 return stab_best;
9703b513
TT
5777}
5778
927aa2e7
JK
5779/* This dumps minimal information about .debug_names. It is called
5780 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5781 uses this to verify that .debug_names has been loaded. */
9291a0cd 5782
927aa2e7
JK
5783static void
5784dw2_debug_names_dump (struct objfile *objfile)
5785{
ed2dc618
SM
5786 struct dwarf2_per_objfile *dwarf2_per_objfile
5787 = get_dwarf2_per_objfile (objfile);
5788
927aa2e7
JK
5789 gdb_assert (dwarf2_per_objfile->using_index);
5790 printf_filtered (".debug_names:");
5791 if (dwarf2_per_objfile->debug_names_table)
5792 printf_filtered (" exists\n");
5793 else
5794 printf_filtered (" faked for \"readnow\"\n");
5795 printf_filtered ("\n");
9291a0cd
TT
5796}
5797
9291a0cd 5798static void
927aa2e7
JK
5799dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5800 const char *func_name)
9291a0cd 5801{
ed2dc618
SM
5802 struct dwarf2_per_objfile *dwarf2_per_objfile
5803 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5804
927aa2e7
JK
5805 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5806 if (dwarf2_per_objfile->debug_names_table)
24c79950 5807 {
927aa2e7 5808 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5809
2b79f376 5810 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5811
927aa2e7
JK
5812 struct dwarf2_per_cu_data *per_cu;
5813 while ((per_cu = iter.next ()) != NULL)
58f0c718 5814 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5815 }
5816}
24c79950 5817
3b00ef10
TT
5818static void
5819dw2_debug_names_map_matching_symbols
5820 (struct objfile *objfile,
5821 const lookup_name_info &name, domain_enum domain,
5822 int global,
5823 gdb::function_view<symbol_found_callback_ftype> callback,
5824 symbol_compare_ftype *ordered_compare)
5825{
5826 struct dwarf2_per_objfile *dwarf2_per_objfile
5827 = get_dwarf2_per_objfile (objfile);
5828
5829 /* debug_names_table is NULL if OBJF_READNOW. */
5830 if (!dwarf2_per_objfile->debug_names_table)
5831 return;
5832
5833 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5834 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5835
5836 const char *match_name = name.ada ().lookup_name ().c_str ();
5837 auto matcher = [&] (const char *symname)
5838 {
5839 if (ordered_compare == nullptr)
5840 return true;
5841 return ordered_compare (symname, match_name) == 0;
5842 };
5843
5844 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5845 [&] (offset_type namei)
5846 {
5847 /* The name was matched, now expand corresponding CUs that were
5848 marked. */
5849 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5850
5851 struct dwarf2_per_cu_data *per_cu;
5852 while ((per_cu = iter.next ()) != NULL)
5853 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5854 return true;
5855 });
5856
5857 /* It's a shame we couldn't do this inside the
5858 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5859 that have already been expanded. Instead, this loop matches what
5860 the psymtab code does. */
5861 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5862 {
5863 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5864 if (cust != nullptr)
5865 {
5866 const struct block *block
5867 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5868 if (!iterate_over_symbols_terminated (block, name,
5869 domain, callback))
5870 break;
5871 }
5872 }
5873}
5874
927aa2e7
JK
5875static void
5876dw2_debug_names_expand_symtabs_matching
5877 (struct objfile *objfile,
5878 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5879 const lookup_name_info &lookup_name,
5880 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5881 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5882 enum search_domain kind)
5883{
ed2dc618
SM
5884 struct dwarf2_per_objfile *dwarf2_per_objfile
5885 = get_dwarf2_per_objfile (objfile);
9291a0cd 5886
927aa2e7
JK
5887 /* debug_names_table is NULL if OBJF_READNOW. */
5888 if (!dwarf2_per_objfile->debug_names_table)
5889 return;
9291a0cd 5890
ed2dc618 5891 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5892
44ed8f3e 5893 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5894
44ed8f3e
PA
5895 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5896 symbol_matcher,
5897 kind, [&] (offset_type namei)
927aa2e7 5898 {
927aa2e7
JK
5899 /* The name was matched, now expand corresponding CUs that were
5900 marked. */
5901 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5902
927aa2e7
JK
5903 struct dwarf2_per_cu_data *per_cu;
5904 while ((per_cu = iter.next ()) != NULL)
5905 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5906 expansion_notify);
3b00ef10 5907 return true;
44ed8f3e 5908 });
9291a0cd
TT
5909}
5910
927aa2e7 5911const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5912{
5913 dw2_has_symbols,
5914 dw2_find_last_source_symtab,
5915 dw2_forget_cached_source_info,
f8eba3c6 5916 dw2_map_symtabs_matching_filename,
927aa2e7 5917 dw2_debug_names_lookup_symbol,
9291a0cd 5918 dw2_print_stats,
927aa2e7 5919 dw2_debug_names_dump,
927aa2e7 5920 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5921 dw2_expand_all_symtabs,
652a8996 5922 dw2_expand_symtabs_with_fullname,
3b00ef10 5923 dw2_debug_names_map_matching_symbols,
927aa2e7 5924 dw2_debug_names_expand_symtabs_matching,
43f3e411 5925 dw2_find_pc_sect_compunit_symtab,
71a3c369 5926 NULL,
9291a0cd
TT
5927 dw2_map_symbol_filenames
5928};
5929
4485a1c1
SM
5930/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5931 to either a dwarf2_per_objfile or dwz_file object. */
5932
5933template <typename T>
5934static gdb::array_view<const gdb_byte>
5935get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5936{
5937 dwarf2_section_info *section = &section_owner->gdb_index;
5938
96b79293 5939 if (section->empty ())
4485a1c1
SM
5940 return {};
5941
5942 /* Older elfutils strip versions could keep the section in the main
5943 executable while splitting it for the separate debug info file. */
96b79293 5944 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5945 return {};
5946
96b79293 5947 section->read (obj);
4485a1c1 5948
8bebfcda
PA
5949 /* dwarf2_section_info::size is a bfd_size_type, while
5950 gdb::array_view works with size_t. On 32-bit hosts, with
5951 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5952 is 32-bit. So we need an explicit narrowing conversion here.
5953 This is fine, because it's impossible to allocate or mmap an
5954 array/buffer larger than what size_t can represent. */
5955 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5956}
5957
87d6a7aa
SM
5958/* Lookup the index cache for the contents of the index associated to
5959 DWARF2_OBJ. */
5960
5961static gdb::array_view<const gdb_byte>
5962get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5963{
5964 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5965 if (build_id == nullptr)
5966 return {};
5967
5968 return global_index_cache.lookup_gdb_index (build_id,
5969 &dwarf2_obj->index_cache_res);
5970}
5971
5972/* Same as the above, but for DWZ. */
5973
5974static gdb::array_view<const gdb_byte>
5975get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5976{
5977 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5978 if (build_id == nullptr)
5979 return {};
5980
5981 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5982}
5983
3c0aa29a 5984/* See symfile.h. */
9291a0cd 5985
3c0aa29a
PA
5986bool
5987dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5988{
ed2dc618
SM
5989 struct dwarf2_per_objfile *dwarf2_per_objfile
5990 = get_dwarf2_per_objfile (objfile);
5991
9291a0cd
TT
5992 /* If we're about to read full symbols, don't bother with the
5993 indices. In this case we also don't care if some other debug
5994 format is making psymtabs, because they are all about to be
5995 expanded anyway. */
5996 if ((objfile->flags & OBJF_READNOW))
5997 {
9291a0cd 5998 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5999 create_all_comp_units (dwarf2_per_objfile);
6000 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6001 dwarf2_per_objfile->quick_file_names_table
6002 = create_quick_file_names_table
6003 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6004
b76e467d 6005 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6006 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6007 {
ff4c9fec 6008 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6009
e254ef6a
DE
6010 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6011 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6012 }
6013
6014 /* Return 1 so that gdb sees the "quick" functions. However,
6015 these functions will be no-ops because we will have expanded
6016 all symtabs. */
3c0aa29a
PA
6017 *index_kind = dw_index_kind::GDB_INDEX;
6018 return true;
9291a0cd
TT
6019 }
6020
ed2dc618 6021 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6022 {
6023 *index_kind = dw_index_kind::DEBUG_NAMES;
6024 return true;
6025 }
927aa2e7 6026
4485a1c1
SM
6027 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6028 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6029 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6030 {
6031 *index_kind = dw_index_kind::GDB_INDEX;
6032 return true;
6033 }
9291a0cd 6034
87d6a7aa
SM
6035 /* ... otherwise, try to find the index in the index cache. */
6036 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6037 get_gdb_index_contents_from_cache,
6038 get_gdb_index_contents_from_cache_dwz))
6039 {
6040 global_index_cache.hit ();
6041 *index_kind = dw_index_kind::GDB_INDEX;
6042 return true;
6043 }
6044
6045 global_index_cache.miss ();
3c0aa29a 6046 return false;
9291a0cd
TT
6047}
6048
6049\f
6050
dce234bc
PP
6051/* Build a partial symbol table. */
6052
6053void
f29dff0a 6054dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6055{
ed2dc618
SM
6056 struct dwarf2_per_objfile *dwarf2_per_objfile
6057 = get_dwarf2_per_objfile (objfile);
c9bf0622 6058
6eee24ce 6059 init_psymbol_list (objfile, 1024);
c906108c 6060
a70b8144 6061 try
c9bf0622
TT
6062 {
6063 /* This isn't really ideal: all the data we allocate on the
6064 objfile's obstack is still uselessly kept around. However,
6065 freeing it seems unsafe. */
906768f9 6066 psymtab_discarder psymtabs (objfile);
ed2dc618 6067 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6068 psymtabs.keep ();
87d6a7aa
SM
6069
6070 /* (maybe) store an index in the cache. */
6071 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6072 }
230d2906 6073 catch (const gdb_exception_error &except)
492d29ea
PA
6074 {
6075 exception_print (gdb_stderr, except);
6076 }
c906108c 6077}
c906108c 6078
1ce1cefd
DE
6079/* Return the total length of the CU described by HEADER. */
6080
6081static unsigned int
6082get_cu_length (const struct comp_unit_head *header)
6083{
6084 return header->initial_length_size + header->length;
6085}
6086
9c541725 6087/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6088
9c541725
PA
6089static inline bool
6090offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6091{
9c541725
PA
6092 sect_offset bottom = cu_header->sect_off;
6093 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6094
9c541725 6095 return sect_off >= bottom && sect_off < top;
45452591
DE
6096}
6097
3b80fe9b
DE
6098/* Find the base address of the compilation unit for range lists and
6099 location lists. It will normally be specified by DW_AT_low_pc.
6100 In DWARF-3 draft 4, the base address could be overridden by
6101 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6102 compilation units with discontinuous ranges. */
6103
6104static void
6105dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6106{
6107 struct attribute *attr;
6108
6109 cu->base_known = 0;
6110 cu->base_address = 0;
6111
6112 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6113 if (attr != nullptr)
3b80fe9b 6114 {
cd6c91b4 6115 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6116 cu->base_known = 1;
6117 }
6118 else
6119 {
6120 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6121 if (attr != nullptr)
3b80fe9b 6122 {
cd6c91b4 6123 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6124 cu->base_known = 1;
6125 }
6126 }
6127}
6128
93311388 6129/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6130 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6131 NOTE: This leaves members offset, first_die_offset to be filled in
6132 by the caller. */
107d2387 6133
d521ce57 6134static const gdb_byte *
107d2387 6135read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6136 const gdb_byte *info_ptr,
6137 struct dwarf2_section_info *section,
6138 rcuh_kind section_kind)
107d2387
AC
6139{
6140 int signed_addr;
891d2f0b 6141 unsigned int bytes_read;
96b79293
TT
6142 const char *filename = section->get_file_name ();
6143 bfd *abfd = section->get_bfd_owner ();
c764a876
DE
6144
6145 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6146 cu_header->initial_length_size = bytes_read;
6147 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6148 info_ptr += bytes_read;
107d2387 6149 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6150 if (cu_header->version < 2 || cu_header->version > 5)
6151 error (_("Dwarf Error: wrong version in compilation unit header "
6152 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6153 cu_header->version, filename);
107d2387 6154 info_ptr += 2;
43988095
JK
6155 if (cu_header->version < 5)
6156 switch (section_kind)
6157 {
6158 case rcuh_kind::COMPILE:
6159 cu_header->unit_type = DW_UT_compile;
6160 break;
6161 case rcuh_kind::TYPE:
6162 cu_header->unit_type = DW_UT_type;
6163 break;
6164 default:
6165 internal_error (__FILE__, __LINE__,
6166 _("read_comp_unit_head: invalid section_kind"));
6167 }
6168 else
6169 {
6170 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6171 (read_1_byte (abfd, info_ptr));
6172 info_ptr += 1;
6173 switch (cu_header->unit_type)
6174 {
6175 case DW_UT_compile:
a084a2a6
AT
6176 case DW_UT_partial:
6177 case DW_UT_skeleton:
6178 case DW_UT_split_compile:
43988095
JK
6179 if (section_kind != rcuh_kind::COMPILE)
6180 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6181 "(is %s, should be %s) [in module %s]"),
6182 dwarf_unit_type_name (cu_header->unit_type),
6183 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6184 break;
6185 case DW_UT_type:
a084a2a6 6186 case DW_UT_split_type:
43988095
JK
6187 section_kind = rcuh_kind::TYPE;
6188 break;
6189 default:
6190 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6191 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6192 "[in module %s]"), cu_header->unit_type,
6193 dwarf_unit_type_name (DW_UT_compile),
6194 dwarf_unit_type_name (DW_UT_skeleton),
6195 dwarf_unit_type_name (DW_UT_split_compile),
6196 dwarf_unit_type_name (DW_UT_type),
6197 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6198 }
6199
6200 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6201 info_ptr += 1;
6202 }
9c541725
PA
6203 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6204 cu_header,
6205 &bytes_read);
613e1657 6206 info_ptr += bytes_read;
43988095
JK
6207 if (cu_header->version < 5)
6208 {
6209 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6210 info_ptr += 1;
6211 }
107d2387
AC
6212 signed_addr = bfd_get_sign_extend_vma (abfd);
6213 if (signed_addr < 0)
8e65ff28 6214 internal_error (__FILE__, __LINE__,
e2e0b3e5 6215 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6216 cu_header->signed_addr_p = signed_addr;
c764a876 6217
a084a2a6
AT
6218 bool header_has_signature = section_kind == rcuh_kind::TYPE
6219 || cu_header->unit_type == DW_UT_skeleton
6220 || cu_header->unit_type == DW_UT_split_compile;
43988095 6221
a084a2a6
AT
6222 if (header_has_signature)
6223 {
43988095
JK
6224 cu_header->signature = read_8_bytes (abfd, info_ptr);
6225 info_ptr += 8;
a084a2a6 6226 }
43988095 6227
a084a2a6
AT
6228 if (section_kind == rcuh_kind::TYPE)
6229 {
6230 LONGEST type_offset;
43988095
JK
6231 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6232 info_ptr += bytes_read;
9c541725
PA
6233 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6234 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6235 error (_("Dwarf Error: Too big type_offset in compilation unit "
6236 "header (is %s) [in module %s]"), plongest (type_offset),
6237 filename);
6238 }
6239
107d2387
AC
6240 return info_ptr;
6241}
6242
36586728
TT
6243/* Helper function that returns the proper abbrev section for
6244 THIS_CU. */
6245
6246static struct dwarf2_section_info *
6247get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6248{
6249 struct dwarf2_section_info *abbrev;
ed2dc618 6250 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6251
6252 if (this_cu->is_dwz)
ed2dc618 6253 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6254 else
6255 abbrev = &dwarf2_per_objfile->abbrev;
6256
6257 return abbrev;
6258}
6259
9ff913ba
DE
6260/* Subroutine of read_and_check_comp_unit_head and
6261 read_and_check_type_unit_head to simplify them.
6262 Perform various error checking on the header. */
6263
6264static void
ed2dc618
SM
6265error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6266 struct comp_unit_head *header,
4bdcc0c1
DE
6267 struct dwarf2_section_info *section,
6268 struct dwarf2_section_info *abbrev_section)
9ff913ba 6269{
96b79293 6270 const char *filename = section->get_file_name ();
9ff913ba 6271
9c541725 6272 if (to_underlying (header->abbrev_sect_off)
36586728 6273 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6274 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6275 "(offset %s + 6) [in module %s]"),
6276 sect_offset_str (header->abbrev_sect_off),
6277 sect_offset_str (header->sect_off),
9ff913ba
DE
6278 filename);
6279
9c541725 6280 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6281 avoid potential 32-bit overflow. */
9c541725 6282 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6283 > section->size)
9c541725 6284 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6285 "(offset %s + 0) [in module %s]"),
6286 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6287 filename);
6288}
6289
6290/* Read in a CU/TU header and perform some basic error checking.
6291 The contents of the header are stored in HEADER.
6292 The result is a pointer to the start of the first DIE. */
adabb602 6293
d521ce57 6294static const gdb_byte *
ed2dc618
SM
6295read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6296 struct comp_unit_head *header,
9ff913ba 6297 struct dwarf2_section_info *section,
4bdcc0c1 6298 struct dwarf2_section_info *abbrev_section,
d521ce57 6299 const gdb_byte *info_ptr,
43988095 6300 rcuh_kind section_kind)
72bf9492 6301{
d521ce57 6302 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6303
9c541725 6304 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6305
43988095 6306 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6307
9c541725 6308 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6309
ed2dc618
SM
6310 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6311 abbrev_section);
9ff913ba
DE
6312
6313 return info_ptr;
348e048f
DE
6314}
6315
f4dc4d17
DE
6316/* Fetch the abbreviation table offset from a comp or type unit header. */
6317
6318static sect_offset
ed2dc618
SM
6319read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6320 struct dwarf2_section_info *section,
9c541725 6321 sect_offset sect_off)
f4dc4d17 6322{
96b79293 6323 bfd *abfd = section->get_bfd_owner ();
d521ce57 6324 const gdb_byte *info_ptr;
ac298888 6325 unsigned int initial_length_size, offset_size;
43988095 6326 uint16_t version;
f4dc4d17 6327
96b79293 6328 section->read (dwarf2_per_objfile->objfile);
9c541725 6329 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6330 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6331 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6332 info_ptr += initial_length_size;
6333
6334 version = read_2_bytes (abfd, info_ptr);
6335 info_ptr += 2;
6336 if (version >= 5)
6337 {
6338 /* Skip unit type and address size. */
6339 info_ptr += 2;
6340 }
6341
9c541725 6342 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6343}
6344
aaa75496
JB
6345/* Allocate a new partial symtab for file named NAME and mark this new
6346 partial symtab as being an include of PST. */
6347
6348static void
891813be 6349dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6350 struct objfile *objfile)
6351{
891813be 6352 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 6353
fbd9ab74
JK
6354 if (!IS_ABSOLUTE_PATH (subpst->filename))
6355 {
6356 /* It shares objfile->objfile_obstack. */
6357 subpst->dirname = pst->dirname;
6358 }
6359
a9342b62 6360 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6361 subpst->dependencies[0] = pst;
6362 subpst->number_of_dependencies = 1;
6363
aaa75496
JB
6364 /* No private part is necessary for include psymtabs. This property
6365 can be used to differentiate between such include psymtabs and
10b3939b 6366 the regular ones. */
891813be 6367 subpst->per_cu_data = nullptr;
aaa75496
JB
6368}
6369
6370/* Read the Line Number Program data and extract the list of files
6371 included by the source file represented by PST. Build an include
d85a05f0 6372 partial symtab for each of these included files. */
aaa75496
JB
6373
6374static void
6375dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6376 struct die_info *die,
891813be 6377 dwarf2_psymtab *pst)
aaa75496 6378{
fff8551c 6379 line_header_up lh;
d85a05f0 6380 struct attribute *attr;
aaa75496 6381
d85a05f0 6382 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6383 if (attr != nullptr)
9c541725 6384 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6385 if (lh == NULL)
6386 return; /* No linetable, so no includes. */
6387
79748972
TT
6388 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6389 that we pass in the raw text_low here; that is ok because we're
6390 only decoding the line table to make include partial symtabs, and
6391 so the addresses aren't really used. */
4ae976d1 6392 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6393 pst->raw_text_low (), 1);
aaa75496
JB
6394}
6395
348e048f 6396static hashval_t
52dc124a 6397hash_signatured_type (const void *item)
348e048f 6398{
9a3c8263
SM
6399 const struct signatured_type *sig_type
6400 = (const struct signatured_type *) item;
9a619af0 6401
348e048f 6402 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6403 return sig_type->signature;
348e048f
DE
6404}
6405
6406static int
52dc124a 6407eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6408{
9a3c8263
SM
6409 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6410 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6411
348e048f
DE
6412 return lhs->signature == rhs->signature;
6413}
6414
1fd400ff
TT
6415/* Allocate a hash table for signatured types. */
6416
6417static htab_t
673bfd45 6418allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6419{
6420 return htab_create_alloc_ex (41,
52dc124a
DE
6421 hash_signatured_type,
6422 eq_signatured_type,
1fd400ff
TT
6423 NULL,
6424 &objfile->objfile_obstack,
6425 hashtab_obstack_allocate,
6426 dummy_obstack_deallocate);
6427}
6428
d467dd73 6429/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6430
6431static int
d467dd73 6432add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6433{
9a3c8263 6434 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6435 std::vector<signatured_type *> *all_type_units
6436 = (std::vector<signatured_type *> *) datum;
1fd400ff 6437
b2bdb8cf 6438 all_type_units->push_back (sigt);
1fd400ff
TT
6439
6440 return 1;
6441}
6442
78d4d2c5 6443/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6444 and fill them into TYPES_HTAB. It will process only type units,
6445 therefore DW_UT_type. */
c88ee1f0 6446
78d4d2c5 6447static void
ed2dc618
SM
6448create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6449 struct dwo_file *dwo_file,
43988095
JK
6450 dwarf2_section_info *section, htab_t &types_htab,
6451 rcuh_kind section_kind)
348e048f 6452{
3019eac3 6453 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6454 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6455 bfd *abfd;
6456 const gdb_byte *info_ptr, *end_ptr;
348e048f 6457
4bdcc0c1
DE
6458 abbrev_section = (dwo_file != NULL
6459 ? &dwo_file->sections.abbrev
6460 : &dwarf2_per_objfile->abbrev);
6461
b4f54984 6462 if (dwarf_read_debug)
43988095 6463 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6464 section->get_name (),
6465 abbrev_section->get_file_name ());
09406207 6466
96b79293 6467 section->read (objfile);
78d4d2c5 6468 info_ptr = section->buffer;
348e048f 6469
78d4d2c5
JK
6470 if (info_ptr == NULL)
6471 return;
348e048f 6472
78d4d2c5
JK
6473 /* We can't set abfd until now because the section may be empty or
6474 not present, in which case the bfd is unknown. */
96b79293 6475 abfd = section->get_bfd_owner ();
348e048f 6476
c0ab21c2
TT
6477 /* We don't use cutu_reader here because we don't need to read
6478 any dies: the signature is in the header. */
3019eac3 6479
78d4d2c5
JK
6480 end_ptr = info_ptr + section->size;
6481 while (info_ptr < end_ptr)
6482 {
78d4d2c5
JK
6483 struct signatured_type *sig_type;
6484 struct dwo_unit *dwo_tu;
6485 void **slot;
6486 const gdb_byte *ptr = info_ptr;
6487 struct comp_unit_head header;
6488 unsigned int length;
8b70b953 6489
9c541725 6490 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6491
a49dd8dd
JK
6492 /* Initialize it due to a false compiler warning. */
6493 header.signature = -1;
9c541725 6494 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6495
78d4d2c5
JK
6496 /* We need to read the type's signature in order to build the hash
6497 table, but we don't need anything else just yet. */
348e048f 6498
ed2dc618 6499 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6500 abbrev_section, ptr, section_kind);
348e048f 6501
78d4d2c5 6502 length = get_cu_length (&header);
6caca83c 6503
78d4d2c5
JK
6504 /* Skip dummy type units. */
6505 if (ptr >= info_ptr + length
43988095
JK
6506 || peek_abbrev_code (abfd, ptr) == 0
6507 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6508 {
6509 info_ptr += length;
6510 continue;
6511 }
dee91e82 6512
78d4d2c5
JK
6513 if (types_htab == NULL)
6514 {
6515 if (dwo_file)
6516 types_htab = allocate_dwo_unit_table (objfile);
6517 else
6518 types_htab = allocate_signatured_type_table (objfile);
6519 }
8b70b953 6520
78d4d2c5
JK
6521 if (dwo_file)
6522 {
6523 sig_type = NULL;
6524 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6525 struct dwo_unit);
6526 dwo_tu->dwo_file = dwo_file;
43988095 6527 dwo_tu->signature = header.signature;
9c541725 6528 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6529 dwo_tu->section = section;
9c541725 6530 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6531 dwo_tu->length = length;
6532 }
6533 else
6534 {
6535 /* N.B.: type_offset is not usable if this type uses a DWO file.
6536 The real type_offset is in the DWO file. */
6537 dwo_tu = NULL;
6538 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6539 struct signatured_type);
43988095 6540 sig_type->signature = header.signature;
9c541725 6541 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6542 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6543 sig_type->per_cu.is_debug_types = 1;
6544 sig_type->per_cu.section = section;
9c541725 6545 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6546 sig_type->per_cu.length = length;
6547 }
6548
6549 slot = htab_find_slot (types_htab,
6550 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6551 INSERT);
6552 gdb_assert (slot != NULL);
6553 if (*slot != NULL)
6554 {
9c541725 6555 sect_offset dup_sect_off;
0349ea22 6556
3019eac3
DE
6557 if (dwo_file)
6558 {
78d4d2c5
JK
6559 const struct dwo_unit *dup_tu
6560 = (const struct dwo_unit *) *slot;
6561
9c541725 6562 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6563 }
6564 else
6565 {
78d4d2c5
JK
6566 const struct signatured_type *dup_tu
6567 = (const struct signatured_type *) *slot;
6568
9c541725 6569 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6570 }
8b70b953 6571
b98664d3 6572 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6573 " the entry at offset %s, signature %s"),
6574 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6575 hex_string (header.signature));
78d4d2c5
JK
6576 }
6577 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6578
78d4d2c5 6579 if (dwarf_read_debug > 1)
9d8780f0
SM
6580 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6581 sect_offset_str (sect_off),
43988095 6582 hex_string (header.signature));
3019eac3 6583
78d4d2c5
JK
6584 info_ptr += length;
6585 }
6586}
3019eac3 6587
78d4d2c5
JK
6588/* Create the hash table of all entries in the .debug_types
6589 (or .debug_types.dwo) section(s).
6590 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6591 otherwise it is NULL.
b3c8eb43 6592
78d4d2c5 6593 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6594
78d4d2c5 6595 Note: This function processes DWO files only, not DWP files. */
348e048f 6596
78d4d2c5 6597static void
ed2dc618
SM
6598create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6599 struct dwo_file *dwo_file,
fd5866f6 6600 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6601 htab_t &types_htab)
6602{
fd5866f6
SM
6603 for (dwarf2_section_info &section : type_sections)
6604 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6605 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6606}
6607
6608/* Create the hash table of all entries in the .debug_types section,
6609 and initialize all_type_units.
6610 The result is zero if there is an error (e.g. missing .debug_types section),
6611 otherwise non-zero. */
6612
6613static int
ed2dc618 6614create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6615{
78d4d2c5 6616 htab_t types_htab = NULL;
3019eac3 6617
ed2dc618
SM
6618 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6619 &dwarf2_per_objfile->info, types_htab,
43988095 6620 rcuh_kind::COMPILE);
ed2dc618
SM
6621 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6622 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6623 if (types_htab == NULL)
6624 {
6625 dwarf2_per_objfile->signatured_types = NULL;
6626 return 0;
6627 }
6628
348e048f
DE
6629 dwarf2_per_objfile->signatured_types = types_htab;
6630
b2bdb8cf
SM
6631 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6632 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6633
6634 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6635 &dwarf2_per_objfile->all_type_units);
1fd400ff 6636
348e048f
DE
6637 return 1;
6638}
6639
6aa5f3a6
DE
6640/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6641 If SLOT is non-NULL, it is the entry to use in the hash table.
6642 Otherwise we find one. */
6643
6644static struct signatured_type *
ed2dc618
SM
6645add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6646 void **slot)
6aa5f3a6
DE
6647{
6648 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6649
b2bdb8cf
SM
6650 if (dwarf2_per_objfile->all_type_units.size ()
6651 == dwarf2_per_objfile->all_type_units.capacity ())
6652 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6653
b2bdb8cf
SM
6654 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6655 struct signatured_type);
6656
6657 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6658 sig_type->signature = sig;
6659 sig_type->per_cu.is_debug_types = 1;
6660 if (dwarf2_per_objfile->using_index)
6661 {
6662 sig_type->per_cu.v.quick =
6663 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6664 struct dwarf2_per_cu_quick_data);
6665 }
6666
6667 if (slot == NULL)
6668 {
6669 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6670 sig_type, INSERT);
6671 }
6672 gdb_assert (*slot == NULL);
6673 *slot = sig_type;
6674 /* The rest of sig_type must be filled in by the caller. */
6675 return sig_type;
6676}
6677
a2ce51a0
DE
6678/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6679 Fill in SIG_ENTRY with DWO_ENTRY. */
6680
6681static void
ed2dc618 6682fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6683 struct signatured_type *sig_entry,
6684 struct dwo_unit *dwo_entry)
6685{
7ee85ab1 6686 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6687 gdb_assert (! sig_entry->per_cu.queued);
6688 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6689 if (dwarf2_per_objfile->using_index)
6690 {
6691 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6692 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6693 }
6694 else
6695 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6696 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6697 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6698 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6699 gdb_assert (sig_entry->dwo_unit == NULL);
6700
6701 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6702 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6703 sig_entry->per_cu.length = dwo_entry->length;
6704 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6705 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6706 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6707 sig_entry->dwo_unit = dwo_entry;
6708}
6709
6710/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6711 If we haven't read the TU yet, create the signatured_type data structure
6712 for a TU to be read in directly from a DWO file, bypassing the stub.
6713 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6714 using .gdb_index, then when reading a CU we want to stay in the DWO file
6715 containing that CU. Otherwise we could end up reading several other DWO
6716 files (due to comdat folding) to process the transitive closure of all the
6717 mentioned TUs, and that can be slow. The current DWO file will have every
6718 type signature that it needs.
a2ce51a0
DE
6719 We only do this for .gdb_index because in the psymtab case we already have
6720 to read all the DWOs to build the type unit groups. */
6721
6722static struct signatured_type *
6723lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6724{
518817b3
SM
6725 struct dwarf2_per_objfile *dwarf2_per_objfile
6726 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6727 struct objfile *objfile = dwarf2_per_objfile->objfile;
6728 struct dwo_file *dwo_file;
6729 struct dwo_unit find_dwo_entry, *dwo_entry;
6730 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6731 void **slot;
a2ce51a0
DE
6732
6733 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6734
6aa5f3a6
DE
6735 /* If TU skeletons have been removed then we may not have read in any
6736 TUs yet. */
6737 if (dwarf2_per_objfile->signatured_types == NULL)
6738 {
6739 dwarf2_per_objfile->signatured_types
6740 = allocate_signatured_type_table (objfile);
6741 }
a2ce51a0
DE
6742
6743 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6744 Use the global signatured_types array to do our own comdat-folding
6745 of types. If this is the first time we're reading this TU, and
6746 the TU has an entry in .gdb_index, replace the recorded data from
6747 .gdb_index with this TU. */
a2ce51a0 6748
a2ce51a0 6749 find_sig_entry.signature = sig;
6aa5f3a6
DE
6750 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6751 &find_sig_entry, INSERT);
9a3c8263 6752 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6753
6754 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6755 read. Don't reassign the global entry to point to this DWO if that's
6756 the case. Also note that if the TU is already being read, it may not
6757 have come from a DWO, the program may be a mix of Fission-compiled
6758 code and non-Fission-compiled code. */
6759
6760 /* Have we already tried to read this TU?
6761 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6762 needn't exist in the global table yet). */
6763 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6764 return sig_entry;
6765
6aa5f3a6
DE
6766 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6767 dwo_unit of the TU itself. */
6768 dwo_file = cu->dwo_unit->dwo_file;
6769
a2ce51a0
DE
6770 /* Ok, this is the first time we're reading this TU. */
6771 if (dwo_file->tus == NULL)
6772 return NULL;
6773 find_dwo_entry.signature = sig;
9a3c8263 6774 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6775 if (dwo_entry == NULL)
6776 return NULL;
6777
6aa5f3a6
DE
6778 /* If the global table doesn't have an entry for this TU, add one. */
6779 if (sig_entry == NULL)
ed2dc618 6780 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6781
ed2dc618 6782 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6783 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6784 return sig_entry;
6785}
6786
a2ce51a0
DE
6787/* Subroutine of lookup_signatured_type.
6788 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6789 then try the DWP file. If the TU stub (skeleton) has been removed then
6790 it won't be in .gdb_index. */
a2ce51a0
DE
6791
6792static struct signatured_type *
6793lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6794{
518817b3
SM
6795 struct dwarf2_per_objfile *dwarf2_per_objfile
6796 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6797 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6798 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6799 struct dwo_unit *dwo_entry;
6800 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6801 void **slot;
a2ce51a0
DE
6802
6803 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6804 gdb_assert (dwp_file != NULL);
6805
6aa5f3a6
DE
6806 /* If TU skeletons have been removed then we may not have read in any
6807 TUs yet. */
6808 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6809 {
6aa5f3a6
DE
6810 dwarf2_per_objfile->signatured_types
6811 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6812 }
6813
6aa5f3a6
DE
6814 find_sig_entry.signature = sig;
6815 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6816 &find_sig_entry, INSERT);
9a3c8263 6817 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6818
6819 /* Have we already tried to read this TU?
6820 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6821 needn't exist in the global table yet). */
6822 if (sig_entry != NULL)
6823 return sig_entry;
6824
a2ce51a0
DE
6825 if (dwp_file->tus == NULL)
6826 return NULL;
ed2dc618 6827 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6828 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6829 if (dwo_entry == NULL)
6830 return NULL;
6831
ed2dc618
SM
6832 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6833 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6834
a2ce51a0
DE
6835 return sig_entry;
6836}
6837
380bca97 6838/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6839 Returns NULL if signature SIG is not present in the table.
6840 It is up to the caller to complain about this. */
348e048f
DE
6841
6842static struct signatured_type *
a2ce51a0 6843lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6844{
518817b3
SM
6845 struct dwarf2_per_objfile *dwarf2_per_objfile
6846 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6847
a2ce51a0
DE
6848 if (cu->dwo_unit
6849 && dwarf2_per_objfile->using_index)
6850 {
6851 /* We're in a DWO/DWP file, and we're using .gdb_index.
6852 These cases require special processing. */
ed2dc618 6853 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6854 return lookup_dwo_signatured_type (cu, sig);
6855 else
6856 return lookup_dwp_signatured_type (cu, sig);
6857 }
6858 else
6859 {
6860 struct signatured_type find_entry, *entry;
348e048f 6861
a2ce51a0
DE
6862 if (dwarf2_per_objfile->signatured_types == NULL)
6863 return NULL;
6864 find_entry.signature = sig;
9a3c8263
SM
6865 entry = ((struct signatured_type *)
6866 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
6867 return entry;
6868 }
348e048f 6869}
18a8505e
AT
6870
6871/* Return the address base of the compile unit, which, if exists, is stored
6872 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6873static gdb::optional<ULONGEST>
6874lookup_addr_base (struct die_info *comp_unit_die)
6875{
6876 struct attribute *attr;
6877 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6878 if (attr == nullptr)
6879 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6880 if (attr == nullptr)
6881 return gdb::optional<ULONGEST> ();
6882 return DW_UNSND (attr);
6883}
6884
6885/* Return range lists base of the compile unit, which, if exists, is stored
6886 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6887static ULONGEST
6888lookup_ranges_base (struct die_info *comp_unit_die)
6889{
6890 struct attribute *attr;
6891 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6892 if (attr == nullptr)
6893 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6894 if (attr == nullptr)
6895 return 0;
6896 return DW_UNSND (attr);
6897}
6898
42e7ad6c 6899/* Low level DIE reading support. */
348e048f 6900
d85a05f0
DJ
6901/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6902
6903static void
6904init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6905 struct dwarf2_cu *cu,
3019eac3 6906 struct dwarf2_section_info *section,
685af9cd
TT
6907 struct dwo_file *dwo_file,
6908 struct abbrev_table *abbrev_table)
d85a05f0 6909{
fceca515 6910 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6911 reader->abfd = section->get_bfd_owner ();
d85a05f0 6912 reader->cu = cu;
3019eac3 6913 reader->dwo_file = dwo_file;
dee91e82
DE
6914 reader->die_section = section;
6915 reader->buffer = section->buffer;
f664829e 6916 reader->buffer_end = section->buffer + section->size;
a2ce51a0 6917 reader->comp_dir = NULL;
685af9cd 6918 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6919}
6920
c0ab21c2 6921/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6922 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6923 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6924 already.
6925
6926 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6927 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6928 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6929 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6930 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6931 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
6932 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6933 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6934 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6935 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6936 kept around for at least as long as *RESULT_READER.
6937
b0c7bfa9
DE
6938 The result is non-zero if a valid (non-dummy) DIE was found. */
6939
6940static int
6941read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6942 struct dwo_unit *dwo_unit,
b0c7bfa9 6943 struct die_info *stub_comp_unit_die,
a2ce51a0 6944 const char *stub_comp_dir,
b0c7bfa9 6945 struct die_reader_specs *result_reader,
d521ce57 6946 const gdb_byte **result_info_ptr,
b0c7bfa9 6947 struct die_info **result_comp_unit_die,
685af9cd
TT
6948 int *result_has_children,
6949 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6950{
ed2dc618 6951 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6952 struct objfile *objfile = dwarf2_per_objfile->objfile;
6953 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6954 bfd *abfd;
d521ce57 6955 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6956 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6957 int i,num_extra_attrs;
6958 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6959 struct die_info *comp_unit_die;
6960
b0aeadb3
DE
6961 /* At most one of these may be provided. */
6962 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6963
b0c7bfa9
DE
6964 /* These attributes aren't processed until later:
6965 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6966 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6967 referenced later. However, these attributes are found in the stub
6968 which we won't have later. In order to not impose this complication
6969 on the rest of the code, we read them here and copy them to the
6970 DWO CU/TU die. */
b0c7bfa9
DE
6971
6972 stmt_list = NULL;
6973 low_pc = NULL;
6974 high_pc = NULL;
6975 ranges = NULL;
6976 comp_dir = NULL;
6977
6978 if (stub_comp_unit_die != NULL)
6979 {
6980 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6981 DWO file. */
6982 if (! this_cu->is_debug_types)
6983 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6984 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6985 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6986 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6987 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6988
18a8505e 6989 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6990
18a8505e
AT
6991 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6992 here (if needed). We need the value before we can process
6993 DW_AT_ranges. */
6994 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6995 }
a2ce51a0
DE
6996 else if (stub_comp_dir != NULL)
6997 {
6998 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6999 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7000 comp_dir->name = DW_AT_comp_dir;
7001 comp_dir->form = DW_FORM_string;
7002 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7003 DW_STRING (comp_dir) = stub_comp_dir;
7004 }
b0c7bfa9
DE
7005
7006 /* Set up for reading the DWO CU/TU. */
7007 cu->dwo_unit = dwo_unit;
685af9cd 7008 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
7009 section->read (objfile);
7010 abfd = section->get_bfd_owner ();
9c541725
PA
7011 begin_info_ptr = info_ptr = (section->buffer
7012 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7013 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7014
7015 if (this_cu->is_debug_types)
7016 {
b0c7bfa9
DE
7017 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7018
ed2dc618
SM
7019 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7020 &cu->header, section,
b0c7bfa9 7021 dwo_abbrev_section,
43988095 7022 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7023 /* This is not an assert because it can be caused by bad debug info. */
43988095 7024 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7025 {
7026 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7027 " TU at offset %s [in module %s]"),
a2ce51a0 7028 hex_string (sig_type->signature),
43988095 7029 hex_string (cu->header.signature),
9d8780f0 7030 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7031 bfd_get_filename (abfd));
7032 }
9c541725 7033 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7034 /* For DWOs coming from DWP files, we don't know the CU length
7035 nor the type's offset in the TU until now. */
7036 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7037 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7038
7039 /* Establish the type offset that can be used to lookup the type.
7040 For DWO files, we don't know it until now. */
9c541725
PA
7041 sig_type->type_offset_in_section
7042 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7043 }
7044 else
7045 {
ed2dc618
SM
7046 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7047 &cu->header, section,
b0c7bfa9 7048 dwo_abbrev_section,
43988095 7049 info_ptr, rcuh_kind::COMPILE);
9c541725 7050 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7051 /* For DWOs coming from DWP files, we don't know the CU length
7052 until now. */
7053 dwo_unit->length = get_cu_length (&cu->header);
7054 }
7055
685af9cd 7056 *result_dwo_abbrev_table
3054dd54 7057 = abbrev_table_read_table (objfile, dwo_abbrev_section,
685af9cd
TT
7058 cu->header.abbrev_sect_off);
7059 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7060 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7061
7062 /* Read in the die, but leave space to copy over the attributes
7063 from the stub. This has the benefit of simplifying the rest of
7064 the code - all the work to maintain the illusion of a single
7065 DW_TAG_{compile,type}_unit DIE is done here. */
7066 num_extra_attrs = ((stmt_list != NULL)
7067 + (low_pc != NULL)
7068 + (high_pc != NULL)
7069 + (ranges != NULL)
7070 + (comp_dir != NULL));
7071 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7072 result_has_children, num_extra_attrs);
7073
7074 /* Copy over the attributes from the stub to the DIE we just read in. */
7075 comp_unit_die = *result_comp_unit_die;
7076 i = comp_unit_die->num_attrs;
7077 if (stmt_list != NULL)
7078 comp_unit_die->attrs[i++] = *stmt_list;
7079 if (low_pc != NULL)
7080 comp_unit_die->attrs[i++] = *low_pc;
7081 if (high_pc != NULL)
7082 comp_unit_die->attrs[i++] = *high_pc;
7083 if (ranges != NULL)
7084 comp_unit_die->attrs[i++] = *ranges;
7085 if (comp_dir != NULL)
7086 comp_unit_die->attrs[i++] = *comp_dir;
7087 comp_unit_die->num_attrs += num_extra_attrs;
7088
b4f54984 7089 if (dwarf_die_debug)
bf6af496
DE
7090 {
7091 fprintf_unfiltered (gdb_stdlog,
7092 "Read die from %s@0x%x of %s:\n",
96b79293 7093 section->get_name (),
bf6af496
DE
7094 (unsigned) (begin_info_ptr - section->buffer),
7095 bfd_get_filename (abfd));
b4f54984 7096 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7097 }
7098
a2ce51a0
DE
7099 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7100 TUs by skipping the stub and going directly to the entry in the DWO file.
7101 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7102 to get it via circuitous means. Blech. */
7103 if (comp_dir != NULL)
7104 result_reader->comp_dir = DW_STRING (comp_dir);
7105
b0c7bfa9
DE
7106 /* Skip dummy compilation units. */
7107 if (info_ptr >= begin_info_ptr + dwo_unit->length
7108 || peek_abbrev_code (abfd, info_ptr) == 0)
7109 return 0;
7110
7111 *result_info_ptr = info_ptr;
7112 return 1;
7113}
7114
a084a2a6
AT
7115/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7116 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7117 signature is part of the header. */
7118static gdb::optional<ULONGEST>
7119lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7120{
7121 if (cu->header.version >= 5)
7122 return cu->header.signature;
7123 struct attribute *attr;
7124 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7125 if (attr == nullptr)
7126 return gdb::optional<ULONGEST> ();
7127 return DW_UNSND (attr);
7128}
7129
c0ab21c2 7130/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7131 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7132 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7133
7134static struct dwo_unit *
7135lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
7136 struct die_info *comp_unit_die,
7137 const char *dwo_name)
b0c7bfa9
DE
7138{
7139 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7140 struct dwo_unit *dwo_unit;
c0ab21c2 7141 const char *comp_dir;
b0c7bfa9 7142
a2ce51a0
DE
7143 gdb_assert (cu != NULL);
7144
b0c7bfa9 7145 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7146 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7147 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7148
7149 if (this_cu->is_debug_types)
7150 {
7151 struct signatured_type *sig_type;
7152
7153 /* Since this_cu is the first member of struct signatured_type,
7154 we can go from a pointer to one to a pointer to the other. */
7155 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7156 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7157 }
7158 else
7159 {
a084a2a6
AT
7160 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7161 if (!signature.has_value ())
b0c7bfa9
DE
7162 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7163 " [in module %s]"),
e3b94546 7164 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7165 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7166 *signature);
b0c7bfa9
DE
7167 }
7168
b0c7bfa9
DE
7169 return dwo_unit;
7170}
7171
c0ab21c2 7172/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7173 See it for a description of the parameters.
fcd3b13d 7174 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7175
c0ab21c2
TT
7176void
7177cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7178 int use_existing_cu, int keep)
a2ce51a0 7179{
a2ce51a0 7180 struct signatured_type *sig_type;
a2ce51a0 7181 struct die_reader_specs reader;
a2ce51a0
DE
7182
7183 /* Verify we can do the following downcast, and that we have the
7184 data we need. */
7185 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7186 sig_type = (struct signatured_type *) this_cu;
7187 gdb_assert (sig_type->dwo_unit != NULL);
7188
6aa5f3a6
DE
7189 if (use_existing_cu && this_cu->cu != NULL)
7190 {
7191 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7192 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7193 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7194 }
7195 else
7196 {
7197 /* If !use_existing_cu, this_cu->cu must be NULL. */
7198 gdb_assert (this_cu->cu == NULL);
c0ab21c2 7199 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7200 }
7201
7202 /* A future optimization, if needed, would be to use an existing
7203 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7204 could share abbrev tables. */
a2ce51a0
DE
7205
7206 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7207 NULL /* stub_comp_unit_die */,
7208 sig_type->dwo_unit->dwo_file->comp_dir,
7209 &reader, &info_ptr,
685af9cd 7210 &comp_unit_die, &has_children,
c0ab21c2 7211 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7212 {
7213 /* Dummy die. */
c0ab21c2 7214 dummy_p = true;
a2ce51a0 7215 }
a2ce51a0
DE
7216}
7217
fd820528 7218/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7219 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7220
f4dc4d17
DE
7221 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7222 Otherwise the table specified in the comp unit header is read in and used.
7223 This is an optimization for when we already have the abbrev table.
7224
dee91e82
DE
7225 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7226 Otherwise, a new CU is allocated with xmalloc.
7227
7228 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
c0ab21c2
TT
7229 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7230 end. */
aaa75496 7231
c0ab21c2
TT
7232cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7233 struct abbrev_table *abbrev_table,
7234 int use_existing_cu, int keep,
7235 bool skip_partial)
7236 : die_reader_specs {},
7237 m_this_cu (this_cu),
7238 m_keep (keep)
c906108c 7239{
ed2dc618 7240 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7241 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7242 struct dwarf2_section_info *section = this_cu->section;
96b79293 7243 bfd *abfd = section->get_bfd_owner ();
dee91e82 7244 struct dwarf2_cu *cu;
c0ab21c2 7245 const gdb_byte *begin_info_ptr;
dee91e82 7246 struct signatured_type *sig_type = NULL;
4bdcc0c1 7247 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7248 /* Non-zero if CU currently points to a DWO file and we need to
7249 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7250 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7251 int rereading_dwo_cu = 0;
c906108c 7252
b4f54984 7253 if (dwarf_die_debug)
9d8780f0 7254 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7255 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7256 sect_offset_str (this_cu->sect_off));
09406207 7257
dee91e82
DE
7258 if (use_existing_cu)
7259 gdb_assert (keep);
23745b47 7260
a2ce51a0
DE
7261 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7262 file (instead of going through the stub), short-circuit all of this. */
7263 if (this_cu->reading_dwo_directly)
7264 {
7265 /* Narrow down the scope of possibilities to have to understand. */
7266 gdb_assert (this_cu->is_debug_types);
7267 gdb_assert (abbrev_table == NULL);
c0ab21c2 7268 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
a2ce51a0
DE
7269 return;
7270 }
7271
dee91e82 7272 /* This is cheap if the section is already read in. */
96b79293 7273 section->read (objfile);
dee91e82 7274
9c541725 7275 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7276
7277 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7278
7279 if (use_existing_cu && this_cu->cu != NULL)
7280 {
7281 cu = this_cu->cu;
42e7ad6c
DE
7282 /* If this CU is from a DWO file we need to start over, we need to
7283 refetch the attributes from the skeleton CU.
7284 This could be optimized by retrieving those attributes from when we
7285 were here the first time: the previous comp_unit_die was stored in
7286 comp_unit_obstack. But there's no data yet that we need this
7287 optimization. */
7288 if (cu->dwo_unit != NULL)
7289 rereading_dwo_cu = 1;
dee91e82
DE
7290 }
7291 else
7292 {
7293 /* If !use_existing_cu, this_cu->cu must be NULL. */
7294 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
7295 m_new_cu.reset (new dwarf2_cu (this_cu));
7296 cu = m_new_cu.get ();
42e7ad6c 7297 }
dee91e82 7298
b0c7bfa9 7299 /* Get the header. */
9c541725 7300 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7301 {
7302 /* We already have the header, there's no need to read it in again. */
9c541725 7303 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7304 }
7305 else
7306 {
3019eac3 7307 if (this_cu->is_debug_types)
dee91e82 7308 {
ed2dc618
SM
7309 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7310 &cu->header, section,
4bdcc0c1 7311 abbrev_section, info_ptr,
43988095 7312 rcuh_kind::TYPE);
dee91e82 7313
42e7ad6c
DE
7314 /* Since per_cu is the first member of struct signatured_type,
7315 we can go from a pointer to one to a pointer to the other. */
7316 sig_type = (struct signatured_type *) this_cu;
43988095 7317 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7318 gdb_assert (sig_type->type_offset_in_tu
7319 == cu->header.type_cu_offset_in_tu);
7320 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7321
42e7ad6c
DE
7322 /* LENGTH has not been set yet for type units if we're
7323 using .gdb_index. */
1ce1cefd 7324 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7325
7326 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7327 sig_type->type_offset_in_section =
7328 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7329
7330 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7331 }
7332 else
7333 {
ed2dc618
SM
7334 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7335 &cu->header, section,
4bdcc0c1 7336 abbrev_section,
43988095
JK
7337 info_ptr,
7338 rcuh_kind::COMPILE);
dee91e82 7339
9c541725 7340 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7341 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7342 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7343 }
7344 }
10b3939b 7345
6caca83c 7346 /* Skip dummy compilation units. */
dee91e82 7347 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7348 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7349 {
7350 dummy_p = true;
7351 return;
7352 }
6caca83c 7353
433df2d4
DE
7354 /* If we don't have them yet, read the abbrevs for this compilation unit.
7355 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7356 done. */
f4dc4d17 7357 if (abbrev_table != NULL)
685af9cd
TT
7358 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7359 else
f4dc4d17 7360 {
c0ab21c2 7361 m_abbrev_table_holder
3054dd54 7362 = abbrev_table_read_table (objfile, abbrev_section,
685af9cd 7363 cu->header.abbrev_sect_off);
c0ab21c2 7364 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7365 }
af703f96 7366
dee91e82 7367 /* Read the top level CU/TU die. */
c0ab21c2
TT
7368 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7369 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
93311388 7370
58f0c718 7371 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7372 {
7373 dummy_p = true;
7374 return;
7375 }
58f0c718 7376
b0c7bfa9 7377 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7378 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7379 table from the DWO file and pass the ownership over to us. It will be
7380 referenced from READER, so we must make sure to free it after we're done
7381 with READER.
7382
b0c7bfa9
DE
7383 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7384 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7385 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7386 if (dwo_name != nullptr)
3019eac3 7387 {
3019eac3 7388 struct dwo_unit *dwo_unit;
b0c7bfa9 7389 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7390
7391 if (has_children)
6a506a2d 7392 {
b98664d3 7393 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7394 " has children (offset %s) [in module %s]"),
7395 sect_offset_str (this_cu->sect_off),
7396 bfd_get_filename (abfd));
6a506a2d 7397 }
c0ab21c2 7398 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7399 if (dwo_unit != NULL)
3019eac3 7400 {
6a506a2d 7401 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7402 comp_unit_die, NULL,
c0ab21c2 7403 this, &info_ptr,
685af9cd 7404 &dwo_comp_unit_die, &has_children,
c0ab21c2 7405 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7406 {
7407 /* Dummy die. */
c0ab21c2 7408 dummy_p = true;
6a506a2d
DE
7409 return;
7410 }
7411 comp_unit_die = dwo_comp_unit_die;
7412 }
7413 else
7414 {
7415 /* Yikes, we couldn't find the rest of the DIE, we only have
7416 the stub. A complaint has already been logged. There's
7417 not much more we can do except pass on the stub DIE to
7418 die_reader_func. We don't want to throw an error on bad
7419 debug info. */
3019eac3
DE
7420 }
7421 }
c0ab21c2 7422}
3019eac3 7423
c0ab21c2
TT
7424cutu_reader::~cutu_reader ()
7425{
b0c7bfa9 7426 /* Done, clean up. */
c0ab21c2 7427 if (m_new_cu != NULL && m_keep && !dummy_p)
348e048f 7428 {
c0ab21c2
TT
7429 struct dwarf2_per_objfile *dwarf2_per_objfile
7430 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7431 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7432 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7433 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7434 /* The chain owns it now. */
c0ab21c2 7435 m_new_cu.release ();
348e048f 7436 }
dee91e82
DE
7437}
7438
18a8505e
AT
7439/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7440 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7441 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7442
7443 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7444 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7445
7446 We fill in THIS_CU->length.
7447
dee91e82 7448 THIS_CU->cu is always freed when done.
3019eac3 7449 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7450 to care whether it refers to the "main" CU or the DWO CU.
7451
7452 When parent_cu is passed, it is used to provide a default value for
7453 str_offsets_base and addr_base from the parent. */
dee91e82 7454
c0ab21c2
TT
7455cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7456 struct dwarf2_cu *parent_cu,
7457 struct dwo_file *dwo_file)
7458 : die_reader_specs {},
7459 m_this_cu (this_cu)
dee91e82 7460{
ed2dc618 7461 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7462 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7463 struct dwarf2_section_info *section = this_cu->section;
96b79293 7464 bfd *abfd = section->get_bfd_owner ();
33e80786 7465 struct dwarf2_section_info *abbrev_section;
d521ce57 7466 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
7467 int has_children;
7468
b4f54984 7469 if (dwarf_die_debug)
9d8780f0 7470 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7471 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7472 sect_offset_str (this_cu->sect_off));
09406207 7473
dee91e82
DE
7474 gdb_assert (this_cu->cu == NULL);
7475
33e80786
DE
7476 abbrev_section = (dwo_file != NULL
7477 ? &dwo_file->sections.abbrev
7478 : get_abbrev_section_for_cu (this_cu));
7479
dee91e82 7480 /* This is cheap if the section is already read in. */
96b79293 7481 section->read (objfile);
dee91e82 7482
c0ab21c2 7483 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7484
9c541725 7485 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7486 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7487 &m_new_cu->header, section,
4bdcc0c1 7488 abbrev_section, info_ptr,
43988095
JK
7489 (this_cu->is_debug_types
7490 ? rcuh_kind::TYPE
7491 : rcuh_kind::COMPILE));
dee91e82 7492
18a8505e
AT
7493 if (parent_cu != nullptr)
7494 {
c0ab21c2
TT
7495 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7496 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7497 }
c0ab21c2 7498 this_cu->length = get_cu_length (&m_new_cu->header);
dee91e82
DE
7499
7500 /* Skip dummy compilation units. */
7501 if (info_ptr >= begin_info_ptr + this_cu->length
7502 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7503 {
7504 dummy_p = true;
7505 return;
7506 }
72bf9492 7507
c0ab21c2 7508 m_abbrev_table_holder
3054dd54 7509 = abbrev_table_read_table (objfile, abbrev_section,
c0ab21c2 7510 m_new_cu->header.abbrev_sect_off);
dee91e82 7511
c0ab21c2
TT
7512 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7513 m_abbrev_table_holder.get ());
7514 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
dee91e82
DE
7515}
7516
0018ea6f
DE
7517\f
7518/* Type Unit Groups.
dee91e82 7519
0018ea6f
DE
7520 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7521 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7522 so that all types coming from the same compilation (.o file) are grouped
7523 together. A future step could be to put the types in the same symtab as
7524 the CU the types ultimately came from. */
ff013f42 7525
f4dc4d17
DE
7526static hashval_t
7527hash_type_unit_group (const void *item)
7528{
9a3c8263
SM
7529 const struct type_unit_group *tu_group
7530 = (const struct type_unit_group *) item;
f4dc4d17 7531
094b34ac 7532 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7533}
348e048f
DE
7534
7535static int
f4dc4d17 7536eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7537{
9a3c8263
SM
7538 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7539 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7540
094b34ac 7541 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7542}
348e048f 7543
f4dc4d17
DE
7544/* Allocate a hash table for type unit groups. */
7545
7546static htab_t
ed2dc618 7547allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7548{
7549 return htab_create_alloc_ex (3,
7550 hash_type_unit_group,
7551 eq_type_unit_group,
7552 NULL,
ed2dc618 7553 &objfile->objfile_obstack,
f4dc4d17
DE
7554 hashtab_obstack_allocate,
7555 dummy_obstack_deallocate);
7556}
dee91e82 7557
f4dc4d17
DE
7558/* Type units that don't have DW_AT_stmt_list are grouped into their own
7559 partial symtabs. We combine several TUs per psymtab to not let the size
7560 of any one psymtab grow too big. */
7561#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7562#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7563
094b34ac 7564/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7565 Create the type_unit_group object used to hold one or more TUs. */
7566
7567static struct type_unit_group *
094b34ac 7568create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7569{
518817b3
SM
7570 struct dwarf2_per_objfile *dwarf2_per_objfile
7571 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7572 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7573 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7574 struct type_unit_group *tu_group;
f4dc4d17
DE
7575
7576 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7577 struct type_unit_group);
094b34ac 7578 per_cu = &tu_group->per_cu;
518817b3 7579 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7580
094b34ac
DE
7581 if (dwarf2_per_objfile->using_index)
7582 {
7583 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7584 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7585 }
7586 else
7587 {
9c541725 7588 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7589 dwarf2_psymtab *pst;
528e1572 7590 std::string name;
094b34ac
DE
7591
7592 /* Give the symtab a useful name for debug purposes. */
7593 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7594 name = string_printf ("<type_units_%d>",
7595 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7596 else
528e1572 7597 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7598
528e1572 7599 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7600 pst->anonymous = true;
094b34ac 7601 }
f4dc4d17 7602
094b34ac 7603 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7604 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7605
7606 return tu_group;
7607}
7608
094b34ac
DE
7609/* Look up the type_unit_group for type unit CU, and create it if necessary.
7610 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7611
7612static struct type_unit_group *
ff39bb5e 7613get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7614{
518817b3
SM
7615 struct dwarf2_per_objfile *dwarf2_per_objfile
7616 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7617 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7618 struct type_unit_group *tu_group;
7619 void **slot;
7620 unsigned int line_offset;
7621 struct type_unit_group type_unit_group_for_lookup;
7622
7623 if (dwarf2_per_objfile->type_unit_groups == NULL)
7624 {
7625 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7626 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7627 }
7628
7629 /* Do we need to create a new group, or can we use an existing one? */
7630
7631 if (stmt_list)
7632 {
7633 line_offset = DW_UNSND (stmt_list);
7634 ++tu_stats->nr_symtab_sharers;
7635 }
7636 else
7637 {
7638 /* Ugh, no stmt_list. Rare, but we have to handle it.
7639 We can do various things here like create one group per TU or
7640 spread them over multiple groups to split up the expansion work.
7641 To avoid worst case scenarios (too many groups or too large groups)
7642 we, umm, group them in bunches. */
7643 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7644 | (tu_stats->nr_stmt_less_type_units
7645 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7646 ++tu_stats->nr_stmt_less_type_units;
7647 }
7648
094b34ac 7649 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7650 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7651 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7652 &type_unit_group_for_lookup, INSERT);
7653 if (*slot != NULL)
7654 {
9a3c8263 7655 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7656 gdb_assert (tu_group != NULL);
7657 }
7658 else
7659 {
9c541725 7660 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7661 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7662 *slot = tu_group;
7663 ++tu_stats->nr_symtabs;
7664 }
7665
7666 return tu_group;
7667}
0018ea6f
DE
7668\f
7669/* Partial symbol tables. */
7670
7671/* Create a psymtab named NAME and assign it to PER_CU.
7672
7673 The caller must fill in the following details:
7674 dirname, textlow, texthigh. */
7675
891813be 7676static dwarf2_psymtab *
0018ea6f
DE
7677create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7678{
e3b94546 7679 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7680 dwarf2_psymtab *pst;
0018ea6f 7681
891813be 7682 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7683
6d94535f 7684 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7685
7686 /* This is the glue that links PST into GDB's symbol API. */
891813be 7687 pst->per_cu_data = per_cu;
0018ea6f
DE
7688 per_cu->v.psymtab = pst;
7689
7690 return pst;
7691}
7692
c0ab21c2 7693/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7694
7695static void
7696process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7697 const gdb_byte *info_ptr,
0018ea6f
DE
7698 struct die_info *comp_unit_die,
7699 int has_children,
c0ab21c2
TT
7700 int want_partial_unit,
7701 enum language pretend_language)
0018ea6f
DE
7702{
7703 struct dwarf2_cu *cu = reader->cu;
518817b3 7704 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7705 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7706 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7707 CORE_ADDR baseaddr;
7708 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7709 dwarf2_psymtab *pst;
3a2b436a 7710 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7711 const char *filename;
0018ea6f 7712
c0ab21c2 7713 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
0018ea6f
DE
7714 return;
7715
7716 gdb_assert (! per_cu->is_debug_types);
7717
c0ab21c2 7718 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7719
0018ea6f 7720 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7721 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7722 if (filename == NULL)
0018ea6f 7723 filename = "";
0018ea6f
DE
7724
7725 pst = create_partial_symtab (per_cu, filename);
7726
7727 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7728 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7729
b3b3bada 7730 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7731
7732 dwarf2_find_base_address (comp_unit_die, cu);
7733
7734 /* Possibly set the default values of LOWPC and HIGHPC from
7735 `DW_AT_ranges'. */
3a2b436a
JK
7736 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7737 &best_highpc, cu, pst);
7738 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7739 {
7740 CORE_ADDR low
7741 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7742 - baseaddr);
7743 CORE_ADDR high
7744 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7745 - baseaddr - 1);
7746 /* Store the contiguous range if it is not empty; it can be
7747 empty for CUs with no code. */
d320c2b5
TT
7748 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7749 low, high, pst);
79748972 7750 }
0018ea6f
DE
7751
7752 /* Check if comp unit has_children.
7753 If so, read the rest of the partial symbols from this comp unit.
7754 If not, there's no more debug_info for this comp unit. */
7755 if (has_children)
7756 {
7757 struct partial_die_info *first_die;
7758 CORE_ADDR lowpc, highpc;
7759
7760 lowpc = ((CORE_ADDR) -1);
7761 highpc = ((CORE_ADDR) 0);
7762
7763 first_die = load_partial_dies (reader, info_ptr, 1);
7764
7765 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7766 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7767
7768 /* If we didn't find a lowpc, set it to highpc to avoid
7769 complaints from `maint check'. */
7770 if (lowpc == ((CORE_ADDR) -1))
7771 lowpc = highpc;
7772
7773 /* If the compilation unit didn't have an explicit address range,
7774 then use the information extracted from its child dies. */
e385593e 7775 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7776 {
7777 best_lowpc = lowpc;
7778 best_highpc = highpc;
7779 }
7780 }
4ae976d1 7781 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7782 best_lowpc + baseaddr)
7783 - baseaddr);
4ae976d1 7784 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7785 best_highpc + baseaddr)
7786 - baseaddr);
0018ea6f 7787
8763cede 7788 end_psymtab_common (objfile, pst);
0018ea6f 7789
ae640021 7790 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7791 {
7792 int i;
ae640021 7793 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7794
7795 /* Fill in 'dependencies' here; we fill in 'users' in a
7796 post-pass. */
7797 pst->number_of_dependencies = len;
a9342b62
TT
7798 pst->dependencies
7799 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7800 for (i = 0; i < len; ++i)
7801 {
7802 pst->dependencies[i]
7803 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7804 }
0018ea6f 7805
ae640021 7806 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7807 }
7808
7809 /* Get the list of files included in the current compilation unit,
7810 and build a psymtab for each of them. */
7811 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7812
b4f54984 7813 if (dwarf_read_debug)
b926417a
TT
7814 fprintf_unfiltered (gdb_stdlog,
7815 "Psymtab for %s unit @%s: %s - %s"
7816 ", %d global, %d static syms\n",
7817 per_cu->is_debug_types ? "type" : "comp",
7818 sect_offset_str (per_cu->sect_off),
7819 paddress (gdbarch, pst->text_low (objfile)),
7820 paddress (gdbarch, pst->text_high (objfile)),
7821 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7822}
7823
7824/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7825 Process compilation unit THIS_CU for a psymtab. */
7826
7827static void
7828process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
7829 int want_partial_unit,
7830 enum language pretend_language)
0018ea6f
DE
7831{
7832 /* If this compilation unit was already read in, free the
7833 cached copy in order to read it in again. This is
7834 necessary because we skipped some symbols when we first
7835 read in the compilation unit (see load_partial_dies).
7836 This problem could be avoided, but the benefit is unclear. */
7837 if (this_cu->cu != NULL)
7838 free_one_cached_comp_unit (this_cu);
7839
c0ab21c2
TT
7840 cutu_reader reader (this_cu, NULL, 0, 0, false);
7841
7842 if (reader.dummy_p)
f1902523 7843 {
c0ab21c2 7844 /* Nothing. */
f1902523 7845 }
c0ab21c2
TT
7846 else if (this_cu->is_debug_types)
7847 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
7848 reader.has_children);
7849 else
7850 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7851 reader.comp_unit_die,
7852 reader.has_children,
7853 want_partial_unit,
7854 pretend_language);
0018ea6f
DE
7855
7856 /* Age out any secondary CUs. */
ed2dc618 7857 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7858}
f4dc4d17
DE
7859
7860/* Reader function for build_type_psymtabs. */
7861
7862static void
7863build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7864 const gdb_byte *info_ptr,
f4dc4d17 7865 struct die_info *type_unit_die,
c0ab21c2 7866 int has_children)
f4dc4d17 7867{
ed2dc618 7868 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7869 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7870 struct objfile *objfile = dwarf2_per_objfile->objfile;
7871 struct dwarf2_cu *cu = reader->cu;
7872 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7873 struct signatured_type *sig_type;
f4dc4d17
DE
7874 struct type_unit_group *tu_group;
7875 struct attribute *attr;
7876 struct partial_die_info *first_die;
7877 CORE_ADDR lowpc, highpc;
891813be 7878 dwarf2_psymtab *pst;
f4dc4d17 7879
0186c6a7
DE
7880 gdb_assert (per_cu->is_debug_types);
7881 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7882
7883 if (! has_children)
7884 return;
7885
7886 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7887 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7888
df07e2c7 7889 if (tu_group->tus == nullptr)
a8b3b8e9 7890 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7891 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7892
7893 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7894 pst = create_partial_symtab (per_cu, "");
6d94535f 7895 pst->anonymous = true;
f4dc4d17
DE
7896
7897 first_die = load_partial_dies (reader, info_ptr, 1);
7898
7899 lowpc = (CORE_ADDR) -1;
7900 highpc = (CORE_ADDR) 0;
7901 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7902
8763cede 7903 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7904}
7905
73051182
DE
7906/* Struct used to sort TUs by their abbreviation table offset. */
7907
7908struct tu_abbrev_offset
7909{
b2bdb8cf
SM
7910 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7911 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7912 {}
7913
7914 signatured_type *sig_type;
73051182
DE
7915 sect_offset abbrev_offset;
7916};
7917
484cf504 7918/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7919
484cf504
TT
7920static bool
7921sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7922 const struct tu_abbrev_offset &b)
73051182 7923{
484cf504 7924 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7925}
7926
7927/* Efficiently read all the type units.
7928 This does the bulk of the work for build_type_psymtabs.
7929
7930 The efficiency is because we sort TUs by the abbrev table they use and
7931 only read each abbrev table once. In one program there are 200K TUs
7932 sharing 8K abbrev tables.
7933
7934 The main purpose of this function is to support building the
7935 dwarf2_per_objfile->type_unit_groups table.
7936 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7937 can collapse the search space by grouping them by stmt_list.
7938 The savings can be significant, in the same program from above the 200K TUs
7939 share 8K stmt_list tables.
7940
7941 FUNC is expected to call get_type_unit_group, which will create the
7942 struct type_unit_group if necessary and add it to
7943 dwarf2_per_objfile->type_unit_groups. */
7944
7945static void
ed2dc618 7946build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7947{
73051182 7948 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7949 abbrev_table_up abbrev_table;
73051182 7950 sect_offset abbrev_offset;
73051182
DE
7951
7952 /* It's up to the caller to not call us multiple times. */
7953 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7954
b2bdb8cf 7955 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7956 return;
7957
7958 /* TUs typically share abbrev tables, and there can be way more TUs than
7959 abbrev tables. Sort by abbrev table to reduce the number of times we
7960 read each abbrev table in.
7961 Alternatives are to punt or to maintain a cache of abbrev tables.
7962 This is simpler and efficient enough for now.
7963
7964 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7965 symtab to use). Typically TUs with the same abbrev offset have the same
7966 stmt_list value too so in practice this should work well.
7967
7968 The basic algorithm here is:
7969
7970 sort TUs by abbrev table
7971 for each TU with same abbrev table:
7972 read abbrev table if first user
7973 read TU top level DIE
7974 [IWBN if DWO skeletons had DW_AT_stmt_list]
7975 call FUNC */
7976
b4f54984 7977 if (dwarf_read_debug)
73051182
DE
7978 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7979
7980 /* Sort in a separate table to maintain the order of all_type_units
7981 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7982 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7983 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7984
7985 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7986 sorted_by_abbrev.emplace_back
7987 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7988 sig_type->per_cu.section,
7989 sig_type->per_cu.sect_off));
73051182 7990
484cf504
TT
7991 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7992 sort_tu_by_abbrev_offset);
73051182 7993
9c541725 7994 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7995
b2bdb8cf 7996 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7997 {
73051182
DE
7998 /* Switch to the next abbrev table if necessary. */
7999 if (abbrev_table == NULL
b2bdb8cf 8000 || tu.abbrev_offset != abbrev_offset)
73051182 8001 {
b2bdb8cf 8002 abbrev_offset = tu.abbrev_offset;
73051182 8003 abbrev_table =
3054dd54 8004 abbrev_table_read_table (dwarf2_per_objfile->objfile,
ed2dc618 8005 &dwarf2_per_objfile->abbrev,
73051182
DE
8006 abbrev_offset);
8007 ++tu_stats->nr_uniq_abbrev_tables;
8008 }
8009
c0ab21c2
TT
8010 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8011 0, 0, false);
8012 if (!reader.dummy_p)
8013 build_type_psymtabs_reader (&reader, reader.info_ptr,
8014 reader.comp_unit_die,
8015 reader.has_children);
73051182 8016 }
6aa5f3a6 8017}
73051182 8018
6aa5f3a6
DE
8019/* Print collected type unit statistics. */
8020
8021static void
ed2dc618 8022print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8023{
8024 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8025
8026 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8027 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8028 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8029 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8030 tu_stats->nr_uniq_abbrev_tables);
8031 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8032 tu_stats->nr_symtabs);
8033 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8034 tu_stats->nr_symtab_sharers);
8035 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8036 tu_stats->nr_stmt_less_type_units);
8037 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8038 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8039}
8040
f4dc4d17
DE
8041/* Traversal function for build_type_psymtabs. */
8042
8043static int
8044build_type_psymtab_dependencies (void **slot, void *info)
8045{
ed2dc618
SM
8046 struct dwarf2_per_objfile *dwarf2_per_objfile
8047 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8048 struct objfile *objfile = dwarf2_per_objfile->objfile;
8049 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8050 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 8051 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 8052 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8053 int i;
8054
8055 gdb_assert (len > 0);
0186c6a7 8056 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8057
8058 pst->number_of_dependencies = len;
a9342b62 8059 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8060 for (i = 0; i < len; ++i)
f4dc4d17 8061 {
df07e2c7 8062 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8063 gdb_assert (iter->per_cu.is_debug_types);
8064 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8065 iter->type_unit_group = tu_group;
f4dc4d17
DE
8066 }
8067
df07e2c7
AB
8068 delete tu_group->tus;
8069 tu_group->tus = nullptr;
348e048f
DE
8070
8071 return 1;
8072}
8073
8074/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8075 Build partial symbol tables for the .debug_types comp-units. */
8076
8077static void
ed2dc618 8078build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8079{
ed2dc618 8080 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8081 return;
8082
ed2dc618 8083 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8084}
f4dc4d17 8085
6aa5f3a6
DE
8086/* Traversal function for process_skeletonless_type_unit.
8087 Read a TU in a DWO file and build partial symbols for it. */
8088
8089static int
8090process_skeletonless_type_unit (void **slot, void *info)
8091{
8092 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8093 struct dwarf2_per_objfile *dwarf2_per_objfile
8094 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8095 struct signatured_type find_entry, *entry;
8096
8097 /* If this TU doesn't exist in the global table, add it and read it in. */
8098
8099 if (dwarf2_per_objfile->signatured_types == NULL)
8100 {
8101 dwarf2_per_objfile->signatured_types
ed2dc618 8102 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8103 }
8104
8105 find_entry.signature = dwo_unit->signature;
8106 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8107 INSERT);
8108 /* If we've already seen this type there's nothing to do. What's happening
8109 is we're doing our own version of comdat-folding here. */
8110 if (*slot != NULL)
8111 return 1;
8112
8113 /* This does the job that create_all_type_units would have done for
8114 this TU. */
ed2dc618
SM
8115 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8116 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8117 *slot = entry;
8118
8119 /* This does the job that build_type_psymtabs_1 would have done. */
c0ab21c2
TT
8120 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8121 if (!reader.dummy_p)
8122 build_type_psymtabs_reader (&reader, reader.info_ptr,
8123 reader.comp_unit_die, reader.has_children);
6aa5f3a6
DE
8124
8125 return 1;
8126}
8127
8128/* Traversal function for process_skeletonless_type_units. */
8129
8130static int
8131process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8132{
8133 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8134
8135 if (dwo_file->tus != NULL)
8136 {
8137 htab_traverse_noresize (dwo_file->tus,
8138 process_skeletonless_type_unit, info);
8139 }
8140
8141 return 1;
8142}
8143
8144/* Scan all TUs of DWO files, verifying we've processed them.
8145 This is needed in case a TU was emitted without its skeleton.
8146 Note: This can't be done until we know what all the DWO files are. */
8147
8148static void
ed2dc618 8149process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8150{
8151 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8152 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8153 && dwarf2_per_objfile->dwo_files != NULL)
8154 {
51ac9db5 8155 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8156 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8157 dwarf2_per_objfile);
6aa5f3a6 8158 }
348e048f
DE
8159}
8160
ed2dc618 8161/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8162
8163static void
ed2dc618 8164set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8165{
b76e467d 8166 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8167 {
891813be 8168 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8169
36586728
TT
8170 if (pst == NULL)
8171 continue;
8172
b76e467d 8173 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8174 {
8175 /* Set the 'user' field only if it is not already set. */
8176 if (pst->dependencies[j]->user == NULL)
8177 pst->dependencies[j]->user = pst;
8178 }
8179 }
8180}
8181
93311388
DE
8182/* Build the partial symbol table by doing a quick pass through the
8183 .debug_info and .debug_abbrev sections. */
72bf9492 8184
93311388 8185static void
ed2dc618 8186dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8187{
ed2dc618 8188 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8189
b4f54984 8190 if (dwarf_read_debug)
45cfd468
DE
8191 {
8192 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8193 objfile_name (objfile));
45cfd468
DE
8194 }
8195
98bfdba5
PA
8196 dwarf2_per_objfile->reading_partial_symbols = 1;
8197
96b79293 8198 dwarf2_per_objfile->info.read (objfile);
91c24f0a 8199
93311388
DE
8200 /* Any cached compilation units will be linked by the per-objfile
8201 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8202 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8203
ed2dc618 8204 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8205
ed2dc618 8206 create_all_comp_units (dwarf2_per_objfile);
c906108c 8207
60606b2c
TT
8208 /* Create a temporary address map on a temporary obstack. We later
8209 copy this to the final obstack. */
8268c778 8210 auto_obstack temp_obstack;
791afaa2
TT
8211
8212 scoped_restore save_psymtabs_addrmap
d320c2b5 8213 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8214 addrmap_create_mutable (&temp_obstack));
72bf9492 8215
b76e467d
SM
8216 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8217 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8218
6aa5f3a6 8219 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8220 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8221
8222 /* Now that all TUs have been processed we can fill in the dependencies. */
8223 if (dwarf2_per_objfile->type_unit_groups != NULL)
8224 {
8225 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8226 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8227 }
8228
b4f54984 8229 if (dwarf_read_debug)
ed2dc618 8230 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8231
ed2dc618 8232 set_partial_user (dwarf2_per_objfile);
95554aad 8233
d320c2b5
TT
8234 objfile->partial_symtabs->psymtabs_addrmap
8235 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8236 objfile->partial_symtabs->obstack ());
791afaa2
TT
8237 /* At this point we want to keep the address map. */
8238 save_psymtabs_addrmap.release ();
ff013f42 8239
b4f54984 8240 if (dwarf_read_debug)
45cfd468 8241 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8242 objfile_name (objfile));
ae038cb0
DJ
8243}
8244
dee91e82
DE
8245/* Load the partial DIEs for a secondary CU into memory.
8246 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8247
dee91e82
DE
8248static void
8249load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8250{
c0ab21c2
TT
8251 cutu_reader reader (this_cu, NULL, 1, 1, false);
8252
8253 if (!reader.dummy_p)
8254 {
8255 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8256 language_minimal);
8257
8258 /* Check if comp unit has_children.
8259 If so, read the rest of the partial symbols from this comp unit.
8260 If not, there's no more debug_info for this comp unit. */
8261 if (reader.has_children)
8262 load_partial_dies (&reader, reader.info_ptr, 0);
8263 }
ae038cb0
DJ
8264}
8265
ae038cb0 8266static void
ed2dc618 8267read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8268 struct dwarf2_section_info *section,
f1902523 8269 struct dwarf2_section_info *abbrev_section,
b76e467d 8270 unsigned int is_dwz)
ae038cb0 8271{
d521ce57 8272 const gdb_byte *info_ptr;
ed2dc618 8273 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8274
b4f54984 8275 if (dwarf_read_debug)
bf6af496 8276 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8277 section->get_name (),
8278 section->get_file_name ());
bf6af496 8279
96b79293 8280 section->read (objfile);
ae038cb0 8281
36586728 8282 info_ptr = section->buffer;
6e70227d 8283
36586728 8284 while (info_ptr < section->buffer + section->size)
ae038cb0 8285 {
ae038cb0 8286 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8287
9c541725 8288 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8289
f1902523 8290 comp_unit_head cu_header;
ed2dc618
SM
8291 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8292 abbrev_section, info_ptr,
8293 rcuh_kind::COMPILE);
ae038cb0
DJ
8294
8295 /* Save the compilation unit for later lookup. */
f1902523
JK
8296 if (cu_header.unit_type != DW_UT_type)
8297 {
8298 this_cu = XOBNEW (&objfile->objfile_obstack,
8299 struct dwarf2_per_cu_data);
8300 memset (this_cu, 0, sizeof (*this_cu));
8301 }
8302 else
8303 {
8304 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8305 struct signatured_type);
8306 memset (sig_type, 0, sizeof (*sig_type));
8307 sig_type->signature = cu_header.signature;
8308 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8309 this_cu = &sig_type->per_cu;
8310 }
8311 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8312 this_cu->sect_off = sect_off;
f1902523 8313 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8314 this_cu->is_dwz = is_dwz;
e3b94546 8315 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8316 this_cu->section = section;
ae038cb0 8317
b76e467d 8318 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8319
8320 info_ptr = info_ptr + this_cu->length;
8321 }
36586728
TT
8322}
8323
8324/* Create a list of all compilation units in OBJFILE.
8325 This is only done for -readnow and building partial symtabs. */
8326
8327static void
ed2dc618 8328create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8329{
b76e467d 8330 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8331 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8332 &dwarf2_per_objfile->abbrev, 0);
36586728 8333
b76e467d 8334 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8335 if (dwz != NULL)
ed2dc618 8336 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8337 1);
c906108c
SS
8338}
8339
5734ee8b 8340/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8341 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8342 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8343 DW_AT_ranges). See the comments of add_partial_subprogram on how
8344 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8345
72bf9492
DJ
8346static void
8347scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8348 CORE_ADDR *highpc, int set_addrmap,
8349 struct dwarf2_cu *cu)
c906108c 8350{
72bf9492 8351 struct partial_die_info *pdi;
c906108c 8352
91c24f0a
DC
8353 /* Now, march along the PDI's, descending into ones which have
8354 interesting children but skipping the children of the other ones,
8355 until we reach the end of the compilation unit. */
c906108c 8356
72bf9492 8357 pdi = first_die;
91c24f0a 8358
72bf9492
DJ
8359 while (pdi != NULL)
8360 {
52356b79 8361 pdi->fixup (cu);
c906108c 8362
f55ee35c 8363 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8364 children, so we need to look at them. Ditto for anonymous
8365 enums. */
933c6fe4 8366
72bf9492 8367 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8368 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8369 || pdi->tag == DW_TAG_imported_unit
8370 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8371 {
72bf9492 8372 switch (pdi->tag)
c906108c
SS
8373 {
8374 case DW_TAG_subprogram:
b1dc1806 8375 case DW_TAG_inlined_subroutine:
cdc07690 8376 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8377 break;
72929c62 8378 case DW_TAG_constant:
c906108c
SS
8379 case DW_TAG_variable:
8380 case DW_TAG_typedef:
91c24f0a 8381 case DW_TAG_union_type:
72bf9492 8382 if (!pdi->is_declaration)
63d06c5c 8383 {
72bf9492 8384 add_partial_symbol (pdi, cu);
63d06c5c
DC
8385 }
8386 break;
c906108c 8387 case DW_TAG_class_type:
680b30c7 8388 case DW_TAG_interface_type:
c906108c 8389 case DW_TAG_structure_type:
72bf9492 8390 if (!pdi->is_declaration)
c906108c 8391 {
72bf9492 8392 add_partial_symbol (pdi, cu);
c906108c 8393 }
b7fee5a3
KS
8394 if ((cu->language == language_rust
8395 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8396 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8397 set_addrmap, cu);
c906108c 8398 break;
91c24f0a 8399 case DW_TAG_enumeration_type:
72bf9492
DJ
8400 if (!pdi->is_declaration)
8401 add_partial_enumeration (pdi, cu);
c906108c
SS
8402 break;
8403 case DW_TAG_base_type:
a02abb62 8404 case DW_TAG_subrange_type:
c906108c 8405 /* File scope base type definitions are added to the partial
c5aa993b 8406 symbol table. */
72bf9492 8407 add_partial_symbol (pdi, cu);
c906108c 8408 break;
d9fa45fe 8409 case DW_TAG_namespace:
cdc07690 8410 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8411 break;
5d7cb8df 8412 case DW_TAG_module:
59c35742
AB
8413 if (!pdi->is_declaration)
8414 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8415 break;
95554aad
TT
8416 case DW_TAG_imported_unit:
8417 {
8418 struct dwarf2_per_cu_data *per_cu;
8419
f4dc4d17
DE
8420 /* For now we don't handle imported units in type units. */
8421 if (cu->per_cu->is_debug_types)
8422 {
8423 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8424 " supported in type units [in module %s]"),
518817b3 8425 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8426 }
8427
e3b94546
SM
8428 per_cu = dwarf2_find_containing_comp_unit
8429 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8430 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8431
8432 /* Go read the partial unit, if needed. */
8433 if (per_cu->v.psymtab == NULL)
b93601f3 8434 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8435
ae640021 8436 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8437 }
8438 break;
74921315
KS
8439 case DW_TAG_imported_declaration:
8440 add_partial_symbol (pdi, cu);
8441 break;
c906108c
SS
8442 default:
8443 break;
8444 }
8445 }
8446
72bf9492
DJ
8447 /* If the die has a sibling, skip to the sibling. */
8448
8449 pdi = pdi->die_sibling;
8450 }
8451}
8452
8453/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8454
72bf9492 8455 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8456 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8457 Enumerators are an exception; they use the scope of their parent
8458 enumeration type, i.e. the name of the enumeration type is not
8459 prepended to the enumerator.
91c24f0a 8460
72bf9492
DJ
8461 There are two complexities. One is DW_AT_specification; in this
8462 case "parent" means the parent of the target of the specification,
8463 instead of the direct parent of the DIE. The other is compilers
8464 which do not emit DW_TAG_namespace; in this case we try to guess
8465 the fully qualified name of structure types from their members'
8466 linkage names. This must be done using the DIE's children rather
8467 than the children of any DW_AT_specification target. We only need
8468 to do this for structures at the top level, i.e. if the target of
8469 any DW_AT_specification (if any; otherwise the DIE itself) does not
8470 have a parent. */
8471
8472/* Compute the scope prefix associated with PDI's parent, in
8473 compilation unit CU. The result will be allocated on CU's
8474 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8475 field. NULL is returned if no prefix is necessary. */
15d034d0 8476static const char *
72bf9492
DJ
8477partial_die_parent_scope (struct partial_die_info *pdi,
8478 struct dwarf2_cu *cu)
8479{
15d034d0 8480 const char *grandparent_scope;
72bf9492 8481 struct partial_die_info *parent, *real_pdi;
91c24f0a 8482
72bf9492
DJ
8483 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8484 then this means the parent of the specification DIE. */
8485
8486 real_pdi = pdi;
72bf9492 8487 while (real_pdi->has_specification)
fb816e8b 8488 {
122cf0f2
AB
8489 auto res = find_partial_die (real_pdi->spec_offset,
8490 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8491 real_pdi = res.pdi;
8492 cu = res.cu;
8493 }
72bf9492
DJ
8494
8495 parent = real_pdi->die_parent;
8496 if (parent == NULL)
8497 return NULL;
8498
8499 if (parent->scope_set)
8500 return parent->scope;
8501
52356b79 8502 parent->fixup (cu);
72bf9492 8503
10b3939b 8504 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8505
acebe513
UW
8506 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8507 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8508 Work around this problem here. */
8509 if (cu->language == language_cplus
6e70227d 8510 && parent->tag == DW_TAG_namespace
acebe513
UW
8511 && strcmp (parent->name, "::") == 0
8512 && grandparent_scope == NULL)
8513 {
8514 parent->scope = NULL;
8515 parent->scope_set = 1;
8516 return NULL;
8517 }
8518
0a4b0913 8519 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8520 if (pdi->tag == DW_TAG_enumerator)
8521 /* Enumerators should not get the name of the enumeration as a prefix. */
8522 parent->scope = grandparent_scope;
8523 else if (parent->tag == DW_TAG_namespace
f55ee35c 8524 || parent->tag == DW_TAG_module
72bf9492
DJ
8525 || parent->tag == DW_TAG_structure_type
8526 || parent->tag == DW_TAG_class_type
680b30c7 8527 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8528 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8529 || parent->tag == DW_TAG_enumeration_type
8530 || (cu->language == language_fortran
8531 && parent->tag == DW_TAG_subprogram
8532 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8533 {
8534 if (grandparent_scope == NULL)
8535 parent->scope = parent->name;
8536 else
3e43a32a
MS
8537 parent->scope = typename_concat (&cu->comp_unit_obstack,
8538 grandparent_scope,
f55ee35c 8539 parent->name, 0, cu);
72bf9492 8540 }
72bf9492
DJ
8541 else
8542 {
8543 /* FIXME drow/2004-04-01: What should we be doing with
8544 function-local names? For partial symbols, we should probably be
8545 ignoring them. */
fa9c3fa0
TT
8546 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8547 dwarf_tag_name (parent->tag),
8548 sect_offset_str (pdi->sect_off));
72bf9492 8549 parent->scope = grandparent_scope;
c906108c
SS
8550 }
8551
72bf9492
DJ
8552 parent->scope_set = 1;
8553 return parent->scope;
8554}
8555
8556/* Return the fully scoped name associated with PDI, from compilation unit
8557 CU. The result will be allocated with malloc. */
4568ecf9 8558
43816ebc 8559static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8560partial_die_full_name (struct partial_die_info *pdi,
8561 struct dwarf2_cu *cu)
8562{
15d034d0 8563 const char *parent_scope;
72bf9492 8564
98bfdba5
PA
8565 /* If this is a template instantiation, we can not work out the
8566 template arguments from partial DIEs. So, unfortunately, we have
8567 to go through the full DIEs. At least any work we do building
8568 types here will be reused if full symbols are loaded later. */
8569 if (pdi->has_template_arguments)
8570 {
52356b79 8571 pdi->fixup (cu);
98bfdba5
PA
8572
8573 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8574 {
8575 struct die_info *die;
8576 struct attribute attr;
8577 struct dwarf2_cu *ref_cu = cu;
8578
b64f50a1 8579 /* DW_FORM_ref_addr is using section offset. */
b4069958 8580 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8581 attr.form = DW_FORM_ref_addr;
9c541725 8582 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8583 die = follow_die_ref (NULL, &attr, &ref_cu);
8584
43816ebc 8585 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8586 }
8587 }
8588
72bf9492
DJ
8589 parent_scope = partial_die_parent_scope (pdi, cu);
8590 if (parent_scope == NULL)
8591 return NULL;
8592 else
43816ebc
TT
8593 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8594 pdi->name, 0, cu));
c906108c
SS
8595}
8596
8597static void
72bf9492 8598add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8599{
518817b3
SM
8600 struct dwarf2_per_objfile *dwarf2_per_objfile
8601 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8602 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8603 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8604 CORE_ADDR addr = 0;
15d034d0 8605 const char *actual_name = NULL;
e142c38c
DJ
8606 CORE_ADDR baseaddr;
8607
b3b3bada 8608 baseaddr = objfile->text_section_offset ();
c906108c 8609
43816ebc
TT
8610 gdb::unique_xmalloc_ptr<char> built_actual_name
8611 = partial_die_full_name (pdi, cu);
15d034d0 8612 if (built_actual_name != NULL)
43816ebc 8613 actual_name = built_actual_name.get ();
63d06c5c 8614
72bf9492
DJ
8615 if (actual_name == NULL)
8616 actual_name = pdi->name;
8617
c906108c
SS
8618 switch (pdi->tag)
8619 {
b1dc1806 8620 case DW_TAG_inlined_subroutine:
c906108c 8621 case DW_TAG_subprogram:
79748972
TT
8622 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8623 - baseaddr);
0a4b0913
AB
8624 if (pdi->is_external
8625 || cu->language == language_ada
8626 || (cu->language == language_fortran
8627 && pdi->die_parent != NULL
8628 && pdi->die_parent->tag == DW_TAG_subprogram))
8629 {
8630 /* Normally, only "external" DIEs are part of the global scope.
8631 But in Ada and Fortran, we want to be able to access nested
8632 procedures globally. So all Ada and Fortran subprograms are
8633 stored in the global scope. */
31edb802 8634 add_psymbol_to_list (actual_name,
15d034d0 8635 built_actual_name != NULL,
f47fb265 8636 VAR_DOMAIN, LOC_BLOCK,
79748972 8637 SECT_OFF_TEXT (objfile),
75aedd27 8638 psymbol_placement::GLOBAL,
79748972
TT
8639 addr,
8640 cu->language, objfile);
c906108c
SS
8641 }
8642 else
8643 {
31edb802 8644 add_psymbol_to_list (actual_name,
15d034d0 8645 built_actual_name != NULL,
f47fb265 8646 VAR_DOMAIN, LOC_BLOCK,
79748972 8647 SECT_OFF_TEXT (objfile),
75aedd27 8648 psymbol_placement::STATIC,
1762568f 8649 addr, cu->language, objfile);
c906108c 8650 }
0c1b455e
TT
8651
8652 if (pdi->main_subprogram && actual_name != NULL)
8653 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8654 break;
72929c62 8655 case DW_TAG_constant:
31edb802 8656 add_psymbol_to_list (actual_name,
75aedd27
TT
8657 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8658 -1, (pdi->is_external
8659 ? psymbol_placement::GLOBAL
8660 : psymbol_placement::STATIC),
8661 0, cu->language, objfile);
72929c62 8662 break;
c906108c 8663 case DW_TAG_variable:
95554aad
TT
8664 if (pdi->d.locdesc)
8665 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8666
95554aad 8667 if (pdi->d.locdesc
caac4577
JG
8668 && addr == 0
8669 && !dwarf2_per_objfile->has_section_at_zero)
8670 {
8671 /* A global or static variable may also have been stripped
8672 out by the linker if unused, in which case its address
8673 will be nullified; do not add such variables into partial
8674 symbol table then. */
8675 }
8676 else if (pdi->is_external)
c906108c
SS
8677 {
8678 /* Global Variable.
8679 Don't enter into the minimal symbol tables as there is
8680 a minimal symbol table entry from the ELF symbols already.
8681 Enter into partial symbol table if it has a location
8682 descriptor or a type.
8683 If the location descriptor is missing, new_symbol will create
8684 a LOC_UNRESOLVED symbol, the address of the variable will then
8685 be determined from the minimal symbol table whenever the variable
8686 is referenced.
8687 The address for the partial symbol table entry is not
8688 used by GDB, but it comes in handy for debugging partial symbol
8689 table building. */
8690
95554aad 8691 if (pdi->d.locdesc || pdi->has_type)
31edb802 8692 add_psymbol_to_list (actual_name,
15d034d0 8693 built_actual_name != NULL,
f47fb265 8694 VAR_DOMAIN, LOC_STATIC,
79748972 8695 SECT_OFF_TEXT (objfile),
75aedd27 8696 psymbol_placement::GLOBAL,
79748972 8697 addr, cu->language, objfile);
c906108c
SS
8698 }
8699 else
8700 {
ff908ebf
AW
8701 int has_loc = pdi->d.locdesc != NULL;
8702
8703 /* Static Variable. Skip symbols whose value we cannot know (those
8704 without location descriptors or constant values). */
8705 if (!has_loc && !pdi->has_const_value)
43816ebc 8706 return;
ff908ebf 8707
31edb802 8708 add_psymbol_to_list (actual_name,
15d034d0 8709 built_actual_name != NULL,
f47fb265 8710 VAR_DOMAIN, LOC_STATIC,
79748972 8711 SECT_OFF_TEXT (objfile),
75aedd27 8712 psymbol_placement::STATIC,
79748972 8713 has_loc ? addr : 0,
f47fb265 8714 cu->language, objfile);
c906108c
SS
8715 }
8716 break;
8717 case DW_TAG_typedef:
8718 case DW_TAG_base_type:
a02abb62 8719 case DW_TAG_subrange_type:
31edb802 8720 add_psymbol_to_list (actual_name,
15d034d0 8721 built_actual_name != NULL,
79748972 8722 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8723 psymbol_placement::STATIC,
1762568f 8724 0, cu->language, objfile);
c906108c 8725 break;
74921315 8726 case DW_TAG_imported_declaration:
72bf9492 8727 case DW_TAG_namespace:
31edb802 8728 add_psymbol_to_list (actual_name,
15d034d0 8729 built_actual_name != NULL,
79748972 8730 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8731 psymbol_placement::GLOBAL,
1762568f 8732 0, cu->language, objfile);
72bf9492 8733 break;
530e8392 8734 case DW_TAG_module:
a5fd13a9
BH
8735 /* With Fortran 77 there might be a "BLOCK DATA" module
8736 available without any name. If so, we skip the module as it
8737 doesn't bring any value. */
8738 if (actual_name != nullptr)
31edb802 8739 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8740 built_actual_name != NULL,
8741 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8742 psymbol_placement::GLOBAL,
8743 0, cu->language, objfile);
530e8392 8744 break;
c906108c 8745 case DW_TAG_class_type:
680b30c7 8746 case DW_TAG_interface_type:
c906108c
SS
8747 case DW_TAG_structure_type:
8748 case DW_TAG_union_type:
8749 case DW_TAG_enumeration_type:
fa4028e9
JB
8750 /* Skip external references. The DWARF standard says in the section
8751 about "Structure, Union, and Class Type Entries": "An incomplete
8752 structure, union or class type is represented by a structure,
8753 union or class entry that does not have a byte size attribute
8754 and that has a DW_AT_declaration attribute." */
8755 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8756 return;
fa4028e9 8757
63d06c5c
DC
8758 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8759 static vs. global. */
31edb802 8760 add_psymbol_to_list (actual_name,
15d034d0 8761 built_actual_name != NULL,
79748972 8762 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8763 cu->language == language_cplus
75aedd27
TT
8764 ? psymbol_placement::GLOBAL
8765 : psymbol_placement::STATIC,
1762568f 8766 0, cu->language, objfile);
c906108c 8767
c906108c
SS
8768 break;
8769 case DW_TAG_enumerator:
31edb802 8770 add_psymbol_to_list (actual_name,
15d034d0 8771 built_actual_name != NULL,
79748972 8772 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8773 cu->language == language_cplus
75aedd27
TT
8774 ? psymbol_placement::GLOBAL
8775 : psymbol_placement::STATIC,
1762568f 8776 0, cu->language, objfile);
c906108c
SS
8777 break;
8778 default:
8779 break;
8780 }
8781}
8782
5c4e30ca
DC
8783/* Read a partial die corresponding to a namespace; also, add a symbol
8784 corresponding to that namespace to the symbol table. NAMESPACE is
8785 the name of the enclosing namespace. */
91c24f0a 8786
72bf9492
DJ
8787static void
8788add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8789 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8790 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8791{
72bf9492 8792 /* Add a symbol for the namespace. */
e7c27a73 8793
72bf9492 8794 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8795
8796 /* Now scan partial symbols in that namespace. */
8797
91c24f0a 8798 if (pdi->has_children)
cdc07690 8799 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8800}
8801
5d7cb8df
JK
8802/* Read a partial die corresponding to a Fortran module. */
8803
8804static void
8805add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8806 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8807{
530e8392
KB
8808 /* Add a symbol for the namespace. */
8809
8810 add_partial_symbol (pdi, cu);
8811
f55ee35c 8812 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8813
8814 if (pdi->has_children)
cdc07690 8815 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8816}
8817
b1dc1806
XR
8818/* Read a partial die corresponding to a subprogram or an inlined
8819 subprogram and create a partial symbol for that subprogram.
8820 When the CU language allows it, this routine also defines a partial
8821 symbol for each nested subprogram that this subprogram contains.
8822 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8823 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8824
cdc07690
YQ
8825 PDI may also be a lexical block, in which case we simply search
8826 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8827 Again, this is only performed when the CU language allows this
8828 type of definitions. */
8829
8830static void
8831add_partial_subprogram (struct partial_die_info *pdi,
8832 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8833 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8834{
b1dc1806 8835 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8836 {
8837 if (pdi->has_pc_info)
8838 {
8839 if (pdi->lowpc < *lowpc)
8840 *lowpc = pdi->lowpc;
8841 if (pdi->highpc > *highpc)
8842 *highpc = pdi->highpc;
cdc07690 8843 if (set_addrmap)
5734ee8b 8844 {
518817b3 8845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8847 CORE_ADDR baseaddr;
b926417a
TT
8848 CORE_ADDR this_highpc;
8849 CORE_ADDR this_lowpc;
5734ee8b 8850
b3b3bada 8851 baseaddr = objfile->text_section_offset ();
b926417a
TT
8852 this_lowpc
8853 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8854 pdi->lowpc + baseaddr)
8855 - baseaddr);
8856 this_highpc
8857 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8858 pdi->highpc + baseaddr)
8859 - baseaddr);
d320c2b5 8860 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8861 this_lowpc, this_highpc - 1,
9291a0cd 8862 cu->per_cu->v.psymtab);
5734ee8b 8863 }
481860b3
GB
8864 }
8865
8866 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8867 {
bc30ff58 8868 if (!pdi->is_declaration)
e8d05480
JB
8869 /* Ignore subprogram DIEs that do not have a name, they are
8870 illegal. Do not emit a complaint at this point, we will
8871 do so when we convert this psymtab into a symtab. */
8872 if (pdi->name)
8873 add_partial_symbol (pdi, cu);
bc30ff58
JB
8874 }
8875 }
6e70227d 8876
bc30ff58
JB
8877 if (! pdi->has_children)
8878 return;
8879
0a4b0913 8880 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8881 {
8882 pdi = pdi->die_child;
8883 while (pdi != NULL)
8884 {
52356b79 8885 pdi->fixup (cu);
bc30ff58 8886 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8887 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8888 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8889 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8890 pdi = pdi->die_sibling;
8891 }
8892 }
8893}
8894
91c24f0a
DC
8895/* Read a partial die corresponding to an enumeration type. */
8896
72bf9492
DJ
8897static void
8898add_partial_enumeration (struct partial_die_info *enum_pdi,
8899 struct dwarf2_cu *cu)
91c24f0a 8900{
72bf9492 8901 struct partial_die_info *pdi;
91c24f0a
DC
8902
8903 if (enum_pdi->name != NULL)
72bf9492
DJ
8904 add_partial_symbol (enum_pdi, cu);
8905
8906 pdi = enum_pdi->die_child;
8907 while (pdi)
91c24f0a 8908 {
72bf9492 8909 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8910 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8911 else
72bf9492
DJ
8912 add_partial_symbol (pdi, cu);
8913 pdi = pdi->die_sibling;
91c24f0a 8914 }
91c24f0a
DC
8915}
8916
6caca83c
CC
8917/* Return the initial uleb128 in the die at INFO_PTR. */
8918
8919static unsigned int
d521ce57 8920peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8921{
8922 unsigned int bytes_read;
8923
8924 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8925}
8926
685af9cd
TT
8927/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8928 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8929
4bb7a0a7
DJ
8930 Return the corresponding abbrev, or NULL if the number is zero (indicating
8931 an empty DIE). In either case *BYTES_READ will be set to the length of
8932 the initial number. */
8933
8934static struct abbrev_info *
685af9cd
TT
8935peek_die_abbrev (const die_reader_specs &reader,
8936 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8937{
685af9cd 8938 dwarf2_cu *cu = reader.cu;
518817b3 8939 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8940 unsigned int abbrev_number
8941 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8942
8943 if (abbrev_number == 0)
8944 return NULL;
8945
685af9cd 8946 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8947 if (!abbrev)
8948 {
422b9917 8949 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8950 " at offset %s [in module %s]"),
422b9917 8951 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8952 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8953 }
8954
8955 return abbrev;
8956}
8957
93311388
DE
8958/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8959 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8960 DIE. Any children of the skipped DIEs will also be skipped. */
8961
d521ce57
TT
8962static const gdb_byte *
8963skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8964{
4bb7a0a7
DJ
8965 while (1)
8966 {
685af9cd
TT
8967 unsigned int bytes_read;
8968 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8969
4bb7a0a7
DJ
8970 if (abbrev == NULL)
8971 return info_ptr + bytes_read;
8972 else
dee91e82 8973 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8974 }
8975}
8976
93311388
DE
8977/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8978 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8979 abbrev corresponding to that skipped uleb128 should be passed in
8980 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8981 children. */
8982
d521ce57
TT
8983static const gdb_byte *
8984skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8985 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8986{
8987 unsigned int bytes_read;
8988 struct attribute attr;
dee91e82
DE
8989 bfd *abfd = reader->abfd;
8990 struct dwarf2_cu *cu = reader->cu;
d521ce57 8991 const gdb_byte *buffer = reader->buffer;
f664829e 8992 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8993 unsigned int form, i;
8994
8995 for (i = 0; i < abbrev->num_attrs; i++)
8996 {
8997 /* The only abbrev we care about is DW_AT_sibling. */
8998 if (abbrev->attrs[i].name == DW_AT_sibling)
8999 {
18a8505e
AT
9000 bool ignored;
9001 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9002 &ignored);
4bb7a0a7 9003 if (attr.form == DW_FORM_ref_addr)
b98664d3 9004 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9005 else
b9502d3f 9006 {
9c541725
PA
9007 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9008 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9009
9010 if (sibling_ptr < info_ptr)
b98664d3 9011 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9012 else if (sibling_ptr > reader->buffer_end)
9013 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9014 else
9015 return sibling_ptr;
9016 }
4bb7a0a7
DJ
9017 }
9018
9019 /* If it isn't DW_AT_sibling, skip this attribute. */
9020 form = abbrev->attrs[i].form;
9021 skip_attribute:
9022 switch (form)
9023 {
4bb7a0a7 9024 case DW_FORM_ref_addr:
ae411497
TT
9025 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9026 and later it is offset sized. */
9027 if (cu->header.version == 2)
9028 info_ptr += cu->header.addr_size;
9029 else
9030 info_ptr += cu->header.offset_size;
9031 break;
36586728
TT
9032 case DW_FORM_GNU_ref_alt:
9033 info_ptr += cu->header.offset_size;
9034 break;
ae411497 9035 case DW_FORM_addr:
4bb7a0a7
DJ
9036 info_ptr += cu->header.addr_size;
9037 break;
9038 case DW_FORM_data1:
9039 case DW_FORM_ref1:
9040 case DW_FORM_flag:
8fe0f950 9041 case DW_FORM_strx1:
4bb7a0a7
DJ
9042 info_ptr += 1;
9043 break;
2dc7f7b3 9044 case DW_FORM_flag_present:
43988095 9045 case DW_FORM_implicit_const:
2dc7f7b3 9046 break;
4bb7a0a7
DJ
9047 case DW_FORM_data2:
9048 case DW_FORM_ref2:
8fe0f950 9049 case DW_FORM_strx2:
4bb7a0a7
DJ
9050 info_ptr += 2;
9051 break;
8fe0f950
AT
9052 case DW_FORM_strx3:
9053 info_ptr += 3;
9054 break;
4bb7a0a7
DJ
9055 case DW_FORM_data4:
9056 case DW_FORM_ref4:
8fe0f950 9057 case DW_FORM_strx4:
4bb7a0a7
DJ
9058 info_ptr += 4;
9059 break;
9060 case DW_FORM_data8:
9061 case DW_FORM_ref8:
55f1336d 9062 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9063 info_ptr += 8;
9064 break;
0224619f
JK
9065 case DW_FORM_data16:
9066 info_ptr += 16;
9067 break;
4bb7a0a7 9068 case DW_FORM_string:
9b1c24c8 9069 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9070 info_ptr += bytes_read;
9071 break;
2dc7f7b3 9072 case DW_FORM_sec_offset:
4bb7a0a7 9073 case DW_FORM_strp:
36586728 9074 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9075 info_ptr += cu->header.offset_size;
9076 break;
2dc7f7b3 9077 case DW_FORM_exprloc:
4bb7a0a7
DJ
9078 case DW_FORM_block:
9079 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9080 info_ptr += bytes_read;
9081 break;
9082 case DW_FORM_block1:
9083 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9084 break;
9085 case DW_FORM_block2:
9086 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9087 break;
9088 case DW_FORM_block4:
9089 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9090 break;
336d760d 9091 case DW_FORM_addrx:
cf532bd1 9092 case DW_FORM_strx:
4bb7a0a7
DJ
9093 case DW_FORM_sdata:
9094 case DW_FORM_udata:
9095 case DW_FORM_ref_udata:
3019eac3
DE
9096 case DW_FORM_GNU_addr_index:
9097 case DW_FORM_GNU_str_index:
18a8505e 9098 case DW_FORM_rnglistx:
d521ce57 9099 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9100 break;
9101 case DW_FORM_indirect:
9102 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9103 info_ptr += bytes_read;
9104 /* We need to continue parsing from here, so just go back to
9105 the top. */
9106 goto skip_attribute;
9107
9108 default:
3e43a32a
MS
9109 error (_("Dwarf Error: Cannot handle %s "
9110 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9111 dwarf_form_name (form),
9112 bfd_get_filename (abfd));
9113 }
9114 }
9115
9116 if (abbrev->has_children)
dee91e82 9117 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9118 else
9119 return info_ptr;
9120}
9121
93311388 9122/* Locate ORIG_PDI's sibling.
dee91e82 9123 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9124
d521ce57 9125static const gdb_byte *
dee91e82
DE
9126locate_pdi_sibling (const struct die_reader_specs *reader,
9127 struct partial_die_info *orig_pdi,
d521ce57 9128 const gdb_byte *info_ptr)
91c24f0a
DC
9129{
9130 /* Do we know the sibling already? */
72bf9492 9131
91c24f0a
DC
9132 if (orig_pdi->sibling)
9133 return orig_pdi->sibling;
9134
9135 /* Are there any children to deal with? */
9136
9137 if (!orig_pdi->has_children)
9138 return info_ptr;
9139
4bb7a0a7 9140 /* Skip the children the long way. */
91c24f0a 9141
dee91e82 9142 return skip_children (reader, info_ptr);
91c24f0a
DC
9143}
9144
257e7a09 9145/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9146 not NULL. */
c906108c 9147
891813be
TT
9148void
9149dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9150{
ed2dc618
SM
9151 struct dwarf2_per_objfile *dwarf2_per_objfile
9152 = get_dwarf2_per_objfile (objfile);
9153
077cbab2
TT
9154 gdb_assert (!readin);
9155 /* If this psymtab is constructed from a debug-only objfile, the
9156 has_section_at_zero flag will not necessarily be correct. We
9157 can get the correct value for this flag by looking at the data
9158 associated with the (presumably stripped) associated objfile. */
9159 if (objfile->separate_debug_objfile_backlink)
c906108c 9160 {
077cbab2
TT
9161 struct dwarf2_per_objfile *dpo_backlink
9162 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9163
077cbab2
TT
9164 dwarf2_per_objfile->has_section_at_zero
9165 = dpo_backlink->has_section_at_zero;
9166 }
98bfdba5 9167
077cbab2 9168 dwarf2_per_objfile->reading_partial_symbols = 0;
c906108c 9169
8566b89b 9170 expand_psymtab (objfile);
95554aad 9171
ed2dc618 9172 process_cu_includes (dwarf2_per_objfile);
c906108c 9173}
9cdd5dbd
DE
9174\f
9175/* Reading in full CUs. */
c906108c 9176
10b3939b
DJ
9177/* Add PER_CU to the queue. */
9178
9179static void
95554aad
TT
9180queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9181 enum language pretend_language)
10b3939b
DJ
9182{
9183 struct dwarf2_queue_item *item;
9184
9185 per_cu->queued = 1;
8d749320 9186 item = XNEW (struct dwarf2_queue_item);
10b3939b 9187 item->per_cu = per_cu;
95554aad 9188 item->pretend_language = pretend_language;
10b3939b
DJ
9189 item->next = NULL;
9190
9191 if (dwarf2_queue == NULL)
9192 dwarf2_queue = item;
9193 else
9194 dwarf2_queue_tail->next = item;
9195
9196 dwarf2_queue_tail = item;
9197}
9198
89e63ee4
DE
9199/* If PER_CU is not yet queued, add it to the queue.
9200 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9201 dependency.
0907af0c 9202 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9203 meaning either PER_CU is already queued or it is already loaded.
9204
9205 N.B. There is an invariant here that if a CU is queued then it is loaded.
9206 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9207
9208static int
89e63ee4 9209maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9210 struct dwarf2_per_cu_data *per_cu,
9211 enum language pretend_language)
9212{
9213 /* We may arrive here during partial symbol reading, if we need full
9214 DIEs to process an unusual case (e.g. template arguments). Do
9215 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9216 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9217 {
9218 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9219 return 1;
9220 return 0;
9221 }
9222
9223 /* Mark the dependence relation so that we don't flush PER_CU
9224 too early. */
89e63ee4
DE
9225 if (dependent_cu != NULL)
9226 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9227
9228 /* If it's already on the queue, we have nothing to do. */
9229 if (per_cu->queued)
9230 return 0;
9231
9232 /* If the compilation unit is already loaded, just mark it as
9233 used. */
9234 if (per_cu->cu != NULL)
9235 {
9236 per_cu->cu->last_used = 0;
9237 return 0;
9238 }
9239
9240 /* Add it to the queue. */
9241 queue_comp_unit (per_cu, pretend_language);
9242
9243 return 1;
9244}
9245
10b3939b
DJ
9246/* Process the queue. */
9247
9248static void
ed2dc618 9249process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9250{
9251 struct dwarf2_queue_item *item, *next_item;
9252
b4f54984 9253 if (dwarf_read_debug)
45cfd468
DE
9254 {
9255 fprintf_unfiltered (gdb_stdlog,
9256 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9257 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9258 }
9259
03dd20cc
DJ
9260 /* The queue starts out with one item, but following a DIE reference
9261 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9262 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9263 {
cc12ce38
DE
9264 if ((dwarf2_per_objfile->using_index
9265 ? !item->per_cu->v.quick->compunit_symtab
9266 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9267 /* Skip dummy CUs. */
9268 && item->per_cu->cu != NULL)
f4dc4d17
DE
9269 {
9270 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9271 unsigned int debug_print_threshold;
247f5c4f 9272 char buf[100];
f4dc4d17 9273
247f5c4f 9274 if (per_cu->is_debug_types)
f4dc4d17 9275 {
247f5c4f
DE
9276 struct signatured_type *sig_type =
9277 (struct signatured_type *) per_cu;
9278
9d8780f0 9279 sprintf (buf, "TU %s at offset %s",
73be47f5 9280 hex_string (sig_type->signature),
9d8780f0 9281 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9282 /* There can be 100s of TUs.
9283 Only print them in verbose mode. */
9284 debug_print_threshold = 2;
f4dc4d17 9285 }
247f5c4f 9286 else
73be47f5 9287 {
9d8780f0
SM
9288 sprintf (buf, "CU at offset %s",
9289 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9290 debug_print_threshold = 1;
9291 }
247f5c4f 9292
b4f54984 9293 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9294 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9295
9296 if (per_cu->is_debug_types)
9297 process_full_type_unit (per_cu, item->pretend_language);
9298 else
9299 process_full_comp_unit (per_cu, item->pretend_language);
9300
b4f54984 9301 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9302 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9303 }
10b3939b
DJ
9304
9305 item->per_cu->queued = 0;
9306 next_item = item->next;
9307 xfree (item);
9308 }
9309
9310 dwarf2_queue_tail = NULL;
45cfd468 9311
b4f54984 9312 if (dwarf_read_debug)
45cfd468
DE
9313 {
9314 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9315 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9316 }
10b3939b
DJ
9317}
9318
10b3939b
DJ
9319/* Read in full symbols for PST, and anything it depends on. */
9320
8566b89b
TT
9321void
9322dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9323{
10b3939b 9324 struct dwarf2_per_cu_data *per_cu;
aaa75496 9325
8566b89b 9326 if (readin)
95554aad
TT
9327 return;
9328
0494dbec 9329 read_dependencies (objfile);
aaa75496 9330
8566b89b 9331 per_cu = per_cu_data;
10b3939b
DJ
9332
9333 if (per_cu == NULL)
aaa75496
JB
9334 {
9335 /* It's an include file, no symbols to read for it.
9336 Everything is in the parent symtab. */
8566b89b 9337 readin = true;
aaa75496
JB
9338 return;
9339 }
c906108c 9340
58f0c718 9341 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9342}
9343
dee91e82
DE
9344/* Trivial hash function for die_info: the hash value of a DIE
9345 is its offset in .debug_info for this objfile. */
10b3939b 9346
dee91e82
DE
9347static hashval_t
9348die_hash (const void *item)
10b3939b 9349{
9a3c8263 9350 const struct die_info *die = (const struct die_info *) item;
6502dd73 9351
9c541725 9352 return to_underlying (die->sect_off);
dee91e82 9353}
63d06c5c 9354
dee91e82
DE
9355/* Trivial comparison function for die_info structures: two DIEs
9356 are equal if they have the same offset. */
98bfdba5 9357
dee91e82
DE
9358static int
9359die_eq (const void *item_lhs, const void *item_rhs)
9360{
9a3c8263
SM
9361 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9362 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9363
9c541725 9364 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9365}
c906108c 9366
c0ab21c2 9367/* Load the DIEs associated with PER_CU into memory. */
c906108c 9368
dee91e82 9369static void
c0ab21c2
TT
9370load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9371 bool skip_partial,
9372 enum language pretend_language)
dee91e82 9373{
c0ab21c2
TT
9374 gdb_assert (! this_cu->is_debug_types);
9375
9376 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9377 if (reader.dummy_p)
9378 return;
9379
9380 struct dwarf2_cu *cu = reader.cu;
9381 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9382
dee91e82
DE
9383 gdb_assert (cu->die_hash == NULL);
9384 cu->die_hash =
9385 htab_create_alloc_ex (cu->header.length / 12,
9386 die_hash,
9387 die_eq,
9388 NULL,
9389 &cu->comp_unit_obstack,
9390 hashtab_obstack_allocate,
9391 dummy_obstack_deallocate);
e142c38c 9392
c0ab21c2
TT
9393 if (reader.has_children)
9394 reader.comp_unit_die->child
9395 = read_die_and_siblings (&reader, reader.info_ptr,
9396 &info_ptr, reader.comp_unit_die);
9397 cu->dies = reader.comp_unit_die;
dee91e82 9398 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9399
9400 /* We try not to read any attributes in this function, because not
9cdd5dbd 9401 all CUs needed for references have been loaded yet, and symbol
10b3939b 9402 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9403 or we won't be able to build types correctly.
9404 Similarly, if we do not read the producer, we can not apply
9405 producer-specific interpretation. */
c0ab21c2 9406 prepare_one_comp_unit (cu, cu->dies, pretend_language);
10b3939b
DJ
9407}
9408
3da10d80
KS
9409/* Add a DIE to the delayed physname list. */
9410
9411static void
9412add_to_method_list (struct type *type, int fnfield_index, int index,
9413 const char *name, struct die_info *die,
9414 struct dwarf2_cu *cu)
9415{
9416 struct delayed_method_info mi;
9417 mi.type = type;
9418 mi.fnfield_index = fnfield_index;
9419 mi.index = index;
9420 mi.name = name;
9421 mi.die = die;
c89b44cd 9422 cu->method_list.push_back (mi);
3da10d80
KS
9423}
9424
3693fdb3
PA
9425/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9426 "const" / "volatile". If so, decrements LEN by the length of the
9427 modifier and return true. Otherwise return false. */
9428
9429template<size_t N>
9430static bool
9431check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9432{
9433 size_t mod_len = sizeof (mod) - 1;
9434 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9435 {
9436 len -= mod_len;
9437 return true;
9438 }
9439 return false;
9440}
9441
3da10d80
KS
9442/* Compute the physnames of any methods on the CU's method list.
9443
9444 The computation of method physnames is delayed in order to avoid the
9445 (bad) condition that one of the method's formal parameters is of an as yet
9446 incomplete type. */
9447
9448static void
9449compute_delayed_physnames (struct dwarf2_cu *cu)
9450{
3693fdb3 9451 /* Only C++ delays computing physnames. */
c89b44cd 9452 if (cu->method_list.empty ())
3693fdb3
PA
9453 return;
9454 gdb_assert (cu->language == language_cplus);
9455
52941706 9456 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9457 {
1d06ead6 9458 const char *physname;
3da10d80 9459 struct fn_fieldlist *fn_flp
c89b44cd
TT
9460 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9461 physname = dwarf2_physname (mi.name, mi.die, cu);
9462 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9463 = physname ? physname : "";
3693fdb3
PA
9464
9465 /* Since there's no tag to indicate whether a method is a
9466 const/volatile overload, extract that information out of the
9467 demangled name. */
9468 if (physname != NULL)
9469 {
9470 size_t len = strlen (physname);
9471
9472 while (1)
9473 {
9474 if (physname[len] == ')') /* shortcut */
9475 break;
9476 else if (check_modifier (physname, len, " const"))
c89b44cd 9477 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9478 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9479 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9480 else
9481 break;
9482 }
9483 }
3da10d80 9484 }
c89b44cd
TT
9485
9486 /* The list is no longer needed. */
9487 cu->method_list.clear ();
3da10d80
KS
9488}
9489
a766d390
DE
9490/* Go objects should be embedded in a DW_TAG_module DIE,
9491 and it's not clear if/how imported objects will appear.
9492 To keep Go support simple until that's worked out,
9493 go back through what we've read and create something usable.
9494 We could do this while processing each DIE, and feels kinda cleaner,
9495 but that way is more invasive.
9496 This is to, for example, allow the user to type "p var" or "b main"
9497 without having to specify the package name, and allow lookups
9498 of module.object to work in contexts that use the expression
9499 parser. */
9500
9501static void
9502fixup_go_packaging (struct dwarf2_cu *cu)
9503{
421d1616 9504 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9505 struct pending *list;
9506 int i;
9507
c24bdb02 9508 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9509 list != NULL;
9510 list = list->next)
a766d390
DE
9511 {
9512 for (i = 0; i < list->nsyms; ++i)
9513 {
9514 struct symbol *sym = list->symbol[i];
9515
c1b5c1eb 9516 if (sym->language () == language_go
a766d390
DE
9517 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9518 {
421d1616
TT
9519 gdb::unique_xmalloc_ptr<char> this_package_name
9520 (go_symbol_package_name (sym));
a766d390
DE
9521
9522 if (this_package_name == NULL)
9523 continue;
9524 if (package_name == NULL)
421d1616 9525 package_name = std::move (this_package_name);
a766d390
DE
9526 else
9527 {
518817b3
SM
9528 struct objfile *objfile
9529 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9530 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9531 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9532 (symbol_symtab (sym) != NULL
9533 ? symtab_to_filename_for_display
9534 (symbol_symtab (sym))
e3b94546 9535 : objfile_name (objfile)),
421d1616 9536 this_package_name.get (), package_name.get ());
a766d390
DE
9537 }
9538 }
9539 }
9540 }
9541
9542 if (package_name != NULL)
9543 {
518817b3 9544 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9545 const char *saved_package_name
421d1616 9546 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9547 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9548 saved_package_name);
a766d390
DE
9549 struct symbol *sym;
9550
e623cf5d 9551 sym = allocate_symbol (objfile);
d3ecddab 9552 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9553 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9554 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9555 e.g., "main" finds the "main" module and not C's main(). */
9556 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9557 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9558 SYMBOL_TYPE (sym) = type;
9559
c24bdb02 9560 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9561 }
9562}
9563
c9317f21
TT
9564/* Allocate a fully-qualified name consisting of the two parts on the
9565 obstack. */
9566
9567static const char *
9568rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9569{
9570 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9571}
9572
9573/* A helper that allocates a struct discriminant_info to attach to a
9574 union type. */
9575
9576static struct discriminant_info *
9577alloc_discriminant_info (struct type *type, int discriminant_index,
9578 int default_index)
9579{
9580 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9581 gdb_assert (discriminant_index == -1
9582 || (discriminant_index >= 0
9583 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9584 gdb_assert (default_index == -1
c7b15a66 9585 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9586
9587 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9588
9589 struct discriminant_info *disc
9590 = ((struct discriminant_info *)
9591 TYPE_ZALLOC (type,
9592 offsetof (struct discriminant_info, discriminants)
9593 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9594 disc->default_index = default_index;
9595 disc->discriminant_index = discriminant_index;
9596
9597 struct dynamic_prop prop;
9598 prop.kind = PROP_UNDEFINED;
9599 prop.data.baton = disc;
9600
9601 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9602
9603 return disc;
9604}
9605
9606/* Some versions of rustc emitted enums in an unusual way.
9607
9608 Ordinary enums were emitted as unions. The first element of each
9609 structure in the union was named "RUST$ENUM$DISR". This element
9610 held the discriminant.
9611
9612 These versions of Rust also implemented the "non-zero"
9613 optimization. When the enum had two values, and one is empty and
9614 the other holds a pointer that cannot be zero, the pointer is used
9615 as the discriminant, with a zero value meaning the empty variant.
9616 Here, the union's first member is of the form
9617 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9618 where the fieldnos are the indices of the fields that should be
9619 traversed in order to find the field (which may be several fields deep)
9620 and the variantname is the name of the variant of the case when the
9621 field is zero.
9622
9623 This function recognizes whether TYPE is of one of these forms,
9624 and, if so, smashes it to be a variant type. */
9625
9626static void
9627quirk_rust_enum (struct type *type, struct objfile *objfile)
9628{
9629 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9630
9631 /* We don't need to deal with empty enums. */
9632 if (TYPE_NFIELDS (type) == 0)
9633 return;
9634
9635#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9636 if (TYPE_NFIELDS (type) == 1
9637 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9638 {
9639 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9640
9641 /* Decode the field name to find the offset of the
9642 discriminant. */
9643 ULONGEST bit_offset = 0;
9644 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9645 while (name[0] >= '0' && name[0] <= '9')
9646 {
9647 char *tail;
9648 unsigned long index = strtoul (name, &tail, 10);
9649 name = tail;
9650 if (*name != '$'
9651 || index >= TYPE_NFIELDS (field_type)
9652 || (TYPE_FIELD_LOC_KIND (field_type, index)
9653 != FIELD_LOC_KIND_BITPOS))
9654 {
b98664d3 9655 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9656 "[in module %s]"),
9657 TYPE_FIELD_NAME (type, 0),
9658 objfile_name (objfile));
9659 return;
9660 }
9661 ++name;
9662
9663 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9664 field_type = TYPE_FIELD_TYPE (field_type, index);
9665 }
9666
9667 /* Make a union to hold the variants. */
9668 struct type *union_type = alloc_type (objfile);
9669 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9670 TYPE_NFIELDS (union_type) = 3;
9671 TYPE_FIELDS (union_type)
9672 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9673 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9674 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9675
9676 /* Put the discriminant must at index 0. */
9677 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9678 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9679 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9680 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9681
9682 /* The order of fields doesn't really matter, so put the real
9683 field at index 1 and the data-less field at index 2. */
9684 struct discriminant_info *disc
9685 = alloc_discriminant_info (union_type, 0, 1);
9686 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9687 TYPE_FIELD_NAME (union_type, 1)
9688 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9689 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9690 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9691 TYPE_FIELD_NAME (union_type, 1));
9692
9693 const char *dataless_name
9694 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9695 name);
9696 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9697 dataless_name);
9698 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9699 /* NAME points into the original discriminant name, which
9700 already has the correct lifetime. */
9701 TYPE_FIELD_NAME (union_type, 2) = name;
9702 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9703 disc->discriminants[2] = 0;
9704
9705 /* Smash this type to be a structure type. We have to do this
9706 because the type has already been recorded. */
9707 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9708 TYPE_NFIELDS (type) = 1;
9709 TYPE_FIELDS (type)
9710 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9711
9712 /* Install the variant part. */
9713 TYPE_FIELD_TYPE (type, 0) = union_type;
9714 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9715 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9716 }
77c2dba3
TT
9717 /* A union with a single anonymous field is probably an old-style
9718 univariant enum. */
9719 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9720 {
c9317f21
TT
9721 /* Smash this type to be a structure type. We have to do this
9722 because the type has already been recorded. */
9723 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9724
9725 /* Make a union to hold the variants. */
9726 struct type *union_type = alloc_type (objfile);
9727 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9728 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9729 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9730 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9731 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9732
9733 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9734 const char *variant_name
9735 = rust_last_path_segment (TYPE_NAME (field_type));
9736 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9737 TYPE_NAME (field_type)
9738 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9739 TYPE_NAME (type), variant_name);
c9317f21
TT
9740
9741 /* Install the union in the outer struct type. */
9742 TYPE_NFIELDS (type) = 1;
9743 TYPE_FIELDS (type)
9744 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9745 TYPE_FIELD_TYPE (type, 0) = union_type;
9746 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9747 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9748
9749 alloc_discriminant_info (union_type, -1, 0);
9750 }
9751 else
9752 {
9753 struct type *disr_type = nullptr;
9754 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9755 {
9756 disr_type = TYPE_FIELD_TYPE (type, i);
9757
a037790e
TT
9758 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9759 {
9760 /* All fields of a true enum will be structs. */
9761 return;
9762 }
9763 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9764 {
9765 /* Could be data-less variant, so keep going. */
a037790e 9766 disr_type = nullptr;
c9317f21
TT
9767 }
9768 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9769 "RUST$ENUM$DISR") != 0)
9770 {
9771 /* Not a Rust enum. */
9772 return;
9773 }
9774 else
9775 {
9776 /* Found one. */
9777 break;
9778 }
9779 }
9780
9781 /* If we got here without a discriminant, then it's probably
9782 just a union. */
9783 if (disr_type == nullptr)
9784 return;
9785
9786 /* Smash this type to be a structure type. We have to do this
9787 because the type has already been recorded. */
9788 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9789
9790 /* Make a union to hold the variants. */
9791 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9792 struct type *union_type = alloc_type (objfile);
9793 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9794 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9795 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9796 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9797 TYPE_FIELDS (union_type)
9798 = (struct field *) TYPE_ZALLOC (union_type,
9799 (TYPE_NFIELDS (union_type)
9800 * sizeof (struct field)));
9801
9802 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9803 TYPE_NFIELDS (type) * sizeof (struct field));
9804
9805 /* Install the discriminant at index 0 in the union. */
9806 TYPE_FIELD (union_type, 0) = *disr_field;
9807 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9808 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9809
9810 /* Install the union in the outer struct type. */
9811 TYPE_FIELD_TYPE (type, 0) = union_type;
9812 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9813 TYPE_NFIELDS (type) = 1;
9814
9815 /* Set the size and offset of the union type. */
9816 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9817
9818 /* We need a way to find the correct discriminant given a
9819 variant name. For convenience we build a map here. */
9820 struct type *enum_type = FIELD_TYPE (*disr_field);
9821 std::unordered_map<std::string, ULONGEST> discriminant_map;
9822 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9823 {
9824 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9825 {
9826 const char *name
9827 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9828 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9829 }
9830 }
9831
9832 int n_fields = TYPE_NFIELDS (union_type);
9833 struct discriminant_info *disc
9834 = alloc_discriminant_info (union_type, 0, -1);
9835 /* Skip the discriminant here. */
9836 for (int i = 1; i < n_fields; ++i)
9837 {
9838 /* Find the final word in the name of this variant's type.
9839 That name can be used to look up the correct
9840 discriminant. */
9841 const char *variant_name
9842 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9843 i)));
9844
9845 auto iter = discriminant_map.find (variant_name);
9846 if (iter != discriminant_map.end ())
9847 disc->discriminants[i] = iter->second;
9848
bedda9ac 9849 /* Remove the discriminant field, if it exists. */
c9317f21 9850 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9851 if (TYPE_NFIELDS (sub_type) > 0)
9852 {
9853 --TYPE_NFIELDS (sub_type);
9854 ++TYPE_FIELDS (sub_type);
9855 }
c9317f21
TT
9856 TYPE_FIELD_NAME (union_type, i) = variant_name;
9857 TYPE_NAME (sub_type)
9858 = rust_fully_qualify (&objfile->objfile_obstack,
9859 TYPE_NAME (type), variant_name);
9860 }
9861 }
9862}
9863
9864/* Rewrite some Rust unions to be structures with variants parts. */
9865
9866static void
9867rust_union_quirks (struct dwarf2_cu *cu)
9868{
9869 gdb_assert (cu->language == language_rust);
52941706
SM
9870 for (type *type_ : cu->rust_unions)
9871 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9872 /* We don't need this any more. */
9873 cu->rust_unions.clear ();
c9317f21
TT
9874}
9875
95554aad
TT
9876/* Return the symtab for PER_CU. This works properly regardless of
9877 whether we're using the index or psymtabs. */
9878
43f3e411
DE
9879static struct compunit_symtab *
9880get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9881{
ed2dc618 9882 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9883 ? per_cu->v.quick->compunit_symtab
9884 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9885}
9886
9887/* A helper function for computing the list of all symbol tables
9888 included by PER_CU. */
9889
9890static void
4c39bc03 9891recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9892 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9893 struct dwarf2_per_cu_data *per_cu,
43f3e411 9894 struct compunit_symtab *immediate_parent)
95554aad
TT
9895{
9896 void **slot;
43f3e411 9897 struct compunit_symtab *cust;
95554aad
TT
9898
9899 slot = htab_find_slot (all_children, per_cu, INSERT);
9900 if (*slot != NULL)
9901 {
9902 /* This inclusion and its children have been processed. */
9903 return;
9904 }
9905
9906 *slot = per_cu;
9907 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9908 cust = get_compunit_symtab (per_cu);
9909 if (cust != NULL)
ec94af83
DE
9910 {
9911 /* If this is a type unit only add its symbol table if we haven't
9912 seen it yet (type unit per_cu's can share symtabs). */
9913 if (per_cu->is_debug_types)
9914 {
43f3e411 9915 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9916 if (*slot == NULL)
9917 {
43f3e411 9918 *slot = cust;
4c39bc03 9919 result->push_back (cust);
43f3e411
DE
9920 if (cust->user == NULL)
9921 cust->user = immediate_parent;
ec94af83
DE
9922 }
9923 }
9924 else
f9125b6c 9925 {
4c39bc03 9926 result->push_back (cust);
43f3e411
DE
9927 if (cust->user == NULL)
9928 cust->user = immediate_parent;
f9125b6c 9929 }
ec94af83 9930 }
95554aad 9931
ae640021
AB
9932 if (!per_cu->imported_symtabs_empty ())
9933 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9934 {
9935 recursively_compute_inclusions (result, all_children,
9936 all_type_symtabs, ptr, cust);
9937 }
95554aad
TT
9938}
9939
43f3e411 9940/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9941 PER_CU. */
9942
9943static void
43f3e411 9944compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9945{
f4dc4d17
DE
9946 gdb_assert (! per_cu->is_debug_types);
9947
ae640021 9948 if (!per_cu->imported_symtabs_empty ())
95554aad 9949 {
ae640021 9950 int len;
4c39bc03 9951 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9952 htab_t all_children, all_type_symtabs;
43f3e411 9953 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9954
9955 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9956 if (cust == NULL)
95554aad
TT
9957 return;
9958
9959 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9960 NULL, xcalloc, xfree);
ec94af83
DE
9961 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9962 NULL, xcalloc, xfree);
95554aad 9963
ae640021 9964 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9965 {
9966 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9967 all_type_symtabs, ptr, cust);
ec94af83 9968 }
95554aad 9969
ec94af83 9970 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9971 len = result_symtabs.size ();
43f3e411 9972 cust->includes
ed2dc618 9973 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9974 struct compunit_symtab *, len + 1);
4c39bc03
TT
9975 memcpy (cust->includes, result_symtabs.data (),
9976 len * sizeof (compunit_symtab *));
43f3e411 9977 cust->includes[len] = NULL;
95554aad 9978
95554aad 9979 htab_delete (all_children);
ec94af83 9980 htab_delete (all_type_symtabs);
95554aad
TT
9981 }
9982}
9983
9984/* Compute the 'includes' field for the symtabs of all the CUs we just
9985 read. */
9986
9987static void
ed2dc618 9988process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9989{
71b73764 9990 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9991 {
9992 if (! iter->is_debug_types)
43f3e411 9993 compute_compunit_symtab_includes (iter);
f4dc4d17 9994 }
95554aad 9995
c5d0225d 9996 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9997}
9998
9cdd5dbd 9999/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10000 already been loaded into memory. */
10001
10002static void
95554aad
TT
10003process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10004 enum language pretend_language)
10b3939b 10005{
10b3939b 10006 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10007 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10008 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10009 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10010 CORE_ADDR lowpc, highpc;
43f3e411 10011 struct compunit_symtab *cust;
10b3939b 10012 CORE_ADDR baseaddr;
4359dff1 10013 struct block *static_block;
3e29f34a 10014 CORE_ADDR addr;
10b3939b 10015
b3b3bada 10016 baseaddr = objfile->text_section_offset ();
10b3939b 10017
c89b44cd
TT
10018 /* Clear the list here in case something was left over. */
10019 cu->method_list.clear ();
10b3939b 10020
95554aad
TT
10021 cu->language = pretend_language;
10022 cu->language_defn = language_def (cu->language);
10023
c906108c 10024 /* Do line number decoding in read_file_scope () */
10b3939b 10025 process_die (cu->dies, cu);
c906108c 10026
a766d390
DE
10027 /* For now fudge the Go package. */
10028 if (cu->language == language_go)
10029 fixup_go_packaging (cu);
10030
5f48f8f3 10031 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10032 should be complete, and it should now be safe to compute all of the
10033 physnames. */
10034 compute_delayed_physnames (cu);
3da10d80 10035
c9317f21
TT
10036 if (cu->language == language_rust)
10037 rust_union_quirks (cu);
10038
fae299cd
DC
10039 /* Some compilers don't define a DW_AT_high_pc attribute for the
10040 compilation unit. If the DW_AT_high_pc is missing, synthesize
10041 it, by scanning the DIE's below the compilation unit. */
10b3939b 10042 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10043
3e29f34a 10044 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10045 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10046
10047 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10048 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10049 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10050 addrmap to help ensure it has an accurate map of pc values belonging to
10051 this comp unit. */
10052 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10053
c24bdb02 10054 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10055 SECT_OFF_TEXT (objfile),
10056 0);
c906108c 10057
43f3e411 10058 if (cust != NULL)
c906108c 10059 {
df15bd07 10060 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10061
8be455d7
JK
10062 /* Set symtab language to language from DW_AT_language. If the
10063 compilation is from a C file generated by language preprocessors, do
10064 not set the language if it was already deduced by start_subfile. */
43f3e411 10065 if (!(cu->language == language_c
40e3ad0e 10066 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10067 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10068
10069 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10070 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10071 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10072 there were bugs in prologue debug info, fixed later in GCC-4.5
10073 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10074
10075 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10076 needed, it would be wrong due to missing DW_AT_producer there.
10077
10078 Still one can confuse GDB by using non-standard GCC compilation
10079 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10080 */
ab260dad 10081 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10082 cust->locations_valid = 1;
e0d00bc7
JK
10083
10084 if (gcc_4_minor >= 5)
43f3e411 10085 cust->epilogue_unwind_valid = 1;
96408a79 10086
43f3e411 10087 cust->call_site_htab = cu->call_site_htab;
c906108c 10088 }
9291a0cd
TT
10089
10090 if (dwarf2_per_objfile->using_index)
43f3e411 10091 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10092 else
10093 {
891813be 10094 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10095 pst->compunit_symtab = cust;
6d94535f 10096 pst->readin = true;
9291a0cd 10097 }
c906108c 10098
95554aad 10099 /* Push it for inclusion processing later. */
c5d0225d 10100 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10101
10102 /* Not needed any more. */
c24bdb02 10103 cu->reset_builder ();
f4dc4d17 10104}
45cfd468 10105
f4dc4d17
DE
10106/* Generate full symbol information for type unit PER_CU, whose DIEs have
10107 already been loaded into memory. */
10108
10109static void
10110process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10111 enum language pretend_language)
10112{
10113 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10114 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10115 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10116 struct compunit_symtab *cust;
0186c6a7
DE
10117 struct signatured_type *sig_type;
10118
10119 gdb_assert (per_cu->is_debug_types);
10120 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10121
c89b44cd
TT
10122 /* Clear the list here in case something was left over. */
10123 cu->method_list.clear ();
f4dc4d17 10124
f4dc4d17
DE
10125 cu->language = pretend_language;
10126 cu->language_defn = language_def (cu->language);
10127
10128 /* The symbol tables are set up in read_type_unit_scope. */
10129 process_die (cu->dies, cu);
10130
10131 /* For now fudge the Go package. */
10132 if (cu->language == language_go)
10133 fixup_go_packaging (cu);
10134
5f48f8f3 10135 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10136 should be complete, and it should now be safe to compute all of the
10137 physnames. */
10138 compute_delayed_physnames (cu);
f4dc4d17 10139
c9317f21
TT
10140 if (cu->language == language_rust)
10141 rust_union_quirks (cu);
10142
f4dc4d17
DE
10143 /* TUs share symbol tables.
10144 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10145 of it with end_expandable_symtab. Otherwise, complete the addition of
10146 this TU's symbols to the existing symtab. */
43f3e411 10147 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10148 {
c24bdb02
KS
10149 buildsym_compunit *builder = cu->get_builder ();
10150 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10151 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10152
43f3e411 10153 if (cust != NULL)
f4dc4d17
DE
10154 {
10155 /* Set symtab language to language from DW_AT_language. If the
10156 compilation is from a C file generated by language preprocessors,
10157 do not set the language if it was already deduced by
10158 start_subfile. */
43f3e411
DE
10159 if (!(cu->language == language_c
10160 && COMPUNIT_FILETABS (cust)->language != language_c))
10161 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10162 }
10163 }
10164 else
10165 {
c24bdb02 10166 cu->get_builder ()->augment_type_symtab ();
43f3e411 10167 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10168 }
10169
10170 if (dwarf2_per_objfile->using_index)
43f3e411 10171 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10172 else
10173 {
891813be 10174 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10175 pst->compunit_symtab = cust;
6d94535f 10176 pst->readin = true;
45cfd468 10177 }
804d2729
TT
10178
10179 /* Not needed any more. */
c24bdb02 10180 cu->reset_builder ();
c906108c
SS
10181}
10182
95554aad
TT
10183/* Process an imported unit DIE. */
10184
10185static void
10186process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10187{
10188 struct attribute *attr;
10189
f4dc4d17
DE
10190 /* For now we don't handle imported units in type units. */
10191 if (cu->per_cu->is_debug_types)
10192 {
10193 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10194 " supported in type units [in module %s]"),
518817b3 10195 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10196 }
10197
95554aad
TT
10198 attr = dwarf2_attr (die, DW_AT_import, cu);
10199 if (attr != NULL)
10200 {
9c541725
PA
10201 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10202 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10203 dwarf2_per_cu_data *per_cu
e3b94546 10204 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10205 cu->per_cu->dwarf2_per_objfile);
95554aad 10206
69d751e3 10207 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10208 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10209 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10210
ae640021 10211 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10212 }
10213}
10214
4c8aa72d
PA
10215/* RAII object that represents a process_die scope: i.e.,
10216 starts/finishes processing a DIE. */
10217class process_die_scope
adde2bff 10218{
4c8aa72d
PA
10219public:
10220 process_die_scope (die_info *die, dwarf2_cu *cu)
10221 : m_die (die), m_cu (cu)
10222 {
10223 /* We should only be processing DIEs not already in process. */
10224 gdb_assert (!m_die->in_process);
10225 m_die->in_process = true;
10226 }
8c3cb9fa 10227
4c8aa72d
PA
10228 ~process_die_scope ()
10229 {
10230 m_die->in_process = false;
10231
10232 /* If we're done processing the DIE for the CU that owns the line
10233 header, we don't need the line header anymore. */
10234 if (m_cu->line_header_die_owner == m_die)
10235 {
10236 delete m_cu->line_header;
10237 m_cu->line_header = NULL;
10238 m_cu->line_header_die_owner = NULL;
10239 }
10240 }
10241
10242private:
10243 die_info *m_die;
10244 dwarf2_cu *m_cu;
10245};
adde2bff 10246
c906108c
SS
10247/* Process a die and its children. */
10248
10249static void
e7c27a73 10250process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10251{
4c8aa72d 10252 process_die_scope scope (die, cu);
adde2bff 10253
c906108c
SS
10254 switch (die->tag)
10255 {
10256 case DW_TAG_padding:
10257 break;
10258 case DW_TAG_compile_unit:
95554aad 10259 case DW_TAG_partial_unit:
e7c27a73 10260 read_file_scope (die, cu);
c906108c 10261 break;
348e048f
DE
10262 case DW_TAG_type_unit:
10263 read_type_unit_scope (die, cu);
10264 break;
c906108c 10265 case DW_TAG_subprogram:
0a4b0913
AB
10266 /* Nested subprograms in Fortran get a prefix. */
10267 if (cu->language == language_fortran
10268 && die->parent != NULL
10269 && die->parent->tag == DW_TAG_subprogram)
10270 cu->processing_has_namespace_info = true;
10271 /* Fall through. */
c906108c 10272 case DW_TAG_inlined_subroutine:
edb3359d 10273 read_func_scope (die, cu);
c906108c
SS
10274 break;
10275 case DW_TAG_lexical_block:
14898363
L
10276 case DW_TAG_try_block:
10277 case DW_TAG_catch_block:
e7c27a73 10278 read_lexical_block_scope (die, cu);
c906108c 10279 break;
216f72a1 10280 case DW_TAG_call_site:
96408a79
SA
10281 case DW_TAG_GNU_call_site:
10282 read_call_site_scope (die, cu);
10283 break;
c906108c 10284 case DW_TAG_class_type:
680b30c7 10285 case DW_TAG_interface_type:
c906108c
SS
10286 case DW_TAG_structure_type:
10287 case DW_TAG_union_type:
134d01f1 10288 process_structure_scope (die, cu);
c906108c
SS
10289 break;
10290 case DW_TAG_enumeration_type:
134d01f1 10291 process_enumeration_scope (die, cu);
c906108c 10292 break;
134d01f1 10293
f792889a
DJ
10294 /* These dies have a type, but processing them does not create
10295 a symbol or recurse to process the children. Therefore we can
10296 read them on-demand through read_type_die. */
c906108c 10297 case DW_TAG_subroutine_type:
72019c9c 10298 case DW_TAG_set_type:
c906108c 10299 case DW_TAG_array_type:
c906108c 10300 case DW_TAG_pointer_type:
c906108c 10301 case DW_TAG_ptr_to_member_type:
c906108c 10302 case DW_TAG_reference_type:
4297a3f0 10303 case DW_TAG_rvalue_reference_type:
c906108c 10304 case DW_TAG_string_type:
c906108c 10305 break;
134d01f1 10306
c906108c 10307 case DW_TAG_base_type:
a02abb62 10308 case DW_TAG_subrange_type:
cb249c71 10309 case DW_TAG_typedef:
134d01f1
DJ
10310 /* Add a typedef symbol for the type definition, if it has a
10311 DW_AT_name. */
f792889a 10312 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10313 break;
c906108c 10314 case DW_TAG_common_block:
e7c27a73 10315 read_common_block (die, cu);
c906108c
SS
10316 break;
10317 case DW_TAG_common_inclusion:
10318 break;
d9fa45fe 10319 case DW_TAG_namespace:
9068261f 10320 cu->processing_has_namespace_info = true;
e7c27a73 10321 read_namespace (die, cu);
d9fa45fe 10322 break;
5d7cb8df 10323 case DW_TAG_module:
9068261f 10324 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10325 read_module (die, cu);
10326 break;
d9fa45fe 10327 case DW_TAG_imported_declaration:
9068261f 10328 cu->processing_has_namespace_info = true;
74921315
KS
10329 if (read_namespace_alias (die, cu))
10330 break;
86a73007
TT
10331 /* The declaration is not a global namespace alias. */
10332 /* Fall through. */
d9fa45fe 10333 case DW_TAG_imported_module:
9068261f 10334 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10335 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10336 || cu->language != language_fortran))
b98664d3 10337 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10338 dwarf_tag_name (die->tag));
10339 read_import_statement (die, cu);
d9fa45fe 10340 break;
95554aad
TT
10341
10342 case DW_TAG_imported_unit:
10343 process_imported_unit_die (die, cu);
10344 break;
10345
71a3c369
TT
10346 case DW_TAG_variable:
10347 read_variable (die, cu);
10348 break;
10349
c906108c 10350 default:
e7c27a73 10351 new_symbol (die, NULL, cu);
c906108c
SS
10352 break;
10353 }
10354}
ca69b9e6
DE
10355\f
10356/* DWARF name computation. */
c906108c 10357
94af9270
KS
10358/* A helper function for dwarf2_compute_name which determines whether DIE
10359 needs to have the name of the scope prepended to the name listed in the
10360 die. */
10361
10362static int
10363die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10364{
1c809c68
TT
10365 struct attribute *attr;
10366
94af9270
KS
10367 switch (die->tag)
10368 {
10369 case DW_TAG_namespace:
10370 case DW_TAG_typedef:
10371 case DW_TAG_class_type:
10372 case DW_TAG_interface_type:
10373 case DW_TAG_structure_type:
10374 case DW_TAG_union_type:
10375 case DW_TAG_enumeration_type:
10376 case DW_TAG_enumerator:
10377 case DW_TAG_subprogram:
08a76f8a 10378 case DW_TAG_inlined_subroutine:
94af9270 10379 case DW_TAG_member:
74921315 10380 case DW_TAG_imported_declaration:
94af9270
KS
10381 return 1;
10382
10383 case DW_TAG_variable:
c2b0a229 10384 case DW_TAG_constant:
94af9270
KS
10385 /* We only need to prefix "globally" visible variables. These include
10386 any variable marked with DW_AT_external or any variable that
10387 lives in a namespace. [Variables in anonymous namespaces
10388 require prefixing, but they are not DW_AT_external.] */
10389
10390 if (dwarf2_attr (die, DW_AT_specification, cu))
10391 {
10392 struct dwarf2_cu *spec_cu = cu;
9a619af0 10393
94af9270
KS
10394 return die_needs_namespace (die_specification (die, &spec_cu),
10395 spec_cu);
10396 }
10397
1c809c68 10398 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10399 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10400 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10401 return 0;
10402 /* A variable in a lexical block of some kind does not need a
10403 namespace, even though in C++ such variables may be external
10404 and have a mangled name. */
10405 if (die->parent->tag == DW_TAG_lexical_block
10406 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10407 || die->parent->tag == DW_TAG_catch_block
10408 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10409 return 0;
10410 return 1;
94af9270
KS
10411
10412 default:
10413 return 0;
10414 }
10415}
10416
73b9be8b
KS
10417/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10418 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10419 defined for the given DIE. */
10420
10421static struct attribute *
10422dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10423{
10424 struct attribute *attr;
10425
10426 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10427 if (attr == NULL)
10428 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10429
10430 return attr;
10431}
10432
10433/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10434 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10435 defined for the given DIE. */
10436
10437static const char *
10438dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10439{
10440 const char *linkage_name;
10441
10442 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10443 if (linkage_name == NULL)
10444 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10445
10446 return linkage_name;
10447}
10448
94af9270 10449/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10450 compute the physname for the object, which include a method's:
9c37b5ae 10451 - formal parameters (C++),
a766d390 10452 - receiver type (Go),
a766d390
DE
10453
10454 The term "physname" is a bit confusing.
10455 For C++, for example, it is the demangled name.
10456 For Go, for example, it's the mangled name.
94af9270 10457
af6b7be1
JB
10458 For Ada, return the DIE's linkage name rather than the fully qualified
10459 name. PHYSNAME is ignored..
10460
94af9270
KS
10461 The result is allocated on the objfile_obstack and canonicalized. */
10462
10463static const char *
15d034d0
TT
10464dwarf2_compute_name (const char *name,
10465 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10466 int physname)
10467{
518817b3 10468 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10469
94af9270
KS
10470 if (name == NULL)
10471 name = dwarf2_name (die, cu);
10472
2ee7123e
DE
10473 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10474 but otherwise compute it by typename_concat inside GDB.
10475 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10476 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10477 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10478 will set the demangled name to the result of dwarf2_full_name, and it is
10479 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10480 if (cu->language == language_ada
10481 || (cu->language == language_fortran && physname))
10482 {
10483 /* For Ada unit, we prefer the linkage name over the name, as
10484 the former contains the exported name, which the user expects
10485 to be able to reference. Ideally, we want the user to be able
10486 to reference this entity using either natural or linkage name,
10487 but we haven't started looking at this enhancement yet. */
73b9be8b 10488 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10489
2ee7123e
DE
10490 if (linkage_name != NULL)
10491 return linkage_name;
f55ee35c
JK
10492 }
10493
94af9270
KS
10494 /* These are the only languages we know how to qualify names in. */
10495 if (name != NULL
9c37b5ae 10496 && (cu->language == language_cplus
c44af4eb
TT
10497 || cu->language == language_fortran || cu->language == language_d
10498 || cu->language == language_rust))
94af9270
KS
10499 {
10500 if (die_needs_namespace (die, cu))
10501 {
0d5cff50 10502 const char *prefix;
34a68019 10503 const char *canonical_name = NULL;
94af9270 10504
d7e74731
PA
10505 string_file buf;
10506
94af9270 10507 prefix = determine_prefix (die, cu);
94af9270
KS
10508 if (*prefix != '\0')
10509 {
43816ebc
TT
10510 gdb::unique_xmalloc_ptr<char> prefixed_name
10511 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10512
43816ebc 10513 buf.puts (prefixed_name.get ());
94af9270
KS
10514 }
10515 else
d7e74731 10516 buf.puts (name);
94af9270 10517
98bfdba5
PA
10518 /* Template parameters may be specified in the DIE's DW_AT_name, or
10519 as children with DW_TAG_template_type_param or
10520 DW_TAG_value_type_param. If the latter, add them to the name
10521 here. If the name already has template parameters, then
10522 skip this step; some versions of GCC emit both, and
10523 it is more efficient to use the pre-computed name.
10524
10525 Something to keep in mind about this process: it is very
10526 unlikely, or in some cases downright impossible, to produce
10527 something that will match the mangled name of a function.
10528 If the definition of the function has the same debug info,
10529 we should be able to match up with it anyway. But fallbacks
10530 using the minimal symbol, for instance to find a method
10531 implemented in a stripped copy of libstdc++, will not work.
10532 If we do not have debug info for the definition, we will have to
10533 match them up some other way.
10534
10535 When we do name matching there is a related problem with function
10536 templates; two instantiated function templates are allowed to
10537 differ only by their return types, which we do not add here. */
10538
10539 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10540 {
10541 struct attribute *attr;
10542 struct die_info *child;
10543 int first = 1;
10544
10545 die->building_fullname = 1;
10546
10547 for (child = die->child; child != NULL; child = child->sibling)
10548 {
10549 struct type *type;
12df843f 10550 LONGEST value;
d521ce57 10551 const gdb_byte *bytes;
98bfdba5
PA
10552 struct dwarf2_locexpr_baton *baton;
10553 struct value *v;
10554
10555 if (child->tag != DW_TAG_template_type_param
10556 && child->tag != DW_TAG_template_value_param)
10557 continue;
10558
10559 if (first)
10560 {
d7e74731 10561 buf.puts ("<");
98bfdba5
PA
10562 first = 0;
10563 }
10564 else
d7e74731 10565 buf.puts (", ");
98bfdba5
PA
10566
10567 attr = dwarf2_attr (child, DW_AT_type, cu);
10568 if (attr == NULL)
10569 {
b98664d3 10570 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10571 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10572 continue;
10573 }
10574 type = die_type (child, cu);
10575
10576 if (child->tag == DW_TAG_template_type_param)
10577 {
c1ec8cea
TT
10578 c_print_type (type, "", &buf, -1, 0, cu->language,
10579 &type_print_raw_options);
98bfdba5
PA
10580 continue;
10581 }
10582
10583 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10584 if (attr == NULL)
10585 {
b98664d3 10586 complaint (_("template parameter missing "
3e43a32a 10587 "DW_AT_const_value"));
d7e74731 10588 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10589 continue;
10590 }
10591
10592 dwarf2_const_value_attr (attr, type, name,
10593 &cu->comp_unit_obstack, cu,
10594 &value, &bytes, &baton);
10595
10596 if (TYPE_NOSIGN (type))
10597 /* GDB prints characters as NUMBER 'CHAR'. If that's
10598 changed, this can use value_print instead. */
d7e74731 10599 c_printchar (value, type, &buf);
98bfdba5
PA
10600 else
10601 {
10602 struct value_print_options opts;
10603
10604 if (baton != NULL)
10605 v = dwarf2_evaluate_loc_desc (type, NULL,
10606 baton->data,
10607 baton->size,
10608 baton->per_cu);
10609 else if (bytes != NULL)
10610 {
10611 v = allocate_value (type);
10612 memcpy (value_contents_writeable (v), bytes,
10613 TYPE_LENGTH (type));
10614 }
10615 else
10616 v = value_from_longest (type, value);
10617
3e43a32a
MS
10618 /* Specify decimal so that we do not depend on
10619 the radix. */
98bfdba5
PA
10620 get_formatted_print_options (&opts, 'd');
10621 opts.raw = 1;
d7e74731 10622 value_print (v, &buf, &opts);
98bfdba5 10623 release_value (v);
98bfdba5
PA
10624 }
10625 }
10626
10627 die->building_fullname = 0;
10628
10629 if (!first)
10630 {
10631 /* Close the argument list, with a space if necessary
10632 (nested templates). */
d7e74731
PA
10633 if (!buf.empty () && buf.string ().back () == '>')
10634 buf.puts (" >");
98bfdba5 10635 else
d7e74731 10636 buf.puts (">");
98bfdba5
PA
10637 }
10638 }
10639
9c37b5ae 10640 /* For C++ methods, append formal parameter type
94af9270 10641 information, if PHYSNAME. */
6e70227d 10642
94af9270 10643 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10644 && cu->language == language_cplus)
94af9270
KS
10645 {
10646 struct type *type = read_type_die (die, cu);
10647
d7e74731 10648 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10649 &type_print_raw_options);
94af9270 10650
9c37b5ae 10651 if (cu->language == language_cplus)
94af9270 10652 {
60430eff
DJ
10653 /* Assume that an artificial first parameter is
10654 "this", but do not crash if it is not. RealView
10655 marks unnamed (and thus unused) parameters as
10656 artificial; there is no way to differentiate
10657 the two cases. */
94af9270
KS
10658 if (TYPE_NFIELDS (type) > 0
10659 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10660 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10661 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10662 0))))
d7e74731 10663 buf.puts (" const");
94af9270
KS
10664 }
10665 }
10666
d7e74731 10667 const std::string &intermediate_name = buf.string ();
94af9270
KS
10668
10669 if (cu->language == language_cplus)
34a68019 10670 canonical_name
322a8516 10671 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10672 &objfile->per_bfd->storage_obstack);
10673
10674 /* If we only computed INTERMEDIATE_NAME, or if
10675 INTERMEDIATE_NAME is already canonical, then we need to
10676 copy it to the appropriate obstack. */
322a8516 10677 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10678 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10679 intermediate_name);
34a68019
TT
10680 else
10681 name = canonical_name;
94af9270
KS
10682 }
10683 }
10684
10685 return name;
10686}
10687
0114d602
DJ
10688/* Return the fully qualified name of DIE, based on its DW_AT_name.
10689 If scope qualifiers are appropriate they will be added. The result
34a68019 10690 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10691 not have a name. NAME may either be from a previous call to
10692 dwarf2_name or NULL.
10693
9c37b5ae 10694 The output string will be canonicalized (if C++). */
0114d602
DJ
10695
10696static const char *
15d034d0 10697dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10698{
94af9270
KS
10699 return dwarf2_compute_name (name, die, cu, 0);
10700}
0114d602 10701
94af9270
KS
10702/* Construct a physname for the given DIE in CU. NAME may either be
10703 from a previous call to dwarf2_name or NULL. The result will be
10704 allocated on the objfile_objstack or NULL if the DIE does not have a
10705 name.
0114d602 10706
9c37b5ae 10707 The output string will be canonicalized (if C++). */
0114d602 10708
94af9270 10709static const char *
15d034d0 10710dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10711{
518817b3 10712 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10713 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10714 int need_copy = 1;
10715
10716 /* In this case dwarf2_compute_name is just a shortcut not building anything
10717 on its own. */
10718 if (!die_needs_namespace (die, cu))
10719 return dwarf2_compute_name (name, die, cu, 1);
10720
73b9be8b 10721 mangled = dw2_linkage_name (die, cu);
900e11f9 10722
e98c9e7c
TT
10723 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10724 See https://github.com/rust-lang/rust/issues/32925. */
10725 if (cu->language == language_rust && mangled != NULL
10726 && strchr (mangled, '{') != NULL)
10727 mangled = NULL;
10728
900e11f9
JK
10729 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10730 has computed. */
791afaa2 10731 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10732 if (mangled != NULL)
900e11f9 10733 {
900e11f9 10734
59cc4834
JB
10735 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10736 {
10737 /* Do nothing (do not demangle the symbol name). */
10738 }
10739 else if (cu->language == language_go)
a766d390 10740 {
5e2db402
TT
10741 /* This is a lie, but we already lie to the caller new_symbol.
10742 new_symbol assumes we return the mangled name.
a766d390 10743 This just undoes that lie until things are cleaned up. */
a766d390
DE
10744 }
10745 else
10746 {
0eb876f5
JB
10747 /* Use DMGL_RET_DROP for C++ template functions to suppress
10748 their return type. It is easier for GDB users to search
10749 for such functions as `name(params)' than `long name(params)'.
10750 In such case the minimal symbol names do not match the full
10751 symbol names but for template functions there is never a need
10752 to look up their definition from their declaration so
10753 the only disadvantage remains the minimal symbol variant
10754 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10755 demangled.reset (gdb_demangle (mangled,
10756 (DMGL_PARAMS | DMGL_ANSI
10757 | DMGL_RET_DROP)));
a766d390 10758 }
900e11f9 10759 if (demangled)
791afaa2 10760 canon = demangled.get ();
900e11f9
JK
10761 else
10762 {
10763 canon = mangled;
10764 need_copy = 0;
10765 }
10766 }
10767
10768 if (canon == NULL || check_physname)
10769 {
10770 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10771
10772 if (canon != NULL && strcmp (physname, canon) != 0)
10773 {
10774 /* It may not mean a bug in GDB. The compiler could also
10775 compute DW_AT_linkage_name incorrectly. But in such case
10776 GDB would need to be bug-to-bug compatible. */
10777
b98664d3 10778 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10779 "(from linkage <%s>) - DIE at %s [in module %s]"),
10780 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10781 objfile_name (objfile));
900e11f9
JK
10782
10783 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10784 is available here - over computed PHYSNAME. It is safer
10785 against both buggy GDB and buggy compilers. */
10786
10787 retval = canon;
10788 }
10789 else
10790 {
10791 retval = physname;
10792 need_copy = 0;
10793 }
10794 }
10795 else
10796 retval = canon;
10797
10798 if (need_copy)
021887d8 10799 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 10800
900e11f9 10801 return retval;
0114d602
DJ
10802}
10803
74921315
KS
10804/* Inspect DIE in CU for a namespace alias. If one exists, record
10805 a new symbol for it.
10806
10807 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10808
10809static int
10810read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10811{
10812 struct attribute *attr;
10813
10814 /* If the die does not have a name, this is not a namespace
10815 alias. */
10816 attr = dwarf2_attr (die, DW_AT_name, cu);
10817 if (attr != NULL)
10818 {
10819 int num;
10820 struct die_info *d = die;
10821 struct dwarf2_cu *imported_cu = cu;
10822
10823 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10824 keep inspecting DIEs until we hit the underlying import. */
10825#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10826 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10827 {
10828 attr = dwarf2_attr (d, DW_AT_import, cu);
10829 if (attr == NULL)
10830 break;
10831
10832 d = follow_die_ref (d, attr, &imported_cu);
10833 if (d->tag != DW_TAG_imported_declaration)
10834 break;
10835 }
10836
10837 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10838 {
b98664d3 10839 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10840 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10841 return 0;
10842 }
10843
10844 if (attr != NULL)
10845 {
10846 struct type *type;
9c541725 10847 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10848
9c541725 10849 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10850 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10851 {
10852 /* This declaration is a global namespace alias. Add
10853 a symbol for it whose type is the aliased namespace. */
10854 new_symbol (die, type, cu);
10855 return 1;
10856 }
10857 }
10858 }
10859
10860 return 0;
10861}
10862
22cee43f 10863/* Return the using directives repository (global or local?) to use in the
804d2729 10864 current context for CU.
22cee43f
PMR
10865
10866 For Ada, imported declarations can materialize renamings, which *may* be
10867 global. However it is impossible (for now?) in DWARF to distinguish
10868 "external" imported declarations and "static" ones. As all imported
10869 declarations seem to be static in all other languages, make them all CU-wide
10870 global only in Ada. */
10871
10872static struct using_direct **
804d2729 10873using_directives (struct dwarf2_cu *cu)
22cee43f 10874{
c24bdb02
KS
10875 if (cu->language == language_ada
10876 && cu->get_builder ()->outermost_context_p ())
10877 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10878 else
c24bdb02 10879 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10880}
10881
27aa8d6a
SW
10882/* Read the import statement specified by the given die and record it. */
10883
10884static void
10885read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10886{
518817b3 10887 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10888 struct attribute *import_attr;
32019081 10889 struct die_info *imported_die, *child_die;
de4affc9 10890 struct dwarf2_cu *imported_cu;
27aa8d6a 10891 const char *imported_name;
794684b6 10892 const char *imported_name_prefix;
13387711
SW
10893 const char *canonical_name;
10894 const char *import_alias;
10895 const char *imported_declaration = NULL;
794684b6 10896 const char *import_prefix;
eb1e02fd 10897 std::vector<const char *> excludes;
13387711 10898
27aa8d6a
SW
10899 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10900 if (import_attr == NULL)
10901 {
b98664d3 10902 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10903 dwarf_tag_name (die->tag));
10904 return;
10905 }
10906
de4affc9
CC
10907 imported_cu = cu;
10908 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10909 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10910 if (imported_name == NULL)
10911 {
10912 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10913
10914 The import in the following code:
10915 namespace A
10916 {
10917 typedef int B;
10918 }
10919
10920 int main ()
10921 {
10922 using A::B;
10923 B b;
10924 return b;
10925 }
10926
10927 ...
10928 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10929 <52> DW_AT_decl_file : 1
10930 <53> DW_AT_decl_line : 6
10931 <54> DW_AT_import : <0x75>
10932 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10933 <59> DW_AT_name : B
10934 <5b> DW_AT_decl_file : 1
10935 <5c> DW_AT_decl_line : 2
10936 <5d> DW_AT_type : <0x6e>
10937 ...
10938 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10939 <76> DW_AT_byte_size : 4
10940 <77> DW_AT_encoding : 5 (signed)
10941
10942 imports the wrong die ( 0x75 instead of 0x58 ).
10943 This case will be ignored until the gcc bug is fixed. */
10944 return;
10945 }
10946
82856980
SW
10947 /* Figure out the local name after import. */
10948 import_alias = dwarf2_name (die, cu);
27aa8d6a 10949
794684b6
SW
10950 /* Figure out where the statement is being imported to. */
10951 import_prefix = determine_prefix (die, cu);
10952
10953 /* Figure out what the scope of the imported die is and prepend it
10954 to the name of the imported die. */
de4affc9 10955 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10956
f55ee35c
JK
10957 if (imported_die->tag != DW_TAG_namespace
10958 && imported_die->tag != DW_TAG_module)
794684b6 10959 {
13387711
SW
10960 imported_declaration = imported_name;
10961 canonical_name = imported_name_prefix;
794684b6 10962 }
13387711 10963 else if (strlen (imported_name_prefix) > 0)
12aaed36 10964 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10965 imported_name_prefix,
10966 (cu->language == language_d ? "." : "::"),
10967 imported_name, (char *) NULL);
13387711
SW
10968 else
10969 canonical_name = imported_name;
794684b6 10970
32019081
JK
10971 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10972 for (child_die = die->child; child_die && child_die->tag;
10973 child_die = sibling_die (child_die))
10974 {
10975 /* DWARF-4: A Fortran use statement with a “rename list” may be
10976 represented by an imported module entry with an import attribute
10977 referring to the module and owned entries corresponding to those
10978 entities that are renamed as part of being imported. */
10979
10980 if (child_die->tag != DW_TAG_imported_declaration)
10981 {
b98664d3 10982 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10983 "- DIE at %s [in module %s]"),
10984 sect_offset_str (child_die->sect_off),
10985 objfile_name (objfile));
32019081
JK
10986 continue;
10987 }
10988
10989 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10990 if (import_attr == NULL)
10991 {
b98664d3 10992 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10993 dwarf_tag_name (child_die->tag));
10994 continue;
10995 }
10996
10997 imported_cu = cu;
10998 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10999 &imported_cu);
11000 imported_name = dwarf2_name (imported_die, imported_cu);
11001 if (imported_name == NULL)
11002 {
b98664d3 11003 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11004 "imported name - DIE at %s [in module %s]"),
11005 sect_offset_str (child_die->sect_off),
11006 objfile_name (objfile));
32019081
JK
11007 continue;
11008 }
11009
eb1e02fd 11010 excludes.push_back (imported_name);
32019081
JK
11011
11012 process_die (child_die, cu);
11013 }
11014
804d2729 11015 add_using_directive (using_directives (cu),
22cee43f
PMR
11016 import_prefix,
11017 canonical_name,
11018 import_alias,
11019 imported_declaration,
11020 excludes,
11021 0,
11022 &objfile->objfile_obstack);
27aa8d6a
SW
11023}
11024
5230b05a
WT
11025/* ICC<14 does not output the required DW_AT_declaration on incomplete
11026 types, but gives them a size of zero. Starting with version 14,
11027 ICC is compatible with GCC. */
11028
9068261f 11029static bool
5230b05a
WT
11030producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11031{
11032 if (!cu->checked_producer)
11033 check_producer (cu);
11034
11035 return cu->producer_is_icc_lt_14;
11036}
11037
eb77c9df
AB
11038/* ICC generates a DW_AT_type for C void functions. This was observed on
11039 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11040 which says that void functions should not have a DW_AT_type. */
11041
11042static bool
11043producer_is_icc (struct dwarf2_cu *cu)
11044{
11045 if (!cu->checked_producer)
11046 check_producer (cu);
11047
11048 return cu->producer_is_icc;
11049}
11050
1b80a9fa
JK
11051/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11052 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11053 this, it was first present in GCC release 4.3.0. */
11054
9068261f 11055static bool
1b80a9fa
JK
11056producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11057{
11058 if (!cu->checked_producer)
11059 check_producer (cu);
11060
11061 return cu->producer_is_gcc_lt_4_3;
11062}
11063
d721ba37
PA
11064static file_and_directory
11065find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11066{
d721ba37
PA
11067 file_and_directory res;
11068
9291a0cd
TT
11069 /* Find the filename. Do not use dwarf2_name here, since the filename
11070 is not a source language identifier. */
d721ba37
PA
11071 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11072 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11073
d721ba37
PA
11074 if (res.comp_dir == NULL
11075 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11076 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11077 {
d721ba37
PA
11078 res.comp_dir_storage = ldirname (res.name);
11079 if (!res.comp_dir_storage.empty ())
11080 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11081 }
d721ba37 11082 if (res.comp_dir != NULL)
9291a0cd
TT
11083 {
11084 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11085 directory, get rid of it. */
d721ba37 11086 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11087
d721ba37
PA
11088 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11089 res.comp_dir = cp + 1;
9291a0cd
TT
11090 }
11091
d721ba37
PA
11092 if (res.name == NULL)
11093 res.name = "<unknown>";
11094
11095 return res;
9291a0cd
TT
11096}
11097
f4dc4d17
DE
11098/* Handle DW_AT_stmt_list for a compilation unit.
11099 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11100 COMP_DIR is the compilation directory. LOWPC is passed to
11101 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11102
11103static void
11104handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11105 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11106{
518817b3
SM
11107 struct dwarf2_per_objfile *dwarf2_per_objfile
11108 = cu->per_cu->dwarf2_per_objfile;
527f3840 11109 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11110 struct attribute *attr;
527f3840
JK
11111 struct line_header line_header_local;
11112 hashval_t line_header_local_hash;
527f3840
JK
11113 void **slot;
11114 int decode_mapping;
2ab95328 11115
f4dc4d17
DE
11116 gdb_assert (! cu->per_cu->is_debug_types);
11117
2ab95328 11118 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11119 if (attr == NULL)
11120 return;
11121
9c541725 11122 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11123
11124 /* The line header hash table is only created if needed (it exists to
11125 prevent redundant reading of the line table for partial_units).
11126 If we're given a partial_unit, we'll need it. If we're given a
11127 compile_unit, then use the line header hash table if it's already
11128 created, but don't create one just yet. */
11129
11130 if (dwarf2_per_objfile->line_header_hash == NULL
11131 && die->tag == DW_TAG_partial_unit)
2ab95328 11132 {
527f3840
JK
11133 dwarf2_per_objfile->line_header_hash
11134 = htab_create_alloc_ex (127, line_header_hash_voidp,
11135 line_header_eq_voidp,
11136 free_line_header_voidp,
11137 &objfile->objfile_obstack,
11138 hashtab_obstack_allocate,
11139 dummy_obstack_deallocate);
11140 }
2ab95328 11141
9c541725 11142 line_header_local.sect_off = line_offset;
527f3840
JK
11143 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11144 line_header_local_hash = line_header_hash (&line_header_local);
11145 if (dwarf2_per_objfile->line_header_hash != NULL)
11146 {
11147 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11148 &line_header_local,
11149 line_header_local_hash, NO_INSERT);
11150
11151 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11152 is not present in *SLOT (since if there is something in *SLOT then
11153 it will be for a partial_unit). */
11154 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11155 {
527f3840 11156 gdb_assert (*slot != NULL);
9a3c8263 11157 cu->line_header = (struct line_header *) *slot;
527f3840 11158 return;
dee91e82 11159 }
2ab95328 11160 }
527f3840
JK
11161
11162 /* dwarf_decode_line_header does not yet provide sufficient information.
11163 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11164 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11165 if (lh == NULL)
527f3840 11166 return;
4c8aa72d
PA
11167
11168 cu->line_header = lh.release ();
11169 cu->line_header_die_owner = die;
527f3840
JK
11170
11171 if (dwarf2_per_objfile->line_header_hash == NULL)
11172 slot = NULL;
11173 else
11174 {
11175 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11176 &line_header_local,
11177 line_header_local_hash, INSERT);
11178 gdb_assert (slot != NULL);
11179 }
11180 if (slot != NULL && *slot == NULL)
11181 {
11182 /* This newly decoded line number information unit will be owned
11183 by line_header_hash hash table. */
11184 *slot = cu->line_header;
4c8aa72d 11185 cu->line_header_die_owner = NULL;
527f3840
JK
11186 }
11187 else
11188 {
11189 /* We cannot free any current entry in (*slot) as that struct line_header
11190 may be already used by multiple CUs. Create only temporary decoded
11191 line_header for this CU - it may happen at most once for each line
11192 number information unit. And if we're not using line_header_hash
11193 then this is what we want as well. */
11194 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11195 }
11196 decode_mapping = (die->tag != DW_TAG_partial_unit);
11197 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11198 decode_mapping);
fff8551c 11199
2ab95328
TT
11200}
11201
95554aad 11202/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11203
c906108c 11204static void
e7c27a73 11205read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11206{
518817b3
SM
11207 struct dwarf2_per_objfile *dwarf2_per_objfile
11208 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11209 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11210 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11211 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11212 CORE_ADDR highpc = ((CORE_ADDR) 0);
11213 struct attribute *attr;
c906108c 11214 struct die_info *child_die;
e142c38c 11215 CORE_ADDR baseaddr;
6e70227d 11216
380618d6 11217 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11218 baseaddr = objfile->text_section_offset ();
c906108c 11219
fae299cd 11220 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11221
11222 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11223 from finish_block. */
2acceee2 11224 if (lowpc == ((CORE_ADDR) -1))
c906108c 11225 lowpc = highpc;
3e29f34a 11226 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11227
d721ba37 11228 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11229
f4b8a18d
KW
11230 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11231 standardised yet. As a workaround for the language detection we fall
11232 back to the DW_AT_producer string. */
11233 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11234 cu->language = language_opencl;
11235
3019eac3
DE
11236 /* Similar hack for Go. */
11237 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11238 set_cu_language (DW_LANG_Go, cu);
11239
c24bdb02 11240 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11241
11242 /* Decode line number information if present. We do this before
11243 processing child DIEs, so that the line header table is available
11244 for DW_AT_decl_file. */
d721ba37 11245 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11246
11247 /* Process all dies in compilation unit. */
11248 if (die->child != NULL)
11249 {
11250 child_die = die->child;
11251 while (child_die && child_die->tag)
11252 {
11253 process_die (child_die, cu);
11254 child_die = sibling_die (child_die);
11255 }
11256 }
11257
11258 /* Decode macro information, if present. Dwarf 2 macro information
11259 refers to information in the line number info statement program
11260 header, so we can only read it if we've read the header
11261 successfully. */
0af92d60
JK
11262 attr = dwarf2_attr (die, DW_AT_macros, cu);
11263 if (attr == NULL)
11264 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11265 if (attr && cu->line_header)
11266 {
11267 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11268 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11269
43f3e411 11270 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11271 }
11272 else
11273 {
11274 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11275 if (attr && cu->line_header)
11276 {
11277 unsigned int macro_offset = DW_UNSND (attr);
11278
43f3e411 11279 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11280 }
11281 }
3019eac3
DE
11282}
11283
c24bdb02
KS
11284void
11285dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11286{
f4dc4d17
DE
11287 struct type_unit_group *tu_group;
11288 int first_time;
3019eac3 11289 struct attribute *attr;
9c541725 11290 unsigned int i;
0186c6a7 11291 struct signatured_type *sig_type;
3019eac3 11292
f4dc4d17 11293 gdb_assert (per_cu->is_debug_types);
0186c6a7 11294 sig_type = (struct signatured_type *) per_cu;
3019eac3 11295
c24bdb02 11296 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11297
f4dc4d17 11298 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11299 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11300 if (sig_type->type_unit_group == NULL)
c24bdb02 11301 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11302 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11303
11304 /* If we've already processed this stmt_list there's no real need to
11305 do it again, we could fake it and just recreate the part we need
11306 (file name,index -> symtab mapping). If data shows this optimization
11307 is useful we can do it then. */
43f3e411 11308 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11309
11310 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11311 debug info. */
fff8551c 11312 line_header_up lh;
f4dc4d17 11313 if (attr != NULL)
3019eac3 11314 {
9c541725 11315 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11316 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11317 }
11318 if (lh == NULL)
11319 {
11320 if (first_time)
c24bdb02 11321 start_symtab ("", NULL, 0);
f4dc4d17
DE
11322 else
11323 {
11324 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11325 gdb_assert (m_builder == nullptr);
804d2729 11326 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11327 m_builder.reset (new struct buildsym_compunit
11328 (COMPUNIT_OBJFILE (cust), "",
11329 COMPUNIT_DIRNAME (cust),
11330 compunit_language (cust),
11331 0, cust));
f4dc4d17 11332 }
f4dc4d17 11333 return;
3019eac3
DE
11334 }
11335
c24bdb02
KS
11336 line_header = lh.release ();
11337 line_header_die_owner = die;
3019eac3 11338
f4dc4d17
DE
11339 if (first_time)
11340 {
c24bdb02 11341 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11342
1fd60fc0
DE
11343 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11344 still initializing it, and our caller (a few levels up)
11345 process_full_type_unit still needs to know if this is the first
11346 time. */
11347
7ba99d21 11348 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11349 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11350 line_header->file_names_size ());
3019eac3 11351
7ba99d21
AT
11352 auto &file_names = line_header->file_names ();
11353 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11354 {
7ba99d21 11355 file_entry &fe = file_names[i];
c24bdb02
KS
11356 dwarf2_start_subfile (this, fe.name,
11357 fe.include_dir (line_header));
11358 buildsym_compunit *b = get_builder ();
11359 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11360 {
4c8aa72d
PA
11361 /* NOTE: start_subfile will recognize when it's been
11362 passed a file it has already seen. So we can't
11363 assume there's a simple mapping from
11364 cu->line_header->file_names to subfiles, plus
11365 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11366 b->get_current_subfile ()->symtab
11367 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11368 }
11369
c24bdb02 11370 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11371 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11372 }
11373 }
11374 else
3019eac3 11375 {
c24bdb02 11376 gdb_assert (m_builder == nullptr);
804d2729 11377 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11378 m_builder.reset (new struct buildsym_compunit
11379 (COMPUNIT_OBJFILE (cust), "",
11380 COMPUNIT_DIRNAME (cust),
11381 compunit_language (cust),
11382 0, cust));
f4dc4d17 11383
7ba99d21
AT
11384 auto &file_names = line_header->file_names ();
11385 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11386 {
7ba99d21 11387 file_entry &fe = file_names[i];
4c8aa72d 11388 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11389 }
3019eac3
DE
11390 }
11391
f4dc4d17
DE
11392 /* The main symtab is allocated last. Type units don't have DW_AT_name
11393 so they don't have a "real" (so to speak) symtab anyway.
11394 There is later code that will assign the main symtab to all symbols
11395 that don't have one. We need to handle the case of a symbol with a
11396 missing symtab (DW_AT_decl_file) anyway. */
11397}
3019eac3 11398
f4dc4d17
DE
11399/* Process DW_TAG_type_unit.
11400 For TUs we want to skip the first top level sibling if it's not the
11401 actual type being defined by this TU. In this case the first top
11402 level sibling is there to provide context only. */
3019eac3 11403
f4dc4d17
DE
11404static void
11405read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11406{
11407 struct die_info *child_die;
3019eac3 11408
f4dc4d17
DE
11409 prepare_one_comp_unit (cu, die, language_minimal);
11410
11411 /* Initialize (or reinitialize) the machinery for building symtabs.
11412 We do this before processing child DIEs, so that the line header table
11413 is available for DW_AT_decl_file. */
c24bdb02 11414 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11415
11416 if (die->child != NULL)
11417 {
11418 child_die = die->child;
11419 while (child_die && child_die->tag)
11420 {
11421 process_die (child_die, cu);
11422 child_die = sibling_die (child_die);
11423 }
11424 }
3019eac3
DE
11425}
11426\f
80626a55
DE
11427/* DWO/DWP files.
11428
11429 http://gcc.gnu.org/wiki/DebugFission
11430 http://gcc.gnu.org/wiki/DebugFissionDWP
11431
11432 To simplify handling of both DWO files ("object" files with the DWARF info)
11433 and DWP files (a file with the DWOs packaged up into one file), we treat
11434 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11435
11436static hashval_t
11437hash_dwo_file (const void *item)
11438{
9a3c8263 11439 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11440 hashval_t hash;
3019eac3 11441
a2ce51a0
DE
11442 hash = htab_hash_string (dwo_file->dwo_name);
11443 if (dwo_file->comp_dir != NULL)
11444 hash += htab_hash_string (dwo_file->comp_dir);
11445 return hash;
3019eac3
DE
11446}
11447
11448static int
11449eq_dwo_file (const void *item_lhs, const void *item_rhs)
11450{
9a3c8263
SM
11451 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11452 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11453
a2ce51a0
DE
11454 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11455 return 0;
11456 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11457 return lhs->comp_dir == rhs->comp_dir;
11458 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11459}
11460
11461/* Allocate a hash table for DWO files. */
11462
51ac9db5 11463static htab_up
ed2dc618 11464allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11465{
51ac9db5
SM
11466 auto delete_dwo_file = [] (void *item)
11467 {
11468 struct dwo_file *dwo_file = (struct dwo_file *) item;
11469
11470 delete dwo_file;
11471 };
11472
11473 return htab_up (htab_create_alloc_ex (41,
11474 hash_dwo_file,
11475 eq_dwo_file,
11476 delete_dwo_file,
11477 &objfile->objfile_obstack,
11478 hashtab_obstack_allocate,
11479 dummy_obstack_deallocate));
3019eac3
DE
11480}
11481
80626a55
DE
11482/* Lookup DWO file DWO_NAME. */
11483
11484static void **
ed2dc618
SM
11485lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11486 const char *dwo_name,
11487 const char *comp_dir)
80626a55
DE
11488{
11489 struct dwo_file find_entry;
11490 void **slot;
11491
11492 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11493 dwarf2_per_objfile->dwo_files
11494 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11495
0ac5b59e
DE
11496 find_entry.dwo_name = dwo_name;
11497 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11498 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11499 INSERT);
80626a55
DE
11500
11501 return slot;
11502}
11503
3019eac3
DE
11504static hashval_t
11505hash_dwo_unit (const void *item)
11506{
9a3c8263 11507 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11508
11509 /* This drops the top 32 bits of the id, but is ok for a hash. */
11510 return dwo_unit->signature;
11511}
11512
11513static int
11514eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11515{
9a3c8263
SM
11516 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11517 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11518
11519 /* The signature is assumed to be unique within the DWO file.
11520 So while object file CU dwo_id's always have the value zero,
11521 that's OK, assuming each object file DWO file has only one CU,
11522 and that's the rule for now. */
11523 return lhs->signature == rhs->signature;
11524}
11525
11526/* Allocate a hash table for DWO CUs,TUs.
11527 There is one of these tables for each of CUs,TUs for each DWO file. */
11528
11529static htab_t
11530allocate_dwo_unit_table (struct objfile *objfile)
11531{
11532 /* Start out with a pretty small number.
11533 Generally DWO files contain only one CU and maybe some TUs. */
11534 return htab_create_alloc_ex (3,
11535 hash_dwo_unit,
11536 eq_dwo_unit,
11537 NULL,
11538 &objfile->objfile_obstack,
11539 hashtab_obstack_allocate,
11540 dummy_obstack_deallocate);
11541}
11542
19c3d4c9 11543/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11544
11545static void
19c3d4c9
DE
11546create_dwo_cu_reader (const struct die_reader_specs *reader,
11547 const gdb_byte *info_ptr,
11548 struct die_info *comp_unit_die,
11549 int has_children,
c0ab21c2
TT
11550 struct dwo_file *dwo_file,
11551 struct dwo_unit *dwo_unit)
3019eac3
DE
11552{
11553 struct dwarf2_cu *cu = reader->cu;
9c541725 11554 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11555 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11556
a084a2a6
AT
11557 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11558 if (!signature.has_value ())
3019eac3 11559 {
b98664d3 11560 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11561 " its dwo_id [in module %s]"),
9d8780f0 11562 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11563 return;
11564 }
11565
3019eac3 11566 dwo_unit->dwo_file = dwo_file;
a084a2a6 11567 dwo_unit->signature = *signature;
8a0459fd 11568 dwo_unit->section = section;
9c541725 11569 dwo_unit->sect_off = sect_off;
3019eac3
DE
11570 dwo_unit->length = cu->per_cu->length;
11571
b4f54984 11572 if (dwarf_read_debug)
9d8780f0
SM
11573 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11574 sect_offset_str (sect_off),
9c541725 11575 hex_string (dwo_unit->signature));
3019eac3
DE
11576}
11577
33c5cd75 11578/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11579 Note: This function processes DWO files only, not DWP files. */
3019eac3 11580
33c5cd75 11581static void
ed2dc618 11582create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
11583 dwarf2_cu *cu, struct dwo_file &dwo_file,
11584 dwarf2_section_info &section, htab_t &cus_htab)
3019eac3
DE
11585{
11586 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11587 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11588
96b79293 11589 section.read (objfile);
33c5cd75 11590 info_ptr = section.buffer;
3019eac3
DE
11591
11592 if (info_ptr == NULL)
33c5cd75 11593 return;
3019eac3 11594
b4f54984 11595 if (dwarf_read_debug)
19c3d4c9
DE
11596 {
11597 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11598 section.get_name (),
11599 section.get_file_name ());
19c3d4c9 11600 }
3019eac3 11601
33c5cd75 11602 end_ptr = info_ptr + section.size;
3019eac3
DE
11603 while (info_ptr < end_ptr)
11604 {
11605 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11606 struct dwo_unit read_unit {};
33c5cd75
DB
11607 struct dwo_unit *dwo_unit;
11608 void **slot;
11609 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11610
11611 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11612 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11613 per_cu.is_debug_types = 0;
33c5cd75
DB
11614 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11615 per_cu.section = &section;
11616
c0ab21c2
TT
11617 cutu_reader reader (&per_cu, cu, &dwo_file);
11618 if (!reader.dummy_p)
11619 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11620 reader.has_children, &dwo_file, &read_unit);
33c5cd75
DB
11621 info_ptr += per_cu.length;
11622
11623 // If the unit could not be parsed, skip it.
c0ab21c2 11624 if (read_unit.dwo_file == NULL)
33c5cd75 11625 continue;
3019eac3 11626
33c5cd75
DB
11627 if (cus_htab == NULL)
11628 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11629
33c5cd75 11630 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11631 *dwo_unit = read_unit;
33c5cd75
DB
11632 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11633 gdb_assert (slot != NULL);
11634 if (*slot != NULL)
19c3d4c9 11635 {
33c5cd75
DB
11636 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11637 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11638
b98664d3 11639 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11640 " the entry at offset %s, signature %s"),
11641 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11642 hex_string (dwo_unit->signature));
19c3d4c9 11643 }
33c5cd75 11644 *slot = (void *)dwo_unit;
3019eac3 11645 }
3019eac3
DE
11646}
11647
80626a55
DE
11648/* DWP file .debug_{cu,tu}_index section format:
11649 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11650
d2415c6c
DE
11651 DWP Version 1:
11652
80626a55
DE
11653 Both index sections have the same format, and serve to map a 64-bit
11654 signature to a set of section numbers. Each section begins with a header,
11655 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11656 indexes, and a pool of 32-bit section numbers. The index sections will be
11657 aligned at 8-byte boundaries in the file.
11658
d2415c6c
DE
11659 The index section header consists of:
11660
11661 V, 32 bit version number
11662 -, 32 bits unused
11663 N, 32 bit number of compilation units or type units in the index
11664 M, 32 bit number of slots in the hash table
80626a55 11665
d2415c6c 11666 Numbers are recorded using the byte order of the application binary.
80626a55 11667
d2415c6c
DE
11668 The hash table begins at offset 16 in the section, and consists of an array
11669 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11670 order of the application binary). Unused slots in the hash table are 0.
11671 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11672
d2415c6c
DE
11673 The parallel table begins immediately after the hash table
11674 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11675 array of 32-bit indexes (using the byte order of the application binary),
11676 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11677 table contains a 32-bit index into the pool of section numbers. For unused
11678 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11679
73869dc2
DE
11680 The pool of section numbers begins immediately following the hash table
11681 (at offset 16 + 12 * M from the beginning of the section). The pool of
11682 section numbers consists of an array of 32-bit words (using the byte order
11683 of the application binary). Each item in the array is indexed starting
11684 from 0. The hash table entry provides the index of the first section
11685 number in the set. Additional section numbers in the set follow, and the
11686 set is terminated by a 0 entry (section number 0 is not used in ELF).
11687
11688 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11689 section must be the first entry in the set, and the .debug_abbrev.dwo must
11690 be the second entry. Other members of the set may follow in any order.
11691
11692 ---
11693
11694 DWP Version 2:
11695
11696 DWP Version 2 combines all the .debug_info, etc. sections into one,
11697 and the entries in the index tables are now offsets into these sections.
11698 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11699 section.
11700
11701 Index Section Contents:
11702 Header
11703 Hash Table of Signatures dwp_hash_table.hash_table
11704 Parallel Table of Indices dwp_hash_table.unit_table
11705 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11706 Table of Section Sizes dwp_hash_table.v2.sizes
11707
11708 The index section header consists of:
11709
11710 V, 32 bit version number
11711 L, 32 bit number of columns in the table of section offsets
11712 N, 32 bit number of compilation units or type units in the index
11713 M, 32 bit number of slots in the hash table
11714
11715 Numbers are recorded using the byte order of the application binary.
11716
11717 The hash table has the same format as version 1.
11718 The parallel table of indices has the same format as version 1,
11719 except that the entries are origin-1 indices into the table of sections
11720 offsets and the table of section sizes.
11721
11722 The table of offsets begins immediately following the parallel table
11723 (at offset 16 + 12 * M from the beginning of the section). The table is
11724 a two-dimensional array of 32-bit words (using the byte order of the
11725 application binary), with L columns and N+1 rows, in row-major order.
11726 Each row in the array is indexed starting from 0. The first row provides
11727 a key to the remaining rows: each column in this row provides an identifier
11728 for a debug section, and the offsets in the same column of subsequent rows
11729 refer to that section. The section identifiers are:
11730
11731 DW_SECT_INFO 1 .debug_info.dwo
11732 DW_SECT_TYPES 2 .debug_types.dwo
11733 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11734 DW_SECT_LINE 4 .debug_line.dwo
11735 DW_SECT_LOC 5 .debug_loc.dwo
11736 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11737 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11738 DW_SECT_MACRO 8 .debug_macro.dwo
11739
11740 The offsets provided by the CU and TU index sections are the base offsets
11741 for the contributions made by each CU or TU to the corresponding section
11742 in the package file. Each CU and TU header contains an abbrev_offset
11743 field, used to find the abbreviations table for that CU or TU within the
11744 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11745 be interpreted as relative to the base offset given in the index section.
11746 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11747 should be interpreted as relative to the base offset for .debug_line.dwo,
11748 and offsets into other debug sections obtained from DWARF attributes should
11749 also be interpreted as relative to the corresponding base offset.
11750
11751 The table of sizes begins immediately following the table of offsets.
11752 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11753 with L columns and N rows, in row-major order. Each row in the array is
11754 indexed starting from 1 (row 0 is shared by the two tables).
11755
11756 ---
11757
11758 Hash table lookup is handled the same in version 1 and 2:
11759
11760 We assume that N and M will not exceed 2^32 - 1.
11761 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11762
d2415c6c
DE
11763 Given a 64-bit compilation unit signature or a type signature S, an entry
11764 in the hash table is located as follows:
80626a55 11765
d2415c6c
DE
11766 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11767 the low-order k bits all set to 1.
80626a55 11768
d2415c6c 11769 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11770
d2415c6c
DE
11771 3) If the hash table entry at index H matches the signature, use that
11772 entry. If the hash table entry at index H is unused (all zeroes),
11773 terminate the search: the signature is not present in the table.
80626a55 11774
d2415c6c 11775 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11776
d2415c6c 11777 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11778 to stop at an unused slot or find the match. */
80626a55
DE
11779
11780/* Create a hash table to map DWO IDs to their CU/TU entry in
11781 .debug_{info,types}.dwo in DWP_FILE.
11782 Returns NULL if there isn't one.
11783 Note: This function processes DWP files only, not DWO files. */
11784
11785static struct dwp_hash_table *
ed2dc618
SM
11786create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11787 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11788{
11789 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11790 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11791 const gdb_byte *index_ptr, *index_end;
80626a55 11792 struct dwarf2_section_info *index;
73869dc2 11793 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11794 struct dwp_hash_table *htab;
11795
11796 if (is_debug_types)
11797 index = &dwp_file->sections.tu_index;
11798 else
11799 index = &dwp_file->sections.cu_index;
11800
96b79293 11801 if (index->empty ())
80626a55 11802 return NULL;
96b79293 11803 index->read (objfile);
80626a55
DE
11804
11805 index_ptr = index->buffer;
11806 index_end = index_ptr + index->size;
11807
11808 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11809 index_ptr += 4;
11810 if (version == 2)
11811 nr_columns = read_4_bytes (dbfd, index_ptr);
11812 else
11813 nr_columns = 0;
11814 index_ptr += 4;
80626a55
DE
11815 nr_units = read_4_bytes (dbfd, index_ptr);
11816 index_ptr += 4;
11817 nr_slots = read_4_bytes (dbfd, index_ptr);
11818 index_ptr += 4;
11819
73869dc2 11820 if (version != 1 && version != 2)
80626a55 11821 {
21aa081e 11822 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11823 " [in module %s]"),
21aa081e 11824 pulongest (version), dwp_file->name);
80626a55
DE
11825 }
11826 if (nr_slots != (nr_slots & -nr_slots))
11827 {
21aa081e 11828 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11829 " is not power of 2 [in module %s]"),
21aa081e 11830 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11831 }
11832
11833 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11834 htab->version = version;
11835 htab->nr_columns = nr_columns;
80626a55
DE
11836 htab->nr_units = nr_units;
11837 htab->nr_slots = nr_slots;
11838 htab->hash_table = index_ptr;
11839 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11840
11841 /* Exit early if the table is empty. */
11842 if (nr_slots == 0 || nr_units == 0
11843 || (version == 2 && nr_columns == 0))
11844 {
11845 /* All must be zero. */
11846 if (nr_slots != 0 || nr_units != 0
11847 || (version == 2 && nr_columns != 0))
11848 {
b98664d3 11849 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11850 " all zero [in modules %s]"),
11851 dwp_file->name);
11852 }
11853 return htab;
11854 }
11855
11856 if (version == 1)
11857 {
11858 htab->section_pool.v1.indices =
11859 htab->unit_table + sizeof (uint32_t) * nr_slots;
11860 /* It's harder to decide whether the section is too small in v1.
11861 V1 is deprecated anyway so we punt. */
11862 }
11863 else
11864 {
11865 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11866 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11867 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11868 /* Reverse map for error checking. */
11869 int ids_seen[DW_SECT_MAX + 1];
11870 int i;
11871
11872 if (nr_columns < 2)
11873 {
11874 error (_("Dwarf Error: bad DWP hash table, too few columns"
11875 " in section table [in module %s]"),
11876 dwp_file->name);
11877 }
11878 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11879 {
11880 error (_("Dwarf Error: bad DWP hash table, too many columns"
11881 " in section table [in module %s]"),
11882 dwp_file->name);
11883 }
04fd5eed
GB
11884 memset (ids, 255, sizeof_ids);
11885 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11886 for (i = 0; i < nr_columns; ++i)
11887 {
11888 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11889
11890 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11891 {
11892 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11893 " in section table [in module %s]"),
11894 id, dwp_file->name);
11895 }
11896 if (ids_seen[id] != -1)
11897 {
11898 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11899 " id %d in section table [in module %s]"),
11900 id, dwp_file->name);
11901 }
11902 ids_seen[id] = i;
11903 ids[i] = id;
11904 }
11905 /* Must have exactly one info or types section. */
11906 if (((ids_seen[DW_SECT_INFO] != -1)
11907 + (ids_seen[DW_SECT_TYPES] != -1))
11908 != 1)
11909 {
11910 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11911 " DWO info/types section [in module %s]"),
11912 dwp_file->name);
11913 }
11914 /* Must have an abbrev section. */
11915 if (ids_seen[DW_SECT_ABBREV] == -1)
11916 {
11917 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11918 " section [in module %s]"),
11919 dwp_file->name);
11920 }
11921 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11922 htab->section_pool.v2.sizes =
11923 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11924 * nr_units * nr_columns);
11925 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11926 * nr_units * nr_columns))
11927 > index_end)
11928 {
11929 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11930 " [in module %s]"),
11931 dwp_file->name);
11932 }
11933 }
80626a55
DE
11934
11935 return htab;
11936}
11937
11938/* Update SECTIONS with the data from SECTP.
11939
11940 This function is like the other "locate" section routines that are
11941 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11942 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11943
11944 The result is non-zero for success, or zero if an error was found. */
11945
11946static int
73869dc2
DE
11947locate_v1_virtual_dwo_sections (asection *sectp,
11948 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11949{
11950 const struct dwop_section_names *names = &dwop_section_names;
11951
11952 if (section_is_p (sectp->name, &names->abbrev_dwo))
11953 {
11954 /* There can be only one. */
049412e3 11955 if (sections->abbrev.s.section != NULL)
80626a55 11956 return 0;
049412e3 11957 sections->abbrev.s.section = sectp;
fd361982 11958 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11959 }
11960 else if (section_is_p (sectp->name, &names->info_dwo)
11961 || section_is_p (sectp->name, &names->types_dwo))
11962 {
11963 /* There can be only one. */
049412e3 11964 if (sections->info_or_types.s.section != NULL)
80626a55 11965 return 0;
049412e3 11966 sections->info_or_types.s.section = sectp;
fd361982 11967 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11968 }
11969 else if (section_is_p (sectp->name, &names->line_dwo))
11970 {
11971 /* There can be only one. */
049412e3 11972 if (sections->line.s.section != NULL)
80626a55 11973 return 0;
049412e3 11974 sections->line.s.section = sectp;
fd361982 11975 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11976 }
11977 else if (section_is_p (sectp->name, &names->loc_dwo))
11978 {
11979 /* There can be only one. */
049412e3 11980 if (sections->loc.s.section != NULL)
80626a55 11981 return 0;
049412e3 11982 sections->loc.s.section = sectp;
fd361982 11983 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11984 }
11985 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11986 {
11987 /* There can be only one. */
049412e3 11988 if (sections->macinfo.s.section != NULL)
80626a55 11989 return 0;
049412e3 11990 sections->macinfo.s.section = sectp;
fd361982 11991 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11992 }
11993 else if (section_is_p (sectp->name, &names->macro_dwo))
11994 {
11995 /* There can be only one. */
049412e3 11996 if (sections->macro.s.section != NULL)
80626a55 11997 return 0;
049412e3 11998 sections->macro.s.section = sectp;
fd361982 11999 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12000 }
12001 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12002 {
12003 /* There can be only one. */
049412e3 12004 if (sections->str_offsets.s.section != NULL)
80626a55 12005 return 0;
049412e3 12006 sections->str_offsets.s.section = sectp;
fd361982 12007 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12008 }
12009 else
12010 {
12011 /* No other kind of section is valid. */
12012 return 0;
12013 }
12014
12015 return 1;
12016}
12017
73869dc2
DE
12018/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12019 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12020 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12021 This is for DWP version 1 files. */
80626a55
DE
12022
12023static struct dwo_unit *
ed2dc618
SM
12024create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12025 struct dwp_file *dwp_file,
73869dc2
DE
12026 uint32_t unit_index,
12027 const char *comp_dir,
12028 ULONGEST signature, int is_debug_types)
80626a55
DE
12029{
12030 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12031 const struct dwp_hash_table *dwp_htab =
12032 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12033 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12034 const char *kind = is_debug_types ? "TU" : "CU";
12035 struct dwo_file *dwo_file;
12036 struct dwo_unit *dwo_unit;
73869dc2 12037 struct virtual_v1_dwo_sections sections;
80626a55 12038 void **dwo_file_slot;
80626a55
DE
12039 int i;
12040
73869dc2
DE
12041 gdb_assert (dwp_file->version == 1);
12042
b4f54984 12043 if (dwarf_read_debug)
80626a55 12044 {
73869dc2 12045 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12046 kind,
73869dc2 12047 pulongest (unit_index), hex_string (signature),
80626a55
DE
12048 dwp_file->name);
12049 }
12050
19ac8c2e 12051 /* Fetch the sections of this DWO unit.
80626a55
DE
12052 Put a limit on the number of sections we look for so that bad data
12053 doesn't cause us to loop forever. */
12054
73869dc2 12055#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12056 (1 /* .debug_info or .debug_types */ \
12057 + 1 /* .debug_abbrev */ \
12058 + 1 /* .debug_line */ \
12059 + 1 /* .debug_loc */ \
12060 + 1 /* .debug_str_offsets */ \
19ac8c2e 12061 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12062 + 1 /* trailing zero */)
12063
12064 memset (&sections, 0, sizeof (sections));
80626a55 12065
73869dc2 12066 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12067 {
12068 asection *sectp;
12069 uint32_t section_nr =
12070 read_4_bytes (dbfd,
73869dc2
DE
12071 dwp_htab->section_pool.v1.indices
12072 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12073
12074 if (section_nr == 0)
12075 break;
12076 if (section_nr >= dwp_file->num_sections)
12077 {
12078 error (_("Dwarf Error: bad DWP hash table, section number too large"
12079 " [in module %s]"),
12080 dwp_file->name);
12081 }
12082
12083 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12084 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12085 {
12086 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12087 " [in module %s]"),
12088 dwp_file->name);
12089 }
12090 }
12091
12092 if (i < 2
96b79293
TT
12093 || sections.info_or_types.empty ()
12094 || sections.abbrev.empty ())
80626a55
DE
12095 {
12096 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12097 " [in module %s]"),
12098 dwp_file->name);
12099 }
73869dc2 12100 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12101 {
12102 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12103 " [in module %s]"),
12104 dwp_file->name);
12105 }
12106
12107 /* It's easier for the rest of the code if we fake a struct dwo_file and
12108 have dwo_unit "live" in that. At least for now.
12109
12110 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12111 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12112 file, we can combine them back into a virtual DWO file to save space
12113 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12114 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12115
791afaa2
TT
12116 std::string virtual_dwo_name =
12117 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12118 sections.abbrev.get_id (),
12119 sections.line.get_id (),
12120 sections.loc.get_id (),
12121 sections.str_offsets.get_id ());
80626a55 12122 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12123 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12124 virtual_dwo_name.c_str (),
12125 comp_dir);
80626a55
DE
12126 /* Create one if necessary. */
12127 if (*dwo_file_slot == NULL)
12128 {
b4f54984 12129 if (dwarf_read_debug)
80626a55
DE
12130 {
12131 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12132 virtual_dwo_name.c_str ());
80626a55 12133 }
51ac9db5 12134 dwo_file = new struct dwo_file;
efba19b0
TT
12135 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12136 virtual_dwo_name);
0ac5b59e 12137 dwo_file->comp_dir = comp_dir;
80626a55
DE
12138 dwo_file->sections.abbrev = sections.abbrev;
12139 dwo_file->sections.line = sections.line;
12140 dwo_file->sections.loc = sections.loc;
12141 dwo_file->sections.macinfo = sections.macinfo;
12142 dwo_file->sections.macro = sections.macro;
12143 dwo_file->sections.str_offsets = sections.str_offsets;
12144 /* The "str" section is global to the entire DWP file. */
12145 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12146 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12147 there's no need to record it in dwo_file.
12148 Also, we can't simply record type sections in dwo_file because
12149 we record a pointer into the vector in dwo_unit. As we collect more
12150 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12151 for it, invalidating all copies of pointers into the previous
12152 contents. */
80626a55
DE
12153 *dwo_file_slot = dwo_file;
12154 }
12155 else
12156 {
b4f54984 12157 if (dwarf_read_debug)
80626a55
DE
12158 {
12159 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12160 virtual_dwo_name.c_str ());
80626a55 12161 }
9a3c8263 12162 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12163 }
80626a55
DE
12164
12165 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12166 dwo_unit->dwo_file = dwo_file;
12167 dwo_unit->signature = signature;
8d749320
SM
12168 dwo_unit->section =
12169 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12170 *dwo_unit->section = sections.info_or_types;
57d63ce2 12171 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12172
12173 return dwo_unit;
12174}
12175
73869dc2
DE
12176/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12177 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12178 piece within that section used by a TU/CU, return a virtual section
12179 of just that piece. */
12180
12181static struct dwarf2_section_info
ed2dc618
SM
12182create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12183 struct dwarf2_section_info *section,
73869dc2
DE
12184 bfd_size_type offset, bfd_size_type size)
12185{
12186 struct dwarf2_section_info result;
12187 asection *sectp;
12188
12189 gdb_assert (section != NULL);
12190 gdb_assert (!section->is_virtual);
12191
12192 memset (&result, 0, sizeof (result));
12193 result.s.containing_section = section;
dc4ccb6f 12194 result.is_virtual = true;
73869dc2
DE
12195
12196 if (size == 0)
12197 return result;
12198
96b79293 12199 sectp = section->get_bfd_section ();
73869dc2
DE
12200
12201 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12202 bounds of the real section. This is a pretty-rare event, so just
12203 flag an error (easier) instead of a warning and trying to cope. */
12204 if (sectp == NULL
fd361982 12205 || offset + size > bfd_section_size (sectp))
73869dc2 12206 {
73869dc2
DE
12207 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12208 " in section %s [in module %s]"),
fd361982 12209 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12210 objfile_name (dwarf2_per_objfile->objfile));
12211 }
12212
12213 result.virtual_offset = offset;
12214 result.size = size;
12215 return result;
12216}
12217
12218/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12219 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12220 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12221 This is for DWP version 2 files. */
12222
12223static struct dwo_unit *
ed2dc618
SM
12224create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12225 struct dwp_file *dwp_file,
73869dc2
DE
12226 uint32_t unit_index,
12227 const char *comp_dir,
12228 ULONGEST signature, int is_debug_types)
12229{
12230 struct objfile *objfile = dwarf2_per_objfile->objfile;
12231 const struct dwp_hash_table *dwp_htab =
12232 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12233 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12234 const char *kind = is_debug_types ? "TU" : "CU";
12235 struct dwo_file *dwo_file;
12236 struct dwo_unit *dwo_unit;
12237 struct virtual_v2_dwo_sections sections;
12238 void **dwo_file_slot;
73869dc2
DE
12239 int i;
12240
12241 gdb_assert (dwp_file->version == 2);
12242
b4f54984 12243 if (dwarf_read_debug)
73869dc2
DE
12244 {
12245 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12246 kind,
12247 pulongest (unit_index), hex_string (signature),
12248 dwp_file->name);
12249 }
12250
12251 /* Fetch the section offsets of this DWO unit. */
12252
12253 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12254
12255 for (i = 0; i < dwp_htab->nr_columns; ++i)
12256 {
12257 uint32_t offset = read_4_bytes (dbfd,
12258 dwp_htab->section_pool.v2.offsets
12259 + (((unit_index - 1) * dwp_htab->nr_columns
12260 + i)
12261 * sizeof (uint32_t)));
12262 uint32_t size = read_4_bytes (dbfd,
12263 dwp_htab->section_pool.v2.sizes
12264 + (((unit_index - 1) * dwp_htab->nr_columns
12265 + i)
12266 * sizeof (uint32_t)));
12267
12268 switch (dwp_htab->section_pool.v2.section_ids[i])
12269 {
12270 case DW_SECT_INFO:
12271 case DW_SECT_TYPES:
12272 sections.info_or_types_offset = offset;
12273 sections.info_or_types_size = size;
12274 break;
12275 case DW_SECT_ABBREV:
12276 sections.abbrev_offset = offset;
12277 sections.abbrev_size = size;
12278 break;
12279 case DW_SECT_LINE:
12280 sections.line_offset = offset;
12281 sections.line_size = size;
12282 break;
12283 case DW_SECT_LOC:
12284 sections.loc_offset = offset;
12285 sections.loc_size = size;
12286 break;
12287 case DW_SECT_STR_OFFSETS:
12288 sections.str_offsets_offset = offset;
12289 sections.str_offsets_size = size;
12290 break;
12291 case DW_SECT_MACINFO:
12292 sections.macinfo_offset = offset;
12293 sections.macinfo_size = size;
12294 break;
12295 case DW_SECT_MACRO:
12296 sections.macro_offset = offset;
12297 sections.macro_size = size;
12298 break;
12299 }
12300 }
12301
12302 /* It's easier for the rest of the code if we fake a struct dwo_file and
12303 have dwo_unit "live" in that. At least for now.
12304
12305 The DWP file can be made up of a random collection of CUs and TUs.
12306 However, for each CU + set of TUs that came from the same original DWO
12307 file, we can combine them back into a virtual DWO file to save space
12308 (fewer struct dwo_file objects to allocate). Remember that for really
12309 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12310
791afaa2
TT
12311 std::string virtual_dwo_name =
12312 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12313 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12314 (long) (sections.line_size ? sections.line_offset : 0),
12315 (long) (sections.loc_size ? sections.loc_offset : 0),
12316 (long) (sections.str_offsets_size
12317 ? sections.str_offsets_offset : 0));
73869dc2 12318 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12319 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12320 virtual_dwo_name.c_str (),
12321 comp_dir);
73869dc2
DE
12322 /* Create one if necessary. */
12323 if (*dwo_file_slot == NULL)
12324 {
b4f54984 12325 if (dwarf_read_debug)
73869dc2
DE
12326 {
12327 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12328 virtual_dwo_name.c_str ());
73869dc2 12329 }
51ac9db5 12330 dwo_file = new struct dwo_file;
efba19b0
TT
12331 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12332 virtual_dwo_name);
73869dc2
DE
12333 dwo_file->comp_dir = comp_dir;
12334 dwo_file->sections.abbrev =
ed2dc618 12335 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12336 sections.abbrev_offset, sections.abbrev_size);
12337 dwo_file->sections.line =
ed2dc618 12338 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12339 sections.line_offset, sections.line_size);
12340 dwo_file->sections.loc =
ed2dc618 12341 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12342 sections.loc_offset, sections.loc_size);
12343 dwo_file->sections.macinfo =
ed2dc618 12344 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12345 sections.macinfo_offset, sections.macinfo_size);
12346 dwo_file->sections.macro =
ed2dc618 12347 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12348 sections.macro_offset, sections.macro_size);
12349 dwo_file->sections.str_offsets =
ed2dc618
SM
12350 create_dwp_v2_section (dwarf2_per_objfile,
12351 &dwp_file->sections.str_offsets,
73869dc2
DE
12352 sections.str_offsets_offset,
12353 sections.str_offsets_size);
12354 /* The "str" section is global to the entire DWP file. */
12355 dwo_file->sections.str = dwp_file->sections.str;
12356 /* The info or types section is assigned below to dwo_unit,
12357 there's no need to record it in dwo_file.
12358 Also, we can't simply record type sections in dwo_file because
12359 we record a pointer into the vector in dwo_unit. As we collect more
12360 types we'll grow the vector and eventually have to reallocate space
12361 for it, invalidating all copies of pointers into the previous
12362 contents. */
12363 *dwo_file_slot = dwo_file;
12364 }
12365 else
12366 {
b4f54984 12367 if (dwarf_read_debug)
73869dc2
DE
12368 {
12369 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12370 virtual_dwo_name.c_str ());
73869dc2 12371 }
9a3c8263 12372 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12373 }
73869dc2
DE
12374
12375 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12376 dwo_unit->dwo_file = dwo_file;
12377 dwo_unit->signature = signature;
8d749320
SM
12378 dwo_unit->section =
12379 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12380 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12381 is_debug_types
73869dc2
DE
12382 ? &dwp_file->sections.types
12383 : &dwp_file->sections.info,
12384 sections.info_or_types_offset,
12385 sections.info_or_types_size);
12386 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12387
12388 return dwo_unit;
12389}
12390
57d63ce2
DE
12391/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12392 Returns NULL if the signature isn't found. */
80626a55
DE
12393
12394static struct dwo_unit *
ed2dc618
SM
12395lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12396 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12397 ULONGEST signature, int is_debug_types)
80626a55 12398{
57d63ce2
DE
12399 const struct dwp_hash_table *dwp_htab =
12400 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12401 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12402 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12403 uint32_t hash = signature & mask;
12404 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12405 unsigned int i;
12406 void **slot;
870f88f7 12407 struct dwo_unit find_dwo_cu;
80626a55
DE
12408
12409 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12410 find_dwo_cu.signature = signature;
19ac8c2e
DE
12411 slot = htab_find_slot (is_debug_types
12412 ? dwp_file->loaded_tus
12413 : dwp_file->loaded_cus,
12414 &find_dwo_cu, INSERT);
80626a55
DE
12415
12416 if (*slot != NULL)
9a3c8263 12417 return (struct dwo_unit *) *slot;
80626a55
DE
12418
12419 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12420 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12421 {
12422 ULONGEST signature_in_table;
12423
12424 signature_in_table =
57d63ce2 12425 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12426 if (signature_in_table == signature)
12427 {
57d63ce2
DE
12428 uint32_t unit_index =
12429 read_4_bytes (dbfd,
12430 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12431
73869dc2
DE
12432 if (dwp_file->version == 1)
12433 {
ed2dc618
SM
12434 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12435 dwp_file, unit_index,
73869dc2
DE
12436 comp_dir, signature,
12437 is_debug_types);
12438 }
12439 else
12440 {
ed2dc618
SM
12441 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12442 dwp_file, unit_index,
73869dc2
DE
12443 comp_dir, signature,
12444 is_debug_types);
12445 }
9a3c8263 12446 return (struct dwo_unit *) *slot;
80626a55
DE
12447 }
12448 if (signature_in_table == 0)
12449 return NULL;
12450 hash = (hash + hash2) & mask;
12451 }
12452
12453 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12454 " [in module %s]"),
12455 dwp_file->name);
12456}
12457
ab5088bf 12458/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12459 Open the file specified by FILE_NAME and hand it off to BFD for
12460 preliminary analysis. Return a newly initialized bfd *, which
12461 includes a canonicalized copy of FILE_NAME.
80626a55 12462 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12463 SEARCH_CWD is true if the current directory is to be searched.
12464 It will be searched before debug-file-directory.
13aaf454
DE
12465 If successful, the file is added to the bfd include table of the
12466 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12467 If unable to find/open the file, return NULL.
3019eac3
DE
12468 NOTE: This function is derived from symfile_bfd_open. */
12469
192b62ce 12470static gdb_bfd_ref_ptr
ed2dc618
SM
12471try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12472 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12473{
24b9144d 12474 int desc;
9c02c129
DE
12475 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12476 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12477 to debug_file_directory. */
e0cc99a6 12478 const char *search_path;
9c02c129
DE
12479 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12480
e0cc99a6 12481 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12482 if (search_cwd)
12483 {
12484 if (*debug_file_directory != '\0')
e0cc99a6
TT
12485 {
12486 search_path_holder.reset (concat (".", dirname_separator_string,
12487 debug_file_directory,
12488 (char *) NULL));
12489 search_path = search_path_holder.get ();
12490 }
6ac97d4c 12491 else
e0cc99a6 12492 search_path = ".";
6ac97d4c 12493 }
9c02c129 12494 else
e0cc99a6 12495 search_path = debug_file_directory;
3019eac3 12496
24b9144d 12497 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12498 if (is_dwp)
12499 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12500
12501 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12502 desc = openp (search_path, flags, file_name,
3019eac3
DE
12503 O_RDONLY | O_BINARY, &absolute_name);
12504 if (desc < 0)
12505 return NULL;
12506
e0cc99a6
TT
12507 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12508 gnutarget, desc));
9c02c129
DE
12509 if (sym_bfd == NULL)
12510 return NULL;
192b62ce 12511 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12512
192b62ce
TT
12513 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12514 return NULL;
3019eac3 12515
13aaf454
DE
12516 /* Success. Record the bfd as having been included by the objfile's bfd.
12517 This is important because things like demangled_names_hash lives in the
12518 objfile's per_bfd space and may have references to things like symbol
12519 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12520 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12521
3019eac3
DE
12522 return sym_bfd;
12523}
12524
ab5088bf 12525/* Try to open DWO file FILE_NAME.
3019eac3
DE
12526 COMP_DIR is the DW_AT_comp_dir attribute.
12527 The result is the bfd handle of the file.
12528 If there is a problem finding or opening the file, return NULL.
12529 Upon success, the canonicalized path of the file is stored in the bfd,
12530 same as symfile_bfd_open. */
12531
192b62ce 12532static gdb_bfd_ref_ptr
ed2dc618
SM
12533open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12534 const char *file_name, const char *comp_dir)
3019eac3 12535{
80626a55 12536 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12537 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12538 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12539
12540 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12541
12542 if (comp_dir != NULL)
12543 {
43816ebc
TT
12544 gdb::unique_xmalloc_ptr<char> path_to_try
12545 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12546
12547 /* NOTE: If comp_dir is a relative path, this will also try the
12548 search path, which seems useful. */
ed2dc618 12549 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12550 path_to_try.get (),
ed2dc618 12551 0 /*is_dwp*/,
192b62ce 12552 1 /*search_cwd*/));
3019eac3
DE
12553 if (abfd != NULL)
12554 return abfd;
12555 }
12556
12557 /* That didn't work, try debug-file-directory, which, despite its name,
12558 is a list of paths. */
12559
12560 if (*debug_file_directory == '\0')
12561 return NULL;
12562
ed2dc618
SM
12563 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12564 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12565}
12566
80626a55
DE
12567/* This function is mapped across the sections and remembers the offset and
12568 size of each of the DWO debugging sections we are interested in. */
12569
12570static void
12571dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12572{
9a3c8263 12573 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12574 const struct dwop_section_names *names = &dwop_section_names;
12575
12576 if (section_is_p (sectp->name, &names->abbrev_dwo))
12577 {
049412e3 12578 dwo_sections->abbrev.s.section = sectp;
fd361982 12579 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12580 }
12581 else if (section_is_p (sectp->name, &names->info_dwo))
12582 {
049412e3 12583 dwo_sections->info.s.section = sectp;
fd361982 12584 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12585 }
12586 else if (section_is_p (sectp->name, &names->line_dwo))
12587 {
049412e3 12588 dwo_sections->line.s.section = sectp;
fd361982 12589 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12590 }
12591 else if (section_is_p (sectp->name, &names->loc_dwo))
12592 {
049412e3 12593 dwo_sections->loc.s.section = sectp;
fd361982 12594 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12595 }
12596 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12597 {
049412e3 12598 dwo_sections->macinfo.s.section = sectp;
fd361982 12599 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12600 }
12601 else if (section_is_p (sectp->name, &names->macro_dwo))
12602 {
049412e3 12603 dwo_sections->macro.s.section = sectp;
fd361982 12604 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12605 }
12606 else if (section_is_p (sectp->name, &names->str_dwo))
12607 {
049412e3 12608 dwo_sections->str.s.section = sectp;
fd361982 12609 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12610 }
12611 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12612 {
049412e3 12613 dwo_sections->str_offsets.s.section = sectp;
fd361982 12614 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12615 }
12616 else if (section_is_p (sectp->name, &names->types_dwo))
12617 {
12618 struct dwarf2_section_info type_section;
12619
12620 memset (&type_section, 0, sizeof (type_section));
049412e3 12621 type_section.s.section = sectp;
fd361982 12622 type_section.size = bfd_section_size (sectp);
fd5866f6 12623 dwo_sections->types.push_back (type_section);
80626a55
DE
12624 }
12625}
12626
ab5088bf 12627/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12628 by PER_CU. This is for the non-DWP case.
80626a55 12629 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12630
12631static struct dwo_file *
0ac5b59e
DE
12632open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12633 const char *dwo_name, const char *comp_dir)
3019eac3 12634{
ed2dc618 12635 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12636
fb1eb2f9 12637 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12638 if (dbfd == NULL)
12639 {
b4f54984 12640 if (dwarf_read_debug)
80626a55
DE
12641 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12642 return NULL;
12643 }
263db9a1 12644
51ac9db5 12645 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12646 dwo_file->dwo_name = dwo_name;
12647 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12648 dwo_file->dbfd = std::move (dbfd);
3019eac3 12649
fb1eb2f9 12650 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12651 &dwo_file->sections);
3019eac3 12652
18a8505e
AT
12653 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12654 dwo_file->sections.info, dwo_file->cus);
3019eac3 12655
263db9a1 12656 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12657 dwo_file->sections.types, dwo_file->tus);
3019eac3 12658
b4f54984 12659 if (dwarf_read_debug)
80626a55
DE
12660 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12661
263db9a1 12662 return dwo_file.release ();
3019eac3
DE
12663}
12664
80626a55 12665/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12666 size of each of the DWP debugging sections common to version 1 and 2 that
12667 we are interested in. */
3019eac3 12668
80626a55 12669static void
73869dc2
DE
12670dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12671 void *dwp_file_ptr)
3019eac3 12672{
9a3c8263 12673 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12674 const struct dwop_section_names *names = &dwop_section_names;
12675 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12676
80626a55 12677 /* Record the ELF section number for later lookup: this is what the
73869dc2 12678 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12679 gdb_assert (elf_section_nr < dwp_file->num_sections);
12680 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12681
80626a55
DE
12682 /* Look for specific sections that we need. */
12683 if (section_is_p (sectp->name, &names->str_dwo))
12684 {
049412e3 12685 dwp_file->sections.str.s.section = sectp;
fd361982 12686 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12687 }
12688 else if (section_is_p (sectp->name, &names->cu_index))
12689 {
049412e3 12690 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12691 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12692 }
12693 else if (section_is_p (sectp->name, &names->tu_index))
12694 {
049412e3 12695 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12696 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12697 }
12698}
3019eac3 12699
73869dc2
DE
12700/* This function is mapped across the sections and remembers the offset and
12701 size of each of the DWP version 2 debugging sections that we are interested
12702 in. This is split into a separate function because we don't know if we
12703 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12704
12705static void
12706dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12707{
9a3c8263 12708 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12709 const struct dwop_section_names *names = &dwop_section_names;
12710 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12711
12712 /* Record the ELF section number for later lookup: this is what the
12713 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12714 gdb_assert (elf_section_nr < dwp_file->num_sections);
12715 dwp_file->elf_sections[elf_section_nr] = sectp;
12716
12717 /* Look for specific sections that we need. */
12718 if (section_is_p (sectp->name, &names->abbrev_dwo))
12719 {
049412e3 12720 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12721 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12722 }
12723 else if (section_is_p (sectp->name, &names->info_dwo))
12724 {
049412e3 12725 dwp_file->sections.info.s.section = sectp;
fd361982 12726 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12727 }
12728 else if (section_is_p (sectp->name, &names->line_dwo))
12729 {
049412e3 12730 dwp_file->sections.line.s.section = sectp;
fd361982 12731 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12732 }
12733 else if (section_is_p (sectp->name, &names->loc_dwo))
12734 {
049412e3 12735 dwp_file->sections.loc.s.section = sectp;
fd361982 12736 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12737 }
12738 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12739 {
049412e3 12740 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12741 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12742 }
12743 else if (section_is_p (sectp->name, &names->macro_dwo))
12744 {
049412e3 12745 dwp_file->sections.macro.s.section = sectp;
fd361982 12746 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12747 }
12748 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12749 {
049412e3 12750 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12751 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12752 }
12753 else if (section_is_p (sectp->name, &names->types_dwo))
12754 {
049412e3 12755 dwp_file->sections.types.s.section = sectp;
fd361982 12756 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12757 }
12758}
12759
80626a55 12760/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12761
80626a55
DE
12762static hashval_t
12763hash_dwp_loaded_cutus (const void *item)
12764{
9a3c8263 12765 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12766
80626a55
DE
12767 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12768 return dwo_unit->signature;
3019eac3
DE
12769}
12770
80626a55 12771/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12772
80626a55
DE
12773static int
12774eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12775{
9a3c8263
SM
12776 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12777 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12778
80626a55
DE
12779 return dua->signature == dub->signature;
12780}
3019eac3 12781
80626a55 12782/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12783
80626a55
DE
12784static htab_t
12785allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12786{
12787 return htab_create_alloc_ex (3,
12788 hash_dwp_loaded_cutus,
12789 eq_dwp_loaded_cutus,
12790 NULL,
12791 &objfile->objfile_obstack,
12792 hashtab_obstack_allocate,
12793 dummy_obstack_deallocate);
12794}
3019eac3 12795
ab5088bf
DE
12796/* Try to open DWP file FILE_NAME.
12797 The result is the bfd handle of the file.
12798 If there is a problem finding or opening the file, return NULL.
12799 Upon success, the canonicalized path of the file is stored in the bfd,
12800 same as symfile_bfd_open. */
12801
192b62ce 12802static gdb_bfd_ref_ptr
ed2dc618
SM
12803open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12804 const char *file_name)
ab5088bf 12805{
ed2dc618
SM
12806 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12807 1 /*is_dwp*/,
192b62ce 12808 1 /*search_cwd*/));
6ac97d4c
DE
12809 if (abfd != NULL)
12810 return abfd;
12811
12812 /* Work around upstream bug 15652.
12813 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12814 [Whether that's a "bug" is debatable, but it is getting in our way.]
12815 We have no real idea where the dwp file is, because gdb's realpath-ing
12816 of the executable's path may have discarded the needed info.
12817 [IWBN if the dwp file name was recorded in the executable, akin to
12818 .gnu_debuglink, but that doesn't exist yet.]
12819 Strip the directory from FILE_NAME and search again. */
12820 if (*debug_file_directory != '\0')
12821 {
12822 /* Don't implicitly search the current directory here.
12823 If the user wants to search "." to handle this case,
12824 it must be added to debug-file-directory. */
ed2dc618
SM
12825 return try_open_dwop_file (dwarf2_per_objfile,
12826 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12827 0 /*search_cwd*/);
12828 }
12829
12830 return NULL;
ab5088bf
DE
12831}
12832
80626a55
DE
12833/* Initialize the use of the DWP file for the current objfile.
12834 By convention the name of the DWP file is ${objfile}.dwp.
12835 The result is NULL if it can't be found. */
a766d390 12836
400174b1 12837static std::unique_ptr<struct dwp_file>
ed2dc618 12838open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12839{
12840 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12841
82bf32bc
JK
12842 /* Try to find first .dwp for the binary file before any symbolic links
12843 resolving. */
6c447423
DE
12844
12845 /* If the objfile is a debug file, find the name of the real binary
12846 file and get the name of dwp file from there. */
d721ba37 12847 std::string dwp_name;
6c447423
DE
12848 if (objfile->separate_debug_objfile_backlink != NULL)
12849 {
12850 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12851 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12852
d721ba37 12853 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12854 }
12855 else
d721ba37
PA
12856 dwp_name = objfile->original_name;
12857
12858 dwp_name += ".dwp";
80626a55 12859
ed2dc618 12860 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12861 if (dbfd == NULL
12862 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12863 {
12864 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12865 dwp_name = objfile_name (objfile);
12866 dwp_name += ".dwp";
ed2dc618 12867 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12868 }
12869
80626a55
DE
12870 if (dbfd == NULL)
12871 {
b4f54984 12872 if (dwarf_read_debug)
d721ba37 12873 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12874 return std::unique_ptr<dwp_file> ();
3019eac3 12875 }
400174b1
TT
12876
12877 const char *name = bfd_get_filename (dbfd.get ());
12878 std::unique_ptr<struct dwp_file> dwp_file
12879 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12880
0a0f4c01 12881 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12882 dwp_file->elf_sections =
12883 OBSTACK_CALLOC (&objfile->objfile_obstack,
12884 dwp_file->num_sections, asection *);
12885
400174b1
TT
12886 bfd_map_over_sections (dwp_file->dbfd.get (),
12887 dwarf2_locate_common_dwp_sections,
12888 dwp_file.get ());
80626a55 12889
400174b1
TT
12890 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12891 0);
80626a55 12892
400174b1
TT
12893 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12894 1);
80626a55 12895
73869dc2 12896 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12897 if (dwp_file->cus && dwp_file->tus
12898 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12899 {
12900 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12901 pretty bizarre. We use pulongest here because that's the established
4d65956b 12902 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12903 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12904 " TU version %s [in DWP file %s]"),
12905 pulongest (dwp_file->cus->version),
d721ba37 12906 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12907 }
08302ed2
DE
12908
12909 if (dwp_file->cus)
12910 dwp_file->version = dwp_file->cus->version;
12911 else if (dwp_file->tus)
12912 dwp_file->version = dwp_file->tus->version;
12913 else
12914 dwp_file->version = 2;
73869dc2
DE
12915
12916 if (dwp_file->version == 2)
400174b1
TT
12917 bfd_map_over_sections (dwp_file->dbfd.get (),
12918 dwarf2_locate_v2_dwp_sections,
12919 dwp_file.get ());
73869dc2 12920
19ac8c2e
DE
12921 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12922 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 12923
b4f54984 12924 if (dwarf_read_debug)
80626a55
DE
12925 {
12926 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12927 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12928 " %s CUs, %s TUs\n",
12929 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12930 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12931 }
12932
12933 return dwp_file;
3019eac3 12934}
c906108c 12935
ab5088bf
DE
12936/* Wrapper around open_and_init_dwp_file, only open it once. */
12937
12938static struct dwp_file *
ed2dc618 12939get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12940{
12941 if (! dwarf2_per_objfile->dwp_checked)
12942 {
ed2dc618
SM
12943 dwarf2_per_objfile->dwp_file
12944 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12945 dwarf2_per_objfile->dwp_checked = 1;
12946 }
400174b1 12947 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12948}
12949
80626a55
DE
12950/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12951 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12952 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12953 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12954 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12955
12956 This is called, for example, when wanting to read a variable with a
12957 complex location. Therefore we don't want to do file i/o for every call.
12958 Therefore we don't want to look for a DWO file on every call.
12959 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12960 then we check if we've already seen DWO_NAME, and only THEN do we check
12961 for a DWO file.
12962
1c658ad5 12963 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12964 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12965
3019eac3 12966static struct dwo_unit *
80626a55
DE
12967lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12968 const char *dwo_name, const char *comp_dir,
12969 ULONGEST signature, int is_debug_types)
3019eac3 12970{
ed2dc618 12971 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12972 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12973 const char *kind = is_debug_types ? "TU" : "CU";
12974 void **dwo_file_slot;
3019eac3 12975 struct dwo_file *dwo_file;
80626a55 12976 struct dwp_file *dwp_file;
cb1df416 12977
6a506a2d
DE
12978 /* First see if there's a DWP file.
12979 If we have a DWP file but didn't find the DWO inside it, don't
12980 look for the original DWO file. It makes gdb behave differently
12981 depending on whether one is debugging in the build tree. */
cf2c3c16 12982
ed2dc618 12983 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12984 if (dwp_file != NULL)
cf2c3c16 12985 {
80626a55
DE
12986 const struct dwp_hash_table *dwp_htab =
12987 is_debug_types ? dwp_file->tus : dwp_file->cus;
12988
12989 if (dwp_htab != NULL)
12990 {
12991 struct dwo_unit *dwo_cutu =
ed2dc618 12992 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12993 signature, is_debug_types);
80626a55
DE
12994
12995 if (dwo_cutu != NULL)
12996 {
b4f54984 12997 if (dwarf_read_debug)
80626a55
DE
12998 {
12999 fprintf_unfiltered (gdb_stdlog,
13000 "Virtual DWO %s %s found: @%s\n",
13001 kind, hex_string (signature),
13002 host_address_to_string (dwo_cutu));
13003 }
13004 return dwo_cutu;
13005 }
13006 }
13007 }
6a506a2d 13008 else
80626a55 13009 {
6a506a2d 13010 /* No DWP file, look for the DWO file. */
80626a55 13011
ed2dc618
SM
13012 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13013 dwo_name, comp_dir);
6a506a2d 13014 if (*dwo_file_slot == NULL)
80626a55 13015 {
6a506a2d
DE
13016 /* Read in the file and build a table of the CUs/TUs it contains. */
13017 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13018 }
6a506a2d 13019 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13020 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13021
6a506a2d 13022 if (dwo_file != NULL)
19c3d4c9 13023 {
6a506a2d
DE
13024 struct dwo_unit *dwo_cutu = NULL;
13025
13026 if (is_debug_types && dwo_file->tus)
13027 {
13028 struct dwo_unit find_dwo_cutu;
13029
13030 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13031 find_dwo_cutu.signature = signature;
9a3c8263
SM
13032 dwo_cutu
13033 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13034 }
33c5cd75 13035 else if (!is_debug_types && dwo_file->cus)
80626a55 13036 {
33c5cd75
DB
13037 struct dwo_unit find_dwo_cutu;
13038
13039 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13040 find_dwo_cutu.signature = signature;
13041 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13042 &find_dwo_cutu);
6a506a2d
DE
13043 }
13044
13045 if (dwo_cutu != NULL)
13046 {
b4f54984 13047 if (dwarf_read_debug)
6a506a2d
DE
13048 {
13049 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13050 kind, dwo_name, hex_string (signature),
13051 host_address_to_string (dwo_cutu));
13052 }
13053 return dwo_cutu;
80626a55
DE
13054 }
13055 }
2e276125 13056 }
9cdd5dbd 13057
80626a55
DE
13058 /* We didn't find it. This could mean a dwo_id mismatch, or
13059 someone deleted the DWO/DWP file, or the search path isn't set up
13060 correctly to find the file. */
13061
b4f54984 13062 if (dwarf_read_debug)
80626a55
DE
13063 {
13064 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13065 kind, dwo_name, hex_string (signature));
13066 }
3019eac3 13067
6656a72d
DE
13068 /* This is a warning and not a complaint because it can be caused by
13069 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13070 {
13071 /* Print the name of the DWP file if we looked there, helps the user
13072 better diagnose the problem. */
791afaa2 13073 std::string dwp_text;
43942612
DE
13074
13075 if (dwp_file != NULL)
791afaa2
TT
13076 dwp_text = string_printf (" [in DWP file %s]",
13077 lbasename (dwp_file->name));
43942612 13078
9d8780f0 13079 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13080 " [in module %s]"),
13081 kind, dwo_name, hex_string (signature),
791afaa2 13082 dwp_text.c_str (),
43942612 13083 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13084 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13085 }
3019eac3 13086 return NULL;
5fb290d7
DJ
13087}
13088
80626a55
DE
13089/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13090 See lookup_dwo_cutu_unit for details. */
13091
13092static struct dwo_unit *
13093lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13094 const char *dwo_name, const char *comp_dir,
13095 ULONGEST signature)
13096{
13097 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13098}
13099
13100/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13101 See lookup_dwo_cutu_unit for details. */
13102
13103static struct dwo_unit *
13104lookup_dwo_type_unit (struct signatured_type *this_tu,
13105 const char *dwo_name, const char *comp_dir)
13106{
13107 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13108}
13109
89e63ee4
DE
13110/* Traversal function for queue_and_load_all_dwo_tus. */
13111
13112static int
13113queue_and_load_dwo_tu (void **slot, void *info)
13114{
13115 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13116 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13117 ULONGEST signature = dwo_unit->signature;
13118 struct signatured_type *sig_type =
13119 lookup_dwo_signatured_type (per_cu->cu, signature);
13120
13121 if (sig_type != NULL)
13122 {
13123 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13124
13125 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13126 a real dependency of PER_CU on SIG_TYPE. That is detected later
13127 while processing PER_CU. */
13128 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13129 load_full_type_unit (sig_cu);
ae640021 13130 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13131 }
13132
13133 return 1;
13134}
13135
13136/* Queue all TUs contained in the DWO of PER_CU to be read in.
13137 The DWO may have the only definition of the type, though it may not be
13138 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13139 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13140
13141static void
13142queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13143{
13144 struct dwo_unit *dwo_unit;
13145 struct dwo_file *dwo_file;
13146
13147 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13148 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13149 gdb_assert (per_cu->cu != NULL);
13150
13151 dwo_unit = per_cu->cu->dwo_unit;
13152 gdb_assert (dwo_unit != NULL);
13153
13154 dwo_file = dwo_unit->dwo_file;
13155 if (dwo_file->tus != NULL)
13156 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13157}
13158
3019eac3 13159/* Read in various DIEs. */
348e048f 13160
d389af10 13161/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13162 Inherit only the children of the DW_AT_abstract_origin DIE not being
13163 already referenced by DW_AT_abstract_origin from the children of the
13164 current DIE. */
d389af10
JK
13165
13166static void
13167inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13168{
13169 struct die_info *child_die;
791afaa2 13170 sect_offset *offsetp;
d389af10
JK
13171 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13172 struct die_info *origin_die;
13173 /* Iterator of the ORIGIN_DIE children. */
13174 struct die_info *origin_child_die;
d389af10 13175 struct attribute *attr;
cd02d79d
PA
13176 struct dwarf2_cu *origin_cu;
13177 struct pending **origin_previous_list_in_scope;
d389af10
JK
13178
13179 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13180 if (!attr)
13181 return;
13182
cd02d79d
PA
13183 /* Note that following die references may follow to a die in a
13184 different cu. */
13185
13186 origin_cu = cu;
13187 origin_die = follow_die_ref (die, attr, &origin_cu);
13188
13189 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13190 symbols in. */
13191 origin_previous_list_in_scope = origin_cu->list_in_scope;
13192 origin_cu->list_in_scope = cu->list_in_scope;
13193
edb3359d
DJ
13194 if (die->tag != origin_die->tag
13195 && !(die->tag == DW_TAG_inlined_subroutine
13196 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13197 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13198 sect_offset_str (die->sect_off),
13199 sect_offset_str (origin_die->sect_off));
d389af10 13200
791afaa2 13201 std::vector<sect_offset> offsets;
d389af10 13202
3ea89b92
PMR
13203 for (child_die = die->child;
13204 child_die && child_die->tag;
13205 child_die = sibling_die (child_die))
13206 {
13207 struct die_info *child_origin_die;
13208 struct dwarf2_cu *child_origin_cu;
13209
13210 /* We are trying to process concrete instance entries:
216f72a1 13211 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13212 it's not relevant to our analysis here. i.e. detecting DIEs that are
13213 present in the abstract instance but not referenced in the concrete
13214 one. */
216f72a1
JK
13215 if (child_die->tag == DW_TAG_call_site
13216 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13217 continue;
13218
c38f313d
DJ
13219 /* For each CHILD_DIE, find the corresponding child of
13220 ORIGIN_DIE. If there is more than one layer of
13221 DW_AT_abstract_origin, follow them all; there shouldn't be,
13222 but GCC versions at least through 4.4 generate this (GCC PR
13223 40573). */
3ea89b92
PMR
13224 child_origin_die = child_die;
13225 child_origin_cu = cu;
c38f313d
DJ
13226 while (1)
13227 {
cd02d79d
PA
13228 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13229 child_origin_cu);
c38f313d
DJ
13230 if (attr == NULL)
13231 break;
cd02d79d
PA
13232 child_origin_die = follow_die_ref (child_origin_die, attr,
13233 &child_origin_cu);
c38f313d
DJ
13234 }
13235
d389af10
JK
13236 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13237 counterpart may exist. */
c38f313d 13238 if (child_origin_die != child_die)
d389af10 13239 {
edb3359d
DJ
13240 if (child_die->tag != child_origin_die->tag
13241 && !(child_die->tag == DW_TAG_inlined_subroutine
13242 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13243 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13244 "different tags"),
9d8780f0
SM
13245 sect_offset_str (child_die->sect_off),
13246 sect_offset_str (child_origin_die->sect_off));
c38f313d 13247 if (child_origin_die->parent != origin_die)
b98664d3 13248 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13249 "different parents"),
9d8780f0
SM
13250 sect_offset_str (child_die->sect_off),
13251 sect_offset_str (child_origin_die->sect_off));
c38f313d 13252 else
791afaa2 13253 offsets.push_back (child_origin_die->sect_off);
d389af10 13254 }
d389af10 13255 }
791afaa2
TT
13256 std::sort (offsets.begin (), offsets.end ());
13257 sect_offset *offsets_end = offsets.data () + offsets.size ();
13258 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13259 if (offsetp[-1] == *offsetp)
b98664d3 13260 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13261 "to DIE %s as their abstract origin"),
13262 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13263
791afaa2 13264 offsetp = offsets.data ();
d389af10
JK
13265 origin_child_die = origin_die->child;
13266 while (origin_child_die && origin_child_die->tag)
13267 {
13268 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13269 while (offsetp < offsets_end
9c541725 13270 && *offsetp < origin_child_die->sect_off)
d389af10 13271 offsetp++;
b64f50a1 13272 if (offsetp >= offsets_end
9c541725 13273 || *offsetp > origin_child_die->sect_off)
d389af10 13274 {
adde2bff
DE
13275 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13276 Check whether we're already processing ORIGIN_CHILD_DIE.
13277 This can happen with mutually referenced abstract_origins.
13278 PR 16581. */
13279 if (!origin_child_die->in_process)
13280 process_die (origin_child_die, origin_cu);
d389af10
JK
13281 }
13282 origin_child_die = sibling_die (origin_child_die);
13283 }
cd02d79d 13284 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13285
13286 if (cu != origin_cu)
13287 compute_delayed_physnames (origin_cu);
d389af10
JK
13288}
13289
c906108c 13290static void
e7c27a73 13291read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13292{
518817b3 13293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13294 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13295 struct context_stack *newobj;
c906108c
SS
13296 CORE_ADDR lowpc;
13297 CORE_ADDR highpc;
13298 struct die_info *child_die;
edb3359d 13299 struct attribute *attr, *call_line, *call_file;
15d034d0 13300 const char *name;
e142c38c 13301 CORE_ADDR baseaddr;
801e3a5b 13302 struct block *block;
edb3359d 13303 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13304 std::vector<struct symbol *> template_args;
34eaf542 13305 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13306
13307 if (inlined_func)
13308 {
13309 /* If we do not have call site information, we can't show the
13310 caller of this inlined function. That's too confusing, so
13311 only use the scope for local variables. */
13312 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13313 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13314 if (call_line == NULL || call_file == NULL)
13315 {
13316 read_lexical_block_scope (die, cu);
13317 return;
13318 }
13319 }
c906108c 13320
b3b3bada 13321 baseaddr = objfile->text_section_offset ();
e142c38c 13322
94af9270 13323 name = dwarf2_name (die, cu);
c906108c 13324
e8d05480
JB
13325 /* Ignore functions with missing or empty names. These are actually
13326 illegal according to the DWARF standard. */
13327 if (name == NULL)
13328 {
b98664d3 13329 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13330 sect_offset_str (die->sect_off));
e8d05480
JB
13331 return;
13332 }
13333
13334 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13335 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13336 <= PC_BOUNDS_INVALID)
e8d05480 13337 {
ae4d0c03
PM
13338 attr = dwarf2_attr (die, DW_AT_external, cu);
13339 if (!attr || !DW_UNSND (attr))
b98664d3 13340 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13341 "for subprogram DIE at %s"),
13342 sect_offset_str (die->sect_off));
e8d05480
JB
13343 return;
13344 }
c906108c 13345
3e29f34a
MR
13346 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13347 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13348
34eaf542
TT
13349 /* If we have any template arguments, then we must allocate a
13350 different sort of symbol. */
13351 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13352 {
13353 if (child_die->tag == DW_TAG_template_type_param
13354 || child_die->tag == DW_TAG_template_value_param)
13355 {
e623cf5d 13356 templ_func = allocate_template_symbol (objfile);
cf724bc9 13357 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13358 break;
13359 }
13360 }
13361
c24bdb02 13362 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13363 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13364 (struct symbol *) templ_func);
4c2df51b 13365
81873cc8 13366 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13367 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13368 cu->language);
13369
4cecd739
DJ
13370 /* If there is a location expression for DW_AT_frame_base, record
13371 it. */
e142c38c 13372 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13373 if (attr != nullptr)
fe978cb0 13374 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13375
63e43d3a
PMR
13376 /* If there is a location for the static link, record it. */
13377 newobj->static_link = NULL;
13378 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13379 if (attr != nullptr)
63e43d3a 13380 {
224c3ddb
SM
13381 newobj->static_link
13382 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13383 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13384 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13385 }
13386
c24bdb02 13387 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13388
639d11d3 13389 if (die->child != NULL)
c906108c 13390 {
639d11d3 13391 child_die = die->child;
c906108c
SS
13392 while (child_die && child_die->tag)
13393 {
34eaf542
TT
13394 if (child_die->tag == DW_TAG_template_type_param
13395 || child_die->tag == DW_TAG_template_value_param)
13396 {
13397 struct symbol *arg = new_symbol (child_die, NULL, cu);
13398
f1078f66 13399 if (arg != NULL)
2f4732b0 13400 template_args.push_back (arg);
34eaf542
TT
13401 }
13402 else
13403 process_die (child_die, cu);
c906108c
SS
13404 child_die = sibling_die (child_die);
13405 }
13406 }
13407
d389af10
JK
13408 inherit_abstract_dies (die, cu);
13409
4a811a97
UW
13410 /* If we have a DW_AT_specification, we might need to import using
13411 directives from the context of the specification DIE. See the
13412 comment in determine_prefix. */
13413 if (cu->language == language_cplus
13414 && dwarf2_attr (die, DW_AT_specification, cu))
13415 {
13416 struct dwarf2_cu *spec_cu = cu;
13417 struct die_info *spec_die = die_specification (die, &spec_cu);
13418
13419 while (spec_die)
13420 {
13421 child_die = spec_die->child;
13422 while (child_die && child_die->tag)
13423 {
13424 if (child_die->tag == DW_TAG_imported_module)
13425 process_die (child_die, spec_cu);
13426 child_die = sibling_die (child_die);
13427 }
13428
13429 /* In some cases, GCC generates specification DIEs that
13430 themselves contain DW_AT_specification attributes. */
13431 spec_die = die_specification (spec_die, &spec_cu);
13432 }
13433 }
13434
c24bdb02 13435 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13436 /* Make a block for the local symbols within. */
c24bdb02 13437 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13438 cstk.static_link, lowpc, highpc);
801e3a5b 13439
df8a16a1 13440 /* For C++, set the block's scope. */
45280282
IB
13441 if ((cu->language == language_cplus
13442 || cu->language == language_fortran
c44af4eb
TT
13443 || cu->language == language_d
13444 || cu->language == language_rust)
4d4ec4e5 13445 && cu->processing_has_namespace_info)
195a3f6c
TT
13446 block_set_scope (block, determine_prefix (die, cu),
13447 &objfile->objfile_obstack);
df8a16a1 13448
801e3a5b
JB
13449 /* If we have address ranges, record them. */
13450 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13451
a60f3166 13452 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13453
34eaf542 13454 /* Attach template arguments to function. */
2f4732b0 13455 if (!template_args.empty ())
34eaf542
TT
13456 {
13457 gdb_assert (templ_func != NULL);
13458
2f4732b0 13459 templ_func->n_template_arguments = template_args.size ();
34eaf542 13460 templ_func->template_arguments
8d749320
SM
13461 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13462 templ_func->n_template_arguments);
34eaf542 13463 memcpy (templ_func->template_arguments,
2f4732b0 13464 template_args.data (),
34eaf542 13465 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13466
13467 /* Make sure that the symtab is set on the new symbols. Even
13468 though they don't appear in this symtab directly, other parts
13469 of gdb assume that symbols do, and this is reasonably
13470 true. */
8634679f 13471 for (symbol *sym : template_args)
3e1d3d8c 13472 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13473 }
13474
208d8187
JB
13475 /* In C++, we can have functions nested inside functions (e.g., when
13476 a function declares a class that has methods). This means that
13477 when we finish processing a function scope, we may need to go
13478 back to building a containing block's symbol lists. */
c24bdb02
KS
13479 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13480 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13481
921e78cf
JB
13482 /* If we've finished processing a top-level function, subsequent
13483 symbols go in the file symbol list. */
c24bdb02
KS
13484 if (cu->get_builder ()->outermost_context_p ())
13485 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13486}
13487
13488/* Process all the DIES contained within a lexical block scope. Start
13489 a new scope, process the dies, and then close the scope. */
13490
13491static void
e7c27a73 13492read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13493{
518817b3 13494 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13495 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13496 CORE_ADDR lowpc, highpc;
13497 struct die_info *child_die;
e142c38c
DJ
13498 CORE_ADDR baseaddr;
13499
b3b3bada 13500 baseaddr = objfile->text_section_offset ();
c906108c
SS
13501
13502 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13503 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13504 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13505 be nasty. Might be easier to properly extend generic blocks to
af34e669 13506 describe ranges. */
e385593e
JK
13507 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13508 {
13509 case PC_BOUNDS_NOT_PRESENT:
13510 /* DW_TAG_lexical_block has no attributes, process its children as if
13511 there was no wrapping by that DW_TAG_lexical_block.
13512 GCC does no longer produces such DWARF since GCC r224161. */
13513 for (child_die = die->child;
13514 child_die != NULL && child_die->tag;
13515 child_die = sibling_die (child_die))
13516 process_die (child_die, cu);
13517 return;
13518 case PC_BOUNDS_INVALID:
13519 return;
13520 }
3e29f34a
MR
13521 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13522 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13523
c24bdb02 13524 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13525 if (die->child != NULL)
c906108c 13526 {
639d11d3 13527 child_die = die->child;
c906108c
SS
13528 while (child_die && child_die->tag)
13529 {
e7c27a73 13530 process_die (child_die, cu);
c906108c
SS
13531 child_die = sibling_die (child_die);
13532 }
13533 }
3ea89b92 13534 inherit_abstract_dies (die, cu);
c24bdb02 13535 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13536
c24bdb02
KS
13537 if (*cu->get_builder ()->get_local_symbols () != NULL
13538 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13539 {
801e3a5b 13540 struct block *block
c24bdb02 13541 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13542 cstk.start_addr, highpc);
801e3a5b
JB
13543
13544 /* Note that recording ranges after traversing children, as we
13545 do here, means that recording a parent's ranges entails
13546 walking across all its children's ranges as they appear in
13547 the address map, which is quadratic behavior.
13548
13549 It would be nicer to record the parent's ranges before
13550 traversing its children, simply overriding whatever you find
13551 there. But since we don't even decide whether to create a
13552 block until after we've traversed its children, that's hard
13553 to do. */
13554 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13555 }
c24bdb02
KS
13556 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13557 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13558}
13559
216f72a1 13560/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13561
13562static void
13563read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13564{
518817b3 13565 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13566 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13567 CORE_ADDR pc, baseaddr;
13568 struct attribute *attr;
13569 struct call_site *call_site, call_site_local;
13570 void **slot;
13571 int nparams;
13572 struct die_info *child_die;
13573
b3b3bada 13574 baseaddr = objfile->text_section_offset ();
96408a79 13575
216f72a1
JK
13576 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13577 if (attr == NULL)
13578 {
13579 /* This was a pre-DWARF-5 GNU extension alias
13580 for DW_AT_call_return_pc. */
13581 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13582 }
96408a79
SA
13583 if (!attr)
13584 {
b98664d3 13585 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13586 "DIE %s [in module %s]"),
13587 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13588 return;
13589 }
cd6c91b4 13590 pc = attr->value_as_address () + baseaddr;
3e29f34a 13591 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13592
13593 if (cu->call_site_htab == NULL)
13594 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13595 NULL, &objfile->objfile_obstack,
13596 hashtab_obstack_allocate, NULL);
13597 call_site_local.pc = pc;
13598 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13599 if (*slot != NULL)
13600 {
b98664d3 13601 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13602 "DIE %s [in module %s]"),
13603 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13604 objfile_name (objfile));
96408a79
SA
13605 return;
13606 }
13607
13608 /* Count parameters at the caller. */
13609
13610 nparams = 0;
13611 for (child_die = die->child; child_die && child_die->tag;
13612 child_die = sibling_die (child_die))
13613 {
216f72a1
JK
13614 if (child_die->tag != DW_TAG_call_site_parameter
13615 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13616 {
b98664d3 13617 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13618 "DW_TAG_call_site child DIE %s [in module %s]"),
13619 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13620 objfile_name (objfile));
96408a79
SA
13621 continue;
13622 }
13623
13624 nparams++;
13625 }
13626
224c3ddb
SM
13627 call_site
13628 = ((struct call_site *)
13629 obstack_alloc (&objfile->objfile_obstack,
13630 sizeof (*call_site)
13631 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13632 *slot = call_site;
13633 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13634 call_site->pc = pc;
13635
216f72a1
JK
13636 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13637 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13638 {
13639 struct die_info *func_die;
13640
13641 /* Skip also over DW_TAG_inlined_subroutine. */
13642 for (func_die = die->parent;
13643 func_die && func_die->tag != DW_TAG_subprogram
13644 && func_die->tag != DW_TAG_subroutine_type;
13645 func_die = func_die->parent);
13646
216f72a1
JK
13647 /* DW_AT_call_all_calls is a superset
13648 of DW_AT_call_all_tail_calls. */
96408a79 13649 if (func_die
216f72a1 13650 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13651 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13652 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13653 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13654 {
13655 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13656 not complete. But keep CALL_SITE for look ups via call_site_htab,
13657 both the initial caller containing the real return address PC and
13658 the final callee containing the current PC of a chain of tail
13659 calls do not need to have the tail call list complete. But any
13660 function candidate for a virtual tail call frame searched via
13661 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13662 determined unambiguously. */
13663 }
13664 else
13665 {
13666 struct type *func_type = NULL;
13667
13668 if (func_die)
13669 func_type = get_die_type (func_die, cu);
13670 if (func_type != NULL)
13671 {
13672 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13673
13674 /* Enlist this call site to the function. */
13675 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13676 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13677 }
13678 else
b98664d3 13679 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13680 "DIE %s [in module %s]"),
13681 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13682 }
13683 }
13684
216f72a1
JK
13685 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13686 if (attr == NULL)
13687 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13688 if (attr == NULL)
13689 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13690 if (attr == NULL)
216f72a1
JK
13691 {
13692 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13693 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13694 }
96408a79 13695 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13696 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13697 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13698 else if (attr->form_is_block ())
96408a79
SA
13699 {
13700 struct dwarf2_locexpr_baton *dlbaton;
13701
8d749320 13702 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13703 dlbaton->data = DW_BLOCK (attr)->data;
13704 dlbaton->size = DW_BLOCK (attr)->size;
13705 dlbaton->per_cu = cu->per_cu;
13706
13707 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13708 }
cd6c91b4 13709 else if (attr->form_is_ref ())
96408a79 13710 {
96408a79
SA
13711 struct dwarf2_cu *target_cu = cu;
13712 struct die_info *target_die;
13713
ac9ec31b 13714 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13715 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13716 if (die_is_declaration (target_die, target_cu))
13717 {
7d45c7c3 13718 const char *target_physname;
9112db09
JK
13719
13720 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13721 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13722 if (target_physname == NULL)
9112db09 13723 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13724 if (target_physname == NULL)
b98664d3 13725 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13726 "physname, for referencing DIE %s [in module %s]"),
13727 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13728 else
7d455152 13729 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13730 }
13731 else
13732 {
13733 CORE_ADDR lowpc;
13734
13735 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13736 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13737 <= PC_BOUNDS_INVALID)
b98664d3 13738 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13739 "low pc, for referencing DIE %s [in module %s]"),
13740 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13741 else
3e29f34a
MR
13742 {
13743 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13744 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13745 }
96408a79
SA
13746 }
13747 }
13748 else
b98664d3 13749 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13750 "block nor reference, for DIE %s [in module %s]"),
13751 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13752
13753 call_site->per_cu = cu->per_cu;
13754
13755 for (child_die = die->child;
13756 child_die && child_die->tag;
13757 child_die = sibling_die (child_die))
13758 {
96408a79 13759 struct call_site_parameter *parameter;
1788b2d3 13760 struct attribute *loc, *origin;
96408a79 13761
216f72a1
JK
13762 if (child_die->tag != DW_TAG_call_site_parameter
13763 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13764 {
13765 /* Already printed the complaint above. */
13766 continue;
13767 }
13768
13769 gdb_assert (call_site->parameter_count < nparams);
13770 parameter = &call_site->parameter[call_site->parameter_count];
13771
1788b2d3
JK
13772 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13773 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13774 register is contained in DW_AT_call_value. */
96408a79 13775
24c5c679 13776 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13777 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13778 if (origin == NULL)
13779 {
13780 /* This was a pre-DWARF-5 GNU extension alias
13781 for DW_AT_call_parameter. */
13782 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13783 }
cd6c91b4 13784 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13785 {
1788b2d3 13786 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13787
13788 sect_offset sect_off
13789 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13790 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
13791 {
13792 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13793 binding can be done only inside one CU. Such referenced DIE
13794 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13795 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13796 "DW_TAG_call_site child DIE %s [in module %s]"),
13797 sect_offset_str (child_die->sect_off),
9c541725 13798 objfile_name (objfile));
d76b7dbc
JK
13799 continue;
13800 }
9c541725
PA
13801 parameter->u.param_cu_off
13802 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13803 }
4fc6c0d5 13804 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13805 {
b98664d3 13806 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13807 "DW_TAG_call_site child DIE %s [in module %s]"),
13808 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13809 continue;
13810 }
24c5c679 13811 else
96408a79 13812 {
24c5c679
JK
13813 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13814 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13815 if (parameter->u.dwarf_reg != -1)
13816 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13817 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13818 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13819 &parameter->u.fb_offset))
13820 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13821 else
13822 {
b98664d3 13823 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13824 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13825 "DW_TAG_call_site child DIE %s "
24c5c679 13826 "[in module %s]"),
9d8780f0 13827 sect_offset_str (child_die->sect_off),
9c541725 13828 objfile_name (objfile));
24c5c679
JK
13829 continue;
13830 }
96408a79
SA
13831 }
13832
216f72a1
JK
13833 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13834 if (attr == NULL)
13835 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13836 if (attr == NULL || !attr->form_is_block ())
96408a79 13837 {
b98664d3 13838 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13839 "DW_TAG_call_site child DIE %s [in module %s]"),
13840 sect_offset_str (child_die->sect_off),
9c541725 13841 objfile_name (objfile));
96408a79
SA
13842 continue;
13843 }
13844 parameter->value = DW_BLOCK (attr)->data;
13845 parameter->value_size = DW_BLOCK (attr)->size;
13846
13847 /* Parameters are not pre-cleared by memset above. */
13848 parameter->data_value = NULL;
13849 parameter->data_value_size = 0;
13850 call_site->parameter_count++;
13851
216f72a1
JK
13852 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13853 if (attr == NULL)
13854 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13855 if (attr != nullptr)
96408a79 13856 {
4fc6c0d5 13857 if (!attr->form_is_block ())
b98664d3 13858 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13859 "DW_TAG_call_site child DIE %s [in module %s]"),
13860 sect_offset_str (child_die->sect_off),
9c541725 13861 objfile_name (objfile));
96408a79
SA
13862 else
13863 {
13864 parameter->data_value = DW_BLOCK (attr)->data;
13865 parameter->data_value_size = DW_BLOCK (attr)->size;
13866 }
13867 }
13868 }
13869}
13870
71a3c369
TT
13871/* Helper function for read_variable. If DIE represents a virtual
13872 table, then return the type of the concrete object that is
13873 associated with the virtual table. Otherwise, return NULL. */
13874
13875static struct type *
13876rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13877{
13878 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13879 if (attr == NULL)
13880 return NULL;
13881
13882 /* Find the type DIE. */
13883 struct die_info *type_die = NULL;
13884 struct dwarf2_cu *type_cu = cu;
13885
cd6c91b4 13886 if (attr->form_is_ref ())
71a3c369
TT
13887 type_die = follow_die_ref (die, attr, &type_cu);
13888 if (type_die == NULL)
13889 return NULL;
13890
13891 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13892 return NULL;
13893 return die_containing_type (type_die, type_cu);
13894}
13895
13896/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13897
13898static void
13899read_variable (struct die_info *die, struct dwarf2_cu *cu)
13900{
13901 struct rust_vtable_symbol *storage = NULL;
13902
13903 if (cu->language == language_rust)
13904 {
13905 struct type *containing_type = rust_containing_type (die, cu);
13906
13907 if (containing_type != NULL)
13908 {
518817b3 13909 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13910
468c0cbb 13911 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13912 initialize_objfile_symbol (storage);
13913 storage->concrete_type = containing_type;
cf724bc9 13914 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13915 }
13916 }
13917
e4a62c65
TV
13918 struct symbol *res = new_symbol (die, NULL, cu, storage);
13919 struct attribute *abstract_origin
13920 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13921 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13922 if (res == NULL && loc && abstract_origin)
13923 {
13924 /* We have a variable without a name, but with a location and an abstract
13925 origin. This may be a concrete instance of an abstract variable
13926 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13927 later. */
13928 struct dwarf2_cu *origin_cu = cu;
13929 struct die_info *origin_die
13930 = follow_die_ref (die, abstract_origin, &origin_cu);
13931 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13932 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13933 }
71a3c369
TT
13934}
13935
43988095
JK
13936/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13937 reading .debug_rnglists.
13938 Callback's type should be:
13939 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13940 Return true if the attributes are present and valid, otherwise,
13941 return false. */
13942
13943template <typename Callback>
13944static bool
13945dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13946 Callback &&callback)
13947{
ed2dc618 13948 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13949 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13950 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13951 bfd *obfd = objfile->obfd;
43988095
JK
13952 /* Base address selection entry. */
13953 CORE_ADDR base;
13954 int found_base;
43988095 13955 const gdb_byte *buffer;
43988095
JK
13956 CORE_ADDR baseaddr;
13957 bool overflow = false;
13958
13959 found_base = cu->base_known;
13960 base = cu->base_address;
13961
96b79293 13962 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13963 if (offset >= dwarf2_per_objfile->rnglists.size)
13964 {
b98664d3 13965 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13966 offset);
13967 return false;
13968 }
13969 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13970
b3b3bada 13971 baseaddr = objfile->text_section_offset ();
43988095
JK
13972
13973 while (1)
13974 {
7814882a
JK
13975 /* Initialize it due to a false compiler warning. */
13976 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13977 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13978 + dwarf2_per_objfile->rnglists.size);
13979 unsigned int bytes_read;
13980
13981 if (buffer == buf_end)
13982 {
13983 overflow = true;
13984 break;
13985 }
13986 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13987 switch (rlet)
13988 {
13989 case DW_RLE_end_of_list:
13990 break;
13991 case DW_RLE_base_address:
13992 if (buffer + cu->header.addr_size > buf_end)
13993 {
13994 overflow = true;
13995 break;
13996 }
13997 base = read_address (obfd, buffer, cu, &bytes_read);
13998 found_base = 1;
13999 buffer += bytes_read;
14000 break;
14001 case DW_RLE_start_length:
14002 if (buffer + cu->header.addr_size > buf_end)
14003 {
14004 overflow = true;
14005 break;
14006 }
14007 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14008 buffer += bytes_read;
14009 range_end = (range_beginning
14010 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14011 buffer += bytes_read;
14012 if (buffer > buf_end)
14013 {
14014 overflow = true;
14015 break;
14016 }
14017 break;
14018 case DW_RLE_offset_pair:
14019 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14020 buffer += bytes_read;
14021 if (buffer > buf_end)
14022 {
14023 overflow = true;
14024 break;
14025 }
14026 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14027 buffer += bytes_read;
14028 if (buffer > buf_end)
14029 {
14030 overflow = true;
14031 break;
14032 }
14033 break;
14034 case DW_RLE_start_end:
14035 if (buffer + 2 * cu->header.addr_size > buf_end)
14036 {
14037 overflow = true;
14038 break;
14039 }
14040 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14041 buffer += bytes_read;
14042 range_end = read_address (obfd, buffer, cu, &bytes_read);
14043 buffer += bytes_read;
14044 break;
14045 default:
b98664d3 14046 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14047 return false;
14048 }
14049 if (rlet == DW_RLE_end_of_list || overflow)
14050 break;
14051 if (rlet == DW_RLE_base_address)
14052 continue;
14053
14054 if (!found_base)
14055 {
14056 /* We have no valid base address for the ranges
14057 data. */
b98664d3 14058 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14059 return false;
14060 }
14061
14062 if (range_beginning > range_end)
14063 {
14064 /* Inverted range entries are invalid. */
b98664d3 14065 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14066 return false;
14067 }
14068
14069 /* Empty range entries have no effect. */
14070 if (range_beginning == range_end)
14071 continue;
14072
14073 range_beginning += base;
14074 range_end += base;
14075
14076 /* A not-uncommon case of bad debug info.
14077 Don't pollute the addrmap with bad data. */
14078 if (range_beginning + baseaddr == 0
14079 && !dwarf2_per_objfile->has_section_at_zero)
14080 {
b98664d3 14081 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14082 " [in module %s]"), objfile_name (objfile));
14083 continue;
14084 }
14085
14086 callback (range_beginning, range_end);
14087 }
14088
14089 if (overflow)
14090 {
b98664d3 14091 complaint (_("Offset %d is not terminated "
43988095
JK
14092 "for DW_AT_ranges attribute"),
14093 offset);
14094 return false;
14095 }
14096
14097 return true;
14098}
14099
14100/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14101 Callback's type should be:
14102 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14103 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14104
43988095 14105template <typename Callback>
43039443 14106static int
5f46c5a5 14107dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14108 Callback &&callback)
43039443 14109{
ed2dc618 14110 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14111 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14112 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14113 struct comp_unit_head *cu_header = &cu->header;
14114 bfd *obfd = objfile->obfd;
14115 unsigned int addr_size = cu_header->addr_size;
14116 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14117 /* Base address selection entry. */
14118 CORE_ADDR base;
14119 int found_base;
14120 unsigned int dummy;
d521ce57 14121 const gdb_byte *buffer;
ff013f42 14122 CORE_ADDR baseaddr;
43039443 14123
43988095
JK
14124 if (cu_header->version >= 5)
14125 return dwarf2_rnglists_process (offset, cu, callback);
14126
d00adf39
DE
14127 found_base = cu->base_known;
14128 base = cu->base_address;
43039443 14129
96b79293 14130 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 14131 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14132 {
b98664d3 14133 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14134 offset);
14135 return 0;
14136 }
dce234bc 14137 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14138
b3b3bada 14139 baseaddr = objfile->text_section_offset ();
ff013f42 14140
43039443
JK
14141 while (1)
14142 {
14143 CORE_ADDR range_beginning, range_end;
14144
14145 range_beginning = read_address (obfd, buffer, cu, &dummy);
14146 buffer += addr_size;
14147 range_end = read_address (obfd, buffer, cu, &dummy);
14148 buffer += addr_size;
14149 offset += 2 * addr_size;
14150
14151 /* An end of list marker is a pair of zero addresses. */
14152 if (range_beginning == 0 && range_end == 0)
14153 /* Found the end of list entry. */
14154 break;
14155
14156 /* Each base address selection entry is a pair of 2 values.
14157 The first is the largest possible address, the second is
14158 the base address. Check for a base address here. */
14159 if ((range_beginning & mask) == mask)
14160 {
28d2bfb9
AB
14161 /* If we found the largest possible address, then we already
14162 have the base address in range_end. */
14163 base = range_end;
43039443
JK
14164 found_base = 1;
14165 continue;
14166 }
14167
14168 if (!found_base)
14169 {
14170 /* We have no valid base address for the ranges
14171 data. */
b98664d3 14172 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14173 return 0;
14174 }
14175
9277c30c
UW
14176 if (range_beginning > range_end)
14177 {
14178 /* Inverted range entries are invalid. */
b98664d3 14179 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14180 return 0;
14181 }
14182
14183 /* Empty range entries have no effect. */
14184 if (range_beginning == range_end)
14185 continue;
14186
43039443
JK
14187 range_beginning += base;
14188 range_end += base;
14189
01093045
DE
14190 /* A not-uncommon case of bad debug info.
14191 Don't pollute the addrmap with bad data. */
14192 if (range_beginning + baseaddr == 0
14193 && !dwarf2_per_objfile->has_section_at_zero)
14194 {
b98664d3 14195 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14196 " [in module %s]"), objfile_name (objfile));
01093045
DE
14197 continue;
14198 }
14199
5f46c5a5
JK
14200 callback (range_beginning, range_end);
14201 }
14202
14203 return 1;
14204}
14205
14206/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14207 Return 1 if the attributes are present and valid, otherwise, return 0.
14208 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14209
14210static int
14211dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14212 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14213 dwarf2_psymtab *ranges_pst)
5f46c5a5 14214{
518817b3 14215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 14216 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 14217 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14218 int low_set = 0;
14219 CORE_ADDR low = 0;
14220 CORE_ADDR high = 0;
14221 int retval;
14222
14223 retval = dwarf2_ranges_process (offset, cu,
14224 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14225 {
9277c30c 14226 if (ranges_pst != NULL)
3e29f34a
MR
14227 {
14228 CORE_ADDR lowpc;
14229 CORE_ADDR highpc;
14230
79748972
TT
14231 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14232 range_beginning + baseaddr)
14233 - baseaddr);
14234 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14235 range_end + baseaddr)
14236 - baseaddr);
d320c2b5
TT
14237 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14238 lowpc, highpc - 1, ranges_pst);
3e29f34a 14239 }
ff013f42 14240
43039443
JK
14241 /* FIXME: This is recording everything as a low-high
14242 segment of consecutive addresses. We should have a
14243 data structure for discontiguous block ranges
14244 instead. */
14245 if (! low_set)
14246 {
14247 low = range_beginning;
14248 high = range_end;
14249 low_set = 1;
14250 }
14251 else
14252 {
14253 if (range_beginning < low)
14254 low = range_beginning;
14255 if (range_end > high)
14256 high = range_end;
14257 }
5f46c5a5
JK
14258 });
14259 if (!retval)
14260 return 0;
43039443
JK
14261
14262 if (! low_set)
14263 /* If the first entry is an end-of-list marker, the range
14264 describes an empty scope, i.e. no instructions. */
14265 return 0;
14266
14267 if (low_return)
14268 *low_return = low;
14269 if (high_return)
14270 *high_return = high;
14271 return 1;
14272}
14273
3a2b436a
JK
14274/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14275 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14276 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14277
3a2b436a 14278static enum pc_bounds_kind
af34e669 14279dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14280 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14281 dwarf2_psymtab *pst)
c906108c 14282{
518817b3
SM
14283 struct dwarf2_per_objfile *dwarf2_per_objfile
14284 = cu->per_cu->dwarf2_per_objfile;
c906108c 14285 struct attribute *attr;
91da1414 14286 struct attribute *attr_high;
af34e669
DJ
14287 CORE_ADDR low = 0;
14288 CORE_ADDR high = 0;
e385593e 14289 enum pc_bounds_kind ret;
c906108c 14290
91da1414
MW
14291 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14292 if (attr_high)
af34e669 14293 {
e142c38c 14294 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14295 if (attr != nullptr)
91da1414 14296 {
cd6c91b4
TT
14297 low = attr->value_as_address ();
14298 high = attr_high->value_as_address ();
14299 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14300 high += low;
91da1414 14301 }
af34e669
DJ
14302 else
14303 /* Found high w/o low attribute. */
e385593e 14304 return PC_BOUNDS_INVALID;
af34e669
DJ
14305
14306 /* Found consecutive range of addresses. */
3a2b436a 14307 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14308 }
c906108c 14309 else
af34e669 14310 {
e142c38c 14311 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14312 if (attr != NULL)
14313 {
18a8505e 14314 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14315 We take advantage of the fact that DW_AT_ranges does not appear
14316 in DW_TAG_compile_unit of DWO files. */
14317 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14318 unsigned int ranges_offset = (DW_UNSND (attr)
14319 + (need_ranges_base
14320 ? cu->ranges_base
14321 : 0));
2e3cf129 14322
af34e669 14323 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14324 .debug_ranges section. */
2e3cf129 14325 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14326 return PC_BOUNDS_INVALID;
43039443 14327 /* Found discontinuous range of addresses. */
3a2b436a 14328 ret = PC_BOUNDS_RANGES;
af34e669 14329 }
e385593e
JK
14330 else
14331 return PC_BOUNDS_NOT_PRESENT;
af34e669 14332 }
c906108c 14333
48fbe735 14334 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14335 if (high <= low)
e385593e 14336 return PC_BOUNDS_INVALID;
c906108c
SS
14337
14338 /* When using the GNU linker, .gnu.linkonce. sections are used to
14339 eliminate duplicate copies of functions and vtables and such.
14340 The linker will arbitrarily choose one and discard the others.
14341 The AT_*_pc values for such functions refer to local labels in
14342 these sections. If the section from that file was discarded, the
14343 labels are not in the output, so the relocs get a value of 0.
14344 If this is a discarded function, mark the pc bounds as invalid,
14345 so that GDB will ignore it. */
72dca2f5 14346 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14347 return PC_BOUNDS_INVALID;
c906108c
SS
14348
14349 *lowpc = low;
96408a79
SA
14350 if (highpc)
14351 *highpc = high;
af34e669 14352 return ret;
c906108c
SS
14353}
14354
b084d499
JB
14355/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14356 its low and high PC addresses. Do nothing if these addresses could not
14357 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14358 and HIGHPC to the high address if greater than HIGHPC. */
14359
14360static void
14361dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14362 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14363 struct dwarf2_cu *cu)
14364{
14365 CORE_ADDR low, high;
14366 struct die_info *child = die->child;
14367
e385593e 14368 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14369 {
325fac50
PA
14370 *lowpc = std::min (*lowpc, low);
14371 *highpc = std::max (*highpc, high);
b084d499
JB
14372 }
14373
14374 /* If the language does not allow nested subprograms (either inside
14375 subprograms or lexical blocks), we're done. */
14376 if (cu->language != language_ada)
14377 return;
6e70227d 14378
b084d499
JB
14379 /* Check all the children of the given DIE. If it contains nested
14380 subprograms, then check their pc bounds. Likewise, we need to
14381 check lexical blocks as well, as they may also contain subprogram
14382 definitions. */
14383 while (child && child->tag)
14384 {
14385 if (child->tag == DW_TAG_subprogram
14386 || child->tag == DW_TAG_lexical_block)
14387 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14388 child = sibling_die (child);
14389 }
14390}
14391
fae299cd
DC
14392/* Get the low and high pc's represented by the scope DIE, and store
14393 them in *LOWPC and *HIGHPC. If the correct values can't be
14394 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14395
14396static void
14397get_scope_pc_bounds (struct die_info *die,
14398 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14399 struct dwarf2_cu *cu)
14400{
14401 CORE_ADDR best_low = (CORE_ADDR) -1;
14402 CORE_ADDR best_high = (CORE_ADDR) 0;
14403 CORE_ADDR current_low, current_high;
14404
3a2b436a 14405 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14406 >= PC_BOUNDS_RANGES)
fae299cd
DC
14407 {
14408 best_low = current_low;
14409 best_high = current_high;
14410 }
14411 else
14412 {
14413 struct die_info *child = die->child;
14414
14415 while (child && child->tag)
14416 {
14417 switch (child->tag) {
14418 case DW_TAG_subprogram:
b084d499 14419 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14420 break;
14421 case DW_TAG_namespace:
f55ee35c 14422 case DW_TAG_module:
fae299cd
DC
14423 /* FIXME: carlton/2004-01-16: Should we do this for
14424 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14425 that current GCC's always emit the DIEs corresponding
14426 to definitions of methods of classes as children of a
14427 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14428 the DIEs giving the declarations, which could be
14429 anywhere). But I don't see any reason why the
14430 standards says that they have to be there. */
14431 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14432
14433 if (current_low != ((CORE_ADDR) -1))
14434 {
325fac50
PA
14435 best_low = std::min (best_low, current_low);
14436 best_high = std::max (best_high, current_high);
fae299cd
DC
14437 }
14438 break;
14439 default:
0963b4bd 14440 /* Ignore. */
fae299cd
DC
14441 break;
14442 }
14443
14444 child = sibling_die (child);
14445 }
14446 }
14447
14448 *lowpc = best_low;
14449 *highpc = best_high;
14450}
14451
801e3a5b
JB
14452/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14453 in DIE. */
380bca97 14454
801e3a5b
JB
14455static void
14456dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14457 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14458{
518817b3 14459 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14460 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14461 struct attribute *attr;
91da1414 14462 struct attribute *attr_high;
801e3a5b 14463
91da1414
MW
14464 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14465 if (attr_high)
801e3a5b 14466 {
801e3a5b 14467 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14468 if (attr != nullptr)
801e3a5b 14469 {
cd6c91b4
TT
14470 CORE_ADDR low = attr->value_as_address ();
14471 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14472
cd6c91b4 14473 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14474 high += low;
9a619af0 14475
3e29f34a
MR
14476 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14477 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14478 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14479 }
14480 }
14481
14482 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14483 if (attr != nullptr)
801e3a5b 14484 {
18a8505e 14485 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14486 We take advantage of the fact that DW_AT_ranges does not appear
14487 in DW_TAG_compile_unit of DWO files. */
14488 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14489
14490 /* The value of the DW_AT_ranges attribute is the offset of the
14491 address range list in the .debug_ranges section. */
ab435259
DE
14492 unsigned long offset = (DW_UNSND (attr)
14493 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14494
2d5f09ec 14495 std::vector<blockrange> blockvec;
5f46c5a5
JK
14496 dwarf2_ranges_process (offset, cu,
14497 [&] (CORE_ADDR start, CORE_ADDR end)
14498 {
58fdfd2c
JK
14499 start += baseaddr;
14500 end += baseaddr;
5f46c5a5
JK
14501 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14502 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14503 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14504 blockvec.emplace_back (start, end);
5f46c5a5 14505 });
2d5f09ec
KB
14506
14507 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14508 }
14509}
14510
685b1105
JK
14511/* Check whether the producer field indicates either of GCC < 4.6, or the
14512 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14513
685b1105
JK
14514static void
14515check_producer (struct dwarf2_cu *cu)
60d5a603 14516{
38360086 14517 int major, minor;
60d5a603
JK
14518
14519 if (cu->producer == NULL)
14520 {
14521 /* For unknown compilers expect their behavior is DWARF version
14522 compliant.
14523
14524 GCC started to support .debug_types sections by -gdwarf-4 since
14525 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14526 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14527 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14528 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14529 }
b1ffba5a 14530 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14531 {
38360086
MW
14532 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14533 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14534 }
5230b05a 14535 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14536 {
14537 cu->producer_is_icc = true;
14538 cu->producer_is_icc_lt_14 = major < 14;
14539 }
c258c396
JD
14540 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14541 cu->producer_is_codewarrior = true;
685b1105
JK
14542 else
14543 {
14544 /* For other non-GCC compilers, expect their behavior is DWARF version
14545 compliant. */
60d5a603
JK
14546 }
14547
9068261f 14548 cu->checked_producer = true;
685b1105 14549}
ba919b58 14550
685b1105
JK
14551/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14552 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14553 during 4.6.0 experimental. */
14554
9068261f 14555static bool
685b1105
JK
14556producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14557{
14558 if (!cu->checked_producer)
14559 check_producer (cu);
14560
14561 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14562}
14563
c258c396
JD
14564
14565/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14566 with incorrect is_stmt attributes. */
14567
14568static bool
14569producer_is_codewarrior (struct dwarf2_cu *cu)
14570{
14571 if (!cu->checked_producer)
14572 check_producer (cu);
14573
14574 return cu->producer_is_codewarrior;
14575}
14576
405feb71 14577/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14578 DW_AT_accessibility. */
14579
14580static enum dwarf_access_attribute
14581dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14582{
14583 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14584 {
14585 /* The default DWARF 2 accessibility for members is public, the default
14586 accessibility for inheritance is private. */
14587
14588 if (die->tag != DW_TAG_inheritance)
14589 return DW_ACCESS_public;
14590 else
14591 return DW_ACCESS_private;
14592 }
14593 else
14594 {
14595 /* DWARF 3+ defines the default accessibility a different way. The same
14596 rules apply now for DW_TAG_inheritance as for the members and it only
14597 depends on the container kind. */
14598
14599 if (die->parent->tag == DW_TAG_class_type)
14600 return DW_ACCESS_private;
14601 else
14602 return DW_ACCESS_public;
14603 }
14604}
14605
74ac6d43
TT
14606/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14607 offset. If the attribute was not found return 0, otherwise return
14608 1. If it was found but could not properly be handled, set *OFFSET
14609 to 0. */
14610
14611static int
14612handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14613 LONGEST *offset)
14614{
14615 struct attribute *attr;
14616
14617 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14618 if (attr != NULL)
14619 {
14620 *offset = 0;
14621
14622 /* Note that we do not check for a section offset first here.
14623 This is because DW_AT_data_member_location is new in DWARF 4,
14624 so if we see it, we can assume that a constant form is really
14625 a constant and not a section offset. */
cd6c91b4 14626 if (attr->form_is_constant ())
74ac6d43 14627 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14628 else if (attr->form_is_section_offset ())
74ac6d43 14629 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14630 else if (attr->form_is_block ())
74ac6d43
TT
14631 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14632 else
14633 dwarf2_complex_location_expr_complaint ();
14634
14635 return 1;
14636 }
14637
14638 return 0;
14639}
14640
c906108c
SS
14641/* Add an aggregate field to the field list. */
14642
14643static void
107d2387 14644dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14645 struct dwarf2_cu *cu)
6e70227d 14646{
518817b3 14647 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14649 struct nextfield *new_field;
14650 struct attribute *attr;
14651 struct field *fp;
15d034d0 14652 const char *fieldname = "";
c906108c 14653
7d0ccb61
DJ
14654 if (die->tag == DW_TAG_inheritance)
14655 {
be2daae6
TT
14656 fip->baseclasses.emplace_back ();
14657 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14658 }
14659 else
14660 {
be2daae6
TT
14661 fip->fields.emplace_back ();
14662 new_field = &fip->fields.back ();
7d0ccb61 14663 }
be2daae6 14664
c906108c
SS
14665 fip->nfields++;
14666
e142c38c 14667 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14668 if (attr != nullptr)
c906108c 14669 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14670 else
14671 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14672 if (new_field->accessibility != DW_ACCESS_public)
14673 fip->non_public_fields = 1;
60d5a603 14674
e142c38c 14675 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14676 if (attr != nullptr)
c906108c 14677 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14678 else
14679 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14680
14681 fp = &new_field->field;
a9a9bd0f 14682
e142c38c 14683 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14684 {
74ac6d43
TT
14685 LONGEST offset;
14686
a9a9bd0f 14687 /* Data member other than a C++ static data member. */
6e70227d 14688
c906108c 14689 /* Get type of field. */
e7c27a73 14690 fp->type = die_type (die, cu);
c906108c 14691
d6a843b5 14692 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14693
c906108c 14694 /* Get bit size of field (zero if none). */
e142c38c 14695 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14696 if (attr != nullptr)
c906108c
SS
14697 {
14698 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14699 }
14700 else
14701 {
14702 FIELD_BITSIZE (*fp) = 0;
14703 }
14704
14705 /* Get bit offset of field. */
74ac6d43
TT
14706 if (handle_data_member_location (die, cu, &offset))
14707 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14708 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14709 if (attr != nullptr)
c906108c 14710 {
d5a22e77 14711 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14712 {
14713 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14714 additional bit offset from the MSB of the containing
14715 anonymous object to the MSB of the field. We don't
14716 have to do anything special since we don't need to
14717 know the size of the anonymous object. */
f41f5e61 14718 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14719 }
14720 else
14721 {
14722 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14723 MSB of the anonymous object, subtract off the number of
14724 bits from the MSB of the field to the MSB of the
14725 object, and then subtract off the number of bits of
14726 the field itself. The result is the bit offset of
14727 the LSB of the field. */
c906108c
SS
14728 int anonymous_size;
14729 int bit_offset = DW_UNSND (attr);
14730
e142c38c 14731 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14732 if (attr != nullptr)
c906108c
SS
14733 {
14734 /* The size of the anonymous object containing
14735 the bit field is explicit, so use the
14736 indicated size (in bytes). */
14737 anonymous_size = DW_UNSND (attr);
14738 }
14739 else
14740 {
14741 /* The size of the anonymous object containing
14742 the bit field must be inferred from the type
14743 attribute of the data member containing the
14744 bit field. */
14745 anonymous_size = TYPE_LENGTH (fp->type);
14746 }
f41f5e61
PA
14747 SET_FIELD_BITPOS (*fp,
14748 (FIELD_BITPOS (*fp)
14749 + anonymous_size * bits_per_byte
14750 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14751 }
14752 }
da5b30da
AA
14753 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14754 if (attr != NULL)
14755 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14756 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14757
14758 /* Get name of field. */
39cbfefa
DJ
14759 fieldname = dwarf2_name (die, cu);
14760 if (fieldname == NULL)
14761 fieldname = "";
d8151005
DJ
14762
14763 /* The name is already allocated along with this objfile, so we don't
14764 need to duplicate it for the type. */
14765 fp->name = fieldname;
c906108c
SS
14766
14767 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14768 pointer or virtual base class pointer) to private. */
e142c38c 14769 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14770 {
d48cc9dd 14771 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14772 new_field->accessibility = DW_ACCESS_private;
14773 fip->non_public_fields = 1;
14774 }
14775 }
a9a9bd0f 14776 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14777 {
a9a9bd0f
DC
14778 /* C++ static member. */
14779
14780 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14781 is a declaration, but all versions of G++ as of this writing
14782 (so through at least 3.2.1) incorrectly generate
14783 DW_TAG_variable tags. */
6e70227d 14784
ff355380 14785 const char *physname;
c906108c 14786
a9a9bd0f 14787 /* Get name of field. */
39cbfefa
DJ
14788 fieldname = dwarf2_name (die, cu);
14789 if (fieldname == NULL)
c906108c
SS
14790 return;
14791
254e6b9e 14792 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14793 if (attr
14794 /* Only create a symbol if this is an external value.
14795 new_symbol checks this and puts the value in the global symbol
14796 table, which we want. If it is not external, new_symbol
14797 will try to put the value in cu->list_in_scope which is wrong. */
14798 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14799 {
14800 /* A static const member, not much different than an enum as far as
14801 we're concerned, except that we can support more types. */
14802 new_symbol (die, NULL, cu);
14803 }
14804
2df3850c 14805 /* Get physical name. */
ff355380 14806 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14807
d8151005
DJ
14808 /* The name is already allocated along with this objfile, so we don't
14809 need to duplicate it for the type. */
14810 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14811 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14812 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14813 }
14814 else if (die->tag == DW_TAG_inheritance)
14815 {
74ac6d43 14816 LONGEST offset;
d4b96c9a 14817
74ac6d43
TT
14818 /* C++ base class field. */
14819 if (handle_data_member_location (die, cu, &offset))
14820 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14821 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14822 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14823 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14824 }
2ddeaf8a
TT
14825 else if (die->tag == DW_TAG_variant_part)
14826 {
14827 /* process_structure_scope will treat this DIE as a union. */
14828 process_structure_scope (die, cu);
14829
14830 /* The variant part is relative to the start of the enclosing
14831 structure. */
14832 SET_FIELD_BITPOS (*fp, 0);
14833 fp->type = get_die_type (die, cu);
14834 fp->artificial = 1;
14835 fp->name = "<<variant>>";
c8c81635
TT
14836
14837 /* Normally a DW_TAG_variant_part won't have a size, but our
14838 representation requires one, so set it to the maximum of the
489dbda6
TT
14839 child sizes, being sure to account for the offset at which
14840 each child is seen. */
c8c81635
TT
14841 if (TYPE_LENGTH (fp->type) == 0)
14842 {
14843 unsigned max = 0;
14844 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14845 {
14846 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14847 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14848 if (len > max)
14849 max = len;
14850 }
c8c81635
TT
14851 TYPE_LENGTH (fp->type) = max;
14852 }
2ddeaf8a
TT
14853 }
14854 else
14855 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14856}
14857
883fd55a
KS
14858/* Can the type given by DIE define another type? */
14859
14860static bool
14861type_can_define_types (const struct die_info *die)
14862{
14863 switch (die->tag)
14864 {
14865 case DW_TAG_typedef:
14866 case DW_TAG_class_type:
14867 case DW_TAG_structure_type:
14868 case DW_TAG_union_type:
14869 case DW_TAG_enumeration_type:
14870 return true;
14871
14872 default:
14873 return false;
14874 }
14875}
14876
14877/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14878
14879static void
883fd55a
KS
14880dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14881 struct dwarf2_cu *cu)
6e70227d 14882{
be2daae6
TT
14883 struct decl_field fp;
14884 memset (&fp, 0, sizeof (fp));
98751a41 14885
883fd55a 14886 gdb_assert (type_can_define_types (die));
98751a41 14887
883fd55a 14888 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14889 fp.name = dwarf2_name (die, cu);
14890 fp.type = read_type_die (die, cu);
98751a41 14891
c191a687
KS
14892 /* Save accessibility. */
14893 enum dwarf_access_attribute accessibility;
14894 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14895 if (attr != NULL)
14896 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14897 else
14898 accessibility = dwarf2_default_access_attribute (die, cu);
14899 switch (accessibility)
14900 {
14901 case DW_ACCESS_public:
14902 /* The assumed value if neither private nor protected. */
14903 break;
14904 case DW_ACCESS_private:
be2daae6 14905 fp.is_private = 1;
c191a687
KS
14906 break;
14907 case DW_ACCESS_protected:
be2daae6 14908 fp.is_protected = 1;
c191a687
KS
14909 break;
14910 default:
b98664d3 14911 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14912 }
14913
883fd55a 14914 if (die->tag == DW_TAG_typedef)
be2daae6 14915 fip->typedef_field_list.push_back (fp);
883fd55a 14916 else
be2daae6 14917 fip->nested_types_list.push_back (fp);
98751a41
JK
14918}
14919
c906108c
SS
14920/* Create the vector of fields, and attach it to the type. */
14921
14922static void
fba45db2 14923dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14924 struct dwarf2_cu *cu)
c906108c
SS
14925{
14926 int nfields = fip->nfields;
14927
14928 /* Record the field count, allocate space for the array of fields,
14929 and create blank accessibility bitfields if necessary. */
14930 TYPE_NFIELDS (type) = nfields;
14931 TYPE_FIELDS (type) = (struct field *)
be2daae6 14932 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14933
b4ba55a1 14934 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14935 {
14936 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14937
14938 TYPE_FIELD_PRIVATE_BITS (type) =
14939 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14940 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14941
14942 TYPE_FIELD_PROTECTED_BITS (type) =
14943 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14944 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14945
774b6a14
TT
14946 TYPE_FIELD_IGNORE_BITS (type) =
14947 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14948 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14949 }
14950
14951 /* If the type has baseclasses, allocate and clear a bit vector for
14952 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14953 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14954 {
be2daae6 14955 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14956 unsigned char *pointer;
c906108c
SS
14957
14958 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14959 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14960 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14961 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14962 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14963 }
14964
2ddeaf8a
TT
14965 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14966 {
14967 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14968
be2daae6 14969 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14970 {
be2daae6
TT
14971 struct nextfield &field = fip->fields[index];
14972
14973 if (field.variant.is_discriminant)
2ddeaf8a 14974 di->discriminant_index = index;
be2daae6 14975 else if (field.variant.default_branch)
2ddeaf8a
TT
14976 di->default_index = index;
14977 else
be2daae6 14978 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14979 }
14980 }
14981
be2daae6
TT
14982 /* Copy the saved-up fields into the field vector. */
14983 for (int i = 0; i < nfields; ++i)
c906108c 14984 {
be2daae6
TT
14985 struct nextfield &field
14986 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14987 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14988
be2daae6
TT
14989 TYPE_FIELD (type, i) = field.field;
14990 switch (field.accessibility)
c906108c 14991 {
c5aa993b 14992 case DW_ACCESS_private:
b4ba55a1 14993 if (cu->language != language_ada)
be2daae6 14994 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14995 break;
c906108c 14996
c5aa993b 14997 case DW_ACCESS_protected:
b4ba55a1 14998 if (cu->language != language_ada)
be2daae6 14999 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15000 break;
c906108c 15001
c5aa993b
JM
15002 case DW_ACCESS_public:
15003 break;
c906108c 15004
c5aa993b
JM
15005 default:
15006 /* Unknown accessibility. Complain and treat it as public. */
15007 {
b98664d3 15008 complaint (_("unsupported accessibility %d"),
be2daae6 15009 field.accessibility);
c5aa993b
JM
15010 }
15011 break;
c906108c 15012 }
be2daae6 15013 if (i < fip->baseclasses.size ())
c906108c 15014 {
be2daae6 15015 switch (field.virtuality)
c906108c 15016 {
c5aa993b
JM
15017 case DW_VIRTUALITY_virtual:
15018 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15019 if (cu->language == language_ada)
a73c6dcd 15020 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15021 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15022 break;
c906108c
SS
15023 }
15024 }
c906108c
SS
15025 }
15026}
15027
7d27a96d
TT
15028/* Return true if this member function is a constructor, false
15029 otherwise. */
15030
15031static int
15032dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15033{
15034 const char *fieldname;
fe978cb0 15035 const char *type_name;
7d27a96d
TT
15036 int len;
15037
15038 if (die->parent == NULL)
15039 return 0;
15040
15041 if (die->parent->tag != DW_TAG_structure_type
15042 && die->parent->tag != DW_TAG_union_type
15043 && die->parent->tag != DW_TAG_class_type)
15044 return 0;
15045
15046 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15047 type_name = dwarf2_name (die->parent, cu);
15048 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15049 return 0;
15050
15051 len = strlen (fieldname);
fe978cb0
PA
15052 return (strncmp (fieldname, type_name, len) == 0
15053 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15054}
15055
e35000a7
TBA
15056/* Check if the given VALUE is a recognized enum
15057 dwarf_defaulted_attribute constant according to DWARF5 spec,
15058 Table 7.24. */
15059
15060static bool
15061is_valid_DW_AT_defaulted (ULONGEST value)
15062{
15063 switch (value)
15064 {
15065 case DW_DEFAULTED_no:
15066 case DW_DEFAULTED_in_class:
15067 case DW_DEFAULTED_out_of_class:
15068 return true;
15069 }
15070
3142e908 15071 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15072 return false;
15073}
15074
c906108c
SS
15075/* Add a member function to the proper fieldlist. */
15076
15077static void
107d2387 15078dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15079 struct type *type, struct dwarf2_cu *cu)
c906108c 15080{
518817b3 15081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15082 struct attribute *attr;
c906108c 15083 int i;
be2daae6 15084 struct fnfieldlist *flp = nullptr;
c906108c 15085 struct fn_field *fnp;
15d034d0 15086 const char *fieldname;
f792889a 15087 struct type *this_type;
60d5a603 15088 enum dwarf_access_attribute accessibility;
c906108c 15089
b4ba55a1 15090 if (cu->language == language_ada)
a73c6dcd 15091 error (_("unexpected member function in Ada type"));
b4ba55a1 15092
2df3850c 15093 /* Get name of member function. */
39cbfefa
DJ
15094 fieldname = dwarf2_name (die, cu);
15095 if (fieldname == NULL)
2df3850c 15096 return;
c906108c 15097
c906108c 15098 /* Look up member function name in fieldlist. */
be2daae6 15099 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15100 {
27bfe10e 15101 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15102 {
15103 flp = &fip->fnfieldlists[i];
15104 break;
15105 }
c906108c
SS
15106 }
15107
be2daae6
TT
15108 /* Create a new fnfieldlist if necessary. */
15109 if (flp == nullptr)
c906108c 15110 {
be2daae6
TT
15111 fip->fnfieldlists.emplace_back ();
15112 flp = &fip->fnfieldlists.back ();
c906108c 15113 flp->name = fieldname;
be2daae6 15114 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15115 }
15116
be2daae6
TT
15117 /* Create a new member function field and add it to the vector of
15118 fnfieldlists. */
15119 flp->fnfields.emplace_back ();
15120 fnp = &flp->fnfields.back ();
3da10d80
KS
15121
15122 /* Delay processing of the physname until later. */
9c37b5ae 15123 if (cu->language == language_cplus)
be2daae6
TT
15124 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15125 die, cu);
3da10d80
KS
15126 else
15127 {
1d06ead6 15128 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15129 fnp->physname = physname ? physname : "";
15130 }
15131
c906108c 15132 fnp->type = alloc_type (objfile);
f792889a
DJ
15133 this_type = read_type_die (die, cu);
15134 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15135 {
f792889a 15136 int nparams = TYPE_NFIELDS (this_type);
c906108c 15137
f792889a 15138 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15139 of the method itself (TYPE_CODE_METHOD). */
15140 smash_to_method_type (fnp->type, type,
f792889a
DJ
15141 TYPE_TARGET_TYPE (this_type),
15142 TYPE_FIELDS (this_type),
15143 TYPE_NFIELDS (this_type),
15144 TYPE_VARARGS (this_type));
c906108c
SS
15145
15146 /* Handle static member functions.
c5aa993b 15147 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15148 member functions. G++ helps GDB by marking the first
15149 parameter for non-static member functions (which is the this
15150 pointer) as artificial. We obtain this information from
15151 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15152 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15153 fnp->voffset = VOFFSET_STATIC;
15154 }
15155 else
b98664d3 15156 complaint (_("member function type missing for '%s'"),
3da10d80 15157 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15158
15159 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15160 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15161 fnp->fcontext = die_containing_type (die, cu);
c906108c 15162
3e43a32a
MS
15163 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15164 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15165
15166 /* Get accessibility. */
e142c38c 15167 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15168 if (attr != nullptr)
aead7601 15169 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15170 else
15171 accessibility = dwarf2_default_access_attribute (die, cu);
15172 switch (accessibility)
c906108c 15173 {
60d5a603
JK
15174 case DW_ACCESS_private:
15175 fnp->is_private = 1;
15176 break;
15177 case DW_ACCESS_protected:
15178 fnp->is_protected = 1;
15179 break;
c906108c
SS
15180 }
15181
b02dede2 15182 /* Check for artificial methods. */
e142c38c 15183 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15184 if (attr && DW_UNSND (attr) != 0)
15185 fnp->is_artificial = 1;
15186
e35000a7
TBA
15187 /* Check for defaulted methods. */
15188 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15189 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15190 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15191
15192 /* Check for deleted methods. */
15193 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15194 if (attr != nullptr && DW_UNSND (attr) != 0)
15195 fnp->is_deleted = 1;
15196
7d27a96d
TT
15197 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15198
0d564a31 15199 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15200 function. For older versions of GCC, this is an offset in the
15201 appropriate virtual table, as specified by DW_AT_containing_type.
15202 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15203 to the object address. */
15204
e142c38c 15205 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15206 if (attr != nullptr)
8e19ed76 15207 {
4fc6c0d5 15208 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15209 {
aec5aa8b
TT
15210 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15211 {
15212 /* Old-style GCC. */
15213 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15214 }
15215 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15216 || (DW_BLOCK (attr)->size > 1
15217 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15218 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15219 {
aec5aa8b
TT
15220 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15221 if ((fnp->voffset % cu->header.addr_size) != 0)
15222 dwarf2_complex_location_expr_complaint ();
15223 else
15224 fnp->voffset /= cu->header.addr_size;
15225 fnp->voffset += 2;
15226 }
15227 else
15228 dwarf2_complex_location_expr_complaint ();
15229
15230 if (!fnp->fcontext)
7e993ebf
KS
15231 {
15232 /* If there is no `this' field and no DW_AT_containing_type,
15233 we cannot actually find a base class context for the
15234 vtable! */
15235 if (TYPE_NFIELDS (this_type) == 0
15236 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15237 {
b98664d3 15238 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15239 "function \"%s\" (offset %s)"),
15240 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15241 }
15242 else
15243 {
15244 fnp->fcontext
15245 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15246 }
15247 }
aec5aa8b 15248 }
cd6c91b4 15249 else if (attr->form_is_section_offset ())
8e19ed76 15250 {
4d3c2250 15251 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15252 }
15253 else
15254 {
4d3c2250
KB
15255 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15256 fieldname);
8e19ed76 15257 }
0d564a31 15258 }
d48cc9dd
DJ
15259 else
15260 {
15261 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15262 if (attr && DW_UNSND (attr))
15263 {
15264 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15265 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15266 "but the vtable offset is not specified"),
9d8780f0 15267 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15268 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15269 TYPE_CPLUS_DYNAMIC (type) = 1;
15270 }
15271 }
c906108c
SS
15272}
15273
15274/* Create the vector of member function fields, and attach it to the type. */
15275
15276static void
fba45db2 15277dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15278 struct dwarf2_cu *cu)
c906108c 15279{
b4ba55a1 15280 if (cu->language == language_ada)
a73c6dcd 15281 error (_("unexpected member functions in Ada type"));
b4ba55a1 15282
c906108c
SS
15283 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15284 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15285 TYPE_ALLOC (type,
15286 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15287
be2daae6 15288 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15289 {
be2daae6 15290 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15291 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15292
be2daae6
TT
15293 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15294 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15295 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15296 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15297
15298 for (int k = 0; k < nf.fnfields.size (); ++k)
15299 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15300 }
15301
be2daae6 15302 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15303}
15304
1168df01
JB
15305/* Returns non-zero if NAME is the name of a vtable member in CU's
15306 language, zero otherwise. */
15307static int
15308is_vtable_name (const char *name, struct dwarf2_cu *cu)
15309{
15310 static const char vptr[] = "_vptr";
15311
9c37b5ae
TT
15312 /* Look for the C++ form of the vtable. */
15313 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15314 return 1;
15315
15316 return 0;
15317}
15318
c0dd20ea 15319/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15320 functions, with the ABI-specified layout. If TYPE describes
15321 such a structure, smash it into a member function type.
61049d3b
DJ
15322
15323 GCC shouldn't do this; it should just output pointer to member DIEs.
15324 This is GCC PR debug/28767. */
c0dd20ea 15325
0b92b5bb
TT
15326static void
15327quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15328{
09e2d7c7 15329 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15330
15331 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15332 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15333 return;
c0dd20ea
DJ
15334
15335 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15336 if (TYPE_FIELD_NAME (type, 0) == NULL
15337 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15338 || TYPE_FIELD_NAME (type, 1) == NULL
15339 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15340 return;
c0dd20ea
DJ
15341
15342 /* Find the type of the method. */
0b92b5bb 15343 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15344 if (pfn_type == NULL
15345 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15346 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15347 return;
c0dd20ea
DJ
15348
15349 /* Look for the "this" argument. */
15350 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15351 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15352 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15353 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15354 return;
c0dd20ea 15355
09e2d7c7 15356 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15357 new_type = alloc_type (objfile);
09e2d7c7 15358 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15359 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15360 TYPE_VARARGS (pfn_type));
0b92b5bb 15361 smash_to_methodptr_type (type, new_type);
c0dd20ea 15362}
1168df01 15363
2b4424c3
TT
15364/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15365 appropriate error checking and issuing complaints if there is a
15366 problem. */
15367
15368static ULONGEST
15369get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15370{
15371 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15372
15373 if (attr == nullptr)
15374 return 0;
15375
cd6c91b4 15376 if (!attr->form_is_constant ())
2b4424c3 15377 {
b98664d3 15378 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15379 " - DIE at %s [in module %s]"),
15380 sect_offset_str (die->sect_off),
15381 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15382 return 0;
15383 }
15384
15385 ULONGEST align;
15386 if (attr->form == DW_FORM_sdata)
15387 {
15388 LONGEST val = DW_SND (attr);
15389 if (val < 0)
15390 {
b98664d3 15391 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15392 " - DIE at %s [in module %s]"),
15393 sect_offset_str (die->sect_off),
15394 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15395 return 0;
15396 }
15397 align = val;
15398 }
15399 else
15400 align = DW_UNSND (attr);
15401
15402 if (align == 0)
15403 {
b98664d3 15404 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15405 " - DIE at %s [in module %s]"),
15406 sect_offset_str (die->sect_off),
15407 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15408 return 0;
15409 }
15410 if ((align & (align - 1)) != 0)
15411 {
b98664d3 15412 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15413 " - DIE at %s [in module %s]"),
15414 sect_offset_str (die->sect_off),
15415 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15416 return 0;
15417 }
15418
15419 return align;
15420}
15421
15422/* If the DIE has a DW_AT_alignment attribute, use its value to set
15423 the alignment for TYPE. */
15424
15425static void
15426maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15427 struct type *type)
15428{
15429 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15430 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15431 " - DIE at %s [in module %s]"),
15432 sect_offset_str (die->sect_off),
15433 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15434}
685b1105 15435
e35000a7
TBA
15436/* Check if the given VALUE is a valid enum dwarf_calling_convention
15437 constant for a type, according to DWARF5 spec, Table 5.5. */
15438
15439static bool
15440is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15441{
15442 switch (value)
15443 {
15444 case DW_CC_normal:
15445 case DW_CC_pass_by_reference:
15446 case DW_CC_pass_by_value:
15447 return true;
15448
15449 default:
15450 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15451 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15452 return false;
15453 }
15454}
15455
d0922fcf
TBA
15456/* Check if the given VALUE is a valid enum dwarf_calling_convention
15457 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15458 also according to GNU-specific values (see include/dwarf2.h). */
15459
15460static bool
15461is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15462{
15463 switch (value)
15464 {
15465 case DW_CC_normal:
15466 case DW_CC_program:
15467 case DW_CC_nocall:
15468 return true;
15469
15470 case DW_CC_GNU_renesas_sh:
15471 case DW_CC_GNU_borland_fastcall_i386:
15472 case DW_CC_GDB_IBM_OpenCL:
15473 return true;
15474
15475 default:
15476 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15477 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15478 return false;
15479 }
15480}
15481
c906108c 15482/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15483 (definition) to create a type for the structure or union. Fill in
15484 the type's name and general properties; the members will not be
83655187
DE
15485 processed until process_structure_scope. A symbol table entry for
15486 the type will also not be done until process_structure_scope (assuming
15487 the type has a name).
c906108c 15488
c767944b
DJ
15489 NOTE: we need to call these functions regardless of whether or not the
15490 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15491 structure or union. This gets the type entered into our set of
83655187 15492 user defined types. */
c906108c 15493
f792889a 15494static struct type *
134d01f1 15495read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15496{
518817b3 15497 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15498 struct type *type;
15499 struct attribute *attr;
15d034d0 15500 const char *name;
c906108c 15501
348e048f
DE
15502 /* If the definition of this type lives in .debug_types, read that type.
15503 Don't follow DW_AT_specification though, that will take us back up
15504 the chain and we want to go down. */
45e58e77 15505 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15506 if (attr != nullptr)
348e048f 15507 {
ac9ec31b 15508 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15509
ac9ec31b 15510 /* The type's CU may not be the same as CU.
02142a6c 15511 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15512 return set_die_type (die, type, cu);
15513 }
15514
c0dd20ea 15515 type = alloc_type (objfile);
c906108c 15516 INIT_CPLUS_SPECIFIC (type);
93311388 15517
39cbfefa
DJ
15518 name = dwarf2_name (die, cu);
15519 if (name != NULL)
c906108c 15520 {
987504bb 15521 if (cu->language == language_cplus
c44af4eb
TT
15522 || cu->language == language_d
15523 || cu->language == language_rust)
63d06c5c 15524 {
15d034d0 15525 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15526
15527 /* dwarf2_full_name might have already finished building the DIE's
15528 type. If so, there is no need to continue. */
15529 if (get_die_type (die, cu) != NULL)
15530 return get_die_type (die, cu);
15531
e86ca25f 15532 TYPE_NAME (type) = full_name;
63d06c5c
DC
15533 }
15534 else
15535 {
d8151005
DJ
15536 /* The name is already allocated along with this objfile, so
15537 we don't need to duplicate it for the type. */
e86ca25f 15538 TYPE_NAME (type) = name;
63d06c5c 15539 }
c906108c
SS
15540 }
15541
15542 if (die->tag == DW_TAG_structure_type)
15543 {
15544 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15545 }
15546 else if (die->tag == DW_TAG_union_type)
15547 {
15548 TYPE_CODE (type) = TYPE_CODE_UNION;
15549 }
2ddeaf8a
TT
15550 else if (die->tag == DW_TAG_variant_part)
15551 {
15552 TYPE_CODE (type) = TYPE_CODE_UNION;
15553 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15554 }
c906108c
SS
15555 else
15556 {
4753d33b 15557 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15558 }
15559
0cc2414c
TT
15560 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15561 TYPE_DECLARED_CLASS (type) = 1;
15562
e35000a7
TBA
15563 /* Store the calling convention in the type if it's available in
15564 the die. Otherwise the calling convention remains set to
15565 the default value DW_CC_normal. */
15566 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15567 if (attr != nullptr
15568 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15569 {
15570 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15571 TYPE_CPLUS_CALLING_CONVENTION (type)
15572 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15573 }
15574
e142c38c 15575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15576 if (attr != nullptr)
c906108c 15577 {
cd6c91b4 15578 if (attr->form_is_constant ())
155bfbd3
JB
15579 TYPE_LENGTH (type) = DW_UNSND (attr);
15580 else
15581 {
15582 /* For the moment, dynamic type sizes are not supported
15583 by GDB's struct type. The actual size is determined
15584 on-demand when resolving the type of a given object,
15585 so set the type's length to zero for now. Otherwise,
15586 we record an expression as the length, and that expression
15587 could lead to a very large value, which could eventually
15588 lead to us trying to allocate that much memory when creating
15589 a value of that type. */
15590 TYPE_LENGTH (type) = 0;
15591 }
c906108c
SS
15592 }
15593 else
15594 {
15595 TYPE_LENGTH (type) = 0;
15596 }
15597
2b4424c3
TT
15598 maybe_set_alignment (cu, die, type);
15599
5230b05a 15600 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15601 {
5230b05a
WT
15602 /* ICC<14 does not output the required DW_AT_declaration on
15603 incomplete types, but gives them a size of zero. */
422b1cb0 15604 TYPE_STUB (type) = 1;
685b1105
JK
15605 }
15606 else
15607 TYPE_STUB_SUPPORTED (type) = 1;
15608
dc718098 15609 if (die_is_declaration (die, cu))
876cecd0 15610 TYPE_STUB (type) = 1;
a6c727b2
DJ
15611 else if (attr == NULL && die->child == NULL
15612 && producer_is_realview (cu->producer))
15613 /* RealView does not output the required DW_AT_declaration
15614 on incomplete types. */
15615 TYPE_STUB (type) = 1;
dc718098 15616
c906108c
SS
15617 /* We need to add the type field to the die immediately so we don't
15618 infinitely recurse when dealing with pointers to the structure
0963b4bd 15619 type within the structure itself. */
1c379e20 15620 set_die_type (die, type, cu);
c906108c 15621
7e314c57
JK
15622 /* set_die_type should be already done. */
15623 set_descriptive_type (type, die, cu);
15624
c767944b
DJ
15625 return type;
15626}
15627
2ddeaf8a
TT
15628/* A helper for process_structure_scope that handles a single member
15629 DIE. */
15630
15631static void
15632handle_struct_member_die (struct die_info *child_die, struct type *type,
15633 struct field_info *fi,
15634 std::vector<struct symbol *> *template_args,
15635 struct dwarf2_cu *cu)
15636{
15637 if (child_die->tag == DW_TAG_member
15638 || child_die->tag == DW_TAG_variable
15639 || child_die->tag == DW_TAG_variant_part)
15640 {
15641 /* NOTE: carlton/2002-11-05: A C++ static data member
15642 should be a DW_TAG_member that is a declaration, but
15643 all versions of G++ as of this writing (so through at
15644 least 3.2.1) incorrectly generate DW_TAG_variable
15645 tags for them instead. */
15646 dwarf2_add_field (fi, child_die, cu);
15647 }
15648 else if (child_die->tag == DW_TAG_subprogram)
15649 {
15650 /* Rust doesn't have member functions in the C++ sense.
15651 However, it does emit ordinary functions as children
15652 of a struct DIE. */
15653 if (cu->language == language_rust)
15654 read_func_scope (child_die, cu);
15655 else
15656 {
15657 /* C++ member function. */
15658 dwarf2_add_member_fn (fi, child_die, type, cu);
15659 }
15660 }
15661 else if (child_die->tag == DW_TAG_inheritance)
15662 {
15663 /* C++ base class field. */
15664 dwarf2_add_field (fi, child_die, cu);
15665 }
15666 else if (type_can_define_types (child_die))
15667 dwarf2_add_type_defn (fi, child_die, cu);
15668 else if (child_die->tag == DW_TAG_template_type_param
15669 || child_die->tag == DW_TAG_template_value_param)
15670 {
15671 struct symbol *arg = new_symbol (child_die, NULL, cu);
15672
15673 if (arg != NULL)
15674 template_args->push_back (arg);
15675 }
15676 else if (child_die->tag == DW_TAG_variant)
15677 {
15678 /* In a variant we want to get the discriminant and also add a
15679 field for our sole member child. */
15680 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15681
bde09ab7 15682 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15683 variant_child != NULL;
15684 variant_child = sibling_die (variant_child))
15685 {
15686 if (variant_child->tag == DW_TAG_member)
15687 {
15688 handle_struct_member_die (variant_child, type, fi,
15689 template_args, cu);
15690 /* Only handle the one. */
15691 break;
15692 }
15693 }
15694
15695 /* We don't handle this but we might as well report it if we see
15696 it. */
15697 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15698 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15699 " - DIE at %s [in module %s]"),
15700 sect_offset_str (child_die->sect_off),
15701 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15702
15703 /* The first field was just added, so we can stash the
15704 discriminant there. */
be2daae6 15705 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15706 if (discr == NULL)
be2daae6 15707 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15708 else
be2daae6 15709 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15710 }
15711}
15712
c767944b
DJ
15713/* Finish creating a structure or union type, including filling in
15714 its members and creating a symbol for it. */
15715
15716static void
15717process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15718{
518817b3 15719 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15720 struct die_info *child_die;
c767944b
DJ
15721 struct type *type;
15722
15723 type = get_die_type (die, cu);
15724 if (type == NULL)
15725 type = read_structure_type (die, cu);
15726
2ddeaf8a
TT
15727 /* When reading a DW_TAG_variant_part, we need to notice when we
15728 read the discriminant member, so we can record it later in the
15729 discriminant_info. */
15730 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15731 sect_offset discr_offset {};
3e1d3d8c 15732 bool has_template_parameters = false;
2ddeaf8a
TT
15733
15734 if (is_variant_part)
15735 {
15736 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15737 if (discr == NULL)
15738 {
15739 /* Maybe it's a univariant form, an extension we support.
15740 In this case arrange not to check the offset. */
15741 is_variant_part = false;
15742 }
cd6c91b4 15743 else if (discr->form_is_ref ())
2ddeaf8a
TT
15744 {
15745 struct dwarf2_cu *target_cu = cu;
15746 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15747
15748 discr_offset = target_die->sect_off;
15749 }
15750 else
15751 {
b98664d3 15752 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15753 " - DIE at %s [in module %s]"),
15754 sect_offset_str (die->sect_off),
15755 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15756 is_variant_part = false;
15757 }
15758 }
15759
e142c38c 15760 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15761 {
15762 struct field_info fi;
2f4732b0 15763 std::vector<struct symbol *> template_args;
c906108c 15764
639d11d3 15765 child_die = die->child;
c906108c
SS
15766
15767 while (child_die && child_die->tag)
15768 {
2ddeaf8a 15769 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15770
2ddeaf8a 15771 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15772 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15773
c906108c
SS
15774 child_die = sibling_die (child_die);
15775 }
15776
34eaf542 15777 /* Attach template arguments to type. */
2f4732b0 15778 if (!template_args.empty ())
34eaf542 15779 {
3e1d3d8c 15780 has_template_parameters = true;
34eaf542 15781 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15782 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15783 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15784 = XOBNEWVEC (&objfile->objfile_obstack,
15785 struct symbol *,
15786 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15787 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15788 template_args.data (),
34eaf542
TT
15789 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15790 * sizeof (struct symbol *)));
34eaf542
TT
15791 }
15792
c906108c
SS
15793 /* Attach fields and member functions to the type. */
15794 if (fi.nfields)
e7c27a73 15795 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15796 if (!fi.fnfieldlists.empty ())
c906108c 15797 {
e7c27a73 15798 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15799
c5aa993b 15800 /* Get the type which refers to the base class (possibly this
c906108c 15801 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15802 class from the DW_AT_containing_type attribute. This use of
15803 DW_AT_containing_type is a GNU extension. */
c906108c 15804
e142c38c 15805 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15806 {
e7c27a73 15807 struct type *t = die_containing_type (die, cu);
c906108c 15808
ae6ae975 15809 set_type_vptr_basetype (type, t);
c906108c
SS
15810 if (type == t)
15811 {
c906108c
SS
15812 int i;
15813
15814 /* Our own class provides vtbl ptr. */
15815 for (i = TYPE_NFIELDS (t) - 1;
15816 i >= TYPE_N_BASECLASSES (t);
15817 --i)
15818 {
0d5cff50 15819 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15820
1168df01 15821 if (is_vtable_name (fieldname, cu))
c906108c 15822 {
ae6ae975 15823 set_type_vptr_fieldno (type, i);
c906108c
SS
15824 break;
15825 }
15826 }
15827
15828 /* Complain if virtual function table field not found. */
15829 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15830 complaint (_("virtual function table pointer "
3e43a32a 15831 "not found when defining class '%s'"),
e86ca25f 15832 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15833 }
15834 else
15835 {
ae6ae975 15836 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15837 }
15838 }
f6235d4c 15839 else if (cu->producer
61012eef 15840 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15841 {
15842 /* The IBM XLC compiler does not provide direct indication
15843 of the containing type, but the vtable pointer is
15844 always named __vfp. */
15845
15846 int i;
15847
15848 for (i = TYPE_NFIELDS (type) - 1;
15849 i >= TYPE_N_BASECLASSES (type);
15850 --i)
15851 {
15852 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15853 {
ae6ae975
DE
15854 set_type_vptr_fieldno (type, i);
15855 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15856 break;
15857 }
15858 }
15859 }
c906108c 15860 }
98751a41
JK
15861
15862 /* Copy fi.typedef_field_list linked list elements content into the
15863 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15864 if (!fi.typedef_field_list.empty ())
98751a41 15865 {
be2daae6 15866 int count = fi.typedef_field_list.size ();
98751a41 15867
a0d7a4ff 15868 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15869 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15870 = ((struct decl_field *)
be2daae6
TT
15871 TYPE_ALLOC (type,
15872 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15873 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15874
be2daae6
TT
15875 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15876 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15877 }
c767944b 15878
883fd55a
KS
15879 /* Copy fi.nested_types_list linked list elements content into the
15880 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15881 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15882 {
be2daae6 15883 int count = fi.nested_types_list.size ();
883fd55a
KS
15884
15885 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15886 TYPE_NESTED_TYPES_ARRAY (type)
15887 = ((struct decl_field *)
be2daae6
TT
15888 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15889 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15890
be2daae6
TT
15891 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15892 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15893 }
c906108c 15894 }
63d06c5c 15895
bb5ed363 15896 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15897 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15898 cu->rust_unions.push_back (type);
0b92b5bb 15899
90aeadfc
DC
15900 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15901 snapshots) has been known to create a die giving a declaration
15902 for a class that has, as a child, a die giving a definition for a
15903 nested class. So we have to process our children even if the
15904 current die is a declaration. Normally, of course, a declaration
15905 won't have any children at all. */
134d01f1 15906
ca040673
DE
15907 child_die = die->child;
15908
90aeadfc
DC
15909 while (child_die != NULL && child_die->tag)
15910 {
15911 if (child_die->tag == DW_TAG_member
15912 || child_die->tag == DW_TAG_variable
34eaf542
TT
15913 || child_die->tag == DW_TAG_inheritance
15914 || child_die->tag == DW_TAG_template_value_param
15915 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15916 {
90aeadfc 15917 /* Do nothing. */
134d01f1 15918 }
90aeadfc
DC
15919 else
15920 process_die (child_die, cu);
134d01f1 15921
90aeadfc 15922 child_die = sibling_die (child_die);
134d01f1
DJ
15923 }
15924
fa4028e9
JB
15925 /* Do not consider external references. According to the DWARF standard,
15926 these DIEs are identified by the fact that they have no byte_size
15927 attribute, and a declaration attribute. */
15928 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15929 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15930 {
15931 struct symbol *sym = new_symbol (die, type, cu);
15932
15933 if (has_template_parameters)
15934 {
a776957c
TT
15935 struct symtab *symtab;
15936 if (sym != nullptr)
15937 symtab = symbol_symtab (sym);
15938 else if (cu->line_header != nullptr)
15939 {
15940 /* Any related symtab will do. */
15941 symtab
7ba99d21 15942 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15943 }
15944 else
15945 {
15946 symtab = nullptr;
15947 complaint (_("could not find suitable "
15948 "symtab for template parameter"
15949 " - DIE at %s [in module %s]"),
15950 sect_offset_str (die->sect_off),
15951 objfile_name (objfile));
15952 }
15953
15954 if (symtab != nullptr)
15955 {
15956 /* Make sure that the symtab is set on the new symbols.
15957 Even though they don't appear in this symtab directly,
15958 other parts of gdb assume that symbols do, and this is
15959 reasonably true. */
15960 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15961 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15962 }
3e1d3d8c
TT
15963 }
15964 }
134d01f1
DJ
15965}
15966
55426c9d
JB
15967/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15968 update TYPE using some information only available in DIE's children. */
15969
15970static void
15971update_enumeration_type_from_children (struct die_info *die,
15972 struct type *type,
15973 struct dwarf2_cu *cu)
15974{
60f7655a 15975 struct die_info *child_die;
55426c9d
JB
15976 int unsigned_enum = 1;
15977 int flag_enum = 1;
15978 ULONGEST mask = 0;
55426c9d 15979
8268c778 15980 auto_obstack obstack;
55426c9d 15981
60f7655a
DE
15982 for (child_die = die->child;
15983 child_die != NULL && child_die->tag;
15984 child_die = sibling_die (child_die))
55426c9d
JB
15985 {
15986 struct attribute *attr;
15987 LONGEST value;
15988 const gdb_byte *bytes;
15989 struct dwarf2_locexpr_baton *baton;
15990 const char *name;
60f7655a 15991
55426c9d
JB
15992 if (child_die->tag != DW_TAG_enumerator)
15993 continue;
15994
15995 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15996 if (attr == NULL)
15997 continue;
15998
15999 name = dwarf2_name (child_die, cu);
16000 if (name == NULL)
16001 name = "<anonymous enumerator>";
16002
16003 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16004 &value, &bytes, &baton);
16005 if (value < 0)
16006 {
16007 unsigned_enum = 0;
16008 flag_enum = 0;
16009 }
16010 else if ((mask & value) != 0)
16011 flag_enum = 0;
16012 else
16013 mask |= value;
16014
16015 /* If we already know that the enum type is neither unsigned, nor
16016 a flag type, no need to look at the rest of the enumerates. */
16017 if (!unsigned_enum && !flag_enum)
16018 break;
55426c9d
JB
16019 }
16020
16021 if (unsigned_enum)
16022 TYPE_UNSIGNED (type) = 1;
16023 if (flag_enum)
16024 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16025}
16026
134d01f1
DJ
16027/* Given a DW_AT_enumeration_type die, set its type. We do not
16028 complete the type's fields yet, or create any symbols. */
c906108c 16029
f792889a 16030static struct type *
134d01f1 16031read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16032{
518817b3 16033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16034 struct type *type;
c906108c 16035 struct attribute *attr;
0114d602 16036 const char *name;
134d01f1 16037
348e048f
DE
16038 /* If the definition of this type lives in .debug_types, read that type.
16039 Don't follow DW_AT_specification though, that will take us back up
16040 the chain and we want to go down. */
45e58e77 16041 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16042 if (attr != nullptr)
348e048f 16043 {
ac9ec31b 16044 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16045
ac9ec31b 16046 /* The type's CU may not be the same as CU.
02142a6c 16047 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16048 return set_die_type (die, type, cu);
16049 }
16050
c906108c
SS
16051 type = alloc_type (objfile);
16052
16053 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16054 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16055 if (name != NULL)
e86ca25f 16056 TYPE_NAME (type) = name;
c906108c 16057
0626fc76
TT
16058 attr = dwarf2_attr (die, DW_AT_type, cu);
16059 if (attr != NULL)
16060 {
16061 struct type *underlying_type = die_type (die, cu);
16062
16063 TYPE_TARGET_TYPE (type) = underlying_type;
16064 }
16065
e142c38c 16066 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16067 if (attr != nullptr)
c906108c
SS
16068 {
16069 TYPE_LENGTH (type) = DW_UNSND (attr);
16070 }
16071 else
16072 {
16073 TYPE_LENGTH (type) = 0;
16074 }
16075
2b4424c3
TT
16076 maybe_set_alignment (cu, die, type);
16077
137033e9
JB
16078 /* The enumeration DIE can be incomplete. In Ada, any type can be
16079 declared as private in the package spec, and then defined only
16080 inside the package body. Such types are known as Taft Amendment
16081 Types. When another package uses such a type, an incomplete DIE
16082 may be generated by the compiler. */
02eb380e 16083 if (die_is_declaration (die, cu))
876cecd0 16084 TYPE_STUB (type) = 1;
02eb380e 16085
0626fc76
TT
16086 /* Finish the creation of this type by using the enum's children.
16087 We must call this even when the underlying type has been provided
16088 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16089 update_enumeration_type_from_children (die, type, cu);
16090
0626fc76
TT
16091 /* If this type has an underlying type that is not a stub, then we
16092 may use its attributes. We always use the "unsigned" attribute
16093 in this situation, because ordinarily we guess whether the type
16094 is unsigned -- but the guess can be wrong and the underlying type
16095 can tell us the reality. However, we defer to a local size
16096 attribute if one exists, because this lets the compiler override
16097 the underlying type if needed. */
16098 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16099 {
16100 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16101 if (TYPE_LENGTH (type) == 0)
16102 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16103 if (TYPE_RAW_ALIGN (type) == 0
16104 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16105 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16106 }
16107
3d567982
TT
16108 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16109
f792889a 16110 return set_die_type (die, type, cu);
134d01f1
DJ
16111}
16112
16113/* Given a pointer to a die which begins an enumeration, process all
16114 the dies that define the members of the enumeration, and create the
16115 symbol for the enumeration type.
16116
16117 NOTE: We reverse the order of the element list. */
16118
16119static void
16120process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16121{
f792889a 16122 struct type *this_type;
134d01f1 16123
f792889a
DJ
16124 this_type = get_die_type (die, cu);
16125 if (this_type == NULL)
16126 this_type = read_enumeration_type (die, cu);
9dc481d3 16127
639d11d3 16128 if (die->child != NULL)
c906108c 16129 {
9dc481d3
DE
16130 struct die_info *child_die;
16131 struct symbol *sym;
43816ebc 16132 std::vector<struct field> fields;
15d034d0 16133 const char *name;
9dc481d3 16134
639d11d3 16135 child_die = die->child;
c906108c
SS
16136 while (child_die && child_die->tag)
16137 {
16138 if (child_die->tag != DW_TAG_enumerator)
16139 {
e7c27a73 16140 process_die (child_die, cu);
c906108c
SS
16141 }
16142 else
16143 {
39cbfefa
DJ
16144 name = dwarf2_name (child_die, cu);
16145 if (name)
c906108c 16146 {
f792889a 16147 sym = new_symbol (child_die, this_type, cu);
c906108c 16148
43816ebc
TT
16149 fields.emplace_back ();
16150 struct field &field = fields.back ();
c906108c 16151
43816ebc
TT
16152 FIELD_NAME (field) = sym->linkage_name ();
16153 FIELD_TYPE (field) = NULL;
16154 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16155 FIELD_BITSIZE (field) = 0;
c906108c
SS
16156 }
16157 }
16158
16159 child_die = sibling_die (child_die);
16160 }
16161
43816ebc 16162 if (!fields.empty ())
c906108c 16163 {
43816ebc 16164 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16165 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16166 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16167 memcpy (TYPE_FIELDS (this_type), fields.data (),
16168 sizeof (struct field) * fields.size ());
c906108c 16169 }
c906108c 16170 }
134d01f1 16171
6c83ed52
TT
16172 /* If we are reading an enum from a .debug_types unit, and the enum
16173 is a declaration, and the enum is not the signatured type in the
16174 unit, then we do not want to add a symbol for it. Adding a
16175 symbol would in some cases obscure the true definition of the
16176 enum, giving users an incomplete type when the definition is
16177 actually available. Note that we do not want to do this for all
16178 enums which are just declarations, because C++0x allows forward
16179 enum declarations. */
3019eac3 16180 if (cu->per_cu->is_debug_types
6c83ed52
TT
16181 && die_is_declaration (die, cu))
16182 {
52dc124a 16183 struct signatured_type *sig_type;
6c83ed52 16184
c0f78cd4 16185 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16186 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16187 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16188 return;
16189 }
16190
f792889a 16191 new_symbol (die, this_type, cu);
c906108c
SS
16192}
16193
16194/* Extract all information from a DW_TAG_array_type DIE and put it in
16195 the DIE's type field. For now, this only handles one dimensional
16196 arrays. */
16197
f792889a 16198static struct type *
e7c27a73 16199read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16200{
518817b3 16201 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16202 struct die_info *child_die;
7e314c57 16203 struct type *type;
c906108c 16204 struct type *element_type, *range_type, *index_type;
c906108c 16205 struct attribute *attr;
15d034d0 16206 const char *name;
a405673c 16207 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16208 unsigned int bit_stride = 0;
c906108c 16209
e7c27a73 16210 element_type = die_type (die, cu);
c906108c 16211
7e314c57
JK
16212 /* The die_type call above may have already set the type for this DIE. */
16213 type = get_die_type (die, cu);
16214 if (type)
16215 return type;
16216
dc53a7ad
JB
16217 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16218 if (attr != NULL)
a405673c
JB
16219 {
16220 int stride_ok;
9a49df9d
AB
16221 struct type *prop_type
16222 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16223
16224 byte_stride_prop
16225 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16226 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16227 prop_type);
a405673c
JB
16228 if (!stride_ok)
16229 {
b98664d3 16230 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16231 " - DIE at %s [in module %s]"),
16232 sect_offset_str (die->sect_off),
518817b3 16233 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16234 /* Ignore this attribute. We will likely not be able to print
16235 arrays of this type correctly, but there is little we can do
16236 to help if we cannot read the attribute's value. */
16237 byte_stride_prop = NULL;
16238 }
16239 }
dc53a7ad
JB
16240
16241 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16242 if (attr != NULL)
16243 bit_stride = DW_UNSND (attr);
16244
c906108c
SS
16245 /* Irix 6.2 native cc creates array types without children for
16246 arrays with unspecified length. */
639d11d3 16247 if (die->child == NULL)
c906108c 16248 {
46bf5051 16249 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16250 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16251 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16252 byte_stride_prop, bit_stride);
f792889a 16253 return set_die_type (die, type, cu);
c906108c
SS
16254 }
16255
791afaa2 16256 std::vector<struct type *> range_types;
639d11d3 16257 child_die = die->child;
c906108c
SS
16258 while (child_die && child_die->tag)
16259 {
16260 if (child_die->tag == DW_TAG_subrange_type)
16261 {
f792889a 16262 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16263
f792889a 16264 if (child_type != NULL)
a02abb62 16265 {
0963b4bd
MS
16266 /* The range type was succesfully read. Save it for the
16267 array type creation. */
791afaa2 16268 range_types.push_back (child_type);
a02abb62 16269 }
c906108c
SS
16270 }
16271 child_die = sibling_die (child_die);
16272 }
16273
16274 /* Dwarf2 dimensions are output from left to right, create the
16275 necessary array types in backwards order. */
7ca2d3a3 16276
c906108c 16277 type = element_type;
7ca2d3a3
DL
16278
16279 if (read_array_order (die, cu) == DW_ORD_col_major)
16280 {
16281 int i = 0;
9a619af0 16282
791afaa2 16283 while (i < range_types.size ())
dc53a7ad 16284 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16285 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16286 }
16287 else
16288 {
791afaa2 16289 size_t ndim = range_types.size ();
7ca2d3a3 16290 while (ndim-- > 0)
dc53a7ad 16291 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16292 byte_stride_prop, bit_stride);
7ca2d3a3 16293 }
c906108c 16294
f5f8a009
EZ
16295 /* Understand Dwarf2 support for vector types (like they occur on
16296 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16297 array type. This is not part of the Dwarf2/3 standard yet, but a
16298 custom vendor extension. The main difference between a regular
16299 array and the vector variant is that vectors are passed by value
16300 to functions. */
e142c38c 16301 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16302 if (attr != nullptr)
ea37ba09 16303 make_vector_type (type);
f5f8a009 16304
dbc98a8b
KW
16305 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16306 implementation may choose to implement triple vectors using this
16307 attribute. */
16308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16309 if (attr != nullptr)
dbc98a8b
KW
16310 {
16311 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16312 TYPE_LENGTH (type) = DW_UNSND (attr);
16313 else
b98664d3 16314 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16315 "than the total size of elements"));
dbc98a8b
KW
16316 }
16317
39cbfefa
DJ
16318 name = dwarf2_name (die, cu);
16319 if (name)
16320 TYPE_NAME (type) = name;
6e70227d 16321
2b4424c3
TT
16322 maybe_set_alignment (cu, die, type);
16323
0963b4bd 16324 /* Install the type in the die. */
7e314c57
JK
16325 set_die_type (die, type, cu);
16326
16327 /* set_die_type should be already done. */
b4ba55a1
JB
16328 set_descriptive_type (type, die, cu);
16329
7e314c57 16330 return type;
c906108c
SS
16331}
16332
7ca2d3a3 16333static enum dwarf_array_dim_ordering
6e70227d 16334read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16335{
16336 struct attribute *attr;
16337
16338 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16339
435d3d88 16340 if (attr != nullptr)
aead7601 16341 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16342
0963b4bd
MS
16343 /* GNU F77 is a special case, as at 08/2004 array type info is the
16344 opposite order to the dwarf2 specification, but data is still
16345 laid out as per normal fortran.
7ca2d3a3 16346
0963b4bd
MS
16347 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16348 version checking. */
7ca2d3a3 16349
905e0470
PM
16350 if (cu->language == language_fortran
16351 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16352 {
16353 return DW_ORD_row_major;
16354 }
16355
6e70227d 16356 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16357 {
16358 case array_column_major:
16359 return DW_ORD_col_major;
16360 case array_row_major:
16361 default:
16362 return DW_ORD_row_major;
16363 };
16364}
16365
72019c9c 16366/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16367 the DIE's type field. */
72019c9c 16368
f792889a 16369static struct type *
72019c9c
GM
16370read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16371{
7e314c57
JK
16372 struct type *domain_type, *set_type;
16373 struct attribute *attr;
f792889a 16374
7e314c57
JK
16375 domain_type = die_type (die, cu);
16376
16377 /* The die_type call above may have already set the type for this DIE. */
16378 set_type = get_die_type (die, cu);
16379 if (set_type)
16380 return set_type;
16381
16382 set_type = create_set_type (NULL, domain_type);
16383
16384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16385 if (attr != nullptr)
d09039dd 16386 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16387
2b4424c3
TT
16388 maybe_set_alignment (cu, die, set_type);
16389
f792889a 16390 return set_die_type (die, set_type, cu);
72019c9c 16391}
7ca2d3a3 16392
0971de02
TT
16393/* A helper for read_common_block that creates a locexpr baton.
16394 SYM is the symbol which we are marking as computed.
16395 COMMON_DIE is the DIE for the common block.
16396 COMMON_LOC is the location expression attribute for the common
16397 block itself.
16398 MEMBER_LOC is the location expression attribute for the particular
16399 member of the common block that we are processing.
16400 CU is the CU from which the above come. */
16401
16402static void
16403mark_common_block_symbol_computed (struct symbol *sym,
16404 struct die_info *common_die,
16405 struct attribute *common_loc,
16406 struct attribute *member_loc,
16407 struct dwarf2_cu *cu)
16408{
518817b3
SM
16409 struct dwarf2_per_objfile *dwarf2_per_objfile
16410 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16411 struct objfile *objfile = dwarf2_per_objfile->objfile;
16412 struct dwarf2_locexpr_baton *baton;
16413 gdb_byte *ptr;
16414 unsigned int cu_off;
16415 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16416 LONGEST offset = 0;
16417
16418 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16419 gdb_assert (common_loc->form_is_block ());
16420 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16421 || member_loc->form_is_constant ());
0971de02 16422
8d749320 16423 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16424 baton->per_cu = cu->per_cu;
16425 gdb_assert (baton->per_cu);
16426
16427 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16428
cd6c91b4 16429 if (member_loc->form_is_constant ())
0971de02
TT
16430 {
16431 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16432 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16433 }
16434 else
16435 baton->size += DW_BLOCK (member_loc)->size;
16436
224c3ddb 16437 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16438 baton->data = ptr;
16439
16440 *ptr++ = DW_OP_call4;
9c541725 16441 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16442 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16443 ptr += 4;
16444
cd6c91b4 16445 if (member_loc->form_is_constant ())
0971de02
TT
16446 {
16447 *ptr++ = DW_OP_addr;
16448 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16449 ptr += cu->header.addr_size;
16450 }
16451 else
16452 {
16453 /* We have to copy the data here, because DW_OP_call4 will only
16454 use a DW_AT_location attribute. */
16455 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16456 ptr += DW_BLOCK (member_loc)->size;
16457 }
16458
16459 *ptr++ = DW_OP_plus;
16460 gdb_assert (ptr - baton->data == baton->size);
16461
0971de02 16462 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16463 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16464}
16465
4357ac6c
TT
16466/* Create appropriate locally-scoped variables for all the
16467 DW_TAG_common_block entries. Also create a struct common_block
16468 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16469 is used to separate the common blocks name namespace from regular
4357ac6c 16470 variable names. */
c906108c
SS
16471
16472static void
e7c27a73 16473read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16474{
0971de02
TT
16475 struct attribute *attr;
16476
16477 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16478 if (attr != nullptr)
0971de02
TT
16479 {
16480 /* Support the .debug_loc offsets. */
4fc6c0d5 16481 if (attr->form_is_block ())
0971de02
TT
16482 {
16483 /* Ok. */
16484 }
cd6c91b4 16485 else if (attr->form_is_section_offset ())
0971de02
TT
16486 {
16487 dwarf2_complex_location_expr_complaint ();
16488 attr = NULL;
16489 }
16490 else
16491 {
16492 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16493 "common block member");
16494 attr = NULL;
16495 }
16496 }
16497
639d11d3 16498 if (die->child != NULL)
c906108c 16499 {
518817b3 16500 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16501 struct die_info *child_die;
16502 size_t n_entries = 0, size;
16503 struct common_block *common_block;
16504 struct symbol *sym;
74ac6d43 16505
4357ac6c
TT
16506 for (child_die = die->child;
16507 child_die && child_die->tag;
16508 child_die = sibling_die (child_die))
16509 ++n_entries;
16510
16511 size = (sizeof (struct common_block)
16512 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16513 common_block
16514 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16515 size);
4357ac6c
TT
16516 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16517 common_block->n_entries = 0;
16518
16519 for (child_die = die->child;
16520 child_die && child_die->tag;
16521 child_die = sibling_die (child_die))
16522 {
16523 /* Create the symbol in the DW_TAG_common_block block in the current
16524 symbol scope. */
e7c27a73 16525 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16526 if (sym != NULL)
16527 {
16528 struct attribute *member_loc;
16529
16530 common_block->contents[common_block->n_entries++] = sym;
16531
16532 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16533 cu);
16534 if (member_loc)
16535 {
16536 /* GDB has handled this for a long time, but it is
16537 not specified by DWARF. It seems to have been
16538 emitted by gfortran at least as recently as:
16539 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16540 complaint (_("Variable in common block has "
0971de02 16541 "DW_AT_data_member_location "
9d8780f0
SM
16542 "- DIE at %s [in module %s]"),
16543 sect_offset_str (child_die->sect_off),
518817b3 16544 objfile_name (objfile));
0971de02 16545
cd6c91b4 16546 if (member_loc->form_is_section_offset ())
0971de02 16547 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16548 else if (member_loc->form_is_constant ()
4fc6c0d5 16549 || member_loc->form_is_block ())
0971de02 16550 {
435d3d88 16551 if (attr != nullptr)
0971de02
TT
16552 mark_common_block_symbol_computed (sym, die, attr,
16553 member_loc, cu);
16554 }
16555 else
16556 dwarf2_complex_location_expr_complaint ();
16557 }
16558 }
c906108c 16559 }
4357ac6c
TT
16560
16561 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16562 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16563 }
16564}
16565
0114d602 16566/* Create a type for a C++ namespace. */
d9fa45fe 16567
0114d602
DJ
16568static struct type *
16569read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16570{
518817b3 16571 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16572 const char *previous_prefix, *name;
9219021c 16573 int is_anonymous;
0114d602
DJ
16574 struct type *type;
16575
16576 /* For extensions, reuse the type of the original namespace. */
16577 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16578 {
16579 struct die_info *ext_die;
16580 struct dwarf2_cu *ext_cu = cu;
9a619af0 16581
0114d602
DJ
16582 ext_die = dwarf2_extension (die, &ext_cu);
16583 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16584
16585 /* EXT_CU may not be the same as CU.
02142a6c 16586 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16587 return set_die_type (die, type, cu);
16588 }
9219021c 16589
e142c38c 16590 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16591
16592 /* Now build the name of the current namespace. */
16593
0114d602
DJ
16594 previous_prefix = determine_prefix (die, cu);
16595 if (previous_prefix[0] != '\0')
16596 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16597 previous_prefix, name, 0, cu);
0114d602
DJ
16598
16599 /* Create the type. */
19f392bc 16600 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16601
60531b24 16602 return set_die_type (die, type, cu);
0114d602
DJ
16603}
16604
22cee43f 16605/* Read a namespace scope. */
0114d602
DJ
16606
16607static void
16608read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16609{
518817b3 16610 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16611 int is_anonymous;
9219021c 16612
5c4e30ca
DC
16613 /* Add a symbol associated to this if we haven't seen the namespace
16614 before. Also, add a using directive if it's an anonymous
16615 namespace. */
9219021c 16616
f2f0e013 16617 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16618 {
16619 struct type *type;
16620
0114d602 16621 type = read_type_die (die, cu);
e7c27a73 16622 new_symbol (die, type, cu);
5c4e30ca 16623
e8e80198 16624 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16625 if (is_anonymous)
0114d602
DJ
16626 {
16627 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16628
eb1e02fd 16629 std::vector<const char *> excludes;
804d2729 16630 add_using_directive (using_directives (cu),
22cee43f 16631 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16632 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16633 }
5c4e30ca 16634 }
9219021c 16635
639d11d3 16636 if (die->child != NULL)
d9fa45fe 16637 {
639d11d3 16638 struct die_info *child_die = die->child;
6e70227d 16639
d9fa45fe
DC
16640 while (child_die && child_die->tag)
16641 {
e7c27a73 16642 process_die (child_die, cu);
d9fa45fe
DC
16643 child_die = sibling_die (child_die);
16644 }
16645 }
38d518c9
EZ
16646}
16647
f55ee35c
JK
16648/* Read a Fortran module as type. This DIE can be only a declaration used for
16649 imported module. Still we need that type as local Fortran "use ... only"
16650 declaration imports depend on the created type in determine_prefix. */
16651
16652static struct type *
16653read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16654{
518817b3 16655 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16656 const char *module_name;
f55ee35c
JK
16657 struct type *type;
16658
16659 module_name = dwarf2_name (die, cu);
19f392bc 16660 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16661
f55ee35c
JK
16662 return set_die_type (die, type, cu);
16663}
16664
5d7cb8df
JK
16665/* Read a Fortran module. */
16666
16667static void
16668read_module (struct die_info *die, struct dwarf2_cu *cu)
16669{
16670 struct die_info *child_die = die->child;
530e8392
KB
16671 struct type *type;
16672
16673 type = read_type_die (die, cu);
16674 new_symbol (die, type, cu);
5d7cb8df 16675
5d7cb8df
JK
16676 while (child_die && child_die->tag)
16677 {
16678 process_die (child_die, cu);
16679 child_die = sibling_die (child_die);
16680 }
16681}
16682
38d518c9
EZ
16683/* Return the name of the namespace represented by DIE. Set
16684 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16685 namespace. */
16686
16687static const char *
e142c38c 16688namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16689{
16690 struct die_info *current_die;
16691 const char *name = NULL;
16692
16693 /* Loop through the extensions until we find a name. */
16694
16695 for (current_die = die;
16696 current_die != NULL;
f2f0e013 16697 current_die = dwarf2_extension (die, &cu))
38d518c9 16698 {
96553a0c
DE
16699 /* We don't use dwarf2_name here so that we can detect the absence
16700 of a name -> anonymous namespace. */
7d45c7c3 16701 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16702
38d518c9
EZ
16703 if (name != NULL)
16704 break;
16705 }
16706
16707 /* Is it an anonymous namespace? */
16708
16709 *is_anonymous = (name == NULL);
16710 if (*is_anonymous)
2b1dbab0 16711 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16712
16713 return name;
d9fa45fe
DC
16714}
16715
c906108c
SS
16716/* Extract all information from a DW_TAG_pointer_type DIE and add to
16717 the user defined type vector. */
16718
f792889a 16719static struct type *
e7c27a73 16720read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16721{
518817b3
SM
16722 struct gdbarch *gdbarch
16723 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16724 struct comp_unit_head *cu_header = &cu->header;
c906108c 16725 struct type *type;
8b2dbe47
KB
16726 struct attribute *attr_byte_size;
16727 struct attribute *attr_address_class;
16728 int byte_size, addr_class;
7e314c57
JK
16729 struct type *target_type;
16730
16731 target_type = die_type (die, cu);
c906108c 16732
7e314c57
JK
16733 /* The die_type call above may have already set the type for this DIE. */
16734 type = get_die_type (die, cu);
16735 if (type)
16736 return type;
16737
16738 type = lookup_pointer_type (target_type);
8b2dbe47 16739
e142c38c 16740 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16741 if (attr_byte_size)
16742 byte_size = DW_UNSND (attr_byte_size);
c906108c 16743 else
8b2dbe47
KB
16744 byte_size = cu_header->addr_size;
16745
e142c38c 16746 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16747 if (attr_address_class)
16748 addr_class = DW_UNSND (attr_address_class);
16749 else
16750 addr_class = DW_ADDR_none;
16751
2b4424c3
TT
16752 ULONGEST alignment = get_alignment (cu, die);
16753
16754 /* If the pointer size, alignment, or address class is different
16755 than the default, create a type variant marked as such and set
16756 the length accordingly. */
16757 if (TYPE_LENGTH (type) != byte_size
16758 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16759 && alignment != TYPE_RAW_ALIGN (type))
16760 || addr_class != DW_ADDR_none)
c906108c 16761 {
5e2b427d 16762 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16763 {
16764 int type_flags;
16765
849957d9 16766 type_flags = gdbarch_address_class_type_flags
5e2b427d 16767 (gdbarch, byte_size, addr_class);
876cecd0
TT
16768 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16769 == 0);
8b2dbe47
KB
16770 type = make_type_with_address_space (type, type_flags);
16771 }
16772 else if (TYPE_LENGTH (type) != byte_size)
16773 {
b98664d3 16774 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16775 }
2b4424c3
TT
16776 else if (TYPE_RAW_ALIGN (type) != alignment)
16777 {
b98664d3 16778 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16779 " - DIE at %s [in module %s]"),
16780 sect_offset_str (die->sect_off),
16781 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16782 }
6e70227d 16783 else
9a619af0
MS
16784 {
16785 /* Should we also complain about unhandled address classes? */
16786 }
c906108c 16787 }
8b2dbe47
KB
16788
16789 TYPE_LENGTH (type) = byte_size;
2b4424c3 16790 set_type_align (type, alignment);
f792889a 16791 return set_die_type (die, type, cu);
c906108c
SS
16792}
16793
16794/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16795 the user defined type vector. */
16796
f792889a 16797static struct type *
e7c27a73 16798read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16799{
16800 struct type *type;
16801 struct type *to_type;
16802 struct type *domain;
16803
e7c27a73
DJ
16804 to_type = die_type (die, cu);
16805 domain = die_containing_type (die, cu);
0d5de010 16806
7e314c57
JK
16807 /* The calls above may have already set the type for this DIE. */
16808 type = get_die_type (die, cu);
16809 if (type)
16810 return type;
16811
0d5de010
DJ
16812 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16813 type = lookup_methodptr_type (to_type);
7078baeb
TT
16814 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16815 {
518817b3
SM
16816 struct type *new_type
16817 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16818
16819 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16820 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16821 TYPE_VARARGS (to_type));
16822 type = lookup_methodptr_type (new_type);
16823 }
0d5de010
DJ
16824 else
16825 type = lookup_memberptr_type (to_type, domain);
c906108c 16826
f792889a 16827 return set_die_type (die, type, cu);
c906108c
SS
16828}
16829
4297a3f0 16830/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16831 the user defined type vector. */
16832
f792889a 16833static struct type *
4297a3f0
AV
16834read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16835 enum type_code refcode)
c906108c 16836{
e7c27a73 16837 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16838 struct type *type, *target_type;
c906108c
SS
16839 struct attribute *attr;
16840
4297a3f0
AV
16841 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16842
7e314c57
JK
16843 target_type = die_type (die, cu);
16844
16845 /* The die_type call above may have already set the type for this DIE. */
16846 type = get_die_type (die, cu);
16847 if (type)
16848 return type;
16849
4297a3f0 16850 type = lookup_reference_type (target_type, refcode);
e142c38c 16851 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16852 if (attr != nullptr)
c906108c
SS
16853 {
16854 TYPE_LENGTH (type) = DW_UNSND (attr);
16855 }
16856 else
16857 {
107d2387 16858 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16859 }
2b4424c3 16860 maybe_set_alignment (cu, die, type);
f792889a 16861 return set_die_type (die, type, cu);
c906108c
SS
16862}
16863
cf363f18
MW
16864/* Add the given cv-qualifiers to the element type of the array. GCC
16865 outputs DWARF type qualifiers that apply to an array, not the
16866 element type. But GDB relies on the array element type to carry
16867 the cv-qualifiers. This mimics section 6.7.3 of the C99
16868 specification. */
16869
16870static struct type *
16871add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16872 struct type *base_type, int cnst, int voltl)
16873{
16874 struct type *el_type, *inner_array;
16875
16876 base_type = copy_type (base_type);
16877 inner_array = base_type;
16878
16879 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16880 {
16881 TYPE_TARGET_TYPE (inner_array) =
16882 copy_type (TYPE_TARGET_TYPE (inner_array));
16883 inner_array = TYPE_TARGET_TYPE (inner_array);
16884 }
16885
16886 el_type = TYPE_TARGET_TYPE (inner_array);
16887 cnst |= TYPE_CONST (el_type);
16888 voltl |= TYPE_VOLATILE (el_type);
16889 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16890
16891 return set_die_type (die, base_type, cu);
16892}
16893
f792889a 16894static struct type *
e7c27a73 16895read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16896{
f792889a 16897 struct type *base_type, *cv_type;
c906108c 16898
e7c27a73 16899 base_type = die_type (die, cu);
7e314c57
JK
16900
16901 /* The die_type call above may have already set the type for this DIE. */
16902 cv_type = get_die_type (die, cu);
16903 if (cv_type)
16904 return cv_type;
16905
2f608a3a
KW
16906 /* In case the const qualifier is applied to an array type, the element type
16907 is so qualified, not the array type (section 6.7.3 of C99). */
16908 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16909 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16910
f792889a
DJ
16911 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16912 return set_die_type (die, cv_type, cu);
c906108c
SS
16913}
16914
f792889a 16915static struct type *
e7c27a73 16916read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16917{
f792889a 16918 struct type *base_type, *cv_type;
c906108c 16919
e7c27a73 16920 base_type = die_type (die, cu);
7e314c57
JK
16921
16922 /* The die_type call above may have already set the type for this DIE. */
16923 cv_type = get_die_type (die, cu);
16924 if (cv_type)
16925 return cv_type;
16926
cf363f18
MW
16927 /* In case the volatile qualifier is applied to an array type, the
16928 element type is so qualified, not the array type (section 6.7.3
16929 of C99). */
16930 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16931 return add_array_cv_type (die, cu, base_type, 0, 1);
16932
f792889a
DJ
16933 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16934 return set_die_type (die, cv_type, cu);
c906108c
SS
16935}
16936
06d66ee9
TT
16937/* Handle DW_TAG_restrict_type. */
16938
16939static struct type *
16940read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16941{
16942 struct type *base_type, *cv_type;
16943
16944 base_type = die_type (die, cu);
16945
16946 /* The die_type call above may have already set the type for this DIE. */
16947 cv_type = get_die_type (die, cu);
16948 if (cv_type)
16949 return cv_type;
16950
16951 cv_type = make_restrict_type (base_type);
16952 return set_die_type (die, cv_type, cu);
16953}
16954
a2c2acaf
MW
16955/* Handle DW_TAG_atomic_type. */
16956
16957static struct type *
16958read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16959{
16960 struct type *base_type, *cv_type;
16961
16962 base_type = die_type (die, cu);
16963
16964 /* The die_type call above may have already set the type for this DIE. */
16965 cv_type = get_die_type (die, cu);
16966 if (cv_type)
16967 return cv_type;
16968
16969 cv_type = make_atomic_type (base_type);
16970 return set_die_type (die, cv_type, cu);
16971}
16972
c906108c
SS
16973/* Extract all information from a DW_TAG_string_type DIE and add to
16974 the user defined type vector. It isn't really a user defined type,
16975 but it behaves like one, with other DIE's using an AT_user_def_type
16976 attribute to reference it. */
16977
f792889a 16978static struct type *
e7c27a73 16979read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16980{
518817b3 16981 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16982 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16983 struct type *type, *range_type, *index_type, *char_type;
16984 struct attribute *attr;
216a7e6b
AB
16985 struct dynamic_prop prop;
16986 bool length_is_constant = true;
16987 LONGEST length;
16988
16989 /* There are a couple of places where bit sizes might be made use of
16990 when parsing a DW_TAG_string_type, however, no producer that we know
16991 of make use of these. Handling bit sizes that are a multiple of the
16992 byte size is easy enough, but what about other bit sizes? Lets deal
16993 with that problem when we have to. Warn about these attributes being
16994 unsupported, then parse the type and ignore them like we always
16995 have. */
16996 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16997 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16998 {
16999 static bool warning_printed = false;
17000 if (!warning_printed)
17001 {
17002 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17003 "currently supported on DW_TAG_string_type."));
17004 warning_printed = true;
17005 }
17006 }
c906108c 17007
e142c38c 17008 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17009 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17010 {
17011 /* The string length describes the location at which the length of
17012 the string can be found. The size of the length field can be
17013 specified with one of the attributes below. */
17014 struct type *prop_type;
17015 struct attribute *len
17016 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17017 if (len == nullptr)
17018 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17019 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17020 {
17021 /* Pass 0 as the default as we know this attribute is constant
17022 and the default value will not be returned. */
17023 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17024 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17025 }
17026 else
17027 {
17028 /* If the size is not specified then we assume it is the size of
17029 an address on this target. */
17030 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17031 }
17032
17033 /* Convert the attribute into a dynamic property. */
17034 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17035 length = 1;
17036 else
17037 length_is_constant = false;
17038 }
17039 else if (attr != nullptr)
17040 {
17041 /* This DW_AT_string_length just contains the length with no
17042 indirection. There's no need to create a dynamic property in this
17043 case. Pass 0 for the default value as we know it will not be
17044 returned in this case. */
17045 length = dwarf2_get_attr_constant_value (attr, 0);
17046 }
17047 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17048 {
216a7e6b
AB
17049 /* We don't currently support non-constant byte sizes for strings. */
17050 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17051 }
17052 else
17053 {
216a7e6b
AB
17054 /* Use 1 as a fallback length if we have nothing else. */
17055 length = 1;
c906108c 17056 }
6ccb9162 17057
46bf5051 17058 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17059 if (length_is_constant)
17060 range_type = create_static_range_type (NULL, index_type, 1, length);
17061 else
17062 {
17063 struct dynamic_prop low_bound;
17064
17065 low_bound.kind = PROP_CONST;
17066 low_bound.data.const_val = 1;
17067 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17068 }
3b7538c0
UW
17069 char_type = language_string_char_type (cu->language_defn, gdbarch);
17070 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17071
f792889a 17072 return set_die_type (die, type, cu);
c906108c
SS
17073}
17074
4d804846
JB
17075/* Assuming that DIE corresponds to a function, returns nonzero
17076 if the function is prototyped. */
17077
17078static int
17079prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17080{
17081 struct attribute *attr;
17082
17083 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17084 if (attr && (DW_UNSND (attr) != 0))
17085 return 1;
17086
17087 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17088 is only meaningful for C, but the concept also extends to other
4d804846
JB
17089 languages that allow unprototyped functions (Eg: Objective C).
17090 For all other languages, assume that functions are always
17091 prototyped. */
17092 if (cu->language != language_c
17093 && cu->language != language_objc
17094 && cu->language != language_opencl)
17095 return 1;
17096
17097 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17098 prototyped and unprototyped functions; default to prototyped,
17099 since that is more common in modern code (and RealView warns
17100 about unprototyped functions). */
17101 if (producer_is_realview (cu->producer))
17102 return 1;
17103
17104 return 0;
17105}
17106
c906108c
SS
17107/* Handle DIES due to C code like:
17108
17109 struct foo
c5aa993b
JM
17110 {
17111 int (*funcp)(int a, long l);
17112 int b;
17113 };
c906108c 17114
0963b4bd 17115 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17116
f792889a 17117static struct type *
e7c27a73 17118read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17119{
518817b3 17120 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17121 struct type *type; /* Type that this function returns. */
17122 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17123 struct attribute *attr;
17124
e7c27a73 17125 type = die_type (die, cu);
7e314c57
JK
17126
17127 /* The die_type call above may have already set the type for this DIE. */
17128 ftype = get_die_type (die, cu);
17129 if (ftype)
17130 return ftype;
17131
0c8b41f1 17132 ftype = lookup_function_type (type);
c906108c 17133
4d804846 17134 if (prototyped_function_p (die, cu))
a6c727b2 17135 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17136
c055b101
CV
17137 /* Store the calling convention in the type if it's available in
17138 the subroutine die. Otherwise set the calling convention to
17139 the default value DW_CC_normal. */
17140 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17141 if (attr != nullptr
17142 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17143 TYPE_CALLING_CONVENTION (ftype)
17144 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17145 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17146 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17147 else
17148 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17149
743649fd
MW
17150 /* Record whether the function returns normally to its caller or not
17151 if the DWARF producer set that information. */
17152 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17153 if (attr && (DW_UNSND (attr) != 0))
17154 TYPE_NO_RETURN (ftype) = 1;
17155
76c10ea2
GM
17156 /* We need to add the subroutine type to the die immediately so
17157 we don't infinitely recurse when dealing with parameters
0963b4bd 17158 declared as the same subroutine type. */
76c10ea2 17159 set_die_type (die, ftype, cu);
6e70227d 17160
639d11d3 17161 if (die->child != NULL)
c906108c 17162 {
bb5ed363 17163 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17164 struct die_info *child_die;
8072405b 17165 int nparams, iparams;
c906108c
SS
17166
17167 /* Count the number of parameters.
17168 FIXME: GDB currently ignores vararg functions, but knows about
17169 vararg member functions. */
8072405b 17170 nparams = 0;
639d11d3 17171 child_die = die->child;
c906108c
SS
17172 while (child_die && child_die->tag)
17173 {
17174 if (child_die->tag == DW_TAG_formal_parameter)
17175 nparams++;
17176 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17177 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17178 child_die = sibling_die (child_die);
17179 }
17180
17181 /* Allocate storage for parameters and fill them in. */
17182 TYPE_NFIELDS (ftype) = nparams;
17183 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17184 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17185
8072405b
JK
17186 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17187 even if we error out during the parameters reading below. */
17188 for (iparams = 0; iparams < nparams; iparams++)
17189 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17190
17191 iparams = 0;
639d11d3 17192 child_die = die->child;
c906108c
SS
17193 while (child_die && child_die->tag)
17194 {
17195 if (child_die->tag == DW_TAG_formal_parameter)
17196 {
3ce3b1ba
PA
17197 struct type *arg_type;
17198
17199 /* DWARF version 2 has no clean way to discern C++
17200 static and non-static member functions. G++ helps
17201 GDB by marking the first parameter for non-static
17202 member functions (which is the this pointer) as
17203 artificial. We pass this information to
17204 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17205
17206 DWARF version 3 added DW_AT_object_pointer, which GCC
17207 4.5 does not yet generate. */
e142c38c 17208 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17209 if (attr != nullptr)
c906108c
SS
17210 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17211 else
9c37b5ae 17212 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17213 arg_type = die_type (child_die, cu);
17214
17215 /* RealView does not mark THIS as const, which the testsuite
17216 expects. GCC marks THIS as const in method definitions,
17217 but not in the class specifications (GCC PR 43053). */
17218 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17219 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17220 {
17221 int is_this = 0;
17222 struct dwarf2_cu *arg_cu = cu;
17223 const char *name = dwarf2_name (child_die, cu);
17224
17225 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17226 if (attr != nullptr)
3ce3b1ba
PA
17227 {
17228 /* If the compiler emits this, use it. */
17229 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17230 is_this = 1;
17231 }
17232 else if (name && strcmp (name, "this") == 0)
17233 /* Function definitions will have the argument names. */
17234 is_this = 1;
17235 else if (name == NULL && iparams == 0)
17236 /* Declarations may not have the names, so like
17237 elsewhere in GDB, assume an artificial first
17238 argument is "this". */
17239 is_this = 1;
17240
17241 if (is_this)
17242 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17243 arg_type, 0);
17244 }
17245
17246 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17247 iparams++;
17248 }
17249 child_die = sibling_die (child_die);
17250 }
17251 }
17252
76c10ea2 17253 return ftype;
c906108c
SS
17254}
17255
f792889a 17256static struct type *
e7c27a73 17257read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17258{
518817b3 17259 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17260 const char *name = NULL;
3c8e0968 17261 struct type *this_type, *target_type;
c906108c 17262
94af9270 17263 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17264 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17265 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17266 set_die_type (die, this_type, cu);
3c8e0968
DE
17267 target_type = die_type (die, cu);
17268 if (target_type != this_type)
17269 TYPE_TARGET_TYPE (this_type) = target_type;
17270 else
17271 {
17272 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17273 spec and cause infinite loops in GDB. */
b98664d3 17274 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17275 "- DIE at %s [in module %s]"),
17276 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17277 TYPE_TARGET_TYPE (this_type) = NULL;
17278 }
f792889a 17279 return this_type;
c906108c
SS
17280}
17281
9b790ce7
UW
17282/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17283 (which may be different from NAME) to the architecture back-end to allow
17284 it to guess the correct format if necessary. */
17285
17286static struct type *
17287dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17288 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17289{
17290 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17291 const struct floatformat **format;
17292 struct type *type;
17293
17294 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17295 if (format)
103a685e 17296 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17297 else
77b7c781 17298 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17299
17300 return type;
17301}
17302
eb77c9df
AB
17303/* Allocate an integer type of size BITS and name NAME. */
17304
17305static struct type *
17306dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17307 int bits, int unsigned_p, const char *name)
17308{
17309 struct type *type;
17310
17311 /* Versions of Intel's C Compiler generate an integer type called "void"
17312 instead of using DW_TAG_unspecified_type. This has been seen on
17313 at least versions 14, 17, and 18. */
35ee2dc2
AB
17314 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17315 && strcmp (name, "void") == 0)
eb77c9df
AB
17316 type = objfile_type (objfile)->builtin_void;
17317 else
17318 type = init_integer_type (objfile, bits, unsigned_p, name);
17319
17320 return type;
17321}
17322
8bdc1658
AB
17323/* Initialise and return a floating point type of size BITS suitable for
17324 use as a component of a complex number. The NAME_HINT is passed through
17325 when initialising the floating point type and is the name of the complex
17326 type.
17327
17328 As DWARF doesn't currently provide an explicit name for the components
17329 of a complex number, but it can be helpful to have these components
17330 named, we try to select a suitable name based on the size of the
17331 component. */
17332static struct type *
17333dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17334 struct objfile *objfile,
103a685e
TT
17335 int bits, const char *name_hint,
17336 enum bfd_endian byte_order)
8bdc1658
AB
17337{
17338 gdbarch *gdbarch = get_objfile_arch (objfile);
17339 struct type *tt = nullptr;
17340
35add35e
AB
17341 /* Try to find a suitable floating point builtin type of size BITS.
17342 We're going to use the name of this type as the name for the complex
17343 target type that we are about to create. */
1db455a7 17344 switch (cu->language)
8bdc1658 17345 {
1db455a7
AB
17346 case language_fortran:
17347 switch (bits)
17348 {
17349 case 32:
17350 tt = builtin_f_type (gdbarch)->builtin_real;
17351 break;
17352 case 64:
17353 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17354 break;
17355 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17356 case 128:
17357 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17358 break;
17359 }
8bdc1658 17360 break;
1db455a7
AB
17361 default:
17362 switch (bits)
17363 {
17364 case 32:
17365 tt = builtin_type (gdbarch)->builtin_float;
17366 break;
17367 case 64:
17368 tt = builtin_type (gdbarch)->builtin_double;
17369 break;
17370 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17371 case 128:
17372 tt = builtin_type (gdbarch)->builtin_long_double;
17373 break;
17374 }
8bdc1658
AB
17375 break;
17376 }
17377
35add35e
AB
17378 /* If the type we found doesn't match the size we were looking for, then
17379 pretend we didn't find a type at all, the complex target type we
17380 create will then be nameless. */
a12e5744 17381 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17382 tt = nullptr;
17383
8bdc1658 17384 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17385 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17386}
17387
c906108c
SS
17388/* Find a representation of a given base type and install
17389 it in the TYPE field of the die. */
17390
f792889a 17391static struct type *
e7c27a73 17392read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17393{
518817b3 17394 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17395 struct type *type;
17396 struct attribute *attr;
19f392bc 17397 int encoding = 0, bits = 0;
15d034d0 17398 const char *name;
34877895 17399 gdbarch *arch;
c906108c 17400
e142c38c 17401 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17402 if (attr != nullptr)
34877895 17403 encoding = DW_UNSND (attr);
e142c38c 17404 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17405 if (attr != nullptr)
34877895 17406 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17407 name = dwarf2_name (die, cu);
6ccb9162 17408 if (!name)
34877895 17409 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17410
17411 arch = get_objfile_arch (objfile);
17412 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17413
34877895
PJ
17414 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17415 if (attr)
103a685e
TT
17416 {
17417 int endianity = DW_UNSND (attr);
17418
17419 switch (endianity)
17420 {
17421 case DW_END_big:
17422 byte_order = BFD_ENDIAN_BIG;
17423 break;
17424 case DW_END_little:
17425 byte_order = BFD_ENDIAN_LITTLE;
17426 break;
17427 default:
17428 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17429 break;
17430 }
17431 }
6ccb9162
UW
17432
17433 switch (encoding)
c906108c 17434 {
6ccb9162
UW
17435 case DW_ATE_address:
17436 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17437 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17438 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17439 break;
17440 case DW_ATE_boolean:
19f392bc 17441 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17442 break;
17443 case DW_ATE_complex_float:
103a685e
TT
17444 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17445 byte_order);
19f392bc 17446 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17447 break;
17448 case DW_ATE_decimal_float:
19f392bc 17449 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17450 break;
17451 case DW_ATE_float:
103a685e 17452 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17453 break;
17454 case DW_ATE_signed:
eb77c9df 17455 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17456 break;
17457 case DW_ATE_unsigned:
3b2b8fea
TT
17458 if (cu->language == language_fortran
17459 && name
61012eef 17460 && startswith (name, "character("))
19f392bc
UW
17461 type = init_character_type (objfile, bits, 1, name);
17462 else
eb77c9df 17463 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17464 break;
17465 case DW_ATE_signed_char:
6e70227d 17466 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17467 || cu->language == language_pascal
17468 || cu->language == language_fortran)
19f392bc
UW
17469 type = init_character_type (objfile, bits, 0, name);
17470 else
eb77c9df 17471 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17472 break;
17473 case DW_ATE_unsigned_char:
868a0084 17474 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17475 || cu->language == language_pascal
c44af4eb
TT
17476 || cu->language == language_fortran
17477 || cu->language == language_rust)
19f392bc
UW
17478 type = init_character_type (objfile, bits, 1, name);
17479 else
eb77c9df 17480 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17481 break;
75079b2b 17482 case DW_ATE_UTF:
53e710ac 17483 {
53e710ac
PA
17484 if (bits == 16)
17485 type = builtin_type (arch)->builtin_char16;
17486 else if (bits == 32)
17487 type = builtin_type (arch)->builtin_char32;
17488 else
17489 {
b98664d3 17490 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17491 bits);
eb77c9df 17492 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17493 }
17494 return set_die_type (die, type, cu);
17495 }
75079b2b
TT
17496 break;
17497
6ccb9162 17498 default:
b98664d3 17499 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17500 dwarf_type_encoding_name (encoding));
77b7c781 17501 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17502 break;
c906108c 17503 }
6ccb9162 17504
0114d602 17505 if (name && strcmp (name, "char") == 0)
876cecd0 17506 TYPE_NOSIGN (type) = 1;
0114d602 17507
2b4424c3
TT
17508 maybe_set_alignment (cu, die, type);
17509
103a685e 17510 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17511
f792889a 17512 return set_die_type (die, type, cu);
c906108c
SS
17513}
17514
80180f79
SA
17515/* Parse dwarf attribute if it's a block, reference or constant and put the
17516 resulting value of the attribute into struct bound_prop.
17517 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17518
17519static int
17520attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17521 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17522 struct type *default_type)
80180f79
SA
17523{
17524 struct dwarf2_property_baton *baton;
518817b3
SM
17525 struct obstack *obstack
17526 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17527
9a49df9d
AB
17528 gdb_assert (default_type != NULL);
17529
80180f79
SA
17530 if (attr == NULL || prop == NULL)
17531 return 0;
17532
4fc6c0d5 17533 if (attr->form_is_block ())
80180f79 17534 {
8d749320 17535 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17536 baton->property_type = default_type;
80180f79
SA
17537 baton->locexpr.per_cu = cu->per_cu;
17538 baton->locexpr.size = DW_BLOCK (attr)->size;
17539 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17540 switch (attr->name)
17541 {
17542 case DW_AT_string_length:
17543 baton->locexpr.is_reference = true;
17544 break;
17545 default:
17546 baton->locexpr.is_reference = false;
17547 break;
17548 }
80180f79
SA
17549 prop->data.baton = baton;
17550 prop->kind = PROP_LOCEXPR;
17551 gdb_assert (prop->data.baton != NULL);
17552 }
cd6c91b4 17553 else if (attr->form_is_ref ())
80180f79
SA
17554 {
17555 struct dwarf2_cu *target_cu = cu;
17556 struct die_info *target_die;
17557 struct attribute *target_attr;
17558
17559 target_die = follow_die_ref (die, attr, &target_cu);
17560 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17561 if (target_attr == NULL)
17562 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17563 target_cu);
80180f79
SA
17564 if (target_attr == NULL)
17565 return 0;
17566
df25ebbd 17567 switch (target_attr->name)
80180f79 17568 {
df25ebbd 17569 case DW_AT_location:
cd6c91b4 17570 if (target_attr->form_is_section_offset ())
df25ebbd 17571 {
8d749320 17572 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17573 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17574 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17575 prop->data.baton = baton;
17576 prop->kind = PROP_LOCLIST;
17577 gdb_assert (prop->data.baton != NULL);
17578 }
4fc6c0d5 17579 else if (target_attr->form_is_block ())
df25ebbd 17580 {
8d749320 17581 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17582 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17583 baton->locexpr.per_cu = cu->per_cu;
17584 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17585 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17586 baton->locexpr.is_reference = true;
df25ebbd
JB
17587 prop->data.baton = baton;
17588 prop->kind = PROP_LOCEXPR;
17589 gdb_assert (prop->data.baton != NULL);
17590 }
17591 else
17592 {
17593 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17594 "dynamic property");
17595 return 0;
17596 }
17597 break;
17598 case DW_AT_data_member_location:
17599 {
17600 LONGEST offset;
17601
17602 if (!handle_data_member_location (target_die, target_cu,
17603 &offset))
17604 return 0;
17605
8d749320 17606 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17607 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17608 target_cu);
df25ebbd
JB
17609 baton->offset_info.offset = offset;
17610 baton->offset_info.type = die_type (target_die, target_cu);
17611 prop->data.baton = baton;
17612 prop->kind = PROP_ADDR_OFFSET;
17613 break;
17614 }
80180f79
SA
17615 }
17616 }
cd6c91b4 17617 else if (attr->form_is_constant ())
80180f79
SA
17618 {
17619 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17620 prop->kind = PROP_CONST;
17621 }
17622 else
17623 {
17624 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17625 dwarf2_name (die, cu));
17626 return 0;
17627 }
17628
17629 return 1;
17630}
17631
11a8b164
AB
17632/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17633 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
17634
17635static struct type *
11a8b164
AB
17636dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17637 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
17638{
17639 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
17640 struct type *int_type;
17641
17642 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17643#define TRY_TYPE(F) \
17644 int_type = (unsigned_p \
17645 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17646 : objfile_type (objfile)->builtin_ ## F); \
17647 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17648 return int_type
17649
17650 TRY_TYPE (char);
17651 TRY_TYPE (short);
17652 TRY_TYPE (int);
17653 TRY_TYPE (long);
17654 TRY_TYPE (long_long);
17655
17656#undef TRY_TYPE
17657
17658 gdb_assert_not_reached ("unable to find suitable integer type");
17659}
17660
11a8b164
AB
17661/* Find an integer type the same size as the address size given in the
17662 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17663 is unsigned or not. */
17664
17665static struct type *
17666dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17667 bool unsigned_p)
17668{
17669 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17670 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17671}
17672
b86352cf
AB
17673/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17674 present (which is valid) then compute the default type based on the
17675 compilation units address size. */
17676
17677static struct type *
17678read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17679{
17680 struct type *index_type = die_type (die, cu);
17681
17682 /* Dwarf-2 specifications explicitly allows to create subrange types
17683 without specifying a base type.
17684 In that case, the base type must be set to the type of
17685 the lower bound, upper bound or count, in that order, if any of these
17686 three attributes references an object that has a type.
17687 If no base type is found, the Dwarf-2 specifications say that
17688 a signed integer type of size equal to the size of an address should
17689 be used.
17690 For the following C code: `extern char gdb_int [];'
17691 GCC produces an empty range DIE.
17692 FIXME: muller/2010-05-28: Possible references to object for low bound,
17693 high bound or count are not yet handled by this code. */
17694 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17695 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17696
17697 return index_type;
17698}
17699
a02abb62
JB
17700/* Read the given DW_AT_subrange DIE. */
17701
f792889a 17702static struct type *
a02abb62
JB
17703read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17704{
4c9ad8c2 17705 struct type *base_type, *orig_base_type;
a02abb62
JB
17706 struct type *range_type;
17707 struct attribute *attr;
729efb13 17708 struct dynamic_prop low, high;
4fae6e18 17709 int low_default_is_valid;
c451ebe5 17710 int high_bound_is_count = 0;
15d034d0 17711 const char *name;
d359392f 17712 ULONGEST negative_mask;
e77813c8 17713
b86352cf
AB
17714 orig_base_type = read_subrange_index_type (die, cu);
17715
4c9ad8c2
TT
17716 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17717 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17718 creating the range type, but we use the result of check_typedef
17719 when examining properties of the type. */
17720 base_type = check_typedef (orig_base_type);
a02abb62 17721
7e314c57
JK
17722 /* The die_type call above may have already set the type for this DIE. */
17723 range_type = get_die_type (die, cu);
17724 if (range_type)
17725 return range_type;
17726
729efb13
SA
17727 low.kind = PROP_CONST;
17728 high.kind = PROP_CONST;
17729 high.data.const_val = 0;
17730
4fae6e18
JK
17731 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17732 omitting DW_AT_lower_bound. */
17733 switch (cu->language)
6e70227d 17734 {
4fae6e18
JK
17735 case language_c:
17736 case language_cplus:
729efb13 17737 low.data.const_val = 0;
4fae6e18
JK
17738 low_default_is_valid = 1;
17739 break;
17740 case language_fortran:
729efb13 17741 low.data.const_val = 1;
4fae6e18
JK
17742 low_default_is_valid = 1;
17743 break;
17744 case language_d:
4fae6e18 17745 case language_objc:
c44af4eb 17746 case language_rust:
729efb13 17747 low.data.const_val = 0;
4fae6e18
JK
17748 low_default_is_valid = (cu->header.version >= 4);
17749 break;
17750 case language_ada:
17751 case language_m2:
17752 case language_pascal:
729efb13 17753 low.data.const_val = 1;
4fae6e18
JK
17754 low_default_is_valid = (cu->header.version >= 4);
17755 break;
17756 default:
729efb13 17757 low.data.const_val = 0;
4fae6e18
JK
17758 low_default_is_valid = 0;
17759 break;
a02abb62
JB
17760 }
17761
e142c38c 17762 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17763 if (attr != nullptr)
9a49df9d 17764 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17765 else if (!low_default_is_valid)
b98664d3 17766 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17767 "- DIE at %s [in module %s]"),
17768 sect_offset_str (die->sect_off),
518817b3 17769 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17770
506f5c41
TV
17771 struct attribute *attr_ub, *attr_count;
17772 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17773 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17774 {
506f5c41 17775 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17776 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17777 {
c451ebe5
SA
17778 /* If bounds are constant do the final calculation here. */
17779 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17780 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17781 else
17782 high_bound_is_count = 1;
c2ff108b 17783 }
506f5c41
TV
17784 else
17785 {
17786 if (attr_ub != NULL)
17787 complaint (_("Unresolved DW_AT_upper_bound "
17788 "- DIE at %s [in module %s]"),
17789 sect_offset_str (die->sect_off),
17790 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17791 if (attr_count != NULL)
17792 complaint (_("Unresolved DW_AT_count "
17793 "- DIE at %s [in module %s]"),
17794 sect_offset_str (die->sect_off),
17795 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17796 }
e77813c8 17797 }
a02abb62 17798
4e962e74
TT
17799 LONGEST bias = 0;
17800 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17801 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17802 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17803
dbb9c2b1
JB
17804 /* Normally, the DWARF producers are expected to use a signed
17805 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17806 But this is unfortunately not always the case, as witnessed
17807 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17808 is used instead. To work around that ambiguity, we treat
17809 the bounds as signed, and thus sign-extend their values, when
17810 the base type is signed. */
6e70227d 17811 negative_mask =
d359392f 17812 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17813 if (low.kind == PROP_CONST
17814 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17815 low.data.const_val |= negative_mask;
17816 if (high.kind == PROP_CONST
17817 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17818 high.data.const_val |= negative_mask;
43bbcdc2 17819
5bbd8269
AB
17820 /* Check for bit and byte strides. */
17821 struct dynamic_prop byte_stride_prop;
17822 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17823 if (attr_byte_stride != nullptr)
17824 {
17825 struct type *prop_type
17826 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17827 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17828 prop_type);
17829 }
17830
17831 struct dynamic_prop bit_stride_prop;
17832 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17833 if (attr_bit_stride != nullptr)
17834 {
17835 /* It only makes sense to have either a bit or byte stride. */
17836 if (attr_byte_stride != nullptr)
17837 {
17838 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17839 "- DIE at %s [in module %s]"),
17840 sect_offset_str (die->sect_off),
17841 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17842 attr_bit_stride = nullptr;
17843 }
17844 else
17845 {
17846 struct type *prop_type
17847 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17848 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17849 prop_type);
17850 }
17851 }
17852
17853 if (attr_byte_stride != nullptr
17854 || attr_bit_stride != nullptr)
17855 {
17856 bool byte_stride_p = (attr_byte_stride != nullptr);
17857 struct dynamic_prop *stride
17858 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17859
17860 range_type
17861 = create_range_type_with_stride (NULL, orig_base_type, &low,
17862 &high, bias, stride, byte_stride_p);
17863 }
17864 else
17865 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17866
c451ebe5
SA
17867 if (high_bound_is_count)
17868 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17869
c2ff108b
JK
17870 /* Ada expects an empty array on no boundary attributes. */
17871 if (attr == NULL && cu->language != language_ada)
729efb13 17872 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17873
39cbfefa
DJ
17874 name = dwarf2_name (die, cu);
17875 if (name)
17876 TYPE_NAME (range_type) = name;
6e70227d 17877
e142c38c 17878 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17879 if (attr != nullptr)
a02abb62
JB
17880 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17881
2b4424c3
TT
17882 maybe_set_alignment (cu, die, range_type);
17883
7e314c57
JK
17884 set_die_type (die, range_type, cu);
17885
17886 /* set_die_type should be already done. */
b4ba55a1
JB
17887 set_descriptive_type (range_type, die, cu);
17888
7e314c57 17889 return range_type;
a02abb62 17890}
6e70227d 17891
f792889a 17892static struct type *
81a17f79
JB
17893read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17894{
17895 struct type *type;
81a17f79 17896
518817b3
SM
17897 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17898 NULL);
0114d602 17899 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17900
74a2f8ff 17901 /* In Ada, an unspecified type is typically used when the description
85102364 17902 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17903 such a type, we treat it as a stub, and try to resolve it later on,
17904 when needed. */
17905 if (cu->language == language_ada)
17906 TYPE_STUB (type) = 1;
17907
f792889a 17908 return set_die_type (die, type, cu);
81a17f79 17909}
a02abb62 17910
639d11d3
DC
17911/* Read a single die and all its descendents. Set the die's sibling
17912 field to NULL; set other fields in the die correctly, and set all
17913 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17914 location of the info_ptr after reading all of those dies. PARENT
17915 is the parent of the die in question. */
17916
17917static struct die_info *
dee91e82 17918read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17919 const gdb_byte *info_ptr,
17920 const gdb_byte **new_info_ptr,
dee91e82 17921 struct die_info *parent)
639d11d3
DC
17922{
17923 struct die_info *die;
d521ce57 17924 const gdb_byte *cur_ptr;
639d11d3
DC
17925 int has_children;
17926
bf6af496 17927 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17928 if (die == NULL)
17929 {
17930 *new_info_ptr = cur_ptr;
17931 return NULL;
17932 }
93311388 17933 store_in_ref_table (die, reader->cu);
639d11d3
DC
17934
17935 if (has_children)
bf6af496 17936 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17937 else
17938 {
17939 die->child = NULL;
17940 *new_info_ptr = cur_ptr;
17941 }
17942
17943 die->sibling = NULL;
17944 die->parent = parent;
17945 return die;
17946}
17947
17948/* Read a die, all of its descendents, and all of its siblings; set
17949 all of the fields of all of the dies correctly. Arguments are as
17950 in read_die_and_children. */
17951
17952static struct die_info *
bf6af496 17953read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17954 const gdb_byte *info_ptr,
17955 const gdb_byte **new_info_ptr,
bf6af496 17956 struct die_info *parent)
639d11d3
DC
17957{
17958 struct die_info *first_die, *last_sibling;
d521ce57 17959 const gdb_byte *cur_ptr;
639d11d3 17960
c906108c 17961 cur_ptr = info_ptr;
639d11d3
DC
17962 first_die = last_sibling = NULL;
17963
17964 while (1)
c906108c 17965 {
639d11d3 17966 struct die_info *die
dee91e82 17967 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17968
1d325ec1 17969 if (die == NULL)
c906108c 17970 {
639d11d3
DC
17971 *new_info_ptr = cur_ptr;
17972 return first_die;
c906108c 17973 }
1d325ec1
DJ
17974
17975 if (!first_die)
17976 first_die = die;
c906108c 17977 else
1d325ec1
DJ
17978 last_sibling->sibling = die;
17979
17980 last_sibling = die;
c906108c 17981 }
c906108c
SS
17982}
17983
bf6af496
DE
17984/* Read a die, all of its descendents, and all of its siblings; set
17985 all of the fields of all of the dies correctly. Arguments are as
17986 in read_die_and_children.
17987 This the main entry point for reading a DIE and all its children. */
17988
17989static struct die_info *
17990read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17991 const gdb_byte *info_ptr,
17992 const gdb_byte **new_info_ptr,
bf6af496
DE
17993 struct die_info *parent)
17994{
17995 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17996 new_info_ptr, parent);
17997
b4f54984 17998 if (dwarf_die_debug)
bf6af496
DE
17999 {
18000 fprintf_unfiltered (gdb_stdlog,
18001 "Read die from %s@0x%x of %s:\n",
96b79293 18002 reader->die_section->get_name (),
bf6af496
DE
18003 (unsigned) (info_ptr - reader->die_section->buffer),
18004 bfd_get_filename (reader->abfd));
b4f54984 18005 dump_die (die, dwarf_die_debug);
bf6af496
DE
18006 }
18007
18008 return die;
18009}
18010
3019eac3
DE
18011/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18012 attributes.
18013 The caller is responsible for filling in the extra attributes
18014 and updating (*DIEP)->num_attrs.
18015 Set DIEP to point to a newly allocated die with its information,
18016 except for its child, sibling, and parent fields.
18017 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18018
d521ce57 18019static const gdb_byte *
3019eac3 18020read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18021 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18022 int *has_children, int num_extra_attrs)
93311388 18023{
b64f50a1 18024 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18025 struct abbrev_info *abbrev;
18026 struct die_info *die;
18027 struct dwarf2_cu *cu = reader->cu;
18028 bfd *abfd = reader->abfd;
18029
9c541725 18030 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18031 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18032 info_ptr += bytes_read;
18033 if (!abbrev_number)
18034 {
18035 *diep = NULL;
18036 *has_children = 0;
18037 return info_ptr;
18038 }
18039
685af9cd 18040 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18041 if (!abbrev)
348e048f
DE
18042 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18043 abbrev_number,
18044 bfd_get_filename (abfd));
18045
3019eac3 18046 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18047 die->sect_off = sect_off;
93311388
DE
18048 die->tag = abbrev->tag;
18049 die->abbrev = abbrev_number;
18050
3019eac3
DE
18051 /* Make the result usable.
18052 The caller needs to update num_attrs after adding the extra
18053 attributes. */
93311388
DE
18054 die->num_attrs = abbrev->num_attrs;
18055
18a8505e 18056 std::vector<int> indexes_that_need_reprocess;
93311388 18057 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18058 {
18059 bool need_reprocess;
18060 info_ptr =
18061 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18062 info_ptr, &need_reprocess);
18063 if (need_reprocess)
18064 indexes_that_need_reprocess.push_back (i);
18065 }
18066
18067 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18068 if (attr != nullptr)
18069 cu->str_offsets_base = DW_UNSND (attr);
93311388 18070
18a8505e
AT
18071 auto maybe_addr_base = lookup_addr_base(die);
18072 if (maybe_addr_base.has_value ())
18073 cu->addr_base = *maybe_addr_base;
18074 for (int index : indexes_that_need_reprocess)
18075 read_attribute_reprocess (reader, &die->attrs[index]);
93311388
DE
18076 *diep = die;
18077 *has_children = abbrev->has_children;
18078 return info_ptr;
18079}
18080
3019eac3
DE
18081/* Read a die and all its attributes.
18082 Set DIEP to point to a newly allocated die with its information,
18083 except for its child, sibling, and parent fields.
18084 Set HAS_CHILDREN to tell whether the die has children or not. */
18085
d521ce57 18086static const gdb_byte *
3019eac3 18087read_full_die (const struct die_reader_specs *reader,
d521ce57 18088 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18089 int *has_children)
18090{
d521ce57 18091 const gdb_byte *result;
bf6af496
DE
18092
18093 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18094
b4f54984 18095 if (dwarf_die_debug)
bf6af496
DE
18096 {
18097 fprintf_unfiltered (gdb_stdlog,
18098 "Read die from %s@0x%x of %s:\n",
96b79293 18099 reader->die_section->get_name (),
bf6af496
DE
18100 (unsigned) (info_ptr - reader->die_section->buffer),
18101 bfd_get_filename (reader->abfd));
b4f54984 18102 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18103 }
18104
18105 return result;
3019eac3 18106}
433df2d4 18107\f
c906108c 18108
72bf9492
DJ
18109/* Returns nonzero if TAG represents a type that we might generate a partial
18110 symbol for. */
18111
18112static int
18113is_type_tag_for_partial (int tag)
18114{
18115 switch (tag)
18116 {
18117#if 0
18118 /* Some types that would be reasonable to generate partial symbols for,
18119 that we don't at present. */
18120 case DW_TAG_array_type:
18121 case DW_TAG_file_type:
18122 case DW_TAG_ptr_to_member_type:
18123 case DW_TAG_set_type:
18124 case DW_TAG_string_type:
18125 case DW_TAG_subroutine_type:
18126#endif
18127 case DW_TAG_base_type:
18128 case DW_TAG_class_type:
680b30c7 18129 case DW_TAG_interface_type:
72bf9492
DJ
18130 case DW_TAG_enumeration_type:
18131 case DW_TAG_structure_type:
18132 case DW_TAG_subrange_type:
18133 case DW_TAG_typedef:
18134 case DW_TAG_union_type:
18135 return 1;
18136 default:
18137 return 0;
18138 }
18139}
18140
18141/* Load all DIEs that are interesting for partial symbols into memory. */
18142
18143static struct partial_die_info *
dee91e82 18144load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18145 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18146{
dee91e82 18147 struct dwarf2_cu *cu = reader->cu;
518817b3 18148 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18149 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18150 unsigned int bytes_read;
5afb4e99 18151 unsigned int load_all = 0;
72bf9492
DJ
18152 int nesting_level = 1;
18153
18154 parent_die = NULL;
18155 last_die = NULL;
18156
7adf1e79
DE
18157 gdb_assert (cu->per_cu != NULL);
18158 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18159 load_all = 1;
18160
72bf9492
DJ
18161 cu->partial_dies
18162 = htab_create_alloc_ex (cu->header.length / 12,
18163 partial_die_hash,
18164 partial_die_eq,
18165 NULL,
18166 &cu->comp_unit_obstack,
18167 hashtab_obstack_allocate,
18168 dummy_obstack_deallocate);
18169
72bf9492
DJ
18170 while (1)
18171 {
685af9cd 18172 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18173
18174 /* A NULL abbrev means the end of a series of children. */
18175 if (abbrev == NULL)
18176 {
18177 if (--nesting_level == 0)
cd9983dd
YQ
18178 return first_die;
18179
72bf9492
DJ
18180 info_ptr += bytes_read;
18181 last_die = parent_die;
18182 parent_die = parent_die->die_parent;
18183 continue;
18184 }
18185
98bfdba5
PA
18186 /* Check for template arguments. We never save these; if
18187 they're seen, we just mark the parent, and go on our way. */
18188 if (parent_die != NULL
18189 && cu->language == language_cplus
18190 && (abbrev->tag == DW_TAG_template_type_param
18191 || abbrev->tag == DW_TAG_template_value_param))
18192 {
18193 parent_die->has_template_arguments = 1;
18194
18195 if (!load_all)
18196 {
18197 /* We don't need a partial DIE for the template argument. */
dee91e82 18198 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18199 continue;
18200 }
18201 }
18202
0d99eb77 18203 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18204 Skip their other children. */
18205 if (!load_all
18206 && cu->language == language_cplus
18207 && parent_die != NULL
18208 && parent_die->tag == DW_TAG_subprogram)
18209 {
dee91e82 18210 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18211 continue;
18212 }
18213
5afb4e99
DJ
18214 /* Check whether this DIE is interesting enough to save. Normally
18215 we would not be interested in members here, but there may be
18216 later variables referencing them via DW_AT_specification (for
18217 static members). */
18218 if (!load_all
18219 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18220 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18221 && abbrev->tag != DW_TAG_enumerator
18222 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18223 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18224 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18225 && abbrev->tag != DW_TAG_variable
5afb4e99 18226 && abbrev->tag != DW_TAG_namespace
f55ee35c 18227 && abbrev->tag != DW_TAG_module
95554aad 18228 && abbrev->tag != DW_TAG_member
74921315
KS
18229 && abbrev->tag != DW_TAG_imported_unit
18230 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18231 {
18232 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18233 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18234 continue;
18235 }
18236
6f06d47b
YQ
18237 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18238 abbrev);
cd9983dd 18239
48fbe735 18240 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18241
18242 /* This two-pass algorithm for processing partial symbols has a
18243 high cost in cache pressure. Thus, handle some simple cases
18244 here which cover the majority of C partial symbols. DIEs
18245 which neither have specification tags in them, nor could have
18246 specification tags elsewhere pointing at them, can simply be
18247 processed and discarded.
18248
18249 This segment is also optional; scan_partial_symbols and
18250 add_partial_symbol will handle these DIEs if we chain
18251 them in normally. When compilers which do not emit large
18252 quantities of duplicate debug information are more common,
18253 this code can probably be removed. */
18254
18255 /* Any complete simple types at the top level (pretty much all
18256 of them, for a language without namespaces), can be processed
18257 directly. */
18258 if (parent_die == NULL
cd9983dd
YQ
18259 && pdi.has_specification == 0
18260 && pdi.is_declaration == 0
18261 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18262 || pdi.tag == DW_TAG_base_type
18263 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18264 {
cd9983dd 18265 if (building_psymtab && pdi.name != NULL)
31edb802 18266 add_psymbol_to_list (pdi.name, false,
79748972 18267 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18268 psymbol_placement::STATIC,
1762568f 18269 0, cu->language, objfile);
cd9983dd 18270 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18271 continue;
18272 }
18273
d8228535
JK
18274 /* The exception for DW_TAG_typedef with has_children above is
18275 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18276 type_name_or_error will error on such types later.
d8228535
JK
18277
18278 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18279 it could not find the child DIEs referenced later, this is checked
18280 above. In correct DWARF DW_TAG_typedef should have no children. */
18281
cd9983dd 18282 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18283 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18284 "- DIE at %s [in module %s]"),
cd9983dd 18285 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18286
72bf9492
DJ
18287 /* If we're at the second level, and we're an enumerator, and
18288 our parent has no specification (meaning possibly lives in a
18289 namespace elsewhere), then we can add the partial symbol now
18290 instead of queueing it. */
cd9983dd 18291 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18292 && parent_die != NULL
18293 && parent_die->die_parent == NULL
18294 && parent_die->tag == DW_TAG_enumeration_type
18295 && parent_die->has_specification == 0)
18296 {
cd9983dd 18297 if (pdi.name == NULL)
b98664d3 18298 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18299 else if (building_psymtab)
31edb802 18300 add_psymbol_to_list (pdi.name, false,
79748972 18301 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18302 cu->language == language_cplus
75aedd27
TT
18303 ? psymbol_placement::GLOBAL
18304 : psymbol_placement::STATIC,
1762568f 18305 0, cu->language, objfile);
72bf9492 18306
cd9983dd 18307 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18308 continue;
18309 }
18310
cd9983dd 18311 struct partial_die_info *part_die
6f06d47b 18312 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18313
72bf9492
DJ
18314 /* We'll save this DIE so link it in. */
18315 part_die->die_parent = parent_die;
18316 part_die->die_sibling = NULL;
18317 part_die->die_child = NULL;
18318
18319 if (last_die && last_die == parent_die)
18320 last_die->die_child = part_die;
18321 else if (last_die)
18322 last_die->die_sibling = part_die;
18323
18324 last_die = part_die;
18325
18326 if (first_die == NULL)
18327 first_die = part_die;
18328
18329 /* Maybe add the DIE to the hash table. Not all DIEs that we
18330 find interesting need to be in the hash table, because we
18331 also have the parent/sibling/child chains; only those that we
18332 might refer to by offset later during partial symbol reading.
18333
18334 For now this means things that might have be the target of a
18335 DW_AT_specification, DW_AT_abstract_origin, or
18336 DW_AT_extension. DW_AT_extension will refer only to
18337 namespaces; DW_AT_abstract_origin refers to functions (and
18338 many things under the function DIE, but we do not recurse
18339 into function DIEs during partial symbol reading) and
18340 possibly variables as well; DW_AT_specification refers to
18341 declarations. Declarations ought to have the DW_AT_declaration
18342 flag. It happens that GCC forgets to put it in sometimes, but
18343 only for functions, not for types.
18344
18345 Adding more things than necessary to the hash table is harmless
18346 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18347 wasted time in find_partial_die, when we reread the compilation
18348 unit with load_all_dies set. */
72bf9492 18349
5afb4e99 18350 if (load_all
72929c62 18351 || abbrev->tag == DW_TAG_constant
5afb4e99 18352 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18353 || abbrev->tag == DW_TAG_variable
18354 || abbrev->tag == DW_TAG_namespace
18355 || part_die->is_declaration)
18356 {
18357 void **slot;
18358
18359 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18360 to_underlying (part_die->sect_off),
18361 INSERT);
72bf9492
DJ
18362 *slot = part_die;
18363 }
18364
72bf9492 18365 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18366 we have no reason to follow the children of structures; for other
98bfdba5
PA
18367 languages we have to, so that we can get at method physnames
18368 to infer fully qualified class names, for DW_AT_specification,
18369 and for C++ template arguments. For C++, we also look one level
18370 inside functions to find template arguments (if the name of the
18371 function does not already contain the template arguments).
bc30ff58 18372
0a4b0913
AB
18373 For Ada and Fortran, we need to scan the children of subprograms
18374 and lexical blocks as well because these languages allow the
18375 definition of nested entities that could be interesting for the
18376 debugger, such as nested subprograms for instance. */
72bf9492 18377 if (last_die->has_children
5afb4e99
DJ
18378 && (load_all
18379 || last_die->tag == DW_TAG_namespace
f55ee35c 18380 || last_die->tag == DW_TAG_module
72bf9492 18381 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18382 || (cu->language == language_cplus
18383 && last_die->tag == DW_TAG_subprogram
18384 && (last_die->name == NULL
18385 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18386 || (cu->language != language_c
18387 && (last_die->tag == DW_TAG_class_type
680b30c7 18388 || last_die->tag == DW_TAG_interface_type
72bf9492 18389 || last_die->tag == DW_TAG_structure_type
bc30ff58 18390 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18391 || ((cu->language == language_ada
18392 || cu->language == language_fortran)
bc30ff58
JB
18393 && (last_die->tag == DW_TAG_subprogram
18394 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18395 {
18396 nesting_level++;
18397 parent_die = last_die;
18398 continue;
18399 }
18400
18401 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18402 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18403
18404 /* Back to the top, do it again. */
18405 }
18406}
18407
6f06d47b
YQ
18408partial_die_info::partial_die_info (sect_offset sect_off_,
18409 struct abbrev_info *abbrev)
18410 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18411{
18412}
18413
35cc7ed7
YQ
18414/* Read a minimal amount of information into the minimal die structure.
18415 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18416
48fbe735
YQ
18417const gdb_byte *
18418partial_die_info::read (const struct die_reader_specs *reader,
18419 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18420{
dee91e82 18421 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18422 struct dwarf2_per_objfile *dwarf2_per_objfile
18423 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18424 unsigned int i;
c5aa993b 18425 int has_low_pc_attr = 0;
c906108c 18426 int has_high_pc_attr = 0;
91da1414 18427 int high_pc_relative = 0;
c906108c 18428
18a8505e 18429 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 18430 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18431 {
18a8505e
AT
18432 bool need_reprocess;
18433 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18434 info_ptr, &need_reprocess);
18435 /* String and address offsets that need to do the reprocessing have
18436 already been read at this point, so there is no need to wait until
18437 the loop terminates to do the reprocessing. */
18438 if (need_reprocess)
18439 read_attribute_reprocess (reader, &attr_vec[i]);
18440 attribute &attr = attr_vec[i];
c906108c 18441 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18442 partial symbol table. */
c906108c
SS
18443 switch (attr.name)
18444 {
18445 case DW_AT_name:
48fbe735 18446 switch (tag)
71c25dea
TT
18447 {
18448 case DW_TAG_compile_unit:
95554aad 18449 case DW_TAG_partial_unit:
348e048f 18450 case DW_TAG_type_unit:
71c25dea
TT
18451 /* Compilation units have a DW_AT_name that is a filename, not
18452 a source language identifier. */
18453 case DW_TAG_enumeration_type:
18454 case DW_TAG_enumerator:
18455 /* These tags always have simple identifiers already; no need
18456 to canonicalize them. */
48fbe735 18457 name = DW_STRING (&attr);
71c25dea
TT
18458 break;
18459 default:
48fbe735
YQ
18460 {
18461 struct objfile *objfile = dwarf2_per_objfile->objfile;
18462
18463 name
18464 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18465 &objfile->per_bfd->storage_obstack);
18466 }
71c25dea
TT
18467 break;
18468 }
c906108c 18469 break;
31ef98ae 18470 case DW_AT_linkage_name:
c906108c 18471 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18472 /* Note that both forms of linkage name might appear. We
18473 assume they will be the same, and we only store the last
18474 one we see. */
48fbe735 18475 linkage_name = DW_STRING (&attr);
c906108c
SS
18476 break;
18477 case DW_AT_low_pc:
18478 has_low_pc_attr = 1;
cd6c91b4 18479 lowpc = attr.value_as_address ();
c906108c
SS
18480 break;
18481 case DW_AT_high_pc:
18482 has_high_pc_attr = 1;
cd6c91b4
TT
18483 highpc = attr.value_as_address ();
18484 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18485 high_pc_relative = 1;
c906108c
SS
18486 break;
18487 case DW_AT_location:
0963b4bd 18488 /* Support the .debug_loc offsets. */
4fc6c0d5 18489 if (attr.form_is_block ())
8e19ed76 18490 {
48fbe735 18491 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18492 }
cd6c91b4 18493 else if (attr.form_is_section_offset ())
8e19ed76 18494 {
4d3c2250 18495 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18496 }
18497 else
18498 {
4d3c2250
KB
18499 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18500 "partial symbol information");
8e19ed76 18501 }
c906108c 18502 break;
c906108c 18503 case DW_AT_external:
48fbe735 18504 is_external = DW_UNSND (&attr);
c906108c
SS
18505 break;
18506 case DW_AT_declaration:
48fbe735 18507 is_declaration = DW_UNSND (&attr);
c906108c
SS
18508 break;
18509 case DW_AT_type:
48fbe735 18510 has_type = 1;
c906108c
SS
18511 break;
18512 case DW_AT_abstract_origin:
18513 case DW_AT_specification:
72bf9492 18514 case DW_AT_extension:
48fbe735
YQ
18515 has_specification = 1;
18516 spec_offset = dwarf2_get_ref_die_offset (&attr);
18517 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18518 || cu->per_cu->is_dwz);
c906108c
SS
18519 break;
18520 case DW_AT_sibling:
18521 /* Ignore absolute siblings, they might point outside of
18522 the current compile unit. */
18523 if (attr.form == DW_FORM_ref_addr)
b98664d3 18524 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18525 else
b9502d3f 18526 {
48fbe735 18527 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18528 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18529 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18530
18531 if (sibling_ptr < info_ptr)
b98664d3 18532 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18533 else if (sibling_ptr > reader->buffer_end)
18534 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18535 else
48fbe735 18536 sibling = sibling_ptr;
b9502d3f 18537 }
c906108c 18538 break;
fa4028e9 18539 case DW_AT_byte_size:
48fbe735 18540 has_byte_size = 1;
fa4028e9 18541 break;
ff908ebf 18542 case DW_AT_const_value:
48fbe735 18543 has_const_value = 1;
ff908ebf 18544 break;
68511cec
CES
18545 case DW_AT_calling_convention:
18546 /* DWARF doesn't provide a way to identify a program's source-level
18547 entry point. DW_AT_calling_convention attributes are only meant
18548 to describe functions' calling conventions.
18549
18550 However, because it's a necessary piece of information in
0c1b455e
TT
18551 Fortran, and before DWARF 4 DW_CC_program was the only
18552 piece of debugging information whose definition refers to
18553 a 'main program' at all, several compilers marked Fortran
18554 main programs with DW_CC_program --- even when those
18555 functions use the standard calling conventions.
18556
18557 Although DWARF now specifies a way to provide this
18558 information, we support this practice for backward
18559 compatibility. */
68511cec 18560 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18561 && cu->language == language_fortran)
48fbe735 18562 main_subprogram = 1;
68511cec 18563 break;
481860b3
GB
18564 case DW_AT_inline:
18565 if (DW_UNSND (&attr) == DW_INL_inlined
18566 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18567 may_be_inlined = 1;
481860b3 18568 break;
95554aad
TT
18569
18570 case DW_AT_import:
48fbe735 18571 if (tag == DW_TAG_imported_unit)
36586728 18572 {
48fbe735
YQ
18573 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18574 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18575 || cu->per_cu->is_dwz);
18576 }
95554aad
TT
18577 break;
18578
0c1b455e 18579 case DW_AT_main_subprogram:
48fbe735 18580 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18581 break;
18582
05caa1d2
TT
18583 case DW_AT_ranges:
18584 {
18585 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18586 but that requires a full DIE, so instead we just
18587 reimplement it. */
18588 int need_ranges_base = tag != DW_TAG_compile_unit;
18589 unsigned int ranges_offset = (DW_UNSND (&attr)
18590 + (need_ranges_base
18591 ? cu->ranges_base
18592 : 0));
18593
18594 /* Value of the DW_AT_ranges attribute is the offset in the
18595 .debug_ranges section. */
18596 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18597 nullptr))
18598 has_pc_info = 1;
18599 }
18600 break;
18601
c906108c
SS
18602 default:
18603 break;
18604 }
18605 }
18606
10d06d82
TT
18607 /* For Ada, if both the name and the linkage name appear, we prefer
18608 the latter. This lets "catch exception" work better, regardless
18609 of the order in which the name and linkage name were emitted.
18610 Really, though, this is just a workaround for the fact that gdb
18611 doesn't store both the name and the linkage name. */
18612 if (cu->language == language_ada && linkage_name != nullptr)
18613 name = linkage_name;
18614
91da1414 18615 if (high_pc_relative)
48fbe735 18616 highpc += lowpc;
91da1414 18617
9373cf26
JK
18618 if (has_low_pc_attr && has_high_pc_attr)
18619 {
18620 /* When using the GNU linker, .gnu.linkonce. sections are used to
18621 eliminate duplicate copies of functions and vtables and such.
18622 The linker will arbitrarily choose one and discard the others.
18623 The AT_*_pc values for such functions refer to local labels in
18624 these sections. If the section from that file was discarded, the
18625 labels are not in the output, so the relocs get a value of 0.
18626 If this is a discarded function, mark the pc bounds as invalid,
18627 so that GDB will ignore it. */
48fbe735 18628 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18629 {
48fbe735 18630 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18631 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18632
b98664d3 18633 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18634 "for DIE at %s [in module %s]"),
48fbe735
YQ
18635 paddress (gdbarch, lowpc),
18636 sect_offset_str (sect_off),
9d8780f0 18637 objfile_name (objfile));
9373cf26
JK
18638 }
18639 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18640 else if (lowpc >= highpc)
9373cf26 18641 {
48fbe735 18642 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18643 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18644
b98664d3 18645 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18646 "for DIE at %s [in module %s]"),
48fbe735
YQ
18647 paddress (gdbarch, lowpc),
18648 paddress (gdbarch, highpc),
18649 sect_offset_str (sect_off),
9c541725 18650 objfile_name (objfile));
9373cf26
JK
18651 }
18652 else
48fbe735 18653 has_pc_info = 1;
9373cf26 18654 }
85cbf3d3 18655
c906108c
SS
18656 return info_ptr;
18657}
18658
72bf9492
DJ
18659/* Find a cached partial DIE at OFFSET in CU. */
18660
d590ff25
YQ
18661struct partial_die_info *
18662dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18663{
18664 struct partial_die_info *lookup_die = NULL;
6f06d47b 18665 struct partial_die_info part_die (sect_off);
72bf9492 18666
9a3c8263 18667 lookup_die = ((struct partial_die_info *)
d590ff25 18668 htab_find_with_hash (partial_dies, &part_die,
9c541725 18669 to_underlying (sect_off)));
72bf9492 18670
72bf9492
DJ
18671 return lookup_die;
18672}
18673
348e048f
DE
18674/* Find a partial DIE at OFFSET, which may or may not be in CU,
18675 except in the case of .debug_types DIEs which do not reference
18676 outside their CU (they do however referencing other types via
55f1336d 18677 DW_FORM_ref_sig8). */
72bf9492 18678
122cf0f2 18679static const struct cu_partial_die_info
9c541725 18680find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18681{
518817b3
SM
18682 struct dwarf2_per_objfile *dwarf2_per_objfile
18683 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18684 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18685 struct dwarf2_per_cu_data *per_cu = NULL;
18686 struct partial_die_info *pd = NULL;
72bf9492 18687
36586728 18688 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18689 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18690 {
d590ff25 18691 pd = cu->find_partial_die (sect_off);
5afb4e99 18692 if (pd != NULL)
fb816e8b 18693 return { cu, pd };
0d99eb77
DE
18694 /* We missed recording what we needed.
18695 Load all dies and try again. */
18696 per_cu = cu->per_cu;
5afb4e99 18697 }
0d99eb77
DE
18698 else
18699 {
18700 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18701 if (cu->per_cu->is_debug_types)
0d99eb77 18702 {
9d8780f0
SM
18703 error (_("Dwarf Error: Type Unit at offset %s contains"
18704 " external reference to offset %s [in module %s].\n"),
18705 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18706 bfd_get_filename (objfile->obfd));
18707 }
9c541725 18708 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18709 dwarf2_per_objfile);
72bf9492 18710
0d99eb77
DE
18711 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18712 load_partial_comp_unit (per_cu);
ae038cb0 18713
0d99eb77 18714 per_cu->cu->last_used = 0;
d590ff25 18715 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18716 }
5afb4e99 18717
dee91e82
DE
18718 /* If we didn't find it, and not all dies have been loaded,
18719 load them all and try again. */
18720
5afb4e99
DJ
18721 if (pd == NULL && per_cu->load_all_dies == 0)
18722 {
5afb4e99 18723 per_cu->load_all_dies = 1;
fd820528
DE
18724
18725 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18726 THIS_CU->cu may already be in use. So we can't just free it and
18727 replace its DIEs with the ones we read in. Instead, we leave those
18728 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18729 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18730 set. */
dee91e82 18731 load_partial_comp_unit (per_cu);
5afb4e99 18732
d590ff25 18733 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18734 }
18735
18736 if (pd == NULL)
18737 internal_error (__FILE__, __LINE__,
9d8780f0 18738 _("could not find partial DIE %s "
3e43a32a 18739 "in cache [from module %s]\n"),
9d8780f0 18740 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18741 return { per_cu->cu, pd };
72bf9492
DJ
18742}
18743
abc72ce4
DE
18744/* See if we can figure out if the class lives in a namespace. We do
18745 this by looking for a member function; its demangled name will
18746 contain namespace info, if there is any. */
18747
18748static void
18749guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18750 struct dwarf2_cu *cu)
18751{
18752 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18753 what template types look like, because the demangler
18754 frequently doesn't give the same name as the debug info. We
18755 could fix this by only using the demangled name to get the
18756 prefix (but see comment in read_structure_type). */
18757
18758 struct partial_die_info *real_pdi;
18759 struct partial_die_info *child_pdi;
18760
18761 /* If this DIE (this DIE's specification, if any) has a parent, then
18762 we should not do this. We'll prepend the parent's fully qualified
18763 name when we create the partial symbol. */
18764
18765 real_pdi = struct_pdi;
18766 while (real_pdi->has_specification)
fb816e8b 18767 {
122cf0f2
AB
18768 auto res = find_partial_die (real_pdi->spec_offset,
18769 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18770 real_pdi = res.pdi;
18771 cu = res.cu;
18772 }
abc72ce4
DE
18773
18774 if (real_pdi->die_parent != NULL)
18775 return;
18776
18777 for (child_pdi = struct_pdi->die_child;
18778 child_pdi != NULL;
18779 child_pdi = child_pdi->die_sibling)
18780 {
18781 if (child_pdi->tag == DW_TAG_subprogram
18782 && child_pdi->linkage_name != NULL)
18783 {
43816ebc
TT
18784 gdb::unique_xmalloc_ptr<char> actual_class_name
18785 (language_class_name_from_physname (cu->language_defn,
18786 child_pdi->linkage_name));
abc72ce4
DE
18787 if (actual_class_name != NULL)
18788 {
518817b3 18789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18790 struct_pdi->name
021887d8 18791 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 18792 actual_class_name.get ());
abc72ce4
DE
18793 }
18794 break;
18795 }
18796 }
18797}
18798
52356b79
YQ
18799void
18800partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18801{
abc72ce4
DE
18802 /* Once we've fixed up a die, there's no point in doing so again.
18803 This also avoids a memory leak if we were to call
18804 guess_partial_die_structure_name multiple times. */
52356b79 18805 if (fixup_called)
abc72ce4
DE
18806 return;
18807
72bf9492
DJ
18808 /* If we found a reference attribute and the DIE has no name, try
18809 to find a name in the referred to DIE. */
18810
52356b79 18811 if (name == NULL && has_specification)
72bf9492
DJ
18812 {
18813 struct partial_die_info *spec_die;
72bf9492 18814
122cf0f2 18815 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18816 spec_die = res.pdi;
18817 cu = res.cu;
72bf9492 18818
52356b79 18819 spec_die->fixup (cu);
72bf9492
DJ
18820
18821 if (spec_die->name)
18822 {
52356b79 18823 name = spec_die->name;
72bf9492
DJ
18824
18825 /* Copy DW_AT_external attribute if it is set. */
18826 if (spec_die->is_external)
52356b79 18827 is_external = spec_die->is_external;
72bf9492
DJ
18828 }
18829 }
18830
18831 /* Set default names for some unnamed DIEs. */
72bf9492 18832
52356b79
YQ
18833 if (name == NULL && tag == DW_TAG_namespace)
18834 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18835
abc72ce4
DE
18836 /* If there is no parent die to provide a namespace, and there are
18837 children, see if we can determine the namespace from their linkage
122d1940 18838 name. */
abc72ce4 18839 if (cu->language == language_cplus
fd5866f6 18840 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18841 && die_parent == NULL
18842 && has_children
18843 && (tag == DW_TAG_class_type
18844 || tag == DW_TAG_structure_type
18845 || tag == DW_TAG_union_type))
18846 guess_partial_die_structure_name (this, cu);
abc72ce4 18847
53832f31
TT
18848 /* GCC might emit a nameless struct or union that has a linkage
18849 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18850 if (name == NULL
18851 && (tag == DW_TAG_class_type
18852 || tag == DW_TAG_interface_type
18853 || tag == DW_TAG_structure_type
18854 || tag == DW_TAG_union_type)
18855 && linkage_name != NULL)
53832f31 18856 {
43816ebc
TT
18857 gdb::unique_xmalloc_ptr<char> demangled
18858 (gdb_demangle (linkage_name, DMGL_TYPES));
18859 if (demangled != nullptr)
53832f31 18860 {
96408a79
SA
18861 const char *base;
18862
18863 /* Strip any leading namespaces/classes, keep only the base name.
18864 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18865 base = strrchr (demangled.get (), ':');
18866 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18867 base++;
18868 else
43816ebc 18869 base = demangled.get ();
96408a79 18870
518817b3 18871 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 18872 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
18873 }
18874 }
18875
52356b79 18876 fixup_called = 1;
72bf9492
DJ
18877}
18878
18a8505e
AT
18879/* Process the attributes that had to be skipped in the first round. These
18880 attributes are the ones that need str_offsets_base or addr_base attributes.
18881 They could not have been processed in the first round, because at the time
18882 the values of str_offsets_base or addr_base may not have been known. */
18883void read_attribute_reprocess (const struct die_reader_specs *reader,
18884 struct attribute *attr)
18885{
18886 struct dwarf2_cu *cu = reader->cu;
18887 switch (attr->form)
18888 {
18889 case DW_FORM_addrx:
18890 case DW_FORM_GNU_addr_index:
18891 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18892 break;
18893 case DW_FORM_strx:
18894 case DW_FORM_strx1:
18895 case DW_FORM_strx2:
18896 case DW_FORM_strx3:
18897 case DW_FORM_strx4:
18898 case DW_FORM_GNU_str_index:
18899 {
18900 unsigned int str_index = DW_UNSND (attr);
18901 if (reader->dwo_file != NULL)
18902 {
18903 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18904 DW_STRING_IS_CANONICAL (attr) = 0;
18905 }
18906 else
18907 {
18908 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18909 DW_STRING_IS_CANONICAL (attr) = 0;
18910 }
18911 break;
18912 }
18913 default:
18914 gdb_assert_not_reached (_("Unexpected DWARF form."));
18915 }
18916}
18917
a8329558 18918/* Read an attribute value described by an attribute form. */
c906108c 18919
d521ce57 18920static const gdb_byte *
dee91e82
DE
18921read_attribute_value (const struct die_reader_specs *reader,
18922 struct attribute *attr, unsigned form,
18a8505e
AT
18923 LONGEST implicit_const, const gdb_byte *info_ptr,
18924 bool *need_reprocess)
c906108c 18925{
dee91e82 18926 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18927 struct dwarf2_per_objfile *dwarf2_per_objfile
18928 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18929 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18930 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18931 bfd *abfd = reader->abfd;
e7c27a73 18932 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18933 unsigned int bytes_read;
18934 struct dwarf_block *blk;
18a8505e 18935 *need_reprocess = false;
c906108c 18936
aead7601 18937 attr->form = (enum dwarf_form) form;
a8329558 18938 switch (form)
c906108c 18939 {
c906108c 18940 case DW_FORM_ref_addr:
ae411497 18941 if (cu->header.version == 2)
4568ecf9 18942 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18943 else
4568ecf9
DE
18944 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18945 &cu->header, &bytes_read);
ae411497
TT
18946 info_ptr += bytes_read;
18947 break;
36586728
TT
18948 case DW_FORM_GNU_ref_alt:
18949 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18950 info_ptr += bytes_read;
18951 break;
ae411497 18952 case DW_FORM_addr:
e7c27a73 18953 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18954 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18955 info_ptr += bytes_read;
c906108c
SS
18956 break;
18957 case DW_FORM_block2:
7b5a2f43 18958 blk = dwarf_alloc_block (cu);
c906108c
SS
18959 blk->size = read_2_bytes (abfd, info_ptr);
18960 info_ptr += 2;
18961 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18962 info_ptr += blk->size;
18963 DW_BLOCK (attr) = blk;
18964 break;
18965 case DW_FORM_block4:
7b5a2f43 18966 blk = dwarf_alloc_block (cu);
c906108c
SS
18967 blk->size = read_4_bytes (abfd, info_ptr);
18968 info_ptr += 4;
18969 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18970 info_ptr += blk->size;
18971 DW_BLOCK (attr) = blk;
18972 break;
18973 case DW_FORM_data2:
18974 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18975 info_ptr += 2;
18976 break;
18977 case DW_FORM_data4:
18978 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18979 info_ptr += 4;
18980 break;
18981 case DW_FORM_data8:
18982 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18983 info_ptr += 8;
18984 break;
0224619f
JK
18985 case DW_FORM_data16:
18986 blk = dwarf_alloc_block (cu);
18987 blk->size = 16;
18988 blk->data = read_n_bytes (abfd, info_ptr, 16);
18989 info_ptr += 16;
18990 DW_BLOCK (attr) = blk;
18991 break;
2dc7f7b3
TT
18992 case DW_FORM_sec_offset:
18993 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18994 info_ptr += bytes_read;
18995 break;
c906108c 18996 case DW_FORM_string:
9b1c24c8 18997 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18998 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18999 info_ptr += bytes_read;
19000 break;
4bdf3d34 19001 case DW_FORM_strp:
36586728
TT
19002 if (!cu->per_cu->is_dwz)
19003 {
ed2dc618
SM
19004 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19005 abfd, info_ptr, cu_header,
36586728
TT
19006 &bytes_read);
19007 DW_STRING_IS_CANONICAL (attr) = 0;
19008 info_ptr += bytes_read;
19009 break;
19010 }
19011 /* FALLTHROUGH */
43988095
JK
19012 case DW_FORM_line_strp:
19013 if (!cu->per_cu->is_dwz)
19014 {
ed2dc618
SM
19015 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19016 abfd, info_ptr,
43988095
JK
19017 cu_header, &bytes_read);
19018 DW_STRING_IS_CANONICAL (attr) = 0;
19019 info_ptr += bytes_read;
19020 break;
19021 }
19022 /* FALLTHROUGH */
36586728
TT
19023 case DW_FORM_GNU_strp_alt:
19024 {
ed2dc618 19025 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19026 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19027 &bytes_read);
19028
ed2dc618
SM
19029 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19030 dwz, str_offset);
36586728
TT
19031 DW_STRING_IS_CANONICAL (attr) = 0;
19032 info_ptr += bytes_read;
19033 }
4bdf3d34 19034 break;
2dc7f7b3 19035 case DW_FORM_exprloc:
c906108c 19036 case DW_FORM_block:
7b5a2f43 19037 blk = dwarf_alloc_block (cu);
c906108c
SS
19038 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19039 info_ptr += bytes_read;
19040 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19041 info_ptr += blk->size;
19042 DW_BLOCK (attr) = blk;
19043 break;
19044 case DW_FORM_block1:
7b5a2f43 19045 blk = dwarf_alloc_block (cu);
c906108c
SS
19046 blk->size = read_1_byte (abfd, info_ptr);
19047 info_ptr += 1;
19048 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19049 info_ptr += blk->size;
19050 DW_BLOCK (attr) = blk;
19051 break;
19052 case DW_FORM_data1:
19053 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19054 info_ptr += 1;
19055 break;
19056 case DW_FORM_flag:
19057 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19058 info_ptr += 1;
19059 break;
2dc7f7b3
TT
19060 case DW_FORM_flag_present:
19061 DW_UNSND (attr) = 1;
19062 break;
c906108c
SS
19063 case DW_FORM_sdata:
19064 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19065 info_ptr += bytes_read;
19066 break;
19067 case DW_FORM_udata:
18a8505e 19068 case DW_FORM_rnglistx:
c906108c
SS
19069 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19070 info_ptr += bytes_read;
19071 break;
19072 case DW_FORM_ref1:
9c541725 19073 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19074 + read_1_byte (abfd, info_ptr));
c906108c
SS
19075 info_ptr += 1;
19076 break;
19077 case DW_FORM_ref2:
9c541725 19078 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19079 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19080 info_ptr += 2;
19081 break;
19082 case DW_FORM_ref4:
9c541725 19083 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19084 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19085 info_ptr += 4;
19086 break;
613e1657 19087 case DW_FORM_ref8:
9c541725 19088 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19089 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19090 info_ptr += 8;
19091 break;
55f1336d 19092 case DW_FORM_ref_sig8:
ac9ec31b 19093 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19094 info_ptr += 8;
19095 break;
c906108c 19096 case DW_FORM_ref_udata:
9c541725 19097 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19098 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19099 info_ptr += bytes_read;
19100 break;
c906108c 19101 case DW_FORM_indirect:
a8329558
KW
19102 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19103 info_ptr += bytes_read;
43988095
JK
19104 if (form == DW_FORM_implicit_const)
19105 {
19106 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19107 info_ptr += bytes_read;
19108 }
19109 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19110 info_ptr, need_reprocess);
43988095
JK
19111 break;
19112 case DW_FORM_implicit_const:
19113 DW_SND (attr) = implicit_const;
a8329558 19114 break;
336d760d 19115 case DW_FORM_addrx:
3019eac3 19116 case DW_FORM_GNU_addr_index:
18a8505e
AT
19117 *need_reprocess = true;
19118 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19119 info_ptr += bytes_read;
19120 break;
cf532bd1 19121 case DW_FORM_strx:
15f18d14
AT
19122 case DW_FORM_strx1:
19123 case DW_FORM_strx2:
19124 case DW_FORM_strx3:
19125 case DW_FORM_strx4:
3019eac3 19126 case DW_FORM_GNU_str_index:
3019eac3 19127 {
15f18d14
AT
19128 ULONGEST str_index;
19129 if (form == DW_FORM_strx1)
19130 {
19131 str_index = read_1_byte (abfd, info_ptr);
19132 info_ptr += 1;
19133 }
19134 else if (form == DW_FORM_strx2)
19135 {
19136 str_index = read_2_bytes (abfd, info_ptr);
19137 info_ptr += 2;
19138 }
19139 else if (form == DW_FORM_strx3)
19140 {
19141 str_index = read_3_bytes (abfd, info_ptr);
19142 info_ptr += 3;
19143 }
19144 else if (form == DW_FORM_strx4)
19145 {
19146 str_index = read_4_bytes (abfd, info_ptr);
19147 info_ptr += 4;
19148 }
19149 else
19150 {
19151 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19152 info_ptr += bytes_read;
19153 }
18a8505e
AT
19154 *need_reprocess = true;
19155 DW_UNSND (attr) = str_index;
19156 }
3019eac3 19157 break;
c906108c 19158 default:
8a3fe4f8 19159 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19160 dwarf_form_name (form),
19161 bfd_get_filename (abfd));
c906108c 19162 }
28e94949 19163
36586728 19164 /* Super hack. */
cd6c91b4 19165 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19166 attr->form = DW_FORM_GNU_ref_alt;
19167
28e94949
JB
19168 /* We have seen instances where the compiler tried to emit a byte
19169 size attribute of -1 which ended up being encoded as an unsigned
19170 0xffffffff. Although 0xffffffff is technically a valid size value,
19171 an object of this size seems pretty unlikely so we can relatively
19172 safely treat these cases as if the size attribute was invalid and
19173 treat them as zero by default. */
19174 if (attr->name == DW_AT_byte_size
19175 && form == DW_FORM_data4
19176 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19177 {
19178 complaint
b98664d3 19179 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19180 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19181 DW_UNSND (attr) = 0;
19182 }
28e94949 19183
c906108c
SS
19184 return info_ptr;
19185}
19186
a8329558
KW
19187/* Read an attribute described by an abbreviated attribute. */
19188
d521ce57 19189static const gdb_byte *
dee91e82
DE
19190read_attribute (const struct die_reader_specs *reader,
19191 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19192 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19193{
19194 attr->name = abbrev->name;
43988095 19195 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19196 abbrev->implicit_const, info_ptr,
19197 need_reprocess);
a8329558
KW
19198}
19199
c906108c 19200static CORE_ADDR
d521ce57 19201read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19202 unsigned int *bytes_read)
c906108c 19203{
e7c27a73 19204 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19205 CORE_ADDR retval = 0;
19206
107d2387 19207 if (cu_header->signed_addr_p)
c906108c 19208 {
107d2387
AC
19209 switch (cu_header->addr_size)
19210 {
19211 case 2:
fe1b8b76 19212 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19213 break;
19214 case 4:
fe1b8b76 19215 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19216 break;
19217 case 8:
fe1b8b76 19218 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19219 break;
19220 default:
8e65ff28 19221 internal_error (__FILE__, __LINE__,
e2e0b3e5 19222 _("read_address: bad switch, signed [in module %s]"),
659b0389 19223 bfd_get_filename (abfd));
107d2387
AC
19224 }
19225 }
19226 else
19227 {
19228 switch (cu_header->addr_size)
19229 {
19230 case 2:
fe1b8b76 19231 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19232 break;
19233 case 4:
fe1b8b76 19234 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19235 break;
19236 case 8:
fe1b8b76 19237 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19238 break;
19239 default:
8e65ff28 19240 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19241 _("read_address: bad switch, "
19242 "unsigned [in module %s]"),
659b0389 19243 bfd_get_filename (abfd));
107d2387 19244 }
c906108c 19245 }
64367e0a 19246
107d2387
AC
19247 *bytes_read = cu_header->addr_size;
19248 return retval;
c906108c
SS
19249}
19250
f7ef9339 19251/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19252 specification allows the initial length to take up either 4 bytes
19253 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19254 bytes describe the length and all offsets will be 8 bytes in length
19255 instead of 4.
19256
f7ef9339
KB
19257 An older, non-standard 64-bit format is also handled by this
19258 function. The older format in question stores the initial length
19259 as an 8-byte quantity without an escape value. Lengths greater
19260 than 2^32 aren't very common which means that the initial 4 bytes
19261 is almost always zero. Since a length value of zero doesn't make
19262 sense for the 32-bit format, this initial zero can be considered to
19263 be an escape value which indicates the presence of the older 64-bit
19264 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19265 greater than 4GB. If it becomes necessary to handle lengths
19266 somewhat larger than 4GB, we could allow other small values (such
19267 as the non-sensical values of 1, 2, and 3) to also be used as
19268 escape values indicating the presence of the old format.
f7ef9339 19269
917c78fc
MK
19270 The value returned via bytes_read should be used to increment the
19271 relevant pointer after calling read_initial_length().
c764a876 19272
613e1657
KB
19273 [ Note: read_initial_length() and read_offset() are based on the
19274 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19275 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19276 from:
19277
f7ef9339 19278 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19279
613e1657
KB
19280 This document is only a draft and is subject to change. (So beware.)
19281
f7ef9339 19282 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19283 determined empirically by examining 64-bit ELF files produced by
19284 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19285
19286 - Kevin, July 16, 2002
613e1657
KB
19287 ] */
19288
19289static LONGEST
d521ce57 19290read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19291{
fe1b8b76 19292 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19293
dd373385 19294 if (length == 0xffffffff)
613e1657 19295 {
fe1b8b76 19296 length = bfd_get_64 (abfd, buf + 4);
613e1657 19297 *bytes_read = 12;
613e1657 19298 }
dd373385 19299 else if (length == 0)
f7ef9339 19300 {
dd373385 19301 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19302 length = bfd_get_64 (abfd, buf);
f7ef9339 19303 *bytes_read = 8;
f7ef9339 19304 }
613e1657
KB
19305 else
19306 {
19307 *bytes_read = 4;
613e1657
KB
19308 }
19309
c764a876
DE
19310 return length;
19311}
dd373385 19312
c764a876
DE
19313/* Cover function for read_initial_length.
19314 Returns the length of the object at BUF, and stores the size of the
19315 initial length in *BYTES_READ and stores the size that offsets will be in
19316 *OFFSET_SIZE.
19317 If the initial length size is not equivalent to that specified in
19318 CU_HEADER then issue a complaint.
19319 This is useful when reading non-comp-unit headers. */
dd373385 19320
c764a876 19321static LONGEST
d521ce57 19322read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19323 const struct comp_unit_head *cu_header,
19324 unsigned int *bytes_read,
19325 unsigned int *offset_size)
19326{
19327 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19328
19329 gdb_assert (cu_header->initial_length_size == 4
19330 || cu_header->initial_length_size == 8
19331 || cu_header->initial_length_size == 12);
19332
19333 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19334 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19335
c764a876 19336 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19337 return length;
613e1657
KB
19338}
19339
19340/* Read an offset from the data stream. The size of the offset is
917c78fc 19341 given by cu_header->offset_size. */
613e1657
KB
19342
19343static LONGEST
d521ce57
TT
19344read_offset (bfd *abfd, const gdb_byte *buf,
19345 const struct comp_unit_head *cu_header,
891d2f0b 19346 unsigned int *bytes_read)
c764a876
DE
19347{
19348 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19349
c764a876
DE
19350 *bytes_read = cu_header->offset_size;
19351 return offset;
19352}
19353
19354/* Read an offset from the data stream. */
19355
19356static LONGEST
d521ce57 19357read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19358{
19359 LONGEST retval = 0;
19360
c764a876 19361 switch (offset_size)
613e1657
KB
19362 {
19363 case 4:
fe1b8b76 19364 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19365 break;
19366 case 8:
fe1b8b76 19367 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19368 break;
19369 default:
8e65ff28 19370 internal_error (__FILE__, __LINE__,
c764a876 19371 _("read_offset_1: bad switch [in module %s]"),
659b0389 19372 bfd_get_filename (abfd));
613e1657
KB
19373 }
19374
917c78fc 19375 return retval;
613e1657
KB
19376}
19377
d521ce57
TT
19378static const gdb_byte *
19379read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19380{
19381 /* If the size of a host char is 8 bits, we can return a pointer
19382 to the buffer, otherwise we have to copy the data to a buffer
19383 allocated on the temporary obstack. */
4bdf3d34 19384 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19385 return buf;
c906108c
SS
19386}
19387
d521ce57
TT
19388static const char *
19389read_direct_string (bfd *abfd, const gdb_byte *buf,
19390 unsigned int *bytes_read_ptr)
c906108c
SS
19391{
19392 /* If the size of a host char is 8 bits, we can return a pointer
19393 to the string, otherwise we have to copy the string to a buffer
19394 allocated on the temporary obstack. */
4bdf3d34 19395 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19396 if (*buf == '\0')
19397 {
19398 *bytes_read_ptr = 1;
19399 return NULL;
19400 }
d521ce57
TT
19401 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19402 return (const char *) buf;
4bdf3d34
JJ
19403}
19404
43988095
JK
19405/* Return pointer to string at section SECT offset STR_OFFSET with error
19406 reporting strings FORM_NAME and SECT_NAME. */
19407
d521ce57 19408static const char *
ed2dc618
SM
19409read_indirect_string_at_offset_from (struct objfile *objfile,
19410 bfd *abfd, LONGEST str_offset,
43988095
JK
19411 struct dwarf2_section_info *sect,
19412 const char *form_name,
19413 const char *sect_name)
19414{
96b79293 19415 sect->read (objfile);
43988095
JK
19416 if (sect->buffer == NULL)
19417 error (_("%s used without %s section [in module %s]"),
19418 form_name, sect_name, bfd_get_filename (abfd));
19419 if (str_offset >= sect->size)
19420 error (_("%s pointing outside of %s section [in module %s]"),
19421 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19422 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19423 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19424 return NULL;
43988095
JK
19425 return (const char *) (sect->buffer + str_offset);
19426}
19427
19428/* Return pointer to string at .debug_str offset STR_OFFSET. */
19429
19430static const char *
ed2dc618
SM
19431read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19432 bfd *abfd, LONGEST str_offset)
43988095 19433{
ed2dc618
SM
19434 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19435 abfd, str_offset,
43988095
JK
19436 &dwarf2_per_objfile->str,
19437 "DW_FORM_strp", ".debug_str");
19438}
19439
19440/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19441
19442static const char *
ed2dc618
SM
19443read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19444 bfd *abfd, LONGEST str_offset)
43988095 19445{
ed2dc618
SM
19446 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19447 abfd, str_offset,
43988095
JK
19448 &dwarf2_per_objfile->line_str,
19449 "DW_FORM_line_strp",
19450 ".debug_line_str");
c906108c
SS
19451}
19452
36586728
TT
19453/* Read a string at offset STR_OFFSET in the .debug_str section from
19454 the .dwz file DWZ. Throw an error if the offset is too large. If
19455 the string consists of a single NUL byte, return NULL; otherwise
19456 return a pointer to the string. */
19457
d521ce57 19458static const char *
ed2dc618
SM
19459read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19460 LONGEST str_offset)
36586728 19461{
96b79293 19462 dwz->str.read (objfile);
36586728
TT
19463
19464 if (dwz->str.buffer == NULL)
19465 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19466 "section [in module %s]"),
00f93c44 19467 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19468 if (str_offset >= dwz->str.size)
19469 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19470 ".debug_str section [in module %s]"),
00f93c44 19471 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19472 gdb_assert (HOST_CHAR_BIT == 8);
19473 if (dwz->str.buffer[str_offset] == '\0')
19474 return NULL;
d521ce57 19475 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19476}
19477
43988095
JK
19478/* Return pointer to string at .debug_str offset as read from BUF.
19479 BUF is assumed to be in a compilation unit described by CU_HEADER.
19480 Return *BYTES_READ_PTR count of bytes read from BUF. */
19481
d521ce57 19482static const char *
ed2dc618
SM
19483read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19484 const gdb_byte *buf,
cf2c3c16
TT
19485 const struct comp_unit_head *cu_header,
19486 unsigned int *bytes_read_ptr)
19487{
19488 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19489
ed2dc618 19490 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19491}
19492
43988095
JK
19493/* Return pointer to string at .debug_line_str offset as read from BUF.
19494 BUF is assumed to be in a compilation unit described by CU_HEADER.
19495 Return *BYTES_READ_PTR count of bytes read from BUF. */
19496
19497static const char *
ed2dc618
SM
19498read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19499 bfd *abfd, const gdb_byte *buf,
43988095
JK
19500 const struct comp_unit_head *cu_header,
19501 unsigned int *bytes_read_ptr)
19502{
19503 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19504
ed2dc618
SM
19505 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19506 str_offset);
43988095
JK
19507}
19508
3019eac3 19509/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19510 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19511 ADDR_SIZE is the size of addresses from the CU header. */
19512
19513static CORE_ADDR
ed2dc618 19514read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19515 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19516 int addr_size)
3019eac3
DE
19517{
19518 struct objfile *objfile = dwarf2_per_objfile->objfile;
19519 bfd *abfd = objfile->obfd;
19520 const gdb_byte *info_ptr;
18a8505e 19521 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19522
96b79293 19523 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
19524 if (dwarf2_per_objfile->addr.buffer == NULL)
19525 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19526 objfile_name (objfile));
18a8505e
AT
19527 if (addr_base_or_zero + addr_index * addr_size
19528 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
19529 error (_("DW_FORM_addr_index pointing outside of "
19530 ".debug_addr section [in module %s]"),
4262abfb 19531 objfile_name (objfile));
3019eac3 19532 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 19533 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19534 if (addr_size == 4)
19535 return bfd_get_32 (abfd, info_ptr);
19536 else
19537 return bfd_get_64 (abfd, info_ptr);
19538}
19539
19540/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19541
19542static CORE_ADDR
19543read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19544{
518817b3
SM
19545 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19546 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19547}
19548
19549/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19550
19551static CORE_ADDR
d521ce57 19552read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19553 unsigned int *bytes_read)
19554{
518817b3 19555 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19556 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19557
19558 return read_addr_index (cu, addr_index);
19559}
19560
3019eac3
DE
19561/* Given an index in .debug_addr, fetch the value.
19562 NOTE: This can be called during dwarf expression evaluation,
19563 long after the debug information has been read, and thus per_cu->cu
19564 may no longer exist. */
19565
19566CORE_ADDR
19567dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19568 unsigned int addr_index)
19569{
ed2dc618 19570 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19571 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19572 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19573 int addr_size;
19574
3019eac3
DE
19575 /* We need addr_base and addr_size.
19576 If we don't have PER_CU->cu, we have to get it.
19577 Nasty, but the alternative is storing the needed info in PER_CU,
19578 which at this point doesn't seem justified: it's not clear how frequently
19579 it would get used and it would increase the size of every PER_CU.
19580 Entry points like dwarf2_per_cu_addr_size do a similar thing
19581 so we're not in uncharted territory here.
19582 Alas we need to be a bit more complicated as addr_base is contained
19583 in the DIE.
19584
19585 We don't need to read the entire CU(/TU).
19586 We just need the header and top level die.
a1b64ce1 19587
3019eac3 19588 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19589 For now we skip this optimization. */
3019eac3
DE
19590
19591 if (cu != NULL)
19592 {
19593 addr_base = cu->addr_base;
19594 addr_size = cu->header.addr_size;
19595 }
19596 else
19597 {
c0ab21c2
TT
19598 cutu_reader reader (per_cu, NULL, 0, 0, false);
19599 addr_base = reader.cu->addr_base;
19600 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19601 }
19602
ed2dc618
SM
19603 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19604 addr_size);
3019eac3
DE
19605}
19606
18a8505e
AT
19607/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19608 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19609 DWO file. */
3019eac3 19610
d521ce57 19611static const char *
18a8505e
AT
19612read_str_index (struct dwarf2_cu *cu,
19613 struct dwarf2_section_info *str_section,
19614 struct dwarf2_section_info *str_offsets_section,
19615 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19616{
518817b3
SM
19617 struct dwarf2_per_objfile *dwarf2_per_objfile
19618 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19619 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19620 const char *objf_name = objfile_name (objfile);
3019eac3 19621 bfd *abfd = objfile->obfd;
d521ce57 19622 const gdb_byte *info_ptr;
3019eac3 19623 ULONGEST str_offset;
cf532bd1 19624 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19625
96b79293
TT
19626 str_section->read (objfile);
19627 str_offsets_section->read (objfile);
73869dc2 19628 if (str_section->buffer == NULL)
18a8505e 19629 error (_("%s used without %s section"
9d8780f0 19630 " in CU at offset %s [in module %s]"),
96b79293 19631 form_name, str_section->get_name (),
18a8505e 19632 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19633 if (str_offsets_section->buffer == NULL)
18a8505e 19634 error (_("%s used without %s section"
9d8780f0 19635 " in CU at offset %s [in module %s]"),
96b79293 19636 form_name, str_section->get_name (),
18a8505e 19637 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19638 info_ptr = (str_offsets_section->buffer
18a8505e 19639 + str_offsets_base
3019eac3
DE
19640 + str_index * cu->header.offset_size);
19641 if (cu->header.offset_size == 4)
19642 str_offset = bfd_get_32 (abfd, info_ptr);
19643 else
19644 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19645 if (str_offset >= str_section->size)
57d63ce2 19646 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19647 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19648 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19649 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19650}
19651
18a8505e
AT
19652/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19653
19654static const char *
19655read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19656{
19657 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19658 ? reader->cu->header.addr_size : 0;
19659 return read_str_index (reader->cu,
19660 &reader->dwo_file->sections.str,
19661 &reader->dwo_file->sections.str_offsets,
19662 str_offsets_base, str_index);
19663}
19664
19665/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19666
19667static const char *
19668read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19669{
19670 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19671 const char *objf_name = objfile_name (objfile);
19672 static const char form_name[] = "DW_FORM_GNU_str_index";
19673 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19674
19675 if (!cu->str_offsets_base.has_value ())
19676 error (_("%s used in Fission stub without %s"
19677 " in CU at offset 0x%lx [in module %s]"),
19678 form_name, str_offsets_attr_name,
19679 (long) cu->header.offset_size, objf_name);
19680
19681 return read_str_index (cu,
19682 &cu->per_cu->dwarf2_per_objfile->str,
19683 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19684 *cu->str_offsets_base, str_index);
19685}
19686
3019eac3
DE
19687/* Return the length of an LEB128 number in BUF. */
19688
19689static int
19690leb128_size (const gdb_byte *buf)
19691{
19692 const gdb_byte *begin = buf;
19693 gdb_byte byte;
19694
19695 while (1)
19696 {
19697 byte = *buf++;
19698 if ((byte & 128) == 0)
19699 return buf - begin;
19700 }
19701}
19702
c906108c 19703static void
e142c38c 19704set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19705{
19706 switch (lang)
19707 {
19708 case DW_LANG_C89:
76bee0cc 19709 case DW_LANG_C99:
0cfd832f 19710 case DW_LANG_C11:
c906108c 19711 case DW_LANG_C:
d1be3247 19712 case DW_LANG_UPC:
e142c38c 19713 cu->language = language_c;
c906108c 19714 break;
9c37b5ae 19715 case DW_LANG_Java:
c906108c 19716 case DW_LANG_C_plus_plus:
0cfd832f
MW
19717 case DW_LANG_C_plus_plus_11:
19718 case DW_LANG_C_plus_plus_14:
e142c38c 19719 cu->language = language_cplus;
c906108c 19720 break;
6aecb9c2
JB
19721 case DW_LANG_D:
19722 cu->language = language_d;
19723 break;
c906108c
SS
19724 case DW_LANG_Fortran77:
19725 case DW_LANG_Fortran90:
b21b22e0 19726 case DW_LANG_Fortran95:
f7de9aab
MW
19727 case DW_LANG_Fortran03:
19728 case DW_LANG_Fortran08:
e142c38c 19729 cu->language = language_fortran;
c906108c 19730 break;
a766d390
DE
19731 case DW_LANG_Go:
19732 cu->language = language_go;
19733 break;
c906108c 19734 case DW_LANG_Mips_Assembler:
e142c38c 19735 cu->language = language_asm;
c906108c
SS
19736 break;
19737 case DW_LANG_Ada83:
8aaf0b47 19738 case DW_LANG_Ada95:
bc5f45f8
JB
19739 cu->language = language_ada;
19740 break;
72019c9c
GM
19741 case DW_LANG_Modula2:
19742 cu->language = language_m2;
19743 break;
fe8e67fd
PM
19744 case DW_LANG_Pascal83:
19745 cu->language = language_pascal;
19746 break;
22566fbd
DJ
19747 case DW_LANG_ObjC:
19748 cu->language = language_objc;
19749 break;
c44af4eb
TT
19750 case DW_LANG_Rust:
19751 case DW_LANG_Rust_old:
19752 cu->language = language_rust;
19753 break;
c906108c
SS
19754 case DW_LANG_Cobol74:
19755 case DW_LANG_Cobol85:
c906108c 19756 default:
e142c38c 19757 cu->language = language_minimal;
c906108c
SS
19758 break;
19759 }
e142c38c 19760 cu->language_defn = language_def (cu->language);
c906108c
SS
19761}
19762
19763/* Return the named attribute or NULL if not there. */
19764
19765static struct attribute *
e142c38c 19766dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19767{
a48e046c 19768 for (;;)
c906108c 19769 {
a48e046c
TT
19770 unsigned int i;
19771 struct attribute *spec = NULL;
19772
19773 for (i = 0; i < die->num_attrs; ++i)
19774 {
19775 if (die->attrs[i].name == name)
19776 return &die->attrs[i];
19777 if (die->attrs[i].name == DW_AT_specification
19778 || die->attrs[i].name == DW_AT_abstract_origin)
19779 spec = &die->attrs[i];
19780 }
19781
19782 if (!spec)
19783 break;
c906108c 19784
f2f0e013 19785 die = follow_die_ref (die, spec, &cu);
f2f0e013 19786 }
c5aa993b 19787
c906108c
SS
19788 return NULL;
19789}
19790
348e048f
DE
19791/* Return the named attribute or NULL if not there,
19792 but do not follow DW_AT_specification, etc.
19793 This is for use in contexts where we're reading .debug_types dies.
19794 Following DW_AT_specification, DW_AT_abstract_origin will take us
19795 back up the chain, and we want to go down. */
19796
19797static struct attribute *
45e58e77 19798dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19799{
19800 unsigned int i;
19801
19802 for (i = 0; i < die->num_attrs; ++i)
19803 if (die->attrs[i].name == name)
19804 return &die->attrs[i];
19805
19806 return NULL;
19807}
19808
7d45c7c3
KB
19809/* Return the string associated with a string-typed attribute, or NULL if it
19810 is either not found or is of an incorrect type. */
19811
19812static const char *
19813dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19814{
19815 struct attribute *attr;
19816 const char *str = NULL;
19817
19818 attr = dwarf2_attr (die, name, cu);
19819
19820 if (attr != NULL)
19821 {
43988095 19822 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19823 || attr->form == DW_FORM_string
cf532bd1 19824 || attr->form == DW_FORM_strx
8fe0f950
AT
19825 || attr->form == DW_FORM_strx1
19826 || attr->form == DW_FORM_strx2
19827 || attr->form == DW_FORM_strx3
19828 || attr->form == DW_FORM_strx4
b3340438 19829 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19830 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19831 str = DW_STRING (attr);
19832 else
b98664d3 19833 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19834 "DIE at %s in module %s"),
19835 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19836 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19837 }
19838
19839 return str;
19840}
19841
a084a2a6 19842/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19843 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19844static const char *
19845dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19846{
19847 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19848 if (dwo_name == nullptr)
19849 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19850 return dwo_name;
19851}
19852
05cf31d1
JB
19853/* Return non-zero iff the attribute NAME is defined for the given DIE,
19854 and holds a non-zero value. This function should only be used for
2dc7f7b3 19855 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19856
19857static int
19858dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19859{
19860 struct attribute *attr = dwarf2_attr (die, name, cu);
19861
19862 return (attr && DW_UNSND (attr));
19863}
19864
3ca72b44 19865static int
e142c38c 19866die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19867{
05cf31d1
JB
19868 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19869 which value is non-zero. However, we have to be careful with
19870 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19871 (via dwarf2_flag_true_p) follows this attribute. So we may
19872 end up accidently finding a declaration attribute that belongs
19873 to a different DIE referenced by the specification attribute,
19874 even though the given DIE does not have a declaration attribute. */
19875 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19876 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19877}
19878
63d06c5c 19879/* Return the die giving the specification for DIE, if there is
f2f0e013 19880 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19881 containing the return value on output. If there is no
19882 specification, but there is an abstract origin, that is
19883 returned. */
63d06c5c
DC
19884
19885static struct die_info *
f2f0e013 19886die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19887{
f2f0e013
DJ
19888 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19889 *spec_cu);
63d06c5c 19890
edb3359d
DJ
19891 if (spec_attr == NULL)
19892 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19893
63d06c5c
DC
19894 if (spec_attr == NULL)
19895 return NULL;
19896 else
f2f0e013 19897 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19898}
c906108c 19899
527f3840
JK
19900/* Stub for free_line_header to match void * callback types. */
19901
19902static void
19903free_line_header_voidp (void *arg)
19904{
9a3c8263 19905 struct line_header *lh = (struct line_header *) arg;
527f3840 19906
fff8551c 19907 delete lh;
527f3840
JK
19908}
19909
fff8551c
PA
19910void
19911line_header::add_include_dir (const char *include_dir)
c906108c 19912{
27e0867f 19913 if (dwarf_line_debug >= 2)
7ba99d21
AT
19914 {
19915 size_t new_size;
19916 if (version >= 5)
19917 new_size = m_include_dirs.size ();
19918 else
19919 new_size = m_include_dirs.size () + 1;
19920 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19921 new_size, include_dir);
19922 }
19923 m_include_dirs.push_back (include_dir);
debd256d 19924}
6e70227d 19925
fff8551c
PA
19926void
19927line_header::add_file_name (const char *name,
ecfb656c 19928 dir_index d_index,
fff8551c
PA
19929 unsigned int mod_time,
19930 unsigned int length)
debd256d 19931{
27e0867f 19932 if (dwarf_line_debug >= 2)
7ba99d21
AT
19933 {
19934 size_t new_size;
19935 if (version >= 5)
19936 new_size = file_names_size ();
19937 else
19938 new_size = file_names_size () + 1;
19939 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
19940 new_size, name);
19941 }
19942 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19943}
6e70227d 19944
83769d0b 19945/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19946
19947static struct dwarf2_section_info *
19948get_debug_line_section (struct dwarf2_cu *cu)
19949{
19950 struct dwarf2_section_info *section;
518817b3
SM
19951 struct dwarf2_per_objfile *dwarf2_per_objfile
19952 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19953
19954 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19955 DWO file. */
19956 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19957 section = &cu->dwo_unit->dwo_file->sections.line;
19958 else if (cu->per_cu->is_dwz)
19959 {
ed2dc618 19960 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19961
19962 section = &dwz->line;
19963 }
19964 else
19965 section = &dwarf2_per_objfile->line;
19966
19967 return section;
19968}
19969
43988095
JK
19970/* Read directory or file name entry format, starting with byte of
19971 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19972 entries count and the entries themselves in the described entry
19973 format. */
19974
19975static void
ed2dc618
SM
19976read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19977 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19978 struct line_header *lh,
19979 const struct comp_unit_head *cu_header,
19980 void (*callback) (struct line_header *lh,
19981 const char *name,
ecfb656c 19982 dir_index d_index,
43988095
JK
19983 unsigned int mod_time,
19984 unsigned int length))
19985{
19986 gdb_byte format_count, formati;
19987 ULONGEST data_count, datai;
19988 const gdb_byte *buf = *bufp;
19989 const gdb_byte *format_header_data;
43988095
JK
19990 unsigned int bytes_read;
19991
19992 format_count = read_1_byte (abfd, buf);
19993 buf += 1;
19994 format_header_data = buf;
19995 for (formati = 0; formati < format_count; formati++)
19996 {
19997 read_unsigned_leb128 (abfd, buf, &bytes_read);
19998 buf += bytes_read;
19999 read_unsigned_leb128 (abfd, buf, &bytes_read);
20000 buf += bytes_read;
20001 }
20002
20003 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20004 buf += bytes_read;
20005 for (datai = 0; datai < data_count; datai++)
20006 {
20007 const gdb_byte *format = format_header_data;
20008 struct file_entry fe;
20009
43988095
JK
20010 for (formati = 0; formati < format_count; formati++)
20011 {
ecfb656c 20012 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20013 format += bytes_read;
43988095 20014
ecfb656c 20015 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20016 format += bytes_read;
ecfb656c
PA
20017
20018 gdb::optional<const char *> string;
20019 gdb::optional<unsigned int> uint;
20020
43988095
JK
20021 switch (form)
20022 {
20023 case DW_FORM_string:
ecfb656c 20024 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20025 buf += bytes_read;
20026 break;
20027
20028 case DW_FORM_line_strp:
ed2dc618
SM
20029 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20030 abfd, buf,
ecfb656c
PA
20031 cu_header,
20032 &bytes_read));
43988095
JK
20033 buf += bytes_read;
20034 break;
20035
20036 case DW_FORM_data1:
ecfb656c 20037 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20038 buf += 1;
20039 break;
20040
20041 case DW_FORM_data2:
ecfb656c 20042 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20043 buf += 2;
20044 break;
20045
20046 case DW_FORM_data4:
ecfb656c 20047 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20048 buf += 4;
20049 break;
20050
20051 case DW_FORM_data8:
ecfb656c 20052 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20053 buf += 8;
20054 break;
20055
7ba99d21
AT
20056 case DW_FORM_data16:
20057 /* This is used for MD5, but file_entry does not record MD5s. */
20058 buf += 16;
20059 break;
20060
43988095 20061 case DW_FORM_udata:
ecfb656c 20062 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20063 buf += bytes_read;
20064 break;
20065
20066 case DW_FORM_block:
20067 /* It is valid only for DW_LNCT_timestamp which is ignored by
20068 current GDB. */
20069 break;
20070 }
ecfb656c
PA
20071
20072 switch (content_type)
20073 {
20074 case DW_LNCT_path:
20075 if (string.has_value ())
20076 fe.name = *string;
20077 break;
20078 case DW_LNCT_directory_index:
20079 if (uint.has_value ())
20080 fe.d_index = (dir_index) *uint;
20081 break;
20082 case DW_LNCT_timestamp:
20083 if (uint.has_value ())
20084 fe.mod_time = *uint;
20085 break;
20086 case DW_LNCT_size:
20087 if (uint.has_value ())
20088 fe.length = *uint;
20089 break;
20090 case DW_LNCT_MD5:
20091 break;
20092 default:
b98664d3 20093 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20094 pulongest (content_type));
20095 }
43988095
JK
20096 }
20097
ecfb656c 20098 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20099 }
20100
20101 *bufp = buf;
20102}
20103
debd256d 20104/* Read the statement program header starting at OFFSET in
3019eac3 20105 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20106 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20107 Returns NULL if there is a problem reading the header, e.g., if it
20108 has a version we don't understand.
debd256d
JB
20109
20110 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20111 the returned object point into the dwarf line section buffer,
20112 and must not be freed. */
ae2de4f8 20113
fff8551c 20114static line_header_up
9c541725 20115dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20116{
d521ce57 20117 const gdb_byte *line_ptr;
c764a876 20118 unsigned int bytes_read, offset_size;
debd256d 20119 int i;
d521ce57 20120 const char *cur_dir, *cur_file;
3019eac3
DE
20121 struct dwarf2_section_info *section;
20122 bfd *abfd;
518817b3
SM
20123 struct dwarf2_per_objfile *dwarf2_per_objfile
20124 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20125
36586728 20126 section = get_debug_line_section (cu);
96b79293 20127 section->read (dwarf2_per_objfile->objfile);
3019eac3 20128 if (section->buffer == NULL)
debd256d 20129 {
3019eac3 20130 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20131 complaint (_("missing .debug_line.dwo section"));
3019eac3 20132 else
b98664d3 20133 complaint (_("missing .debug_line section"));
debd256d
JB
20134 return 0;
20135 }
20136
fceca515
DE
20137 /* We can't do this until we know the section is non-empty.
20138 Only then do we know we have such a section. */
96b79293 20139 abfd = section->get_bfd_owner ();
fceca515 20140
a738430d
MK
20141 /* Make sure that at least there's room for the total_length field.
20142 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20143 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20144 {
4d3c2250 20145 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20146 return 0;
20147 }
20148
fff8551c 20149 line_header_up lh (new line_header ());
debd256d 20150
9c541725 20151 lh->sect_off = sect_off;
527f3840
JK
20152 lh->offset_in_dwz = cu->per_cu->is_dwz;
20153
9c541725 20154 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20155
a738430d 20156 /* Read in the header. */
6e70227d 20157 lh->total_length =
c764a876
DE
20158 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20159 &bytes_read, &offset_size);
debd256d 20160 line_ptr += bytes_read;
7ba99d21
AT
20161
20162 const gdb_byte *start_here = line_ptr;
20163
3019eac3 20164 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20165 {
4d3c2250 20166 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20167 return 0;
20168 }
7ba99d21 20169 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20170 lh->version = read_2_bytes (abfd, line_ptr);
20171 line_ptr += 2;
43988095 20172 if (lh->version > 5)
cd366ee8
DE
20173 {
20174 /* This is a version we don't understand. The format could have
20175 changed in ways we don't handle properly so just punt. */
b98664d3 20176 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20177 return NULL;
20178 }
43988095
JK
20179 if (lh->version >= 5)
20180 {
20181 gdb_byte segment_selector_size;
20182
20183 /* Skip address size. */
20184 read_1_byte (abfd, line_ptr);
20185 line_ptr += 1;
20186
20187 segment_selector_size = read_1_byte (abfd, line_ptr);
20188 line_ptr += 1;
20189 if (segment_selector_size != 0)
20190 {
b98664d3 20191 complaint (_("unsupported segment selector size %u "
43988095
JK
20192 "in .debug_line section"),
20193 segment_selector_size);
20194 return NULL;
20195 }
20196 }
c764a876
DE
20197 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20198 line_ptr += offset_size;
7ba99d21 20199 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20200 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20201 line_ptr += 1;
2dc7f7b3
TT
20202 if (lh->version >= 4)
20203 {
20204 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20205 line_ptr += 1;
20206 }
20207 else
20208 lh->maximum_ops_per_instruction = 1;
20209
20210 if (lh->maximum_ops_per_instruction == 0)
20211 {
20212 lh->maximum_ops_per_instruction = 1;
b98664d3 20213 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20214 "in `.debug_line' section"));
2dc7f7b3
TT
20215 }
20216
debd256d
JB
20217 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20218 line_ptr += 1;
20219 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20220 line_ptr += 1;
20221 lh->line_range = read_1_byte (abfd, line_ptr);
20222 line_ptr += 1;
20223 lh->opcode_base = read_1_byte (abfd, line_ptr);
20224 line_ptr += 1;
fff8551c 20225 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20226
20227 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20228 for (i = 1; i < lh->opcode_base; ++i)
20229 {
20230 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20231 line_ptr += 1;
20232 }
20233
43988095 20234 if (lh->version >= 5)
debd256d 20235 {
43988095 20236 /* Read directory table. */
ed2dc618
SM
20237 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20238 &cu->header,
b926417a 20239 [] (struct line_header *header, const char *name,
ecfb656c 20240 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20241 unsigned int length)
20242 {
b926417a 20243 header->add_include_dir (name);
fff8551c 20244 });
debd256d 20245
43988095 20246 /* Read file name table. */
ed2dc618
SM
20247 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20248 &cu->header,
b926417a 20249 [] (struct line_header *header, const char *name,
ecfb656c 20250 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20251 unsigned int length)
20252 {
b926417a 20253 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20254 });
43988095
JK
20255 }
20256 else
debd256d 20257 {
43988095
JK
20258 /* Read directory table. */
20259 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20260 {
20261 line_ptr += bytes_read;
fff8551c 20262 lh->add_include_dir (cur_dir);
43988095 20263 }
debd256d
JB
20264 line_ptr += bytes_read;
20265
43988095
JK
20266 /* Read file name table. */
20267 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20268 {
ecfb656c
PA
20269 unsigned int mod_time, length;
20270 dir_index d_index;
43988095
JK
20271
20272 line_ptr += bytes_read;
ecfb656c 20273 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20274 line_ptr += bytes_read;
20275 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20276 line_ptr += bytes_read;
20277 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20278 line_ptr += bytes_read;
20279
ecfb656c 20280 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20281 }
20282 line_ptr += bytes_read;
debd256d 20283 }
debd256d 20284
3019eac3 20285 if (line_ptr > (section->buffer + section->size))
b98664d3 20286 complaint (_("line number info header doesn't "
3e43a32a 20287 "fit in `.debug_line' section"));
debd256d 20288
debd256d
JB
20289 return lh;
20290}
c906108c 20291
c6da4cef 20292/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20293 Return the file name of the psymtab for the given file_entry.
c6da4cef 20294 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20295 If space for the result is malloc'd, *NAME_HOLDER will be set.
20296 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20297
d521ce57 20298static const char *
7ba99d21 20299psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20300 const dwarf2_psymtab *pst,
c89b44cd
TT
20301 const char *comp_dir,
20302 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20303{
d521ce57
TT
20304 const char *include_name = fe.name;
20305 const char *include_name_to_compare = include_name;
72b9f47f 20306 const char *pst_filename;
c6da4cef
DE
20307 int file_is_pst;
20308
8c43009f 20309 const char *dir_name = fe.include_dir (lh);
c6da4cef 20310
c89b44cd 20311 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20312 if (!IS_ABSOLUTE_PATH (include_name)
20313 && (dir_name != NULL || comp_dir != NULL))
20314 {
20315 /* Avoid creating a duplicate psymtab for PST.
20316 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20317 Before we do the comparison, however, we need to account
20318 for DIR_NAME and COMP_DIR.
20319 First prepend dir_name (if non-NULL). If we still don't
20320 have an absolute path prepend comp_dir (if non-NULL).
20321 However, the directory we record in the include-file's
20322 psymtab does not contain COMP_DIR (to match the
20323 corresponding symtab(s)).
20324
20325 Example:
20326
20327 bash$ cd /tmp
20328 bash$ gcc -g ./hello.c
20329 include_name = "hello.c"
20330 dir_name = "."
20331 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20332 DW_AT_name = "./hello.c"
20333
20334 */
c6da4cef
DE
20335
20336 if (dir_name != NULL)
20337 {
c89b44cd
TT
20338 name_holder->reset (concat (dir_name, SLASH_STRING,
20339 include_name, (char *) NULL));
20340 include_name = name_holder->get ();
c6da4cef 20341 include_name_to_compare = include_name;
c6da4cef
DE
20342 }
20343 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20344 {
c89b44cd
TT
20345 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20346 include_name, (char *) NULL));
20347 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20348 }
20349 }
20350
20351 pst_filename = pst->filename;
c89b44cd 20352 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20353 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20354 {
c89b44cd
TT
20355 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20356 pst_filename, (char *) NULL));
20357 pst_filename = copied_name.get ();
c6da4cef
DE
20358 }
20359
1e3fad37 20360 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20361
c6da4cef
DE
20362 if (file_is_pst)
20363 return NULL;
20364 return include_name;
20365}
20366
d9b3de22
DE
20367/* State machine to track the state of the line number program. */
20368
6f77053d 20369class lnp_state_machine
d9b3de22 20370{
6f77053d
PA
20371public:
20372 /* Initialize a machine state for the start of a line number
20373 program. */
804d2729
TT
20374 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20375 bool record_lines_p);
6f77053d 20376
8c43009f
PA
20377 file_entry *current_file ()
20378 {
20379 /* lh->file_names is 0-based, but the file name numbers in the
20380 statement program are 1-based. */
6f77053d
PA
20381 return m_line_header->file_name_at (m_file);
20382 }
20383
20384 /* Record the line in the state machine. END_SEQUENCE is true if
20385 we're processing the end of a sequence. */
20386 void record_line (bool end_sequence);
20387
7ab6656f
OJ
20388 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20389 nop-out rest of the lines in this sequence. */
6f77053d
PA
20390 void check_line_address (struct dwarf2_cu *cu,
20391 const gdb_byte *line_ptr,
7ab6656f 20392 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20393
20394 void handle_set_discriminator (unsigned int discriminator)
20395 {
20396 m_discriminator = discriminator;
20397 m_line_has_non_zero_discriminator |= discriminator != 0;
20398 }
20399
20400 /* Handle DW_LNE_set_address. */
20401 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20402 {
20403 m_op_index = 0;
20404 address += baseaddr;
20405 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20406 }
20407
20408 /* Handle DW_LNS_advance_pc. */
20409 void handle_advance_pc (CORE_ADDR adjust);
20410
20411 /* Handle a special opcode. */
20412 void handle_special_opcode (unsigned char op_code);
20413
20414 /* Handle DW_LNS_advance_line. */
20415 void handle_advance_line (int line_delta)
20416 {
20417 advance_line (line_delta);
20418 }
20419
20420 /* Handle DW_LNS_set_file. */
20421 void handle_set_file (file_name_index file);
20422
20423 /* Handle DW_LNS_negate_stmt. */
20424 void handle_negate_stmt ()
20425 {
20426 m_is_stmt = !m_is_stmt;
20427 }
20428
20429 /* Handle DW_LNS_const_add_pc. */
20430 void handle_const_add_pc ();
20431
20432 /* Handle DW_LNS_fixed_advance_pc. */
20433 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20434 {
20435 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20436 m_op_index = 0;
20437 }
20438
20439 /* Handle DW_LNS_copy. */
20440 void handle_copy ()
20441 {
20442 record_line (false);
20443 m_discriminator = 0;
20444 }
20445
20446 /* Handle DW_LNE_end_sequence. */
20447 void handle_end_sequence ()
20448 {
804d2729 20449 m_currently_recording_lines = true;
6f77053d
PA
20450 }
20451
20452private:
20453 /* Advance the line by LINE_DELTA. */
20454 void advance_line (int line_delta)
20455 {
20456 m_line += line_delta;
20457
20458 if (line_delta != 0)
20459 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20460 }
20461
804d2729
TT
20462 struct dwarf2_cu *m_cu;
20463
6f77053d
PA
20464 gdbarch *m_gdbarch;
20465
20466 /* True if we're recording lines.
20467 Otherwise we're building partial symtabs and are just interested in
20468 finding include files mentioned by the line number program. */
20469 bool m_record_lines_p;
20470
8c43009f 20471 /* The line number header. */
6f77053d 20472 line_header *m_line_header;
8c43009f 20473
6f77053d
PA
20474 /* These are part of the standard DWARF line number state machine,
20475 and initialized according to the DWARF spec. */
d9b3de22 20476
6f77053d 20477 unsigned char m_op_index = 0;
7ba99d21
AT
20478 /* The line table index of the current file. */
20479 file_name_index m_file = 1;
6f77053d
PA
20480 unsigned int m_line = 1;
20481
20482 /* These are initialized in the constructor. */
20483
20484 CORE_ADDR m_address;
20485 bool m_is_stmt;
20486 unsigned int m_discriminator;
d9b3de22
DE
20487
20488 /* Additional bits of state we need to track. */
20489
20490 /* The last file that we called dwarf2_start_subfile for.
20491 This is only used for TLLs. */
6f77053d 20492 unsigned int m_last_file = 0;
d9b3de22 20493 /* The last file a line number was recorded for. */
6f77053d 20494 struct subfile *m_last_subfile = NULL;
d9b3de22 20495
804d2729
TT
20496 /* When true, record the lines we decode. */
20497 bool m_currently_recording_lines = false;
d9b3de22
DE
20498
20499 /* The last line number that was recorded, used to coalesce
20500 consecutive entries for the same line. This can happen, for
20501 example, when discriminators are present. PR 17276. */
6f77053d
PA
20502 unsigned int m_last_line = 0;
20503 bool m_line_has_non_zero_discriminator = false;
8c43009f 20504};
d9b3de22 20505
6f77053d
PA
20506void
20507lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20508{
20509 CORE_ADDR addr_adj = (((m_op_index + adjust)
20510 / m_line_header->maximum_ops_per_instruction)
20511 * m_line_header->minimum_instruction_length);
20512 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20513 m_op_index = ((m_op_index + adjust)
20514 % m_line_header->maximum_ops_per_instruction);
20515}
d9b3de22 20516
6f77053d
PA
20517void
20518lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20519{
6f77053d
PA
20520 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20521 CORE_ADDR addr_adj = (((m_op_index
20522 + (adj_opcode / m_line_header->line_range))
20523 / m_line_header->maximum_ops_per_instruction)
20524 * m_line_header->minimum_instruction_length);
20525 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20526 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20527 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20528
6f77053d
PA
20529 int line_delta = (m_line_header->line_base
20530 + (adj_opcode % m_line_header->line_range));
20531 advance_line (line_delta);
20532 record_line (false);
20533 m_discriminator = 0;
20534}
d9b3de22 20535
6f77053d
PA
20536void
20537lnp_state_machine::handle_set_file (file_name_index file)
20538{
20539 m_file = file;
20540
20541 const file_entry *fe = current_file ();
20542 if (fe == NULL)
20543 dwarf2_debug_line_missing_file_complaint ();
20544 else if (m_record_lines_p)
20545 {
20546 const char *dir = fe->include_dir (m_line_header);
20547
c24bdb02 20548 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20549 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20550 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20551 }
20552}
20553
20554void
20555lnp_state_machine::handle_const_add_pc ()
20556{
20557 CORE_ADDR adjust
20558 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20559
20560 CORE_ADDR addr_adj
20561 = (((m_op_index + adjust)
20562 / m_line_header->maximum_ops_per_instruction)
20563 * m_line_header->minimum_instruction_length);
20564
20565 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20566 m_op_index = ((m_op_index + adjust)
20567 % m_line_header->maximum_ops_per_instruction);
20568}
d9b3de22 20569
a05a36a5
DE
20570/* Return non-zero if we should add LINE to the line number table.
20571 LINE is the line to add, LAST_LINE is the last line that was added,
20572 LAST_SUBFILE is the subfile for LAST_LINE.
20573 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20574 had a non-zero discriminator.
20575
20576 We have to be careful in the presence of discriminators.
20577 E.g., for this line:
20578
20579 for (i = 0; i < 100000; i++);
20580
20581 clang can emit four line number entries for that one line,
20582 each with a different discriminator.
20583 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20584
20585 However, we want gdb to coalesce all four entries into one.
20586 Otherwise the user could stepi into the middle of the line and
20587 gdb would get confused about whether the pc really was in the
20588 middle of the line.
20589
20590 Things are further complicated by the fact that two consecutive
20591 line number entries for the same line is a heuristic used by gcc
20592 to denote the end of the prologue. So we can't just discard duplicate
20593 entries, we have to be selective about it. The heuristic we use is
20594 that we only collapse consecutive entries for the same line if at least
20595 one of those entries has a non-zero discriminator. PR 17276.
20596
20597 Note: Addresses in the line number state machine can never go backwards
20598 within one sequence, thus this coalescing is ok. */
20599
20600static int
804d2729
TT
20601dwarf_record_line_p (struct dwarf2_cu *cu,
20602 unsigned int line, unsigned int last_line,
a05a36a5
DE
20603 int line_has_non_zero_discriminator,
20604 struct subfile *last_subfile)
20605{
c24bdb02 20606 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20607 return 1;
20608 if (line != last_line)
20609 return 1;
20610 /* Same line for the same file that we've seen already.
20611 As a last check, for pr 17276, only record the line if the line
20612 has never had a non-zero discriminator. */
20613 if (!line_has_non_zero_discriminator)
20614 return 1;
20615 return 0;
20616}
20617
804d2729
TT
20618/* Use the CU's builder to record line number LINE beginning at
20619 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20620
20621static void
d9b3de22
DE
20622dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20623 unsigned int line, CORE_ADDR address,
804d2729 20624 struct dwarf2_cu *cu)
252a6764
DE
20625{
20626 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20627
27e0867f
DE
20628 if (dwarf_line_debug)
20629 {
20630 fprintf_unfiltered (gdb_stdlog,
20631 "Recording line %u, file %s, address %s\n",
20632 line, lbasename (subfile->name),
20633 paddress (gdbarch, address));
20634 }
20635
804d2729 20636 if (cu != nullptr)
c24bdb02 20637 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20638}
20639
20640/* Subroutine of dwarf_decode_lines_1 to simplify it.
20641 Mark the end of a set of line number records.
d9b3de22 20642 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20643 If SUBFILE is NULL the request is ignored. */
20644
20645static void
20646dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20647 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20648{
27e0867f
DE
20649 if (subfile == NULL)
20650 return;
20651
20652 if (dwarf_line_debug)
20653 {
20654 fprintf_unfiltered (gdb_stdlog,
20655 "Finishing current line, file %s, address %s\n",
20656 lbasename (subfile->name),
20657 paddress (gdbarch, address));
20658 }
20659
804d2729 20660 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20661}
20662
6f77053d
PA
20663void
20664lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20665{
d9b3de22
DE
20666 if (dwarf_line_debug)
20667 {
20668 fprintf_unfiltered (gdb_stdlog,
20669 "Processing actual line %u: file %u,"
94a72be7 20670 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20671 m_line, m_file,
6f77053d 20672 paddress (m_gdbarch, m_address),
94a72be7
AB
20673 m_is_stmt, m_discriminator,
20674 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20675 }
20676
6f77053d 20677 file_entry *fe = current_file ();
8c43009f
PA
20678
20679 if (fe == NULL)
d9b3de22
DE
20680 dwarf2_debug_line_missing_file_complaint ();
20681 /* For now we ignore lines not starting on an instruction boundary.
20682 But not when processing end_sequence for compatibility with the
20683 previous version of the code. */
6f77053d 20684 else if (m_op_index == 0 || end_sequence)
d9b3de22 20685 {
8c43009f 20686 fe->included_p = 1;
94a72be7
AB
20687 if (m_record_lines_p
20688 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 20689 {
c24bdb02 20690 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20691 || end_sequence)
d9b3de22 20692 {
804d2729
TT
20693 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20694 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20695 }
20696
20697 if (!end_sequence)
20698 {
804d2729 20699 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20700 m_line_has_non_zero_discriminator,
20701 m_last_subfile))
d9b3de22 20702 {
c24bdb02 20703 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20704 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20705 builder->get_current_subfile (),
6f77053d 20706 m_line, m_address,
804d2729 20707 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20708 }
c24bdb02 20709 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20710 m_last_line = m_line;
d9b3de22
DE
20711 }
20712 }
20713 }
20714}
20715
804d2729
TT
20716lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20717 line_header *lh, bool record_lines_p)
d9b3de22 20718{
804d2729 20719 m_cu = cu;
6f77053d
PA
20720 m_gdbarch = arch;
20721 m_record_lines_p = record_lines_p;
20722 m_line_header = lh;
d9b3de22 20723
804d2729 20724 m_currently_recording_lines = true;
d9b3de22 20725
d9b3de22
DE
20726 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20727 was a line entry for it so that the backend has a chance to adjust it
20728 and also record it in case it needs it. This is currently used by MIPS
20729 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20730 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20731 m_is_stmt = lh->default_is_stmt;
20732 m_discriminator = 0;
252a6764
DE
20733}
20734
6f77053d
PA
20735void
20736lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20737 const gdb_byte *line_ptr,
7ab6656f 20738 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20739{
7ab6656f
OJ
20740 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20741 the pc range of the CU. However, we restrict the test to only ADDRESS
20742 values of zero to preserve GDB's previous behaviour which is to handle
20743 the specific case of a function being GC'd by the linker. */
924c2928 20744
7ab6656f 20745 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20746 {
20747 /* This line table is for a function which has been
20748 GCd by the linker. Ignore it. PR gdb/12528 */
20749
518817b3 20750 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20751 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20752
b98664d3 20753 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20754 line_offset, objfile_name (objfile));
804d2729
TT
20755 m_currently_recording_lines = false;
20756 /* Note: m_currently_recording_lines is left as false until we see
20757 DW_LNE_end_sequence. */
924c2928
DE
20758 }
20759}
20760
f3f5162e 20761/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20762 Process the line number information in LH.
20763 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20764 program in order to set included_p for every referenced header. */
debd256d 20765
c906108c 20766static void
43f3e411
DE
20767dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20768 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20769{
d521ce57
TT
20770 const gdb_byte *line_ptr, *extended_end;
20771 const gdb_byte *line_end;
a8c50c1f 20772 unsigned int bytes_read, extended_len;
699ca60a 20773 unsigned char op_code, extended_op;
e142c38c 20774 CORE_ADDR baseaddr;
518817b3 20775 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20776 bfd *abfd = objfile->obfd;
fbf65064 20777 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20778 /* True if we're recording line info (as opposed to building partial
20779 symtabs and just interested in finding include files mentioned by
20780 the line number program). */
20781 bool record_lines_p = !decode_for_pst_p;
e142c38c 20782
b3b3bada 20783 baseaddr = objfile->text_section_offset ();
c906108c 20784
debd256d
JB
20785 line_ptr = lh->statement_program_start;
20786 line_end = lh->statement_program_end;
c906108c
SS
20787
20788 /* Read the statement sequences until there's nothing left. */
20789 while (line_ptr < line_end)
20790 {
6f77053d
PA
20791 /* The DWARF line number program state machine. Reset the state
20792 machine at the start of each sequence. */
804d2729 20793 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20794 bool end_sequence = false;
d9b3de22 20795
8c43009f 20796 if (record_lines_p)
c906108c 20797 {
8c43009f
PA
20798 /* Start a subfile for the current file of the state
20799 machine. */
20800 const file_entry *fe = state_machine.current_file ();
20801
20802 if (fe != NULL)
804d2729 20803 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20804 }
20805
a738430d 20806 /* Decode the table. */
d9b3de22 20807 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20808 {
20809 op_code = read_1_byte (abfd, line_ptr);
20810 line_ptr += 1;
9aa1fe7e 20811
debd256d 20812 if (op_code >= lh->opcode_base)
6e70227d 20813 {
8e07a239 20814 /* Special opcode. */
6f77053d 20815 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20816 }
20817 else switch (op_code)
c906108c
SS
20818 {
20819 case DW_LNS_extended_op:
3e43a32a
MS
20820 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20821 &bytes_read);
473b7be6 20822 line_ptr += bytes_read;
a8c50c1f 20823 extended_end = line_ptr + extended_len;
c906108c
SS
20824 extended_op = read_1_byte (abfd, line_ptr);
20825 line_ptr += 1;
20826 switch (extended_op)
20827 {
20828 case DW_LNE_end_sequence:
6f77053d
PA
20829 state_machine.handle_end_sequence ();
20830 end_sequence = true;
c906108c
SS
20831 break;
20832 case DW_LNE_set_address:
d9b3de22
DE
20833 {
20834 CORE_ADDR address
20835 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20836 line_ptr += bytes_read;
6f77053d
PA
20837
20838 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20839 lowpc - baseaddr, address);
6f77053d 20840 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20841 }
c906108c
SS
20842 break;
20843 case DW_LNE_define_file:
debd256d 20844 {
d521ce57 20845 const char *cur_file;
ecfb656c
PA
20846 unsigned int mod_time, length;
20847 dir_index dindex;
6e70227d 20848
3e43a32a
MS
20849 cur_file = read_direct_string (abfd, line_ptr,
20850 &bytes_read);
debd256d 20851 line_ptr += bytes_read;
ecfb656c 20852 dindex = (dir_index)
debd256d
JB
20853 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20854 line_ptr += bytes_read;
20855 mod_time =
20856 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20857 line_ptr += bytes_read;
20858 length =
20859 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20860 line_ptr += bytes_read;
ecfb656c 20861 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20862 }
c906108c 20863 break;
d0c6ba3d 20864 case DW_LNE_set_discriminator:
6f77053d
PA
20865 {
20866 /* The discriminator is not interesting to the
20867 debugger; just ignore it. We still need to
20868 check its value though:
20869 if there are consecutive entries for the same
20870 (non-prologue) line we want to coalesce them.
20871 PR 17276. */
20872 unsigned int discr
20873 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20874 line_ptr += bytes_read;
20875
20876 state_machine.handle_set_discriminator (discr);
20877 }
d0c6ba3d 20878 break;
c906108c 20879 default:
b98664d3 20880 complaint (_("mangled .debug_line section"));
debd256d 20881 return;
c906108c 20882 }
a8c50c1f
DJ
20883 /* Make sure that we parsed the extended op correctly. If e.g.
20884 we expected a different address size than the producer used,
20885 we may have read the wrong number of bytes. */
20886 if (line_ptr != extended_end)
20887 {
b98664d3 20888 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20889 return;
20890 }
c906108c
SS
20891 break;
20892 case DW_LNS_copy:
6f77053d 20893 state_machine.handle_copy ();
c906108c
SS
20894 break;
20895 case DW_LNS_advance_pc:
2dc7f7b3
TT
20896 {
20897 CORE_ADDR adjust
20898 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20899 line_ptr += bytes_read;
6f77053d
PA
20900
20901 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20902 }
c906108c
SS
20903 break;
20904 case DW_LNS_advance_line:
a05a36a5
DE
20905 {
20906 int line_delta
20907 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20908 line_ptr += bytes_read;
6f77053d
PA
20909
20910 state_machine.handle_advance_line (line_delta);
a05a36a5 20911 }
c906108c
SS
20912 break;
20913 case DW_LNS_set_file:
d9b3de22 20914 {
6f77053d 20915 file_name_index file
ecfb656c
PA
20916 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20917 &bytes_read);
d9b3de22 20918 line_ptr += bytes_read;
8c43009f 20919
6f77053d 20920 state_machine.handle_set_file (file);
d9b3de22 20921 }
c906108c
SS
20922 break;
20923 case DW_LNS_set_column:
0ad93d4f 20924 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20925 line_ptr += bytes_read;
20926 break;
20927 case DW_LNS_negate_stmt:
6f77053d 20928 state_machine.handle_negate_stmt ();
c906108c
SS
20929 break;
20930 case DW_LNS_set_basic_block:
c906108c 20931 break;
c2c6d25f
JM
20932 /* Add to the address register of the state machine the
20933 address increment value corresponding to special opcode
a738430d
MK
20934 255. I.e., this value is scaled by the minimum
20935 instruction length since special opcode 255 would have
b021a221 20936 scaled the increment. */
c906108c 20937 case DW_LNS_const_add_pc:
6f77053d 20938 state_machine.handle_const_add_pc ();
c906108c
SS
20939 break;
20940 case DW_LNS_fixed_advance_pc:
3e29f34a 20941 {
6f77053d 20942 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20943 line_ptr += 2;
6f77053d
PA
20944
20945 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20946 }
c906108c 20947 break;
9aa1fe7e 20948 default:
a738430d
MK
20949 {
20950 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20951 int i;
a738430d 20952
debd256d 20953 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20954 {
20955 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20956 line_ptr += bytes_read;
20957 }
20958 }
c906108c
SS
20959 }
20960 }
d9b3de22
DE
20961
20962 if (!end_sequence)
20963 dwarf2_debug_line_missing_end_sequence_complaint ();
20964
20965 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20966 in which case we still finish recording the last line). */
6f77053d 20967 state_machine.record_line (true);
c906108c 20968 }
f3f5162e
DE
20969}
20970
20971/* Decode the Line Number Program (LNP) for the given line_header
20972 structure and CU. The actual information extracted and the type
20973 of structures created from the LNP depends on the value of PST.
20974
20975 1. If PST is NULL, then this procedure uses the data from the program
20976 to create all necessary symbol tables, and their linetables.
20977
20978 2. If PST is not NULL, this procedure reads the program to determine
20979 the list of files included by the unit represented by PST, and
20980 builds all the associated partial symbol tables.
20981
20982 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20983 It is used for relative paths in the line table.
20984 NOTE: When processing partial symtabs (pst != NULL),
20985 comp_dir == pst->dirname.
20986
20987 NOTE: It is important that psymtabs have the same file name (via strcmp)
20988 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20989 symtab we don't use it in the name of the psymtabs we create.
20990 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20991 A good testcase for this is mb-inline.exp.
20992
527f3840
JK
20993 LOWPC is the lowest address in CU (or 0 if not known).
20994
20995 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20996 for its PC<->lines mapping information. Otherwise only the filename
20997 table is read in. */
f3f5162e
DE
20998
20999static void
21000dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21001 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21002 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21003{
518817b3 21004 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21005 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21006
527f3840
JK
21007 if (decode_mapping)
21008 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21009
21010 if (decode_for_pst_p)
21011 {
aaa75496
JB
21012 /* Now that we're done scanning the Line Header Program, we can
21013 create the psymtab of each included file. */
7ba99d21
AT
21014 for (auto &file_entry : lh->file_names ())
21015 if (file_entry.included_p == 1)
aaa75496 21016 {
c89b44cd 21017 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21018 const char *include_name =
7ba99d21
AT
21019 psymtab_include_file_name (lh, file_entry, pst,
21020 comp_dir, &name_holder);
c6da4cef 21021 if (include_name != NULL)
aaa75496
JB
21022 dwarf2_create_include_psymtab (include_name, pst, objfile);
21023 }
21024 }
cb1df416
DJ
21025 else
21026 {
21027 /* Make sure a symtab is created for every file, even files
21028 which contain only variables (i.e. no code with associated
21029 line numbers). */
c24bdb02
KS
21030 buildsym_compunit *builder = cu->get_builder ();
21031 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21032
7ba99d21 21033 for (auto &fe : lh->file_names ())
cb1df416 21034 {
804d2729 21035 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21036 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21037 {
c24bdb02 21038 builder->get_current_subfile ()->symtab
804d2729 21039 = allocate_symtab (cust,
c24bdb02 21040 builder->get_current_subfile ()->name);
43f3e411 21041 }
c24bdb02 21042 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21043 }
21044 }
c906108c
SS
21045}
21046
21047/* Start a subfile for DWARF. FILENAME is the name of the file and
21048 DIRNAME the name of the source directory which contains FILENAME
4d663531 21049 or NULL if not known.
c906108c
SS
21050 This routine tries to keep line numbers from identical absolute and
21051 relative file names in a common subfile.
21052
21053 Using the `list' example from the GDB testsuite, which resides in
21054 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21055 of /srcdir/list0.c yields the following debugging information for list0.c:
21056
c5aa993b 21057 DW_AT_name: /srcdir/list0.c
4d663531 21058 DW_AT_comp_dir: /compdir
357e46e7 21059 files.files[0].name: list0.h
c5aa993b 21060 files.files[0].dir: /srcdir
357e46e7 21061 files.files[1].name: list0.c
c5aa993b 21062 files.files[1].dir: /srcdir
c906108c
SS
21063
21064 The line number information for list0.c has to end up in a single
4f1520fb
FR
21065 subfile, so that `break /srcdir/list0.c:1' works as expected.
21066 start_subfile will ensure that this happens provided that we pass the
21067 concatenation of files.files[1].dir and files.files[1].name as the
21068 subfile's name. */
c906108c
SS
21069
21070static void
804d2729
TT
21071dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21072 const char *dirname)
c906108c 21073{
43816ebc 21074 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21075
4d663531 21076 /* In order not to lose the line information directory,
4f1520fb
FR
21077 we concatenate it to the filename when it makes sense.
21078 Note that the Dwarf3 standard says (speaking of filenames in line
21079 information): ``The directory index is ignored for file names
21080 that represent full path names''. Thus ignoring dirname in the
21081 `else' branch below isn't an issue. */
c906108c 21082
d5166ae1 21083 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21084 {
43816ebc
TT
21085 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21086 filename = copy.get ();
d521ce57 21087 }
c906108c 21088
c24bdb02 21089 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21090}
21091
804d2729
TT
21092/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21093 buildsym_compunit constructor. */
f4dc4d17 21094
c24bdb02
KS
21095struct compunit_symtab *
21096dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21097 CORE_ADDR low_pc)
f4dc4d17 21098{
c24bdb02 21099 gdb_assert (m_builder == nullptr);
43f3e411 21100
c24bdb02
KS
21101 m_builder.reset (new struct buildsym_compunit
21102 (per_cu->dwarf2_per_objfile->objfile,
21103 name, comp_dir, language, low_pc));
93b8bea4 21104
c24bdb02 21105 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21106
c24bdb02
KS
21107 get_builder ()->record_debugformat ("DWARF 2");
21108 get_builder ()->record_producer (producer);
f4dc4d17 21109
c24bdb02 21110 processing_has_namespace_info = false;
43f3e411 21111
c24bdb02 21112 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21113}
21114
4c2df51b
DJ
21115static void
21116var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21117 struct dwarf2_cu *cu)
4c2df51b 21118{
518817b3 21119 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21120 struct comp_unit_head *cu_header = &cu->header;
21121
4c2df51b
DJ
21122 /* NOTE drow/2003-01-30: There used to be a comment and some special
21123 code here to turn a symbol with DW_AT_external and a
21124 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21125 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21126 with some versions of binutils) where shared libraries could have
21127 relocations against symbols in their debug information - the
21128 minimal symbol would have the right address, but the debug info
21129 would not. It's no longer necessary, because we will explicitly
21130 apply relocations when we read in the debug information now. */
21131
21132 /* A DW_AT_location attribute with no contents indicates that a
21133 variable has been optimized away. */
4fc6c0d5 21134 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 21135 {
f1e6e072 21136 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21137 return;
21138 }
21139
21140 /* Handle one degenerate form of location expression specially, to
21141 preserve GDB's previous behavior when section offsets are
336d760d
AT
21142 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21143 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21144
4fc6c0d5 21145 if (attr->form_is_block ()
3019eac3
DE
21146 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21147 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21148 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21149 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21150 && (DW_BLOCK (attr)->size
21151 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21152 {
891d2f0b 21153 unsigned int dummy;
4c2df51b 21154
3019eac3 21155 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21156 SET_SYMBOL_VALUE_ADDRESS (sym,
21157 read_address (objfile->obfd,
21158 DW_BLOCK (attr)->data + 1,
21159 cu, &dummy));
3019eac3 21160 else
38583298
TT
21161 SET_SYMBOL_VALUE_ADDRESS
21162 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21163 &dummy));
f1e6e072 21164 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21165 fixup_symbol_section (sym, objfile);
6a053cb1
TT
21166 SET_SYMBOL_VALUE_ADDRESS
21167 (sym,
21168 SYMBOL_VALUE_ADDRESS (sym)
21169 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
21170 return;
21171 }
21172
21173 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21174 expression evaluator, and use LOC_COMPUTED only when necessary
21175 (i.e. when the value of a register or memory location is
21176 referenced, or a thread-local block, etc.). Then again, it might
21177 not be worthwhile. I'm assuming that it isn't unless performance
21178 or memory numbers show me otherwise. */
21179
f1e6e072 21180 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21181
f1e6e072 21182 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21183 cu->has_loclist = true;
4c2df51b
DJ
21184}
21185
c906108c
SS
21186/* Given a pointer to a DWARF information entry, figure out if we need
21187 to make a symbol table entry for it, and if so, create a new entry
21188 and return a pointer to it.
21189 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21190 used the passed type.
21191 If SPACE is not NULL, use it to hold the new symbol. If it is
21192 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21193
21194static struct symbol *
5e2db402
TT
21195new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21196 struct symbol *space)
c906108c 21197{
518817b3
SM
21198 struct dwarf2_per_objfile *dwarf2_per_objfile
21199 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21200 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21201 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21202 struct symbol *sym = NULL;
15d034d0 21203 const char *name;
c906108c
SS
21204 struct attribute *attr = NULL;
21205 struct attribute *attr2 = NULL;
e142c38c 21206 CORE_ADDR baseaddr;
e37fd15a
SW
21207 struct pending **list_to_add = NULL;
21208
edb3359d 21209 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21210
b3b3bada 21211 baseaddr = objfile->text_section_offset ();
c906108c 21212
94af9270 21213 name = dwarf2_name (die, cu);
c906108c
SS
21214 if (name)
21215 {
94af9270 21216 const char *linkagename;
34eaf542 21217 int suppress_add = 0;
94af9270 21218
34eaf542
TT
21219 if (space)
21220 sym = space;
21221 else
e623cf5d 21222 sym = allocate_symbol (objfile);
c906108c 21223 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21224
21225 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21226 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21227 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21228 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21229
f55ee35c
JK
21230 /* Fortran does not have mangling standard and the mangling does differ
21231 between gfortran, iFort etc. */
21232 if (cu->language == language_fortran
468c0cbb
CB
21233 && symbol_get_demangled_name (sym) == NULL)
21234 symbol_set_demangled_name (sym,
cfc594ee 21235 dwarf2_full_name (name, die, cu),
29df156d 21236 NULL);
f55ee35c 21237
c906108c 21238 /* Default assumptions.
c5aa993b 21239 Use the passed type or decode it from the die. */
176620f1 21240 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21241 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21242 if (type != NULL)
21243 SYMBOL_TYPE (sym) = type;
21244 else
e7c27a73 21245 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21246 attr = dwarf2_attr (die,
21247 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21248 cu);
435d3d88 21249 if (attr != nullptr)
c906108c
SS
21250 {
21251 SYMBOL_LINE (sym) = DW_UNSND (attr);
21252 }
cb1df416 21253
edb3359d
DJ
21254 attr = dwarf2_attr (die,
21255 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21256 cu);
435d3d88 21257 if (attr != nullptr)
cb1df416 21258 {
ecfb656c 21259 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21260 struct file_entry *fe;
9a619af0 21261
ecfb656c
PA
21262 if (cu->line_header != NULL)
21263 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21264 else
21265 fe = NULL;
21266
21267 if (fe == NULL)
b98664d3 21268 complaint (_("file index out of range"));
8c43009f
PA
21269 else
21270 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21271 }
21272
c906108c
SS
21273 switch (die->tag)
21274 {
21275 case DW_TAG_label:
e142c38c 21276 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21277 if (attr != nullptr)
3e29f34a
MR
21278 {
21279 CORE_ADDR addr;
21280
cd6c91b4 21281 addr = attr->value_as_address ();
3e29f34a 21282 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21283 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21284 }
0f5238ed
TT
21285 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21286 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21287 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21288 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21289 break;
21290 case DW_TAG_subprogram:
21291 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21292 finish_block. */
f1e6e072 21293 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21294 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21295 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21296 || cu->language == language_ada
21297 || cu->language == language_fortran)
c906108c 21298 {
2cfa0c8d 21299 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21300 Ada and Fortran subprograms, whether marked external or
21301 not, are always stored as a global symbol, because we want
21302 to be able to access them globally. For instance, we want
21303 to be able to break on a nested subprogram without having
21304 to specify the context. */
c24bdb02 21305 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21306 }
21307 else
21308 {
e37fd15a 21309 list_to_add = cu->list_in_scope;
c906108c
SS
21310 }
21311 break;
edb3359d
DJ
21312 case DW_TAG_inlined_subroutine:
21313 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21314 finish_block. */
f1e6e072 21315 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21316 SYMBOL_INLINED (sym) = 1;
481860b3 21317 list_to_add = cu->list_in_scope;
edb3359d 21318 break;
34eaf542
TT
21319 case DW_TAG_template_value_param:
21320 suppress_add = 1;
21321 /* Fall through. */
72929c62 21322 case DW_TAG_constant:
c906108c 21323 case DW_TAG_variable:
254e6b9e 21324 case DW_TAG_member:
0963b4bd
MS
21325 /* Compilation with minimal debug info may result in
21326 variables with missing type entries. Change the
21327 misleading `void' type to something sensible. */
c906108c 21328 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21329 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21330
e142c38c 21331 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21332 /* In the case of DW_TAG_member, we should only be called for
21333 static const members. */
21334 if (die->tag == DW_TAG_member)
21335 {
3863f96c
DE
21336 /* dwarf2_add_field uses die_is_declaration,
21337 so we do the same. */
254e6b9e
DE
21338 gdb_assert (die_is_declaration (die, cu));
21339 gdb_assert (attr);
21340 }
435d3d88 21341 if (attr != nullptr)
c906108c 21342 {
e7c27a73 21343 dwarf2_const_value (attr, sym, cu);
e142c38c 21344 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21345 if (!suppress_add)
34eaf542
TT
21346 {
21347 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21348 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21349 else
e37fd15a 21350 list_to_add = cu->list_in_scope;
34eaf542 21351 }
c906108c
SS
21352 break;
21353 }
e142c38c 21354 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21355 if (attr != nullptr)
c906108c 21356 {
e7c27a73 21357 var_decode_location (attr, sym, cu);
e142c38c 21358 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21359
21360 /* Fortran explicitly imports any global symbols to the local
21361 scope by DW_TAG_common_block. */
21362 if (cu->language == language_fortran && die->parent
21363 && die->parent->tag == DW_TAG_common_block)
21364 attr2 = NULL;
21365
caac4577
JG
21366 if (SYMBOL_CLASS (sym) == LOC_STATIC
21367 && SYMBOL_VALUE_ADDRESS (sym) == 0
21368 && !dwarf2_per_objfile->has_section_at_zero)
21369 {
21370 /* When a static variable is eliminated by the linker,
21371 the corresponding debug information is not stripped
21372 out, but the variable address is set to null;
21373 do not add such variables into symbol table. */
21374 }
21375 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21376 {
4b610737
TT
21377 if (SYMBOL_CLASS (sym) == LOC_STATIC
21378 && (objfile->flags & OBJF_MAINLINE) == 0
21379 && dwarf2_per_objfile->can_copy)
21380 {
21381 /* A global static variable might be subject to
21382 copy relocation. We first check for a local
21383 minsym, though, because maybe the symbol was
21384 marked hidden, in which case this would not
21385 apply. */
21386 bound_minimal_symbol found
21387 = (lookup_minimal_symbol_linkage
987012b8 21388 (sym->linkage_name (), objfile));
4b610737
TT
21389 if (found.minsym != nullptr)
21390 sym->maybe_copied = 1;
21391 }
f55ee35c 21392
1c809c68
TT
21393 /* A variable with DW_AT_external is never static,
21394 but it may be block-scoped. */
804d2729 21395 list_to_add
c24bdb02
KS
21396 = ((cu->list_in_scope
21397 == cu->get_builder ()->get_file_symbols ())
21398 ? cu->get_builder ()->get_global_symbols ()
804d2729 21399 : cu->list_in_scope);
1c809c68 21400 }
c906108c 21401 else
e37fd15a 21402 list_to_add = cu->list_in_scope;
c906108c
SS
21403 }
21404 else
21405 {
21406 /* We do not know the address of this symbol.
c5aa993b
JM
21407 If it is an external symbol and we have type information
21408 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21409 The address of the variable will then be determined from
21410 the minimal symbol table whenever the variable is
21411 referenced. */
e142c38c 21412 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21413
21414 /* Fortran explicitly imports any global symbols to the local
21415 scope by DW_TAG_common_block. */
21416 if (cu->language == language_fortran && die->parent
21417 && die->parent->tag == DW_TAG_common_block)
21418 {
21419 /* SYMBOL_CLASS doesn't matter here because
21420 read_common_block is going to reset it. */
21421 if (!suppress_add)
21422 list_to_add = cu->list_in_scope;
21423 }
21424 else if (attr2 && (DW_UNSND (attr2) != 0)
21425 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21426 {
0fe7935b
DJ
21427 /* A variable with DW_AT_external is never static, but it
21428 may be block-scoped. */
804d2729 21429 list_to_add
c24bdb02
KS
21430 = ((cu->list_in_scope
21431 == cu->get_builder ()->get_file_symbols ())
21432 ? cu->get_builder ()->get_global_symbols ()
804d2729 21433 : cu->list_in_scope);
0fe7935b 21434
f1e6e072 21435 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21436 }
442ddf59
JK
21437 else if (!die_is_declaration (die, cu))
21438 {
21439 /* Use the default LOC_OPTIMIZED_OUT class. */
21440 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21441 if (!suppress_add)
21442 list_to_add = cu->list_in_scope;
442ddf59 21443 }
c906108c
SS
21444 }
21445 break;
21446 case DW_TAG_formal_parameter:
a60f3166
TT
21447 {
21448 /* If we are inside a function, mark this as an argument. If
21449 not, we might be looking at an argument to an inlined function
21450 when we do not have enough information to show inlined frames;
21451 pretend it's a local variable in that case so that the user can
21452 still see it. */
804d2729 21453 struct context_stack *curr
c24bdb02 21454 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21455 if (curr != nullptr && curr->name != nullptr)
21456 SYMBOL_IS_ARGUMENT (sym) = 1;
21457 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21458 if (attr != nullptr)
a60f3166
TT
21459 {
21460 var_decode_location (attr, sym, cu);
21461 }
21462 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21463 if (attr != nullptr)
a60f3166
TT
21464 {
21465 dwarf2_const_value (attr, sym, cu);
21466 }
f346a30d 21467
a60f3166
TT
21468 list_to_add = cu->list_in_scope;
21469 }
c906108c
SS
21470 break;
21471 case DW_TAG_unspecified_parameters:
21472 /* From varargs functions; gdb doesn't seem to have any
21473 interest in this information, so just ignore it for now.
21474 (FIXME?) */
21475 break;
34eaf542
TT
21476 case DW_TAG_template_type_param:
21477 suppress_add = 1;
21478 /* Fall through. */
c906108c 21479 case DW_TAG_class_type:
680b30c7 21480 case DW_TAG_interface_type:
c906108c
SS
21481 case DW_TAG_structure_type:
21482 case DW_TAG_union_type:
72019c9c 21483 case DW_TAG_set_type:
c906108c 21484 case DW_TAG_enumeration_type:
f1e6e072 21485 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21486 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21487
63d06c5c 21488 {
9c37b5ae 21489 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21490 really ever be static objects: otherwise, if you try
21491 to, say, break of a class's method and you're in a file
21492 which doesn't mention that class, it won't work unless
21493 the check for all static symbols in lookup_symbol_aux
21494 saves you. See the OtherFileClass tests in
21495 gdb.c++/namespace.exp. */
21496
e37fd15a 21497 if (!suppress_add)
34eaf542 21498 {
c24bdb02 21499 buildsym_compunit *builder = cu->get_builder ();
804d2729 21500 list_to_add
c24bdb02 21501 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21502 && cu->language == language_cplus
c24bdb02 21503 ? builder->get_global_symbols ()
804d2729 21504 : cu->list_in_scope);
63d06c5c 21505
64382290 21506 /* The semantics of C++ state that "struct foo {
9c37b5ae 21507 ... }" also defines a typedef for "foo". */
64382290 21508 if (cu->language == language_cplus
45280282 21509 || cu->language == language_ada
c44af4eb
TT
21510 || cu->language == language_d
21511 || cu->language == language_rust)
64382290
TT
21512 {
21513 /* The symbol's name is already allocated along
21514 with this objfile, so we don't need to
21515 duplicate it for the type. */
21516 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 21517 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 21518 }
63d06c5c
DC
21519 }
21520 }
c906108c
SS
21521 break;
21522 case DW_TAG_typedef:
f1e6e072 21523 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21524 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21525 list_to_add = cu->list_in_scope;
63d06c5c 21526 break;
c906108c 21527 case DW_TAG_base_type:
a02abb62 21528 case DW_TAG_subrange_type:
f1e6e072 21529 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21530 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21531 list_to_add = cu->list_in_scope;
c906108c
SS
21532 break;
21533 case DW_TAG_enumerator:
e142c38c 21534 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21535 if (attr != nullptr)
c906108c 21536 {
e7c27a73 21537 dwarf2_const_value (attr, sym, cu);
c906108c 21538 }
63d06c5c
DC
21539 {
21540 /* NOTE: carlton/2003-11-10: See comment above in the
21541 DW_TAG_class_type, etc. block. */
21542
804d2729 21543 list_to_add
c24bdb02 21544 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21545 && cu->language == language_cplus
c24bdb02 21546 ? cu->get_builder ()->get_global_symbols ()
804d2729 21547 : cu->list_in_scope);
63d06c5c 21548 }
c906108c 21549 break;
74921315 21550 case DW_TAG_imported_declaration:
5c4e30ca 21551 case DW_TAG_namespace:
f1e6e072 21552 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21553 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21554 break;
530e8392
KB
21555 case DW_TAG_module:
21556 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21557 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21558 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21559 break;
4357ac6c 21560 case DW_TAG_common_block:
f1e6e072 21561 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21562 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21563 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21564 break;
c906108c
SS
21565 default:
21566 /* Not a tag we recognize. Hopefully we aren't processing
21567 trash data, but since we must specifically ignore things
21568 we don't recognize, there is nothing else we should do at
0963b4bd 21569 this point. */
b98664d3 21570 complaint (_("unsupported tag: '%s'"),
4d3c2250 21571 dwarf_tag_name (die->tag));
c906108c
SS
21572 break;
21573 }
df8a16a1 21574
e37fd15a
SW
21575 if (suppress_add)
21576 {
21577 sym->hash_next = objfile->template_symbols;
21578 objfile->template_symbols = sym;
21579 list_to_add = NULL;
21580 }
21581
21582 if (list_to_add != NULL)
d3cb6808 21583 add_symbol_to_list (sym, list_to_add);
e37fd15a 21584
df8a16a1
DJ
21585 /* For the benefit of old versions of GCC, check for anonymous
21586 namespaces based on the demangled name. */
4d4ec4e5 21587 if (!cu->processing_has_namespace_info
94af9270 21588 && cu->language == language_cplus)
c24bdb02 21589 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21590 }
21591 return (sym);
21592}
21593
98bfdba5
PA
21594/* Given an attr with a DW_FORM_dataN value in host byte order,
21595 zero-extend it as appropriate for the symbol's type. The DWARF
21596 standard (v4) is not entirely clear about the meaning of using
21597 DW_FORM_dataN for a constant with a signed type, where the type is
21598 wider than the data. The conclusion of a discussion on the DWARF
21599 list was that this is unspecified. We choose to always zero-extend
21600 because that is the interpretation long in use by GCC. */
c906108c 21601
98bfdba5 21602static gdb_byte *
ff39bb5e 21603dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21604 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21605{
518817b3 21606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21607 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21608 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21609 LONGEST l = DW_UNSND (attr);
21610
21611 if (bits < sizeof (*value) * 8)
21612 {
21613 l &= ((LONGEST) 1 << bits) - 1;
21614 *value = l;
21615 }
21616 else if (bits == sizeof (*value) * 8)
21617 *value = l;
21618 else
21619 {
224c3ddb 21620 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21621 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21622 return bytes;
21623 }
21624
21625 return NULL;
21626}
21627
21628/* Read a constant value from an attribute. Either set *VALUE, or if
21629 the value does not fit in *VALUE, set *BYTES - either already
21630 allocated on the objfile obstack, or newly allocated on OBSTACK,
21631 or, set *BATON, if we translated the constant to a location
21632 expression. */
21633
21634static void
ff39bb5e 21635dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21636 const char *name, struct obstack *obstack,
21637 struct dwarf2_cu *cu,
d521ce57 21638 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21639 struct dwarf2_locexpr_baton **baton)
21640{
518817b3 21641 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21642 struct comp_unit_head *cu_header = &cu->header;
c906108c 21643 struct dwarf_block *blk;
98bfdba5
PA
21644 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21645 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21646
21647 *value = 0;
21648 *bytes = NULL;
21649 *baton = NULL;
c906108c
SS
21650
21651 switch (attr->form)
21652 {
21653 case DW_FORM_addr:
336d760d 21654 case DW_FORM_addrx:
3019eac3 21655 case DW_FORM_GNU_addr_index:
ac56253d 21656 {
ac56253d
TT
21657 gdb_byte *data;
21658
98bfdba5
PA
21659 if (TYPE_LENGTH (type) != cu_header->addr_size)
21660 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21661 cu_header->addr_size,
98bfdba5 21662 TYPE_LENGTH (type));
ac56253d
TT
21663 /* Symbols of this form are reasonably rare, so we just
21664 piggyback on the existing location code rather than writing
21665 a new implementation of symbol_computed_ops. */
8d749320 21666 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21667 (*baton)->per_cu = cu->per_cu;
21668 gdb_assert ((*baton)->per_cu);
ac56253d 21669
98bfdba5 21670 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21671 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21672 (*baton)->data = data;
ac56253d
TT
21673
21674 data[0] = DW_OP_addr;
21675 store_unsigned_integer (&data[1], cu_header->addr_size,
21676 byte_order, DW_ADDR (attr));
21677 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21678 }
c906108c 21679 break;
4ac36638 21680 case DW_FORM_string:
93b5768b 21681 case DW_FORM_strp:
cf532bd1 21682 case DW_FORM_strx:
3019eac3 21683 case DW_FORM_GNU_str_index:
36586728 21684 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21685 /* DW_STRING is already allocated on the objfile obstack, point
21686 directly to it. */
d521ce57 21687 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21688 break;
c906108c
SS
21689 case DW_FORM_block1:
21690 case DW_FORM_block2:
21691 case DW_FORM_block4:
21692 case DW_FORM_block:
2dc7f7b3 21693 case DW_FORM_exprloc:
0224619f 21694 case DW_FORM_data16:
c906108c 21695 blk = DW_BLOCK (attr);
98bfdba5
PA
21696 if (TYPE_LENGTH (type) != blk->size)
21697 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21698 TYPE_LENGTH (type));
21699 *bytes = blk->data;
c906108c 21700 break;
2df3850c
JM
21701
21702 /* The DW_AT_const_value attributes are supposed to carry the
21703 symbol's value "represented as it would be on the target
21704 architecture." By the time we get here, it's already been
21705 converted to host endianness, so we just need to sign- or
21706 zero-extend it as appropriate. */
21707 case DW_FORM_data1:
3aef2284 21708 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21709 break;
c906108c 21710 case DW_FORM_data2:
3aef2284 21711 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21712 break;
c906108c 21713 case DW_FORM_data4:
3aef2284 21714 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21715 break;
c906108c 21716 case DW_FORM_data8:
3aef2284 21717 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21718 break;
21719
c906108c 21720 case DW_FORM_sdata:
663c44ac 21721 case DW_FORM_implicit_const:
98bfdba5 21722 *value = DW_SND (attr);
2df3850c
JM
21723 break;
21724
c906108c 21725 case DW_FORM_udata:
98bfdba5 21726 *value = DW_UNSND (attr);
c906108c 21727 break;
2df3850c 21728
c906108c 21729 default:
b98664d3 21730 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21731 dwarf_form_name (attr->form));
98bfdba5 21732 *value = 0;
c906108c
SS
21733 break;
21734 }
21735}
21736
2df3850c 21737
98bfdba5
PA
21738/* Copy constant value from an attribute to a symbol. */
21739
2df3850c 21740static void
ff39bb5e 21741dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21742 struct dwarf2_cu *cu)
2df3850c 21743{
518817b3 21744 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21745 LONGEST value;
d521ce57 21746 const gdb_byte *bytes;
98bfdba5 21747 struct dwarf2_locexpr_baton *baton;
2df3850c 21748
98bfdba5 21749 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21750 sym->print_name (),
98bfdba5
PA
21751 &objfile->objfile_obstack, cu,
21752 &value, &bytes, &baton);
2df3850c 21753
98bfdba5
PA
21754 if (baton != NULL)
21755 {
98bfdba5 21756 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21757 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21758 }
21759 else if (bytes != NULL)
21760 {
21761 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21762 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21763 }
21764 else
21765 {
21766 SYMBOL_VALUE (sym) = value;
f1e6e072 21767 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21768 }
2df3850c
JM
21769}
21770
c906108c
SS
21771/* Return the type of the die in question using its DW_AT_type attribute. */
21772
21773static struct type *
e7c27a73 21774die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21775{
c906108c 21776 struct attribute *type_attr;
c906108c 21777
e142c38c 21778 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21779 if (!type_attr)
21780 {
518817b3 21781 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21782 /* A missing DW_AT_type represents a void type. */
518817b3 21783 return objfile_type (objfile)->builtin_void;
c906108c 21784 }
348e048f 21785
673bfd45 21786 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21787}
21788
b4ba55a1
JB
21789/* True iff CU's producer generates GNAT Ada auxiliary information
21790 that allows to find parallel types through that information instead
21791 of having to do expensive parallel lookups by type name. */
21792
21793static int
21794need_gnat_info (struct dwarf2_cu *cu)
21795{
de4cb04a
JB
21796 /* Assume that the Ada compiler was GNAT, which always produces
21797 the auxiliary information. */
21798 return (cu->language == language_ada);
b4ba55a1
JB
21799}
21800
b4ba55a1
JB
21801/* Return the auxiliary type of the die in question using its
21802 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21803 attribute is not present. */
21804
21805static struct type *
21806die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21807{
b4ba55a1 21808 struct attribute *type_attr;
b4ba55a1
JB
21809
21810 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21811 if (!type_attr)
21812 return NULL;
21813
673bfd45 21814 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21815}
21816
21817/* If DIE has a descriptive_type attribute, then set the TYPE's
21818 descriptive type accordingly. */
21819
21820static void
21821set_descriptive_type (struct type *type, struct die_info *die,
21822 struct dwarf2_cu *cu)
21823{
21824 struct type *descriptive_type = die_descriptive_type (die, cu);
21825
21826 if (descriptive_type)
21827 {
21828 ALLOCATE_GNAT_AUX_TYPE (type);
21829 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21830 }
21831}
21832
c906108c
SS
21833/* Return the containing type of the die in question using its
21834 DW_AT_containing_type attribute. */
21835
21836static struct type *
e7c27a73 21837die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21838{
c906108c 21839 struct attribute *type_attr;
518817b3 21840 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21841
e142c38c 21842 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21843 if (!type_attr)
21844 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21845 "[in module %s]"), objfile_name (objfile));
33ac96f0 21846
673bfd45 21847 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21848}
21849
ac9ec31b
DE
21850/* Return an error marker type to use for the ill formed type in DIE/CU. */
21851
21852static struct type *
21853build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21854{
518817b3
SM
21855 struct dwarf2_per_objfile *dwarf2_per_objfile
21856 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21857 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21858 char *saved;
ac9ec31b 21859
528e1572
SM
21860 std::string message
21861 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21862 objfile_name (objfile),
21863 sect_offset_str (cu->header.sect_off),
21864 sect_offset_str (die->sect_off));
efba19b0 21865 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21866
19f392bc 21867 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21868}
21869
673bfd45 21870/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21871 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21872 DW_AT_containing_type.
673bfd45
DE
21873 If there is no type substitute an error marker. */
21874
c906108c 21875static struct type *
ff39bb5e 21876lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21877 struct dwarf2_cu *cu)
c906108c 21878{
518817b3
SM
21879 struct dwarf2_per_objfile *dwarf2_per_objfile
21880 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21881 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21882 struct type *this_type;
21883
ac9ec31b
DE
21884 gdb_assert (attr->name == DW_AT_type
21885 || attr->name == DW_AT_GNAT_descriptive_type
21886 || attr->name == DW_AT_containing_type);
21887
673bfd45
DE
21888 /* First see if we have it cached. */
21889
36586728
TT
21890 if (attr->form == DW_FORM_GNU_ref_alt)
21891 {
21892 struct dwarf2_per_cu_data *per_cu;
9c541725 21893 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21894
ed2dc618
SM
21895 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21896 dwarf2_per_objfile);
9c541725 21897 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21898 }
cd6c91b4 21899 else if (attr->form_is_ref ())
673bfd45 21900 {
9c541725 21901 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21902
9c541725 21903 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21904 }
55f1336d 21905 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21906 {
ac9ec31b 21907 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21908
ac9ec31b 21909 return get_signatured_type (die, signature, cu);
673bfd45
DE
21910 }
21911 else
21912 {
b98664d3 21913 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21914 " at %s [in module %s]"),
21915 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21916 objfile_name (objfile));
ac9ec31b 21917 return build_error_marker_type (cu, die);
673bfd45
DE
21918 }
21919
21920 /* If not cached we need to read it in. */
21921
21922 if (this_type == NULL)
21923 {
ac9ec31b 21924 struct die_info *type_die = NULL;
673bfd45
DE
21925 struct dwarf2_cu *type_cu = cu;
21926
cd6c91b4 21927 if (attr->form_is_ref ())
ac9ec31b
DE
21928 type_die = follow_die_ref (die, attr, &type_cu);
21929 if (type_die == NULL)
21930 return build_error_marker_type (cu, die);
21931 /* If we find the type now, it's probably because the type came
3019eac3
DE
21932 from an inter-CU reference and the type's CU got expanded before
21933 ours. */
ac9ec31b 21934 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21935 }
21936
21937 /* If we still don't have a type use an error marker. */
21938
21939 if (this_type == NULL)
ac9ec31b 21940 return build_error_marker_type (cu, die);
673bfd45 21941
f792889a 21942 return this_type;
c906108c
SS
21943}
21944
673bfd45
DE
21945/* Return the type in DIE, CU.
21946 Returns NULL for invalid types.
21947
02142a6c 21948 This first does a lookup in die_type_hash,
673bfd45
DE
21949 and only reads the die in if necessary.
21950
21951 NOTE: This can be called when reading in partial or full symbols. */
21952
f792889a 21953static struct type *
e7c27a73 21954read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21955{
f792889a
DJ
21956 struct type *this_type;
21957
21958 this_type = get_die_type (die, cu);
21959 if (this_type)
21960 return this_type;
21961
673bfd45
DE
21962 return read_type_die_1 (die, cu);
21963}
21964
21965/* Read the type in DIE, CU.
21966 Returns NULL for invalid types. */
21967
21968static struct type *
21969read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21970{
21971 struct type *this_type = NULL;
21972
c906108c
SS
21973 switch (die->tag)
21974 {
21975 case DW_TAG_class_type:
680b30c7 21976 case DW_TAG_interface_type:
c906108c
SS
21977 case DW_TAG_structure_type:
21978 case DW_TAG_union_type:
f792889a 21979 this_type = read_structure_type (die, cu);
c906108c
SS
21980 break;
21981 case DW_TAG_enumeration_type:
f792889a 21982 this_type = read_enumeration_type (die, cu);
c906108c
SS
21983 break;
21984 case DW_TAG_subprogram:
21985 case DW_TAG_subroutine_type:
edb3359d 21986 case DW_TAG_inlined_subroutine:
f792889a 21987 this_type = read_subroutine_type (die, cu);
c906108c
SS
21988 break;
21989 case DW_TAG_array_type:
f792889a 21990 this_type = read_array_type (die, cu);
c906108c 21991 break;
72019c9c 21992 case DW_TAG_set_type:
f792889a 21993 this_type = read_set_type (die, cu);
72019c9c 21994 break;
c906108c 21995 case DW_TAG_pointer_type:
f792889a 21996 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21997 break;
21998 case DW_TAG_ptr_to_member_type:
f792889a 21999 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22000 break;
22001 case DW_TAG_reference_type:
4297a3f0
AV
22002 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22003 break;
22004 case DW_TAG_rvalue_reference_type:
22005 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22006 break;
22007 case DW_TAG_const_type:
f792889a 22008 this_type = read_tag_const_type (die, cu);
c906108c
SS
22009 break;
22010 case DW_TAG_volatile_type:
f792889a 22011 this_type = read_tag_volatile_type (die, cu);
c906108c 22012 break;
06d66ee9
TT
22013 case DW_TAG_restrict_type:
22014 this_type = read_tag_restrict_type (die, cu);
22015 break;
c906108c 22016 case DW_TAG_string_type:
f792889a 22017 this_type = read_tag_string_type (die, cu);
c906108c
SS
22018 break;
22019 case DW_TAG_typedef:
f792889a 22020 this_type = read_typedef (die, cu);
c906108c 22021 break;
a02abb62 22022 case DW_TAG_subrange_type:
f792889a 22023 this_type = read_subrange_type (die, cu);
a02abb62 22024 break;
c906108c 22025 case DW_TAG_base_type:
f792889a 22026 this_type = read_base_type (die, cu);
c906108c 22027 break;
81a17f79 22028 case DW_TAG_unspecified_type:
f792889a 22029 this_type = read_unspecified_type (die, cu);
81a17f79 22030 break;
0114d602
DJ
22031 case DW_TAG_namespace:
22032 this_type = read_namespace_type (die, cu);
22033 break;
f55ee35c
JK
22034 case DW_TAG_module:
22035 this_type = read_module_type (die, cu);
22036 break;
a2c2acaf
MW
22037 case DW_TAG_atomic_type:
22038 this_type = read_tag_atomic_type (die, cu);
22039 break;
c906108c 22040 default:
b98664d3 22041 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22042 dwarf_tag_name (die->tag));
c906108c
SS
22043 break;
22044 }
63d06c5c 22045
f792889a 22046 return this_type;
63d06c5c
DC
22047}
22048
abc72ce4
DE
22049/* See if we can figure out if the class lives in a namespace. We do
22050 this by looking for a member function; its demangled name will
22051 contain namespace info, if there is any.
22052 Return the computed name or NULL.
22053 Space for the result is allocated on the objfile's obstack.
22054 This is the full-die version of guess_partial_die_structure_name.
22055 In this case we know DIE has no useful parent. */
22056
43816ebc 22057static const char *
abc72ce4
DE
22058guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22059{
22060 struct die_info *spec_die;
22061 struct dwarf2_cu *spec_cu;
22062 struct die_info *child;
518817b3 22063 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22064
22065 spec_cu = cu;
22066 spec_die = die_specification (die, &spec_cu);
22067 if (spec_die != NULL)
22068 {
22069 die = spec_die;
22070 cu = spec_cu;
22071 }
22072
22073 for (child = die->child;
22074 child != NULL;
22075 child = child->sibling)
22076 {
22077 if (child->tag == DW_TAG_subprogram)
22078 {
73b9be8b 22079 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22080
7d45c7c3 22081 if (linkage_name != NULL)
abc72ce4 22082 {
43816ebc
TT
22083 gdb::unique_xmalloc_ptr<char> actual_name
22084 (language_class_name_from_physname (cu->language_defn,
22085 linkage_name));
22086 const char *name = NULL;
abc72ce4
DE
22087
22088 if (actual_name != NULL)
22089 {
15d034d0 22090 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22091
22092 if (die_name != NULL
43816ebc 22093 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22094 {
22095 /* Strip off the class name from the full name.
22096 We want the prefix. */
22097 int die_name_len = strlen (die_name);
43816ebc
TT
22098 int actual_name_len = strlen (actual_name.get ());
22099 const char *ptr = actual_name.get ();
abc72ce4
DE
22100
22101 /* Test for '::' as a sanity check. */
22102 if (actual_name_len > die_name_len + 2
43816ebc 22103 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22104 name = obstack_strndup (
e3b94546 22105 &objfile->per_bfd->storage_obstack,
43816ebc 22106 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22107 }
22108 }
abc72ce4
DE
22109 return name;
22110 }
22111 }
22112 }
22113
22114 return NULL;
22115}
22116
96408a79
SA
22117/* GCC might emit a nameless typedef that has a linkage name. Determine the
22118 prefix part in such case. See
22119 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22120
a121b7c1 22121static const char *
96408a79
SA
22122anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22123{
22124 struct attribute *attr;
e6a959d6 22125 const char *base;
96408a79
SA
22126
22127 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22128 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22129 return NULL;
22130
7d45c7c3 22131 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22132 return NULL;
22133
73b9be8b 22134 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22135 if (attr == NULL || DW_STRING (attr) == NULL)
22136 return NULL;
22137
22138 /* dwarf2_name had to be already called. */
22139 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22140
22141 /* Strip the base name, keep any leading namespaces/classes. */
22142 base = strrchr (DW_STRING (attr), ':');
22143 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22144 return "";
22145
518817b3 22146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22147 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22148 DW_STRING (attr),
22149 &base[-1] - DW_STRING (attr));
96408a79
SA
22150}
22151
fdde2d81 22152/* Return the name of the namespace/class that DIE is defined within,
0114d602 22153 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22154
0114d602
DJ
22155 For example, if we're within the method foo() in the following
22156 code:
22157
22158 namespace N {
22159 class C {
22160 void foo () {
22161 }
22162 };
22163 }
22164
22165 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22166
0d5cff50 22167static const char *
e142c38c 22168determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22169{
518817b3
SM
22170 struct dwarf2_per_objfile *dwarf2_per_objfile
22171 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22172 struct die_info *parent, *spec_die;
22173 struct dwarf2_cu *spec_cu;
22174 struct type *parent_type;
a121b7c1 22175 const char *retval;
63d06c5c 22176
9c37b5ae 22177 if (cu->language != language_cplus
c44af4eb
TT
22178 && cu->language != language_fortran && cu->language != language_d
22179 && cu->language != language_rust)
0114d602
DJ
22180 return "";
22181
96408a79
SA
22182 retval = anonymous_struct_prefix (die, cu);
22183 if (retval)
22184 return retval;
22185
0114d602
DJ
22186 /* We have to be careful in the presence of DW_AT_specification.
22187 For example, with GCC 3.4, given the code
22188
22189 namespace N {
22190 void foo() {
22191 // Definition of N::foo.
22192 }
22193 }
22194
22195 then we'll have a tree of DIEs like this:
22196
22197 1: DW_TAG_compile_unit
22198 2: DW_TAG_namespace // N
22199 3: DW_TAG_subprogram // declaration of N::foo
22200 4: DW_TAG_subprogram // definition of N::foo
22201 DW_AT_specification // refers to die #3
22202
22203 Thus, when processing die #4, we have to pretend that we're in
22204 the context of its DW_AT_specification, namely the contex of die
22205 #3. */
22206 spec_cu = cu;
22207 spec_die = die_specification (die, &spec_cu);
22208 if (spec_die == NULL)
22209 parent = die->parent;
22210 else
63d06c5c 22211 {
0114d602
DJ
22212 parent = spec_die->parent;
22213 cu = spec_cu;
63d06c5c 22214 }
0114d602
DJ
22215
22216 if (parent == NULL)
22217 return "";
98bfdba5
PA
22218 else if (parent->building_fullname)
22219 {
22220 const char *name;
22221 const char *parent_name;
22222
22223 /* It has been seen on RealView 2.2 built binaries,
22224 DW_TAG_template_type_param types actually _defined_ as
22225 children of the parent class:
22226
22227 enum E {};
22228 template class <class Enum> Class{};
22229 Class<enum E> class_e;
22230
22231 1: DW_TAG_class_type (Class)
22232 2: DW_TAG_enumeration_type (E)
22233 3: DW_TAG_enumerator (enum1:0)
22234 3: DW_TAG_enumerator (enum2:1)
22235 ...
22236 2: DW_TAG_template_type_param
22237 DW_AT_type DW_FORM_ref_udata (E)
22238
22239 Besides being broken debug info, it can put GDB into an
22240 infinite loop. Consider:
22241
22242 When we're building the full name for Class<E>, we'll start
22243 at Class, and go look over its template type parameters,
22244 finding E. We'll then try to build the full name of E, and
22245 reach here. We're now trying to build the full name of E,
22246 and look over the parent DIE for containing scope. In the
22247 broken case, if we followed the parent DIE of E, we'd again
22248 find Class, and once again go look at its template type
22249 arguments, etc., etc. Simply don't consider such parent die
22250 as source-level parent of this die (it can't be, the language
22251 doesn't allow it), and break the loop here. */
22252 name = dwarf2_name (die, cu);
22253 parent_name = dwarf2_name (parent, cu);
b98664d3 22254 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22255 name ? name : "<unknown>",
22256 parent_name ? parent_name : "<unknown>");
22257 return "";
22258 }
63d06c5c 22259 else
0114d602
DJ
22260 switch (parent->tag)
22261 {
63d06c5c 22262 case DW_TAG_namespace:
0114d602 22263 parent_type = read_type_die (parent, cu);
acebe513
UW
22264 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22265 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22266 Work around this problem here. */
22267 if (cu->language == language_cplus
e86ca25f 22268 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22269 return "";
0114d602 22270 /* We give a name to even anonymous namespaces. */
e86ca25f 22271 return TYPE_NAME (parent_type);
63d06c5c 22272 case DW_TAG_class_type:
680b30c7 22273 case DW_TAG_interface_type:
63d06c5c 22274 case DW_TAG_structure_type:
0114d602 22275 case DW_TAG_union_type:
f55ee35c 22276 case DW_TAG_module:
0114d602 22277 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22278 if (TYPE_NAME (parent_type) != NULL)
22279 return TYPE_NAME (parent_type);
0114d602
DJ
22280 else
22281 /* An anonymous structure is only allowed non-static data
22282 members; no typedefs, no member functions, et cetera.
22283 So it does not need a prefix. */
22284 return "";
abc72ce4 22285 case DW_TAG_compile_unit:
95554aad 22286 case DW_TAG_partial_unit:
abc72ce4
DE
22287 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22288 if (cu->language == language_cplus
fd5866f6 22289 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22290 && die->child != NULL
22291 && (die->tag == DW_TAG_class_type
22292 || die->tag == DW_TAG_structure_type
22293 || die->tag == DW_TAG_union_type))
22294 {
43816ebc 22295 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22296 if (name != NULL)
22297 return name;
22298 }
22299 return "";
0a4b0913
AB
22300 case DW_TAG_subprogram:
22301 /* Nested subroutines in Fortran get a prefix with the name
22302 of the parent's subroutine. */
22303 if (cu->language == language_fortran)
22304 {
22305 if ((die->tag == DW_TAG_subprogram)
22306 && (dwarf2_name (parent, cu) != NULL))
22307 return dwarf2_name (parent, cu);
22308 }
22309 return determine_prefix (parent, cu);
3d567982
TT
22310 case DW_TAG_enumeration_type:
22311 parent_type = read_type_die (parent, cu);
22312 if (TYPE_DECLARED_CLASS (parent_type))
22313 {
e86ca25f
TT
22314 if (TYPE_NAME (parent_type) != NULL)
22315 return TYPE_NAME (parent_type);
3d567982
TT
22316 return "";
22317 }
22318 /* Fall through. */
63d06c5c 22319 default:
8176b9b8 22320 return determine_prefix (parent, cu);
63d06c5c 22321 }
63d06c5c
DC
22322}
22323
3e43a32a
MS
22324/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22325 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22326 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22327 an obconcat, otherwise allocate storage for the result. The CU argument is
22328 used to determine the language and hence, the appropriate separator. */
987504bb 22329
f55ee35c 22330#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22331
22332static char *
f55ee35c
JK
22333typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22334 int physname, struct dwarf2_cu *cu)
63d06c5c 22335{
f55ee35c 22336 const char *lead = "";
5c315b68 22337 const char *sep;
63d06c5c 22338
3e43a32a
MS
22339 if (suffix == NULL || suffix[0] == '\0'
22340 || prefix == NULL || prefix[0] == '\0')
987504bb 22341 sep = "";
45280282
IB
22342 else if (cu->language == language_d)
22343 {
22344 /* For D, the 'main' function could be defined in any module, but it
22345 should never be prefixed. */
22346 if (strcmp (suffix, "D main") == 0)
22347 {
22348 prefix = "";
22349 sep = "";
22350 }
22351 else
22352 sep = ".";
22353 }
f55ee35c
JK
22354 else if (cu->language == language_fortran && physname)
22355 {
22356 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22357 DW_AT_MIPS_linkage_name is preferred and used instead. */
22358
22359 lead = "__";
22360 sep = "_MOD_";
22361 }
987504bb
JJ
22362 else
22363 sep = "::";
63d06c5c 22364
6dd47d34
DE
22365 if (prefix == NULL)
22366 prefix = "";
22367 if (suffix == NULL)
22368 suffix = "";
22369
987504bb
JJ
22370 if (obs == NULL)
22371 {
3e43a32a 22372 char *retval
224c3ddb
SM
22373 = ((char *)
22374 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22375
f55ee35c
JK
22376 strcpy (retval, lead);
22377 strcat (retval, prefix);
6dd47d34
DE
22378 strcat (retval, sep);
22379 strcat (retval, suffix);
63d06c5c
DC
22380 return retval;
22381 }
987504bb
JJ
22382 else
22383 {
22384 /* We have an obstack. */
f55ee35c 22385 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22386 }
63d06c5c
DC
22387}
22388
c906108c
SS
22389/* Return sibling of die, NULL if no sibling. */
22390
f9aca02d 22391static struct die_info *
fba45db2 22392sibling_die (struct die_info *die)
c906108c 22393{
639d11d3 22394 return die->sibling;
c906108c
SS
22395}
22396
71c25dea
TT
22397/* Get name of a die, return NULL if not found. */
22398
15d034d0
TT
22399static const char *
22400dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22401 struct obstack *obstack)
22402{
22403 if (name && cu->language == language_cplus)
22404 {
2f408ecb 22405 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22406
2f408ecb 22407 if (!canon_name.empty ())
71c25dea 22408 {
2f408ecb 22409 if (canon_name != name)
efba19b0 22410 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22411 }
22412 }
22413
22414 return name;
c906108c
SS
22415}
22416
96553a0c
DE
22417/* Get name of a die, return NULL if not found.
22418 Anonymous namespaces are converted to their magic string. */
9219021c 22419
15d034d0 22420static const char *
e142c38c 22421dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22422{
22423 struct attribute *attr;
518817b3 22424 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22425
e142c38c 22426 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22427 if ((!attr || !DW_STRING (attr))
96553a0c 22428 && die->tag != DW_TAG_namespace
53832f31
TT
22429 && die->tag != DW_TAG_class_type
22430 && die->tag != DW_TAG_interface_type
22431 && die->tag != DW_TAG_structure_type
22432 && die->tag != DW_TAG_union_type)
71c25dea
TT
22433 return NULL;
22434
22435 switch (die->tag)
22436 {
22437 case DW_TAG_compile_unit:
95554aad 22438 case DW_TAG_partial_unit:
71c25dea
TT
22439 /* Compilation units have a DW_AT_name that is a filename, not
22440 a source language identifier. */
22441 case DW_TAG_enumeration_type:
22442 case DW_TAG_enumerator:
22443 /* These tags always have simple identifiers already; no need
22444 to canonicalize them. */
22445 return DW_STRING (attr);
907af001 22446
96553a0c
DE
22447 case DW_TAG_namespace:
22448 if (attr != NULL && DW_STRING (attr) != NULL)
22449 return DW_STRING (attr);
22450 return CP_ANONYMOUS_NAMESPACE_STR;
22451
907af001
UW
22452 case DW_TAG_class_type:
22453 case DW_TAG_interface_type:
22454 case DW_TAG_structure_type:
22455 case DW_TAG_union_type:
22456 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22457 structures or unions. These were of the form "._%d" in GCC 4.1,
22458 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22459 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22460 if (attr && DW_STRING (attr)
61012eef
GB
22461 && (startswith (DW_STRING (attr), "._")
22462 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22463 return NULL;
53832f31
TT
22464
22465 /* GCC might emit a nameless typedef that has a linkage name. See
22466 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22467 if (!attr || DW_STRING (attr) == NULL)
22468 {
73b9be8b 22469 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22470 if (attr == NULL || DW_STRING (attr) == NULL)
22471 return NULL;
22472
df5c6c50
JK
22473 /* Avoid demangling DW_STRING (attr) the second time on a second
22474 call for the same DIE. */
22475 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22476 {
43816ebc
TT
22477 gdb::unique_xmalloc_ptr<char> demangled
22478 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22479
e6a959d6 22480 const char *base;
96408a79 22481
53832f31 22482 /* FIXME: we already did this for the partial symbol... */
34a68019 22483 DW_STRING (attr)
021887d8 22484 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 22485 demangled.get ());
53832f31 22486 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
22487
22488 /* Strip any leading namespaces/classes, keep only the base name.
22489 DW_AT_name for named DIEs does not contain the prefixes. */
22490 base = strrchr (DW_STRING (attr), ':');
22491 if (base && base > DW_STRING (attr) && base[-1] == ':')
22492 return &base[1];
22493 else
22494 return DW_STRING (attr);
53832f31
TT
22495 }
22496 }
907af001
UW
22497 break;
22498
71c25dea 22499 default:
907af001
UW
22500 break;
22501 }
22502
22503 if (!DW_STRING_IS_CANONICAL (attr))
22504 {
22505 DW_STRING (attr)
22506 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22507 &objfile->per_bfd->storage_obstack);
907af001 22508 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22509 }
907af001 22510 return DW_STRING (attr);
9219021c
DC
22511}
22512
22513/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22514 is none. *EXT_CU is the CU containing DIE on input, and the CU
22515 containing the return value on output. */
9219021c
DC
22516
22517static struct die_info *
f2f0e013 22518dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22519{
22520 struct attribute *attr;
9219021c 22521
f2f0e013 22522 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22523 if (attr == NULL)
22524 return NULL;
22525
f2f0e013 22526 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22527}
22528
fa9c3fa0
TT
22529/* A convenience function that returns an "unknown" DWARF name,
22530 including the value of V. STR is the name of the entity being
22531 printed, e.g., "TAG". */
22532
22533static const char *
22534dwarf_unknown (const char *str, unsigned v)
22535{
22536 char *cell = get_print_cell ();
22537 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22538 return cell;
22539}
22540
c906108c
SS
22541/* Convert a DIE tag into its string name. */
22542
f39c6ffd 22543static const char *
aa1ee363 22544dwarf_tag_name (unsigned tag)
c906108c 22545{
f39c6ffd
TT
22546 const char *name = get_DW_TAG_name (tag);
22547
22548 if (name == NULL)
fa9c3fa0 22549 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22550
22551 return name;
c906108c
SS
22552}
22553
22554/* Convert a DWARF attribute code into its string name. */
22555
f39c6ffd 22556static const char *
aa1ee363 22557dwarf_attr_name (unsigned attr)
c906108c 22558{
f39c6ffd
TT
22559 const char *name;
22560
c764a876 22561#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22562 if (attr == DW_AT_MIPS_fde)
22563 return "DW_AT_MIPS_fde";
22564#else
22565 if (attr == DW_AT_HP_block_index)
22566 return "DW_AT_HP_block_index";
c764a876 22567#endif
f39c6ffd
TT
22568
22569 name = get_DW_AT_name (attr);
22570
22571 if (name == NULL)
fa9c3fa0 22572 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22573
22574 return name;
c906108c
SS
22575}
22576
a084a2a6
AT
22577/* Convert a unit type to corresponding DW_UT name. */
22578
22579static const char *
22580dwarf_unit_type_name (int unit_type) {
22581 switch (unit_type)
22582 {
22583 case 0x01:
22584 return "DW_UT_compile (0x01)";
22585 case 0x02:
22586 return "DW_UT_type (0x02)";
22587 case 0x03:
22588 return "DW_UT_partial (0x03)";
22589 case 0x04:
22590 return "DW_UT_skeleton (0x04)";
22591 case 0x05:
22592 return "DW_UT_split_compile (0x05)";
22593 case 0x06:
22594 return "DW_UT_split_type (0x06)";
22595 case 0x80:
22596 return "DW_UT_lo_user (0x80)";
22597 case 0xff:
22598 return "DW_UT_hi_user (0xff)";
22599 default:
22600 return nullptr;
22601 }
22602}
22603
c906108c
SS
22604/* Convert a DWARF value form code into its string name. */
22605
f39c6ffd 22606static const char *
aa1ee363 22607dwarf_form_name (unsigned form)
c906108c 22608{
f39c6ffd
TT
22609 const char *name = get_DW_FORM_name (form);
22610
22611 if (name == NULL)
fa9c3fa0 22612 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22613
22614 return name;
c906108c
SS
22615}
22616
a121b7c1 22617static const char *
fba45db2 22618dwarf_bool_name (unsigned mybool)
c906108c
SS
22619{
22620 if (mybool)
22621 return "TRUE";
22622 else
22623 return "FALSE";
22624}
22625
22626/* Convert a DWARF type code into its string name. */
22627
f39c6ffd 22628static const char *
aa1ee363 22629dwarf_type_encoding_name (unsigned enc)
c906108c 22630{
f39c6ffd 22631 const char *name = get_DW_ATE_name (enc);
c906108c 22632
f39c6ffd 22633 if (name == NULL)
fa9c3fa0 22634 return dwarf_unknown ("ATE", enc);
c906108c 22635
f39c6ffd 22636 return name;
c906108c 22637}
c906108c 22638
f9aca02d 22639static void
d97bc12b 22640dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22641{
22642 unsigned int i;
22643
d97bc12b 22644 print_spaces (indent, f);
9d8780f0 22645 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22646 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22647 sect_offset_str (die->sect_off));
d97bc12b
DE
22648
22649 if (die->parent != NULL)
22650 {
22651 print_spaces (indent, f);
9d8780f0
SM
22652 fprintf_unfiltered (f, " parent at offset: %s\n",
22653 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22654 }
22655
22656 print_spaces (indent, f);
22657 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22658 dwarf_bool_name (die->child != NULL));
c906108c 22659
d97bc12b
DE
22660 print_spaces (indent, f);
22661 fprintf_unfiltered (f, " attributes:\n");
22662
c906108c
SS
22663 for (i = 0; i < die->num_attrs; ++i)
22664 {
d97bc12b
DE
22665 print_spaces (indent, f);
22666 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22667 dwarf_attr_name (die->attrs[i].name),
22668 dwarf_form_name (die->attrs[i].form));
d97bc12b 22669
c906108c
SS
22670 switch (die->attrs[i].form)
22671 {
c906108c 22672 case DW_FORM_addr:
336d760d 22673 case DW_FORM_addrx:
3019eac3 22674 case DW_FORM_GNU_addr_index:
d97bc12b 22675 fprintf_unfiltered (f, "address: ");
5af949e3 22676 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22677 break;
22678 case DW_FORM_block2:
22679 case DW_FORM_block4:
22680 case DW_FORM_block:
22681 case DW_FORM_block1:
56eb65bd
SP
22682 fprintf_unfiltered (f, "block: size %s",
22683 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22684 break;
2dc7f7b3 22685 case DW_FORM_exprloc:
56eb65bd
SP
22686 fprintf_unfiltered (f, "expression: size %s",
22687 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22688 break;
0224619f
JK
22689 case DW_FORM_data16:
22690 fprintf_unfiltered (f, "constant of 16 bytes");
22691 break;
4568ecf9
DE
22692 case DW_FORM_ref_addr:
22693 fprintf_unfiltered (f, "ref address: ");
22694 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22695 break;
36586728
TT
22696 case DW_FORM_GNU_ref_alt:
22697 fprintf_unfiltered (f, "alt ref address: ");
22698 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22699 break;
10b3939b
DJ
22700 case DW_FORM_ref1:
22701 case DW_FORM_ref2:
22702 case DW_FORM_ref4:
4568ecf9
DE
22703 case DW_FORM_ref8:
22704 case DW_FORM_ref_udata:
d97bc12b 22705 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22706 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22707 break;
c906108c
SS
22708 case DW_FORM_data1:
22709 case DW_FORM_data2:
22710 case DW_FORM_data4:
ce5d95e1 22711 case DW_FORM_data8:
c906108c
SS
22712 case DW_FORM_udata:
22713 case DW_FORM_sdata:
43bbcdc2
PH
22714 fprintf_unfiltered (f, "constant: %s",
22715 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22716 break;
2dc7f7b3
TT
22717 case DW_FORM_sec_offset:
22718 fprintf_unfiltered (f, "section offset: %s",
22719 pulongest (DW_UNSND (&die->attrs[i])));
22720 break;
55f1336d 22721 case DW_FORM_ref_sig8:
ac9ec31b
DE
22722 fprintf_unfiltered (f, "signature: %s",
22723 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22724 break;
c906108c 22725 case DW_FORM_string:
4bdf3d34 22726 case DW_FORM_strp:
43988095 22727 case DW_FORM_line_strp:
cf532bd1 22728 case DW_FORM_strx:
3019eac3 22729 case DW_FORM_GNU_str_index:
36586728 22730 case DW_FORM_GNU_strp_alt:
8285870a 22731 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22732 DW_STRING (&die->attrs[i])
8285870a
JK
22733 ? DW_STRING (&die->attrs[i]) : "",
22734 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22735 break;
22736 case DW_FORM_flag:
22737 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22738 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22739 else
d97bc12b 22740 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22741 break;
2dc7f7b3
TT
22742 case DW_FORM_flag_present:
22743 fprintf_unfiltered (f, "flag: TRUE");
22744 break;
a8329558 22745 case DW_FORM_indirect:
0963b4bd
MS
22746 /* The reader will have reduced the indirect form to
22747 the "base form" so this form should not occur. */
5f48f8f3 22748 fprintf_unfiltered (f,
3e43a32a 22749 "unexpected attribute form: DW_FORM_indirect");
a8329558 22750 break;
663c44ac
JK
22751 case DW_FORM_implicit_const:
22752 fprintf_unfiltered (f, "constant: %s",
22753 plongest (DW_SND (&die->attrs[i])));
22754 break;
c906108c 22755 default:
d97bc12b 22756 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22757 die->attrs[i].form);
d97bc12b 22758 break;
c906108c 22759 }
d97bc12b 22760 fprintf_unfiltered (f, "\n");
c906108c
SS
22761 }
22762}
22763
f9aca02d 22764static void
d97bc12b 22765dump_die_for_error (struct die_info *die)
c906108c 22766{
d97bc12b
DE
22767 dump_die_shallow (gdb_stderr, 0, die);
22768}
22769
22770static void
22771dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22772{
22773 int indent = level * 4;
22774
22775 gdb_assert (die != NULL);
22776
22777 if (level >= max_level)
22778 return;
22779
22780 dump_die_shallow (f, indent, die);
22781
22782 if (die->child != NULL)
c906108c 22783 {
d97bc12b
DE
22784 print_spaces (indent, f);
22785 fprintf_unfiltered (f, " Children:");
22786 if (level + 1 < max_level)
22787 {
22788 fprintf_unfiltered (f, "\n");
22789 dump_die_1 (f, level + 1, max_level, die->child);
22790 }
22791 else
22792 {
3e43a32a
MS
22793 fprintf_unfiltered (f,
22794 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22795 }
22796 }
22797
22798 if (die->sibling != NULL && level > 0)
22799 {
22800 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22801 }
22802}
22803
d97bc12b
DE
22804/* This is called from the pdie macro in gdbinit.in.
22805 It's not static so gcc will keep a copy callable from gdb. */
22806
22807void
22808dump_die (struct die_info *die, int max_level)
22809{
22810 dump_die_1 (gdb_stdlog, 0, max_level, die);
22811}
22812
f9aca02d 22813static void
51545339 22814store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22815{
51545339 22816 void **slot;
c906108c 22817
9c541725
PA
22818 slot = htab_find_slot_with_hash (cu->die_hash, die,
22819 to_underlying (die->sect_off),
b64f50a1 22820 INSERT);
51545339
DJ
22821
22822 *slot = die;
c906108c
SS
22823}
22824
b64f50a1
JK
22825/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22826 required kind. */
22827
22828static sect_offset
ff39bb5e 22829dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22830{
cd6c91b4 22831 if (attr->form_is_ref ())
9c541725 22832 return (sect_offset) DW_UNSND (attr);
93311388 22833
b98664d3 22834 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22835 dwarf_form_name (attr->form));
9c541725 22836 return {};
c906108c
SS
22837}
22838
43bbcdc2
PH
22839/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22840 * the value held by the attribute is not constant. */
a02abb62 22841
43bbcdc2 22842static LONGEST
ff39bb5e 22843dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22844{
663c44ac 22845 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22846 return DW_SND (attr);
22847 else if (attr->form == DW_FORM_udata
22848 || attr->form == DW_FORM_data1
22849 || attr->form == DW_FORM_data2
22850 || attr->form == DW_FORM_data4
22851 || attr->form == DW_FORM_data8)
22852 return DW_UNSND (attr);
22853 else
22854 {
cd6c91b4 22855 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22856 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22857 dwarf_form_name (attr->form));
22858 return default_value;
22859 }
22860}
22861
348e048f
DE
22862/* Follow reference or signature attribute ATTR of SRC_DIE.
22863 On entry *REF_CU is the CU of SRC_DIE.
22864 On exit *REF_CU is the CU of the result. */
22865
22866static struct die_info *
ff39bb5e 22867follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22868 struct dwarf2_cu **ref_cu)
22869{
22870 struct die_info *die;
22871
cd6c91b4 22872 if (attr->form_is_ref ())
348e048f 22873 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22874 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22875 die = follow_die_sig (src_die, attr, ref_cu);
22876 else
22877 {
22878 dump_die_for_error (src_die);
22879 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22880 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22881 }
22882
22883 return die;
03dd20cc
DJ
22884}
22885
5c631832 22886/* Follow reference OFFSET.
673bfd45
DE
22887 On entry *REF_CU is the CU of the source die referencing OFFSET.
22888 On exit *REF_CU is the CU of the result.
22889 Returns NULL if OFFSET is invalid. */
f504f079 22890
f9aca02d 22891static struct die_info *
9c541725 22892follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22893 struct dwarf2_cu **ref_cu)
c906108c 22894{
10b3939b 22895 struct die_info temp_die;
f2f0e013 22896 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22897 struct dwarf2_per_objfile *dwarf2_per_objfile
22898 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22899
348e048f
DE
22900 gdb_assert (cu->per_cu != NULL);
22901
98bfdba5
PA
22902 target_cu = cu;
22903
3019eac3 22904 if (cu->per_cu->is_debug_types)
348e048f
DE
22905 {
22906 /* .debug_types CUs cannot reference anything outside their CU.
22907 If they need to, they have to reference a signatured type via
55f1336d 22908 DW_FORM_ref_sig8. */
9c541725 22909 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22910 return NULL;
348e048f 22911 }
36586728 22912 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22913 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22914 {
22915 struct dwarf2_per_cu_data *per_cu;
9a619af0 22916
9c541725 22917 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22918 dwarf2_per_objfile);
03dd20cc
DJ
22919
22920 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22921 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22922 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22923
10b3939b
DJ
22924 target_cu = per_cu->cu;
22925 }
98bfdba5
PA
22926 else if (cu->dies == NULL)
22927 {
22928 /* We're loading full DIEs during partial symbol reading. */
22929 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22930 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22931 }
c906108c 22932
f2f0e013 22933 *ref_cu = target_cu;
9c541725 22934 temp_die.sect_off = sect_off;
c24bdb02
KS
22935
22936 if (target_cu != cu)
22937 target_cu->ancestor = cu;
22938
9a3c8263 22939 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22940 &temp_die,
22941 to_underlying (sect_off));
5c631832 22942}
10b3939b 22943
5c631832
JK
22944/* Follow reference attribute ATTR of SRC_DIE.
22945 On entry *REF_CU is the CU of SRC_DIE.
22946 On exit *REF_CU is the CU of the result. */
22947
22948static struct die_info *
ff39bb5e 22949follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22950 struct dwarf2_cu **ref_cu)
22951{
9c541725 22952 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22953 struct dwarf2_cu *cu = *ref_cu;
22954 struct die_info *die;
22955
9c541725 22956 die = follow_die_offset (sect_off,
36586728
TT
22957 (attr->form == DW_FORM_GNU_ref_alt
22958 || cu->per_cu->is_dwz),
22959 ref_cu);
5c631832 22960 if (!die)
9d8780f0
SM
22961 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22962 "at %s [in module %s]"),
22963 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22964 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22965
5c631832
JK
22966 return die;
22967}
22968
9c541725 22969/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22970 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22971 dwarf2_locexpr_baton->data has lifetime of
22972 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22973
22974struct dwarf2_locexpr_baton
9c541725 22975dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22976 struct dwarf2_per_cu_data *per_cu,
22977 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22978 void *baton, bool resolve_abstract_p)
5c631832 22979{
918dd910 22980 struct dwarf2_cu *cu;
5c631832
JK
22981 struct die_info *die;
22982 struct attribute *attr;
22983 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22984 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22985 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22986
918dd910 22987 if (per_cu->cu == NULL)
58f0c718 22988 load_cu (per_cu, false);
918dd910 22989 cu = per_cu->cu;
cc12ce38
DE
22990 if (cu == NULL)
22991 {
22992 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22993 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22994 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22995 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22996 }
918dd910 22997
9c541725 22998 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22999 if (!die)
9d8780f0
SM
23000 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23001 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23002
23003 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23004 if (!attr && resolve_abstract_p
3360b6e7 23005 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23006 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23007 {
23008 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 23009 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 23010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23011
3360b6e7
TV
23012 for (const auto &cand_off
23013 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23014 {
3360b6e7
TV
23015 struct dwarf2_cu *cand_cu = cu;
23016 struct die_info *cand
23017 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23018 if (!cand
23019 || !cand->parent
e4a62c65
TV
23020 || cand->parent->tag != DW_TAG_subprogram)
23021 continue;
23022
23023 CORE_ADDR pc_low, pc_high;
23024 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23025 if (pc_low == ((CORE_ADDR) -1))
23026 continue;
23027 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23028 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23029 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23030 continue;
23031
23032 die = cand;
23033 attr = dwarf2_attr (die, DW_AT_location, cu);
23034 break;
23035 }
23036 }
23037
5c631832
JK
23038 if (!attr)
23039 {
e103e986
JK
23040 /* DWARF: "If there is no such attribute, then there is no effect.".
23041 DATA is ignored if SIZE is 0. */
5c631832 23042
e103e986 23043 retval.data = NULL;
5c631832
JK
23044 retval.size = 0;
23045 }
cd6c91b4 23046 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23047 {
23048 struct dwarf2_loclist_baton loclist_baton;
23049 CORE_ADDR pc = (*get_frame_pc) (baton);
23050 size_t size;
23051
23052 fill_in_loclist_baton (cu, &loclist_baton, attr);
23053
23054 retval.data = dwarf2_find_location_expression (&loclist_baton,
23055 &size, pc);
23056 retval.size = size;
23057 }
5c631832
JK
23058 else
23059 {
4fc6c0d5 23060 if (!attr->form_is_block ())
9d8780f0 23061 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23062 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23063 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23064
23065 retval.data = DW_BLOCK (attr)->data;
23066 retval.size = DW_BLOCK (attr)->size;
23067 }
23068 retval.per_cu = cu->per_cu;
918dd910 23069
ed2dc618 23070 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23071
5c631832 23072 return retval;
348e048f
DE
23073}
23074
8b9737bf
TT
23075/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23076 offset. */
23077
23078struct dwarf2_locexpr_baton
23079dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23080 struct dwarf2_per_cu_data *per_cu,
23081 CORE_ADDR (*get_frame_pc) (void *baton),
23082 void *baton)
23083{
9c541725 23084 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23085
9c541725 23086 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23087}
23088
b6807d98
TT
23089/* Write a constant of a given type as target-ordered bytes into
23090 OBSTACK. */
23091
23092static const gdb_byte *
23093write_constant_as_bytes (struct obstack *obstack,
23094 enum bfd_endian byte_order,
23095 struct type *type,
23096 ULONGEST value,
23097 LONGEST *len)
23098{
23099 gdb_byte *result;
23100
23101 *len = TYPE_LENGTH (type);
224c3ddb 23102 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23103 store_unsigned_integer (result, *len, byte_order, value);
23104
23105 return result;
23106}
23107
23108/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23109 pointer to the constant bytes and set LEN to the length of the
23110 data. If memory is needed, allocate it on OBSTACK. If the DIE
23111 does not have a DW_AT_const_value, return NULL. */
23112
23113const gdb_byte *
9c541725 23114dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23115 struct dwarf2_per_cu_data *per_cu,
23116 struct obstack *obstack,
23117 LONGEST *len)
23118{
23119 struct dwarf2_cu *cu;
23120 struct die_info *die;
23121 struct attribute *attr;
23122 const gdb_byte *result = NULL;
23123 struct type *type;
23124 LONGEST value;
23125 enum bfd_endian byte_order;
e3b94546 23126 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23127
b6807d98 23128 if (per_cu->cu == NULL)
58f0c718 23129 load_cu (per_cu, false);
b6807d98 23130 cu = per_cu->cu;
cc12ce38
DE
23131 if (cu == NULL)
23132 {
23133 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23134 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23135 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23136 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23137 }
b6807d98 23138
9c541725 23139 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23140 if (!die)
9d8780f0
SM
23141 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23142 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23143
23144 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23145 if (attr == NULL)
23146 return NULL;
23147
e3b94546 23148 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23149 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23150
23151 switch (attr->form)
23152 {
23153 case DW_FORM_addr:
336d760d 23154 case DW_FORM_addrx:
b6807d98
TT
23155 case DW_FORM_GNU_addr_index:
23156 {
23157 gdb_byte *tem;
23158
23159 *len = cu->header.addr_size;
224c3ddb 23160 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23161 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23162 result = tem;
23163 }
23164 break;
23165 case DW_FORM_string:
23166 case DW_FORM_strp:
cf532bd1 23167 case DW_FORM_strx:
b6807d98
TT
23168 case DW_FORM_GNU_str_index:
23169 case DW_FORM_GNU_strp_alt:
23170 /* DW_STRING is already allocated on the objfile obstack, point
23171 directly to it. */
23172 result = (const gdb_byte *) DW_STRING (attr);
23173 *len = strlen (DW_STRING (attr));
23174 break;
23175 case DW_FORM_block1:
23176 case DW_FORM_block2:
23177 case DW_FORM_block4:
23178 case DW_FORM_block:
23179 case DW_FORM_exprloc:
0224619f 23180 case DW_FORM_data16:
b6807d98
TT
23181 result = DW_BLOCK (attr)->data;
23182 *len = DW_BLOCK (attr)->size;
23183 break;
23184
23185 /* The DW_AT_const_value attributes are supposed to carry the
23186 symbol's value "represented as it would be on the target
23187 architecture." By the time we get here, it's already been
23188 converted to host endianness, so we just need to sign- or
23189 zero-extend it as appropriate. */
23190 case DW_FORM_data1:
23191 type = die_type (die, cu);
23192 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23193 if (result == NULL)
23194 result = write_constant_as_bytes (obstack, byte_order,
23195 type, value, len);
23196 break;
23197 case DW_FORM_data2:
23198 type = die_type (die, cu);
23199 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23200 if (result == NULL)
23201 result = write_constant_as_bytes (obstack, byte_order,
23202 type, value, len);
23203 break;
23204 case DW_FORM_data4:
23205 type = die_type (die, cu);
23206 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23207 if (result == NULL)
23208 result = write_constant_as_bytes (obstack, byte_order,
23209 type, value, len);
23210 break;
23211 case DW_FORM_data8:
23212 type = die_type (die, cu);
23213 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23214 if (result == NULL)
23215 result = write_constant_as_bytes (obstack, byte_order,
23216 type, value, len);
23217 break;
23218
23219 case DW_FORM_sdata:
663c44ac 23220 case DW_FORM_implicit_const:
b6807d98
TT
23221 type = die_type (die, cu);
23222 result = write_constant_as_bytes (obstack, byte_order,
23223 type, DW_SND (attr), len);
23224 break;
23225
23226 case DW_FORM_udata:
23227 type = die_type (die, cu);
23228 result = write_constant_as_bytes (obstack, byte_order,
23229 type, DW_UNSND (attr), len);
23230 break;
23231
23232 default:
b98664d3 23233 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23234 dwarf_form_name (attr->form));
23235 break;
23236 }
23237
23238 return result;
23239}
23240
7942e96e
AA
23241/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23242 valid type for this die is found. */
23243
23244struct type *
9c541725 23245dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23246 struct dwarf2_per_cu_data *per_cu)
23247{
23248 struct dwarf2_cu *cu;
23249 struct die_info *die;
23250
7942e96e 23251 if (per_cu->cu == NULL)
58f0c718 23252 load_cu (per_cu, false);
7942e96e
AA
23253 cu = per_cu->cu;
23254 if (!cu)
23255 return NULL;
23256
9c541725 23257 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23258 if (!die)
23259 return NULL;
23260
23261 return die_type (die, cu);
23262}
23263
8a9b8146
TT
23264/* Return the type of the DIE at DIE_OFFSET in the CU named by
23265 PER_CU. */
23266
23267struct type *
b64f50a1 23268dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23269 struct dwarf2_per_cu_data *per_cu)
23270{
9c541725 23271 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23272 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23273}
23274
ac9ec31b 23275/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23276 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23277 On exit *REF_CU is the CU of the result.
23278 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23279
23280static struct die_info *
ac9ec31b
DE
23281follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23282 struct dwarf2_cu **ref_cu)
348e048f 23283{
348e048f 23284 struct die_info temp_die;
c24bdb02 23285 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23286 struct die_info *die;
23287
ac9ec31b
DE
23288 /* While it might be nice to assert sig_type->type == NULL here,
23289 we can get here for DW_AT_imported_declaration where we need
23290 the DIE not the type. */
348e048f
DE
23291
23292 /* If necessary, add it to the queue and load its DIEs. */
23293
95554aad 23294 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23295 read_signatured_type (sig_type);
348e048f 23296
348e048f 23297 sig_cu = sig_type->per_cu.cu;
69d751e3 23298 gdb_assert (sig_cu != NULL);
9c541725
PA
23299 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23300 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23301 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23302 to_underlying (temp_die.sect_off));
348e048f
DE
23303 if (die)
23304 {
ed2dc618 23305 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23306 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23307
796a7ff8
DE
23308 /* For .gdb_index version 7 keep track of included TUs.
23309 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23310 if (dwarf2_per_objfile->index_table != NULL
23311 && dwarf2_per_objfile->index_table->version <= 7)
23312 {
ae640021 23313 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23314 }
23315
348e048f 23316 *ref_cu = sig_cu;
c24bdb02
KS
23317 if (sig_cu != cu)
23318 sig_cu->ancestor = cu;
23319
348e048f
DE
23320 return die;
23321 }
23322
ac9ec31b
DE
23323 return NULL;
23324}
23325
23326/* Follow signatured type referenced by ATTR in SRC_DIE.
23327 On entry *REF_CU is the CU of SRC_DIE.
23328 On exit *REF_CU is the CU of the result.
23329 The result is the DIE of the type.
23330 If the referenced type cannot be found an error is thrown. */
23331
23332static struct die_info *
ff39bb5e 23333follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23334 struct dwarf2_cu **ref_cu)
23335{
23336 ULONGEST signature = DW_SIGNATURE (attr);
23337 struct signatured_type *sig_type;
23338 struct die_info *die;
23339
23340 gdb_assert (attr->form == DW_FORM_ref_sig8);
23341
a2ce51a0 23342 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23343 /* sig_type will be NULL if the signatured type is missing from
23344 the debug info. */
23345 if (sig_type == NULL)
23346 {
23347 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23348 " from DIE at %s [in module %s]"),
23349 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23350 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23351 }
23352
23353 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23354 if (die == NULL)
23355 {
23356 dump_die_for_error (src_die);
23357 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23358 " from DIE at %s [in module %s]"),
23359 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23360 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23361 }
23362
23363 return die;
23364}
23365
23366/* Get the type specified by SIGNATURE referenced in DIE/CU,
23367 reading in and processing the type unit if necessary. */
23368
23369static struct type *
23370get_signatured_type (struct die_info *die, ULONGEST signature,
23371 struct dwarf2_cu *cu)
23372{
518817b3
SM
23373 struct dwarf2_per_objfile *dwarf2_per_objfile
23374 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23375 struct signatured_type *sig_type;
23376 struct dwarf2_cu *type_cu;
23377 struct die_info *type_die;
23378 struct type *type;
23379
a2ce51a0 23380 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23381 /* sig_type will be NULL if the signatured type is missing from
23382 the debug info. */
23383 if (sig_type == NULL)
23384 {
b98664d3 23385 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23386 " from DIE at %s [in module %s]"),
23387 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23388 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23389 return build_error_marker_type (cu, die);
23390 }
23391
23392 /* If we already know the type we're done. */
23393 if (sig_type->type != NULL)
23394 return sig_type->type;
23395
23396 type_cu = cu;
23397 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23398 if (type_die != NULL)
23399 {
23400 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23401 is created. This is important, for example, because for c++ classes
23402 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23403 type = read_type_die (type_die, type_cu);
23404 if (type == NULL)
23405 {
b98664d3 23406 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23407 " referenced from DIE at %s [in module %s]"),
23408 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23409 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23410 type = build_error_marker_type (cu, die);
23411 }
23412 }
23413 else
23414 {
b98664d3 23415 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23416 " from DIE at %s [in module %s]"),
23417 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23418 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23419 type = build_error_marker_type (cu, die);
23420 }
23421 sig_type->type = type;
23422
23423 return type;
23424}
23425
23426/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23427 reading in and processing the type unit if necessary. */
23428
23429static struct type *
ff39bb5e 23430get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23431 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23432{
23433 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23434 if (attr->form_is_ref ())
ac9ec31b
DE
23435 {
23436 struct dwarf2_cu *type_cu = cu;
23437 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23438
23439 return read_type_die (type_die, type_cu);
23440 }
23441 else if (attr->form == DW_FORM_ref_sig8)
23442 {
23443 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23444 }
23445 else
23446 {
518817b3
SM
23447 struct dwarf2_per_objfile *dwarf2_per_objfile
23448 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23449
b98664d3 23450 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23451 " at %s [in module %s]"),
23452 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23453 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23454 return build_error_marker_type (cu, die);
23455 }
348e048f
DE
23456}
23457
e5fe5e75 23458/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23459
23460static void
e5fe5e75 23461load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23462{
52dc124a 23463 struct signatured_type *sig_type;
348e048f 23464
f4dc4d17
DE
23465 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23466 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23467
6721b2ec
DE
23468 /* We have the per_cu, but we need the signatured_type.
23469 Fortunately this is an easy translation. */
23470 gdb_assert (per_cu->is_debug_types);
23471 sig_type = (struct signatured_type *) per_cu;
348e048f 23472
6721b2ec 23473 gdb_assert (per_cu->cu == NULL);
348e048f 23474
52dc124a 23475 read_signatured_type (sig_type);
348e048f 23476
6721b2ec 23477 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23478}
23479
3019eac3
DE
23480/* Read in a signatured type and build its CU and DIEs.
23481 If the type is a stub for the real type in a DWO file,
23482 read in the real type from the DWO file as well. */
dee91e82
DE
23483
23484static void
23485read_signatured_type (struct signatured_type *sig_type)
23486{
23487 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23488
3019eac3 23489 gdb_assert (per_cu->is_debug_types);
dee91e82 23490 gdb_assert (per_cu->cu == NULL);
348e048f 23491
c0ab21c2
TT
23492 cutu_reader reader (per_cu, NULL, 0, 1, false);
23493
23494 if (!reader.dummy_p)
23495 {
23496 struct dwarf2_cu *cu = reader.cu;
23497 const gdb_byte *info_ptr = reader.info_ptr;
23498
23499 gdb_assert (cu->die_hash == NULL);
23500 cu->die_hash =
23501 htab_create_alloc_ex (cu->header.length / 12,
23502 die_hash,
23503 die_eq,
23504 NULL,
23505 &cu->comp_unit_obstack,
23506 hashtab_obstack_allocate,
23507 dummy_obstack_deallocate);
23508
23509 if (reader.has_children)
23510 reader.comp_unit_die->child
23511 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23512 reader.comp_unit_die);
23513 cu->dies = reader.comp_unit_die;
23514 /* comp_unit_die is not stored in die_hash, no need. */
23515
23516 /* We try not to read any attributes in this function, because
23517 not all CUs needed for references have been loaded yet, and
23518 symbol table processing isn't initialized. But we have to
23519 set the CU language, or we won't be able to build types
23520 correctly. Similarly, if we do not read the producer, we can
23521 not apply producer-specific interpretation. */
23522 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23523 }
23524
7ee85ab1 23525 sig_type->per_cu.tu_read = 1;
c906108c
SS
23526}
23527
c906108c
SS
23528/* Decode simple location descriptions.
23529 Given a pointer to a dwarf block that defines a location, compute
23530 the location and return the value.
23531
4cecd739
DJ
23532 NOTE drow/2003-11-18: This function is called in two situations
23533 now: for the address of static or global variables (partial symbols
23534 only) and for offsets into structures which are expected to be
23535 (more or less) constant. The partial symbol case should go away,
23536 and only the constant case should remain. That will let this
23537 function complain more accurately. A few special modes are allowed
23538 without complaint for global variables (for instance, global
23539 register values and thread-local values).
c906108c
SS
23540
23541 A location description containing no operations indicates that the
4cecd739 23542 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23543 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23544 callers will only want a very basic result and this can become a
21ae7a4d
JK
23545 complaint.
23546
23547 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23548
23549static CORE_ADDR
e7c27a73 23550decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23551{
518817b3 23552 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23553 size_t i;
23554 size_t size = blk->size;
d521ce57 23555 const gdb_byte *data = blk->data;
21ae7a4d
JK
23556 CORE_ADDR stack[64];
23557 int stacki;
23558 unsigned int bytes_read, unsnd;
23559 gdb_byte op;
c906108c 23560
21ae7a4d
JK
23561 i = 0;
23562 stacki = 0;
23563 stack[stacki] = 0;
23564 stack[++stacki] = 0;
23565
23566 while (i < size)
23567 {
23568 op = data[i++];
23569 switch (op)
23570 {
23571 case DW_OP_lit0:
23572 case DW_OP_lit1:
23573 case DW_OP_lit2:
23574 case DW_OP_lit3:
23575 case DW_OP_lit4:
23576 case DW_OP_lit5:
23577 case DW_OP_lit6:
23578 case DW_OP_lit7:
23579 case DW_OP_lit8:
23580 case DW_OP_lit9:
23581 case DW_OP_lit10:
23582 case DW_OP_lit11:
23583 case DW_OP_lit12:
23584 case DW_OP_lit13:
23585 case DW_OP_lit14:
23586 case DW_OP_lit15:
23587 case DW_OP_lit16:
23588 case DW_OP_lit17:
23589 case DW_OP_lit18:
23590 case DW_OP_lit19:
23591 case DW_OP_lit20:
23592 case DW_OP_lit21:
23593 case DW_OP_lit22:
23594 case DW_OP_lit23:
23595 case DW_OP_lit24:
23596 case DW_OP_lit25:
23597 case DW_OP_lit26:
23598 case DW_OP_lit27:
23599 case DW_OP_lit28:
23600 case DW_OP_lit29:
23601 case DW_OP_lit30:
23602 case DW_OP_lit31:
23603 stack[++stacki] = op - DW_OP_lit0;
23604 break;
f1bea926 23605
21ae7a4d
JK
23606 case DW_OP_reg0:
23607 case DW_OP_reg1:
23608 case DW_OP_reg2:
23609 case DW_OP_reg3:
23610 case DW_OP_reg4:
23611 case DW_OP_reg5:
23612 case DW_OP_reg6:
23613 case DW_OP_reg7:
23614 case DW_OP_reg8:
23615 case DW_OP_reg9:
23616 case DW_OP_reg10:
23617 case DW_OP_reg11:
23618 case DW_OP_reg12:
23619 case DW_OP_reg13:
23620 case DW_OP_reg14:
23621 case DW_OP_reg15:
23622 case DW_OP_reg16:
23623 case DW_OP_reg17:
23624 case DW_OP_reg18:
23625 case DW_OP_reg19:
23626 case DW_OP_reg20:
23627 case DW_OP_reg21:
23628 case DW_OP_reg22:
23629 case DW_OP_reg23:
23630 case DW_OP_reg24:
23631 case DW_OP_reg25:
23632 case DW_OP_reg26:
23633 case DW_OP_reg27:
23634 case DW_OP_reg28:
23635 case DW_OP_reg29:
23636 case DW_OP_reg30:
23637 case DW_OP_reg31:
23638 stack[++stacki] = op - DW_OP_reg0;
23639 if (i < size)
23640 dwarf2_complex_location_expr_complaint ();
23641 break;
c906108c 23642
21ae7a4d
JK
23643 case DW_OP_regx:
23644 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23645 i += bytes_read;
23646 stack[++stacki] = unsnd;
23647 if (i < size)
23648 dwarf2_complex_location_expr_complaint ();
23649 break;
c906108c 23650
21ae7a4d
JK
23651 case DW_OP_addr:
23652 stack[++stacki] = read_address (objfile->obfd, &data[i],
23653 cu, &bytes_read);
23654 i += bytes_read;
23655 break;
d53d4ac5 23656
21ae7a4d
JK
23657 case DW_OP_const1u:
23658 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23659 i += 1;
23660 break;
23661
23662 case DW_OP_const1s:
23663 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23664 i += 1;
23665 break;
23666
23667 case DW_OP_const2u:
23668 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23669 i += 2;
23670 break;
23671
23672 case DW_OP_const2s:
23673 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23674 i += 2;
23675 break;
d53d4ac5 23676
21ae7a4d
JK
23677 case DW_OP_const4u:
23678 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23679 i += 4;
23680 break;
23681
23682 case DW_OP_const4s:
23683 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23684 i += 4;
23685 break;
23686
585861ea
JK
23687 case DW_OP_const8u:
23688 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23689 i += 8;
23690 break;
23691
21ae7a4d
JK
23692 case DW_OP_constu:
23693 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23694 &bytes_read);
23695 i += bytes_read;
23696 break;
23697
23698 case DW_OP_consts:
23699 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23700 i += bytes_read;
23701 break;
23702
23703 case DW_OP_dup:
23704 stack[stacki + 1] = stack[stacki];
23705 stacki++;
23706 break;
23707
23708 case DW_OP_plus:
23709 stack[stacki - 1] += stack[stacki];
23710 stacki--;
23711 break;
23712
23713 case DW_OP_plus_uconst:
23714 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23715 &bytes_read);
23716 i += bytes_read;
23717 break;
23718
23719 case DW_OP_minus:
23720 stack[stacki - 1] -= stack[stacki];
23721 stacki--;
23722 break;
23723
23724 case DW_OP_deref:
23725 /* If we're not the last op, then we definitely can't encode
23726 this using GDB's address_class enum. This is valid for partial
23727 global symbols, although the variable's address will be bogus
23728 in the psymtab. */
23729 if (i < size)
23730 dwarf2_complex_location_expr_complaint ();
23731 break;
23732
23733 case DW_OP_GNU_push_tls_address:
4aa4e28b 23734 case DW_OP_form_tls_address:
21ae7a4d
JK
23735 /* The top of the stack has the offset from the beginning
23736 of the thread control block at which the variable is located. */
23737 /* Nothing should follow this operator, so the top of stack would
23738 be returned. */
23739 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23740 address will be bogus in the psymtab. Make it always at least
23741 non-zero to not look as a variable garbage collected by linker
23742 which have DW_OP_addr 0. */
21ae7a4d
JK
23743 if (i < size)
23744 dwarf2_complex_location_expr_complaint ();
585861ea 23745 stack[stacki]++;
21ae7a4d
JK
23746 break;
23747
23748 case DW_OP_GNU_uninit:
23749 break;
23750
336d760d 23751 case DW_OP_addrx:
3019eac3 23752 case DW_OP_GNU_addr_index:
49f6c839 23753 case DW_OP_GNU_const_index:
3019eac3
DE
23754 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23755 &bytes_read);
23756 i += bytes_read;
23757 break;
23758
21ae7a4d
JK
23759 default:
23760 {
f39c6ffd 23761 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23762
23763 if (name)
b98664d3 23764 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23765 name);
23766 else
b98664d3 23767 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23768 op);
23769 }
23770
23771 return (stack[stacki]);
d53d4ac5 23772 }
3c6e0cb3 23773
21ae7a4d
JK
23774 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23775 outside of the allocated space. Also enforce minimum>0. */
23776 if (stacki >= ARRAY_SIZE (stack) - 1)
23777 {
b98664d3 23778 complaint (_("location description stack overflow"));
21ae7a4d
JK
23779 return 0;
23780 }
23781
23782 if (stacki <= 0)
23783 {
b98664d3 23784 complaint (_("location description stack underflow"));
21ae7a4d
JK
23785 return 0;
23786 }
23787 }
23788 return (stack[stacki]);
c906108c
SS
23789}
23790
23791/* memory allocation interface */
23792
c906108c 23793static struct dwarf_block *
7b5a2f43 23794dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23795{
8d749320 23796 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23797}
23798
c906108c 23799static struct die_info *
b60c80d6 23800dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23801{
23802 struct die_info *die;
b60c80d6
DJ
23803 size_t size = sizeof (struct die_info);
23804
23805 if (num_attrs > 1)
23806 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23807
b60c80d6 23808 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23809 memset (die, 0, sizeof (struct die_info));
23810 return (die);
23811}
2e276125
JB
23812
23813\f
23814/* Macro support. */
23815
233d95b5
JK
23816/* Return file name relative to the compilation directory of file number I in
23817 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23818 responsible for freeing it. */
233d95b5 23819
2e276125 23820static char *
233d95b5 23821file_file_name (int file, struct line_header *lh)
2e276125 23822{
6a83a1e6
EZ
23823 /* Is the file number a valid index into the line header's file name
23824 table? Remember that file numbers start with one, not zero. */
7ba99d21 23825 if (lh->is_valid_file_index (file))
6a83a1e6 23826 {
7ba99d21 23827 const file_entry *fe = lh->file_name_at (file);
6e70227d 23828
7ba99d21 23829 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 23830 {
7ba99d21 23831 const char *dir = fe->include_dir (lh);
8c43009f 23832 if (dir != NULL)
7ba99d21 23833 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 23834 }
7ba99d21 23835 return xstrdup (fe->name);
6a83a1e6 23836 }
2e276125
JB
23837 else
23838 {
6a83a1e6
EZ
23839 /* The compiler produced a bogus file number. We can at least
23840 record the macro definitions made in the file, even if we
23841 won't be able to find the file by name. */
23842 char fake_name[80];
9a619af0 23843
8c042590
PM
23844 xsnprintf (fake_name, sizeof (fake_name),
23845 "<bad macro file number %d>", file);
2e276125 23846
b98664d3 23847 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 23848 file);
2e276125 23849
6a83a1e6 23850 return xstrdup (fake_name);
2e276125
JB
23851 }
23852}
23853
233d95b5
JK
23854/* Return the full name of file number I in *LH's file name table.
23855 Use COMP_DIR as the name of the current directory of the
23856 compilation. The result is allocated using xmalloc; the caller is
23857 responsible for freeing it. */
23858static char *
23859file_full_name (int file, struct line_header *lh, const char *comp_dir)
23860{
23861 /* Is the file number a valid index into the line header's file name
23862 table? Remember that file numbers start with one, not zero. */
7ba99d21 23863 if (lh->is_valid_file_index (file))
233d95b5
JK
23864 {
23865 char *relative = file_file_name (file, lh);
23866
23867 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23868 return relative;
b36cec19
PA
23869 return reconcat (relative, comp_dir, SLASH_STRING,
23870 relative, (char *) NULL);
233d95b5
JK
23871 }
23872 else
23873 return file_file_name (file, lh);
23874}
23875
2e276125
JB
23876
23877static struct macro_source_file *
804d2729
TT
23878macro_start_file (struct dwarf2_cu *cu,
23879 int file, int line,
2e276125 23880 struct macro_source_file *current_file,
43f3e411 23881 struct line_header *lh)
2e276125 23882{
233d95b5
JK
23883 /* File name relative to the compilation directory of this source file. */
23884 char *file_name = file_file_name (file, lh);
2e276125 23885
2e276125 23886 if (! current_file)
abc9d0dc 23887 {
fc474241
DE
23888 /* Note: We don't create a macro table for this compilation unit
23889 at all until we actually get a filename. */
c24bdb02 23890 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 23891
abc9d0dc
TT
23892 /* If we have no current file, then this must be the start_file
23893 directive for the compilation unit's main source file. */
fc474241
DE
23894 current_file = macro_set_main (macro_table, file_name);
23895 macro_define_special (macro_table);
abc9d0dc 23896 }
2e276125 23897 else
233d95b5 23898 current_file = macro_include (current_file, line, file_name);
2e276125 23899
233d95b5 23900 xfree (file_name);
6e70227d 23901
2e276125
JB
23902 return current_file;
23903}
23904
2e276125
JB
23905static const char *
23906consume_improper_spaces (const char *p, const char *body)
23907{
23908 if (*p == ' ')
23909 {
b98664d3 23910 complaint (_("macro definition contains spaces "
3e43a32a 23911 "in formal argument list:\n`%s'"),
4d3c2250 23912 body);
2e276125
JB
23913
23914 while (*p == ' ')
23915 p++;
23916 }
23917
23918 return p;
23919}
23920
23921
23922static void
23923parse_macro_definition (struct macro_source_file *file, int line,
23924 const char *body)
23925{
23926 const char *p;
23927
23928 /* The body string takes one of two forms. For object-like macro
23929 definitions, it should be:
23930
23931 <macro name> " " <definition>
23932
23933 For function-like macro definitions, it should be:
23934
23935 <macro name> "() " <definition>
23936 or
23937 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23938
23939 Spaces may appear only where explicitly indicated, and in the
23940 <definition>.
23941
23942 The Dwarf 2 spec says that an object-like macro's name is always
23943 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23944 the space when the macro's definition is the empty string.
2e276125
JB
23945
23946 The Dwarf 2 spec says that there should be no spaces between the
23947 formal arguments in a function-like macro's formal argument list,
23948 but versions of GCC around March 2002 include spaces after the
23949 commas. */
23950
23951
23952 /* Find the extent of the macro name. The macro name is terminated
23953 by either a space or null character (for an object-like macro) or
23954 an opening paren (for a function-like macro). */
23955 for (p = body; *p; p++)
23956 if (*p == ' ' || *p == '(')
23957 break;
23958
23959 if (*p == ' ' || *p == '\0')
23960 {
23961 /* It's an object-like macro. */
23962 int name_len = p - body;
456e800a 23963 std::string name (body, name_len);
2e276125
JB
23964 const char *replacement;
23965
23966 if (*p == ' ')
23967 replacement = body + name_len + 1;
23968 else
23969 {
4d3c2250 23970 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23971 replacement = body + name_len;
23972 }
6e70227d 23973
456e800a 23974 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23975 }
23976 else if (*p == '(')
23977 {
23978 /* It's a function-like macro. */
456e800a 23979 std::string name (body, p - body);
2e276125
JB
23980 int argc = 0;
23981 int argv_size = 1;
8d749320 23982 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23983
23984 p++;
23985
23986 p = consume_improper_spaces (p, body);
23987
23988 /* Parse the formal argument list. */
23989 while (*p && *p != ')')
23990 {
23991 /* Find the extent of the current argument name. */
23992 const char *arg_start = p;
23993
23994 while (*p && *p != ',' && *p != ')' && *p != ' ')
23995 p++;
23996
23997 if (! *p || p == arg_start)
4d3c2250 23998 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23999 else
24000 {
24001 /* Make sure argv has room for the new argument. */
24002 if (argc >= argv_size)
24003 {
24004 argv_size *= 2;
224c3ddb 24005 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24006 }
24007
3f8a7804 24008 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24009 }
24010
24011 p = consume_improper_spaces (p, body);
24012
24013 /* Consume the comma, if present. */
24014 if (*p == ',')
24015 {
24016 p++;
24017
24018 p = consume_improper_spaces (p, body);
24019 }
24020 }
24021
24022 if (*p == ')')
24023 {
24024 p++;
24025
24026 if (*p == ' ')
24027 /* Perfectly formed definition, no complaints. */
456e800a 24028 macro_define_function (file, line, name.c_str (),
6e70227d 24029 argc, (const char **) argv,
2e276125
JB
24030 p + 1);
24031 else if (*p == '\0')
24032 {
24033 /* Complain, but do define it. */
4d3c2250 24034 dwarf2_macro_malformed_definition_complaint (body);
456e800a 24035 macro_define_function (file, line, name.c_str (),
6e70227d 24036 argc, (const char **) argv,
2e276125
JB
24037 p);
24038 }
24039 else
24040 /* Just complain. */
4d3c2250 24041 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24042 }
24043 else
24044 /* Just complain. */
4d3c2250 24045 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24046
2e276125
JB
24047 {
24048 int i;
24049
24050 for (i = 0; i < argc; i++)
24051 xfree (argv[i]);
24052 }
24053 xfree (argv);
24054 }
24055 else
4d3c2250 24056 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24057}
24058
cf2c3c16
TT
24059/* Skip some bytes from BYTES according to the form given in FORM.
24060 Returns the new pointer. */
2e276125 24061
d521ce57
TT
24062static const gdb_byte *
24063skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24064 enum dwarf_form form,
24065 unsigned int offset_size,
24066 struct dwarf2_section_info *section)
2e276125 24067{
cf2c3c16 24068 unsigned int bytes_read;
2e276125 24069
cf2c3c16 24070 switch (form)
2e276125 24071 {
cf2c3c16
TT
24072 case DW_FORM_data1:
24073 case DW_FORM_flag:
24074 ++bytes;
24075 break;
24076
24077 case DW_FORM_data2:
24078 bytes += 2;
24079 break;
24080
24081 case DW_FORM_data4:
24082 bytes += 4;
24083 break;
24084
24085 case DW_FORM_data8:
24086 bytes += 8;
24087 break;
24088
0224619f
JK
24089 case DW_FORM_data16:
24090 bytes += 16;
24091 break;
24092
cf2c3c16
TT
24093 case DW_FORM_string:
24094 read_direct_string (abfd, bytes, &bytes_read);
24095 bytes += bytes_read;
24096 break;
24097
24098 case DW_FORM_sec_offset:
24099 case DW_FORM_strp:
36586728 24100 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24101 bytes += offset_size;
24102 break;
24103
24104 case DW_FORM_block:
24105 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24106 bytes += bytes_read;
24107 break;
24108
24109 case DW_FORM_block1:
24110 bytes += 1 + read_1_byte (abfd, bytes);
24111 break;
24112 case DW_FORM_block2:
24113 bytes += 2 + read_2_bytes (abfd, bytes);
24114 break;
24115 case DW_FORM_block4:
24116 bytes += 4 + read_4_bytes (abfd, bytes);
24117 break;
24118
336d760d 24119 case DW_FORM_addrx:
cf2c3c16 24120 case DW_FORM_sdata:
cf532bd1 24121 case DW_FORM_strx:
cf2c3c16 24122 case DW_FORM_udata:
3019eac3
DE
24123 case DW_FORM_GNU_addr_index:
24124 case DW_FORM_GNU_str_index:
d521ce57 24125 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24126 if (bytes == NULL)
24127 {
24128 dwarf2_section_buffer_overflow_complaint (section);
24129 return NULL;
24130 }
cf2c3c16
TT
24131 break;
24132
663c44ac
JK
24133 case DW_FORM_implicit_const:
24134 break;
24135
cf2c3c16
TT
24136 default:
24137 {
b98664d3 24138 complaint (_("invalid form 0x%x in `%s'"),
96b79293 24139 form, section->get_name ());
cf2c3c16
TT
24140 return NULL;
24141 }
2e276125
JB
24142 }
24143
cf2c3c16
TT
24144 return bytes;
24145}
757a13d0 24146
cf2c3c16
TT
24147/* A helper for dwarf_decode_macros that handles skipping an unknown
24148 opcode. Returns an updated pointer to the macro data buffer; or,
24149 on error, issues a complaint and returns NULL. */
757a13d0 24150
d521ce57 24151static const gdb_byte *
cf2c3c16 24152skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24153 const gdb_byte **opcode_definitions,
24154 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24155 bfd *abfd,
24156 unsigned int offset_size,
24157 struct dwarf2_section_info *section)
24158{
24159 unsigned int bytes_read, i;
24160 unsigned long arg;
d521ce57 24161 const gdb_byte *defn;
2e276125 24162
cf2c3c16 24163 if (opcode_definitions[opcode] == NULL)
2e276125 24164 {
b98664d3 24165 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24166 opcode);
24167 return NULL;
24168 }
2e276125 24169
cf2c3c16
TT
24170 defn = opcode_definitions[opcode];
24171 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24172 defn += bytes_read;
2e276125 24173
cf2c3c16
TT
24174 for (i = 0; i < arg; ++i)
24175 {
aead7601
SM
24176 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24177 (enum dwarf_form) defn[i], offset_size,
f664829e 24178 section);
cf2c3c16
TT
24179 if (mac_ptr == NULL)
24180 {
24181 /* skip_form_bytes already issued the complaint. */
24182 return NULL;
24183 }
24184 }
757a13d0 24185
cf2c3c16
TT
24186 return mac_ptr;
24187}
757a13d0 24188
cf2c3c16
TT
24189/* A helper function which parses the header of a macro section.
24190 If the macro section is the extended (for now called "GNU") type,
24191 then this updates *OFFSET_SIZE. Returns a pointer to just after
24192 the header, or issues a complaint and returns NULL on error. */
757a13d0 24193
d521ce57
TT
24194static const gdb_byte *
24195dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24196 bfd *abfd,
d521ce57 24197 const gdb_byte *mac_ptr,
cf2c3c16
TT
24198 unsigned int *offset_size,
24199 int section_is_gnu)
24200{
24201 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24202
cf2c3c16
TT
24203 if (section_is_gnu)
24204 {
24205 unsigned int version, flags;
757a13d0 24206
cf2c3c16 24207 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24208 if (version != 4 && version != 5)
cf2c3c16 24209 {
b98664d3 24210 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24211 version);
24212 return NULL;
24213 }
24214 mac_ptr += 2;
757a13d0 24215
cf2c3c16
TT
24216 flags = read_1_byte (abfd, mac_ptr);
24217 ++mac_ptr;
24218 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24219
cf2c3c16
TT
24220 if ((flags & 2) != 0)
24221 /* We don't need the line table offset. */
24222 mac_ptr += *offset_size;
757a13d0 24223
cf2c3c16
TT
24224 /* Vendor opcode descriptions. */
24225 if ((flags & 4) != 0)
24226 {
24227 unsigned int i, count;
757a13d0 24228
cf2c3c16
TT
24229 count = read_1_byte (abfd, mac_ptr);
24230 ++mac_ptr;
24231 for (i = 0; i < count; ++i)
24232 {
24233 unsigned int opcode, bytes_read;
24234 unsigned long arg;
24235
24236 opcode = read_1_byte (abfd, mac_ptr);
24237 ++mac_ptr;
24238 opcode_definitions[opcode] = mac_ptr;
24239 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24240 mac_ptr += bytes_read;
24241 mac_ptr += arg;
24242 }
757a13d0 24243 }
cf2c3c16 24244 }
757a13d0 24245
cf2c3c16
TT
24246 return mac_ptr;
24247}
757a13d0 24248
cf2c3c16 24249/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24250 including DW_MACRO_import. */
cf2c3c16
TT
24251
24252static void
804d2729 24253dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24254 bfd *abfd,
d521ce57 24255 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24256 struct macro_source_file *current_file,
43f3e411 24257 struct line_header *lh,
cf2c3c16 24258 struct dwarf2_section_info *section,
36586728 24259 int section_is_gnu, int section_is_dwz,
cf2c3c16 24260 unsigned int offset_size,
8fc3fc34 24261 htab_t include_hash)
cf2c3c16 24262{
804d2729
TT
24263 struct dwarf2_per_objfile *dwarf2_per_objfile
24264 = cu->per_cu->dwarf2_per_objfile;
4d663531 24265 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24266 enum dwarf_macro_record_type macinfo_type;
24267 int at_commandline;
d521ce57 24268 const gdb_byte *opcode_definitions[256];
757a13d0 24269
cf2c3c16
TT
24270 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24271 &offset_size, section_is_gnu);
24272 if (mac_ptr == NULL)
24273 {
24274 /* We already issued a complaint. */
24275 return;
24276 }
757a13d0
JK
24277
24278 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24279 GDB is still reading the definitions from command line. First
24280 DW_MACINFO_start_file will need to be ignored as it was already executed
24281 to create CURRENT_FILE for the main source holding also the command line
24282 definitions. On first met DW_MACINFO_start_file this flag is reset to
24283 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24284
24285 at_commandline = 1;
24286
24287 do
24288 {
24289 /* Do we at least have room for a macinfo type byte? */
24290 if (mac_ptr >= mac_end)
24291 {
f664829e 24292 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24293 break;
24294 }
24295
aead7601 24296 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24297 mac_ptr++;
24298
cf2c3c16
TT
24299 /* Note that we rely on the fact that the corresponding GNU and
24300 DWARF constants are the same. */
132448f8
SM
24301 DIAGNOSTIC_PUSH
24302 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24303 switch (macinfo_type)
24304 {
24305 /* A zero macinfo type indicates the end of the macro
24306 information. */
24307 case 0:
24308 break;
2e276125 24309
0af92d60
JK
24310 case DW_MACRO_define:
24311 case DW_MACRO_undef:
24312 case DW_MACRO_define_strp:
24313 case DW_MACRO_undef_strp:
24314 case DW_MACRO_define_sup:
24315 case DW_MACRO_undef_sup:
2e276125 24316 {
891d2f0b 24317 unsigned int bytes_read;
2e276125 24318 int line;
d521ce57 24319 const char *body;
cf2c3c16 24320 int is_define;
2e276125 24321
cf2c3c16
TT
24322 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24323 mac_ptr += bytes_read;
24324
0af92d60
JK
24325 if (macinfo_type == DW_MACRO_define
24326 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24327 {
24328 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24329 mac_ptr += bytes_read;
24330 }
24331 else
24332 {
24333 LONGEST str_offset;
24334
24335 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24336 mac_ptr += offset_size;
2e276125 24337
0af92d60
JK
24338 if (macinfo_type == DW_MACRO_define_sup
24339 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24340 || section_is_dwz)
36586728 24341 {
ed2dc618
SM
24342 struct dwz_file *dwz
24343 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24344
ed2dc618
SM
24345 body = read_indirect_string_from_dwz (objfile,
24346 dwz, str_offset);
36586728
TT
24347 }
24348 else
ed2dc618
SM
24349 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24350 abfd, str_offset);
cf2c3c16
TT
24351 }
24352
0af92d60
JK
24353 is_define = (macinfo_type == DW_MACRO_define
24354 || macinfo_type == DW_MACRO_define_strp
24355 || macinfo_type == DW_MACRO_define_sup);
2e276125 24356 if (! current_file)
757a13d0
JK
24357 {
24358 /* DWARF violation as no main source is present. */
b98664d3 24359 complaint (_("debug info with no main source gives macro %s "
757a13d0 24360 "on line %d: %s"),
cf2c3c16
TT
24361 is_define ? _("definition") : _("undefinition"),
24362 line, body);
757a13d0
JK
24363 break;
24364 }
3e43a32a
MS
24365 if ((line == 0 && !at_commandline)
24366 || (line != 0 && at_commandline))
b98664d3 24367 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24368 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24369 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24370 line == 0 ? _("zero") : _("non-zero"), line, body);
24371
955b06fa 24372 if (body == NULL)
7bede828 24373 {
955b06fa
SDJ
24374 /* Fedora's rpm-build's "debugedit" binary
24375 corrupted .debug_macro sections.
24376
24377 For more info, see
24378 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24379 complaint (_("debug info gives %s invalid macro %s "
24380 "without body (corrupted?) at line %d "
24381 "on file %s"),
24382 at_commandline ? _("command-line") : _("in-file"),
24383 is_define ? _("definition") : _("undefinition"),
24384 line, current_file->filename);
7bede828 24385 }
955b06fa
SDJ
24386 else if (is_define)
24387 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24388 else
24389 {
0af92d60
JK
24390 gdb_assert (macinfo_type == DW_MACRO_undef
24391 || macinfo_type == DW_MACRO_undef_strp
24392 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24393 macro_undef (current_file, line, body);
24394 }
2e276125
JB
24395 }
24396 break;
24397
0af92d60 24398 case DW_MACRO_start_file:
2e276125 24399 {
891d2f0b 24400 unsigned int bytes_read;
2e276125
JB
24401 int line, file;
24402
24403 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24404 mac_ptr += bytes_read;
24405 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24406 mac_ptr += bytes_read;
24407
3e43a32a
MS
24408 if ((line == 0 && !at_commandline)
24409 || (line != 0 && at_commandline))
b98664d3 24410 complaint (_("debug info gives source %d included "
757a13d0
JK
24411 "from %s at %s line %d"),
24412 file, at_commandline ? _("command-line") : _("file"),
24413 line == 0 ? _("zero") : _("non-zero"), line);
24414
24415 if (at_commandline)
24416 {
0af92d60 24417 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24418 pass one. */
757a13d0
JK
24419 at_commandline = 0;
24420 }
24421 else
804d2729
TT
24422 current_file = macro_start_file (cu, file, line, current_file,
24423 lh);
2e276125
JB
24424 }
24425 break;
24426
0af92d60 24427 case DW_MACRO_end_file:
2e276125 24428 if (! current_file)
b98664d3 24429 complaint (_("macro debug info has an unmatched "
3e43a32a 24430 "`close_file' directive"));
2e276125
JB
24431 else
24432 {
24433 current_file = current_file->included_by;
24434 if (! current_file)
24435 {
cf2c3c16 24436 enum dwarf_macro_record_type next_type;
2e276125
JB
24437
24438 /* GCC circa March 2002 doesn't produce the zero
24439 type byte marking the end of the compilation
24440 unit. Complain if it's not there, but exit no
24441 matter what. */
24442
24443 /* Do we at least have room for a macinfo type byte? */
24444 if (mac_ptr >= mac_end)
24445 {
f664829e 24446 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24447 return;
24448 }
24449
24450 /* We don't increment mac_ptr here, so this is just
24451 a look-ahead. */
aead7601
SM
24452 next_type
24453 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24454 mac_ptr);
2e276125 24455 if (next_type != 0)
b98664d3 24456 complaint (_("no terminating 0-type entry for "
3e43a32a 24457 "macros in `.debug_macinfo' section"));
2e276125
JB
24458
24459 return;
24460 }
24461 }
24462 break;
24463
0af92d60
JK
24464 case DW_MACRO_import:
24465 case DW_MACRO_import_sup:
cf2c3c16
TT
24466 {
24467 LONGEST offset;
8fc3fc34 24468 void **slot;
a036ba48
TT
24469 bfd *include_bfd = abfd;
24470 struct dwarf2_section_info *include_section = section;
d521ce57 24471 const gdb_byte *include_mac_end = mac_end;
a036ba48 24472 int is_dwz = section_is_dwz;
d521ce57 24473 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24474
24475 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24476 mac_ptr += offset_size;
24477
0af92d60 24478 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24479 {
ed2dc618 24480 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24481
96b79293 24482 dwz->macro.read (objfile);
a036ba48 24483
a036ba48 24484 include_section = &dwz->macro;
96b79293 24485 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
24486 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24487 is_dwz = 1;
24488 }
24489
24490 new_mac_ptr = include_section->buffer + offset;
24491 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24492
8fc3fc34
TT
24493 if (*slot != NULL)
24494 {
24495 /* This has actually happened; see
24496 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24497 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24498 ".debug_macro section"));
24499 }
24500 else
24501 {
d521ce57 24502 *slot = (void *) new_mac_ptr;
36586728 24503
804d2729 24504 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24505 include_mac_end, current_file, lh,
36586728 24506 section, section_is_gnu, is_dwz,
4d663531 24507 offset_size, include_hash);
8fc3fc34 24508
d521ce57 24509 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24510 }
cf2c3c16
TT
24511 }
24512 break;
24513
2e276125 24514 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24515 if (!section_is_gnu)
24516 {
24517 unsigned int bytes_read;
2e276125 24518
ac298888
TT
24519 /* This reads the constant, but since we don't recognize
24520 any vendor extensions, we ignore it. */
24521 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24522 mac_ptr += bytes_read;
24523 read_direct_string (abfd, mac_ptr, &bytes_read);
24524 mac_ptr += bytes_read;
2e276125 24525
cf2c3c16
TT
24526 /* We don't recognize any vendor extensions. */
24527 break;
24528 }
24529 /* FALLTHROUGH */
24530
24531 default:
24532 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24533 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24534 section);
24535 if (mac_ptr == NULL)
24536 return;
24537 break;
2e276125 24538 }
132448f8 24539 DIAGNOSTIC_POP
757a13d0 24540 } while (macinfo_type != 0);
2e276125 24541}
8e19ed76 24542
cf2c3c16 24543static void
09262596 24544dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24545 int section_is_gnu)
cf2c3c16 24546{
518817b3
SM
24547 struct dwarf2_per_objfile *dwarf2_per_objfile
24548 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24549 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24550 struct line_header *lh = cu->line_header;
24551 bfd *abfd;
d521ce57 24552 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24553 struct macro_source_file *current_file = 0;
24554 enum dwarf_macro_record_type macinfo_type;
24555 unsigned int offset_size = cu->header.offset_size;
d521ce57 24556 const gdb_byte *opcode_definitions[256];
8fc3fc34 24557 void **slot;
09262596
DE
24558 struct dwarf2_section_info *section;
24559 const char *section_name;
24560
24561 if (cu->dwo_unit != NULL)
24562 {
24563 if (section_is_gnu)
24564 {
24565 section = &cu->dwo_unit->dwo_file->sections.macro;
24566 section_name = ".debug_macro.dwo";
24567 }
24568 else
24569 {
24570 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24571 section_name = ".debug_macinfo.dwo";
24572 }
24573 }
24574 else
24575 {
24576 if (section_is_gnu)
24577 {
24578 section = &dwarf2_per_objfile->macro;
24579 section_name = ".debug_macro";
24580 }
24581 else
24582 {
24583 section = &dwarf2_per_objfile->macinfo;
24584 section_name = ".debug_macinfo";
24585 }
24586 }
cf2c3c16 24587
96b79293 24588 section->read (objfile);
cf2c3c16
TT
24589 if (section->buffer == NULL)
24590 {
b98664d3 24591 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24592 return;
24593 }
96b79293 24594 abfd = section->get_bfd_owner ();
cf2c3c16
TT
24595
24596 /* First pass: Find the name of the base filename.
24597 This filename is needed in order to process all macros whose definition
24598 (or undefinition) comes from the command line. These macros are defined
24599 before the first DW_MACINFO_start_file entry, and yet still need to be
24600 associated to the base file.
24601
24602 To determine the base file name, we scan the macro definitions until we
24603 reach the first DW_MACINFO_start_file entry. We then initialize
24604 CURRENT_FILE accordingly so that any macro definition found before the
24605 first DW_MACINFO_start_file can still be associated to the base file. */
24606
24607 mac_ptr = section->buffer + offset;
24608 mac_end = section->buffer + section->size;
24609
24610 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24611 &offset_size, section_is_gnu);
24612 if (mac_ptr == NULL)
24613 {
24614 /* We already issued a complaint. */
24615 return;
24616 }
24617
24618 do
24619 {
24620 /* Do we at least have room for a macinfo type byte? */
24621 if (mac_ptr >= mac_end)
24622 {
24623 /* Complaint is printed during the second pass as GDB will probably
24624 stop the first pass earlier upon finding
24625 DW_MACINFO_start_file. */
24626 break;
24627 }
24628
aead7601 24629 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24630 mac_ptr++;
24631
24632 /* Note that we rely on the fact that the corresponding GNU and
24633 DWARF constants are the same. */
132448f8
SM
24634 DIAGNOSTIC_PUSH
24635 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24636 switch (macinfo_type)
24637 {
24638 /* A zero macinfo type indicates the end of the macro
24639 information. */
24640 case 0:
24641 break;
24642
0af92d60
JK
24643 case DW_MACRO_define:
24644 case DW_MACRO_undef:
cf2c3c16
TT
24645 /* Only skip the data by MAC_PTR. */
24646 {
24647 unsigned int bytes_read;
24648
24649 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24650 mac_ptr += bytes_read;
24651 read_direct_string (abfd, mac_ptr, &bytes_read);
24652 mac_ptr += bytes_read;
24653 }
24654 break;
24655
0af92d60 24656 case DW_MACRO_start_file:
cf2c3c16
TT
24657 {
24658 unsigned int bytes_read;
24659 int line, file;
24660
24661 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24662 mac_ptr += bytes_read;
24663 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24664 mac_ptr += bytes_read;
24665
804d2729 24666 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
24667 }
24668 break;
24669
0af92d60 24670 case DW_MACRO_end_file:
cf2c3c16
TT
24671 /* No data to skip by MAC_PTR. */
24672 break;
24673
0af92d60
JK
24674 case DW_MACRO_define_strp:
24675 case DW_MACRO_undef_strp:
24676 case DW_MACRO_define_sup:
24677 case DW_MACRO_undef_sup:
cf2c3c16
TT
24678 {
24679 unsigned int bytes_read;
24680
24681 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24682 mac_ptr += bytes_read;
24683 mac_ptr += offset_size;
24684 }
24685 break;
24686
0af92d60
JK
24687 case DW_MACRO_import:
24688 case DW_MACRO_import_sup:
cf2c3c16 24689 /* Note that, according to the spec, a transparent include
0af92d60 24690 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24691 skip this opcode. */
24692 mac_ptr += offset_size;
24693 break;
24694
24695 case DW_MACINFO_vendor_ext:
24696 /* Only skip the data by MAC_PTR. */
24697 if (!section_is_gnu)
24698 {
24699 unsigned int bytes_read;
24700
24701 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24702 mac_ptr += bytes_read;
24703 read_direct_string (abfd, mac_ptr, &bytes_read);
24704 mac_ptr += bytes_read;
24705 }
24706 /* FALLTHROUGH */
24707
24708 default:
24709 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24710 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24711 section);
24712 if (mac_ptr == NULL)
24713 return;
24714 break;
24715 }
132448f8 24716 DIAGNOSTIC_POP
cf2c3c16
TT
24717 } while (macinfo_type != 0 && current_file == NULL);
24718
24719 /* Second pass: Process all entries.
24720
24721 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24722 command-line macro definitions/undefinitions. This flag is unset when we
24723 reach the first DW_MACINFO_start_file entry. */
24724
fc4007c9
TT
24725 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24726 htab_eq_pointer,
24727 NULL, xcalloc, xfree));
8fc3fc34 24728 mac_ptr = section->buffer + offset;
fc4007c9 24729 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24730 *slot = (void *) mac_ptr;
804d2729 24731 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 24732 current_file, lh, section,
fc4007c9
TT
24733 section_is_gnu, 0, offset_size,
24734 include_hash.get ());
cf2c3c16
TT
24735}
24736
3019eac3
DE
24737/* Return the .debug_loc section to use for CU.
24738 For DWO files use .debug_loc.dwo. */
24739
24740static struct dwarf2_section_info *
24741cu_debug_loc_section (struct dwarf2_cu *cu)
24742{
518817b3
SM
24743 struct dwarf2_per_objfile *dwarf2_per_objfile
24744 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24745
3019eac3 24746 if (cu->dwo_unit)
43988095
JK
24747 {
24748 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24749
43988095
JK
24750 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24751 }
24752 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24753 : &dwarf2_per_objfile->loc);
3019eac3
DE
24754}
24755
8cf6f0b1
TT
24756/* A helper function that fills in a dwarf2_loclist_baton. */
24757
24758static void
24759fill_in_loclist_baton (struct dwarf2_cu *cu,
24760 struct dwarf2_loclist_baton *baton,
ff39bb5e 24761 const struct attribute *attr)
8cf6f0b1 24762{
518817b3
SM
24763 struct dwarf2_per_objfile *dwarf2_per_objfile
24764 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24765 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24766
96b79293 24767 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
24768
24769 baton->per_cu = cu->per_cu;
24770 gdb_assert (baton->per_cu);
24771 /* We don't know how long the location list is, but make sure we
24772 don't run off the edge of the section. */
3019eac3
DE
24773 baton->size = section->size - DW_UNSND (attr);
24774 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24775 baton->base_address = cu->base_address;
f664829e 24776 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24777}
24778
4c2df51b 24779static void
ff39bb5e 24780dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24781 struct dwarf2_cu *cu, int is_block)
4c2df51b 24782{
518817b3
SM
24783 struct dwarf2_per_objfile *dwarf2_per_objfile
24784 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24785 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24786 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24787
cd6c91b4 24788 if (attr->form_is_section_offset ()
3019eac3 24789 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24790 the section. If so, fall through to the complaint in the
24791 other branch. */
3019eac3 24792 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24793 {
0d53c4c4 24794 struct dwarf2_loclist_baton *baton;
4c2df51b 24795
8d749320 24796 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24797
8cf6f0b1 24798 fill_in_loclist_baton (cu, baton, attr);
be391dca 24799
d00adf39 24800 if (cu->base_known == 0)
b98664d3 24801 complaint (_("Location list used without "
3e43a32a 24802 "specifying the CU base address."));
4c2df51b 24803
f1e6e072
TT
24804 SYMBOL_ACLASS_INDEX (sym) = (is_block
24805 ? dwarf2_loclist_block_index
24806 : dwarf2_loclist_index);
0d53c4c4
DJ
24807 SYMBOL_LOCATION_BATON (sym) = baton;
24808 }
24809 else
24810 {
24811 struct dwarf2_locexpr_baton *baton;
24812
8d749320 24813 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24814 baton->per_cu = cu->per_cu;
24815 gdb_assert (baton->per_cu);
0d53c4c4 24816
4fc6c0d5 24817 if (attr->form_is_block ())
0d53c4c4
DJ
24818 {
24819 /* Note that we're just copying the block's data pointer
24820 here, not the actual data. We're still pointing into the
6502dd73
DJ
24821 info_buffer for SYM's objfile; right now we never release
24822 that buffer, but when we do clean up properly this may
24823 need to change. */
0d53c4c4
DJ
24824 baton->size = DW_BLOCK (attr)->size;
24825 baton->data = DW_BLOCK (attr)->data;
24826 }
24827 else
24828 {
24829 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24830 sym->natural_name ());
0d53c4c4 24831 baton->size = 0;
0d53c4c4 24832 }
6e70227d 24833
f1e6e072
TT
24834 SYMBOL_ACLASS_INDEX (sym) = (is_block
24835 ? dwarf2_locexpr_block_index
24836 : dwarf2_locexpr_index);
0d53c4c4
DJ
24837 SYMBOL_LOCATION_BATON (sym) = baton;
24838 }
4c2df51b 24839}
6502dd73 24840
9aa1f1e3
TT
24841/* Return the OBJFILE associated with the compilation unit CU. If CU
24842 came from a separate debuginfo file, then the master objfile is
24843 returned. */
ae0d2f24
UW
24844
24845struct objfile *
24846dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24847{
e3b94546 24848 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24849
24850 /* Return the master objfile, so that we can report and look up the
24851 correct file containing this variable. */
24852 if (objfile->separate_debug_objfile_backlink)
24853 objfile = objfile->separate_debug_objfile_backlink;
24854
24855 return objfile;
24856}
24857
96408a79
SA
24858/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24859 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24860 CU_HEADERP first. */
24861
24862static const struct comp_unit_head *
24863per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24864 struct dwarf2_per_cu_data *per_cu)
24865{
d521ce57 24866 const gdb_byte *info_ptr;
96408a79
SA
24867
24868 if (per_cu->cu)
24869 return &per_cu->cu->header;
24870
9c541725 24871 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24872
24873 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24874 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24875 rcuh_kind::COMPILE);
96408a79
SA
24876
24877 return cu_headerp;
24878}
24879
ae0d2f24
UW
24880/* Return the address size given in the compilation unit header for CU. */
24881
98714339 24882int
ae0d2f24
UW
24883dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24884{
96408a79
SA
24885 struct comp_unit_head cu_header_local;
24886 const struct comp_unit_head *cu_headerp;
c471e790 24887
96408a79
SA
24888 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24889
24890 return cu_headerp->addr_size;
ae0d2f24
UW
24891}
24892
9eae7c52
TT
24893/* Return the offset size given in the compilation unit header for CU. */
24894
24895int
24896dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24897{
96408a79
SA
24898 struct comp_unit_head cu_header_local;
24899 const struct comp_unit_head *cu_headerp;
9c6c53f7 24900
96408a79
SA
24901 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24902
24903 return cu_headerp->offset_size;
24904}
24905
24906/* See its dwarf2loc.h declaration. */
24907
24908int
24909dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24910{
24911 struct comp_unit_head cu_header_local;
24912 const struct comp_unit_head *cu_headerp;
24913
24914 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24915
24916 if (cu_headerp->version == 2)
24917 return cu_headerp->addr_size;
24918 else
24919 return cu_headerp->offset_size;
181cebd4
JK
24920}
24921
9aa1f1e3
TT
24922/* Return the text offset of the CU. The returned offset comes from
24923 this CU's objfile. If this objfile came from a separate debuginfo
24924 file, then the offset may be different from the corresponding
24925 offset in the parent objfile. */
24926
24927CORE_ADDR
24928dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24929{
b3b3bada 24930 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
9aa1f1e3
TT
24931}
24932
9a49df9d
AB
24933/* Return a type that is a generic pointer type, the size of which matches
24934 the address size given in the compilation unit header for PER_CU. */
24935static struct type *
24936dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
24937{
24938 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24939 struct type *void_type = objfile_type (objfile)->builtin_void;
24940 struct type *addr_type = lookup_pointer_type (void_type);
24941 int addr_size = dwarf2_per_cu_addr_size (per_cu);
24942
24943 if (TYPE_LENGTH (addr_type) == addr_size)
24944 return addr_type;
24945
24946 addr_type
24947 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
24948 return addr_type;
24949}
24950
43988095
JK
24951/* Return DWARF version number of PER_CU. */
24952
24953short
24954dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24955{
24956 return per_cu->dwarf_version;
24957}
24958
348e048f
DE
24959/* Locate the .debug_info compilation unit from CU's objfile which contains
24960 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24961
24962static struct dwarf2_per_cu_data *
9c541725 24963dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24964 unsigned int offset_in_dwz,
ed2dc618 24965 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24966{
24967 struct dwarf2_per_cu_data *this_cu;
24968 int low, high;
24969
ae038cb0 24970 low = 0;
b76e467d 24971 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24972 while (high > low)
24973 {
36586728 24974 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24975 int mid = low + (high - low) / 2;
9a619af0 24976
36586728 24977 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 24978 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24979 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 24980 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
24981 high = mid;
24982 else
24983 low = mid + 1;
24984 }
24985 gdb_assert (low == high);
36586728 24986 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 24987 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24988 {
36586728 24989 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24990 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24991 "offset %s [in module %s]"),
24992 sect_offset_str (sect_off),
ed2dc618 24993 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24994
9c541725
PA
24995 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24996 <= sect_off);
ae038cb0
DJ
24997 return dwarf2_per_objfile->all_comp_units[low-1];
24998 }
24999 else
25000 {
b76e467d 25001 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25002 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25003 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25004 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25005 return this_cu;
25006 }
25007}
25008
23745b47 25009/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25010
fcd3b13d
SM
25011dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25012 : per_cu (per_cu_),
9068261f
AB
25013 mark (false),
25014 has_loclist (false),
25015 checked_producer (false),
25016 producer_is_gxx_lt_4_6 (false),
25017 producer_is_gcc_lt_4_3 (false),
eb77c9df 25018 producer_is_icc (false),
9068261f 25019 producer_is_icc_lt_14 (false),
c258c396 25020 producer_is_codewarrior (false),
9068261f 25021 processing_has_namespace_info (false)
93311388 25022{
fcd3b13d
SM
25023 per_cu->cu = this;
25024}
25025
25026/* Destroy a dwarf2_cu. */
25027
25028dwarf2_cu::~dwarf2_cu ()
25029{
25030 per_cu->cu = NULL;
9816fde3
JK
25031}
25032
25033/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25034
25035static void
95554aad
TT
25036prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25037 enum language pretend_language)
9816fde3
JK
25038{
25039 struct attribute *attr;
25040
25041 /* Set the language we're debugging. */
25042 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25043 if (attr != nullptr)
9816fde3
JK
25044 set_cu_language (DW_UNSND (attr), cu);
25045 else
9cded63f 25046 {
95554aad 25047 cu->language = pretend_language;
9cded63f
TT
25048 cu->language_defn = language_def (cu->language);
25049 }
dee91e82 25050
7d45c7c3 25051 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25052}
25053
ae038cb0
DJ
25054/* Increase the age counter on each cached compilation unit, and free
25055 any that are too old. */
25056
25057static void
ed2dc618 25058age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25059{
25060 struct dwarf2_per_cu_data *per_cu, **last_chain;
25061
25062 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25063 per_cu = dwarf2_per_objfile->read_in_chain;
25064 while (per_cu != NULL)
25065 {
25066 per_cu->cu->last_used ++;
b4f54984 25067 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25068 dwarf2_mark (per_cu->cu);
25069 per_cu = per_cu->cu->read_in_chain;
25070 }
25071
25072 per_cu = dwarf2_per_objfile->read_in_chain;
25073 last_chain = &dwarf2_per_objfile->read_in_chain;
25074 while (per_cu != NULL)
25075 {
25076 struct dwarf2_per_cu_data *next_cu;
25077
25078 next_cu = per_cu->cu->read_in_chain;
25079
25080 if (!per_cu->cu->mark)
25081 {
fcd3b13d 25082 delete per_cu->cu;
ae038cb0
DJ
25083 *last_chain = next_cu;
25084 }
25085 else
25086 last_chain = &per_cu->cu->read_in_chain;
25087
25088 per_cu = next_cu;
25089 }
25090}
25091
25092/* Remove a single compilation unit from the cache. */
25093
25094static void
dee91e82 25095free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25096{
25097 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25098 struct dwarf2_per_objfile *dwarf2_per_objfile
25099 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25100
25101 per_cu = dwarf2_per_objfile->read_in_chain;
25102 last_chain = &dwarf2_per_objfile->read_in_chain;
25103 while (per_cu != NULL)
25104 {
25105 struct dwarf2_per_cu_data *next_cu;
25106
25107 next_cu = per_cu->cu->read_in_chain;
25108
dee91e82 25109 if (per_cu == target_per_cu)
ae038cb0 25110 {
fcd3b13d 25111 delete per_cu->cu;
dee91e82 25112 per_cu->cu = NULL;
ae038cb0
DJ
25113 *last_chain = next_cu;
25114 break;
25115 }
25116 else
25117 last_chain = &per_cu->cu->read_in_chain;
25118
25119 per_cu = next_cu;
25120 }
25121}
25122
dee91e82
DE
25123/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25124 We store these in a hash table separate from the DIEs, and preserve them
25125 when the DIEs are flushed out of cache.
25126
25127 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25128 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25129 or the type may come from a DWO file. Furthermore, while it's more logical
25130 to use per_cu->section+offset, with Fission the section with the data is in
25131 the DWO file but we don't know that section at the point we need it.
25132 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25133 because we can enter the lookup routine, get_die_type_at_offset, from
25134 outside this file, and thus won't necessarily have PER_CU->cu.
25135 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25136
dee91e82 25137struct dwarf2_per_cu_offset_and_type
1c379e20 25138{
dee91e82 25139 const struct dwarf2_per_cu_data *per_cu;
9c541725 25140 sect_offset sect_off;
1c379e20
DJ
25141 struct type *type;
25142};
25143
dee91e82 25144/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25145
25146static hashval_t
dee91e82 25147per_cu_offset_and_type_hash (const void *item)
1c379e20 25148{
9a3c8263
SM
25149 const struct dwarf2_per_cu_offset_and_type *ofs
25150 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25151
9c541725 25152 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25153}
25154
dee91e82 25155/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25156
25157static int
dee91e82 25158per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25159{
9a3c8263
SM
25160 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25161 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25162 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25163 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25164
dee91e82 25165 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25166 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25167}
25168
25169/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25170 table if necessary. For convenience, return TYPE.
25171
25172 The DIEs reading must have careful ordering to:
85102364 25173 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25174 reading current DIE.
25175 * Not trying to dereference contents of still incompletely read in types
25176 while reading in other DIEs.
25177 * Enable referencing still incompletely read in types just by a pointer to
25178 the type without accessing its fields.
25179
25180 Therefore caller should follow these rules:
25181 * Try to fetch any prerequisite types we may need to build this DIE type
25182 before building the type and calling set_die_type.
e71ec853 25183 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25184 possible before fetching more types to complete the current type.
25185 * Make the type as complete as possible before fetching more types. */
1c379e20 25186
f792889a 25187static struct type *
1c379e20
DJ
25188set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25189{
518817b3
SM
25190 struct dwarf2_per_objfile *dwarf2_per_objfile
25191 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25192 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25193 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25194 struct attribute *attr;
25195 struct dynamic_prop prop;
1c379e20 25196
b4ba55a1
JB
25197 /* For Ada types, make sure that the gnat-specific data is always
25198 initialized (if not already set). There are a few types where
25199 we should not be doing so, because the type-specific area is
25200 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25201 where the type-specific area is used to store the floatformat).
25202 But this is not a problem, because the gnat-specific information
25203 is actually not needed for these types. */
25204 if (need_gnat_info (cu)
25205 && TYPE_CODE (type) != TYPE_CODE_FUNC
25206 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25207 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25208 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25209 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25210 && !HAVE_GNAT_AUX_INFO (type))
25211 INIT_GNAT_SPECIFIC (type);
25212
3f2f83dd
KB
25213 /* Read DW_AT_allocated and set in type. */
25214 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 25215 if (attr != NULL && attr->form_is_block ())
3f2f83dd 25216 {
9a49df9d
AB
25217 struct type *prop_type
25218 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25219 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25220 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25221 }
25222 else if (attr != NULL)
25223 {
b98664d3 25224 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25225 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25226 sect_offset_str (die->sect_off));
3f2f83dd
KB
25227 }
25228
25229 /* Read DW_AT_associated and set in type. */
25230 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 25231 if (attr != NULL && attr->form_is_block ())
3f2f83dd 25232 {
9a49df9d
AB
25233 struct type *prop_type
25234 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25235 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25236 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25237 }
25238 else if (attr != NULL)
25239 {
b98664d3 25240 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25241 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25242 sect_offset_str (die->sect_off));
3f2f83dd
KB
25243 }
25244
3cdcd0ce
JB
25245 /* Read DW_AT_data_location and set in type. */
25246 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25247 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25248 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25249 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25250
dee91e82 25251 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25252 {
dee91e82
DE
25253 dwarf2_per_objfile->die_type_hash =
25254 htab_create_alloc_ex (127,
25255 per_cu_offset_and_type_hash,
25256 per_cu_offset_and_type_eq,
25257 NULL,
25258 &objfile->objfile_obstack,
25259 hashtab_obstack_allocate,
25260 dummy_obstack_deallocate);
f792889a 25261 }
1c379e20 25262
dee91e82 25263 ofs.per_cu = cu->per_cu;
9c541725 25264 ofs.sect_off = die->sect_off;
1c379e20 25265 ofs.type = type;
dee91e82
DE
25266 slot = (struct dwarf2_per_cu_offset_and_type **)
25267 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25268 if (*slot)
b98664d3 25269 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25270 sect_offset_str (die->sect_off));
8d749320
SM
25271 *slot = XOBNEW (&objfile->objfile_obstack,
25272 struct dwarf2_per_cu_offset_and_type);
1c379e20 25273 **slot = ofs;
f792889a 25274 return type;
1c379e20
DJ
25275}
25276
9c541725 25277/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25278 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25279
25280static struct type *
9c541725 25281get_die_type_at_offset (sect_offset sect_off,
673bfd45 25282 struct dwarf2_per_cu_data *per_cu)
1c379e20 25283{
dee91e82 25284 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25286
dee91e82 25287 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25288 return NULL;
1c379e20 25289
dee91e82 25290 ofs.per_cu = per_cu;
9c541725 25291 ofs.sect_off = sect_off;
9a3c8263
SM
25292 slot = ((struct dwarf2_per_cu_offset_and_type *)
25293 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25294 if (slot)
25295 return slot->type;
25296 else
25297 return NULL;
25298}
25299
02142a6c 25300/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25301 or return NULL if DIE does not have a saved type. */
25302
25303static struct type *
25304get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25305{
9c541725 25306 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25307}
25308
10b3939b
DJ
25309/* Add a dependence relationship from CU to REF_PER_CU. */
25310
25311static void
25312dwarf2_add_dependence (struct dwarf2_cu *cu,
25313 struct dwarf2_per_cu_data *ref_per_cu)
25314{
25315 void **slot;
25316
25317 if (cu->dependencies == NULL)
25318 cu->dependencies
25319 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25320 NULL, &cu->comp_unit_obstack,
25321 hashtab_obstack_allocate,
25322 dummy_obstack_deallocate);
25323
25324 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25325 if (*slot == NULL)
25326 *slot = ref_per_cu;
25327}
1c379e20 25328
f504f079
DE
25329/* Subroutine of dwarf2_mark to pass to htab_traverse.
25330 Set the mark field in every compilation unit in the
ae038cb0
DJ
25331 cache that we must keep because we are keeping CU. */
25332
10b3939b
DJ
25333static int
25334dwarf2_mark_helper (void **slot, void *data)
25335{
25336 struct dwarf2_per_cu_data *per_cu;
25337
25338 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25339
25340 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25341 reading of the chain. As such dependencies remain valid it is not much
25342 useful to track and undo them during QUIT cleanups. */
25343 if (per_cu->cu == NULL)
25344 return 1;
25345
10b3939b
DJ
25346 if (per_cu->cu->mark)
25347 return 1;
9068261f 25348 per_cu->cu->mark = true;
10b3939b
DJ
25349
25350 if (per_cu->cu->dependencies != NULL)
25351 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25352
25353 return 1;
25354}
25355
f504f079
DE
25356/* Set the mark field in CU and in every other compilation unit in the
25357 cache that we must keep because we are keeping CU. */
25358
ae038cb0
DJ
25359static void
25360dwarf2_mark (struct dwarf2_cu *cu)
25361{
25362 if (cu->mark)
25363 return;
9068261f 25364 cu->mark = true;
10b3939b
DJ
25365 if (cu->dependencies != NULL)
25366 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25367}
25368
25369static void
25370dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25371{
25372 while (per_cu)
25373 {
9068261f 25374 per_cu->cu->mark = false;
ae038cb0
DJ
25375 per_cu = per_cu->cu->read_in_chain;
25376 }
72bf9492
DJ
25377}
25378
72bf9492
DJ
25379/* Trivial hash function for partial_die_info: the hash value of a DIE
25380 is its offset in .debug_info for this objfile. */
25381
25382static hashval_t
25383partial_die_hash (const void *item)
25384{
9a3c8263
SM
25385 const struct partial_die_info *part_die
25386 = (const struct partial_die_info *) item;
9a619af0 25387
9c541725 25388 return to_underlying (part_die->sect_off);
72bf9492
DJ
25389}
25390
25391/* Trivial comparison function for partial_die_info structures: two DIEs
25392 are equal if they have the same offset. */
25393
25394static int
25395partial_die_eq (const void *item_lhs, const void *item_rhs)
25396{
9a3c8263
SM
25397 const struct partial_die_info *part_die_lhs
25398 = (const struct partial_die_info *) item_lhs;
25399 const struct partial_die_info *part_die_rhs
25400 = (const struct partial_die_info *) item_rhs;
9a619af0 25401
9c541725 25402 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25403}
25404
3c3bb058
AB
25405struct cmd_list_element *set_dwarf_cmdlist;
25406struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25407
25408static void
981a3fb3 25409set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25410{
b4f54984 25411 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25412 gdb_stdout);
ae038cb0
DJ
25413}
25414
25415static void
981a3fb3 25416show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25417{
b4f54984 25418 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25419}
25420
491144b5 25421bool dwarf_always_disassemble;
437afbb8 25422
437afbb8 25423static void
cd4fb1b2
SM
25424show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25425 struct cmd_list_element *c, const char *value)
9291a0cd 25426{
cd4fb1b2
SM
25427 fprintf_filtered (file,
25428 _("Whether to always disassemble "
25429 "DWARF expressions is %s.\n"),
25430 value);
9291a0cd
TT
25431}
25432
9291a0cd 25433static void
cd4fb1b2
SM
25434show_check_physname (struct ui_file *file, int from_tty,
25435 struct cmd_list_element *c, const char *value)
9291a0cd 25436{
cd4fb1b2
SM
25437 fprintf_filtered (file,
25438 _("Whether to check \"physname\" is %s.\n"),
25439 value);
9291a0cd
TT
25440}
25441
6c265988 25442void _initialize_dwarf2_read ();
cd4fb1b2 25443void
6c265988 25444_initialize_dwarf2_read ()
9291a0cd 25445{
cd4fb1b2
SM
25446 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25447Set DWARF specific variables.\n\
590042fc 25448Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25449 &set_dwarf_cmdlist, "maintenance set dwarf ",
25450 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25451
cd4fb1b2 25452 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25453Show DWARF specific variables.\n\
25454Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25455 &show_dwarf_cmdlist, "maintenance show dwarf ",
25456 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25457
cd4fb1b2
SM
25458 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25459 &dwarf_max_cache_age, _("\
25460Set the upper bound on the age of cached DWARF compilation units."), _("\
25461Show the upper bound on the age of cached DWARF compilation units."), _("\
25462A higher limit means that cached compilation units will be stored\n\
25463in memory longer, and more total memory will be used. Zero disables\n\
25464caching, which can slow down startup."),
25465 NULL,
25466 show_dwarf_max_cache_age,
25467 &set_dwarf_cmdlist,
25468 &show_dwarf_cmdlist);
156942c7 25469
cd4fb1b2
SM
25470 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25471 &dwarf_always_disassemble, _("\
25472Set whether `info address' always disassembles DWARF expressions."), _("\
25473Show whether `info address' always disassembles DWARF expressions."), _("\
25474When enabled, DWARF expressions are always printed in an assembly-like\n\
25475syntax. When disabled, expressions will be printed in a more\n\
25476conversational style, when possible."),
25477 NULL,
25478 show_dwarf_always_disassemble,
25479 &set_dwarf_cmdlist,
25480 &show_dwarf_cmdlist);
9291a0cd 25481
cd4fb1b2
SM
25482 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25483Set debugging of the DWARF reader."), _("\
25484Show debugging of the DWARF reader."), _("\
25485When enabled (non-zero), debugging messages are printed during DWARF\n\
25486reading and symtab expansion. A value of 1 (one) provides basic\n\
25487information. A value greater than 1 provides more verbose information."),
25488 NULL,
25489 NULL,
25490 &setdebuglist, &showdebuglist);
9291a0cd 25491
cd4fb1b2
SM
25492 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25493Set debugging of the DWARF DIE reader."), _("\
25494Show debugging of the DWARF DIE reader."), _("\
25495When enabled (non-zero), DIEs are dumped after they are read in.\n\
25496The value is the maximum depth to print."),
25497 NULL,
25498 NULL,
25499 &setdebuglist, &showdebuglist);
9291a0cd 25500
cd4fb1b2
SM
25501 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25502Set debugging of the dwarf line reader."), _("\
25503Show debugging of the dwarf line reader."), _("\
25504When enabled (non-zero), line number entries are dumped as they are read in.\n\
25505A value of 1 (one) provides basic information.\n\
25506A value greater than 1 provides more verbose information."),
25507 NULL,
25508 NULL,
25509 &setdebuglist, &showdebuglist);
437afbb8 25510
cd4fb1b2
SM
25511 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25512Set cross-checking of \"physname\" code against demangler."), _("\
25513Show cross-checking of \"physname\" code against demangler."), _("\
25514When enabled, GDB's internal \"physname\" code is checked against\n\
25515the demangler."),
25516 NULL, show_check_physname,
25517 &setdebuglist, &showdebuglist);
900e11f9 25518
e615022a
DE
25519 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25520 no_class, &use_deprecated_index_sections, _("\
25521Set whether to use deprecated gdb_index sections."), _("\
25522Show whether to use deprecated gdb_index sections."), _("\
25523When enabled, deprecated .gdb_index sections are used anyway.\n\
25524Normally they are ignored either because of a missing feature or\n\
25525performance issue.\n\
25526Warning: This option must be enabled before gdb reads the file."),
25527 NULL,
25528 NULL,
25529 &setlist, &showlist);
25530
f1e6e072
TT
25531 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25532 &dwarf2_locexpr_funcs);
25533 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25534 &dwarf2_loclist_funcs);
25535
25536 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25537 &dwarf2_block_frame_base_locexpr_funcs);
25538 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25539 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25540
25541#if GDB_SELF_TEST
25542 selftests::register_test ("dw2_expand_symtabs_matching",
25543 selftests::dw2_expand_symtabs_matching::run_test);
25544#endif
6502dd73 25545}
This page took 5.622356 seconds and 4 git commands to generate.