Change two functions to be methods on struct attribute
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
3f563c84
PA
117/* An index into a (C++) symbol name component in a symbol name as
118 recorded in the mapped_index's symbol table. For each C++ symbol
119 in the symbol table, we record one entry for the start of each
120 component in the symbol in a table of name components, and then
121 sort the table, in order to be able to binary search symbol names,
122 ignoring leading namespaces, both completion and regular look up.
123 For example, for symbol "A::B::C", we'll have an entry that points
124 to "A::B::C", another that points to "B::C", and another for "C".
125 Note that function symbols in GDB index have no parameter
126 information, just the function/method names. You can convert a
127 name_component to a "const char *" using the
128 'mapped_index::symbol_name_at(offset_type)' method. */
129
130struct name_component
131{
132 /* Offset in the symbol name where the component starts. Stored as
133 a (32-bit) offset instead of a pointer to save memory and improve
134 locality on 64-bit architectures. */
135 offset_type name_offset;
136
137 /* The symbol's index in the symbol and constant pool tables of a
138 mapped_index. */
139 offset_type idx;
140};
141
44ed8f3e
PA
142/* Base class containing bits shared by both .gdb_index and
143 .debug_name indexes. */
144
145struct mapped_index_base
146{
22ca247e
TT
147 mapped_index_base () = default;
148 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
149
44ed8f3e
PA
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
3b00ef10
TT
179 find_name_components_bounds (const lookup_name_info &ln_no_params,
180 enum language lang) const;
44ed8f3e
PA
181
182 /* Prevent deleting/destroying via a base class pointer. */
183protected:
184 ~mapped_index_base() = default;
185};
186
9291a0cd
TT
187/* A description of the mapped index. The file format is described in
188 a comment by the code that writes the index. */
fc898b42 189struct mapped_index final : public mapped_index_base
9291a0cd 190{
f00a2de2
PA
191 /* A slot/bucket in the symbol table hash. */
192 struct symbol_table_slot
193 {
194 const offset_type name;
195 const offset_type vec;
196 };
197
559a7a62 198 /* Index data format version. */
3063847f 199 int version = 0;
559a7a62 200
f00a2de2
PA
201 /* The address table data. */
202 gdb::array_view<const gdb_byte> address_table;
b11b1f88 203
3876f04e 204 /* The symbol table, implemented as a hash table. */
f00a2de2 205 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 206
9291a0cd 207 /* A pointer to the constant pool. */
3063847f 208 const char *constant_pool = nullptr;
3f563c84 209
44ed8f3e
PA
210 bool symbol_name_slot_invalid (offset_type idx) const override
211 {
212 const auto &bucket = this->symbol_table[idx];
9ab08412 213 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 214 }
5c58de74 215
3f563c84
PA
216 /* Convenience method to get at the name of the symbol at IDX in the
217 symbol table. */
44ed8f3e 218 const char *symbol_name_at (offset_type idx) const override
f00a2de2 219 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 220
44ed8f3e
PA
221 size_t symbol_name_count () const override
222 { return this->symbol_table.size (); }
9291a0cd
TT
223};
224
927aa2e7
JK
225/* A description of the mapped .debug_names.
226 Uninitialized map has CU_COUNT 0. */
fc898b42 227struct mapped_debug_names final : public mapped_index_base
927aa2e7 228{
ed2dc618
SM
229 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
230 : dwarf2_per_objfile (dwarf2_per_objfile_)
231 {}
232
233 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
234 bfd_endian dwarf5_byte_order;
235 bool dwarf5_is_dwarf64;
236 bool augmentation_is_gdb;
237 uint8_t offset_size;
238 uint32_t cu_count = 0;
239 uint32_t tu_count, bucket_count, name_count;
240 const gdb_byte *cu_table_reordered, *tu_table_reordered;
241 const uint32_t *bucket_table_reordered, *hash_table_reordered;
242 const gdb_byte *name_table_string_offs_reordered;
243 const gdb_byte *name_table_entry_offs_reordered;
244 const gdb_byte *entry_pool;
245
246 struct index_val
247 {
248 ULONGEST dwarf_tag;
249 struct attr
250 {
251 /* Attribute name DW_IDX_*. */
252 ULONGEST dw_idx;
253
254 /* Attribute form DW_FORM_*. */
255 ULONGEST form;
256
257 /* Value if FORM is DW_FORM_implicit_const. */
258 LONGEST implicit_const;
259 };
260 std::vector<attr> attr_vec;
261 };
262
263 std::unordered_map<ULONGEST, index_val> abbrev_map;
264
265 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
266
267 /* Implementation of the mapped_index_base virtual interface, for
268 the name_components cache. */
269
270 const char *symbol_name_at (offset_type idx) const override
271 { return namei_to_name (idx); }
272
273 size_t symbol_name_count () const override
274 { return this->name_count; }
927aa2e7
JK
275};
276
cd4fb1b2 277/* See dwarf2read.h. */
ed2dc618 278
cd4fb1b2 279dwarf2_per_objfile *
ed2dc618
SM
280get_dwarf2_per_objfile (struct objfile *objfile)
281{
5bfd760d 282 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 283}
c906108c 284
251d32d9 285/* Default names of the debugging sections. */
c906108c 286
233a11ab
CS
287/* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
9cdd5dbd
DE
290static const struct dwarf2_debug_sections dwarf2_elf_names =
291{
251d32d9
TG
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
43988095 296 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 297 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 298 { ".debug_macro", ".zdebug_macro" },
251d32d9 299 { ".debug_str", ".zdebug_str" },
18a8505e 300 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 301 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 302 { ".debug_ranges", ".zdebug_ranges" },
43988095 303 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 304 { ".debug_types", ".zdebug_types" },
3019eac3 305 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
306 { ".debug_frame", ".zdebug_frame" },
307 { ".eh_frame", NULL },
24d3216f 308 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
309 { ".debug_names", ".zdebug_names" },
310 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 311 23
251d32d9 312};
c906108c 313
80626a55 314/* List of DWO/DWP sections. */
3019eac3 315
80626a55 316static const struct dwop_section_names
3019eac3
DE
317{
318 struct dwarf2_section_names abbrev_dwo;
319 struct dwarf2_section_names info_dwo;
320 struct dwarf2_section_names line_dwo;
321 struct dwarf2_section_names loc_dwo;
43988095 322 struct dwarf2_section_names loclists_dwo;
09262596
DE
323 struct dwarf2_section_names macinfo_dwo;
324 struct dwarf2_section_names macro_dwo;
3019eac3
DE
325 struct dwarf2_section_names str_dwo;
326 struct dwarf2_section_names str_offsets_dwo;
327 struct dwarf2_section_names types_dwo;
80626a55
DE
328 struct dwarf2_section_names cu_index;
329 struct dwarf2_section_names tu_index;
3019eac3 330}
80626a55 331dwop_section_names =
3019eac3
DE
332{
333 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
334 { ".debug_info.dwo", ".zdebug_info.dwo" },
335 { ".debug_line.dwo", ".zdebug_line.dwo" },
336 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 337 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
338 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
339 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
340 { ".debug_str.dwo", ".zdebug_str.dwo" },
341 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
342 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
343 { ".debug_cu_index", ".zdebug_cu_index" },
344 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
345};
346
c906108c
SS
347/* local data types */
348
3da10d80
KS
349/* Type used for delaying computation of method physnames.
350 See comments for compute_delayed_physnames. */
351struct delayed_method_info
352{
353 /* The type to which the method is attached, i.e., its parent class. */
354 struct type *type;
355
356 /* The index of the method in the type's function fieldlists. */
357 int fnfield_index;
358
359 /* The index of the method in the fieldlist. */
360 int index;
361
362 /* The name of the DIE. */
363 const char *name;
364
365 /* The DIE associated with this method. */
366 struct die_info *die;
367};
368
e7c27a73
DJ
369/* Internal state when decoding a particular compilation unit. */
370struct dwarf2_cu
371{
fcd3b13d
SM
372 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
373 ~dwarf2_cu ();
374
375 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
376
c24bdb02
KS
377 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
378 Create the set of symtabs used by this TU, or if this TU is sharing
379 symtabs with another TU and the symtabs have already been created
380 then restore those symtabs in the line header.
381 We don't need the pc/line-number mapping for type units. */
382 void setup_type_unit_groups (struct die_info *die);
383
384 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
385 buildsym_compunit constructor. */
386 struct compunit_symtab *start_symtab (const char *name,
387 const char *comp_dir,
388 CORE_ADDR low_pc);
389
390 /* Reset the builder. */
391 void reset_builder () { m_builder.reset (); }
392
d00adf39 393 /* The header of the compilation unit. */
fcd3b13d 394 struct comp_unit_head header {};
e142c38c 395
d00adf39 396 /* Base address of this compilation unit. */
2b24b6e4 397 gdb::optional<CORE_ADDR> base_address;
d00adf39 398
e142c38c 399 /* The language we are debugging. */
fcd3b13d
SM
400 enum language language = language_unknown;
401 const struct language_defn *language_defn = nullptr;
e142c38c 402
fcd3b13d 403 const char *producer = nullptr;
b0f35d58 404
c24bdb02 405private:
804d2729
TT
406 /* The symtab builder for this CU. This is only non-NULL when full
407 symbols are being read. */
c24bdb02 408 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 409
c24bdb02 410public:
e142c38c
DJ
411 /* The generic symbol table building routines have separate lists for
412 file scope symbols and all all other scopes (local scopes). So
413 we need to select the right one to pass to add_symbol_to_list().
414 We do it by keeping a pointer to the correct list in list_in_scope.
415
416 FIXME: The original dwarf code just treated the file scope as the
417 first local scope, and all other local scopes as nested local
418 scopes, and worked fine. Check to see if we really need to
419 distinguish these in buildsym.c. */
fcd3b13d 420 struct pending **list_in_scope = nullptr;
e142c38c 421
b64f50a1
JK
422 /* Hash table holding all the loaded partial DIEs
423 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 424 htab_t partial_dies = nullptr;
72bf9492
DJ
425
426 /* Storage for things with the same lifetime as this read-in compilation
427 unit, including partial DIEs. */
fcd3b13d 428 auto_obstack comp_unit_obstack;
72bf9492 429
ae038cb0
DJ
430 /* When multiple dwarf2_cu structures are living in memory, this field
431 chains them all together, so that they can be released efficiently.
432 We will probably also want a generation counter so that most-recently-used
433 compilation units are cached... */
fcd3b13d 434 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 435
69d751e3 436 /* Backlink to our per_cu entry. */
ae038cb0
DJ
437 struct dwarf2_per_cu_data *per_cu;
438
439 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 440 int last_used = 0;
ae038cb0 441
b64f50a1
JK
442 /* A hash table of DIE cu_offset for following references with
443 die_info->offset.sect_off as hash. */
fcd3b13d 444 htab_t die_hash = nullptr;
10b3939b
DJ
445
446 /* Full DIEs if read in. */
fcd3b13d 447 struct die_info *dies = nullptr;
10b3939b
DJ
448
449 /* A set of pointers to dwarf2_per_cu_data objects for compilation
450 units referenced by this one. Only set during full symbol processing;
451 partial symbol tables do not have dependencies. */
fcd3b13d 452 htab_t dependencies = nullptr;
10b3939b 453
cb1df416 454 /* Header data from the line table, during full symbol processing. */
fcd3b13d 455 struct line_header *line_header = nullptr;
4c8aa72d
PA
456 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
457 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
458 this is the DW_TAG_compile_unit die for this CU. We'll hold on
459 to the line header as long as this DIE is being processed. See
460 process_die_scope. */
fcd3b13d 461 die_info *line_header_die_owner = nullptr;
cb1df416 462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
c89b44cd 465 std::vector<delayed_method_info> method_list;
3da10d80 466
96408a79 467 /* To be copied to symtab->call_site_htab. */
fcd3b13d 468 htab_t call_site_htab = nullptr;
96408a79 469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
18a8505e 474 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
fcd3b13d 479 struct dwo_unit *dwo_unit = nullptr;
3019eac3 480
18a8505e 481 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 482 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 483 gdb::optional<ULONGEST> addr_base;
3019eac3 484
18a8505e 485 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 486 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 487 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 492 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 494 ULONGEST ranges_base = 0;
2e3cf129 495
c9317f21
TT
496 /* When reading debug info generated by older versions of rustc, we
497 have to rewrite some union types to be struct types with a
498 variant part. This rewriting must be done after the CU is fully
499 read in, because otherwise at the point of rewriting some struct
500 type might not have been fully processed. So, we keep a list of
501 all such types here and process them after expansion. */
502 std::vector<struct type *> rust_unions;
503
18a8505e
AT
504 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
505 files, the value is implicitly zero. For DWARF 5 version DWO files, the
506 value is often implicit and is the size of the header of
507 .debug_str_offsets section (8 or 4, depending on the address size). */
508 gdb::optional<ULONGEST> str_offsets_base;
509
ae038cb0 510 /* Mark used when releasing cached dies. */
9068261f 511 bool mark : 1;
ae038cb0 512
8be455d7
JK
513 /* This CU references .debug_loc. See the symtab->locations_valid field.
514 This test is imperfect as there may exist optimized debug code not using
515 any location list and still facing inlining issues if handled as
516 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 517 bool has_loclist : 1;
ba919b58 518
9068261f 519 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
520 if all the producer_is_* fields are valid. This information is cached
521 because profiling CU expansion showed excessive time spent in
522 producer_is_gxx_lt_4_6. */
9068261f
AB
523 bool checked_producer : 1;
524 bool producer_is_gxx_lt_4_6 : 1;
525 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 526 bool producer_is_icc : 1;
9068261f 527 bool producer_is_icc_lt_14 : 1;
c258c396 528 bool producer_is_codewarrior : 1;
4d4ec4e5 529
9068261f 530 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
531 debugging info for C++ namespaces. GCC 3.3.x did not produce
532 this information, but later versions do. */
533
9068261f 534 bool processing_has_namespace_info : 1;
d590ff25
YQ
535
536 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
537
538 /* If this CU was inherited by another CU (via specification,
539 abstract_origin, etc), this is the ancestor CU. */
540 dwarf2_cu *ancestor;
541
542 /* Get the buildsym_compunit for this CU. */
543 buildsym_compunit *get_builder ()
544 {
545 /* If this CU has a builder associated with it, use that. */
546 if (m_builder != nullptr)
547 return m_builder.get ();
548
549 /* Otherwise, search ancestors for a valid builder. */
550 if (ancestor != nullptr)
551 return ancestor->get_builder ();
552
553 return nullptr;
554 }
e7c27a73
DJ
555};
556
094b34ac
DE
557/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
558 This includes type_unit_group and quick_file_names. */
559
560struct stmt_list_hash
561{
562 /* The DWO unit this table is from or NULL if there is none. */
563 struct dwo_unit *dwo_unit;
564
565 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 566 sect_offset line_sect_off;
094b34ac
DE
567};
568
f4dc4d17
DE
569/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
570 an object of this type. */
571
572struct type_unit_group
573{
0186c6a7 574 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
575 To simplify things we create an artificial CU that "includes" all the
576 type units using this stmt_list so that the rest of the code still has
197400e8 577 a "per_cu" handle on the symtab. */
094b34ac
DE
578 struct dwarf2_per_cu_data per_cu;
579
0186c6a7
DE
580 /* The TUs that share this DW_AT_stmt_list entry.
581 This is added to while parsing type units to build partial symtabs,
582 and is deleted afterwards and not used again. */
a8b3b8e9 583 std::vector<signatured_type *> *tus;
f4dc4d17 584
43f3e411 585 /* The compunit symtab.
094b34ac 586 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
587 so we create an essentially anonymous symtab as the compunit symtab. */
588 struct compunit_symtab *compunit_symtab;
f4dc4d17 589
094b34ac
DE
590 /* The data used to construct the hash key. */
591 struct stmt_list_hash hash;
f4dc4d17 592
f4dc4d17
DE
593 /* The symbol tables for this TU (obtained from the files listed in
594 DW_AT_stmt_list).
595 WARNING: The order of entries here must match the order of entries
596 in the line header. After the first TU using this type_unit_group, the
597 line header for the subsequent TUs is recreated from this. This is done
598 because we need to use the same symtabs for each TU using the same
599 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
600 there's no guarantee the line header doesn't have duplicate entries. */
601 struct symtab **symtabs;
602};
603
73869dc2 604/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
605
606struct dwo_sections
607{
608 struct dwarf2_section_info abbrev;
3019eac3
DE
609 struct dwarf2_section_info line;
610 struct dwarf2_section_info loc;
43988095 611 struct dwarf2_section_info loclists;
09262596
DE
612 struct dwarf2_section_info macinfo;
613 struct dwarf2_section_info macro;
3019eac3
DE
614 struct dwarf2_section_info str;
615 struct dwarf2_section_info str_offsets;
80626a55
DE
616 /* In the case of a virtual DWO file, these two are unused. */
617 struct dwarf2_section_info info;
fd5866f6 618 std::vector<dwarf2_section_info> types;
3019eac3
DE
619};
620
c88ee1f0 621/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
622
623struct dwo_unit
624{
625 /* Backlink to the containing struct dwo_file. */
626 struct dwo_file *dwo_file;
627
628 /* The "id" that distinguishes this CU/TU.
629 .debug_info calls this "dwo_id", .debug_types calls this "signature".
630 Since signatures came first, we stick with it for consistency. */
631 ULONGEST signature;
632
633 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 634 struct dwarf2_section_info *section;
3019eac3 635
9c541725
PA
636 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
637 sect_offset sect_off;
3019eac3
DE
638 unsigned int length;
639
640 /* For types, offset in the type's DIE of the type defined by this TU. */
641 cu_offset type_offset_in_tu;
642};
643
73869dc2
DE
644/* include/dwarf2.h defines the DWP section codes.
645 It defines a max value but it doesn't define a min value, which we
646 use for error checking, so provide one. */
647
648enum dwp_v2_section_ids
649{
650 DW_SECT_MIN = 1
651};
652
80626a55 653/* Data for one DWO file.
57d63ce2
DE
654
655 This includes virtual DWO files (a virtual DWO file is a DWO file as it
656 appears in a DWP file). DWP files don't really have DWO files per se -
657 comdat folding of types "loses" the DWO file they came from, and from
658 a high level view DWP files appear to contain a mass of random types.
659 However, to maintain consistency with the non-DWP case we pretend DWP
660 files contain virtual DWO files, and we assign each TU with one virtual
661 DWO file (generally based on the line and abbrev section offsets -
662 a heuristic that seems to work in practice). */
3019eac3
DE
663
664struct dwo_file
665{
51ac9db5
SM
666 dwo_file () = default;
667 DISABLE_COPY_AND_ASSIGN (dwo_file);
668
18a8505e 669 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
670 For virtual DWO files the name is constructed from the section offsets
671 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
672 from related CU+TUs. */
51ac9db5 673 const char *dwo_name = nullptr;
0ac5b59e
DE
674
675 /* The DW_AT_comp_dir attribute. */
51ac9db5 676 const char *comp_dir = nullptr;
3019eac3 677
80626a55
DE
678 /* The bfd, when the file is open. Otherwise this is NULL.
679 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 680 gdb_bfd_ref_ptr dbfd;
3019eac3 681
73869dc2
DE
682 /* The sections that make up this DWO file.
683 Remember that for virtual DWO files in DWP V2, these are virtual
684 sections (for lack of a better name). */
51ac9db5 685 struct dwo_sections sections {};
3019eac3 686
33c5cd75
DB
687 /* The CUs in the file.
688 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
689 an extension to handle LLVM's Link Time Optimization output (where
690 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 691 htab_up cus;
3019eac3
DE
692
693 /* Table of TUs in the file.
694 Each element is a struct dwo_unit. */
b0b6a987 695 htab_up tus;
3019eac3
DE
696};
697
80626a55
DE
698/* These sections are what may appear in a DWP file. */
699
700struct dwp_sections
701{
73869dc2 702 /* These are used by both DWP version 1 and 2. */
80626a55
DE
703 struct dwarf2_section_info str;
704 struct dwarf2_section_info cu_index;
705 struct dwarf2_section_info tu_index;
73869dc2
DE
706
707 /* These are only used by DWP version 2 files.
708 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
709 sections are referenced by section number, and are not recorded here.
710 In DWP version 2 there is at most one copy of all these sections, each
711 section being (effectively) comprised of the concatenation of all of the
712 individual sections that exist in the version 1 format.
713 To keep the code simple we treat each of these concatenated pieces as a
714 section itself (a virtual section?). */
715 struct dwarf2_section_info abbrev;
716 struct dwarf2_section_info info;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
721 struct dwarf2_section_info str_offsets;
722 struct dwarf2_section_info types;
80626a55
DE
723};
724
73869dc2
DE
725/* These sections are what may appear in a virtual DWO file in DWP version 1.
726 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 727
73869dc2 728struct virtual_v1_dwo_sections
80626a55
DE
729{
730 struct dwarf2_section_info abbrev;
731 struct dwarf2_section_info line;
732 struct dwarf2_section_info loc;
733 struct dwarf2_section_info macinfo;
734 struct dwarf2_section_info macro;
735 struct dwarf2_section_info str_offsets;
736 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 737 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
738 struct dwarf2_section_info info_or_types;
739};
740
73869dc2
DE
741/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
742 In version 2, the sections of the DWO files are concatenated together
743 and stored in one section of that name. Thus each ELF section contains
744 several "virtual" sections. */
745
746struct virtual_v2_dwo_sections
747{
748 bfd_size_type abbrev_offset;
749 bfd_size_type abbrev_size;
750
751 bfd_size_type line_offset;
752 bfd_size_type line_size;
753
754 bfd_size_type loc_offset;
755 bfd_size_type loc_size;
756
757 bfd_size_type macinfo_offset;
758 bfd_size_type macinfo_size;
759
760 bfd_size_type macro_offset;
761 bfd_size_type macro_size;
762
763 bfd_size_type str_offsets_offset;
764 bfd_size_type str_offsets_size;
765
766 /* Each DWP hash table entry records one CU or one TU.
767 That is recorded here, and copied to dwo_unit.section. */
768 bfd_size_type info_or_types_offset;
769 bfd_size_type info_or_types_size;
770};
771
80626a55
DE
772/* Contents of DWP hash tables. */
773
774struct dwp_hash_table
775{
73869dc2 776 uint32_t version, nr_columns;
80626a55 777 uint32_t nr_units, nr_slots;
73869dc2
DE
778 const gdb_byte *hash_table, *unit_table;
779 union
780 {
781 struct
782 {
783 const gdb_byte *indices;
784 } v1;
785 struct
786 {
787 /* This is indexed by column number and gives the id of the section
788 in that column. */
789#define MAX_NR_V2_DWO_SECTIONS \
790 (1 /* .debug_info or .debug_types */ \
791 + 1 /* .debug_abbrev */ \
792 + 1 /* .debug_line */ \
793 + 1 /* .debug_loc */ \
794 + 1 /* .debug_str_offsets */ \
795 + 1 /* .debug_macro or .debug_macinfo */)
796 int section_ids[MAX_NR_V2_DWO_SECTIONS];
797 const gdb_byte *offsets;
798 const gdb_byte *sizes;
799 } v2;
800 } section_pool;
80626a55
DE
801};
802
803/* Data for one DWP file. */
804
805struct dwp_file
806{
400174b1
TT
807 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
808 : name (name_),
809 dbfd (std::move (abfd))
810 {
811 }
812
80626a55
DE
813 /* Name of the file. */
814 const char *name;
815
73869dc2 816 /* File format version. */
400174b1 817 int version = 0;
73869dc2 818
93417882 819 /* The bfd. */
400174b1 820 gdb_bfd_ref_ptr dbfd;
80626a55
DE
821
822 /* Section info for this file. */
400174b1 823 struct dwp_sections sections {};
80626a55 824
57d63ce2 825 /* Table of CUs in the file. */
400174b1 826 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
827
828 /* Table of TUs in the file. */
400174b1 829 const struct dwp_hash_table *tus = nullptr;
80626a55 830
19ac8c2e 831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
832 htab_up loaded_cus;
833 htab_up loaded_tus;
80626a55 834
73869dc2
DE
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
400174b1
TT
837 unsigned int num_sections = 0;
838 asection **elf_sections = nullptr;
80626a55
DE
839};
840
0963b4bd
MS
841/* Struct used to pass misc. parameters to read_die_and_children, et
842 al. which are used for both .debug_info and .debug_types dies.
843 All parameters here are unchanging for the life of the call. This
dee91e82 844 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
845
846struct die_reader_specs
847{
a32a8923 848 /* The bfd of die_section. */
93311388
DE
849 bfd* abfd;
850
851 /* The CU of the DIE we are parsing. */
852 struct dwarf2_cu *cu;
853
80626a55 854 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
855 struct dwo_file *dwo_file;
856
dee91e82 857 /* The section the die comes from.
3019eac3 858 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
859 struct dwarf2_section_info *die_section;
860
861 /* die_section->buffer. */
d521ce57 862 const gdb_byte *buffer;
f664829e
DE
863
864 /* The end of the buffer. */
865 const gdb_byte *buffer_end;
a2ce51a0 866
685af9cd
TT
867 /* The abbreviation table to use when reading the DIEs. */
868 struct abbrev_table *abbrev_table;
93311388
DE
869};
870
c0ab21c2
TT
871/* A subclass of die_reader_specs that holds storage and has complex
872 constructor and destructor behavior. */
873
874class cutu_reader : public die_reader_specs
875{
876public:
877
878 cutu_reader (struct dwarf2_per_cu_data *this_cu,
879 struct abbrev_table *abbrev_table,
6751ebae 880 int use_existing_cu,
c0ab21c2
TT
881 bool skip_partial);
882
883 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
884 struct dwarf2_cu *parent_cu = nullptr,
885 struct dwo_file *dwo_file = nullptr);
886
c0ab21c2
TT
887 DISABLE_COPY_AND_ASSIGN (cutu_reader);
888
889 const gdb_byte *info_ptr = nullptr;
890 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
891 bool dummy_p = false;
892
6751ebae
TT
893 /* Release the new CU, putting it on the chain. This cannot be done
894 for dummy CUs. */
895 void keep ();
896
c0ab21c2
TT
897private:
898 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 899 int use_existing_cu);
c0ab21c2
TT
900
901 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
902 std::unique_ptr<dwarf2_cu> m_new_cu;
903
904 /* The ordinary abbreviation table. */
905 abbrev_table_up m_abbrev_table_holder;
906
907 /* The DWO abbreviation table. */
908 abbrev_table_up m_dwo_abbrev_table;
909};
dee91e82 910
c906108c 911/* When we construct a partial symbol table entry we only
0963b4bd 912 need this much information. */
6f06d47b 913struct partial_die_info : public allocate_on_obstack
c906108c 914 {
6f06d47b
YQ
915 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
916
917 /* Disable assign but still keep copy ctor, which is needed
918 load_partial_dies. */
919 partial_die_info& operator=(const partial_die_info& rhs) = delete;
920
52356b79
YQ
921 /* Adjust the partial die before generating a symbol for it. This
922 function may set the is_external flag or change the DIE's
923 name. */
924 void fixup (struct dwarf2_cu *cu);
925
48fbe735
YQ
926 /* Read a minimal amount of information into the minimal die
927 structure. */
928 const gdb_byte *read (const struct die_reader_specs *reader,
929 const struct abbrev_info &abbrev,
930 const gdb_byte *info_ptr);
931
72bf9492 932 /* Offset of this DIE. */
6f06d47b 933 const sect_offset sect_off;
72bf9492
DJ
934
935 /* DWARF-2 tag for this DIE. */
6f06d47b 936 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 937
72bf9492 938 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
939 const unsigned int has_children : 1;
940
72bf9492
DJ
941 unsigned int is_external : 1;
942 unsigned int is_declaration : 1;
943 unsigned int has_type : 1;
944 unsigned int has_specification : 1;
945 unsigned int has_pc_info : 1;
481860b3 946 unsigned int may_be_inlined : 1;
72bf9492 947
0c1b455e
TT
948 /* This DIE has been marked DW_AT_main_subprogram. */
949 unsigned int main_subprogram : 1;
950
72bf9492
DJ
951 /* Flag set if the SCOPE field of this structure has been
952 computed. */
953 unsigned int scope_set : 1;
954
fa4028e9
JB
955 /* Flag set if the DIE has a byte_size attribute. */
956 unsigned int has_byte_size : 1;
957
ff908ebf
AW
958 /* Flag set if the DIE has a DW_AT_const_value attribute. */
959 unsigned int has_const_value : 1;
960
98bfdba5
PA
961 /* Flag set if any of the DIE's children are template arguments. */
962 unsigned int has_template_arguments : 1;
963
52356b79 964 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
965 unsigned int fixup_called : 1;
966
36586728
TT
967 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
968 unsigned int is_dwz : 1;
969
970 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
971 unsigned int spec_is_dwz : 1;
972
72bf9492 973 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 974 sometimes a default name for unnamed DIEs. */
6f06d47b 975 const char *name = nullptr;
72bf9492 976
abc72ce4 977 /* The linkage name, if present. */
6f06d47b 978 const char *linkage_name = nullptr;
abc72ce4 979
72bf9492
DJ
980 /* The scope to prepend to our children. This is generally
981 allocated on the comp_unit_obstack, so will disappear
982 when this compilation unit leaves the cache. */
6f06d47b 983 const char *scope = nullptr;
72bf9492 984
95554aad
TT
985 /* Some data associated with the partial DIE. The tag determines
986 which field is live. */
987 union
988 {
989 /* The location description associated with this DIE, if any. */
990 struct dwarf_block *locdesc;
991 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 992 sect_offset sect_off;
6f06d47b 993 } d {};
72bf9492
DJ
994
995 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
996 CORE_ADDR lowpc = 0;
997 CORE_ADDR highpc = 0;
72bf9492 998
93311388 999 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1000 DW_AT_sibling, if any. */
48fbe735
YQ
1001 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1002 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1003 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1004
1005 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1006 DW_AT_specification (or DW_AT_abstract_origin or
1007 DW_AT_extension). */
6f06d47b 1008 sect_offset spec_offset {};
72bf9492
DJ
1009
1010 /* Pointers to this DIE's parent, first child, and next sibling,
1011 if any. */
6f06d47b
YQ
1012 struct partial_die_info *die_parent = nullptr;
1013 struct partial_die_info *die_child = nullptr;
1014 struct partial_die_info *die_sibling = nullptr;
1015
1016 friend struct partial_die_info *
1017 dwarf2_cu::find_partial_die (sect_offset sect_off);
1018
1019 private:
1020 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1021 partial_die_info (sect_offset sect_off)
1022 : partial_die_info (sect_off, DW_TAG_padding, 0)
1023 {
1024 }
1025
1026 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1027 int has_children_)
1028 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1029 {
1030 is_external = 0;
1031 is_declaration = 0;
1032 has_type = 0;
1033 has_specification = 0;
1034 has_pc_info = 0;
1035 may_be_inlined = 0;
1036 main_subprogram = 0;
1037 scope_set = 0;
1038 has_byte_size = 0;
1039 has_const_value = 0;
1040 has_template_arguments = 0;
1041 fixup_called = 0;
1042 is_dwz = 0;
1043 spec_is_dwz = 0;
1044 }
c906108c
SS
1045 };
1046
c906108c
SS
1047/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1048 but this would require a corresponding change in unpack_field_as_long
1049 and friends. */
1050static int bits_per_byte = 8;
1051
2ddeaf8a
TT
1052/* When reading a variant or variant part, we track a bit more
1053 information about the field, and store it in an object of this
1054 type. */
1055
1056struct variant_field
1057{
1058 /* If we see a DW_TAG_variant, then this will be the discriminant
1059 value. */
1060 ULONGEST discriminant_value;
1061 /* If we see a DW_TAG_variant, then this will be set if this is the
1062 default branch. */
1063 bool default_branch;
1064 /* While reading a DW_TAG_variant_part, this will be set if this
1065 field is the discriminant. */
1066 bool is_discriminant;
1067};
1068
52059ffd
TT
1069struct nextfield
1070{
be2daae6
TT
1071 int accessibility = 0;
1072 int virtuality = 0;
2ddeaf8a 1073 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1074 struct variant_field variant {};
1075 struct field field {};
52059ffd
TT
1076};
1077
1078struct fnfieldlist
1079{
be2daae6
TT
1080 const char *name = nullptr;
1081 std::vector<struct fn_field> fnfields;
52059ffd
TT
1082};
1083
c906108c
SS
1084/* The routines that read and process dies for a C struct or C++ class
1085 pass lists of data member fields and lists of member function fields
1086 in an instance of a field_info structure, as defined below. */
1087struct field_info
c5aa993b 1088 {
0963b4bd 1089 /* List of data member and baseclasses fields. */
be2daae6
TT
1090 std::vector<struct nextfield> fields;
1091 std::vector<struct nextfield> baseclasses;
c906108c 1092
85102364 1093 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1094 int non_public_fields = 0;
c906108c 1095
c5aa993b
JM
1096 /* Member function fieldlist array, contains name of possibly overloaded
1097 member function, number of overloaded member functions and a pointer
1098 to the head of the member function field chain. */
be2daae6 1099 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1100
1101 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1102 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1103 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1104
1105 /* Nested types defined by this class and the number of elements in this
1106 list. */
be2daae6 1107 std::vector<struct decl_field> nested_types_list;
317f7127
TT
1108
1109 /* Return the total number of fields (including baseclasses). */
1110 int nfields () const
1111 {
1112 return fields.size () + baseclasses.size ();
1113 }
c5aa993b 1114 };
c906108c 1115
ae038cb0
DJ
1116/* Loaded secondary compilation units are kept in memory until they
1117 have not been referenced for the processing of this many
1118 compilation units. Set this to zero to disable caching. Cache
1119 sizes of up to at least twenty will improve startup time for
1120 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1121static int dwarf_max_cache_age = 5;
920d2a44 1122static void
b4f54984
DE
1123show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1124 struct cmd_list_element *c, const char *value)
920d2a44 1125{
3e43a32a 1126 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1127 "DWARF compilation units is %s.\n"),
920d2a44
AC
1128 value);
1129}
4390d890 1130\f
c906108c
SS
1131/* local function prototypes */
1132
918dd910
JK
1133static void dwarf2_find_base_address (struct die_info *die,
1134 struct dwarf2_cu *cu);
1135
891813be 1136static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1137 (struct dwarf2_per_cu_data *per_cu, const char *name);
1138
f1902523
JK
1139static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1140 const gdb_byte *info_ptr,
3e225074 1141 struct die_info *type_unit_die);
f1902523 1142
ed2dc618
SM
1143static void dwarf2_build_psymtabs_hard
1144 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1145
72bf9492
DJ
1146static void scan_partial_symbols (struct partial_die_info *,
1147 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1148 int, struct dwarf2_cu *);
c906108c 1149
72bf9492
DJ
1150static void add_partial_symbol (struct partial_die_info *,
1151 struct dwarf2_cu *);
63d06c5c 1152
72bf9492
DJ
1153static void add_partial_namespace (struct partial_die_info *pdi,
1154 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1155 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1156
5d7cb8df 1157static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1158 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1159 struct dwarf2_cu *cu);
1160
72bf9492
DJ
1161static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1162 struct dwarf2_cu *cu);
91c24f0a 1163
bc30ff58
JB
1164static void add_partial_subprogram (struct partial_die_info *pdi,
1165 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1166 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1167
d521ce57 1168static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1169
dee91e82 1170static struct partial_die_info *load_partial_dies
d521ce57 1171 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1172
fb816e8b
TV
1173/* A pair of partial_die_info and compilation unit. */
1174struct cu_partial_die_info
1175{
1176 /* The compilation unit of the partial_die_info. */
1177 struct dwarf2_cu *cu;
1178 /* A partial_die_info. */
1179 struct partial_die_info *pdi;
122cf0f2
AB
1180
1181 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1182 : cu (cu),
1183 pdi (pdi)
405feb71 1184 { /* Nothing. */ }
122cf0f2
AB
1185
1186private:
1187 cu_partial_die_info () = delete;
fb816e8b
TV
1188};
1189
122cf0f2
AB
1190static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1191 struct dwarf2_cu *);
72bf9492 1192
d521ce57
TT
1193static const gdb_byte *read_attribute (const struct die_reader_specs *,
1194 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1195 const gdb_byte *, bool *need_reprocess);
1196
1197static void read_attribute_reprocess (const struct die_reader_specs *reader,
1198 struct attribute *attr);
1199
1200static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1201
ed2dc618
SM
1202static sect_offset read_abbrev_offset
1203 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1204 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1205
ed2dc618
SM
1206static const char *read_indirect_string
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1208 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1209
ed2dc618 1210static const char *read_indirect_string_at_offset
4f44ae6c 1211 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1212
d521ce57
TT
1213static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1214 const gdb_byte *,
3019eac3
DE
1215 unsigned int *);
1216
18a8505e
AT
1217static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1218 ULONGEST str_index);
1219
1220static const char *read_stub_str_index (struct dwarf2_cu *cu,
1221 ULONGEST str_index);
3019eac3 1222
e142c38c 1223static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1224
e142c38c
DJ
1225static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1226 struct dwarf2_cu *);
c906108c 1227
7d45c7c3
KB
1228static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1229 struct dwarf2_cu *cu);
1230
a084a2a6
AT
1231static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1232
05cf31d1
JB
1233static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1234 struct dwarf2_cu *cu);
1235
e142c38c 1236static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1237
e142c38c 1238static struct die_info *die_specification (struct die_info *die,
f2f0e013 1239 struct dwarf2_cu **);
63d06c5c 1240
9c541725 1241static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1242 struct dwarf2_cu *cu);
debd256d 1243
f3f5162e 1244static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1245 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1246 CORE_ADDR, int decode_mapping);
c906108c 1247
804d2729
TT
1248static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1249 const char *);
c906108c 1250
a14ed312 1251static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1252 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1253
ff39bb5e 1254static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1255 struct dwarf2_cu *);
c906108c 1256
ff39bb5e 1257static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1258 struct type *type,
1259 const char *name,
1260 struct obstack *obstack,
12df843f 1261 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1262 const gdb_byte **bytes,
98bfdba5 1263 struct dwarf2_locexpr_baton **baton);
2df3850c 1264
e7c27a73 1265static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1266
b4ba55a1
JB
1267static int need_gnat_info (struct dwarf2_cu *);
1268
3e43a32a
MS
1269static struct type *die_descriptive_type (struct die_info *,
1270 struct dwarf2_cu *);
b4ba55a1
JB
1271
1272static void set_descriptive_type (struct type *, struct die_info *,
1273 struct dwarf2_cu *);
1274
e7c27a73
DJ
1275static struct type *die_containing_type (struct die_info *,
1276 struct dwarf2_cu *);
c906108c 1277
ff39bb5e 1278static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1279 struct dwarf2_cu *);
c906108c 1280
f792889a 1281static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1282
673bfd45
DE
1283static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1284
0d5cff50 1285static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1286
6e70227d 1287static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1288 const char *suffix, int physname,
1289 struct dwarf2_cu *cu);
63d06c5c 1290
e7c27a73 1291static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1292
348e048f
DE
1293static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1294
e7c27a73 1295static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1296
e7c27a73 1297static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1298
96408a79
SA
1299static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1300
71a3c369
TT
1301static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1302
ff013f42 1303static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1304 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1305
3a2b436a 1306/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1307 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1308enum pc_bounds_kind
1309{
e385593e 1310 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1311 PC_BOUNDS_NOT_PRESENT,
1312
e385593e
JK
1313 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1314 were present but they do not form a valid range of PC addresses. */
1315 PC_BOUNDS_INVALID,
1316
3a2b436a
JK
1317 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1318 PC_BOUNDS_RANGES,
1319
1320 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1321 PC_BOUNDS_HIGH_LOW,
1322};
1323
1324static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1325 CORE_ADDR *, CORE_ADDR *,
1326 struct dwarf2_cu *,
891813be 1327 dwarf2_psymtab *);
c906108c 1328
fae299cd
DC
1329static void get_scope_pc_bounds (struct die_info *,
1330 CORE_ADDR *, CORE_ADDR *,
1331 struct dwarf2_cu *);
1332
801e3a5b
JB
1333static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1334 CORE_ADDR, struct dwarf2_cu *);
1335
a14ed312 1336static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1337 struct dwarf2_cu *);
c906108c 1338
a14ed312 1339static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1340 struct type *, struct dwarf2_cu *);
c906108c 1341
a14ed312 1342static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1343 struct die_info *, struct type *,
e7c27a73 1344 struct dwarf2_cu *);
c906108c 1345
a14ed312 1346static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1347 struct type *,
1348 struct dwarf2_cu *);
c906108c 1349
134d01f1 1350static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1351
e7c27a73 1352static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1353
e7c27a73 1354static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1355
5d7cb8df
JK
1356static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1357
804d2729 1358static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1359
27aa8d6a
SW
1360static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1361
74921315
KS
1362static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1363
f55ee35c
JK
1364static struct type *read_module_type (struct die_info *die,
1365 struct dwarf2_cu *cu);
1366
38d518c9 1367static const char *namespace_name (struct die_info *die,
e142c38c 1368 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1369
134d01f1 1370static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1371
e7c27a73 1372static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1373
6e70227d 1374static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1375 struct dwarf2_cu *);
1376
bf6af496 1377static struct die_info *read_die_and_siblings_1
d521ce57 1378 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1379 struct die_info *);
639d11d3 1380
dee91e82 1381static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1382 const gdb_byte *info_ptr,
1383 const gdb_byte **new_info_ptr,
639d11d3
DC
1384 struct die_info *parent);
1385
d521ce57
TT
1386static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1387 struct die_info **, const gdb_byte *,
3e225074 1388 int);
3019eac3 1389
d521ce57 1390static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1391 struct die_info **, const gdb_byte *);
93311388 1392
e7c27a73 1393static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1394
15d034d0 1395static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1396 struct objfile *);
71c25dea 1397
15d034d0 1398static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1399
15d034d0 1400static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1401 struct die_info *die,
1402 struct dwarf2_cu *cu);
1403
ca69b9e6
DE
1404static const char *dwarf2_physname (const char *name, struct die_info *die,
1405 struct dwarf2_cu *cu);
1406
e142c38c 1407static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1408 struct dwarf2_cu **);
9219021c 1409
d97bc12b
DE
1410static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1411
1412static void dump_die_for_error (struct die_info *);
1413
1414static void dump_die_1 (struct ui_file *, int level, int max_level,
1415 struct die_info *);
c906108c 1416
d97bc12b 1417/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1418
51545339 1419static void store_in_ref_table (struct die_info *,
10b3939b 1420 struct dwarf2_cu *);
c906108c 1421
348e048f 1422static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1423 const struct attribute *,
348e048f
DE
1424 struct dwarf2_cu **);
1425
10b3939b 1426static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1427 const struct attribute *,
f2f0e013 1428 struct dwarf2_cu **);
c906108c 1429
348e048f 1430static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1431 const struct attribute *,
348e048f
DE
1432 struct dwarf2_cu **);
1433
ac9ec31b
DE
1434static struct type *get_signatured_type (struct die_info *, ULONGEST,
1435 struct dwarf2_cu *);
1436
1437static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1438 const struct attribute *,
ac9ec31b
DE
1439 struct dwarf2_cu *);
1440
e5fe5e75 1441static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1442
52dc124a 1443static void read_signatured_type (struct signatured_type *);
348e048f 1444
63e43d3a
PMR
1445static int attr_to_dynamic_prop (const struct attribute *attr,
1446 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1447 struct dynamic_prop *prop, struct type *type);
63e43d3a 1448
c906108c
SS
1449/* memory allocation interface */
1450
7b5a2f43 1451static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1452
b60c80d6 1453static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1454
43f3e411 1455static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1456
8cf6f0b1
TT
1457static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1458 struct dwarf2_loclist_baton *baton,
ff39bb5e 1459 const struct attribute *attr);
8cf6f0b1 1460
ff39bb5e 1461static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1462 struct symbol *sym,
f1e6e072
TT
1463 struct dwarf2_cu *cu,
1464 int is_block);
4c2df51b 1465
d521ce57
TT
1466static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1467 const gdb_byte *info_ptr,
1468 struct abbrev_info *abbrev);
4bb7a0a7 1469
72bf9492
DJ
1470static hashval_t partial_die_hash (const void *item);
1471
1472static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1473
ae038cb0 1474static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1475 (sect_offset sect_off, unsigned int offset_in_dwz,
1476 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1477
9816fde3 1478static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1479 struct die_info *comp_unit_die,
1480 enum language pretend_language);
93311388 1481
ed2dc618 1482static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1483
dee91e82 1484static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1485
f792889a
DJ
1486static struct type *set_die_type (struct die_info *, struct type *,
1487 struct dwarf2_cu *);
1c379e20 1488
ed2dc618 1489static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1490
ed2dc618 1491static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1492
58f0c718 1493static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1494 enum language);
10b3939b 1495
95554aad
TT
1496static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1497 enum language);
10b3939b 1498
f4dc4d17
DE
1499static void process_full_type_unit (struct dwarf2_per_cu_data *,
1500 enum language);
1501
10b3939b
DJ
1502static void dwarf2_add_dependence (struct dwarf2_cu *,
1503 struct dwarf2_per_cu_data *);
1504
ae038cb0
DJ
1505static void dwarf2_mark (struct dwarf2_cu *);
1506
1507static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1508
b64f50a1 1509static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1510 struct dwarf2_per_cu_data *);
673bfd45 1511
f792889a 1512static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1513
95554aad
TT
1514static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1515 enum language pretend_language);
1516
ed2dc618 1517static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1518
b303c6f6
AB
1519/* Class, the destructor of which frees all allocated queue entries. This
1520 will only have work to do if an error was thrown while processing the
1521 dwarf. If no error was thrown then the queue entries should have all
1522 been processed, and freed, as we went along. */
1523
1524class dwarf2_queue_guard
1525{
1526public:
39856def
TT
1527 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1528 : m_per_objfile (per_objfile)
1529 {
1530 }
b303c6f6
AB
1531
1532 /* Free any entries remaining on the queue. There should only be
1533 entries left if we hit an error while processing the dwarf. */
1534 ~dwarf2_queue_guard ()
1535 {
39856def
TT
1536 /* Ensure that no memory is allocated by the queue. */
1537 std::queue<dwarf2_queue_item> empty;
1538 std::swap (m_per_objfile->queue, empty);
1539 }
b303c6f6 1540
39856def 1541 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1542
39856def
TT
1543private:
1544 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1545};
1546
39856def
TT
1547dwarf2_queue_item::~dwarf2_queue_item ()
1548{
1549 /* Anything still marked queued is likely to be in an
1550 inconsistent state, so discard it. */
1551 if (per_cu->queued)
1552 {
1553 if (per_cu->cu != NULL)
1554 free_one_cached_comp_unit (per_cu);
1555 per_cu->queued = 0;
1556 }
1557}
1558
d721ba37
PA
1559/* The return type of find_file_and_directory. Note, the enclosed
1560 string pointers are only valid while this object is valid. */
1561
1562struct file_and_directory
1563{
1564 /* The filename. This is never NULL. */
1565 const char *name;
1566
1567 /* The compilation directory. NULL if not known. If we needed to
1568 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1569 points directly to the DW_AT_comp_dir string attribute owned by
1570 the obstack that owns the DIE. */
1571 const char *comp_dir;
1572
1573 /* If we needed to build a new string for comp_dir, this is what
1574 owns the storage. */
1575 std::string comp_dir_storage;
1576};
1577
1578static file_and_directory find_file_and_directory (struct die_info *die,
1579 struct dwarf2_cu *cu);
9291a0cd 1580
298e9637 1581static htab_up allocate_signatured_type_table ();
1fd400ff 1582
298e9637 1583static htab_up allocate_dwo_unit_table ();
3019eac3 1584
57d63ce2 1585static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1586 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1587 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1588 ULONGEST signature, int is_debug_types);
a2ce51a0 1589
ed2dc618
SM
1590static struct dwp_file *get_dwp_file
1591 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1592
3019eac3 1593static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1594 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1595
1596static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1597 (struct signatured_type *, const char *, const char *);
3019eac3 1598
89e63ee4
DE
1599static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1600
263db9a1
TT
1601/* A unique pointer to a dwo_file. */
1602
51ac9db5 1603typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1604
ed2dc618 1605static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1606
1b80a9fa 1607static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1608
1609static void free_line_header_voidp (void *arg);
4390d890
DE
1610\f
1611/* Various complaints about symbol reading that don't abort the process. */
1612
4390d890
DE
1613static void
1614dwarf2_debug_line_missing_file_complaint (void)
1615{
b98664d3 1616 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1617}
1618
1619static void
1620dwarf2_debug_line_missing_end_sequence_complaint (void)
1621{
b98664d3 1622 complaint (_(".debug_line section has line "
4390d890
DE
1623 "program sequence without an end"));
1624}
1625
1626static void
1627dwarf2_complex_location_expr_complaint (void)
1628{
b98664d3 1629 complaint (_("location expression too complex"));
4390d890
DE
1630}
1631
1632static void
1633dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1634 int arg3)
1635{
b98664d3 1636 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1637 arg1, arg2, arg3);
1638}
1639
4390d890
DE
1640static void
1641dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1642{
b98664d3 1643 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1644 arg1, arg2);
1645}
527f3840
JK
1646
1647/* Hash function for line_header_hash. */
1648
1649static hashval_t
1650line_header_hash (const struct line_header *ofs)
1651{
9c541725 1652 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1653}
1654
1655/* Hash function for htab_create_alloc_ex for line_header_hash. */
1656
1657static hashval_t
1658line_header_hash_voidp (const void *item)
1659{
9a3c8263 1660 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1661
1662 return line_header_hash (ofs);
1663}
1664
1665/* Equality function for line_header_hash. */
1666
1667static int
1668line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1669{
9a3c8263
SM
1670 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1671 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1672
9c541725 1673 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1674 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1675}
1676
4390d890 1677\f
9291a0cd 1678
330cdd98
PA
1679/* See declaration. */
1680
1681dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1682 const dwarf2_debug_sections *names,
1683 bool can_copy_)
1684 : objfile (objfile_),
1685 can_copy (can_copy_)
330cdd98
PA
1686{
1687 if (names == NULL)
1688 names = &dwarf2_elf_names;
1689
1690 bfd *obfd = objfile->obfd;
1691
1692 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1693 locate_sections (obfd, sec, *names);
1694}
1695
1696dwarf2_per_objfile::~dwarf2_per_objfile ()
1697{
1698 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1699 free_cached_comp_units ();
1700
b76e467d 1701 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1702 per_cu->imported_symtabs_free ();
fc8e7e75 1703
b2bdb8cf 1704 for (signatured_type *sig_type : all_type_units)
ae640021 1705 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1706
330cdd98
PA
1707 /* Everything else should be on the objfile obstack. */
1708}
1709
1710/* See declaration. */
1711
1712void
1713dwarf2_per_objfile::free_cached_comp_units ()
1714{
1715 dwarf2_per_cu_data *per_cu = read_in_chain;
1716 dwarf2_per_cu_data **last_chain = &read_in_chain;
1717 while (per_cu != NULL)
1718 {
1719 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1720
fcd3b13d 1721 delete per_cu->cu;
330cdd98
PA
1722 *last_chain = next_cu;
1723 per_cu = next_cu;
1724 }
1725}
1726
11ed8cad
TT
1727/* A helper class that calls free_cached_comp_units on
1728 destruction. */
1729
1730class free_cached_comp_units
1731{
1732public:
1733
1734 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1735 : m_per_objfile (per_objfile)
1736 {
1737 }
1738
1739 ~free_cached_comp_units ()
1740 {
1741 m_per_objfile->free_cached_comp_units ();
1742 }
1743
1744 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1745
1746private:
1747
1748 dwarf2_per_objfile *m_per_objfile;
1749};
1750
c906108c 1751/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1752 information and return true if we have enough to do something.
1753 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1754 ELF names are used. CAN_COPY is true for formats where symbol
1755 interposition is possible and so symbol values must follow copy
1756 relocation rules. */
c906108c
SS
1757
1758int
251d32d9 1759dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1760 const struct dwarf2_debug_sections *names,
1761 bool can_copy)
c906108c 1762{
97cbe998
SDJ
1763 if (objfile->flags & OBJF_READNEVER)
1764 return 0;
1765
ed2dc618
SM
1766 struct dwarf2_per_objfile *dwarf2_per_objfile
1767 = get_dwarf2_per_objfile (objfile);
1768
1769 if (dwarf2_per_objfile == NULL)
5bfd760d 1770 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1771 names,
1772 can_copy);
5bfd760d 1773
73869dc2 1774 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1775 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1776 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1777 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1778}
1779
251d32d9
TG
1780/* When loading sections, we look either for uncompressed section or for
1781 compressed section names. */
233a11ab
CS
1782
1783static int
251d32d9
TG
1784section_is_p (const char *section_name,
1785 const struct dwarf2_section_names *names)
233a11ab 1786{
251d32d9
TG
1787 if (names->normal != NULL
1788 && strcmp (section_name, names->normal) == 0)
1789 return 1;
1790 if (names->compressed != NULL
1791 && strcmp (section_name, names->compressed) == 0)
1792 return 1;
1793 return 0;
233a11ab
CS
1794}
1795
330cdd98 1796/* See declaration. */
c906108c 1797
330cdd98
PA
1798void
1799dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1800 const dwarf2_debug_sections &names)
c906108c 1801{
fd361982 1802 flagword aflag = bfd_section_flags (sectp);
251d32d9 1803
dc7650b8
JK
1804 if ((aflag & SEC_HAS_CONTENTS) == 0)
1805 {
1806 }
950b7495
KS
1807 else if (elf_section_data (sectp)->this_hdr.sh_size
1808 > bfd_get_file_size (abfd))
1809 {
1810 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1811 warning (_("Discarding section %s which has a section size (%s"
1812 ") larger than the file size [in module %s]"),
1813 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1814 bfd_get_filename (abfd));
1815 }
330cdd98 1816 else if (section_is_p (sectp->name, &names.info))
c906108c 1817 {
330cdd98 1818 this->info.s.section = sectp;
fd361982 1819 this->info.size = bfd_section_size (sectp);
c906108c 1820 }
330cdd98 1821 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1822 {
330cdd98 1823 this->abbrev.s.section = sectp;
fd361982 1824 this->abbrev.size = bfd_section_size (sectp);
c906108c 1825 }
330cdd98 1826 else if (section_is_p (sectp->name, &names.line))
c906108c 1827 {
330cdd98 1828 this->line.s.section = sectp;
fd361982 1829 this->line.size = bfd_section_size (sectp);
c906108c 1830 }
330cdd98 1831 else if (section_is_p (sectp->name, &names.loc))
c906108c 1832 {
330cdd98 1833 this->loc.s.section = sectp;
fd361982 1834 this->loc.size = bfd_section_size (sectp);
c906108c 1835 }
330cdd98 1836 else if (section_is_p (sectp->name, &names.loclists))
43988095 1837 {
330cdd98 1838 this->loclists.s.section = sectp;
fd361982 1839 this->loclists.size = bfd_section_size (sectp);
43988095 1840 }
330cdd98 1841 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1842 {
330cdd98 1843 this->macinfo.s.section = sectp;
fd361982 1844 this->macinfo.size = bfd_section_size (sectp);
c906108c 1845 }
330cdd98 1846 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1847 {
330cdd98 1848 this->macro.s.section = sectp;
fd361982 1849 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1850 }
330cdd98 1851 else if (section_is_p (sectp->name, &names.str))
c906108c 1852 {
330cdd98 1853 this->str.s.section = sectp;
fd361982 1854 this->str.size = bfd_section_size (sectp);
c906108c 1855 }
18a8505e
AT
1856 else if (section_is_p (sectp->name, &names.str_offsets))
1857 {
1858 this->str_offsets.s.section = sectp;
1859 this->str_offsets.size = bfd_section_size (sectp);
1860 }
330cdd98 1861 else if (section_is_p (sectp->name, &names.line_str))
43988095 1862 {
330cdd98 1863 this->line_str.s.section = sectp;
fd361982 1864 this->line_str.size = bfd_section_size (sectp);
43988095 1865 }
330cdd98 1866 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1867 {
330cdd98 1868 this->addr.s.section = sectp;
fd361982 1869 this->addr.size = bfd_section_size (sectp);
3019eac3 1870 }
330cdd98 1871 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1872 {
330cdd98 1873 this->frame.s.section = sectp;
fd361982 1874 this->frame.size = bfd_section_size (sectp);
b6af0555 1875 }
330cdd98 1876 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1877 {
330cdd98 1878 this->eh_frame.s.section = sectp;
fd361982 1879 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1880 }
330cdd98 1881 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1882 {
330cdd98 1883 this->ranges.s.section = sectp;
fd361982 1884 this->ranges.size = bfd_section_size (sectp);
af34e669 1885 }
330cdd98 1886 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1887 {
330cdd98 1888 this->rnglists.s.section = sectp;
fd361982 1889 this->rnglists.size = bfd_section_size (sectp);
43988095 1890 }
330cdd98 1891 else if (section_is_p (sectp->name, &names.types))
348e048f 1892 {
8b70b953
TT
1893 struct dwarf2_section_info type_section;
1894
1895 memset (&type_section, 0, sizeof (type_section));
049412e3 1896 type_section.s.section = sectp;
fd361982 1897 type_section.size = bfd_section_size (sectp);
8b70b953 1898
fd5866f6 1899 this->types.push_back (type_section);
348e048f 1900 }
330cdd98 1901 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1902 {
330cdd98 1903 this->gdb_index.s.section = sectp;
fd361982 1904 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1905 }
927aa2e7
JK
1906 else if (section_is_p (sectp->name, &names.debug_names))
1907 {
1908 this->debug_names.s.section = sectp;
fd361982 1909 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1910 }
1911 else if (section_is_p (sectp->name, &names.debug_aranges))
1912 {
1913 this->debug_aranges.s.section = sectp;
fd361982 1914 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1915 }
dce234bc 1916
fd361982
AM
1917 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1918 && bfd_section_vma (sectp) == 0)
330cdd98 1919 this->has_section_at_zero = true;
c906108c
SS
1920}
1921
dce234bc 1922/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1923 SECTION_NAME. */
af34e669 1924
dce234bc 1925void
3017a003
TG
1926dwarf2_get_section_info (struct objfile *objfile,
1927 enum dwarf2_section_enum sect,
d521ce57 1928 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1929 bfd_size_type *sizep)
1930{
5bfd760d 1931 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 1932 struct dwarf2_section_info *info;
a3b2a86b
TT
1933
1934 /* We may see an objfile without any DWARF, in which case we just
1935 return nothing. */
1936 if (data == NULL)
1937 {
1938 *sectp = NULL;
1939 *bufp = NULL;
1940 *sizep = 0;
1941 return;
1942 }
3017a003
TG
1943 switch (sect)
1944 {
1945 case DWARF2_DEBUG_FRAME:
1946 info = &data->frame;
1947 break;
1948 case DWARF2_EH_FRAME:
1949 info = &data->eh_frame;
1950 break;
1951 default:
1952 gdb_assert_not_reached ("unexpected section");
1953 }
dce234bc 1954
96b79293 1955 info->read (objfile);
dce234bc 1956
96b79293 1957 *sectp = info->get_bfd_section ();
dce234bc
PP
1958 *bufp = info->buffer;
1959 *sizep = info->size;
1960}
1961
36586728
TT
1962/* A helper function to find the sections for a .dwz file. */
1963
1964static void
1965locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1966{
9a3c8263 1967 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
1968
1969 /* Note that we only support the standard ELF names, because .dwz
1970 is ELF-only (at the time of writing). */
1971 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1972 {
049412e3 1973 dwz_file->abbrev.s.section = sectp;
fd361982 1974 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
1975 }
1976 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1977 {
049412e3 1978 dwz_file->info.s.section = sectp;
fd361982 1979 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
1980 }
1981 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1982 {
049412e3 1983 dwz_file->str.s.section = sectp;
fd361982 1984 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
1985 }
1986 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1987 {
049412e3 1988 dwz_file->line.s.section = sectp;
fd361982 1989 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
1990 }
1991 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1992 {
049412e3 1993 dwz_file->macro.s.section = sectp;
fd361982 1994 dwz_file->macro.size = bfd_section_size (sectp);
36586728 1995 }
2ec9a5e0
TT
1996 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1997 {
049412e3 1998 dwz_file->gdb_index.s.section = sectp;
fd361982 1999 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2000 }
927aa2e7
JK
2001 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2002 {
2003 dwz_file->debug_names.s.section = sectp;
fd361982 2004 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2005 }
36586728
TT
2006}
2007
c4973306 2008/* See dwarf2read.h. */
36586728 2009
c4973306 2010struct dwz_file *
ed2dc618 2011dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2012{
36586728 2013 const char *filename;
acd13123 2014 bfd_size_type buildid_len_arg;
dc294be5
TT
2015 size_t buildid_len;
2016 bfd_byte *buildid;
36586728
TT
2017
2018 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2019 return dwarf2_per_objfile->dwz_file.get ();
36586728 2020
4db1a1dc 2021 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2022 gdb::unique_xmalloc_ptr<char> data
2023 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2024 &buildid_len_arg, &buildid));
4db1a1dc
TT
2025 if (data == NULL)
2026 {
2027 if (bfd_get_error () == bfd_error_no_error)
2028 return NULL;
2029 error (_("could not read '.gnu_debugaltlink' section: %s"),
2030 bfd_errmsg (bfd_get_error ()));
2031 }
791afaa2
TT
2032
2033 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2034
acd13123
TT
2035 buildid_len = (size_t) buildid_len_arg;
2036
791afaa2 2037 filename = data.get ();
d721ba37
PA
2038
2039 std::string abs_storage;
36586728
TT
2040 if (!IS_ABSOLUTE_PATH (filename))
2041 {
14278e1f
TT
2042 gdb::unique_xmalloc_ptr<char> abs
2043 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2044
14278e1f 2045 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2046 filename = abs_storage.c_str ();
36586728
TT
2047 }
2048
dc294be5
TT
2049 /* First try the file name given in the section. If that doesn't
2050 work, try to use the build-id instead. */
192b62ce 2051 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2052 if (dwz_bfd != NULL)
36586728 2053 {
192b62ce 2054 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2055 dwz_bfd.reset (nullptr);
36586728
TT
2056 }
2057
dc294be5
TT
2058 if (dwz_bfd == NULL)
2059 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2060
0d79cdc4
AM
2061 if (dwz_bfd == nullptr)
2062 {
2063 gdb::unique_xmalloc_ptr<char> alt_filename;
2064 const char *origname = dwarf2_per_objfile->objfile->original_name;
2065
2066 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2067 buildid_len,
2068 origname,
2069 &alt_filename));
2070
2071 if (fd.get () >= 0)
2072 {
2073 /* File successfully retrieved from server. */
2074 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2075
2076 if (dwz_bfd == nullptr)
2077 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2078 alt_filename.get ());
2079 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2080 dwz_bfd.reset (nullptr);
2081 }
2082 }
2083
dc294be5
TT
2084 if (dwz_bfd == NULL)
2085 error (_("could not find '.gnu_debugaltlink' file for %s"),
2086 objfile_name (dwarf2_per_objfile->objfile));
2087
7ff8cb8c
TT
2088 std::unique_ptr<struct dwz_file> result
2089 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2090
7ff8cb8c
TT
2091 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2092 result.get ());
36586728 2093
7ff8cb8c
TT
2094 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2095 result->dwz_bfd.get ());
2096 dwarf2_per_objfile->dwz_file = std::move (result);
2097 return dwarf2_per_objfile->dwz_file.get ();
36586728 2098}
9291a0cd 2099\f
7b9f3c50
DE
2100/* DWARF quick_symbols_functions support. */
2101
2102/* TUs can share .debug_line entries, and there can be a lot more TUs than
2103 unique line tables, so we maintain a separate table of all .debug_line
2104 derived entries to support the sharing.
2105 All the quick functions need is the list of file names. We discard the
2106 line_header when we're done and don't need to record it here. */
2107struct quick_file_names
2108{
094b34ac
DE
2109 /* The data used to construct the hash key. */
2110 struct stmt_list_hash hash;
7b9f3c50
DE
2111
2112 /* The number of entries in file_names, real_names. */
2113 unsigned int num_file_names;
2114
2115 /* The file names from the line table, after being run through
2116 file_full_name. */
2117 const char **file_names;
2118
2119 /* The file names from the line table after being run through
2120 gdb_realpath. These are computed lazily. */
2121 const char **real_names;
2122};
2123
2124/* When using the index (and thus not using psymtabs), each CU has an
2125 object of this type. This is used to hold information needed by
2126 the various "quick" methods. */
2127struct dwarf2_per_cu_quick_data
2128{
2129 /* The file table. This can be NULL if there was no file table
2130 or it's currently not read in.
2131 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2132 struct quick_file_names *file_names;
2133
2134 /* The corresponding symbol table. This is NULL if symbols for this
2135 CU have not yet been read. */
43f3e411 2136 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2137
2138 /* A temporary mark bit used when iterating over all CUs in
2139 expand_symtabs_matching. */
2140 unsigned int mark : 1;
2141
2142 /* True if we've tried to read the file table and found there isn't one.
2143 There will be no point in trying to read it again next time. */
2144 unsigned int no_file_data : 1;
2145};
2146
094b34ac
DE
2147/* Utility hash function for a stmt_list_hash. */
2148
2149static hashval_t
2150hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2151{
2152 hashval_t v = 0;
2153
2154 if (stmt_list_hash->dwo_unit != NULL)
2155 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2156 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2157 return v;
2158}
2159
2160/* Utility equality function for a stmt_list_hash. */
2161
2162static int
2163eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2164 const struct stmt_list_hash *rhs)
2165{
2166 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2167 return 0;
2168 if (lhs->dwo_unit != NULL
2169 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2170 return 0;
2171
9c541725 2172 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2173}
2174
7b9f3c50
DE
2175/* Hash function for a quick_file_names. */
2176
2177static hashval_t
2178hash_file_name_entry (const void *e)
2179{
9a3c8263
SM
2180 const struct quick_file_names *file_data
2181 = (const struct quick_file_names *) e;
7b9f3c50 2182
094b34ac 2183 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2184}
2185
2186/* Equality function for a quick_file_names. */
2187
2188static int
2189eq_file_name_entry (const void *a, const void *b)
2190{
9a3c8263
SM
2191 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2192 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2193
094b34ac 2194 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2195}
2196
2197/* Delete function for a quick_file_names. */
2198
2199static void
2200delete_file_name_entry (void *e)
2201{
9a3c8263 2202 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2203 int i;
2204
2205 for (i = 0; i < file_data->num_file_names; ++i)
2206 {
2207 xfree ((void*) file_data->file_names[i]);
2208 if (file_data->real_names)
2209 xfree ((void*) file_data->real_names[i]);
2210 }
2211
2212 /* The space for the struct itself lives on objfile_obstack,
2213 so we don't free it here. */
2214}
2215
2216/* Create a quick_file_names hash table. */
2217
5895093f 2218static htab_up
7b9f3c50
DE
2219create_quick_file_names_table (unsigned int nr_initial_entries)
2220{
5895093f
TT
2221 return htab_up (htab_create_alloc (nr_initial_entries,
2222 hash_file_name_entry, eq_file_name_entry,
2223 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2224}
9291a0cd 2225
918dd910
JK
2226/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2227 have to be created afterwards. You should call age_cached_comp_units after
2228 processing PER_CU->CU. dw2_setup must have been already called. */
2229
2230static void
58f0c718 2231load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2232{
3019eac3 2233 if (per_cu->is_debug_types)
e5fe5e75 2234 load_full_type_unit (per_cu);
918dd910 2235 else
58f0c718 2236 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2237
cc12ce38
DE
2238 if (per_cu->cu == NULL)
2239 return; /* Dummy CU. */
2dc860c0
DE
2240
2241 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2242}
2243
a0f42c21 2244/* Read in the symbols for PER_CU. */
2fdf6df6 2245
9291a0cd 2246static void
58f0c718 2247dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2248{
ed2dc618 2249 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2250
f4dc4d17
DE
2251 /* Skip type_unit_groups, reading the type units they contain
2252 is handled elsewhere. */
197400e8 2253 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2254 return;
2255
b303c6f6
AB
2256 /* The destructor of dwarf2_queue_guard frees any entries left on
2257 the queue. After this point we're guaranteed to leave this function
2258 with the dwarf queue empty. */
39856def 2259 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2260
95554aad 2261 if (dwarf2_per_objfile->using_index
43f3e411 2262 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2263 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2264 {
2265 queue_comp_unit (per_cu, language_minimal);
58f0c718 2266 load_cu (per_cu, skip_partial);
89e63ee4
DE
2267
2268 /* If we just loaded a CU from a DWO, and we're working with an index
2269 that may badly handle TUs, load all the TUs in that DWO as well.
2270 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2271 if (!per_cu->is_debug_types
cc12ce38 2272 && per_cu->cu != NULL
89e63ee4
DE
2273 && per_cu->cu->dwo_unit != NULL
2274 && dwarf2_per_objfile->index_table != NULL
2275 && dwarf2_per_objfile->index_table->version <= 7
2276 /* DWP files aren't supported yet. */
ed2dc618 2277 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2278 queue_and_load_all_dwo_tus (per_cu);
95554aad 2279 }
9291a0cd 2280
ed2dc618 2281 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2282
2283 /* Age the cache, releasing compilation units that have not
2284 been used recently. */
ed2dc618 2285 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2286}
2287
2288/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2289 the objfile from which this CU came. Returns the resulting symbol
2290 table. */
2fdf6df6 2291
43f3e411 2292static struct compunit_symtab *
58f0c718 2293dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2294{
ed2dc618
SM
2295 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2296
95554aad 2297 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2298 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2299 {
11ed8cad 2300 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2301 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2302 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2303 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2304 }
f194fefb 2305
43f3e411 2306 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2307}
2308
ff4c9fec 2309/* See declaration. */
f4dc4d17 2310
ff4c9fec
SM
2311dwarf2_per_cu_data *
2312dwarf2_per_objfile::get_cutu (int index)
2313{
b76e467d 2314 if (index >= this->all_comp_units.size ())
ff4c9fec 2315 {
b76e467d 2316 index -= this->all_comp_units.size ();
b2bdb8cf 2317 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2318 return &this->all_type_units[index]->per_cu;
2319 }
f4dc4d17 2320
ff4c9fec
SM
2321 return this->all_comp_units[index];
2322}
f4dc4d17 2323
ff4c9fec 2324/* See declaration. */
2fdf6df6 2325
ff4c9fec
SM
2326dwarf2_per_cu_data *
2327dwarf2_per_objfile::get_cu (int index)
1fd400ff 2328{
b76e467d 2329 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2330
ff4c9fec 2331 return this->all_comp_units[index];
f4dc4d17
DE
2332}
2333
ff4c9fec 2334/* See declaration. */
f4dc4d17 2335
ff4c9fec
SM
2336signatured_type *
2337dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2338{
b2bdb8cf 2339 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2340
ff4c9fec 2341 return this->all_type_units[index];
1fd400ff
TT
2342}
2343
4b514bc8
JK
2344/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2345 objfile_obstack, and constructed with the specified field
2346 values. */
2347
2348static dwarf2_per_cu_data *
ed2dc618 2349create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2350 struct dwarf2_section_info *section,
2351 int is_dwz,
2352 sect_offset sect_off, ULONGEST length)
2353{
ed2dc618 2354 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2355 dwarf2_per_cu_data *the_cu
2356 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2357 struct dwarf2_per_cu_data);
2358 the_cu->sect_off = sect_off;
2359 the_cu->length = length;
e3b94546 2360 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2361 the_cu->section = section;
2362 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2363 struct dwarf2_per_cu_quick_data);
2364 the_cu->is_dwz = is_dwz;
2365 return the_cu;
2366}
2367
2ec9a5e0
TT
2368/* A helper for create_cus_from_index that handles a given list of
2369 CUs. */
2fdf6df6 2370
74a0d9f6 2371static void
12359b5e 2372create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2373 const gdb_byte *cu_list, offset_type n_elements,
2374 struct dwarf2_section_info *section,
b76e467d 2375 int is_dwz)
9291a0cd 2376{
12359b5e 2377 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2378 {
74a0d9f6 2379 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2380
2381 sect_offset sect_off
2382 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2383 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2384 cu_list += 2 * 8;
2385
b76e467d 2386 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2387 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2388 sect_off, length);
b76e467d 2389 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2390 }
9291a0cd
TT
2391}
2392
2ec9a5e0 2393/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2394 the CU objects for this objfile. */
2ec9a5e0 2395
74a0d9f6 2396static void
12359b5e 2397create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2398 const gdb_byte *cu_list, offset_type cu_list_elements,
2399 const gdb_byte *dwz_list, offset_type dwz_elements)
2400{
b76e467d
SM
2401 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2402 dwarf2_per_objfile->all_comp_units.reserve
2403 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2404
12359b5e 2405 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2406 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2407
2408 if (dwz_elements == 0)
74a0d9f6 2409 return;
2ec9a5e0 2410
12359b5e
SM
2411 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2412 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2413 &dwz->info, 1);
2ec9a5e0
TT
2414}
2415
1fd400ff 2416/* Create the signatured type hash table from the index. */
673bfd45 2417
74a0d9f6 2418static void
12359b5e
SM
2419create_signatured_type_table_from_index
2420 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2421 struct dwarf2_section_info *section,
2422 const gdb_byte *bytes,
2423 offset_type elements)
1fd400ff 2424{
12359b5e 2425 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2426
b2bdb8cf
SM
2427 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2428 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2429
298e9637 2430 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2431
12359b5e 2432 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2433 {
52dc124a 2434 struct signatured_type *sig_type;
9c541725 2435 ULONGEST signature;
1fd400ff 2436 void **slot;
9c541725 2437 cu_offset type_offset_in_tu;
1fd400ff 2438
74a0d9f6 2439 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2440 sect_offset sect_off
2441 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2442 type_offset_in_tu
2443 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2444 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2445 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2446 bytes += 3 * 8;
2447
52dc124a 2448 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2449 struct signatured_type);
52dc124a 2450 sig_type->signature = signature;
9c541725 2451 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2452 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2453 sig_type->per_cu.section = section;
9c541725 2454 sig_type->per_cu.sect_off = sect_off;
e3b94546 2455 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2456 sig_type->per_cu.v.quick
1fd400ff
TT
2457 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2458 struct dwarf2_per_cu_quick_data);
2459
b0b6a987 2460 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2461 *slot = sig_type;
1fd400ff 2462
b2bdb8cf 2463 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2464 }
2465
b0b6a987 2466 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2467}
2468
927aa2e7
JK
2469/* Create the signatured type hash table from .debug_names. */
2470
2471static void
2472create_signatured_type_table_from_debug_names
ed2dc618 2473 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2474 const mapped_debug_names &map,
2475 struct dwarf2_section_info *section,
2476 struct dwarf2_section_info *abbrev_section)
2477{
ed2dc618
SM
2478 struct objfile *objfile = dwarf2_per_objfile->objfile;
2479
96b79293
TT
2480 section->read (objfile);
2481 abbrev_section->read (objfile);
927aa2e7 2482
b2bdb8cf
SM
2483 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2484 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2485
298e9637 2486 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2487
2488 for (uint32_t i = 0; i < map.tu_count; ++i)
2489 {
2490 struct signatured_type *sig_type;
927aa2e7 2491 void **slot;
927aa2e7
JK
2492
2493 sect_offset sect_off
2494 = (sect_offset) (extract_unsigned_integer
2495 (map.tu_table_reordered + i * map.offset_size,
2496 map.offset_size,
2497 map.dwarf5_byte_order));
2498
2499 comp_unit_head cu_header;
ed2dc618
SM
2500 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2501 abbrev_section,
927aa2e7
JK
2502 section->buffer + to_underlying (sect_off),
2503 rcuh_kind::TYPE);
2504
2505 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2506 struct signatured_type);
2507 sig_type->signature = cu_header.signature;
2508 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2509 sig_type->per_cu.is_debug_types = 1;
2510 sig_type->per_cu.section = section;
2511 sig_type->per_cu.sect_off = sect_off;
e3b94546 2512 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2513 sig_type->per_cu.v.quick
2514 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2515 struct dwarf2_per_cu_quick_data);
2516
b0b6a987 2517 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2518 *slot = sig_type;
2519
b2bdb8cf 2520 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2521 }
2522
b0b6a987 2523 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2524}
2525
9291a0cd
TT
2526/* Read the address map data from the mapped index, and use it to
2527 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2528
9291a0cd 2529static void
ed2dc618
SM
2530create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2531 struct mapped_index *index)
9291a0cd 2532{
ed2dc618 2533 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2534 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2535 const gdb_byte *iter, *end;
9291a0cd 2536 struct addrmap *mutable_map;
9291a0cd
TT
2537 CORE_ADDR baseaddr;
2538
8268c778
PA
2539 auto_obstack temp_obstack;
2540
9291a0cd
TT
2541 mutable_map = addrmap_create_mutable (&temp_obstack);
2542
f00a2de2
PA
2543 iter = index->address_table.data ();
2544 end = iter + index->address_table.size ();
9291a0cd 2545
b3b3bada 2546 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2547
2548 while (iter < end)
2549 {
2550 ULONGEST hi, lo, cu_index;
2551 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2552 iter += 8;
2553 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2554 iter += 8;
2555 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2556 iter += 4;
f652bce2 2557
24a55014 2558 if (lo > hi)
f652bce2 2559 {
b98664d3 2560 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2561 hex_string (lo), hex_string (hi));
24a55014 2562 continue;
f652bce2 2563 }
24a55014 2564
b76e467d 2565 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2566 {
b98664d3 2567 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2568 (unsigned) cu_index);
24a55014 2569 continue;
f652bce2 2570 }
24a55014 2571
79748972
TT
2572 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2573 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2574 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2575 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2576 }
2577
d320c2b5 2578 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2579 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2580}
2581
927aa2e7
JK
2582/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2583 populate the objfile's psymtabs_addrmap. */
2584
2585static void
ed2dc618 2586create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2587 struct dwarf2_section_info *section)
2588{
ed2dc618 2589 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2590 bfd *abfd = objfile->obfd;
2591 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2592 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2593
2594 auto_obstack temp_obstack;
2595 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2596
2597 std::unordered_map<sect_offset,
2598 dwarf2_per_cu_data *,
2599 gdb::hash_enum<sect_offset>>
2600 debug_info_offset_to_per_cu;
b76e467d 2601 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2602 {
927aa2e7
JK
2603 const auto insertpair
2604 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2605 if (!insertpair.second)
2606 {
2607 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2608 "debug_info_offset %s, ignoring .debug_aranges."),
2609 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2610 return;
2611 }
2612 }
2613
96b79293 2614 section->read (objfile);
927aa2e7
JK
2615
2616 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2617
2618 const gdb_byte *addr = section->buffer;
2619
2620 while (addr < section->buffer + section->size)
2621 {
2622 const gdb_byte *const entry_addr = addr;
2623 unsigned int bytes_read;
2624
2625 const LONGEST entry_length = read_initial_length (abfd, addr,
2626 &bytes_read);
2627 addr += bytes_read;
2628
2629 const gdb_byte *const entry_end = addr + entry_length;
2630 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2631 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2632 if (addr + entry_length > section->buffer + section->size)
2633 {
47e3f474 2634 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2635 "length %s exceeds section length %s, "
2636 "ignoring .debug_aranges."),
47e3f474
TV
2637 objfile_name (objfile),
2638 plongest (entry_addr - section->buffer),
927aa2e7
JK
2639 plongest (bytes_read + entry_length),
2640 pulongest (section->size));
2641 return;
2642 }
2643
2644 /* The version number. */
2645 const uint16_t version = read_2_bytes (abfd, addr);
2646 addr += 2;
2647 if (version != 2)
2648 {
47e3f474 2649 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2650 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2651 objfile_name (objfile),
2652 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2653 return;
2654 }
2655
2656 const uint64_t debug_info_offset
2657 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2658 addr += offset_size;
2659 const auto per_cu_it
2660 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2661 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2662 {
47e3f474 2663 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2664 "debug_info_offset %s does not exists, "
2665 "ignoring .debug_aranges."),
47e3f474
TV
2666 objfile_name (objfile),
2667 plongest (entry_addr - section->buffer),
927aa2e7
JK
2668 pulongest (debug_info_offset));
2669 return;
2670 }
2671 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2672
2673 const uint8_t address_size = *addr++;
2674 if (address_size < 1 || address_size > 8)
2675 {
47e3f474 2676 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2677 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2678 objfile_name (objfile),
2679 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2680 return;
2681 }
2682
2683 const uint8_t segment_selector_size = *addr++;
2684 if (segment_selector_size != 0)
2685 {
47e3f474 2686 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2687 "segment_selector_size %u is not supported, "
2688 "ignoring .debug_aranges."),
47e3f474
TV
2689 objfile_name (objfile),
2690 plongest (entry_addr - section->buffer),
927aa2e7
JK
2691 segment_selector_size);
2692 return;
2693 }
2694
2695 /* Must pad to an alignment boundary that is twice the address
2696 size. It is undocumented by the DWARF standard but GCC does
2697 use it. */
2698 for (size_t padding = ((-(addr - section->buffer))
2699 & (2 * address_size - 1));
2700 padding > 0; padding--)
2701 if (*addr++ != 0)
2702 {
47e3f474 2703 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2704 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2705 objfile_name (objfile),
2706 plongest (entry_addr - section->buffer));
927aa2e7
JK
2707 return;
2708 }
2709
2710 for (;;)
2711 {
2712 if (addr + 2 * address_size > entry_end)
2713 {
47e3f474 2714 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2715 "address list is not properly terminated, "
2716 "ignoring .debug_aranges."),
47e3f474
TV
2717 objfile_name (objfile),
2718 plongest (entry_addr - section->buffer));
927aa2e7
JK
2719 return;
2720 }
2721 ULONGEST start = extract_unsigned_integer (addr, address_size,
2722 dwarf5_byte_order);
2723 addr += address_size;
2724 ULONGEST length = extract_unsigned_integer (addr, address_size,
2725 dwarf5_byte_order);
2726 addr += address_size;
2727 if (start == 0 && length == 0)
2728 break;
2729 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2730 {
2731 /* Symbol was eliminated due to a COMDAT group. */
2732 continue;
2733 }
2734 ULONGEST end = start + length;
79748972
TT
2735 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2736 - baseaddr);
2737 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2738 - baseaddr);
927aa2e7
JK
2739 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2740 }
2741 }
2742
d320c2b5 2743 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2744 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2745}
2746
9291a0cd
TT
2747/* Find a slot in the mapped index INDEX for the object named NAME.
2748 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2749 constant pool and return true. If NAME cannot be found, return
2750 false. */
2fdf6df6 2751
109483d9 2752static bool
9291a0cd
TT
2753find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2754 offset_type **vec_out)
2755{
0cf03b49 2756 offset_type hash;
9291a0cd 2757 offset_type slot, step;
559a7a62 2758 int (*cmp) (const char *, const char *);
9291a0cd 2759
791afaa2 2760 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2761 if (current_language->la_language == language_cplus
45280282
IB
2762 || current_language->la_language == language_fortran
2763 || current_language->la_language == language_d)
0cf03b49
JK
2764 {
2765 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2766 not contain any. */
a8719064 2767
72998fb3 2768 if (strchr (name, '(') != NULL)
0cf03b49 2769 {
109483d9 2770 without_params = cp_remove_params (name);
0cf03b49 2771
72998fb3 2772 if (without_params != NULL)
791afaa2 2773 name = without_params.get ();
0cf03b49
JK
2774 }
2775 }
2776
559a7a62 2777 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2778 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2779 simulate our NAME being searched is also lowercased. */
2780 hash = mapped_index_string_hash ((index->version == 4
2781 && case_sensitivity == case_sensitive_off
2782 ? 5 : index->version),
2783 name);
2784
f00a2de2
PA
2785 slot = hash & (index->symbol_table.size () - 1);
2786 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2787 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2788
2789 for (;;)
2790 {
9291a0cd 2791 const char *str;
f00a2de2
PA
2792
2793 const auto &bucket = index->symbol_table[slot];
2794 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2795 return false;
9291a0cd 2796
f00a2de2 2797 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2798 if (!cmp (name, str))
9291a0cd
TT
2799 {
2800 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2801 + MAYBE_SWAP (bucket.vec));
109483d9 2802 return true;
9291a0cd
TT
2803 }
2804
f00a2de2 2805 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2806 }
2807}
2808
4485a1c1
SM
2809/* A helper function that reads the .gdb_index from BUFFER and fills
2810 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2811 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2812 ok to use deprecated sections.
2813
2814 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2815 out parameters that are filled in with information about the CU and
2816 TU lists in the section.
2817
4485a1c1 2818 Returns true if all went well, false otherwise. */
2fdf6df6 2819
d33bc52e 2820static bool
4485a1c1
SM
2821read_gdb_index_from_buffer (struct objfile *objfile,
2822 const char *filename,
2823 bool deprecated_ok,
2824 gdb::array_view<const gdb_byte> buffer,
2825 struct mapped_index *map,
2826 const gdb_byte **cu_list,
2827 offset_type *cu_list_elements,
2828 const gdb_byte **types_list,
2829 offset_type *types_list_elements)
2830{
2831 const gdb_byte *addr = &buffer[0];
82430852 2832
9291a0cd 2833 /* Version check. */
4485a1c1 2834 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2835 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2836 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2837 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2838 indices. */
831adc1f 2839 if (version < 4)
481860b3
GB
2840 {
2841 static int warning_printed = 0;
2842 if (!warning_printed)
2843 {
2844 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2845 filename);
481860b3
GB
2846 warning_printed = 1;
2847 }
2848 return 0;
2849 }
2850 /* Index version 4 uses a different hash function than index version
2851 5 and later.
2852
2853 Versions earlier than 6 did not emit psymbols for inlined
2854 functions. Using these files will cause GDB not to be able to
2855 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2856 indices unless the user has done
2857 "set use-deprecated-index-sections on". */
2ec9a5e0 2858 if (version < 6 && !deprecated_ok)
481860b3
GB
2859 {
2860 static int warning_printed = 0;
2861 if (!warning_printed)
2862 {
e615022a
DE
2863 warning (_("\
2864Skipping deprecated .gdb_index section in %s.\n\
2865Do \"set use-deprecated-index-sections on\" before the file is read\n\
2866to use the section anyway."),
2ec9a5e0 2867 filename);
481860b3
GB
2868 warning_printed = 1;
2869 }
2870 return 0;
2871 }
796a7ff8 2872 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2873 of the TU (for symbols coming from TUs),
2874 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2875 Plus gold-generated indices can have duplicate entries for global symbols,
2876 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2877 These are just performance bugs, and we can't distinguish gdb-generated
2878 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2879
481860b3 2880 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2881 longer backward compatible. */
796a7ff8 2882 if (version > 8)
594e8718 2883 return 0;
9291a0cd 2884
559a7a62 2885 map->version = version;
9291a0cd 2886
4485a1c1 2887 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2888
4485a1c1 2889 int i = 0;
2ec9a5e0
TT
2890 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2891 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2892 / 8);
1fd400ff
TT
2893 ++i;
2894
2ec9a5e0
TT
2895 *types_list = addr + MAYBE_SWAP (metadata[i]);
2896 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2897 - MAYBE_SWAP (metadata[i]))
2898 / 8);
987d643c 2899 ++i;
1fd400ff 2900
f00a2de2
PA
2901 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2902 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2903 map->address_table
2904 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2905 ++i;
2906
f00a2de2
PA
2907 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2908 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2909 map->symbol_table
2910 = gdb::array_view<mapped_index::symbol_table_slot>
2911 ((mapped_index::symbol_table_slot *) symbol_table,
2912 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2913
f00a2de2 2914 ++i;
f9d83a0b 2915 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2916
2ec9a5e0
TT
2917 return 1;
2918}
2919
4485a1c1
SM
2920/* Callback types for dwarf2_read_gdb_index. */
2921
2922typedef gdb::function_view
2923 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2924 get_gdb_index_contents_ftype;
2925typedef gdb::function_view
2926 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2927 get_gdb_index_contents_dwz_ftype;
2928
927aa2e7 2929/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2930 elements of all the CUs and return 1. Otherwise, return 0. */
2931
2932static int
4485a1c1
SM
2933dwarf2_read_gdb_index
2934 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2935 get_gdb_index_contents_ftype get_gdb_index_contents,
2936 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2937{
2ec9a5e0
TT
2938 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2939 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2940 struct dwz_file *dwz;
12359b5e 2941 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 2942
4485a1c1
SM
2943 gdb::array_view<const gdb_byte> main_index_contents
2944 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2945
2946 if (main_index_contents.empty ())
2947 return 0;
2948
3063847f 2949 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
2950 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2951 use_deprecated_index_sections,
2952 main_index_contents, map.get (), &cu_list,
2953 &cu_list_elements, &types_list,
2954 &types_list_elements))
2ec9a5e0
TT
2955 return 0;
2956
0fefef59 2957 /* Don't use the index if it's empty. */
3063847f 2958 if (map->symbol_table.empty ())
0fefef59
DE
2959 return 0;
2960
2ec9a5e0
TT
2961 /* If there is a .dwz file, read it so we can get its CU list as
2962 well. */
ed2dc618 2963 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 2964 if (dwz != NULL)
2ec9a5e0 2965 {
2ec9a5e0
TT
2966 struct mapped_index dwz_map;
2967 const gdb_byte *dwz_types_ignore;
2968 offset_type dwz_types_elements_ignore;
2969
4485a1c1
SM
2970 gdb::array_view<const gdb_byte> dwz_index_content
2971 = get_gdb_index_contents_dwz (objfile, dwz);
2972
2973 if (dwz_index_content.empty ())
2974 return 0;
2975
2976 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
2977 bfd_get_filename (dwz->dwz_bfd.get ()),
2978 1, dwz_index_content, &dwz_map,
4485a1c1
SM
2979 &dwz_list, &dwz_list_elements,
2980 &dwz_types_ignore,
2981 &dwz_types_elements_ignore))
2ec9a5e0
TT
2982 {
2983 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 2984 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
2985 return 0;
2986 }
2987 }
2988
12359b5e
SM
2989 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
2990 dwz_list, dwz_list_elements);
1fd400ff 2991
8b70b953
TT
2992 if (types_list_elements)
2993 {
8b70b953
TT
2994 /* We can only handle a single .debug_types when we have an
2995 index. */
fd5866f6 2996 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
2997 return 0;
2998
fd5866f6 2999 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3000
12359b5e
SM
3001 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3002 types_list, types_list_elements);
8b70b953 3003 }
9291a0cd 3004
3063847f 3005 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3006
3063847f 3007 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3008 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3009 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3010 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3011
3012 return 1;
3013}
3014
dee91e82 3015/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3016
dee91e82
DE
3017static void
3018dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3019 const gdb_byte *info_ptr,
3e225074 3020 struct die_info *comp_unit_die)
9291a0cd 3021{
dee91e82 3022 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3023 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3024 struct dwarf2_per_objfile *dwarf2_per_objfile
3025 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3026 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3027 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3028 struct attribute *attr;
7b9f3c50
DE
3029 void **slot;
3030 struct quick_file_names *qfn;
9291a0cd 3031
0186c6a7
DE
3032 gdb_assert (! this_cu->is_debug_types);
3033
07261596
TT
3034 /* Our callers never want to match partial units -- instead they
3035 will match the enclosing full CU. */
3036 if (comp_unit_die->tag == DW_TAG_partial_unit)
3037 {
3038 this_cu->v.quick->no_file_data = 1;
3039 return;
3040 }
3041
0186c6a7 3042 lh_cu = this_cu;
7b9f3c50 3043 slot = NULL;
dee91e82 3044
fff8551c 3045 line_header_up lh;
9c541725 3046 sect_offset line_offset {};
fff8551c 3047
dee91e82 3048 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3049 if (attr != nullptr)
9291a0cd 3050 {
7b9f3c50
DE
3051 struct quick_file_names find_entry;
3052
9c541725 3053 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3054
3055 /* We may have already read in this line header (TU line header sharing).
3056 If we have we're done. */
094b34ac 3057 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3058 find_entry.hash.line_sect_off = line_offset;
5895093f 3059 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3060 &find_entry, INSERT);
3061 if (*slot != NULL)
3062 {
9a3c8263 3063 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3064 return;
7b9f3c50
DE
3065 }
3066
3019eac3 3067 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3068 }
3069 if (lh == NULL)
3070 {
094b34ac 3071 lh_cu->v.quick->no_file_data = 1;
dee91e82 3072 return;
9291a0cd
TT
3073 }
3074
8d749320 3075 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3076 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3077 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3078 gdb_assert (slot != NULL);
3079 *slot = qfn;
9291a0cd 3080
d721ba37 3081 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3082
aa391654
TT
3083 int offset = 0;
3084 if (strcmp (fnd.name, "<unknown>") != 0)
3085 ++offset;
3086
7ba99d21 3087 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3088 qfn->file_names =
aa391654
TT
3089 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3090 if (offset != 0)
3091 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3092 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3093 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3094 fnd.comp_dir).release ();
7b9f3c50 3095 qfn->real_names = NULL;
9291a0cd 3096
094b34ac 3097 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3098}
3099
3100/* A helper for the "quick" functions which attempts to read the line
3101 table for THIS_CU. */
3102
3103static struct quick_file_names *
e4a48d9d 3104dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3105{
0186c6a7
DE
3106 /* This should never be called for TUs. */
3107 gdb_assert (! this_cu->is_debug_types);
3108 /* Nor type unit groups. */
197400e8 3109 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3110
dee91e82
DE
3111 if (this_cu->v.quick->file_names != NULL)
3112 return this_cu->v.quick->file_names;
3113 /* If we know there is no line data, no point in looking again. */
3114 if (this_cu->v.quick->no_file_data)
3115 return NULL;
3116
c0ab21c2
TT
3117 cutu_reader reader (this_cu);
3118 if (!reader.dummy_p)
3e225074 3119 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3120
3121 if (this_cu->v.quick->no_file_data)
3122 return NULL;
3123 return this_cu->v.quick->file_names;
9291a0cd
TT
3124}
3125
3126/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3127 real path for a given file name from the line table. */
2fdf6df6 3128
9291a0cd 3129static const char *
7b9f3c50
DE
3130dw2_get_real_path (struct objfile *objfile,
3131 struct quick_file_names *qfn, int index)
9291a0cd 3132{
7b9f3c50
DE
3133 if (qfn->real_names == NULL)
3134 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3135 qfn->num_file_names, const char *);
9291a0cd 3136
7b9f3c50 3137 if (qfn->real_names[index] == NULL)
14278e1f 3138 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3139
7b9f3c50 3140 return qfn->real_names[index];
9291a0cd
TT
3141}
3142
3143static struct symtab *
3144dw2_find_last_source_symtab (struct objfile *objfile)
3145{
ed2dc618
SM
3146 struct dwarf2_per_objfile *dwarf2_per_objfile
3147 = get_dwarf2_per_objfile (objfile);
b76e467d 3148 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3149 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3150
43f3e411
DE
3151 if (cust == NULL)
3152 return NULL;
ed2dc618 3153
43f3e411 3154 return compunit_primary_filetab (cust);
9291a0cd
TT
3155}
3156
7b9f3c50
DE
3157/* Traversal function for dw2_forget_cached_source_info. */
3158
3159static int
3160dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3161{
7b9f3c50 3162 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3163
7b9f3c50 3164 if (file_data->real_names)
9291a0cd 3165 {
7b9f3c50 3166 int i;
9291a0cd 3167
7b9f3c50 3168 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3169 {
7b9f3c50
DE
3170 xfree ((void*) file_data->real_names[i]);
3171 file_data->real_names[i] = NULL;
9291a0cd
TT
3172 }
3173 }
7b9f3c50
DE
3174
3175 return 1;
3176}
3177
3178static void
3179dw2_forget_cached_source_info (struct objfile *objfile)
3180{
ed2dc618
SM
3181 struct dwarf2_per_objfile *dwarf2_per_objfile
3182 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3183
5895093f 3184 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3185 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3186}
3187
f8eba3c6
TT
3188/* Helper function for dw2_map_symtabs_matching_filename that expands
3189 the symtabs and calls the iterator. */
3190
3191static int
3192dw2_map_expand_apply (struct objfile *objfile,
3193 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3194 const char *name, const char *real_path,
14bc53a8 3195 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3196{
43f3e411 3197 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3198
3199 /* Don't visit already-expanded CUs. */
43f3e411 3200 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3201 return 0;
3202
3203 /* This may expand more than one symtab, and we want to iterate over
3204 all of them. */
58f0c718 3205 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3206
14bc53a8
PA
3207 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3208 last_made, callback);
f8eba3c6
TT
3209}
3210
3211/* Implementation of the map_symtabs_matching_filename method. */
3212
14bc53a8
PA
3213static bool
3214dw2_map_symtabs_matching_filename
3215 (struct objfile *objfile, const char *name, const char *real_path,
3216 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3217{
c011a4f4 3218 const char *name_basename = lbasename (name);
ed2dc618
SM
3219 struct dwarf2_per_objfile *dwarf2_per_objfile
3220 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3221
848e3e78
DE
3222 /* The rule is CUs specify all the files, including those used by
3223 any TU, so there's no need to scan TUs here. */
f4dc4d17 3224
b76e467d 3225 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3226 {
3d7bb9d9 3227 /* We only need to look at symtabs not already expanded. */
43f3e411 3228 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3229 continue;
3230
b76e467d 3231 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3232 if (file_data == NULL)
9291a0cd
TT
3233 continue;
3234
b76e467d 3235 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3236 {
7b9f3c50 3237 const char *this_name = file_data->file_names[j];
da235a7c 3238 const char *this_real_name;
9291a0cd 3239
af529f8f 3240 if (compare_filenames_for_search (this_name, name))
9291a0cd 3241 {
f5b95b50 3242 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3243 callback))
3244 return true;
288e77a7 3245 continue;
4aac40c8 3246 }
9291a0cd 3247
c011a4f4
DE
3248 /* Before we invoke realpath, which can get expensive when many
3249 files are involved, do a quick comparison of the basenames. */
3250 if (! basenames_may_differ
3251 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3252 continue;
3253
da235a7c
JK
3254 this_real_name = dw2_get_real_path (objfile, file_data, j);
3255 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3256 {
da235a7c 3257 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3258 callback))
3259 return true;
288e77a7 3260 continue;
da235a7c 3261 }
9291a0cd 3262
da235a7c
JK
3263 if (real_path != NULL)
3264 {
af529f8f
JK
3265 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3266 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3267 if (this_real_name != NULL
af529f8f 3268 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3269 {
f5b95b50 3270 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3271 callback))
3272 return true;
288e77a7 3273 continue;
9291a0cd
TT
3274 }
3275 }
3276 }
3277 }
3278
14bc53a8 3279 return false;
9291a0cd
TT
3280}
3281
da51c347
DE
3282/* Struct used to manage iterating over all CUs looking for a symbol. */
3283
3284struct dw2_symtab_iterator
9291a0cd 3285{
ed2dc618
SM
3286 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3287 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3288 /* If set, only look for symbols that match that block. Valid values are
3289 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3290 gdb::optional<block_enum> block_index;
da51c347
DE
3291 /* The kind of symbol we're looking for. */
3292 domain_enum domain;
3293 /* The list of CUs from the index entry of the symbol,
3294 or NULL if not found. */
3295 offset_type *vec;
3296 /* The next element in VEC to look at. */
3297 int next;
3298 /* The number of elements in VEC, or zero if there is no match. */
3299 int length;
8943b874
DE
3300 /* Have we seen a global version of the symbol?
3301 If so we can ignore all further global instances.
3302 This is to work around gold/15646, inefficient gold-generated
3303 indices. */
3304 int global_seen;
da51c347 3305};
9291a0cd 3306
2b79f376 3307/* Initialize the index symtab iterator ITER. */
2fdf6df6 3308
9291a0cd 3309static void
da51c347 3310dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3311 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3312 gdb::optional<block_enum> block_index,
da51c347
DE
3313 domain_enum domain,
3314 const char *name)
3315{
ed2dc618 3316 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3317 iter->block_index = block_index;
3318 iter->domain = domain;
3319 iter->next = 0;
8943b874 3320 iter->global_seen = 0;
da51c347 3321
3063847f 3322 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3323
3324 /* index is NULL if OBJF_READNOW. */
3325 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3326 iter->length = MAYBE_SWAP (*iter->vec);
3327 else
3328 {
3329 iter->vec = NULL;
3330 iter->length = 0;
3331 }
3332}
3333
3334/* Return the next matching CU or NULL if there are no more. */
3335
3336static struct dwarf2_per_cu_data *
3337dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3338{
ed2dc618
SM
3339 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3340
da51c347
DE
3341 for ( ; iter->next < iter->length; ++iter->next)
3342 {
3343 offset_type cu_index_and_attrs =
3344 MAYBE_SWAP (iter->vec[iter->next + 1]);
3345 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3346 gdb_index_symbol_kind symbol_kind =
3347 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3348 /* Only check the symbol attributes if they're present.
3349 Indices prior to version 7 don't record them,
3350 and indices >= 7 may elide them for certain symbols
3351 (gold does this). */
3352 int attrs_valid =
ed2dc618 3353 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3354 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3355
3190f0c6 3356 /* Don't crash on bad data. */
b76e467d 3357 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3358 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3359 {
b98664d3 3360 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3361 " [in module %s]"),
3362 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3363 continue;
3364 }
3365
ff4c9fec 3366 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3367
da51c347 3368 /* Skip if already read in. */
43f3e411 3369 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3370 continue;
3371
8943b874
DE
3372 /* Check static vs global. */
3373 if (attrs_valid)
3374 {
2b79f376
SM
3375 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3376
3377 if (iter->block_index.has_value ())
3378 {
3379 bool want_static = *iter->block_index == STATIC_BLOCK;
3380
3381 if (is_static != want_static)
3382 continue;
3383 }
3384
8943b874
DE
3385 /* Work around gold/15646. */
3386 if (!is_static && iter->global_seen)
3387 continue;
3388 if (!is_static)
3389 iter->global_seen = 1;
3390 }
da51c347
DE
3391
3392 /* Only check the symbol's kind if it has one. */
3393 if (attrs_valid)
3394 {
3395 switch (iter->domain)
3396 {
3397 case VAR_DOMAIN:
3398 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3399 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3400 /* Some types are also in VAR_DOMAIN. */
3401 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3402 continue;
3403 break;
3404 case STRUCT_DOMAIN:
3405 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3406 continue;
3407 break;
3408 case LABEL_DOMAIN:
3409 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3410 continue;
3411 break;
59c35742
AB
3412 case MODULE_DOMAIN:
3413 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3414 continue;
3415 break;
da51c347
DE
3416 default:
3417 break;
3418 }
3419 }
3420
3421 ++iter->next;
3422 return per_cu;
3423 }
3424
3425 return NULL;
3426}
3427
43f3e411 3428static struct compunit_symtab *
c7f839cb 3429dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3430 const char *name, domain_enum domain)
9291a0cd 3431{
43f3e411 3432 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3433 struct dwarf2_per_objfile *dwarf2_per_objfile
3434 = get_dwarf2_per_objfile (objfile);
9291a0cd 3435
b5ec771e
PA
3436 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3437
ed2dc618
SM
3438 struct dw2_symtab_iterator iter;
3439 struct dwarf2_per_cu_data *per_cu;
da51c347 3440
2b79f376 3441 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3442
ed2dc618
SM
3443 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3444 {
3445 struct symbol *sym, *with_opaque = NULL;
58f0c718 3446 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3447 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3448 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3449
ed2dc618
SM
3450 sym = block_find_symbol (block, name, domain,
3451 block_find_non_opaque_type_preferred,
3452 &with_opaque);
b2e2f908 3453
ed2dc618
SM
3454 /* Some caution must be observed with overloaded functions
3455 and methods, since the index will not contain any overload
3456 information (but NAME might contain it). */
da51c347 3457
ed2dc618
SM
3458 if (sym != NULL
3459 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3460 return stab;
3461 if (with_opaque != NULL
3462 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3463 stab_best = stab;
da51c347 3464
ed2dc618 3465 /* Keep looking through other CUs. */
9291a0cd 3466 }
9291a0cd 3467
da51c347 3468 return stab_best;
9291a0cd
TT
3469}
3470
3471static void
3472dw2_print_stats (struct objfile *objfile)
3473{
ed2dc618
SM
3474 struct dwarf2_per_objfile *dwarf2_per_objfile
3475 = get_dwarf2_per_objfile (objfile);
b76e467d 3476 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3477 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3478 int count = 0;
9291a0cd 3479
ed2dc618 3480 for (int i = 0; i < total; ++i)
9291a0cd 3481 {
ff4c9fec 3482 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3483
43f3e411 3484 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3485 ++count;
3486 }
e4a48d9d 3487 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3488 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3489}
3490
779bd270
DE
3491/* This dumps minimal information about the index.
3492 It is called via "mt print objfiles".
3493 One use is to verify .gdb_index has been loaded by the
3494 gdb.dwarf2/gdb-index.exp testcase. */
3495
9291a0cd
TT
3496static void
3497dw2_dump (struct objfile *objfile)
3498{
ed2dc618
SM
3499 struct dwarf2_per_objfile *dwarf2_per_objfile
3500 = get_dwarf2_per_objfile (objfile);
3501
779bd270
DE
3502 gdb_assert (dwarf2_per_objfile->using_index);
3503 printf_filtered (".gdb_index:");
3504 if (dwarf2_per_objfile->index_table != NULL)
3505 {
3506 printf_filtered (" version %d\n",
3507 dwarf2_per_objfile->index_table->version);
3508 }
3509 else
3510 printf_filtered (" faked for \"readnow\"\n");
3511 printf_filtered ("\n");
9291a0cd
TT
3512}
3513
9291a0cd
TT
3514static void
3515dw2_expand_symtabs_for_function (struct objfile *objfile,
3516 const char *func_name)
3517{
ed2dc618
SM
3518 struct dwarf2_per_objfile *dwarf2_per_objfile
3519 = get_dwarf2_per_objfile (objfile);
da51c347 3520
ed2dc618
SM
3521 struct dw2_symtab_iterator iter;
3522 struct dwarf2_per_cu_data *per_cu;
da51c347 3523
2b79f376 3524 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3525
ed2dc618 3526 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3527 dw2_instantiate_symtab (per_cu, false);
da51c347 3528
9291a0cd
TT
3529}
3530
3531static void
3532dw2_expand_all_symtabs (struct objfile *objfile)
3533{
ed2dc618
SM
3534 struct dwarf2_per_objfile *dwarf2_per_objfile
3535 = get_dwarf2_per_objfile (objfile);
b76e467d 3536 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3537 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3538
ed2dc618 3539 for (int i = 0; i < total_units; ++i)
9291a0cd 3540 {
ff4c9fec 3541 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3542
58f0c718
TT
3543 /* We don't want to directly expand a partial CU, because if we
3544 read it with the wrong language, then assertion failures can
3545 be triggered later on. See PR symtab/23010. So, tell
3546 dw2_instantiate_symtab to skip partial CUs -- any important
3547 partial CU will be read via DW_TAG_imported_unit anyway. */
3548 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3549 }
3550}
3551
3552static void
652a8996
JK
3553dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3554 const char *fullname)
9291a0cd 3555{
ed2dc618
SM
3556 struct dwarf2_per_objfile *dwarf2_per_objfile
3557 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3558
3559 /* We don't need to consider type units here.
3560 This is only called for examining code, e.g. expand_line_sal.
3561 There can be an order of magnitude (or more) more type units
3562 than comp units, and we avoid them if we can. */
3563
b76e467d 3564 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3565 {
3d7bb9d9 3566 /* We only need to look at symtabs not already expanded. */
43f3e411 3567 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3568 continue;
3569
b76e467d 3570 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3571 if (file_data == NULL)
9291a0cd
TT
3572 continue;
3573
b76e467d 3574 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3575 {
652a8996
JK
3576 const char *this_fullname = file_data->file_names[j];
3577
3578 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3579 {
58f0c718 3580 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3581 break;
3582 }
3583 }
3584 }
3585}
3586
9291a0cd 3587static void
199b4314
TT
3588dw2_map_matching_symbols
3589 (struct objfile *objfile,
b054970d 3590 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3591 int global,
3592 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3593 symbol_compare_ftype *ordered_compare)
9291a0cd 3594{
40658b94 3595 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3596 current language is Ada for a non-Ada objfile using GNU index. As Ada
3597 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3598}
3599
e1ef7d7a
PA
3600/* Starting from a search name, return the string that finds the upper
3601 bound of all strings that start with SEARCH_NAME in a sorted name
3602 list. Returns the empty string to indicate that the upper bound is
3603 the end of the list. */
3604
3605static std::string
3606make_sort_after_prefix_name (const char *search_name)
3607{
3608 /* When looking to complete "func", we find the upper bound of all
3609 symbols that start with "func" by looking for where we'd insert
3610 the closest string that would follow "func" in lexicographical
3611 order. Usually, that's "func"-with-last-character-incremented,
3612 i.e. "fund". Mind non-ASCII characters, though. Usually those
3613 will be UTF-8 multi-byte sequences, but we can't be certain.
3614 Especially mind the 0xff character, which is a valid character in
3615 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3616 rule out compilers allowing it in identifiers. Note that
3617 conveniently, strcmp/strcasecmp are specified to compare
3618 characters interpreted as unsigned char. So what we do is treat
3619 the whole string as a base 256 number composed of a sequence of
3620 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3621 to 0, and carries 1 to the following more-significant position.
3622 If the very first character in SEARCH_NAME ends up incremented
3623 and carries/overflows, then the upper bound is the end of the
3624 list. The string after the empty string is also the empty
3625 string.
3626
3627 Some examples of this operation:
3628
3629 SEARCH_NAME => "+1" RESULT
3630
3631 "abc" => "abd"
3632 "ab\xff" => "ac"
3633 "\xff" "a" "\xff" => "\xff" "b"
3634 "\xff" => ""
3635 "\xff\xff" => ""
3636 "" => ""
3637
3638 Then, with these symbols for example:
3639
3640 func
3641 func1
3642 fund
3643
3644 completing "func" looks for symbols between "func" and
3645 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3646 which finds "func" and "func1", but not "fund".
3647
3648 And with:
3649
3650 funcÿ (Latin1 'ÿ' [0xff])
3651 funcÿ1
3652 fund
3653
3654 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3655 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3656
3657 And with:
3658
3659 ÿÿ (Latin1 'ÿ' [0xff])
3660 ÿÿ1
3661
3662 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3663 the end of the list.
3664 */
3665 std::string after = search_name;
3666 while (!after.empty () && (unsigned char) after.back () == 0xff)
3667 after.pop_back ();
3668 if (!after.empty ())
3669 after.back () = (unsigned char) after.back () + 1;
3670 return after;
3671}
3672
5c58de74 3673/* See declaration. */
61d96d7e 3674
5c58de74
PA
3675std::pair<std::vector<name_component>::const_iterator,
3676 std::vector<name_component>::const_iterator>
44ed8f3e 3677mapped_index_base::find_name_components_bounds
3b00ef10 3678 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3679{
5c58de74
PA
3680 auto *name_cmp
3681 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3682
3b00ef10
TT
3683 const char *lang_name
3684 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3685
3f563c84
PA
3686 /* Comparison function object for lower_bound that matches against a
3687 given symbol name. */
3688 auto lookup_compare_lower = [&] (const name_component &elem,
3689 const char *name)
3690 {
5c58de74 3691 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3692 const char *elem_name = elem_qualified + elem.name_offset;
3693 return name_cmp (elem_name, name) < 0;
3694 };
3695
3696 /* Comparison function object for upper_bound that matches against a
3697 given symbol name. */
3698 auto lookup_compare_upper = [&] (const char *name,
3699 const name_component &elem)
3700 {
5c58de74 3701 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3702 const char *elem_name = elem_qualified + elem.name_offset;
3703 return name_cmp (name, elem_name) < 0;
3704 };
3705
5c58de74
PA
3706 auto begin = this->name_components.begin ();
3707 auto end = this->name_components.end ();
3f563c84
PA
3708
3709 /* Find the lower bound. */
3710 auto lower = [&] ()
3711 {
3b00ef10 3712 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3713 return begin;
3714 else
3b00ef10 3715 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3716 } ();
3717
3718 /* Find the upper bound. */
3719 auto upper = [&] ()
3720 {
5c58de74 3721 if (lookup_name_without_params.completion_mode ())
3f563c84 3722 {
e1ef7d7a
PA
3723 /* In completion mode, we want UPPER to point past all
3724 symbols names that have the same prefix. I.e., with
3725 these symbols, and completing "func":
3726
3727 function << lower bound
3728 function1
3729 other_function << upper bound
3730
3731 We find the upper bound by looking for the insertion
3732 point of "func"-with-last-character-incremented,
3733 i.e. "fund". */
3b00ef10 3734 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3735 if (after.empty ())
3f563c84 3736 return end;
e6b2f5ef
PA
3737 return std::lower_bound (lower, end, after.c_str (),
3738 lookup_compare_lower);
3f563c84
PA
3739 }
3740 else
3b00ef10 3741 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3742 } ();
3743
5c58de74
PA
3744 return {lower, upper};
3745}
3746
3747/* See declaration. */
3748
3749void
44ed8f3e 3750mapped_index_base::build_name_components ()
5c58de74
PA
3751{
3752 if (!this->name_components.empty ())
3753 return;
3754
3755 this->name_components_casing = case_sensitivity;
3756 auto *name_cmp
3757 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3758
3759 /* The code below only knows how to break apart components of C++
3760 symbol names (and other languages that use '::' as
3b00ef10 3761 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3762 auto count = this->symbol_name_count ();
3763 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3764 {
44ed8f3e 3765 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3766 continue;
3767
3768 const char *name = this->symbol_name_at (idx);
3769
3770 /* Add each name component to the name component table. */
3771 unsigned int previous_len = 0;
3b00ef10
TT
3772
3773 if (strstr (name, "::") != nullptr)
3774 {
3775 for (unsigned int current_len = cp_find_first_component (name);
3776 name[current_len] != '\0';
3777 current_len += cp_find_first_component (name + current_len))
3778 {
3779 gdb_assert (name[current_len] == ':');
3780 this->name_components.push_back ({previous_len, idx});
3781 /* Skip the '::'. */
3782 current_len += 2;
3783 previous_len = current_len;
3784 }
3785 }
3786 else
5c58de74 3787 {
3b00ef10
TT
3788 /* Handle the Ada encoded (aka mangled) form here. */
3789 for (const char *iter = strstr (name, "__");
3790 iter != nullptr;
3791 iter = strstr (iter, "__"))
3792 {
3793 this->name_components.push_back ({previous_len, idx});
3794 iter += 2;
3795 previous_len = iter - name;
3796 }
5c58de74 3797 }
3b00ef10 3798
5c58de74
PA
3799 this->name_components.push_back ({previous_len, idx});
3800 }
3801
3802 /* Sort name_components elements by name. */
3803 auto name_comp_compare = [&] (const name_component &left,
3804 const name_component &right)
3805 {
3806 const char *left_qualified = this->symbol_name_at (left.idx);
3807 const char *right_qualified = this->symbol_name_at (right.idx);
3808
3809 const char *left_name = left_qualified + left.name_offset;
3810 const char *right_name = right_qualified + right.name_offset;
3811
3812 return name_cmp (left_name, right_name) < 0;
3813 };
3814
3815 std::sort (this->name_components.begin (),
3816 this->name_components.end (),
3817 name_comp_compare);
3818}
3819
3820/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3821 mapped_index_base instead of the containing objfile. This is split
3822 to a separate function in order to be able to unit test the
3823 name_components matching using a mock mapped_index_base. For each
5c58de74 3824 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3825 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3826
3827static void
3828dw2_expand_symtabs_matching_symbol
44ed8f3e 3829 (mapped_index_base &index,
5c58de74
PA
3830 const lookup_name_info &lookup_name_in,
3831 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3832 enum search_domain kind,
3b00ef10 3833 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3834{
3835 lookup_name_info lookup_name_without_params
3836 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3837
3838 /* Build the symbol name component sorted vector, if we haven't
3839 yet. */
3840 index.build_name_components ();
3841
3f563c84
PA
3842 /* The same symbol may appear more than once in the range though.
3843 E.g., if we're looking for symbols that complete "w", and we have
3844 a symbol named "w1::w2", we'll find the two name components for
3845 that same symbol in the range. To be sure we only call the
3846 callback once per symbol, we first collect the symbol name
3847 indexes that matched in a temporary vector and ignore
3848 duplicates. */
3849 std::vector<offset_type> matches;
3f563c84 3850
3b00ef10
TT
3851 struct name_and_matcher
3852 {
3853 symbol_name_matcher_ftype *matcher;
3854 const std::string &name;
3855
3856 bool operator== (const name_and_matcher &other) const
3f563c84 3857 {
3b00ef10
TT
3858 return matcher == other.matcher && name == other.name;
3859 }
3860 };
3861
3862 /* A vector holding all the different symbol name matchers, for all
3863 languages. */
3864 std::vector<name_and_matcher> matchers;
3865
3866 for (int i = 0; i < nr_languages; i++)
3867 {
3868 enum language lang_e = (enum language) i;
3869
3870 const language_defn *lang = language_def (lang_e);
3871 symbol_name_matcher_ftype *name_matcher
3872 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3873
3b00ef10
TT
3874 name_and_matcher key {
3875 name_matcher,
3876 lookup_name_without_params.language_lookup_name (lang_e)
3877 };
3878
3879 /* Don't insert the same comparison routine more than once.
3880 Note that we do this linear walk. This is not a problem in
3881 practice because the number of supported languages is
3882 low. */
3883 if (std::find (matchers.begin (), matchers.end (), key)
3884 != matchers.end ())
9291a0cd 3885 continue;
3b00ef10
TT
3886 matchers.push_back (std::move (key));
3887
3888 auto bounds
3889 = index.find_name_components_bounds (lookup_name_without_params,
3890 lang_e);
3891
3892 /* Now for each symbol name in range, check to see if we have a name
3893 match, and if so, call the MATCH_CALLBACK callback. */
3894
3895 for (; bounds.first != bounds.second; ++bounds.first)
3896 {
3897 const char *qualified = index.symbol_name_at (bounds.first->idx);
3898
3899 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3900 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3901 continue;
9291a0cd 3902
3b00ef10
TT
3903 matches.push_back (bounds.first->idx);
3904 }
3f563c84
PA
3905 }
3906
3907 std::sort (matches.begin (), matches.end ());
3908
3909 /* Finally call the callback, once per match. */
3910 ULONGEST prev = -1;
3911 for (offset_type idx : matches)
3912 {
3913 if (prev != idx)
3914 {
3b00ef10
TT
3915 if (!match_callback (idx))
3916 break;
3f563c84
PA
3917 prev = idx;
3918 }
3919 }
3920
3921 /* Above we use a type wider than idx's for 'prev', since 0 and
3922 (offset_type)-1 are both possible values. */
3923 static_assert (sizeof (prev) > sizeof (offset_type), "");
3924}
3925
c62446b1
PA
3926#if GDB_SELF_TEST
3927
3928namespace selftests { namespace dw2_expand_symtabs_matching {
3929
a3c5fafd
PA
3930/* A mock .gdb_index/.debug_names-like name index table, enough to
3931 exercise dw2_expand_symtabs_matching_symbol, which works with the
3932 mapped_index_base interface. Builds an index from the symbol list
3933 passed as parameter to the constructor. */
3934class mock_mapped_index : public mapped_index_base
c62446b1
PA
3935{
3936public:
a3c5fafd
PA
3937 mock_mapped_index (gdb::array_view<const char *> symbols)
3938 : m_symbol_table (symbols)
c62446b1
PA
3939 {}
3940
a3c5fafd 3941 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3942
a3c5fafd 3943 /* Return the number of names in the symbol table. */
632e107b 3944 size_t symbol_name_count () const override
c62446b1 3945 {
a3c5fafd 3946 return m_symbol_table.size ();
c62446b1
PA
3947 }
3948
a3c5fafd 3949 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 3950 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
3951 {
3952 return m_symbol_table[idx];
3953 }
c62446b1 3954
a3c5fafd
PA
3955private:
3956 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3957};
3958
3959/* Convenience function that converts a NULL pointer to a "<null>"
3960 string, to pass to print routines. */
3961
3962static const char *
3963string_or_null (const char *str)
3964{
3965 return str != NULL ? str : "<null>";
3966}
3967
3968/* Check if a lookup_name_info built from
3969 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3970 index. EXPECTED_LIST is the list of expected matches, in expected
3971 matching order. If no match expected, then an empty list is
3972 specified. Returns true on success. On failure prints a warning
3973 indicating the file:line that failed, and returns false. */
3974
3975static bool
3976check_match (const char *file, int line,
3977 mock_mapped_index &mock_index,
3978 const char *name, symbol_name_match_type match_type,
3979 bool completion_mode,
3980 std::initializer_list<const char *> expected_list)
3981{
3982 lookup_name_info lookup_name (name, match_type, completion_mode);
3983
3984 bool matched = true;
3985
3986 auto mismatch = [&] (const char *expected_str,
3987 const char *got)
3988 {
3989 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3990 "expected=\"%s\", got=\"%s\"\n"),
3991 file, line,
3992 (match_type == symbol_name_match_type::FULL
3993 ? "FULL" : "WILD"),
3994 name, string_or_null (expected_str), string_or_null (got));
3995 matched = false;
3996 };
3997
3998 auto expected_it = expected_list.begin ();
3999 auto expected_end = expected_list.end ();
4000
a3c5fafd 4001 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4002 NULL, ALL_DOMAIN,
4003 [&] (offset_type idx)
4004 {
a3c5fafd 4005 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4006 const char *expected_str
4007 = expected_it == expected_end ? NULL : *expected_it++;
4008
4009 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4010 mismatch (expected_str, matched_name);
3b00ef10 4011 return true;
c62446b1
PA
4012 });
4013
4014 const char *expected_str
4015 = expected_it == expected_end ? NULL : *expected_it++;
4016 if (expected_str != NULL)
4017 mismatch (expected_str, NULL);
4018
4019 return matched;
4020}
4021
4022/* The symbols added to the mock mapped_index for testing (in
4023 canonical form). */
4024static const char *test_symbols[] = {
4025 "function",
4026 "std::bar",
4027 "std::zfunction",
4028 "std::zfunction2",
4029 "w1::w2",
4030 "ns::foo<char*>",
4031 "ns::foo<int>",
4032 "ns::foo<long>",
a20714ff
PA
4033 "ns2::tmpl<int>::foo2",
4034 "(anonymous namespace)::A::B::C",
c62446b1 4035
e1ef7d7a
PA
4036 /* These are used to check that the increment-last-char in the
4037 matching algorithm for completion doesn't match "t1_fund" when
4038 completing "t1_func". */
4039 "t1_func",
4040 "t1_func1",
4041 "t1_fund",
4042 "t1_fund1",
4043
4044 /* A UTF-8 name with multi-byte sequences to make sure that
4045 cp-name-parser understands this as a single identifier ("função"
4046 is "function" in PT). */
4047 u8"u8função",
4048
4049 /* \377 (0xff) is Latin1 'ÿ'. */
4050 "yfunc\377",
4051
4052 /* \377 (0xff) is Latin1 'ÿ'. */
4053 "\377",
4054 "\377\377123",
4055
c62446b1
PA
4056 /* A name with all sorts of complications. Starts with "z" to make
4057 it easier for the completion tests below. */
4058#define Z_SYM_NAME \
4059 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4060 "::tuple<(anonymous namespace)::ui*, " \
4061 "std::default_delete<(anonymous namespace)::ui>, void>"
4062
4063 Z_SYM_NAME
4064};
4065
a3c5fafd
PA
4066/* Returns true if the mapped_index_base::find_name_component_bounds
4067 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4068 in completion mode. */
5c58de74
PA
4069
4070static bool
a3c5fafd 4071check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4072 const char *search_name,
4073 gdb::array_view<const char *> expected_syms)
4074{
4075 lookup_name_info lookup_name (search_name,
4076 symbol_name_match_type::FULL, true);
4077
3b00ef10
TT
4078 auto bounds = index.find_name_components_bounds (lookup_name,
4079 language_cplus);
5c58de74
PA
4080
4081 size_t distance = std::distance (bounds.first, bounds.second);
4082 if (distance != expected_syms.size ())
4083 return false;
4084
4085 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4086 {
4087 auto nc_elem = bounds.first + exp_elem;
4088 const char *qualified = index.symbol_name_at (nc_elem->idx);
4089 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4090 return false;
4091 }
4092
4093 return true;
4094}
4095
4096/* Test the lower-level mapped_index::find_name_component_bounds
4097 method. */
4098
c62446b1 4099static void
5c58de74
PA
4100test_mapped_index_find_name_component_bounds ()
4101{
4102 mock_mapped_index mock_index (test_symbols);
4103
a3c5fafd 4104 mock_index.build_name_components ();
5c58de74
PA
4105
4106 /* Test the lower-level mapped_index::find_name_component_bounds
4107 method in completion mode. */
4108 {
4109 static const char *expected_syms[] = {
4110 "t1_func",
4111 "t1_func1",
5c58de74
PA
4112 };
4113
a3c5fafd 4114 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4115 "t1_func", expected_syms));
4116 }
4117
4118 /* Check that the increment-last-char in the name matching algorithm
4119 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4120 {
4121 static const char *expected_syms1[] = {
4122 "\377",
4123 "\377\377123",
4124 };
a3c5fafd 4125 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4126 "\377", expected_syms1));
4127
4128 static const char *expected_syms2[] = {
4129 "\377\377123",
4130 };
a3c5fafd 4131 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4132 "\377\377", expected_syms2));
4133 }
4134}
4135
4136/* Test dw2_expand_symtabs_matching_symbol. */
4137
4138static void
4139test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4140{
4141 mock_mapped_index mock_index (test_symbols);
4142
4143 /* We let all tests run until the end even if some fails, for debug
4144 convenience. */
4145 bool any_mismatch = false;
4146
4147 /* Create the expected symbols list (an initializer_list). Needed
4148 because lists have commas, and we need to pass them to CHECK,
4149 which is a macro. */
4150#define EXPECT(...) { __VA_ARGS__ }
4151
4152 /* Wrapper for check_match that passes down the current
4153 __FILE__/__LINE__. */
4154#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4155 any_mismatch |= !check_match (__FILE__, __LINE__, \
4156 mock_index, \
4157 NAME, MATCH_TYPE, COMPLETION_MODE, \
4158 EXPECTED_LIST)
4159
4160 /* Identity checks. */
4161 for (const char *sym : test_symbols)
4162 {
4163 /* Should be able to match all existing symbols. */
4164 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4165 EXPECT (sym));
4166
4167 /* Should be able to match all existing symbols with
4168 parameters. */
4169 std::string with_params = std::string (sym) + "(int)";
4170 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4171 EXPECT (sym));
4172
4173 /* Should be able to match all existing symbols with
4174 parameters and qualifiers. */
4175 with_params = std::string (sym) + " ( int ) const";
4176 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4177 EXPECT (sym));
4178
4179 /* This should really find sym, but cp-name-parser.y doesn't
4180 know about lvalue/rvalue qualifiers yet. */
4181 with_params = std::string (sym) + " ( int ) &&";
4182 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4183 {});
4184 }
4185
e1ef7d7a
PA
4186 /* Check that the name matching algorithm for completion doesn't get
4187 confused with Latin1 'ÿ' / 0xff. */
4188 {
4189 static const char str[] = "\377";
4190 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4191 EXPECT ("\377", "\377\377123"));
4192 }
4193
4194 /* Check that the increment-last-char in the matching algorithm for
4195 completion doesn't match "t1_fund" when completing "t1_func". */
4196 {
4197 static const char str[] = "t1_func";
4198 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4199 EXPECT ("t1_func", "t1_func1"));
4200 }
4201
c62446b1
PA
4202 /* Check that completion mode works at each prefix of the expected
4203 symbol name. */
4204 {
4205 static const char str[] = "function(int)";
4206 size_t len = strlen (str);
4207 std::string lookup;
4208
4209 for (size_t i = 1; i < len; i++)
4210 {
4211 lookup.assign (str, i);
4212 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4213 EXPECT ("function"));
4214 }
4215 }
4216
4217 /* While "w" is a prefix of both components, the match function
4218 should still only be called once. */
4219 {
4220 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4221 EXPECT ("w1::w2"));
a20714ff
PA
4222 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4223 EXPECT ("w1::w2"));
c62446b1
PA
4224 }
4225
4226 /* Same, with a "complicated" symbol. */
4227 {
4228 static const char str[] = Z_SYM_NAME;
4229 size_t len = strlen (str);
4230 std::string lookup;
4231
4232 for (size_t i = 1; i < len; i++)
4233 {
4234 lookup.assign (str, i);
4235 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4236 EXPECT (Z_SYM_NAME));
4237 }
4238 }
4239
4240 /* In FULL mode, an incomplete symbol doesn't match. */
4241 {
4242 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4243 {});
4244 }
4245
4246 /* A complete symbol with parameters matches any overload, since the
4247 index has no overload info. */
4248 {
4249 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4250 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4251 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4252 EXPECT ("std::zfunction", "std::zfunction2"));
4253 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4254 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4255 }
4256
4257 /* Check that whitespace is ignored appropriately. A symbol with a
4258 template argument list. */
4259 {
4260 static const char expected[] = "ns::foo<int>";
4261 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4262 EXPECT (expected));
a20714ff
PA
4263 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4264 EXPECT (expected));
c62446b1
PA
4265 }
4266
4267 /* Check that whitespace is ignored appropriately. A symbol with a
4268 template argument list that includes a pointer. */
4269 {
4270 static const char expected[] = "ns::foo<char*>";
4271 /* Try both completion and non-completion modes. */
4272 static const bool completion_mode[2] = {false, true};
4273 for (size_t i = 0; i < 2; i++)
4274 {
4275 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4276 completion_mode[i], EXPECT (expected));
a20714ff
PA
4277 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4278 completion_mode[i], EXPECT (expected));
c62446b1
PA
4279
4280 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4281 completion_mode[i], EXPECT (expected));
a20714ff
PA
4282 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4283 completion_mode[i], EXPECT (expected));
c62446b1
PA
4284 }
4285 }
4286
4287 {
4288 /* Check method qualifiers are ignored. */
4289 static const char expected[] = "ns::foo<char*>";
4290 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4291 symbol_name_match_type::FULL, true, EXPECT (expected));
4292 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4293 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4294 CHECK_MATCH ("foo < char * > ( int ) const",
4295 symbol_name_match_type::WILD, true, EXPECT (expected));
4296 CHECK_MATCH ("foo < char * > ( int ) &&",
4297 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4298 }
4299
4300 /* Test lookup names that don't match anything. */
4301 {
a20714ff
PA
4302 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4303 {});
4304
c62446b1
PA
4305 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4306 {});
4307 }
4308
a20714ff
PA
4309 /* Some wild matching tests, exercising "(anonymous namespace)",
4310 which should not be confused with a parameter list. */
4311 {
4312 static const char *syms[] = {
4313 "A::B::C",
4314 "B::C",
4315 "C",
4316 "A :: B :: C ( int )",
4317 "B :: C ( int )",
4318 "C ( int )",
4319 };
4320
4321 for (const char *s : syms)
4322 {
4323 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4324 EXPECT ("(anonymous namespace)::A::B::C"));
4325 }
4326 }
4327
4328 {
4329 static const char expected[] = "ns2::tmpl<int>::foo2";
4330 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4331 EXPECT (expected));
4332 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4333 EXPECT (expected));
4334 }
4335
c62446b1
PA
4336 SELF_CHECK (!any_mismatch);
4337
4338#undef EXPECT
4339#undef CHECK_MATCH
4340}
4341
5c58de74
PA
4342static void
4343run_test ()
4344{
4345 test_mapped_index_find_name_component_bounds ();
4346 test_dw2_expand_symtabs_matching_symbol ();
4347}
4348
c62446b1
PA
4349}} // namespace selftests::dw2_expand_symtabs_matching
4350
4351#endif /* GDB_SELF_TEST */
4352
4b514bc8
JK
4353/* If FILE_MATCHER is NULL or if PER_CU has
4354 dwarf2_per_cu_quick_data::MARK set (see
4355 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4356 EXPANSION_NOTIFY on it. */
4357
4358static void
4359dw2_expand_symtabs_matching_one
4360 (struct dwarf2_per_cu_data *per_cu,
4361 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4362 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4363{
4364 if (file_matcher == NULL || per_cu->v.quick->mark)
4365 {
4366 bool symtab_was_null
4367 = (per_cu->v.quick->compunit_symtab == NULL);
4368
58f0c718 4369 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4370
4371 if (expansion_notify != NULL
4372 && symtab_was_null
4373 && per_cu->v.quick->compunit_symtab != NULL)
4374 expansion_notify (per_cu->v.quick->compunit_symtab);
4375 }
4376}
4377
3f563c84
PA
4378/* Helper for dw2_expand_matching symtabs. Called on each symbol
4379 matched, to expand corresponding CUs that were marked. IDX is the
4380 index of the symbol name that matched. */
4381
4382static void
4383dw2_expand_marked_cus
ed2dc618 4384 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4385 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4386 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4387 search_domain kind)
4388{
3f563c84
PA
4389 offset_type *vec, vec_len, vec_idx;
4390 bool global_seen = false;
ed2dc618 4391 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4392
61920122 4393 vec = (offset_type *) (index.constant_pool
f00a2de2 4394 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4395 vec_len = MAYBE_SWAP (vec[0]);
4396 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4397 {
61920122
PA
4398 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4399 /* This value is only valid for index versions >= 7. */
4400 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4401 gdb_index_symbol_kind symbol_kind =
4402 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4403 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4404 /* Only check the symbol attributes if they're present.
4405 Indices prior to version 7 don't record them,
4406 and indices >= 7 may elide them for certain symbols
4407 (gold does this). */
4408 int attrs_valid =
4409 (index.version >= 7
4410 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4411
4412 /* Work around gold/15646. */
4413 if (attrs_valid)
9291a0cd 4414 {
61920122
PA
4415 if (!is_static && global_seen)
4416 continue;
4417 if (!is_static)
4418 global_seen = true;
4419 }
3190f0c6 4420
61920122
PA
4421 /* Only check the symbol's kind if it has one. */
4422 if (attrs_valid)
4423 {
4424 switch (kind)
8943b874 4425 {
61920122
PA
4426 case VARIABLES_DOMAIN:
4427 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4428 continue;
4429 break;
4430 case FUNCTIONS_DOMAIN:
4431 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4432 continue;
61920122
PA
4433 break;
4434 case TYPES_DOMAIN:
4435 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4436 continue;
4437 break;
59c35742
AB
4438 case MODULES_DOMAIN:
4439 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4440 continue;
4441 break;
61920122
PA
4442 default:
4443 break;
8943b874 4444 }
61920122 4445 }
8943b874 4446
61920122 4447 /* Don't crash on bad data. */
b76e467d 4448 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4449 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4450 {
b98664d3 4451 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4452 " [in module %s]"),
4453 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4454 continue;
4455 }
4456
ff4c9fec 4457 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4458 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4459 expansion_notify);
61920122
PA
4460 }
4461}
4462
4b514bc8
JK
4463/* If FILE_MATCHER is non-NULL, set all the
4464 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4465 that match FILE_MATCHER. */
4466
61920122 4467static void
4b514bc8 4468dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4469 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4470 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4471{
4b514bc8 4472 if (file_matcher == NULL)
61920122
PA
4473 return;
4474
4b514bc8
JK
4475 objfile *const objfile = dwarf2_per_objfile->objfile;
4476
4477 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4478 htab_eq_pointer,
4479 NULL, xcalloc, xfree));
4480 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4481 htab_eq_pointer,
4482 NULL, xcalloc, xfree));
61920122 4483
4b514bc8
JK
4484 /* The rule is CUs specify all the files, including those used by
4485 any TU, so there's no need to scan TUs here. */
61920122 4486
b76e467d 4487 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4488 {
927aa2e7
JK
4489 QUIT;
4490
4491 per_cu->v.quick->mark = 0;
4492
4493 /* We only need to look at symtabs not already expanded. */
4494 if (per_cu->v.quick->compunit_symtab)
4495 continue;
4496
b76e467d 4497 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4498 if (file_data == NULL)
4499 continue;
4500
4501 if (htab_find (visited_not_found.get (), file_data) != NULL)
4502 continue;
4503 else if (htab_find (visited_found.get (), file_data) != NULL)
4504 {
4505 per_cu->v.quick->mark = 1;
4506 continue;
4507 }
4508
b76e467d 4509 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4510 {
4511 const char *this_real_name;
4512
4513 if (file_matcher (file_data->file_names[j], false))
4514 {
4515 per_cu->v.quick->mark = 1;
4516 break;
4517 }
4518
4519 /* Before we invoke realpath, which can get expensive when many
4520 files are involved, do a quick comparison of the basenames. */
4521 if (!basenames_may_differ
4522 && !file_matcher (lbasename (file_data->file_names[j]),
4523 true))
4524 continue;
4525
4526 this_real_name = dw2_get_real_path (objfile, file_data, j);
4527 if (file_matcher (this_real_name, false))
4528 {
4529 per_cu->v.quick->mark = 1;
4530 break;
4531 }
4532 }
4533
b76e467d
SM
4534 void **slot = htab_find_slot (per_cu->v.quick->mark
4535 ? visited_found.get ()
4536 : visited_not_found.get (),
4537 file_data, INSERT);
927aa2e7
JK
4538 *slot = file_data;
4539 }
4540}
4541
4542static void
4543dw2_expand_symtabs_matching
4544 (struct objfile *objfile,
4545 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4546 const lookup_name_info &lookup_name,
4547 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4548 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4549 enum search_domain kind)
4550{
ed2dc618
SM
4551 struct dwarf2_per_objfile *dwarf2_per_objfile
4552 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4553
4554 /* index_table is NULL if OBJF_READNOW. */
4555 if (!dwarf2_per_objfile->index_table)
4556 return;
4557
ed2dc618 4558 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4559
4560 mapped_index &index = *dwarf2_per_objfile->index_table;
4561
4562 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4563 symbol_matcher,
4564 kind, [&] (offset_type idx)
4565 {
ed2dc618 4566 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4567 expansion_notify, kind);
3b00ef10 4568 return true;
927aa2e7
JK
4569 });
4570}
4571
4572/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4573 symtab. */
4574
4575static struct compunit_symtab *
4576recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4577 CORE_ADDR pc)
4578{
4579 int i;
4580
4581 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4582 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4583 return cust;
4584
4585 if (cust->includes == NULL)
4586 return NULL;
4587
4588 for (i = 0; cust->includes[i]; ++i)
4589 {
4590 struct compunit_symtab *s = cust->includes[i];
4591
4592 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4593 if (s != NULL)
4594 return s;
4595 }
4596
4597 return NULL;
4598}
4599
4600static struct compunit_symtab *
4601dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4602 struct bound_minimal_symbol msymbol,
4603 CORE_ADDR pc,
4604 struct obj_section *section,
4605 int warn_if_readin)
4606{
4607 struct dwarf2_per_cu_data *data;
4608 struct compunit_symtab *result;
4609
d320c2b5 4610 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4611 return NULL;
4612
b3b3bada 4613 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4614 data = (struct dwarf2_per_cu_data *) addrmap_find
4615 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4616 if (!data)
4617 return NULL;
4618
4619 if (warn_if_readin && data->v.quick->compunit_symtab)
4620 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4621 paddress (get_objfile_arch (objfile), pc));
4622
4623 result
58f0c718
TT
4624 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4625 false),
927aa2e7
JK
4626 pc);
4627 gdb_assert (result != NULL);
4628 return result;
4629}
4630
4631static void
4632dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4633 void *data, int need_fullname)
4634{
ed2dc618
SM
4635 struct dwarf2_per_objfile *dwarf2_per_objfile
4636 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4637
4638 if (!dwarf2_per_objfile->filenames_cache)
4639 {
4640 dwarf2_per_objfile->filenames_cache.emplace ();
4641
4642 htab_up visited (htab_create_alloc (10,
4643 htab_hash_pointer, htab_eq_pointer,
4644 NULL, xcalloc, xfree));
4645
4646 /* The rule is CUs specify all the files, including those used
4647 by any TU, so there's no need to scan TUs here. We can
4648 ignore file names coming from already-expanded CUs. */
4649
b76e467d 4650 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4651 {
927aa2e7
JK
4652 if (per_cu->v.quick->compunit_symtab)
4653 {
4654 void **slot = htab_find_slot (visited.get (),
4655 per_cu->v.quick->file_names,
4656 INSERT);
4657
4658 *slot = per_cu->v.quick->file_names;
4659 }
4660 }
4661
b76e467d 4662 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4663 {
927aa2e7
JK
4664 /* We only need to look at symtabs not already expanded. */
4665 if (per_cu->v.quick->compunit_symtab)
4666 continue;
4667
b76e467d 4668 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4669 if (file_data == NULL)
4670 continue;
4671
b76e467d 4672 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4673 if (*slot)
4674 {
4675 /* Already visited. */
4676 continue;
4677 }
4678 *slot = file_data;
4679
4680 for (int j = 0; j < file_data->num_file_names; ++j)
4681 {
4682 const char *filename = file_data->file_names[j];
4683 dwarf2_per_objfile->filenames_cache->seen (filename);
4684 }
4685 }
4686 }
4687
4688 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4689 {
4690 gdb::unique_xmalloc_ptr<char> this_real_name;
4691
4692 if (need_fullname)
4693 this_real_name = gdb_realpath (filename);
4694 (*fun) (filename, this_real_name.get (), data);
4695 });
4696}
4697
4698static int
4699dw2_has_symbols (struct objfile *objfile)
4700{
4701 return 1;
4702}
4703
4704const struct quick_symbol_functions dwarf2_gdb_index_functions =
4705{
4706 dw2_has_symbols,
4707 dw2_find_last_source_symtab,
4708 dw2_forget_cached_source_info,
4709 dw2_map_symtabs_matching_filename,
4710 dw2_lookup_symbol,
4711 dw2_print_stats,
4712 dw2_dump,
927aa2e7
JK
4713 dw2_expand_symtabs_for_function,
4714 dw2_expand_all_symtabs,
4715 dw2_expand_symtabs_with_fullname,
4716 dw2_map_matching_symbols,
4717 dw2_expand_symtabs_matching,
4718 dw2_find_pc_sect_compunit_symtab,
4719 NULL,
4720 dw2_map_symbol_filenames
4721};
4722
4723/* DWARF-5 debug_names reader. */
4724
4725/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4726static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4727
4728/* A helper function that reads the .debug_names section in SECTION
4729 and fills in MAP. FILENAME is the name of the file containing the
4730 section; it is used for error reporting.
4731
4732 Returns true if all went well, false otherwise. */
4733
4734static bool
4735read_debug_names_from_section (struct objfile *objfile,
4736 const char *filename,
4737 struct dwarf2_section_info *section,
4738 mapped_debug_names &map)
4739{
96b79293 4740 if (section->empty ())
927aa2e7
JK
4741 return false;
4742
4743 /* Older elfutils strip versions could keep the section in the main
4744 executable while splitting it for the separate debug info file. */
96b79293 4745 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4746 return false;
4747
96b79293 4748 section->read (objfile);
927aa2e7
JK
4749
4750 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4751
4752 const gdb_byte *addr = section->buffer;
4753
96b79293 4754 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4755
4756 unsigned int bytes_read;
4757 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4758 addr += bytes_read;
4759
4760 map.dwarf5_is_dwarf64 = bytes_read != 4;
4761 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4762 if (bytes_read + length != section->size)
4763 {
4764 /* There may be multiple per-CU indices. */
4765 warning (_("Section .debug_names in %s length %s does not match "
4766 "section length %s, ignoring .debug_names."),
4767 filename, plongest (bytes_read + length),
4768 pulongest (section->size));
4769 return false;
4770 }
4771
4772 /* The version number. */
4773 uint16_t version = read_2_bytes (abfd, addr);
4774 addr += 2;
4775 if (version != 5)
4776 {
4777 warning (_("Section .debug_names in %s has unsupported version %d, "
4778 "ignoring .debug_names."),
4779 filename, version);
4780 return false;
4781 }
4782
4783 /* Padding. */
4784 uint16_t padding = read_2_bytes (abfd, addr);
4785 addr += 2;
4786 if (padding != 0)
4787 {
4788 warning (_("Section .debug_names in %s has unsupported padding %d, "
4789 "ignoring .debug_names."),
4790 filename, padding);
4791 return false;
4792 }
4793
4794 /* comp_unit_count - The number of CUs in the CU list. */
4795 map.cu_count = read_4_bytes (abfd, addr);
4796 addr += 4;
4797
4798 /* local_type_unit_count - The number of TUs in the local TU
4799 list. */
4800 map.tu_count = read_4_bytes (abfd, addr);
4801 addr += 4;
4802
4803 /* foreign_type_unit_count - The number of TUs in the foreign TU
4804 list. */
4805 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4806 addr += 4;
4807 if (foreign_tu_count != 0)
4808 {
4809 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4810 "ignoring .debug_names."),
4811 filename, static_cast<unsigned long> (foreign_tu_count));
4812 return false;
4813 }
4814
4815 /* bucket_count - The number of hash buckets in the hash lookup
4816 table. */
4817 map.bucket_count = read_4_bytes (abfd, addr);
4818 addr += 4;
4819
4820 /* name_count - The number of unique names in the index. */
4821 map.name_count = read_4_bytes (abfd, addr);
4822 addr += 4;
4823
4824 /* abbrev_table_size - The size in bytes of the abbreviations
4825 table. */
4826 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4827 addr += 4;
4828
4829 /* augmentation_string_size - The size in bytes of the augmentation
4830 string. This value is rounded up to a multiple of 4. */
4831 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4832 addr += 4;
4833 map.augmentation_is_gdb = ((augmentation_string_size
4834 == sizeof (dwarf5_augmentation))
4835 && memcmp (addr, dwarf5_augmentation,
4836 sizeof (dwarf5_augmentation)) == 0);
4837 augmentation_string_size += (-augmentation_string_size) & 3;
4838 addr += augmentation_string_size;
4839
4840 /* List of CUs */
4841 map.cu_table_reordered = addr;
4842 addr += map.cu_count * map.offset_size;
4843
4844 /* List of Local TUs */
4845 map.tu_table_reordered = addr;
4846 addr += map.tu_count * map.offset_size;
4847
4848 /* Hash Lookup Table */
4849 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4850 addr += map.bucket_count * 4;
4851 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4852 addr += map.name_count * 4;
4853
4854 /* Name Table */
4855 map.name_table_string_offs_reordered = addr;
4856 addr += map.name_count * map.offset_size;
4857 map.name_table_entry_offs_reordered = addr;
4858 addr += map.name_count * map.offset_size;
4859
4860 const gdb_byte *abbrev_table_start = addr;
4861 for (;;)
4862 {
927aa2e7
JK
4863 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4864 addr += bytes_read;
4865 if (index_num == 0)
4866 break;
4867
4868 const auto insertpair
4869 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4870 if (!insertpair.second)
4871 {
4872 warning (_("Section .debug_names in %s has duplicate index %s, "
4873 "ignoring .debug_names."),
4874 filename, pulongest (index_num));
4875 return false;
4876 }
4877 mapped_debug_names::index_val &indexval = insertpair.first->second;
4878 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4879 addr += bytes_read;
4880
4881 for (;;)
4882 {
4883 mapped_debug_names::index_val::attr attr;
4884 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4885 addr += bytes_read;
4886 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4887 addr += bytes_read;
4888 if (attr.form == DW_FORM_implicit_const)
4889 {
4890 attr.implicit_const = read_signed_leb128 (abfd, addr,
4891 &bytes_read);
4892 addr += bytes_read;
4893 }
4894 if (attr.dw_idx == 0 && attr.form == 0)
4895 break;
4896 indexval.attr_vec.push_back (std::move (attr));
4897 }
4898 }
4899 if (addr != abbrev_table_start + abbrev_table_size)
4900 {
4901 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4902 "of size %s vs. written as %u, ignoring .debug_names."),
4903 filename, plongest (addr - abbrev_table_start),
4904 abbrev_table_size);
927aa2e7
JK
4905 return false;
4906 }
4907 map.entry_pool = addr;
4908
4909 return true;
4910}
4911
4912/* A helper for create_cus_from_debug_names that handles the MAP's CU
4913 list. */
4914
4915static void
ed2dc618 4916create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4917 const mapped_debug_names &map,
4918 dwarf2_section_info &section,
b76e467d 4919 bool is_dwz)
927aa2e7
JK
4920{
4921 sect_offset sect_off_prev;
4922 for (uint32_t i = 0; i <= map.cu_count; ++i)
4923 {
4924 sect_offset sect_off_next;
4925 if (i < map.cu_count)
4926 {
4927 sect_off_next
4928 = (sect_offset) (extract_unsigned_integer
4929 (map.cu_table_reordered + i * map.offset_size,
4930 map.offset_size,
4931 map.dwarf5_byte_order));
4932 }
4933 else
4934 sect_off_next = (sect_offset) section.size;
4935 if (i >= 1)
4936 {
4937 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 4938 dwarf2_per_cu_data *per_cu
ed2dc618 4939 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 4940 sect_off_prev, length);
b76e467d 4941 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
4942 }
4943 sect_off_prev = sect_off_next;
4944 }
4945}
4946
4947/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4948 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4949
4950static void
ed2dc618 4951create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4952 const mapped_debug_names &map,
4953 const mapped_debug_names &dwz_map)
4954{
b76e467d
SM
4955 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4956 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4957
ed2dc618
SM
4958 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4959 dwarf2_per_objfile->info,
b76e467d 4960 false /* is_dwz */);
927aa2e7
JK
4961
4962 if (dwz_map.cu_count == 0)
4963 return;
4964
ed2dc618
SM
4965 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4966 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 4967 true /* is_dwz */);
927aa2e7
JK
4968}
4969
4970/* Read .debug_names. If everything went ok, initialize the "quick"
4971 elements of all the CUs and return true. Otherwise, return false. */
4972
4973static bool
ed2dc618 4974dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 4975{
22ca247e
TT
4976 std::unique_ptr<mapped_debug_names> map
4977 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
4978 mapped_debug_names dwz_map (dwarf2_per_objfile);
4979 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
4980
4981 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4982 &dwarf2_per_objfile->debug_names,
22ca247e 4983 *map))
927aa2e7
JK
4984 return false;
4985
4986 /* Don't use the index if it's empty. */
22ca247e 4987 if (map->name_count == 0)
927aa2e7
JK
4988 return false;
4989
4990 /* If there is a .dwz file, read it so we can get its CU list as
4991 well. */
ed2dc618 4992 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
4993 if (dwz != NULL)
4994 {
4995 if (!read_debug_names_from_section (objfile,
00f93c44 4996 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
4997 &dwz->debug_names, dwz_map))
4998 {
4999 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5000 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5001 return false;
5002 }
5003 }
5004
22ca247e 5005 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5006
22ca247e 5007 if (map->tu_count != 0)
927aa2e7
JK
5008 {
5009 /* We can only handle a single .debug_types when we have an
5010 index. */
fd5866f6 5011 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5012 return false;
5013
fd5866f6 5014 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5015
5016 create_signatured_type_table_from_debug_names
22ca247e 5017 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5018 }
5019
ed2dc618
SM
5020 create_addrmap_from_aranges (dwarf2_per_objfile,
5021 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5022
22ca247e 5023 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5024 dwarf2_per_objfile->using_index = 1;
5025 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5026 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5027
5028 return true;
5029}
5030
927aa2e7
JK
5031/* Type used to manage iterating over all CUs looking for a symbol for
5032 .debug_names. */
5033
5034class dw2_debug_names_iterator
5035{
5036public:
927aa2e7 5037 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5038 gdb::optional<block_enum> block_index,
5039 domain_enum domain,
927aa2e7 5040 const char *name)
2b79f376 5041 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5042 m_addr (find_vec_in_debug_names (map, name))
5043 {}
5044
5045 dw2_debug_names_iterator (const mapped_debug_names &map,
5046 search_domain search, uint32_t namei)
5047 : m_map (map),
5048 m_search (search),
5049 m_addr (find_vec_in_debug_names (map, namei))
5050 {}
5051
3b00ef10
TT
5052 dw2_debug_names_iterator (const mapped_debug_names &map,
5053 block_enum block_index, domain_enum domain,
5054 uint32_t namei)
5055 : m_map (map), m_block_index (block_index), m_domain (domain),
5056 m_addr (find_vec_in_debug_names (map, namei))
5057 {}
5058
927aa2e7
JK
5059 /* Return the next matching CU or NULL if there are no more. */
5060 dwarf2_per_cu_data *next ();
5061
5062private:
5063 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5064 const char *name);
5065 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5066 uint32_t namei);
5067
5068 /* The internalized form of .debug_names. */
5069 const mapped_debug_names &m_map;
5070
2b79f376
SM
5071 /* If set, only look for symbols that match that block. Valid values are
5072 GLOBAL_BLOCK and STATIC_BLOCK. */
5073 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5074
5075 /* The kind of symbol we're looking for. */
5076 const domain_enum m_domain = UNDEF_DOMAIN;
5077 const search_domain m_search = ALL_DOMAIN;
5078
5079 /* The list of CUs from the index entry of the symbol, or NULL if
5080 not found. */
5081 const gdb_byte *m_addr;
5082};
5083
5084const char *
5085mapped_debug_names::namei_to_name (uint32_t namei) const
5086{
5087 const ULONGEST namei_string_offs
5088 = extract_unsigned_integer ((name_table_string_offs_reordered
5089 + namei * offset_size),
5090 offset_size,
5091 dwarf5_byte_order);
4f44ae6c
TT
5092 return read_indirect_string_at_offset (dwarf2_per_objfile,
5093 namei_string_offs);
927aa2e7
JK
5094}
5095
5096/* Find a slot in .debug_names for the object named NAME. If NAME is
5097 found, return pointer to its pool data. If NAME cannot be found,
5098 return NULL. */
5099
5100const gdb_byte *
5101dw2_debug_names_iterator::find_vec_in_debug_names
5102 (const mapped_debug_names &map, const char *name)
5103{
5104 int (*cmp) (const char *, const char *);
5105
54ee4252 5106 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5107 if (current_language->la_language == language_cplus
5108 || current_language->la_language == language_fortran
5109 || current_language->la_language == language_d)
5110 {
5111 /* NAME is already canonical. Drop any qualifiers as
5112 .debug_names does not contain any. */
5113
5114 if (strchr (name, '(') != NULL)
5115 {
54ee4252 5116 without_params = cp_remove_params (name);
927aa2e7 5117 if (without_params != NULL)
54ee4252 5118 name = without_params.get ();
927aa2e7
JK
5119 }
5120 }
5121
5122 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5123
5124 const uint32_t full_hash = dwarf5_djb_hash (name);
5125 uint32_t namei
5126 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5127 (map.bucket_table_reordered
5128 + (full_hash % map.bucket_count)), 4,
5129 map.dwarf5_byte_order);
5130 if (namei == 0)
5131 return NULL;
5132 --namei;
5133 if (namei >= map.name_count)
5134 {
b98664d3 5135 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5136 "[in module %s]"),
5137 namei, map.name_count,
ed2dc618 5138 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5139 return NULL;
5140 }
5141
5142 for (;;)
5143 {
5144 const uint32_t namei_full_hash
5145 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5146 (map.hash_table_reordered + namei), 4,
5147 map.dwarf5_byte_order);
5148 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5149 return NULL;
5150
5151 if (full_hash == namei_full_hash)
5152 {
5153 const char *const namei_string = map.namei_to_name (namei);
5154
5155#if 0 /* An expensive sanity check. */
5156 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5157 {
b98664d3 5158 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5159 "[in module %s]"),
5160 namei, objfile_name (dwarf2_per_objfile->objfile));
5161 return NULL;
5162 }
5163#endif
5164
5165 if (cmp (namei_string, name) == 0)
5166 {
5167 const ULONGEST namei_entry_offs
5168 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5169 + namei * map.offset_size),
5170 map.offset_size, map.dwarf5_byte_order);
5171 return map.entry_pool + namei_entry_offs;
5172 }
5173 }
5174
5175 ++namei;
5176 if (namei >= map.name_count)
5177 return NULL;
5178 }
5179}
5180
5181const gdb_byte *
5182dw2_debug_names_iterator::find_vec_in_debug_names
5183 (const mapped_debug_names &map, uint32_t namei)
5184{
5185 if (namei >= map.name_count)
5186 {
b98664d3 5187 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5188 "[in module %s]"),
5189 namei, map.name_count,
ed2dc618 5190 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5191 return NULL;
5192 }
5193
5194 const ULONGEST namei_entry_offs
5195 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5196 + namei * map.offset_size),
5197 map.offset_size, map.dwarf5_byte_order);
5198 return map.entry_pool + namei_entry_offs;
5199}
5200
5201/* See dw2_debug_names_iterator. */
5202
5203dwarf2_per_cu_data *
5204dw2_debug_names_iterator::next ()
5205{
5206 if (m_addr == NULL)
5207 return NULL;
5208
ed2dc618
SM
5209 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5210 struct objfile *objfile = dwarf2_per_objfile->objfile;
5211 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5212
5213 again:
5214
5215 unsigned int bytes_read;
5216 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5217 m_addr += bytes_read;
5218 if (abbrev == 0)
5219 return NULL;
5220
5221 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5222 if (indexval_it == m_map.abbrev_map.cend ())
5223 {
b98664d3 5224 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5225 "[in module %s]"),
ed2dc618 5226 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5227 return NULL;
5228 }
5229 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5230 enum class symbol_linkage {
5231 unknown,
5232 static_,
5233 extern_,
23c13d42 5234 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5235 dwarf2_per_cu_data *per_cu = NULL;
5236 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5237 {
5238 ULONGEST ull;
5239 switch (attr.form)
5240 {
5241 case DW_FORM_implicit_const:
5242 ull = attr.implicit_const;
5243 break;
5244 case DW_FORM_flag_present:
5245 ull = 1;
5246 break;
5247 case DW_FORM_udata:
5248 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5249 m_addr += bytes_read;
5250 break;
5251 default:
b98664d3 5252 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5253 dwarf_form_name (attr.form),
ed2dc618 5254 objfile_name (objfile));
927aa2e7
JK
5255 return NULL;
5256 }
5257 switch (attr.dw_idx)
5258 {
5259 case DW_IDX_compile_unit:
5260 /* Don't crash on bad data. */
b76e467d 5261 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5262 {
b98664d3 5263 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5264 " [in module %s]"),
5265 pulongest (ull),
5266 objfile_name (dwarf2_per_objfile->objfile));
5267 continue;
5268 }
ff4c9fec 5269 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5270 break;
8af5c486
JK
5271 case DW_IDX_type_unit:
5272 /* Don't crash on bad data. */
b2bdb8cf 5273 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5274 {
b98664d3 5275 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5276 " [in module %s]"),
5277 pulongest (ull),
5278 objfile_name (dwarf2_per_objfile->objfile));
5279 continue;
5280 }
ff4c9fec 5281 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5282 break;
927aa2e7
JK
5283 case DW_IDX_GNU_internal:
5284 if (!m_map.augmentation_is_gdb)
5285 break;
23c13d42 5286 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5287 break;
5288 case DW_IDX_GNU_external:
5289 if (!m_map.augmentation_is_gdb)
5290 break;
23c13d42 5291 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5292 break;
5293 }
5294 }
5295
5296 /* Skip if already read in. */
5297 if (per_cu->v.quick->compunit_symtab)
5298 goto again;
5299
5300 /* Check static vs global. */
23c13d42 5301 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5302 {
2b79f376 5303 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5304 const bool symbol_is_static =
5305 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5306 if (want_static != symbol_is_static)
2b79f376 5307 goto again;
927aa2e7
JK
5308 }
5309
5310 /* Match dw2_symtab_iter_next, symbol_kind
5311 and debug_names::psymbol_tag. */
5312 switch (m_domain)
5313 {
5314 case VAR_DOMAIN:
5315 switch (indexval.dwarf_tag)
5316 {
5317 case DW_TAG_variable:
5318 case DW_TAG_subprogram:
5319 /* Some types are also in VAR_DOMAIN. */
5320 case DW_TAG_typedef:
5321 case DW_TAG_structure_type:
5322 break;
5323 default:
5324 goto again;
5325 }
5326 break;
5327 case STRUCT_DOMAIN:
5328 switch (indexval.dwarf_tag)
5329 {
5330 case DW_TAG_typedef:
5331 case DW_TAG_structure_type:
5332 break;
5333 default:
5334 goto again;
5335 }
5336 break;
5337 case LABEL_DOMAIN:
5338 switch (indexval.dwarf_tag)
5339 {
5340 case 0:
5341 case DW_TAG_variable:
5342 break;
5343 default:
5344 goto again;
5345 }
5346 break;
59c35742
AB
5347 case MODULE_DOMAIN:
5348 switch (indexval.dwarf_tag)
5349 {
5350 case DW_TAG_module:
5351 break;
5352 default:
5353 goto again;
5354 }
5355 break;
927aa2e7
JK
5356 default:
5357 break;
5358 }
5359
5360 /* Match dw2_expand_symtabs_matching, symbol_kind and
5361 debug_names::psymbol_tag. */
5362 switch (m_search)
4b514bc8 5363 {
927aa2e7
JK
5364 case VARIABLES_DOMAIN:
5365 switch (indexval.dwarf_tag)
4b514bc8 5366 {
927aa2e7
JK
5367 case DW_TAG_variable:
5368 break;
5369 default:
5370 goto again;
4b514bc8 5371 }
927aa2e7
JK
5372 break;
5373 case FUNCTIONS_DOMAIN:
5374 switch (indexval.dwarf_tag)
4b514bc8 5375 {
927aa2e7
JK
5376 case DW_TAG_subprogram:
5377 break;
5378 default:
5379 goto again;
4b514bc8 5380 }
927aa2e7
JK
5381 break;
5382 case TYPES_DOMAIN:
5383 switch (indexval.dwarf_tag)
5384 {
5385 case DW_TAG_typedef:
5386 case DW_TAG_structure_type:
5387 break;
5388 default:
5389 goto again;
5390 }
5391 break;
59c35742
AB
5392 case MODULES_DOMAIN:
5393 switch (indexval.dwarf_tag)
5394 {
5395 case DW_TAG_module:
5396 break;
5397 default:
5398 goto again;
5399 }
927aa2e7
JK
5400 default:
5401 break;
4b514bc8 5402 }
927aa2e7
JK
5403
5404 return per_cu;
4b514bc8 5405}
61920122 5406
927aa2e7 5407static struct compunit_symtab *
c7f839cb 5408dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5409 const char *name, domain_enum domain)
4b514bc8 5410{
ed2dc618
SM
5411 struct dwarf2_per_objfile *dwarf2_per_objfile
5412 = get_dwarf2_per_objfile (objfile);
61920122 5413
927aa2e7
JK
5414 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5415 if (!mapp)
61920122 5416 {
927aa2e7
JK
5417 /* index is NULL if OBJF_READNOW. */
5418 return NULL;
5419 }
5420 const auto &map = *mapp;
9291a0cd 5421
2b79f376 5422 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5423
927aa2e7
JK
5424 struct compunit_symtab *stab_best = NULL;
5425 struct dwarf2_per_cu_data *per_cu;
5426 while ((per_cu = iter.next ()) != NULL)
5427 {
5428 struct symbol *sym, *with_opaque = NULL;
58f0c718 5429 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5430 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5431 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5432
927aa2e7
JK
5433 sym = block_find_symbol (block, name, domain,
5434 block_find_non_opaque_type_preferred,
5435 &with_opaque);
9703b513 5436
927aa2e7
JK
5437 /* Some caution must be observed with overloaded functions and
5438 methods, since the index will not contain any overload
5439 information (but NAME might contain it). */
a3ec0bb1 5440
927aa2e7 5441 if (sym != NULL
987012b8 5442 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5443 return stab;
5444 if (with_opaque != NULL
987012b8 5445 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5446 stab_best = stab;
9703b513 5447
927aa2e7 5448 /* Keep looking through other CUs. */
9703b513
TT
5449 }
5450
927aa2e7 5451 return stab_best;
9703b513
TT
5452}
5453
927aa2e7
JK
5454/* This dumps minimal information about .debug_names. It is called
5455 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5456 uses this to verify that .debug_names has been loaded. */
9291a0cd 5457
927aa2e7
JK
5458static void
5459dw2_debug_names_dump (struct objfile *objfile)
5460{
ed2dc618
SM
5461 struct dwarf2_per_objfile *dwarf2_per_objfile
5462 = get_dwarf2_per_objfile (objfile);
5463
927aa2e7
JK
5464 gdb_assert (dwarf2_per_objfile->using_index);
5465 printf_filtered (".debug_names:");
5466 if (dwarf2_per_objfile->debug_names_table)
5467 printf_filtered (" exists\n");
5468 else
5469 printf_filtered (" faked for \"readnow\"\n");
5470 printf_filtered ("\n");
9291a0cd
TT
5471}
5472
9291a0cd 5473static void
927aa2e7
JK
5474dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5475 const char *func_name)
9291a0cd 5476{
ed2dc618
SM
5477 struct dwarf2_per_objfile *dwarf2_per_objfile
5478 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5479
927aa2e7
JK
5480 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5481 if (dwarf2_per_objfile->debug_names_table)
24c79950 5482 {
927aa2e7 5483 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5484
2b79f376 5485 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5486
927aa2e7
JK
5487 struct dwarf2_per_cu_data *per_cu;
5488 while ((per_cu = iter.next ()) != NULL)
58f0c718 5489 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5490 }
5491}
24c79950 5492
3b00ef10
TT
5493static void
5494dw2_debug_names_map_matching_symbols
5495 (struct objfile *objfile,
5496 const lookup_name_info &name, domain_enum domain,
5497 int global,
5498 gdb::function_view<symbol_found_callback_ftype> callback,
5499 symbol_compare_ftype *ordered_compare)
5500{
5501 struct dwarf2_per_objfile *dwarf2_per_objfile
5502 = get_dwarf2_per_objfile (objfile);
5503
5504 /* debug_names_table is NULL if OBJF_READNOW. */
5505 if (!dwarf2_per_objfile->debug_names_table)
5506 return;
5507
5508 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5509 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5510
5511 const char *match_name = name.ada ().lookup_name ().c_str ();
5512 auto matcher = [&] (const char *symname)
5513 {
5514 if (ordered_compare == nullptr)
5515 return true;
5516 return ordered_compare (symname, match_name) == 0;
5517 };
5518
5519 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5520 [&] (offset_type namei)
5521 {
5522 /* The name was matched, now expand corresponding CUs that were
5523 marked. */
5524 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5525
5526 struct dwarf2_per_cu_data *per_cu;
5527 while ((per_cu = iter.next ()) != NULL)
5528 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5529 return true;
5530 });
5531
5532 /* It's a shame we couldn't do this inside the
5533 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5534 that have already been expanded. Instead, this loop matches what
5535 the psymtab code does. */
5536 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5537 {
5538 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5539 if (cust != nullptr)
5540 {
5541 const struct block *block
5542 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5543 if (!iterate_over_symbols_terminated (block, name,
5544 domain, callback))
5545 break;
5546 }
5547 }
5548}
5549
927aa2e7
JK
5550static void
5551dw2_debug_names_expand_symtabs_matching
5552 (struct objfile *objfile,
5553 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5554 const lookup_name_info &lookup_name,
5555 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5556 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5557 enum search_domain kind)
5558{
ed2dc618
SM
5559 struct dwarf2_per_objfile *dwarf2_per_objfile
5560 = get_dwarf2_per_objfile (objfile);
9291a0cd 5561
927aa2e7
JK
5562 /* debug_names_table is NULL if OBJF_READNOW. */
5563 if (!dwarf2_per_objfile->debug_names_table)
5564 return;
9291a0cd 5565
ed2dc618 5566 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5567
44ed8f3e 5568 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5569
44ed8f3e
PA
5570 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5571 symbol_matcher,
5572 kind, [&] (offset_type namei)
927aa2e7 5573 {
927aa2e7
JK
5574 /* The name was matched, now expand corresponding CUs that were
5575 marked. */
5576 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5577
927aa2e7
JK
5578 struct dwarf2_per_cu_data *per_cu;
5579 while ((per_cu = iter.next ()) != NULL)
5580 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5581 expansion_notify);
3b00ef10 5582 return true;
44ed8f3e 5583 });
9291a0cd
TT
5584}
5585
927aa2e7 5586const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5587{
5588 dw2_has_symbols,
5589 dw2_find_last_source_symtab,
5590 dw2_forget_cached_source_info,
f8eba3c6 5591 dw2_map_symtabs_matching_filename,
927aa2e7 5592 dw2_debug_names_lookup_symbol,
9291a0cd 5593 dw2_print_stats,
927aa2e7 5594 dw2_debug_names_dump,
927aa2e7 5595 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5596 dw2_expand_all_symtabs,
652a8996 5597 dw2_expand_symtabs_with_fullname,
3b00ef10 5598 dw2_debug_names_map_matching_symbols,
927aa2e7 5599 dw2_debug_names_expand_symtabs_matching,
43f3e411 5600 dw2_find_pc_sect_compunit_symtab,
71a3c369 5601 NULL,
9291a0cd
TT
5602 dw2_map_symbol_filenames
5603};
5604
4485a1c1
SM
5605/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5606 to either a dwarf2_per_objfile or dwz_file object. */
5607
5608template <typename T>
5609static gdb::array_view<const gdb_byte>
5610get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5611{
5612 dwarf2_section_info *section = &section_owner->gdb_index;
5613
96b79293 5614 if (section->empty ())
4485a1c1
SM
5615 return {};
5616
5617 /* Older elfutils strip versions could keep the section in the main
5618 executable while splitting it for the separate debug info file. */
96b79293 5619 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5620 return {};
5621
96b79293 5622 section->read (obj);
4485a1c1 5623
8bebfcda
PA
5624 /* dwarf2_section_info::size is a bfd_size_type, while
5625 gdb::array_view works with size_t. On 32-bit hosts, with
5626 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5627 is 32-bit. So we need an explicit narrowing conversion here.
5628 This is fine, because it's impossible to allocate or mmap an
5629 array/buffer larger than what size_t can represent. */
5630 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5631}
5632
87d6a7aa
SM
5633/* Lookup the index cache for the contents of the index associated to
5634 DWARF2_OBJ. */
5635
5636static gdb::array_view<const gdb_byte>
5637get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5638{
5639 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5640 if (build_id == nullptr)
5641 return {};
5642
5643 return global_index_cache.lookup_gdb_index (build_id,
5644 &dwarf2_obj->index_cache_res);
5645}
5646
5647/* Same as the above, but for DWZ. */
5648
5649static gdb::array_view<const gdb_byte>
5650get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5651{
5652 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5653 if (build_id == nullptr)
5654 return {};
5655
5656 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5657}
5658
3c0aa29a 5659/* See symfile.h. */
9291a0cd 5660
3c0aa29a
PA
5661bool
5662dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5663{
ed2dc618
SM
5664 struct dwarf2_per_objfile *dwarf2_per_objfile
5665 = get_dwarf2_per_objfile (objfile);
5666
9291a0cd
TT
5667 /* If we're about to read full symbols, don't bother with the
5668 indices. In this case we also don't care if some other debug
5669 format is making psymtabs, because they are all about to be
5670 expanded anyway. */
5671 if ((objfile->flags & OBJF_READNOW))
5672 {
9291a0cd 5673 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5674 create_all_comp_units (dwarf2_per_objfile);
5675 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5676 dwarf2_per_objfile->quick_file_names_table
5677 = create_quick_file_names_table
5678 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5679
b76e467d 5680 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5681 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5682 {
ff4c9fec 5683 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5684
e254ef6a
DE
5685 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5686 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5687 }
5688
5689 /* Return 1 so that gdb sees the "quick" functions. However,
5690 these functions will be no-ops because we will have expanded
5691 all symtabs. */
3c0aa29a
PA
5692 *index_kind = dw_index_kind::GDB_INDEX;
5693 return true;
9291a0cd
TT
5694 }
5695
ed2dc618 5696 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5697 {
5698 *index_kind = dw_index_kind::DEBUG_NAMES;
5699 return true;
5700 }
927aa2e7 5701
4485a1c1
SM
5702 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5703 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5704 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5705 {
5706 *index_kind = dw_index_kind::GDB_INDEX;
5707 return true;
5708 }
9291a0cd 5709
87d6a7aa
SM
5710 /* ... otherwise, try to find the index in the index cache. */
5711 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5712 get_gdb_index_contents_from_cache,
5713 get_gdb_index_contents_from_cache_dwz))
5714 {
5715 global_index_cache.hit ();
5716 *index_kind = dw_index_kind::GDB_INDEX;
5717 return true;
5718 }
5719
5720 global_index_cache.miss ();
3c0aa29a 5721 return false;
9291a0cd
TT
5722}
5723
5724\f
5725
dce234bc
PP
5726/* Build a partial symbol table. */
5727
5728void
f29dff0a 5729dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5730{
ed2dc618
SM
5731 struct dwarf2_per_objfile *dwarf2_per_objfile
5732 = get_dwarf2_per_objfile (objfile);
c9bf0622 5733
6eee24ce 5734 init_psymbol_list (objfile, 1024);
c906108c 5735
a70b8144 5736 try
c9bf0622
TT
5737 {
5738 /* This isn't really ideal: all the data we allocate on the
5739 objfile's obstack is still uselessly kept around. However,
5740 freeing it seems unsafe. */
906768f9 5741 psymtab_discarder psymtabs (objfile);
ed2dc618 5742 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5743 psymtabs.keep ();
87d6a7aa
SM
5744
5745 /* (maybe) store an index in the cache. */
5746 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5747 }
230d2906 5748 catch (const gdb_exception_error &except)
492d29ea
PA
5749 {
5750 exception_print (gdb_stderr, except);
5751 }
c906108c 5752}
c906108c 5753
3b80fe9b
DE
5754/* Find the base address of the compilation unit for range lists and
5755 location lists. It will normally be specified by DW_AT_low_pc.
5756 In DWARF-3 draft 4, the base address could be overridden by
5757 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5758 compilation units with discontinuous ranges. */
5759
5760static void
5761dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5762{
5763 struct attribute *attr;
5764
2b24b6e4 5765 cu->base_address.reset ();
3b80fe9b
DE
5766
5767 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5768 if (attr != nullptr)
2b24b6e4 5769 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5770 else
5771 {
5772 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5773 if (attr != nullptr)
2b24b6e4 5774 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5775 }
5776}
5777
36586728
TT
5778/* Helper function that returns the proper abbrev section for
5779 THIS_CU. */
5780
5781static struct dwarf2_section_info *
5782get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5783{
5784 struct dwarf2_section_info *abbrev;
ed2dc618 5785 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5786
5787 if (this_cu->is_dwz)
ed2dc618 5788 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5789 else
5790 abbrev = &dwarf2_per_objfile->abbrev;
5791
5792 return abbrev;
5793}
5794
f4dc4d17
DE
5795/* Fetch the abbreviation table offset from a comp or type unit header. */
5796
5797static sect_offset
ed2dc618
SM
5798read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5799 struct dwarf2_section_info *section,
9c541725 5800 sect_offset sect_off)
f4dc4d17 5801{
96b79293 5802 bfd *abfd = section->get_bfd_owner ();
d521ce57 5803 const gdb_byte *info_ptr;
ac298888 5804 unsigned int initial_length_size, offset_size;
43988095 5805 uint16_t version;
f4dc4d17 5806
96b79293 5807 section->read (dwarf2_per_objfile->objfile);
9c541725 5808 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5809 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5810 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5811 info_ptr += initial_length_size;
5812
5813 version = read_2_bytes (abfd, info_ptr);
5814 info_ptr += 2;
5815 if (version >= 5)
5816 {
5817 /* Skip unit type and address size. */
5818 info_ptr += 2;
5819 }
5820
24aa364d 5821 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5822}
5823
b83470bf
TT
5824/* A partial symtab that is used only for include files. */
5825struct dwarf2_include_psymtab : public partial_symtab
5826{
5827 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5828 : partial_symtab (filename, objfile)
5829 {
5830 }
5831
5832 void read_symtab (struct objfile *objfile) override
5833 {
5834 expand_psymtab (objfile);
5835 }
5836
5837 void expand_psymtab (struct objfile *objfile) override
5838 {
5839 if (m_readin)
5840 return;
5841 /* It's an include file, no symbols to read for it.
5842 Everything is in the parent symtab. */
5843 read_dependencies (objfile);
5844 m_readin = true;
5845 }
5846
5847 bool readin_p () const override
5848 {
5849 return m_readin;
5850 }
5851
5852 struct compunit_symtab *get_compunit_symtab () const override
5853 {
5854 return nullptr;
5855 }
5856
5857private:
5858
5859 bool m_readin = false;
5860};
5861
aaa75496
JB
5862/* Allocate a new partial symtab for file named NAME and mark this new
5863 partial symtab as being an include of PST. */
5864
5865static void
891813be 5866dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5867 struct objfile *objfile)
5868{
b83470bf 5869 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 5870
fbd9ab74
JK
5871 if (!IS_ABSOLUTE_PATH (subpst->filename))
5872 {
5873 /* It shares objfile->objfile_obstack. */
5874 subpst->dirname = pst->dirname;
5875 }
5876
a9342b62 5877 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5878 subpst->dependencies[0] = pst;
5879 subpst->number_of_dependencies = 1;
aaa75496
JB
5880}
5881
5882/* Read the Line Number Program data and extract the list of files
5883 included by the source file represented by PST. Build an include
d85a05f0 5884 partial symtab for each of these included files. */
aaa75496
JB
5885
5886static void
5887dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5888 struct die_info *die,
891813be 5889 dwarf2_psymtab *pst)
aaa75496 5890{
fff8551c 5891 line_header_up lh;
d85a05f0 5892 struct attribute *attr;
aaa75496 5893
d85a05f0 5894 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5895 if (attr != nullptr)
9c541725 5896 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5897 if (lh == NULL)
5898 return; /* No linetable, so no includes. */
5899
79748972
TT
5900 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5901 that we pass in the raw text_low here; that is ok because we're
5902 only decoding the line table to make include partial symtabs, and
5903 so the addresses aren't really used. */
4ae976d1 5904 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5905 pst->raw_text_low (), 1);
aaa75496
JB
5906}
5907
348e048f 5908static hashval_t
52dc124a 5909hash_signatured_type (const void *item)
348e048f 5910{
9a3c8263
SM
5911 const struct signatured_type *sig_type
5912 = (const struct signatured_type *) item;
9a619af0 5913
348e048f 5914 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5915 return sig_type->signature;
348e048f
DE
5916}
5917
5918static int
52dc124a 5919eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5920{
9a3c8263
SM
5921 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5922 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5923
348e048f
DE
5924 return lhs->signature == rhs->signature;
5925}
5926
1fd400ff
TT
5927/* Allocate a hash table for signatured types. */
5928
b0b6a987 5929static htab_up
298e9637 5930allocate_signatured_type_table ()
1fd400ff 5931{
b0b6a987
TT
5932 return htab_up (htab_create_alloc (41,
5933 hash_signatured_type,
5934 eq_signatured_type,
5935 NULL, xcalloc, xfree));
1fd400ff
TT
5936}
5937
d467dd73 5938/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5939
5940static int
d467dd73 5941add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5942{
9a3c8263 5943 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
5944 std::vector<signatured_type *> *all_type_units
5945 = (std::vector<signatured_type *> *) datum;
1fd400ff 5946
b2bdb8cf 5947 all_type_units->push_back (sigt);
1fd400ff
TT
5948
5949 return 1;
5950}
5951
78d4d2c5 5952/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5953 and fill them into TYPES_HTAB. It will process only type units,
5954 therefore DW_UT_type. */
c88ee1f0 5955
78d4d2c5 5956static void
ed2dc618
SM
5957create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5958 struct dwo_file *dwo_file,
b0b6a987 5959 dwarf2_section_info *section, htab_up &types_htab,
43988095 5960 rcuh_kind section_kind)
348e048f 5961{
3019eac3 5962 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 5963 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5964 bfd *abfd;
5965 const gdb_byte *info_ptr, *end_ptr;
348e048f 5966
4bdcc0c1
DE
5967 abbrev_section = (dwo_file != NULL
5968 ? &dwo_file->sections.abbrev
5969 : &dwarf2_per_objfile->abbrev);
5970
b4f54984 5971 if (dwarf_read_debug)
43988095 5972 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
5973 section->get_name (),
5974 abbrev_section->get_file_name ());
09406207 5975
96b79293 5976 section->read (objfile);
78d4d2c5 5977 info_ptr = section->buffer;
348e048f 5978
78d4d2c5
JK
5979 if (info_ptr == NULL)
5980 return;
348e048f 5981
78d4d2c5
JK
5982 /* We can't set abfd until now because the section may be empty or
5983 not present, in which case the bfd is unknown. */
96b79293 5984 abfd = section->get_bfd_owner ();
348e048f 5985
c0ab21c2
TT
5986 /* We don't use cutu_reader here because we don't need to read
5987 any dies: the signature is in the header. */
3019eac3 5988
78d4d2c5
JK
5989 end_ptr = info_ptr + section->size;
5990 while (info_ptr < end_ptr)
5991 {
78d4d2c5
JK
5992 struct signatured_type *sig_type;
5993 struct dwo_unit *dwo_tu;
5994 void **slot;
5995 const gdb_byte *ptr = info_ptr;
5996 struct comp_unit_head header;
5997 unsigned int length;
8b70b953 5998
9c541725 5999 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6000
a49dd8dd
JK
6001 /* Initialize it due to a false compiler warning. */
6002 header.signature = -1;
9c541725 6003 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6004
78d4d2c5
JK
6005 /* We need to read the type's signature in order to build the hash
6006 table, but we don't need anything else just yet. */
348e048f 6007
ed2dc618 6008 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6009 abbrev_section, ptr, section_kind);
348e048f 6010
4057dfde 6011 length = header.get_length ();
6caca83c 6012
78d4d2c5
JK
6013 /* Skip dummy type units. */
6014 if (ptr >= info_ptr + length
43988095
JK
6015 || peek_abbrev_code (abfd, ptr) == 0
6016 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6017 {
6018 info_ptr += length;
6019 continue;
6020 }
dee91e82 6021
78d4d2c5
JK
6022 if (types_htab == NULL)
6023 {
6024 if (dwo_file)
298e9637 6025 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6026 else
298e9637 6027 types_htab = allocate_signatured_type_table ();
78d4d2c5 6028 }
8b70b953 6029
78d4d2c5
JK
6030 if (dwo_file)
6031 {
6032 sig_type = NULL;
6033 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6034 struct dwo_unit);
6035 dwo_tu->dwo_file = dwo_file;
43988095 6036 dwo_tu->signature = header.signature;
9c541725 6037 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6038 dwo_tu->section = section;
9c541725 6039 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6040 dwo_tu->length = length;
6041 }
6042 else
6043 {
6044 /* N.B.: type_offset is not usable if this type uses a DWO file.
6045 The real type_offset is in the DWO file. */
6046 dwo_tu = NULL;
6047 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6048 struct signatured_type);
43988095 6049 sig_type->signature = header.signature;
9c541725 6050 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6051 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6052 sig_type->per_cu.is_debug_types = 1;
6053 sig_type->per_cu.section = section;
9c541725 6054 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6055 sig_type->per_cu.length = length;
6056 }
6057
b0b6a987 6058 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6059 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6060 INSERT);
6061 gdb_assert (slot != NULL);
6062 if (*slot != NULL)
6063 {
9c541725 6064 sect_offset dup_sect_off;
0349ea22 6065
3019eac3
DE
6066 if (dwo_file)
6067 {
78d4d2c5
JK
6068 const struct dwo_unit *dup_tu
6069 = (const struct dwo_unit *) *slot;
6070
9c541725 6071 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6072 }
6073 else
6074 {
78d4d2c5
JK
6075 const struct signatured_type *dup_tu
6076 = (const struct signatured_type *) *slot;
6077
9c541725 6078 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6079 }
8b70b953 6080
b98664d3 6081 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6082 " the entry at offset %s, signature %s"),
6083 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6084 hex_string (header.signature));
78d4d2c5
JK
6085 }
6086 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6087
78d4d2c5 6088 if (dwarf_read_debug > 1)
9d8780f0
SM
6089 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6090 sect_offset_str (sect_off),
43988095 6091 hex_string (header.signature));
3019eac3 6092
78d4d2c5
JK
6093 info_ptr += length;
6094 }
6095}
3019eac3 6096
78d4d2c5
JK
6097/* Create the hash table of all entries in the .debug_types
6098 (or .debug_types.dwo) section(s).
6099 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6100 otherwise it is NULL.
b3c8eb43 6101
78d4d2c5 6102 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6103
78d4d2c5 6104 Note: This function processes DWO files only, not DWP files. */
348e048f 6105
78d4d2c5 6106static void
ed2dc618
SM
6107create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6108 struct dwo_file *dwo_file,
fd5866f6 6109 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6110 htab_up &types_htab)
78d4d2c5 6111{
fd5866f6
SM
6112 for (dwarf2_section_info &section : type_sections)
6113 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6114 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6115}
6116
6117/* Create the hash table of all entries in the .debug_types section,
6118 and initialize all_type_units.
6119 The result is zero if there is an error (e.g. missing .debug_types section),
6120 otherwise non-zero. */
6121
6122static int
ed2dc618 6123create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6124{
b0b6a987 6125 htab_up types_htab;
3019eac3 6126
ed2dc618
SM
6127 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6128 &dwarf2_per_objfile->info, types_htab,
43988095 6129 rcuh_kind::COMPILE);
ed2dc618
SM
6130 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6131 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6132 if (types_htab == NULL)
6133 {
6134 dwarf2_per_objfile->signatured_types = NULL;
6135 return 0;
6136 }
6137
b0b6a987 6138 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6139
b2bdb8cf 6140 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6141 dwarf2_per_objfile->all_type_units.reserve
6142 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6143
b0b6a987
TT
6144 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6145 add_signatured_type_cu_to_table,
b2bdb8cf 6146 &dwarf2_per_objfile->all_type_units);
1fd400ff 6147
348e048f
DE
6148 return 1;
6149}
6150
6aa5f3a6
DE
6151/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6152 If SLOT is non-NULL, it is the entry to use in the hash table.
6153 Otherwise we find one. */
6154
6155static struct signatured_type *
ed2dc618
SM
6156add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6157 void **slot)
6aa5f3a6
DE
6158{
6159 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6160
b2bdb8cf
SM
6161 if (dwarf2_per_objfile->all_type_units.size ()
6162 == dwarf2_per_objfile->all_type_units.capacity ())
6163 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6164
b2bdb8cf
SM
6165 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6166 struct signatured_type);
6167
6168 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6169 sig_type->signature = sig;
6170 sig_type->per_cu.is_debug_types = 1;
6171 if (dwarf2_per_objfile->using_index)
6172 {
6173 sig_type->per_cu.v.quick =
6174 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6175 struct dwarf2_per_cu_quick_data);
6176 }
6177
6178 if (slot == NULL)
6179 {
b0b6a987 6180 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6181 sig_type, INSERT);
6182 }
6183 gdb_assert (*slot == NULL);
6184 *slot = sig_type;
6185 /* The rest of sig_type must be filled in by the caller. */
6186 return sig_type;
6187}
6188
a2ce51a0
DE
6189/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6190 Fill in SIG_ENTRY with DWO_ENTRY. */
6191
6192static void
ed2dc618 6193fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6194 struct signatured_type *sig_entry,
6195 struct dwo_unit *dwo_entry)
6196{
7ee85ab1 6197 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6198 gdb_assert (! sig_entry->per_cu.queued);
6199 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6200 if (dwarf2_per_objfile->using_index)
6201 {
6202 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6203 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6204 }
6205 else
6206 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6207 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6208 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6209 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6210 gdb_assert (sig_entry->dwo_unit == NULL);
6211
6212 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6213 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6214 sig_entry->per_cu.length = dwo_entry->length;
6215 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6216 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6217 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6218 sig_entry->dwo_unit = dwo_entry;
6219}
6220
6221/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6222 If we haven't read the TU yet, create the signatured_type data structure
6223 for a TU to be read in directly from a DWO file, bypassing the stub.
6224 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6225 using .gdb_index, then when reading a CU we want to stay in the DWO file
6226 containing that CU. Otherwise we could end up reading several other DWO
6227 files (due to comdat folding) to process the transitive closure of all the
6228 mentioned TUs, and that can be slow. The current DWO file will have every
6229 type signature that it needs.
a2ce51a0
DE
6230 We only do this for .gdb_index because in the psymtab case we already have
6231 to read all the DWOs to build the type unit groups. */
6232
6233static struct signatured_type *
6234lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6235{
518817b3
SM
6236 struct dwarf2_per_objfile *dwarf2_per_objfile
6237 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6238 struct dwo_file *dwo_file;
6239 struct dwo_unit find_dwo_entry, *dwo_entry;
6240 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6241 void **slot;
a2ce51a0
DE
6242
6243 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6244
6aa5f3a6
DE
6245 /* If TU skeletons have been removed then we may not have read in any
6246 TUs yet. */
6247 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6248 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6249
6250 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6251 Use the global signatured_types array to do our own comdat-folding
6252 of types. If this is the first time we're reading this TU, and
6253 the TU has an entry in .gdb_index, replace the recorded data from
6254 .gdb_index with this TU. */
a2ce51a0 6255
a2ce51a0 6256 find_sig_entry.signature = sig;
b0b6a987 6257 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6258 &find_sig_entry, INSERT);
9a3c8263 6259 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6260
6261 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6262 read. Don't reassign the global entry to point to this DWO if that's
6263 the case. Also note that if the TU is already being read, it may not
6264 have come from a DWO, the program may be a mix of Fission-compiled
6265 code and non-Fission-compiled code. */
6266
6267 /* Have we already tried to read this TU?
6268 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6269 needn't exist in the global table yet). */
6270 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6271 return sig_entry;
6272
6aa5f3a6
DE
6273 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6274 dwo_unit of the TU itself. */
6275 dwo_file = cu->dwo_unit->dwo_file;
6276
a2ce51a0
DE
6277 /* Ok, this is the first time we're reading this TU. */
6278 if (dwo_file->tus == NULL)
6279 return NULL;
6280 find_dwo_entry.signature = sig;
b0b6a987
TT
6281 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6282 &find_dwo_entry);
a2ce51a0
DE
6283 if (dwo_entry == NULL)
6284 return NULL;
6285
6aa5f3a6
DE
6286 /* If the global table doesn't have an entry for this TU, add one. */
6287 if (sig_entry == NULL)
ed2dc618 6288 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6289
ed2dc618 6290 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6291 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6292 return sig_entry;
6293}
6294
a2ce51a0
DE
6295/* Subroutine of lookup_signatured_type.
6296 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6297 then try the DWP file. If the TU stub (skeleton) has been removed then
6298 it won't be in .gdb_index. */
a2ce51a0
DE
6299
6300static struct signatured_type *
6301lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6302{
518817b3
SM
6303 struct dwarf2_per_objfile *dwarf2_per_objfile
6304 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6305 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6306 struct dwo_unit *dwo_entry;
6307 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6308 void **slot;
a2ce51a0
DE
6309
6310 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6311 gdb_assert (dwp_file != NULL);
6312
6aa5f3a6
DE
6313 /* If TU skeletons have been removed then we may not have read in any
6314 TUs yet. */
6315 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6316 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6317
6aa5f3a6 6318 find_sig_entry.signature = sig;
b0b6a987 6319 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6320 &find_sig_entry, INSERT);
9a3c8263 6321 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6322
6323 /* Have we already tried to read this TU?
6324 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6325 needn't exist in the global table yet). */
6326 if (sig_entry != NULL)
6327 return sig_entry;
6328
a2ce51a0
DE
6329 if (dwp_file->tus == NULL)
6330 return NULL;
ed2dc618 6331 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6332 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6333 if (dwo_entry == NULL)
6334 return NULL;
6335
ed2dc618
SM
6336 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6337 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6338
a2ce51a0
DE
6339 return sig_entry;
6340}
6341
380bca97 6342/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6343 Returns NULL if signature SIG is not present in the table.
6344 It is up to the caller to complain about this. */
348e048f
DE
6345
6346static struct signatured_type *
a2ce51a0 6347lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6348{
518817b3
SM
6349 struct dwarf2_per_objfile *dwarf2_per_objfile
6350 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6351
a2ce51a0
DE
6352 if (cu->dwo_unit
6353 && dwarf2_per_objfile->using_index)
6354 {
6355 /* We're in a DWO/DWP file, and we're using .gdb_index.
6356 These cases require special processing. */
ed2dc618 6357 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6358 return lookup_dwo_signatured_type (cu, sig);
6359 else
6360 return lookup_dwp_signatured_type (cu, sig);
6361 }
6362 else
6363 {
6364 struct signatured_type find_entry, *entry;
348e048f 6365
a2ce51a0
DE
6366 if (dwarf2_per_objfile->signatured_types == NULL)
6367 return NULL;
6368 find_entry.signature = sig;
9a3c8263 6369 entry = ((struct signatured_type *)
b0b6a987
TT
6370 htab_find (dwarf2_per_objfile->signatured_types.get (),
6371 &find_entry));
a2ce51a0
DE
6372 return entry;
6373 }
348e048f 6374}
18a8505e 6375
42e7ad6c 6376/* Low level DIE reading support. */
348e048f 6377
d85a05f0
DJ
6378/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6379
6380static void
6381init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6382 struct dwarf2_cu *cu,
3019eac3 6383 struct dwarf2_section_info *section,
685af9cd
TT
6384 struct dwo_file *dwo_file,
6385 struct abbrev_table *abbrev_table)
d85a05f0 6386{
fceca515 6387 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6388 reader->abfd = section->get_bfd_owner ();
d85a05f0 6389 reader->cu = cu;
3019eac3 6390 reader->dwo_file = dwo_file;
dee91e82
DE
6391 reader->die_section = section;
6392 reader->buffer = section->buffer;
f664829e 6393 reader->buffer_end = section->buffer + section->size;
685af9cd 6394 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6395}
6396
c0ab21c2 6397/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6398 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6399 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6400 already.
6401
6402 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6403 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6404 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6405 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6406 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6407 STUB_COMP_DIR may be non-NULL.
3e225074 6408 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6409 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6410 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6411 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6412 kept around for at least as long as *RESULT_READER.
6413
b0c7bfa9
DE
6414 The result is non-zero if a valid (non-dummy) DIE was found. */
6415
6416static int
6417read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6418 struct dwo_unit *dwo_unit,
b0c7bfa9 6419 struct die_info *stub_comp_unit_die,
a2ce51a0 6420 const char *stub_comp_dir,
b0c7bfa9 6421 struct die_reader_specs *result_reader,
d521ce57 6422 const gdb_byte **result_info_ptr,
b0c7bfa9 6423 struct die_info **result_comp_unit_die,
685af9cd 6424 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6425{
ed2dc618 6426 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6427 struct objfile *objfile = dwarf2_per_objfile->objfile;
6428 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6429 bfd *abfd;
d521ce57 6430 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6431 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6432 int i,num_extra_attrs;
6433 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6434 struct die_info *comp_unit_die;
6435
b0aeadb3
DE
6436 /* At most one of these may be provided. */
6437 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6438
b0c7bfa9
DE
6439 /* These attributes aren't processed until later:
6440 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6441 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6442 referenced later. However, these attributes are found in the stub
6443 which we won't have later. In order to not impose this complication
6444 on the rest of the code, we read them here and copy them to the
6445 DWO CU/TU die. */
b0c7bfa9
DE
6446
6447 stmt_list = NULL;
6448 low_pc = NULL;
6449 high_pc = NULL;
6450 ranges = NULL;
6451 comp_dir = NULL;
6452
6453 if (stub_comp_unit_die != NULL)
6454 {
6455 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6456 DWO file. */
6457 if (! this_cu->is_debug_types)
6458 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6459 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6460 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6461 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6462 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6463
a39fdb41 6464 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6465
18a8505e
AT
6466 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6467 here (if needed). We need the value before we can process
6468 DW_AT_ranges. */
a39fdb41 6469 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6470 }
a2ce51a0
DE
6471 else if (stub_comp_dir != NULL)
6472 {
6473 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6474 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6475 comp_dir->name = DW_AT_comp_dir;
6476 comp_dir->form = DW_FORM_string;
6477 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6478 DW_STRING (comp_dir) = stub_comp_dir;
6479 }
b0c7bfa9
DE
6480
6481 /* Set up for reading the DWO CU/TU. */
6482 cu->dwo_unit = dwo_unit;
685af9cd 6483 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6484 section->read (objfile);
6485 abfd = section->get_bfd_owner ();
9c541725
PA
6486 begin_info_ptr = info_ptr = (section->buffer
6487 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6488 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6489
6490 if (this_cu->is_debug_types)
6491 {
b0c7bfa9
DE
6492 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6493
ed2dc618
SM
6494 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6495 &cu->header, section,
b0c7bfa9 6496 dwo_abbrev_section,
43988095 6497 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6498 /* This is not an assert because it can be caused by bad debug info. */
43988095 6499 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6500 {
6501 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6502 " TU at offset %s [in module %s]"),
a2ce51a0 6503 hex_string (sig_type->signature),
43988095 6504 hex_string (cu->header.signature),
9d8780f0 6505 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6506 bfd_get_filename (abfd));
6507 }
9c541725 6508 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6509 /* For DWOs coming from DWP files, we don't know the CU length
6510 nor the type's offset in the TU until now. */
4057dfde 6511 dwo_unit->length = cu->header.get_length ();
9c541725 6512 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6513
6514 /* Establish the type offset that can be used to lookup the type.
6515 For DWO files, we don't know it until now. */
9c541725
PA
6516 sig_type->type_offset_in_section
6517 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6518 }
6519 else
6520 {
ed2dc618
SM
6521 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6522 &cu->header, section,
b0c7bfa9 6523 dwo_abbrev_section,
43988095 6524 info_ptr, rcuh_kind::COMPILE);
9c541725 6525 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6526 /* For DWOs coming from DWP files, we don't know the CU length
6527 until now. */
4057dfde 6528 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6529 }
6530
685af9cd 6531 *result_dwo_abbrev_table
86de1d91
TT
6532 = abbrev_table::read (objfile, dwo_abbrev_section,
6533 cu->header.abbrev_sect_off);
685af9cd
TT
6534 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6535 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6536
6537 /* Read in the die, but leave space to copy over the attributes
6538 from the stub. This has the benefit of simplifying the rest of
6539 the code - all the work to maintain the illusion of a single
6540 DW_TAG_{compile,type}_unit DIE is done here. */
6541 num_extra_attrs = ((stmt_list != NULL)
6542 + (low_pc != NULL)
6543 + (high_pc != NULL)
6544 + (ranges != NULL)
6545 + (comp_dir != NULL));
6546 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6547 num_extra_attrs);
b0c7bfa9
DE
6548
6549 /* Copy over the attributes from the stub to the DIE we just read in. */
6550 comp_unit_die = *result_comp_unit_die;
6551 i = comp_unit_die->num_attrs;
6552 if (stmt_list != NULL)
6553 comp_unit_die->attrs[i++] = *stmt_list;
6554 if (low_pc != NULL)
6555 comp_unit_die->attrs[i++] = *low_pc;
6556 if (high_pc != NULL)
6557 comp_unit_die->attrs[i++] = *high_pc;
6558 if (ranges != NULL)
6559 comp_unit_die->attrs[i++] = *ranges;
6560 if (comp_dir != NULL)
6561 comp_unit_die->attrs[i++] = *comp_dir;
6562 comp_unit_die->num_attrs += num_extra_attrs;
6563
b4f54984 6564 if (dwarf_die_debug)
bf6af496
DE
6565 {
6566 fprintf_unfiltered (gdb_stdlog,
6567 "Read die from %s@0x%x of %s:\n",
96b79293 6568 section->get_name (),
bf6af496
DE
6569 (unsigned) (begin_info_ptr - section->buffer),
6570 bfd_get_filename (abfd));
b4f54984 6571 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6572 }
6573
b0c7bfa9
DE
6574 /* Skip dummy compilation units. */
6575 if (info_ptr >= begin_info_ptr + dwo_unit->length
6576 || peek_abbrev_code (abfd, info_ptr) == 0)
6577 return 0;
6578
6579 *result_info_ptr = info_ptr;
6580 return 1;
6581}
6582
a084a2a6
AT
6583/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6584 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6585 signature is part of the header. */
6586static gdb::optional<ULONGEST>
6587lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6588{
6589 if (cu->header.version >= 5)
6590 return cu->header.signature;
6591 struct attribute *attr;
6592 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6593 if (attr == nullptr)
6594 return gdb::optional<ULONGEST> ();
6595 return DW_UNSND (attr);
6596}
6597
c0ab21c2 6598/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6599 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6600 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6601
6602static struct dwo_unit *
6603lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6604 struct die_info *comp_unit_die,
6605 const char *dwo_name)
b0c7bfa9
DE
6606{
6607 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6608 struct dwo_unit *dwo_unit;
c0ab21c2 6609 const char *comp_dir;
b0c7bfa9 6610
a2ce51a0
DE
6611 gdb_assert (cu != NULL);
6612
b0c7bfa9 6613 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6614 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6615 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6616
6617 if (this_cu->is_debug_types)
6618 {
6619 struct signatured_type *sig_type;
6620
6621 /* Since this_cu is the first member of struct signatured_type,
6622 we can go from a pointer to one to a pointer to the other. */
6623 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6624 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6625 }
6626 else
6627 {
a084a2a6
AT
6628 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6629 if (!signature.has_value ())
b0c7bfa9
DE
6630 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6631 " [in module %s]"),
e3b94546 6632 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6633 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6634 *signature);
b0c7bfa9
DE
6635 }
6636
b0c7bfa9
DE
6637 return dwo_unit;
6638}
6639
c0ab21c2 6640/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6641 See it for a description of the parameters.
fcd3b13d 6642 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6643
c0ab21c2
TT
6644void
6645cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6646 int use_existing_cu)
a2ce51a0 6647{
a2ce51a0 6648 struct signatured_type *sig_type;
a2ce51a0
DE
6649
6650 /* Verify we can do the following downcast, and that we have the
6651 data we need. */
6652 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6653 sig_type = (struct signatured_type *) this_cu;
6654 gdb_assert (sig_type->dwo_unit != NULL);
6655
6aa5f3a6
DE
6656 if (use_existing_cu && this_cu->cu != NULL)
6657 {
6658 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6659 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6660 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6661 }
6662 else
6663 {
6664 /* If !use_existing_cu, this_cu->cu must be NULL. */
6665 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6666 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6667 }
6668
6669 /* A future optimization, if needed, would be to use an existing
6670 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6671 could share abbrev tables. */
a2ce51a0
DE
6672
6673 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6674 NULL /* stub_comp_unit_die */,
6675 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6676 this, &info_ptr,
3e225074 6677 &comp_unit_die,
c0ab21c2 6678 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6679 {
6680 /* Dummy die. */
c0ab21c2 6681 dummy_p = true;
a2ce51a0 6682 }
a2ce51a0
DE
6683}
6684
fd820528 6685/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6686 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6687
f4dc4d17
DE
6688 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6689 Otherwise the table specified in the comp unit header is read in and used.
6690 This is an optimization for when we already have the abbrev table.
6691
dee91e82 6692 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6693 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6694
c0ab21c2
TT
6695cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6696 struct abbrev_table *abbrev_table,
6751ebae 6697 int use_existing_cu,
c0ab21c2
TT
6698 bool skip_partial)
6699 : die_reader_specs {},
6751ebae 6700 m_this_cu (this_cu)
c906108c 6701{
ed2dc618 6702 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6703 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6704 struct dwarf2_section_info *section = this_cu->section;
96b79293 6705 bfd *abfd = section->get_bfd_owner ();
dee91e82 6706 struct dwarf2_cu *cu;
c0ab21c2 6707 const gdb_byte *begin_info_ptr;
dee91e82 6708 struct signatured_type *sig_type = NULL;
4bdcc0c1 6709 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6710 /* Non-zero if CU currently points to a DWO file and we need to
6711 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6712 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6713 int rereading_dwo_cu = 0;
c906108c 6714
b4f54984 6715 if (dwarf_die_debug)
9d8780f0 6716 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6717 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6718 sect_offset_str (this_cu->sect_off));
09406207 6719
a2ce51a0
DE
6720 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6721 file (instead of going through the stub), short-circuit all of this. */
6722 if (this_cu->reading_dwo_directly)
6723 {
6724 /* Narrow down the scope of possibilities to have to understand. */
6725 gdb_assert (this_cu->is_debug_types);
6726 gdb_assert (abbrev_table == NULL);
6751ebae 6727 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6728 return;
6729 }
6730
dee91e82 6731 /* This is cheap if the section is already read in. */
96b79293 6732 section->read (objfile);
dee91e82 6733
9c541725 6734 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6735
6736 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6737
6738 if (use_existing_cu && this_cu->cu != NULL)
6739 {
6740 cu = this_cu->cu;
42e7ad6c
DE
6741 /* If this CU is from a DWO file we need to start over, we need to
6742 refetch the attributes from the skeleton CU.
6743 This could be optimized by retrieving those attributes from when we
6744 were here the first time: the previous comp_unit_die was stored in
6745 comp_unit_obstack. But there's no data yet that we need this
6746 optimization. */
6747 if (cu->dwo_unit != NULL)
6748 rereading_dwo_cu = 1;
dee91e82
DE
6749 }
6750 else
6751 {
6752 /* If !use_existing_cu, this_cu->cu must be NULL. */
6753 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6754 m_new_cu.reset (new dwarf2_cu (this_cu));
6755 cu = m_new_cu.get ();
42e7ad6c 6756 }
dee91e82 6757
b0c7bfa9 6758 /* Get the header. */
9c541725 6759 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6760 {
6761 /* We already have the header, there's no need to read it in again. */
9c541725 6762 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6763 }
6764 else
6765 {
3019eac3 6766 if (this_cu->is_debug_types)
dee91e82 6767 {
ed2dc618
SM
6768 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6769 &cu->header, section,
4bdcc0c1 6770 abbrev_section, info_ptr,
43988095 6771 rcuh_kind::TYPE);
dee91e82 6772
42e7ad6c
DE
6773 /* Since per_cu is the first member of struct signatured_type,
6774 we can go from a pointer to one to a pointer to the other. */
6775 sig_type = (struct signatured_type *) this_cu;
43988095 6776 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6777 gdb_assert (sig_type->type_offset_in_tu
6778 == cu->header.type_cu_offset_in_tu);
6779 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6780
42e7ad6c
DE
6781 /* LENGTH has not been set yet for type units if we're
6782 using .gdb_index. */
4057dfde 6783 this_cu->length = cu->header.get_length ();
3019eac3
DE
6784
6785 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6786 sig_type->type_offset_in_section =
6787 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6788
6789 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6790 }
6791 else
6792 {
ed2dc618
SM
6793 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6794 &cu->header, section,
4bdcc0c1 6795 abbrev_section,
43988095
JK
6796 info_ptr,
6797 rcuh_kind::COMPILE);
dee91e82 6798
9c541725 6799 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6800 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6801 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6802 }
6803 }
10b3939b 6804
6caca83c 6805 /* Skip dummy compilation units. */
dee91e82 6806 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6807 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6808 {
6809 dummy_p = true;
6810 return;
6811 }
6caca83c 6812
433df2d4
DE
6813 /* If we don't have them yet, read the abbrevs for this compilation unit.
6814 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6815 done. */
f4dc4d17 6816 if (abbrev_table != NULL)
685af9cd
TT
6817 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6818 else
f4dc4d17 6819 {
c0ab21c2 6820 m_abbrev_table_holder
86de1d91
TT
6821 = abbrev_table::read (objfile, abbrev_section,
6822 cu->header.abbrev_sect_off);
c0ab21c2 6823 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6824 }
af703f96 6825
dee91e82 6826 /* Read the top level CU/TU die. */
c0ab21c2 6827 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6828 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6829
58f0c718 6830 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6831 {
6832 dummy_p = true;
6833 return;
6834 }
58f0c718 6835
b0c7bfa9 6836 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6837 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6838 table from the DWO file and pass the ownership over to us. It will be
6839 referenced from READER, so we must make sure to free it after we're done
6840 with READER.
6841
b0c7bfa9
DE
6842 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6843 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6844 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6845 if (dwo_name != nullptr)
3019eac3 6846 {
3019eac3 6847 struct dwo_unit *dwo_unit;
b0c7bfa9 6848 struct die_info *dwo_comp_unit_die;
3019eac3 6849
3e225074 6850 if (comp_unit_die->has_children)
6a506a2d 6851 {
b98664d3 6852 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6853 " has children (offset %s) [in module %s]"),
6854 sect_offset_str (this_cu->sect_off),
6855 bfd_get_filename (abfd));
6a506a2d 6856 }
c0ab21c2 6857 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6858 if (dwo_unit != NULL)
3019eac3 6859 {
6a506a2d 6860 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6861 comp_unit_die, NULL,
c0ab21c2 6862 this, &info_ptr,
3e225074 6863 &dwo_comp_unit_die,
c0ab21c2 6864 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6865 {
6866 /* Dummy die. */
c0ab21c2 6867 dummy_p = true;
6a506a2d
DE
6868 return;
6869 }
6870 comp_unit_die = dwo_comp_unit_die;
6871 }
6872 else
6873 {
6874 /* Yikes, we couldn't find the rest of the DIE, we only have
6875 the stub. A complaint has already been logged. There's
6876 not much more we can do except pass on the stub DIE to
6877 die_reader_func. We don't want to throw an error on bad
6878 debug info. */
3019eac3
DE
6879 }
6880 }
c0ab21c2 6881}
3019eac3 6882
6751ebae
TT
6883void
6884cutu_reader::keep ()
c0ab21c2 6885{
b0c7bfa9 6886 /* Done, clean up. */
6751ebae
TT
6887 gdb_assert (!dummy_p);
6888 if (m_new_cu != NULL)
348e048f 6889 {
c0ab21c2
TT
6890 struct dwarf2_per_objfile *dwarf2_per_objfile
6891 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6892 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6893 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6894 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6895 /* The chain owns it now. */
c0ab21c2 6896 m_new_cu.release ();
348e048f 6897 }
dee91e82
DE
6898}
6899
18a8505e
AT
6900/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6901 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6902 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6903
6904 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6905 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6906
6907 We fill in THIS_CU->length.
6908
dee91e82 6909 THIS_CU->cu is always freed when done.
3019eac3 6910 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6911 to care whether it refers to the "main" CU or the DWO CU.
6912
6913 When parent_cu is passed, it is used to provide a default value for
6914 str_offsets_base and addr_base from the parent. */
dee91e82 6915
c0ab21c2
TT
6916cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6917 struct dwarf2_cu *parent_cu,
6918 struct dwo_file *dwo_file)
6919 : die_reader_specs {},
6920 m_this_cu (this_cu)
dee91e82 6921{
ed2dc618 6922 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6923 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6924 struct dwarf2_section_info *section = this_cu->section;
96b79293 6925 bfd *abfd = section->get_bfd_owner ();
33e80786 6926 struct dwarf2_section_info *abbrev_section;
d521ce57 6927 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6928
b4f54984 6929 if (dwarf_die_debug)
9d8780f0 6930 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6931 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6932 sect_offset_str (this_cu->sect_off));
09406207 6933
dee91e82
DE
6934 gdb_assert (this_cu->cu == NULL);
6935
33e80786
DE
6936 abbrev_section = (dwo_file != NULL
6937 ? &dwo_file->sections.abbrev
6938 : get_abbrev_section_for_cu (this_cu));
6939
dee91e82 6940 /* This is cheap if the section is already read in. */
96b79293 6941 section->read (objfile);
dee91e82 6942
c0ab21c2 6943 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 6944
9c541725 6945 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 6946 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 6947 &m_new_cu->header, section,
4bdcc0c1 6948 abbrev_section, info_ptr,
43988095
JK
6949 (this_cu->is_debug_types
6950 ? rcuh_kind::TYPE
6951 : rcuh_kind::COMPILE));
dee91e82 6952
18a8505e
AT
6953 if (parent_cu != nullptr)
6954 {
c0ab21c2
TT
6955 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6956 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6957 }
4057dfde 6958 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
6959
6960 /* Skip dummy compilation units. */
6961 if (info_ptr >= begin_info_ptr + this_cu->length
6962 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6963 {
6964 dummy_p = true;
6965 return;
6966 }
72bf9492 6967
c0ab21c2 6968 m_abbrev_table_holder
86de1d91
TT
6969 = abbrev_table::read (objfile, abbrev_section,
6970 m_new_cu->header.abbrev_sect_off);
dee91e82 6971
c0ab21c2
TT
6972 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6973 m_abbrev_table_holder.get ());
3e225074 6974 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
6975}
6976
0018ea6f
DE
6977\f
6978/* Type Unit Groups.
dee91e82 6979
0018ea6f
DE
6980 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6981 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6982 so that all types coming from the same compilation (.o file) are grouped
6983 together. A future step could be to put the types in the same symtab as
6984 the CU the types ultimately came from. */
ff013f42 6985
f4dc4d17
DE
6986static hashval_t
6987hash_type_unit_group (const void *item)
6988{
9a3c8263
SM
6989 const struct type_unit_group *tu_group
6990 = (const struct type_unit_group *) item;
f4dc4d17 6991
094b34ac 6992 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6993}
348e048f
DE
6994
6995static int
f4dc4d17 6996eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6997{
9a3c8263
SM
6998 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6999 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7000
094b34ac 7001 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7002}
348e048f 7003
f4dc4d17
DE
7004/* Allocate a hash table for type unit groups. */
7005
eaa5fa8b 7006static htab_up
298e9637 7007allocate_type_unit_groups_table ()
f4dc4d17 7008{
eaa5fa8b
TT
7009 return htab_up (htab_create_alloc (3,
7010 hash_type_unit_group,
7011 eq_type_unit_group,
7012 NULL, xcalloc, xfree));
f4dc4d17 7013}
dee91e82 7014
f4dc4d17
DE
7015/* Type units that don't have DW_AT_stmt_list are grouped into their own
7016 partial symtabs. We combine several TUs per psymtab to not let the size
7017 of any one psymtab grow too big. */
7018#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7019#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7020
094b34ac 7021/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7022 Create the type_unit_group object used to hold one or more TUs. */
7023
7024static struct type_unit_group *
094b34ac 7025create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7026{
518817b3
SM
7027 struct dwarf2_per_objfile *dwarf2_per_objfile
7028 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7029 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7030 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7031 struct type_unit_group *tu_group;
f4dc4d17
DE
7032
7033 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7034 struct type_unit_group);
094b34ac 7035 per_cu = &tu_group->per_cu;
518817b3 7036 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7037
094b34ac
DE
7038 if (dwarf2_per_objfile->using_index)
7039 {
7040 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7041 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7042 }
7043 else
7044 {
9c541725 7045 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7046 dwarf2_psymtab *pst;
528e1572 7047 std::string name;
094b34ac
DE
7048
7049 /* Give the symtab a useful name for debug purposes. */
7050 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7051 name = string_printf ("<type_units_%d>",
7052 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7053 else
528e1572 7054 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7055
528e1572 7056 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7057 pst->anonymous = true;
094b34ac 7058 }
f4dc4d17 7059
094b34ac 7060 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7061 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7062
7063 return tu_group;
7064}
7065
094b34ac
DE
7066/* Look up the type_unit_group for type unit CU, and create it if necessary.
7067 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7068
7069static struct type_unit_group *
ff39bb5e 7070get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7071{
518817b3
SM
7072 struct dwarf2_per_objfile *dwarf2_per_objfile
7073 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7074 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7075 struct type_unit_group *tu_group;
7076 void **slot;
7077 unsigned int line_offset;
7078 struct type_unit_group type_unit_group_for_lookup;
7079
7080 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7081 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7082
7083 /* Do we need to create a new group, or can we use an existing one? */
7084
7085 if (stmt_list)
7086 {
7087 line_offset = DW_UNSND (stmt_list);
7088 ++tu_stats->nr_symtab_sharers;
7089 }
7090 else
7091 {
7092 /* Ugh, no stmt_list. Rare, but we have to handle it.
7093 We can do various things here like create one group per TU or
7094 spread them over multiple groups to split up the expansion work.
7095 To avoid worst case scenarios (too many groups or too large groups)
7096 we, umm, group them in bunches. */
7097 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7098 | (tu_stats->nr_stmt_less_type_units
7099 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7100 ++tu_stats->nr_stmt_less_type_units;
7101 }
7102
094b34ac 7103 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7104 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7105 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7106 &type_unit_group_for_lookup, INSERT);
7107 if (*slot != NULL)
7108 {
9a3c8263 7109 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7110 gdb_assert (tu_group != NULL);
7111 }
7112 else
7113 {
9c541725 7114 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7115 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7116 *slot = tu_group;
7117 ++tu_stats->nr_symtabs;
7118 }
7119
7120 return tu_group;
7121}
0018ea6f
DE
7122\f
7123/* Partial symbol tables. */
7124
7125/* Create a psymtab named NAME and assign it to PER_CU.
7126
7127 The caller must fill in the following details:
7128 dirname, textlow, texthigh. */
7129
891813be 7130static dwarf2_psymtab *
0018ea6f
DE
7131create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7132{
e3b94546 7133 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7134 dwarf2_psymtab *pst;
0018ea6f 7135
891813be 7136 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7137
6d94535f 7138 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7139
7140 /* This is the glue that links PST into GDB's symbol API. */
891813be 7141 pst->per_cu_data = per_cu;
0018ea6f
DE
7142 per_cu->v.psymtab = pst;
7143
7144 return pst;
7145}
7146
c0ab21c2 7147/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7148
7149static void
7150process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7151 const gdb_byte *info_ptr,
0018ea6f 7152 struct die_info *comp_unit_die,
c0ab21c2 7153 enum language pretend_language)
0018ea6f
DE
7154{
7155 struct dwarf2_cu *cu = reader->cu;
518817b3 7156 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7157 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7158 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7159 CORE_ADDR baseaddr;
7160 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7161 dwarf2_psymtab *pst;
3a2b436a 7162 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7163 const char *filename;
0018ea6f 7164
0018ea6f
DE
7165 gdb_assert (! per_cu->is_debug_types);
7166
c0ab21c2 7167 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7168
0018ea6f 7169 /* Allocate a new partial symbol table structure. */
2e927613
TV
7170 gdb::unique_xmalloc_ptr<char> debug_filename;
7171 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7172 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7173 if (filename == NULL)
0018ea6f 7174 filename = "";
2e927613
TV
7175 else if (strcmp (filename, artificial) == 0)
7176 {
7177 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7178 sect_offset_str (per_cu->sect_off),
7179 (char *) NULL));
2e927613
TV
7180 filename = debug_filename.get ();
7181 }
0018ea6f
DE
7182
7183 pst = create_partial_symtab (per_cu, filename);
7184
7185 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7186 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7187
b3b3bada 7188 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7189
7190 dwarf2_find_base_address (comp_unit_die, cu);
7191
7192 /* Possibly set the default values of LOWPC and HIGHPC from
7193 `DW_AT_ranges'. */
3a2b436a
JK
7194 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7195 &best_highpc, cu, pst);
7196 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7197 {
7198 CORE_ADDR low
7199 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7200 - baseaddr);
7201 CORE_ADDR high
7202 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7203 - baseaddr - 1);
7204 /* Store the contiguous range if it is not empty; it can be
7205 empty for CUs with no code. */
d320c2b5
TT
7206 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7207 low, high, pst);
79748972 7208 }
0018ea6f
DE
7209
7210 /* Check if comp unit has_children.
7211 If so, read the rest of the partial symbols from this comp unit.
7212 If not, there's no more debug_info for this comp unit. */
3e225074 7213 if (comp_unit_die->has_children)
0018ea6f
DE
7214 {
7215 struct partial_die_info *first_die;
7216 CORE_ADDR lowpc, highpc;
7217
7218 lowpc = ((CORE_ADDR) -1);
7219 highpc = ((CORE_ADDR) 0);
7220
7221 first_die = load_partial_dies (reader, info_ptr, 1);
7222
7223 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7224 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7225
7226 /* If we didn't find a lowpc, set it to highpc to avoid
7227 complaints from `maint check'. */
7228 if (lowpc == ((CORE_ADDR) -1))
7229 lowpc = highpc;
7230
7231 /* If the compilation unit didn't have an explicit address range,
7232 then use the information extracted from its child dies. */
e385593e 7233 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7234 {
7235 best_lowpc = lowpc;
7236 best_highpc = highpc;
7237 }
7238 }
4ae976d1 7239 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7240 best_lowpc + baseaddr)
7241 - baseaddr);
4ae976d1 7242 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7243 best_highpc + baseaddr)
7244 - baseaddr);
0018ea6f 7245
8763cede 7246 end_psymtab_common (objfile, pst);
0018ea6f 7247
ae640021 7248 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7249 {
7250 int i;
ae640021 7251 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7252
7253 /* Fill in 'dependencies' here; we fill in 'users' in a
7254 post-pass. */
7255 pst->number_of_dependencies = len;
a9342b62
TT
7256 pst->dependencies
7257 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7258 for (i = 0; i < len; ++i)
7259 {
7260 pst->dependencies[i]
7261 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7262 }
0018ea6f 7263
ae640021 7264 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7265 }
7266
7267 /* Get the list of files included in the current compilation unit,
7268 and build a psymtab for each of them. */
7269 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7270
b4f54984 7271 if (dwarf_read_debug)
b926417a
TT
7272 fprintf_unfiltered (gdb_stdlog,
7273 "Psymtab for %s unit @%s: %s - %s"
7274 ", %d global, %d static syms\n",
7275 per_cu->is_debug_types ? "type" : "comp",
7276 sect_offset_str (per_cu->sect_off),
7277 paddress (gdbarch, pst->text_low (objfile)),
7278 paddress (gdbarch, pst->text_high (objfile)),
7279 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7280}
7281
7282/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7283 Process compilation unit THIS_CU for a psymtab. */
7284
7285static void
7286process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7287 bool want_partial_unit,
b93601f3 7288 enum language pretend_language)
0018ea6f
DE
7289{
7290 /* If this compilation unit was already read in, free the
7291 cached copy in order to read it in again. This is
7292 necessary because we skipped some symbols when we first
7293 read in the compilation unit (see load_partial_dies).
7294 This problem could be avoided, but the benefit is unclear. */
7295 if (this_cu->cu != NULL)
7296 free_one_cached_comp_unit (this_cu);
7297
6751ebae 7298 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7299
58990295
TV
7300 switch (reader.comp_unit_die->tag)
7301 {
7302 case DW_TAG_compile_unit:
7303 this_cu->unit_type = DW_UT_compile;
7304 break;
7305 case DW_TAG_partial_unit:
7306 this_cu->unit_type = DW_UT_partial;
7307 break;
7308 default:
7309 abort ();
7310 }
7311
c0ab21c2 7312 if (reader.dummy_p)
f1902523 7313 {
c0ab21c2 7314 /* Nothing. */
f1902523 7315 }
c0ab21c2 7316 else if (this_cu->is_debug_types)
3e225074
TT
7317 build_type_psymtabs_reader (&reader, reader.info_ptr,
7318 reader.comp_unit_die);
135f5437
TT
7319 else if (want_partial_unit
7320 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7321 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7322 reader.comp_unit_die,
c0ab21c2 7323 pretend_language);
0018ea6f 7324
58990295
TV
7325 this_cu->lang = this_cu->cu->language;
7326
0018ea6f 7327 /* Age out any secondary CUs. */
ed2dc618 7328 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7329}
f4dc4d17
DE
7330
7331/* Reader function for build_type_psymtabs. */
7332
7333static void
7334build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7335 const gdb_byte *info_ptr,
3e225074 7336 struct die_info *type_unit_die)
f4dc4d17 7337{
ed2dc618 7338 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7339 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7340 struct objfile *objfile = dwarf2_per_objfile->objfile;
7341 struct dwarf2_cu *cu = reader->cu;
7342 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7343 struct signatured_type *sig_type;
f4dc4d17
DE
7344 struct type_unit_group *tu_group;
7345 struct attribute *attr;
7346 struct partial_die_info *first_die;
7347 CORE_ADDR lowpc, highpc;
891813be 7348 dwarf2_psymtab *pst;
f4dc4d17 7349
0186c6a7
DE
7350 gdb_assert (per_cu->is_debug_types);
7351 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7352
3e225074 7353 if (! type_unit_die->has_children)
f4dc4d17
DE
7354 return;
7355
052c8bb8 7356 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7357 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7358
df07e2c7 7359 if (tu_group->tus == nullptr)
a8b3b8e9 7360 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7361 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7362
7363 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7364 pst = create_partial_symtab (per_cu, "");
6d94535f 7365 pst->anonymous = true;
f4dc4d17
DE
7366
7367 first_die = load_partial_dies (reader, info_ptr, 1);
7368
7369 lowpc = (CORE_ADDR) -1;
7370 highpc = (CORE_ADDR) 0;
7371 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7372
8763cede 7373 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7374}
7375
73051182
DE
7376/* Struct used to sort TUs by their abbreviation table offset. */
7377
7378struct tu_abbrev_offset
7379{
b2bdb8cf
SM
7380 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7381 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7382 {}
7383
7384 signatured_type *sig_type;
73051182
DE
7385 sect_offset abbrev_offset;
7386};
7387
484cf504 7388/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7389
484cf504
TT
7390static bool
7391sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7392 const struct tu_abbrev_offset &b)
73051182 7393{
484cf504 7394 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7395}
7396
7397/* Efficiently read all the type units.
7398 This does the bulk of the work for build_type_psymtabs.
7399
7400 The efficiency is because we sort TUs by the abbrev table they use and
7401 only read each abbrev table once. In one program there are 200K TUs
7402 sharing 8K abbrev tables.
7403
7404 The main purpose of this function is to support building the
7405 dwarf2_per_objfile->type_unit_groups table.
7406 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7407 can collapse the search space by grouping them by stmt_list.
7408 The savings can be significant, in the same program from above the 200K TUs
7409 share 8K stmt_list tables.
7410
7411 FUNC is expected to call get_type_unit_group, which will create the
7412 struct type_unit_group if necessary and add it to
7413 dwarf2_per_objfile->type_unit_groups. */
7414
7415static void
ed2dc618 7416build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7417{
73051182 7418 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7419 abbrev_table_up abbrev_table;
73051182 7420 sect_offset abbrev_offset;
73051182
DE
7421
7422 /* It's up to the caller to not call us multiple times. */
7423 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7424
b2bdb8cf 7425 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7426 return;
7427
7428 /* TUs typically share abbrev tables, and there can be way more TUs than
7429 abbrev tables. Sort by abbrev table to reduce the number of times we
7430 read each abbrev table in.
7431 Alternatives are to punt or to maintain a cache of abbrev tables.
7432 This is simpler and efficient enough for now.
7433
7434 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7435 symtab to use). Typically TUs with the same abbrev offset have the same
7436 stmt_list value too so in practice this should work well.
7437
7438 The basic algorithm here is:
7439
7440 sort TUs by abbrev table
7441 for each TU with same abbrev table:
7442 read abbrev table if first user
7443 read TU top level DIE
7444 [IWBN if DWO skeletons had DW_AT_stmt_list]
7445 call FUNC */
7446
b4f54984 7447 if (dwarf_read_debug)
73051182
DE
7448 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7449
7450 /* Sort in a separate table to maintain the order of all_type_units
7451 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7452 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7453 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7454
7455 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7456 sorted_by_abbrev.emplace_back
7457 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7458 sig_type->per_cu.section,
7459 sig_type->per_cu.sect_off));
73051182 7460
484cf504
TT
7461 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7462 sort_tu_by_abbrev_offset);
73051182 7463
9c541725 7464 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7465
b2bdb8cf 7466 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7467 {
73051182
DE
7468 /* Switch to the next abbrev table if necessary. */
7469 if (abbrev_table == NULL
b2bdb8cf 7470 || tu.abbrev_offset != abbrev_offset)
73051182 7471 {
b2bdb8cf 7472 abbrev_offset = tu.abbrev_offset;
73051182 7473 abbrev_table =
86de1d91
TT
7474 abbrev_table::read (dwarf2_per_objfile->objfile,
7475 &dwarf2_per_objfile->abbrev,
7476 abbrev_offset);
73051182
DE
7477 ++tu_stats->nr_uniq_abbrev_tables;
7478 }
7479
c0ab21c2 7480 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7481 0, false);
c0ab21c2
TT
7482 if (!reader.dummy_p)
7483 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7484 reader.comp_unit_die);
73051182 7485 }
6aa5f3a6 7486}
73051182 7487
6aa5f3a6
DE
7488/* Print collected type unit statistics. */
7489
7490static void
ed2dc618 7491print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7492{
7493 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7494
7495 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7496 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7497 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7498 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7499 tu_stats->nr_uniq_abbrev_tables);
7500 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7501 tu_stats->nr_symtabs);
7502 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7503 tu_stats->nr_symtab_sharers);
7504 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7505 tu_stats->nr_stmt_less_type_units);
7506 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7507 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7508}
7509
f4dc4d17
DE
7510/* Traversal function for build_type_psymtabs. */
7511
7512static int
7513build_type_psymtab_dependencies (void **slot, void *info)
7514{
ed2dc618
SM
7515 struct dwarf2_per_objfile *dwarf2_per_objfile
7516 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7517 struct objfile *objfile = dwarf2_per_objfile->objfile;
7518 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7519 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7520 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7521 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7522 int i;
7523
7524 gdb_assert (len > 0);
197400e8 7525 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7526
7527 pst->number_of_dependencies = len;
a9342b62 7528 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7529 for (i = 0; i < len; ++i)
f4dc4d17 7530 {
df07e2c7 7531 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7532 gdb_assert (iter->per_cu.is_debug_types);
7533 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7534 iter->type_unit_group = tu_group;
f4dc4d17
DE
7535 }
7536
df07e2c7
AB
7537 delete tu_group->tus;
7538 tu_group->tus = nullptr;
348e048f
DE
7539
7540 return 1;
7541}
7542
7543/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7544 Build partial symbol tables for the .debug_types comp-units. */
7545
7546static void
ed2dc618 7547build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7548{
ed2dc618 7549 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7550 return;
7551
ed2dc618 7552 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7553}
f4dc4d17 7554
6aa5f3a6
DE
7555/* Traversal function for process_skeletonless_type_unit.
7556 Read a TU in a DWO file and build partial symbols for it. */
7557
7558static int
7559process_skeletonless_type_unit (void **slot, void *info)
7560{
7561 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7562 struct dwarf2_per_objfile *dwarf2_per_objfile
7563 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7564 struct signatured_type find_entry, *entry;
7565
7566 /* If this TU doesn't exist in the global table, add it and read it in. */
7567
7568 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7569 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7570
7571 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7572 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7573 &find_entry, INSERT);
6aa5f3a6
DE
7574 /* If we've already seen this type there's nothing to do. What's happening
7575 is we're doing our own version of comdat-folding here. */
7576 if (*slot != NULL)
7577 return 1;
7578
7579 /* This does the job that create_all_type_units would have done for
7580 this TU. */
ed2dc618
SM
7581 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7582 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7583 *slot = entry;
7584
7585 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7586 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7587 if (!reader.dummy_p)
7588 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7589 reader.comp_unit_die);
6aa5f3a6
DE
7590
7591 return 1;
7592}
7593
7594/* Traversal function for process_skeletonless_type_units. */
7595
7596static int
7597process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7598{
7599 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7600
7601 if (dwo_file->tus != NULL)
b0b6a987
TT
7602 htab_traverse_noresize (dwo_file->tus.get (),
7603 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7604
7605 return 1;
7606}
7607
7608/* Scan all TUs of DWO files, verifying we've processed them.
7609 This is needed in case a TU was emitted without its skeleton.
7610 Note: This can't be done until we know what all the DWO files are. */
7611
7612static void
ed2dc618 7613process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7614{
7615 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7616 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7617 && dwarf2_per_objfile->dwo_files != NULL)
7618 {
51ac9db5 7619 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7620 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7621 dwarf2_per_objfile);
6aa5f3a6 7622 }
348e048f
DE
7623}
7624
ed2dc618 7625/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7626
7627static void
ed2dc618 7628set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7629{
b76e467d 7630 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7631 {
891813be 7632 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7633
36586728
TT
7634 if (pst == NULL)
7635 continue;
7636
b76e467d 7637 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7638 {
7639 /* Set the 'user' field only if it is not already set. */
7640 if (pst->dependencies[j]->user == NULL)
7641 pst->dependencies[j]->user = pst;
7642 }
7643 }
7644}
7645
93311388
DE
7646/* Build the partial symbol table by doing a quick pass through the
7647 .debug_info and .debug_abbrev sections. */
72bf9492 7648
93311388 7649static void
ed2dc618 7650dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7651{
ed2dc618 7652 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7653
b4f54984 7654 if (dwarf_read_debug)
45cfd468
DE
7655 {
7656 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7657 objfile_name (objfile));
45cfd468
DE
7658 }
7659
76935768
TT
7660 scoped_restore restore_reading_psyms
7661 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7662 true);
98bfdba5 7663
96b79293 7664 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7665
93311388
DE
7666 /* Any cached compilation units will be linked by the per-objfile
7667 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7668 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7669
ed2dc618 7670 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7671
ed2dc618 7672 create_all_comp_units (dwarf2_per_objfile);
c906108c 7673
60606b2c
TT
7674 /* Create a temporary address map on a temporary obstack. We later
7675 copy this to the final obstack. */
8268c778 7676 auto_obstack temp_obstack;
791afaa2
TT
7677
7678 scoped_restore save_psymtabs_addrmap
d320c2b5 7679 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7680 addrmap_create_mutable (&temp_obstack));
72bf9492 7681
b76e467d 7682 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7683 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7684
6aa5f3a6 7685 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7686 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7687
7688 /* Now that all TUs have been processed we can fill in the dependencies. */
7689 if (dwarf2_per_objfile->type_unit_groups != NULL)
7690 {
eaa5fa8b 7691 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7692 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7693 }
7694
b4f54984 7695 if (dwarf_read_debug)
ed2dc618 7696 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7697
ed2dc618 7698 set_partial_user (dwarf2_per_objfile);
95554aad 7699
d320c2b5
TT
7700 objfile->partial_symtabs->psymtabs_addrmap
7701 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7702 objfile->partial_symtabs->obstack ());
791afaa2
TT
7703 /* At this point we want to keep the address map. */
7704 save_psymtabs_addrmap.release ();
ff013f42 7705
b4f54984 7706 if (dwarf_read_debug)
45cfd468 7707 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7708 objfile_name (objfile));
ae038cb0
DJ
7709}
7710
dee91e82
DE
7711/* Load the partial DIEs for a secondary CU into memory.
7712 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7713
dee91e82
DE
7714static void
7715load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7716{
6751ebae 7717 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7718
7719 if (!reader.dummy_p)
7720 {
7721 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7722 language_minimal);
7723
7724 /* Check if comp unit has_children.
7725 If so, read the rest of the partial symbols from this comp unit.
7726 If not, there's no more debug_info for this comp unit. */
3e225074 7727 if (reader.comp_unit_die->has_children)
c0ab21c2 7728 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7729
7730 reader.keep ();
c0ab21c2 7731 }
ae038cb0
DJ
7732}
7733
ae038cb0 7734static void
ed2dc618 7735read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7736 struct dwarf2_section_info *section,
f1902523 7737 struct dwarf2_section_info *abbrev_section,
b76e467d 7738 unsigned int is_dwz)
ae038cb0 7739{
d521ce57 7740 const gdb_byte *info_ptr;
ed2dc618 7741 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7742
b4f54984 7743 if (dwarf_read_debug)
bf6af496 7744 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7745 section->get_name (),
7746 section->get_file_name ());
bf6af496 7747
96b79293 7748 section->read (objfile);
ae038cb0 7749
36586728 7750 info_ptr = section->buffer;
6e70227d 7751
36586728 7752 while (info_ptr < section->buffer + section->size)
ae038cb0 7753 {
ae038cb0 7754 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7755
9c541725 7756 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7757
f1902523 7758 comp_unit_head cu_header;
ed2dc618
SM
7759 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7760 abbrev_section, info_ptr,
7761 rcuh_kind::COMPILE);
ae038cb0
DJ
7762
7763 /* Save the compilation unit for later lookup. */
f1902523
JK
7764 if (cu_header.unit_type != DW_UT_type)
7765 {
7766 this_cu = XOBNEW (&objfile->objfile_obstack,
7767 struct dwarf2_per_cu_data);
7768 memset (this_cu, 0, sizeof (*this_cu));
7769 }
7770 else
7771 {
7772 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7773 struct signatured_type);
7774 memset (sig_type, 0, sizeof (*sig_type));
7775 sig_type->signature = cu_header.signature;
7776 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7777 this_cu = &sig_type->per_cu;
7778 }
7779 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7780 this_cu->sect_off = sect_off;
f1902523 7781 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7782 this_cu->is_dwz = is_dwz;
e3b94546 7783 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7784 this_cu->section = section;
ae038cb0 7785
b76e467d 7786 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7787
7788 info_ptr = info_ptr + this_cu->length;
7789 }
36586728
TT
7790}
7791
7792/* Create a list of all compilation units in OBJFILE.
7793 This is only done for -readnow and building partial symtabs. */
7794
7795static void
ed2dc618 7796create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7797{
b76e467d 7798 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7799 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7800 &dwarf2_per_objfile->abbrev, 0);
36586728 7801
b76e467d 7802 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7803 if (dwz != NULL)
ed2dc618 7804 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7805 1);
c906108c
SS
7806}
7807
5734ee8b 7808/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7809 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7810 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7811 DW_AT_ranges). See the comments of add_partial_subprogram on how
7812 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7813
72bf9492
DJ
7814static void
7815scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7816 CORE_ADDR *highpc, int set_addrmap,
7817 struct dwarf2_cu *cu)
c906108c 7818{
72bf9492 7819 struct partial_die_info *pdi;
c906108c 7820
91c24f0a
DC
7821 /* Now, march along the PDI's, descending into ones which have
7822 interesting children but skipping the children of the other ones,
7823 until we reach the end of the compilation unit. */
c906108c 7824
72bf9492 7825 pdi = first_die;
91c24f0a 7826
72bf9492
DJ
7827 while (pdi != NULL)
7828 {
52356b79 7829 pdi->fixup (cu);
c906108c 7830
f55ee35c 7831 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7832 children, so we need to look at them. Ditto for anonymous
7833 enums. */
933c6fe4 7834
72bf9492 7835 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7836 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7837 || pdi->tag == DW_TAG_imported_unit
7838 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7839 {
72bf9492 7840 switch (pdi->tag)
c906108c
SS
7841 {
7842 case DW_TAG_subprogram:
b1dc1806 7843 case DW_TAG_inlined_subroutine:
cdc07690 7844 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7845 break;
72929c62 7846 case DW_TAG_constant:
c906108c
SS
7847 case DW_TAG_variable:
7848 case DW_TAG_typedef:
91c24f0a 7849 case DW_TAG_union_type:
72bf9492 7850 if (!pdi->is_declaration)
63d06c5c 7851 {
72bf9492 7852 add_partial_symbol (pdi, cu);
63d06c5c
DC
7853 }
7854 break;
c906108c 7855 case DW_TAG_class_type:
680b30c7 7856 case DW_TAG_interface_type:
c906108c 7857 case DW_TAG_structure_type:
72bf9492 7858 if (!pdi->is_declaration)
c906108c 7859 {
72bf9492 7860 add_partial_symbol (pdi, cu);
c906108c 7861 }
b7fee5a3
KS
7862 if ((cu->language == language_rust
7863 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7864 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7865 set_addrmap, cu);
c906108c 7866 break;
91c24f0a 7867 case DW_TAG_enumeration_type:
72bf9492
DJ
7868 if (!pdi->is_declaration)
7869 add_partial_enumeration (pdi, cu);
c906108c
SS
7870 break;
7871 case DW_TAG_base_type:
a02abb62 7872 case DW_TAG_subrange_type:
c906108c 7873 /* File scope base type definitions are added to the partial
c5aa993b 7874 symbol table. */
72bf9492 7875 add_partial_symbol (pdi, cu);
c906108c 7876 break;
d9fa45fe 7877 case DW_TAG_namespace:
cdc07690 7878 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7879 break;
5d7cb8df 7880 case DW_TAG_module:
59c35742
AB
7881 if (!pdi->is_declaration)
7882 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7883 break;
95554aad
TT
7884 case DW_TAG_imported_unit:
7885 {
7886 struct dwarf2_per_cu_data *per_cu;
7887
f4dc4d17
DE
7888 /* For now we don't handle imported units in type units. */
7889 if (cu->per_cu->is_debug_types)
7890 {
7891 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7892 " supported in type units [in module %s]"),
518817b3 7893 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7894 }
7895
e3b94546
SM
7896 per_cu = dwarf2_find_containing_comp_unit
7897 (pdi->d.sect_off, pdi->is_dwz,
518817b3 7898 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
7899
7900 /* Go read the partial unit, if needed. */
7901 if (per_cu->v.psymtab == NULL)
135f5437 7902 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 7903
ae640021 7904 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7905 }
7906 break;
74921315
KS
7907 case DW_TAG_imported_declaration:
7908 add_partial_symbol (pdi, cu);
7909 break;
c906108c
SS
7910 default:
7911 break;
7912 }
7913 }
7914
72bf9492
DJ
7915 /* If the die has a sibling, skip to the sibling. */
7916
7917 pdi = pdi->die_sibling;
7918 }
7919}
7920
7921/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7922
72bf9492 7923 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7924 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7925 Enumerators are an exception; they use the scope of their parent
7926 enumeration type, i.e. the name of the enumeration type is not
7927 prepended to the enumerator.
91c24f0a 7928
72bf9492
DJ
7929 There are two complexities. One is DW_AT_specification; in this
7930 case "parent" means the parent of the target of the specification,
7931 instead of the direct parent of the DIE. The other is compilers
7932 which do not emit DW_TAG_namespace; in this case we try to guess
7933 the fully qualified name of structure types from their members'
7934 linkage names. This must be done using the DIE's children rather
7935 than the children of any DW_AT_specification target. We only need
7936 to do this for structures at the top level, i.e. if the target of
7937 any DW_AT_specification (if any; otherwise the DIE itself) does not
7938 have a parent. */
7939
7940/* Compute the scope prefix associated with PDI's parent, in
7941 compilation unit CU. The result will be allocated on CU's
7942 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7943 field. NULL is returned if no prefix is necessary. */
15d034d0 7944static const char *
72bf9492
DJ
7945partial_die_parent_scope (struct partial_die_info *pdi,
7946 struct dwarf2_cu *cu)
7947{
15d034d0 7948 const char *grandparent_scope;
72bf9492 7949 struct partial_die_info *parent, *real_pdi;
91c24f0a 7950
72bf9492
DJ
7951 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7952 then this means the parent of the specification DIE. */
7953
7954 real_pdi = pdi;
72bf9492 7955 while (real_pdi->has_specification)
fb816e8b 7956 {
122cf0f2
AB
7957 auto res = find_partial_die (real_pdi->spec_offset,
7958 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
7959 real_pdi = res.pdi;
7960 cu = res.cu;
7961 }
72bf9492
DJ
7962
7963 parent = real_pdi->die_parent;
7964 if (parent == NULL)
7965 return NULL;
7966
7967 if (parent->scope_set)
7968 return parent->scope;
7969
52356b79 7970 parent->fixup (cu);
72bf9492 7971
10b3939b 7972 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7973
acebe513
UW
7974 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7975 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7976 Work around this problem here. */
7977 if (cu->language == language_cplus
6e70227d 7978 && parent->tag == DW_TAG_namespace
acebe513
UW
7979 && strcmp (parent->name, "::") == 0
7980 && grandparent_scope == NULL)
7981 {
7982 parent->scope = NULL;
7983 parent->scope_set = 1;
7984 return NULL;
7985 }
7986
0a4b0913 7987 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
7988 if (pdi->tag == DW_TAG_enumerator)
7989 /* Enumerators should not get the name of the enumeration as a prefix. */
7990 parent->scope = grandparent_scope;
7991 else if (parent->tag == DW_TAG_namespace
f55ee35c 7992 || parent->tag == DW_TAG_module
72bf9492
DJ
7993 || parent->tag == DW_TAG_structure_type
7994 || parent->tag == DW_TAG_class_type
680b30c7 7995 || parent->tag == DW_TAG_interface_type
ceeb3d5a 7996 || parent->tag == DW_TAG_union_type
0a4b0913
AB
7997 || parent->tag == DW_TAG_enumeration_type
7998 || (cu->language == language_fortran
7999 && parent->tag == DW_TAG_subprogram
8000 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8001 {
8002 if (grandparent_scope == NULL)
8003 parent->scope = parent->name;
8004 else
3e43a32a
MS
8005 parent->scope = typename_concat (&cu->comp_unit_obstack,
8006 grandparent_scope,
f55ee35c 8007 parent->name, 0, cu);
72bf9492 8008 }
72bf9492
DJ
8009 else
8010 {
8011 /* FIXME drow/2004-04-01: What should we be doing with
8012 function-local names? For partial symbols, we should probably be
8013 ignoring them. */
fa9c3fa0
TT
8014 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8015 dwarf_tag_name (parent->tag),
8016 sect_offset_str (pdi->sect_off));
72bf9492 8017 parent->scope = grandparent_scope;
c906108c
SS
8018 }
8019
72bf9492
DJ
8020 parent->scope_set = 1;
8021 return parent->scope;
8022}
8023
8024/* Return the fully scoped name associated with PDI, from compilation unit
8025 CU. The result will be allocated with malloc. */
4568ecf9 8026
43816ebc 8027static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8028partial_die_full_name (struct partial_die_info *pdi,
8029 struct dwarf2_cu *cu)
8030{
15d034d0 8031 const char *parent_scope;
72bf9492 8032
98bfdba5
PA
8033 /* If this is a template instantiation, we can not work out the
8034 template arguments from partial DIEs. So, unfortunately, we have
8035 to go through the full DIEs. At least any work we do building
8036 types here will be reused if full symbols are loaded later. */
8037 if (pdi->has_template_arguments)
8038 {
52356b79 8039 pdi->fixup (cu);
98bfdba5
PA
8040
8041 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8042 {
8043 struct die_info *die;
8044 struct attribute attr;
8045 struct dwarf2_cu *ref_cu = cu;
8046
b64f50a1 8047 /* DW_FORM_ref_addr is using section offset. */
b4069958 8048 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8049 attr.form = DW_FORM_ref_addr;
9c541725 8050 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8051 die = follow_die_ref (NULL, &attr, &ref_cu);
8052
43816ebc 8053 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8054 }
8055 }
8056
72bf9492
DJ
8057 parent_scope = partial_die_parent_scope (pdi, cu);
8058 if (parent_scope == NULL)
8059 return NULL;
8060 else
43816ebc
TT
8061 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8062 pdi->name, 0, cu));
c906108c
SS
8063}
8064
8065static void
72bf9492 8066add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8067{
518817b3
SM
8068 struct dwarf2_per_objfile *dwarf2_per_objfile
8069 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8070 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8071 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8072 CORE_ADDR addr = 0;
15d034d0 8073 const char *actual_name = NULL;
e142c38c
DJ
8074 CORE_ADDR baseaddr;
8075
b3b3bada 8076 baseaddr = objfile->text_section_offset ();
c906108c 8077
43816ebc
TT
8078 gdb::unique_xmalloc_ptr<char> built_actual_name
8079 = partial_die_full_name (pdi, cu);
15d034d0 8080 if (built_actual_name != NULL)
43816ebc 8081 actual_name = built_actual_name.get ();
63d06c5c 8082
72bf9492
DJ
8083 if (actual_name == NULL)
8084 actual_name = pdi->name;
8085
c906108c
SS
8086 switch (pdi->tag)
8087 {
b1dc1806 8088 case DW_TAG_inlined_subroutine:
c906108c 8089 case DW_TAG_subprogram:
79748972
TT
8090 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8091 - baseaddr);
0a4b0913
AB
8092 if (pdi->is_external
8093 || cu->language == language_ada
8094 || (cu->language == language_fortran
8095 && pdi->die_parent != NULL
8096 && pdi->die_parent->tag == DW_TAG_subprogram))
8097 {
8098 /* Normally, only "external" DIEs are part of the global scope.
8099 But in Ada and Fortran, we want to be able to access nested
8100 procedures globally. So all Ada and Fortran subprograms are
8101 stored in the global scope. */
31edb802 8102 add_psymbol_to_list (actual_name,
15d034d0 8103 built_actual_name != NULL,
f47fb265 8104 VAR_DOMAIN, LOC_BLOCK,
79748972 8105 SECT_OFF_TEXT (objfile),
75aedd27 8106 psymbol_placement::GLOBAL,
79748972
TT
8107 addr,
8108 cu->language, objfile);
c906108c
SS
8109 }
8110 else
8111 {
31edb802 8112 add_psymbol_to_list (actual_name,
15d034d0 8113 built_actual_name != NULL,
f47fb265 8114 VAR_DOMAIN, LOC_BLOCK,
79748972 8115 SECT_OFF_TEXT (objfile),
75aedd27 8116 psymbol_placement::STATIC,
1762568f 8117 addr, cu->language, objfile);
c906108c 8118 }
0c1b455e
TT
8119
8120 if (pdi->main_subprogram && actual_name != NULL)
8121 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8122 break;
72929c62 8123 case DW_TAG_constant:
31edb802 8124 add_psymbol_to_list (actual_name,
75aedd27
TT
8125 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8126 -1, (pdi->is_external
8127 ? psymbol_placement::GLOBAL
8128 : psymbol_placement::STATIC),
8129 0, cu->language, objfile);
72929c62 8130 break;
c906108c 8131 case DW_TAG_variable:
95554aad
TT
8132 if (pdi->d.locdesc)
8133 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8134
95554aad 8135 if (pdi->d.locdesc
caac4577
JG
8136 && addr == 0
8137 && !dwarf2_per_objfile->has_section_at_zero)
8138 {
8139 /* A global or static variable may also have been stripped
8140 out by the linker if unused, in which case its address
8141 will be nullified; do not add such variables into partial
8142 symbol table then. */
8143 }
8144 else if (pdi->is_external)
c906108c
SS
8145 {
8146 /* Global Variable.
8147 Don't enter into the minimal symbol tables as there is
8148 a minimal symbol table entry from the ELF symbols already.
8149 Enter into partial symbol table if it has a location
8150 descriptor or a type.
8151 If the location descriptor is missing, new_symbol will create
8152 a LOC_UNRESOLVED symbol, the address of the variable will then
8153 be determined from the minimal symbol table whenever the variable
8154 is referenced.
8155 The address for the partial symbol table entry is not
8156 used by GDB, but it comes in handy for debugging partial symbol
8157 table building. */
8158
95554aad 8159 if (pdi->d.locdesc || pdi->has_type)
31edb802 8160 add_psymbol_to_list (actual_name,
15d034d0 8161 built_actual_name != NULL,
f47fb265 8162 VAR_DOMAIN, LOC_STATIC,
79748972 8163 SECT_OFF_TEXT (objfile),
75aedd27 8164 psymbol_placement::GLOBAL,
79748972 8165 addr, cu->language, objfile);
c906108c
SS
8166 }
8167 else
8168 {
ff908ebf
AW
8169 int has_loc = pdi->d.locdesc != NULL;
8170
8171 /* Static Variable. Skip symbols whose value we cannot know (those
8172 without location descriptors or constant values). */
8173 if (!has_loc && !pdi->has_const_value)
43816ebc 8174 return;
ff908ebf 8175
31edb802 8176 add_psymbol_to_list (actual_name,
15d034d0 8177 built_actual_name != NULL,
f47fb265 8178 VAR_DOMAIN, LOC_STATIC,
79748972 8179 SECT_OFF_TEXT (objfile),
75aedd27 8180 psymbol_placement::STATIC,
79748972 8181 has_loc ? addr : 0,
f47fb265 8182 cu->language, objfile);
c906108c
SS
8183 }
8184 break;
8185 case DW_TAG_typedef:
8186 case DW_TAG_base_type:
a02abb62 8187 case DW_TAG_subrange_type:
31edb802 8188 add_psymbol_to_list (actual_name,
15d034d0 8189 built_actual_name != NULL,
79748972 8190 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8191 psymbol_placement::STATIC,
1762568f 8192 0, cu->language, objfile);
c906108c 8193 break;
74921315 8194 case DW_TAG_imported_declaration:
72bf9492 8195 case DW_TAG_namespace:
31edb802 8196 add_psymbol_to_list (actual_name,
15d034d0 8197 built_actual_name != NULL,
79748972 8198 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8199 psymbol_placement::GLOBAL,
1762568f 8200 0, cu->language, objfile);
72bf9492 8201 break;
530e8392 8202 case DW_TAG_module:
a5fd13a9
BH
8203 /* With Fortran 77 there might be a "BLOCK DATA" module
8204 available without any name. If so, we skip the module as it
8205 doesn't bring any value. */
8206 if (actual_name != nullptr)
31edb802 8207 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8208 built_actual_name != NULL,
8209 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8210 psymbol_placement::GLOBAL,
8211 0, cu->language, objfile);
530e8392 8212 break;
c906108c 8213 case DW_TAG_class_type:
680b30c7 8214 case DW_TAG_interface_type:
c906108c
SS
8215 case DW_TAG_structure_type:
8216 case DW_TAG_union_type:
8217 case DW_TAG_enumeration_type:
fa4028e9
JB
8218 /* Skip external references. The DWARF standard says in the section
8219 about "Structure, Union, and Class Type Entries": "An incomplete
8220 structure, union or class type is represented by a structure,
8221 union or class entry that does not have a byte size attribute
8222 and that has a DW_AT_declaration attribute." */
8223 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8224 return;
fa4028e9 8225
63d06c5c
DC
8226 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8227 static vs. global. */
31edb802 8228 add_psymbol_to_list (actual_name,
15d034d0 8229 built_actual_name != NULL,
79748972 8230 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8231 cu->language == language_cplus
75aedd27
TT
8232 ? psymbol_placement::GLOBAL
8233 : psymbol_placement::STATIC,
1762568f 8234 0, cu->language, objfile);
c906108c 8235
c906108c
SS
8236 break;
8237 case DW_TAG_enumerator:
31edb802 8238 add_psymbol_to_list (actual_name,
15d034d0 8239 built_actual_name != NULL,
79748972 8240 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8241 cu->language == language_cplus
75aedd27
TT
8242 ? psymbol_placement::GLOBAL
8243 : psymbol_placement::STATIC,
1762568f 8244 0, cu->language, objfile);
c906108c
SS
8245 break;
8246 default:
8247 break;
8248 }
8249}
8250
5c4e30ca
DC
8251/* Read a partial die corresponding to a namespace; also, add a symbol
8252 corresponding to that namespace to the symbol table. NAMESPACE is
8253 the name of the enclosing namespace. */
91c24f0a 8254
72bf9492
DJ
8255static void
8256add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8257 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8258 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8259{
72bf9492 8260 /* Add a symbol for the namespace. */
e7c27a73 8261
72bf9492 8262 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8263
8264 /* Now scan partial symbols in that namespace. */
8265
91c24f0a 8266 if (pdi->has_children)
cdc07690 8267 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8268}
8269
5d7cb8df
JK
8270/* Read a partial die corresponding to a Fortran module. */
8271
8272static void
8273add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8274 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8275{
530e8392
KB
8276 /* Add a symbol for the namespace. */
8277
8278 add_partial_symbol (pdi, cu);
8279
f55ee35c 8280 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8281
8282 if (pdi->has_children)
cdc07690 8283 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8284}
8285
b1dc1806
XR
8286/* Read a partial die corresponding to a subprogram or an inlined
8287 subprogram and create a partial symbol for that subprogram.
8288 When the CU language allows it, this routine also defines a partial
8289 symbol for each nested subprogram that this subprogram contains.
8290 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8291 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8292
cdc07690
YQ
8293 PDI may also be a lexical block, in which case we simply search
8294 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8295 Again, this is only performed when the CU language allows this
8296 type of definitions. */
8297
8298static void
8299add_partial_subprogram (struct partial_die_info *pdi,
8300 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8301 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8302{
b1dc1806 8303 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8304 {
8305 if (pdi->has_pc_info)
8306 {
8307 if (pdi->lowpc < *lowpc)
8308 *lowpc = pdi->lowpc;
8309 if (pdi->highpc > *highpc)
8310 *highpc = pdi->highpc;
cdc07690 8311 if (set_addrmap)
5734ee8b 8312 {
518817b3 8313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8314 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8315 CORE_ADDR baseaddr;
b926417a
TT
8316 CORE_ADDR this_highpc;
8317 CORE_ADDR this_lowpc;
5734ee8b 8318
b3b3bada 8319 baseaddr = objfile->text_section_offset ();
b926417a
TT
8320 this_lowpc
8321 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8322 pdi->lowpc + baseaddr)
8323 - baseaddr);
8324 this_highpc
8325 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8326 pdi->highpc + baseaddr)
8327 - baseaddr);
d320c2b5 8328 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8329 this_lowpc, this_highpc - 1,
9291a0cd 8330 cu->per_cu->v.psymtab);
5734ee8b 8331 }
481860b3
GB
8332 }
8333
8334 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8335 {
bc30ff58 8336 if (!pdi->is_declaration)
e8d05480
JB
8337 /* Ignore subprogram DIEs that do not have a name, they are
8338 illegal. Do not emit a complaint at this point, we will
8339 do so when we convert this psymtab into a symtab. */
8340 if (pdi->name)
8341 add_partial_symbol (pdi, cu);
bc30ff58
JB
8342 }
8343 }
6e70227d 8344
bc30ff58
JB
8345 if (! pdi->has_children)
8346 return;
8347
0a4b0913 8348 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8349 {
8350 pdi = pdi->die_child;
8351 while (pdi != NULL)
8352 {
52356b79 8353 pdi->fixup (cu);
bc30ff58 8354 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8355 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8356 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8357 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8358 pdi = pdi->die_sibling;
8359 }
8360 }
8361}
8362
91c24f0a
DC
8363/* Read a partial die corresponding to an enumeration type. */
8364
72bf9492
DJ
8365static void
8366add_partial_enumeration (struct partial_die_info *enum_pdi,
8367 struct dwarf2_cu *cu)
91c24f0a 8368{
72bf9492 8369 struct partial_die_info *pdi;
91c24f0a
DC
8370
8371 if (enum_pdi->name != NULL)
72bf9492
DJ
8372 add_partial_symbol (enum_pdi, cu);
8373
8374 pdi = enum_pdi->die_child;
8375 while (pdi)
91c24f0a 8376 {
72bf9492 8377 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8378 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8379 else
72bf9492
DJ
8380 add_partial_symbol (pdi, cu);
8381 pdi = pdi->die_sibling;
91c24f0a 8382 }
91c24f0a
DC
8383}
8384
6caca83c
CC
8385/* Return the initial uleb128 in the die at INFO_PTR. */
8386
8387static unsigned int
d521ce57 8388peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8389{
8390 unsigned int bytes_read;
8391
8392 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8393}
8394
685af9cd
TT
8395/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8396 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8397
4bb7a0a7
DJ
8398 Return the corresponding abbrev, or NULL if the number is zero (indicating
8399 an empty DIE). In either case *BYTES_READ will be set to the length of
8400 the initial number. */
8401
8402static struct abbrev_info *
685af9cd
TT
8403peek_die_abbrev (const die_reader_specs &reader,
8404 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8405{
685af9cd 8406 dwarf2_cu *cu = reader.cu;
518817b3 8407 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8408 unsigned int abbrev_number
8409 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8410
8411 if (abbrev_number == 0)
8412 return NULL;
8413
685af9cd 8414 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8415 if (!abbrev)
8416 {
422b9917 8417 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8418 " at offset %s [in module %s]"),
422b9917 8419 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8420 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8421 }
8422
8423 return abbrev;
8424}
8425
93311388
DE
8426/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8427 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8428 DIE. Any children of the skipped DIEs will also be skipped. */
8429
d521ce57
TT
8430static const gdb_byte *
8431skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8432{
4bb7a0a7
DJ
8433 while (1)
8434 {
685af9cd
TT
8435 unsigned int bytes_read;
8436 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8437
4bb7a0a7
DJ
8438 if (abbrev == NULL)
8439 return info_ptr + bytes_read;
8440 else
dee91e82 8441 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8442 }
8443}
8444
93311388
DE
8445/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8446 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8447 abbrev corresponding to that skipped uleb128 should be passed in
8448 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8449 children. */
8450
d521ce57
TT
8451static const gdb_byte *
8452skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8453 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8454{
8455 unsigned int bytes_read;
8456 struct attribute attr;
dee91e82
DE
8457 bfd *abfd = reader->abfd;
8458 struct dwarf2_cu *cu = reader->cu;
d521ce57 8459 const gdb_byte *buffer = reader->buffer;
f664829e 8460 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8461 unsigned int form, i;
8462
8463 for (i = 0; i < abbrev->num_attrs; i++)
8464 {
8465 /* The only abbrev we care about is DW_AT_sibling. */
8466 if (abbrev->attrs[i].name == DW_AT_sibling)
8467 {
18a8505e
AT
8468 bool ignored;
8469 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8470 &ignored);
4bb7a0a7 8471 if (attr.form == DW_FORM_ref_addr)
b98664d3 8472 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8473 else
b9502d3f 8474 {
0826b30a 8475 sect_offset off = attr.get_ref_die_offset ();
9c541725 8476 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8477
8478 if (sibling_ptr < info_ptr)
b98664d3 8479 complaint (_("DW_AT_sibling points backwards"));
22869d73 8480 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8481 reader->die_section->overflow_complaint ();
b9502d3f
WN
8482 else
8483 return sibling_ptr;
8484 }
4bb7a0a7
DJ
8485 }
8486
8487 /* If it isn't DW_AT_sibling, skip this attribute. */
8488 form = abbrev->attrs[i].form;
8489 skip_attribute:
8490 switch (form)
8491 {
4bb7a0a7 8492 case DW_FORM_ref_addr:
ae411497
TT
8493 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8494 and later it is offset sized. */
8495 if (cu->header.version == 2)
8496 info_ptr += cu->header.addr_size;
8497 else
8498 info_ptr += cu->header.offset_size;
8499 break;
36586728
TT
8500 case DW_FORM_GNU_ref_alt:
8501 info_ptr += cu->header.offset_size;
8502 break;
ae411497 8503 case DW_FORM_addr:
4bb7a0a7
DJ
8504 info_ptr += cu->header.addr_size;
8505 break;
8506 case DW_FORM_data1:
8507 case DW_FORM_ref1:
8508 case DW_FORM_flag:
8fe0f950 8509 case DW_FORM_strx1:
4bb7a0a7
DJ
8510 info_ptr += 1;
8511 break;
2dc7f7b3 8512 case DW_FORM_flag_present:
43988095 8513 case DW_FORM_implicit_const:
2dc7f7b3 8514 break;
4bb7a0a7
DJ
8515 case DW_FORM_data2:
8516 case DW_FORM_ref2:
8fe0f950 8517 case DW_FORM_strx2:
4bb7a0a7
DJ
8518 info_ptr += 2;
8519 break;
8fe0f950
AT
8520 case DW_FORM_strx3:
8521 info_ptr += 3;
8522 break;
4bb7a0a7
DJ
8523 case DW_FORM_data4:
8524 case DW_FORM_ref4:
8fe0f950 8525 case DW_FORM_strx4:
4bb7a0a7
DJ
8526 info_ptr += 4;
8527 break;
8528 case DW_FORM_data8:
8529 case DW_FORM_ref8:
55f1336d 8530 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8531 info_ptr += 8;
8532 break;
0224619f
JK
8533 case DW_FORM_data16:
8534 info_ptr += 16;
8535 break;
4bb7a0a7 8536 case DW_FORM_string:
9b1c24c8 8537 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8538 info_ptr += bytes_read;
8539 break;
2dc7f7b3 8540 case DW_FORM_sec_offset:
4bb7a0a7 8541 case DW_FORM_strp:
36586728 8542 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8543 info_ptr += cu->header.offset_size;
8544 break;
2dc7f7b3 8545 case DW_FORM_exprloc:
4bb7a0a7
DJ
8546 case DW_FORM_block:
8547 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8548 info_ptr += bytes_read;
8549 break;
8550 case DW_FORM_block1:
8551 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8552 break;
8553 case DW_FORM_block2:
8554 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8555 break;
8556 case DW_FORM_block4:
8557 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8558 break;
336d760d 8559 case DW_FORM_addrx:
cf532bd1 8560 case DW_FORM_strx:
4bb7a0a7
DJ
8561 case DW_FORM_sdata:
8562 case DW_FORM_udata:
8563 case DW_FORM_ref_udata:
3019eac3
DE
8564 case DW_FORM_GNU_addr_index:
8565 case DW_FORM_GNU_str_index:
18a8505e 8566 case DW_FORM_rnglistx:
d521ce57 8567 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8568 break;
8569 case DW_FORM_indirect:
8570 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8571 info_ptr += bytes_read;
8572 /* We need to continue parsing from here, so just go back to
8573 the top. */
8574 goto skip_attribute;
8575
8576 default:
3e43a32a
MS
8577 error (_("Dwarf Error: Cannot handle %s "
8578 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8579 dwarf_form_name (form),
8580 bfd_get_filename (abfd));
8581 }
8582 }
8583
8584 if (abbrev->has_children)
dee91e82 8585 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8586 else
8587 return info_ptr;
8588}
8589
93311388 8590/* Locate ORIG_PDI's sibling.
dee91e82 8591 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8592
d521ce57 8593static const gdb_byte *
dee91e82
DE
8594locate_pdi_sibling (const struct die_reader_specs *reader,
8595 struct partial_die_info *orig_pdi,
d521ce57 8596 const gdb_byte *info_ptr)
91c24f0a
DC
8597{
8598 /* Do we know the sibling already? */
72bf9492 8599
91c24f0a
DC
8600 if (orig_pdi->sibling)
8601 return orig_pdi->sibling;
8602
8603 /* Are there any children to deal with? */
8604
8605 if (!orig_pdi->has_children)
8606 return info_ptr;
8607
4bb7a0a7 8608 /* Skip the children the long way. */
91c24f0a 8609
dee91e82 8610 return skip_children (reader, info_ptr);
91c24f0a
DC
8611}
8612
257e7a09 8613/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8614 not NULL. */
c906108c 8615
891813be
TT
8616void
8617dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8618{
ed2dc618
SM
8619 struct dwarf2_per_objfile *dwarf2_per_objfile
8620 = get_dwarf2_per_objfile (objfile);
8621
077cbab2
TT
8622 gdb_assert (!readin);
8623 /* If this psymtab is constructed from a debug-only objfile, the
8624 has_section_at_zero flag will not necessarily be correct. We
8625 can get the correct value for this flag by looking at the data
8626 associated with the (presumably stripped) associated objfile. */
8627 if (objfile->separate_debug_objfile_backlink)
c906108c 8628 {
077cbab2
TT
8629 struct dwarf2_per_objfile *dpo_backlink
8630 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8631
077cbab2
TT
8632 dwarf2_per_objfile->has_section_at_zero
8633 = dpo_backlink->has_section_at_zero;
8634 }
98bfdba5 8635
8566b89b 8636 expand_psymtab (objfile);
95554aad 8637
ed2dc618 8638 process_cu_includes (dwarf2_per_objfile);
c906108c 8639}
9cdd5dbd
DE
8640\f
8641/* Reading in full CUs. */
c906108c 8642
10b3939b
DJ
8643/* Add PER_CU to the queue. */
8644
8645static void
95554aad
TT
8646queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8647 enum language pretend_language)
10b3939b 8648{
10b3939b 8649 per_cu->queued = 1;
39856def 8650 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8651}
8652
89e63ee4
DE
8653/* If PER_CU is not yet queued, add it to the queue.
8654 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8655 dependency.
0907af0c 8656 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8657 meaning either PER_CU is already queued or it is already loaded.
8658
8659 N.B. There is an invariant here that if a CU is queued then it is loaded.
8660 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8661
8662static int
89e63ee4 8663maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8664 struct dwarf2_per_cu_data *per_cu,
8665 enum language pretend_language)
8666{
8667 /* We may arrive here during partial symbol reading, if we need full
8668 DIEs to process an unusual case (e.g. template arguments). Do
8669 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8670 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8671 {
8672 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8673 return 1;
8674 return 0;
8675 }
8676
8677 /* Mark the dependence relation so that we don't flush PER_CU
8678 too early. */
89e63ee4
DE
8679 if (dependent_cu != NULL)
8680 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8681
8682 /* If it's already on the queue, we have nothing to do. */
8683 if (per_cu->queued)
8684 return 0;
8685
8686 /* If the compilation unit is already loaded, just mark it as
8687 used. */
8688 if (per_cu->cu != NULL)
8689 {
8690 per_cu->cu->last_used = 0;
8691 return 0;
8692 }
8693
8694 /* Add it to the queue. */
8695 queue_comp_unit (per_cu, pretend_language);
8696
8697 return 1;
8698}
8699
10b3939b
DJ
8700/* Process the queue. */
8701
8702static void
ed2dc618 8703process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8704{
b4f54984 8705 if (dwarf_read_debug)
45cfd468
DE
8706 {
8707 fprintf_unfiltered (gdb_stdlog,
8708 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8709 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8710 }
8711
03dd20cc
DJ
8712 /* The queue starts out with one item, but following a DIE reference
8713 may load a new CU, adding it to the end of the queue. */
39856def 8714 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8715 {
39856def
TT
8716 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8717
cc12ce38 8718 if ((dwarf2_per_objfile->using_index
39856def
TT
8719 ? !item.per_cu->v.quick->compunit_symtab
8720 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8721 /* Skip dummy CUs. */
39856def 8722 && item.per_cu->cu != NULL)
f4dc4d17 8723 {
39856def 8724 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8725 unsigned int debug_print_threshold;
247f5c4f 8726 char buf[100];
f4dc4d17 8727
247f5c4f 8728 if (per_cu->is_debug_types)
f4dc4d17 8729 {
247f5c4f
DE
8730 struct signatured_type *sig_type =
8731 (struct signatured_type *) per_cu;
8732
9d8780f0 8733 sprintf (buf, "TU %s at offset %s",
73be47f5 8734 hex_string (sig_type->signature),
9d8780f0 8735 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8736 /* There can be 100s of TUs.
8737 Only print them in verbose mode. */
8738 debug_print_threshold = 2;
f4dc4d17 8739 }
247f5c4f 8740 else
73be47f5 8741 {
9d8780f0
SM
8742 sprintf (buf, "CU at offset %s",
8743 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8744 debug_print_threshold = 1;
8745 }
247f5c4f 8746
b4f54984 8747 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8748 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8749
8750 if (per_cu->is_debug_types)
39856def 8751 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8752 else
39856def 8753 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8754
b4f54984 8755 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8756 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8757 }
10b3939b 8758
39856def
TT
8759 item.per_cu->queued = 0;
8760 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8761 }
8762
b4f54984 8763 if (dwarf_read_debug)
45cfd468
DE
8764 {
8765 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8766 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8767 }
10b3939b
DJ
8768}
8769
10b3939b
DJ
8770/* Read in full symbols for PST, and anything it depends on. */
8771
8566b89b
TT
8772void
8773dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8774{
8566b89b 8775 if (readin)
95554aad
TT
8776 return;
8777
0494dbec 8778 read_dependencies (objfile);
aaa75496 8779
b83470bf
TT
8780 dw2_do_instantiate_symtab (per_cu_data, false);
8781 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8782}
8783
dee91e82
DE
8784/* Trivial hash function for die_info: the hash value of a DIE
8785 is its offset in .debug_info for this objfile. */
10b3939b 8786
dee91e82
DE
8787static hashval_t
8788die_hash (const void *item)
10b3939b 8789{
9a3c8263 8790 const struct die_info *die = (const struct die_info *) item;
6502dd73 8791
9c541725 8792 return to_underlying (die->sect_off);
dee91e82 8793}
63d06c5c 8794
dee91e82
DE
8795/* Trivial comparison function for die_info structures: two DIEs
8796 are equal if they have the same offset. */
98bfdba5 8797
dee91e82
DE
8798static int
8799die_eq (const void *item_lhs, const void *item_rhs)
8800{
9a3c8263
SM
8801 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8802 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8803
9c541725 8804 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8805}
c906108c 8806
c0ab21c2 8807/* Load the DIEs associated with PER_CU into memory. */
c906108c 8808
dee91e82 8809static void
c0ab21c2
TT
8810load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8811 bool skip_partial,
8812 enum language pretend_language)
dee91e82 8813{
c0ab21c2
TT
8814 gdb_assert (! this_cu->is_debug_types);
8815
6751ebae 8816 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8817 if (reader.dummy_p)
8818 return;
8819
8820 struct dwarf2_cu *cu = reader.cu;
8821 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8822
dee91e82
DE
8823 gdb_assert (cu->die_hash == NULL);
8824 cu->die_hash =
8825 htab_create_alloc_ex (cu->header.length / 12,
8826 die_hash,
8827 die_eq,
8828 NULL,
8829 &cu->comp_unit_obstack,
8830 hashtab_obstack_allocate,
8831 dummy_obstack_deallocate);
e142c38c 8832
3e225074 8833 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8834 reader.comp_unit_die->child
8835 = read_die_and_siblings (&reader, reader.info_ptr,
8836 &info_ptr, reader.comp_unit_die);
8837 cu->dies = reader.comp_unit_die;
dee91e82 8838 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8839
8840 /* We try not to read any attributes in this function, because not
9cdd5dbd 8841 all CUs needed for references have been loaded yet, and symbol
10b3939b 8842 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8843 or we won't be able to build types correctly.
8844 Similarly, if we do not read the producer, we can not apply
8845 producer-specific interpretation. */
c0ab21c2 8846 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8847
8848 reader.keep ();
10b3939b
DJ
8849}
8850
3da10d80
KS
8851/* Add a DIE to the delayed physname list. */
8852
8853static void
8854add_to_method_list (struct type *type, int fnfield_index, int index,
8855 const char *name, struct die_info *die,
8856 struct dwarf2_cu *cu)
8857{
8858 struct delayed_method_info mi;
8859 mi.type = type;
8860 mi.fnfield_index = fnfield_index;
8861 mi.index = index;
8862 mi.name = name;
8863 mi.die = die;
c89b44cd 8864 cu->method_list.push_back (mi);
3da10d80
KS
8865}
8866
3693fdb3
PA
8867/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8868 "const" / "volatile". If so, decrements LEN by the length of the
8869 modifier and return true. Otherwise return false. */
8870
8871template<size_t N>
8872static bool
8873check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8874{
8875 size_t mod_len = sizeof (mod) - 1;
8876 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8877 {
8878 len -= mod_len;
8879 return true;
8880 }
8881 return false;
8882}
8883
3da10d80
KS
8884/* Compute the physnames of any methods on the CU's method list.
8885
8886 The computation of method physnames is delayed in order to avoid the
8887 (bad) condition that one of the method's formal parameters is of an as yet
8888 incomplete type. */
8889
8890static void
8891compute_delayed_physnames (struct dwarf2_cu *cu)
8892{
3693fdb3 8893 /* Only C++ delays computing physnames. */
c89b44cd 8894 if (cu->method_list.empty ())
3693fdb3
PA
8895 return;
8896 gdb_assert (cu->language == language_cplus);
8897
52941706 8898 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8899 {
1d06ead6 8900 const char *physname;
3da10d80 8901 struct fn_fieldlist *fn_flp
c89b44cd
TT
8902 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8903 physname = dwarf2_physname (mi.name, mi.die, cu);
8904 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8905 = physname ? physname : "";
3693fdb3
PA
8906
8907 /* Since there's no tag to indicate whether a method is a
8908 const/volatile overload, extract that information out of the
8909 demangled name. */
8910 if (physname != NULL)
8911 {
8912 size_t len = strlen (physname);
8913
8914 while (1)
8915 {
8916 if (physname[len] == ')') /* shortcut */
8917 break;
8918 else if (check_modifier (physname, len, " const"))
c89b44cd 8919 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8920 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8921 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8922 else
8923 break;
8924 }
8925 }
3da10d80 8926 }
c89b44cd
TT
8927
8928 /* The list is no longer needed. */
8929 cu->method_list.clear ();
3da10d80
KS
8930}
8931
a766d390
DE
8932/* Go objects should be embedded in a DW_TAG_module DIE,
8933 and it's not clear if/how imported objects will appear.
8934 To keep Go support simple until that's worked out,
8935 go back through what we've read and create something usable.
8936 We could do this while processing each DIE, and feels kinda cleaner,
8937 but that way is more invasive.
8938 This is to, for example, allow the user to type "p var" or "b main"
8939 without having to specify the package name, and allow lookups
8940 of module.object to work in contexts that use the expression
8941 parser. */
8942
8943static void
8944fixup_go_packaging (struct dwarf2_cu *cu)
8945{
421d1616 8946 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8947 struct pending *list;
8948 int i;
8949
c24bdb02 8950 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8951 list != NULL;
8952 list = list->next)
a766d390
DE
8953 {
8954 for (i = 0; i < list->nsyms; ++i)
8955 {
8956 struct symbol *sym = list->symbol[i];
8957
c1b5c1eb 8958 if (sym->language () == language_go
a766d390
DE
8959 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8960 {
421d1616
TT
8961 gdb::unique_xmalloc_ptr<char> this_package_name
8962 (go_symbol_package_name (sym));
a766d390
DE
8963
8964 if (this_package_name == NULL)
8965 continue;
8966 if (package_name == NULL)
421d1616 8967 package_name = std::move (this_package_name);
a766d390
DE
8968 else
8969 {
518817b3
SM
8970 struct objfile *objfile
8971 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 8972 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 8973 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8974 (symbol_symtab (sym) != NULL
8975 ? symtab_to_filename_for_display
8976 (symbol_symtab (sym))
e3b94546 8977 : objfile_name (objfile)),
421d1616 8978 this_package_name.get (), package_name.get ());
a766d390
DE
8979 }
8980 }
8981 }
8982 }
8983
8984 if (package_name != NULL)
8985 {
518817b3 8986 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 8987 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
8988 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8989 saved_package_name);
a766d390
DE
8990 struct symbol *sym;
8991
e623cf5d 8992 sym = allocate_symbol (objfile);
d3ecddab 8993 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 8994 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
8995 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8996 e.g., "main" finds the "main" module and not C's main(). */
8997 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8998 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8999 SYMBOL_TYPE (sym) = type;
9000
c24bdb02 9001 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9002 }
9003}
9004
c9317f21
TT
9005/* Allocate a fully-qualified name consisting of the two parts on the
9006 obstack. */
9007
9008static const char *
9009rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9010{
9011 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9012}
9013
9014/* A helper that allocates a struct discriminant_info to attach to a
9015 union type. */
9016
9017static struct discriminant_info *
9018alloc_discriminant_info (struct type *type, int discriminant_index,
9019 int default_index)
9020{
9021 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9022 gdb_assert (discriminant_index == -1
9023 || (discriminant_index >= 0
9024 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9025 gdb_assert (default_index == -1
c7b15a66 9026 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9027
9028 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9029
9030 struct discriminant_info *disc
9031 = ((struct discriminant_info *)
9032 TYPE_ZALLOC (type,
9033 offsetof (struct discriminant_info, discriminants)
9034 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9035 disc->default_index = default_index;
9036 disc->discriminant_index = discriminant_index;
9037
9038 struct dynamic_prop prop;
9039 prop.kind = PROP_UNDEFINED;
9040 prop.data.baton = disc;
9041
9042 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9043
9044 return disc;
9045}
9046
9047/* Some versions of rustc emitted enums in an unusual way.
9048
9049 Ordinary enums were emitted as unions. The first element of each
9050 structure in the union was named "RUST$ENUM$DISR". This element
9051 held the discriminant.
9052
9053 These versions of Rust also implemented the "non-zero"
9054 optimization. When the enum had two values, and one is empty and
9055 the other holds a pointer that cannot be zero, the pointer is used
9056 as the discriminant, with a zero value meaning the empty variant.
9057 Here, the union's first member is of the form
9058 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9059 where the fieldnos are the indices of the fields that should be
9060 traversed in order to find the field (which may be several fields deep)
9061 and the variantname is the name of the variant of the case when the
9062 field is zero.
9063
9064 This function recognizes whether TYPE is of one of these forms,
9065 and, if so, smashes it to be a variant type. */
9066
9067static void
9068quirk_rust_enum (struct type *type, struct objfile *objfile)
9069{
9070 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9071
9072 /* We don't need to deal with empty enums. */
9073 if (TYPE_NFIELDS (type) == 0)
9074 return;
9075
9076#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9077 if (TYPE_NFIELDS (type) == 1
9078 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9079 {
9080 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9081
9082 /* Decode the field name to find the offset of the
9083 discriminant. */
9084 ULONGEST bit_offset = 0;
9085 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9086 while (name[0] >= '0' && name[0] <= '9')
9087 {
9088 char *tail;
9089 unsigned long index = strtoul (name, &tail, 10);
9090 name = tail;
9091 if (*name != '$'
9092 || index >= TYPE_NFIELDS (field_type)
9093 || (TYPE_FIELD_LOC_KIND (field_type, index)
9094 != FIELD_LOC_KIND_BITPOS))
9095 {
b98664d3 9096 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9097 "[in module %s]"),
9098 TYPE_FIELD_NAME (type, 0),
9099 objfile_name (objfile));
9100 return;
9101 }
9102 ++name;
9103
9104 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9105 field_type = TYPE_FIELD_TYPE (field_type, index);
9106 }
9107
9108 /* Make a union to hold the variants. */
9109 struct type *union_type = alloc_type (objfile);
9110 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9111 TYPE_NFIELDS (union_type) = 3;
9112 TYPE_FIELDS (union_type)
9113 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9114 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9115 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9116
9117 /* Put the discriminant must at index 0. */
9118 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9119 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9120 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9121 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9122
9123 /* The order of fields doesn't really matter, so put the real
9124 field at index 1 and the data-less field at index 2. */
9125 struct discriminant_info *disc
9126 = alloc_discriminant_info (union_type, 0, 1);
9127 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9128 TYPE_FIELD_NAME (union_type, 1)
9129 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9130 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9131 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9132 TYPE_FIELD_NAME (union_type, 1));
9133
9134 const char *dataless_name
9135 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9136 name);
9137 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9138 dataless_name);
9139 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9140 /* NAME points into the original discriminant name, which
9141 already has the correct lifetime. */
9142 TYPE_FIELD_NAME (union_type, 2) = name;
9143 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9144 disc->discriminants[2] = 0;
9145
9146 /* Smash this type to be a structure type. We have to do this
9147 because the type has already been recorded. */
9148 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9149 TYPE_NFIELDS (type) = 1;
9150 TYPE_FIELDS (type)
9151 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9152
9153 /* Install the variant part. */
9154 TYPE_FIELD_TYPE (type, 0) = union_type;
9155 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9156 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9157 }
77c2dba3
TT
9158 /* A union with a single anonymous field is probably an old-style
9159 univariant enum. */
9160 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9161 {
c9317f21
TT
9162 /* Smash this type to be a structure type. We have to do this
9163 because the type has already been recorded. */
9164 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9165
9166 /* Make a union to hold the variants. */
9167 struct type *union_type = alloc_type (objfile);
9168 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9169 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9170 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9171 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9172 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9173
9174 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9175 const char *variant_name
9176 = rust_last_path_segment (TYPE_NAME (field_type));
9177 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9178 TYPE_NAME (field_type)
9179 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9180 TYPE_NAME (type), variant_name);
c9317f21
TT
9181
9182 /* Install the union in the outer struct type. */
9183 TYPE_NFIELDS (type) = 1;
9184 TYPE_FIELDS (type)
9185 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9186 TYPE_FIELD_TYPE (type, 0) = union_type;
9187 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9188 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9189
9190 alloc_discriminant_info (union_type, -1, 0);
9191 }
9192 else
9193 {
9194 struct type *disr_type = nullptr;
9195 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9196 {
9197 disr_type = TYPE_FIELD_TYPE (type, i);
9198
a037790e
TT
9199 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9200 {
9201 /* All fields of a true enum will be structs. */
9202 return;
9203 }
9204 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9205 {
9206 /* Could be data-less variant, so keep going. */
a037790e 9207 disr_type = nullptr;
c9317f21
TT
9208 }
9209 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9210 "RUST$ENUM$DISR") != 0)
9211 {
9212 /* Not a Rust enum. */
9213 return;
9214 }
9215 else
9216 {
9217 /* Found one. */
9218 break;
9219 }
9220 }
9221
9222 /* If we got here without a discriminant, then it's probably
9223 just a union. */
9224 if (disr_type == nullptr)
9225 return;
9226
9227 /* Smash this type to be a structure type. We have to do this
9228 because the type has already been recorded. */
9229 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9230
9231 /* Make a union to hold the variants. */
9232 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9233 struct type *union_type = alloc_type (objfile);
9234 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9235 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9236 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9237 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9238 TYPE_FIELDS (union_type)
9239 = (struct field *) TYPE_ZALLOC (union_type,
9240 (TYPE_NFIELDS (union_type)
9241 * sizeof (struct field)));
9242
9243 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9244 TYPE_NFIELDS (type) * sizeof (struct field));
9245
9246 /* Install the discriminant at index 0 in the union. */
9247 TYPE_FIELD (union_type, 0) = *disr_field;
9248 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9249 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9250
9251 /* Install the union in the outer struct type. */
9252 TYPE_FIELD_TYPE (type, 0) = union_type;
9253 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9254 TYPE_NFIELDS (type) = 1;
9255
9256 /* Set the size and offset of the union type. */
9257 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9258
9259 /* We need a way to find the correct discriminant given a
9260 variant name. For convenience we build a map here. */
9261 struct type *enum_type = FIELD_TYPE (*disr_field);
9262 std::unordered_map<std::string, ULONGEST> discriminant_map;
9263 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9264 {
9265 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9266 {
9267 const char *name
9268 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9269 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9270 }
9271 }
9272
9273 int n_fields = TYPE_NFIELDS (union_type);
9274 struct discriminant_info *disc
9275 = alloc_discriminant_info (union_type, 0, -1);
9276 /* Skip the discriminant here. */
9277 for (int i = 1; i < n_fields; ++i)
9278 {
9279 /* Find the final word in the name of this variant's type.
9280 That name can be used to look up the correct
9281 discriminant. */
9282 const char *variant_name
9283 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9284 i)));
9285
9286 auto iter = discriminant_map.find (variant_name);
9287 if (iter != discriminant_map.end ())
9288 disc->discriminants[i] = iter->second;
9289
bedda9ac 9290 /* Remove the discriminant field, if it exists. */
c9317f21 9291 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9292 if (TYPE_NFIELDS (sub_type) > 0)
9293 {
9294 --TYPE_NFIELDS (sub_type);
9295 ++TYPE_FIELDS (sub_type);
9296 }
c9317f21
TT
9297 TYPE_FIELD_NAME (union_type, i) = variant_name;
9298 TYPE_NAME (sub_type)
9299 = rust_fully_qualify (&objfile->objfile_obstack,
9300 TYPE_NAME (type), variant_name);
9301 }
9302 }
9303}
9304
9305/* Rewrite some Rust unions to be structures with variants parts. */
9306
9307static void
9308rust_union_quirks (struct dwarf2_cu *cu)
9309{
9310 gdb_assert (cu->language == language_rust);
52941706
SM
9311 for (type *type_ : cu->rust_unions)
9312 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9313 /* We don't need this any more. */
9314 cu->rust_unions.clear ();
c9317f21
TT
9315}
9316
95554aad
TT
9317/* Return the symtab for PER_CU. This works properly regardless of
9318 whether we're using the index or psymtabs. */
9319
43f3e411
DE
9320static struct compunit_symtab *
9321get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9322{
ed2dc618 9323 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9324 ? per_cu->v.quick->compunit_symtab
9325 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9326}
9327
9328/* A helper function for computing the list of all symbol tables
9329 included by PER_CU. */
9330
9331static void
4c39bc03 9332recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9333 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9334 struct dwarf2_per_cu_data *per_cu,
43f3e411 9335 struct compunit_symtab *immediate_parent)
95554aad
TT
9336{
9337 void **slot;
43f3e411 9338 struct compunit_symtab *cust;
95554aad
TT
9339
9340 slot = htab_find_slot (all_children, per_cu, INSERT);
9341 if (*slot != NULL)
9342 {
9343 /* This inclusion and its children have been processed. */
9344 return;
9345 }
9346
9347 *slot = per_cu;
9348 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9349 cust = get_compunit_symtab (per_cu);
9350 if (cust != NULL)
ec94af83
DE
9351 {
9352 /* If this is a type unit only add its symbol table if we haven't
9353 seen it yet (type unit per_cu's can share symtabs). */
9354 if (per_cu->is_debug_types)
9355 {
43f3e411 9356 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9357 if (*slot == NULL)
9358 {
43f3e411 9359 *slot = cust;
4c39bc03 9360 result->push_back (cust);
43f3e411
DE
9361 if (cust->user == NULL)
9362 cust->user = immediate_parent;
ec94af83
DE
9363 }
9364 }
9365 else
f9125b6c 9366 {
4c39bc03 9367 result->push_back (cust);
43f3e411
DE
9368 if (cust->user == NULL)
9369 cust->user = immediate_parent;
f9125b6c 9370 }
ec94af83 9371 }
95554aad 9372
ae640021
AB
9373 if (!per_cu->imported_symtabs_empty ())
9374 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9375 {
9376 recursively_compute_inclusions (result, all_children,
9377 all_type_symtabs, ptr, cust);
9378 }
95554aad
TT
9379}
9380
43f3e411 9381/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9382 PER_CU. */
9383
9384static void
43f3e411 9385compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9386{
f4dc4d17
DE
9387 gdb_assert (! per_cu->is_debug_types);
9388
ae640021 9389 if (!per_cu->imported_symtabs_empty ())
95554aad 9390 {
ae640021 9391 int len;
4c39bc03 9392 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9393 htab_t all_children, all_type_symtabs;
43f3e411 9394 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9395
9396 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9397 if (cust == NULL)
95554aad
TT
9398 return;
9399
9400 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9401 NULL, xcalloc, xfree);
ec94af83
DE
9402 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9403 NULL, xcalloc, xfree);
95554aad 9404
ae640021 9405 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9406 {
9407 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9408 all_type_symtabs, ptr, cust);
ec94af83 9409 }
95554aad 9410
ec94af83 9411 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9412 len = result_symtabs.size ();
43f3e411 9413 cust->includes
ed2dc618 9414 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9415 struct compunit_symtab *, len + 1);
4c39bc03
TT
9416 memcpy (cust->includes, result_symtabs.data (),
9417 len * sizeof (compunit_symtab *));
43f3e411 9418 cust->includes[len] = NULL;
95554aad 9419
95554aad 9420 htab_delete (all_children);
ec94af83 9421 htab_delete (all_type_symtabs);
95554aad
TT
9422 }
9423}
9424
9425/* Compute the 'includes' field for the symtabs of all the CUs we just
9426 read. */
9427
9428static void
ed2dc618 9429process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9430{
71b73764 9431 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9432 {
9433 if (! iter->is_debug_types)
43f3e411 9434 compute_compunit_symtab_includes (iter);
f4dc4d17 9435 }
95554aad 9436
c5d0225d 9437 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9438}
9439
9cdd5dbd 9440/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9441 already been loaded into memory. */
9442
9443static void
95554aad
TT
9444process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9445 enum language pretend_language)
10b3939b 9446{
10b3939b 9447 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9448 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9449 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9450 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9451 CORE_ADDR lowpc, highpc;
43f3e411 9452 struct compunit_symtab *cust;
10b3939b 9453 CORE_ADDR baseaddr;
4359dff1 9454 struct block *static_block;
3e29f34a 9455 CORE_ADDR addr;
10b3939b 9456
b3b3bada 9457 baseaddr = objfile->text_section_offset ();
10b3939b 9458
c89b44cd
TT
9459 /* Clear the list here in case something was left over. */
9460 cu->method_list.clear ();
10b3939b 9461
95554aad
TT
9462 cu->language = pretend_language;
9463 cu->language_defn = language_def (cu->language);
9464
c906108c 9465 /* Do line number decoding in read_file_scope () */
10b3939b 9466 process_die (cu->dies, cu);
c906108c 9467
a766d390
DE
9468 /* For now fudge the Go package. */
9469 if (cu->language == language_go)
9470 fixup_go_packaging (cu);
9471
5f48f8f3 9472 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9473 should be complete, and it should now be safe to compute all of the
9474 physnames. */
9475 compute_delayed_physnames (cu);
3da10d80 9476
c9317f21
TT
9477 if (cu->language == language_rust)
9478 rust_union_quirks (cu);
9479
fae299cd
DC
9480 /* Some compilers don't define a DW_AT_high_pc attribute for the
9481 compilation unit. If the DW_AT_high_pc is missing, synthesize
9482 it, by scanning the DIE's below the compilation unit. */
10b3939b 9483 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9484
3e29f34a 9485 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9486 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9487
9488 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9489 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9490 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9491 addrmap to help ensure it has an accurate map of pc values belonging to
9492 this comp unit. */
9493 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9494
c24bdb02 9495 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9496 SECT_OFF_TEXT (objfile),
9497 0);
c906108c 9498
43f3e411 9499 if (cust != NULL)
c906108c 9500 {
df15bd07 9501 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9502
8be455d7
JK
9503 /* Set symtab language to language from DW_AT_language. If the
9504 compilation is from a C file generated by language preprocessors, do
9505 not set the language if it was already deduced by start_subfile. */
43f3e411 9506 if (!(cu->language == language_c
40e3ad0e 9507 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9508 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9509
9510 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9511 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9512 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9513 there were bugs in prologue debug info, fixed later in GCC-4.5
9514 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9515
9516 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9517 needed, it would be wrong due to missing DW_AT_producer there.
9518
9519 Still one can confuse GDB by using non-standard GCC compilation
9520 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9521 */
ab260dad 9522 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9523 cust->locations_valid = 1;
e0d00bc7
JK
9524
9525 if (gcc_4_minor >= 5)
43f3e411 9526 cust->epilogue_unwind_valid = 1;
96408a79 9527
43f3e411 9528 cust->call_site_htab = cu->call_site_htab;
c906108c 9529 }
9291a0cd
TT
9530
9531 if (dwarf2_per_objfile->using_index)
43f3e411 9532 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9533 else
9534 {
891813be 9535 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9536 pst->compunit_symtab = cust;
6d94535f 9537 pst->readin = true;
9291a0cd 9538 }
c906108c 9539
95554aad 9540 /* Push it for inclusion processing later. */
c5d0225d 9541 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9542
9543 /* Not needed any more. */
c24bdb02 9544 cu->reset_builder ();
f4dc4d17 9545}
45cfd468 9546
f4dc4d17
DE
9547/* Generate full symbol information for type unit PER_CU, whose DIEs have
9548 already been loaded into memory. */
9549
9550static void
9551process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9552 enum language pretend_language)
9553{
9554 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9555 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9556 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9557 struct compunit_symtab *cust;
0186c6a7
DE
9558 struct signatured_type *sig_type;
9559
9560 gdb_assert (per_cu->is_debug_types);
9561 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9562
c89b44cd
TT
9563 /* Clear the list here in case something was left over. */
9564 cu->method_list.clear ();
f4dc4d17 9565
f4dc4d17
DE
9566 cu->language = pretend_language;
9567 cu->language_defn = language_def (cu->language);
9568
9569 /* The symbol tables are set up in read_type_unit_scope. */
9570 process_die (cu->dies, cu);
9571
9572 /* For now fudge the Go package. */
9573 if (cu->language == language_go)
9574 fixup_go_packaging (cu);
9575
5f48f8f3 9576 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9577 should be complete, and it should now be safe to compute all of the
9578 physnames. */
9579 compute_delayed_physnames (cu);
f4dc4d17 9580
c9317f21
TT
9581 if (cu->language == language_rust)
9582 rust_union_quirks (cu);
9583
f4dc4d17
DE
9584 /* TUs share symbol tables.
9585 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9586 of it with end_expandable_symtab. Otherwise, complete the addition of
9587 this TU's symbols to the existing symtab. */
43f3e411 9588 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9589 {
c24bdb02
KS
9590 buildsym_compunit *builder = cu->get_builder ();
9591 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9592 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9593
43f3e411 9594 if (cust != NULL)
f4dc4d17
DE
9595 {
9596 /* Set symtab language to language from DW_AT_language. If the
9597 compilation is from a C file generated by language preprocessors,
9598 do not set the language if it was already deduced by
9599 start_subfile. */
43f3e411
DE
9600 if (!(cu->language == language_c
9601 && COMPUNIT_FILETABS (cust)->language != language_c))
9602 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9603 }
9604 }
9605 else
9606 {
c24bdb02 9607 cu->get_builder ()->augment_type_symtab ();
43f3e411 9608 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9609 }
9610
9611 if (dwarf2_per_objfile->using_index)
43f3e411 9612 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9613 else
9614 {
891813be 9615 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9616 pst->compunit_symtab = cust;
6d94535f 9617 pst->readin = true;
45cfd468 9618 }
804d2729
TT
9619
9620 /* Not needed any more. */
c24bdb02 9621 cu->reset_builder ();
c906108c
SS
9622}
9623
95554aad
TT
9624/* Process an imported unit DIE. */
9625
9626static void
9627process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9628{
9629 struct attribute *attr;
9630
f4dc4d17
DE
9631 /* For now we don't handle imported units in type units. */
9632 if (cu->per_cu->is_debug_types)
9633 {
9634 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9635 " supported in type units [in module %s]"),
518817b3 9636 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9637 }
9638
95554aad
TT
9639 attr = dwarf2_attr (die, DW_AT_import, cu);
9640 if (attr != NULL)
9641 {
0826b30a 9642 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9643 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9644 dwarf2_per_cu_data *per_cu
e3b94546 9645 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9646 cu->per_cu->dwarf2_per_objfile);
95554aad 9647
58990295
TV
9648 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9649 into another compilation unit, at root level. Regard this as a hint,
9650 and ignore it. */
9651 if (die->parent && die->parent->parent == NULL
9652 && per_cu->unit_type == DW_UT_compile
9653 && per_cu->lang == language_cplus)
9654 return;
9655
69d751e3 9656 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9657 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9658 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9659
ae640021 9660 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9661 }
9662}
9663
4c8aa72d
PA
9664/* RAII object that represents a process_die scope: i.e.,
9665 starts/finishes processing a DIE. */
9666class process_die_scope
adde2bff 9667{
4c8aa72d
PA
9668public:
9669 process_die_scope (die_info *die, dwarf2_cu *cu)
9670 : m_die (die), m_cu (cu)
9671 {
9672 /* We should only be processing DIEs not already in process. */
9673 gdb_assert (!m_die->in_process);
9674 m_die->in_process = true;
9675 }
8c3cb9fa 9676
4c8aa72d
PA
9677 ~process_die_scope ()
9678 {
9679 m_die->in_process = false;
9680
9681 /* If we're done processing the DIE for the CU that owns the line
9682 header, we don't need the line header anymore. */
9683 if (m_cu->line_header_die_owner == m_die)
9684 {
9685 delete m_cu->line_header;
9686 m_cu->line_header = NULL;
9687 m_cu->line_header_die_owner = NULL;
9688 }
9689 }
9690
9691private:
9692 die_info *m_die;
9693 dwarf2_cu *m_cu;
9694};
adde2bff 9695
c906108c
SS
9696/* Process a die and its children. */
9697
9698static void
e7c27a73 9699process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9700{
4c8aa72d 9701 process_die_scope scope (die, cu);
adde2bff 9702
c906108c
SS
9703 switch (die->tag)
9704 {
9705 case DW_TAG_padding:
9706 break;
9707 case DW_TAG_compile_unit:
95554aad 9708 case DW_TAG_partial_unit:
e7c27a73 9709 read_file_scope (die, cu);
c906108c 9710 break;
348e048f
DE
9711 case DW_TAG_type_unit:
9712 read_type_unit_scope (die, cu);
9713 break;
c906108c 9714 case DW_TAG_subprogram:
0a4b0913
AB
9715 /* Nested subprograms in Fortran get a prefix. */
9716 if (cu->language == language_fortran
9717 && die->parent != NULL
9718 && die->parent->tag == DW_TAG_subprogram)
9719 cu->processing_has_namespace_info = true;
9720 /* Fall through. */
c906108c 9721 case DW_TAG_inlined_subroutine:
edb3359d 9722 read_func_scope (die, cu);
c906108c
SS
9723 break;
9724 case DW_TAG_lexical_block:
14898363
L
9725 case DW_TAG_try_block:
9726 case DW_TAG_catch_block:
e7c27a73 9727 read_lexical_block_scope (die, cu);
c906108c 9728 break;
216f72a1 9729 case DW_TAG_call_site:
96408a79
SA
9730 case DW_TAG_GNU_call_site:
9731 read_call_site_scope (die, cu);
9732 break;
c906108c 9733 case DW_TAG_class_type:
680b30c7 9734 case DW_TAG_interface_type:
c906108c
SS
9735 case DW_TAG_structure_type:
9736 case DW_TAG_union_type:
134d01f1 9737 process_structure_scope (die, cu);
c906108c
SS
9738 break;
9739 case DW_TAG_enumeration_type:
134d01f1 9740 process_enumeration_scope (die, cu);
c906108c 9741 break;
134d01f1 9742
f792889a
DJ
9743 /* These dies have a type, but processing them does not create
9744 a symbol or recurse to process the children. Therefore we can
9745 read them on-demand through read_type_die. */
c906108c 9746 case DW_TAG_subroutine_type:
72019c9c 9747 case DW_TAG_set_type:
c906108c 9748 case DW_TAG_array_type:
c906108c 9749 case DW_TAG_pointer_type:
c906108c 9750 case DW_TAG_ptr_to_member_type:
c906108c 9751 case DW_TAG_reference_type:
4297a3f0 9752 case DW_TAG_rvalue_reference_type:
c906108c 9753 case DW_TAG_string_type:
c906108c 9754 break;
134d01f1 9755
c906108c 9756 case DW_TAG_base_type:
a02abb62 9757 case DW_TAG_subrange_type:
cb249c71 9758 case DW_TAG_typedef:
134d01f1
DJ
9759 /* Add a typedef symbol for the type definition, if it has a
9760 DW_AT_name. */
f792889a 9761 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9762 break;
c906108c 9763 case DW_TAG_common_block:
e7c27a73 9764 read_common_block (die, cu);
c906108c
SS
9765 break;
9766 case DW_TAG_common_inclusion:
9767 break;
d9fa45fe 9768 case DW_TAG_namespace:
9068261f 9769 cu->processing_has_namespace_info = true;
e7c27a73 9770 read_namespace (die, cu);
d9fa45fe 9771 break;
5d7cb8df 9772 case DW_TAG_module:
9068261f 9773 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9774 read_module (die, cu);
9775 break;
d9fa45fe 9776 case DW_TAG_imported_declaration:
9068261f 9777 cu->processing_has_namespace_info = true;
74921315
KS
9778 if (read_namespace_alias (die, cu))
9779 break;
86a73007
TT
9780 /* The declaration is not a global namespace alias. */
9781 /* Fall through. */
d9fa45fe 9782 case DW_TAG_imported_module:
9068261f 9783 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9784 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9785 || cu->language != language_fortran))
b98664d3 9786 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9787 dwarf_tag_name (die->tag));
9788 read_import_statement (die, cu);
d9fa45fe 9789 break;
95554aad
TT
9790
9791 case DW_TAG_imported_unit:
9792 process_imported_unit_die (die, cu);
9793 break;
9794
71a3c369
TT
9795 case DW_TAG_variable:
9796 read_variable (die, cu);
9797 break;
9798
c906108c 9799 default:
e7c27a73 9800 new_symbol (die, NULL, cu);
c906108c
SS
9801 break;
9802 }
9803}
ca69b9e6
DE
9804\f
9805/* DWARF name computation. */
c906108c 9806
94af9270
KS
9807/* A helper function for dwarf2_compute_name which determines whether DIE
9808 needs to have the name of the scope prepended to the name listed in the
9809 die. */
9810
9811static int
9812die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9813{
1c809c68
TT
9814 struct attribute *attr;
9815
94af9270
KS
9816 switch (die->tag)
9817 {
9818 case DW_TAG_namespace:
9819 case DW_TAG_typedef:
9820 case DW_TAG_class_type:
9821 case DW_TAG_interface_type:
9822 case DW_TAG_structure_type:
9823 case DW_TAG_union_type:
9824 case DW_TAG_enumeration_type:
9825 case DW_TAG_enumerator:
9826 case DW_TAG_subprogram:
08a76f8a 9827 case DW_TAG_inlined_subroutine:
94af9270 9828 case DW_TAG_member:
74921315 9829 case DW_TAG_imported_declaration:
94af9270
KS
9830 return 1;
9831
9832 case DW_TAG_variable:
c2b0a229 9833 case DW_TAG_constant:
94af9270
KS
9834 /* We only need to prefix "globally" visible variables. These include
9835 any variable marked with DW_AT_external or any variable that
9836 lives in a namespace. [Variables in anonymous namespaces
9837 require prefixing, but they are not DW_AT_external.] */
9838
9839 if (dwarf2_attr (die, DW_AT_specification, cu))
9840 {
9841 struct dwarf2_cu *spec_cu = cu;
9a619af0 9842
94af9270
KS
9843 return die_needs_namespace (die_specification (die, &spec_cu),
9844 spec_cu);
9845 }
9846
1c809c68 9847 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9848 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9849 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9850 return 0;
9851 /* A variable in a lexical block of some kind does not need a
9852 namespace, even though in C++ such variables may be external
9853 and have a mangled name. */
9854 if (die->parent->tag == DW_TAG_lexical_block
9855 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9856 || die->parent->tag == DW_TAG_catch_block
9857 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9858 return 0;
9859 return 1;
94af9270
KS
9860
9861 default:
9862 return 0;
9863 }
9864}
9865
73b9be8b
KS
9866/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9867 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9868 defined for the given DIE. */
9869
9870static struct attribute *
9871dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9872{
9873 struct attribute *attr;
9874
9875 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9876 if (attr == NULL)
9877 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9878
9879 return attr;
9880}
9881
9882/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9883 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9884 defined for the given DIE. */
9885
9886static const char *
9887dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9888{
9889 const char *linkage_name;
9890
9891 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9892 if (linkage_name == NULL)
9893 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9894
9895 return linkage_name;
9896}
9897
94af9270 9898/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9899 compute the physname for the object, which include a method's:
9c37b5ae 9900 - formal parameters (C++),
a766d390 9901 - receiver type (Go),
a766d390
DE
9902
9903 The term "physname" is a bit confusing.
9904 For C++, for example, it is the demangled name.
9905 For Go, for example, it's the mangled name.
94af9270 9906
af6b7be1
JB
9907 For Ada, return the DIE's linkage name rather than the fully qualified
9908 name. PHYSNAME is ignored..
9909
94af9270
KS
9910 The result is allocated on the objfile_obstack and canonicalized. */
9911
9912static const char *
15d034d0
TT
9913dwarf2_compute_name (const char *name,
9914 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9915 int physname)
9916{
518817b3 9917 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 9918
94af9270
KS
9919 if (name == NULL)
9920 name = dwarf2_name (die, cu);
9921
2ee7123e
DE
9922 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9923 but otherwise compute it by typename_concat inside GDB.
9924 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9925 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9926 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9927 will set the demangled name to the result of dwarf2_full_name, and it is
9928 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9929 if (cu->language == language_ada
9930 || (cu->language == language_fortran && physname))
9931 {
9932 /* For Ada unit, we prefer the linkage name over the name, as
9933 the former contains the exported name, which the user expects
9934 to be able to reference. Ideally, we want the user to be able
9935 to reference this entity using either natural or linkage name,
9936 but we haven't started looking at this enhancement yet. */
73b9be8b 9937 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9938
2ee7123e
DE
9939 if (linkage_name != NULL)
9940 return linkage_name;
f55ee35c
JK
9941 }
9942
94af9270
KS
9943 /* These are the only languages we know how to qualify names in. */
9944 if (name != NULL
9c37b5ae 9945 && (cu->language == language_cplus
c44af4eb
TT
9946 || cu->language == language_fortran || cu->language == language_d
9947 || cu->language == language_rust))
94af9270
KS
9948 {
9949 if (die_needs_namespace (die, cu))
9950 {
0d5cff50 9951 const char *prefix;
34a68019 9952 const char *canonical_name = NULL;
94af9270 9953
d7e74731
PA
9954 string_file buf;
9955
94af9270 9956 prefix = determine_prefix (die, cu);
94af9270
KS
9957 if (*prefix != '\0')
9958 {
43816ebc
TT
9959 gdb::unique_xmalloc_ptr<char> prefixed_name
9960 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 9961
43816ebc 9962 buf.puts (prefixed_name.get ());
94af9270
KS
9963 }
9964 else
d7e74731 9965 buf.puts (name);
94af9270 9966
98bfdba5
PA
9967 /* Template parameters may be specified in the DIE's DW_AT_name, or
9968 as children with DW_TAG_template_type_param or
9969 DW_TAG_value_type_param. If the latter, add them to the name
9970 here. If the name already has template parameters, then
9971 skip this step; some versions of GCC emit both, and
9972 it is more efficient to use the pre-computed name.
9973
9974 Something to keep in mind about this process: it is very
9975 unlikely, or in some cases downright impossible, to produce
9976 something that will match the mangled name of a function.
9977 If the definition of the function has the same debug info,
9978 we should be able to match up with it anyway. But fallbacks
9979 using the minimal symbol, for instance to find a method
9980 implemented in a stripped copy of libstdc++, will not work.
9981 If we do not have debug info for the definition, we will have to
9982 match them up some other way.
9983
9984 When we do name matching there is a related problem with function
9985 templates; two instantiated function templates are allowed to
9986 differ only by their return types, which we do not add here. */
9987
9988 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9989 {
9990 struct attribute *attr;
9991 struct die_info *child;
9992 int first = 1;
9993
9994 die->building_fullname = 1;
9995
9996 for (child = die->child; child != NULL; child = child->sibling)
9997 {
9998 struct type *type;
12df843f 9999 LONGEST value;
d521ce57 10000 const gdb_byte *bytes;
98bfdba5
PA
10001 struct dwarf2_locexpr_baton *baton;
10002 struct value *v;
10003
10004 if (child->tag != DW_TAG_template_type_param
10005 && child->tag != DW_TAG_template_value_param)
10006 continue;
10007
10008 if (first)
10009 {
d7e74731 10010 buf.puts ("<");
98bfdba5
PA
10011 first = 0;
10012 }
10013 else
d7e74731 10014 buf.puts (", ");
98bfdba5
PA
10015
10016 attr = dwarf2_attr (child, DW_AT_type, cu);
10017 if (attr == NULL)
10018 {
b98664d3 10019 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10020 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10021 continue;
10022 }
10023 type = die_type (child, cu);
10024
10025 if (child->tag == DW_TAG_template_type_param)
10026 {
c1ec8cea
TT
10027 c_print_type (type, "", &buf, -1, 0, cu->language,
10028 &type_print_raw_options);
98bfdba5
PA
10029 continue;
10030 }
10031
10032 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10033 if (attr == NULL)
10034 {
b98664d3 10035 complaint (_("template parameter missing "
3e43a32a 10036 "DW_AT_const_value"));
d7e74731 10037 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10038 continue;
10039 }
10040
10041 dwarf2_const_value_attr (attr, type, name,
10042 &cu->comp_unit_obstack, cu,
10043 &value, &bytes, &baton);
10044
10045 if (TYPE_NOSIGN (type))
10046 /* GDB prints characters as NUMBER 'CHAR'. If that's
10047 changed, this can use value_print instead. */
d7e74731 10048 c_printchar (value, type, &buf);
98bfdba5
PA
10049 else
10050 {
10051 struct value_print_options opts;
10052
10053 if (baton != NULL)
10054 v = dwarf2_evaluate_loc_desc (type, NULL,
10055 baton->data,
10056 baton->size,
10057 baton->per_cu);
10058 else if (bytes != NULL)
10059 {
10060 v = allocate_value (type);
10061 memcpy (value_contents_writeable (v), bytes,
10062 TYPE_LENGTH (type));
10063 }
10064 else
10065 v = value_from_longest (type, value);
10066
3e43a32a
MS
10067 /* Specify decimal so that we do not depend on
10068 the radix. */
98bfdba5
PA
10069 get_formatted_print_options (&opts, 'd');
10070 opts.raw = 1;
d7e74731 10071 value_print (v, &buf, &opts);
98bfdba5 10072 release_value (v);
98bfdba5
PA
10073 }
10074 }
10075
10076 die->building_fullname = 0;
10077
10078 if (!first)
10079 {
10080 /* Close the argument list, with a space if necessary
10081 (nested templates). */
d7e74731
PA
10082 if (!buf.empty () && buf.string ().back () == '>')
10083 buf.puts (" >");
98bfdba5 10084 else
d7e74731 10085 buf.puts (">");
98bfdba5
PA
10086 }
10087 }
10088
9c37b5ae 10089 /* For C++ methods, append formal parameter type
94af9270 10090 information, if PHYSNAME. */
6e70227d 10091
94af9270 10092 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10093 && cu->language == language_cplus)
94af9270
KS
10094 {
10095 struct type *type = read_type_die (die, cu);
10096
d7e74731 10097 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10098 &type_print_raw_options);
94af9270 10099
9c37b5ae 10100 if (cu->language == language_cplus)
94af9270 10101 {
60430eff
DJ
10102 /* Assume that an artificial first parameter is
10103 "this", but do not crash if it is not. RealView
10104 marks unnamed (and thus unused) parameters as
10105 artificial; there is no way to differentiate
10106 the two cases. */
94af9270
KS
10107 if (TYPE_NFIELDS (type) > 0
10108 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10109 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10110 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10111 0))))
d7e74731 10112 buf.puts (" const");
94af9270
KS
10113 }
10114 }
10115
d7e74731 10116 const std::string &intermediate_name = buf.string ();
94af9270
KS
10117
10118 if (cu->language == language_cplus)
34a68019 10119 canonical_name
322a8516 10120 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10121 objfile);
34a68019
TT
10122
10123 /* If we only computed INTERMEDIATE_NAME, or if
10124 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10125 intern it. */
322a8516 10126 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10127 name = objfile->intern (intermediate_name);
34a68019
TT
10128 else
10129 name = canonical_name;
94af9270
KS
10130 }
10131 }
10132
10133 return name;
10134}
10135
0114d602
DJ
10136/* Return the fully qualified name of DIE, based on its DW_AT_name.
10137 If scope qualifiers are appropriate they will be added. The result
34a68019 10138 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10139 not have a name. NAME may either be from a previous call to
10140 dwarf2_name or NULL.
10141
9c37b5ae 10142 The output string will be canonicalized (if C++). */
0114d602
DJ
10143
10144static const char *
15d034d0 10145dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10146{
94af9270
KS
10147 return dwarf2_compute_name (name, die, cu, 0);
10148}
0114d602 10149
94af9270
KS
10150/* Construct a physname for the given DIE in CU. NAME may either be
10151 from a previous call to dwarf2_name or NULL. The result will be
10152 allocated on the objfile_objstack or NULL if the DIE does not have a
10153 name.
0114d602 10154
9c37b5ae 10155 The output string will be canonicalized (if C++). */
0114d602 10156
94af9270 10157static const char *
15d034d0 10158dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10159{
518817b3 10160 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10161 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10162 int need_copy = 1;
10163
10164 /* In this case dwarf2_compute_name is just a shortcut not building anything
10165 on its own. */
10166 if (!die_needs_namespace (die, cu))
10167 return dwarf2_compute_name (name, die, cu, 1);
10168
73b9be8b 10169 mangled = dw2_linkage_name (die, cu);
900e11f9 10170
e98c9e7c
TT
10171 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10172 See https://github.com/rust-lang/rust/issues/32925. */
10173 if (cu->language == language_rust && mangled != NULL
10174 && strchr (mangled, '{') != NULL)
10175 mangled = NULL;
10176
900e11f9
JK
10177 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10178 has computed. */
791afaa2 10179 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10180 if (mangled != NULL)
900e11f9 10181 {
900e11f9 10182
59cc4834
JB
10183 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10184 {
10185 /* Do nothing (do not demangle the symbol name). */
10186 }
10187 else if (cu->language == language_go)
a766d390 10188 {
5e2db402
TT
10189 /* This is a lie, but we already lie to the caller new_symbol.
10190 new_symbol assumes we return the mangled name.
a766d390 10191 This just undoes that lie until things are cleaned up. */
a766d390
DE
10192 }
10193 else
10194 {
0eb876f5
JB
10195 /* Use DMGL_RET_DROP for C++ template functions to suppress
10196 their return type. It is easier for GDB users to search
10197 for such functions as `name(params)' than `long name(params)'.
10198 In such case the minimal symbol names do not match the full
10199 symbol names but for template functions there is never a need
10200 to look up their definition from their declaration so
10201 the only disadvantage remains the minimal symbol variant
10202 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10203 demangled.reset (gdb_demangle (mangled,
10204 (DMGL_PARAMS | DMGL_ANSI
10205 | DMGL_RET_DROP)));
a766d390 10206 }
900e11f9 10207 if (demangled)
791afaa2 10208 canon = demangled.get ();
900e11f9
JK
10209 else
10210 {
10211 canon = mangled;
10212 need_copy = 0;
10213 }
10214 }
10215
10216 if (canon == NULL || check_physname)
10217 {
10218 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10219
10220 if (canon != NULL && strcmp (physname, canon) != 0)
10221 {
10222 /* It may not mean a bug in GDB. The compiler could also
10223 compute DW_AT_linkage_name incorrectly. But in such case
10224 GDB would need to be bug-to-bug compatible. */
10225
b98664d3 10226 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10227 "(from linkage <%s>) - DIE at %s [in module %s]"),
10228 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10229 objfile_name (objfile));
900e11f9
JK
10230
10231 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10232 is available here - over computed PHYSNAME. It is safer
10233 against both buggy GDB and buggy compilers. */
10234
10235 retval = canon;
10236 }
10237 else
10238 {
10239 retval = physname;
10240 need_copy = 0;
10241 }
10242 }
10243 else
10244 retval = canon;
10245
10246 if (need_copy)
be1e3d3e 10247 retval = objfile->intern (retval);
900e11f9 10248
900e11f9 10249 return retval;
0114d602
DJ
10250}
10251
74921315
KS
10252/* Inspect DIE in CU for a namespace alias. If one exists, record
10253 a new symbol for it.
10254
10255 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10256
10257static int
10258read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10259{
10260 struct attribute *attr;
10261
10262 /* If the die does not have a name, this is not a namespace
10263 alias. */
10264 attr = dwarf2_attr (die, DW_AT_name, cu);
10265 if (attr != NULL)
10266 {
10267 int num;
10268 struct die_info *d = die;
10269 struct dwarf2_cu *imported_cu = cu;
10270
10271 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10272 keep inspecting DIEs until we hit the underlying import. */
10273#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10274 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10275 {
10276 attr = dwarf2_attr (d, DW_AT_import, cu);
10277 if (attr == NULL)
10278 break;
10279
10280 d = follow_die_ref (d, attr, &imported_cu);
10281 if (d->tag != DW_TAG_imported_declaration)
10282 break;
10283 }
10284
10285 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10286 {
b98664d3 10287 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10288 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10289 return 0;
10290 }
10291
10292 if (attr != NULL)
10293 {
10294 struct type *type;
0826b30a 10295 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10296
9c541725 10297 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10298 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10299 {
10300 /* This declaration is a global namespace alias. Add
10301 a symbol for it whose type is the aliased namespace. */
10302 new_symbol (die, type, cu);
10303 return 1;
10304 }
10305 }
10306 }
10307
10308 return 0;
10309}
10310
22cee43f 10311/* Return the using directives repository (global or local?) to use in the
804d2729 10312 current context for CU.
22cee43f
PMR
10313
10314 For Ada, imported declarations can materialize renamings, which *may* be
10315 global. However it is impossible (for now?) in DWARF to distinguish
10316 "external" imported declarations and "static" ones. As all imported
10317 declarations seem to be static in all other languages, make them all CU-wide
10318 global only in Ada. */
10319
10320static struct using_direct **
804d2729 10321using_directives (struct dwarf2_cu *cu)
22cee43f 10322{
c24bdb02
KS
10323 if (cu->language == language_ada
10324 && cu->get_builder ()->outermost_context_p ())
10325 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10326 else
c24bdb02 10327 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10328}
10329
27aa8d6a
SW
10330/* Read the import statement specified by the given die and record it. */
10331
10332static void
10333read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10334{
518817b3 10335 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10336 struct attribute *import_attr;
32019081 10337 struct die_info *imported_die, *child_die;
de4affc9 10338 struct dwarf2_cu *imported_cu;
27aa8d6a 10339 const char *imported_name;
794684b6 10340 const char *imported_name_prefix;
13387711
SW
10341 const char *canonical_name;
10342 const char *import_alias;
10343 const char *imported_declaration = NULL;
794684b6 10344 const char *import_prefix;
eb1e02fd 10345 std::vector<const char *> excludes;
13387711 10346
27aa8d6a
SW
10347 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10348 if (import_attr == NULL)
10349 {
b98664d3 10350 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10351 dwarf_tag_name (die->tag));
10352 return;
10353 }
10354
de4affc9
CC
10355 imported_cu = cu;
10356 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10357 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10358 if (imported_name == NULL)
10359 {
10360 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10361
10362 The import in the following code:
10363 namespace A
10364 {
10365 typedef int B;
10366 }
10367
10368 int main ()
10369 {
10370 using A::B;
10371 B b;
10372 return b;
10373 }
10374
10375 ...
10376 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10377 <52> DW_AT_decl_file : 1
10378 <53> DW_AT_decl_line : 6
10379 <54> DW_AT_import : <0x75>
10380 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10381 <59> DW_AT_name : B
10382 <5b> DW_AT_decl_file : 1
10383 <5c> DW_AT_decl_line : 2
10384 <5d> DW_AT_type : <0x6e>
10385 ...
10386 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10387 <76> DW_AT_byte_size : 4
10388 <77> DW_AT_encoding : 5 (signed)
10389
10390 imports the wrong die ( 0x75 instead of 0x58 ).
10391 This case will be ignored until the gcc bug is fixed. */
10392 return;
10393 }
10394
82856980
SW
10395 /* Figure out the local name after import. */
10396 import_alias = dwarf2_name (die, cu);
27aa8d6a 10397
794684b6
SW
10398 /* Figure out where the statement is being imported to. */
10399 import_prefix = determine_prefix (die, cu);
10400
10401 /* Figure out what the scope of the imported die is and prepend it
10402 to the name of the imported die. */
de4affc9 10403 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10404
f55ee35c
JK
10405 if (imported_die->tag != DW_TAG_namespace
10406 && imported_die->tag != DW_TAG_module)
794684b6 10407 {
13387711
SW
10408 imported_declaration = imported_name;
10409 canonical_name = imported_name_prefix;
794684b6 10410 }
13387711 10411 else if (strlen (imported_name_prefix) > 0)
12aaed36 10412 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10413 imported_name_prefix,
10414 (cu->language == language_d ? "." : "::"),
10415 imported_name, (char *) NULL);
13387711
SW
10416 else
10417 canonical_name = imported_name;
794684b6 10418
32019081
JK
10419 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10420 for (child_die = die->child; child_die && child_die->tag;
436c571c 10421 child_die = child_die->sibling)
32019081
JK
10422 {
10423 /* DWARF-4: A Fortran use statement with a “rename list” may be
10424 represented by an imported module entry with an import attribute
10425 referring to the module and owned entries corresponding to those
10426 entities that are renamed as part of being imported. */
10427
10428 if (child_die->tag != DW_TAG_imported_declaration)
10429 {
b98664d3 10430 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10431 "- DIE at %s [in module %s]"),
10432 sect_offset_str (child_die->sect_off),
10433 objfile_name (objfile));
32019081
JK
10434 continue;
10435 }
10436
10437 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10438 if (import_attr == NULL)
10439 {
b98664d3 10440 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10441 dwarf_tag_name (child_die->tag));
10442 continue;
10443 }
10444
10445 imported_cu = cu;
10446 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10447 &imported_cu);
10448 imported_name = dwarf2_name (imported_die, imported_cu);
10449 if (imported_name == NULL)
10450 {
b98664d3 10451 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10452 "imported name - DIE at %s [in module %s]"),
10453 sect_offset_str (child_die->sect_off),
10454 objfile_name (objfile));
32019081
JK
10455 continue;
10456 }
10457
eb1e02fd 10458 excludes.push_back (imported_name);
32019081
JK
10459
10460 process_die (child_die, cu);
10461 }
10462
804d2729 10463 add_using_directive (using_directives (cu),
22cee43f
PMR
10464 import_prefix,
10465 canonical_name,
10466 import_alias,
10467 imported_declaration,
10468 excludes,
10469 0,
10470 &objfile->objfile_obstack);
27aa8d6a
SW
10471}
10472
5230b05a
WT
10473/* ICC<14 does not output the required DW_AT_declaration on incomplete
10474 types, but gives them a size of zero. Starting with version 14,
10475 ICC is compatible with GCC. */
10476
9068261f 10477static bool
5230b05a
WT
10478producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10479{
10480 if (!cu->checked_producer)
10481 check_producer (cu);
10482
10483 return cu->producer_is_icc_lt_14;
10484}
10485
eb77c9df
AB
10486/* ICC generates a DW_AT_type for C void functions. This was observed on
10487 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10488 which says that void functions should not have a DW_AT_type. */
10489
10490static bool
10491producer_is_icc (struct dwarf2_cu *cu)
10492{
10493 if (!cu->checked_producer)
10494 check_producer (cu);
10495
10496 return cu->producer_is_icc;
10497}
10498
1b80a9fa
JK
10499/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10500 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10501 this, it was first present in GCC release 4.3.0. */
10502
9068261f 10503static bool
1b80a9fa
JK
10504producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10505{
10506 if (!cu->checked_producer)
10507 check_producer (cu);
10508
10509 return cu->producer_is_gcc_lt_4_3;
10510}
10511
d721ba37
PA
10512static file_and_directory
10513find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10514{
d721ba37
PA
10515 file_and_directory res;
10516
9291a0cd
TT
10517 /* Find the filename. Do not use dwarf2_name here, since the filename
10518 is not a source language identifier. */
d721ba37
PA
10519 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10520 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10521
d721ba37
PA
10522 if (res.comp_dir == NULL
10523 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10524 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10525 {
d721ba37
PA
10526 res.comp_dir_storage = ldirname (res.name);
10527 if (!res.comp_dir_storage.empty ())
10528 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10529 }
d721ba37 10530 if (res.comp_dir != NULL)
9291a0cd
TT
10531 {
10532 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10533 directory, get rid of it. */
d721ba37 10534 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10535
d721ba37
PA
10536 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10537 res.comp_dir = cp + 1;
9291a0cd
TT
10538 }
10539
d721ba37
PA
10540 if (res.name == NULL)
10541 res.name = "<unknown>";
10542
10543 return res;
9291a0cd
TT
10544}
10545
f4dc4d17
DE
10546/* Handle DW_AT_stmt_list for a compilation unit.
10547 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10548 COMP_DIR is the compilation directory. LOWPC is passed to
10549 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10550
10551static void
10552handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10553 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10554{
518817b3
SM
10555 struct dwarf2_per_objfile *dwarf2_per_objfile
10556 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10557 struct attribute *attr;
527f3840
JK
10558 struct line_header line_header_local;
10559 hashval_t line_header_local_hash;
527f3840
JK
10560 void **slot;
10561 int decode_mapping;
2ab95328 10562
f4dc4d17
DE
10563 gdb_assert (! cu->per_cu->is_debug_types);
10564
2ab95328 10565 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10566 if (attr == NULL)
10567 return;
10568
9c541725 10569 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10570
10571 /* The line header hash table is only created if needed (it exists to
10572 prevent redundant reading of the line table for partial_units).
10573 If we're given a partial_unit, we'll need it. If we're given a
10574 compile_unit, then use the line header hash table if it's already
10575 created, but don't create one just yet. */
10576
10577 if (dwarf2_per_objfile->line_header_hash == NULL
10578 && die->tag == DW_TAG_partial_unit)
2ab95328 10579 {
527f3840 10580 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10581 .reset (htab_create_alloc (127, line_header_hash_voidp,
10582 line_header_eq_voidp,
10583 free_line_header_voidp,
10584 xcalloc, xfree));
527f3840 10585 }
2ab95328 10586
9c541725 10587 line_header_local.sect_off = line_offset;
527f3840
JK
10588 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10589 line_header_local_hash = line_header_hash (&line_header_local);
10590 if (dwarf2_per_objfile->line_header_hash != NULL)
10591 {
d15acc42 10592 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10593 &line_header_local,
10594 line_header_local_hash, NO_INSERT);
10595
10596 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10597 is not present in *SLOT (since if there is something in *SLOT then
10598 it will be for a partial_unit). */
10599 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10600 {
527f3840 10601 gdb_assert (*slot != NULL);
9a3c8263 10602 cu->line_header = (struct line_header *) *slot;
527f3840 10603 return;
dee91e82 10604 }
2ab95328 10605 }
527f3840
JK
10606
10607 /* dwarf_decode_line_header does not yet provide sufficient information.
10608 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10609 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10610 if (lh == NULL)
527f3840 10611 return;
4c8aa72d
PA
10612
10613 cu->line_header = lh.release ();
10614 cu->line_header_die_owner = die;
527f3840
JK
10615
10616 if (dwarf2_per_objfile->line_header_hash == NULL)
10617 slot = NULL;
10618 else
10619 {
d15acc42 10620 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10621 &line_header_local,
10622 line_header_local_hash, INSERT);
10623 gdb_assert (slot != NULL);
10624 }
10625 if (slot != NULL && *slot == NULL)
10626 {
10627 /* This newly decoded line number information unit will be owned
10628 by line_header_hash hash table. */
10629 *slot = cu->line_header;
4c8aa72d 10630 cu->line_header_die_owner = NULL;
527f3840
JK
10631 }
10632 else
10633 {
10634 /* We cannot free any current entry in (*slot) as that struct line_header
10635 may be already used by multiple CUs. Create only temporary decoded
10636 line_header for this CU - it may happen at most once for each line
10637 number information unit. And if we're not using line_header_hash
10638 then this is what we want as well. */
10639 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10640 }
10641 decode_mapping = (die->tag != DW_TAG_partial_unit);
10642 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10643 decode_mapping);
fff8551c 10644
2ab95328
TT
10645}
10646
95554aad 10647/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10648
c906108c 10649static void
e7c27a73 10650read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10651{
518817b3
SM
10652 struct dwarf2_per_objfile *dwarf2_per_objfile
10653 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10654 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10655 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10656 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10657 CORE_ADDR highpc = ((CORE_ADDR) 0);
10658 struct attribute *attr;
c906108c 10659 struct die_info *child_die;
e142c38c 10660 CORE_ADDR baseaddr;
6e70227d 10661
380618d6 10662 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10663 baseaddr = objfile->text_section_offset ();
c906108c 10664
fae299cd 10665 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10666
10667 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10668 from finish_block. */
2acceee2 10669 if (lowpc == ((CORE_ADDR) -1))
c906108c 10670 lowpc = highpc;
3e29f34a 10671 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10672
d721ba37 10673 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10674
f4b8a18d
KW
10675 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10676 standardised yet. As a workaround for the language detection we fall
10677 back to the DW_AT_producer string. */
10678 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10679 cu->language = language_opencl;
10680
3019eac3
DE
10681 /* Similar hack for Go. */
10682 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10683 set_cu_language (DW_LANG_Go, cu);
10684
c24bdb02 10685 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10686
10687 /* Decode line number information if present. We do this before
10688 processing child DIEs, so that the line header table is available
10689 for DW_AT_decl_file. */
d721ba37 10690 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10691
10692 /* Process all dies in compilation unit. */
10693 if (die->child != NULL)
10694 {
10695 child_die = die->child;
10696 while (child_die && child_die->tag)
10697 {
10698 process_die (child_die, cu);
436c571c 10699 child_die = child_die->sibling;
3019eac3
DE
10700 }
10701 }
10702
10703 /* Decode macro information, if present. Dwarf 2 macro information
10704 refers to information in the line number info statement program
10705 header, so we can only read it if we've read the header
10706 successfully. */
0af92d60
JK
10707 attr = dwarf2_attr (die, DW_AT_macros, cu);
10708 if (attr == NULL)
10709 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10710 if (attr && cu->line_header)
10711 {
10712 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10713 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10714
43f3e411 10715 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10716 }
10717 else
10718 {
10719 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10720 if (attr && cu->line_header)
10721 {
10722 unsigned int macro_offset = DW_UNSND (attr);
10723
43f3e411 10724 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10725 }
10726 }
3019eac3
DE
10727}
10728
c24bdb02
KS
10729void
10730dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10731{
f4dc4d17
DE
10732 struct type_unit_group *tu_group;
10733 int first_time;
3019eac3 10734 struct attribute *attr;
9c541725 10735 unsigned int i;
0186c6a7 10736 struct signatured_type *sig_type;
3019eac3 10737
f4dc4d17 10738 gdb_assert (per_cu->is_debug_types);
0186c6a7 10739 sig_type = (struct signatured_type *) per_cu;
3019eac3 10740
c24bdb02 10741 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10742
f4dc4d17 10743 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10744 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10745 if (sig_type->type_unit_group == NULL)
c24bdb02 10746 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10747 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10748
10749 /* If we've already processed this stmt_list there's no real need to
10750 do it again, we could fake it and just recreate the part we need
10751 (file name,index -> symtab mapping). If data shows this optimization
10752 is useful we can do it then. */
43f3e411 10753 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10754
10755 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10756 debug info. */
fff8551c 10757 line_header_up lh;
f4dc4d17 10758 if (attr != NULL)
3019eac3 10759 {
9c541725 10760 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10761 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10762 }
10763 if (lh == NULL)
10764 {
10765 if (first_time)
c24bdb02 10766 start_symtab ("", NULL, 0);
f4dc4d17
DE
10767 else
10768 {
10769 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10770 gdb_assert (m_builder == nullptr);
804d2729 10771 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10772 m_builder.reset (new struct buildsym_compunit
10773 (COMPUNIT_OBJFILE (cust), "",
10774 COMPUNIT_DIRNAME (cust),
10775 compunit_language (cust),
10776 0, cust));
f4dc4d17 10777 }
f4dc4d17 10778 return;
3019eac3
DE
10779 }
10780
c24bdb02
KS
10781 line_header = lh.release ();
10782 line_header_die_owner = die;
3019eac3 10783
f4dc4d17
DE
10784 if (first_time)
10785 {
c24bdb02 10786 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10787
1fd60fc0
DE
10788 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10789 still initializing it, and our caller (a few levels up)
10790 process_full_type_unit still needs to know if this is the first
10791 time. */
10792
4ac93832
TT
10793 tu_group->symtabs
10794 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10795 struct symtab *, line_header->file_names_size ());
3019eac3 10796
7ba99d21
AT
10797 auto &file_names = line_header->file_names ();
10798 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10799 {
7ba99d21 10800 file_entry &fe = file_names[i];
c24bdb02
KS
10801 dwarf2_start_subfile (this, fe.name,
10802 fe.include_dir (line_header));
10803 buildsym_compunit *b = get_builder ();
10804 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10805 {
4c8aa72d
PA
10806 /* NOTE: start_subfile will recognize when it's been
10807 passed a file it has already seen. So we can't
10808 assume there's a simple mapping from
10809 cu->line_header->file_names to subfiles, plus
10810 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10811 b->get_current_subfile ()->symtab
10812 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10813 }
10814
c24bdb02 10815 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10816 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10817 }
10818 }
10819 else
3019eac3 10820 {
c24bdb02 10821 gdb_assert (m_builder == nullptr);
804d2729 10822 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10823 m_builder.reset (new struct buildsym_compunit
10824 (COMPUNIT_OBJFILE (cust), "",
10825 COMPUNIT_DIRNAME (cust),
10826 compunit_language (cust),
10827 0, cust));
f4dc4d17 10828
7ba99d21
AT
10829 auto &file_names = line_header->file_names ();
10830 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10831 {
7ba99d21 10832 file_entry &fe = file_names[i];
4c8aa72d 10833 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10834 }
3019eac3
DE
10835 }
10836
f4dc4d17
DE
10837 /* The main symtab is allocated last. Type units don't have DW_AT_name
10838 so they don't have a "real" (so to speak) symtab anyway.
10839 There is later code that will assign the main symtab to all symbols
10840 that don't have one. We need to handle the case of a symbol with a
10841 missing symtab (DW_AT_decl_file) anyway. */
10842}
3019eac3 10843
f4dc4d17
DE
10844/* Process DW_TAG_type_unit.
10845 For TUs we want to skip the first top level sibling if it's not the
10846 actual type being defined by this TU. In this case the first top
10847 level sibling is there to provide context only. */
3019eac3 10848
f4dc4d17
DE
10849static void
10850read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10851{
10852 struct die_info *child_die;
3019eac3 10853
f4dc4d17
DE
10854 prepare_one_comp_unit (cu, die, language_minimal);
10855
10856 /* Initialize (or reinitialize) the machinery for building symtabs.
10857 We do this before processing child DIEs, so that the line header table
10858 is available for DW_AT_decl_file. */
c24bdb02 10859 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10860
10861 if (die->child != NULL)
10862 {
10863 child_die = die->child;
10864 while (child_die && child_die->tag)
10865 {
10866 process_die (child_die, cu);
436c571c 10867 child_die = child_die->sibling;
f4dc4d17
DE
10868 }
10869 }
3019eac3
DE
10870}
10871\f
80626a55
DE
10872/* DWO/DWP files.
10873
10874 http://gcc.gnu.org/wiki/DebugFission
10875 http://gcc.gnu.org/wiki/DebugFissionDWP
10876
10877 To simplify handling of both DWO files ("object" files with the DWARF info)
10878 and DWP files (a file with the DWOs packaged up into one file), we treat
10879 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10880
10881static hashval_t
10882hash_dwo_file (const void *item)
10883{
9a3c8263 10884 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10885 hashval_t hash;
3019eac3 10886
a2ce51a0
DE
10887 hash = htab_hash_string (dwo_file->dwo_name);
10888 if (dwo_file->comp_dir != NULL)
10889 hash += htab_hash_string (dwo_file->comp_dir);
10890 return hash;
3019eac3
DE
10891}
10892
10893static int
10894eq_dwo_file (const void *item_lhs, const void *item_rhs)
10895{
9a3c8263
SM
10896 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10897 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10898
a2ce51a0
DE
10899 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10900 return 0;
10901 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10902 return lhs->comp_dir == rhs->comp_dir;
10903 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10904}
10905
10906/* Allocate a hash table for DWO files. */
10907
51ac9db5 10908static htab_up
298e9637 10909allocate_dwo_file_hash_table ()
3019eac3 10910{
51ac9db5
SM
10911 auto delete_dwo_file = [] (void *item)
10912 {
10913 struct dwo_file *dwo_file = (struct dwo_file *) item;
10914
10915 delete dwo_file;
10916 };
10917
bc68fb19
TT
10918 return htab_up (htab_create_alloc (41,
10919 hash_dwo_file,
10920 eq_dwo_file,
10921 delete_dwo_file,
10922 xcalloc, xfree));
3019eac3
DE
10923}
10924
80626a55
DE
10925/* Lookup DWO file DWO_NAME. */
10926
10927static void **
ed2dc618
SM
10928lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10929 const char *dwo_name,
10930 const char *comp_dir)
80626a55
DE
10931{
10932 struct dwo_file find_entry;
10933 void **slot;
10934
10935 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 10936 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 10937
0ac5b59e
DE
10938 find_entry.dwo_name = dwo_name;
10939 find_entry.comp_dir = comp_dir;
51ac9db5
SM
10940 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10941 INSERT);
80626a55
DE
10942
10943 return slot;
10944}
10945
3019eac3
DE
10946static hashval_t
10947hash_dwo_unit (const void *item)
10948{
9a3c8263 10949 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10950
10951 /* This drops the top 32 bits of the id, but is ok for a hash. */
10952 return dwo_unit->signature;
10953}
10954
10955static int
10956eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10957{
9a3c8263
SM
10958 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10959 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10960
10961 /* The signature is assumed to be unique within the DWO file.
10962 So while object file CU dwo_id's always have the value zero,
10963 that's OK, assuming each object file DWO file has only one CU,
10964 and that's the rule for now. */
10965 return lhs->signature == rhs->signature;
10966}
10967
10968/* Allocate a hash table for DWO CUs,TUs.
10969 There is one of these tables for each of CUs,TUs for each DWO file. */
10970
b0b6a987 10971static htab_up
298e9637 10972allocate_dwo_unit_table ()
3019eac3
DE
10973{
10974 /* Start out with a pretty small number.
10975 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
10976 return htab_up (htab_create_alloc (3,
10977 hash_dwo_unit,
10978 eq_dwo_unit,
10979 NULL, xcalloc, xfree));
3019eac3
DE
10980}
10981
19c3d4c9 10982/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10983
10984static void
19c3d4c9
DE
10985create_dwo_cu_reader (const struct die_reader_specs *reader,
10986 const gdb_byte *info_ptr,
10987 struct die_info *comp_unit_die,
c0ab21c2
TT
10988 struct dwo_file *dwo_file,
10989 struct dwo_unit *dwo_unit)
3019eac3
DE
10990{
10991 struct dwarf2_cu *cu = reader->cu;
9c541725 10992 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10993 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 10994
a084a2a6
AT
10995 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10996 if (!signature.has_value ())
3019eac3 10997 {
b98664d3 10998 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 10999 " its dwo_id [in module %s]"),
9d8780f0 11000 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11001 return;
11002 }
11003
3019eac3 11004 dwo_unit->dwo_file = dwo_file;
a084a2a6 11005 dwo_unit->signature = *signature;
8a0459fd 11006 dwo_unit->section = section;
9c541725 11007 dwo_unit->sect_off = sect_off;
3019eac3
DE
11008 dwo_unit->length = cu->per_cu->length;
11009
b4f54984 11010 if (dwarf_read_debug)
9d8780f0
SM
11011 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11012 sect_offset_str (sect_off),
9c541725 11013 hex_string (dwo_unit->signature));
3019eac3
DE
11014}
11015
33c5cd75 11016/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11017 Note: This function processes DWO files only, not DWP files. */
3019eac3 11018
33c5cd75 11019static void
ed2dc618 11020create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11021 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11022 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11023{
11024 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11025 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11026
96b79293 11027 section.read (objfile);
33c5cd75 11028 info_ptr = section.buffer;
3019eac3
DE
11029
11030 if (info_ptr == NULL)
33c5cd75 11031 return;
3019eac3 11032
b4f54984 11033 if (dwarf_read_debug)
19c3d4c9
DE
11034 {
11035 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11036 section.get_name (),
11037 section.get_file_name ());
19c3d4c9 11038 }
3019eac3 11039
33c5cd75 11040 end_ptr = info_ptr + section.size;
3019eac3
DE
11041 while (info_ptr < end_ptr)
11042 {
11043 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11044 struct dwo_unit read_unit {};
33c5cd75
DB
11045 struct dwo_unit *dwo_unit;
11046 void **slot;
11047 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11048
11049 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11050 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11051 per_cu.is_debug_types = 0;
33c5cd75
DB
11052 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11053 per_cu.section = &section;
11054
c0ab21c2
TT
11055 cutu_reader reader (&per_cu, cu, &dwo_file);
11056 if (!reader.dummy_p)
11057 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11058 &dwo_file, &read_unit);
33c5cd75
DB
11059 info_ptr += per_cu.length;
11060
11061 // If the unit could not be parsed, skip it.
c0ab21c2 11062 if (read_unit.dwo_file == NULL)
33c5cd75 11063 continue;
3019eac3 11064
33c5cd75 11065 if (cus_htab == NULL)
298e9637 11066 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11067
33c5cd75 11068 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11069 *dwo_unit = read_unit;
b0b6a987 11070 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11071 gdb_assert (slot != NULL);
11072 if (*slot != NULL)
19c3d4c9 11073 {
33c5cd75
DB
11074 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11075 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11076
b98664d3 11077 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11078 " the entry at offset %s, signature %s"),
11079 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11080 hex_string (dwo_unit->signature));
19c3d4c9 11081 }
33c5cd75 11082 *slot = (void *)dwo_unit;
3019eac3 11083 }
3019eac3
DE
11084}
11085
80626a55
DE
11086/* DWP file .debug_{cu,tu}_index section format:
11087 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11088
d2415c6c
DE
11089 DWP Version 1:
11090
80626a55
DE
11091 Both index sections have the same format, and serve to map a 64-bit
11092 signature to a set of section numbers. Each section begins with a header,
11093 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11094 indexes, and a pool of 32-bit section numbers. The index sections will be
11095 aligned at 8-byte boundaries in the file.
11096
d2415c6c
DE
11097 The index section header consists of:
11098
11099 V, 32 bit version number
11100 -, 32 bits unused
11101 N, 32 bit number of compilation units or type units in the index
11102 M, 32 bit number of slots in the hash table
80626a55 11103
d2415c6c 11104 Numbers are recorded using the byte order of the application binary.
80626a55 11105
d2415c6c
DE
11106 The hash table begins at offset 16 in the section, and consists of an array
11107 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11108 order of the application binary). Unused slots in the hash table are 0.
11109 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11110
d2415c6c
DE
11111 The parallel table begins immediately after the hash table
11112 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11113 array of 32-bit indexes (using the byte order of the application binary),
11114 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11115 table contains a 32-bit index into the pool of section numbers. For unused
11116 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11117
73869dc2
DE
11118 The pool of section numbers begins immediately following the hash table
11119 (at offset 16 + 12 * M from the beginning of the section). The pool of
11120 section numbers consists of an array of 32-bit words (using the byte order
11121 of the application binary). Each item in the array is indexed starting
11122 from 0. The hash table entry provides the index of the first section
11123 number in the set. Additional section numbers in the set follow, and the
11124 set is terminated by a 0 entry (section number 0 is not used in ELF).
11125
11126 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11127 section must be the first entry in the set, and the .debug_abbrev.dwo must
11128 be the second entry. Other members of the set may follow in any order.
11129
11130 ---
11131
11132 DWP Version 2:
11133
11134 DWP Version 2 combines all the .debug_info, etc. sections into one,
11135 and the entries in the index tables are now offsets into these sections.
11136 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11137 section.
11138
11139 Index Section Contents:
11140 Header
11141 Hash Table of Signatures dwp_hash_table.hash_table
11142 Parallel Table of Indices dwp_hash_table.unit_table
11143 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11144 Table of Section Sizes dwp_hash_table.v2.sizes
11145
11146 The index section header consists of:
11147
11148 V, 32 bit version number
11149 L, 32 bit number of columns in the table of section offsets
11150 N, 32 bit number of compilation units or type units in the index
11151 M, 32 bit number of slots in the hash table
11152
11153 Numbers are recorded using the byte order of the application binary.
11154
11155 The hash table has the same format as version 1.
11156 The parallel table of indices has the same format as version 1,
11157 except that the entries are origin-1 indices into the table of sections
11158 offsets and the table of section sizes.
11159
11160 The table of offsets begins immediately following the parallel table
11161 (at offset 16 + 12 * M from the beginning of the section). The table is
11162 a two-dimensional array of 32-bit words (using the byte order of the
11163 application binary), with L columns and N+1 rows, in row-major order.
11164 Each row in the array is indexed starting from 0. The first row provides
11165 a key to the remaining rows: each column in this row provides an identifier
11166 for a debug section, and the offsets in the same column of subsequent rows
11167 refer to that section. The section identifiers are:
11168
11169 DW_SECT_INFO 1 .debug_info.dwo
11170 DW_SECT_TYPES 2 .debug_types.dwo
11171 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11172 DW_SECT_LINE 4 .debug_line.dwo
11173 DW_SECT_LOC 5 .debug_loc.dwo
11174 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11175 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11176 DW_SECT_MACRO 8 .debug_macro.dwo
11177
11178 The offsets provided by the CU and TU index sections are the base offsets
11179 for the contributions made by each CU or TU to the corresponding section
11180 in the package file. Each CU and TU header contains an abbrev_offset
11181 field, used to find the abbreviations table for that CU or TU within the
11182 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11183 be interpreted as relative to the base offset given in the index section.
11184 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11185 should be interpreted as relative to the base offset for .debug_line.dwo,
11186 and offsets into other debug sections obtained from DWARF attributes should
11187 also be interpreted as relative to the corresponding base offset.
11188
11189 The table of sizes begins immediately following the table of offsets.
11190 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11191 with L columns and N rows, in row-major order. Each row in the array is
11192 indexed starting from 1 (row 0 is shared by the two tables).
11193
11194 ---
11195
11196 Hash table lookup is handled the same in version 1 and 2:
11197
11198 We assume that N and M will not exceed 2^32 - 1.
11199 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11200
d2415c6c
DE
11201 Given a 64-bit compilation unit signature or a type signature S, an entry
11202 in the hash table is located as follows:
80626a55 11203
d2415c6c
DE
11204 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11205 the low-order k bits all set to 1.
80626a55 11206
d2415c6c 11207 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11208
d2415c6c
DE
11209 3) If the hash table entry at index H matches the signature, use that
11210 entry. If the hash table entry at index H is unused (all zeroes),
11211 terminate the search: the signature is not present in the table.
80626a55 11212
d2415c6c 11213 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11214
d2415c6c 11215 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11216 to stop at an unused slot or find the match. */
80626a55
DE
11217
11218/* Create a hash table to map DWO IDs to their CU/TU entry in
11219 .debug_{info,types}.dwo in DWP_FILE.
11220 Returns NULL if there isn't one.
11221 Note: This function processes DWP files only, not DWO files. */
11222
11223static struct dwp_hash_table *
ed2dc618
SM
11224create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11225 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11226{
11227 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11228 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11229 const gdb_byte *index_ptr, *index_end;
80626a55 11230 struct dwarf2_section_info *index;
73869dc2 11231 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11232 struct dwp_hash_table *htab;
11233
11234 if (is_debug_types)
11235 index = &dwp_file->sections.tu_index;
11236 else
11237 index = &dwp_file->sections.cu_index;
11238
96b79293 11239 if (index->empty ())
80626a55 11240 return NULL;
96b79293 11241 index->read (objfile);
80626a55
DE
11242
11243 index_ptr = index->buffer;
11244 index_end = index_ptr + index->size;
11245
11246 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11247 index_ptr += 4;
11248 if (version == 2)
11249 nr_columns = read_4_bytes (dbfd, index_ptr);
11250 else
11251 nr_columns = 0;
11252 index_ptr += 4;
80626a55
DE
11253 nr_units = read_4_bytes (dbfd, index_ptr);
11254 index_ptr += 4;
11255 nr_slots = read_4_bytes (dbfd, index_ptr);
11256 index_ptr += 4;
11257
73869dc2 11258 if (version != 1 && version != 2)
80626a55 11259 {
21aa081e 11260 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11261 " [in module %s]"),
21aa081e 11262 pulongest (version), dwp_file->name);
80626a55
DE
11263 }
11264 if (nr_slots != (nr_slots & -nr_slots))
11265 {
21aa081e 11266 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11267 " is not power of 2 [in module %s]"),
21aa081e 11268 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11269 }
11270
11271 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11272 htab->version = version;
11273 htab->nr_columns = nr_columns;
80626a55
DE
11274 htab->nr_units = nr_units;
11275 htab->nr_slots = nr_slots;
11276 htab->hash_table = index_ptr;
11277 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11278
11279 /* Exit early if the table is empty. */
11280 if (nr_slots == 0 || nr_units == 0
11281 || (version == 2 && nr_columns == 0))
11282 {
11283 /* All must be zero. */
11284 if (nr_slots != 0 || nr_units != 0
11285 || (version == 2 && nr_columns != 0))
11286 {
b98664d3 11287 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11288 " all zero [in modules %s]"),
11289 dwp_file->name);
11290 }
11291 return htab;
11292 }
11293
11294 if (version == 1)
11295 {
11296 htab->section_pool.v1.indices =
11297 htab->unit_table + sizeof (uint32_t) * nr_slots;
11298 /* It's harder to decide whether the section is too small in v1.
11299 V1 is deprecated anyway so we punt. */
11300 }
11301 else
11302 {
11303 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11304 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11305 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11306 /* Reverse map for error checking. */
11307 int ids_seen[DW_SECT_MAX + 1];
11308 int i;
11309
11310 if (nr_columns < 2)
11311 {
11312 error (_("Dwarf Error: bad DWP hash table, too few columns"
11313 " in section table [in module %s]"),
11314 dwp_file->name);
11315 }
11316 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11317 {
11318 error (_("Dwarf Error: bad DWP hash table, too many columns"
11319 " in section table [in module %s]"),
11320 dwp_file->name);
11321 }
04fd5eed
GB
11322 memset (ids, 255, sizeof_ids);
11323 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11324 for (i = 0; i < nr_columns; ++i)
11325 {
11326 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11327
11328 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11329 {
11330 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11331 " in section table [in module %s]"),
11332 id, dwp_file->name);
11333 }
11334 if (ids_seen[id] != -1)
11335 {
11336 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11337 " id %d in section table [in module %s]"),
11338 id, dwp_file->name);
11339 }
11340 ids_seen[id] = i;
11341 ids[i] = id;
11342 }
11343 /* Must have exactly one info or types section. */
11344 if (((ids_seen[DW_SECT_INFO] != -1)
11345 + (ids_seen[DW_SECT_TYPES] != -1))
11346 != 1)
11347 {
11348 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11349 " DWO info/types section [in module %s]"),
11350 dwp_file->name);
11351 }
11352 /* Must have an abbrev section. */
11353 if (ids_seen[DW_SECT_ABBREV] == -1)
11354 {
11355 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11356 " section [in module %s]"),
11357 dwp_file->name);
11358 }
11359 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11360 htab->section_pool.v2.sizes =
11361 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11362 * nr_units * nr_columns);
11363 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11364 * nr_units * nr_columns))
11365 > index_end)
11366 {
11367 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11368 " [in module %s]"),
11369 dwp_file->name);
11370 }
11371 }
80626a55
DE
11372
11373 return htab;
11374}
11375
11376/* Update SECTIONS with the data from SECTP.
11377
11378 This function is like the other "locate" section routines that are
11379 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11380 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11381
11382 The result is non-zero for success, or zero if an error was found. */
11383
11384static int
73869dc2
DE
11385locate_v1_virtual_dwo_sections (asection *sectp,
11386 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11387{
11388 const struct dwop_section_names *names = &dwop_section_names;
11389
11390 if (section_is_p (sectp->name, &names->abbrev_dwo))
11391 {
11392 /* There can be only one. */
049412e3 11393 if (sections->abbrev.s.section != NULL)
80626a55 11394 return 0;
049412e3 11395 sections->abbrev.s.section = sectp;
fd361982 11396 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11397 }
11398 else if (section_is_p (sectp->name, &names->info_dwo)
11399 || section_is_p (sectp->name, &names->types_dwo))
11400 {
11401 /* There can be only one. */
049412e3 11402 if (sections->info_or_types.s.section != NULL)
80626a55 11403 return 0;
049412e3 11404 sections->info_or_types.s.section = sectp;
fd361982 11405 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11406 }
11407 else if (section_is_p (sectp->name, &names->line_dwo))
11408 {
11409 /* There can be only one. */
049412e3 11410 if (sections->line.s.section != NULL)
80626a55 11411 return 0;
049412e3 11412 sections->line.s.section = sectp;
fd361982 11413 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11414 }
11415 else if (section_is_p (sectp->name, &names->loc_dwo))
11416 {
11417 /* There can be only one. */
049412e3 11418 if (sections->loc.s.section != NULL)
80626a55 11419 return 0;
049412e3 11420 sections->loc.s.section = sectp;
fd361982 11421 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11422 }
11423 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11424 {
11425 /* There can be only one. */
049412e3 11426 if (sections->macinfo.s.section != NULL)
80626a55 11427 return 0;
049412e3 11428 sections->macinfo.s.section = sectp;
fd361982 11429 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11430 }
11431 else if (section_is_p (sectp->name, &names->macro_dwo))
11432 {
11433 /* There can be only one. */
049412e3 11434 if (sections->macro.s.section != NULL)
80626a55 11435 return 0;
049412e3 11436 sections->macro.s.section = sectp;
fd361982 11437 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11438 }
11439 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11440 {
11441 /* There can be only one. */
049412e3 11442 if (sections->str_offsets.s.section != NULL)
80626a55 11443 return 0;
049412e3 11444 sections->str_offsets.s.section = sectp;
fd361982 11445 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11446 }
11447 else
11448 {
11449 /* No other kind of section is valid. */
11450 return 0;
11451 }
11452
11453 return 1;
11454}
11455
73869dc2
DE
11456/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11457 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11458 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11459 This is for DWP version 1 files. */
80626a55
DE
11460
11461static struct dwo_unit *
ed2dc618
SM
11462create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11463 struct dwp_file *dwp_file,
73869dc2
DE
11464 uint32_t unit_index,
11465 const char *comp_dir,
11466 ULONGEST signature, int is_debug_types)
80626a55
DE
11467{
11468 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11469 const struct dwp_hash_table *dwp_htab =
11470 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11471 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11472 const char *kind = is_debug_types ? "TU" : "CU";
11473 struct dwo_file *dwo_file;
11474 struct dwo_unit *dwo_unit;
73869dc2 11475 struct virtual_v1_dwo_sections sections;
80626a55 11476 void **dwo_file_slot;
80626a55
DE
11477 int i;
11478
73869dc2
DE
11479 gdb_assert (dwp_file->version == 1);
11480
b4f54984 11481 if (dwarf_read_debug)
80626a55 11482 {
73869dc2 11483 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11484 kind,
73869dc2 11485 pulongest (unit_index), hex_string (signature),
80626a55
DE
11486 dwp_file->name);
11487 }
11488
19ac8c2e 11489 /* Fetch the sections of this DWO unit.
80626a55
DE
11490 Put a limit on the number of sections we look for so that bad data
11491 doesn't cause us to loop forever. */
11492
73869dc2 11493#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11494 (1 /* .debug_info or .debug_types */ \
11495 + 1 /* .debug_abbrev */ \
11496 + 1 /* .debug_line */ \
11497 + 1 /* .debug_loc */ \
11498 + 1 /* .debug_str_offsets */ \
19ac8c2e 11499 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11500 + 1 /* trailing zero */)
11501
11502 memset (&sections, 0, sizeof (sections));
80626a55 11503
73869dc2 11504 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11505 {
11506 asection *sectp;
11507 uint32_t section_nr =
11508 read_4_bytes (dbfd,
73869dc2
DE
11509 dwp_htab->section_pool.v1.indices
11510 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11511
11512 if (section_nr == 0)
11513 break;
11514 if (section_nr >= dwp_file->num_sections)
11515 {
11516 error (_("Dwarf Error: bad DWP hash table, section number too large"
11517 " [in module %s]"),
11518 dwp_file->name);
11519 }
11520
11521 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11522 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11523 {
11524 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11525 " [in module %s]"),
11526 dwp_file->name);
11527 }
11528 }
11529
11530 if (i < 2
96b79293
TT
11531 || sections.info_or_types.empty ()
11532 || sections.abbrev.empty ())
80626a55
DE
11533 {
11534 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11535 " [in module %s]"),
11536 dwp_file->name);
11537 }
73869dc2 11538 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11539 {
11540 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11541 " [in module %s]"),
11542 dwp_file->name);
11543 }
11544
11545 /* It's easier for the rest of the code if we fake a struct dwo_file and
11546 have dwo_unit "live" in that. At least for now.
11547
11548 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11549 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11550 file, we can combine them back into a virtual DWO file to save space
11551 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11552 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11553
791afaa2
TT
11554 std::string virtual_dwo_name =
11555 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11556 sections.abbrev.get_id (),
11557 sections.line.get_id (),
11558 sections.loc.get_id (),
11559 sections.str_offsets.get_id ());
80626a55 11560 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11561 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11562 virtual_dwo_name.c_str (),
11563 comp_dir);
80626a55
DE
11564 /* Create one if necessary. */
11565 if (*dwo_file_slot == NULL)
11566 {
b4f54984 11567 if (dwarf_read_debug)
80626a55
DE
11568 {
11569 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11570 virtual_dwo_name.c_str ());
80626a55 11571 }
51ac9db5 11572 dwo_file = new struct dwo_file;
be1e3d3e 11573 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11574 dwo_file->comp_dir = comp_dir;
80626a55
DE
11575 dwo_file->sections.abbrev = sections.abbrev;
11576 dwo_file->sections.line = sections.line;
11577 dwo_file->sections.loc = sections.loc;
11578 dwo_file->sections.macinfo = sections.macinfo;
11579 dwo_file->sections.macro = sections.macro;
11580 dwo_file->sections.str_offsets = sections.str_offsets;
11581 /* The "str" section is global to the entire DWP file. */
11582 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11583 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11584 there's no need to record it in dwo_file.
11585 Also, we can't simply record type sections in dwo_file because
11586 we record a pointer into the vector in dwo_unit. As we collect more
11587 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11588 for it, invalidating all copies of pointers into the previous
11589 contents. */
80626a55
DE
11590 *dwo_file_slot = dwo_file;
11591 }
11592 else
11593 {
b4f54984 11594 if (dwarf_read_debug)
80626a55
DE
11595 {
11596 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11597 virtual_dwo_name.c_str ());
80626a55 11598 }
9a3c8263 11599 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11600 }
80626a55
DE
11601
11602 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11603 dwo_unit->dwo_file = dwo_file;
11604 dwo_unit->signature = signature;
8d749320
SM
11605 dwo_unit->section =
11606 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11607 *dwo_unit->section = sections.info_or_types;
57d63ce2 11608 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11609
11610 return dwo_unit;
11611}
11612
73869dc2
DE
11613/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11614 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11615 piece within that section used by a TU/CU, return a virtual section
11616 of just that piece. */
11617
11618static struct dwarf2_section_info
ed2dc618
SM
11619create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11620 struct dwarf2_section_info *section,
73869dc2
DE
11621 bfd_size_type offset, bfd_size_type size)
11622{
11623 struct dwarf2_section_info result;
11624 asection *sectp;
11625
11626 gdb_assert (section != NULL);
11627 gdb_assert (!section->is_virtual);
11628
11629 memset (&result, 0, sizeof (result));
11630 result.s.containing_section = section;
dc4ccb6f 11631 result.is_virtual = true;
73869dc2
DE
11632
11633 if (size == 0)
11634 return result;
11635
96b79293 11636 sectp = section->get_bfd_section ();
73869dc2
DE
11637
11638 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11639 bounds of the real section. This is a pretty-rare event, so just
11640 flag an error (easier) instead of a warning and trying to cope. */
11641 if (sectp == NULL
fd361982 11642 || offset + size > bfd_section_size (sectp))
73869dc2 11643 {
73869dc2
DE
11644 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11645 " in section %s [in module %s]"),
fd361982 11646 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11647 objfile_name (dwarf2_per_objfile->objfile));
11648 }
11649
11650 result.virtual_offset = offset;
11651 result.size = size;
11652 return result;
11653}
11654
11655/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11656 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11657 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11658 This is for DWP version 2 files. */
11659
11660static struct dwo_unit *
ed2dc618
SM
11661create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11662 struct dwp_file *dwp_file,
73869dc2
DE
11663 uint32_t unit_index,
11664 const char *comp_dir,
11665 ULONGEST signature, int is_debug_types)
11666{
11667 struct objfile *objfile = dwarf2_per_objfile->objfile;
11668 const struct dwp_hash_table *dwp_htab =
11669 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11670 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11671 const char *kind = is_debug_types ? "TU" : "CU";
11672 struct dwo_file *dwo_file;
11673 struct dwo_unit *dwo_unit;
11674 struct virtual_v2_dwo_sections sections;
11675 void **dwo_file_slot;
73869dc2
DE
11676 int i;
11677
11678 gdb_assert (dwp_file->version == 2);
11679
b4f54984 11680 if (dwarf_read_debug)
73869dc2
DE
11681 {
11682 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11683 kind,
11684 pulongest (unit_index), hex_string (signature),
11685 dwp_file->name);
11686 }
11687
11688 /* Fetch the section offsets of this DWO unit. */
11689
11690 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11691
11692 for (i = 0; i < dwp_htab->nr_columns; ++i)
11693 {
11694 uint32_t offset = read_4_bytes (dbfd,
11695 dwp_htab->section_pool.v2.offsets
11696 + (((unit_index - 1) * dwp_htab->nr_columns
11697 + i)
11698 * sizeof (uint32_t)));
11699 uint32_t size = read_4_bytes (dbfd,
11700 dwp_htab->section_pool.v2.sizes
11701 + (((unit_index - 1) * dwp_htab->nr_columns
11702 + i)
11703 * sizeof (uint32_t)));
11704
11705 switch (dwp_htab->section_pool.v2.section_ids[i])
11706 {
11707 case DW_SECT_INFO:
11708 case DW_SECT_TYPES:
11709 sections.info_or_types_offset = offset;
11710 sections.info_or_types_size = size;
11711 break;
11712 case DW_SECT_ABBREV:
11713 sections.abbrev_offset = offset;
11714 sections.abbrev_size = size;
11715 break;
11716 case DW_SECT_LINE:
11717 sections.line_offset = offset;
11718 sections.line_size = size;
11719 break;
11720 case DW_SECT_LOC:
11721 sections.loc_offset = offset;
11722 sections.loc_size = size;
11723 break;
11724 case DW_SECT_STR_OFFSETS:
11725 sections.str_offsets_offset = offset;
11726 sections.str_offsets_size = size;
11727 break;
11728 case DW_SECT_MACINFO:
11729 sections.macinfo_offset = offset;
11730 sections.macinfo_size = size;
11731 break;
11732 case DW_SECT_MACRO:
11733 sections.macro_offset = offset;
11734 sections.macro_size = size;
11735 break;
11736 }
11737 }
11738
11739 /* It's easier for the rest of the code if we fake a struct dwo_file and
11740 have dwo_unit "live" in that. At least for now.
11741
11742 The DWP file can be made up of a random collection of CUs and TUs.
11743 However, for each CU + set of TUs that came from the same original DWO
11744 file, we can combine them back into a virtual DWO file to save space
11745 (fewer struct dwo_file objects to allocate). Remember that for really
11746 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11747
791afaa2
TT
11748 std::string virtual_dwo_name =
11749 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11750 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11751 (long) (sections.line_size ? sections.line_offset : 0),
11752 (long) (sections.loc_size ? sections.loc_offset : 0),
11753 (long) (sections.str_offsets_size
11754 ? sections.str_offsets_offset : 0));
73869dc2 11755 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11756 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11757 virtual_dwo_name.c_str (),
11758 comp_dir);
73869dc2
DE
11759 /* Create one if necessary. */
11760 if (*dwo_file_slot == NULL)
11761 {
b4f54984 11762 if (dwarf_read_debug)
73869dc2
DE
11763 {
11764 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11765 virtual_dwo_name.c_str ());
73869dc2 11766 }
51ac9db5 11767 dwo_file = new struct dwo_file;
be1e3d3e 11768 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11769 dwo_file->comp_dir = comp_dir;
11770 dwo_file->sections.abbrev =
ed2dc618 11771 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11772 sections.abbrev_offset, sections.abbrev_size);
11773 dwo_file->sections.line =
ed2dc618 11774 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11775 sections.line_offset, sections.line_size);
11776 dwo_file->sections.loc =
ed2dc618 11777 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11778 sections.loc_offset, sections.loc_size);
11779 dwo_file->sections.macinfo =
ed2dc618 11780 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11781 sections.macinfo_offset, sections.macinfo_size);
11782 dwo_file->sections.macro =
ed2dc618 11783 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11784 sections.macro_offset, sections.macro_size);
11785 dwo_file->sections.str_offsets =
ed2dc618
SM
11786 create_dwp_v2_section (dwarf2_per_objfile,
11787 &dwp_file->sections.str_offsets,
73869dc2
DE
11788 sections.str_offsets_offset,
11789 sections.str_offsets_size);
11790 /* The "str" section is global to the entire DWP file. */
11791 dwo_file->sections.str = dwp_file->sections.str;
11792 /* The info or types section is assigned below to dwo_unit,
11793 there's no need to record it in dwo_file.
11794 Also, we can't simply record type sections in dwo_file because
11795 we record a pointer into the vector in dwo_unit. As we collect more
11796 types we'll grow the vector and eventually have to reallocate space
11797 for it, invalidating all copies of pointers into the previous
11798 contents. */
11799 *dwo_file_slot = dwo_file;
11800 }
11801 else
11802 {
b4f54984 11803 if (dwarf_read_debug)
73869dc2
DE
11804 {
11805 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11806 virtual_dwo_name.c_str ());
73869dc2 11807 }
9a3c8263 11808 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11809 }
73869dc2
DE
11810
11811 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11812 dwo_unit->dwo_file = dwo_file;
11813 dwo_unit->signature = signature;
8d749320
SM
11814 dwo_unit->section =
11815 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11816 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11817 is_debug_types
73869dc2
DE
11818 ? &dwp_file->sections.types
11819 : &dwp_file->sections.info,
11820 sections.info_or_types_offset,
11821 sections.info_or_types_size);
11822 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11823
11824 return dwo_unit;
11825}
11826
57d63ce2
DE
11827/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11828 Returns NULL if the signature isn't found. */
80626a55
DE
11829
11830static struct dwo_unit *
ed2dc618
SM
11831lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11832 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11833 ULONGEST signature, int is_debug_types)
80626a55 11834{
57d63ce2
DE
11835 const struct dwp_hash_table *dwp_htab =
11836 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11837 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11838 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11839 uint32_t hash = signature & mask;
11840 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11841 unsigned int i;
11842 void **slot;
870f88f7 11843 struct dwo_unit find_dwo_cu;
80626a55
DE
11844
11845 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11846 find_dwo_cu.signature = signature;
19ac8c2e 11847 slot = htab_find_slot (is_debug_types
48b490f2
TT
11848 ? dwp_file->loaded_tus.get ()
11849 : dwp_file->loaded_cus.get (),
19ac8c2e 11850 &find_dwo_cu, INSERT);
80626a55
DE
11851
11852 if (*slot != NULL)
9a3c8263 11853 return (struct dwo_unit *) *slot;
80626a55
DE
11854
11855 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11856 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11857 {
11858 ULONGEST signature_in_table;
11859
11860 signature_in_table =
57d63ce2 11861 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11862 if (signature_in_table == signature)
11863 {
57d63ce2
DE
11864 uint32_t unit_index =
11865 read_4_bytes (dbfd,
11866 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11867
73869dc2
DE
11868 if (dwp_file->version == 1)
11869 {
ed2dc618
SM
11870 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11871 dwp_file, unit_index,
73869dc2
DE
11872 comp_dir, signature,
11873 is_debug_types);
11874 }
11875 else
11876 {
ed2dc618
SM
11877 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11878 dwp_file, unit_index,
73869dc2
DE
11879 comp_dir, signature,
11880 is_debug_types);
11881 }
9a3c8263 11882 return (struct dwo_unit *) *slot;
80626a55
DE
11883 }
11884 if (signature_in_table == 0)
11885 return NULL;
11886 hash = (hash + hash2) & mask;
11887 }
11888
11889 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11890 " [in module %s]"),
11891 dwp_file->name);
11892}
11893
ab5088bf 11894/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11895 Open the file specified by FILE_NAME and hand it off to BFD for
11896 preliminary analysis. Return a newly initialized bfd *, which
11897 includes a canonicalized copy of FILE_NAME.
80626a55 11898 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11899 SEARCH_CWD is true if the current directory is to be searched.
11900 It will be searched before debug-file-directory.
13aaf454
DE
11901 If successful, the file is added to the bfd include table of the
11902 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11903 If unable to find/open the file, return NULL.
3019eac3
DE
11904 NOTE: This function is derived from symfile_bfd_open. */
11905
192b62ce 11906static gdb_bfd_ref_ptr
ed2dc618
SM
11907try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11908 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11909{
24b9144d 11910 int desc;
9c02c129
DE
11911 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11912 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11913 to debug_file_directory. */
e0cc99a6 11914 const char *search_path;
9c02c129
DE
11915 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11916
e0cc99a6 11917 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
11918 if (search_cwd)
11919 {
11920 if (*debug_file_directory != '\0')
e0cc99a6
TT
11921 {
11922 search_path_holder.reset (concat (".", dirname_separator_string,
11923 debug_file_directory,
11924 (char *) NULL));
11925 search_path = search_path_holder.get ();
11926 }
6ac97d4c 11927 else
e0cc99a6 11928 search_path = ".";
6ac97d4c 11929 }
9c02c129 11930 else
e0cc99a6 11931 search_path = debug_file_directory;
3019eac3 11932
24b9144d 11933 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
11934 if (is_dwp)
11935 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
11936
11937 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 11938 desc = openp (search_path, flags, file_name,
3019eac3
DE
11939 O_RDONLY | O_BINARY, &absolute_name);
11940 if (desc < 0)
11941 return NULL;
11942
e0cc99a6
TT
11943 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11944 gnutarget, desc));
9c02c129
DE
11945 if (sym_bfd == NULL)
11946 return NULL;
192b62ce 11947 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 11948
192b62ce
TT
11949 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11950 return NULL;
3019eac3 11951
13aaf454
DE
11952 /* Success. Record the bfd as having been included by the objfile's bfd.
11953 This is important because things like demangled_names_hash lives in the
11954 objfile's per_bfd space and may have references to things like symbol
11955 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 11956 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 11957
3019eac3
DE
11958 return sym_bfd;
11959}
11960
ab5088bf 11961/* Try to open DWO file FILE_NAME.
3019eac3
DE
11962 COMP_DIR is the DW_AT_comp_dir attribute.
11963 The result is the bfd handle of the file.
11964 If there is a problem finding or opening the file, return NULL.
11965 Upon success, the canonicalized path of the file is stored in the bfd,
11966 same as symfile_bfd_open. */
11967
192b62ce 11968static gdb_bfd_ref_ptr
ed2dc618
SM
11969open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11970 const char *file_name, const char *comp_dir)
3019eac3 11971{
80626a55 11972 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
11973 return try_open_dwop_file (dwarf2_per_objfile, file_name,
11974 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
11975
11976 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11977
11978 if (comp_dir != NULL)
11979 {
43816ebc
TT
11980 gdb::unique_xmalloc_ptr<char> path_to_try
11981 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
11982
11983 /* NOTE: If comp_dir is a relative path, this will also try the
11984 search path, which seems useful. */
ed2dc618 11985 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 11986 path_to_try.get (),
ed2dc618 11987 0 /*is_dwp*/,
192b62ce 11988 1 /*search_cwd*/));
3019eac3
DE
11989 if (abfd != NULL)
11990 return abfd;
11991 }
11992
11993 /* That didn't work, try debug-file-directory, which, despite its name,
11994 is a list of paths. */
11995
11996 if (*debug_file_directory == '\0')
11997 return NULL;
11998
ed2dc618
SM
11999 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12000 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12001}
12002
80626a55
DE
12003/* This function is mapped across the sections and remembers the offset and
12004 size of each of the DWO debugging sections we are interested in. */
12005
12006static void
12007dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12008{
9a3c8263 12009 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12010 const struct dwop_section_names *names = &dwop_section_names;
12011
12012 if (section_is_p (sectp->name, &names->abbrev_dwo))
12013 {
049412e3 12014 dwo_sections->abbrev.s.section = sectp;
fd361982 12015 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12016 }
12017 else if (section_is_p (sectp->name, &names->info_dwo))
12018 {
049412e3 12019 dwo_sections->info.s.section = sectp;
fd361982 12020 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12021 }
12022 else if (section_is_p (sectp->name, &names->line_dwo))
12023 {
049412e3 12024 dwo_sections->line.s.section = sectp;
fd361982 12025 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12026 }
12027 else if (section_is_p (sectp->name, &names->loc_dwo))
12028 {
049412e3 12029 dwo_sections->loc.s.section = sectp;
fd361982 12030 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12031 }
12032 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12033 {
049412e3 12034 dwo_sections->macinfo.s.section = sectp;
fd361982 12035 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12036 }
12037 else if (section_is_p (sectp->name, &names->macro_dwo))
12038 {
049412e3 12039 dwo_sections->macro.s.section = sectp;
fd361982 12040 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12041 }
12042 else if (section_is_p (sectp->name, &names->str_dwo))
12043 {
049412e3 12044 dwo_sections->str.s.section = sectp;
fd361982 12045 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12046 }
12047 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12048 {
049412e3 12049 dwo_sections->str_offsets.s.section = sectp;
fd361982 12050 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12051 }
12052 else if (section_is_p (sectp->name, &names->types_dwo))
12053 {
12054 struct dwarf2_section_info type_section;
12055
12056 memset (&type_section, 0, sizeof (type_section));
049412e3 12057 type_section.s.section = sectp;
fd361982 12058 type_section.size = bfd_section_size (sectp);
fd5866f6 12059 dwo_sections->types.push_back (type_section);
80626a55
DE
12060 }
12061}
12062
ab5088bf 12063/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12064 by PER_CU. This is for the non-DWP case.
80626a55 12065 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12066
12067static struct dwo_file *
0ac5b59e
DE
12068open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12069 const char *dwo_name, const char *comp_dir)
3019eac3 12070{
ed2dc618 12071 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12072
fb1eb2f9 12073 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12074 if (dbfd == NULL)
12075 {
b4f54984 12076 if (dwarf_read_debug)
80626a55
DE
12077 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12078 return NULL;
12079 }
263db9a1 12080
51ac9db5 12081 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12082 dwo_file->dwo_name = dwo_name;
12083 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12084 dwo_file->dbfd = std::move (dbfd);
3019eac3 12085
fb1eb2f9 12086 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12087 &dwo_file->sections);
3019eac3 12088
18a8505e
AT
12089 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12090 dwo_file->sections.info, dwo_file->cus);
3019eac3 12091
263db9a1 12092 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12093 dwo_file->sections.types, dwo_file->tus);
3019eac3 12094
b4f54984 12095 if (dwarf_read_debug)
80626a55
DE
12096 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12097
263db9a1 12098 return dwo_file.release ();
3019eac3
DE
12099}
12100
80626a55 12101/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12102 size of each of the DWP debugging sections common to version 1 and 2 that
12103 we are interested in. */
3019eac3 12104
80626a55 12105static void
73869dc2
DE
12106dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12107 void *dwp_file_ptr)
3019eac3 12108{
9a3c8263 12109 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12110 const struct dwop_section_names *names = &dwop_section_names;
12111 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12112
80626a55 12113 /* Record the ELF section number for later lookup: this is what the
73869dc2 12114 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12115 gdb_assert (elf_section_nr < dwp_file->num_sections);
12116 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12117
80626a55
DE
12118 /* Look for specific sections that we need. */
12119 if (section_is_p (sectp->name, &names->str_dwo))
12120 {
049412e3 12121 dwp_file->sections.str.s.section = sectp;
fd361982 12122 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12123 }
12124 else if (section_is_p (sectp->name, &names->cu_index))
12125 {
049412e3 12126 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12127 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12128 }
12129 else if (section_is_p (sectp->name, &names->tu_index))
12130 {
049412e3 12131 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12132 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12133 }
12134}
3019eac3 12135
73869dc2
DE
12136/* This function is mapped across the sections and remembers the offset and
12137 size of each of the DWP version 2 debugging sections that we are interested
12138 in. This is split into a separate function because we don't know if we
12139 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12140
12141static void
12142dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12143{
9a3c8263 12144 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12145 const struct dwop_section_names *names = &dwop_section_names;
12146 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12147
12148 /* Record the ELF section number for later lookup: this is what the
12149 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12150 gdb_assert (elf_section_nr < dwp_file->num_sections);
12151 dwp_file->elf_sections[elf_section_nr] = sectp;
12152
12153 /* Look for specific sections that we need. */
12154 if (section_is_p (sectp->name, &names->abbrev_dwo))
12155 {
049412e3 12156 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12157 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12158 }
12159 else if (section_is_p (sectp->name, &names->info_dwo))
12160 {
049412e3 12161 dwp_file->sections.info.s.section = sectp;
fd361982 12162 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12163 }
12164 else if (section_is_p (sectp->name, &names->line_dwo))
12165 {
049412e3 12166 dwp_file->sections.line.s.section = sectp;
fd361982 12167 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12168 }
12169 else if (section_is_p (sectp->name, &names->loc_dwo))
12170 {
049412e3 12171 dwp_file->sections.loc.s.section = sectp;
fd361982 12172 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12173 }
12174 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12175 {
049412e3 12176 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12177 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12178 }
12179 else if (section_is_p (sectp->name, &names->macro_dwo))
12180 {
049412e3 12181 dwp_file->sections.macro.s.section = sectp;
fd361982 12182 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12183 }
12184 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12185 {
049412e3 12186 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12187 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12188 }
12189 else if (section_is_p (sectp->name, &names->types_dwo))
12190 {
049412e3 12191 dwp_file->sections.types.s.section = sectp;
fd361982 12192 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12193 }
12194}
12195
80626a55 12196/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12197
80626a55
DE
12198static hashval_t
12199hash_dwp_loaded_cutus (const void *item)
12200{
9a3c8263 12201 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12202
80626a55
DE
12203 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12204 return dwo_unit->signature;
3019eac3
DE
12205}
12206
80626a55 12207/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12208
80626a55
DE
12209static int
12210eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12211{
9a3c8263
SM
12212 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12213 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12214
80626a55
DE
12215 return dua->signature == dub->signature;
12216}
3019eac3 12217
80626a55 12218/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12219
48b490f2 12220static htab_up
298e9637 12221allocate_dwp_loaded_cutus_table ()
80626a55 12222{
48b490f2
TT
12223 return htab_up (htab_create_alloc (3,
12224 hash_dwp_loaded_cutus,
12225 eq_dwp_loaded_cutus,
12226 NULL, xcalloc, xfree));
80626a55 12227}
3019eac3 12228
ab5088bf
DE
12229/* Try to open DWP file FILE_NAME.
12230 The result is the bfd handle of the file.
12231 If there is a problem finding or opening the file, return NULL.
12232 Upon success, the canonicalized path of the file is stored in the bfd,
12233 same as symfile_bfd_open. */
12234
192b62ce 12235static gdb_bfd_ref_ptr
ed2dc618
SM
12236open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12237 const char *file_name)
ab5088bf 12238{
ed2dc618
SM
12239 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12240 1 /*is_dwp*/,
192b62ce 12241 1 /*search_cwd*/));
6ac97d4c
DE
12242 if (abfd != NULL)
12243 return abfd;
12244
12245 /* Work around upstream bug 15652.
12246 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12247 [Whether that's a "bug" is debatable, but it is getting in our way.]
12248 We have no real idea where the dwp file is, because gdb's realpath-ing
12249 of the executable's path may have discarded the needed info.
12250 [IWBN if the dwp file name was recorded in the executable, akin to
12251 .gnu_debuglink, but that doesn't exist yet.]
12252 Strip the directory from FILE_NAME and search again. */
12253 if (*debug_file_directory != '\0')
12254 {
12255 /* Don't implicitly search the current directory here.
12256 If the user wants to search "." to handle this case,
12257 it must be added to debug-file-directory. */
ed2dc618
SM
12258 return try_open_dwop_file (dwarf2_per_objfile,
12259 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12260 0 /*search_cwd*/);
12261 }
12262
12263 return NULL;
ab5088bf
DE
12264}
12265
80626a55
DE
12266/* Initialize the use of the DWP file for the current objfile.
12267 By convention the name of the DWP file is ${objfile}.dwp.
12268 The result is NULL if it can't be found. */
a766d390 12269
400174b1 12270static std::unique_ptr<struct dwp_file>
ed2dc618 12271open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12272{
12273 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12274
82bf32bc
JK
12275 /* Try to find first .dwp for the binary file before any symbolic links
12276 resolving. */
6c447423
DE
12277
12278 /* If the objfile is a debug file, find the name of the real binary
12279 file and get the name of dwp file from there. */
d721ba37 12280 std::string dwp_name;
6c447423
DE
12281 if (objfile->separate_debug_objfile_backlink != NULL)
12282 {
12283 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12284 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12285
d721ba37 12286 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12287 }
12288 else
d721ba37
PA
12289 dwp_name = objfile->original_name;
12290
12291 dwp_name += ".dwp";
80626a55 12292
ed2dc618 12293 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12294 if (dbfd == NULL
12295 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12296 {
12297 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12298 dwp_name = objfile_name (objfile);
12299 dwp_name += ".dwp";
ed2dc618 12300 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12301 }
12302
80626a55
DE
12303 if (dbfd == NULL)
12304 {
b4f54984 12305 if (dwarf_read_debug)
d721ba37 12306 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12307 return std::unique_ptr<dwp_file> ();
3019eac3 12308 }
400174b1
TT
12309
12310 const char *name = bfd_get_filename (dbfd.get ());
12311 std::unique_ptr<struct dwp_file> dwp_file
12312 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12313
0a0f4c01 12314 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12315 dwp_file->elf_sections =
12316 OBSTACK_CALLOC (&objfile->objfile_obstack,
12317 dwp_file->num_sections, asection *);
12318
400174b1
TT
12319 bfd_map_over_sections (dwp_file->dbfd.get (),
12320 dwarf2_locate_common_dwp_sections,
12321 dwp_file.get ());
80626a55 12322
400174b1
TT
12323 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12324 0);
80626a55 12325
400174b1
TT
12326 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12327 1);
80626a55 12328
73869dc2 12329 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12330 if (dwp_file->cus && dwp_file->tus
12331 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12332 {
12333 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12334 pretty bizarre. We use pulongest here because that's the established
4d65956b 12335 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12336 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12337 " TU version %s [in DWP file %s]"),
12338 pulongest (dwp_file->cus->version),
d721ba37 12339 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12340 }
08302ed2
DE
12341
12342 if (dwp_file->cus)
12343 dwp_file->version = dwp_file->cus->version;
12344 else if (dwp_file->tus)
12345 dwp_file->version = dwp_file->tus->version;
12346 else
12347 dwp_file->version = 2;
73869dc2
DE
12348
12349 if (dwp_file->version == 2)
400174b1
TT
12350 bfd_map_over_sections (dwp_file->dbfd.get (),
12351 dwarf2_locate_v2_dwp_sections,
12352 dwp_file.get ());
73869dc2 12353
298e9637
SM
12354 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12355 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12356
b4f54984 12357 if (dwarf_read_debug)
80626a55
DE
12358 {
12359 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12360 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12361 " %s CUs, %s TUs\n",
12362 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12363 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12364 }
12365
12366 return dwp_file;
3019eac3 12367}
c906108c 12368
ab5088bf
DE
12369/* Wrapper around open_and_init_dwp_file, only open it once. */
12370
12371static struct dwp_file *
ed2dc618 12372get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12373{
12374 if (! dwarf2_per_objfile->dwp_checked)
12375 {
ed2dc618
SM
12376 dwarf2_per_objfile->dwp_file
12377 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12378 dwarf2_per_objfile->dwp_checked = 1;
12379 }
400174b1 12380 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12381}
12382
80626a55
DE
12383/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12384 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12385 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12386 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12387 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12388
12389 This is called, for example, when wanting to read a variable with a
12390 complex location. Therefore we don't want to do file i/o for every call.
12391 Therefore we don't want to look for a DWO file on every call.
12392 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12393 then we check if we've already seen DWO_NAME, and only THEN do we check
12394 for a DWO file.
12395
1c658ad5 12396 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12397 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12398
3019eac3 12399static struct dwo_unit *
80626a55
DE
12400lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12401 const char *dwo_name, const char *comp_dir,
12402 ULONGEST signature, int is_debug_types)
3019eac3 12403{
ed2dc618 12404 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12405 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12406 const char *kind = is_debug_types ? "TU" : "CU";
12407 void **dwo_file_slot;
3019eac3 12408 struct dwo_file *dwo_file;
80626a55 12409 struct dwp_file *dwp_file;
cb1df416 12410
6a506a2d
DE
12411 /* First see if there's a DWP file.
12412 If we have a DWP file but didn't find the DWO inside it, don't
12413 look for the original DWO file. It makes gdb behave differently
12414 depending on whether one is debugging in the build tree. */
cf2c3c16 12415
ed2dc618 12416 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12417 if (dwp_file != NULL)
cf2c3c16 12418 {
80626a55
DE
12419 const struct dwp_hash_table *dwp_htab =
12420 is_debug_types ? dwp_file->tus : dwp_file->cus;
12421
12422 if (dwp_htab != NULL)
12423 {
12424 struct dwo_unit *dwo_cutu =
ed2dc618 12425 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12426 signature, is_debug_types);
80626a55
DE
12427
12428 if (dwo_cutu != NULL)
12429 {
b4f54984 12430 if (dwarf_read_debug)
80626a55
DE
12431 {
12432 fprintf_unfiltered (gdb_stdlog,
12433 "Virtual DWO %s %s found: @%s\n",
12434 kind, hex_string (signature),
12435 host_address_to_string (dwo_cutu));
12436 }
12437 return dwo_cutu;
12438 }
12439 }
12440 }
6a506a2d 12441 else
80626a55 12442 {
6a506a2d 12443 /* No DWP file, look for the DWO file. */
80626a55 12444
ed2dc618
SM
12445 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12446 dwo_name, comp_dir);
6a506a2d 12447 if (*dwo_file_slot == NULL)
80626a55 12448 {
6a506a2d
DE
12449 /* Read in the file and build a table of the CUs/TUs it contains. */
12450 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12451 }
6a506a2d 12452 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12453 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12454
6a506a2d 12455 if (dwo_file != NULL)
19c3d4c9 12456 {
6a506a2d
DE
12457 struct dwo_unit *dwo_cutu = NULL;
12458
12459 if (is_debug_types && dwo_file->tus)
12460 {
12461 struct dwo_unit find_dwo_cutu;
12462
12463 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12464 find_dwo_cutu.signature = signature;
9a3c8263 12465 dwo_cutu
b0b6a987
TT
12466 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12467 &find_dwo_cutu);
6a506a2d 12468 }
33c5cd75 12469 else if (!is_debug_types && dwo_file->cus)
80626a55 12470 {
33c5cd75
DB
12471 struct dwo_unit find_dwo_cutu;
12472
12473 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12474 find_dwo_cutu.signature = signature;
b0b6a987 12475 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12476 &find_dwo_cutu);
6a506a2d
DE
12477 }
12478
12479 if (dwo_cutu != NULL)
12480 {
b4f54984 12481 if (dwarf_read_debug)
6a506a2d
DE
12482 {
12483 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12484 kind, dwo_name, hex_string (signature),
12485 host_address_to_string (dwo_cutu));
12486 }
12487 return dwo_cutu;
80626a55
DE
12488 }
12489 }
2e276125 12490 }
9cdd5dbd 12491
80626a55
DE
12492 /* We didn't find it. This could mean a dwo_id mismatch, or
12493 someone deleted the DWO/DWP file, or the search path isn't set up
12494 correctly to find the file. */
12495
b4f54984 12496 if (dwarf_read_debug)
80626a55
DE
12497 {
12498 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12499 kind, dwo_name, hex_string (signature));
12500 }
3019eac3 12501
6656a72d
DE
12502 /* This is a warning and not a complaint because it can be caused by
12503 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12504 {
12505 /* Print the name of the DWP file if we looked there, helps the user
12506 better diagnose the problem. */
791afaa2 12507 std::string dwp_text;
43942612
DE
12508
12509 if (dwp_file != NULL)
791afaa2
TT
12510 dwp_text = string_printf (" [in DWP file %s]",
12511 lbasename (dwp_file->name));
43942612 12512
9d8780f0 12513 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12514 " [in module %s]"),
12515 kind, dwo_name, hex_string (signature),
791afaa2 12516 dwp_text.c_str (),
43942612 12517 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12518 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12519 }
3019eac3 12520 return NULL;
5fb290d7
DJ
12521}
12522
80626a55
DE
12523/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12524 See lookup_dwo_cutu_unit for details. */
12525
12526static struct dwo_unit *
12527lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12528 const char *dwo_name, const char *comp_dir,
12529 ULONGEST signature)
12530{
12531 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12532}
12533
12534/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12535 See lookup_dwo_cutu_unit for details. */
12536
12537static struct dwo_unit *
12538lookup_dwo_type_unit (struct signatured_type *this_tu,
12539 const char *dwo_name, const char *comp_dir)
12540{
12541 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12542}
12543
89e63ee4
DE
12544/* Traversal function for queue_and_load_all_dwo_tus. */
12545
12546static int
12547queue_and_load_dwo_tu (void **slot, void *info)
12548{
12549 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12550 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12551 ULONGEST signature = dwo_unit->signature;
12552 struct signatured_type *sig_type =
12553 lookup_dwo_signatured_type (per_cu->cu, signature);
12554
12555 if (sig_type != NULL)
12556 {
12557 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12558
12559 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12560 a real dependency of PER_CU on SIG_TYPE. That is detected later
12561 while processing PER_CU. */
12562 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12563 load_full_type_unit (sig_cu);
ae640021 12564 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12565 }
12566
12567 return 1;
12568}
12569
12570/* Queue all TUs contained in the DWO of PER_CU to be read in.
12571 The DWO may have the only definition of the type, though it may not be
12572 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12573 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12574
12575static void
12576queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12577{
12578 struct dwo_unit *dwo_unit;
12579 struct dwo_file *dwo_file;
12580
12581 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12582 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12583 gdb_assert (per_cu->cu != NULL);
12584
12585 dwo_unit = per_cu->cu->dwo_unit;
12586 gdb_assert (dwo_unit != NULL);
12587
12588 dwo_file = dwo_unit->dwo_file;
12589 if (dwo_file->tus != NULL)
b0b6a987
TT
12590 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12591 per_cu);
89e63ee4
DE
12592}
12593
3019eac3 12594/* Read in various DIEs. */
348e048f 12595
d389af10 12596/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12597 Inherit only the children of the DW_AT_abstract_origin DIE not being
12598 already referenced by DW_AT_abstract_origin from the children of the
12599 current DIE. */
d389af10
JK
12600
12601static void
12602inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12603{
12604 struct die_info *child_die;
791afaa2 12605 sect_offset *offsetp;
d389af10
JK
12606 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12607 struct die_info *origin_die;
12608 /* Iterator of the ORIGIN_DIE children. */
12609 struct die_info *origin_child_die;
d389af10 12610 struct attribute *attr;
cd02d79d
PA
12611 struct dwarf2_cu *origin_cu;
12612 struct pending **origin_previous_list_in_scope;
d389af10
JK
12613
12614 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12615 if (!attr)
12616 return;
12617
cd02d79d
PA
12618 /* Note that following die references may follow to a die in a
12619 different cu. */
12620
12621 origin_cu = cu;
12622 origin_die = follow_die_ref (die, attr, &origin_cu);
12623
12624 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12625 symbols in. */
12626 origin_previous_list_in_scope = origin_cu->list_in_scope;
12627 origin_cu->list_in_scope = cu->list_in_scope;
12628
edb3359d
DJ
12629 if (die->tag != origin_die->tag
12630 && !(die->tag == DW_TAG_inlined_subroutine
12631 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12632 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12633 sect_offset_str (die->sect_off),
12634 sect_offset_str (origin_die->sect_off));
d389af10 12635
791afaa2 12636 std::vector<sect_offset> offsets;
d389af10 12637
3ea89b92
PMR
12638 for (child_die = die->child;
12639 child_die && child_die->tag;
436c571c 12640 child_die = child_die->sibling)
3ea89b92
PMR
12641 {
12642 struct die_info *child_origin_die;
12643 struct dwarf2_cu *child_origin_cu;
12644
12645 /* We are trying to process concrete instance entries:
216f72a1 12646 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12647 it's not relevant to our analysis here. i.e. detecting DIEs that are
12648 present in the abstract instance but not referenced in the concrete
12649 one. */
216f72a1
JK
12650 if (child_die->tag == DW_TAG_call_site
12651 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12652 continue;
12653
c38f313d
DJ
12654 /* For each CHILD_DIE, find the corresponding child of
12655 ORIGIN_DIE. If there is more than one layer of
12656 DW_AT_abstract_origin, follow them all; there shouldn't be,
12657 but GCC versions at least through 4.4 generate this (GCC PR
12658 40573). */
3ea89b92
PMR
12659 child_origin_die = child_die;
12660 child_origin_cu = cu;
c38f313d
DJ
12661 while (1)
12662 {
cd02d79d
PA
12663 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12664 child_origin_cu);
c38f313d
DJ
12665 if (attr == NULL)
12666 break;
cd02d79d
PA
12667 child_origin_die = follow_die_ref (child_origin_die, attr,
12668 &child_origin_cu);
c38f313d
DJ
12669 }
12670
d389af10
JK
12671 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12672 counterpart may exist. */
c38f313d 12673 if (child_origin_die != child_die)
d389af10 12674 {
edb3359d
DJ
12675 if (child_die->tag != child_origin_die->tag
12676 && !(child_die->tag == DW_TAG_inlined_subroutine
12677 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12678 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12679 "different tags"),
9d8780f0
SM
12680 sect_offset_str (child_die->sect_off),
12681 sect_offset_str (child_origin_die->sect_off));
c38f313d 12682 if (child_origin_die->parent != origin_die)
b98664d3 12683 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12684 "different parents"),
9d8780f0
SM
12685 sect_offset_str (child_die->sect_off),
12686 sect_offset_str (child_origin_die->sect_off));
c38f313d 12687 else
791afaa2 12688 offsets.push_back (child_origin_die->sect_off);
d389af10 12689 }
d389af10 12690 }
791afaa2
TT
12691 std::sort (offsets.begin (), offsets.end ());
12692 sect_offset *offsets_end = offsets.data () + offsets.size ();
12693 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12694 if (offsetp[-1] == *offsetp)
b98664d3 12695 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12696 "to DIE %s as their abstract origin"),
12697 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12698
791afaa2 12699 offsetp = offsets.data ();
d389af10
JK
12700 origin_child_die = origin_die->child;
12701 while (origin_child_die && origin_child_die->tag)
12702 {
12703 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12704 while (offsetp < offsets_end
9c541725 12705 && *offsetp < origin_child_die->sect_off)
d389af10 12706 offsetp++;
b64f50a1 12707 if (offsetp >= offsets_end
9c541725 12708 || *offsetp > origin_child_die->sect_off)
d389af10 12709 {
adde2bff
DE
12710 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12711 Check whether we're already processing ORIGIN_CHILD_DIE.
12712 This can happen with mutually referenced abstract_origins.
12713 PR 16581. */
12714 if (!origin_child_die->in_process)
12715 process_die (origin_child_die, origin_cu);
d389af10 12716 }
436c571c 12717 origin_child_die = origin_child_die->sibling;
d389af10 12718 }
cd02d79d 12719 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12720
12721 if (cu != origin_cu)
12722 compute_delayed_physnames (origin_cu);
d389af10
JK
12723}
12724
c906108c 12725static void
e7c27a73 12726read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12727{
518817b3 12728 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12729 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12730 struct context_stack *newobj;
c906108c
SS
12731 CORE_ADDR lowpc;
12732 CORE_ADDR highpc;
12733 struct die_info *child_die;
edb3359d 12734 struct attribute *attr, *call_line, *call_file;
15d034d0 12735 const char *name;
e142c38c 12736 CORE_ADDR baseaddr;
801e3a5b 12737 struct block *block;
edb3359d 12738 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12739 std::vector<struct symbol *> template_args;
34eaf542 12740 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12741
12742 if (inlined_func)
12743 {
12744 /* If we do not have call site information, we can't show the
12745 caller of this inlined function. That's too confusing, so
12746 only use the scope for local variables. */
12747 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12748 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12749 if (call_line == NULL || call_file == NULL)
12750 {
12751 read_lexical_block_scope (die, cu);
12752 return;
12753 }
12754 }
c906108c 12755
b3b3bada 12756 baseaddr = objfile->text_section_offset ();
e142c38c 12757
94af9270 12758 name = dwarf2_name (die, cu);
c906108c 12759
e8d05480
JB
12760 /* Ignore functions with missing or empty names. These are actually
12761 illegal according to the DWARF standard. */
12762 if (name == NULL)
12763 {
b98664d3 12764 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12765 sect_offset_str (die->sect_off));
e8d05480
JB
12766 return;
12767 }
12768
12769 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12770 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12771 <= PC_BOUNDS_INVALID)
e8d05480 12772 {
ae4d0c03
PM
12773 attr = dwarf2_attr (die, DW_AT_external, cu);
12774 if (!attr || !DW_UNSND (attr))
b98664d3 12775 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12776 "for subprogram DIE at %s"),
12777 sect_offset_str (die->sect_off));
e8d05480
JB
12778 return;
12779 }
c906108c 12780
3e29f34a
MR
12781 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12782 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12783
34eaf542
TT
12784 /* If we have any template arguments, then we must allocate a
12785 different sort of symbol. */
436c571c 12786 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12787 {
12788 if (child_die->tag == DW_TAG_template_type_param
12789 || child_die->tag == DW_TAG_template_value_param)
12790 {
e623cf5d 12791 templ_func = allocate_template_symbol (objfile);
cf724bc9 12792 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12793 break;
12794 }
12795 }
12796
c24bdb02 12797 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12798 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12799 (struct symbol *) templ_func);
4c2df51b 12800
81873cc8 12801 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12802 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12803 cu->language);
12804
4cecd739
DJ
12805 /* If there is a location expression for DW_AT_frame_base, record
12806 it. */
e142c38c 12807 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12808 if (attr != nullptr)
fe978cb0 12809 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12810
63e43d3a
PMR
12811 /* If there is a location for the static link, record it. */
12812 newobj->static_link = NULL;
12813 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12814 if (attr != nullptr)
63e43d3a 12815 {
224c3ddb
SM
12816 newobj->static_link
12817 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12818 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12819 cu->per_cu->addr_type ());
63e43d3a
PMR
12820 }
12821
c24bdb02 12822 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12823
639d11d3 12824 if (die->child != NULL)
c906108c 12825 {
639d11d3 12826 child_die = die->child;
c906108c
SS
12827 while (child_die && child_die->tag)
12828 {
34eaf542
TT
12829 if (child_die->tag == DW_TAG_template_type_param
12830 || child_die->tag == DW_TAG_template_value_param)
12831 {
12832 struct symbol *arg = new_symbol (child_die, NULL, cu);
12833
f1078f66 12834 if (arg != NULL)
2f4732b0 12835 template_args.push_back (arg);
34eaf542
TT
12836 }
12837 else
12838 process_die (child_die, cu);
436c571c 12839 child_die = child_die->sibling;
c906108c
SS
12840 }
12841 }
12842
d389af10
JK
12843 inherit_abstract_dies (die, cu);
12844
4a811a97
UW
12845 /* If we have a DW_AT_specification, we might need to import using
12846 directives from the context of the specification DIE. See the
12847 comment in determine_prefix. */
12848 if (cu->language == language_cplus
12849 && dwarf2_attr (die, DW_AT_specification, cu))
12850 {
12851 struct dwarf2_cu *spec_cu = cu;
12852 struct die_info *spec_die = die_specification (die, &spec_cu);
12853
12854 while (spec_die)
12855 {
12856 child_die = spec_die->child;
12857 while (child_die && child_die->tag)
12858 {
12859 if (child_die->tag == DW_TAG_imported_module)
12860 process_die (child_die, spec_cu);
436c571c 12861 child_die = child_die->sibling;
4a811a97
UW
12862 }
12863
12864 /* In some cases, GCC generates specification DIEs that
12865 themselves contain DW_AT_specification attributes. */
12866 spec_die = die_specification (spec_die, &spec_cu);
12867 }
12868 }
12869
c24bdb02 12870 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12871 /* Make a block for the local symbols within. */
c24bdb02 12872 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12873 cstk.static_link, lowpc, highpc);
801e3a5b 12874
df8a16a1 12875 /* For C++, set the block's scope. */
45280282
IB
12876 if ((cu->language == language_cplus
12877 || cu->language == language_fortran
c44af4eb
TT
12878 || cu->language == language_d
12879 || cu->language == language_rust)
4d4ec4e5 12880 && cu->processing_has_namespace_info)
195a3f6c
TT
12881 block_set_scope (block, determine_prefix (die, cu),
12882 &objfile->objfile_obstack);
df8a16a1 12883
801e3a5b
JB
12884 /* If we have address ranges, record them. */
12885 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12886
a60f3166 12887 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12888
34eaf542 12889 /* Attach template arguments to function. */
2f4732b0 12890 if (!template_args.empty ())
34eaf542
TT
12891 {
12892 gdb_assert (templ_func != NULL);
12893
2f4732b0 12894 templ_func->n_template_arguments = template_args.size ();
34eaf542 12895 templ_func->template_arguments
8d749320
SM
12896 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12897 templ_func->n_template_arguments);
34eaf542 12898 memcpy (templ_func->template_arguments,
2f4732b0 12899 template_args.data (),
34eaf542 12900 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
12901
12902 /* Make sure that the symtab is set on the new symbols. Even
12903 though they don't appear in this symtab directly, other parts
12904 of gdb assume that symbols do, and this is reasonably
12905 true. */
8634679f 12906 for (symbol *sym : template_args)
3e1d3d8c 12907 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
12908 }
12909
208d8187
JB
12910 /* In C++, we can have functions nested inside functions (e.g., when
12911 a function declares a class that has methods). This means that
12912 when we finish processing a function scope, we may need to go
12913 back to building a containing block's symbol lists. */
c24bdb02
KS
12914 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12915 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 12916
921e78cf
JB
12917 /* If we've finished processing a top-level function, subsequent
12918 symbols go in the file symbol list. */
c24bdb02
KS
12919 if (cu->get_builder ()->outermost_context_p ())
12920 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
12921}
12922
12923/* Process all the DIES contained within a lexical block scope. Start
12924 a new scope, process the dies, and then close the scope. */
12925
12926static void
e7c27a73 12927read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12928{
518817b3 12929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12930 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12931 CORE_ADDR lowpc, highpc;
12932 struct die_info *child_die;
e142c38c
DJ
12933 CORE_ADDR baseaddr;
12934
b3b3bada 12935 baseaddr = objfile->text_section_offset ();
c906108c
SS
12936
12937 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
12938 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12939 as multiple lexical blocks? Handling children in a sane way would
6e70227d 12940 be nasty. Might be easier to properly extend generic blocks to
af34e669 12941 describe ranges. */
e385593e
JK
12942 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12943 {
12944 case PC_BOUNDS_NOT_PRESENT:
12945 /* DW_TAG_lexical_block has no attributes, process its children as if
12946 there was no wrapping by that DW_TAG_lexical_block.
12947 GCC does no longer produces such DWARF since GCC r224161. */
12948 for (child_die = die->child;
12949 child_die != NULL && child_die->tag;
436c571c 12950 child_die = child_die->sibling)
e385593e
JK
12951 process_die (child_die, cu);
12952 return;
12953 case PC_BOUNDS_INVALID:
12954 return;
12955 }
3e29f34a
MR
12956 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12957 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12958
c24bdb02 12959 cu->get_builder ()->push_context (0, lowpc);
639d11d3 12960 if (die->child != NULL)
c906108c 12961 {
639d11d3 12962 child_die = die->child;
c906108c
SS
12963 while (child_die && child_die->tag)
12964 {
e7c27a73 12965 process_die (child_die, cu);
436c571c 12966 child_die = child_die->sibling;
c906108c
SS
12967 }
12968 }
3ea89b92 12969 inherit_abstract_dies (die, cu);
c24bdb02 12970 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12971
c24bdb02
KS
12972 if (*cu->get_builder ()->get_local_symbols () != NULL
12973 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 12974 {
801e3a5b 12975 struct block *block
c24bdb02 12976 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 12977 cstk.start_addr, highpc);
801e3a5b
JB
12978
12979 /* Note that recording ranges after traversing children, as we
12980 do here, means that recording a parent's ranges entails
12981 walking across all its children's ranges as they appear in
12982 the address map, which is quadratic behavior.
12983
12984 It would be nicer to record the parent's ranges before
12985 traversing its children, simply overriding whatever you find
12986 there. But since we don't even decide whether to create a
12987 block until after we've traversed its children, that's hard
12988 to do. */
12989 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 12990 }
c24bdb02
KS
12991 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12992 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
12993}
12994
216f72a1 12995/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
12996
12997static void
12998read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12999{
518817b3 13000 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13001 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13002 CORE_ADDR pc, baseaddr;
13003 struct attribute *attr;
13004 struct call_site *call_site, call_site_local;
13005 void **slot;
13006 int nparams;
13007 struct die_info *child_die;
13008
b3b3bada 13009 baseaddr = objfile->text_section_offset ();
96408a79 13010
216f72a1
JK
13011 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13012 if (attr == NULL)
13013 {
13014 /* This was a pre-DWARF-5 GNU extension alias
13015 for DW_AT_call_return_pc. */
13016 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13017 }
96408a79
SA
13018 if (!attr)
13019 {
b98664d3 13020 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13021 "DIE %s [in module %s]"),
13022 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13023 return;
13024 }
cd6c91b4 13025 pc = attr->value_as_address () + baseaddr;
3e29f34a 13026 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13027
13028 if (cu->call_site_htab == NULL)
13029 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13030 NULL, &objfile->objfile_obstack,
13031 hashtab_obstack_allocate, NULL);
13032 call_site_local.pc = pc;
13033 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13034 if (*slot != NULL)
13035 {
b98664d3 13036 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13037 "DIE %s [in module %s]"),
13038 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13039 objfile_name (objfile));
96408a79
SA
13040 return;
13041 }
13042
13043 /* Count parameters at the caller. */
13044
13045 nparams = 0;
13046 for (child_die = die->child; child_die && child_die->tag;
436c571c 13047 child_die = child_die->sibling)
96408a79 13048 {
216f72a1
JK
13049 if (child_die->tag != DW_TAG_call_site_parameter
13050 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13051 {
b98664d3 13052 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13053 "DW_TAG_call_site child DIE %s [in module %s]"),
13054 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13055 objfile_name (objfile));
96408a79
SA
13056 continue;
13057 }
13058
13059 nparams++;
13060 }
13061
224c3ddb
SM
13062 call_site
13063 = ((struct call_site *)
13064 obstack_alloc (&objfile->objfile_obstack,
13065 sizeof (*call_site)
13066 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13067 *slot = call_site;
13068 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13069 call_site->pc = pc;
13070
216f72a1
JK
13071 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13072 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13073 {
13074 struct die_info *func_die;
13075
13076 /* Skip also over DW_TAG_inlined_subroutine. */
13077 for (func_die = die->parent;
13078 func_die && func_die->tag != DW_TAG_subprogram
13079 && func_die->tag != DW_TAG_subroutine_type;
13080 func_die = func_die->parent);
13081
216f72a1
JK
13082 /* DW_AT_call_all_calls is a superset
13083 of DW_AT_call_all_tail_calls. */
96408a79 13084 if (func_die
216f72a1 13085 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13086 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13087 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13088 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13089 {
13090 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13091 not complete. But keep CALL_SITE for look ups via call_site_htab,
13092 both the initial caller containing the real return address PC and
13093 the final callee containing the current PC of a chain of tail
13094 calls do not need to have the tail call list complete. But any
13095 function candidate for a virtual tail call frame searched via
13096 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13097 determined unambiguously. */
13098 }
13099 else
13100 {
13101 struct type *func_type = NULL;
13102
13103 if (func_die)
13104 func_type = get_die_type (func_die, cu);
13105 if (func_type != NULL)
13106 {
13107 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13108
13109 /* Enlist this call site to the function. */
13110 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13111 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13112 }
13113 else
b98664d3 13114 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13115 "DIE %s [in module %s]"),
13116 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13117 }
13118 }
13119
216f72a1
JK
13120 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13121 if (attr == NULL)
13122 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13123 if (attr == NULL)
13124 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13125 if (attr == NULL)
216f72a1
JK
13126 {
13127 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13128 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13129 }
96408a79 13130 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13131 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13132 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13133 else if (attr->form_is_block ())
96408a79
SA
13134 {
13135 struct dwarf2_locexpr_baton *dlbaton;
13136
8d749320 13137 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13138 dlbaton->data = DW_BLOCK (attr)->data;
13139 dlbaton->size = DW_BLOCK (attr)->size;
13140 dlbaton->per_cu = cu->per_cu;
13141
13142 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13143 }
cd6c91b4 13144 else if (attr->form_is_ref ())
96408a79 13145 {
96408a79
SA
13146 struct dwarf2_cu *target_cu = cu;
13147 struct die_info *target_die;
13148
ac9ec31b 13149 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13150 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13151 if (die_is_declaration (target_die, target_cu))
13152 {
7d45c7c3 13153 const char *target_physname;
9112db09
JK
13154
13155 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13156 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13157 if (target_physname == NULL)
9112db09 13158 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13159 if (target_physname == NULL)
b98664d3 13160 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13161 "physname, for referencing DIE %s [in module %s]"),
13162 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13163 else
7d455152 13164 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13165 }
13166 else
13167 {
13168 CORE_ADDR lowpc;
13169
13170 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13171 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13172 <= PC_BOUNDS_INVALID)
b98664d3 13173 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13174 "low pc, for referencing DIE %s [in module %s]"),
13175 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13176 else
3e29f34a
MR
13177 {
13178 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13179 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13180 }
96408a79
SA
13181 }
13182 }
13183 else
b98664d3 13184 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13185 "block nor reference, for DIE %s [in module %s]"),
13186 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13187
13188 call_site->per_cu = cu->per_cu;
13189
13190 for (child_die = die->child;
13191 child_die && child_die->tag;
436c571c 13192 child_die = child_die->sibling)
96408a79 13193 {
96408a79 13194 struct call_site_parameter *parameter;
1788b2d3 13195 struct attribute *loc, *origin;
96408a79 13196
216f72a1
JK
13197 if (child_die->tag != DW_TAG_call_site_parameter
13198 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13199 {
13200 /* Already printed the complaint above. */
13201 continue;
13202 }
13203
13204 gdb_assert (call_site->parameter_count < nparams);
13205 parameter = &call_site->parameter[call_site->parameter_count];
13206
1788b2d3
JK
13207 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13208 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13209 register is contained in DW_AT_call_value. */
96408a79 13210
24c5c679 13211 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13212 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13213 if (origin == NULL)
13214 {
13215 /* This was a pre-DWARF-5 GNU extension alias
13216 for DW_AT_call_parameter. */
13217 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13218 }
cd6c91b4 13219 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13220 {
1788b2d3 13221 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13222
0826b30a 13223 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13224 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13225 {
13226 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13227 binding can be done only inside one CU. Such referenced DIE
13228 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13229 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13230 "DW_TAG_call_site child DIE %s [in module %s]"),
13231 sect_offset_str (child_die->sect_off),
9c541725 13232 objfile_name (objfile));
d76b7dbc
JK
13233 continue;
13234 }
9c541725
PA
13235 parameter->u.param_cu_off
13236 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13237 }
4fc6c0d5 13238 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13239 {
b98664d3 13240 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13241 "DW_TAG_call_site child DIE %s [in module %s]"),
13242 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13243 continue;
13244 }
24c5c679 13245 else
96408a79 13246 {
24c5c679
JK
13247 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13248 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13249 if (parameter->u.dwarf_reg != -1)
13250 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13251 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13252 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13253 &parameter->u.fb_offset))
13254 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13255 else
13256 {
b98664d3 13257 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13258 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13259 "DW_TAG_call_site child DIE %s "
24c5c679 13260 "[in module %s]"),
9d8780f0 13261 sect_offset_str (child_die->sect_off),
9c541725 13262 objfile_name (objfile));
24c5c679
JK
13263 continue;
13264 }
96408a79
SA
13265 }
13266
216f72a1
JK
13267 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13268 if (attr == NULL)
13269 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13270 if (attr == NULL || !attr->form_is_block ())
96408a79 13271 {
b98664d3 13272 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13273 "DW_TAG_call_site child DIE %s [in module %s]"),
13274 sect_offset_str (child_die->sect_off),
9c541725 13275 objfile_name (objfile));
96408a79
SA
13276 continue;
13277 }
13278 parameter->value = DW_BLOCK (attr)->data;
13279 parameter->value_size = DW_BLOCK (attr)->size;
13280
13281 /* Parameters are not pre-cleared by memset above. */
13282 parameter->data_value = NULL;
13283 parameter->data_value_size = 0;
13284 call_site->parameter_count++;
13285
216f72a1
JK
13286 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13287 if (attr == NULL)
13288 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13289 if (attr != nullptr)
96408a79 13290 {
4fc6c0d5 13291 if (!attr->form_is_block ())
b98664d3 13292 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13293 "DW_TAG_call_site child DIE %s [in module %s]"),
13294 sect_offset_str (child_die->sect_off),
9c541725 13295 objfile_name (objfile));
96408a79
SA
13296 else
13297 {
13298 parameter->data_value = DW_BLOCK (attr)->data;
13299 parameter->data_value_size = DW_BLOCK (attr)->size;
13300 }
13301 }
13302 }
13303}
13304
71a3c369
TT
13305/* Helper function for read_variable. If DIE represents a virtual
13306 table, then return the type of the concrete object that is
13307 associated with the virtual table. Otherwise, return NULL. */
13308
13309static struct type *
13310rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13311{
13312 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13313 if (attr == NULL)
13314 return NULL;
13315
13316 /* Find the type DIE. */
13317 struct die_info *type_die = NULL;
13318 struct dwarf2_cu *type_cu = cu;
13319
cd6c91b4 13320 if (attr->form_is_ref ())
71a3c369
TT
13321 type_die = follow_die_ref (die, attr, &type_cu);
13322 if (type_die == NULL)
13323 return NULL;
13324
13325 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13326 return NULL;
13327 return die_containing_type (type_die, type_cu);
13328}
13329
13330/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13331
13332static void
13333read_variable (struct die_info *die, struct dwarf2_cu *cu)
13334{
13335 struct rust_vtable_symbol *storage = NULL;
13336
13337 if (cu->language == language_rust)
13338 {
13339 struct type *containing_type = rust_containing_type (die, cu);
13340
13341 if (containing_type != NULL)
13342 {
518817b3 13343 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13344
468c0cbb 13345 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13346 initialize_objfile_symbol (storage);
13347 storage->concrete_type = containing_type;
cf724bc9 13348 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13349 }
13350 }
13351
e4a62c65
TV
13352 struct symbol *res = new_symbol (die, NULL, cu, storage);
13353 struct attribute *abstract_origin
13354 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13355 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13356 if (res == NULL && loc && abstract_origin)
13357 {
13358 /* We have a variable without a name, but with a location and an abstract
13359 origin. This may be a concrete instance of an abstract variable
13360 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13361 later. */
13362 struct dwarf2_cu *origin_cu = cu;
13363 struct die_info *origin_die
13364 = follow_die_ref (die, abstract_origin, &origin_cu);
13365 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13366 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13367 }
71a3c369
TT
13368}
13369
43988095
JK
13370/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13371 reading .debug_rnglists.
13372 Callback's type should be:
13373 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13374 Return true if the attributes are present and valid, otherwise,
13375 return false. */
13376
13377template <typename Callback>
13378static bool
13379dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13380 Callback &&callback)
13381{
ed2dc618 13382 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13383 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13384 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13385 bfd *obfd = objfile->obfd;
43988095 13386 /* Base address selection entry. */
2b24b6e4 13387 gdb::optional<CORE_ADDR> base;
43988095 13388 const gdb_byte *buffer;
43988095
JK
13389 CORE_ADDR baseaddr;
13390 bool overflow = false;
13391
43988095
JK
13392 base = cu->base_address;
13393
96b79293 13394 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13395 if (offset >= dwarf2_per_objfile->rnglists.size)
13396 {
b98664d3 13397 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13398 offset);
13399 return false;
13400 }
13401 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13402
b3b3bada 13403 baseaddr = objfile->text_section_offset ();
43988095
JK
13404
13405 while (1)
13406 {
7814882a
JK
13407 /* Initialize it due to a false compiler warning. */
13408 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13409 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13410 + dwarf2_per_objfile->rnglists.size);
13411 unsigned int bytes_read;
13412
13413 if (buffer == buf_end)
13414 {
13415 overflow = true;
13416 break;
13417 }
13418 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13419 switch (rlet)
13420 {
13421 case DW_RLE_end_of_list:
13422 break;
13423 case DW_RLE_base_address:
13424 if (buffer + cu->header.addr_size > buf_end)
13425 {
13426 overflow = true;
13427 break;
13428 }
c8a7a66f 13429 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13430 buffer += bytes_read;
13431 break;
13432 case DW_RLE_start_length:
13433 if (buffer + cu->header.addr_size > buf_end)
13434 {
13435 overflow = true;
13436 break;
13437 }
c8a7a66f
TT
13438 range_beginning = cu->header.read_address (obfd, buffer,
13439 &bytes_read);
43988095
JK
13440 buffer += bytes_read;
13441 range_end = (range_beginning
13442 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13443 buffer += bytes_read;
13444 if (buffer > buf_end)
13445 {
13446 overflow = true;
13447 break;
13448 }
13449 break;
13450 case DW_RLE_offset_pair:
13451 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13452 buffer += bytes_read;
13453 if (buffer > buf_end)
13454 {
13455 overflow = true;
13456 break;
13457 }
13458 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13459 buffer += bytes_read;
13460 if (buffer > buf_end)
13461 {
13462 overflow = true;
13463 break;
13464 }
13465 break;
13466 case DW_RLE_start_end:
13467 if (buffer + 2 * cu->header.addr_size > buf_end)
13468 {
13469 overflow = true;
13470 break;
13471 }
c8a7a66f
TT
13472 range_beginning = cu->header.read_address (obfd, buffer,
13473 &bytes_read);
43988095 13474 buffer += bytes_read;
c8a7a66f 13475 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13476 buffer += bytes_read;
13477 break;
13478 default:
b98664d3 13479 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13480 return false;
13481 }
13482 if (rlet == DW_RLE_end_of_list || overflow)
13483 break;
13484 if (rlet == DW_RLE_base_address)
13485 continue;
13486
2b24b6e4 13487 if (!base.has_value ())
43988095
JK
13488 {
13489 /* We have no valid base address for the ranges
13490 data. */
b98664d3 13491 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13492 return false;
13493 }
13494
13495 if (range_beginning > range_end)
13496 {
13497 /* Inverted range entries are invalid. */
b98664d3 13498 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13499 return false;
13500 }
13501
13502 /* Empty range entries have no effect. */
13503 if (range_beginning == range_end)
13504 continue;
13505
2b24b6e4
TT
13506 range_beginning += *base;
13507 range_end += *base;
43988095
JK
13508
13509 /* A not-uncommon case of bad debug info.
13510 Don't pollute the addrmap with bad data. */
13511 if (range_beginning + baseaddr == 0
13512 && !dwarf2_per_objfile->has_section_at_zero)
13513 {
b98664d3 13514 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13515 " [in module %s]"), objfile_name (objfile));
13516 continue;
13517 }
13518
13519 callback (range_beginning, range_end);
13520 }
13521
13522 if (overflow)
13523 {
b98664d3 13524 complaint (_("Offset %d is not terminated "
43988095
JK
13525 "for DW_AT_ranges attribute"),
13526 offset);
13527 return false;
13528 }
13529
13530 return true;
13531}
13532
13533/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13534 Callback's type should be:
13535 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13536 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13537
43988095 13538template <typename Callback>
43039443 13539static int
5f46c5a5 13540dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13541 Callback &&callback)
43039443 13542{
ed2dc618 13543 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13544 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13545 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13546 struct comp_unit_head *cu_header = &cu->header;
13547 bfd *obfd = objfile->obfd;
13548 unsigned int addr_size = cu_header->addr_size;
13549 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13550 /* Base address selection entry. */
2b24b6e4 13551 gdb::optional<CORE_ADDR> base;
43039443 13552 unsigned int dummy;
d521ce57 13553 const gdb_byte *buffer;
ff013f42 13554 CORE_ADDR baseaddr;
43039443 13555
43988095
JK
13556 if (cu_header->version >= 5)
13557 return dwarf2_rnglists_process (offset, cu, callback);
13558
d00adf39 13559 base = cu->base_address;
43039443 13560
96b79293 13561 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13562 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13563 {
b98664d3 13564 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13565 offset);
13566 return 0;
13567 }
dce234bc 13568 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13569
b3b3bada 13570 baseaddr = objfile->text_section_offset ();
ff013f42 13571
43039443
JK
13572 while (1)
13573 {
13574 CORE_ADDR range_beginning, range_end;
13575
c8a7a66f 13576 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13577 buffer += addr_size;
c8a7a66f 13578 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13579 buffer += addr_size;
13580 offset += 2 * addr_size;
13581
13582 /* An end of list marker is a pair of zero addresses. */
13583 if (range_beginning == 0 && range_end == 0)
13584 /* Found the end of list entry. */
13585 break;
13586
13587 /* Each base address selection entry is a pair of 2 values.
13588 The first is the largest possible address, the second is
13589 the base address. Check for a base address here. */
13590 if ((range_beginning & mask) == mask)
13591 {
28d2bfb9
AB
13592 /* If we found the largest possible address, then we already
13593 have the base address in range_end. */
13594 base = range_end;
43039443
JK
13595 continue;
13596 }
13597
2b24b6e4 13598 if (!base.has_value ())
43039443
JK
13599 {
13600 /* We have no valid base address for the ranges
13601 data. */
b98664d3 13602 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13603 return 0;
13604 }
13605
9277c30c
UW
13606 if (range_beginning > range_end)
13607 {
13608 /* Inverted range entries are invalid. */
b98664d3 13609 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13610 return 0;
13611 }
13612
13613 /* Empty range entries have no effect. */
13614 if (range_beginning == range_end)
13615 continue;
13616
2b24b6e4
TT
13617 range_beginning += *base;
13618 range_end += *base;
43039443 13619
01093045
DE
13620 /* A not-uncommon case of bad debug info.
13621 Don't pollute the addrmap with bad data. */
13622 if (range_beginning + baseaddr == 0
13623 && !dwarf2_per_objfile->has_section_at_zero)
13624 {
b98664d3 13625 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13626 " [in module %s]"), objfile_name (objfile));
01093045
DE
13627 continue;
13628 }
13629
5f46c5a5
JK
13630 callback (range_beginning, range_end);
13631 }
13632
13633 return 1;
13634}
13635
13636/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13637 Return 1 if the attributes are present and valid, otherwise, return 0.
13638 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13639
13640static int
13641dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13642 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13643 dwarf2_psymtab *ranges_pst)
5f46c5a5 13644{
518817b3 13645 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13646 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13647 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13648 int low_set = 0;
13649 CORE_ADDR low = 0;
13650 CORE_ADDR high = 0;
13651 int retval;
13652
13653 retval = dwarf2_ranges_process (offset, cu,
13654 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13655 {
9277c30c 13656 if (ranges_pst != NULL)
3e29f34a
MR
13657 {
13658 CORE_ADDR lowpc;
13659 CORE_ADDR highpc;
13660
79748972
TT
13661 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13662 range_beginning + baseaddr)
13663 - baseaddr);
13664 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13665 range_end + baseaddr)
13666 - baseaddr);
d320c2b5
TT
13667 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13668 lowpc, highpc - 1, ranges_pst);
3e29f34a 13669 }
ff013f42 13670
43039443
JK
13671 /* FIXME: This is recording everything as a low-high
13672 segment of consecutive addresses. We should have a
13673 data structure for discontiguous block ranges
13674 instead. */
13675 if (! low_set)
13676 {
13677 low = range_beginning;
13678 high = range_end;
13679 low_set = 1;
13680 }
13681 else
13682 {
13683 if (range_beginning < low)
13684 low = range_beginning;
13685 if (range_end > high)
13686 high = range_end;
13687 }
5f46c5a5
JK
13688 });
13689 if (!retval)
13690 return 0;
43039443
JK
13691
13692 if (! low_set)
13693 /* If the first entry is an end-of-list marker, the range
13694 describes an empty scope, i.e. no instructions. */
13695 return 0;
13696
13697 if (low_return)
13698 *low_return = low;
13699 if (high_return)
13700 *high_return = high;
13701 return 1;
13702}
13703
3a2b436a
JK
13704/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13705 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13706 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13707
3a2b436a 13708static enum pc_bounds_kind
af34e669 13709dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13710 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13711 dwarf2_psymtab *pst)
c906108c 13712{
518817b3
SM
13713 struct dwarf2_per_objfile *dwarf2_per_objfile
13714 = cu->per_cu->dwarf2_per_objfile;
c906108c 13715 struct attribute *attr;
91da1414 13716 struct attribute *attr_high;
af34e669
DJ
13717 CORE_ADDR low = 0;
13718 CORE_ADDR high = 0;
e385593e 13719 enum pc_bounds_kind ret;
c906108c 13720
91da1414
MW
13721 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13722 if (attr_high)
af34e669 13723 {
e142c38c 13724 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13725 if (attr != nullptr)
91da1414 13726 {
cd6c91b4
TT
13727 low = attr->value_as_address ();
13728 high = attr_high->value_as_address ();
13729 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13730 high += low;
91da1414 13731 }
af34e669
DJ
13732 else
13733 /* Found high w/o low attribute. */
e385593e 13734 return PC_BOUNDS_INVALID;
af34e669
DJ
13735
13736 /* Found consecutive range of addresses. */
3a2b436a 13737 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13738 }
c906108c 13739 else
af34e669 13740 {
e142c38c 13741 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13742 if (attr != NULL)
13743 {
18a8505e 13744 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13745 We take advantage of the fact that DW_AT_ranges does not appear
13746 in DW_TAG_compile_unit of DWO files. */
13747 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13748 unsigned int ranges_offset = (DW_UNSND (attr)
13749 + (need_ranges_base
13750 ? cu->ranges_base
13751 : 0));
2e3cf129 13752
af34e669 13753 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13754 .debug_ranges section. */
2e3cf129 13755 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13756 return PC_BOUNDS_INVALID;
43039443 13757 /* Found discontinuous range of addresses. */
3a2b436a 13758 ret = PC_BOUNDS_RANGES;
af34e669 13759 }
e385593e
JK
13760 else
13761 return PC_BOUNDS_NOT_PRESENT;
af34e669 13762 }
c906108c 13763
48fbe735 13764 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13765 if (high <= low)
e385593e 13766 return PC_BOUNDS_INVALID;
c906108c
SS
13767
13768 /* When using the GNU linker, .gnu.linkonce. sections are used to
13769 eliminate duplicate copies of functions and vtables and such.
13770 The linker will arbitrarily choose one and discard the others.
13771 The AT_*_pc values for such functions refer to local labels in
13772 these sections. If the section from that file was discarded, the
13773 labels are not in the output, so the relocs get a value of 0.
13774 If this is a discarded function, mark the pc bounds as invalid,
13775 so that GDB will ignore it. */
72dca2f5 13776 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13777 return PC_BOUNDS_INVALID;
c906108c
SS
13778
13779 *lowpc = low;
96408a79
SA
13780 if (highpc)
13781 *highpc = high;
af34e669 13782 return ret;
c906108c
SS
13783}
13784
b084d499
JB
13785/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13786 its low and high PC addresses. Do nothing if these addresses could not
13787 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13788 and HIGHPC to the high address if greater than HIGHPC. */
13789
13790static void
13791dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13792 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13793 struct dwarf2_cu *cu)
13794{
13795 CORE_ADDR low, high;
13796 struct die_info *child = die->child;
13797
e385593e 13798 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13799 {
325fac50
PA
13800 *lowpc = std::min (*lowpc, low);
13801 *highpc = std::max (*highpc, high);
b084d499
JB
13802 }
13803
13804 /* If the language does not allow nested subprograms (either inside
13805 subprograms or lexical blocks), we're done. */
13806 if (cu->language != language_ada)
13807 return;
6e70227d 13808
b084d499
JB
13809 /* Check all the children of the given DIE. If it contains nested
13810 subprograms, then check their pc bounds. Likewise, we need to
13811 check lexical blocks as well, as they may also contain subprogram
13812 definitions. */
13813 while (child && child->tag)
13814 {
13815 if (child->tag == DW_TAG_subprogram
13816 || child->tag == DW_TAG_lexical_block)
13817 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 13818 child = child->sibling;
b084d499
JB
13819 }
13820}
13821
fae299cd
DC
13822/* Get the low and high pc's represented by the scope DIE, and store
13823 them in *LOWPC and *HIGHPC. If the correct values can't be
13824 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13825
13826static void
13827get_scope_pc_bounds (struct die_info *die,
13828 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13829 struct dwarf2_cu *cu)
13830{
13831 CORE_ADDR best_low = (CORE_ADDR) -1;
13832 CORE_ADDR best_high = (CORE_ADDR) 0;
13833 CORE_ADDR current_low, current_high;
13834
3a2b436a 13835 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13836 >= PC_BOUNDS_RANGES)
fae299cd
DC
13837 {
13838 best_low = current_low;
13839 best_high = current_high;
13840 }
13841 else
13842 {
13843 struct die_info *child = die->child;
13844
13845 while (child && child->tag)
13846 {
13847 switch (child->tag) {
13848 case DW_TAG_subprogram:
b084d499 13849 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13850 break;
13851 case DW_TAG_namespace:
f55ee35c 13852 case DW_TAG_module:
fae299cd
DC
13853 /* FIXME: carlton/2004-01-16: Should we do this for
13854 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13855 that current GCC's always emit the DIEs corresponding
13856 to definitions of methods of classes as children of a
13857 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13858 the DIEs giving the declarations, which could be
13859 anywhere). But I don't see any reason why the
13860 standards says that they have to be there. */
13861 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13862
13863 if (current_low != ((CORE_ADDR) -1))
13864 {
325fac50
PA
13865 best_low = std::min (best_low, current_low);
13866 best_high = std::max (best_high, current_high);
fae299cd
DC
13867 }
13868 break;
13869 default:
0963b4bd 13870 /* Ignore. */
fae299cd
DC
13871 break;
13872 }
13873
436c571c 13874 child = child->sibling;
fae299cd
DC
13875 }
13876 }
13877
13878 *lowpc = best_low;
13879 *highpc = best_high;
13880}
13881
801e3a5b
JB
13882/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13883 in DIE. */
380bca97 13884
801e3a5b
JB
13885static void
13886dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13887 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13888{
518817b3 13889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13890 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13891 struct attribute *attr;
91da1414 13892 struct attribute *attr_high;
801e3a5b 13893
91da1414
MW
13894 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13895 if (attr_high)
801e3a5b 13896 {
801e3a5b 13897 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13898 if (attr != nullptr)
801e3a5b 13899 {
cd6c91b4
TT
13900 CORE_ADDR low = attr->value_as_address ();
13901 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 13902
cd6c91b4 13903 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13904 high += low;
9a619af0 13905
3e29f34a
MR
13906 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13907 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 13908 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
13909 }
13910 }
13911
13912 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 13913 if (attr != nullptr)
801e3a5b 13914 {
18a8505e 13915 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13916 We take advantage of the fact that DW_AT_ranges does not appear
13917 in DW_TAG_compile_unit of DWO files. */
13918 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13919
13920 /* The value of the DW_AT_ranges attribute is the offset of the
13921 address range list in the .debug_ranges section. */
ab435259
DE
13922 unsigned long offset = (DW_UNSND (attr)
13923 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 13924
2d5f09ec 13925 std::vector<blockrange> blockvec;
5f46c5a5
JK
13926 dwarf2_ranges_process (offset, cu,
13927 [&] (CORE_ADDR start, CORE_ADDR end)
13928 {
58fdfd2c
JK
13929 start += baseaddr;
13930 end += baseaddr;
5f46c5a5
JK
13931 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13932 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 13933 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 13934 blockvec.emplace_back (start, end);
5f46c5a5 13935 });
2d5f09ec
KB
13936
13937 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
13938 }
13939}
13940
685b1105
JK
13941/* Check whether the producer field indicates either of GCC < 4.6, or the
13942 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 13943
685b1105
JK
13944static void
13945check_producer (struct dwarf2_cu *cu)
60d5a603 13946{
38360086 13947 int major, minor;
60d5a603
JK
13948
13949 if (cu->producer == NULL)
13950 {
13951 /* For unknown compilers expect their behavior is DWARF version
13952 compliant.
13953
13954 GCC started to support .debug_types sections by -gdwarf-4 since
13955 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13956 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13957 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13958 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 13959 }
b1ffba5a 13960 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 13961 {
38360086
MW
13962 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13963 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 13964 }
5230b05a 13965 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
13966 {
13967 cu->producer_is_icc = true;
13968 cu->producer_is_icc_lt_14 = major < 14;
13969 }
c258c396
JD
13970 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13971 cu->producer_is_codewarrior = true;
685b1105
JK
13972 else
13973 {
13974 /* For other non-GCC compilers, expect their behavior is DWARF version
13975 compliant. */
60d5a603
JK
13976 }
13977
9068261f 13978 cu->checked_producer = true;
685b1105 13979}
ba919b58 13980
685b1105
JK
13981/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13982 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13983 during 4.6.0 experimental. */
13984
9068261f 13985static bool
685b1105
JK
13986producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13987{
13988 if (!cu->checked_producer)
13989 check_producer (cu);
13990
13991 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
13992}
13993
c258c396
JD
13994
13995/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13996 with incorrect is_stmt attributes. */
13997
13998static bool
13999producer_is_codewarrior (struct dwarf2_cu *cu)
14000{
14001 if (!cu->checked_producer)
14002 check_producer (cu);
14003
14004 return cu->producer_is_codewarrior;
14005}
14006
405feb71 14007/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14008 DW_AT_accessibility. */
14009
14010static enum dwarf_access_attribute
14011dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14012{
14013 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14014 {
14015 /* The default DWARF 2 accessibility for members is public, the default
14016 accessibility for inheritance is private. */
14017
14018 if (die->tag != DW_TAG_inheritance)
14019 return DW_ACCESS_public;
14020 else
14021 return DW_ACCESS_private;
14022 }
14023 else
14024 {
14025 /* DWARF 3+ defines the default accessibility a different way. The same
14026 rules apply now for DW_TAG_inheritance as for the members and it only
14027 depends on the container kind. */
14028
14029 if (die->parent->tag == DW_TAG_class_type)
14030 return DW_ACCESS_private;
14031 else
14032 return DW_ACCESS_public;
14033 }
14034}
14035
74ac6d43
TT
14036/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14037 offset. If the attribute was not found return 0, otherwise return
14038 1. If it was found but could not properly be handled, set *OFFSET
14039 to 0. */
14040
14041static int
14042handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14043 LONGEST *offset)
14044{
14045 struct attribute *attr;
14046
14047 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14048 if (attr != NULL)
14049 {
14050 *offset = 0;
14051
14052 /* Note that we do not check for a section offset first here.
14053 This is because DW_AT_data_member_location is new in DWARF 4,
14054 so if we see it, we can assume that a constant form is really
14055 a constant and not a section offset. */
cd6c91b4 14056 if (attr->form_is_constant ())
0826b30a 14057 *offset = attr->constant_value (0);
cd6c91b4 14058 else if (attr->form_is_section_offset ())
74ac6d43 14059 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14060 else if (attr->form_is_block ())
74ac6d43
TT
14061 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14062 else
14063 dwarf2_complex_location_expr_complaint ();
14064
14065 return 1;
14066 }
14067
14068 return 0;
14069}
14070
c906108c
SS
14071/* Add an aggregate field to the field list. */
14072
14073static void
107d2387 14074dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14075 struct dwarf2_cu *cu)
6e70227d 14076{
518817b3 14077 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14078 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14079 struct nextfield *new_field;
14080 struct attribute *attr;
14081 struct field *fp;
15d034d0 14082 const char *fieldname = "";
c906108c 14083
7d0ccb61
DJ
14084 if (die->tag == DW_TAG_inheritance)
14085 {
be2daae6
TT
14086 fip->baseclasses.emplace_back ();
14087 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14088 }
14089 else
14090 {
be2daae6
TT
14091 fip->fields.emplace_back ();
14092 new_field = &fip->fields.back ();
7d0ccb61 14093 }
be2daae6 14094
e142c38c 14095 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14096 if (attr != nullptr)
c906108c 14097 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14098 else
14099 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14100 if (new_field->accessibility != DW_ACCESS_public)
14101 fip->non_public_fields = 1;
60d5a603 14102
e142c38c 14103 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14104 if (attr != nullptr)
c906108c 14105 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14106 else
14107 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14108
14109 fp = &new_field->field;
a9a9bd0f 14110
e142c38c 14111 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14112 {
74ac6d43
TT
14113 LONGEST offset;
14114
a9a9bd0f 14115 /* Data member other than a C++ static data member. */
6e70227d 14116
c906108c 14117 /* Get type of field. */
e7c27a73 14118 fp->type = die_type (die, cu);
c906108c 14119
d6a843b5 14120 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14121
c906108c 14122 /* Get bit size of field (zero if none). */
e142c38c 14123 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14124 if (attr != nullptr)
c906108c
SS
14125 {
14126 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14127 }
14128 else
14129 {
14130 FIELD_BITSIZE (*fp) = 0;
14131 }
14132
14133 /* Get bit offset of field. */
74ac6d43
TT
14134 if (handle_data_member_location (die, cu, &offset))
14135 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14136 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14137 if (attr != nullptr)
c906108c 14138 {
d5a22e77 14139 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14140 {
14141 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14142 additional bit offset from the MSB of the containing
14143 anonymous object to the MSB of the field. We don't
14144 have to do anything special since we don't need to
14145 know the size of the anonymous object. */
f41f5e61 14146 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14147 }
14148 else
14149 {
14150 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14151 MSB of the anonymous object, subtract off the number of
14152 bits from the MSB of the field to the MSB of the
14153 object, and then subtract off the number of bits of
14154 the field itself. The result is the bit offset of
14155 the LSB of the field. */
c906108c
SS
14156 int anonymous_size;
14157 int bit_offset = DW_UNSND (attr);
14158
e142c38c 14159 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14160 if (attr != nullptr)
c906108c
SS
14161 {
14162 /* The size of the anonymous object containing
14163 the bit field is explicit, so use the
14164 indicated size (in bytes). */
14165 anonymous_size = DW_UNSND (attr);
14166 }
14167 else
14168 {
14169 /* The size of the anonymous object containing
14170 the bit field must be inferred from the type
14171 attribute of the data member containing the
14172 bit field. */
14173 anonymous_size = TYPE_LENGTH (fp->type);
14174 }
f41f5e61
PA
14175 SET_FIELD_BITPOS (*fp,
14176 (FIELD_BITPOS (*fp)
14177 + anonymous_size * bits_per_byte
14178 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14179 }
14180 }
da5b30da
AA
14181 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14182 if (attr != NULL)
14183 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14184 + attr->constant_value (0)));
c906108c
SS
14185
14186 /* Get name of field. */
39cbfefa
DJ
14187 fieldname = dwarf2_name (die, cu);
14188 if (fieldname == NULL)
14189 fieldname = "";
d8151005
DJ
14190
14191 /* The name is already allocated along with this objfile, so we don't
14192 need to duplicate it for the type. */
14193 fp->name = fieldname;
c906108c
SS
14194
14195 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14196 pointer or virtual base class pointer) to private. */
e142c38c 14197 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14198 {
d48cc9dd 14199 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14200 new_field->accessibility = DW_ACCESS_private;
14201 fip->non_public_fields = 1;
14202 }
14203 }
a9a9bd0f 14204 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14205 {
a9a9bd0f
DC
14206 /* C++ static member. */
14207
14208 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14209 is a declaration, but all versions of G++ as of this writing
14210 (so through at least 3.2.1) incorrectly generate
14211 DW_TAG_variable tags. */
6e70227d 14212
ff355380 14213 const char *physname;
c906108c 14214
a9a9bd0f 14215 /* Get name of field. */
39cbfefa
DJ
14216 fieldname = dwarf2_name (die, cu);
14217 if (fieldname == NULL)
c906108c
SS
14218 return;
14219
254e6b9e 14220 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14221 if (attr
14222 /* Only create a symbol if this is an external value.
14223 new_symbol checks this and puts the value in the global symbol
14224 table, which we want. If it is not external, new_symbol
14225 will try to put the value in cu->list_in_scope which is wrong. */
14226 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14227 {
14228 /* A static const member, not much different than an enum as far as
14229 we're concerned, except that we can support more types. */
14230 new_symbol (die, NULL, cu);
14231 }
14232
2df3850c 14233 /* Get physical name. */
ff355380 14234 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14235
d8151005
DJ
14236 /* The name is already allocated along with this objfile, so we don't
14237 need to duplicate it for the type. */
14238 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14239 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14240 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14241 }
14242 else if (die->tag == DW_TAG_inheritance)
14243 {
74ac6d43 14244 LONGEST offset;
d4b96c9a 14245
74ac6d43
TT
14246 /* C++ base class field. */
14247 if (handle_data_member_location (die, cu, &offset))
14248 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14249 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14250 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14251 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14252 }
2ddeaf8a
TT
14253 else if (die->tag == DW_TAG_variant_part)
14254 {
14255 /* process_structure_scope will treat this DIE as a union. */
14256 process_structure_scope (die, cu);
14257
14258 /* The variant part is relative to the start of the enclosing
14259 structure. */
14260 SET_FIELD_BITPOS (*fp, 0);
14261 fp->type = get_die_type (die, cu);
14262 fp->artificial = 1;
14263 fp->name = "<<variant>>";
c8c81635
TT
14264
14265 /* Normally a DW_TAG_variant_part won't have a size, but our
14266 representation requires one, so set it to the maximum of the
489dbda6
TT
14267 child sizes, being sure to account for the offset at which
14268 each child is seen. */
c8c81635
TT
14269 if (TYPE_LENGTH (fp->type) == 0)
14270 {
14271 unsigned max = 0;
14272 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14273 {
14274 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14275 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14276 if (len > max)
14277 max = len;
14278 }
c8c81635
TT
14279 TYPE_LENGTH (fp->type) = max;
14280 }
2ddeaf8a
TT
14281 }
14282 else
14283 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14284}
14285
883fd55a
KS
14286/* Can the type given by DIE define another type? */
14287
14288static bool
14289type_can_define_types (const struct die_info *die)
14290{
14291 switch (die->tag)
14292 {
14293 case DW_TAG_typedef:
14294 case DW_TAG_class_type:
14295 case DW_TAG_structure_type:
14296 case DW_TAG_union_type:
14297 case DW_TAG_enumeration_type:
14298 return true;
14299
14300 default:
14301 return false;
14302 }
14303}
14304
14305/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14306
14307static void
883fd55a
KS
14308dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14309 struct dwarf2_cu *cu)
6e70227d 14310{
be2daae6
TT
14311 struct decl_field fp;
14312 memset (&fp, 0, sizeof (fp));
98751a41 14313
883fd55a 14314 gdb_assert (type_can_define_types (die));
98751a41 14315
883fd55a 14316 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14317 fp.name = dwarf2_name (die, cu);
14318 fp.type = read_type_die (die, cu);
98751a41 14319
c191a687
KS
14320 /* Save accessibility. */
14321 enum dwarf_access_attribute accessibility;
14322 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14323 if (attr != NULL)
14324 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14325 else
14326 accessibility = dwarf2_default_access_attribute (die, cu);
14327 switch (accessibility)
14328 {
14329 case DW_ACCESS_public:
14330 /* The assumed value if neither private nor protected. */
14331 break;
14332 case DW_ACCESS_private:
be2daae6 14333 fp.is_private = 1;
c191a687
KS
14334 break;
14335 case DW_ACCESS_protected:
be2daae6 14336 fp.is_protected = 1;
c191a687
KS
14337 break;
14338 default:
b98664d3 14339 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14340 }
14341
883fd55a 14342 if (die->tag == DW_TAG_typedef)
be2daae6 14343 fip->typedef_field_list.push_back (fp);
883fd55a 14344 else
be2daae6 14345 fip->nested_types_list.push_back (fp);
98751a41
JK
14346}
14347
c906108c
SS
14348/* Create the vector of fields, and attach it to the type. */
14349
14350static void
fba45db2 14351dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14352 struct dwarf2_cu *cu)
c906108c 14353{
317f7127 14354 int nfields = fip->nfields ();
c906108c
SS
14355
14356 /* Record the field count, allocate space for the array of fields,
14357 and create blank accessibility bitfields if necessary. */
14358 TYPE_NFIELDS (type) = nfields;
14359 TYPE_FIELDS (type) = (struct field *)
be2daae6 14360 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14361
b4ba55a1 14362 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14363 {
14364 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14365
14366 TYPE_FIELD_PRIVATE_BITS (type) =
14367 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14368 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14369
14370 TYPE_FIELD_PROTECTED_BITS (type) =
14371 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14372 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14373
774b6a14
TT
14374 TYPE_FIELD_IGNORE_BITS (type) =
14375 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14376 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14377 }
14378
14379 /* If the type has baseclasses, allocate and clear a bit vector for
14380 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14381 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14382 {
be2daae6 14383 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14384 unsigned char *pointer;
c906108c
SS
14385
14386 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14387 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14388 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14389 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14390 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14391 }
14392
2ddeaf8a
TT
14393 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14394 {
14395 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14396
be2daae6 14397 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14398 {
be2daae6
TT
14399 struct nextfield &field = fip->fields[index];
14400
14401 if (field.variant.is_discriminant)
2ddeaf8a 14402 di->discriminant_index = index;
be2daae6 14403 else if (field.variant.default_branch)
2ddeaf8a
TT
14404 di->default_index = index;
14405 else
be2daae6 14406 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14407 }
14408 }
14409
be2daae6
TT
14410 /* Copy the saved-up fields into the field vector. */
14411 for (int i = 0; i < nfields; ++i)
c906108c 14412 {
be2daae6
TT
14413 struct nextfield &field
14414 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14415 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14416
be2daae6
TT
14417 TYPE_FIELD (type, i) = field.field;
14418 switch (field.accessibility)
c906108c 14419 {
c5aa993b 14420 case DW_ACCESS_private:
b4ba55a1 14421 if (cu->language != language_ada)
be2daae6 14422 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14423 break;
c906108c 14424
c5aa993b 14425 case DW_ACCESS_protected:
b4ba55a1 14426 if (cu->language != language_ada)
be2daae6 14427 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14428 break;
c906108c 14429
c5aa993b
JM
14430 case DW_ACCESS_public:
14431 break;
c906108c 14432
c5aa993b
JM
14433 default:
14434 /* Unknown accessibility. Complain and treat it as public. */
14435 {
b98664d3 14436 complaint (_("unsupported accessibility %d"),
be2daae6 14437 field.accessibility);
c5aa993b
JM
14438 }
14439 break;
c906108c 14440 }
be2daae6 14441 if (i < fip->baseclasses.size ())
c906108c 14442 {
be2daae6 14443 switch (field.virtuality)
c906108c 14444 {
c5aa993b
JM
14445 case DW_VIRTUALITY_virtual:
14446 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14447 if (cu->language == language_ada)
a73c6dcd 14448 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14449 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14450 break;
c906108c
SS
14451 }
14452 }
c906108c
SS
14453 }
14454}
14455
7d27a96d
TT
14456/* Return true if this member function is a constructor, false
14457 otherwise. */
14458
14459static int
14460dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14461{
14462 const char *fieldname;
fe978cb0 14463 const char *type_name;
7d27a96d
TT
14464 int len;
14465
14466 if (die->parent == NULL)
14467 return 0;
14468
14469 if (die->parent->tag != DW_TAG_structure_type
14470 && die->parent->tag != DW_TAG_union_type
14471 && die->parent->tag != DW_TAG_class_type)
14472 return 0;
14473
14474 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14475 type_name = dwarf2_name (die->parent, cu);
14476 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14477 return 0;
14478
14479 len = strlen (fieldname);
fe978cb0
PA
14480 return (strncmp (fieldname, type_name, len) == 0
14481 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14482}
14483
e35000a7
TBA
14484/* Check if the given VALUE is a recognized enum
14485 dwarf_defaulted_attribute constant according to DWARF5 spec,
14486 Table 7.24. */
14487
14488static bool
14489is_valid_DW_AT_defaulted (ULONGEST value)
14490{
14491 switch (value)
14492 {
14493 case DW_DEFAULTED_no:
14494 case DW_DEFAULTED_in_class:
14495 case DW_DEFAULTED_out_of_class:
14496 return true;
14497 }
14498
3142e908 14499 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14500 return false;
14501}
14502
c906108c
SS
14503/* Add a member function to the proper fieldlist. */
14504
14505static void
107d2387 14506dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14507 struct type *type, struct dwarf2_cu *cu)
c906108c 14508{
518817b3 14509 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14510 struct attribute *attr;
c906108c 14511 int i;
be2daae6 14512 struct fnfieldlist *flp = nullptr;
c906108c 14513 struct fn_field *fnp;
15d034d0 14514 const char *fieldname;
f792889a 14515 struct type *this_type;
60d5a603 14516 enum dwarf_access_attribute accessibility;
c906108c 14517
b4ba55a1 14518 if (cu->language == language_ada)
a73c6dcd 14519 error (_("unexpected member function in Ada type"));
b4ba55a1 14520
2df3850c 14521 /* Get name of member function. */
39cbfefa
DJ
14522 fieldname = dwarf2_name (die, cu);
14523 if (fieldname == NULL)
2df3850c 14524 return;
c906108c 14525
c906108c 14526 /* Look up member function name in fieldlist. */
be2daae6 14527 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14528 {
27bfe10e 14529 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14530 {
14531 flp = &fip->fnfieldlists[i];
14532 break;
14533 }
c906108c
SS
14534 }
14535
be2daae6
TT
14536 /* Create a new fnfieldlist if necessary. */
14537 if (flp == nullptr)
c906108c 14538 {
be2daae6
TT
14539 fip->fnfieldlists.emplace_back ();
14540 flp = &fip->fnfieldlists.back ();
c906108c 14541 flp->name = fieldname;
be2daae6 14542 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14543 }
14544
be2daae6
TT
14545 /* Create a new member function field and add it to the vector of
14546 fnfieldlists. */
14547 flp->fnfields.emplace_back ();
14548 fnp = &flp->fnfields.back ();
3da10d80
KS
14549
14550 /* Delay processing of the physname until later. */
9c37b5ae 14551 if (cu->language == language_cplus)
be2daae6
TT
14552 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14553 die, cu);
3da10d80
KS
14554 else
14555 {
1d06ead6 14556 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14557 fnp->physname = physname ? physname : "";
14558 }
14559
c906108c 14560 fnp->type = alloc_type (objfile);
f792889a
DJ
14561 this_type = read_type_die (die, cu);
14562 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14563 {
f792889a 14564 int nparams = TYPE_NFIELDS (this_type);
c906108c 14565
f792889a 14566 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14567 of the method itself (TYPE_CODE_METHOD). */
14568 smash_to_method_type (fnp->type, type,
f792889a
DJ
14569 TYPE_TARGET_TYPE (this_type),
14570 TYPE_FIELDS (this_type),
14571 TYPE_NFIELDS (this_type),
14572 TYPE_VARARGS (this_type));
c906108c
SS
14573
14574 /* Handle static member functions.
c5aa993b 14575 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14576 member functions. G++ helps GDB by marking the first
14577 parameter for non-static member functions (which is the this
14578 pointer) as artificial. We obtain this information from
14579 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14580 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14581 fnp->voffset = VOFFSET_STATIC;
14582 }
14583 else
b98664d3 14584 complaint (_("member function type missing for '%s'"),
3da10d80 14585 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14586
14587 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14588 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14589 fnp->fcontext = die_containing_type (die, cu);
c906108c 14590
3e43a32a
MS
14591 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14592 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14593
14594 /* Get accessibility. */
e142c38c 14595 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14596 if (attr != nullptr)
aead7601 14597 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14598 else
14599 accessibility = dwarf2_default_access_attribute (die, cu);
14600 switch (accessibility)
c906108c 14601 {
60d5a603
JK
14602 case DW_ACCESS_private:
14603 fnp->is_private = 1;
14604 break;
14605 case DW_ACCESS_protected:
14606 fnp->is_protected = 1;
14607 break;
c906108c
SS
14608 }
14609
b02dede2 14610 /* Check for artificial methods. */
e142c38c 14611 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14612 if (attr && DW_UNSND (attr) != 0)
14613 fnp->is_artificial = 1;
14614
e35000a7
TBA
14615 /* Check for defaulted methods. */
14616 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14617 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14618 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14619
14620 /* Check for deleted methods. */
14621 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14622 if (attr != nullptr && DW_UNSND (attr) != 0)
14623 fnp->is_deleted = 1;
14624
7d27a96d
TT
14625 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14626
0d564a31 14627 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14628 function. For older versions of GCC, this is an offset in the
14629 appropriate virtual table, as specified by DW_AT_containing_type.
14630 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14631 to the object address. */
14632
e142c38c 14633 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14634 if (attr != nullptr)
8e19ed76 14635 {
4fc6c0d5 14636 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14637 {
aec5aa8b
TT
14638 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14639 {
14640 /* Old-style GCC. */
14641 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14642 }
14643 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14644 || (DW_BLOCK (attr)->size > 1
14645 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14646 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14647 {
aec5aa8b
TT
14648 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14649 if ((fnp->voffset % cu->header.addr_size) != 0)
14650 dwarf2_complex_location_expr_complaint ();
14651 else
14652 fnp->voffset /= cu->header.addr_size;
14653 fnp->voffset += 2;
14654 }
14655 else
14656 dwarf2_complex_location_expr_complaint ();
14657
14658 if (!fnp->fcontext)
7e993ebf
KS
14659 {
14660 /* If there is no `this' field and no DW_AT_containing_type,
14661 we cannot actually find a base class context for the
14662 vtable! */
14663 if (TYPE_NFIELDS (this_type) == 0
14664 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14665 {
b98664d3 14666 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14667 "function \"%s\" (offset %s)"),
14668 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14669 }
14670 else
14671 {
14672 fnp->fcontext
14673 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14674 }
14675 }
aec5aa8b 14676 }
cd6c91b4 14677 else if (attr->form_is_section_offset ())
8e19ed76 14678 {
4d3c2250 14679 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14680 }
14681 else
14682 {
4d3c2250
KB
14683 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14684 fieldname);
8e19ed76 14685 }
0d564a31 14686 }
d48cc9dd
DJ
14687 else
14688 {
14689 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14690 if (attr && DW_UNSND (attr))
14691 {
14692 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14693 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14694 "but the vtable offset is not specified"),
9d8780f0 14695 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14696 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14697 TYPE_CPLUS_DYNAMIC (type) = 1;
14698 }
14699 }
c906108c
SS
14700}
14701
14702/* Create the vector of member function fields, and attach it to the type. */
14703
14704static void
fba45db2 14705dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14706 struct dwarf2_cu *cu)
c906108c 14707{
b4ba55a1 14708 if (cu->language == language_ada)
a73c6dcd 14709 error (_("unexpected member functions in Ada type"));
b4ba55a1 14710
c906108c
SS
14711 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14712 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14713 TYPE_ALLOC (type,
14714 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14715
be2daae6 14716 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14717 {
be2daae6 14718 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14719 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14720
be2daae6
TT
14721 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14722 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14723 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14724 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14725
14726 for (int k = 0; k < nf.fnfields.size (); ++k)
14727 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14728 }
14729
be2daae6 14730 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14731}
14732
1168df01
JB
14733/* Returns non-zero if NAME is the name of a vtable member in CU's
14734 language, zero otherwise. */
14735static int
14736is_vtable_name (const char *name, struct dwarf2_cu *cu)
14737{
14738 static const char vptr[] = "_vptr";
14739
9c37b5ae
TT
14740 /* Look for the C++ form of the vtable. */
14741 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14742 return 1;
14743
14744 return 0;
14745}
14746
c0dd20ea 14747/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14748 functions, with the ABI-specified layout. If TYPE describes
14749 such a structure, smash it into a member function type.
61049d3b
DJ
14750
14751 GCC shouldn't do this; it should just output pointer to member DIEs.
14752 This is GCC PR debug/28767. */
c0dd20ea 14753
0b92b5bb
TT
14754static void
14755quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14756{
09e2d7c7 14757 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14758
14759 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14760 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14761 return;
c0dd20ea
DJ
14762
14763 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14764 if (TYPE_FIELD_NAME (type, 0) == NULL
14765 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14766 || TYPE_FIELD_NAME (type, 1) == NULL
14767 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14768 return;
c0dd20ea
DJ
14769
14770 /* Find the type of the method. */
0b92b5bb 14771 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14772 if (pfn_type == NULL
14773 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14774 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14775 return;
c0dd20ea
DJ
14776
14777 /* Look for the "this" argument. */
14778 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14779 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14780 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14781 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14782 return;
c0dd20ea 14783
09e2d7c7 14784 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14785 new_type = alloc_type (objfile);
09e2d7c7 14786 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14787 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14788 TYPE_VARARGS (pfn_type));
0b92b5bb 14789 smash_to_methodptr_type (type, new_type);
c0dd20ea 14790}
1168df01 14791
2b4424c3
TT
14792/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14793 appropriate error checking and issuing complaints if there is a
14794 problem. */
14795
14796static ULONGEST
14797get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14798{
14799 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14800
14801 if (attr == nullptr)
14802 return 0;
14803
cd6c91b4 14804 if (!attr->form_is_constant ())
2b4424c3 14805 {
b98664d3 14806 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14807 " - DIE at %s [in module %s]"),
14808 sect_offset_str (die->sect_off),
14809 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14810 return 0;
14811 }
14812
14813 ULONGEST align;
14814 if (attr->form == DW_FORM_sdata)
14815 {
14816 LONGEST val = DW_SND (attr);
14817 if (val < 0)
14818 {
b98664d3 14819 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14820 " - DIE at %s [in module %s]"),
14821 sect_offset_str (die->sect_off),
14822 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14823 return 0;
14824 }
14825 align = val;
14826 }
14827 else
14828 align = DW_UNSND (attr);
14829
14830 if (align == 0)
14831 {
b98664d3 14832 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14833 " - DIE at %s [in module %s]"),
14834 sect_offset_str (die->sect_off),
14835 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14836 return 0;
14837 }
14838 if ((align & (align - 1)) != 0)
14839 {
b98664d3 14840 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14841 " - DIE at %s [in module %s]"),
14842 sect_offset_str (die->sect_off),
14843 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14844 return 0;
14845 }
14846
14847 return align;
14848}
14849
14850/* If the DIE has a DW_AT_alignment attribute, use its value to set
14851 the alignment for TYPE. */
14852
14853static void
14854maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14855 struct type *type)
14856{
14857 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14858 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14859 " - DIE at %s [in module %s]"),
14860 sect_offset_str (die->sect_off),
14861 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14862}
685b1105 14863
e35000a7
TBA
14864/* Check if the given VALUE is a valid enum dwarf_calling_convention
14865 constant for a type, according to DWARF5 spec, Table 5.5. */
14866
14867static bool
14868is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14869{
14870 switch (value)
14871 {
14872 case DW_CC_normal:
14873 case DW_CC_pass_by_reference:
14874 case DW_CC_pass_by_value:
14875 return true;
14876
14877 default:
14878 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14879 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14880 return false;
14881 }
14882}
14883
d0922fcf
TBA
14884/* Check if the given VALUE is a valid enum dwarf_calling_convention
14885 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14886 also according to GNU-specific values (see include/dwarf2.h). */
14887
14888static bool
14889is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14890{
14891 switch (value)
14892 {
14893 case DW_CC_normal:
14894 case DW_CC_program:
14895 case DW_CC_nocall:
14896 return true;
14897
14898 case DW_CC_GNU_renesas_sh:
14899 case DW_CC_GNU_borland_fastcall_i386:
14900 case DW_CC_GDB_IBM_OpenCL:
14901 return true;
14902
14903 default:
14904 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14905 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
14906 return false;
14907 }
14908}
14909
c906108c 14910/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14911 (definition) to create a type for the structure or union. Fill in
14912 the type's name and general properties; the members will not be
83655187
DE
14913 processed until process_structure_scope. A symbol table entry for
14914 the type will also not be done until process_structure_scope (assuming
14915 the type has a name).
c906108c 14916
c767944b
DJ
14917 NOTE: we need to call these functions regardless of whether or not the
14918 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14919 structure or union. This gets the type entered into our set of
83655187 14920 user defined types. */
c906108c 14921
f792889a 14922static struct type *
134d01f1 14923read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14924{
518817b3 14925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
14926 struct type *type;
14927 struct attribute *attr;
15d034d0 14928 const char *name;
c906108c 14929
348e048f
DE
14930 /* If the definition of this type lives in .debug_types, read that type.
14931 Don't follow DW_AT_specification though, that will take us back up
14932 the chain and we want to go down. */
052c8bb8 14933 attr = die->attr (DW_AT_signature);
435d3d88 14934 if (attr != nullptr)
348e048f 14935 {
ac9ec31b 14936 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14937
ac9ec31b 14938 /* The type's CU may not be the same as CU.
02142a6c 14939 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14940 return set_die_type (die, type, cu);
14941 }
14942
c0dd20ea 14943 type = alloc_type (objfile);
c906108c 14944 INIT_CPLUS_SPECIFIC (type);
93311388 14945
39cbfefa
DJ
14946 name = dwarf2_name (die, cu);
14947 if (name != NULL)
c906108c 14948 {
987504bb 14949 if (cu->language == language_cplus
c44af4eb
TT
14950 || cu->language == language_d
14951 || cu->language == language_rust)
63d06c5c 14952 {
15d034d0 14953 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
14954
14955 /* dwarf2_full_name might have already finished building the DIE's
14956 type. If so, there is no need to continue. */
14957 if (get_die_type (die, cu) != NULL)
14958 return get_die_type (die, cu);
14959
e86ca25f 14960 TYPE_NAME (type) = full_name;
63d06c5c
DC
14961 }
14962 else
14963 {
d8151005
DJ
14964 /* The name is already allocated along with this objfile, so
14965 we don't need to duplicate it for the type. */
e86ca25f 14966 TYPE_NAME (type) = name;
63d06c5c 14967 }
c906108c
SS
14968 }
14969
14970 if (die->tag == DW_TAG_structure_type)
14971 {
14972 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14973 }
14974 else if (die->tag == DW_TAG_union_type)
14975 {
14976 TYPE_CODE (type) = TYPE_CODE_UNION;
14977 }
2ddeaf8a
TT
14978 else if (die->tag == DW_TAG_variant_part)
14979 {
14980 TYPE_CODE (type) = TYPE_CODE_UNION;
14981 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
14982 }
c906108c
SS
14983 else
14984 {
4753d33b 14985 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
14986 }
14987
0cc2414c
TT
14988 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14989 TYPE_DECLARED_CLASS (type) = 1;
14990
e35000a7
TBA
14991 /* Store the calling convention in the type if it's available in
14992 the die. Otherwise the calling convention remains set to
14993 the default value DW_CC_normal. */
14994 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14995 if (attr != nullptr
14996 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
14997 {
14998 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14999 TYPE_CPLUS_CALLING_CONVENTION (type)
15000 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15001 }
15002
e142c38c 15003 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15004 if (attr != nullptr)
c906108c 15005 {
cd6c91b4 15006 if (attr->form_is_constant ())
155bfbd3
JB
15007 TYPE_LENGTH (type) = DW_UNSND (attr);
15008 else
15009 {
15010 /* For the moment, dynamic type sizes are not supported
15011 by GDB's struct type. The actual size is determined
15012 on-demand when resolving the type of a given object,
15013 so set the type's length to zero for now. Otherwise,
15014 we record an expression as the length, and that expression
15015 could lead to a very large value, which could eventually
15016 lead to us trying to allocate that much memory when creating
15017 a value of that type. */
15018 TYPE_LENGTH (type) = 0;
15019 }
c906108c
SS
15020 }
15021 else
15022 {
15023 TYPE_LENGTH (type) = 0;
15024 }
15025
2b4424c3
TT
15026 maybe_set_alignment (cu, die, type);
15027
5230b05a 15028 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15029 {
5230b05a
WT
15030 /* ICC<14 does not output the required DW_AT_declaration on
15031 incomplete types, but gives them a size of zero. */
422b1cb0 15032 TYPE_STUB (type) = 1;
685b1105
JK
15033 }
15034 else
15035 TYPE_STUB_SUPPORTED (type) = 1;
15036
dc718098 15037 if (die_is_declaration (die, cu))
876cecd0 15038 TYPE_STUB (type) = 1;
a6c727b2
DJ
15039 else if (attr == NULL && die->child == NULL
15040 && producer_is_realview (cu->producer))
15041 /* RealView does not output the required DW_AT_declaration
15042 on incomplete types. */
15043 TYPE_STUB (type) = 1;
dc718098 15044
c906108c
SS
15045 /* We need to add the type field to the die immediately so we don't
15046 infinitely recurse when dealing with pointers to the structure
0963b4bd 15047 type within the structure itself. */
1c379e20 15048 set_die_type (die, type, cu);
c906108c 15049
7e314c57
JK
15050 /* set_die_type should be already done. */
15051 set_descriptive_type (type, die, cu);
15052
c767944b
DJ
15053 return type;
15054}
15055
2ddeaf8a
TT
15056/* A helper for process_structure_scope that handles a single member
15057 DIE. */
15058
15059static void
15060handle_struct_member_die (struct die_info *child_die, struct type *type,
15061 struct field_info *fi,
15062 std::vector<struct symbol *> *template_args,
15063 struct dwarf2_cu *cu)
15064{
15065 if (child_die->tag == DW_TAG_member
15066 || child_die->tag == DW_TAG_variable
15067 || child_die->tag == DW_TAG_variant_part)
15068 {
15069 /* NOTE: carlton/2002-11-05: A C++ static data member
15070 should be a DW_TAG_member that is a declaration, but
15071 all versions of G++ as of this writing (so through at
15072 least 3.2.1) incorrectly generate DW_TAG_variable
15073 tags for them instead. */
15074 dwarf2_add_field (fi, child_die, cu);
15075 }
15076 else if (child_die->tag == DW_TAG_subprogram)
15077 {
15078 /* Rust doesn't have member functions in the C++ sense.
15079 However, it does emit ordinary functions as children
15080 of a struct DIE. */
15081 if (cu->language == language_rust)
15082 read_func_scope (child_die, cu);
15083 else
15084 {
15085 /* C++ member function. */
15086 dwarf2_add_member_fn (fi, child_die, type, cu);
15087 }
15088 }
15089 else if (child_die->tag == DW_TAG_inheritance)
15090 {
15091 /* C++ base class field. */
15092 dwarf2_add_field (fi, child_die, cu);
15093 }
15094 else if (type_can_define_types (child_die))
15095 dwarf2_add_type_defn (fi, child_die, cu);
15096 else if (child_die->tag == DW_TAG_template_type_param
15097 || child_die->tag == DW_TAG_template_value_param)
15098 {
15099 struct symbol *arg = new_symbol (child_die, NULL, cu);
15100
15101 if (arg != NULL)
15102 template_args->push_back (arg);
15103 }
15104 else if (child_die->tag == DW_TAG_variant)
15105 {
15106 /* In a variant we want to get the discriminant and also add a
15107 field for our sole member child. */
15108 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15109
bde09ab7 15110 for (die_info *variant_child = child_die->child;
2ddeaf8a 15111 variant_child != NULL;
436c571c 15112 variant_child = variant_child->sibling)
2ddeaf8a
TT
15113 {
15114 if (variant_child->tag == DW_TAG_member)
15115 {
15116 handle_struct_member_die (variant_child, type, fi,
15117 template_args, cu);
15118 /* Only handle the one. */
15119 break;
15120 }
15121 }
15122
15123 /* We don't handle this but we might as well report it if we see
15124 it. */
15125 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15126 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15127 " - DIE at %s [in module %s]"),
15128 sect_offset_str (child_die->sect_off),
15129 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15130
15131 /* The first field was just added, so we can stash the
15132 discriminant there. */
be2daae6 15133 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15134 if (discr == NULL)
be2daae6 15135 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15136 else
be2daae6 15137 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15138 }
15139}
15140
c767944b
DJ
15141/* Finish creating a structure or union type, including filling in
15142 its members and creating a symbol for it. */
15143
15144static void
15145process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15146{
518817b3 15147 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15148 struct die_info *child_die;
c767944b
DJ
15149 struct type *type;
15150
15151 type = get_die_type (die, cu);
15152 if (type == NULL)
15153 type = read_structure_type (die, cu);
15154
2ddeaf8a
TT
15155 /* When reading a DW_TAG_variant_part, we need to notice when we
15156 read the discriminant member, so we can record it later in the
15157 discriminant_info. */
15158 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15159 sect_offset discr_offset {};
3e1d3d8c 15160 bool has_template_parameters = false;
2ddeaf8a
TT
15161
15162 if (is_variant_part)
15163 {
15164 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15165 if (discr == NULL)
15166 {
15167 /* Maybe it's a univariant form, an extension we support.
15168 In this case arrange not to check the offset. */
15169 is_variant_part = false;
15170 }
cd6c91b4 15171 else if (discr->form_is_ref ())
2ddeaf8a
TT
15172 {
15173 struct dwarf2_cu *target_cu = cu;
15174 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15175
15176 discr_offset = target_die->sect_off;
15177 }
15178 else
15179 {
b98664d3 15180 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15181 " - DIE at %s [in module %s]"),
15182 sect_offset_str (die->sect_off),
15183 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15184 is_variant_part = false;
15185 }
15186 }
15187
e142c38c 15188 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15189 {
15190 struct field_info fi;
2f4732b0 15191 std::vector<struct symbol *> template_args;
c906108c 15192
639d11d3 15193 child_die = die->child;
c906108c
SS
15194
15195 while (child_die && child_die->tag)
15196 {
2ddeaf8a 15197 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15198
2ddeaf8a 15199 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15200 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15201
436c571c 15202 child_die = child_die->sibling;
c906108c
SS
15203 }
15204
34eaf542 15205 /* Attach template arguments to type. */
2f4732b0 15206 if (!template_args.empty ())
34eaf542 15207 {
3e1d3d8c 15208 has_template_parameters = true;
34eaf542 15209 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15210 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15211 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15212 = XOBNEWVEC (&objfile->objfile_obstack,
15213 struct symbol *,
15214 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15215 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15216 template_args.data (),
34eaf542
TT
15217 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15218 * sizeof (struct symbol *)));
34eaf542
TT
15219 }
15220
c906108c 15221 /* Attach fields and member functions to the type. */
317f7127 15222 if (fi.nfields () > 0)
e7c27a73 15223 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15224 if (!fi.fnfieldlists.empty ())
c906108c 15225 {
e7c27a73 15226 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15227
c5aa993b 15228 /* Get the type which refers to the base class (possibly this
c906108c 15229 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15230 class from the DW_AT_containing_type attribute. This use of
15231 DW_AT_containing_type is a GNU extension. */
c906108c 15232
e142c38c 15233 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15234 {
e7c27a73 15235 struct type *t = die_containing_type (die, cu);
c906108c 15236
ae6ae975 15237 set_type_vptr_basetype (type, t);
c906108c
SS
15238 if (type == t)
15239 {
c906108c
SS
15240 int i;
15241
15242 /* Our own class provides vtbl ptr. */
15243 for (i = TYPE_NFIELDS (t) - 1;
15244 i >= TYPE_N_BASECLASSES (t);
15245 --i)
15246 {
0d5cff50 15247 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15248
1168df01 15249 if (is_vtable_name (fieldname, cu))
c906108c 15250 {
ae6ae975 15251 set_type_vptr_fieldno (type, i);
c906108c
SS
15252 break;
15253 }
15254 }
15255
15256 /* Complain if virtual function table field not found. */
15257 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15258 complaint (_("virtual function table pointer "
3e43a32a 15259 "not found when defining class '%s'"),
e86ca25f 15260 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15261 }
15262 else
15263 {
ae6ae975 15264 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15265 }
15266 }
f6235d4c 15267 else if (cu->producer
61012eef 15268 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15269 {
15270 /* The IBM XLC compiler does not provide direct indication
15271 of the containing type, but the vtable pointer is
15272 always named __vfp. */
15273
15274 int i;
15275
15276 for (i = TYPE_NFIELDS (type) - 1;
15277 i >= TYPE_N_BASECLASSES (type);
15278 --i)
15279 {
15280 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15281 {
ae6ae975
DE
15282 set_type_vptr_fieldno (type, i);
15283 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15284 break;
15285 }
15286 }
15287 }
c906108c 15288 }
98751a41
JK
15289
15290 /* Copy fi.typedef_field_list linked list elements content into the
15291 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15292 if (!fi.typedef_field_list.empty ())
98751a41 15293 {
be2daae6 15294 int count = fi.typedef_field_list.size ();
98751a41 15295
a0d7a4ff 15296 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15297 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15298 = ((struct decl_field *)
be2daae6
TT
15299 TYPE_ALLOC (type,
15300 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15301 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15302
be2daae6
TT
15303 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15304 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15305 }
c767944b 15306
883fd55a
KS
15307 /* Copy fi.nested_types_list linked list elements content into the
15308 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15309 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15310 {
be2daae6 15311 int count = fi.nested_types_list.size ();
883fd55a
KS
15312
15313 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15314 TYPE_NESTED_TYPES_ARRAY (type)
15315 = ((struct decl_field *)
be2daae6
TT
15316 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15317 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15318
be2daae6
TT
15319 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15320 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15321 }
c906108c 15322 }
63d06c5c 15323
bb5ed363 15324 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15325 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15326 cu->rust_unions.push_back (type);
0b92b5bb 15327
90aeadfc
DC
15328 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15329 snapshots) has been known to create a die giving a declaration
15330 for a class that has, as a child, a die giving a definition for a
15331 nested class. So we have to process our children even if the
15332 current die is a declaration. Normally, of course, a declaration
15333 won't have any children at all. */
134d01f1 15334
ca040673
DE
15335 child_die = die->child;
15336
90aeadfc
DC
15337 while (child_die != NULL && child_die->tag)
15338 {
15339 if (child_die->tag == DW_TAG_member
15340 || child_die->tag == DW_TAG_variable
34eaf542
TT
15341 || child_die->tag == DW_TAG_inheritance
15342 || child_die->tag == DW_TAG_template_value_param
15343 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15344 {
90aeadfc 15345 /* Do nothing. */
134d01f1 15346 }
90aeadfc
DC
15347 else
15348 process_die (child_die, cu);
134d01f1 15349
436c571c 15350 child_die = child_die->sibling;
134d01f1
DJ
15351 }
15352
fa4028e9
JB
15353 /* Do not consider external references. According to the DWARF standard,
15354 these DIEs are identified by the fact that they have no byte_size
15355 attribute, and a declaration attribute. */
15356 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15357 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15358 {
15359 struct symbol *sym = new_symbol (die, type, cu);
15360
15361 if (has_template_parameters)
15362 {
a776957c
TT
15363 struct symtab *symtab;
15364 if (sym != nullptr)
15365 symtab = symbol_symtab (sym);
15366 else if (cu->line_header != nullptr)
15367 {
15368 /* Any related symtab will do. */
15369 symtab
7ba99d21 15370 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15371 }
15372 else
15373 {
15374 symtab = nullptr;
15375 complaint (_("could not find suitable "
15376 "symtab for template parameter"
15377 " - DIE at %s [in module %s]"),
15378 sect_offset_str (die->sect_off),
15379 objfile_name (objfile));
15380 }
15381
15382 if (symtab != nullptr)
15383 {
15384 /* Make sure that the symtab is set on the new symbols.
15385 Even though they don't appear in this symtab directly,
15386 other parts of gdb assume that symbols do, and this is
15387 reasonably true. */
15388 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15389 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15390 }
3e1d3d8c
TT
15391 }
15392 }
134d01f1
DJ
15393}
15394
55426c9d
JB
15395/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15396 update TYPE using some information only available in DIE's children. */
15397
15398static void
15399update_enumeration_type_from_children (struct die_info *die,
15400 struct type *type,
15401 struct dwarf2_cu *cu)
15402{
60f7655a 15403 struct die_info *child_die;
55426c9d
JB
15404 int unsigned_enum = 1;
15405 int flag_enum = 1;
55426c9d 15406
8268c778 15407 auto_obstack obstack;
55426c9d 15408
60f7655a
DE
15409 for (child_die = die->child;
15410 child_die != NULL && child_die->tag;
436c571c 15411 child_die = child_die->sibling)
55426c9d
JB
15412 {
15413 struct attribute *attr;
15414 LONGEST value;
15415 const gdb_byte *bytes;
15416 struct dwarf2_locexpr_baton *baton;
15417 const char *name;
60f7655a 15418
55426c9d
JB
15419 if (child_die->tag != DW_TAG_enumerator)
15420 continue;
15421
15422 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15423 if (attr == NULL)
15424 continue;
15425
15426 name = dwarf2_name (child_die, cu);
15427 if (name == NULL)
15428 name = "<anonymous enumerator>";
15429
15430 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15431 &value, &bytes, &baton);
15432 if (value < 0)
15433 {
15434 unsigned_enum = 0;
15435 flag_enum = 0;
15436 }
55426c9d 15437 else
edd45eb0
SM
15438 {
15439 if (count_one_bits_ll (value) >= 2)
15440 flag_enum = 0;
edd45eb0 15441 }
55426c9d
JB
15442
15443 /* If we already know that the enum type is neither unsigned, nor
15444 a flag type, no need to look at the rest of the enumerates. */
15445 if (!unsigned_enum && !flag_enum)
15446 break;
55426c9d
JB
15447 }
15448
15449 if (unsigned_enum)
15450 TYPE_UNSIGNED (type) = 1;
15451 if (flag_enum)
15452 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15453}
15454
134d01f1
DJ
15455/* Given a DW_AT_enumeration_type die, set its type. We do not
15456 complete the type's fields yet, or create any symbols. */
c906108c 15457
f792889a 15458static struct type *
134d01f1 15459read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15460{
518817b3 15461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15462 struct type *type;
c906108c 15463 struct attribute *attr;
0114d602 15464 const char *name;
134d01f1 15465
348e048f
DE
15466 /* If the definition of this type lives in .debug_types, read that type.
15467 Don't follow DW_AT_specification though, that will take us back up
15468 the chain and we want to go down. */
052c8bb8 15469 attr = die->attr (DW_AT_signature);
435d3d88 15470 if (attr != nullptr)
348e048f 15471 {
ac9ec31b 15472 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15473
ac9ec31b 15474 /* The type's CU may not be the same as CU.
02142a6c 15475 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15476 return set_die_type (die, type, cu);
15477 }
15478
c906108c
SS
15479 type = alloc_type (objfile);
15480
15481 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15482 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15483 if (name != NULL)
e86ca25f 15484 TYPE_NAME (type) = name;
c906108c 15485
0626fc76
TT
15486 attr = dwarf2_attr (die, DW_AT_type, cu);
15487 if (attr != NULL)
15488 {
15489 struct type *underlying_type = die_type (die, cu);
15490
15491 TYPE_TARGET_TYPE (type) = underlying_type;
15492 }
15493
e142c38c 15494 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15495 if (attr != nullptr)
c906108c
SS
15496 {
15497 TYPE_LENGTH (type) = DW_UNSND (attr);
15498 }
15499 else
15500 {
15501 TYPE_LENGTH (type) = 0;
15502 }
15503
2b4424c3
TT
15504 maybe_set_alignment (cu, die, type);
15505
137033e9
JB
15506 /* The enumeration DIE can be incomplete. In Ada, any type can be
15507 declared as private in the package spec, and then defined only
15508 inside the package body. Such types are known as Taft Amendment
15509 Types. When another package uses such a type, an incomplete DIE
15510 may be generated by the compiler. */
02eb380e 15511 if (die_is_declaration (die, cu))
876cecd0 15512 TYPE_STUB (type) = 1;
02eb380e 15513
0626fc76
TT
15514 /* Finish the creation of this type by using the enum's children.
15515 We must call this even when the underlying type has been provided
15516 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15517 update_enumeration_type_from_children (die, type, cu);
15518
0626fc76
TT
15519 /* If this type has an underlying type that is not a stub, then we
15520 may use its attributes. We always use the "unsigned" attribute
15521 in this situation, because ordinarily we guess whether the type
15522 is unsigned -- but the guess can be wrong and the underlying type
15523 can tell us the reality. However, we defer to a local size
15524 attribute if one exists, because this lets the compiler override
15525 the underlying type if needed. */
15526 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15527 {
15528 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15529 if (TYPE_LENGTH (type) == 0)
15530 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15531 if (TYPE_RAW_ALIGN (type) == 0
15532 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15533 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15534 }
15535
3d567982
TT
15536 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15537
f792889a 15538 return set_die_type (die, type, cu);
134d01f1
DJ
15539}
15540
15541/* Given a pointer to a die which begins an enumeration, process all
15542 the dies that define the members of the enumeration, and create the
15543 symbol for the enumeration type.
15544
15545 NOTE: We reverse the order of the element list. */
15546
15547static void
15548process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15549{
f792889a 15550 struct type *this_type;
134d01f1 15551
f792889a
DJ
15552 this_type = get_die_type (die, cu);
15553 if (this_type == NULL)
15554 this_type = read_enumeration_type (die, cu);
9dc481d3 15555
639d11d3 15556 if (die->child != NULL)
c906108c 15557 {
9dc481d3
DE
15558 struct die_info *child_die;
15559 struct symbol *sym;
43816ebc 15560 std::vector<struct field> fields;
15d034d0 15561 const char *name;
9dc481d3 15562
639d11d3 15563 child_die = die->child;
c906108c
SS
15564 while (child_die && child_die->tag)
15565 {
15566 if (child_die->tag != DW_TAG_enumerator)
15567 {
e7c27a73 15568 process_die (child_die, cu);
c906108c
SS
15569 }
15570 else
15571 {
39cbfefa
DJ
15572 name = dwarf2_name (child_die, cu);
15573 if (name)
c906108c 15574 {
f792889a 15575 sym = new_symbol (child_die, this_type, cu);
c906108c 15576
43816ebc
TT
15577 fields.emplace_back ();
15578 struct field &field = fields.back ();
c906108c 15579
43816ebc
TT
15580 FIELD_NAME (field) = sym->linkage_name ();
15581 FIELD_TYPE (field) = NULL;
15582 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15583 FIELD_BITSIZE (field) = 0;
c906108c
SS
15584 }
15585 }
15586
436c571c 15587 child_die = child_die->sibling;
c906108c
SS
15588 }
15589
43816ebc 15590 if (!fields.empty ())
c906108c 15591 {
43816ebc 15592 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15593 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15594 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15595 memcpy (TYPE_FIELDS (this_type), fields.data (),
15596 sizeof (struct field) * fields.size ());
c906108c 15597 }
c906108c 15598 }
134d01f1 15599
6c83ed52
TT
15600 /* If we are reading an enum from a .debug_types unit, and the enum
15601 is a declaration, and the enum is not the signatured type in the
15602 unit, then we do not want to add a symbol for it. Adding a
15603 symbol would in some cases obscure the true definition of the
15604 enum, giving users an incomplete type when the definition is
15605 actually available. Note that we do not want to do this for all
15606 enums which are just declarations, because C++0x allows forward
15607 enum declarations. */
3019eac3 15608 if (cu->per_cu->is_debug_types
6c83ed52
TT
15609 && die_is_declaration (die, cu))
15610 {
52dc124a 15611 struct signatured_type *sig_type;
6c83ed52 15612
c0f78cd4 15613 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15614 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15615 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15616 return;
15617 }
15618
f792889a 15619 new_symbol (die, this_type, cu);
c906108c
SS
15620}
15621
15622/* Extract all information from a DW_TAG_array_type DIE and put it in
15623 the DIE's type field. For now, this only handles one dimensional
15624 arrays. */
15625
f792889a 15626static struct type *
e7c27a73 15627read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15628{
518817b3 15629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15630 struct die_info *child_die;
7e314c57 15631 struct type *type;
c906108c 15632 struct type *element_type, *range_type, *index_type;
c906108c 15633 struct attribute *attr;
15d034d0 15634 const char *name;
a405673c 15635 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15636 unsigned int bit_stride = 0;
c906108c 15637
e7c27a73 15638 element_type = die_type (die, cu);
c906108c 15639
7e314c57
JK
15640 /* The die_type call above may have already set the type for this DIE. */
15641 type = get_die_type (die, cu);
15642 if (type)
15643 return type;
15644
dc53a7ad
JB
15645 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15646 if (attr != NULL)
a405673c
JB
15647 {
15648 int stride_ok;
09ba997f 15649 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15650
15651 byte_stride_prop
15652 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15653 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15654 prop_type);
a405673c
JB
15655 if (!stride_ok)
15656 {
b98664d3 15657 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15658 " - DIE at %s [in module %s]"),
15659 sect_offset_str (die->sect_off),
518817b3 15660 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15661 /* Ignore this attribute. We will likely not be able to print
15662 arrays of this type correctly, but there is little we can do
15663 to help if we cannot read the attribute's value. */
15664 byte_stride_prop = NULL;
15665 }
15666 }
dc53a7ad
JB
15667
15668 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15669 if (attr != NULL)
15670 bit_stride = DW_UNSND (attr);
15671
c906108c
SS
15672 /* Irix 6.2 native cc creates array types without children for
15673 arrays with unspecified length. */
639d11d3 15674 if (die->child == NULL)
c906108c 15675 {
46bf5051 15676 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15677 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15678 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15679 byte_stride_prop, bit_stride);
f792889a 15680 return set_die_type (die, type, cu);
c906108c
SS
15681 }
15682
791afaa2 15683 std::vector<struct type *> range_types;
639d11d3 15684 child_die = die->child;
c906108c
SS
15685 while (child_die && child_die->tag)
15686 {
15687 if (child_die->tag == DW_TAG_subrange_type)
15688 {
f792889a 15689 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15690
f792889a 15691 if (child_type != NULL)
a02abb62 15692 {
0963b4bd
MS
15693 /* The range type was succesfully read. Save it for the
15694 array type creation. */
791afaa2 15695 range_types.push_back (child_type);
a02abb62 15696 }
c906108c 15697 }
436c571c 15698 child_die = child_die->sibling;
c906108c
SS
15699 }
15700
15701 /* Dwarf2 dimensions are output from left to right, create the
15702 necessary array types in backwards order. */
7ca2d3a3 15703
c906108c 15704 type = element_type;
7ca2d3a3
DL
15705
15706 if (read_array_order (die, cu) == DW_ORD_col_major)
15707 {
15708 int i = 0;
9a619af0 15709
791afaa2 15710 while (i < range_types.size ())
dc53a7ad 15711 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15712 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15713 }
15714 else
15715 {
791afaa2 15716 size_t ndim = range_types.size ();
7ca2d3a3 15717 while (ndim-- > 0)
dc53a7ad 15718 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15719 byte_stride_prop, bit_stride);
7ca2d3a3 15720 }
c906108c 15721
f5f8a009
EZ
15722 /* Understand Dwarf2 support for vector types (like they occur on
15723 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15724 array type. This is not part of the Dwarf2/3 standard yet, but a
15725 custom vendor extension. The main difference between a regular
15726 array and the vector variant is that vectors are passed by value
15727 to functions. */
e142c38c 15728 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15729 if (attr != nullptr)
ea37ba09 15730 make_vector_type (type);
f5f8a009 15731
dbc98a8b
KW
15732 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15733 implementation may choose to implement triple vectors using this
15734 attribute. */
15735 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15736 if (attr != nullptr)
dbc98a8b
KW
15737 {
15738 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15739 TYPE_LENGTH (type) = DW_UNSND (attr);
15740 else
b98664d3 15741 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15742 "than the total size of elements"));
dbc98a8b
KW
15743 }
15744
39cbfefa
DJ
15745 name = dwarf2_name (die, cu);
15746 if (name)
15747 TYPE_NAME (type) = name;
6e70227d 15748
2b4424c3
TT
15749 maybe_set_alignment (cu, die, type);
15750
0963b4bd 15751 /* Install the type in the die. */
7e314c57
JK
15752 set_die_type (die, type, cu);
15753
15754 /* set_die_type should be already done. */
b4ba55a1
JB
15755 set_descriptive_type (type, die, cu);
15756
7e314c57 15757 return type;
c906108c
SS
15758}
15759
7ca2d3a3 15760static enum dwarf_array_dim_ordering
6e70227d 15761read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15762{
15763 struct attribute *attr;
15764
15765 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15766
435d3d88 15767 if (attr != nullptr)
aead7601 15768 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15769
0963b4bd
MS
15770 /* GNU F77 is a special case, as at 08/2004 array type info is the
15771 opposite order to the dwarf2 specification, but data is still
15772 laid out as per normal fortran.
7ca2d3a3 15773
0963b4bd
MS
15774 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15775 version checking. */
7ca2d3a3 15776
905e0470
PM
15777 if (cu->language == language_fortran
15778 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15779 {
15780 return DW_ORD_row_major;
15781 }
15782
6e70227d 15783 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15784 {
15785 case array_column_major:
15786 return DW_ORD_col_major;
15787 case array_row_major:
15788 default:
15789 return DW_ORD_row_major;
15790 };
15791}
15792
72019c9c 15793/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15794 the DIE's type field. */
72019c9c 15795
f792889a 15796static struct type *
72019c9c
GM
15797read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15798{
7e314c57
JK
15799 struct type *domain_type, *set_type;
15800 struct attribute *attr;
f792889a 15801
7e314c57
JK
15802 domain_type = die_type (die, cu);
15803
15804 /* The die_type call above may have already set the type for this DIE. */
15805 set_type = get_die_type (die, cu);
15806 if (set_type)
15807 return set_type;
15808
15809 set_type = create_set_type (NULL, domain_type);
15810
15811 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15812 if (attr != nullptr)
d09039dd 15813 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15814
2b4424c3
TT
15815 maybe_set_alignment (cu, die, set_type);
15816
f792889a 15817 return set_die_type (die, set_type, cu);
72019c9c 15818}
7ca2d3a3 15819
0971de02
TT
15820/* A helper for read_common_block that creates a locexpr baton.
15821 SYM is the symbol which we are marking as computed.
15822 COMMON_DIE is the DIE for the common block.
15823 COMMON_LOC is the location expression attribute for the common
15824 block itself.
15825 MEMBER_LOC is the location expression attribute for the particular
15826 member of the common block that we are processing.
15827 CU is the CU from which the above come. */
15828
15829static void
15830mark_common_block_symbol_computed (struct symbol *sym,
15831 struct die_info *common_die,
15832 struct attribute *common_loc,
15833 struct attribute *member_loc,
15834 struct dwarf2_cu *cu)
15835{
518817b3
SM
15836 struct dwarf2_per_objfile *dwarf2_per_objfile
15837 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15838 struct objfile *objfile = dwarf2_per_objfile->objfile;
15839 struct dwarf2_locexpr_baton *baton;
15840 gdb_byte *ptr;
15841 unsigned int cu_off;
15842 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15843 LONGEST offset = 0;
15844
15845 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15846 gdb_assert (common_loc->form_is_block ());
15847 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15848 || member_loc->form_is_constant ());
0971de02 15849
8d749320 15850 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15851 baton->per_cu = cu->per_cu;
15852 gdb_assert (baton->per_cu);
15853
15854 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15855
cd6c91b4 15856 if (member_loc->form_is_constant ())
0971de02 15857 {
0826b30a 15858 offset = member_loc->constant_value (0);
0971de02
TT
15859 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15860 }
15861 else
15862 baton->size += DW_BLOCK (member_loc)->size;
15863
224c3ddb 15864 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15865 baton->data = ptr;
15866
15867 *ptr++ = DW_OP_call4;
9c541725 15868 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15869 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15870 ptr += 4;
15871
cd6c91b4 15872 if (member_loc->form_is_constant ())
0971de02
TT
15873 {
15874 *ptr++ = DW_OP_addr;
15875 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15876 ptr += cu->header.addr_size;
15877 }
15878 else
15879 {
15880 /* We have to copy the data here, because DW_OP_call4 will only
15881 use a DW_AT_location attribute. */
15882 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15883 ptr += DW_BLOCK (member_loc)->size;
15884 }
15885
15886 *ptr++ = DW_OP_plus;
15887 gdb_assert (ptr - baton->data == baton->size);
15888
0971de02 15889 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15890 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15891}
15892
4357ac6c
TT
15893/* Create appropriate locally-scoped variables for all the
15894 DW_TAG_common_block entries. Also create a struct common_block
15895 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 15896 is used to separate the common blocks name namespace from regular
4357ac6c 15897 variable names. */
c906108c
SS
15898
15899static void
e7c27a73 15900read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15901{
0971de02
TT
15902 struct attribute *attr;
15903
15904 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 15905 if (attr != nullptr)
0971de02
TT
15906 {
15907 /* Support the .debug_loc offsets. */
4fc6c0d5 15908 if (attr->form_is_block ())
0971de02
TT
15909 {
15910 /* Ok. */
15911 }
cd6c91b4 15912 else if (attr->form_is_section_offset ())
0971de02
TT
15913 {
15914 dwarf2_complex_location_expr_complaint ();
15915 attr = NULL;
15916 }
15917 else
15918 {
15919 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15920 "common block member");
15921 attr = NULL;
15922 }
15923 }
15924
639d11d3 15925 if (die->child != NULL)
c906108c 15926 {
518817b3 15927 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
15928 struct die_info *child_die;
15929 size_t n_entries = 0, size;
15930 struct common_block *common_block;
15931 struct symbol *sym;
74ac6d43 15932
4357ac6c
TT
15933 for (child_die = die->child;
15934 child_die && child_die->tag;
436c571c 15935 child_die = child_die->sibling)
4357ac6c
TT
15936 ++n_entries;
15937
15938 size = (sizeof (struct common_block)
15939 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
15940 common_block
15941 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15942 size);
4357ac6c
TT
15943 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15944 common_block->n_entries = 0;
15945
15946 for (child_die = die->child;
15947 child_die && child_die->tag;
436c571c 15948 child_die = child_die->sibling)
4357ac6c
TT
15949 {
15950 /* Create the symbol in the DW_TAG_common_block block in the current
15951 symbol scope. */
e7c27a73 15952 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
15953 if (sym != NULL)
15954 {
15955 struct attribute *member_loc;
15956
15957 common_block->contents[common_block->n_entries++] = sym;
15958
15959 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15960 cu);
15961 if (member_loc)
15962 {
15963 /* GDB has handled this for a long time, but it is
15964 not specified by DWARF. It seems to have been
15965 emitted by gfortran at least as recently as:
15966 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 15967 complaint (_("Variable in common block has "
0971de02 15968 "DW_AT_data_member_location "
9d8780f0
SM
15969 "- DIE at %s [in module %s]"),
15970 sect_offset_str (child_die->sect_off),
518817b3 15971 objfile_name (objfile));
0971de02 15972
cd6c91b4 15973 if (member_loc->form_is_section_offset ())
0971de02 15974 dwarf2_complex_location_expr_complaint ();
cd6c91b4 15975 else if (member_loc->form_is_constant ()
4fc6c0d5 15976 || member_loc->form_is_block ())
0971de02 15977 {
435d3d88 15978 if (attr != nullptr)
0971de02
TT
15979 mark_common_block_symbol_computed (sym, die, attr,
15980 member_loc, cu);
15981 }
15982 else
15983 dwarf2_complex_location_expr_complaint ();
15984 }
15985 }
c906108c 15986 }
4357ac6c
TT
15987
15988 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15989 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
15990 }
15991}
15992
0114d602 15993/* Create a type for a C++ namespace. */
d9fa45fe 15994
0114d602
DJ
15995static struct type *
15996read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 15997{
518817b3 15998 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 15999 const char *previous_prefix, *name;
9219021c 16000 int is_anonymous;
0114d602
DJ
16001 struct type *type;
16002
16003 /* For extensions, reuse the type of the original namespace. */
16004 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16005 {
16006 struct die_info *ext_die;
16007 struct dwarf2_cu *ext_cu = cu;
9a619af0 16008
0114d602
DJ
16009 ext_die = dwarf2_extension (die, &ext_cu);
16010 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16011
16012 /* EXT_CU may not be the same as CU.
02142a6c 16013 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16014 return set_die_type (die, type, cu);
16015 }
9219021c 16016
e142c38c 16017 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16018
16019 /* Now build the name of the current namespace. */
16020
0114d602
DJ
16021 previous_prefix = determine_prefix (die, cu);
16022 if (previous_prefix[0] != '\0')
16023 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16024 previous_prefix, name, 0, cu);
0114d602
DJ
16025
16026 /* Create the type. */
19f392bc 16027 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16028
60531b24 16029 return set_die_type (die, type, cu);
0114d602
DJ
16030}
16031
22cee43f 16032/* Read a namespace scope. */
0114d602
DJ
16033
16034static void
16035read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16036{
518817b3 16037 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16038 int is_anonymous;
9219021c 16039
5c4e30ca
DC
16040 /* Add a symbol associated to this if we haven't seen the namespace
16041 before. Also, add a using directive if it's an anonymous
16042 namespace. */
9219021c 16043
f2f0e013 16044 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16045 {
16046 struct type *type;
16047
0114d602 16048 type = read_type_die (die, cu);
e7c27a73 16049 new_symbol (die, type, cu);
5c4e30ca 16050
e8e80198 16051 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16052 if (is_anonymous)
0114d602
DJ
16053 {
16054 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16055
eb1e02fd 16056 std::vector<const char *> excludes;
804d2729 16057 add_using_directive (using_directives (cu),
22cee43f 16058 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16059 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16060 }
5c4e30ca 16061 }
9219021c 16062
639d11d3 16063 if (die->child != NULL)
d9fa45fe 16064 {
639d11d3 16065 struct die_info *child_die = die->child;
6e70227d 16066
d9fa45fe
DC
16067 while (child_die && child_die->tag)
16068 {
e7c27a73 16069 process_die (child_die, cu);
436c571c 16070 child_die = child_die->sibling;
d9fa45fe
DC
16071 }
16072 }
38d518c9
EZ
16073}
16074
f55ee35c
JK
16075/* Read a Fortran module as type. This DIE can be only a declaration used for
16076 imported module. Still we need that type as local Fortran "use ... only"
16077 declaration imports depend on the created type in determine_prefix. */
16078
16079static struct type *
16080read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16081{
518817b3 16082 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16083 const char *module_name;
f55ee35c
JK
16084 struct type *type;
16085
16086 module_name = dwarf2_name (die, cu);
19f392bc 16087 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16088
f55ee35c
JK
16089 return set_die_type (die, type, cu);
16090}
16091
5d7cb8df
JK
16092/* Read a Fortran module. */
16093
16094static void
16095read_module (struct die_info *die, struct dwarf2_cu *cu)
16096{
16097 struct die_info *child_die = die->child;
530e8392
KB
16098 struct type *type;
16099
16100 type = read_type_die (die, cu);
16101 new_symbol (die, type, cu);
5d7cb8df 16102
5d7cb8df
JK
16103 while (child_die && child_die->tag)
16104 {
16105 process_die (child_die, cu);
436c571c 16106 child_die = child_die->sibling;
5d7cb8df
JK
16107 }
16108}
16109
38d518c9
EZ
16110/* Return the name of the namespace represented by DIE. Set
16111 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16112 namespace. */
16113
16114static const char *
e142c38c 16115namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16116{
16117 struct die_info *current_die;
16118 const char *name = NULL;
16119
16120 /* Loop through the extensions until we find a name. */
16121
16122 for (current_die = die;
16123 current_die != NULL;
f2f0e013 16124 current_die = dwarf2_extension (die, &cu))
38d518c9 16125 {
96553a0c
DE
16126 /* We don't use dwarf2_name here so that we can detect the absence
16127 of a name -> anonymous namespace. */
7d45c7c3 16128 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16129
38d518c9
EZ
16130 if (name != NULL)
16131 break;
16132 }
16133
16134 /* Is it an anonymous namespace? */
16135
16136 *is_anonymous = (name == NULL);
16137 if (*is_anonymous)
2b1dbab0 16138 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16139
16140 return name;
d9fa45fe
DC
16141}
16142
c906108c
SS
16143/* Extract all information from a DW_TAG_pointer_type DIE and add to
16144 the user defined type vector. */
16145
f792889a 16146static struct type *
e7c27a73 16147read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16148{
518817b3
SM
16149 struct gdbarch *gdbarch
16150 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16151 struct comp_unit_head *cu_header = &cu->header;
c906108c 16152 struct type *type;
8b2dbe47
KB
16153 struct attribute *attr_byte_size;
16154 struct attribute *attr_address_class;
16155 int byte_size, addr_class;
7e314c57
JK
16156 struct type *target_type;
16157
16158 target_type = die_type (die, cu);
c906108c 16159
7e314c57
JK
16160 /* The die_type call above may have already set the type for this DIE. */
16161 type = get_die_type (die, cu);
16162 if (type)
16163 return type;
16164
16165 type = lookup_pointer_type (target_type);
8b2dbe47 16166
e142c38c 16167 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16168 if (attr_byte_size)
16169 byte_size = DW_UNSND (attr_byte_size);
c906108c 16170 else
8b2dbe47
KB
16171 byte_size = cu_header->addr_size;
16172
e142c38c 16173 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16174 if (attr_address_class)
16175 addr_class = DW_UNSND (attr_address_class);
16176 else
16177 addr_class = DW_ADDR_none;
16178
2b4424c3
TT
16179 ULONGEST alignment = get_alignment (cu, die);
16180
16181 /* If the pointer size, alignment, or address class is different
16182 than the default, create a type variant marked as such and set
16183 the length accordingly. */
16184 if (TYPE_LENGTH (type) != byte_size
16185 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16186 && alignment != TYPE_RAW_ALIGN (type))
16187 || addr_class != DW_ADDR_none)
c906108c 16188 {
5e2b427d 16189 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16190 {
16191 int type_flags;
16192
849957d9 16193 type_flags = gdbarch_address_class_type_flags
5e2b427d 16194 (gdbarch, byte_size, addr_class);
876cecd0
TT
16195 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16196 == 0);
8b2dbe47
KB
16197 type = make_type_with_address_space (type, type_flags);
16198 }
16199 else if (TYPE_LENGTH (type) != byte_size)
16200 {
b98664d3 16201 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16202 }
2b4424c3
TT
16203 else if (TYPE_RAW_ALIGN (type) != alignment)
16204 {
b98664d3 16205 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16206 " - DIE at %s [in module %s]"),
16207 sect_offset_str (die->sect_off),
16208 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16209 }
6e70227d 16210 else
9a619af0
MS
16211 {
16212 /* Should we also complain about unhandled address classes? */
16213 }
c906108c 16214 }
8b2dbe47
KB
16215
16216 TYPE_LENGTH (type) = byte_size;
2b4424c3 16217 set_type_align (type, alignment);
f792889a 16218 return set_die_type (die, type, cu);
c906108c
SS
16219}
16220
16221/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16222 the user defined type vector. */
16223
f792889a 16224static struct type *
e7c27a73 16225read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16226{
16227 struct type *type;
16228 struct type *to_type;
16229 struct type *domain;
16230
e7c27a73
DJ
16231 to_type = die_type (die, cu);
16232 domain = die_containing_type (die, cu);
0d5de010 16233
7e314c57
JK
16234 /* The calls above may have already set the type for this DIE. */
16235 type = get_die_type (die, cu);
16236 if (type)
16237 return type;
16238
0d5de010
DJ
16239 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16240 type = lookup_methodptr_type (to_type);
7078baeb
TT
16241 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16242 {
518817b3
SM
16243 struct type *new_type
16244 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16245
16246 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16247 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16248 TYPE_VARARGS (to_type));
16249 type = lookup_methodptr_type (new_type);
16250 }
0d5de010
DJ
16251 else
16252 type = lookup_memberptr_type (to_type, domain);
c906108c 16253
f792889a 16254 return set_die_type (die, type, cu);
c906108c
SS
16255}
16256
4297a3f0 16257/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16258 the user defined type vector. */
16259
f792889a 16260static struct type *
4297a3f0
AV
16261read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16262 enum type_code refcode)
c906108c 16263{
e7c27a73 16264 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16265 struct type *type, *target_type;
c906108c
SS
16266 struct attribute *attr;
16267
4297a3f0
AV
16268 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16269
7e314c57
JK
16270 target_type = die_type (die, cu);
16271
16272 /* The die_type call above may have already set the type for this DIE. */
16273 type = get_die_type (die, cu);
16274 if (type)
16275 return type;
16276
4297a3f0 16277 type = lookup_reference_type (target_type, refcode);
e142c38c 16278 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16279 if (attr != nullptr)
c906108c
SS
16280 {
16281 TYPE_LENGTH (type) = DW_UNSND (attr);
16282 }
16283 else
16284 {
107d2387 16285 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16286 }
2b4424c3 16287 maybe_set_alignment (cu, die, type);
f792889a 16288 return set_die_type (die, type, cu);
c906108c
SS
16289}
16290
cf363f18
MW
16291/* Add the given cv-qualifiers to the element type of the array. GCC
16292 outputs DWARF type qualifiers that apply to an array, not the
16293 element type. But GDB relies on the array element type to carry
16294 the cv-qualifiers. This mimics section 6.7.3 of the C99
16295 specification. */
16296
16297static struct type *
16298add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16299 struct type *base_type, int cnst, int voltl)
16300{
16301 struct type *el_type, *inner_array;
16302
16303 base_type = copy_type (base_type);
16304 inner_array = base_type;
16305
16306 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16307 {
16308 TYPE_TARGET_TYPE (inner_array) =
16309 copy_type (TYPE_TARGET_TYPE (inner_array));
16310 inner_array = TYPE_TARGET_TYPE (inner_array);
16311 }
16312
16313 el_type = TYPE_TARGET_TYPE (inner_array);
16314 cnst |= TYPE_CONST (el_type);
16315 voltl |= TYPE_VOLATILE (el_type);
16316 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16317
16318 return set_die_type (die, base_type, cu);
16319}
16320
f792889a 16321static struct type *
e7c27a73 16322read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16323{
f792889a 16324 struct type *base_type, *cv_type;
c906108c 16325
e7c27a73 16326 base_type = die_type (die, cu);
7e314c57
JK
16327
16328 /* The die_type call above may have already set the type for this DIE. */
16329 cv_type = get_die_type (die, cu);
16330 if (cv_type)
16331 return cv_type;
16332
2f608a3a
KW
16333 /* In case the const qualifier is applied to an array type, the element type
16334 is so qualified, not the array type (section 6.7.3 of C99). */
16335 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16336 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16337
f792889a
DJ
16338 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16339 return set_die_type (die, cv_type, cu);
c906108c
SS
16340}
16341
f792889a 16342static struct type *
e7c27a73 16343read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16344{
f792889a 16345 struct type *base_type, *cv_type;
c906108c 16346
e7c27a73 16347 base_type = die_type (die, cu);
7e314c57
JK
16348
16349 /* The die_type call above may have already set the type for this DIE. */
16350 cv_type = get_die_type (die, cu);
16351 if (cv_type)
16352 return cv_type;
16353
cf363f18
MW
16354 /* In case the volatile qualifier is applied to an array type, the
16355 element type is so qualified, not the array type (section 6.7.3
16356 of C99). */
16357 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16358 return add_array_cv_type (die, cu, base_type, 0, 1);
16359
f792889a
DJ
16360 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16361 return set_die_type (die, cv_type, cu);
c906108c
SS
16362}
16363
06d66ee9
TT
16364/* Handle DW_TAG_restrict_type. */
16365
16366static struct type *
16367read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16368{
16369 struct type *base_type, *cv_type;
16370
16371 base_type = die_type (die, cu);
16372
16373 /* The die_type call above may have already set the type for this DIE. */
16374 cv_type = get_die_type (die, cu);
16375 if (cv_type)
16376 return cv_type;
16377
16378 cv_type = make_restrict_type (base_type);
16379 return set_die_type (die, cv_type, cu);
16380}
16381
a2c2acaf
MW
16382/* Handle DW_TAG_atomic_type. */
16383
16384static struct type *
16385read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16386{
16387 struct type *base_type, *cv_type;
16388
16389 base_type = die_type (die, cu);
16390
16391 /* The die_type call above may have already set the type for this DIE. */
16392 cv_type = get_die_type (die, cu);
16393 if (cv_type)
16394 return cv_type;
16395
16396 cv_type = make_atomic_type (base_type);
16397 return set_die_type (die, cv_type, cu);
16398}
16399
c906108c
SS
16400/* Extract all information from a DW_TAG_string_type DIE and add to
16401 the user defined type vector. It isn't really a user defined type,
16402 but it behaves like one, with other DIE's using an AT_user_def_type
16403 attribute to reference it. */
16404
f792889a 16405static struct type *
e7c27a73 16406read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16407{
518817b3 16408 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16409 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16410 struct type *type, *range_type, *index_type, *char_type;
16411 struct attribute *attr;
216a7e6b
AB
16412 struct dynamic_prop prop;
16413 bool length_is_constant = true;
16414 LONGEST length;
16415
16416 /* There are a couple of places where bit sizes might be made use of
16417 when parsing a DW_TAG_string_type, however, no producer that we know
16418 of make use of these. Handling bit sizes that are a multiple of the
16419 byte size is easy enough, but what about other bit sizes? Lets deal
16420 with that problem when we have to. Warn about these attributes being
16421 unsupported, then parse the type and ignore them like we always
16422 have. */
16423 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16424 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16425 {
16426 static bool warning_printed = false;
16427 if (!warning_printed)
16428 {
16429 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16430 "currently supported on DW_TAG_string_type."));
16431 warning_printed = true;
16432 }
16433 }
c906108c 16434
e142c38c 16435 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16436 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16437 {
16438 /* The string length describes the location at which the length of
16439 the string can be found. The size of the length field can be
16440 specified with one of the attributes below. */
16441 struct type *prop_type;
16442 struct attribute *len
16443 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16444 if (len == nullptr)
16445 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16446 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16447 {
16448 /* Pass 0 as the default as we know this attribute is constant
16449 and the default value will not be returned. */
0826b30a 16450 LONGEST sz = len->constant_value (0);
09ba997f 16451 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16452 }
16453 else
16454 {
16455 /* If the size is not specified then we assume it is the size of
16456 an address on this target. */
09ba997f 16457 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16458 }
16459
16460 /* Convert the attribute into a dynamic property. */
16461 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16462 length = 1;
16463 else
16464 length_is_constant = false;
16465 }
16466 else if (attr != nullptr)
16467 {
16468 /* This DW_AT_string_length just contains the length with no
16469 indirection. There's no need to create a dynamic property in this
16470 case. Pass 0 for the default value as we know it will not be
16471 returned in this case. */
0826b30a 16472 length = attr->constant_value (0);
216a7e6b
AB
16473 }
16474 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16475 {
216a7e6b 16476 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16477 length = attr->constant_value (1);
c906108c
SS
16478 }
16479 else
16480 {
216a7e6b
AB
16481 /* Use 1 as a fallback length if we have nothing else. */
16482 length = 1;
c906108c 16483 }
6ccb9162 16484
46bf5051 16485 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16486 if (length_is_constant)
16487 range_type = create_static_range_type (NULL, index_type, 1, length);
16488 else
16489 {
16490 struct dynamic_prop low_bound;
16491
16492 low_bound.kind = PROP_CONST;
16493 low_bound.data.const_val = 1;
16494 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16495 }
3b7538c0
UW
16496 char_type = language_string_char_type (cu->language_defn, gdbarch);
16497 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16498
f792889a 16499 return set_die_type (die, type, cu);
c906108c
SS
16500}
16501
4d804846
JB
16502/* Assuming that DIE corresponds to a function, returns nonzero
16503 if the function is prototyped. */
16504
16505static int
16506prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16507{
16508 struct attribute *attr;
16509
16510 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16511 if (attr && (DW_UNSND (attr) != 0))
16512 return 1;
16513
16514 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16515 is only meaningful for C, but the concept also extends to other
4d804846
JB
16516 languages that allow unprototyped functions (Eg: Objective C).
16517 For all other languages, assume that functions are always
16518 prototyped. */
16519 if (cu->language != language_c
16520 && cu->language != language_objc
16521 && cu->language != language_opencl)
16522 return 1;
16523
16524 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16525 prototyped and unprototyped functions; default to prototyped,
16526 since that is more common in modern code (and RealView warns
16527 about unprototyped functions). */
16528 if (producer_is_realview (cu->producer))
16529 return 1;
16530
16531 return 0;
16532}
16533
c906108c
SS
16534/* Handle DIES due to C code like:
16535
16536 struct foo
c5aa993b
JM
16537 {
16538 int (*funcp)(int a, long l);
16539 int b;
16540 };
c906108c 16541
0963b4bd 16542 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16543
f792889a 16544static struct type *
e7c27a73 16545read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16546{
518817b3 16547 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16548 struct type *type; /* Type that this function returns. */
16549 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16550 struct attribute *attr;
16551
e7c27a73 16552 type = die_type (die, cu);
7e314c57
JK
16553
16554 /* The die_type call above may have already set the type for this DIE. */
16555 ftype = get_die_type (die, cu);
16556 if (ftype)
16557 return ftype;
16558
0c8b41f1 16559 ftype = lookup_function_type (type);
c906108c 16560
4d804846 16561 if (prototyped_function_p (die, cu))
a6c727b2 16562 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16563
c055b101
CV
16564 /* Store the calling convention in the type if it's available in
16565 the subroutine die. Otherwise set the calling convention to
16566 the default value DW_CC_normal. */
16567 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16568 if (attr != nullptr
16569 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16570 TYPE_CALLING_CONVENTION (ftype)
16571 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16572 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16573 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16574 else
16575 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16576
743649fd
MW
16577 /* Record whether the function returns normally to its caller or not
16578 if the DWARF producer set that information. */
16579 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16580 if (attr && (DW_UNSND (attr) != 0))
16581 TYPE_NO_RETURN (ftype) = 1;
16582
76c10ea2
GM
16583 /* We need to add the subroutine type to the die immediately so
16584 we don't infinitely recurse when dealing with parameters
0963b4bd 16585 declared as the same subroutine type. */
76c10ea2 16586 set_die_type (die, ftype, cu);
6e70227d 16587
639d11d3 16588 if (die->child != NULL)
c906108c 16589 {
bb5ed363 16590 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16591 struct die_info *child_die;
8072405b 16592 int nparams, iparams;
c906108c
SS
16593
16594 /* Count the number of parameters.
16595 FIXME: GDB currently ignores vararg functions, but knows about
16596 vararg member functions. */
8072405b 16597 nparams = 0;
639d11d3 16598 child_die = die->child;
c906108c
SS
16599 while (child_die && child_die->tag)
16600 {
16601 if (child_die->tag == DW_TAG_formal_parameter)
16602 nparams++;
16603 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16604 TYPE_VARARGS (ftype) = 1;
436c571c 16605 child_die = child_die->sibling;
c906108c
SS
16606 }
16607
16608 /* Allocate storage for parameters and fill them in. */
16609 TYPE_NFIELDS (ftype) = nparams;
16610 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16611 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16612
8072405b
JK
16613 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16614 even if we error out during the parameters reading below. */
16615 for (iparams = 0; iparams < nparams; iparams++)
16616 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16617
16618 iparams = 0;
639d11d3 16619 child_die = die->child;
c906108c
SS
16620 while (child_die && child_die->tag)
16621 {
16622 if (child_die->tag == DW_TAG_formal_parameter)
16623 {
3ce3b1ba
PA
16624 struct type *arg_type;
16625
16626 /* DWARF version 2 has no clean way to discern C++
16627 static and non-static member functions. G++ helps
16628 GDB by marking the first parameter for non-static
16629 member functions (which is the this pointer) as
16630 artificial. We pass this information to
16631 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16632
16633 DWARF version 3 added DW_AT_object_pointer, which GCC
16634 4.5 does not yet generate. */
e142c38c 16635 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16636 if (attr != nullptr)
c906108c
SS
16637 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16638 else
9c37b5ae 16639 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16640 arg_type = die_type (child_die, cu);
16641
16642 /* RealView does not mark THIS as const, which the testsuite
16643 expects. GCC marks THIS as const in method definitions,
16644 but not in the class specifications (GCC PR 43053). */
16645 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16646 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16647 {
16648 int is_this = 0;
16649 struct dwarf2_cu *arg_cu = cu;
16650 const char *name = dwarf2_name (child_die, cu);
16651
16652 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16653 if (attr != nullptr)
3ce3b1ba
PA
16654 {
16655 /* If the compiler emits this, use it. */
16656 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16657 is_this = 1;
16658 }
16659 else if (name && strcmp (name, "this") == 0)
16660 /* Function definitions will have the argument names. */
16661 is_this = 1;
16662 else if (name == NULL && iparams == 0)
16663 /* Declarations may not have the names, so like
16664 elsewhere in GDB, assume an artificial first
16665 argument is "this". */
16666 is_this = 1;
16667
16668 if (is_this)
16669 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16670 arg_type, 0);
16671 }
16672
16673 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16674 iparams++;
16675 }
436c571c 16676 child_die = child_die->sibling;
c906108c
SS
16677 }
16678 }
16679
76c10ea2 16680 return ftype;
c906108c
SS
16681}
16682
f792889a 16683static struct type *
e7c27a73 16684read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16685{
518817b3 16686 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16687 const char *name = NULL;
3c8e0968 16688 struct type *this_type, *target_type;
c906108c 16689
94af9270 16690 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16691 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16692 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16693 set_die_type (die, this_type, cu);
3c8e0968
DE
16694 target_type = die_type (die, cu);
16695 if (target_type != this_type)
16696 TYPE_TARGET_TYPE (this_type) = target_type;
16697 else
16698 {
16699 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16700 spec and cause infinite loops in GDB. */
b98664d3 16701 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16702 "- DIE at %s [in module %s]"),
16703 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16704 TYPE_TARGET_TYPE (this_type) = NULL;
16705 }
e4003a34
TV
16706 if (name == NULL)
16707 {
16708 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16709 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16710 Handle these by just returning the target type, rather than
16711 constructing an anonymous typedef type and trying to handle this
16712 elsewhere. */
16713 set_die_type (die, target_type, cu);
16714 return target_type;
16715 }
f792889a 16716 return this_type;
c906108c
SS
16717}
16718
9b790ce7
UW
16719/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16720 (which may be different from NAME) to the architecture back-end to allow
16721 it to guess the correct format if necessary. */
16722
16723static struct type *
16724dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16725 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16726{
16727 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16728 const struct floatformat **format;
16729 struct type *type;
16730
16731 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16732 if (format)
103a685e 16733 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16734 else
77b7c781 16735 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16736
16737 return type;
16738}
16739
eb77c9df
AB
16740/* Allocate an integer type of size BITS and name NAME. */
16741
16742static struct type *
16743dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16744 int bits, int unsigned_p, const char *name)
16745{
16746 struct type *type;
16747
16748 /* Versions of Intel's C Compiler generate an integer type called "void"
16749 instead of using DW_TAG_unspecified_type. This has been seen on
16750 at least versions 14, 17, and 18. */
35ee2dc2
AB
16751 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16752 && strcmp (name, "void") == 0)
eb77c9df
AB
16753 type = objfile_type (objfile)->builtin_void;
16754 else
16755 type = init_integer_type (objfile, bits, unsigned_p, name);
16756
16757 return type;
16758}
16759
8bdc1658
AB
16760/* Initialise and return a floating point type of size BITS suitable for
16761 use as a component of a complex number. The NAME_HINT is passed through
16762 when initialising the floating point type and is the name of the complex
16763 type.
16764
16765 As DWARF doesn't currently provide an explicit name for the components
16766 of a complex number, but it can be helpful to have these components
16767 named, we try to select a suitable name based on the size of the
16768 component. */
16769static struct type *
16770dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16771 struct objfile *objfile,
103a685e
TT
16772 int bits, const char *name_hint,
16773 enum bfd_endian byte_order)
8bdc1658
AB
16774{
16775 gdbarch *gdbarch = get_objfile_arch (objfile);
16776 struct type *tt = nullptr;
16777
35add35e
AB
16778 /* Try to find a suitable floating point builtin type of size BITS.
16779 We're going to use the name of this type as the name for the complex
16780 target type that we are about to create. */
1db455a7 16781 switch (cu->language)
8bdc1658 16782 {
1db455a7
AB
16783 case language_fortran:
16784 switch (bits)
16785 {
16786 case 32:
16787 tt = builtin_f_type (gdbarch)->builtin_real;
16788 break;
16789 case 64:
16790 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16791 break;
16792 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16793 case 128:
16794 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16795 break;
16796 }
8bdc1658 16797 break;
1db455a7
AB
16798 default:
16799 switch (bits)
16800 {
16801 case 32:
16802 tt = builtin_type (gdbarch)->builtin_float;
16803 break;
16804 case 64:
16805 tt = builtin_type (gdbarch)->builtin_double;
16806 break;
16807 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16808 case 128:
16809 tt = builtin_type (gdbarch)->builtin_long_double;
16810 break;
16811 }
8bdc1658
AB
16812 break;
16813 }
16814
35add35e
AB
16815 /* If the type we found doesn't match the size we were looking for, then
16816 pretend we didn't find a type at all, the complex target type we
16817 create will then be nameless. */
a12e5744 16818 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16819 tt = nullptr;
16820
8bdc1658 16821 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16822 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16823}
16824
c906108c
SS
16825/* Find a representation of a given base type and install
16826 it in the TYPE field of the die. */
16827
f792889a 16828static struct type *
e7c27a73 16829read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16830{
518817b3 16831 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16832 struct type *type;
16833 struct attribute *attr;
19f392bc 16834 int encoding = 0, bits = 0;
15d034d0 16835 const char *name;
34877895 16836 gdbarch *arch;
c906108c 16837
e142c38c 16838 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16839 if (attr != nullptr)
34877895 16840 encoding = DW_UNSND (attr);
e142c38c 16841 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16842 if (attr != nullptr)
34877895 16843 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16844 name = dwarf2_name (die, cu);
6ccb9162 16845 if (!name)
34877895 16846 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16847
16848 arch = get_objfile_arch (objfile);
16849 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16850
34877895
PJ
16851 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16852 if (attr)
103a685e
TT
16853 {
16854 int endianity = DW_UNSND (attr);
16855
16856 switch (endianity)
16857 {
16858 case DW_END_big:
16859 byte_order = BFD_ENDIAN_BIG;
16860 break;
16861 case DW_END_little:
16862 byte_order = BFD_ENDIAN_LITTLE;
16863 break;
16864 default:
16865 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16866 break;
16867 }
16868 }
6ccb9162
UW
16869
16870 switch (encoding)
c906108c 16871 {
6ccb9162
UW
16872 case DW_ATE_address:
16873 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16874 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16875 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16876 break;
16877 case DW_ATE_boolean:
19f392bc 16878 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16879 break;
16880 case DW_ATE_complex_float:
103a685e
TT
16881 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16882 byte_order);
19f392bc 16883 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16884 break;
16885 case DW_ATE_decimal_float:
19f392bc 16886 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16887 break;
16888 case DW_ATE_float:
103a685e 16889 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16890 break;
16891 case DW_ATE_signed:
eb77c9df 16892 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16893 break;
16894 case DW_ATE_unsigned:
3b2b8fea
TT
16895 if (cu->language == language_fortran
16896 && name
61012eef 16897 && startswith (name, "character("))
19f392bc
UW
16898 type = init_character_type (objfile, bits, 1, name);
16899 else
eb77c9df 16900 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
16901 break;
16902 case DW_ATE_signed_char:
6e70227d 16903 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16904 || cu->language == language_pascal
16905 || cu->language == language_fortran)
19f392bc
UW
16906 type = init_character_type (objfile, bits, 0, name);
16907 else
eb77c9df 16908 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16909 break;
16910 case DW_ATE_unsigned_char:
868a0084 16911 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16912 || cu->language == language_pascal
c44af4eb
TT
16913 || cu->language == language_fortran
16914 || cu->language == language_rust)
19f392bc
UW
16915 type = init_character_type (objfile, bits, 1, name);
16916 else
eb77c9df 16917 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 16918 break;
75079b2b 16919 case DW_ATE_UTF:
53e710ac 16920 {
53e710ac
PA
16921 if (bits == 16)
16922 type = builtin_type (arch)->builtin_char16;
16923 else if (bits == 32)
16924 type = builtin_type (arch)->builtin_char32;
16925 else
16926 {
b98664d3 16927 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 16928 bits);
eb77c9df 16929 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
16930 }
16931 return set_die_type (die, type, cu);
16932 }
75079b2b
TT
16933 break;
16934
6ccb9162 16935 default:
b98664d3 16936 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 16937 dwarf_type_encoding_name (encoding));
77b7c781 16938 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 16939 break;
c906108c 16940 }
6ccb9162 16941
0114d602 16942 if (name && strcmp (name, "char") == 0)
876cecd0 16943 TYPE_NOSIGN (type) = 1;
0114d602 16944
2b4424c3
TT
16945 maybe_set_alignment (cu, die, type);
16946
103a685e 16947 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 16948
f792889a 16949 return set_die_type (die, type, cu);
c906108c
SS
16950}
16951
80180f79
SA
16952/* Parse dwarf attribute if it's a block, reference or constant and put the
16953 resulting value of the attribute into struct bound_prop.
16954 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16955
16956static int
16957attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
16958 struct dwarf2_cu *cu, struct dynamic_prop *prop,
16959 struct type *default_type)
80180f79
SA
16960{
16961 struct dwarf2_property_baton *baton;
518817b3
SM
16962 struct obstack *obstack
16963 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 16964
9a49df9d
AB
16965 gdb_assert (default_type != NULL);
16966
80180f79
SA
16967 if (attr == NULL || prop == NULL)
16968 return 0;
16969
4fc6c0d5 16970 if (attr->form_is_block ())
80180f79 16971 {
8d749320 16972 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 16973 baton->property_type = default_type;
80180f79
SA
16974 baton->locexpr.per_cu = cu->per_cu;
16975 baton->locexpr.size = DW_BLOCK (attr)->size;
16976 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
16977 switch (attr->name)
16978 {
16979 case DW_AT_string_length:
16980 baton->locexpr.is_reference = true;
16981 break;
16982 default:
16983 baton->locexpr.is_reference = false;
16984 break;
16985 }
80180f79
SA
16986 prop->data.baton = baton;
16987 prop->kind = PROP_LOCEXPR;
16988 gdb_assert (prop->data.baton != NULL);
16989 }
cd6c91b4 16990 else if (attr->form_is_ref ())
80180f79
SA
16991 {
16992 struct dwarf2_cu *target_cu = cu;
16993 struct die_info *target_die;
16994 struct attribute *target_attr;
16995
16996 target_die = follow_die_ref (die, attr, &target_cu);
16997 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
16998 if (target_attr == NULL)
16999 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17000 target_cu);
80180f79
SA
17001 if (target_attr == NULL)
17002 return 0;
17003
df25ebbd 17004 switch (target_attr->name)
80180f79 17005 {
df25ebbd 17006 case DW_AT_location:
cd6c91b4 17007 if (target_attr->form_is_section_offset ())
df25ebbd 17008 {
8d749320 17009 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17010 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17011 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17012 prop->data.baton = baton;
17013 prop->kind = PROP_LOCLIST;
17014 gdb_assert (prop->data.baton != NULL);
17015 }
4fc6c0d5 17016 else if (target_attr->form_is_block ())
df25ebbd 17017 {
8d749320 17018 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17019 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17020 baton->locexpr.per_cu = cu->per_cu;
17021 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17022 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17023 baton->locexpr.is_reference = true;
df25ebbd
JB
17024 prop->data.baton = baton;
17025 prop->kind = PROP_LOCEXPR;
17026 gdb_assert (prop->data.baton != NULL);
17027 }
17028 else
17029 {
17030 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17031 "dynamic property");
17032 return 0;
17033 }
17034 break;
17035 case DW_AT_data_member_location:
17036 {
17037 LONGEST offset;
17038
17039 if (!handle_data_member_location (target_die, target_cu,
17040 &offset))
17041 return 0;
17042
8d749320 17043 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17044 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17045 target_cu);
df25ebbd
JB
17046 baton->offset_info.offset = offset;
17047 baton->offset_info.type = die_type (target_die, target_cu);
17048 prop->data.baton = baton;
17049 prop->kind = PROP_ADDR_OFFSET;
17050 break;
17051 }
80180f79
SA
17052 }
17053 }
cd6c91b4 17054 else if (attr->form_is_constant ())
80180f79 17055 {
0826b30a 17056 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17057 prop->kind = PROP_CONST;
17058 }
17059 else
17060 {
17061 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17062 dwarf2_name (die, cu));
17063 return 0;
17064 }
17065
17066 return 1;
17067}
17068
09ba997f 17069/* See read.h. */
9a49df9d 17070
09ba997f
TT
17071struct type *
17072dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17073{
09ba997f 17074 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17075 struct type *int_type;
17076
17077 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17078#define TRY_TYPE(F) \
17079 int_type = (unsigned_p \
17080 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17081 : objfile_type (objfile)->builtin_ ## F); \
17082 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17083 return int_type
17084
17085 TRY_TYPE (char);
17086 TRY_TYPE (short);
17087 TRY_TYPE (int);
17088 TRY_TYPE (long);
17089 TRY_TYPE (long_long);
17090
17091#undef TRY_TYPE
17092
17093 gdb_assert_not_reached ("unable to find suitable integer type");
17094}
17095
09ba997f 17096/* See read.h. */
11a8b164 17097
09ba997f
TT
17098struct type *
17099dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17100{
09ba997f
TT
17101 int addr_size = this->addr_size ();
17102 return int_type (addr_size, unsigned_p);
11a8b164
AB
17103}
17104
b86352cf
AB
17105/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17106 present (which is valid) then compute the default type based on the
17107 compilation units address size. */
17108
17109static struct type *
17110read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17111{
17112 struct type *index_type = die_type (die, cu);
17113
17114 /* Dwarf-2 specifications explicitly allows to create subrange types
17115 without specifying a base type.
17116 In that case, the base type must be set to the type of
17117 the lower bound, upper bound or count, in that order, if any of these
17118 three attributes references an object that has a type.
17119 If no base type is found, the Dwarf-2 specifications say that
17120 a signed integer type of size equal to the size of an address should
17121 be used.
17122 For the following C code: `extern char gdb_int [];'
17123 GCC produces an empty range DIE.
17124 FIXME: muller/2010-05-28: Possible references to object for low bound,
17125 high bound or count are not yet handled by this code. */
17126 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17127 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17128
17129 return index_type;
17130}
17131
a02abb62
JB
17132/* Read the given DW_AT_subrange DIE. */
17133
f792889a 17134static struct type *
a02abb62
JB
17135read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17136{
4c9ad8c2 17137 struct type *base_type, *orig_base_type;
a02abb62
JB
17138 struct type *range_type;
17139 struct attribute *attr;
729efb13 17140 struct dynamic_prop low, high;
4fae6e18 17141 int low_default_is_valid;
c451ebe5 17142 int high_bound_is_count = 0;
15d034d0 17143 const char *name;
d359392f 17144 ULONGEST negative_mask;
e77813c8 17145
b86352cf
AB
17146 orig_base_type = read_subrange_index_type (die, cu);
17147
4c9ad8c2
TT
17148 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17149 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17150 creating the range type, but we use the result of check_typedef
17151 when examining properties of the type. */
17152 base_type = check_typedef (orig_base_type);
a02abb62 17153
7e314c57
JK
17154 /* The die_type call above may have already set the type for this DIE. */
17155 range_type = get_die_type (die, cu);
17156 if (range_type)
17157 return range_type;
17158
729efb13
SA
17159 low.kind = PROP_CONST;
17160 high.kind = PROP_CONST;
17161 high.data.const_val = 0;
17162
4fae6e18
JK
17163 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17164 omitting DW_AT_lower_bound. */
17165 switch (cu->language)
6e70227d 17166 {
4fae6e18
JK
17167 case language_c:
17168 case language_cplus:
729efb13 17169 low.data.const_val = 0;
4fae6e18
JK
17170 low_default_is_valid = 1;
17171 break;
17172 case language_fortran:
729efb13 17173 low.data.const_val = 1;
4fae6e18
JK
17174 low_default_is_valid = 1;
17175 break;
17176 case language_d:
4fae6e18 17177 case language_objc:
c44af4eb 17178 case language_rust:
729efb13 17179 low.data.const_val = 0;
4fae6e18
JK
17180 low_default_is_valid = (cu->header.version >= 4);
17181 break;
17182 case language_ada:
17183 case language_m2:
17184 case language_pascal:
729efb13 17185 low.data.const_val = 1;
4fae6e18
JK
17186 low_default_is_valid = (cu->header.version >= 4);
17187 break;
17188 default:
729efb13 17189 low.data.const_val = 0;
4fae6e18
JK
17190 low_default_is_valid = 0;
17191 break;
a02abb62
JB
17192 }
17193
e142c38c 17194 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17195 if (attr != nullptr)
9a49df9d 17196 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17197 else if (!low_default_is_valid)
b98664d3 17198 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17199 "- DIE at %s [in module %s]"),
17200 sect_offset_str (die->sect_off),
518817b3 17201 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17202
506f5c41
TV
17203 struct attribute *attr_ub, *attr_count;
17204 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17205 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17206 {
506f5c41 17207 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17208 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17209 {
c451ebe5
SA
17210 /* If bounds are constant do the final calculation here. */
17211 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17212 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17213 else
17214 high_bound_is_count = 1;
c2ff108b 17215 }
506f5c41
TV
17216 else
17217 {
17218 if (attr_ub != NULL)
17219 complaint (_("Unresolved DW_AT_upper_bound "
17220 "- DIE at %s [in module %s]"),
17221 sect_offset_str (die->sect_off),
17222 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17223 if (attr_count != NULL)
17224 complaint (_("Unresolved DW_AT_count "
17225 "- DIE at %s [in module %s]"),
17226 sect_offset_str (die->sect_off),
17227 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17228 }
e77813c8 17229 }
a02abb62 17230
4e962e74
TT
17231 LONGEST bias = 0;
17232 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17233 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17234 bias = bias_attr->constant_value (0);
4e962e74 17235
dbb9c2b1
JB
17236 /* Normally, the DWARF producers are expected to use a signed
17237 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17238 But this is unfortunately not always the case, as witnessed
17239 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17240 is used instead. To work around that ambiguity, we treat
17241 the bounds as signed, and thus sign-extend their values, when
17242 the base type is signed. */
6e70227d 17243 negative_mask =
d359392f 17244 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17245 if (low.kind == PROP_CONST
17246 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17247 low.data.const_val |= negative_mask;
17248 if (high.kind == PROP_CONST
17249 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17250 high.data.const_val |= negative_mask;
43bbcdc2 17251
5bbd8269
AB
17252 /* Check for bit and byte strides. */
17253 struct dynamic_prop byte_stride_prop;
17254 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17255 if (attr_byte_stride != nullptr)
17256 {
09ba997f 17257 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17258 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17259 prop_type);
17260 }
17261
17262 struct dynamic_prop bit_stride_prop;
17263 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17264 if (attr_bit_stride != nullptr)
17265 {
17266 /* It only makes sense to have either a bit or byte stride. */
17267 if (attr_byte_stride != nullptr)
17268 {
17269 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17270 "- DIE at %s [in module %s]"),
17271 sect_offset_str (die->sect_off),
17272 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17273 attr_bit_stride = nullptr;
17274 }
17275 else
17276 {
09ba997f 17277 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17278 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17279 prop_type);
17280 }
17281 }
17282
17283 if (attr_byte_stride != nullptr
17284 || attr_bit_stride != nullptr)
17285 {
17286 bool byte_stride_p = (attr_byte_stride != nullptr);
17287 struct dynamic_prop *stride
17288 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17289
17290 range_type
17291 = create_range_type_with_stride (NULL, orig_base_type, &low,
17292 &high, bias, stride, byte_stride_p);
17293 }
17294 else
17295 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17296
c451ebe5
SA
17297 if (high_bound_is_count)
17298 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17299
c2ff108b
JK
17300 /* Ada expects an empty array on no boundary attributes. */
17301 if (attr == NULL && cu->language != language_ada)
729efb13 17302 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17303
39cbfefa
DJ
17304 name = dwarf2_name (die, cu);
17305 if (name)
17306 TYPE_NAME (range_type) = name;
6e70227d 17307
e142c38c 17308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17309 if (attr != nullptr)
a02abb62
JB
17310 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17311
2b4424c3
TT
17312 maybe_set_alignment (cu, die, range_type);
17313
7e314c57
JK
17314 set_die_type (die, range_type, cu);
17315
17316 /* set_die_type should be already done. */
b4ba55a1
JB
17317 set_descriptive_type (range_type, die, cu);
17318
7e314c57 17319 return range_type;
a02abb62 17320}
6e70227d 17321
f792889a 17322static struct type *
81a17f79
JB
17323read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17324{
17325 struct type *type;
81a17f79 17326
518817b3
SM
17327 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17328 NULL);
0114d602 17329 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17330
74a2f8ff 17331 /* In Ada, an unspecified type is typically used when the description
85102364 17332 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17333 such a type, we treat it as a stub, and try to resolve it later on,
17334 when needed. */
17335 if (cu->language == language_ada)
17336 TYPE_STUB (type) = 1;
17337
f792889a 17338 return set_die_type (die, type, cu);
81a17f79 17339}
a02abb62 17340
639d11d3
DC
17341/* Read a single die and all its descendents. Set the die's sibling
17342 field to NULL; set other fields in the die correctly, and set all
17343 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17344 location of the info_ptr after reading all of those dies. PARENT
17345 is the parent of the die in question. */
17346
17347static struct die_info *
dee91e82 17348read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17349 const gdb_byte *info_ptr,
17350 const gdb_byte **new_info_ptr,
dee91e82 17351 struct die_info *parent)
639d11d3
DC
17352{
17353 struct die_info *die;
d521ce57 17354 const gdb_byte *cur_ptr;
639d11d3 17355
3e225074 17356 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17357 if (die == NULL)
17358 {
17359 *new_info_ptr = cur_ptr;
17360 return NULL;
17361 }
93311388 17362 store_in_ref_table (die, reader->cu);
639d11d3 17363
3e225074 17364 if (die->has_children)
bf6af496 17365 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17366 else
17367 {
17368 die->child = NULL;
17369 *new_info_ptr = cur_ptr;
17370 }
17371
17372 die->sibling = NULL;
17373 die->parent = parent;
17374 return die;
17375}
17376
17377/* Read a die, all of its descendents, and all of its siblings; set
17378 all of the fields of all of the dies correctly. Arguments are as
17379 in read_die_and_children. */
17380
17381static struct die_info *
bf6af496 17382read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17383 const gdb_byte *info_ptr,
17384 const gdb_byte **new_info_ptr,
bf6af496 17385 struct die_info *parent)
639d11d3
DC
17386{
17387 struct die_info *first_die, *last_sibling;
d521ce57 17388 const gdb_byte *cur_ptr;
639d11d3 17389
c906108c 17390 cur_ptr = info_ptr;
639d11d3
DC
17391 first_die = last_sibling = NULL;
17392
17393 while (1)
c906108c 17394 {
639d11d3 17395 struct die_info *die
dee91e82 17396 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17397
1d325ec1 17398 if (die == NULL)
c906108c 17399 {
639d11d3
DC
17400 *new_info_ptr = cur_ptr;
17401 return first_die;
c906108c 17402 }
1d325ec1
DJ
17403
17404 if (!first_die)
17405 first_die = die;
c906108c 17406 else
1d325ec1
DJ
17407 last_sibling->sibling = die;
17408
17409 last_sibling = die;
c906108c 17410 }
c906108c
SS
17411}
17412
bf6af496
DE
17413/* Read a die, all of its descendents, and all of its siblings; set
17414 all of the fields of all of the dies correctly. Arguments are as
17415 in read_die_and_children.
17416 This the main entry point for reading a DIE and all its children. */
17417
17418static struct die_info *
17419read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17420 const gdb_byte *info_ptr,
17421 const gdb_byte **new_info_ptr,
bf6af496
DE
17422 struct die_info *parent)
17423{
17424 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17425 new_info_ptr, parent);
17426
b4f54984 17427 if (dwarf_die_debug)
bf6af496
DE
17428 {
17429 fprintf_unfiltered (gdb_stdlog,
17430 "Read die from %s@0x%x of %s:\n",
96b79293 17431 reader->die_section->get_name (),
bf6af496
DE
17432 (unsigned) (info_ptr - reader->die_section->buffer),
17433 bfd_get_filename (reader->abfd));
b4f54984 17434 dump_die (die, dwarf_die_debug);
bf6af496
DE
17435 }
17436
17437 return die;
17438}
17439
3019eac3
DE
17440/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17441 attributes.
17442 The caller is responsible for filling in the extra attributes
17443 and updating (*DIEP)->num_attrs.
17444 Set DIEP to point to a newly allocated die with its information,
3e225074 17445 except for its child, sibling, and parent fields. */
93311388 17446
d521ce57 17447static const gdb_byte *
3019eac3 17448read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17449 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17450 int num_extra_attrs)
93311388 17451{
b64f50a1 17452 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17453 struct abbrev_info *abbrev;
17454 struct die_info *die;
17455 struct dwarf2_cu *cu = reader->cu;
17456 bfd *abfd = reader->abfd;
17457
9c541725 17458 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17459 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17460 info_ptr += bytes_read;
17461 if (!abbrev_number)
17462 {
17463 *diep = NULL;
93311388
DE
17464 return info_ptr;
17465 }
17466
685af9cd 17467 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17468 if (!abbrev)
348e048f
DE
17469 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17470 abbrev_number,
17471 bfd_get_filename (abfd));
17472
3019eac3 17473 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17474 die->sect_off = sect_off;
93311388
DE
17475 die->tag = abbrev->tag;
17476 die->abbrev = abbrev_number;
3e225074 17477 die->has_children = abbrev->has_children;
93311388 17478
3019eac3
DE
17479 /* Make the result usable.
17480 The caller needs to update num_attrs after adding the extra
17481 attributes. */
93311388
DE
17482 die->num_attrs = abbrev->num_attrs;
17483
18a8505e 17484 std::vector<int> indexes_that_need_reprocess;
93311388 17485 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17486 {
17487 bool need_reprocess;
17488 info_ptr =
17489 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17490 info_ptr, &need_reprocess);
17491 if (need_reprocess)
17492 indexes_that_need_reprocess.push_back (i);
17493 }
17494
052c8bb8 17495 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
17496 if (attr != nullptr)
17497 cu->str_offsets_base = DW_UNSND (attr);
93311388 17498
a39fdb41 17499 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
17500 if (maybe_addr_base.has_value ())
17501 cu->addr_base = *maybe_addr_base;
17502 for (int index : indexes_that_need_reprocess)
17503 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17504 *diep = die;
93311388
DE
17505 return info_ptr;
17506}
17507
3019eac3
DE
17508/* Read a die and all its attributes.
17509 Set DIEP to point to a newly allocated die with its information,
3e225074 17510 except for its child, sibling, and parent fields. */
3019eac3 17511
d521ce57 17512static const gdb_byte *
3019eac3 17513read_full_die (const struct die_reader_specs *reader,
3e225074 17514 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17515{
d521ce57 17516 const gdb_byte *result;
bf6af496 17517
3e225074 17518 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17519
b4f54984 17520 if (dwarf_die_debug)
bf6af496
DE
17521 {
17522 fprintf_unfiltered (gdb_stdlog,
17523 "Read die from %s@0x%x of %s:\n",
96b79293 17524 reader->die_section->get_name (),
bf6af496
DE
17525 (unsigned) (info_ptr - reader->die_section->buffer),
17526 bfd_get_filename (reader->abfd));
b4f54984 17527 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17528 }
17529
17530 return result;
3019eac3 17531}
433df2d4 17532\f
c906108c 17533
72bf9492
DJ
17534/* Returns nonzero if TAG represents a type that we might generate a partial
17535 symbol for. */
17536
17537static int
17538is_type_tag_for_partial (int tag)
17539{
17540 switch (tag)
17541 {
17542#if 0
17543 /* Some types that would be reasonable to generate partial symbols for,
17544 that we don't at present. */
17545 case DW_TAG_array_type:
17546 case DW_TAG_file_type:
17547 case DW_TAG_ptr_to_member_type:
17548 case DW_TAG_set_type:
17549 case DW_TAG_string_type:
17550 case DW_TAG_subroutine_type:
17551#endif
17552 case DW_TAG_base_type:
17553 case DW_TAG_class_type:
680b30c7 17554 case DW_TAG_interface_type:
72bf9492
DJ
17555 case DW_TAG_enumeration_type:
17556 case DW_TAG_structure_type:
17557 case DW_TAG_subrange_type:
17558 case DW_TAG_typedef:
17559 case DW_TAG_union_type:
17560 return 1;
17561 default:
17562 return 0;
17563 }
17564}
17565
17566/* Load all DIEs that are interesting for partial symbols into memory. */
17567
17568static struct partial_die_info *
dee91e82 17569load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17570 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17571{
dee91e82 17572 struct dwarf2_cu *cu = reader->cu;
518817b3 17573 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17574 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17575 unsigned int bytes_read;
5afb4e99 17576 unsigned int load_all = 0;
72bf9492
DJ
17577 int nesting_level = 1;
17578
17579 parent_die = NULL;
17580 last_die = NULL;
17581
7adf1e79
DE
17582 gdb_assert (cu->per_cu != NULL);
17583 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17584 load_all = 1;
17585
72bf9492
DJ
17586 cu->partial_dies
17587 = htab_create_alloc_ex (cu->header.length / 12,
17588 partial_die_hash,
17589 partial_die_eq,
17590 NULL,
17591 &cu->comp_unit_obstack,
17592 hashtab_obstack_allocate,
17593 dummy_obstack_deallocate);
17594
72bf9492
DJ
17595 while (1)
17596 {
685af9cd 17597 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17598
17599 /* A NULL abbrev means the end of a series of children. */
17600 if (abbrev == NULL)
17601 {
17602 if (--nesting_level == 0)
cd9983dd
YQ
17603 return first_die;
17604
72bf9492
DJ
17605 info_ptr += bytes_read;
17606 last_die = parent_die;
17607 parent_die = parent_die->die_parent;
17608 continue;
17609 }
17610
98bfdba5
PA
17611 /* Check for template arguments. We never save these; if
17612 they're seen, we just mark the parent, and go on our way. */
17613 if (parent_die != NULL
17614 && cu->language == language_cplus
17615 && (abbrev->tag == DW_TAG_template_type_param
17616 || abbrev->tag == DW_TAG_template_value_param))
17617 {
17618 parent_die->has_template_arguments = 1;
17619
17620 if (!load_all)
17621 {
17622 /* We don't need a partial DIE for the template argument. */
dee91e82 17623 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17624 continue;
17625 }
17626 }
17627
0d99eb77 17628 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17629 Skip their other children. */
17630 if (!load_all
17631 && cu->language == language_cplus
17632 && parent_die != NULL
17633 && parent_die->tag == DW_TAG_subprogram)
17634 {
dee91e82 17635 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17636 continue;
17637 }
17638
5afb4e99
DJ
17639 /* Check whether this DIE is interesting enough to save. Normally
17640 we would not be interested in members here, but there may be
17641 later variables referencing them via DW_AT_specification (for
17642 static members). */
17643 if (!load_all
17644 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17645 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17646 && abbrev->tag != DW_TAG_enumerator
17647 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17648 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17649 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17650 && abbrev->tag != DW_TAG_variable
5afb4e99 17651 && abbrev->tag != DW_TAG_namespace
f55ee35c 17652 && abbrev->tag != DW_TAG_module
95554aad 17653 && abbrev->tag != DW_TAG_member
74921315
KS
17654 && abbrev->tag != DW_TAG_imported_unit
17655 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17656 {
17657 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17658 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17659 continue;
17660 }
17661
6f06d47b
YQ
17662 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17663 abbrev);
cd9983dd 17664
48fbe735 17665 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17666
17667 /* This two-pass algorithm for processing partial symbols has a
17668 high cost in cache pressure. Thus, handle some simple cases
17669 here which cover the majority of C partial symbols. DIEs
17670 which neither have specification tags in them, nor could have
17671 specification tags elsewhere pointing at them, can simply be
17672 processed and discarded.
17673
17674 This segment is also optional; scan_partial_symbols and
17675 add_partial_symbol will handle these DIEs if we chain
17676 them in normally. When compilers which do not emit large
17677 quantities of duplicate debug information are more common,
17678 this code can probably be removed. */
17679
17680 /* Any complete simple types at the top level (pretty much all
17681 of them, for a language without namespaces), can be processed
17682 directly. */
17683 if (parent_die == NULL
cd9983dd
YQ
17684 && pdi.has_specification == 0
17685 && pdi.is_declaration == 0
17686 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17687 || pdi.tag == DW_TAG_base_type
17688 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17689 {
cd9983dd 17690 if (building_psymtab && pdi.name != NULL)
31edb802 17691 add_psymbol_to_list (pdi.name, false,
79748972 17692 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17693 psymbol_placement::STATIC,
1762568f 17694 0, cu->language, objfile);
cd9983dd 17695 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17696 continue;
17697 }
17698
d8228535
JK
17699 /* The exception for DW_TAG_typedef with has_children above is
17700 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17701 type_name_or_error will error on such types later.
d8228535
JK
17702
17703 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17704 it could not find the child DIEs referenced later, this is checked
17705 above. In correct DWARF DW_TAG_typedef should have no children. */
17706
cd9983dd 17707 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17708 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17709 "- DIE at %s [in module %s]"),
cd9983dd 17710 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17711
72bf9492
DJ
17712 /* If we're at the second level, and we're an enumerator, and
17713 our parent has no specification (meaning possibly lives in a
17714 namespace elsewhere), then we can add the partial symbol now
17715 instead of queueing it. */
cd9983dd 17716 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17717 && parent_die != NULL
17718 && parent_die->die_parent == NULL
17719 && parent_die->tag == DW_TAG_enumeration_type
17720 && parent_die->has_specification == 0)
17721 {
cd9983dd 17722 if (pdi.name == NULL)
b98664d3 17723 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17724 else if (building_psymtab)
31edb802 17725 add_psymbol_to_list (pdi.name, false,
79748972 17726 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17727 cu->language == language_cplus
75aedd27
TT
17728 ? psymbol_placement::GLOBAL
17729 : psymbol_placement::STATIC,
1762568f 17730 0, cu->language, objfile);
72bf9492 17731
cd9983dd 17732 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17733 continue;
17734 }
17735
cd9983dd 17736 struct partial_die_info *part_die
6f06d47b 17737 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17738
72bf9492
DJ
17739 /* We'll save this DIE so link it in. */
17740 part_die->die_parent = parent_die;
17741 part_die->die_sibling = NULL;
17742 part_die->die_child = NULL;
17743
17744 if (last_die && last_die == parent_die)
17745 last_die->die_child = part_die;
17746 else if (last_die)
17747 last_die->die_sibling = part_die;
17748
17749 last_die = part_die;
17750
17751 if (first_die == NULL)
17752 first_die = part_die;
17753
17754 /* Maybe add the DIE to the hash table. Not all DIEs that we
17755 find interesting need to be in the hash table, because we
17756 also have the parent/sibling/child chains; only those that we
17757 might refer to by offset later during partial symbol reading.
17758
17759 For now this means things that might have be the target of a
17760 DW_AT_specification, DW_AT_abstract_origin, or
17761 DW_AT_extension. DW_AT_extension will refer only to
17762 namespaces; DW_AT_abstract_origin refers to functions (and
17763 many things under the function DIE, but we do not recurse
17764 into function DIEs during partial symbol reading) and
17765 possibly variables as well; DW_AT_specification refers to
17766 declarations. Declarations ought to have the DW_AT_declaration
17767 flag. It happens that GCC forgets to put it in sometimes, but
17768 only for functions, not for types.
17769
17770 Adding more things than necessary to the hash table is harmless
17771 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17772 wasted time in find_partial_die, when we reread the compilation
17773 unit with load_all_dies set. */
72bf9492 17774
5afb4e99 17775 if (load_all
72929c62 17776 || abbrev->tag == DW_TAG_constant
5afb4e99 17777 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17778 || abbrev->tag == DW_TAG_variable
17779 || abbrev->tag == DW_TAG_namespace
17780 || part_die->is_declaration)
17781 {
17782 void **slot;
17783
17784 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17785 to_underlying (part_die->sect_off),
17786 INSERT);
72bf9492
DJ
17787 *slot = part_die;
17788 }
17789
72bf9492 17790 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17791 we have no reason to follow the children of structures; for other
98bfdba5
PA
17792 languages we have to, so that we can get at method physnames
17793 to infer fully qualified class names, for DW_AT_specification,
17794 and for C++ template arguments. For C++, we also look one level
17795 inside functions to find template arguments (if the name of the
17796 function does not already contain the template arguments).
bc30ff58 17797
0a4b0913
AB
17798 For Ada and Fortran, we need to scan the children of subprograms
17799 and lexical blocks as well because these languages allow the
17800 definition of nested entities that could be interesting for the
17801 debugger, such as nested subprograms for instance. */
72bf9492 17802 if (last_die->has_children
5afb4e99
DJ
17803 && (load_all
17804 || last_die->tag == DW_TAG_namespace
f55ee35c 17805 || last_die->tag == DW_TAG_module
72bf9492 17806 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17807 || (cu->language == language_cplus
17808 && last_die->tag == DW_TAG_subprogram
17809 && (last_die->name == NULL
17810 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17811 || (cu->language != language_c
17812 && (last_die->tag == DW_TAG_class_type
680b30c7 17813 || last_die->tag == DW_TAG_interface_type
72bf9492 17814 || last_die->tag == DW_TAG_structure_type
bc30ff58 17815 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17816 || ((cu->language == language_ada
17817 || cu->language == language_fortran)
bc30ff58
JB
17818 && (last_die->tag == DW_TAG_subprogram
17819 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17820 {
17821 nesting_level++;
17822 parent_die = last_die;
17823 continue;
17824 }
17825
17826 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17827 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17828
17829 /* Back to the top, do it again. */
17830 }
17831}
17832
6f06d47b
YQ
17833partial_die_info::partial_die_info (sect_offset sect_off_,
17834 struct abbrev_info *abbrev)
17835 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17836{
17837}
17838
35cc7ed7
YQ
17839/* Read a minimal amount of information into the minimal die structure.
17840 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17841
48fbe735
YQ
17842const gdb_byte *
17843partial_die_info::read (const struct die_reader_specs *reader,
17844 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17845{
dee91e82 17846 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17847 struct dwarf2_per_objfile *dwarf2_per_objfile
17848 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17849 unsigned int i;
c5aa993b 17850 int has_low_pc_attr = 0;
c906108c 17851 int has_high_pc_attr = 0;
91da1414 17852 int high_pc_relative = 0;
c906108c 17853
18a8505e 17854 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17855 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17856 {
18a8505e
AT
17857 bool need_reprocess;
17858 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17859 info_ptr, &need_reprocess);
17860 /* String and address offsets that need to do the reprocessing have
17861 already been read at this point, so there is no need to wait until
17862 the loop terminates to do the reprocessing. */
17863 if (need_reprocess)
17864 read_attribute_reprocess (reader, &attr_vec[i]);
17865 attribute &attr = attr_vec[i];
c906108c 17866 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17867 partial symbol table. */
c906108c
SS
17868 switch (attr.name)
17869 {
17870 case DW_AT_name:
48fbe735 17871 switch (tag)
71c25dea
TT
17872 {
17873 case DW_TAG_compile_unit:
95554aad 17874 case DW_TAG_partial_unit:
348e048f 17875 case DW_TAG_type_unit:
71c25dea
TT
17876 /* Compilation units have a DW_AT_name that is a filename, not
17877 a source language identifier. */
17878 case DW_TAG_enumeration_type:
17879 case DW_TAG_enumerator:
17880 /* These tags always have simple identifiers already; no need
17881 to canonicalize them. */
48fbe735 17882 name = DW_STRING (&attr);
71c25dea
TT
17883 break;
17884 default:
48fbe735
YQ
17885 {
17886 struct objfile *objfile = dwarf2_per_objfile->objfile;
17887
17888 name
be1e3d3e 17889 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 17890 }
71c25dea
TT
17891 break;
17892 }
c906108c 17893 break;
31ef98ae 17894 case DW_AT_linkage_name:
c906108c 17895 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17896 /* Note that both forms of linkage name might appear. We
17897 assume they will be the same, and we only store the last
17898 one we see. */
48fbe735 17899 linkage_name = DW_STRING (&attr);
c906108c
SS
17900 break;
17901 case DW_AT_low_pc:
17902 has_low_pc_attr = 1;
cd6c91b4 17903 lowpc = attr.value_as_address ();
c906108c
SS
17904 break;
17905 case DW_AT_high_pc:
17906 has_high_pc_attr = 1;
cd6c91b4
TT
17907 highpc = attr.value_as_address ();
17908 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 17909 high_pc_relative = 1;
c906108c
SS
17910 break;
17911 case DW_AT_location:
0963b4bd 17912 /* Support the .debug_loc offsets. */
4fc6c0d5 17913 if (attr.form_is_block ())
8e19ed76 17914 {
48fbe735 17915 d.locdesc = DW_BLOCK (&attr);
8e19ed76 17916 }
cd6c91b4 17917 else if (attr.form_is_section_offset ())
8e19ed76 17918 {
4d3c2250 17919 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
17920 }
17921 else
17922 {
4d3c2250
KB
17923 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17924 "partial symbol information");
8e19ed76 17925 }
c906108c 17926 break;
c906108c 17927 case DW_AT_external:
48fbe735 17928 is_external = DW_UNSND (&attr);
c906108c
SS
17929 break;
17930 case DW_AT_declaration:
48fbe735 17931 is_declaration = DW_UNSND (&attr);
c906108c
SS
17932 break;
17933 case DW_AT_type:
48fbe735 17934 has_type = 1;
c906108c
SS
17935 break;
17936 case DW_AT_abstract_origin:
17937 case DW_AT_specification:
72bf9492 17938 case DW_AT_extension:
48fbe735 17939 has_specification = 1;
0826b30a 17940 spec_offset = attr.get_ref_die_offset ();
48fbe735 17941 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 17942 || cu->per_cu->is_dwz);
c906108c
SS
17943 break;
17944 case DW_AT_sibling:
17945 /* Ignore absolute siblings, they might point outside of
17946 the current compile unit. */
17947 if (attr.form == DW_FORM_ref_addr)
b98664d3 17948 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 17949 else
b9502d3f 17950 {
48fbe735 17951 const gdb_byte *buffer = reader->buffer;
0826b30a 17952 sect_offset off = attr.get_ref_die_offset ();
9c541725 17953 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
17954
17955 if (sibling_ptr < info_ptr)
b98664d3 17956 complaint (_("DW_AT_sibling points backwards"));
22869d73 17957 else if (sibling_ptr > reader->buffer_end)
a0194fa8 17958 reader->die_section->overflow_complaint ();
b9502d3f 17959 else
48fbe735 17960 sibling = sibling_ptr;
b9502d3f 17961 }
c906108c 17962 break;
fa4028e9 17963 case DW_AT_byte_size:
48fbe735 17964 has_byte_size = 1;
fa4028e9 17965 break;
ff908ebf 17966 case DW_AT_const_value:
48fbe735 17967 has_const_value = 1;
ff908ebf 17968 break;
68511cec
CES
17969 case DW_AT_calling_convention:
17970 /* DWARF doesn't provide a way to identify a program's source-level
17971 entry point. DW_AT_calling_convention attributes are only meant
17972 to describe functions' calling conventions.
17973
17974 However, because it's a necessary piece of information in
0c1b455e
TT
17975 Fortran, and before DWARF 4 DW_CC_program was the only
17976 piece of debugging information whose definition refers to
17977 a 'main program' at all, several compilers marked Fortran
17978 main programs with DW_CC_program --- even when those
17979 functions use the standard calling conventions.
17980
17981 Although DWARF now specifies a way to provide this
17982 information, we support this practice for backward
17983 compatibility. */
68511cec 17984 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 17985 && cu->language == language_fortran)
48fbe735 17986 main_subprogram = 1;
68511cec 17987 break;
481860b3
GB
17988 case DW_AT_inline:
17989 if (DW_UNSND (&attr) == DW_INL_inlined
17990 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 17991 may_be_inlined = 1;
481860b3 17992 break;
95554aad
TT
17993
17994 case DW_AT_import:
48fbe735 17995 if (tag == DW_TAG_imported_unit)
36586728 17996 {
0826b30a 17997 d.sect_off = attr.get_ref_die_offset ();
48fbe735 17998 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
17999 || cu->per_cu->is_dwz);
18000 }
95554aad
TT
18001 break;
18002
0c1b455e 18003 case DW_AT_main_subprogram:
48fbe735 18004 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18005 break;
18006
05caa1d2
TT
18007 case DW_AT_ranges:
18008 {
18009 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18010 but that requires a full DIE, so instead we just
18011 reimplement it. */
18012 int need_ranges_base = tag != DW_TAG_compile_unit;
18013 unsigned int ranges_offset = (DW_UNSND (&attr)
18014 + (need_ranges_base
18015 ? cu->ranges_base
18016 : 0));
18017
18018 /* Value of the DW_AT_ranges attribute is the offset in the
18019 .debug_ranges section. */
18020 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18021 nullptr))
18022 has_pc_info = 1;
18023 }
18024 break;
18025
c906108c
SS
18026 default:
18027 break;
18028 }
18029 }
18030
10d06d82
TT
18031 /* For Ada, if both the name and the linkage name appear, we prefer
18032 the latter. This lets "catch exception" work better, regardless
18033 of the order in which the name and linkage name were emitted.
18034 Really, though, this is just a workaround for the fact that gdb
18035 doesn't store both the name and the linkage name. */
18036 if (cu->language == language_ada && linkage_name != nullptr)
18037 name = linkage_name;
18038
91da1414 18039 if (high_pc_relative)
48fbe735 18040 highpc += lowpc;
91da1414 18041
9373cf26
JK
18042 if (has_low_pc_attr && has_high_pc_attr)
18043 {
18044 /* When using the GNU linker, .gnu.linkonce. sections are used to
18045 eliminate duplicate copies of functions and vtables and such.
18046 The linker will arbitrarily choose one and discard the others.
18047 The AT_*_pc values for such functions refer to local labels in
18048 these sections. If the section from that file was discarded, the
18049 labels are not in the output, so the relocs get a value of 0.
18050 If this is a discarded function, mark the pc bounds as invalid,
18051 so that GDB will ignore it. */
48fbe735 18052 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18053 {
48fbe735 18054 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18055 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18056
b98664d3 18057 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18058 "for DIE at %s [in module %s]"),
48fbe735
YQ
18059 paddress (gdbarch, lowpc),
18060 sect_offset_str (sect_off),
9d8780f0 18061 objfile_name (objfile));
9373cf26
JK
18062 }
18063 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18064 else if (lowpc >= highpc)
9373cf26 18065 {
48fbe735 18066 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18068
b98664d3 18069 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18070 "for DIE at %s [in module %s]"),
48fbe735
YQ
18071 paddress (gdbarch, lowpc),
18072 paddress (gdbarch, highpc),
18073 sect_offset_str (sect_off),
9c541725 18074 objfile_name (objfile));
9373cf26
JK
18075 }
18076 else
48fbe735 18077 has_pc_info = 1;
9373cf26 18078 }
85cbf3d3 18079
c906108c
SS
18080 return info_ptr;
18081}
18082
72bf9492
DJ
18083/* Find a cached partial DIE at OFFSET in CU. */
18084
d590ff25
YQ
18085struct partial_die_info *
18086dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18087{
18088 struct partial_die_info *lookup_die = NULL;
6f06d47b 18089 struct partial_die_info part_die (sect_off);
72bf9492 18090
9a3c8263 18091 lookup_die = ((struct partial_die_info *)
d590ff25 18092 htab_find_with_hash (partial_dies, &part_die,
9c541725 18093 to_underlying (sect_off)));
72bf9492 18094
72bf9492
DJ
18095 return lookup_die;
18096}
18097
348e048f
DE
18098/* Find a partial DIE at OFFSET, which may or may not be in CU,
18099 except in the case of .debug_types DIEs which do not reference
18100 outside their CU (they do however referencing other types via
55f1336d 18101 DW_FORM_ref_sig8). */
72bf9492 18102
122cf0f2 18103static const struct cu_partial_die_info
9c541725 18104find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18105{
518817b3
SM
18106 struct dwarf2_per_objfile *dwarf2_per_objfile
18107 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18108 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18109 struct dwarf2_per_cu_data *per_cu = NULL;
18110 struct partial_die_info *pd = NULL;
72bf9492 18111
36586728 18112 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18113 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18114 {
d590ff25 18115 pd = cu->find_partial_die (sect_off);
5afb4e99 18116 if (pd != NULL)
fb816e8b 18117 return { cu, pd };
0d99eb77
DE
18118 /* We missed recording what we needed.
18119 Load all dies and try again. */
18120 per_cu = cu->per_cu;
5afb4e99 18121 }
0d99eb77
DE
18122 else
18123 {
18124 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18125 if (cu->per_cu->is_debug_types)
0d99eb77 18126 {
9d8780f0
SM
18127 error (_("Dwarf Error: Type Unit at offset %s contains"
18128 " external reference to offset %s [in module %s].\n"),
18129 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18130 bfd_get_filename (objfile->obfd));
18131 }
9c541725 18132 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18133 dwarf2_per_objfile);
72bf9492 18134
0d99eb77
DE
18135 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18136 load_partial_comp_unit (per_cu);
ae038cb0 18137
0d99eb77 18138 per_cu->cu->last_used = 0;
d590ff25 18139 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18140 }
5afb4e99 18141
dee91e82
DE
18142 /* If we didn't find it, and not all dies have been loaded,
18143 load them all and try again. */
18144
5afb4e99
DJ
18145 if (pd == NULL && per_cu->load_all_dies == 0)
18146 {
5afb4e99 18147 per_cu->load_all_dies = 1;
fd820528
DE
18148
18149 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18150 THIS_CU->cu may already be in use. So we can't just free it and
18151 replace its DIEs with the ones we read in. Instead, we leave those
18152 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18153 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18154 set. */
dee91e82 18155 load_partial_comp_unit (per_cu);
5afb4e99 18156
d590ff25 18157 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18158 }
18159
18160 if (pd == NULL)
18161 internal_error (__FILE__, __LINE__,
9d8780f0 18162 _("could not find partial DIE %s "
3e43a32a 18163 "in cache [from module %s]\n"),
9d8780f0 18164 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18165 return { per_cu->cu, pd };
72bf9492
DJ
18166}
18167
abc72ce4
DE
18168/* See if we can figure out if the class lives in a namespace. We do
18169 this by looking for a member function; its demangled name will
18170 contain namespace info, if there is any. */
18171
18172static void
18173guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18174 struct dwarf2_cu *cu)
18175{
18176 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18177 what template types look like, because the demangler
18178 frequently doesn't give the same name as the debug info. We
18179 could fix this by only using the demangled name to get the
18180 prefix (but see comment in read_structure_type). */
18181
18182 struct partial_die_info *real_pdi;
18183 struct partial_die_info *child_pdi;
18184
18185 /* If this DIE (this DIE's specification, if any) has a parent, then
18186 we should not do this. We'll prepend the parent's fully qualified
18187 name when we create the partial symbol. */
18188
18189 real_pdi = struct_pdi;
18190 while (real_pdi->has_specification)
fb816e8b 18191 {
122cf0f2
AB
18192 auto res = find_partial_die (real_pdi->spec_offset,
18193 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18194 real_pdi = res.pdi;
18195 cu = res.cu;
18196 }
abc72ce4
DE
18197
18198 if (real_pdi->die_parent != NULL)
18199 return;
18200
18201 for (child_pdi = struct_pdi->die_child;
18202 child_pdi != NULL;
18203 child_pdi = child_pdi->die_sibling)
18204 {
18205 if (child_pdi->tag == DW_TAG_subprogram
18206 && child_pdi->linkage_name != NULL)
18207 {
43816ebc
TT
18208 gdb::unique_xmalloc_ptr<char> actual_class_name
18209 (language_class_name_from_physname (cu->language_defn,
18210 child_pdi->linkage_name));
abc72ce4
DE
18211 if (actual_class_name != NULL)
18212 {
518817b3 18213 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18214 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18215 }
18216 break;
18217 }
18218 }
18219}
18220
52356b79
YQ
18221void
18222partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18223{
abc72ce4
DE
18224 /* Once we've fixed up a die, there's no point in doing so again.
18225 This also avoids a memory leak if we were to call
18226 guess_partial_die_structure_name multiple times. */
52356b79 18227 if (fixup_called)
abc72ce4
DE
18228 return;
18229
72bf9492
DJ
18230 /* If we found a reference attribute and the DIE has no name, try
18231 to find a name in the referred to DIE. */
18232
52356b79 18233 if (name == NULL && has_specification)
72bf9492
DJ
18234 {
18235 struct partial_die_info *spec_die;
72bf9492 18236
122cf0f2 18237 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18238 spec_die = res.pdi;
18239 cu = res.cu;
72bf9492 18240
52356b79 18241 spec_die->fixup (cu);
72bf9492
DJ
18242
18243 if (spec_die->name)
18244 {
52356b79 18245 name = spec_die->name;
72bf9492
DJ
18246
18247 /* Copy DW_AT_external attribute if it is set. */
18248 if (spec_die->is_external)
52356b79 18249 is_external = spec_die->is_external;
72bf9492
DJ
18250 }
18251 }
18252
18253 /* Set default names for some unnamed DIEs. */
72bf9492 18254
52356b79
YQ
18255 if (name == NULL && tag == DW_TAG_namespace)
18256 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18257
abc72ce4
DE
18258 /* If there is no parent die to provide a namespace, and there are
18259 children, see if we can determine the namespace from their linkage
122d1940 18260 name. */
abc72ce4 18261 if (cu->language == language_cplus
fd5866f6 18262 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18263 && die_parent == NULL
18264 && has_children
18265 && (tag == DW_TAG_class_type
18266 || tag == DW_TAG_structure_type
18267 || tag == DW_TAG_union_type))
18268 guess_partial_die_structure_name (this, cu);
abc72ce4 18269
53832f31
TT
18270 /* GCC might emit a nameless struct or union that has a linkage
18271 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18272 if (name == NULL
18273 && (tag == DW_TAG_class_type
18274 || tag == DW_TAG_interface_type
18275 || tag == DW_TAG_structure_type
18276 || tag == DW_TAG_union_type)
18277 && linkage_name != NULL)
53832f31 18278 {
43816ebc
TT
18279 gdb::unique_xmalloc_ptr<char> demangled
18280 (gdb_demangle (linkage_name, DMGL_TYPES));
18281 if (demangled != nullptr)
53832f31 18282 {
96408a79
SA
18283 const char *base;
18284
18285 /* Strip any leading namespaces/classes, keep only the base name.
18286 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18287 base = strrchr (demangled.get (), ':');
18288 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18289 base++;
18290 else
43816ebc 18291 base = demangled.get ();
96408a79 18292
518817b3 18293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18294 name = objfile->intern (base);
53832f31
TT
18295 }
18296 }
18297
52356b79 18298 fixup_called = 1;
72bf9492
DJ
18299}
18300
18a8505e
AT
18301/* Process the attributes that had to be skipped in the first round. These
18302 attributes are the ones that need str_offsets_base or addr_base attributes.
18303 They could not have been processed in the first round, because at the time
18304 the values of str_offsets_base or addr_base may not have been known. */
18305void read_attribute_reprocess (const struct die_reader_specs *reader,
18306 struct attribute *attr)
18307{
18308 struct dwarf2_cu *cu = reader->cu;
18309 switch (attr->form)
18310 {
18311 case DW_FORM_addrx:
18312 case DW_FORM_GNU_addr_index:
18313 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18314 break;
18315 case DW_FORM_strx:
18316 case DW_FORM_strx1:
18317 case DW_FORM_strx2:
18318 case DW_FORM_strx3:
18319 case DW_FORM_strx4:
18320 case DW_FORM_GNU_str_index:
18321 {
18322 unsigned int str_index = DW_UNSND (attr);
18323 if (reader->dwo_file != NULL)
18324 {
18325 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18326 DW_STRING_IS_CANONICAL (attr) = 0;
18327 }
18328 else
18329 {
18330 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18331 DW_STRING_IS_CANONICAL (attr) = 0;
18332 }
18333 break;
18334 }
18335 default:
18336 gdb_assert_not_reached (_("Unexpected DWARF form."));
18337 }
18338}
18339
a8329558 18340/* Read an attribute value described by an attribute form. */
c906108c 18341
d521ce57 18342static const gdb_byte *
dee91e82
DE
18343read_attribute_value (const struct die_reader_specs *reader,
18344 struct attribute *attr, unsigned form,
18a8505e
AT
18345 LONGEST implicit_const, const gdb_byte *info_ptr,
18346 bool *need_reprocess)
c906108c 18347{
dee91e82 18348 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18349 struct dwarf2_per_objfile *dwarf2_per_objfile
18350 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18351 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18352 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18353 bfd *abfd = reader->abfd;
e7c27a73 18354 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18355 unsigned int bytes_read;
18356 struct dwarf_block *blk;
18a8505e 18357 *need_reprocess = false;
c906108c 18358
aead7601 18359 attr->form = (enum dwarf_form) form;
a8329558 18360 switch (form)
c906108c 18361 {
c906108c 18362 case DW_FORM_ref_addr:
ae411497 18363 if (cu->header.version == 2)
c8a7a66f
TT
18364 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18365 &bytes_read);
ae411497 18366 else
8266302d
TT
18367 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18368 &bytes_read);
ae411497
TT
18369 info_ptr += bytes_read;
18370 break;
36586728 18371 case DW_FORM_GNU_ref_alt:
8266302d 18372 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18373 info_ptr += bytes_read;
18374 break;
ae411497 18375 case DW_FORM_addr:
c8a7a66f 18376 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18377 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18378 info_ptr += bytes_read;
c906108c
SS
18379 break;
18380 case DW_FORM_block2:
7b5a2f43 18381 blk = dwarf_alloc_block (cu);
c906108c
SS
18382 blk->size = read_2_bytes (abfd, info_ptr);
18383 info_ptr += 2;
18384 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18385 info_ptr += blk->size;
18386 DW_BLOCK (attr) = blk;
18387 break;
18388 case DW_FORM_block4:
7b5a2f43 18389 blk = dwarf_alloc_block (cu);
c906108c
SS
18390 blk->size = read_4_bytes (abfd, info_ptr);
18391 info_ptr += 4;
18392 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18393 info_ptr += blk->size;
18394 DW_BLOCK (attr) = blk;
18395 break;
18396 case DW_FORM_data2:
18397 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18398 info_ptr += 2;
18399 break;
18400 case DW_FORM_data4:
18401 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18402 info_ptr += 4;
18403 break;
18404 case DW_FORM_data8:
18405 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18406 info_ptr += 8;
18407 break;
0224619f
JK
18408 case DW_FORM_data16:
18409 blk = dwarf_alloc_block (cu);
18410 blk->size = 16;
18411 blk->data = read_n_bytes (abfd, info_ptr, 16);
18412 info_ptr += 16;
18413 DW_BLOCK (attr) = blk;
18414 break;
2dc7f7b3 18415 case DW_FORM_sec_offset:
8266302d 18416 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18417 info_ptr += bytes_read;
18418 break;
c906108c 18419 case DW_FORM_string:
9b1c24c8 18420 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18421 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18422 info_ptr += bytes_read;
18423 break;
4bdf3d34 18424 case DW_FORM_strp:
36586728
TT
18425 if (!cu->per_cu->is_dwz)
18426 {
ed2dc618
SM
18427 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18428 abfd, info_ptr, cu_header,
36586728
TT
18429 &bytes_read);
18430 DW_STRING_IS_CANONICAL (attr) = 0;
18431 info_ptr += bytes_read;
18432 break;
18433 }
18434 /* FALLTHROUGH */
43988095
JK
18435 case DW_FORM_line_strp:
18436 if (!cu->per_cu->is_dwz)
18437 {
86c0bb4c
TT
18438 DW_STRING (attr)
18439 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18440 &bytes_read);
43988095
JK
18441 DW_STRING_IS_CANONICAL (attr) = 0;
18442 info_ptr += bytes_read;
18443 break;
18444 }
18445 /* FALLTHROUGH */
36586728
TT
18446 case DW_FORM_GNU_strp_alt:
18447 {
ed2dc618 18448 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18449 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18450 &bytes_read);
36586728 18451
0314b390 18452 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
18453 DW_STRING_IS_CANONICAL (attr) = 0;
18454 info_ptr += bytes_read;
18455 }
4bdf3d34 18456 break;
2dc7f7b3 18457 case DW_FORM_exprloc:
c906108c 18458 case DW_FORM_block:
7b5a2f43 18459 blk = dwarf_alloc_block (cu);
c906108c
SS
18460 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18461 info_ptr += bytes_read;
18462 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18463 info_ptr += blk->size;
18464 DW_BLOCK (attr) = blk;
18465 break;
18466 case DW_FORM_block1:
7b5a2f43 18467 blk = dwarf_alloc_block (cu);
c906108c
SS
18468 blk->size = read_1_byte (abfd, info_ptr);
18469 info_ptr += 1;
18470 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18471 info_ptr += blk->size;
18472 DW_BLOCK (attr) = blk;
18473 break;
18474 case DW_FORM_data1:
18475 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18476 info_ptr += 1;
18477 break;
18478 case DW_FORM_flag:
18479 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18480 info_ptr += 1;
18481 break;
2dc7f7b3
TT
18482 case DW_FORM_flag_present:
18483 DW_UNSND (attr) = 1;
18484 break;
c906108c
SS
18485 case DW_FORM_sdata:
18486 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18487 info_ptr += bytes_read;
18488 break;
18489 case DW_FORM_udata:
18a8505e 18490 case DW_FORM_rnglistx:
c906108c
SS
18491 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18492 info_ptr += bytes_read;
18493 break;
18494 case DW_FORM_ref1:
9c541725 18495 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18496 + read_1_byte (abfd, info_ptr));
c906108c
SS
18497 info_ptr += 1;
18498 break;
18499 case DW_FORM_ref2:
9c541725 18500 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18501 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18502 info_ptr += 2;
18503 break;
18504 case DW_FORM_ref4:
9c541725 18505 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18506 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18507 info_ptr += 4;
18508 break;
613e1657 18509 case DW_FORM_ref8:
9c541725 18510 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18511 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18512 info_ptr += 8;
18513 break;
55f1336d 18514 case DW_FORM_ref_sig8:
ac9ec31b 18515 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18516 info_ptr += 8;
18517 break;
c906108c 18518 case DW_FORM_ref_udata:
9c541725 18519 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18520 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18521 info_ptr += bytes_read;
18522 break;
c906108c 18523 case DW_FORM_indirect:
a8329558
KW
18524 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18525 info_ptr += bytes_read;
43988095
JK
18526 if (form == DW_FORM_implicit_const)
18527 {
18528 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18529 info_ptr += bytes_read;
18530 }
18531 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18532 info_ptr, need_reprocess);
43988095
JK
18533 break;
18534 case DW_FORM_implicit_const:
18535 DW_SND (attr) = implicit_const;
a8329558 18536 break;
336d760d 18537 case DW_FORM_addrx:
3019eac3 18538 case DW_FORM_GNU_addr_index:
18a8505e
AT
18539 *need_reprocess = true;
18540 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18541 info_ptr += bytes_read;
18542 break;
cf532bd1 18543 case DW_FORM_strx:
15f18d14
AT
18544 case DW_FORM_strx1:
18545 case DW_FORM_strx2:
18546 case DW_FORM_strx3:
18547 case DW_FORM_strx4:
3019eac3 18548 case DW_FORM_GNU_str_index:
3019eac3 18549 {
15f18d14
AT
18550 ULONGEST str_index;
18551 if (form == DW_FORM_strx1)
18552 {
18553 str_index = read_1_byte (abfd, info_ptr);
18554 info_ptr += 1;
18555 }
18556 else if (form == DW_FORM_strx2)
18557 {
18558 str_index = read_2_bytes (abfd, info_ptr);
18559 info_ptr += 2;
18560 }
18561 else if (form == DW_FORM_strx3)
18562 {
18563 str_index = read_3_bytes (abfd, info_ptr);
18564 info_ptr += 3;
18565 }
18566 else if (form == DW_FORM_strx4)
18567 {
18568 str_index = read_4_bytes (abfd, info_ptr);
18569 info_ptr += 4;
18570 }
18571 else
18572 {
18573 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18574 info_ptr += bytes_read;
18575 }
18a8505e
AT
18576 *need_reprocess = true;
18577 DW_UNSND (attr) = str_index;
18578 }
3019eac3 18579 break;
c906108c 18580 default:
8a3fe4f8 18581 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18582 dwarf_form_name (form),
18583 bfd_get_filename (abfd));
c906108c 18584 }
28e94949 18585
36586728 18586 /* Super hack. */
cd6c91b4 18587 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18588 attr->form = DW_FORM_GNU_ref_alt;
18589
28e94949
JB
18590 /* We have seen instances where the compiler tried to emit a byte
18591 size attribute of -1 which ended up being encoded as an unsigned
18592 0xffffffff. Although 0xffffffff is technically a valid size value,
18593 an object of this size seems pretty unlikely so we can relatively
18594 safely treat these cases as if the size attribute was invalid and
18595 treat them as zero by default. */
18596 if (attr->name == DW_AT_byte_size
18597 && form == DW_FORM_data4
18598 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18599 {
18600 complaint
b98664d3 18601 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18602 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18603 DW_UNSND (attr) = 0;
18604 }
28e94949 18605
c906108c
SS
18606 return info_ptr;
18607}
18608
a8329558
KW
18609/* Read an attribute described by an abbreviated attribute. */
18610
d521ce57 18611static const gdb_byte *
dee91e82
DE
18612read_attribute (const struct die_reader_specs *reader,
18613 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18614 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18615{
18616 attr->name = abbrev->name;
43988095 18617 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18618 abbrev->implicit_const, info_ptr,
18619 need_reprocess);
a8329558
KW
18620}
18621
43988095
JK
18622/* Return pointer to string at .debug_str offset STR_OFFSET. */
18623
18624static const char *
ed2dc618 18625read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 18626 LONGEST str_offset)
43988095 18627{
4f44ae6c
TT
18628 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18629 str_offset, "DW_FORM_strp");
c906108c
SS
18630}
18631
43988095
JK
18632/* Return pointer to string at .debug_str offset as read from BUF.
18633 BUF is assumed to be in a compilation unit described by CU_HEADER.
18634 Return *BYTES_READ_PTR count of bytes read from BUF. */
18635
d521ce57 18636static const char *
ed2dc618
SM
18637read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18638 const gdb_byte *buf,
cf2c3c16
TT
18639 const struct comp_unit_head *cu_header,
18640 unsigned int *bytes_read_ptr)
18641{
8266302d 18642 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18643
4f44ae6c 18644 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
18645}
18646
86c0bb4c 18647/* See read.h. */
43988095 18648
86c0bb4c
TT
18649const char *
18650dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
18651 const struct comp_unit_head *cu_header,
18652 unsigned int *bytes_read_ptr)
18653{
86c0bb4c 18654 bfd *abfd = objfile->obfd;
8266302d 18655 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18656
86c0bb4c 18657 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
18658}
18659
3019eac3 18660/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18661 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18662 ADDR_SIZE is the size of addresses from the CU header. */
18663
18664static CORE_ADDR
ed2dc618 18665read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18666 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18667 int addr_size)
3019eac3
DE
18668{
18669 struct objfile *objfile = dwarf2_per_objfile->objfile;
18670 bfd *abfd = objfile->obfd;
18671 const gdb_byte *info_ptr;
18a8505e 18672 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18673
96b79293 18674 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18675 if (dwarf2_per_objfile->addr.buffer == NULL)
18676 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18677 objfile_name (objfile));
18a8505e
AT
18678 if (addr_base_or_zero + addr_index * addr_size
18679 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18680 error (_("DW_FORM_addr_index pointing outside of "
18681 ".debug_addr section [in module %s]"),
4262abfb 18682 objfile_name (objfile));
3019eac3 18683 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18684 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18685 if (addr_size == 4)
18686 return bfd_get_32 (abfd, info_ptr);
18687 else
18688 return bfd_get_64 (abfd, info_ptr);
18689}
18690
18691/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18692
18693static CORE_ADDR
18694read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18695{
518817b3
SM
18696 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18697 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18698}
18699
18700/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18701
18702static CORE_ADDR
d521ce57 18703read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18704 unsigned int *bytes_read)
18705{
518817b3 18706 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18707 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18708
18709 return read_addr_index (cu, addr_index);
18710}
18711
450a1bfc 18712/* See read.h. */
3019eac3
DE
18713
18714CORE_ADDR
450a1bfc 18715dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18716{
ed2dc618 18717 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18718 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18719 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18720 int addr_size;
18721
3019eac3
DE
18722 /* We need addr_base and addr_size.
18723 If we don't have PER_CU->cu, we have to get it.
18724 Nasty, but the alternative is storing the needed info in PER_CU,
18725 which at this point doesn't seem justified: it's not clear how frequently
18726 it would get used and it would increase the size of every PER_CU.
18727 Entry points like dwarf2_per_cu_addr_size do a similar thing
18728 so we're not in uncharted territory here.
18729 Alas we need to be a bit more complicated as addr_base is contained
18730 in the DIE.
18731
18732 We don't need to read the entire CU(/TU).
18733 We just need the header and top level die.
a1b64ce1 18734
3019eac3 18735 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18736 For now we skip this optimization. */
3019eac3
DE
18737
18738 if (cu != NULL)
18739 {
18740 addr_base = cu->addr_base;
18741 addr_size = cu->header.addr_size;
18742 }
18743 else
18744 {
6751ebae 18745 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18746 addr_base = reader.cu->addr_base;
18747 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18748 }
18749
ed2dc618
SM
18750 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18751 addr_size);
3019eac3
DE
18752}
18753
18a8505e
AT
18754/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18755 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18756 DWO file. */
3019eac3 18757
d521ce57 18758static const char *
18a8505e
AT
18759read_str_index (struct dwarf2_cu *cu,
18760 struct dwarf2_section_info *str_section,
18761 struct dwarf2_section_info *str_offsets_section,
18762 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18763{
518817b3
SM
18764 struct dwarf2_per_objfile *dwarf2_per_objfile
18765 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18766 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18767 const char *objf_name = objfile_name (objfile);
3019eac3 18768 bfd *abfd = objfile->obfd;
d521ce57 18769 const gdb_byte *info_ptr;
3019eac3 18770 ULONGEST str_offset;
cf532bd1 18771 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18772
96b79293
TT
18773 str_section->read (objfile);
18774 str_offsets_section->read (objfile);
73869dc2 18775 if (str_section->buffer == NULL)
18a8505e 18776 error (_("%s used without %s section"
9d8780f0 18777 " in CU at offset %s [in module %s]"),
96b79293 18778 form_name, str_section->get_name (),
18a8505e 18779 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18780 if (str_offsets_section->buffer == NULL)
18a8505e 18781 error (_("%s used without %s section"
9d8780f0 18782 " in CU at offset %s [in module %s]"),
96b79293 18783 form_name, str_section->get_name (),
18a8505e 18784 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18785 info_ptr = (str_offsets_section->buffer
18a8505e 18786 + str_offsets_base
3019eac3
DE
18787 + str_index * cu->header.offset_size);
18788 if (cu->header.offset_size == 4)
18789 str_offset = bfd_get_32 (abfd, info_ptr);
18790 else
18791 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18792 if (str_offset >= str_section->size)
57d63ce2 18793 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18794 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18795 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18796 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18797}
18798
18a8505e
AT
18799/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18800
18801static const char *
18802read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18803{
18804 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18805 ? reader->cu->header.addr_size : 0;
18806 return read_str_index (reader->cu,
18807 &reader->dwo_file->sections.str,
18808 &reader->dwo_file->sections.str_offsets,
18809 str_offsets_base, str_index);
18810}
18811
18812/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18813
18814static const char *
18815read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18816{
18817 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18818 const char *objf_name = objfile_name (objfile);
18819 static const char form_name[] = "DW_FORM_GNU_str_index";
18820 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18821
18822 if (!cu->str_offsets_base.has_value ())
18823 error (_("%s used in Fission stub without %s"
18824 " in CU at offset 0x%lx [in module %s]"),
18825 form_name, str_offsets_attr_name,
18826 (long) cu->header.offset_size, objf_name);
18827
18828 return read_str_index (cu,
18829 &cu->per_cu->dwarf2_per_objfile->str,
18830 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18831 *cu->str_offsets_base, str_index);
18832}
18833
3019eac3
DE
18834/* Return the length of an LEB128 number in BUF. */
18835
18836static int
18837leb128_size (const gdb_byte *buf)
18838{
18839 const gdb_byte *begin = buf;
18840 gdb_byte byte;
18841
18842 while (1)
18843 {
18844 byte = *buf++;
18845 if ((byte & 128) == 0)
18846 return buf - begin;
18847 }
18848}
18849
c906108c 18850static void
e142c38c 18851set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
18852{
18853 switch (lang)
18854 {
18855 case DW_LANG_C89:
76bee0cc 18856 case DW_LANG_C99:
0cfd832f 18857 case DW_LANG_C11:
c906108c 18858 case DW_LANG_C:
d1be3247 18859 case DW_LANG_UPC:
e142c38c 18860 cu->language = language_c;
c906108c 18861 break;
9c37b5ae 18862 case DW_LANG_Java:
c906108c 18863 case DW_LANG_C_plus_plus:
0cfd832f
MW
18864 case DW_LANG_C_plus_plus_11:
18865 case DW_LANG_C_plus_plus_14:
e142c38c 18866 cu->language = language_cplus;
c906108c 18867 break;
6aecb9c2
JB
18868 case DW_LANG_D:
18869 cu->language = language_d;
18870 break;
c906108c
SS
18871 case DW_LANG_Fortran77:
18872 case DW_LANG_Fortran90:
b21b22e0 18873 case DW_LANG_Fortran95:
f7de9aab
MW
18874 case DW_LANG_Fortran03:
18875 case DW_LANG_Fortran08:
e142c38c 18876 cu->language = language_fortran;
c906108c 18877 break;
a766d390
DE
18878 case DW_LANG_Go:
18879 cu->language = language_go;
18880 break;
c906108c 18881 case DW_LANG_Mips_Assembler:
e142c38c 18882 cu->language = language_asm;
c906108c
SS
18883 break;
18884 case DW_LANG_Ada83:
8aaf0b47 18885 case DW_LANG_Ada95:
bc5f45f8
JB
18886 cu->language = language_ada;
18887 break;
72019c9c
GM
18888 case DW_LANG_Modula2:
18889 cu->language = language_m2;
18890 break;
fe8e67fd
PM
18891 case DW_LANG_Pascal83:
18892 cu->language = language_pascal;
18893 break;
22566fbd
DJ
18894 case DW_LANG_ObjC:
18895 cu->language = language_objc;
18896 break;
c44af4eb
TT
18897 case DW_LANG_Rust:
18898 case DW_LANG_Rust_old:
18899 cu->language = language_rust;
18900 break;
c906108c
SS
18901 case DW_LANG_Cobol74:
18902 case DW_LANG_Cobol85:
c906108c 18903 default:
e142c38c 18904 cu->language = language_minimal;
c906108c
SS
18905 break;
18906 }
e142c38c 18907 cu->language_defn = language_def (cu->language);
c906108c
SS
18908}
18909
18910/* Return the named attribute or NULL if not there. */
18911
18912static struct attribute *
e142c38c 18913dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 18914{
a48e046c 18915 for (;;)
c906108c 18916 {
a48e046c
TT
18917 unsigned int i;
18918 struct attribute *spec = NULL;
18919
18920 for (i = 0; i < die->num_attrs; ++i)
18921 {
18922 if (die->attrs[i].name == name)
18923 return &die->attrs[i];
18924 if (die->attrs[i].name == DW_AT_specification
18925 || die->attrs[i].name == DW_AT_abstract_origin)
18926 spec = &die->attrs[i];
18927 }
18928
18929 if (!spec)
18930 break;
c906108c 18931
f2f0e013 18932 die = follow_die_ref (die, spec, &cu);
f2f0e013 18933 }
c5aa993b 18934
c906108c
SS
18935 return NULL;
18936}
18937
7d45c7c3
KB
18938/* Return the string associated with a string-typed attribute, or NULL if it
18939 is either not found or is of an incorrect type. */
18940
18941static const char *
18942dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18943{
18944 struct attribute *attr;
18945 const char *str = NULL;
18946
18947 attr = dwarf2_attr (die, name, cu);
18948
18949 if (attr != NULL)
18950 {
43988095 18951 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 18952 || attr->form == DW_FORM_string
cf532bd1 18953 || attr->form == DW_FORM_strx
8fe0f950
AT
18954 || attr->form == DW_FORM_strx1
18955 || attr->form == DW_FORM_strx2
18956 || attr->form == DW_FORM_strx3
18957 || attr->form == DW_FORM_strx4
b3340438 18958 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 18959 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
18960 str = DW_STRING (attr);
18961 else
b98664d3 18962 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
18963 "DIE at %s in module %s"),
18964 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 18965 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
18966 }
18967
18968 return str;
18969}
18970
a084a2a6 18971/* Return the dwo name or NULL if not present. If present, it is in either
85102364 18972 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
18973static const char *
18974dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
18975{
18976 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
18977 if (dwo_name == nullptr)
18978 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
18979 return dwo_name;
18980}
18981
05cf31d1
JB
18982/* Return non-zero iff the attribute NAME is defined for the given DIE,
18983 and holds a non-zero value. This function should only be used for
2dc7f7b3 18984 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
18985
18986static int
18987dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18988{
18989 struct attribute *attr = dwarf2_attr (die, name, cu);
18990
18991 return (attr && DW_UNSND (attr));
18992}
18993
3ca72b44 18994static int
e142c38c 18995die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 18996{
05cf31d1
JB
18997 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18998 which value is non-zero. However, we have to be careful with
18999 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19000 (via dwarf2_flag_true_p) follows this attribute. So we may
19001 end up accidently finding a declaration attribute that belongs
19002 to a different DIE referenced by the specification attribute,
19003 even though the given DIE does not have a declaration attribute. */
19004 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19005 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19006}
19007
63d06c5c 19008/* Return the die giving the specification for DIE, if there is
f2f0e013 19009 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19010 containing the return value on output. If there is no
19011 specification, but there is an abstract origin, that is
19012 returned. */
63d06c5c
DC
19013
19014static struct die_info *
f2f0e013 19015die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19016{
f2f0e013
DJ
19017 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19018 *spec_cu);
63d06c5c 19019
edb3359d
DJ
19020 if (spec_attr == NULL)
19021 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19022
63d06c5c
DC
19023 if (spec_attr == NULL)
19024 return NULL;
19025 else
f2f0e013 19026 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19027}
c906108c 19028
527f3840
JK
19029/* Stub for free_line_header to match void * callback types. */
19030
19031static void
19032free_line_header_voidp (void *arg)
19033{
9a3c8263 19034 struct line_header *lh = (struct line_header *) arg;
527f3840 19035
fff8551c 19036 delete lh;
527f3840
JK
19037}
19038
83769d0b 19039/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19040
19041static struct dwarf2_section_info *
19042get_debug_line_section (struct dwarf2_cu *cu)
19043{
19044 struct dwarf2_section_info *section;
518817b3
SM
19045 struct dwarf2_per_objfile *dwarf2_per_objfile
19046 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19047
19048 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19049 DWO file. */
19050 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19051 section = &cu->dwo_unit->dwo_file->sections.line;
19052 else if (cu->per_cu->is_dwz)
19053 {
ed2dc618 19054 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19055
19056 section = &dwz->line;
19057 }
19058 else
19059 section = &dwarf2_per_objfile->line;
19060
19061 return section;
19062}
19063
debd256d 19064/* Read the statement program header starting at OFFSET in
3019eac3 19065 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19066 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19067 Returns NULL if there is a problem reading the header, e.g., if it
19068 has a version we don't understand.
debd256d
JB
19069
19070 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19071 the returned object point into the dwarf line section buffer,
19072 and must not be freed. */
ae2de4f8 19073
fff8551c 19074static line_header_up
9c541725 19075dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19076{
3019eac3 19077 struct dwarf2_section_info *section;
518817b3
SM
19078 struct dwarf2_per_objfile *dwarf2_per_objfile
19079 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19080
36586728 19081 section = get_debug_line_section (cu);
96b79293 19082 section->read (dwarf2_per_objfile->objfile);
3019eac3 19083 if (section->buffer == NULL)
debd256d 19084 {
3019eac3 19085 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19086 complaint (_("missing .debug_line.dwo section"));
3019eac3 19087 else
b98664d3 19088 complaint (_("missing .debug_line section"));
debd256d
JB
19089 return 0;
19090 }
19091
0df7ad3a
TT
19092 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19093 dwarf2_per_objfile, section,
19094 &cu->header);
debd256d 19095}
c906108c 19096
c6da4cef 19097/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19098 Return the file name of the psymtab for the given file_entry.
c6da4cef 19099 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19100 If space for the result is malloc'd, *NAME_HOLDER will be set.
19101 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19102
d521ce57 19103static const char *
7ba99d21 19104psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19105 const dwarf2_psymtab *pst,
c89b44cd
TT
19106 const char *comp_dir,
19107 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19108{
d521ce57
TT
19109 const char *include_name = fe.name;
19110 const char *include_name_to_compare = include_name;
72b9f47f 19111 const char *pst_filename;
c6da4cef
DE
19112 int file_is_pst;
19113
8c43009f 19114 const char *dir_name = fe.include_dir (lh);
c6da4cef 19115
c89b44cd 19116 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19117 if (!IS_ABSOLUTE_PATH (include_name)
19118 && (dir_name != NULL || comp_dir != NULL))
19119 {
19120 /* Avoid creating a duplicate psymtab for PST.
19121 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19122 Before we do the comparison, however, we need to account
19123 for DIR_NAME and COMP_DIR.
19124 First prepend dir_name (if non-NULL). If we still don't
19125 have an absolute path prepend comp_dir (if non-NULL).
19126 However, the directory we record in the include-file's
19127 psymtab does not contain COMP_DIR (to match the
19128 corresponding symtab(s)).
19129
19130 Example:
19131
19132 bash$ cd /tmp
19133 bash$ gcc -g ./hello.c
19134 include_name = "hello.c"
19135 dir_name = "."
19136 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19137 DW_AT_name = "./hello.c"
19138
19139 */
c6da4cef
DE
19140
19141 if (dir_name != NULL)
19142 {
c89b44cd
TT
19143 name_holder->reset (concat (dir_name, SLASH_STRING,
19144 include_name, (char *) NULL));
19145 include_name = name_holder->get ();
c6da4cef 19146 include_name_to_compare = include_name;
c6da4cef
DE
19147 }
19148 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19149 {
c89b44cd
TT
19150 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19151 include_name, (char *) NULL));
19152 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19153 }
19154 }
19155
19156 pst_filename = pst->filename;
c89b44cd 19157 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19158 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19159 {
c89b44cd
TT
19160 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19161 pst_filename, (char *) NULL));
19162 pst_filename = copied_name.get ();
c6da4cef
DE
19163 }
19164
1e3fad37 19165 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19166
c6da4cef
DE
19167 if (file_is_pst)
19168 return NULL;
19169 return include_name;
19170}
19171
d9b3de22
DE
19172/* State machine to track the state of the line number program. */
19173
6f77053d 19174class lnp_state_machine
d9b3de22 19175{
6f77053d
PA
19176public:
19177 /* Initialize a machine state for the start of a line number
19178 program. */
804d2729
TT
19179 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19180 bool record_lines_p);
6f77053d 19181
8c43009f
PA
19182 file_entry *current_file ()
19183 {
19184 /* lh->file_names is 0-based, but the file name numbers in the
19185 statement program are 1-based. */
6f77053d
PA
19186 return m_line_header->file_name_at (m_file);
19187 }
19188
19189 /* Record the line in the state machine. END_SEQUENCE is true if
19190 we're processing the end of a sequence. */
19191 void record_line (bool end_sequence);
19192
7ab6656f
OJ
19193 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19194 nop-out rest of the lines in this sequence. */
6f77053d
PA
19195 void check_line_address (struct dwarf2_cu *cu,
19196 const gdb_byte *line_ptr,
7ab6656f 19197 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19198
19199 void handle_set_discriminator (unsigned int discriminator)
19200 {
19201 m_discriminator = discriminator;
19202 m_line_has_non_zero_discriminator |= discriminator != 0;
19203 }
19204
19205 /* Handle DW_LNE_set_address. */
19206 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19207 {
19208 m_op_index = 0;
19209 address += baseaddr;
19210 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19211 }
19212
19213 /* Handle DW_LNS_advance_pc. */
19214 void handle_advance_pc (CORE_ADDR adjust);
19215
19216 /* Handle a special opcode. */
19217 void handle_special_opcode (unsigned char op_code);
19218
19219 /* Handle DW_LNS_advance_line. */
19220 void handle_advance_line (int line_delta)
19221 {
19222 advance_line (line_delta);
19223 }
19224
19225 /* Handle DW_LNS_set_file. */
19226 void handle_set_file (file_name_index file);
19227
19228 /* Handle DW_LNS_negate_stmt. */
19229 void handle_negate_stmt ()
19230 {
19231 m_is_stmt = !m_is_stmt;
19232 }
19233
19234 /* Handle DW_LNS_const_add_pc. */
19235 void handle_const_add_pc ();
19236
19237 /* Handle DW_LNS_fixed_advance_pc. */
19238 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19239 {
19240 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19241 m_op_index = 0;
19242 }
19243
19244 /* Handle DW_LNS_copy. */
19245 void handle_copy ()
19246 {
19247 record_line (false);
19248 m_discriminator = 0;
19249 }
19250
19251 /* Handle DW_LNE_end_sequence. */
19252 void handle_end_sequence ()
19253 {
804d2729 19254 m_currently_recording_lines = true;
6f77053d
PA
19255 }
19256
19257private:
19258 /* Advance the line by LINE_DELTA. */
19259 void advance_line (int line_delta)
19260 {
19261 m_line += line_delta;
19262
19263 if (line_delta != 0)
19264 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19265 }
19266
804d2729
TT
19267 struct dwarf2_cu *m_cu;
19268
6f77053d
PA
19269 gdbarch *m_gdbarch;
19270
19271 /* True if we're recording lines.
19272 Otherwise we're building partial symtabs and are just interested in
19273 finding include files mentioned by the line number program. */
19274 bool m_record_lines_p;
19275
8c43009f 19276 /* The line number header. */
6f77053d 19277 line_header *m_line_header;
8c43009f 19278
6f77053d
PA
19279 /* These are part of the standard DWARF line number state machine,
19280 and initialized according to the DWARF spec. */
d9b3de22 19281
6f77053d 19282 unsigned char m_op_index = 0;
7ba99d21
AT
19283 /* The line table index of the current file. */
19284 file_name_index m_file = 1;
6f77053d
PA
19285 unsigned int m_line = 1;
19286
19287 /* These are initialized in the constructor. */
19288
19289 CORE_ADDR m_address;
19290 bool m_is_stmt;
19291 unsigned int m_discriminator;
d9b3de22
DE
19292
19293 /* Additional bits of state we need to track. */
19294
19295 /* The last file that we called dwarf2_start_subfile for.
19296 This is only used for TLLs. */
6f77053d 19297 unsigned int m_last_file = 0;
d9b3de22 19298 /* The last file a line number was recorded for. */
6f77053d 19299 struct subfile *m_last_subfile = NULL;
d9b3de22 19300
804d2729
TT
19301 /* When true, record the lines we decode. */
19302 bool m_currently_recording_lines = false;
d9b3de22
DE
19303
19304 /* The last line number that was recorded, used to coalesce
19305 consecutive entries for the same line. This can happen, for
19306 example, when discriminators are present. PR 17276. */
6f77053d
PA
19307 unsigned int m_last_line = 0;
19308 bool m_line_has_non_zero_discriminator = false;
8c43009f 19309};
d9b3de22 19310
6f77053d
PA
19311void
19312lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19313{
19314 CORE_ADDR addr_adj = (((m_op_index + adjust)
19315 / m_line_header->maximum_ops_per_instruction)
19316 * m_line_header->minimum_instruction_length);
19317 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19318 m_op_index = ((m_op_index + adjust)
19319 % m_line_header->maximum_ops_per_instruction);
19320}
d9b3de22 19321
6f77053d
PA
19322void
19323lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19324{
6f77053d 19325 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19326 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19327 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19328 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19329 / m_line_header->maximum_ops_per_instruction)
19330 * m_line_header->minimum_instruction_length);
19331 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19332 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19333 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19334
258bf0ee 19335 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19336 advance_line (line_delta);
19337 record_line (false);
19338 m_discriminator = 0;
19339}
d9b3de22 19340
6f77053d
PA
19341void
19342lnp_state_machine::handle_set_file (file_name_index file)
19343{
19344 m_file = file;
19345
19346 const file_entry *fe = current_file ();
19347 if (fe == NULL)
19348 dwarf2_debug_line_missing_file_complaint ();
19349 else if (m_record_lines_p)
19350 {
19351 const char *dir = fe->include_dir (m_line_header);
19352
c24bdb02 19353 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19354 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19355 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19356 }
19357}
19358
19359void
19360lnp_state_machine::handle_const_add_pc ()
19361{
19362 CORE_ADDR adjust
19363 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19364
19365 CORE_ADDR addr_adj
19366 = (((m_op_index + adjust)
19367 / m_line_header->maximum_ops_per_instruction)
19368 * m_line_header->minimum_instruction_length);
19369
19370 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19371 m_op_index = ((m_op_index + adjust)
19372 % m_line_header->maximum_ops_per_instruction);
19373}
d9b3de22 19374
a05a36a5
DE
19375/* Return non-zero if we should add LINE to the line number table.
19376 LINE is the line to add, LAST_LINE is the last line that was added,
19377 LAST_SUBFILE is the subfile for LAST_LINE.
19378 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19379 had a non-zero discriminator.
19380
19381 We have to be careful in the presence of discriminators.
19382 E.g., for this line:
19383
19384 for (i = 0; i < 100000; i++);
19385
19386 clang can emit four line number entries for that one line,
19387 each with a different discriminator.
19388 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19389
19390 However, we want gdb to coalesce all four entries into one.
19391 Otherwise the user could stepi into the middle of the line and
19392 gdb would get confused about whether the pc really was in the
19393 middle of the line.
19394
19395 Things are further complicated by the fact that two consecutive
19396 line number entries for the same line is a heuristic used by gcc
19397 to denote the end of the prologue. So we can't just discard duplicate
19398 entries, we have to be selective about it. The heuristic we use is
19399 that we only collapse consecutive entries for the same line if at least
19400 one of those entries has a non-zero discriminator. PR 17276.
19401
19402 Note: Addresses in the line number state machine can never go backwards
19403 within one sequence, thus this coalescing is ok. */
19404
19405static int
804d2729
TT
19406dwarf_record_line_p (struct dwarf2_cu *cu,
19407 unsigned int line, unsigned int last_line,
a05a36a5
DE
19408 int line_has_non_zero_discriminator,
19409 struct subfile *last_subfile)
19410{
c24bdb02 19411 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19412 return 1;
19413 if (line != last_line)
19414 return 1;
19415 /* Same line for the same file that we've seen already.
19416 As a last check, for pr 17276, only record the line if the line
19417 has never had a non-zero discriminator. */
19418 if (!line_has_non_zero_discriminator)
19419 return 1;
19420 return 0;
19421}
19422
804d2729
TT
19423/* Use the CU's builder to record line number LINE beginning at
19424 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19425
19426static void
d9b3de22 19427dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 19428 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 19429 struct dwarf2_cu *cu)
252a6764
DE
19430{
19431 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19432
27e0867f
DE
19433 if (dwarf_line_debug)
19434 {
19435 fprintf_unfiltered (gdb_stdlog,
19436 "Recording line %u, file %s, address %s\n",
19437 line, lbasename (subfile->name),
19438 paddress (gdbarch, address));
19439 }
19440
804d2729 19441 if (cu != nullptr)
8c95582d 19442 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
19443}
19444
19445/* Subroutine of dwarf_decode_lines_1 to simplify it.
19446 Mark the end of a set of line number records.
d9b3de22 19447 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19448 If SUBFILE is NULL the request is ignored. */
19449
19450static void
19451dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19452 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19453{
27e0867f
DE
19454 if (subfile == NULL)
19455 return;
19456
19457 if (dwarf_line_debug)
19458 {
19459 fprintf_unfiltered (gdb_stdlog,
19460 "Finishing current line, file %s, address %s\n",
19461 lbasename (subfile->name),
19462 paddress (gdbarch, address));
19463 }
19464
8c95582d 19465 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
19466}
19467
6f77053d
PA
19468void
19469lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19470{
d9b3de22
DE
19471 if (dwarf_line_debug)
19472 {
19473 fprintf_unfiltered (gdb_stdlog,
19474 "Processing actual line %u: file %u,"
94a72be7 19475 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19476 m_line, m_file,
6f77053d 19477 paddress (m_gdbarch, m_address),
94a72be7
AB
19478 m_is_stmt, m_discriminator,
19479 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19480 }
19481
6f77053d 19482 file_entry *fe = current_file ();
8c43009f
PA
19483
19484 if (fe == NULL)
d9b3de22
DE
19485 dwarf2_debug_line_missing_file_complaint ();
19486 /* For now we ignore lines not starting on an instruction boundary.
19487 But not when processing end_sequence for compatibility with the
19488 previous version of the code. */
6f77053d 19489 else if (m_op_index == 0 || end_sequence)
d9b3de22 19490 {
8c43009f 19491 fe->included_p = 1;
8c95582d 19492 if (m_record_lines_p)
d9b3de22 19493 {
c24bdb02 19494 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19495 || end_sequence)
d9b3de22 19496 {
804d2729
TT
19497 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19498 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19499 }
19500
19501 if (!end_sequence)
19502 {
8c95582d
AB
19503 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19504
804d2729 19505 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
19506 m_line_has_non_zero_discriminator,
19507 m_last_subfile))
d9b3de22 19508 {
c24bdb02 19509 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 19510 dwarf_record_line_1 (m_gdbarch,
c24bdb02 19511 builder->get_current_subfile (),
8c95582d 19512 m_line, m_address, is_stmt,
804d2729 19513 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 19514 }
c24bdb02 19515 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19516 m_last_line = m_line;
d9b3de22
DE
19517 }
19518 }
19519 }
19520}
19521
804d2729
TT
19522lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19523 line_header *lh, bool record_lines_p)
d9b3de22 19524{
804d2729 19525 m_cu = cu;
6f77053d
PA
19526 m_gdbarch = arch;
19527 m_record_lines_p = record_lines_p;
19528 m_line_header = lh;
d9b3de22 19529
804d2729 19530 m_currently_recording_lines = true;
d9b3de22 19531
d9b3de22
DE
19532 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19533 was a line entry for it so that the backend has a chance to adjust it
19534 and also record it in case it needs it. This is currently used by MIPS
19535 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19536 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19537 m_is_stmt = lh->default_is_stmt;
19538 m_discriminator = 0;
252a6764
DE
19539}
19540
6f77053d
PA
19541void
19542lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19543 const gdb_byte *line_ptr,
7ab6656f 19544 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 19545{
7ab6656f
OJ
19546 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19547 the pc range of the CU. However, we restrict the test to only ADDRESS
19548 values of zero to preserve GDB's previous behaviour which is to handle
19549 the specific case of a function being GC'd by the linker. */
924c2928 19550
7ab6656f 19551 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
19552 {
19553 /* This line table is for a function which has been
19554 GCd by the linker. Ignore it. PR gdb/12528 */
19555
518817b3 19556 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
19557 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19558
b98664d3 19559 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 19560 line_offset, objfile_name (objfile));
804d2729
TT
19561 m_currently_recording_lines = false;
19562 /* Note: m_currently_recording_lines is left as false until we see
19563 DW_LNE_end_sequence. */
924c2928
DE
19564 }
19565}
19566
f3f5162e 19567/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19568 Process the line number information in LH.
19569 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19570 program in order to set included_p for every referenced header. */
debd256d 19571
c906108c 19572static void
43f3e411
DE
19573dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19574 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19575{
d521ce57
TT
19576 const gdb_byte *line_ptr, *extended_end;
19577 const gdb_byte *line_end;
a8c50c1f 19578 unsigned int bytes_read, extended_len;
699ca60a 19579 unsigned char op_code, extended_op;
e142c38c 19580 CORE_ADDR baseaddr;
518817b3 19581 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 19582 bfd *abfd = objfile->obfd;
fbf65064 19583 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
19584 /* True if we're recording line info (as opposed to building partial
19585 symtabs and just interested in finding include files mentioned by
19586 the line number program). */
19587 bool record_lines_p = !decode_for_pst_p;
e142c38c 19588
b3b3bada 19589 baseaddr = objfile->text_section_offset ();
c906108c 19590
debd256d
JB
19591 line_ptr = lh->statement_program_start;
19592 line_end = lh->statement_program_end;
c906108c
SS
19593
19594 /* Read the statement sequences until there's nothing left. */
19595 while (line_ptr < line_end)
19596 {
6f77053d
PA
19597 /* The DWARF line number program state machine. Reset the state
19598 machine at the start of each sequence. */
804d2729 19599 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 19600 bool end_sequence = false;
d9b3de22 19601
8c43009f 19602 if (record_lines_p)
c906108c 19603 {
8c43009f
PA
19604 /* Start a subfile for the current file of the state
19605 machine. */
19606 const file_entry *fe = state_machine.current_file ();
19607
19608 if (fe != NULL)
804d2729 19609 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
19610 }
19611
a738430d 19612 /* Decode the table. */
d9b3de22 19613 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19614 {
19615 op_code = read_1_byte (abfd, line_ptr);
19616 line_ptr += 1;
9aa1fe7e 19617
debd256d 19618 if (op_code >= lh->opcode_base)
6e70227d 19619 {
8e07a239 19620 /* Special opcode. */
6f77053d 19621 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19622 }
19623 else switch (op_code)
c906108c
SS
19624 {
19625 case DW_LNS_extended_op:
3e43a32a
MS
19626 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19627 &bytes_read);
473b7be6 19628 line_ptr += bytes_read;
a8c50c1f 19629 extended_end = line_ptr + extended_len;
c906108c
SS
19630 extended_op = read_1_byte (abfd, line_ptr);
19631 line_ptr += 1;
19632 switch (extended_op)
19633 {
19634 case DW_LNE_end_sequence:
6f77053d
PA
19635 state_machine.handle_end_sequence ();
19636 end_sequence = true;
c906108c
SS
19637 break;
19638 case DW_LNE_set_address:
d9b3de22
DE
19639 {
19640 CORE_ADDR address
c8a7a66f 19641 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 19642 line_ptr += bytes_read;
6f77053d
PA
19643
19644 state_machine.check_line_address (cu, line_ptr,
7ab6656f 19645 lowpc - baseaddr, address);
6f77053d 19646 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19647 }
c906108c
SS
19648 break;
19649 case DW_LNE_define_file:
debd256d 19650 {
d521ce57 19651 const char *cur_file;
ecfb656c
PA
19652 unsigned int mod_time, length;
19653 dir_index dindex;
6e70227d 19654
3e43a32a
MS
19655 cur_file = read_direct_string (abfd, line_ptr,
19656 &bytes_read);
debd256d 19657 line_ptr += bytes_read;
ecfb656c 19658 dindex = (dir_index)
debd256d
JB
19659 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19660 line_ptr += bytes_read;
19661 mod_time =
19662 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19663 line_ptr += bytes_read;
19664 length =
19665 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19666 line_ptr += bytes_read;
ecfb656c 19667 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19668 }
c906108c 19669 break;
d0c6ba3d 19670 case DW_LNE_set_discriminator:
6f77053d
PA
19671 {
19672 /* The discriminator is not interesting to the
19673 debugger; just ignore it. We still need to
19674 check its value though:
19675 if there are consecutive entries for the same
19676 (non-prologue) line we want to coalesce them.
19677 PR 17276. */
19678 unsigned int discr
19679 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19680 line_ptr += bytes_read;
19681
19682 state_machine.handle_set_discriminator (discr);
19683 }
d0c6ba3d 19684 break;
c906108c 19685 default:
b98664d3 19686 complaint (_("mangled .debug_line section"));
debd256d 19687 return;
c906108c 19688 }
a8c50c1f
DJ
19689 /* Make sure that we parsed the extended op correctly. If e.g.
19690 we expected a different address size than the producer used,
19691 we may have read the wrong number of bytes. */
19692 if (line_ptr != extended_end)
19693 {
b98664d3 19694 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
19695 return;
19696 }
c906108c
SS
19697 break;
19698 case DW_LNS_copy:
6f77053d 19699 state_machine.handle_copy ();
c906108c
SS
19700 break;
19701 case DW_LNS_advance_pc:
2dc7f7b3
TT
19702 {
19703 CORE_ADDR adjust
19704 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19705 line_ptr += bytes_read;
6f77053d
PA
19706
19707 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19708 }
c906108c
SS
19709 break;
19710 case DW_LNS_advance_line:
a05a36a5
DE
19711 {
19712 int line_delta
19713 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19714 line_ptr += bytes_read;
6f77053d
PA
19715
19716 state_machine.handle_advance_line (line_delta);
a05a36a5 19717 }
c906108c
SS
19718 break;
19719 case DW_LNS_set_file:
d9b3de22 19720 {
6f77053d 19721 file_name_index file
ecfb656c
PA
19722 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19723 &bytes_read);
d9b3de22 19724 line_ptr += bytes_read;
8c43009f 19725
6f77053d 19726 state_machine.handle_set_file (file);
d9b3de22 19727 }
c906108c
SS
19728 break;
19729 case DW_LNS_set_column:
0ad93d4f 19730 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19731 line_ptr += bytes_read;
19732 break;
19733 case DW_LNS_negate_stmt:
6f77053d 19734 state_machine.handle_negate_stmt ();
c906108c
SS
19735 break;
19736 case DW_LNS_set_basic_block:
c906108c 19737 break;
c2c6d25f
JM
19738 /* Add to the address register of the state machine the
19739 address increment value corresponding to special opcode
a738430d
MK
19740 255. I.e., this value is scaled by the minimum
19741 instruction length since special opcode 255 would have
b021a221 19742 scaled the increment. */
c906108c 19743 case DW_LNS_const_add_pc:
6f77053d 19744 state_machine.handle_const_add_pc ();
c906108c
SS
19745 break;
19746 case DW_LNS_fixed_advance_pc:
3e29f34a 19747 {
6f77053d 19748 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 19749 line_ptr += 2;
6f77053d
PA
19750
19751 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 19752 }
c906108c 19753 break;
9aa1fe7e 19754 default:
a738430d
MK
19755 {
19756 /* Unknown standard opcode, ignore it. */
9aa1fe7e 19757 int i;
a738430d 19758
debd256d 19759 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
19760 {
19761 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19762 line_ptr += bytes_read;
19763 }
19764 }
c906108c
SS
19765 }
19766 }
d9b3de22
DE
19767
19768 if (!end_sequence)
19769 dwarf2_debug_line_missing_end_sequence_complaint ();
19770
19771 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19772 in which case we still finish recording the last line). */
6f77053d 19773 state_machine.record_line (true);
c906108c 19774 }
f3f5162e
DE
19775}
19776
19777/* Decode the Line Number Program (LNP) for the given line_header
19778 structure and CU. The actual information extracted and the type
19779 of structures created from the LNP depends on the value of PST.
19780
19781 1. If PST is NULL, then this procedure uses the data from the program
19782 to create all necessary symbol tables, and their linetables.
19783
19784 2. If PST is not NULL, this procedure reads the program to determine
19785 the list of files included by the unit represented by PST, and
19786 builds all the associated partial symbol tables.
19787
19788 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19789 It is used for relative paths in the line table.
19790 NOTE: When processing partial symtabs (pst != NULL),
19791 comp_dir == pst->dirname.
19792
19793 NOTE: It is important that psymtabs have the same file name (via strcmp)
19794 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19795 symtab we don't use it in the name of the psymtabs we create.
19796 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
19797 A good testcase for this is mb-inline.exp.
19798
527f3840
JK
19799 LOWPC is the lowest address in CU (or 0 if not known).
19800
19801 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19802 for its PC<->lines mapping information. Otherwise only the filename
19803 table is read in. */
f3f5162e
DE
19804
19805static void
19806dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 19807 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 19808 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 19809{
518817b3 19810 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 19811 const int decode_for_pst_p = (pst != NULL);
f3f5162e 19812
527f3840
JK
19813 if (decode_mapping)
19814 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
19815
19816 if (decode_for_pst_p)
19817 {
aaa75496
JB
19818 /* Now that we're done scanning the Line Header Program, we can
19819 create the psymtab of each included file. */
7ba99d21
AT
19820 for (auto &file_entry : lh->file_names ())
19821 if (file_entry.included_p == 1)
aaa75496 19822 {
c89b44cd 19823 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 19824 const char *include_name =
7ba99d21
AT
19825 psymtab_include_file_name (lh, file_entry, pst,
19826 comp_dir, &name_holder);
c6da4cef 19827 if (include_name != NULL)
aaa75496
JB
19828 dwarf2_create_include_psymtab (include_name, pst, objfile);
19829 }
19830 }
cb1df416
DJ
19831 else
19832 {
19833 /* Make sure a symtab is created for every file, even files
19834 which contain only variables (i.e. no code with associated
19835 line numbers). */
c24bdb02
KS
19836 buildsym_compunit *builder = cu->get_builder ();
19837 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 19838
7ba99d21 19839 for (auto &fe : lh->file_names ())
cb1df416 19840 {
804d2729 19841 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 19842 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 19843 {
c24bdb02 19844 builder->get_current_subfile ()->symtab
804d2729 19845 = allocate_symtab (cust,
c24bdb02 19846 builder->get_current_subfile ()->name);
43f3e411 19847 }
c24bdb02 19848 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
19849 }
19850 }
c906108c
SS
19851}
19852
19853/* Start a subfile for DWARF. FILENAME is the name of the file and
19854 DIRNAME the name of the source directory which contains FILENAME
4d663531 19855 or NULL if not known.
c906108c
SS
19856 This routine tries to keep line numbers from identical absolute and
19857 relative file names in a common subfile.
19858
19859 Using the `list' example from the GDB testsuite, which resides in
19860 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19861 of /srcdir/list0.c yields the following debugging information for list0.c:
19862
c5aa993b 19863 DW_AT_name: /srcdir/list0.c
4d663531 19864 DW_AT_comp_dir: /compdir
357e46e7 19865 files.files[0].name: list0.h
c5aa993b 19866 files.files[0].dir: /srcdir
357e46e7 19867 files.files[1].name: list0.c
c5aa993b 19868 files.files[1].dir: /srcdir
c906108c
SS
19869
19870 The line number information for list0.c has to end up in a single
4f1520fb
FR
19871 subfile, so that `break /srcdir/list0.c:1' works as expected.
19872 start_subfile will ensure that this happens provided that we pass the
19873 concatenation of files.files[1].dir and files.files[1].name as the
19874 subfile's name. */
c906108c
SS
19875
19876static void
804d2729
TT
19877dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19878 const char *dirname)
c906108c 19879{
43816ebc 19880 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 19881
4d663531 19882 /* In order not to lose the line information directory,
4f1520fb
FR
19883 we concatenate it to the filename when it makes sense.
19884 Note that the Dwarf3 standard says (speaking of filenames in line
19885 information): ``The directory index is ignored for file names
19886 that represent full path names''. Thus ignoring dirname in the
19887 `else' branch below isn't an issue. */
c906108c 19888
d5166ae1 19889 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 19890 {
43816ebc
TT
19891 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19892 filename = copy.get ();
d521ce57 19893 }
c906108c 19894
c24bdb02 19895 cu->get_builder ()->start_subfile (filename);
c906108c
SS
19896}
19897
804d2729
TT
19898/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19899 buildsym_compunit constructor. */
f4dc4d17 19900
c24bdb02
KS
19901struct compunit_symtab *
19902dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19903 CORE_ADDR low_pc)
f4dc4d17 19904{
c24bdb02 19905 gdb_assert (m_builder == nullptr);
43f3e411 19906
c24bdb02
KS
19907 m_builder.reset (new struct buildsym_compunit
19908 (per_cu->dwarf2_per_objfile->objfile,
19909 name, comp_dir, language, low_pc));
93b8bea4 19910
c24bdb02 19911 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 19912
c24bdb02
KS
19913 get_builder ()->record_debugformat ("DWARF 2");
19914 get_builder ()->record_producer (producer);
f4dc4d17 19915
c24bdb02 19916 processing_has_namespace_info = false;
43f3e411 19917
c24bdb02 19918 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
19919}
19920
4c2df51b
DJ
19921static void
19922var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 19923 struct dwarf2_cu *cu)
4c2df51b 19924{
518817b3 19925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
19926 struct comp_unit_head *cu_header = &cu->header;
19927
4c2df51b
DJ
19928 /* NOTE drow/2003-01-30: There used to be a comment and some special
19929 code here to turn a symbol with DW_AT_external and a
19930 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19931 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19932 with some versions of binutils) where shared libraries could have
19933 relocations against symbols in their debug information - the
19934 minimal symbol would have the right address, but the debug info
19935 would not. It's no longer necessary, because we will explicitly
19936 apply relocations when we read in the debug information now. */
19937
19938 /* A DW_AT_location attribute with no contents indicates that a
19939 variable has been optimized away. */
4fc6c0d5 19940 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 19941 {
f1e6e072 19942 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19943 return;
19944 }
19945
19946 /* Handle one degenerate form of location expression specially, to
19947 preserve GDB's previous behavior when section offsets are
336d760d
AT
19948 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
19949 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 19950
4fc6c0d5 19951 if (attr->form_is_block ()
3019eac3
DE
19952 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19953 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
19954 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19955 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
19956 && (DW_BLOCK (attr)->size
19957 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19958 {
891d2f0b 19959 unsigned int dummy;
4c2df51b 19960
3019eac3 19961 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
19962 SET_SYMBOL_VALUE_ADDRESS
19963 (sym, cu->header.read_address (objfile->obfd,
19964 DW_BLOCK (attr)->data + 1,
19965 &dummy));
3019eac3 19966 else
38583298
TT
19967 SET_SYMBOL_VALUE_ADDRESS
19968 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
19969 &dummy));
f1e6e072 19970 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 19971 fixup_symbol_section (sym, objfile);
6a053cb1
TT
19972 SET_SYMBOL_VALUE_ADDRESS
19973 (sym,
19974 SYMBOL_VALUE_ADDRESS (sym)
19975 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
19976 return;
19977 }
19978
19979 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19980 expression evaluator, and use LOC_COMPUTED only when necessary
19981 (i.e. when the value of a register or memory location is
19982 referenced, or a thread-local block, etc.). Then again, it might
19983 not be worthwhile. I'm assuming that it isn't unless performance
19984 or memory numbers show me otherwise. */
19985
f1e6e072 19986 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19987
f1e6e072 19988 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 19989 cu->has_loclist = true;
4c2df51b
DJ
19990}
19991
c906108c
SS
19992/* Given a pointer to a DWARF information entry, figure out if we need
19993 to make a symbol table entry for it, and if so, create a new entry
19994 and return a pointer to it.
19995 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19996 used the passed type.
19997 If SPACE is not NULL, use it to hold the new symbol. If it is
19998 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19999
20000static struct symbol *
5e2db402
TT
20001new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20002 struct symbol *space)
c906108c 20003{
518817b3
SM
20004 struct dwarf2_per_objfile *dwarf2_per_objfile
20005 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20006 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20007 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20008 struct symbol *sym = NULL;
15d034d0 20009 const char *name;
c906108c
SS
20010 struct attribute *attr = NULL;
20011 struct attribute *attr2 = NULL;
e142c38c 20012 CORE_ADDR baseaddr;
e37fd15a
SW
20013 struct pending **list_to_add = NULL;
20014
edb3359d 20015 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20016
b3b3bada 20017 baseaddr = objfile->text_section_offset ();
c906108c 20018
94af9270 20019 name = dwarf2_name (die, cu);
c906108c
SS
20020 if (name)
20021 {
94af9270 20022 const char *linkagename;
34eaf542 20023 int suppress_add = 0;
94af9270 20024
34eaf542
TT
20025 if (space)
20026 sym = space;
20027 else
e623cf5d 20028 sym = allocate_symbol (objfile);
c906108c 20029 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20030
20031 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20032 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20033 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20034 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20035
f55ee35c
JK
20036 /* Fortran does not have mangling standard and the mangling does differ
20037 between gfortran, iFort etc. */
20038 if (cu->language == language_fortran
468c0cbb
CB
20039 && symbol_get_demangled_name (sym) == NULL)
20040 symbol_set_demangled_name (sym,
cfc594ee 20041 dwarf2_full_name (name, die, cu),
29df156d 20042 NULL);
f55ee35c 20043
c906108c 20044 /* Default assumptions.
c5aa993b 20045 Use the passed type or decode it from the die. */
176620f1 20046 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20047 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20048 if (type != NULL)
20049 SYMBOL_TYPE (sym) = type;
20050 else
e7c27a73 20051 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20052 attr = dwarf2_attr (die,
20053 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20054 cu);
435d3d88 20055 if (attr != nullptr)
c906108c
SS
20056 {
20057 SYMBOL_LINE (sym) = DW_UNSND (attr);
20058 }
cb1df416 20059
edb3359d
DJ
20060 attr = dwarf2_attr (die,
20061 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20062 cu);
435d3d88 20063 if (attr != nullptr)
cb1df416 20064 {
ecfb656c 20065 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20066 struct file_entry *fe;
9a619af0 20067
ecfb656c
PA
20068 if (cu->line_header != NULL)
20069 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20070 else
20071 fe = NULL;
20072
20073 if (fe == NULL)
b98664d3 20074 complaint (_("file index out of range"));
8c43009f
PA
20075 else
20076 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20077 }
20078
c906108c
SS
20079 switch (die->tag)
20080 {
20081 case DW_TAG_label:
e142c38c 20082 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20083 if (attr != nullptr)
3e29f34a
MR
20084 {
20085 CORE_ADDR addr;
20086
cd6c91b4 20087 addr = attr->value_as_address ();
3e29f34a 20088 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20089 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20090 }
0f5238ed
TT
20091 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20092 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20093 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20094 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20095 break;
20096 case DW_TAG_subprogram:
20097 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20098 finish_block. */
f1e6e072 20099 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20100 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20101 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20102 || cu->language == language_ada
20103 || cu->language == language_fortran)
c906108c 20104 {
2cfa0c8d 20105 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20106 Ada and Fortran subprograms, whether marked external or
20107 not, are always stored as a global symbol, because we want
20108 to be able to access them globally. For instance, we want
20109 to be able to break on a nested subprogram without having
20110 to specify the context. */
c24bdb02 20111 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20112 }
20113 else
20114 {
e37fd15a 20115 list_to_add = cu->list_in_scope;
c906108c
SS
20116 }
20117 break;
edb3359d
DJ
20118 case DW_TAG_inlined_subroutine:
20119 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20120 finish_block. */
f1e6e072 20121 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20122 SYMBOL_INLINED (sym) = 1;
481860b3 20123 list_to_add = cu->list_in_scope;
edb3359d 20124 break;
34eaf542
TT
20125 case DW_TAG_template_value_param:
20126 suppress_add = 1;
20127 /* Fall through. */
72929c62 20128 case DW_TAG_constant:
c906108c 20129 case DW_TAG_variable:
254e6b9e 20130 case DW_TAG_member:
0963b4bd
MS
20131 /* Compilation with minimal debug info may result in
20132 variables with missing type entries. Change the
20133 misleading `void' type to something sensible. */
c906108c 20134 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20135 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20136
e142c38c 20137 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20138 /* In the case of DW_TAG_member, we should only be called for
20139 static const members. */
20140 if (die->tag == DW_TAG_member)
20141 {
3863f96c
DE
20142 /* dwarf2_add_field uses die_is_declaration,
20143 so we do the same. */
254e6b9e
DE
20144 gdb_assert (die_is_declaration (die, cu));
20145 gdb_assert (attr);
20146 }
435d3d88 20147 if (attr != nullptr)
c906108c 20148 {
e7c27a73 20149 dwarf2_const_value (attr, sym, cu);
e142c38c 20150 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20151 if (!suppress_add)
34eaf542
TT
20152 {
20153 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20154 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20155 else
e37fd15a 20156 list_to_add = cu->list_in_scope;
34eaf542 20157 }
c906108c
SS
20158 break;
20159 }
e142c38c 20160 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20161 if (attr != nullptr)
c906108c 20162 {
e7c27a73 20163 var_decode_location (attr, sym, cu);
e142c38c 20164 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20165
20166 /* Fortran explicitly imports any global symbols to the local
20167 scope by DW_TAG_common_block. */
20168 if (cu->language == language_fortran && die->parent
20169 && die->parent->tag == DW_TAG_common_block)
20170 attr2 = NULL;
20171
caac4577
JG
20172 if (SYMBOL_CLASS (sym) == LOC_STATIC
20173 && SYMBOL_VALUE_ADDRESS (sym) == 0
20174 && !dwarf2_per_objfile->has_section_at_zero)
20175 {
20176 /* When a static variable is eliminated by the linker,
20177 the corresponding debug information is not stripped
20178 out, but the variable address is set to null;
20179 do not add such variables into symbol table. */
20180 }
20181 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20182 {
4b610737
TT
20183 if (SYMBOL_CLASS (sym) == LOC_STATIC
20184 && (objfile->flags & OBJF_MAINLINE) == 0
20185 && dwarf2_per_objfile->can_copy)
20186 {
20187 /* A global static variable might be subject to
20188 copy relocation. We first check for a local
20189 minsym, though, because maybe the symbol was
20190 marked hidden, in which case this would not
20191 apply. */
20192 bound_minimal_symbol found
20193 = (lookup_minimal_symbol_linkage
987012b8 20194 (sym->linkage_name (), objfile));
4b610737
TT
20195 if (found.minsym != nullptr)
20196 sym->maybe_copied = 1;
20197 }
f55ee35c 20198
1c809c68
TT
20199 /* A variable with DW_AT_external is never static,
20200 but it may be block-scoped. */
804d2729 20201 list_to_add
c24bdb02
KS
20202 = ((cu->list_in_scope
20203 == cu->get_builder ()->get_file_symbols ())
20204 ? cu->get_builder ()->get_global_symbols ()
804d2729 20205 : cu->list_in_scope);
1c809c68 20206 }
c906108c 20207 else
e37fd15a 20208 list_to_add = cu->list_in_scope;
c906108c
SS
20209 }
20210 else
20211 {
20212 /* We do not know the address of this symbol.
c5aa993b
JM
20213 If it is an external symbol and we have type information
20214 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20215 The address of the variable will then be determined from
20216 the minimal symbol table whenever the variable is
20217 referenced. */
e142c38c 20218 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20219
20220 /* Fortran explicitly imports any global symbols to the local
20221 scope by DW_TAG_common_block. */
20222 if (cu->language == language_fortran && die->parent
20223 && die->parent->tag == DW_TAG_common_block)
20224 {
20225 /* SYMBOL_CLASS doesn't matter here because
20226 read_common_block is going to reset it. */
20227 if (!suppress_add)
20228 list_to_add = cu->list_in_scope;
20229 }
20230 else if (attr2 && (DW_UNSND (attr2) != 0)
20231 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20232 {
0fe7935b
DJ
20233 /* A variable with DW_AT_external is never static, but it
20234 may be block-scoped. */
804d2729 20235 list_to_add
c24bdb02
KS
20236 = ((cu->list_in_scope
20237 == cu->get_builder ()->get_file_symbols ())
20238 ? cu->get_builder ()->get_global_symbols ()
804d2729 20239 : cu->list_in_scope);
0fe7935b 20240
f1e6e072 20241 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20242 }
442ddf59
JK
20243 else if (!die_is_declaration (die, cu))
20244 {
20245 /* Use the default LOC_OPTIMIZED_OUT class. */
20246 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20247 if (!suppress_add)
20248 list_to_add = cu->list_in_scope;
442ddf59 20249 }
c906108c
SS
20250 }
20251 break;
20252 case DW_TAG_formal_parameter:
a60f3166
TT
20253 {
20254 /* If we are inside a function, mark this as an argument. If
20255 not, we might be looking at an argument to an inlined function
20256 when we do not have enough information to show inlined frames;
20257 pretend it's a local variable in that case so that the user can
20258 still see it. */
804d2729 20259 struct context_stack *curr
c24bdb02 20260 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20261 if (curr != nullptr && curr->name != nullptr)
20262 SYMBOL_IS_ARGUMENT (sym) = 1;
20263 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20264 if (attr != nullptr)
a60f3166
TT
20265 {
20266 var_decode_location (attr, sym, cu);
20267 }
20268 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20269 if (attr != nullptr)
a60f3166
TT
20270 {
20271 dwarf2_const_value (attr, sym, cu);
20272 }
f346a30d 20273
a60f3166
TT
20274 list_to_add = cu->list_in_scope;
20275 }
c906108c
SS
20276 break;
20277 case DW_TAG_unspecified_parameters:
20278 /* From varargs functions; gdb doesn't seem to have any
20279 interest in this information, so just ignore it for now.
20280 (FIXME?) */
20281 break;
34eaf542
TT
20282 case DW_TAG_template_type_param:
20283 suppress_add = 1;
20284 /* Fall through. */
c906108c 20285 case DW_TAG_class_type:
680b30c7 20286 case DW_TAG_interface_type:
c906108c
SS
20287 case DW_TAG_structure_type:
20288 case DW_TAG_union_type:
72019c9c 20289 case DW_TAG_set_type:
c906108c 20290 case DW_TAG_enumeration_type:
f1e6e072 20291 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20292 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20293
63d06c5c 20294 {
9c37b5ae 20295 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20296 really ever be static objects: otherwise, if you try
20297 to, say, break of a class's method and you're in a file
20298 which doesn't mention that class, it won't work unless
20299 the check for all static symbols in lookup_symbol_aux
20300 saves you. See the OtherFileClass tests in
20301 gdb.c++/namespace.exp. */
20302
e37fd15a 20303 if (!suppress_add)
34eaf542 20304 {
c24bdb02 20305 buildsym_compunit *builder = cu->get_builder ();
804d2729 20306 list_to_add
c24bdb02 20307 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20308 && cu->language == language_cplus
c24bdb02 20309 ? builder->get_global_symbols ()
804d2729 20310 : cu->list_in_scope);
63d06c5c 20311
64382290 20312 /* The semantics of C++ state that "struct foo {
9c37b5ae 20313 ... }" also defines a typedef for "foo". */
64382290 20314 if (cu->language == language_cplus
45280282 20315 || cu->language == language_ada
c44af4eb
TT
20316 || cu->language == language_d
20317 || cu->language == language_rust)
64382290
TT
20318 {
20319 /* The symbol's name is already allocated along
20320 with this objfile, so we don't need to
20321 duplicate it for the type. */
20322 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20323 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20324 }
63d06c5c
DC
20325 }
20326 }
c906108c
SS
20327 break;
20328 case DW_TAG_typedef:
f1e6e072 20329 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20330 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20331 list_to_add = cu->list_in_scope;
63d06c5c 20332 break;
c906108c 20333 case DW_TAG_base_type:
a02abb62 20334 case DW_TAG_subrange_type:
f1e6e072 20335 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20336 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20337 list_to_add = cu->list_in_scope;
c906108c
SS
20338 break;
20339 case DW_TAG_enumerator:
e142c38c 20340 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20341 if (attr != nullptr)
c906108c 20342 {
e7c27a73 20343 dwarf2_const_value (attr, sym, cu);
c906108c 20344 }
63d06c5c
DC
20345 {
20346 /* NOTE: carlton/2003-11-10: See comment above in the
20347 DW_TAG_class_type, etc. block. */
20348
804d2729 20349 list_to_add
c24bdb02 20350 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20351 && cu->language == language_cplus
c24bdb02 20352 ? cu->get_builder ()->get_global_symbols ()
804d2729 20353 : cu->list_in_scope);
63d06c5c 20354 }
c906108c 20355 break;
74921315 20356 case DW_TAG_imported_declaration:
5c4e30ca 20357 case DW_TAG_namespace:
f1e6e072 20358 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20359 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20360 break;
530e8392
KB
20361 case DW_TAG_module:
20362 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20363 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20364 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20365 break;
4357ac6c 20366 case DW_TAG_common_block:
f1e6e072 20367 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20368 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20369 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20370 break;
c906108c
SS
20371 default:
20372 /* Not a tag we recognize. Hopefully we aren't processing
20373 trash data, but since we must specifically ignore things
20374 we don't recognize, there is nothing else we should do at
0963b4bd 20375 this point. */
b98664d3 20376 complaint (_("unsupported tag: '%s'"),
4d3c2250 20377 dwarf_tag_name (die->tag));
c906108c
SS
20378 break;
20379 }
df8a16a1 20380
e37fd15a
SW
20381 if (suppress_add)
20382 {
20383 sym->hash_next = objfile->template_symbols;
20384 objfile->template_symbols = sym;
20385 list_to_add = NULL;
20386 }
20387
20388 if (list_to_add != NULL)
d3cb6808 20389 add_symbol_to_list (sym, list_to_add);
e37fd15a 20390
df8a16a1
DJ
20391 /* For the benefit of old versions of GCC, check for anonymous
20392 namespaces based on the demangled name. */
4d4ec4e5 20393 if (!cu->processing_has_namespace_info
94af9270 20394 && cu->language == language_cplus)
c24bdb02 20395 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20396 }
20397 return (sym);
20398}
20399
98bfdba5
PA
20400/* Given an attr with a DW_FORM_dataN value in host byte order,
20401 zero-extend it as appropriate for the symbol's type. The DWARF
20402 standard (v4) is not entirely clear about the meaning of using
20403 DW_FORM_dataN for a constant with a signed type, where the type is
20404 wider than the data. The conclusion of a discussion on the DWARF
20405 list was that this is unspecified. We choose to always zero-extend
20406 because that is the interpretation long in use by GCC. */
c906108c 20407
98bfdba5 20408static gdb_byte *
ff39bb5e 20409dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20410 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20411{
518817b3 20412 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20413 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20414 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20415 LONGEST l = DW_UNSND (attr);
20416
20417 if (bits < sizeof (*value) * 8)
20418 {
20419 l &= ((LONGEST) 1 << bits) - 1;
20420 *value = l;
20421 }
20422 else if (bits == sizeof (*value) * 8)
20423 *value = l;
20424 else
20425 {
224c3ddb 20426 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20427 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20428 return bytes;
20429 }
20430
20431 return NULL;
20432}
20433
20434/* Read a constant value from an attribute. Either set *VALUE, or if
20435 the value does not fit in *VALUE, set *BYTES - either already
20436 allocated on the objfile obstack, or newly allocated on OBSTACK,
20437 or, set *BATON, if we translated the constant to a location
20438 expression. */
20439
20440static void
ff39bb5e 20441dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20442 const char *name, struct obstack *obstack,
20443 struct dwarf2_cu *cu,
d521ce57 20444 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20445 struct dwarf2_locexpr_baton **baton)
20446{
518817b3 20447 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20448 struct comp_unit_head *cu_header = &cu->header;
c906108c 20449 struct dwarf_block *blk;
98bfdba5
PA
20450 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20451 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20452
20453 *value = 0;
20454 *bytes = NULL;
20455 *baton = NULL;
c906108c
SS
20456
20457 switch (attr->form)
20458 {
20459 case DW_FORM_addr:
336d760d 20460 case DW_FORM_addrx:
3019eac3 20461 case DW_FORM_GNU_addr_index:
ac56253d 20462 {
ac56253d
TT
20463 gdb_byte *data;
20464
98bfdba5
PA
20465 if (TYPE_LENGTH (type) != cu_header->addr_size)
20466 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20467 cu_header->addr_size,
98bfdba5 20468 TYPE_LENGTH (type));
ac56253d
TT
20469 /* Symbols of this form are reasonably rare, so we just
20470 piggyback on the existing location code rather than writing
20471 a new implementation of symbol_computed_ops. */
8d749320 20472 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20473 (*baton)->per_cu = cu->per_cu;
20474 gdb_assert ((*baton)->per_cu);
ac56253d 20475
98bfdba5 20476 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20477 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20478 (*baton)->data = data;
ac56253d
TT
20479
20480 data[0] = DW_OP_addr;
20481 store_unsigned_integer (&data[1], cu_header->addr_size,
20482 byte_order, DW_ADDR (attr));
20483 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20484 }
c906108c 20485 break;
4ac36638 20486 case DW_FORM_string:
93b5768b 20487 case DW_FORM_strp:
cf532bd1 20488 case DW_FORM_strx:
3019eac3 20489 case DW_FORM_GNU_str_index:
36586728 20490 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20491 /* DW_STRING is already allocated on the objfile obstack, point
20492 directly to it. */
d521ce57 20493 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20494 break;
c906108c
SS
20495 case DW_FORM_block1:
20496 case DW_FORM_block2:
20497 case DW_FORM_block4:
20498 case DW_FORM_block:
2dc7f7b3 20499 case DW_FORM_exprloc:
0224619f 20500 case DW_FORM_data16:
c906108c 20501 blk = DW_BLOCK (attr);
98bfdba5
PA
20502 if (TYPE_LENGTH (type) != blk->size)
20503 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20504 TYPE_LENGTH (type));
20505 *bytes = blk->data;
c906108c 20506 break;
2df3850c
JM
20507
20508 /* The DW_AT_const_value attributes are supposed to carry the
20509 symbol's value "represented as it would be on the target
20510 architecture." By the time we get here, it's already been
20511 converted to host endianness, so we just need to sign- or
20512 zero-extend it as appropriate. */
20513 case DW_FORM_data1:
3aef2284 20514 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20515 break;
c906108c 20516 case DW_FORM_data2:
3aef2284 20517 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20518 break;
c906108c 20519 case DW_FORM_data4:
3aef2284 20520 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20521 break;
c906108c 20522 case DW_FORM_data8:
3aef2284 20523 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20524 break;
20525
c906108c 20526 case DW_FORM_sdata:
663c44ac 20527 case DW_FORM_implicit_const:
98bfdba5 20528 *value = DW_SND (attr);
2df3850c
JM
20529 break;
20530
c906108c 20531 case DW_FORM_udata:
98bfdba5 20532 *value = DW_UNSND (attr);
c906108c 20533 break;
2df3850c 20534
c906108c 20535 default:
b98664d3 20536 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 20537 dwarf_form_name (attr->form));
98bfdba5 20538 *value = 0;
c906108c
SS
20539 break;
20540 }
20541}
20542
2df3850c 20543
98bfdba5
PA
20544/* Copy constant value from an attribute to a symbol. */
20545
2df3850c 20546static void
ff39bb5e 20547dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20548 struct dwarf2_cu *cu)
2df3850c 20549{
518817b3 20550 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 20551 LONGEST value;
d521ce57 20552 const gdb_byte *bytes;
98bfdba5 20553 struct dwarf2_locexpr_baton *baton;
2df3850c 20554
98bfdba5 20555 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 20556 sym->print_name (),
98bfdba5
PA
20557 &objfile->objfile_obstack, cu,
20558 &value, &bytes, &baton);
2df3850c 20559
98bfdba5
PA
20560 if (baton != NULL)
20561 {
98bfdba5 20562 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20563 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20564 }
20565 else if (bytes != NULL)
20566 {
20567 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20568 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20569 }
20570 else
20571 {
20572 SYMBOL_VALUE (sym) = value;
f1e6e072 20573 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20574 }
2df3850c
JM
20575}
20576
c906108c
SS
20577/* Return the type of the die in question using its DW_AT_type attribute. */
20578
20579static struct type *
e7c27a73 20580die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20581{
c906108c 20582 struct attribute *type_attr;
c906108c 20583
e142c38c 20584 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20585 if (!type_attr)
20586 {
518817b3 20587 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20588 /* A missing DW_AT_type represents a void type. */
518817b3 20589 return objfile_type (objfile)->builtin_void;
c906108c 20590 }
348e048f 20591
673bfd45 20592 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20593}
20594
b4ba55a1
JB
20595/* True iff CU's producer generates GNAT Ada auxiliary information
20596 that allows to find parallel types through that information instead
20597 of having to do expensive parallel lookups by type name. */
20598
20599static int
20600need_gnat_info (struct dwarf2_cu *cu)
20601{
de4cb04a
JB
20602 /* Assume that the Ada compiler was GNAT, which always produces
20603 the auxiliary information. */
20604 return (cu->language == language_ada);
b4ba55a1
JB
20605}
20606
b4ba55a1
JB
20607/* Return the auxiliary type of the die in question using its
20608 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20609 attribute is not present. */
20610
20611static struct type *
20612die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20613{
b4ba55a1 20614 struct attribute *type_attr;
b4ba55a1
JB
20615
20616 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20617 if (!type_attr)
20618 return NULL;
20619
673bfd45 20620 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20621}
20622
20623/* If DIE has a descriptive_type attribute, then set the TYPE's
20624 descriptive type accordingly. */
20625
20626static void
20627set_descriptive_type (struct type *type, struct die_info *die,
20628 struct dwarf2_cu *cu)
20629{
20630 struct type *descriptive_type = die_descriptive_type (die, cu);
20631
20632 if (descriptive_type)
20633 {
20634 ALLOCATE_GNAT_AUX_TYPE (type);
20635 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20636 }
20637}
20638
c906108c
SS
20639/* Return the containing type of the die in question using its
20640 DW_AT_containing_type attribute. */
20641
20642static struct type *
e7c27a73 20643die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20644{
c906108c 20645 struct attribute *type_attr;
518817b3 20646 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20647
e142c38c 20648 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20649 if (!type_attr)
20650 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 20651 "[in module %s]"), objfile_name (objfile));
33ac96f0 20652
673bfd45 20653 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20654}
20655
ac9ec31b
DE
20656/* Return an error marker type to use for the ill formed type in DIE/CU. */
20657
20658static struct type *
20659build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20660{
518817b3
SM
20661 struct dwarf2_per_objfile *dwarf2_per_objfile
20662 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 20663 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 20664 char *saved;
ac9ec31b 20665
528e1572
SM
20666 std::string message
20667 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20668 objfile_name (objfile),
20669 sect_offset_str (cu->header.sect_off),
20670 sect_offset_str (die->sect_off));
efba19b0 20671 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 20672
19f392bc 20673 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20674}
20675
673bfd45 20676/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20677 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20678 DW_AT_containing_type.
673bfd45
DE
20679 If there is no type substitute an error marker. */
20680
c906108c 20681static struct type *
ff39bb5e 20682lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20683 struct dwarf2_cu *cu)
c906108c 20684{
518817b3
SM
20685 struct dwarf2_per_objfile *dwarf2_per_objfile
20686 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20687 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
20688 struct type *this_type;
20689
ac9ec31b
DE
20690 gdb_assert (attr->name == DW_AT_type
20691 || attr->name == DW_AT_GNAT_descriptive_type
20692 || attr->name == DW_AT_containing_type);
20693
673bfd45
DE
20694 /* First see if we have it cached. */
20695
36586728
TT
20696 if (attr->form == DW_FORM_GNU_ref_alt)
20697 {
20698 struct dwarf2_per_cu_data *per_cu;
0826b30a 20699 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 20700
ed2dc618
SM
20701 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20702 dwarf2_per_objfile);
9c541725 20703 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20704 }
cd6c91b4 20705 else if (attr->form_is_ref ())
673bfd45 20706 {
0826b30a 20707 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 20708
9c541725 20709 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20710 }
55f1336d 20711 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20712 {
ac9ec31b 20713 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20714
ac9ec31b 20715 return get_signatured_type (die, signature, cu);
673bfd45
DE
20716 }
20717 else
20718 {
b98664d3 20719 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
20720 " at %s [in module %s]"),
20721 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 20722 objfile_name (objfile));
ac9ec31b 20723 return build_error_marker_type (cu, die);
673bfd45
DE
20724 }
20725
20726 /* If not cached we need to read it in. */
20727
20728 if (this_type == NULL)
20729 {
ac9ec31b 20730 struct die_info *type_die = NULL;
673bfd45
DE
20731 struct dwarf2_cu *type_cu = cu;
20732
cd6c91b4 20733 if (attr->form_is_ref ())
ac9ec31b
DE
20734 type_die = follow_die_ref (die, attr, &type_cu);
20735 if (type_die == NULL)
20736 return build_error_marker_type (cu, die);
20737 /* If we find the type now, it's probably because the type came
3019eac3
DE
20738 from an inter-CU reference and the type's CU got expanded before
20739 ours. */
ac9ec31b 20740 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20741 }
20742
20743 /* If we still don't have a type use an error marker. */
20744
20745 if (this_type == NULL)
ac9ec31b 20746 return build_error_marker_type (cu, die);
673bfd45 20747
f792889a 20748 return this_type;
c906108c
SS
20749}
20750
673bfd45
DE
20751/* Return the type in DIE, CU.
20752 Returns NULL for invalid types.
20753
02142a6c 20754 This first does a lookup in die_type_hash,
673bfd45
DE
20755 and only reads the die in if necessary.
20756
20757 NOTE: This can be called when reading in partial or full symbols. */
20758
f792889a 20759static struct type *
e7c27a73 20760read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20761{
f792889a
DJ
20762 struct type *this_type;
20763
20764 this_type = get_die_type (die, cu);
20765 if (this_type)
20766 return this_type;
20767
673bfd45
DE
20768 return read_type_die_1 (die, cu);
20769}
20770
20771/* Read the type in DIE, CU.
20772 Returns NULL for invalid types. */
20773
20774static struct type *
20775read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20776{
20777 struct type *this_type = NULL;
20778
c906108c
SS
20779 switch (die->tag)
20780 {
20781 case DW_TAG_class_type:
680b30c7 20782 case DW_TAG_interface_type:
c906108c
SS
20783 case DW_TAG_structure_type:
20784 case DW_TAG_union_type:
f792889a 20785 this_type = read_structure_type (die, cu);
c906108c
SS
20786 break;
20787 case DW_TAG_enumeration_type:
f792889a 20788 this_type = read_enumeration_type (die, cu);
c906108c
SS
20789 break;
20790 case DW_TAG_subprogram:
20791 case DW_TAG_subroutine_type:
edb3359d 20792 case DW_TAG_inlined_subroutine:
f792889a 20793 this_type = read_subroutine_type (die, cu);
c906108c
SS
20794 break;
20795 case DW_TAG_array_type:
f792889a 20796 this_type = read_array_type (die, cu);
c906108c 20797 break;
72019c9c 20798 case DW_TAG_set_type:
f792889a 20799 this_type = read_set_type (die, cu);
72019c9c 20800 break;
c906108c 20801 case DW_TAG_pointer_type:
f792889a 20802 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
20803 break;
20804 case DW_TAG_ptr_to_member_type:
f792889a 20805 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
20806 break;
20807 case DW_TAG_reference_type:
4297a3f0
AV
20808 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20809 break;
20810 case DW_TAG_rvalue_reference_type:
20811 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
20812 break;
20813 case DW_TAG_const_type:
f792889a 20814 this_type = read_tag_const_type (die, cu);
c906108c
SS
20815 break;
20816 case DW_TAG_volatile_type:
f792889a 20817 this_type = read_tag_volatile_type (die, cu);
c906108c 20818 break;
06d66ee9
TT
20819 case DW_TAG_restrict_type:
20820 this_type = read_tag_restrict_type (die, cu);
20821 break;
c906108c 20822 case DW_TAG_string_type:
f792889a 20823 this_type = read_tag_string_type (die, cu);
c906108c
SS
20824 break;
20825 case DW_TAG_typedef:
f792889a 20826 this_type = read_typedef (die, cu);
c906108c 20827 break;
a02abb62 20828 case DW_TAG_subrange_type:
f792889a 20829 this_type = read_subrange_type (die, cu);
a02abb62 20830 break;
c906108c 20831 case DW_TAG_base_type:
f792889a 20832 this_type = read_base_type (die, cu);
c906108c 20833 break;
81a17f79 20834 case DW_TAG_unspecified_type:
f792889a 20835 this_type = read_unspecified_type (die, cu);
81a17f79 20836 break;
0114d602
DJ
20837 case DW_TAG_namespace:
20838 this_type = read_namespace_type (die, cu);
20839 break;
f55ee35c
JK
20840 case DW_TAG_module:
20841 this_type = read_module_type (die, cu);
20842 break;
a2c2acaf
MW
20843 case DW_TAG_atomic_type:
20844 this_type = read_tag_atomic_type (die, cu);
20845 break;
c906108c 20846 default:
b98664d3 20847 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 20848 dwarf_tag_name (die->tag));
c906108c
SS
20849 break;
20850 }
63d06c5c 20851
f792889a 20852 return this_type;
63d06c5c
DC
20853}
20854
abc72ce4
DE
20855/* See if we can figure out if the class lives in a namespace. We do
20856 this by looking for a member function; its demangled name will
20857 contain namespace info, if there is any.
20858 Return the computed name or NULL.
20859 Space for the result is allocated on the objfile's obstack.
20860 This is the full-die version of guess_partial_die_structure_name.
20861 In this case we know DIE has no useful parent. */
20862
43816ebc 20863static const char *
abc72ce4
DE
20864guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20865{
20866 struct die_info *spec_die;
20867 struct dwarf2_cu *spec_cu;
20868 struct die_info *child;
518817b3 20869 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
20870
20871 spec_cu = cu;
20872 spec_die = die_specification (die, &spec_cu);
20873 if (spec_die != NULL)
20874 {
20875 die = spec_die;
20876 cu = spec_cu;
20877 }
20878
20879 for (child = die->child;
20880 child != NULL;
20881 child = child->sibling)
20882 {
20883 if (child->tag == DW_TAG_subprogram)
20884 {
73b9be8b 20885 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 20886
7d45c7c3 20887 if (linkage_name != NULL)
abc72ce4 20888 {
43816ebc
TT
20889 gdb::unique_xmalloc_ptr<char> actual_name
20890 (language_class_name_from_physname (cu->language_defn,
20891 linkage_name));
20892 const char *name = NULL;
abc72ce4
DE
20893
20894 if (actual_name != NULL)
20895 {
15d034d0 20896 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
20897
20898 if (die_name != NULL
43816ebc 20899 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
20900 {
20901 /* Strip off the class name from the full name.
20902 We want the prefix. */
20903 int die_name_len = strlen (die_name);
43816ebc
TT
20904 int actual_name_len = strlen (actual_name.get ());
20905 const char *ptr = actual_name.get ();
abc72ce4
DE
20906
20907 /* Test for '::' as a sanity check. */
20908 if (actual_name_len > die_name_len + 2
43816ebc 20909 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 20910 name = obstack_strndup (
e3b94546 20911 &objfile->per_bfd->storage_obstack,
43816ebc 20912 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
20913 }
20914 }
abc72ce4
DE
20915 return name;
20916 }
20917 }
20918 }
20919
20920 return NULL;
20921}
20922
96408a79
SA
20923/* GCC might emit a nameless typedef that has a linkage name. Determine the
20924 prefix part in such case. See
20925 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20926
a121b7c1 20927static const char *
96408a79
SA
20928anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20929{
20930 struct attribute *attr;
e6a959d6 20931 const char *base;
96408a79
SA
20932
20933 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20934 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20935 return NULL;
20936
7d45c7c3 20937 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
20938 return NULL;
20939
73b9be8b 20940 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20941 if (attr == NULL || DW_STRING (attr) == NULL)
20942 return NULL;
20943
20944 /* dwarf2_name had to be already called. */
20945 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20946
20947 /* Strip the base name, keep any leading namespaces/classes. */
20948 base = strrchr (DW_STRING (attr), ':');
20949 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20950 return "";
20951
518817b3 20952 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
20953 return obstack_strndup (&objfile->per_bfd->storage_obstack,
20954 DW_STRING (attr),
20955 &base[-1] - DW_STRING (attr));
96408a79
SA
20956}
20957
fdde2d81 20958/* Return the name of the namespace/class that DIE is defined within,
0114d602 20959 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20960
0114d602
DJ
20961 For example, if we're within the method foo() in the following
20962 code:
20963
20964 namespace N {
20965 class C {
20966 void foo () {
20967 }
20968 };
20969 }
20970
20971 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20972
0d5cff50 20973static const char *
e142c38c 20974determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20975{
518817b3
SM
20976 struct dwarf2_per_objfile *dwarf2_per_objfile
20977 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
20978 struct die_info *parent, *spec_die;
20979 struct dwarf2_cu *spec_cu;
20980 struct type *parent_type;
a121b7c1 20981 const char *retval;
63d06c5c 20982
9c37b5ae 20983 if (cu->language != language_cplus
c44af4eb
TT
20984 && cu->language != language_fortran && cu->language != language_d
20985 && cu->language != language_rust)
0114d602
DJ
20986 return "";
20987
96408a79
SA
20988 retval = anonymous_struct_prefix (die, cu);
20989 if (retval)
20990 return retval;
20991
0114d602
DJ
20992 /* We have to be careful in the presence of DW_AT_specification.
20993 For example, with GCC 3.4, given the code
20994
20995 namespace N {
20996 void foo() {
20997 // Definition of N::foo.
20998 }
20999 }
21000
21001 then we'll have a tree of DIEs like this:
21002
21003 1: DW_TAG_compile_unit
21004 2: DW_TAG_namespace // N
21005 3: DW_TAG_subprogram // declaration of N::foo
21006 4: DW_TAG_subprogram // definition of N::foo
21007 DW_AT_specification // refers to die #3
21008
21009 Thus, when processing die #4, we have to pretend that we're in
21010 the context of its DW_AT_specification, namely the contex of die
21011 #3. */
21012 spec_cu = cu;
21013 spec_die = die_specification (die, &spec_cu);
21014 if (spec_die == NULL)
21015 parent = die->parent;
21016 else
63d06c5c 21017 {
0114d602
DJ
21018 parent = spec_die->parent;
21019 cu = spec_cu;
63d06c5c 21020 }
0114d602
DJ
21021
21022 if (parent == NULL)
21023 return "";
98bfdba5
PA
21024 else if (parent->building_fullname)
21025 {
21026 const char *name;
21027 const char *parent_name;
21028
21029 /* It has been seen on RealView 2.2 built binaries,
21030 DW_TAG_template_type_param types actually _defined_ as
21031 children of the parent class:
21032
21033 enum E {};
21034 template class <class Enum> Class{};
21035 Class<enum E> class_e;
21036
21037 1: DW_TAG_class_type (Class)
21038 2: DW_TAG_enumeration_type (E)
21039 3: DW_TAG_enumerator (enum1:0)
21040 3: DW_TAG_enumerator (enum2:1)
21041 ...
21042 2: DW_TAG_template_type_param
21043 DW_AT_type DW_FORM_ref_udata (E)
21044
21045 Besides being broken debug info, it can put GDB into an
21046 infinite loop. Consider:
21047
21048 When we're building the full name for Class<E>, we'll start
21049 at Class, and go look over its template type parameters,
21050 finding E. We'll then try to build the full name of E, and
21051 reach here. We're now trying to build the full name of E,
21052 and look over the parent DIE for containing scope. In the
21053 broken case, if we followed the parent DIE of E, we'd again
21054 find Class, and once again go look at its template type
21055 arguments, etc., etc. Simply don't consider such parent die
21056 as source-level parent of this die (it can't be, the language
21057 doesn't allow it), and break the loop here. */
21058 name = dwarf2_name (die, cu);
21059 parent_name = dwarf2_name (parent, cu);
b98664d3 21060 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21061 name ? name : "<unknown>",
21062 parent_name ? parent_name : "<unknown>");
21063 return "";
21064 }
63d06c5c 21065 else
0114d602
DJ
21066 switch (parent->tag)
21067 {
63d06c5c 21068 case DW_TAG_namespace:
0114d602 21069 parent_type = read_type_die (parent, cu);
acebe513
UW
21070 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21071 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21072 Work around this problem here. */
21073 if (cu->language == language_cplus
e86ca25f 21074 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21075 return "";
0114d602 21076 /* We give a name to even anonymous namespaces. */
e86ca25f 21077 return TYPE_NAME (parent_type);
63d06c5c 21078 case DW_TAG_class_type:
680b30c7 21079 case DW_TAG_interface_type:
63d06c5c 21080 case DW_TAG_structure_type:
0114d602 21081 case DW_TAG_union_type:
f55ee35c 21082 case DW_TAG_module:
0114d602 21083 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21084 if (TYPE_NAME (parent_type) != NULL)
21085 return TYPE_NAME (parent_type);
0114d602
DJ
21086 else
21087 /* An anonymous structure is only allowed non-static data
21088 members; no typedefs, no member functions, et cetera.
21089 So it does not need a prefix. */
21090 return "";
abc72ce4 21091 case DW_TAG_compile_unit:
95554aad 21092 case DW_TAG_partial_unit:
abc72ce4
DE
21093 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21094 if (cu->language == language_cplus
fd5866f6 21095 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21096 && die->child != NULL
21097 && (die->tag == DW_TAG_class_type
21098 || die->tag == DW_TAG_structure_type
21099 || die->tag == DW_TAG_union_type))
21100 {
43816ebc 21101 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21102 if (name != NULL)
21103 return name;
21104 }
21105 return "";
0a4b0913
AB
21106 case DW_TAG_subprogram:
21107 /* Nested subroutines in Fortran get a prefix with the name
21108 of the parent's subroutine. */
21109 if (cu->language == language_fortran)
21110 {
21111 if ((die->tag == DW_TAG_subprogram)
21112 && (dwarf2_name (parent, cu) != NULL))
21113 return dwarf2_name (parent, cu);
21114 }
21115 return determine_prefix (parent, cu);
3d567982
TT
21116 case DW_TAG_enumeration_type:
21117 parent_type = read_type_die (parent, cu);
21118 if (TYPE_DECLARED_CLASS (parent_type))
21119 {
e86ca25f
TT
21120 if (TYPE_NAME (parent_type) != NULL)
21121 return TYPE_NAME (parent_type);
3d567982
TT
21122 return "";
21123 }
21124 /* Fall through. */
63d06c5c 21125 default:
8176b9b8 21126 return determine_prefix (parent, cu);
63d06c5c 21127 }
63d06c5c
DC
21128}
21129
3e43a32a
MS
21130/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21131 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21132 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21133 an obconcat, otherwise allocate storage for the result. The CU argument is
21134 used to determine the language and hence, the appropriate separator. */
987504bb 21135
f55ee35c 21136#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21137
21138static char *
f55ee35c
JK
21139typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21140 int physname, struct dwarf2_cu *cu)
63d06c5c 21141{
f55ee35c 21142 const char *lead = "";
5c315b68 21143 const char *sep;
63d06c5c 21144
3e43a32a
MS
21145 if (suffix == NULL || suffix[0] == '\0'
21146 || prefix == NULL || prefix[0] == '\0')
987504bb 21147 sep = "";
45280282
IB
21148 else if (cu->language == language_d)
21149 {
21150 /* For D, the 'main' function could be defined in any module, but it
21151 should never be prefixed. */
21152 if (strcmp (suffix, "D main") == 0)
21153 {
21154 prefix = "";
21155 sep = "";
21156 }
21157 else
21158 sep = ".";
21159 }
f55ee35c
JK
21160 else if (cu->language == language_fortran && physname)
21161 {
21162 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21163 DW_AT_MIPS_linkage_name is preferred and used instead. */
21164
21165 lead = "__";
21166 sep = "_MOD_";
21167 }
987504bb
JJ
21168 else
21169 sep = "::";
63d06c5c 21170
6dd47d34
DE
21171 if (prefix == NULL)
21172 prefix = "";
21173 if (suffix == NULL)
21174 suffix = "";
21175
987504bb
JJ
21176 if (obs == NULL)
21177 {
3e43a32a 21178 char *retval
224c3ddb
SM
21179 = ((char *)
21180 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21181
f55ee35c
JK
21182 strcpy (retval, lead);
21183 strcat (retval, prefix);
6dd47d34
DE
21184 strcat (retval, sep);
21185 strcat (retval, suffix);
63d06c5c
DC
21186 return retval;
21187 }
987504bb
JJ
21188 else
21189 {
21190 /* We have an obstack. */
f55ee35c 21191 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21192 }
63d06c5c
DC
21193}
21194
71c25dea
TT
21195/* Get name of a die, return NULL if not found. */
21196
15d034d0
TT
21197static const char *
21198dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21199 struct objfile *objfile)
71c25dea
TT
21200{
21201 if (name && cu->language == language_cplus)
21202 {
2f408ecb 21203 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21204
2f408ecb 21205 if (!canon_name.empty ())
71c25dea 21206 {
2f408ecb 21207 if (canon_name != name)
be1e3d3e 21208 name = objfile->intern (canon_name);
71c25dea
TT
21209 }
21210 }
21211
21212 return name;
c906108c
SS
21213}
21214
96553a0c
DE
21215/* Get name of a die, return NULL if not found.
21216 Anonymous namespaces are converted to their magic string. */
9219021c 21217
15d034d0 21218static const char *
e142c38c 21219dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21220{
21221 struct attribute *attr;
518817b3 21222 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21223
e142c38c 21224 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21225 if ((!attr || !DW_STRING (attr))
96553a0c 21226 && die->tag != DW_TAG_namespace
53832f31
TT
21227 && die->tag != DW_TAG_class_type
21228 && die->tag != DW_TAG_interface_type
21229 && die->tag != DW_TAG_structure_type
21230 && die->tag != DW_TAG_union_type)
71c25dea
TT
21231 return NULL;
21232
21233 switch (die->tag)
21234 {
21235 case DW_TAG_compile_unit:
95554aad 21236 case DW_TAG_partial_unit:
71c25dea
TT
21237 /* Compilation units have a DW_AT_name that is a filename, not
21238 a source language identifier. */
21239 case DW_TAG_enumeration_type:
21240 case DW_TAG_enumerator:
21241 /* These tags always have simple identifiers already; no need
21242 to canonicalize them. */
21243 return DW_STRING (attr);
907af001 21244
96553a0c
DE
21245 case DW_TAG_namespace:
21246 if (attr != NULL && DW_STRING (attr) != NULL)
21247 return DW_STRING (attr);
21248 return CP_ANONYMOUS_NAMESPACE_STR;
21249
907af001
UW
21250 case DW_TAG_class_type:
21251 case DW_TAG_interface_type:
21252 case DW_TAG_structure_type:
21253 case DW_TAG_union_type:
21254 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21255 structures or unions. These were of the form "._%d" in GCC 4.1,
21256 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21257 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21258 if (attr && DW_STRING (attr)
61012eef
GB
21259 && (startswith (DW_STRING (attr), "._")
21260 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21261 return NULL;
53832f31
TT
21262
21263 /* GCC might emit a nameless typedef that has a linkage name. See
21264 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21265 if (!attr || DW_STRING (attr) == NULL)
21266 {
73b9be8b 21267 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21268 if (attr == NULL || DW_STRING (attr) == NULL)
21269 return NULL;
21270
df5c6c50
JK
21271 /* Avoid demangling DW_STRING (attr) the second time on a second
21272 call for the same DIE. */
21273 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21274 {
43816ebc
TT
21275 gdb::unique_xmalloc_ptr<char> demangled
21276 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21277 if (demangled == nullptr)
21278 return nullptr;
43816ebc 21279
be1e3d3e 21280 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21281 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21282 }
67430cd0
TT
21283
21284 /* Strip any leading namespaces/classes, keep only the base name.
21285 DW_AT_name for named DIEs does not contain the prefixes. */
21286 const char *base = strrchr (DW_STRING (attr), ':');
21287 if (base && base > DW_STRING (attr) && base[-1] == ':')
21288 return &base[1];
21289 else
21290 return DW_STRING (attr);
53832f31 21291 }
907af001
UW
21292 break;
21293
71c25dea 21294 default:
907af001
UW
21295 break;
21296 }
21297
21298 if (!DW_STRING_IS_CANONICAL (attr))
21299 {
be1e3d3e
TT
21300 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21301 objfile);
907af001 21302 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21303 }
907af001 21304 return DW_STRING (attr);
9219021c
DC
21305}
21306
21307/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21308 is none. *EXT_CU is the CU containing DIE on input, and the CU
21309 containing the return value on output. */
9219021c
DC
21310
21311static struct die_info *
f2f0e013 21312dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21313{
21314 struct attribute *attr;
9219021c 21315
f2f0e013 21316 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21317 if (attr == NULL)
21318 return NULL;
21319
f2f0e013 21320 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21321}
21322
f9aca02d 21323static void
d97bc12b 21324dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21325{
21326 unsigned int i;
21327
d97bc12b 21328 print_spaces (indent, f);
9d8780f0 21329 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21330 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21331 sect_offset_str (die->sect_off));
d97bc12b
DE
21332
21333 if (die->parent != NULL)
21334 {
21335 print_spaces (indent, f);
9d8780f0
SM
21336 fprintf_unfiltered (f, " parent at offset: %s\n",
21337 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21338 }
21339
21340 print_spaces (indent, f);
21341 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21342 dwarf_bool_name (die->child != NULL));
c906108c 21343
d97bc12b
DE
21344 print_spaces (indent, f);
21345 fprintf_unfiltered (f, " attributes:\n");
21346
c906108c
SS
21347 for (i = 0; i < die->num_attrs; ++i)
21348 {
d97bc12b
DE
21349 print_spaces (indent, f);
21350 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21351 dwarf_attr_name (die->attrs[i].name),
21352 dwarf_form_name (die->attrs[i].form));
d97bc12b 21353
c906108c
SS
21354 switch (die->attrs[i].form)
21355 {
c906108c 21356 case DW_FORM_addr:
336d760d 21357 case DW_FORM_addrx:
3019eac3 21358 case DW_FORM_GNU_addr_index:
d97bc12b 21359 fprintf_unfiltered (f, "address: ");
5af949e3 21360 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21361 break;
21362 case DW_FORM_block2:
21363 case DW_FORM_block4:
21364 case DW_FORM_block:
21365 case DW_FORM_block1:
56eb65bd
SP
21366 fprintf_unfiltered (f, "block: size %s",
21367 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21368 break;
2dc7f7b3 21369 case DW_FORM_exprloc:
56eb65bd
SP
21370 fprintf_unfiltered (f, "expression: size %s",
21371 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21372 break;
0224619f
JK
21373 case DW_FORM_data16:
21374 fprintf_unfiltered (f, "constant of 16 bytes");
21375 break;
4568ecf9
DE
21376 case DW_FORM_ref_addr:
21377 fprintf_unfiltered (f, "ref address: ");
21378 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21379 break;
36586728
TT
21380 case DW_FORM_GNU_ref_alt:
21381 fprintf_unfiltered (f, "alt ref address: ");
21382 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21383 break;
10b3939b
DJ
21384 case DW_FORM_ref1:
21385 case DW_FORM_ref2:
21386 case DW_FORM_ref4:
4568ecf9
DE
21387 case DW_FORM_ref8:
21388 case DW_FORM_ref_udata:
d97bc12b 21389 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21390 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21391 break;
c906108c
SS
21392 case DW_FORM_data1:
21393 case DW_FORM_data2:
21394 case DW_FORM_data4:
ce5d95e1 21395 case DW_FORM_data8:
c906108c
SS
21396 case DW_FORM_udata:
21397 case DW_FORM_sdata:
43bbcdc2
PH
21398 fprintf_unfiltered (f, "constant: %s",
21399 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21400 break;
2dc7f7b3
TT
21401 case DW_FORM_sec_offset:
21402 fprintf_unfiltered (f, "section offset: %s",
21403 pulongest (DW_UNSND (&die->attrs[i])));
21404 break;
55f1336d 21405 case DW_FORM_ref_sig8:
ac9ec31b
DE
21406 fprintf_unfiltered (f, "signature: %s",
21407 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21408 break;
c906108c 21409 case DW_FORM_string:
4bdf3d34 21410 case DW_FORM_strp:
43988095 21411 case DW_FORM_line_strp:
cf532bd1 21412 case DW_FORM_strx:
3019eac3 21413 case DW_FORM_GNU_str_index:
36586728 21414 case DW_FORM_GNU_strp_alt:
8285870a 21415 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21416 DW_STRING (&die->attrs[i])
8285870a
JK
21417 ? DW_STRING (&die->attrs[i]) : "",
21418 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21419 break;
21420 case DW_FORM_flag:
21421 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21422 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21423 else
d97bc12b 21424 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21425 break;
2dc7f7b3
TT
21426 case DW_FORM_flag_present:
21427 fprintf_unfiltered (f, "flag: TRUE");
21428 break;
a8329558 21429 case DW_FORM_indirect:
0963b4bd
MS
21430 /* The reader will have reduced the indirect form to
21431 the "base form" so this form should not occur. */
5f48f8f3 21432 fprintf_unfiltered (f,
3e43a32a 21433 "unexpected attribute form: DW_FORM_indirect");
a8329558 21434 break;
663c44ac
JK
21435 case DW_FORM_implicit_const:
21436 fprintf_unfiltered (f, "constant: %s",
21437 plongest (DW_SND (&die->attrs[i])));
21438 break;
c906108c 21439 default:
d97bc12b 21440 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21441 die->attrs[i].form);
d97bc12b 21442 break;
c906108c 21443 }
d97bc12b 21444 fprintf_unfiltered (f, "\n");
c906108c
SS
21445 }
21446}
21447
f9aca02d 21448static void
d97bc12b 21449dump_die_for_error (struct die_info *die)
c906108c 21450{
d97bc12b
DE
21451 dump_die_shallow (gdb_stderr, 0, die);
21452}
21453
21454static void
21455dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21456{
21457 int indent = level * 4;
21458
21459 gdb_assert (die != NULL);
21460
21461 if (level >= max_level)
21462 return;
21463
21464 dump_die_shallow (f, indent, die);
21465
21466 if (die->child != NULL)
c906108c 21467 {
d97bc12b
DE
21468 print_spaces (indent, f);
21469 fprintf_unfiltered (f, " Children:");
21470 if (level + 1 < max_level)
21471 {
21472 fprintf_unfiltered (f, "\n");
21473 dump_die_1 (f, level + 1, max_level, die->child);
21474 }
21475 else
21476 {
3e43a32a
MS
21477 fprintf_unfiltered (f,
21478 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21479 }
21480 }
21481
21482 if (die->sibling != NULL && level > 0)
21483 {
21484 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21485 }
21486}
21487
d97bc12b
DE
21488/* This is called from the pdie macro in gdbinit.in.
21489 It's not static so gcc will keep a copy callable from gdb. */
21490
21491void
21492dump_die (struct die_info *die, int max_level)
21493{
21494 dump_die_1 (gdb_stdlog, 0, max_level, die);
21495}
21496
f9aca02d 21497static void
51545339 21498store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21499{
51545339 21500 void **slot;
c906108c 21501
9c541725
PA
21502 slot = htab_find_slot_with_hash (cu->die_hash, die,
21503 to_underlying (die->sect_off),
b64f50a1 21504 INSERT);
51545339
DJ
21505
21506 *slot = die;
c906108c
SS
21507}
21508
348e048f
DE
21509/* Follow reference or signature attribute ATTR of SRC_DIE.
21510 On entry *REF_CU is the CU of SRC_DIE.
21511 On exit *REF_CU is the CU of the result. */
21512
21513static struct die_info *
ff39bb5e 21514follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21515 struct dwarf2_cu **ref_cu)
21516{
21517 struct die_info *die;
21518
cd6c91b4 21519 if (attr->form_is_ref ())
348e048f 21520 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21521 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21522 die = follow_die_sig (src_die, attr, ref_cu);
21523 else
21524 {
21525 dump_die_for_error (src_die);
21526 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 21527 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
21528 }
21529
21530 return die;
03dd20cc
DJ
21531}
21532
5c631832 21533/* Follow reference OFFSET.
673bfd45
DE
21534 On entry *REF_CU is the CU of the source die referencing OFFSET.
21535 On exit *REF_CU is the CU of the result.
21536 Returns NULL if OFFSET is invalid. */
f504f079 21537
f9aca02d 21538static struct die_info *
9c541725 21539follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21540 struct dwarf2_cu **ref_cu)
c906108c 21541{
10b3939b 21542 struct die_info temp_die;
f2f0e013 21543 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
21544 struct dwarf2_per_objfile *dwarf2_per_objfile
21545 = cu->per_cu->dwarf2_per_objfile;
10b3939b 21546
348e048f
DE
21547 gdb_assert (cu->per_cu != NULL);
21548
98bfdba5
PA
21549 target_cu = cu;
21550
3019eac3 21551 if (cu->per_cu->is_debug_types)
348e048f
DE
21552 {
21553 /* .debug_types CUs cannot reference anything outside their CU.
21554 If they need to, they have to reference a signatured type via
55f1336d 21555 DW_FORM_ref_sig8. */
4057dfde 21556 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 21557 return NULL;
348e048f 21558 }
36586728 21559 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 21560 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
21561 {
21562 struct dwarf2_per_cu_data *per_cu;
9a619af0 21563
9c541725 21564 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 21565 dwarf2_per_objfile);
03dd20cc
DJ
21566
21567 /* If necessary, add it to the queue and load its DIEs. */
95554aad 21568 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 21569 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 21570
10b3939b
DJ
21571 target_cu = per_cu->cu;
21572 }
98bfdba5
PA
21573 else if (cu->dies == NULL)
21574 {
21575 /* We're loading full DIEs during partial symbol reading. */
21576 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 21577 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 21578 }
c906108c 21579
f2f0e013 21580 *ref_cu = target_cu;
9c541725 21581 temp_die.sect_off = sect_off;
c24bdb02
KS
21582
21583 if (target_cu != cu)
21584 target_cu->ancestor = cu;
21585
9a3c8263 21586 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21587 &temp_die,
21588 to_underlying (sect_off));
5c631832 21589}
10b3939b 21590
5c631832
JK
21591/* Follow reference attribute ATTR of SRC_DIE.
21592 On entry *REF_CU is the CU of SRC_DIE.
21593 On exit *REF_CU is the CU of the result. */
21594
21595static struct die_info *
ff39bb5e 21596follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21597 struct dwarf2_cu **ref_cu)
21598{
0826b30a 21599 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
21600 struct dwarf2_cu *cu = *ref_cu;
21601 struct die_info *die;
21602
9c541725 21603 die = follow_die_offset (sect_off,
36586728
TT
21604 (attr->form == DW_FORM_GNU_ref_alt
21605 || cu->per_cu->is_dwz),
21606 ref_cu);
5c631832 21607 if (!die)
9d8780f0
SM
21608 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21609 "at %s [in module %s]"),
21610 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 21611 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 21612
5c631832
JK
21613 return die;
21614}
21615
d4c9a4f8 21616/* See read.h. */
5c631832
JK
21617
21618struct dwarf2_locexpr_baton
9c541725 21619dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 21620 dwarf2_per_cu_data *per_cu,
8b9737bf 21621 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 21622 void *baton, bool resolve_abstract_p)
5c631832 21623{
918dd910 21624 struct dwarf2_cu *cu;
5c631832
JK
21625 struct die_info *die;
21626 struct attribute *attr;
21627 struct dwarf2_locexpr_baton retval;
12359b5e
SM
21628 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21629 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 21630
918dd910 21631 if (per_cu->cu == NULL)
58f0c718 21632 load_cu (per_cu, false);
918dd910 21633 cu = per_cu->cu;
cc12ce38
DE
21634 if (cu == NULL)
21635 {
21636 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21637 Instead just throw an error, not much else we can do. */
9d8780f0
SM
21638 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21639 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 21640 }
918dd910 21641
9c541725 21642 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 21643 if (!die)
9d8780f0
SM
21644 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21645 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21646
21647 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 21648 if (!attr && resolve_abstract_p
3360b6e7 21649 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
21650 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21651 {
21652 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 21653 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 21654 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 21655
3360b6e7
TV
21656 for (const auto &cand_off
21657 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 21658 {
3360b6e7
TV
21659 struct dwarf2_cu *cand_cu = cu;
21660 struct die_info *cand
21661 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21662 if (!cand
21663 || !cand->parent
e4a62c65
TV
21664 || cand->parent->tag != DW_TAG_subprogram)
21665 continue;
21666
21667 CORE_ADDR pc_low, pc_high;
21668 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
21669 if (pc_low == ((CORE_ADDR) -1))
21670 continue;
21671 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21672 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21673 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
21674 continue;
21675
21676 die = cand;
21677 attr = dwarf2_attr (die, DW_AT_location, cu);
21678 break;
21679 }
21680 }
21681
5c631832
JK
21682 if (!attr)
21683 {
e103e986
JK
21684 /* DWARF: "If there is no such attribute, then there is no effect.".
21685 DATA is ignored if SIZE is 0. */
5c631832 21686
e103e986 21687 retval.data = NULL;
5c631832
JK
21688 retval.size = 0;
21689 }
cd6c91b4 21690 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
21691 {
21692 struct dwarf2_loclist_baton loclist_baton;
21693 CORE_ADDR pc = (*get_frame_pc) (baton);
21694 size_t size;
21695
21696 fill_in_loclist_baton (cu, &loclist_baton, attr);
21697
21698 retval.data = dwarf2_find_location_expression (&loclist_baton,
21699 &size, pc);
21700 retval.size = size;
21701 }
5c631832
JK
21702 else
21703 {
4fc6c0d5 21704 if (!attr->form_is_block ())
9d8780f0 21705 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 21706 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 21707 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21708
21709 retval.data = DW_BLOCK (attr)->data;
21710 retval.size = DW_BLOCK (attr)->size;
21711 }
21712 retval.per_cu = cu->per_cu;
918dd910 21713
ed2dc618 21714 age_cached_comp_units (dwarf2_per_objfile);
918dd910 21715
5c631832 21716 return retval;
348e048f
DE
21717}
21718
d4c9a4f8 21719/* See read.h. */
8b9737bf
TT
21720
21721struct dwarf2_locexpr_baton
21722dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 21723 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
21724 CORE_ADDR (*get_frame_pc) (void *baton),
21725 void *baton)
21726{
9c541725 21727 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21728
9c541725 21729 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21730}
21731
b6807d98
TT
21732/* Write a constant of a given type as target-ordered bytes into
21733 OBSTACK. */
21734
21735static const gdb_byte *
21736write_constant_as_bytes (struct obstack *obstack,
21737 enum bfd_endian byte_order,
21738 struct type *type,
21739 ULONGEST value,
21740 LONGEST *len)
21741{
21742 gdb_byte *result;
21743
21744 *len = TYPE_LENGTH (type);
224c3ddb 21745 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21746 store_unsigned_integer (result, *len, byte_order, value);
21747
21748 return result;
21749}
21750
d4c9a4f8 21751/* See read.h. */
b6807d98
TT
21752
21753const gdb_byte *
9c541725 21754dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
21755 dwarf2_per_cu_data *per_cu,
21756 obstack *obstack,
b6807d98
TT
21757 LONGEST *len)
21758{
21759 struct dwarf2_cu *cu;
21760 struct die_info *die;
21761 struct attribute *attr;
21762 const gdb_byte *result = NULL;
21763 struct type *type;
21764 LONGEST value;
21765 enum bfd_endian byte_order;
e3b94546 21766 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 21767
b6807d98 21768 if (per_cu->cu == NULL)
58f0c718 21769 load_cu (per_cu, false);
b6807d98 21770 cu = per_cu->cu;
cc12ce38
DE
21771 if (cu == NULL)
21772 {
21773 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21774 Instead just throw an error, not much else we can do. */
9d8780f0
SM
21775 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21776 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 21777 }
b6807d98 21778
9c541725 21779 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 21780 if (!die)
9d8780f0
SM
21781 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21782 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
21783
21784 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21785 if (attr == NULL)
21786 return NULL;
21787
e3b94546 21788 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
21789 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21790
21791 switch (attr->form)
21792 {
21793 case DW_FORM_addr:
336d760d 21794 case DW_FORM_addrx:
b6807d98
TT
21795 case DW_FORM_GNU_addr_index:
21796 {
21797 gdb_byte *tem;
21798
21799 *len = cu->header.addr_size;
224c3ddb 21800 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21801 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21802 result = tem;
21803 }
21804 break;
21805 case DW_FORM_string:
21806 case DW_FORM_strp:
cf532bd1 21807 case DW_FORM_strx:
b6807d98
TT
21808 case DW_FORM_GNU_str_index:
21809 case DW_FORM_GNU_strp_alt:
21810 /* DW_STRING is already allocated on the objfile obstack, point
21811 directly to it. */
21812 result = (const gdb_byte *) DW_STRING (attr);
21813 *len = strlen (DW_STRING (attr));
21814 break;
21815 case DW_FORM_block1:
21816 case DW_FORM_block2:
21817 case DW_FORM_block4:
21818 case DW_FORM_block:
21819 case DW_FORM_exprloc:
0224619f 21820 case DW_FORM_data16:
b6807d98
TT
21821 result = DW_BLOCK (attr)->data;
21822 *len = DW_BLOCK (attr)->size;
21823 break;
21824
21825 /* The DW_AT_const_value attributes are supposed to carry the
21826 symbol's value "represented as it would be on the target
21827 architecture." By the time we get here, it's already been
21828 converted to host endianness, so we just need to sign- or
21829 zero-extend it as appropriate. */
21830 case DW_FORM_data1:
21831 type = die_type (die, cu);
21832 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21833 if (result == NULL)
21834 result = write_constant_as_bytes (obstack, byte_order,
21835 type, value, len);
21836 break;
21837 case DW_FORM_data2:
21838 type = die_type (die, cu);
21839 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21840 if (result == NULL)
21841 result = write_constant_as_bytes (obstack, byte_order,
21842 type, value, len);
21843 break;
21844 case DW_FORM_data4:
21845 type = die_type (die, cu);
21846 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21847 if (result == NULL)
21848 result = write_constant_as_bytes (obstack, byte_order,
21849 type, value, len);
21850 break;
21851 case DW_FORM_data8:
21852 type = die_type (die, cu);
21853 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21854 if (result == NULL)
21855 result = write_constant_as_bytes (obstack, byte_order,
21856 type, value, len);
21857 break;
21858
21859 case DW_FORM_sdata:
663c44ac 21860 case DW_FORM_implicit_const:
b6807d98
TT
21861 type = die_type (die, cu);
21862 result = write_constant_as_bytes (obstack, byte_order,
21863 type, DW_SND (attr), len);
21864 break;
21865
21866 case DW_FORM_udata:
21867 type = die_type (die, cu);
21868 result = write_constant_as_bytes (obstack, byte_order,
21869 type, DW_UNSND (attr), len);
21870 break;
21871
21872 default:
b98664d3 21873 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
21874 dwarf_form_name (attr->form));
21875 break;
21876 }
21877
21878 return result;
21879}
21880
d4c9a4f8 21881/* See read.h. */
7942e96e
AA
21882
21883struct type *
9c541725 21884dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 21885 dwarf2_per_cu_data *per_cu)
7942e96e
AA
21886{
21887 struct dwarf2_cu *cu;
21888 struct die_info *die;
21889
7942e96e 21890 if (per_cu->cu == NULL)
58f0c718 21891 load_cu (per_cu, false);
7942e96e
AA
21892 cu = per_cu->cu;
21893 if (!cu)
21894 return NULL;
21895
9c541725 21896 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21897 if (!die)
21898 return NULL;
21899
21900 return die_type (die, cu);
21901}
21902
8cb5117c 21903/* See read.h. */
8a9b8146
TT
21904
21905struct type *
b64f50a1 21906dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21907 struct dwarf2_per_cu_data *per_cu)
21908{
9c541725 21909 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21910 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21911}
21912
ac9ec31b 21913/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21914 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21915 On exit *REF_CU is the CU of the result.
21916 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21917
21918static struct die_info *
ac9ec31b
DE
21919follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21920 struct dwarf2_cu **ref_cu)
348e048f 21921{
348e048f 21922 struct die_info temp_die;
c24bdb02 21923 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
21924 struct die_info *die;
21925
ac9ec31b
DE
21926 /* While it might be nice to assert sig_type->type == NULL here,
21927 we can get here for DW_AT_imported_declaration where we need
21928 the DIE not the type. */
348e048f
DE
21929
21930 /* If necessary, add it to the queue and load its DIEs. */
21931
95554aad 21932 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21933 read_signatured_type (sig_type);
348e048f 21934
348e048f 21935 sig_cu = sig_type->per_cu.cu;
69d751e3 21936 gdb_assert (sig_cu != NULL);
9c541725
PA
21937 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21938 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21939 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21940 to_underlying (temp_die.sect_off));
348e048f
DE
21941 if (die)
21942 {
ed2dc618 21943 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 21944 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 21945
796a7ff8
DE
21946 /* For .gdb_index version 7 keep track of included TUs.
21947 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21948 if (dwarf2_per_objfile->index_table != NULL
21949 && dwarf2_per_objfile->index_table->version <= 7)
21950 {
ae640021 21951 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
21952 }
21953
348e048f 21954 *ref_cu = sig_cu;
c24bdb02
KS
21955 if (sig_cu != cu)
21956 sig_cu->ancestor = cu;
21957
348e048f
DE
21958 return die;
21959 }
21960
ac9ec31b
DE
21961 return NULL;
21962}
21963
21964/* Follow signatured type referenced by ATTR in SRC_DIE.
21965 On entry *REF_CU is the CU of SRC_DIE.
21966 On exit *REF_CU is the CU of the result.
21967 The result is the DIE of the type.
21968 If the referenced type cannot be found an error is thrown. */
21969
21970static struct die_info *
ff39bb5e 21971follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21972 struct dwarf2_cu **ref_cu)
21973{
21974 ULONGEST signature = DW_SIGNATURE (attr);
21975 struct signatured_type *sig_type;
21976 struct die_info *die;
21977
21978 gdb_assert (attr->form == DW_FORM_ref_sig8);
21979
a2ce51a0 21980 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21981 /* sig_type will be NULL if the signatured type is missing from
21982 the debug info. */
21983 if (sig_type == NULL)
21984 {
21985 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
21986 " from DIE at %s [in module %s]"),
21987 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 21988 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
21989 }
21990
21991 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21992 if (die == NULL)
21993 {
21994 dump_die_for_error (src_die);
21995 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
21996 " from DIE at %s [in module %s]"),
21997 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 21998 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
21999 }
22000
22001 return die;
22002}
22003
22004/* Get the type specified by SIGNATURE referenced in DIE/CU,
22005 reading in and processing the type unit if necessary. */
22006
22007static struct type *
22008get_signatured_type (struct die_info *die, ULONGEST signature,
22009 struct dwarf2_cu *cu)
22010{
518817b3
SM
22011 struct dwarf2_per_objfile *dwarf2_per_objfile
22012 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22013 struct signatured_type *sig_type;
22014 struct dwarf2_cu *type_cu;
22015 struct die_info *type_die;
22016 struct type *type;
22017
a2ce51a0 22018 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22019 /* sig_type will be NULL if the signatured type is missing from
22020 the debug info. */
22021 if (sig_type == NULL)
22022 {
b98664d3 22023 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22024 " from DIE at %s [in module %s]"),
22025 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22026 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22027 return build_error_marker_type (cu, die);
22028 }
22029
22030 /* If we already know the type we're done. */
22031 if (sig_type->type != NULL)
22032 return sig_type->type;
22033
22034 type_cu = cu;
22035 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22036 if (type_die != NULL)
22037 {
22038 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22039 is created. This is important, for example, because for c++ classes
22040 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22041 type = read_type_die (type_die, type_cu);
22042 if (type == NULL)
22043 {
b98664d3 22044 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22045 " referenced from DIE at %s [in module %s]"),
22046 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22047 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22048 type = build_error_marker_type (cu, die);
22049 }
22050 }
22051 else
22052 {
b98664d3 22053 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22054 " from DIE at %s [in module %s]"),
22055 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22056 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22057 type = build_error_marker_type (cu, die);
22058 }
22059 sig_type->type = type;
22060
22061 return type;
22062}
22063
22064/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22065 reading in and processing the type unit if necessary. */
22066
22067static struct type *
ff39bb5e 22068get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22069 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22070{
22071 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22072 if (attr->form_is_ref ())
ac9ec31b
DE
22073 {
22074 struct dwarf2_cu *type_cu = cu;
22075 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22076
22077 return read_type_die (type_die, type_cu);
22078 }
22079 else if (attr->form == DW_FORM_ref_sig8)
22080 {
22081 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22082 }
22083 else
22084 {
518817b3
SM
22085 struct dwarf2_per_objfile *dwarf2_per_objfile
22086 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22087
b98664d3 22088 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22089 " at %s [in module %s]"),
22090 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22091 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22092 return build_error_marker_type (cu, die);
22093 }
348e048f
DE
22094}
22095
e5fe5e75 22096/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22097
22098static void
e5fe5e75 22099load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22100{
52dc124a 22101 struct signatured_type *sig_type;
348e048f 22102
f4dc4d17 22103 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22104 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22105
6721b2ec
DE
22106 /* We have the per_cu, but we need the signatured_type.
22107 Fortunately this is an easy translation. */
22108 gdb_assert (per_cu->is_debug_types);
22109 sig_type = (struct signatured_type *) per_cu;
348e048f 22110
6721b2ec 22111 gdb_assert (per_cu->cu == NULL);
348e048f 22112
52dc124a 22113 read_signatured_type (sig_type);
348e048f 22114
6721b2ec 22115 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22116}
22117
3019eac3
DE
22118/* Read in a signatured type and build its CU and DIEs.
22119 If the type is a stub for the real type in a DWO file,
22120 read in the real type from the DWO file as well. */
dee91e82
DE
22121
22122static void
22123read_signatured_type (struct signatured_type *sig_type)
22124{
22125 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22126
3019eac3 22127 gdb_assert (per_cu->is_debug_types);
dee91e82 22128 gdb_assert (per_cu->cu == NULL);
348e048f 22129
6751ebae 22130 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22131
22132 if (!reader.dummy_p)
22133 {
22134 struct dwarf2_cu *cu = reader.cu;
22135 const gdb_byte *info_ptr = reader.info_ptr;
22136
22137 gdb_assert (cu->die_hash == NULL);
22138 cu->die_hash =
22139 htab_create_alloc_ex (cu->header.length / 12,
22140 die_hash,
22141 die_eq,
22142 NULL,
22143 &cu->comp_unit_obstack,
22144 hashtab_obstack_allocate,
22145 dummy_obstack_deallocate);
22146
3e225074 22147 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22148 reader.comp_unit_die->child
22149 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22150 reader.comp_unit_die);
22151 cu->dies = reader.comp_unit_die;
22152 /* comp_unit_die is not stored in die_hash, no need. */
22153
22154 /* We try not to read any attributes in this function, because
22155 not all CUs needed for references have been loaded yet, and
22156 symbol table processing isn't initialized. But we have to
22157 set the CU language, or we won't be able to build types
22158 correctly. Similarly, if we do not read the producer, we can
22159 not apply producer-specific interpretation. */
22160 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22161
22162 reader.keep ();
c0ab21c2
TT
22163 }
22164
7ee85ab1 22165 sig_type->per_cu.tu_read = 1;
c906108c
SS
22166}
22167
c906108c
SS
22168/* Decode simple location descriptions.
22169 Given a pointer to a dwarf block that defines a location, compute
22170 the location and return the value.
22171
4cecd739
DJ
22172 NOTE drow/2003-11-18: This function is called in two situations
22173 now: for the address of static or global variables (partial symbols
22174 only) and for offsets into structures which are expected to be
22175 (more or less) constant. The partial symbol case should go away,
22176 and only the constant case should remain. That will let this
22177 function complain more accurately. A few special modes are allowed
22178 without complaint for global variables (for instance, global
22179 register values and thread-local values).
c906108c
SS
22180
22181 A location description containing no operations indicates that the
4cecd739 22182 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22183 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22184 callers will only want a very basic result and this can become a
21ae7a4d
JK
22185 complaint.
22186
22187 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22188
22189static CORE_ADDR
e7c27a73 22190decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22191{
518817b3 22192 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22193 size_t i;
22194 size_t size = blk->size;
d521ce57 22195 const gdb_byte *data = blk->data;
21ae7a4d
JK
22196 CORE_ADDR stack[64];
22197 int stacki;
22198 unsigned int bytes_read, unsnd;
22199 gdb_byte op;
c906108c 22200
21ae7a4d
JK
22201 i = 0;
22202 stacki = 0;
22203 stack[stacki] = 0;
22204 stack[++stacki] = 0;
22205
22206 while (i < size)
22207 {
22208 op = data[i++];
22209 switch (op)
22210 {
22211 case DW_OP_lit0:
22212 case DW_OP_lit1:
22213 case DW_OP_lit2:
22214 case DW_OP_lit3:
22215 case DW_OP_lit4:
22216 case DW_OP_lit5:
22217 case DW_OP_lit6:
22218 case DW_OP_lit7:
22219 case DW_OP_lit8:
22220 case DW_OP_lit9:
22221 case DW_OP_lit10:
22222 case DW_OP_lit11:
22223 case DW_OP_lit12:
22224 case DW_OP_lit13:
22225 case DW_OP_lit14:
22226 case DW_OP_lit15:
22227 case DW_OP_lit16:
22228 case DW_OP_lit17:
22229 case DW_OP_lit18:
22230 case DW_OP_lit19:
22231 case DW_OP_lit20:
22232 case DW_OP_lit21:
22233 case DW_OP_lit22:
22234 case DW_OP_lit23:
22235 case DW_OP_lit24:
22236 case DW_OP_lit25:
22237 case DW_OP_lit26:
22238 case DW_OP_lit27:
22239 case DW_OP_lit28:
22240 case DW_OP_lit29:
22241 case DW_OP_lit30:
22242 case DW_OP_lit31:
22243 stack[++stacki] = op - DW_OP_lit0;
22244 break;
f1bea926 22245
21ae7a4d
JK
22246 case DW_OP_reg0:
22247 case DW_OP_reg1:
22248 case DW_OP_reg2:
22249 case DW_OP_reg3:
22250 case DW_OP_reg4:
22251 case DW_OP_reg5:
22252 case DW_OP_reg6:
22253 case DW_OP_reg7:
22254 case DW_OP_reg8:
22255 case DW_OP_reg9:
22256 case DW_OP_reg10:
22257 case DW_OP_reg11:
22258 case DW_OP_reg12:
22259 case DW_OP_reg13:
22260 case DW_OP_reg14:
22261 case DW_OP_reg15:
22262 case DW_OP_reg16:
22263 case DW_OP_reg17:
22264 case DW_OP_reg18:
22265 case DW_OP_reg19:
22266 case DW_OP_reg20:
22267 case DW_OP_reg21:
22268 case DW_OP_reg22:
22269 case DW_OP_reg23:
22270 case DW_OP_reg24:
22271 case DW_OP_reg25:
22272 case DW_OP_reg26:
22273 case DW_OP_reg27:
22274 case DW_OP_reg28:
22275 case DW_OP_reg29:
22276 case DW_OP_reg30:
22277 case DW_OP_reg31:
22278 stack[++stacki] = op - DW_OP_reg0;
22279 if (i < size)
22280 dwarf2_complex_location_expr_complaint ();
22281 break;
c906108c 22282
21ae7a4d
JK
22283 case DW_OP_regx:
22284 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22285 i += bytes_read;
22286 stack[++stacki] = unsnd;
22287 if (i < size)
22288 dwarf2_complex_location_expr_complaint ();
22289 break;
c906108c 22290
21ae7a4d 22291 case DW_OP_addr:
c8a7a66f
TT
22292 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22293 &bytes_read);
21ae7a4d
JK
22294 i += bytes_read;
22295 break;
d53d4ac5 22296
21ae7a4d
JK
22297 case DW_OP_const1u:
22298 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22299 i += 1;
22300 break;
22301
22302 case DW_OP_const1s:
22303 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22304 i += 1;
22305 break;
22306
22307 case DW_OP_const2u:
22308 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22309 i += 2;
22310 break;
22311
22312 case DW_OP_const2s:
22313 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22314 i += 2;
22315 break;
d53d4ac5 22316
21ae7a4d
JK
22317 case DW_OP_const4u:
22318 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22319 i += 4;
22320 break;
22321
22322 case DW_OP_const4s:
22323 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22324 i += 4;
22325 break;
22326
585861ea
JK
22327 case DW_OP_const8u:
22328 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22329 i += 8;
22330 break;
22331
21ae7a4d
JK
22332 case DW_OP_constu:
22333 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22334 &bytes_read);
22335 i += bytes_read;
22336 break;
22337
22338 case DW_OP_consts:
22339 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22340 i += bytes_read;
22341 break;
22342
22343 case DW_OP_dup:
22344 stack[stacki + 1] = stack[stacki];
22345 stacki++;
22346 break;
22347
22348 case DW_OP_plus:
22349 stack[stacki - 1] += stack[stacki];
22350 stacki--;
22351 break;
22352
22353 case DW_OP_plus_uconst:
22354 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22355 &bytes_read);
22356 i += bytes_read;
22357 break;
22358
22359 case DW_OP_minus:
22360 stack[stacki - 1] -= stack[stacki];
22361 stacki--;
22362 break;
22363
22364 case DW_OP_deref:
22365 /* If we're not the last op, then we definitely can't encode
22366 this using GDB's address_class enum. This is valid for partial
22367 global symbols, although the variable's address will be bogus
22368 in the psymtab. */
22369 if (i < size)
22370 dwarf2_complex_location_expr_complaint ();
22371 break;
22372
22373 case DW_OP_GNU_push_tls_address:
4aa4e28b 22374 case DW_OP_form_tls_address:
21ae7a4d
JK
22375 /* The top of the stack has the offset from the beginning
22376 of the thread control block at which the variable is located. */
22377 /* Nothing should follow this operator, so the top of stack would
22378 be returned. */
22379 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22380 address will be bogus in the psymtab. Make it always at least
22381 non-zero to not look as a variable garbage collected by linker
22382 which have DW_OP_addr 0. */
21ae7a4d
JK
22383 if (i < size)
22384 dwarf2_complex_location_expr_complaint ();
585861ea 22385 stack[stacki]++;
21ae7a4d
JK
22386 break;
22387
22388 case DW_OP_GNU_uninit:
22389 break;
22390
336d760d 22391 case DW_OP_addrx:
3019eac3 22392 case DW_OP_GNU_addr_index:
49f6c839 22393 case DW_OP_GNU_const_index:
3019eac3
DE
22394 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22395 &bytes_read);
22396 i += bytes_read;
22397 break;
22398
21ae7a4d
JK
22399 default:
22400 {
f39c6ffd 22401 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22402
22403 if (name)
b98664d3 22404 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
22405 name);
22406 else
b98664d3 22407 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
22408 op);
22409 }
22410
22411 return (stack[stacki]);
d53d4ac5 22412 }
3c6e0cb3 22413
21ae7a4d
JK
22414 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22415 outside of the allocated space. Also enforce minimum>0. */
22416 if (stacki >= ARRAY_SIZE (stack) - 1)
22417 {
b98664d3 22418 complaint (_("location description stack overflow"));
21ae7a4d
JK
22419 return 0;
22420 }
22421
22422 if (stacki <= 0)
22423 {
b98664d3 22424 complaint (_("location description stack underflow"));
21ae7a4d
JK
22425 return 0;
22426 }
22427 }
22428 return (stack[stacki]);
c906108c
SS
22429}
22430
22431/* memory allocation interface */
22432
c906108c 22433static struct dwarf_block *
7b5a2f43 22434dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22435{
8d749320 22436 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22437}
22438
c906108c 22439static struct die_info *
b60c80d6 22440dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22441{
22442 struct die_info *die;
b60c80d6
DJ
22443 size_t size = sizeof (struct die_info);
22444
22445 if (num_attrs > 1)
22446 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22447
b60c80d6 22448 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22449 memset (die, 0, sizeof (struct die_info));
22450 return (die);
22451}
2e276125
JB
22452
22453\f
a036ba48 22454
c90ec28a 22455/* Macro support. */
cf2c3c16 22456
9eac9650
TT
22457/* An overload of dwarf_decode_macros that finds the correct section
22458 and ensures it is read in before calling the other overload. */
22459
22460static void
22461dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22462 int section_is_gnu)
22463{
22464 struct dwarf2_per_objfile *dwarf2_per_objfile
22465 = cu->per_cu->dwarf2_per_objfile;
22466 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 22467 const struct line_header *lh = cu->line_header;
9eac9650
TT
22468 unsigned int offset_size = cu->header.offset_size;
22469 struct dwarf2_section_info *section;
22470 const char *section_name;
22471
22472 if (cu->dwo_unit != nullptr)
22473 {
22474 if (section_is_gnu)
22475 {
22476 section = &cu->dwo_unit->dwo_file->sections.macro;
22477 section_name = ".debug_macro.dwo";
22478 }
22479 else
22480 {
22481 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22482 section_name = ".debug_macinfo.dwo";
22483 }
22484 }
22485 else
22486 {
22487 if (section_is_gnu)
22488 {
22489 section = &dwarf2_per_objfile->macro;
22490 section_name = ".debug_macro";
22491 }
22492 else
22493 {
22494 section = &dwarf2_per_objfile->macinfo;
22495 section_name = ".debug_macinfo";
22496 }
22497 }
22498
22499 section->read (objfile);
22500 if (section->buffer == nullptr)
22501 {
22502 complaint (_("missing %s section"), section_name);
22503 return;
22504 }
22505
22506 buildsym_compunit *builder = cu->get_builder ();
22507
22508 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22509 offset_size, offset, section_is_gnu);
22510}
22511
3019eac3
DE
22512/* Return the .debug_loc section to use for CU.
22513 For DWO files use .debug_loc.dwo. */
22514
22515static struct dwarf2_section_info *
22516cu_debug_loc_section (struct dwarf2_cu *cu)
22517{
518817b3
SM
22518 struct dwarf2_per_objfile *dwarf2_per_objfile
22519 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22520
3019eac3 22521 if (cu->dwo_unit)
43988095
JK
22522 {
22523 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 22524
43988095
JK
22525 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22526 }
22527 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22528 : &dwarf2_per_objfile->loc);
3019eac3
DE
22529}
22530
8cf6f0b1
TT
22531/* A helper function that fills in a dwarf2_loclist_baton. */
22532
22533static void
22534fill_in_loclist_baton (struct dwarf2_cu *cu,
22535 struct dwarf2_loclist_baton *baton,
ff39bb5e 22536 const struct attribute *attr)
8cf6f0b1 22537{
518817b3
SM
22538 struct dwarf2_per_objfile *dwarf2_per_objfile
22539 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
22540 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22541
96b79293 22542 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
22543
22544 baton->per_cu = cu->per_cu;
22545 gdb_assert (baton->per_cu);
22546 /* We don't know how long the location list is, but make sure we
22547 don't run off the edge of the section. */
3019eac3
DE
22548 baton->size = section->size - DW_UNSND (attr);
22549 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
22550 if (cu->base_address.has_value ())
22551 baton->base_address = *cu->base_address;
22552 else
22553 baton->base_address = 0;
f664829e 22554 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22555}
22556
4c2df51b 22557static void
ff39bb5e 22558dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22559 struct dwarf2_cu *cu, int is_block)
4c2df51b 22560{
518817b3
SM
22561 struct dwarf2_per_objfile *dwarf2_per_objfile
22562 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 22563 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22564 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22565
cd6c91b4 22566 if (attr->form_is_section_offset ()
3019eac3 22567 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22568 the section. If so, fall through to the complaint in the
22569 other branch. */
2c7d5afc 22570 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 22571 {
0d53c4c4 22572 struct dwarf2_loclist_baton *baton;
4c2df51b 22573
8d749320 22574 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22575
8cf6f0b1 22576 fill_in_loclist_baton (cu, baton, attr);
be391dca 22577
2b24b6e4 22578 if (!cu->base_address.has_value ())
b98664d3 22579 complaint (_("Location list used without "
3e43a32a 22580 "specifying the CU base address."));
4c2df51b 22581
f1e6e072
TT
22582 SYMBOL_ACLASS_INDEX (sym) = (is_block
22583 ? dwarf2_loclist_block_index
22584 : dwarf2_loclist_index);
0d53c4c4
DJ
22585 SYMBOL_LOCATION_BATON (sym) = baton;
22586 }
22587 else
22588 {
22589 struct dwarf2_locexpr_baton *baton;
22590
8d749320 22591 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22592 baton->per_cu = cu->per_cu;
22593 gdb_assert (baton->per_cu);
0d53c4c4 22594
4fc6c0d5 22595 if (attr->form_is_block ())
0d53c4c4
DJ
22596 {
22597 /* Note that we're just copying the block's data pointer
22598 here, not the actual data. We're still pointing into the
6502dd73
DJ
22599 info_buffer for SYM's objfile; right now we never release
22600 that buffer, but when we do clean up properly this may
22601 need to change. */
0d53c4c4
DJ
22602 baton->size = DW_BLOCK (attr)->size;
22603 baton->data = DW_BLOCK (attr)->data;
22604 }
22605 else
22606 {
22607 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 22608 sym->natural_name ());
0d53c4c4 22609 baton->size = 0;
0d53c4c4 22610 }
6e70227d 22611
f1e6e072
TT
22612 SYMBOL_ACLASS_INDEX (sym) = (is_block
22613 ? dwarf2_locexpr_block_index
22614 : dwarf2_locexpr_index);
0d53c4c4
DJ
22615 SYMBOL_LOCATION_BATON (sym) = baton;
22616 }
4c2df51b 22617}
6502dd73 22618
09ba997f 22619/* See read.h. */
ae0d2f24
UW
22620
22621struct objfile *
09ba997f 22622dwarf2_per_cu_data::objfile () const
ae0d2f24 22623{
09ba997f 22624 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
22625
22626 /* Return the master objfile, so that we can report and look up the
22627 correct file containing this variable. */
22628 if (objfile->separate_debug_objfile_backlink)
22629 objfile = objfile->separate_debug_objfile_backlink;
22630
22631 return objfile;
22632}
22633
96408a79
SA
22634/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22635 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22636 CU_HEADERP first. */
22637
22638static const struct comp_unit_head *
22639per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 22640 const struct dwarf2_per_cu_data *per_cu)
96408a79 22641{
d521ce57 22642 const gdb_byte *info_ptr;
96408a79
SA
22643
22644 if (per_cu->cu)
22645 return &per_cu->cu->header;
22646
9c541725 22647 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22648
22649 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22650 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22651 rcuh_kind::COMPILE);
96408a79
SA
22652
22653 return cu_headerp;
22654}
22655
09ba997f 22656/* See read.h. */
ae0d2f24 22657
98714339 22658int
09ba997f 22659dwarf2_per_cu_data::addr_size () const
ae0d2f24 22660{
96408a79
SA
22661 struct comp_unit_head cu_header_local;
22662 const struct comp_unit_head *cu_headerp;
c471e790 22663
09ba997f 22664 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22665
22666 return cu_headerp->addr_size;
ae0d2f24
UW
22667}
22668
09ba997f 22669/* See read.h. */
9eae7c52
TT
22670
22671int
09ba997f 22672dwarf2_per_cu_data::offset_size () const
9eae7c52 22673{
96408a79
SA
22674 struct comp_unit_head cu_header_local;
22675 const struct comp_unit_head *cu_headerp;
9c6c53f7 22676
09ba997f 22677 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22678
22679 return cu_headerp->offset_size;
22680}
22681
09ba997f 22682/* See read.h. */
96408a79
SA
22683
22684int
09ba997f 22685dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
22686{
22687 struct comp_unit_head cu_header_local;
22688 const struct comp_unit_head *cu_headerp;
22689
09ba997f 22690 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22691
22692 if (cu_headerp->version == 2)
22693 return cu_headerp->addr_size;
22694 else
22695 return cu_headerp->offset_size;
181cebd4
JK
22696}
22697
09ba997f 22698/* See read.h. */
9aa1f1e3
TT
22699
22700CORE_ADDR
09ba997f 22701dwarf2_per_cu_data::text_offset () const
9aa1f1e3 22702{
09ba997f
TT
22703 struct objfile *objfile = dwarf2_per_objfile->objfile;
22704
22705 return objfile->text_section_offset ();
9aa1f1e3
TT
22706}
22707
09ba997f
TT
22708/* See read.h. */
22709
22710struct type *
22711dwarf2_per_cu_data::addr_type () const
9a49df9d 22712{
09ba997f 22713 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
22714 struct type *void_type = objfile_type (objfile)->builtin_void;
22715 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 22716 int addr_size = this->addr_size ();
9a49df9d
AB
22717
22718 if (TYPE_LENGTH (addr_type) == addr_size)
22719 return addr_type;
22720
09ba997f 22721 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
22722 return addr_type;
22723}
22724
22b6cd70
TT
22725/* A helper function for dwarf2_find_containing_comp_unit that returns
22726 the index of the result, and that searches a vector. It will
22727 return a result even if the offset in question does not actually
22728 occur in any CU. This is separate so that it can be unit
22729 tested. */
ae038cb0 22730
22b6cd70
TT
22731static int
22732dwarf2_find_containing_comp_unit
22733 (sect_offset sect_off,
22734 unsigned int offset_in_dwz,
22735 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 22736{
ae038cb0
DJ
22737 int low, high;
22738
ae038cb0 22739 low = 0;
22b6cd70 22740 high = all_comp_units.size () - 1;
ae038cb0
DJ
22741 while (high > low)
22742 {
36586728 22743 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22744 int mid = low + (high - low) / 2;
9a619af0 22745
22b6cd70 22746 mid_cu = all_comp_units[mid];
36586728 22747 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 22748 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 22749 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
22750 high = mid;
22751 else
22752 low = mid + 1;
22753 }
22754 gdb_assert (low == high);
22b6cd70
TT
22755 return low;
22756}
22757
22758/* Locate the .debug_info compilation unit from CU's objfile which contains
22759 the DIE at OFFSET. Raises an error on failure. */
22760
22761static struct dwarf2_per_cu_data *
22762dwarf2_find_containing_comp_unit (sect_offset sect_off,
22763 unsigned int offset_in_dwz,
22764 struct dwarf2_per_objfile *dwarf2_per_objfile)
22765{
22766 int low
22767 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22768 dwarf2_per_objfile->all_comp_units);
22769 struct dwarf2_per_cu_data *this_cu
22770 = dwarf2_per_objfile->all_comp_units[low];
22771
45b8ae0c 22772 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 22773 {
36586728 22774 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22775 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
22776 "offset %s [in module %s]"),
22777 sect_offset_str (sect_off),
ed2dc618 22778 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 22779
9c541725
PA
22780 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22781 <= sect_off);
ae038cb0
DJ
22782 return dwarf2_per_objfile->all_comp_units[low-1];
22783 }
22784 else
22785 {
b76e467d 22786 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 22787 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 22788 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 22789 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22790 return this_cu;
22791 }
22792}
22793
22b6cd70
TT
22794#if GDB_SELF_TEST
22795
22796namespace selftests {
22797namespace find_containing_comp_unit {
22798
22799static void
22800run_test ()
22801{
22802 struct dwarf2_per_cu_data one {};
22803 struct dwarf2_per_cu_data two {};
22804 struct dwarf2_per_cu_data three {};
22805 struct dwarf2_per_cu_data four {};
22806
22807 one.length = 5;
22808 two.sect_off = sect_offset (one.length);
22809 two.length = 7;
22810
22811 three.length = 5;
22812 three.is_dwz = 1;
22813 four.sect_off = sect_offset (three.length);
22814 four.length = 7;
22815 four.is_dwz = 1;
22816
22817 std::vector<dwarf2_per_cu_data *> units;
22818 units.push_back (&one);
22819 units.push_back (&two);
22820 units.push_back (&three);
22821 units.push_back (&four);
22822
22823 int result;
22824
22825 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22826 SELF_CHECK (units[result] == &one);
22827 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22828 SELF_CHECK (units[result] == &one);
22829 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22830 SELF_CHECK (units[result] == &two);
22831
22832 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22833 SELF_CHECK (units[result] == &three);
22834 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22835 SELF_CHECK (units[result] == &three);
22836 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22837 SELF_CHECK (units[result] == &four);
22838}
22839
22840}
22841}
22842
22843#endif /* GDB_SELF_TEST */
22844
23745b47 22845/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22846
fcd3b13d
SM
22847dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
22848 : per_cu (per_cu_),
9068261f
AB
22849 mark (false),
22850 has_loclist (false),
22851 checked_producer (false),
22852 producer_is_gxx_lt_4_6 (false),
22853 producer_is_gcc_lt_4_3 (false),
eb77c9df 22854 producer_is_icc (false),
9068261f 22855 producer_is_icc_lt_14 (false),
c258c396 22856 producer_is_codewarrior (false),
9068261f 22857 processing_has_namespace_info (false)
93311388 22858{
fcd3b13d
SM
22859 per_cu->cu = this;
22860}
22861
22862/* Destroy a dwarf2_cu. */
22863
22864dwarf2_cu::~dwarf2_cu ()
22865{
22866 per_cu->cu = NULL;
9816fde3
JK
22867}
22868
22869/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22870
22871static void
95554aad
TT
22872prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22873 enum language pretend_language)
9816fde3
JK
22874{
22875 struct attribute *attr;
22876
22877 /* Set the language we're debugging. */
22878 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 22879 if (attr != nullptr)
9816fde3
JK
22880 set_cu_language (DW_UNSND (attr), cu);
22881 else
9cded63f 22882 {
95554aad 22883 cu->language = pretend_language;
9cded63f
TT
22884 cu->language_defn = language_def (cu->language);
22885 }
dee91e82 22886
7d45c7c3 22887 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22888}
22889
ae038cb0
DJ
22890/* Increase the age counter on each cached compilation unit, and free
22891 any that are too old. */
22892
22893static void
ed2dc618 22894age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
22895{
22896 struct dwarf2_per_cu_data *per_cu, **last_chain;
22897
22898 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22899 per_cu = dwarf2_per_objfile->read_in_chain;
22900 while (per_cu != NULL)
22901 {
22902 per_cu->cu->last_used ++;
b4f54984 22903 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22904 dwarf2_mark (per_cu->cu);
22905 per_cu = per_cu->cu->read_in_chain;
22906 }
22907
22908 per_cu = dwarf2_per_objfile->read_in_chain;
22909 last_chain = &dwarf2_per_objfile->read_in_chain;
22910 while (per_cu != NULL)
22911 {
22912 struct dwarf2_per_cu_data *next_cu;
22913
22914 next_cu = per_cu->cu->read_in_chain;
22915
22916 if (!per_cu->cu->mark)
22917 {
fcd3b13d 22918 delete per_cu->cu;
ae038cb0
DJ
22919 *last_chain = next_cu;
22920 }
22921 else
22922 last_chain = &per_cu->cu->read_in_chain;
22923
22924 per_cu = next_cu;
22925 }
22926}
22927
22928/* Remove a single compilation unit from the cache. */
22929
22930static void
dee91e82 22931free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22932{
22933 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
22934 struct dwarf2_per_objfile *dwarf2_per_objfile
22935 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
22936
22937 per_cu = dwarf2_per_objfile->read_in_chain;
22938 last_chain = &dwarf2_per_objfile->read_in_chain;
22939 while (per_cu != NULL)
22940 {
22941 struct dwarf2_per_cu_data *next_cu;
22942
22943 next_cu = per_cu->cu->read_in_chain;
22944
dee91e82 22945 if (per_cu == target_per_cu)
ae038cb0 22946 {
fcd3b13d 22947 delete per_cu->cu;
dee91e82 22948 per_cu->cu = NULL;
ae038cb0
DJ
22949 *last_chain = next_cu;
22950 break;
22951 }
22952 else
22953 last_chain = &per_cu->cu->read_in_chain;
22954
22955 per_cu = next_cu;
22956 }
22957}
22958
dee91e82
DE
22959/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22960 We store these in a hash table separate from the DIEs, and preserve them
22961 when the DIEs are flushed out of cache.
22962
22963 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22964 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22965 or the type may come from a DWO file. Furthermore, while it's more logical
22966 to use per_cu->section+offset, with Fission the section with the data is in
22967 the DWO file but we don't know that section at the point we need it.
22968 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22969 because we can enter the lookup routine, get_die_type_at_offset, from
22970 outside this file, and thus won't necessarily have PER_CU->cu.
22971 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22972
dee91e82 22973struct dwarf2_per_cu_offset_and_type
1c379e20 22974{
dee91e82 22975 const struct dwarf2_per_cu_data *per_cu;
9c541725 22976 sect_offset sect_off;
1c379e20
DJ
22977 struct type *type;
22978};
22979
dee91e82 22980/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22981
22982static hashval_t
dee91e82 22983per_cu_offset_and_type_hash (const void *item)
1c379e20 22984{
9a3c8263
SM
22985 const struct dwarf2_per_cu_offset_and_type *ofs
22986 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22987
9c541725 22988 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
22989}
22990
dee91e82 22991/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22992
22993static int
dee91e82 22994per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22995{
9a3c8263
SM
22996 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22997 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22998 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22999 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23000
dee91e82 23001 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23002 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23003}
23004
23005/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23006 table if necessary. For convenience, return TYPE.
23007
23008 The DIEs reading must have careful ordering to:
85102364 23009 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23010 reading current DIE.
23011 * Not trying to dereference contents of still incompletely read in types
23012 while reading in other DIEs.
23013 * Enable referencing still incompletely read in types just by a pointer to
23014 the type without accessing its fields.
23015
23016 Therefore caller should follow these rules:
23017 * Try to fetch any prerequisite types we may need to build this DIE type
23018 before building the type and calling set_die_type.
e71ec853 23019 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23020 possible before fetching more types to complete the current type.
23021 * Make the type as complete as possible before fetching more types. */
1c379e20 23022
f792889a 23023static struct type *
1c379e20
DJ
23024set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23025{
518817b3
SM
23026 struct dwarf2_per_objfile *dwarf2_per_objfile
23027 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23028 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23029 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23030 struct attribute *attr;
23031 struct dynamic_prop prop;
1c379e20 23032
b4ba55a1
JB
23033 /* For Ada types, make sure that the gnat-specific data is always
23034 initialized (if not already set). There are a few types where
23035 we should not be doing so, because the type-specific area is
23036 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23037 where the type-specific area is used to store the floatformat).
23038 But this is not a problem, because the gnat-specific information
23039 is actually not needed for these types. */
23040 if (need_gnat_info (cu)
23041 && TYPE_CODE (type) != TYPE_CODE_FUNC
23042 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23043 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23044 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23045 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23046 && !HAVE_GNAT_AUX_INFO (type))
23047 INIT_GNAT_SPECIFIC (type);
23048
3f2f83dd
KB
23049 /* Read DW_AT_allocated and set in type. */
23050 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23051 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23052 {
09ba997f 23053 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23054 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23055 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
23056 }
23057 else if (attr != NULL)
23058 {
b98664d3 23059 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23060 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23061 sect_offset_str (die->sect_off));
3f2f83dd
KB
23062 }
23063
23064 /* Read DW_AT_associated and set in type. */
23065 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23066 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23067 {
09ba997f 23068 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23069 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23070 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
23071 }
23072 else if (attr != NULL)
23073 {
b98664d3 23074 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23075 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23076 sect_offset_str (die->sect_off));
3f2f83dd
KB
23077 }
23078
3cdcd0ce
JB
23079 /* Read DW_AT_data_location and set in type. */
23080 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23081 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23082 cu->per_cu->addr_type ()))
50a82047 23083 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 23084
dee91e82 23085 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23086 dwarf2_per_objfile->die_type_hash
23087 = htab_up (htab_create_alloc (127,
23088 per_cu_offset_and_type_hash,
23089 per_cu_offset_and_type_eq,
23090 NULL, xcalloc, xfree));
1c379e20 23091
dee91e82 23092 ofs.per_cu = cu->per_cu;
9c541725 23093 ofs.sect_off = die->sect_off;
1c379e20 23094 ofs.type = type;
dee91e82 23095 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23096 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23097 if (*slot)
b98664d3 23098 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23099 sect_offset_str (die->sect_off));
8d749320
SM
23100 *slot = XOBNEW (&objfile->objfile_obstack,
23101 struct dwarf2_per_cu_offset_and_type);
1c379e20 23102 **slot = ofs;
f792889a 23103 return type;
1c379e20
DJ
23104}
23105
9c541725 23106/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23107 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23108
23109static struct type *
9c541725 23110get_die_type_at_offset (sect_offset sect_off,
673bfd45 23111 struct dwarf2_per_cu_data *per_cu)
1c379e20 23112{
dee91e82 23113 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23114 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23115
dee91e82 23116 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23117 return NULL;
1c379e20 23118
dee91e82 23119 ofs.per_cu = per_cu;
9c541725 23120 ofs.sect_off = sect_off;
9a3c8263 23121 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23122 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23123 if (slot)
23124 return slot->type;
23125 else
23126 return NULL;
23127}
23128
02142a6c 23129/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23130 or return NULL if DIE does not have a saved type. */
23131
23132static struct type *
23133get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23134{
9c541725 23135 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23136}
23137
10b3939b
DJ
23138/* Add a dependence relationship from CU to REF_PER_CU. */
23139
23140static void
23141dwarf2_add_dependence (struct dwarf2_cu *cu,
23142 struct dwarf2_per_cu_data *ref_per_cu)
23143{
23144 void **slot;
23145
23146 if (cu->dependencies == NULL)
23147 cu->dependencies
23148 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23149 NULL, &cu->comp_unit_obstack,
23150 hashtab_obstack_allocate,
23151 dummy_obstack_deallocate);
23152
23153 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23154 if (*slot == NULL)
23155 *slot = ref_per_cu;
23156}
1c379e20 23157
f504f079
DE
23158/* Subroutine of dwarf2_mark to pass to htab_traverse.
23159 Set the mark field in every compilation unit in the
ae038cb0
DJ
23160 cache that we must keep because we are keeping CU. */
23161
10b3939b
DJ
23162static int
23163dwarf2_mark_helper (void **slot, void *data)
23164{
23165 struct dwarf2_per_cu_data *per_cu;
23166
23167 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23168
23169 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23170 reading of the chain. As such dependencies remain valid it is not much
23171 useful to track and undo them during QUIT cleanups. */
23172 if (per_cu->cu == NULL)
23173 return 1;
23174
10b3939b
DJ
23175 if (per_cu->cu->mark)
23176 return 1;
9068261f 23177 per_cu->cu->mark = true;
10b3939b
DJ
23178
23179 if (per_cu->cu->dependencies != NULL)
23180 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23181
23182 return 1;
23183}
23184
f504f079
DE
23185/* Set the mark field in CU and in every other compilation unit in the
23186 cache that we must keep because we are keeping CU. */
23187
ae038cb0
DJ
23188static void
23189dwarf2_mark (struct dwarf2_cu *cu)
23190{
23191 if (cu->mark)
23192 return;
9068261f 23193 cu->mark = true;
10b3939b
DJ
23194 if (cu->dependencies != NULL)
23195 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23196}
23197
23198static void
23199dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23200{
23201 while (per_cu)
23202 {
9068261f 23203 per_cu->cu->mark = false;
ae038cb0
DJ
23204 per_cu = per_cu->cu->read_in_chain;
23205 }
72bf9492
DJ
23206}
23207
72bf9492
DJ
23208/* Trivial hash function for partial_die_info: the hash value of a DIE
23209 is its offset in .debug_info for this objfile. */
23210
23211static hashval_t
23212partial_die_hash (const void *item)
23213{
9a3c8263
SM
23214 const struct partial_die_info *part_die
23215 = (const struct partial_die_info *) item;
9a619af0 23216
9c541725 23217 return to_underlying (part_die->sect_off);
72bf9492
DJ
23218}
23219
23220/* Trivial comparison function for partial_die_info structures: two DIEs
23221 are equal if they have the same offset. */
23222
23223static int
23224partial_die_eq (const void *item_lhs, const void *item_rhs)
23225{
9a3c8263
SM
23226 const struct partial_die_info *part_die_lhs
23227 = (const struct partial_die_info *) item_lhs;
23228 const struct partial_die_info *part_die_rhs
23229 = (const struct partial_die_info *) item_rhs;
9a619af0 23230
9c541725 23231 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23232}
23233
3c3bb058
AB
23234struct cmd_list_element *set_dwarf_cmdlist;
23235struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23236
23237static void
981a3fb3 23238set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 23239{
b4f54984 23240 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23241 gdb_stdout);
ae038cb0
DJ
23242}
23243
23244static void
981a3fb3 23245show_dwarf_cmd (const char *args, int from_tty)
6e70227d 23246{
b4f54984 23247 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23248}
23249
9291a0cd 23250static void
cd4fb1b2
SM
23251show_check_physname (struct ui_file *file, int from_tty,
23252 struct cmd_list_element *c, const char *value)
9291a0cd 23253{
cd4fb1b2
SM
23254 fprintf_filtered (file,
23255 _("Whether to check \"physname\" is %s.\n"),
23256 value);
9291a0cd
TT
23257}
23258
6c265988 23259void _initialize_dwarf2_read ();
cd4fb1b2 23260void
6c265988 23261_initialize_dwarf2_read ()
9291a0cd 23262{
cd4fb1b2
SM
23263 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23264Set DWARF specific variables.\n\
590042fc 23265Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
23266 &set_dwarf_cmdlist, "maintenance set dwarf ",
23267 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23268
cd4fb1b2 23269 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
23270Show DWARF specific variables.\n\
23271Show DWARF variables such as the cache size."),
cd4fb1b2
SM
23272 &show_dwarf_cmdlist, "maintenance show dwarf ",
23273 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23274
cd4fb1b2
SM
23275 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23276 &dwarf_max_cache_age, _("\
23277Set the upper bound on the age of cached DWARF compilation units."), _("\
23278Show the upper bound on the age of cached DWARF compilation units."), _("\
23279A higher limit means that cached compilation units will be stored\n\
23280in memory longer, and more total memory will be used. Zero disables\n\
23281caching, which can slow down startup."),
23282 NULL,
23283 show_dwarf_max_cache_age,
23284 &set_dwarf_cmdlist,
23285 &show_dwarf_cmdlist);
156942c7 23286
cd4fb1b2
SM
23287 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23288Set debugging of the DWARF reader."), _("\
23289Show debugging of the DWARF reader."), _("\
23290When enabled (non-zero), debugging messages are printed during DWARF\n\
23291reading and symtab expansion. A value of 1 (one) provides basic\n\
23292information. A value greater than 1 provides more verbose information."),
23293 NULL,
23294 NULL,
23295 &setdebuglist, &showdebuglist);
9291a0cd 23296
cd4fb1b2
SM
23297 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23298Set debugging of the DWARF DIE reader."), _("\
23299Show debugging of the DWARF DIE reader."), _("\
23300When enabled (non-zero), DIEs are dumped after they are read in.\n\
23301The value is the maximum depth to print."),
23302 NULL,
23303 NULL,
23304 &setdebuglist, &showdebuglist);
9291a0cd 23305
cd4fb1b2
SM
23306 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23307Set debugging of the dwarf line reader."), _("\
23308Show debugging of the dwarf line reader."), _("\
23309When enabled (non-zero), line number entries are dumped as they are read in.\n\
23310A value of 1 (one) provides basic information.\n\
23311A value greater than 1 provides more verbose information."),
23312 NULL,
23313 NULL,
23314 &setdebuglist, &showdebuglist);
437afbb8 23315
cd4fb1b2
SM
23316 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23317Set cross-checking of \"physname\" code against demangler."), _("\
23318Show cross-checking of \"physname\" code against demangler."), _("\
23319When enabled, GDB's internal \"physname\" code is checked against\n\
23320the demangler."),
23321 NULL, show_check_physname,
23322 &setdebuglist, &showdebuglist);
900e11f9 23323
e615022a
DE
23324 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23325 no_class, &use_deprecated_index_sections, _("\
23326Set whether to use deprecated gdb_index sections."), _("\
23327Show whether to use deprecated gdb_index sections."), _("\
23328When enabled, deprecated .gdb_index sections are used anyway.\n\
23329Normally they are ignored either because of a missing feature or\n\
23330performance issue.\n\
23331Warning: This option must be enabled before gdb reads the file."),
23332 NULL,
23333 NULL,
23334 &setlist, &showlist);
23335
f1e6e072
TT
23336 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23337 &dwarf2_locexpr_funcs);
23338 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23339 &dwarf2_loclist_funcs);
23340
23341 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23342 &dwarf2_block_frame_base_locexpr_funcs);
23343 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23344 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23345
23346#if GDB_SELF_TEST
23347 selftests::register_test ("dw2_expand_symtabs_matching",
23348 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23349 selftests::register_test ("dwarf2_find_containing_comp_unit",
23350 selftests::find_containing_comp_unit::run_test);
c62446b1 23351#endif
6502dd73 23352}
This page took 5.397588 seconds and 4 git commands to generate.