Remove casts from dwarf2/index-write.c
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
4de283e4
TT
40#include "bfd.h"
41#include "elf-bfd.h"
42#include "symtab.h"
43#include "gdbtypes.h"
44#include "objfiles.h"
d55e5aa6 45#include "dwarf2.h"
4de283e4
TT
46#include "buildsym.h"
47#include "demangle.h"
48#include "gdb-demangle.h"
4de283e4
TT
49#include "filenames.h" /* for DOSish file names */
50#include "macrotab.h"
51#include "language.h"
52#include "complaints.h"
82ca8957
TT
53#include "dwarf2/expr.h"
54#include "dwarf2/loc.h"
4de283e4
TT
55#include "cp-support.h"
56#include "hashtab.h"
57#include "command.h"
d55e5aa6 58#include "gdbcmd.h"
4de283e4
TT
59#include "block.h"
60#include "addrmap.h"
61#include "typeprint.h"
62#include "psympriv.h"
4de283e4 63#include "c-lang.h"
d55e5aa6 64#include "go-lang.h"
4de283e4
TT
65#include "valprint.h"
66#include "gdbcore.h" /* for gnutarget */
67#include "gdb/gdb-index.h"
4de283e4
TT
68#include "gdb_bfd.h"
69#include "f-lang.h"
70#include "source.h"
4de283e4 71#include "build-id.h"
d55e5aa6 72#include "namespace.h"
268a13a5
TT
73#include "gdbsupport/function-view.h"
74#include "gdbsupport/gdb_optional.h"
75#include "gdbsupport/underlying.h"
268a13a5 76#include "gdbsupport/hash_enum.h"
4de283e4 77#include "filename-seen-cache.h"
b32b108a 78#include "producer.h"
4de283e4 79#include <fcntl.h>
4de283e4 80#include <algorithm>
4de283e4 81#include <unordered_map>
268a13a5 82#include "gdbsupport/selftest.h"
c9317f21 83#include "rust-lang.h"
268a13a5 84#include "gdbsupport/pathstuff.h"
edd45eb0 85#include "count-one-bits.h"
0d79cdc4 86#include "debuginfod-support.h"
437afbb8 87
73be47f5
DE
88/* When == 1, print basic high level tracing messages.
89 When > 1, be more verbose.
b4f54984
DE
90 This is in contrast to the low level DIE reading of dwarf_die_debug. */
91static unsigned int dwarf_read_debug = 0;
45cfd468 92
d97bc12b 93/* When non-zero, dump DIEs after they are read in. */
b4f54984 94static unsigned int dwarf_die_debug = 0;
d97bc12b 95
27e0867f 96/* When non-zero, dump line number entries as they are read in. */
8fdd972c 97unsigned int dwarf_line_debug = 0;
27e0867f 98
491144b5
CB
99/* When true, cross-check physname against demangler. */
100static bool check_physname = false;
900e11f9 101
491144b5
CB
102/* When true, do not reject deprecated .gdb_index sections. */
103static bool use_deprecated_index_sections = false;
481860b3 104
5bfd760d 105static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 106
f1e6e072
TT
107/* The "aclass" indices for various kinds of computed DWARF symbols. */
108
109static int dwarf2_locexpr_index;
110static int dwarf2_loclist_index;
111static int dwarf2_locexpr_block_index;
112static int dwarf2_loclist_block_index;
113
3f563c84
PA
114/* An index into a (C++) symbol name component in a symbol name as
115 recorded in the mapped_index's symbol table. For each C++ symbol
116 in the symbol table, we record one entry for the start of each
117 component in the symbol in a table of name components, and then
118 sort the table, in order to be able to binary search symbol names,
119 ignoring leading namespaces, both completion and regular look up.
120 For example, for symbol "A::B::C", we'll have an entry that points
121 to "A::B::C", another that points to "B::C", and another for "C".
122 Note that function symbols in GDB index have no parameter
123 information, just the function/method names. You can convert a
124 name_component to a "const char *" using the
125 'mapped_index::symbol_name_at(offset_type)' method. */
126
127struct name_component
128{
129 /* Offset in the symbol name where the component starts. Stored as
130 a (32-bit) offset instead of a pointer to save memory and improve
131 locality on 64-bit architectures. */
132 offset_type name_offset;
133
134 /* The symbol's index in the symbol and constant pool tables of a
135 mapped_index. */
136 offset_type idx;
137};
138
44ed8f3e
PA
139/* Base class containing bits shared by both .gdb_index and
140 .debug_name indexes. */
141
142struct mapped_index_base
143{
22ca247e
TT
144 mapped_index_base () = default;
145 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
146
44ed8f3e
PA
147 /* The name_component table (a sorted vector). See name_component's
148 description above. */
149 std::vector<name_component> name_components;
150
151 /* How NAME_COMPONENTS is sorted. */
152 enum case_sensitivity name_components_casing;
153
154 /* Return the number of names in the symbol table. */
155 virtual size_t symbol_name_count () const = 0;
156
157 /* Get the name of the symbol at IDX in the symbol table. */
158 virtual const char *symbol_name_at (offset_type idx) const = 0;
159
160 /* Return whether the name at IDX in the symbol table should be
161 ignored. */
162 virtual bool symbol_name_slot_invalid (offset_type idx) const
163 {
164 return false;
165 }
166
167 /* Build the symbol name component sorted vector, if we haven't
168 yet. */
169 void build_name_components ();
170
171 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
172 possible matches for LN_NO_PARAMS in the name component
173 vector. */
174 std::pair<std::vector<name_component>::const_iterator,
175 std::vector<name_component>::const_iterator>
3b00ef10
TT
176 find_name_components_bounds (const lookup_name_info &ln_no_params,
177 enum language lang) const;
44ed8f3e
PA
178
179 /* Prevent deleting/destroying via a base class pointer. */
180protected:
181 ~mapped_index_base() = default;
182};
183
9291a0cd
TT
184/* A description of the mapped index. The file format is described in
185 a comment by the code that writes the index. */
fc898b42 186struct mapped_index final : public mapped_index_base
9291a0cd 187{
f00a2de2
PA
188 /* A slot/bucket in the symbol table hash. */
189 struct symbol_table_slot
190 {
191 const offset_type name;
192 const offset_type vec;
193 };
194
559a7a62 195 /* Index data format version. */
3063847f 196 int version = 0;
559a7a62 197
f00a2de2
PA
198 /* The address table data. */
199 gdb::array_view<const gdb_byte> address_table;
b11b1f88 200
3876f04e 201 /* The symbol table, implemented as a hash table. */
f00a2de2 202 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 203
9291a0cd 204 /* A pointer to the constant pool. */
3063847f 205 const char *constant_pool = nullptr;
3f563c84 206
44ed8f3e
PA
207 bool symbol_name_slot_invalid (offset_type idx) const override
208 {
209 const auto &bucket = this->symbol_table[idx];
9ab08412 210 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 211 }
5c58de74 212
3f563c84
PA
213 /* Convenience method to get at the name of the symbol at IDX in the
214 symbol table. */
44ed8f3e 215 const char *symbol_name_at (offset_type idx) const override
f00a2de2 216 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 217
44ed8f3e
PA
218 size_t symbol_name_count () const override
219 { return this->symbol_table.size (); }
9291a0cd
TT
220};
221
927aa2e7
JK
222/* A description of the mapped .debug_names.
223 Uninitialized map has CU_COUNT 0. */
fc898b42 224struct mapped_debug_names final : public mapped_index_base
927aa2e7 225{
ed2dc618
SM
226 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
227 : dwarf2_per_objfile (dwarf2_per_objfile_)
228 {}
229
230 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
231 bfd_endian dwarf5_byte_order;
232 bool dwarf5_is_dwarf64;
233 bool augmentation_is_gdb;
234 uint8_t offset_size;
235 uint32_t cu_count = 0;
236 uint32_t tu_count, bucket_count, name_count;
237 const gdb_byte *cu_table_reordered, *tu_table_reordered;
238 const uint32_t *bucket_table_reordered, *hash_table_reordered;
239 const gdb_byte *name_table_string_offs_reordered;
240 const gdb_byte *name_table_entry_offs_reordered;
241 const gdb_byte *entry_pool;
242
243 struct index_val
244 {
245 ULONGEST dwarf_tag;
246 struct attr
247 {
248 /* Attribute name DW_IDX_*. */
249 ULONGEST dw_idx;
250
251 /* Attribute form DW_FORM_*. */
252 ULONGEST form;
253
254 /* Value if FORM is DW_FORM_implicit_const. */
255 LONGEST implicit_const;
256 };
257 std::vector<attr> attr_vec;
258 };
259
260 std::unordered_map<ULONGEST, index_val> abbrev_map;
261
262 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
263
264 /* Implementation of the mapped_index_base virtual interface, for
265 the name_components cache. */
266
267 const char *symbol_name_at (offset_type idx) const override
268 { return namei_to_name (idx); }
269
270 size_t symbol_name_count () const override
271 { return this->name_count; }
927aa2e7
JK
272};
273
cd4fb1b2 274/* See dwarf2read.h. */
ed2dc618 275
cd4fb1b2 276dwarf2_per_objfile *
ed2dc618
SM
277get_dwarf2_per_objfile (struct objfile *objfile)
278{
5bfd760d 279 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 280}
c906108c 281
251d32d9 282/* Default names of the debugging sections. */
c906108c 283
233a11ab
CS
284/* Note that if the debugging section has been compressed, it might
285 have a name like .zdebug_info. */
286
9cdd5dbd
DE
287static const struct dwarf2_debug_sections dwarf2_elf_names =
288{
251d32d9
TG
289 { ".debug_info", ".zdebug_info" },
290 { ".debug_abbrev", ".zdebug_abbrev" },
291 { ".debug_line", ".zdebug_line" },
292 { ".debug_loc", ".zdebug_loc" },
43988095 293 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 294 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 295 { ".debug_macro", ".zdebug_macro" },
251d32d9 296 { ".debug_str", ".zdebug_str" },
18a8505e 297 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 298 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 299 { ".debug_ranges", ".zdebug_ranges" },
43988095 300 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 301 { ".debug_types", ".zdebug_types" },
3019eac3 302 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
303 { ".debug_frame", ".zdebug_frame" },
304 { ".eh_frame", NULL },
24d3216f 305 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
306 { ".debug_names", ".zdebug_names" },
307 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 308 23
251d32d9 309};
c906108c 310
80626a55 311/* List of DWO/DWP sections. */
3019eac3 312
80626a55 313static const struct dwop_section_names
3019eac3
DE
314{
315 struct dwarf2_section_names abbrev_dwo;
316 struct dwarf2_section_names info_dwo;
317 struct dwarf2_section_names line_dwo;
318 struct dwarf2_section_names loc_dwo;
43988095 319 struct dwarf2_section_names loclists_dwo;
09262596
DE
320 struct dwarf2_section_names macinfo_dwo;
321 struct dwarf2_section_names macro_dwo;
3019eac3
DE
322 struct dwarf2_section_names str_dwo;
323 struct dwarf2_section_names str_offsets_dwo;
324 struct dwarf2_section_names types_dwo;
80626a55
DE
325 struct dwarf2_section_names cu_index;
326 struct dwarf2_section_names tu_index;
3019eac3 327}
80626a55 328dwop_section_names =
3019eac3
DE
329{
330 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
331 { ".debug_info.dwo", ".zdebug_info.dwo" },
332 { ".debug_line.dwo", ".zdebug_line.dwo" },
333 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 334 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
335 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
336 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
337 { ".debug_str.dwo", ".zdebug_str.dwo" },
338 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
339 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
340 { ".debug_cu_index", ".zdebug_cu_index" },
341 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
342};
343
c906108c
SS
344/* local data types */
345
3da10d80
KS
346/* Type used for delaying computation of method physnames.
347 See comments for compute_delayed_physnames. */
348struct delayed_method_info
349{
350 /* The type to which the method is attached, i.e., its parent class. */
351 struct type *type;
352
353 /* The index of the method in the type's function fieldlists. */
354 int fnfield_index;
355
356 /* The index of the method in the fieldlist. */
357 int index;
358
359 /* The name of the DIE. */
360 const char *name;
361
362 /* The DIE associated with this method. */
363 struct die_info *die;
364};
365
e7c27a73
DJ
366/* Internal state when decoding a particular compilation unit. */
367struct dwarf2_cu
368{
fcd3b13d
SM
369 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
370 ~dwarf2_cu ();
371
372 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
373
c24bdb02
KS
374 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
375 Create the set of symtabs used by this TU, or if this TU is sharing
376 symtabs with another TU and the symtabs have already been created
377 then restore those symtabs in the line header.
378 We don't need the pc/line-number mapping for type units. */
379 void setup_type_unit_groups (struct die_info *die);
380
381 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
382 buildsym_compunit constructor. */
383 struct compunit_symtab *start_symtab (const char *name,
384 const char *comp_dir,
385 CORE_ADDR low_pc);
386
387 /* Reset the builder. */
388 void reset_builder () { m_builder.reset (); }
389
d00adf39 390 /* The header of the compilation unit. */
fcd3b13d 391 struct comp_unit_head header {};
e142c38c 392
d00adf39 393 /* Base address of this compilation unit. */
fcd3b13d 394 CORE_ADDR base_address = 0;
d00adf39
DE
395
396 /* Non-zero if base_address has been set. */
fcd3b13d 397 int base_known = 0;
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
0963b4bd 1047/* This data structure holds a complete die structure. */
c906108c
SS
1048struct die_info
1049 {
76815b17
DE
1050 /* DWARF-2 tag for this DIE. */
1051 ENUM_BITFIELD(dwarf_tag) tag : 16;
1052
1053 /* Number of attributes */
98bfdba5
PA
1054 unsigned char num_attrs;
1055
1056 /* True if we're presently building the full type name for the
1057 type derived from this DIE. */
1058 unsigned char building_fullname : 1;
76815b17 1059
adde2bff
DE
1060 /* True if this die is in process. PR 16581. */
1061 unsigned char in_process : 1;
1062
3e225074
TT
1063 /* True if this DIE has children. */
1064 unsigned char has_children : 1;
1065
76815b17
DE
1066 /* Abbrev number */
1067 unsigned int abbrev;
1068
93311388 1069 /* Offset in .debug_info or .debug_types section. */
9c541725 1070 sect_offset sect_off;
78ba4af6
JB
1071
1072 /* The dies in a compilation unit form an n-ary tree. PARENT
1073 points to this die's parent; CHILD points to the first child of
1074 this node; and all the children of a given node are chained
4950bc1c 1075 together via their SIBLING fields. */
639d11d3
DC
1076 struct die_info *child; /* Its first child, if any. */
1077 struct die_info *sibling; /* Its next sibling, if any. */
1078 struct die_info *parent; /* Its parent, if any. */
c906108c 1079
b60c80d6
DJ
1080 /* An array of attributes, with NUM_ATTRS elements. There may be
1081 zero, but it's not common and zero-sized arrays are not
1082 sufficiently portable C. */
1083 struct attribute attrs[1];
c906108c
SS
1084 };
1085
c906108c
SS
1086/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1087 but this would require a corresponding change in unpack_field_as_long
1088 and friends. */
1089static int bits_per_byte = 8;
1090
2ddeaf8a
TT
1091/* When reading a variant or variant part, we track a bit more
1092 information about the field, and store it in an object of this
1093 type. */
1094
1095struct variant_field
1096{
1097 /* If we see a DW_TAG_variant, then this will be the discriminant
1098 value. */
1099 ULONGEST discriminant_value;
1100 /* If we see a DW_TAG_variant, then this will be set if this is the
1101 default branch. */
1102 bool default_branch;
1103 /* While reading a DW_TAG_variant_part, this will be set if this
1104 field is the discriminant. */
1105 bool is_discriminant;
1106};
1107
52059ffd
TT
1108struct nextfield
1109{
be2daae6
TT
1110 int accessibility = 0;
1111 int virtuality = 0;
2ddeaf8a 1112 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1113 struct variant_field variant {};
1114 struct field field {};
52059ffd
TT
1115};
1116
1117struct fnfieldlist
1118{
be2daae6
TT
1119 const char *name = nullptr;
1120 std::vector<struct fn_field> fnfields;
52059ffd
TT
1121};
1122
c906108c
SS
1123/* The routines that read and process dies for a C struct or C++ class
1124 pass lists of data member fields and lists of member function fields
1125 in an instance of a field_info structure, as defined below. */
1126struct field_info
c5aa993b 1127 {
0963b4bd 1128 /* List of data member and baseclasses fields. */
be2daae6
TT
1129 std::vector<struct nextfield> fields;
1130 std::vector<struct nextfield> baseclasses;
c906108c 1131
7d0ccb61 1132 /* Number of fields (including baseclasses). */
be2daae6 1133 int nfields = 0;
c906108c 1134
85102364 1135 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1136 int non_public_fields = 0;
c906108c 1137
c5aa993b
JM
1138 /* Member function fieldlist array, contains name of possibly overloaded
1139 member function, number of overloaded member functions and a pointer
1140 to the head of the member function field chain. */
be2daae6 1141 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1142
1143 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1144 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1145 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1146
1147 /* Nested types defined by this class and the number of elements in this
1148 list. */
be2daae6 1149 std::vector<struct decl_field> nested_types_list;
c5aa993b 1150 };
c906108c 1151
ae038cb0
DJ
1152/* Loaded secondary compilation units are kept in memory until they
1153 have not been referenced for the processing of this many
1154 compilation units. Set this to zero to disable caching. Cache
1155 sizes of up to at least twenty will improve startup time for
1156 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1157static int dwarf_max_cache_age = 5;
920d2a44 1158static void
b4f54984
DE
1159show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1160 struct cmd_list_element *c, const char *value)
920d2a44 1161{
3e43a32a 1162 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1163 "DWARF compilation units is %s.\n"),
920d2a44
AC
1164 value);
1165}
4390d890 1166\f
c906108c
SS
1167/* local function prototypes */
1168
918dd910
JK
1169static void dwarf2_find_base_address (struct die_info *die,
1170 struct dwarf2_cu *cu);
1171
891813be 1172static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1173 (struct dwarf2_per_cu_data *per_cu, const char *name);
1174
f1902523
JK
1175static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1176 const gdb_byte *info_ptr,
3e225074 1177 struct die_info *type_unit_die);
f1902523 1178
ed2dc618
SM
1179static void dwarf2_build_psymtabs_hard
1180 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1181
72bf9492
DJ
1182static void scan_partial_symbols (struct partial_die_info *,
1183 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1184 int, struct dwarf2_cu *);
c906108c 1185
72bf9492
DJ
1186static void add_partial_symbol (struct partial_die_info *,
1187 struct dwarf2_cu *);
63d06c5c 1188
72bf9492
DJ
1189static void add_partial_namespace (struct partial_die_info *pdi,
1190 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1191 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1192
5d7cb8df 1193static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1194 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1195 struct dwarf2_cu *cu);
1196
72bf9492
DJ
1197static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1198 struct dwarf2_cu *cu);
91c24f0a 1199
bc30ff58
JB
1200static void add_partial_subprogram (struct partial_die_info *pdi,
1201 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1202 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1203
d521ce57 1204static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1205
dee91e82 1206static struct partial_die_info *load_partial_dies
d521ce57 1207 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1208
fb816e8b
TV
1209/* A pair of partial_die_info and compilation unit. */
1210struct cu_partial_die_info
1211{
1212 /* The compilation unit of the partial_die_info. */
1213 struct dwarf2_cu *cu;
1214 /* A partial_die_info. */
1215 struct partial_die_info *pdi;
122cf0f2
AB
1216
1217 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1218 : cu (cu),
1219 pdi (pdi)
405feb71 1220 { /* Nothing. */ }
122cf0f2
AB
1221
1222private:
1223 cu_partial_die_info () = delete;
fb816e8b
TV
1224};
1225
122cf0f2
AB
1226static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1227 struct dwarf2_cu *);
72bf9492 1228
d521ce57
TT
1229static const gdb_byte *read_attribute (const struct die_reader_specs *,
1230 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1231 const gdb_byte *, bool *need_reprocess);
1232
1233static void read_attribute_reprocess (const struct die_reader_specs *reader,
1234 struct attribute *attr);
1235
1236static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1237
c764a876 1238static LONGEST read_checked_initial_length_and_offset
d521ce57 1239 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1240 unsigned int *, unsigned int *);
613e1657 1241
ed2dc618
SM
1242static sect_offset read_abbrev_offset
1243 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1244 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1245
ed2dc618
SM
1246static const char *read_indirect_string
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1248 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1249
ed2dc618
SM
1250static const char *read_indirect_line_string
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1252 const struct comp_unit_head *, unsigned int *);
36586728 1253
ed2dc618
SM
1254static const char *read_indirect_string_at_offset
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1256 LONGEST str_offset);
927aa2e7 1257
ed2dc618
SM
1258static const char *read_indirect_string_from_dwz
1259 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1260
d521ce57
TT
1261static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1262 const gdb_byte *,
3019eac3
DE
1263 unsigned int *);
1264
18a8505e
AT
1265static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1266 ULONGEST str_index);
1267
1268static const char *read_stub_str_index (struct dwarf2_cu *cu,
1269 ULONGEST str_index);
3019eac3 1270
e142c38c 1271static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1272
e142c38c
DJ
1273static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1274 struct dwarf2_cu *);
c906108c 1275
348e048f 1276static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1277 unsigned int);
348e048f 1278
7d45c7c3
KB
1279static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1280 struct dwarf2_cu *cu);
1281
a084a2a6
AT
1282static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1283
05cf31d1
JB
1284static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1285 struct dwarf2_cu *cu);
1286
e142c38c 1287static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1288
e142c38c 1289static struct die_info *die_specification (struct die_info *die,
f2f0e013 1290 struct dwarf2_cu **);
63d06c5c 1291
9c541725 1292static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1293 struct dwarf2_cu *cu);
debd256d 1294
f3f5162e 1295static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1296 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1297 CORE_ADDR, int decode_mapping);
c906108c 1298
804d2729
TT
1299static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1300 const char *);
c906108c 1301
a14ed312 1302static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1303 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1304
ff39bb5e 1305static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1306 struct dwarf2_cu *);
c906108c 1307
ff39bb5e 1308static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1309 struct type *type,
1310 const char *name,
1311 struct obstack *obstack,
12df843f 1312 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1313 const gdb_byte **bytes,
98bfdba5 1314 struct dwarf2_locexpr_baton **baton);
2df3850c 1315
e7c27a73 1316static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1317
b4ba55a1
JB
1318static int need_gnat_info (struct dwarf2_cu *);
1319
3e43a32a
MS
1320static struct type *die_descriptive_type (struct die_info *,
1321 struct dwarf2_cu *);
b4ba55a1
JB
1322
1323static void set_descriptive_type (struct type *, struct die_info *,
1324 struct dwarf2_cu *);
1325
e7c27a73
DJ
1326static struct type *die_containing_type (struct die_info *,
1327 struct dwarf2_cu *);
c906108c 1328
ff39bb5e 1329static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1330 struct dwarf2_cu *);
c906108c 1331
f792889a 1332static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1333
673bfd45
DE
1334static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1335
0d5cff50 1336static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1337
6e70227d 1338static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1339 const char *suffix, int physname,
1340 struct dwarf2_cu *cu);
63d06c5c 1341
e7c27a73 1342static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1343
348e048f
DE
1344static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1345
e7c27a73 1346static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1347
e7c27a73 1348static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1349
96408a79
SA
1350static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1351
71a3c369
TT
1352static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1353
ff013f42 1354static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1355 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1356
3a2b436a 1357/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1358 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1359enum pc_bounds_kind
1360{
e385593e 1361 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1362 PC_BOUNDS_NOT_PRESENT,
1363
e385593e
JK
1364 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1365 were present but they do not form a valid range of PC addresses. */
1366 PC_BOUNDS_INVALID,
1367
3a2b436a
JK
1368 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1369 PC_BOUNDS_RANGES,
1370
1371 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1372 PC_BOUNDS_HIGH_LOW,
1373};
1374
1375static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1376 CORE_ADDR *, CORE_ADDR *,
1377 struct dwarf2_cu *,
891813be 1378 dwarf2_psymtab *);
c906108c 1379
fae299cd
DC
1380static void get_scope_pc_bounds (struct die_info *,
1381 CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *);
1383
801e3a5b
JB
1384static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1385 CORE_ADDR, struct dwarf2_cu *);
1386
a14ed312 1387static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1388 struct dwarf2_cu *);
c906108c 1389
a14ed312 1390static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1391 struct type *, struct dwarf2_cu *);
c906108c 1392
a14ed312 1393static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1394 struct die_info *, struct type *,
e7c27a73 1395 struct dwarf2_cu *);
c906108c 1396
a14ed312 1397static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1398 struct type *,
1399 struct dwarf2_cu *);
c906108c 1400
134d01f1 1401static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1402
e7c27a73 1403static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1404
e7c27a73 1405static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1406
5d7cb8df
JK
1407static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1408
804d2729 1409static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1410
27aa8d6a
SW
1411static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1412
74921315
KS
1413static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1414
f55ee35c
JK
1415static struct type *read_module_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
38d518c9 1418static const char *namespace_name (struct die_info *die,
e142c38c 1419 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1420
134d01f1 1421static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1422
e7c27a73 1423static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1424
6e70227d 1425static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1426 struct dwarf2_cu *);
1427
bf6af496 1428static struct die_info *read_die_and_siblings_1
d521ce57 1429 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1430 struct die_info *);
639d11d3 1431
dee91e82 1432static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1433 const gdb_byte *info_ptr,
1434 const gdb_byte **new_info_ptr,
639d11d3
DC
1435 struct die_info *parent);
1436
d521ce57
TT
1437static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1438 struct die_info **, const gdb_byte *,
3e225074 1439 int);
3019eac3 1440
d521ce57 1441static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1442 struct die_info **, const gdb_byte *);
93311388 1443
e7c27a73 1444static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1445
15d034d0
TT
1446static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1447 struct obstack *);
71c25dea 1448
15d034d0 1449static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1450
15d034d0 1451static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1452 struct die_info *die,
1453 struct dwarf2_cu *cu);
1454
ca69b9e6
DE
1455static const char *dwarf2_physname (const char *name, struct die_info *die,
1456 struct dwarf2_cu *cu);
1457
e142c38c 1458static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1459 struct dwarf2_cu **);
9219021c 1460
f39c6ffd 1461static const char *dwarf_tag_name (unsigned int);
c906108c 1462
f39c6ffd 1463static const char *dwarf_attr_name (unsigned int);
c906108c 1464
f39c6ffd 1465static const char *dwarf_form_name (unsigned int);
c906108c 1466
a121b7c1 1467static const char *dwarf_bool_name (unsigned int);
c906108c 1468
f39c6ffd 1469static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1470
f9aca02d 1471static struct die_info *sibling_die (struct die_info *);
c906108c 1472
d97bc12b
DE
1473static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1474
1475static void dump_die_for_error (struct die_info *);
1476
1477static void dump_die_1 (struct ui_file *, int level, int max_level,
1478 struct die_info *);
c906108c 1479
d97bc12b 1480/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1481
51545339 1482static void store_in_ref_table (struct die_info *,
10b3939b 1483 struct dwarf2_cu *);
c906108c 1484
ff39bb5e 1485static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1486
ff39bb5e 1487static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1488
348e048f 1489static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1490 const struct attribute *,
348e048f
DE
1491 struct dwarf2_cu **);
1492
10b3939b 1493static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1494 const struct attribute *,
f2f0e013 1495 struct dwarf2_cu **);
c906108c 1496
348e048f 1497static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1498 const struct attribute *,
348e048f
DE
1499 struct dwarf2_cu **);
1500
ac9ec31b
DE
1501static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1505 const struct attribute *,
ac9ec31b
DE
1506 struct dwarf2_cu *);
1507
e5fe5e75 1508static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1509
52dc124a 1510static void read_signatured_type (struct signatured_type *);
348e048f 1511
63e43d3a
PMR
1512static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1514 struct dynamic_prop *prop, struct type *type);
63e43d3a 1515
c906108c
SS
1516/* memory allocation interface */
1517
7b5a2f43 1518static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1519
b60c80d6 1520static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1521
43f3e411 1522static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1523
8cf6f0b1
TT
1524static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
ff39bb5e 1526 const struct attribute *attr);
8cf6f0b1 1527
ff39bb5e 1528static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1529 struct symbol *sym,
f1e6e072
TT
1530 struct dwarf2_cu *cu,
1531 int is_block);
4c2df51b 1532
d521ce57
TT
1533static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
4bb7a0a7 1536
72bf9492
DJ
1537static hashval_t partial_die_hash (const void *item);
1538
1539static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
ae038cb0 1541static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1544
9816fde3 1545static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
93311388 1548
ed2dc618 1549static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1550
dee91e82 1551static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1552
f792889a
DJ
1553static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1c379e20 1555
ed2dc618 1556static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1557
ed2dc618 1558static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1559
58f0c718 1560static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1561 enum language);
10b3939b 1562
95554aad
TT
1563static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
10b3939b 1565
f4dc4d17
DE
1566static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
10b3939b
DJ
1569static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
ae038cb0
DJ
1572static void dwarf2_mark (struct dwarf2_cu *);
1573
1574static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
b64f50a1 1576static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1577 struct dwarf2_per_cu_data *);
673bfd45 1578
f792889a 1579static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1580
95554aad
TT
1581static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
ed2dc618 1584static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1585
b303c6f6
AB
1586/* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591class dwarf2_queue_guard
1592{
1593public:
39856def
TT
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
b303c6f6
AB
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
39856def
TT
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->queue, empty);
1606 }
b303c6f6 1607
39856def 1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1609
39856def
TT
1610private:
1611 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1612};
1613
39856def
TT
1614dwarf2_queue_item::~dwarf2_queue_item ()
1615{
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624}
1625
d721ba37
PA
1626/* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629struct file_and_directory
1630{
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643};
1644
1645static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
9291a0cd 1647
298e9637 1648static htab_up allocate_signatured_type_table ();
1fd400ff 1649
298e9637 1650static htab_up allocate_dwo_unit_table ();
3019eac3 1651
57d63ce2 1652static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1655 ULONGEST signature, int is_debug_types);
a2ce51a0 1656
ed2dc618
SM
1657static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1659
3019eac3 1660static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1662
1663static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1664 (struct signatured_type *, const char *, const char *);
3019eac3 1665
89e63ee4
DE
1666static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
263db9a1
TT
1668/* A unique pointer to a dwo_file. */
1669
51ac9db5 1670typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1671
ed2dc618 1672static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1673
1b80a9fa 1674static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1675
1676static void free_line_header_voidp (void *arg);
4390d890
DE
1677\f
1678/* Various complaints about symbol reading that don't abort the process. */
1679
1680static void
1681dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1682{
b98664d3 1683 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1684}
1685
1686static void
1687dwarf2_debug_line_missing_file_complaint (void)
1688{
b98664d3 1689 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1690}
1691
1692static void
1693dwarf2_debug_line_missing_end_sequence_complaint (void)
1694{
b98664d3 1695 complaint (_(".debug_line section has line "
4390d890
DE
1696 "program sequence without an end"));
1697}
1698
1699static void
1700dwarf2_complex_location_expr_complaint (void)
1701{
b98664d3 1702 complaint (_("location expression too complex"));
4390d890
DE
1703}
1704
1705static void
1706dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1707 int arg3)
1708{
b98664d3 1709 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1710 arg1, arg2, arg3);
1711}
1712
1713static void
1714dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1715{
b98664d3 1716 complaint (_("debug info runs off end of %s section"
4390d890 1717 " [in module %s]"),
96b79293
TT
1718 section->get_name (),
1719 section->get_file_name ());
4390d890 1720}
1b80a9fa 1721
4390d890
DE
1722static void
1723dwarf2_macro_malformed_definition_complaint (const char *arg1)
1724{
b98664d3 1725 complaint (_("macro debug info contains a "
4390d890
DE
1726 "malformed macro definition:\n`%s'"),
1727 arg1);
1728}
1729
1730static void
1731dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1732{
b98664d3 1733 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1734 arg1, arg2);
1735}
527f3840
JK
1736
1737/* Hash function for line_header_hash. */
1738
1739static hashval_t
1740line_header_hash (const struct line_header *ofs)
1741{
9c541725 1742 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1743}
1744
1745/* Hash function for htab_create_alloc_ex for line_header_hash. */
1746
1747static hashval_t
1748line_header_hash_voidp (const void *item)
1749{
9a3c8263 1750 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1751
1752 return line_header_hash (ofs);
1753}
1754
1755/* Equality function for line_header_hash. */
1756
1757static int
1758line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1759{
9a3c8263
SM
1760 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1761 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1762
9c541725 1763 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1764 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1765}
1766
4390d890 1767\f
9291a0cd 1768
330cdd98
PA
1769/* See declaration. */
1770
1771dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1772 const dwarf2_debug_sections *names,
1773 bool can_copy_)
1774 : objfile (objfile_),
1775 can_copy (can_copy_)
330cdd98
PA
1776{
1777 if (names == NULL)
1778 names = &dwarf2_elf_names;
1779
1780 bfd *obfd = objfile->obfd;
1781
1782 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1783 locate_sections (obfd, sec, *names);
1784}
1785
1786dwarf2_per_objfile::~dwarf2_per_objfile ()
1787{
1788 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1789 free_cached_comp_units ();
1790
b76e467d 1791 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1792 per_cu->imported_symtabs_free ();
fc8e7e75 1793
b2bdb8cf 1794 for (signatured_type *sig_type : all_type_units)
ae640021 1795 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1796
330cdd98
PA
1797 /* Everything else should be on the objfile obstack. */
1798}
1799
1800/* See declaration. */
1801
1802void
1803dwarf2_per_objfile::free_cached_comp_units ()
1804{
1805 dwarf2_per_cu_data *per_cu = read_in_chain;
1806 dwarf2_per_cu_data **last_chain = &read_in_chain;
1807 while (per_cu != NULL)
1808 {
1809 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1810
fcd3b13d 1811 delete per_cu->cu;
330cdd98
PA
1812 *last_chain = next_cu;
1813 per_cu = next_cu;
1814 }
1815}
1816
11ed8cad
TT
1817/* A helper class that calls free_cached_comp_units on
1818 destruction. */
1819
1820class free_cached_comp_units
1821{
1822public:
1823
1824 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1825 : m_per_objfile (per_objfile)
1826 {
1827 }
1828
1829 ~free_cached_comp_units ()
1830 {
1831 m_per_objfile->free_cached_comp_units ();
1832 }
1833
1834 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1835
1836private:
1837
1838 dwarf2_per_objfile *m_per_objfile;
1839};
1840
c906108c 1841/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1842 information and return true if we have enough to do something.
1843 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1844 ELF names are used. CAN_COPY is true for formats where symbol
1845 interposition is possible and so symbol values must follow copy
1846 relocation rules. */
c906108c
SS
1847
1848int
251d32d9 1849dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1850 const struct dwarf2_debug_sections *names,
1851 bool can_copy)
c906108c 1852{
97cbe998
SDJ
1853 if (objfile->flags & OBJF_READNEVER)
1854 return 0;
1855
ed2dc618
SM
1856 struct dwarf2_per_objfile *dwarf2_per_objfile
1857 = get_dwarf2_per_objfile (objfile);
1858
1859 if (dwarf2_per_objfile == NULL)
5bfd760d 1860 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1861 names,
1862 can_copy);
5bfd760d 1863
73869dc2 1864 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1865 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1866 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1867 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1868}
1869
251d32d9
TG
1870/* When loading sections, we look either for uncompressed section or for
1871 compressed section names. */
233a11ab
CS
1872
1873static int
251d32d9
TG
1874section_is_p (const char *section_name,
1875 const struct dwarf2_section_names *names)
233a11ab 1876{
251d32d9
TG
1877 if (names->normal != NULL
1878 && strcmp (section_name, names->normal) == 0)
1879 return 1;
1880 if (names->compressed != NULL
1881 && strcmp (section_name, names->compressed) == 0)
1882 return 1;
1883 return 0;
233a11ab
CS
1884}
1885
330cdd98 1886/* See declaration. */
c906108c 1887
330cdd98
PA
1888void
1889dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1890 const dwarf2_debug_sections &names)
c906108c 1891{
fd361982 1892 flagword aflag = bfd_section_flags (sectp);
251d32d9 1893
dc7650b8
JK
1894 if ((aflag & SEC_HAS_CONTENTS) == 0)
1895 {
1896 }
950b7495
KS
1897 else if (elf_section_data (sectp)->this_hdr.sh_size
1898 > bfd_get_file_size (abfd))
1899 {
1900 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1901 warning (_("Discarding section %s which has a section size (%s"
1902 ") larger than the file size [in module %s]"),
1903 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1904 bfd_get_filename (abfd));
1905 }
330cdd98 1906 else if (section_is_p (sectp->name, &names.info))
c906108c 1907 {
330cdd98 1908 this->info.s.section = sectp;
fd361982 1909 this->info.size = bfd_section_size (sectp);
c906108c 1910 }
330cdd98 1911 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1912 {
330cdd98 1913 this->abbrev.s.section = sectp;
fd361982 1914 this->abbrev.size = bfd_section_size (sectp);
c906108c 1915 }
330cdd98 1916 else if (section_is_p (sectp->name, &names.line))
c906108c 1917 {
330cdd98 1918 this->line.s.section = sectp;
fd361982 1919 this->line.size = bfd_section_size (sectp);
c906108c 1920 }
330cdd98 1921 else if (section_is_p (sectp->name, &names.loc))
c906108c 1922 {
330cdd98 1923 this->loc.s.section = sectp;
fd361982 1924 this->loc.size = bfd_section_size (sectp);
c906108c 1925 }
330cdd98 1926 else if (section_is_p (sectp->name, &names.loclists))
43988095 1927 {
330cdd98 1928 this->loclists.s.section = sectp;
fd361982 1929 this->loclists.size = bfd_section_size (sectp);
43988095 1930 }
330cdd98 1931 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1932 {
330cdd98 1933 this->macinfo.s.section = sectp;
fd361982 1934 this->macinfo.size = bfd_section_size (sectp);
c906108c 1935 }
330cdd98 1936 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1937 {
330cdd98 1938 this->macro.s.section = sectp;
fd361982 1939 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1940 }
330cdd98 1941 else if (section_is_p (sectp->name, &names.str))
c906108c 1942 {
330cdd98 1943 this->str.s.section = sectp;
fd361982 1944 this->str.size = bfd_section_size (sectp);
c906108c 1945 }
18a8505e
AT
1946 else if (section_is_p (sectp->name, &names.str_offsets))
1947 {
1948 this->str_offsets.s.section = sectp;
1949 this->str_offsets.size = bfd_section_size (sectp);
1950 }
330cdd98 1951 else if (section_is_p (sectp->name, &names.line_str))
43988095 1952 {
330cdd98 1953 this->line_str.s.section = sectp;
fd361982 1954 this->line_str.size = bfd_section_size (sectp);
43988095 1955 }
330cdd98 1956 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1957 {
330cdd98 1958 this->addr.s.section = sectp;
fd361982 1959 this->addr.size = bfd_section_size (sectp);
3019eac3 1960 }
330cdd98 1961 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1962 {
330cdd98 1963 this->frame.s.section = sectp;
fd361982 1964 this->frame.size = bfd_section_size (sectp);
b6af0555 1965 }
330cdd98 1966 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1967 {
330cdd98 1968 this->eh_frame.s.section = sectp;
fd361982 1969 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1970 }
330cdd98 1971 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1972 {
330cdd98 1973 this->ranges.s.section = sectp;
fd361982 1974 this->ranges.size = bfd_section_size (sectp);
af34e669 1975 }
330cdd98 1976 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1977 {
330cdd98 1978 this->rnglists.s.section = sectp;
fd361982 1979 this->rnglists.size = bfd_section_size (sectp);
43988095 1980 }
330cdd98 1981 else if (section_is_p (sectp->name, &names.types))
348e048f 1982 {
8b70b953
TT
1983 struct dwarf2_section_info type_section;
1984
1985 memset (&type_section, 0, sizeof (type_section));
049412e3 1986 type_section.s.section = sectp;
fd361982 1987 type_section.size = bfd_section_size (sectp);
8b70b953 1988
fd5866f6 1989 this->types.push_back (type_section);
348e048f 1990 }
330cdd98 1991 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1992 {
330cdd98 1993 this->gdb_index.s.section = sectp;
fd361982 1994 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1995 }
927aa2e7
JK
1996 else if (section_is_p (sectp->name, &names.debug_names))
1997 {
1998 this->debug_names.s.section = sectp;
fd361982 1999 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2000 }
2001 else if (section_is_p (sectp->name, &names.debug_aranges))
2002 {
2003 this->debug_aranges.s.section = sectp;
fd361982 2004 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2005 }
dce234bc 2006
fd361982
AM
2007 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2008 && bfd_section_vma (sectp) == 0)
330cdd98 2009 this->has_section_at_zero = true;
c906108c
SS
2010}
2011
dce234bc 2012/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2013 SECTION_NAME. */
af34e669 2014
dce234bc 2015void
3017a003
TG
2016dwarf2_get_section_info (struct objfile *objfile,
2017 enum dwarf2_section_enum sect,
d521ce57 2018 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2019 bfd_size_type *sizep)
2020{
5bfd760d 2021 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2022 struct dwarf2_section_info *info;
a3b2a86b
TT
2023
2024 /* We may see an objfile without any DWARF, in which case we just
2025 return nothing. */
2026 if (data == NULL)
2027 {
2028 *sectp = NULL;
2029 *bufp = NULL;
2030 *sizep = 0;
2031 return;
2032 }
3017a003
TG
2033 switch (sect)
2034 {
2035 case DWARF2_DEBUG_FRAME:
2036 info = &data->frame;
2037 break;
2038 case DWARF2_EH_FRAME:
2039 info = &data->eh_frame;
2040 break;
2041 default:
2042 gdb_assert_not_reached ("unexpected section");
2043 }
dce234bc 2044
96b79293 2045 info->read (objfile);
dce234bc 2046
96b79293 2047 *sectp = info->get_bfd_section ();
dce234bc
PP
2048 *bufp = info->buffer;
2049 *sizep = info->size;
2050}
2051
36586728
TT
2052/* A helper function to find the sections for a .dwz file. */
2053
2054static void
2055locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2056{
9a3c8263 2057 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2058
2059 /* Note that we only support the standard ELF names, because .dwz
2060 is ELF-only (at the time of writing). */
2061 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2062 {
049412e3 2063 dwz_file->abbrev.s.section = sectp;
fd361982 2064 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2065 }
2066 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2067 {
049412e3 2068 dwz_file->info.s.section = sectp;
fd361982 2069 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2070 }
2071 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2072 {
049412e3 2073 dwz_file->str.s.section = sectp;
fd361982 2074 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2075 }
2076 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2077 {
049412e3 2078 dwz_file->line.s.section = sectp;
fd361982 2079 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2080 }
2081 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2082 {
049412e3 2083 dwz_file->macro.s.section = sectp;
fd361982 2084 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2085 }
2ec9a5e0
TT
2086 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2087 {
049412e3 2088 dwz_file->gdb_index.s.section = sectp;
fd361982 2089 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2090 }
927aa2e7
JK
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2092 {
2093 dwz_file->debug_names.s.section = sectp;
fd361982 2094 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2095 }
36586728
TT
2096}
2097
c4973306 2098/* See dwarf2read.h. */
36586728 2099
c4973306 2100struct dwz_file *
ed2dc618 2101dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2102{
36586728 2103 const char *filename;
acd13123 2104 bfd_size_type buildid_len_arg;
dc294be5
TT
2105 size_t buildid_len;
2106 bfd_byte *buildid;
36586728
TT
2107
2108 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2109 return dwarf2_per_objfile->dwz_file.get ();
36586728 2110
4db1a1dc 2111 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2112 gdb::unique_xmalloc_ptr<char> data
2113 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2114 &buildid_len_arg, &buildid));
4db1a1dc
TT
2115 if (data == NULL)
2116 {
2117 if (bfd_get_error () == bfd_error_no_error)
2118 return NULL;
2119 error (_("could not read '.gnu_debugaltlink' section: %s"),
2120 bfd_errmsg (bfd_get_error ()));
2121 }
791afaa2
TT
2122
2123 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2124
acd13123
TT
2125 buildid_len = (size_t) buildid_len_arg;
2126
791afaa2 2127 filename = data.get ();
d721ba37
PA
2128
2129 std::string abs_storage;
36586728
TT
2130 if (!IS_ABSOLUTE_PATH (filename))
2131 {
14278e1f
TT
2132 gdb::unique_xmalloc_ptr<char> abs
2133 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2134
14278e1f 2135 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2136 filename = abs_storage.c_str ();
36586728
TT
2137 }
2138
dc294be5
TT
2139 /* First try the file name given in the section. If that doesn't
2140 work, try to use the build-id instead. */
192b62ce 2141 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2142 if (dwz_bfd != NULL)
36586728 2143 {
192b62ce 2144 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2145 dwz_bfd.reset (nullptr);
36586728
TT
2146 }
2147
dc294be5
TT
2148 if (dwz_bfd == NULL)
2149 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2150
0d79cdc4
AM
2151 if (dwz_bfd == nullptr)
2152 {
2153 gdb::unique_xmalloc_ptr<char> alt_filename;
2154 const char *origname = dwarf2_per_objfile->objfile->original_name;
2155
2156 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2157 buildid_len,
2158 origname,
2159 &alt_filename));
2160
2161 if (fd.get () >= 0)
2162 {
2163 /* File successfully retrieved from server. */
2164 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2165
2166 if (dwz_bfd == nullptr)
2167 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2168 alt_filename.get ());
2169 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2170 dwz_bfd.reset (nullptr);
2171 }
2172 }
2173
dc294be5
TT
2174 if (dwz_bfd == NULL)
2175 error (_("could not find '.gnu_debugaltlink' file for %s"),
2176 objfile_name (dwarf2_per_objfile->objfile));
2177
7ff8cb8c
TT
2178 std::unique_ptr<struct dwz_file> result
2179 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2180
7ff8cb8c
TT
2181 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2182 result.get ());
36586728 2183
7ff8cb8c
TT
2184 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2185 result->dwz_bfd.get ());
2186 dwarf2_per_objfile->dwz_file = std::move (result);
2187 return dwarf2_per_objfile->dwz_file.get ();
36586728 2188}
9291a0cd 2189\f
7b9f3c50
DE
2190/* DWARF quick_symbols_functions support. */
2191
2192/* TUs can share .debug_line entries, and there can be a lot more TUs than
2193 unique line tables, so we maintain a separate table of all .debug_line
2194 derived entries to support the sharing.
2195 All the quick functions need is the list of file names. We discard the
2196 line_header when we're done and don't need to record it here. */
2197struct quick_file_names
2198{
094b34ac
DE
2199 /* The data used to construct the hash key. */
2200 struct stmt_list_hash hash;
7b9f3c50
DE
2201
2202 /* The number of entries in file_names, real_names. */
2203 unsigned int num_file_names;
2204
2205 /* The file names from the line table, after being run through
2206 file_full_name. */
2207 const char **file_names;
2208
2209 /* The file names from the line table after being run through
2210 gdb_realpath. These are computed lazily. */
2211 const char **real_names;
2212};
2213
2214/* When using the index (and thus not using psymtabs), each CU has an
2215 object of this type. This is used to hold information needed by
2216 the various "quick" methods. */
2217struct dwarf2_per_cu_quick_data
2218{
2219 /* The file table. This can be NULL if there was no file table
2220 or it's currently not read in.
2221 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2222 struct quick_file_names *file_names;
2223
2224 /* The corresponding symbol table. This is NULL if symbols for this
2225 CU have not yet been read. */
43f3e411 2226 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2227
2228 /* A temporary mark bit used when iterating over all CUs in
2229 expand_symtabs_matching. */
2230 unsigned int mark : 1;
2231
2232 /* True if we've tried to read the file table and found there isn't one.
2233 There will be no point in trying to read it again next time. */
2234 unsigned int no_file_data : 1;
2235};
2236
094b34ac
DE
2237/* Utility hash function for a stmt_list_hash. */
2238
2239static hashval_t
2240hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2241{
2242 hashval_t v = 0;
2243
2244 if (stmt_list_hash->dwo_unit != NULL)
2245 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2246 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2247 return v;
2248}
2249
2250/* Utility equality function for a stmt_list_hash. */
2251
2252static int
2253eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2254 const struct stmt_list_hash *rhs)
2255{
2256 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2257 return 0;
2258 if (lhs->dwo_unit != NULL
2259 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2260 return 0;
2261
9c541725 2262 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2263}
2264
7b9f3c50
DE
2265/* Hash function for a quick_file_names. */
2266
2267static hashval_t
2268hash_file_name_entry (const void *e)
2269{
9a3c8263
SM
2270 const struct quick_file_names *file_data
2271 = (const struct quick_file_names *) e;
7b9f3c50 2272
094b34ac 2273 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2274}
2275
2276/* Equality function for a quick_file_names. */
2277
2278static int
2279eq_file_name_entry (const void *a, const void *b)
2280{
9a3c8263
SM
2281 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2282 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2283
094b34ac 2284 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2285}
2286
2287/* Delete function for a quick_file_names. */
2288
2289static void
2290delete_file_name_entry (void *e)
2291{
9a3c8263 2292 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2293 int i;
2294
2295 for (i = 0; i < file_data->num_file_names; ++i)
2296 {
2297 xfree ((void*) file_data->file_names[i]);
2298 if (file_data->real_names)
2299 xfree ((void*) file_data->real_names[i]);
2300 }
2301
2302 /* The space for the struct itself lives on objfile_obstack,
2303 so we don't free it here. */
2304}
2305
2306/* Create a quick_file_names hash table. */
2307
5895093f 2308static htab_up
7b9f3c50
DE
2309create_quick_file_names_table (unsigned int nr_initial_entries)
2310{
5895093f
TT
2311 return htab_up (htab_create_alloc (nr_initial_entries,
2312 hash_file_name_entry, eq_file_name_entry,
2313 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2314}
9291a0cd 2315
918dd910
JK
2316/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2317 have to be created afterwards. You should call age_cached_comp_units after
2318 processing PER_CU->CU. dw2_setup must have been already called. */
2319
2320static void
58f0c718 2321load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2322{
3019eac3 2323 if (per_cu->is_debug_types)
e5fe5e75 2324 load_full_type_unit (per_cu);
918dd910 2325 else
58f0c718 2326 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2327
cc12ce38
DE
2328 if (per_cu->cu == NULL)
2329 return; /* Dummy CU. */
2dc860c0
DE
2330
2331 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2332}
2333
a0f42c21 2334/* Read in the symbols for PER_CU. */
2fdf6df6 2335
9291a0cd 2336static void
58f0c718 2337dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2338{
ed2dc618 2339 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2340
f4dc4d17
DE
2341 /* Skip type_unit_groups, reading the type units they contain
2342 is handled elsewhere. */
197400e8 2343 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2344 return;
2345
b303c6f6
AB
2346 /* The destructor of dwarf2_queue_guard frees any entries left on
2347 the queue. After this point we're guaranteed to leave this function
2348 with the dwarf queue empty. */
39856def 2349 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2350
95554aad 2351 if (dwarf2_per_objfile->using_index
43f3e411 2352 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2353 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2354 {
2355 queue_comp_unit (per_cu, language_minimal);
58f0c718 2356 load_cu (per_cu, skip_partial);
89e63ee4
DE
2357
2358 /* If we just loaded a CU from a DWO, and we're working with an index
2359 that may badly handle TUs, load all the TUs in that DWO as well.
2360 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2361 if (!per_cu->is_debug_types
cc12ce38 2362 && per_cu->cu != NULL
89e63ee4
DE
2363 && per_cu->cu->dwo_unit != NULL
2364 && dwarf2_per_objfile->index_table != NULL
2365 && dwarf2_per_objfile->index_table->version <= 7
2366 /* DWP files aren't supported yet. */
ed2dc618 2367 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2368 queue_and_load_all_dwo_tus (per_cu);
95554aad 2369 }
9291a0cd 2370
ed2dc618 2371 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2372
2373 /* Age the cache, releasing compilation units that have not
2374 been used recently. */
ed2dc618 2375 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2376}
2377
2378/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2379 the objfile from which this CU came. Returns the resulting symbol
2380 table. */
2fdf6df6 2381
43f3e411 2382static struct compunit_symtab *
58f0c718 2383dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2384{
ed2dc618
SM
2385 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2386
95554aad 2387 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2388 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2389 {
11ed8cad 2390 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2391 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2392 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2393 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2394 }
f194fefb 2395
43f3e411 2396 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2397}
2398
ff4c9fec 2399/* See declaration. */
f4dc4d17 2400
ff4c9fec
SM
2401dwarf2_per_cu_data *
2402dwarf2_per_objfile::get_cutu (int index)
2403{
b76e467d 2404 if (index >= this->all_comp_units.size ())
ff4c9fec 2405 {
b76e467d 2406 index -= this->all_comp_units.size ();
b2bdb8cf 2407 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2408 return &this->all_type_units[index]->per_cu;
2409 }
f4dc4d17 2410
ff4c9fec
SM
2411 return this->all_comp_units[index];
2412}
f4dc4d17 2413
ff4c9fec 2414/* See declaration. */
2fdf6df6 2415
ff4c9fec
SM
2416dwarf2_per_cu_data *
2417dwarf2_per_objfile::get_cu (int index)
1fd400ff 2418{
b76e467d 2419 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2420
ff4c9fec 2421 return this->all_comp_units[index];
f4dc4d17
DE
2422}
2423
ff4c9fec 2424/* See declaration. */
f4dc4d17 2425
ff4c9fec
SM
2426signatured_type *
2427dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2428{
b2bdb8cf 2429 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2430
ff4c9fec 2431 return this->all_type_units[index];
1fd400ff
TT
2432}
2433
4b514bc8
JK
2434/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2435 objfile_obstack, and constructed with the specified field
2436 values. */
2437
2438static dwarf2_per_cu_data *
ed2dc618 2439create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2440 struct dwarf2_section_info *section,
2441 int is_dwz,
2442 sect_offset sect_off, ULONGEST length)
2443{
ed2dc618 2444 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2445 dwarf2_per_cu_data *the_cu
2446 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2447 struct dwarf2_per_cu_data);
2448 the_cu->sect_off = sect_off;
2449 the_cu->length = length;
e3b94546 2450 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2451 the_cu->section = section;
2452 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2453 struct dwarf2_per_cu_quick_data);
2454 the_cu->is_dwz = is_dwz;
2455 return the_cu;
2456}
2457
2ec9a5e0
TT
2458/* A helper for create_cus_from_index that handles a given list of
2459 CUs. */
2fdf6df6 2460
74a0d9f6 2461static void
12359b5e 2462create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2463 const gdb_byte *cu_list, offset_type n_elements,
2464 struct dwarf2_section_info *section,
b76e467d 2465 int is_dwz)
9291a0cd 2466{
12359b5e 2467 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2468 {
74a0d9f6 2469 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2470
2471 sect_offset sect_off
2472 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2473 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2474 cu_list += 2 * 8;
2475
b76e467d 2476 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2477 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2478 sect_off, length);
b76e467d 2479 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2480 }
9291a0cd
TT
2481}
2482
2ec9a5e0 2483/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2484 the CU objects for this objfile. */
2ec9a5e0 2485
74a0d9f6 2486static void
12359b5e 2487create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2488 const gdb_byte *cu_list, offset_type cu_list_elements,
2489 const gdb_byte *dwz_list, offset_type dwz_elements)
2490{
b76e467d
SM
2491 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2492 dwarf2_per_objfile->all_comp_units.reserve
2493 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2494
12359b5e 2495 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2496 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2497
2498 if (dwz_elements == 0)
74a0d9f6 2499 return;
2ec9a5e0 2500
12359b5e
SM
2501 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2502 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2503 &dwz->info, 1);
2ec9a5e0
TT
2504}
2505
1fd400ff 2506/* Create the signatured type hash table from the index. */
673bfd45 2507
74a0d9f6 2508static void
12359b5e
SM
2509create_signatured_type_table_from_index
2510 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2511 struct dwarf2_section_info *section,
2512 const gdb_byte *bytes,
2513 offset_type elements)
1fd400ff 2514{
12359b5e 2515 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2516
b2bdb8cf
SM
2517 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2518 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2519
298e9637 2520 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2521
12359b5e 2522 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2523 {
52dc124a 2524 struct signatured_type *sig_type;
9c541725 2525 ULONGEST signature;
1fd400ff 2526 void **slot;
9c541725 2527 cu_offset type_offset_in_tu;
1fd400ff 2528
74a0d9f6 2529 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2530 sect_offset sect_off
2531 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2532 type_offset_in_tu
2533 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2534 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2535 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2536 bytes += 3 * 8;
2537
52dc124a 2538 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2539 struct signatured_type);
52dc124a 2540 sig_type->signature = signature;
9c541725 2541 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2542 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2543 sig_type->per_cu.section = section;
9c541725 2544 sig_type->per_cu.sect_off = sect_off;
e3b94546 2545 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2546 sig_type->per_cu.v.quick
1fd400ff
TT
2547 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2548 struct dwarf2_per_cu_quick_data);
2549
b0b6a987 2550 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2551 *slot = sig_type;
1fd400ff 2552
b2bdb8cf 2553 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2554 }
2555
b0b6a987 2556 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2557}
2558
927aa2e7
JK
2559/* Create the signatured type hash table from .debug_names. */
2560
2561static void
2562create_signatured_type_table_from_debug_names
ed2dc618 2563 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2564 const mapped_debug_names &map,
2565 struct dwarf2_section_info *section,
2566 struct dwarf2_section_info *abbrev_section)
2567{
ed2dc618
SM
2568 struct objfile *objfile = dwarf2_per_objfile->objfile;
2569
96b79293
TT
2570 section->read (objfile);
2571 abbrev_section->read (objfile);
927aa2e7 2572
b2bdb8cf
SM
2573 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2574 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2575
298e9637 2576 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2577
2578 for (uint32_t i = 0; i < map.tu_count; ++i)
2579 {
2580 struct signatured_type *sig_type;
927aa2e7 2581 void **slot;
927aa2e7
JK
2582
2583 sect_offset sect_off
2584 = (sect_offset) (extract_unsigned_integer
2585 (map.tu_table_reordered + i * map.offset_size,
2586 map.offset_size,
2587 map.dwarf5_byte_order));
2588
2589 comp_unit_head cu_header;
ed2dc618
SM
2590 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2591 abbrev_section,
927aa2e7
JK
2592 section->buffer + to_underlying (sect_off),
2593 rcuh_kind::TYPE);
2594
2595 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2596 struct signatured_type);
2597 sig_type->signature = cu_header.signature;
2598 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2599 sig_type->per_cu.is_debug_types = 1;
2600 sig_type->per_cu.section = section;
2601 sig_type->per_cu.sect_off = sect_off;
e3b94546 2602 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2603 sig_type->per_cu.v.quick
2604 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2605 struct dwarf2_per_cu_quick_data);
2606
b0b6a987 2607 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2608 *slot = sig_type;
2609
b2bdb8cf 2610 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2611 }
2612
b0b6a987 2613 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2614}
2615
9291a0cd
TT
2616/* Read the address map data from the mapped index, and use it to
2617 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2618
9291a0cd 2619static void
ed2dc618
SM
2620create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2621 struct mapped_index *index)
9291a0cd 2622{
ed2dc618 2623 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2624 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2625 const gdb_byte *iter, *end;
9291a0cd 2626 struct addrmap *mutable_map;
9291a0cd
TT
2627 CORE_ADDR baseaddr;
2628
8268c778
PA
2629 auto_obstack temp_obstack;
2630
9291a0cd
TT
2631 mutable_map = addrmap_create_mutable (&temp_obstack);
2632
f00a2de2
PA
2633 iter = index->address_table.data ();
2634 end = iter + index->address_table.size ();
9291a0cd 2635
b3b3bada 2636 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2637
2638 while (iter < end)
2639 {
2640 ULONGEST hi, lo, cu_index;
2641 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2642 iter += 8;
2643 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2644 iter += 8;
2645 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2646 iter += 4;
f652bce2 2647
24a55014 2648 if (lo > hi)
f652bce2 2649 {
b98664d3 2650 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2651 hex_string (lo), hex_string (hi));
24a55014 2652 continue;
f652bce2 2653 }
24a55014 2654
b76e467d 2655 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2656 {
b98664d3 2657 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2658 (unsigned) cu_index);
24a55014 2659 continue;
f652bce2 2660 }
24a55014 2661
79748972
TT
2662 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2663 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2664 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2665 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2666 }
2667
d320c2b5 2668 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2669 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2670}
2671
927aa2e7
JK
2672/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2673 populate the objfile's psymtabs_addrmap. */
2674
2675static void
ed2dc618 2676create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2677 struct dwarf2_section_info *section)
2678{
ed2dc618 2679 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2680 bfd *abfd = objfile->obfd;
2681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2682 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2683
2684 auto_obstack temp_obstack;
2685 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2686
2687 std::unordered_map<sect_offset,
2688 dwarf2_per_cu_data *,
2689 gdb::hash_enum<sect_offset>>
2690 debug_info_offset_to_per_cu;
b76e467d 2691 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2692 {
927aa2e7
JK
2693 const auto insertpair
2694 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2695 if (!insertpair.second)
2696 {
2697 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2698 "debug_info_offset %s, ignoring .debug_aranges."),
2699 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2700 return;
2701 }
2702 }
2703
96b79293 2704 section->read (objfile);
927aa2e7
JK
2705
2706 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2707
2708 const gdb_byte *addr = section->buffer;
2709
2710 while (addr < section->buffer + section->size)
2711 {
2712 const gdb_byte *const entry_addr = addr;
2713 unsigned int bytes_read;
2714
2715 const LONGEST entry_length = read_initial_length (abfd, addr,
2716 &bytes_read);
2717 addr += bytes_read;
2718
2719 const gdb_byte *const entry_end = addr + entry_length;
2720 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2721 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2722 if (addr + entry_length > section->buffer + section->size)
2723 {
47e3f474 2724 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2725 "length %s exceeds section length %s, "
2726 "ignoring .debug_aranges."),
47e3f474
TV
2727 objfile_name (objfile),
2728 plongest (entry_addr - section->buffer),
927aa2e7
JK
2729 plongest (bytes_read + entry_length),
2730 pulongest (section->size));
2731 return;
2732 }
2733
2734 /* The version number. */
2735 const uint16_t version = read_2_bytes (abfd, addr);
2736 addr += 2;
2737 if (version != 2)
2738 {
47e3f474 2739 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2740 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2743 return;
2744 }
2745
2746 const uint64_t debug_info_offset
2747 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2748 addr += offset_size;
2749 const auto per_cu_it
2750 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2751 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2752 {
47e3f474 2753 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2754 "debug_info_offset %s does not exists, "
2755 "ignoring .debug_aranges."),
47e3f474
TV
2756 objfile_name (objfile),
2757 plongest (entry_addr - section->buffer),
927aa2e7
JK
2758 pulongest (debug_info_offset));
2759 return;
2760 }
2761 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2762
2763 const uint8_t address_size = *addr++;
2764 if (address_size < 1 || address_size > 8)
2765 {
47e3f474 2766 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2767 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2768 objfile_name (objfile),
2769 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2770 return;
2771 }
2772
2773 const uint8_t segment_selector_size = *addr++;
2774 if (segment_selector_size != 0)
2775 {
47e3f474 2776 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2777 "segment_selector_size %u is not supported, "
2778 "ignoring .debug_aranges."),
47e3f474
TV
2779 objfile_name (objfile),
2780 plongest (entry_addr - section->buffer),
927aa2e7
JK
2781 segment_selector_size);
2782 return;
2783 }
2784
2785 /* Must pad to an alignment boundary that is twice the address
2786 size. It is undocumented by the DWARF standard but GCC does
2787 use it. */
2788 for (size_t padding = ((-(addr - section->buffer))
2789 & (2 * address_size - 1));
2790 padding > 0; padding--)
2791 if (*addr++ != 0)
2792 {
47e3f474 2793 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2794 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2795 objfile_name (objfile),
2796 plongest (entry_addr - section->buffer));
927aa2e7
JK
2797 return;
2798 }
2799
2800 for (;;)
2801 {
2802 if (addr + 2 * address_size > entry_end)
2803 {
47e3f474 2804 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2805 "address list is not properly terminated, "
2806 "ignoring .debug_aranges."),
47e3f474
TV
2807 objfile_name (objfile),
2808 plongest (entry_addr - section->buffer));
927aa2e7
JK
2809 return;
2810 }
2811 ULONGEST start = extract_unsigned_integer (addr, address_size,
2812 dwarf5_byte_order);
2813 addr += address_size;
2814 ULONGEST length = extract_unsigned_integer (addr, address_size,
2815 dwarf5_byte_order);
2816 addr += address_size;
2817 if (start == 0 && length == 0)
2818 break;
2819 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2820 {
2821 /* Symbol was eliminated due to a COMDAT group. */
2822 continue;
2823 }
2824 ULONGEST end = start + length;
79748972
TT
2825 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2826 - baseaddr);
2827 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2828 - baseaddr);
927aa2e7
JK
2829 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2830 }
2831 }
2832
d320c2b5 2833 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2834 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2835}
2836
9291a0cd
TT
2837/* Find a slot in the mapped index INDEX for the object named NAME.
2838 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2839 constant pool and return true. If NAME cannot be found, return
2840 false. */
2fdf6df6 2841
109483d9 2842static bool
9291a0cd
TT
2843find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2844 offset_type **vec_out)
2845{
0cf03b49 2846 offset_type hash;
9291a0cd 2847 offset_type slot, step;
559a7a62 2848 int (*cmp) (const char *, const char *);
9291a0cd 2849
791afaa2 2850 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2851 if (current_language->la_language == language_cplus
45280282
IB
2852 || current_language->la_language == language_fortran
2853 || current_language->la_language == language_d)
0cf03b49
JK
2854 {
2855 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2856 not contain any. */
a8719064 2857
72998fb3 2858 if (strchr (name, '(') != NULL)
0cf03b49 2859 {
109483d9 2860 without_params = cp_remove_params (name);
0cf03b49 2861
72998fb3 2862 if (without_params != NULL)
791afaa2 2863 name = without_params.get ();
0cf03b49
JK
2864 }
2865 }
2866
559a7a62 2867 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2868 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2869 simulate our NAME being searched is also lowercased. */
2870 hash = mapped_index_string_hash ((index->version == 4
2871 && case_sensitivity == case_sensitive_off
2872 ? 5 : index->version),
2873 name);
2874
f00a2de2
PA
2875 slot = hash & (index->symbol_table.size () - 1);
2876 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2877 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2878
2879 for (;;)
2880 {
9291a0cd 2881 const char *str;
f00a2de2
PA
2882
2883 const auto &bucket = index->symbol_table[slot];
2884 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2885 return false;
9291a0cd 2886
f00a2de2 2887 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2888 if (!cmp (name, str))
9291a0cd
TT
2889 {
2890 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2891 + MAYBE_SWAP (bucket.vec));
109483d9 2892 return true;
9291a0cd
TT
2893 }
2894
f00a2de2 2895 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2896 }
2897}
2898
4485a1c1
SM
2899/* A helper function that reads the .gdb_index from BUFFER and fills
2900 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2901 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2902 ok to use deprecated sections.
2903
2904 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2905 out parameters that are filled in with information about the CU and
2906 TU lists in the section.
2907
4485a1c1 2908 Returns true if all went well, false otherwise. */
2fdf6df6 2909
d33bc52e 2910static bool
4485a1c1
SM
2911read_gdb_index_from_buffer (struct objfile *objfile,
2912 const char *filename,
2913 bool deprecated_ok,
2914 gdb::array_view<const gdb_byte> buffer,
2915 struct mapped_index *map,
2916 const gdb_byte **cu_list,
2917 offset_type *cu_list_elements,
2918 const gdb_byte **types_list,
2919 offset_type *types_list_elements)
2920{
2921 const gdb_byte *addr = &buffer[0];
82430852 2922
9291a0cd 2923 /* Version check. */
4485a1c1 2924 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2925 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2926 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2927 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2928 indices. */
831adc1f 2929 if (version < 4)
481860b3
GB
2930 {
2931 static int warning_printed = 0;
2932 if (!warning_printed)
2933 {
2934 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2935 filename);
481860b3
GB
2936 warning_printed = 1;
2937 }
2938 return 0;
2939 }
2940 /* Index version 4 uses a different hash function than index version
2941 5 and later.
2942
2943 Versions earlier than 6 did not emit psymbols for inlined
2944 functions. Using these files will cause GDB not to be able to
2945 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2946 indices unless the user has done
2947 "set use-deprecated-index-sections on". */
2ec9a5e0 2948 if (version < 6 && !deprecated_ok)
481860b3
GB
2949 {
2950 static int warning_printed = 0;
2951 if (!warning_printed)
2952 {
e615022a
DE
2953 warning (_("\
2954Skipping deprecated .gdb_index section in %s.\n\
2955Do \"set use-deprecated-index-sections on\" before the file is read\n\
2956to use the section anyway."),
2ec9a5e0 2957 filename);
481860b3
GB
2958 warning_printed = 1;
2959 }
2960 return 0;
2961 }
796a7ff8 2962 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2963 of the TU (for symbols coming from TUs),
2964 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2965 Plus gold-generated indices can have duplicate entries for global symbols,
2966 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2967 These are just performance bugs, and we can't distinguish gdb-generated
2968 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2969
481860b3 2970 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2971 longer backward compatible. */
796a7ff8 2972 if (version > 8)
594e8718 2973 return 0;
9291a0cd 2974
559a7a62 2975 map->version = version;
9291a0cd 2976
4485a1c1 2977 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2978
4485a1c1 2979 int i = 0;
2ec9a5e0
TT
2980 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2981 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2982 / 8);
1fd400ff
TT
2983 ++i;
2984
2ec9a5e0
TT
2985 *types_list = addr + MAYBE_SWAP (metadata[i]);
2986 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2987 - MAYBE_SWAP (metadata[i]))
2988 / 8);
987d643c 2989 ++i;
1fd400ff 2990
f00a2de2
PA
2991 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2992 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2993 map->address_table
2994 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2995 ++i;
2996
f00a2de2
PA
2997 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2998 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2999 map->symbol_table
3000 = gdb::array_view<mapped_index::symbol_table_slot>
3001 ((mapped_index::symbol_table_slot *) symbol_table,
3002 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3003
f00a2de2 3004 ++i;
f9d83a0b 3005 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3006
2ec9a5e0
TT
3007 return 1;
3008}
3009
4485a1c1
SM
3010/* Callback types for dwarf2_read_gdb_index. */
3011
3012typedef gdb::function_view
3013 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3014 get_gdb_index_contents_ftype;
3015typedef gdb::function_view
3016 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3017 get_gdb_index_contents_dwz_ftype;
3018
927aa2e7 3019/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3020 elements of all the CUs and return 1. Otherwise, return 0. */
3021
3022static int
4485a1c1
SM
3023dwarf2_read_gdb_index
3024 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3025 get_gdb_index_contents_ftype get_gdb_index_contents,
3026 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3027{
2ec9a5e0
TT
3028 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3029 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3030 struct dwz_file *dwz;
12359b5e 3031 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3032
4485a1c1
SM
3033 gdb::array_view<const gdb_byte> main_index_contents
3034 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3035
3036 if (main_index_contents.empty ())
3037 return 0;
3038
3063847f 3039 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3040 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3041 use_deprecated_index_sections,
3042 main_index_contents, map.get (), &cu_list,
3043 &cu_list_elements, &types_list,
3044 &types_list_elements))
2ec9a5e0
TT
3045 return 0;
3046
0fefef59 3047 /* Don't use the index if it's empty. */
3063847f 3048 if (map->symbol_table.empty ())
0fefef59
DE
3049 return 0;
3050
2ec9a5e0
TT
3051 /* If there is a .dwz file, read it so we can get its CU list as
3052 well. */
ed2dc618 3053 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3054 if (dwz != NULL)
2ec9a5e0 3055 {
2ec9a5e0
TT
3056 struct mapped_index dwz_map;
3057 const gdb_byte *dwz_types_ignore;
3058 offset_type dwz_types_elements_ignore;
3059
4485a1c1
SM
3060 gdb::array_view<const gdb_byte> dwz_index_content
3061 = get_gdb_index_contents_dwz (objfile, dwz);
3062
3063 if (dwz_index_content.empty ())
3064 return 0;
3065
3066 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3067 bfd_get_filename (dwz->dwz_bfd.get ()),
3068 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3069 &dwz_list, &dwz_list_elements,
3070 &dwz_types_ignore,
3071 &dwz_types_elements_ignore))
2ec9a5e0
TT
3072 {
3073 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3074 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3075 return 0;
3076 }
3077 }
3078
12359b5e
SM
3079 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3080 dwz_list, dwz_list_elements);
1fd400ff 3081
8b70b953
TT
3082 if (types_list_elements)
3083 {
8b70b953
TT
3084 /* We can only handle a single .debug_types when we have an
3085 index. */
fd5866f6 3086 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3087 return 0;
3088
fd5866f6 3089 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3090
12359b5e
SM
3091 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3092 types_list, types_list_elements);
8b70b953 3093 }
9291a0cd 3094
3063847f 3095 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3096
3063847f 3097 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3098 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3099 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3100 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3101
3102 return 1;
3103}
3104
dee91e82 3105/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3106
dee91e82
DE
3107static void
3108dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3109 const gdb_byte *info_ptr,
3e225074 3110 struct die_info *comp_unit_die)
9291a0cd 3111{
dee91e82 3112 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3113 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3114 struct dwarf2_per_objfile *dwarf2_per_objfile
3115 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3116 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3117 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3118 struct attribute *attr;
7b9f3c50
DE
3119 void **slot;
3120 struct quick_file_names *qfn;
9291a0cd 3121
0186c6a7
DE
3122 gdb_assert (! this_cu->is_debug_types);
3123
07261596
TT
3124 /* Our callers never want to match partial units -- instead they
3125 will match the enclosing full CU. */
3126 if (comp_unit_die->tag == DW_TAG_partial_unit)
3127 {
3128 this_cu->v.quick->no_file_data = 1;
3129 return;
3130 }
3131
0186c6a7 3132 lh_cu = this_cu;
7b9f3c50 3133 slot = NULL;
dee91e82 3134
fff8551c 3135 line_header_up lh;
9c541725 3136 sect_offset line_offset {};
fff8551c 3137
dee91e82 3138 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3139 if (attr != nullptr)
9291a0cd 3140 {
7b9f3c50
DE
3141 struct quick_file_names find_entry;
3142
9c541725 3143 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3144
3145 /* We may have already read in this line header (TU line header sharing).
3146 If we have we're done. */
094b34ac 3147 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3148 find_entry.hash.line_sect_off = line_offset;
5895093f 3149 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3150 &find_entry, INSERT);
3151 if (*slot != NULL)
3152 {
9a3c8263 3153 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3154 return;
7b9f3c50
DE
3155 }
3156
3019eac3 3157 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3158 }
3159 if (lh == NULL)
3160 {
094b34ac 3161 lh_cu->v.quick->no_file_data = 1;
dee91e82 3162 return;
9291a0cd
TT
3163 }
3164
8d749320 3165 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3166 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3167 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3168 gdb_assert (slot != NULL);
3169 *slot = qfn;
9291a0cd 3170
d721ba37 3171 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3172
aa391654
TT
3173 int offset = 0;
3174 if (strcmp (fnd.name, "<unknown>") != 0)
3175 ++offset;
3176
7ba99d21 3177 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3178 qfn->file_names =
aa391654
TT
3179 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3180 if (offset != 0)
3181 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3182 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3183 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3184 fnd.comp_dir).release ();
7b9f3c50 3185 qfn->real_names = NULL;
9291a0cd 3186
094b34ac 3187 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3188}
3189
3190/* A helper for the "quick" functions which attempts to read the line
3191 table for THIS_CU. */
3192
3193static struct quick_file_names *
e4a48d9d 3194dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3195{
0186c6a7
DE
3196 /* This should never be called for TUs. */
3197 gdb_assert (! this_cu->is_debug_types);
3198 /* Nor type unit groups. */
197400e8 3199 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3200
dee91e82
DE
3201 if (this_cu->v.quick->file_names != NULL)
3202 return this_cu->v.quick->file_names;
3203 /* If we know there is no line data, no point in looking again. */
3204 if (this_cu->v.quick->no_file_data)
3205 return NULL;
3206
c0ab21c2
TT
3207 cutu_reader reader (this_cu);
3208 if (!reader.dummy_p)
3e225074 3209 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3210
3211 if (this_cu->v.quick->no_file_data)
3212 return NULL;
3213 return this_cu->v.quick->file_names;
9291a0cd
TT
3214}
3215
3216/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3217 real path for a given file name from the line table. */
2fdf6df6 3218
9291a0cd 3219static const char *
7b9f3c50
DE
3220dw2_get_real_path (struct objfile *objfile,
3221 struct quick_file_names *qfn, int index)
9291a0cd 3222{
7b9f3c50
DE
3223 if (qfn->real_names == NULL)
3224 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3225 qfn->num_file_names, const char *);
9291a0cd 3226
7b9f3c50 3227 if (qfn->real_names[index] == NULL)
14278e1f 3228 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3229
7b9f3c50 3230 return qfn->real_names[index];
9291a0cd
TT
3231}
3232
3233static struct symtab *
3234dw2_find_last_source_symtab (struct objfile *objfile)
3235{
ed2dc618
SM
3236 struct dwarf2_per_objfile *dwarf2_per_objfile
3237 = get_dwarf2_per_objfile (objfile);
b76e467d 3238 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3239 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3240
43f3e411
DE
3241 if (cust == NULL)
3242 return NULL;
ed2dc618 3243
43f3e411 3244 return compunit_primary_filetab (cust);
9291a0cd
TT
3245}
3246
7b9f3c50
DE
3247/* Traversal function for dw2_forget_cached_source_info. */
3248
3249static int
3250dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3251{
7b9f3c50 3252 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3253
7b9f3c50 3254 if (file_data->real_names)
9291a0cd 3255 {
7b9f3c50 3256 int i;
9291a0cd 3257
7b9f3c50 3258 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3259 {
7b9f3c50
DE
3260 xfree ((void*) file_data->real_names[i]);
3261 file_data->real_names[i] = NULL;
9291a0cd
TT
3262 }
3263 }
7b9f3c50
DE
3264
3265 return 1;
3266}
3267
3268static void
3269dw2_forget_cached_source_info (struct objfile *objfile)
3270{
ed2dc618
SM
3271 struct dwarf2_per_objfile *dwarf2_per_objfile
3272 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3273
5895093f 3274 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3275 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3276}
3277
f8eba3c6
TT
3278/* Helper function for dw2_map_symtabs_matching_filename that expands
3279 the symtabs and calls the iterator. */
3280
3281static int
3282dw2_map_expand_apply (struct objfile *objfile,
3283 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3284 const char *name, const char *real_path,
14bc53a8 3285 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3286{
43f3e411 3287 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3288
3289 /* Don't visit already-expanded CUs. */
43f3e411 3290 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3291 return 0;
3292
3293 /* This may expand more than one symtab, and we want to iterate over
3294 all of them. */
58f0c718 3295 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3296
14bc53a8
PA
3297 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3298 last_made, callback);
f8eba3c6
TT
3299}
3300
3301/* Implementation of the map_symtabs_matching_filename method. */
3302
14bc53a8
PA
3303static bool
3304dw2_map_symtabs_matching_filename
3305 (struct objfile *objfile, const char *name, const char *real_path,
3306 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3307{
c011a4f4 3308 const char *name_basename = lbasename (name);
ed2dc618
SM
3309 struct dwarf2_per_objfile *dwarf2_per_objfile
3310 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3311
848e3e78
DE
3312 /* The rule is CUs specify all the files, including those used by
3313 any TU, so there's no need to scan TUs here. */
f4dc4d17 3314
b76e467d 3315 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3316 {
3d7bb9d9 3317 /* We only need to look at symtabs not already expanded. */
43f3e411 3318 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3319 continue;
3320
b76e467d 3321 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3322 if (file_data == NULL)
9291a0cd
TT
3323 continue;
3324
b76e467d 3325 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3326 {
7b9f3c50 3327 const char *this_name = file_data->file_names[j];
da235a7c 3328 const char *this_real_name;
9291a0cd 3329
af529f8f 3330 if (compare_filenames_for_search (this_name, name))
9291a0cd 3331 {
f5b95b50 3332 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3333 callback))
3334 return true;
288e77a7 3335 continue;
4aac40c8 3336 }
9291a0cd 3337
c011a4f4
DE
3338 /* Before we invoke realpath, which can get expensive when many
3339 files are involved, do a quick comparison of the basenames. */
3340 if (! basenames_may_differ
3341 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3342 continue;
3343
da235a7c
JK
3344 this_real_name = dw2_get_real_path (objfile, file_data, j);
3345 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3346 {
da235a7c 3347 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3348 callback))
3349 return true;
288e77a7 3350 continue;
da235a7c 3351 }
9291a0cd 3352
da235a7c
JK
3353 if (real_path != NULL)
3354 {
af529f8f
JK
3355 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3356 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3357 if (this_real_name != NULL
af529f8f 3358 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3359 {
f5b95b50 3360 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3361 callback))
3362 return true;
288e77a7 3363 continue;
9291a0cd
TT
3364 }
3365 }
3366 }
3367 }
3368
14bc53a8 3369 return false;
9291a0cd
TT
3370}
3371
da51c347
DE
3372/* Struct used to manage iterating over all CUs looking for a symbol. */
3373
3374struct dw2_symtab_iterator
9291a0cd 3375{
ed2dc618
SM
3376 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3377 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3378 /* If set, only look for symbols that match that block. Valid values are
3379 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3380 gdb::optional<block_enum> block_index;
da51c347
DE
3381 /* The kind of symbol we're looking for. */
3382 domain_enum domain;
3383 /* The list of CUs from the index entry of the symbol,
3384 or NULL if not found. */
3385 offset_type *vec;
3386 /* The next element in VEC to look at. */
3387 int next;
3388 /* The number of elements in VEC, or zero if there is no match. */
3389 int length;
8943b874
DE
3390 /* Have we seen a global version of the symbol?
3391 If so we can ignore all further global instances.
3392 This is to work around gold/15646, inefficient gold-generated
3393 indices. */
3394 int global_seen;
da51c347 3395};
9291a0cd 3396
2b79f376 3397/* Initialize the index symtab iterator ITER. */
2fdf6df6 3398
9291a0cd 3399static void
da51c347 3400dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3401 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3402 gdb::optional<block_enum> block_index,
da51c347
DE
3403 domain_enum domain,
3404 const char *name)
3405{
ed2dc618 3406 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3407 iter->block_index = block_index;
3408 iter->domain = domain;
3409 iter->next = 0;
8943b874 3410 iter->global_seen = 0;
da51c347 3411
3063847f 3412 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3413
3414 /* index is NULL if OBJF_READNOW. */
3415 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3416 iter->length = MAYBE_SWAP (*iter->vec);
3417 else
3418 {
3419 iter->vec = NULL;
3420 iter->length = 0;
3421 }
3422}
3423
3424/* Return the next matching CU or NULL if there are no more. */
3425
3426static struct dwarf2_per_cu_data *
3427dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3428{
ed2dc618
SM
3429 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3430
da51c347
DE
3431 for ( ; iter->next < iter->length; ++iter->next)
3432 {
3433 offset_type cu_index_and_attrs =
3434 MAYBE_SWAP (iter->vec[iter->next + 1]);
3435 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3436 gdb_index_symbol_kind symbol_kind =
3437 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3438 /* Only check the symbol attributes if they're present.
3439 Indices prior to version 7 don't record them,
3440 and indices >= 7 may elide them for certain symbols
3441 (gold does this). */
3442 int attrs_valid =
ed2dc618 3443 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3444 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3445
3190f0c6 3446 /* Don't crash on bad data. */
b76e467d 3447 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3448 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3449 {
b98664d3 3450 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3451 " [in module %s]"),
3452 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3453 continue;
3454 }
3455
ff4c9fec 3456 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3457
da51c347 3458 /* Skip if already read in. */
43f3e411 3459 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3460 continue;
3461
8943b874
DE
3462 /* Check static vs global. */
3463 if (attrs_valid)
3464 {
2b79f376
SM
3465 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3466
3467 if (iter->block_index.has_value ())
3468 {
3469 bool want_static = *iter->block_index == STATIC_BLOCK;
3470
3471 if (is_static != want_static)
3472 continue;
3473 }
3474
8943b874
DE
3475 /* Work around gold/15646. */
3476 if (!is_static && iter->global_seen)
3477 continue;
3478 if (!is_static)
3479 iter->global_seen = 1;
3480 }
da51c347
DE
3481
3482 /* Only check the symbol's kind if it has one. */
3483 if (attrs_valid)
3484 {
3485 switch (iter->domain)
3486 {
3487 case VAR_DOMAIN:
3488 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3489 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3490 /* Some types are also in VAR_DOMAIN. */
3491 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3492 continue;
3493 break;
3494 case STRUCT_DOMAIN:
3495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3496 continue;
3497 break;
3498 case LABEL_DOMAIN:
3499 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3500 continue;
3501 break;
59c35742
AB
3502 case MODULE_DOMAIN:
3503 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3504 continue;
3505 break;
da51c347
DE
3506 default:
3507 break;
3508 }
3509 }
3510
3511 ++iter->next;
3512 return per_cu;
3513 }
3514
3515 return NULL;
3516}
3517
43f3e411 3518static struct compunit_symtab *
c7f839cb 3519dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3520 const char *name, domain_enum domain)
9291a0cd 3521{
43f3e411 3522 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3523 struct dwarf2_per_objfile *dwarf2_per_objfile
3524 = get_dwarf2_per_objfile (objfile);
9291a0cd 3525
b5ec771e
PA
3526 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3527
ed2dc618
SM
3528 struct dw2_symtab_iterator iter;
3529 struct dwarf2_per_cu_data *per_cu;
da51c347 3530
2b79f376 3531 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3532
ed2dc618
SM
3533 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3534 {
3535 struct symbol *sym, *with_opaque = NULL;
58f0c718 3536 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3537 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3538 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3539
ed2dc618
SM
3540 sym = block_find_symbol (block, name, domain,
3541 block_find_non_opaque_type_preferred,
3542 &with_opaque);
b2e2f908 3543
ed2dc618
SM
3544 /* Some caution must be observed with overloaded functions
3545 and methods, since the index will not contain any overload
3546 information (but NAME might contain it). */
da51c347 3547
ed2dc618
SM
3548 if (sym != NULL
3549 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3550 return stab;
3551 if (with_opaque != NULL
3552 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3553 stab_best = stab;
da51c347 3554
ed2dc618 3555 /* Keep looking through other CUs. */
9291a0cd 3556 }
9291a0cd 3557
da51c347 3558 return stab_best;
9291a0cd
TT
3559}
3560
3561static void
3562dw2_print_stats (struct objfile *objfile)
3563{
ed2dc618
SM
3564 struct dwarf2_per_objfile *dwarf2_per_objfile
3565 = get_dwarf2_per_objfile (objfile);
b76e467d 3566 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3567 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3568 int count = 0;
9291a0cd 3569
ed2dc618 3570 for (int i = 0; i < total; ++i)
9291a0cd 3571 {
ff4c9fec 3572 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3573
43f3e411 3574 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3575 ++count;
3576 }
e4a48d9d 3577 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3578 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3579}
3580
779bd270
DE
3581/* This dumps minimal information about the index.
3582 It is called via "mt print objfiles".
3583 One use is to verify .gdb_index has been loaded by the
3584 gdb.dwarf2/gdb-index.exp testcase. */
3585
9291a0cd
TT
3586static void
3587dw2_dump (struct objfile *objfile)
3588{
ed2dc618
SM
3589 struct dwarf2_per_objfile *dwarf2_per_objfile
3590 = get_dwarf2_per_objfile (objfile);
3591
779bd270
DE
3592 gdb_assert (dwarf2_per_objfile->using_index);
3593 printf_filtered (".gdb_index:");
3594 if (dwarf2_per_objfile->index_table != NULL)
3595 {
3596 printf_filtered (" version %d\n",
3597 dwarf2_per_objfile->index_table->version);
3598 }
3599 else
3600 printf_filtered (" faked for \"readnow\"\n");
3601 printf_filtered ("\n");
9291a0cd
TT
3602}
3603
9291a0cd
TT
3604static void
3605dw2_expand_symtabs_for_function (struct objfile *objfile,
3606 const char *func_name)
3607{
ed2dc618
SM
3608 struct dwarf2_per_objfile *dwarf2_per_objfile
3609 = get_dwarf2_per_objfile (objfile);
da51c347 3610
ed2dc618
SM
3611 struct dw2_symtab_iterator iter;
3612 struct dwarf2_per_cu_data *per_cu;
da51c347 3613
2b79f376 3614 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3615
ed2dc618 3616 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3617 dw2_instantiate_symtab (per_cu, false);
da51c347 3618
9291a0cd
TT
3619}
3620
3621static void
3622dw2_expand_all_symtabs (struct objfile *objfile)
3623{
ed2dc618
SM
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
b76e467d 3626 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3627 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3628
ed2dc618 3629 for (int i = 0; i < total_units; ++i)
9291a0cd 3630 {
ff4c9fec 3631 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3632
58f0c718
TT
3633 /* We don't want to directly expand a partial CU, because if we
3634 read it with the wrong language, then assertion failures can
3635 be triggered later on. See PR symtab/23010. So, tell
3636 dw2_instantiate_symtab to skip partial CUs -- any important
3637 partial CU will be read via DW_TAG_imported_unit anyway. */
3638 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3639 }
3640}
3641
3642static void
652a8996
JK
3643dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3644 const char *fullname)
9291a0cd 3645{
ed2dc618
SM
3646 struct dwarf2_per_objfile *dwarf2_per_objfile
3647 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3648
3649 /* We don't need to consider type units here.
3650 This is only called for examining code, e.g. expand_line_sal.
3651 There can be an order of magnitude (or more) more type units
3652 than comp units, and we avoid them if we can. */
3653
b76e467d 3654 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3655 {
3d7bb9d9 3656 /* We only need to look at symtabs not already expanded. */
43f3e411 3657 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3658 continue;
3659
b76e467d 3660 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3661 if (file_data == NULL)
9291a0cd
TT
3662 continue;
3663
b76e467d 3664 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3665 {
652a8996
JK
3666 const char *this_fullname = file_data->file_names[j];
3667
3668 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3669 {
58f0c718 3670 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3671 break;
3672 }
3673 }
3674 }
3675}
3676
9291a0cd 3677static void
199b4314
TT
3678dw2_map_matching_symbols
3679 (struct objfile *objfile,
b054970d 3680 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3681 int global,
3682 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3683 symbol_compare_ftype *ordered_compare)
9291a0cd 3684{
40658b94 3685 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3686 current language is Ada for a non-Ada objfile using GNU index. As Ada
3687 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3688}
3689
e1ef7d7a
PA
3690/* Starting from a search name, return the string that finds the upper
3691 bound of all strings that start with SEARCH_NAME in a sorted name
3692 list. Returns the empty string to indicate that the upper bound is
3693 the end of the list. */
3694
3695static std::string
3696make_sort_after_prefix_name (const char *search_name)
3697{
3698 /* When looking to complete "func", we find the upper bound of all
3699 symbols that start with "func" by looking for where we'd insert
3700 the closest string that would follow "func" in lexicographical
3701 order. Usually, that's "func"-with-last-character-incremented,
3702 i.e. "fund". Mind non-ASCII characters, though. Usually those
3703 will be UTF-8 multi-byte sequences, but we can't be certain.
3704 Especially mind the 0xff character, which is a valid character in
3705 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3706 rule out compilers allowing it in identifiers. Note that
3707 conveniently, strcmp/strcasecmp are specified to compare
3708 characters interpreted as unsigned char. So what we do is treat
3709 the whole string as a base 256 number composed of a sequence of
3710 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3711 to 0, and carries 1 to the following more-significant position.
3712 If the very first character in SEARCH_NAME ends up incremented
3713 and carries/overflows, then the upper bound is the end of the
3714 list. The string after the empty string is also the empty
3715 string.
3716
3717 Some examples of this operation:
3718
3719 SEARCH_NAME => "+1" RESULT
3720
3721 "abc" => "abd"
3722 "ab\xff" => "ac"
3723 "\xff" "a" "\xff" => "\xff" "b"
3724 "\xff" => ""
3725 "\xff\xff" => ""
3726 "" => ""
3727
3728 Then, with these symbols for example:
3729
3730 func
3731 func1
3732 fund
3733
3734 completing "func" looks for symbols between "func" and
3735 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3736 which finds "func" and "func1", but not "fund".
3737
3738 And with:
3739
3740 funcÿ (Latin1 'ÿ' [0xff])
3741 funcÿ1
3742 fund
3743
3744 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3745 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3746
3747 And with:
3748
3749 ÿÿ (Latin1 'ÿ' [0xff])
3750 ÿÿ1
3751
3752 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3753 the end of the list.
3754 */
3755 std::string after = search_name;
3756 while (!after.empty () && (unsigned char) after.back () == 0xff)
3757 after.pop_back ();
3758 if (!after.empty ())
3759 after.back () = (unsigned char) after.back () + 1;
3760 return after;
3761}
3762
5c58de74 3763/* See declaration. */
61d96d7e 3764
5c58de74
PA
3765std::pair<std::vector<name_component>::const_iterator,
3766 std::vector<name_component>::const_iterator>
44ed8f3e 3767mapped_index_base::find_name_components_bounds
3b00ef10 3768 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3769{
5c58de74
PA
3770 auto *name_cmp
3771 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3772
3b00ef10
TT
3773 const char *lang_name
3774 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3775
3f563c84
PA
3776 /* Comparison function object for lower_bound that matches against a
3777 given symbol name. */
3778 auto lookup_compare_lower = [&] (const name_component &elem,
3779 const char *name)
3780 {
5c58de74 3781 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3782 const char *elem_name = elem_qualified + elem.name_offset;
3783 return name_cmp (elem_name, name) < 0;
3784 };
3785
3786 /* Comparison function object for upper_bound that matches against a
3787 given symbol name. */
3788 auto lookup_compare_upper = [&] (const char *name,
3789 const name_component &elem)
3790 {
5c58de74 3791 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3792 const char *elem_name = elem_qualified + elem.name_offset;
3793 return name_cmp (name, elem_name) < 0;
3794 };
3795
5c58de74
PA
3796 auto begin = this->name_components.begin ();
3797 auto end = this->name_components.end ();
3f563c84
PA
3798
3799 /* Find the lower bound. */
3800 auto lower = [&] ()
3801 {
3b00ef10 3802 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3803 return begin;
3804 else
3b00ef10 3805 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3806 } ();
3807
3808 /* Find the upper bound. */
3809 auto upper = [&] ()
3810 {
5c58de74 3811 if (lookup_name_without_params.completion_mode ())
3f563c84 3812 {
e1ef7d7a
PA
3813 /* In completion mode, we want UPPER to point past all
3814 symbols names that have the same prefix. I.e., with
3815 these symbols, and completing "func":
3816
3817 function << lower bound
3818 function1
3819 other_function << upper bound
3820
3821 We find the upper bound by looking for the insertion
3822 point of "func"-with-last-character-incremented,
3823 i.e. "fund". */
3b00ef10 3824 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3825 if (after.empty ())
3f563c84 3826 return end;
e6b2f5ef
PA
3827 return std::lower_bound (lower, end, after.c_str (),
3828 lookup_compare_lower);
3f563c84
PA
3829 }
3830 else
3b00ef10 3831 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3832 } ();
3833
5c58de74
PA
3834 return {lower, upper};
3835}
3836
3837/* See declaration. */
3838
3839void
44ed8f3e 3840mapped_index_base::build_name_components ()
5c58de74
PA
3841{
3842 if (!this->name_components.empty ())
3843 return;
3844
3845 this->name_components_casing = case_sensitivity;
3846 auto *name_cmp
3847 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3848
3849 /* The code below only knows how to break apart components of C++
3850 symbol names (and other languages that use '::' as
3b00ef10 3851 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3852 auto count = this->symbol_name_count ();
3853 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3854 {
44ed8f3e 3855 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3856 continue;
3857
3858 const char *name = this->symbol_name_at (idx);
3859
3860 /* Add each name component to the name component table. */
3861 unsigned int previous_len = 0;
3b00ef10
TT
3862
3863 if (strstr (name, "::") != nullptr)
3864 {
3865 for (unsigned int current_len = cp_find_first_component (name);
3866 name[current_len] != '\0';
3867 current_len += cp_find_first_component (name + current_len))
3868 {
3869 gdb_assert (name[current_len] == ':');
3870 this->name_components.push_back ({previous_len, idx});
3871 /* Skip the '::'. */
3872 current_len += 2;
3873 previous_len = current_len;
3874 }
3875 }
3876 else
5c58de74 3877 {
3b00ef10
TT
3878 /* Handle the Ada encoded (aka mangled) form here. */
3879 for (const char *iter = strstr (name, "__");
3880 iter != nullptr;
3881 iter = strstr (iter, "__"))
3882 {
3883 this->name_components.push_back ({previous_len, idx});
3884 iter += 2;
3885 previous_len = iter - name;
3886 }
5c58de74 3887 }
3b00ef10 3888
5c58de74
PA
3889 this->name_components.push_back ({previous_len, idx});
3890 }
3891
3892 /* Sort name_components elements by name. */
3893 auto name_comp_compare = [&] (const name_component &left,
3894 const name_component &right)
3895 {
3896 const char *left_qualified = this->symbol_name_at (left.idx);
3897 const char *right_qualified = this->symbol_name_at (right.idx);
3898
3899 const char *left_name = left_qualified + left.name_offset;
3900 const char *right_name = right_qualified + right.name_offset;
3901
3902 return name_cmp (left_name, right_name) < 0;
3903 };
3904
3905 std::sort (this->name_components.begin (),
3906 this->name_components.end (),
3907 name_comp_compare);
3908}
3909
3910/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3911 mapped_index_base instead of the containing objfile. This is split
3912 to a separate function in order to be able to unit test the
3913 name_components matching using a mock mapped_index_base. For each
5c58de74 3914 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3915 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3916
3917static void
3918dw2_expand_symtabs_matching_symbol
44ed8f3e 3919 (mapped_index_base &index,
5c58de74
PA
3920 const lookup_name_info &lookup_name_in,
3921 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3922 enum search_domain kind,
3b00ef10 3923 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3924{
3925 lookup_name_info lookup_name_without_params
3926 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3927
3928 /* Build the symbol name component sorted vector, if we haven't
3929 yet. */
3930 index.build_name_components ();
3931
3f563c84
PA
3932 /* The same symbol may appear more than once in the range though.
3933 E.g., if we're looking for symbols that complete "w", and we have
3934 a symbol named "w1::w2", we'll find the two name components for
3935 that same symbol in the range. To be sure we only call the
3936 callback once per symbol, we first collect the symbol name
3937 indexes that matched in a temporary vector and ignore
3938 duplicates. */
3939 std::vector<offset_type> matches;
3f563c84 3940
3b00ef10
TT
3941 struct name_and_matcher
3942 {
3943 symbol_name_matcher_ftype *matcher;
3944 const std::string &name;
3945
3946 bool operator== (const name_and_matcher &other) const
3f563c84 3947 {
3b00ef10
TT
3948 return matcher == other.matcher && name == other.name;
3949 }
3950 };
3951
3952 /* A vector holding all the different symbol name matchers, for all
3953 languages. */
3954 std::vector<name_and_matcher> matchers;
3955
3956 for (int i = 0; i < nr_languages; i++)
3957 {
3958 enum language lang_e = (enum language) i;
3959
3960 const language_defn *lang = language_def (lang_e);
3961 symbol_name_matcher_ftype *name_matcher
3962 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3963
3b00ef10
TT
3964 name_and_matcher key {
3965 name_matcher,
3966 lookup_name_without_params.language_lookup_name (lang_e)
3967 };
3968
3969 /* Don't insert the same comparison routine more than once.
3970 Note that we do this linear walk. This is not a problem in
3971 practice because the number of supported languages is
3972 low. */
3973 if (std::find (matchers.begin (), matchers.end (), key)
3974 != matchers.end ())
9291a0cd 3975 continue;
3b00ef10
TT
3976 matchers.push_back (std::move (key));
3977
3978 auto bounds
3979 = index.find_name_components_bounds (lookup_name_without_params,
3980 lang_e);
3981
3982 /* Now for each symbol name in range, check to see if we have a name
3983 match, and if so, call the MATCH_CALLBACK callback. */
3984
3985 for (; bounds.first != bounds.second; ++bounds.first)
3986 {
3987 const char *qualified = index.symbol_name_at (bounds.first->idx);
3988
3989 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3990 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3991 continue;
9291a0cd 3992
3b00ef10
TT
3993 matches.push_back (bounds.first->idx);
3994 }
3f563c84
PA
3995 }
3996
3997 std::sort (matches.begin (), matches.end ());
3998
3999 /* Finally call the callback, once per match. */
4000 ULONGEST prev = -1;
4001 for (offset_type idx : matches)
4002 {
4003 if (prev != idx)
4004 {
3b00ef10
TT
4005 if (!match_callback (idx))
4006 break;
3f563c84
PA
4007 prev = idx;
4008 }
4009 }
4010
4011 /* Above we use a type wider than idx's for 'prev', since 0 and
4012 (offset_type)-1 are both possible values. */
4013 static_assert (sizeof (prev) > sizeof (offset_type), "");
4014}
4015
c62446b1
PA
4016#if GDB_SELF_TEST
4017
4018namespace selftests { namespace dw2_expand_symtabs_matching {
4019
a3c5fafd
PA
4020/* A mock .gdb_index/.debug_names-like name index table, enough to
4021 exercise dw2_expand_symtabs_matching_symbol, which works with the
4022 mapped_index_base interface. Builds an index from the symbol list
4023 passed as parameter to the constructor. */
4024class mock_mapped_index : public mapped_index_base
c62446b1
PA
4025{
4026public:
a3c5fafd
PA
4027 mock_mapped_index (gdb::array_view<const char *> symbols)
4028 : m_symbol_table (symbols)
c62446b1
PA
4029 {}
4030
a3c5fafd 4031 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4032
a3c5fafd 4033 /* Return the number of names in the symbol table. */
632e107b 4034 size_t symbol_name_count () const override
c62446b1 4035 {
a3c5fafd 4036 return m_symbol_table.size ();
c62446b1
PA
4037 }
4038
a3c5fafd 4039 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4040 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4041 {
4042 return m_symbol_table[idx];
4043 }
c62446b1 4044
a3c5fafd
PA
4045private:
4046 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4047};
4048
4049/* Convenience function that converts a NULL pointer to a "<null>"
4050 string, to pass to print routines. */
4051
4052static const char *
4053string_or_null (const char *str)
4054{
4055 return str != NULL ? str : "<null>";
4056}
4057
4058/* Check if a lookup_name_info built from
4059 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4060 index. EXPECTED_LIST is the list of expected matches, in expected
4061 matching order. If no match expected, then an empty list is
4062 specified. Returns true on success. On failure prints a warning
4063 indicating the file:line that failed, and returns false. */
4064
4065static bool
4066check_match (const char *file, int line,
4067 mock_mapped_index &mock_index,
4068 const char *name, symbol_name_match_type match_type,
4069 bool completion_mode,
4070 std::initializer_list<const char *> expected_list)
4071{
4072 lookup_name_info lookup_name (name, match_type, completion_mode);
4073
4074 bool matched = true;
4075
4076 auto mismatch = [&] (const char *expected_str,
4077 const char *got)
4078 {
4079 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4080 "expected=\"%s\", got=\"%s\"\n"),
4081 file, line,
4082 (match_type == symbol_name_match_type::FULL
4083 ? "FULL" : "WILD"),
4084 name, string_or_null (expected_str), string_or_null (got));
4085 matched = false;
4086 };
4087
4088 auto expected_it = expected_list.begin ();
4089 auto expected_end = expected_list.end ();
4090
a3c5fafd 4091 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4092 NULL, ALL_DOMAIN,
4093 [&] (offset_type idx)
4094 {
a3c5fafd 4095 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4096 const char *expected_str
4097 = expected_it == expected_end ? NULL : *expected_it++;
4098
4099 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4100 mismatch (expected_str, matched_name);
3b00ef10 4101 return true;
c62446b1
PA
4102 });
4103
4104 const char *expected_str
4105 = expected_it == expected_end ? NULL : *expected_it++;
4106 if (expected_str != NULL)
4107 mismatch (expected_str, NULL);
4108
4109 return matched;
4110}
4111
4112/* The symbols added to the mock mapped_index for testing (in
4113 canonical form). */
4114static const char *test_symbols[] = {
4115 "function",
4116 "std::bar",
4117 "std::zfunction",
4118 "std::zfunction2",
4119 "w1::w2",
4120 "ns::foo<char*>",
4121 "ns::foo<int>",
4122 "ns::foo<long>",
a20714ff
PA
4123 "ns2::tmpl<int>::foo2",
4124 "(anonymous namespace)::A::B::C",
c62446b1 4125
e1ef7d7a
PA
4126 /* These are used to check that the increment-last-char in the
4127 matching algorithm for completion doesn't match "t1_fund" when
4128 completing "t1_func". */
4129 "t1_func",
4130 "t1_func1",
4131 "t1_fund",
4132 "t1_fund1",
4133
4134 /* A UTF-8 name with multi-byte sequences to make sure that
4135 cp-name-parser understands this as a single identifier ("função"
4136 is "function" in PT). */
4137 u8"u8função",
4138
4139 /* \377 (0xff) is Latin1 'ÿ'. */
4140 "yfunc\377",
4141
4142 /* \377 (0xff) is Latin1 'ÿ'. */
4143 "\377",
4144 "\377\377123",
4145
c62446b1
PA
4146 /* A name with all sorts of complications. Starts with "z" to make
4147 it easier for the completion tests below. */
4148#define Z_SYM_NAME \
4149 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4150 "::tuple<(anonymous namespace)::ui*, " \
4151 "std::default_delete<(anonymous namespace)::ui>, void>"
4152
4153 Z_SYM_NAME
4154};
4155
a3c5fafd
PA
4156/* Returns true if the mapped_index_base::find_name_component_bounds
4157 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4158 in completion mode. */
5c58de74
PA
4159
4160static bool
a3c5fafd 4161check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4162 const char *search_name,
4163 gdb::array_view<const char *> expected_syms)
4164{
4165 lookup_name_info lookup_name (search_name,
4166 symbol_name_match_type::FULL, true);
4167
3b00ef10
TT
4168 auto bounds = index.find_name_components_bounds (lookup_name,
4169 language_cplus);
5c58de74
PA
4170
4171 size_t distance = std::distance (bounds.first, bounds.second);
4172 if (distance != expected_syms.size ())
4173 return false;
4174
4175 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4176 {
4177 auto nc_elem = bounds.first + exp_elem;
4178 const char *qualified = index.symbol_name_at (nc_elem->idx);
4179 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4180 return false;
4181 }
4182
4183 return true;
4184}
4185
4186/* Test the lower-level mapped_index::find_name_component_bounds
4187 method. */
4188
c62446b1 4189static void
5c58de74
PA
4190test_mapped_index_find_name_component_bounds ()
4191{
4192 mock_mapped_index mock_index (test_symbols);
4193
a3c5fafd 4194 mock_index.build_name_components ();
5c58de74
PA
4195
4196 /* Test the lower-level mapped_index::find_name_component_bounds
4197 method in completion mode. */
4198 {
4199 static const char *expected_syms[] = {
4200 "t1_func",
4201 "t1_func1",
5c58de74
PA
4202 };
4203
a3c5fafd 4204 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4205 "t1_func", expected_syms));
4206 }
4207
4208 /* Check that the increment-last-char in the name matching algorithm
4209 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4210 {
4211 static const char *expected_syms1[] = {
4212 "\377",
4213 "\377\377123",
4214 };
a3c5fafd 4215 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4216 "\377", expected_syms1));
4217
4218 static const char *expected_syms2[] = {
4219 "\377\377123",
4220 };
a3c5fafd 4221 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4222 "\377\377", expected_syms2));
4223 }
4224}
4225
4226/* Test dw2_expand_symtabs_matching_symbol. */
4227
4228static void
4229test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4230{
4231 mock_mapped_index mock_index (test_symbols);
4232
4233 /* We let all tests run until the end even if some fails, for debug
4234 convenience. */
4235 bool any_mismatch = false;
4236
4237 /* Create the expected symbols list (an initializer_list). Needed
4238 because lists have commas, and we need to pass them to CHECK,
4239 which is a macro. */
4240#define EXPECT(...) { __VA_ARGS__ }
4241
4242 /* Wrapper for check_match that passes down the current
4243 __FILE__/__LINE__. */
4244#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4245 any_mismatch |= !check_match (__FILE__, __LINE__, \
4246 mock_index, \
4247 NAME, MATCH_TYPE, COMPLETION_MODE, \
4248 EXPECTED_LIST)
4249
4250 /* Identity checks. */
4251 for (const char *sym : test_symbols)
4252 {
4253 /* Should be able to match all existing symbols. */
4254 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4255 EXPECT (sym));
4256
4257 /* Should be able to match all existing symbols with
4258 parameters. */
4259 std::string with_params = std::string (sym) + "(int)";
4260 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4261 EXPECT (sym));
4262
4263 /* Should be able to match all existing symbols with
4264 parameters and qualifiers. */
4265 with_params = std::string (sym) + " ( int ) const";
4266 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4267 EXPECT (sym));
4268
4269 /* This should really find sym, but cp-name-parser.y doesn't
4270 know about lvalue/rvalue qualifiers yet. */
4271 with_params = std::string (sym) + " ( int ) &&";
4272 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4273 {});
4274 }
4275
e1ef7d7a
PA
4276 /* Check that the name matching algorithm for completion doesn't get
4277 confused with Latin1 'ÿ' / 0xff. */
4278 {
4279 static const char str[] = "\377";
4280 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4281 EXPECT ("\377", "\377\377123"));
4282 }
4283
4284 /* Check that the increment-last-char in the matching algorithm for
4285 completion doesn't match "t1_fund" when completing "t1_func". */
4286 {
4287 static const char str[] = "t1_func";
4288 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4289 EXPECT ("t1_func", "t1_func1"));
4290 }
4291
c62446b1
PA
4292 /* Check that completion mode works at each prefix of the expected
4293 symbol name. */
4294 {
4295 static const char str[] = "function(int)";
4296 size_t len = strlen (str);
4297 std::string lookup;
4298
4299 for (size_t i = 1; i < len; i++)
4300 {
4301 lookup.assign (str, i);
4302 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4303 EXPECT ("function"));
4304 }
4305 }
4306
4307 /* While "w" is a prefix of both components, the match function
4308 should still only be called once. */
4309 {
4310 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4311 EXPECT ("w1::w2"));
a20714ff
PA
4312 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4313 EXPECT ("w1::w2"));
c62446b1
PA
4314 }
4315
4316 /* Same, with a "complicated" symbol. */
4317 {
4318 static const char str[] = Z_SYM_NAME;
4319 size_t len = strlen (str);
4320 std::string lookup;
4321
4322 for (size_t i = 1; i < len; i++)
4323 {
4324 lookup.assign (str, i);
4325 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4326 EXPECT (Z_SYM_NAME));
4327 }
4328 }
4329
4330 /* In FULL mode, an incomplete symbol doesn't match. */
4331 {
4332 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4333 {});
4334 }
4335
4336 /* A complete symbol with parameters matches any overload, since the
4337 index has no overload info. */
4338 {
4339 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4340 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4341 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4342 EXPECT ("std::zfunction", "std::zfunction2"));
4343 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4344 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4345 }
4346
4347 /* Check that whitespace is ignored appropriately. A symbol with a
4348 template argument list. */
4349 {
4350 static const char expected[] = "ns::foo<int>";
4351 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4352 EXPECT (expected));
a20714ff
PA
4353 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4354 EXPECT (expected));
c62446b1
PA
4355 }
4356
4357 /* Check that whitespace is ignored appropriately. A symbol with a
4358 template argument list that includes a pointer. */
4359 {
4360 static const char expected[] = "ns::foo<char*>";
4361 /* Try both completion and non-completion modes. */
4362 static const bool completion_mode[2] = {false, true};
4363 for (size_t i = 0; i < 2; i++)
4364 {
4365 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4366 completion_mode[i], EXPECT (expected));
a20714ff
PA
4367 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4368 completion_mode[i], EXPECT (expected));
c62446b1
PA
4369
4370 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4371 completion_mode[i], EXPECT (expected));
a20714ff
PA
4372 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4373 completion_mode[i], EXPECT (expected));
c62446b1
PA
4374 }
4375 }
4376
4377 {
4378 /* Check method qualifiers are ignored. */
4379 static const char expected[] = "ns::foo<char*>";
4380 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4381 symbol_name_match_type::FULL, true, EXPECT (expected));
4382 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4383 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4384 CHECK_MATCH ("foo < char * > ( int ) const",
4385 symbol_name_match_type::WILD, true, EXPECT (expected));
4386 CHECK_MATCH ("foo < char * > ( int ) &&",
4387 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4388 }
4389
4390 /* Test lookup names that don't match anything. */
4391 {
a20714ff
PA
4392 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4393 {});
4394
c62446b1
PA
4395 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4396 {});
4397 }
4398
a20714ff
PA
4399 /* Some wild matching tests, exercising "(anonymous namespace)",
4400 which should not be confused with a parameter list. */
4401 {
4402 static const char *syms[] = {
4403 "A::B::C",
4404 "B::C",
4405 "C",
4406 "A :: B :: C ( int )",
4407 "B :: C ( int )",
4408 "C ( int )",
4409 };
4410
4411 for (const char *s : syms)
4412 {
4413 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4414 EXPECT ("(anonymous namespace)::A::B::C"));
4415 }
4416 }
4417
4418 {
4419 static const char expected[] = "ns2::tmpl<int>::foo2";
4420 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4421 EXPECT (expected));
4422 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4423 EXPECT (expected));
4424 }
4425
c62446b1
PA
4426 SELF_CHECK (!any_mismatch);
4427
4428#undef EXPECT
4429#undef CHECK_MATCH
4430}
4431
5c58de74
PA
4432static void
4433run_test ()
4434{
4435 test_mapped_index_find_name_component_bounds ();
4436 test_dw2_expand_symtabs_matching_symbol ();
4437}
4438
c62446b1
PA
4439}} // namespace selftests::dw2_expand_symtabs_matching
4440
4441#endif /* GDB_SELF_TEST */
4442
4b514bc8
JK
4443/* If FILE_MATCHER is NULL or if PER_CU has
4444 dwarf2_per_cu_quick_data::MARK set (see
4445 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4446 EXPANSION_NOTIFY on it. */
4447
4448static void
4449dw2_expand_symtabs_matching_one
4450 (struct dwarf2_per_cu_data *per_cu,
4451 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4452 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4453{
4454 if (file_matcher == NULL || per_cu->v.quick->mark)
4455 {
4456 bool symtab_was_null
4457 = (per_cu->v.quick->compunit_symtab == NULL);
4458
58f0c718 4459 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4460
4461 if (expansion_notify != NULL
4462 && symtab_was_null
4463 && per_cu->v.quick->compunit_symtab != NULL)
4464 expansion_notify (per_cu->v.quick->compunit_symtab);
4465 }
4466}
4467
3f563c84
PA
4468/* Helper for dw2_expand_matching symtabs. Called on each symbol
4469 matched, to expand corresponding CUs that were marked. IDX is the
4470 index of the symbol name that matched. */
4471
4472static void
4473dw2_expand_marked_cus
ed2dc618 4474 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4475 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4476 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4477 search_domain kind)
4478{
3f563c84
PA
4479 offset_type *vec, vec_len, vec_idx;
4480 bool global_seen = false;
ed2dc618 4481 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4482
61920122 4483 vec = (offset_type *) (index.constant_pool
f00a2de2 4484 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4485 vec_len = MAYBE_SWAP (vec[0]);
4486 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4487 {
61920122
PA
4488 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4489 /* This value is only valid for index versions >= 7. */
4490 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4491 gdb_index_symbol_kind symbol_kind =
4492 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4493 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4494 /* Only check the symbol attributes if they're present.
4495 Indices prior to version 7 don't record them,
4496 and indices >= 7 may elide them for certain symbols
4497 (gold does this). */
4498 int attrs_valid =
4499 (index.version >= 7
4500 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4501
4502 /* Work around gold/15646. */
4503 if (attrs_valid)
9291a0cd 4504 {
61920122
PA
4505 if (!is_static && global_seen)
4506 continue;
4507 if (!is_static)
4508 global_seen = true;
4509 }
3190f0c6 4510
61920122
PA
4511 /* Only check the symbol's kind if it has one. */
4512 if (attrs_valid)
4513 {
4514 switch (kind)
8943b874 4515 {
61920122
PA
4516 case VARIABLES_DOMAIN:
4517 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4518 continue;
4519 break;
4520 case FUNCTIONS_DOMAIN:
4521 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4522 continue;
61920122
PA
4523 break;
4524 case TYPES_DOMAIN:
4525 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4526 continue;
4527 break;
59c35742
AB
4528 case MODULES_DOMAIN:
4529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4530 continue;
4531 break;
61920122
PA
4532 default:
4533 break;
8943b874 4534 }
61920122 4535 }
8943b874 4536
61920122 4537 /* Don't crash on bad data. */
b76e467d 4538 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4539 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4540 {
b98664d3 4541 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4542 " [in module %s]"),
4543 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4544 continue;
4545 }
4546
ff4c9fec 4547 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4548 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4549 expansion_notify);
61920122
PA
4550 }
4551}
4552
4b514bc8
JK
4553/* If FILE_MATCHER is non-NULL, set all the
4554 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4555 that match FILE_MATCHER. */
4556
61920122 4557static void
4b514bc8 4558dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4559 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4560 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4561{
4b514bc8 4562 if (file_matcher == NULL)
61920122
PA
4563 return;
4564
4b514bc8
JK
4565 objfile *const objfile = dwarf2_per_objfile->objfile;
4566
4567 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4568 htab_eq_pointer,
4569 NULL, xcalloc, xfree));
4570 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4571 htab_eq_pointer,
4572 NULL, xcalloc, xfree));
61920122 4573
4b514bc8
JK
4574 /* The rule is CUs specify all the files, including those used by
4575 any TU, so there's no need to scan TUs here. */
61920122 4576
b76e467d 4577 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4578 {
927aa2e7
JK
4579 QUIT;
4580
4581 per_cu->v.quick->mark = 0;
4582
4583 /* We only need to look at symtabs not already expanded. */
4584 if (per_cu->v.quick->compunit_symtab)
4585 continue;
4586
b76e467d 4587 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4588 if (file_data == NULL)
4589 continue;
4590
4591 if (htab_find (visited_not_found.get (), file_data) != NULL)
4592 continue;
4593 else if (htab_find (visited_found.get (), file_data) != NULL)
4594 {
4595 per_cu->v.quick->mark = 1;
4596 continue;
4597 }
4598
b76e467d 4599 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4600 {
4601 const char *this_real_name;
4602
4603 if (file_matcher (file_data->file_names[j], false))
4604 {
4605 per_cu->v.quick->mark = 1;
4606 break;
4607 }
4608
4609 /* Before we invoke realpath, which can get expensive when many
4610 files are involved, do a quick comparison of the basenames. */
4611 if (!basenames_may_differ
4612 && !file_matcher (lbasename (file_data->file_names[j]),
4613 true))
4614 continue;
4615
4616 this_real_name = dw2_get_real_path (objfile, file_data, j);
4617 if (file_matcher (this_real_name, false))
4618 {
4619 per_cu->v.quick->mark = 1;
4620 break;
4621 }
4622 }
4623
b76e467d
SM
4624 void **slot = htab_find_slot (per_cu->v.quick->mark
4625 ? visited_found.get ()
4626 : visited_not_found.get (),
4627 file_data, INSERT);
927aa2e7
JK
4628 *slot = file_data;
4629 }
4630}
4631
4632static void
4633dw2_expand_symtabs_matching
4634 (struct objfile *objfile,
4635 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4636 const lookup_name_info &lookup_name,
4637 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4638 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4639 enum search_domain kind)
4640{
ed2dc618
SM
4641 struct dwarf2_per_objfile *dwarf2_per_objfile
4642 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4643
4644 /* index_table is NULL if OBJF_READNOW. */
4645 if (!dwarf2_per_objfile->index_table)
4646 return;
4647
ed2dc618 4648 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4649
4650 mapped_index &index = *dwarf2_per_objfile->index_table;
4651
4652 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4653 symbol_matcher,
4654 kind, [&] (offset_type idx)
4655 {
ed2dc618 4656 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4657 expansion_notify, kind);
3b00ef10 4658 return true;
927aa2e7
JK
4659 });
4660}
4661
4662/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4663 symtab. */
4664
4665static struct compunit_symtab *
4666recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4667 CORE_ADDR pc)
4668{
4669 int i;
4670
4671 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4672 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4673 return cust;
4674
4675 if (cust->includes == NULL)
4676 return NULL;
4677
4678 for (i = 0; cust->includes[i]; ++i)
4679 {
4680 struct compunit_symtab *s = cust->includes[i];
4681
4682 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4683 if (s != NULL)
4684 return s;
4685 }
4686
4687 return NULL;
4688}
4689
4690static struct compunit_symtab *
4691dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4692 struct bound_minimal_symbol msymbol,
4693 CORE_ADDR pc,
4694 struct obj_section *section,
4695 int warn_if_readin)
4696{
4697 struct dwarf2_per_cu_data *data;
4698 struct compunit_symtab *result;
4699
d320c2b5 4700 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4701 return NULL;
4702
b3b3bada 4703 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4704 data = (struct dwarf2_per_cu_data *) addrmap_find
4705 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4706 if (!data)
4707 return NULL;
4708
4709 if (warn_if_readin && data->v.quick->compunit_symtab)
4710 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4711 paddress (get_objfile_arch (objfile), pc));
4712
4713 result
58f0c718
TT
4714 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4715 false),
927aa2e7
JK
4716 pc);
4717 gdb_assert (result != NULL);
4718 return result;
4719}
4720
4721static void
4722dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4723 void *data, int need_fullname)
4724{
ed2dc618
SM
4725 struct dwarf2_per_objfile *dwarf2_per_objfile
4726 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4727
4728 if (!dwarf2_per_objfile->filenames_cache)
4729 {
4730 dwarf2_per_objfile->filenames_cache.emplace ();
4731
4732 htab_up visited (htab_create_alloc (10,
4733 htab_hash_pointer, htab_eq_pointer,
4734 NULL, xcalloc, xfree));
4735
4736 /* The rule is CUs specify all the files, including those used
4737 by any TU, so there's no need to scan TUs here. We can
4738 ignore file names coming from already-expanded CUs. */
4739
b76e467d 4740 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4741 {
927aa2e7
JK
4742 if (per_cu->v.quick->compunit_symtab)
4743 {
4744 void **slot = htab_find_slot (visited.get (),
4745 per_cu->v.quick->file_names,
4746 INSERT);
4747
4748 *slot = per_cu->v.quick->file_names;
4749 }
4750 }
4751
b76e467d 4752 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4753 {
927aa2e7
JK
4754 /* We only need to look at symtabs not already expanded. */
4755 if (per_cu->v.quick->compunit_symtab)
4756 continue;
4757
b76e467d 4758 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4759 if (file_data == NULL)
4760 continue;
4761
b76e467d 4762 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4763 if (*slot)
4764 {
4765 /* Already visited. */
4766 continue;
4767 }
4768 *slot = file_data;
4769
4770 for (int j = 0; j < file_data->num_file_names; ++j)
4771 {
4772 const char *filename = file_data->file_names[j];
4773 dwarf2_per_objfile->filenames_cache->seen (filename);
4774 }
4775 }
4776 }
4777
4778 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4779 {
4780 gdb::unique_xmalloc_ptr<char> this_real_name;
4781
4782 if (need_fullname)
4783 this_real_name = gdb_realpath (filename);
4784 (*fun) (filename, this_real_name.get (), data);
4785 });
4786}
4787
4788static int
4789dw2_has_symbols (struct objfile *objfile)
4790{
4791 return 1;
4792}
4793
4794const struct quick_symbol_functions dwarf2_gdb_index_functions =
4795{
4796 dw2_has_symbols,
4797 dw2_find_last_source_symtab,
4798 dw2_forget_cached_source_info,
4799 dw2_map_symtabs_matching_filename,
4800 dw2_lookup_symbol,
4801 dw2_print_stats,
4802 dw2_dump,
927aa2e7
JK
4803 dw2_expand_symtabs_for_function,
4804 dw2_expand_all_symtabs,
4805 dw2_expand_symtabs_with_fullname,
4806 dw2_map_matching_symbols,
4807 dw2_expand_symtabs_matching,
4808 dw2_find_pc_sect_compunit_symtab,
4809 NULL,
4810 dw2_map_symbol_filenames
4811};
4812
4813/* DWARF-5 debug_names reader. */
4814
4815/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4816static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4817
4818/* A helper function that reads the .debug_names section in SECTION
4819 and fills in MAP. FILENAME is the name of the file containing the
4820 section; it is used for error reporting.
4821
4822 Returns true if all went well, false otherwise. */
4823
4824static bool
4825read_debug_names_from_section (struct objfile *objfile,
4826 const char *filename,
4827 struct dwarf2_section_info *section,
4828 mapped_debug_names &map)
4829{
96b79293 4830 if (section->empty ())
927aa2e7
JK
4831 return false;
4832
4833 /* Older elfutils strip versions could keep the section in the main
4834 executable while splitting it for the separate debug info file. */
96b79293 4835 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4836 return false;
4837
96b79293 4838 section->read (objfile);
927aa2e7
JK
4839
4840 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4841
4842 const gdb_byte *addr = section->buffer;
4843
96b79293 4844 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4845
4846 unsigned int bytes_read;
4847 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4848 addr += bytes_read;
4849
4850 map.dwarf5_is_dwarf64 = bytes_read != 4;
4851 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4852 if (bytes_read + length != section->size)
4853 {
4854 /* There may be multiple per-CU indices. */
4855 warning (_("Section .debug_names in %s length %s does not match "
4856 "section length %s, ignoring .debug_names."),
4857 filename, plongest (bytes_read + length),
4858 pulongest (section->size));
4859 return false;
4860 }
4861
4862 /* The version number. */
4863 uint16_t version = read_2_bytes (abfd, addr);
4864 addr += 2;
4865 if (version != 5)
4866 {
4867 warning (_("Section .debug_names in %s has unsupported version %d, "
4868 "ignoring .debug_names."),
4869 filename, version);
4870 return false;
4871 }
4872
4873 /* Padding. */
4874 uint16_t padding = read_2_bytes (abfd, addr);
4875 addr += 2;
4876 if (padding != 0)
4877 {
4878 warning (_("Section .debug_names in %s has unsupported padding %d, "
4879 "ignoring .debug_names."),
4880 filename, padding);
4881 return false;
4882 }
4883
4884 /* comp_unit_count - The number of CUs in the CU list. */
4885 map.cu_count = read_4_bytes (abfd, addr);
4886 addr += 4;
4887
4888 /* local_type_unit_count - The number of TUs in the local TU
4889 list. */
4890 map.tu_count = read_4_bytes (abfd, addr);
4891 addr += 4;
4892
4893 /* foreign_type_unit_count - The number of TUs in the foreign TU
4894 list. */
4895 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4896 addr += 4;
4897 if (foreign_tu_count != 0)
4898 {
4899 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4900 "ignoring .debug_names."),
4901 filename, static_cast<unsigned long> (foreign_tu_count));
4902 return false;
4903 }
4904
4905 /* bucket_count - The number of hash buckets in the hash lookup
4906 table. */
4907 map.bucket_count = read_4_bytes (abfd, addr);
4908 addr += 4;
4909
4910 /* name_count - The number of unique names in the index. */
4911 map.name_count = read_4_bytes (abfd, addr);
4912 addr += 4;
4913
4914 /* abbrev_table_size - The size in bytes of the abbreviations
4915 table. */
4916 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4917 addr += 4;
4918
4919 /* augmentation_string_size - The size in bytes of the augmentation
4920 string. This value is rounded up to a multiple of 4. */
4921 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4922 addr += 4;
4923 map.augmentation_is_gdb = ((augmentation_string_size
4924 == sizeof (dwarf5_augmentation))
4925 && memcmp (addr, dwarf5_augmentation,
4926 sizeof (dwarf5_augmentation)) == 0);
4927 augmentation_string_size += (-augmentation_string_size) & 3;
4928 addr += augmentation_string_size;
4929
4930 /* List of CUs */
4931 map.cu_table_reordered = addr;
4932 addr += map.cu_count * map.offset_size;
4933
4934 /* List of Local TUs */
4935 map.tu_table_reordered = addr;
4936 addr += map.tu_count * map.offset_size;
4937
4938 /* Hash Lookup Table */
4939 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4940 addr += map.bucket_count * 4;
4941 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4942 addr += map.name_count * 4;
4943
4944 /* Name Table */
4945 map.name_table_string_offs_reordered = addr;
4946 addr += map.name_count * map.offset_size;
4947 map.name_table_entry_offs_reordered = addr;
4948 addr += map.name_count * map.offset_size;
4949
4950 const gdb_byte *abbrev_table_start = addr;
4951 for (;;)
4952 {
927aa2e7
JK
4953 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4954 addr += bytes_read;
4955 if (index_num == 0)
4956 break;
4957
4958 const auto insertpair
4959 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4960 if (!insertpair.second)
4961 {
4962 warning (_("Section .debug_names in %s has duplicate index %s, "
4963 "ignoring .debug_names."),
4964 filename, pulongest (index_num));
4965 return false;
4966 }
4967 mapped_debug_names::index_val &indexval = insertpair.first->second;
4968 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4969 addr += bytes_read;
4970
4971 for (;;)
4972 {
4973 mapped_debug_names::index_val::attr attr;
4974 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4975 addr += bytes_read;
4976 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4977 addr += bytes_read;
4978 if (attr.form == DW_FORM_implicit_const)
4979 {
4980 attr.implicit_const = read_signed_leb128 (abfd, addr,
4981 &bytes_read);
4982 addr += bytes_read;
4983 }
4984 if (attr.dw_idx == 0 && attr.form == 0)
4985 break;
4986 indexval.attr_vec.push_back (std::move (attr));
4987 }
4988 }
4989 if (addr != abbrev_table_start + abbrev_table_size)
4990 {
4991 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4992 "of size %s vs. written as %u, ignoring .debug_names."),
4993 filename, plongest (addr - abbrev_table_start),
4994 abbrev_table_size);
927aa2e7
JK
4995 return false;
4996 }
4997 map.entry_pool = addr;
4998
4999 return true;
5000}
5001
5002/* A helper for create_cus_from_debug_names that handles the MAP's CU
5003 list. */
5004
5005static void
ed2dc618 5006create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5007 const mapped_debug_names &map,
5008 dwarf2_section_info &section,
b76e467d 5009 bool is_dwz)
927aa2e7
JK
5010{
5011 sect_offset sect_off_prev;
5012 for (uint32_t i = 0; i <= map.cu_count; ++i)
5013 {
5014 sect_offset sect_off_next;
5015 if (i < map.cu_count)
5016 {
5017 sect_off_next
5018 = (sect_offset) (extract_unsigned_integer
5019 (map.cu_table_reordered + i * map.offset_size,
5020 map.offset_size,
5021 map.dwarf5_byte_order));
5022 }
5023 else
5024 sect_off_next = (sect_offset) section.size;
5025 if (i >= 1)
5026 {
5027 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5028 dwarf2_per_cu_data *per_cu
ed2dc618 5029 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5030 sect_off_prev, length);
b76e467d 5031 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5032 }
5033 sect_off_prev = sect_off_next;
5034 }
5035}
5036
5037/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5038 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5039
5040static void
ed2dc618 5041create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5042 const mapped_debug_names &map,
5043 const mapped_debug_names &dwz_map)
5044{
b76e467d
SM
5045 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5046 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5047
ed2dc618
SM
5048 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5049 dwarf2_per_objfile->info,
b76e467d 5050 false /* is_dwz */);
927aa2e7
JK
5051
5052 if (dwz_map.cu_count == 0)
5053 return;
5054
ed2dc618
SM
5055 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5056 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5057 true /* is_dwz */);
927aa2e7
JK
5058}
5059
5060/* Read .debug_names. If everything went ok, initialize the "quick"
5061 elements of all the CUs and return true. Otherwise, return false. */
5062
5063static bool
ed2dc618 5064dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5065{
22ca247e
TT
5066 std::unique_ptr<mapped_debug_names> map
5067 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5068 mapped_debug_names dwz_map (dwarf2_per_objfile);
5069 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5070
5071 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5072 &dwarf2_per_objfile->debug_names,
22ca247e 5073 *map))
927aa2e7
JK
5074 return false;
5075
5076 /* Don't use the index if it's empty. */
22ca247e 5077 if (map->name_count == 0)
927aa2e7
JK
5078 return false;
5079
5080 /* If there is a .dwz file, read it so we can get its CU list as
5081 well. */
ed2dc618 5082 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5083 if (dwz != NULL)
5084 {
5085 if (!read_debug_names_from_section (objfile,
00f93c44 5086 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5087 &dwz->debug_names, dwz_map))
5088 {
5089 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5090 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5091 return false;
5092 }
5093 }
5094
22ca247e 5095 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5096
22ca247e 5097 if (map->tu_count != 0)
927aa2e7
JK
5098 {
5099 /* We can only handle a single .debug_types when we have an
5100 index. */
fd5866f6 5101 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5102 return false;
5103
fd5866f6 5104 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5105
5106 create_signatured_type_table_from_debug_names
22ca247e 5107 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5108 }
5109
ed2dc618
SM
5110 create_addrmap_from_aranges (dwarf2_per_objfile,
5111 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5112
22ca247e 5113 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5114 dwarf2_per_objfile->using_index = 1;
5115 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5116 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5117
5118 return true;
5119}
5120
927aa2e7
JK
5121/* Type used to manage iterating over all CUs looking for a symbol for
5122 .debug_names. */
5123
5124class dw2_debug_names_iterator
5125{
5126public:
927aa2e7 5127 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5128 gdb::optional<block_enum> block_index,
5129 domain_enum domain,
927aa2e7 5130 const char *name)
2b79f376 5131 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5132 m_addr (find_vec_in_debug_names (map, name))
5133 {}
5134
5135 dw2_debug_names_iterator (const mapped_debug_names &map,
5136 search_domain search, uint32_t namei)
5137 : m_map (map),
5138 m_search (search),
5139 m_addr (find_vec_in_debug_names (map, namei))
5140 {}
5141
3b00ef10
TT
5142 dw2_debug_names_iterator (const mapped_debug_names &map,
5143 block_enum block_index, domain_enum domain,
5144 uint32_t namei)
5145 : m_map (map), m_block_index (block_index), m_domain (domain),
5146 m_addr (find_vec_in_debug_names (map, namei))
5147 {}
5148
927aa2e7
JK
5149 /* Return the next matching CU or NULL if there are no more. */
5150 dwarf2_per_cu_data *next ();
5151
5152private:
5153 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5154 const char *name);
5155 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5156 uint32_t namei);
5157
5158 /* The internalized form of .debug_names. */
5159 const mapped_debug_names &m_map;
5160
2b79f376
SM
5161 /* If set, only look for symbols that match that block. Valid values are
5162 GLOBAL_BLOCK and STATIC_BLOCK. */
5163 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5164
5165 /* The kind of symbol we're looking for. */
5166 const domain_enum m_domain = UNDEF_DOMAIN;
5167 const search_domain m_search = ALL_DOMAIN;
5168
5169 /* The list of CUs from the index entry of the symbol, or NULL if
5170 not found. */
5171 const gdb_byte *m_addr;
5172};
5173
5174const char *
5175mapped_debug_names::namei_to_name (uint32_t namei) const
5176{
5177 const ULONGEST namei_string_offs
5178 = extract_unsigned_integer ((name_table_string_offs_reordered
5179 + namei * offset_size),
5180 offset_size,
5181 dwarf5_byte_order);
5182 return read_indirect_string_at_offset
ed2dc618 5183 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5184}
5185
5186/* Find a slot in .debug_names for the object named NAME. If NAME is
5187 found, return pointer to its pool data. If NAME cannot be found,
5188 return NULL. */
5189
5190const gdb_byte *
5191dw2_debug_names_iterator::find_vec_in_debug_names
5192 (const mapped_debug_names &map, const char *name)
5193{
5194 int (*cmp) (const char *, const char *);
5195
54ee4252 5196 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5197 if (current_language->la_language == language_cplus
5198 || current_language->la_language == language_fortran
5199 || current_language->la_language == language_d)
5200 {
5201 /* NAME is already canonical. Drop any qualifiers as
5202 .debug_names does not contain any. */
5203
5204 if (strchr (name, '(') != NULL)
5205 {
54ee4252 5206 without_params = cp_remove_params (name);
927aa2e7 5207 if (without_params != NULL)
54ee4252 5208 name = without_params.get ();
927aa2e7
JK
5209 }
5210 }
5211
5212 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5213
5214 const uint32_t full_hash = dwarf5_djb_hash (name);
5215 uint32_t namei
5216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5217 (map.bucket_table_reordered
5218 + (full_hash % map.bucket_count)), 4,
5219 map.dwarf5_byte_order);
5220 if (namei == 0)
5221 return NULL;
5222 --namei;
5223 if (namei >= map.name_count)
5224 {
b98664d3 5225 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5226 "[in module %s]"),
5227 namei, map.name_count,
ed2dc618 5228 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5229 return NULL;
5230 }
5231
5232 for (;;)
5233 {
5234 const uint32_t namei_full_hash
5235 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5236 (map.hash_table_reordered + namei), 4,
5237 map.dwarf5_byte_order);
5238 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5239 return NULL;
5240
5241 if (full_hash == namei_full_hash)
5242 {
5243 const char *const namei_string = map.namei_to_name (namei);
5244
5245#if 0 /* An expensive sanity check. */
5246 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5247 {
b98664d3 5248 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5249 "[in module %s]"),
5250 namei, objfile_name (dwarf2_per_objfile->objfile));
5251 return NULL;
5252 }
5253#endif
5254
5255 if (cmp (namei_string, name) == 0)
5256 {
5257 const ULONGEST namei_entry_offs
5258 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5259 + namei * map.offset_size),
5260 map.offset_size, map.dwarf5_byte_order);
5261 return map.entry_pool + namei_entry_offs;
5262 }
5263 }
5264
5265 ++namei;
5266 if (namei >= map.name_count)
5267 return NULL;
5268 }
5269}
5270
5271const gdb_byte *
5272dw2_debug_names_iterator::find_vec_in_debug_names
5273 (const mapped_debug_names &map, uint32_t namei)
5274{
5275 if (namei >= map.name_count)
5276 {
b98664d3 5277 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5278 "[in module %s]"),
5279 namei, map.name_count,
ed2dc618 5280 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5281 return NULL;
5282 }
5283
5284 const ULONGEST namei_entry_offs
5285 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5286 + namei * map.offset_size),
5287 map.offset_size, map.dwarf5_byte_order);
5288 return map.entry_pool + namei_entry_offs;
5289}
5290
5291/* See dw2_debug_names_iterator. */
5292
5293dwarf2_per_cu_data *
5294dw2_debug_names_iterator::next ()
5295{
5296 if (m_addr == NULL)
5297 return NULL;
5298
ed2dc618
SM
5299 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5300 struct objfile *objfile = dwarf2_per_objfile->objfile;
5301 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5302
5303 again:
5304
5305 unsigned int bytes_read;
5306 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5307 m_addr += bytes_read;
5308 if (abbrev == 0)
5309 return NULL;
5310
5311 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5312 if (indexval_it == m_map.abbrev_map.cend ())
5313 {
b98664d3 5314 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5315 "[in module %s]"),
ed2dc618 5316 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5317 return NULL;
5318 }
5319 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5320 enum class symbol_linkage {
5321 unknown,
5322 static_,
5323 extern_,
23c13d42 5324 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5325 dwarf2_per_cu_data *per_cu = NULL;
5326 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5327 {
5328 ULONGEST ull;
5329 switch (attr.form)
5330 {
5331 case DW_FORM_implicit_const:
5332 ull = attr.implicit_const;
5333 break;
5334 case DW_FORM_flag_present:
5335 ull = 1;
5336 break;
5337 case DW_FORM_udata:
5338 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5339 m_addr += bytes_read;
5340 break;
5341 default:
b98664d3 5342 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5343 dwarf_form_name (attr.form),
ed2dc618 5344 objfile_name (objfile));
927aa2e7
JK
5345 return NULL;
5346 }
5347 switch (attr.dw_idx)
5348 {
5349 case DW_IDX_compile_unit:
5350 /* Don't crash on bad data. */
b76e467d 5351 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5352 {
b98664d3 5353 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5354 " [in module %s]"),
5355 pulongest (ull),
5356 objfile_name (dwarf2_per_objfile->objfile));
5357 continue;
5358 }
ff4c9fec 5359 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5360 break;
8af5c486
JK
5361 case DW_IDX_type_unit:
5362 /* Don't crash on bad data. */
b2bdb8cf 5363 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5364 {
b98664d3 5365 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5366 " [in module %s]"),
5367 pulongest (ull),
5368 objfile_name (dwarf2_per_objfile->objfile));
5369 continue;
5370 }
ff4c9fec 5371 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5372 break;
927aa2e7
JK
5373 case DW_IDX_GNU_internal:
5374 if (!m_map.augmentation_is_gdb)
5375 break;
23c13d42 5376 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5377 break;
5378 case DW_IDX_GNU_external:
5379 if (!m_map.augmentation_is_gdb)
5380 break;
23c13d42 5381 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5382 break;
5383 }
5384 }
5385
5386 /* Skip if already read in. */
5387 if (per_cu->v.quick->compunit_symtab)
5388 goto again;
5389
5390 /* Check static vs global. */
23c13d42 5391 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5392 {
2b79f376 5393 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5394 const bool symbol_is_static =
5395 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5396 if (want_static != symbol_is_static)
2b79f376 5397 goto again;
927aa2e7
JK
5398 }
5399
5400 /* Match dw2_symtab_iter_next, symbol_kind
5401 and debug_names::psymbol_tag. */
5402 switch (m_domain)
5403 {
5404 case VAR_DOMAIN:
5405 switch (indexval.dwarf_tag)
5406 {
5407 case DW_TAG_variable:
5408 case DW_TAG_subprogram:
5409 /* Some types are also in VAR_DOMAIN. */
5410 case DW_TAG_typedef:
5411 case DW_TAG_structure_type:
5412 break;
5413 default:
5414 goto again;
5415 }
5416 break;
5417 case STRUCT_DOMAIN:
5418 switch (indexval.dwarf_tag)
5419 {
5420 case DW_TAG_typedef:
5421 case DW_TAG_structure_type:
5422 break;
5423 default:
5424 goto again;
5425 }
5426 break;
5427 case LABEL_DOMAIN:
5428 switch (indexval.dwarf_tag)
5429 {
5430 case 0:
5431 case DW_TAG_variable:
5432 break;
5433 default:
5434 goto again;
5435 }
5436 break;
59c35742
AB
5437 case MODULE_DOMAIN:
5438 switch (indexval.dwarf_tag)
5439 {
5440 case DW_TAG_module:
5441 break;
5442 default:
5443 goto again;
5444 }
5445 break;
927aa2e7
JK
5446 default:
5447 break;
5448 }
5449
5450 /* Match dw2_expand_symtabs_matching, symbol_kind and
5451 debug_names::psymbol_tag. */
5452 switch (m_search)
4b514bc8 5453 {
927aa2e7
JK
5454 case VARIABLES_DOMAIN:
5455 switch (indexval.dwarf_tag)
4b514bc8 5456 {
927aa2e7
JK
5457 case DW_TAG_variable:
5458 break;
5459 default:
5460 goto again;
4b514bc8 5461 }
927aa2e7
JK
5462 break;
5463 case FUNCTIONS_DOMAIN:
5464 switch (indexval.dwarf_tag)
4b514bc8 5465 {
927aa2e7
JK
5466 case DW_TAG_subprogram:
5467 break;
5468 default:
5469 goto again;
4b514bc8 5470 }
927aa2e7
JK
5471 break;
5472 case TYPES_DOMAIN:
5473 switch (indexval.dwarf_tag)
5474 {
5475 case DW_TAG_typedef:
5476 case DW_TAG_structure_type:
5477 break;
5478 default:
5479 goto again;
5480 }
5481 break;
59c35742
AB
5482 case MODULES_DOMAIN:
5483 switch (indexval.dwarf_tag)
5484 {
5485 case DW_TAG_module:
5486 break;
5487 default:
5488 goto again;
5489 }
927aa2e7
JK
5490 default:
5491 break;
4b514bc8 5492 }
927aa2e7
JK
5493
5494 return per_cu;
4b514bc8 5495}
61920122 5496
927aa2e7 5497static struct compunit_symtab *
c7f839cb 5498dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5499 const char *name, domain_enum domain)
4b514bc8 5500{
ed2dc618
SM
5501 struct dwarf2_per_objfile *dwarf2_per_objfile
5502 = get_dwarf2_per_objfile (objfile);
61920122 5503
927aa2e7
JK
5504 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5505 if (!mapp)
61920122 5506 {
927aa2e7
JK
5507 /* index is NULL if OBJF_READNOW. */
5508 return NULL;
5509 }
5510 const auto &map = *mapp;
9291a0cd 5511
2b79f376 5512 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5513
927aa2e7
JK
5514 struct compunit_symtab *stab_best = NULL;
5515 struct dwarf2_per_cu_data *per_cu;
5516 while ((per_cu = iter.next ()) != NULL)
5517 {
5518 struct symbol *sym, *with_opaque = NULL;
58f0c718 5519 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5520 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5521 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5522
927aa2e7
JK
5523 sym = block_find_symbol (block, name, domain,
5524 block_find_non_opaque_type_preferred,
5525 &with_opaque);
9703b513 5526
927aa2e7
JK
5527 /* Some caution must be observed with overloaded functions and
5528 methods, since the index will not contain any overload
5529 information (but NAME might contain it). */
a3ec0bb1 5530
927aa2e7 5531 if (sym != NULL
987012b8 5532 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5533 return stab;
5534 if (with_opaque != NULL
987012b8 5535 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5536 stab_best = stab;
9703b513 5537
927aa2e7 5538 /* Keep looking through other CUs. */
9703b513
TT
5539 }
5540
927aa2e7 5541 return stab_best;
9703b513
TT
5542}
5543
927aa2e7
JK
5544/* This dumps minimal information about .debug_names. It is called
5545 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5546 uses this to verify that .debug_names has been loaded. */
9291a0cd 5547
927aa2e7
JK
5548static void
5549dw2_debug_names_dump (struct objfile *objfile)
5550{
ed2dc618
SM
5551 struct dwarf2_per_objfile *dwarf2_per_objfile
5552 = get_dwarf2_per_objfile (objfile);
5553
927aa2e7
JK
5554 gdb_assert (dwarf2_per_objfile->using_index);
5555 printf_filtered (".debug_names:");
5556 if (dwarf2_per_objfile->debug_names_table)
5557 printf_filtered (" exists\n");
5558 else
5559 printf_filtered (" faked for \"readnow\"\n");
5560 printf_filtered ("\n");
9291a0cd
TT
5561}
5562
9291a0cd 5563static void
927aa2e7
JK
5564dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5565 const char *func_name)
9291a0cd 5566{
ed2dc618
SM
5567 struct dwarf2_per_objfile *dwarf2_per_objfile
5568 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5569
927aa2e7
JK
5570 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5571 if (dwarf2_per_objfile->debug_names_table)
24c79950 5572 {
927aa2e7 5573 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5574
2b79f376 5575 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5576
927aa2e7
JK
5577 struct dwarf2_per_cu_data *per_cu;
5578 while ((per_cu = iter.next ()) != NULL)
58f0c718 5579 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5580 }
5581}
24c79950 5582
3b00ef10
TT
5583static void
5584dw2_debug_names_map_matching_symbols
5585 (struct objfile *objfile,
5586 const lookup_name_info &name, domain_enum domain,
5587 int global,
5588 gdb::function_view<symbol_found_callback_ftype> callback,
5589 symbol_compare_ftype *ordered_compare)
5590{
5591 struct dwarf2_per_objfile *dwarf2_per_objfile
5592 = get_dwarf2_per_objfile (objfile);
5593
5594 /* debug_names_table is NULL if OBJF_READNOW. */
5595 if (!dwarf2_per_objfile->debug_names_table)
5596 return;
5597
5598 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5599 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5600
5601 const char *match_name = name.ada ().lookup_name ().c_str ();
5602 auto matcher = [&] (const char *symname)
5603 {
5604 if (ordered_compare == nullptr)
5605 return true;
5606 return ordered_compare (symname, match_name) == 0;
5607 };
5608
5609 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5610 [&] (offset_type namei)
5611 {
5612 /* The name was matched, now expand corresponding CUs that were
5613 marked. */
5614 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5615
5616 struct dwarf2_per_cu_data *per_cu;
5617 while ((per_cu = iter.next ()) != NULL)
5618 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5619 return true;
5620 });
5621
5622 /* It's a shame we couldn't do this inside the
5623 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5624 that have already been expanded. Instead, this loop matches what
5625 the psymtab code does. */
5626 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5627 {
5628 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5629 if (cust != nullptr)
5630 {
5631 const struct block *block
5632 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5633 if (!iterate_over_symbols_terminated (block, name,
5634 domain, callback))
5635 break;
5636 }
5637 }
5638}
5639
927aa2e7
JK
5640static void
5641dw2_debug_names_expand_symtabs_matching
5642 (struct objfile *objfile,
5643 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5644 const lookup_name_info &lookup_name,
5645 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5646 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5647 enum search_domain kind)
5648{
ed2dc618
SM
5649 struct dwarf2_per_objfile *dwarf2_per_objfile
5650 = get_dwarf2_per_objfile (objfile);
9291a0cd 5651
927aa2e7
JK
5652 /* debug_names_table is NULL if OBJF_READNOW. */
5653 if (!dwarf2_per_objfile->debug_names_table)
5654 return;
9291a0cd 5655
ed2dc618 5656 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5657
44ed8f3e 5658 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5659
44ed8f3e
PA
5660 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5661 symbol_matcher,
5662 kind, [&] (offset_type namei)
927aa2e7 5663 {
927aa2e7
JK
5664 /* The name was matched, now expand corresponding CUs that were
5665 marked. */
5666 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5667
927aa2e7
JK
5668 struct dwarf2_per_cu_data *per_cu;
5669 while ((per_cu = iter.next ()) != NULL)
5670 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5671 expansion_notify);
3b00ef10 5672 return true;
44ed8f3e 5673 });
9291a0cd
TT
5674}
5675
927aa2e7 5676const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5677{
5678 dw2_has_symbols,
5679 dw2_find_last_source_symtab,
5680 dw2_forget_cached_source_info,
f8eba3c6 5681 dw2_map_symtabs_matching_filename,
927aa2e7 5682 dw2_debug_names_lookup_symbol,
9291a0cd 5683 dw2_print_stats,
927aa2e7 5684 dw2_debug_names_dump,
927aa2e7 5685 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5686 dw2_expand_all_symtabs,
652a8996 5687 dw2_expand_symtabs_with_fullname,
3b00ef10 5688 dw2_debug_names_map_matching_symbols,
927aa2e7 5689 dw2_debug_names_expand_symtabs_matching,
43f3e411 5690 dw2_find_pc_sect_compunit_symtab,
71a3c369 5691 NULL,
9291a0cd
TT
5692 dw2_map_symbol_filenames
5693};
5694
4485a1c1
SM
5695/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5696 to either a dwarf2_per_objfile or dwz_file object. */
5697
5698template <typename T>
5699static gdb::array_view<const gdb_byte>
5700get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5701{
5702 dwarf2_section_info *section = &section_owner->gdb_index;
5703
96b79293 5704 if (section->empty ())
4485a1c1
SM
5705 return {};
5706
5707 /* Older elfutils strip versions could keep the section in the main
5708 executable while splitting it for the separate debug info file. */
96b79293 5709 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5710 return {};
5711
96b79293 5712 section->read (obj);
4485a1c1 5713
8bebfcda
PA
5714 /* dwarf2_section_info::size is a bfd_size_type, while
5715 gdb::array_view works with size_t. On 32-bit hosts, with
5716 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5717 is 32-bit. So we need an explicit narrowing conversion here.
5718 This is fine, because it's impossible to allocate or mmap an
5719 array/buffer larger than what size_t can represent. */
5720 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5721}
5722
87d6a7aa
SM
5723/* Lookup the index cache for the contents of the index associated to
5724 DWARF2_OBJ. */
5725
5726static gdb::array_view<const gdb_byte>
5727get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5728{
5729 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5730 if (build_id == nullptr)
5731 return {};
5732
5733 return global_index_cache.lookup_gdb_index (build_id,
5734 &dwarf2_obj->index_cache_res);
5735}
5736
5737/* Same as the above, but for DWZ. */
5738
5739static gdb::array_view<const gdb_byte>
5740get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5741{
5742 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5743 if (build_id == nullptr)
5744 return {};
5745
5746 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5747}
5748
3c0aa29a 5749/* See symfile.h. */
9291a0cd 5750
3c0aa29a
PA
5751bool
5752dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5753{
ed2dc618
SM
5754 struct dwarf2_per_objfile *dwarf2_per_objfile
5755 = get_dwarf2_per_objfile (objfile);
5756
9291a0cd
TT
5757 /* If we're about to read full symbols, don't bother with the
5758 indices. In this case we also don't care if some other debug
5759 format is making psymtabs, because they are all about to be
5760 expanded anyway. */
5761 if ((objfile->flags & OBJF_READNOW))
5762 {
9291a0cd 5763 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5764 create_all_comp_units (dwarf2_per_objfile);
5765 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5766 dwarf2_per_objfile->quick_file_names_table
5767 = create_quick_file_names_table
5768 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5769
b76e467d 5770 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5771 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5772 {
ff4c9fec 5773 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5774
e254ef6a
DE
5775 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5776 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5777 }
5778
5779 /* Return 1 so that gdb sees the "quick" functions. However,
5780 these functions will be no-ops because we will have expanded
5781 all symtabs. */
3c0aa29a
PA
5782 *index_kind = dw_index_kind::GDB_INDEX;
5783 return true;
9291a0cd
TT
5784 }
5785
ed2dc618 5786 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5787 {
5788 *index_kind = dw_index_kind::DEBUG_NAMES;
5789 return true;
5790 }
927aa2e7 5791
4485a1c1
SM
5792 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5793 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5794 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5795 {
5796 *index_kind = dw_index_kind::GDB_INDEX;
5797 return true;
5798 }
9291a0cd 5799
87d6a7aa
SM
5800 /* ... otherwise, try to find the index in the index cache. */
5801 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5802 get_gdb_index_contents_from_cache,
5803 get_gdb_index_contents_from_cache_dwz))
5804 {
5805 global_index_cache.hit ();
5806 *index_kind = dw_index_kind::GDB_INDEX;
5807 return true;
5808 }
5809
5810 global_index_cache.miss ();
3c0aa29a 5811 return false;
9291a0cd
TT
5812}
5813
5814\f
5815
dce234bc
PP
5816/* Build a partial symbol table. */
5817
5818void
f29dff0a 5819dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5820{
ed2dc618
SM
5821 struct dwarf2_per_objfile *dwarf2_per_objfile
5822 = get_dwarf2_per_objfile (objfile);
c9bf0622 5823
6eee24ce 5824 init_psymbol_list (objfile, 1024);
c906108c 5825
a70b8144 5826 try
c9bf0622
TT
5827 {
5828 /* This isn't really ideal: all the data we allocate on the
5829 objfile's obstack is still uselessly kept around. However,
5830 freeing it seems unsafe. */
906768f9 5831 psymtab_discarder psymtabs (objfile);
ed2dc618 5832 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5833 psymtabs.keep ();
87d6a7aa
SM
5834
5835 /* (maybe) store an index in the cache. */
5836 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5837 }
230d2906 5838 catch (const gdb_exception_error &except)
492d29ea
PA
5839 {
5840 exception_print (gdb_stderr, except);
5841 }
c906108c 5842}
c906108c 5843
3b80fe9b
DE
5844/* Find the base address of the compilation unit for range lists and
5845 location lists. It will normally be specified by DW_AT_low_pc.
5846 In DWARF-3 draft 4, the base address could be overridden by
5847 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5848 compilation units with discontinuous ranges. */
5849
5850static void
5851dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5852{
5853 struct attribute *attr;
5854
5855 cu->base_known = 0;
5856 cu->base_address = 0;
5857
5858 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5859 if (attr != nullptr)
3b80fe9b 5860 {
cd6c91b4 5861 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5862 cu->base_known = 1;
5863 }
5864 else
5865 {
5866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5867 if (attr != nullptr)
3b80fe9b 5868 {
cd6c91b4 5869 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5870 cu->base_known = 1;
5871 }
5872 }
5873}
5874
36586728
TT
5875/* Helper function that returns the proper abbrev section for
5876 THIS_CU. */
5877
5878static struct dwarf2_section_info *
5879get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5880{
5881 struct dwarf2_section_info *abbrev;
ed2dc618 5882 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5883
5884 if (this_cu->is_dwz)
ed2dc618 5885 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5886 else
5887 abbrev = &dwarf2_per_objfile->abbrev;
5888
5889 return abbrev;
5890}
5891
f4dc4d17
DE
5892/* Fetch the abbreviation table offset from a comp or type unit header. */
5893
5894static sect_offset
ed2dc618
SM
5895read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5896 struct dwarf2_section_info *section,
9c541725 5897 sect_offset sect_off)
f4dc4d17 5898{
96b79293 5899 bfd *abfd = section->get_bfd_owner ();
d521ce57 5900 const gdb_byte *info_ptr;
ac298888 5901 unsigned int initial_length_size, offset_size;
43988095 5902 uint16_t version;
f4dc4d17 5903
96b79293 5904 section->read (dwarf2_per_objfile->objfile);
9c541725 5905 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5906 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5907 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5908 info_ptr += initial_length_size;
5909
5910 version = read_2_bytes (abfd, info_ptr);
5911 info_ptr += 2;
5912 if (version >= 5)
5913 {
5914 /* Skip unit type and address size. */
5915 info_ptr += 2;
5916 }
5917
24aa364d 5918 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5919}
5920
aaa75496
JB
5921/* Allocate a new partial symtab for file named NAME and mark this new
5922 partial symtab as being an include of PST. */
5923
5924static void
891813be 5925dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5926 struct objfile *objfile)
5927{
891813be 5928 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 5929
fbd9ab74
JK
5930 if (!IS_ABSOLUTE_PATH (subpst->filename))
5931 {
5932 /* It shares objfile->objfile_obstack. */
5933 subpst->dirname = pst->dirname;
5934 }
5935
a9342b62 5936 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5937 subpst->dependencies[0] = pst;
5938 subpst->number_of_dependencies = 1;
5939
aaa75496
JB
5940 /* No private part is necessary for include psymtabs. This property
5941 can be used to differentiate between such include psymtabs and
10b3939b 5942 the regular ones. */
891813be 5943 subpst->per_cu_data = nullptr;
aaa75496
JB
5944}
5945
5946/* Read the Line Number Program data and extract the list of files
5947 included by the source file represented by PST. Build an include
d85a05f0 5948 partial symtab for each of these included files. */
aaa75496
JB
5949
5950static void
5951dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5952 struct die_info *die,
891813be 5953 dwarf2_psymtab *pst)
aaa75496 5954{
fff8551c 5955 line_header_up lh;
d85a05f0 5956 struct attribute *attr;
aaa75496 5957
d85a05f0 5958 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5959 if (attr != nullptr)
9c541725 5960 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5961 if (lh == NULL)
5962 return; /* No linetable, so no includes. */
5963
79748972
TT
5964 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5965 that we pass in the raw text_low here; that is ok because we're
5966 only decoding the line table to make include partial symtabs, and
5967 so the addresses aren't really used. */
4ae976d1 5968 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5969 pst->raw_text_low (), 1);
aaa75496
JB
5970}
5971
348e048f 5972static hashval_t
52dc124a 5973hash_signatured_type (const void *item)
348e048f 5974{
9a3c8263
SM
5975 const struct signatured_type *sig_type
5976 = (const struct signatured_type *) item;
9a619af0 5977
348e048f 5978 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5979 return sig_type->signature;
348e048f
DE
5980}
5981
5982static int
52dc124a 5983eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5984{
9a3c8263
SM
5985 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5986 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5987
348e048f
DE
5988 return lhs->signature == rhs->signature;
5989}
5990
1fd400ff
TT
5991/* Allocate a hash table for signatured types. */
5992
b0b6a987 5993static htab_up
298e9637 5994allocate_signatured_type_table ()
1fd400ff 5995{
b0b6a987
TT
5996 return htab_up (htab_create_alloc (41,
5997 hash_signatured_type,
5998 eq_signatured_type,
5999 NULL, xcalloc, xfree));
1fd400ff
TT
6000}
6001
d467dd73 6002/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6003
6004static int
d467dd73 6005add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6006{
9a3c8263 6007 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6008 std::vector<signatured_type *> *all_type_units
6009 = (std::vector<signatured_type *> *) datum;
1fd400ff 6010
b2bdb8cf 6011 all_type_units->push_back (sigt);
1fd400ff
TT
6012
6013 return 1;
6014}
6015
78d4d2c5 6016/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6017 and fill them into TYPES_HTAB. It will process only type units,
6018 therefore DW_UT_type. */
c88ee1f0 6019
78d4d2c5 6020static void
ed2dc618
SM
6021create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6022 struct dwo_file *dwo_file,
b0b6a987 6023 dwarf2_section_info *section, htab_up &types_htab,
43988095 6024 rcuh_kind section_kind)
348e048f 6025{
3019eac3 6026 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6027 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6028 bfd *abfd;
6029 const gdb_byte *info_ptr, *end_ptr;
348e048f 6030
4bdcc0c1
DE
6031 abbrev_section = (dwo_file != NULL
6032 ? &dwo_file->sections.abbrev
6033 : &dwarf2_per_objfile->abbrev);
6034
b4f54984 6035 if (dwarf_read_debug)
43988095 6036 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6037 section->get_name (),
6038 abbrev_section->get_file_name ());
09406207 6039
96b79293 6040 section->read (objfile);
78d4d2c5 6041 info_ptr = section->buffer;
348e048f 6042
78d4d2c5
JK
6043 if (info_ptr == NULL)
6044 return;
348e048f 6045
78d4d2c5
JK
6046 /* We can't set abfd until now because the section may be empty or
6047 not present, in which case the bfd is unknown. */
96b79293 6048 abfd = section->get_bfd_owner ();
348e048f 6049
c0ab21c2
TT
6050 /* We don't use cutu_reader here because we don't need to read
6051 any dies: the signature is in the header. */
3019eac3 6052
78d4d2c5
JK
6053 end_ptr = info_ptr + section->size;
6054 while (info_ptr < end_ptr)
6055 {
78d4d2c5
JK
6056 struct signatured_type *sig_type;
6057 struct dwo_unit *dwo_tu;
6058 void **slot;
6059 const gdb_byte *ptr = info_ptr;
6060 struct comp_unit_head header;
6061 unsigned int length;
8b70b953 6062
9c541725 6063 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6064
a49dd8dd
JK
6065 /* Initialize it due to a false compiler warning. */
6066 header.signature = -1;
9c541725 6067 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6068
78d4d2c5
JK
6069 /* We need to read the type's signature in order to build the hash
6070 table, but we don't need anything else just yet. */
348e048f 6071
ed2dc618 6072 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6073 abbrev_section, ptr, section_kind);
348e048f 6074
4057dfde 6075 length = header.get_length ();
6caca83c 6076
78d4d2c5
JK
6077 /* Skip dummy type units. */
6078 if (ptr >= info_ptr + length
43988095
JK
6079 || peek_abbrev_code (abfd, ptr) == 0
6080 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6081 {
6082 info_ptr += length;
6083 continue;
6084 }
dee91e82 6085
78d4d2c5
JK
6086 if (types_htab == NULL)
6087 {
6088 if (dwo_file)
298e9637 6089 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6090 else
298e9637 6091 types_htab = allocate_signatured_type_table ();
78d4d2c5 6092 }
8b70b953 6093
78d4d2c5
JK
6094 if (dwo_file)
6095 {
6096 sig_type = NULL;
6097 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6098 struct dwo_unit);
6099 dwo_tu->dwo_file = dwo_file;
43988095 6100 dwo_tu->signature = header.signature;
9c541725 6101 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6102 dwo_tu->section = section;
9c541725 6103 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6104 dwo_tu->length = length;
6105 }
6106 else
6107 {
6108 /* N.B.: type_offset is not usable if this type uses a DWO file.
6109 The real type_offset is in the DWO file. */
6110 dwo_tu = NULL;
6111 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6112 struct signatured_type);
43988095 6113 sig_type->signature = header.signature;
9c541725 6114 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6115 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6116 sig_type->per_cu.is_debug_types = 1;
6117 sig_type->per_cu.section = section;
9c541725 6118 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6119 sig_type->per_cu.length = length;
6120 }
6121
b0b6a987 6122 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6123 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6124 INSERT);
6125 gdb_assert (slot != NULL);
6126 if (*slot != NULL)
6127 {
9c541725 6128 sect_offset dup_sect_off;
0349ea22 6129
3019eac3
DE
6130 if (dwo_file)
6131 {
78d4d2c5
JK
6132 const struct dwo_unit *dup_tu
6133 = (const struct dwo_unit *) *slot;
6134
9c541725 6135 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6136 }
6137 else
6138 {
78d4d2c5
JK
6139 const struct signatured_type *dup_tu
6140 = (const struct signatured_type *) *slot;
6141
9c541725 6142 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6143 }
8b70b953 6144
b98664d3 6145 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6146 " the entry at offset %s, signature %s"),
6147 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6148 hex_string (header.signature));
78d4d2c5
JK
6149 }
6150 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6151
78d4d2c5 6152 if (dwarf_read_debug > 1)
9d8780f0
SM
6153 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6154 sect_offset_str (sect_off),
43988095 6155 hex_string (header.signature));
3019eac3 6156
78d4d2c5
JK
6157 info_ptr += length;
6158 }
6159}
3019eac3 6160
78d4d2c5
JK
6161/* Create the hash table of all entries in the .debug_types
6162 (or .debug_types.dwo) section(s).
6163 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6164 otherwise it is NULL.
b3c8eb43 6165
78d4d2c5 6166 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6167
78d4d2c5 6168 Note: This function processes DWO files only, not DWP files. */
348e048f 6169
78d4d2c5 6170static void
ed2dc618
SM
6171create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6172 struct dwo_file *dwo_file,
fd5866f6 6173 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6174 htab_up &types_htab)
78d4d2c5 6175{
fd5866f6
SM
6176 for (dwarf2_section_info &section : type_sections)
6177 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6178 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6179}
6180
6181/* Create the hash table of all entries in the .debug_types section,
6182 and initialize all_type_units.
6183 The result is zero if there is an error (e.g. missing .debug_types section),
6184 otherwise non-zero. */
6185
6186static int
ed2dc618 6187create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6188{
b0b6a987 6189 htab_up types_htab;
3019eac3 6190
ed2dc618
SM
6191 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6192 &dwarf2_per_objfile->info, types_htab,
43988095 6193 rcuh_kind::COMPILE);
ed2dc618
SM
6194 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6195 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6196 if (types_htab == NULL)
6197 {
6198 dwarf2_per_objfile->signatured_types = NULL;
6199 return 0;
6200 }
6201
b0b6a987 6202 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6203
b2bdb8cf 6204 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6205 dwarf2_per_objfile->all_type_units.reserve
6206 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6207
b0b6a987
TT
6208 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6209 add_signatured_type_cu_to_table,
b2bdb8cf 6210 &dwarf2_per_objfile->all_type_units);
1fd400ff 6211
348e048f
DE
6212 return 1;
6213}
6214
6aa5f3a6
DE
6215/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6216 If SLOT is non-NULL, it is the entry to use in the hash table.
6217 Otherwise we find one. */
6218
6219static struct signatured_type *
ed2dc618
SM
6220add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6221 void **slot)
6aa5f3a6
DE
6222{
6223 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6224
b2bdb8cf
SM
6225 if (dwarf2_per_objfile->all_type_units.size ()
6226 == dwarf2_per_objfile->all_type_units.capacity ())
6227 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6228
b2bdb8cf
SM
6229 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6230 struct signatured_type);
6231
6232 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6233 sig_type->signature = sig;
6234 sig_type->per_cu.is_debug_types = 1;
6235 if (dwarf2_per_objfile->using_index)
6236 {
6237 sig_type->per_cu.v.quick =
6238 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6239 struct dwarf2_per_cu_quick_data);
6240 }
6241
6242 if (slot == NULL)
6243 {
b0b6a987 6244 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6245 sig_type, INSERT);
6246 }
6247 gdb_assert (*slot == NULL);
6248 *slot = sig_type;
6249 /* The rest of sig_type must be filled in by the caller. */
6250 return sig_type;
6251}
6252
a2ce51a0
DE
6253/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6254 Fill in SIG_ENTRY with DWO_ENTRY. */
6255
6256static void
ed2dc618 6257fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6258 struct signatured_type *sig_entry,
6259 struct dwo_unit *dwo_entry)
6260{
7ee85ab1 6261 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6262 gdb_assert (! sig_entry->per_cu.queued);
6263 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6264 if (dwarf2_per_objfile->using_index)
6265 {
6266 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6267 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6268 }
6269 else
6270 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6271 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6272 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6273 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6274 gdb_assert (sig_entry->dwo_unit == NULL);
6275
6276 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6277 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6278 sig_entry->per_cu.length = dwo_entry->length;
6279 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6280 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6281 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6282 sig_entry->dwo_unit = dwo_entry;
6283}
6284
6285/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6286 If we haven't read the TU yet, create the signatured_type data structure
6287 for a TU to be read in directly from a DWO file, bypassing the stub.
6288 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6289 using .gdb_index, then when reading a CU we want to stay in the DWO file
6290 containing that CU. Otherwise we could end up reading several other DWO
6291 files (due to comdat folding) to process the transitive closure of all the
6292 mentioned TUs, and that can be slow. The current DWO file will have every
6293 type signature that it needs.
a2ce51a0
DE
6294 We only do this for .gdb_index because in the psymtab case we already have
6295 to read all the DWOs to build the type unit groups. */
6296
6297static struct signatured_type *
6298lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6299{
518817b3
SM
6300 struct dwarf2_per_objfile *dwarf2_per_objfile
6301 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6302 struct dwo_file *dwo_file;
6303 struct dwo_unit find_dwo_entry, *dwo_entry;
6304 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6305 void **slot;
a2ce51a0
DE
6306
6307 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6308
6aa5f3a6
DE
6309 /* If TU skeletons have been removed then we may not have read in any
6310 TUs yet. */
6311 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6312 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6313
6314 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6315 Use the global signatured_types array to do our own comdat-folding
6316 of types. If this is the first time we're reading this TU, and
6317 the TU has an entry in .gdb_index, replace the recorded data from
6318 .gdb_index with this TU. */
a2ce51a0 6319
a2ce51a0 6320 find_sig_entry.signature = sig;
b0b6a987 6321 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6322 &find_sig_entry, INSERT);
9a3c8263 6323 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6324
6325 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6326 read. Don't reassign the global entry to point to this DWO if that's
6327 the case. Also note that if the TU is already being read, it may not
6328 have come from a DWO, the program may be a mix of Fission-compiled
6329 code and non-Fission-compiled code. */
6330
6331 /* Have we already tried to read this TU?
6332 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6333 needn't exist in the global table yet). */
6334 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6335 return sig_entry;
6336
6aa5f3a6
DE
6337 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6338 dwo_unit of the TU itself. */
6339 dwo_file = cu->dwo_unit->dwo_file;
6340
a2ce51a0
DE
6341 /* Ok, this is the first time we're reading this TU. */
6342 if (dwo_file->tus == NULL)
6343 return NULL;
6344 find_dwo_entry.signature = sig;
b0b6a987
TT
6345 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6346 &find_dwo_entry);
a2ce51a0
DE
6347 if (dwo_entry == NULL)
6348 return NULL;
6349
6aa5f3a6
DE
6350 /* If the global table doesn't have an entry for this TU, add one. */
6351 if (sig_entry == NULL)
ed2dc618 6352 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6353
ed2dc618 6354 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6355 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6356 return sig_entry;
6357}
6358
a2ce51a0
DE
6359/* Subroutine of lookup_signatured_type.
6360 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6361 then try the DWP file. If the TU stub (skeleton) has been removed then
6362 it won't be in .gdb_index. */
a2ce51a0
DE
6363
6364static struct signatured_type *
6365lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6366{
518817b3
SM
6367 struct dwarf2_per_objfile *dwarf2_per_objfile
6368 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6369 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6370 struct dwo_unit *dwo_entry;
6371 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6372 void **slot;
a2ce51a0
DE
6373
6374 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6375 gdb_assert (dwp_file != NULL);
6376
6aa5f3a6
DE
6377 /* If TU skeletons have been removed then we may not have read in any
6378 TUs yet. */
6379 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6380 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6381
6aa5f3a6 6382 find_sig_entry.signature = sig;
b0b6a987 6383 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6384 &find_sig_entry, INSERT);
9a3c8263 6385 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6386
6387 /* Have we already tried to read this TU?
6388 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6389 needn't exist in the global table yet). */
6390 if (sig_entry != NULL)
6391 return sig_entry;
6392
a2ce51a0
DE
6393 if (dwp_file->tus == NULL)
6394 return NULL;
ed2dc618 6395 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6396 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6397 if (dwo_entry == NULL)
6398 return NULL;
6399
ed2dc618
SM
6400 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6401 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6402
a2ce51a0
DE
6403 return sig_entry;
6404}
6405
380bca97 6406/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6407 Returns NULL if signature SIG is not present in the table.
6408 It is up to the caller to complain about this. */
348e048f
DE
6409
6410static struct signatured_type *
a2ce51a0 6411lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6412{
518817b3
SM
6413 struct dwarf2_per_objfile *dwarf2_per_objfile
6414 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6415
a2ce51a0
DE
6416 if (cu->dwo_unit
6417 && dwarf2_per_objfile->using_index)
6418 {
6419 /* We're in a DWO/DWP file, and we're using .gdb_index.
6420 These cases require special processing. */
ed2dc618 6421 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6422 return lookup_dwo_signatured_type (cu, sig);
6423 else
6424 return lookup_dwp_signatured_type (cu, sig);
6425 }
6426 else
6427 {
6428 struct signatured_type find_entry, *entry;
348e048f 6429
a2ce51a0
DE
6430 if (dwarf2_per_objfile->signatured_types == NULL)
6431 return NULL;
6432 find_entry.signature = sig;
9a3c8263 6433 entry = ((struct signatured_type *)
b0b6a987
TT
6434 htab_find (dwarf2_per_objfile->signatured_types.get (),
6435 &find_entry));
a2ce51a0
DE
6436 return entry;
6437 }
348e048f 6438}
18a8505e
AT
6439
6440/* Return the address base of the compile unit, which, if exists, is stored
6441 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6442static gdb::optional<ULONGEST>
6443lookup_addr_base (struct die_info *comp_unit_die)
6444{
6445 struct attribute *attr;
6446 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6447 if (attr == nullptr)
6448 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6449 if (attr == nullptr)
6450 return gdb::optional<ULONGEST> ();
6451 return DW_UNSND (attr);
6452}
6453
6454/* Return range lists base of the compile unit, which, if exists, is stored
6455 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6456static ULONGEST
6457lookup_ranges_base (struct die_info *comp_unit_die)
6458{
6459 struct attribute *attr;
6460 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6461 if (attr == nullptr)
6462 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6463 if (attr == nullptr)
6464 return 0;
6465 return DW_UNSND (attr);
6466}
6467
42e7ad6c 6468/* Low level DIE reading support. */
348e048f 6469
d85a05f0
DJ
6470/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6471
6472static void
6473init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6474 struct dwarf2_cu *cu,
3019eac3 6475 struct dwarf2_section_info *section,
685af9cd
TT
6476 struct dwo_file *dwo_file,
6477 struct abbrev_table *abbrev_table)
d85a05f0 6478{
fceca515 6479 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6480 reader->abfd = section->get_bfd_owner ();
d85a05f0 6481 reader->cu = cu;
3019eac3 6482 reader->dwo_file = dwo_file;
dee91e82
DE
6483 reader->die_section = section;
6484 reader->buffer = section->buffer;
f664829e 6485 reader->buffer_end = section->buffer + section->size;
685af9cd 6486 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6487}
6488
c0ab21c2 6489/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6490 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6491 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6492 already.
6493
6494 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6495 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6496 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6497 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6498 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6499 STUB_COMP_DIR may be non-NULL.
3e225074 6500 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6501 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6502 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6503 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6504 kept around for at least as long as *RESULT_READER.
6505
b0c7bfa9
DE
6506 The result is non-zero if a valid (non-dummy) DIE was found. */
6507
6508static int
6509read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6510 struct dwo_unit *dwo_unit,
b0c7bfa9 6511 struct die_info *stub_comp_unit_die,
a2ce51a0 6512 const char *stub_comp_dir,
b0c7bfa9 6513 struct die_reader_specs *result_reader,
d521ce57 6514 const gdb_byte **result_info_ptr,
b0c7bfa9 6515 struct die_info **result_comp_unit_die,
685af9cd 6516 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6517{
ed2dc618 6518 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6519 struct objfile *objfile = dwarf2_per_objfile->objfile;
6520 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6521 bfd *abfd;
d521ce57 6522 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6523 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6524 int i,num_extra_attrs;
6525 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6526 struct die_info *comp_unit_die;
6527
b0aeadb3
DE
6528 /* At most one of these may be provided. */
6529 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6530
b0c7bfa9
DE
6531 /* These attributes aren't processed until later:
6532 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6533 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6534 referenced later. However, these attributes are found in the stub
6535 which we won't have later. In order to not impose this complication
6536 on the rest of the code, we read them here and copy them to the
6537 DWO CU/TU die. */
b0c7bfa9
DE
6538
6539 stmt_list = NULL;
6540 low_pc = NULL;
6541 high_pc = NULL;
6542 ranges = NULL;
6543 comp_dir = NULL;
6544
6545 if (stub_comp_unit_die != NULL)
6546 {
6547 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6548 DWO file. */
6549 if (! this_cu->is_debug_types)
6550 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6551 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6552 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6553 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6554 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6555
18a8505e 6556 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6557
18a8505e
AT
6558 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6559 here (if needed). We need the value before we can process
6560 DW_AT_ranges. */
6561 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6562 }
a2ce51a0
DE
6563 else if (stub_comp_dir != NULL)
6564 {
6565 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6566 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6567 comp_dir->name = DW_AT_comp_dir;
6568 comp_dir->form = DW_FORM_string;
6569 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6570 DW_STRING (comp_dir) = stub_comp_dir;
6571 }
b0c7bfa9
DE
6572
6573 /* Set up for reading the DWO CU/TU. */
6574 cu->dwo_unit = dwo_unit;
685af9cd 6575 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6576 section->read (objfile);
6577 abfd = section->get_bfd_owner ();
9c541725
PA
6578 begin_info_ptr = info_ptr = (section->buffer
6579 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6580 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6581
6582 if (this_cu->is_debug_types)
6583 {
b0c7bfa9
DE
6584 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6585
ed2dc618
SM
6586 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6587 &cu->header, section,
b0c7bfa9 6588 dwo_abbrev_section,
43988095 6589 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6590 /* This is not an assert because it can be caused by bad debug info. */
43988095 6591 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6592 {
6593 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6594 " TU at offset %s [in module %s]"),
a2ce51a0 6595 hex_string (sig_type->signature),
43988095 6596 hex_string (cu->header.signature),
9d8780f0 6597 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6598 bfd_get_filename (abfd));
6599 }
9c541725 6600 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6601 /* For DWOs coming from DWP files, we don't know the CU length
6602 nor the type's offset in the TU until now. */
4057dfde 6603 dwo_unit->length = cu->header.get_length ();
9c541725 6604 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6605
6606 /* Establish the type offset that can be used to lookup the type.
6607 For DWO files, we don't know it until now. */
9c541725
PA
6608 sig_type->type_offset_in_section
6609 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6610 }
6611 else
6612 {
ed2dc618
SM
6613 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6614 &cu->header, section,
b0c7bfa9 6615 dwo_abbrev_section,
43988095 6616 info_ptr, rcuh_kind::COMPILE);
9c541725 6617 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6618 /* For DWOs coming from DWP files, we don't know the CU length
6619 until now. */
4057dfde 6620 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6621 }
6622
685af9cd 6623 *result_dwo_abbrev_table
86de1d91
TT
6624 = abbrev_table::read (objfile, dwo_abbrev_section,
6625 cu->header.abbrev_sect_off);
685af9cd
TT
6626 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6627 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6628
6629 /* Read in the die, but leave space to copy over the attributes
6630 from the stub. This has the benefit of simplifying the rest of
6631 the code - all the work to maintain the illusion of a single
6632 DW_TAG_{compile,type}_unit DIE is done here. */
6633 num_extra_attrs = ((stmt_list != NULL)
6634 + (low_pc != NULL)
6635 + (high_pc != NULL)
6636 + (ranges != NULL)
6637 + (comp_dir != NULL));
6638 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6639 num_extra_attrs);
b0c7bfa9
DE
6640
6641 /* Copy over the attributes from the stub to the DIE we just read in. */
6642 comp_unit_die = *result_comp_unit_die;
6643 i = comp_unit_die->num_attrs;
6644 if (stmt_list != NULL)
6645 comp_unit_die->attrs[i++] = *stmt_list;
6646 if (low_pc != NULL)
6647 comp_unit_die->attrs[i++] = *low_pc;
6648 if (high_pc != NULL)
6649 comp_unit_die->attrs[i++] = *high_pc;
6650 if (ranges != NULL)
6651 comp_unit_die->attrs[i++] = *ranges;
6652 if (comp_dir != NULL)
6653 comp_unit_die->attrs[i++] = *comp_dir;
6654 comp_unit_die->num_attrs += num_extra_attrs;
6655
b4f54984 6656 if (dwarf_die_debug)
bf6af496
DE
6657 {
6658 fprintf_unfiltered (gdb_stdlog,
6659 "Read die from %s@0x%x of %s:\n",
96b79293 6660 section->get_name (),
bf6af496
DE
6661 (unsigned) (begin_info_ptr - section->buffer),
6662 bfd_get_filename (abfd));
b4f54984 6663 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6664 }
6665
b0c7bfa9
DE
6666 /* Skip dummy compilation units. */
6667 if (info_ptr >= begin_info_ptr + dwo_unit->length
6668 || peek_abbrev_code (abfd, info_ptr) == 0)
6669 return 0;
6670
6671 *result_info_ptr = info_ptr;
6672 return 1;
6673}
6674
a084a2a6
AT
6675/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6676 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6677 signature is part of the header. */
6678static gdb::optional<ULONGEST>
6679lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6680{
6681 if (cu->header.version >= 5)
6682 return cu->header.signature;
6683 struct attribute *attr;
6684 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6685 if (attr == nullptr)
6686 return gdb::optional<ULONGEST> ();
6687 return DW_UNSND (attr);
6688}
6689
c0ab21c2 6690/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6691 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6692 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6693
6694static struct dwo_unit *
6695lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6696 struct die_info *comp_unit_die,
6697 const char *dwo_name)
b0c7bfa9
DE
6698{
6699 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6700 struct dwo_unit *dwo_unit;
c0ab21c2 6701 const char *comp_dir;
b0c7bfa9 6702
a2ce51a0
DE
6703 gdb_assert (cu != NULL);
6704
b0c7bfa9 6705 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6706 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6707 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6708
6709 if (this_cu->is_debug_types)
6710 {
6711 struct signatured_type *sig_type;
6712
6713 /* Since this_cu is the first member of struct signatured_type,
6714 we can go from a pointer to one to a pointer to the other. */
6715 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6716 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6717 }
6718 else
6719 {
a084a2a6
AT
6720 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6721 if (!signature.has_value ())
b0c7bfa9
DE
6722 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6723 " [in module %s]"),
e3b94546 6724 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6725 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6726 *signature);
b0c7bfa9
DE
6727 }
6728
b0c7bfa9
DE
6729 return dwo_unit;
6730}
6731
c0ab21c2 6732/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6733 See it for a description of the parameters.
fcd3b13d 6734 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6735
c0ab21c2
TT
6736void
6737cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6738 int use_existing_cu)
a2ce51a0 6739{
a2ce51a0 6740 struct signatured_type *sig_type;
a2ce51a0 6741 struct die_reader_specs reader;
a2ce51a0
DE
6742
6743 /* Verify we can do the following downcast, and that we have the
6744 data we need. */
6745 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6746 sig_type = (struct signatured_type *) this_cu;
6747 gdb_assert (sig_type->dwo_unit != NULL);
6748
6aa5f3a6
DE
6749 if (use_existing_cu && this_cu->cu != NULL)
6750 {
6751 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6752 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6753 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6754 }
6755 else
6756 {
6757 /* If !use_existing_cu, this_cu->cu must be NULL. */
6758 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6759 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6760 }
6761
6762 /* A future optimization, if needed, would be to use an existing
6763 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6764 could share abbrev tables. */
a2ce51a0
DE
6765
6766 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6767 NULL /* stub_comp_unit_die */,
6768 sig_type->dwo_unit->dwo_file->comp_dir,
6769 &reader, &info_ptr,
3e225074 6770 &comp_unit_die,
c0ab21c2 6771 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6772 {
6773 /* Dummy die. */
c0ab21c2 6774 dummy_p = true;
a2ce51a0 6775 }
a2ce51a0
DE
6776}
6777
fd820528 6778/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6779 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6780
f4dc4d17
DE
6781 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6782 Otherwise the table specified in the comp unit header is read in and used.
6783 This is an optimization for when we already have the abbrev table.
6784
dee91e82 6785 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6786 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6787
c0ab21c2
TT
6788cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6789 struct abbrev_table *abbrev_table,
6751ebae 6790 int use_existing_cu,
c0ab21c2
TT
6791 bool skip_partial)
6792 : die_reader_specs {},
6751ebae 6793 m_this_cu (this_cu)
c906108c 6794{
ed2dc618 6795 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6796 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6797 struct dwarf2_section_info *section = this_cu->section;
96b79293 6798 bfd *abfd = section->get_bfd_owner ();
dee91e82 6799 struct dwarf2_cu *cu;
c0ab21c2 6800 const gdb_byte *begin_info_ptr;
dee91e82 6801 struct signatured_type *sig_type = NULL;
4bdcc0c1 6802 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6803 /* Non-zero if CU currently points to a DWO file and we need to
6804 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6805 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6806 int rereading_dwo_cu = 0;
c906108c 6807
b4f54984 6808 if (dwarf_die_debug)
9d8780f0 6809 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6810 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6811 sect_offset_str (this_cu->sect_off));
09406207 6812
a2ce51a0
DE
6813 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6814 file (instead of going through the stub), short-circuit all of this. */
6815 if (this_cu->reading_dwo_directly)
6816 {
6817 /* Narrow down the scope of possibilities to have to understand. */
6818 gdb_assert (this_cu->is_debug_types);
6819 gdb_assert (abbrev_table == NULL);
6751ebae 6820 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6821 return;
6822 }
6823
dee91e82 6824 /* This is cheap if the section is already read in. */
96b79293 6825 section->read (objfile);
dee91e82 6826
9c541725 6827 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6828
6829 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6830
6831 if (use_existing_cu && this_cu->cu != NULL)
6832 {
6833 cu = this_cu->cu;
42e7ad6c
DE
6834 /* If this CU is from a DWO file we need to start over, we need to
6835 refetch the attributes from the skeleton CU.
6836 This could be optimized by retrieving those attributes from when we
6837 were here the first time: the previous comp_unit_die was stored in
6838 comp_unit_obstack. But there's no data yet that we need this
6839 optimization. */
6840 if (cu->dwo_unit != NULL)
6841 rereading_dwo_cu = 1;
dee91e82
DE
6842 }
6843 else
6844 {
6845 /* If !use_existing_cu, this_cu->cu must be NULL. */
6846 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6847 m_new_cu.reset (new dwarf2_cu (this_cu));
6848 cu = m_new_cu.get ();
42e7ad6c 6849 }
dee91e82 6850
b0c7bfa9 6851 /* Get the header. */
9c541725 6852 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6853 {
6854 /* We already have the header, there's no need to read it in again. */
9c541725 6855 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6856 }
6857 else
6858 {
3019eac3 6859 if (this_cu->is_debug_types)
dee91e82 6860 {
ed2dc618
SM
6861 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6862 &cu->header, section,
4bdcc0c1 6863 abbrev_section, info_ptr,
43988095 6864 rcuh_kind::TYPE);
dee91e82 6865
42e7ad6c
DE
6866 /* Since per_cu is the first member of struct signatured_type,
6867 we can go from a pointer to one to a pointer to the other. */
6868 sig_type = (struct signatured_type *) this_cu;
43988095 6869 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6870 gdb_assert (sig_type->type_offset_in_tu
6871 == cu->header.type_cu_offset_in_tu);
6872 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6873
42e7ad6c
DE
6874 /* LENGTH has not been set yet for type units if we're
6875 using .gdb_index. */
4057dfde 6876 this_cu->length = cu->header.get_length ();
3019eac3
DE
6877
6878 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6879 sig_type->type_offset_in_section =
6880 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6881
6882 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6883 }
6884 else
6885 {
ed2dc618
SM
6886 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6887 &cu->header, section,
4bdcc0c1 6888 abbrev_section,
43988095
JK
6889 info_ptr,
6890 rcuh_kind::COMPILE);
dee91e82 6891
9c541725 6892 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6893 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6894 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6895 }
6896 }
10b3939b 6897
6caca83c 6898 /* Skip dummy compilation units. */
dee91e82 6899 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6900 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6901 {
6902 dummy_p = true;
6903 return;
6904 }
6caca83c 6905
433df2d4
DE
6906 /* If we don't have them yet, read the abbrevs for this compilation unit.
6907 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6908 done. */
f4dc4d17 6909 if (abbrev_table != NULL)
685af9cd
TT
6910 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6911 else
f4dc4d17 6912 {
c0ab21c2 6913 m_abbrev_table_holder
86de1d91
TT
6914 = abbrev_table::read (objfile, abbrev_section,
6915 cu->header.abbrev_sect_off);
c0ab21c2 6916 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6917 }
af703f96 6918
dee91e82 6919 /* Read the top level CU/TU die. */
c0ab21c2 6920 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6921 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6922
58f0c718 6923 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6924 {
6925 dummy_p = true;
6926 return;
6927 }
58f0c718 6928
b0c7bfa9 6929 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6930 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6931 table from the DWO file and pass the ownership over to us. It will be
6932 referenced from READER, so we must make sure to free it after we're done
6933 with READER.
6934
b0c7bfa9
DE
6935 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6936 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6937 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6938 if (dwo_name != nullptr)
3019eac3 6939 {
3019eac3 6940 struct dwo_unit *dwo_unit;
b0c7bfa9 6941 struct die_info *dwo_comp_unit_die;
3019eac3 6942
3e225074 6943 if (comp_unit_die->has_children)
6a506a2d 6944 {
b98664d3 6945 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6946 " has children (offset %s) [in module %s]"),
6947 sect_offset_str (this_cu->sect_off),
6948 bfd_get_filename (abfd));
6a506a2d 6949 }
c0ab21c2 6950 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6951 if (dwo_unit != NULL)
3019eac3 6952 {
6a506a2d 6953 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6954 comp_unit_die, NULL,
c0ab21c2 6955 this, &info_ptr,
3e225074 6956 &dwo_comp_unit_die,
c0ab21c2 6957 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6958 {
6959 /* Dummy die. */
c0ab21c2 6960 dummy_p = true;
6a506a2d
DE
6961 return;
6962 }
6963 comp_unit_die = dwo_comp_unit_die;
6964 }
6965 else
6966 {
6967 /* Yikes, we couldn't find the rest of the DIE, we only have
6968 the stub. A complaint has already been logged. There's
6969 not much more we can do except pass on the stub DIE to
6970 die_reader_func. We don't want to throw an error on bad
6971 debug info. */
3019eac3
DE
6972 }
6973 }
c0ab21c2 6974}
3019eac3 6975
6751ebae
TT
6976void
6977cutu_reader::keep ()
c0ab21c2 6978{
b0c7bfa9 6979 /* Done, clean up. */
6751ebae
TT
6980 gdb_assert (!dummy_p);
6981 if (m_new_cu != NULL)
348e048f 6982 {
c0ab21c2
TT
6983 struct dwarf2_per_objfile *dwarf2_per_objfile
6984 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6985 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6986 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6987 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6988 /* The chain owns it now. */
c0ab21c2 6989 m_new_cu.release ();
348e048f 6990 }
dee91e82
DE
6991}
6992
18a8505e
AT
6993/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6994 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6995 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6996
6997 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6998 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6999
7000 We fill in THIS_CU->length.
7001
dee91e82 7002 THIS_CU->cu is always freed when done.
3019eac3 7003 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7004 to care whether it refers to the "main" CU or the DWO CU.
7005
7006 When parent_cu is passed, it is used to provide a default value for
7007 str_offsets_base and addr_base from the parent. */
dee91e82 7008
c0ab21c2
TT
7009cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7010 struct dwarf2_cu *parent_cu,
7011 struct dwo_file *dwo_file)
7012 : die_reader_specs {},
7013 m_this_cu (this_cu)
dee91e82 7014{
ed2dc618 7015 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7016 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7017 struct dwarf2_section_info *section = this_cu->section;
96b79293 7018 bfd *abfd = section->get_bfd_owner ();
33e80786 7019 struct dwarf2_section_info *abbrev_section;
d521ce57 7020 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7021
b4f54984 7022 if (dwarf_die_debug)
9d8780f0 7023 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7024 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7025 sect_offset_str (this_cu->sect_off));
09406207 7026
dee91e82
DE
7027 gdb_assert (this_cu->cu == NULL);
7028
33e80786
DE
7029 abbrev_section = (dwo_file != NULL
7030 ? &dwo_file->sections.abbrev
7031 : get_abbrev_section_for_cu (this_cu));
7032
dee91e82 7033 /* This is cheap if the section is already read in. */
96b79293 7034 section->read (objfile);
dee91e82 7035
c0ab21c2 7036 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7037
9c541725 7038 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7039 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7040 &m_new_cu->header, section,
4bdcc0c1 7041 abbrev_section, info_ptr,
43988095
JK
7042 (this_cu->is_debug_types
7043 ? rcuh_kind::TYPE
7044 : rcuh_kind::COMPILE));
dee91e82 7045
18a8505e
AT
7046 if (parent_cu != nullptr)
7047 {
c0ab21c2
TT
7048 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7049 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7050 }
4057dfde 7051 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7052
7053 /* Skip dummy compilation units. */
7054 if (info_ptr >= begin_info_ptr + this_cu->length
7055 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7056 {
7057 dummy_p = true;
7058 return;
7059 }
72bf9492 7060
c0ab21c2 7061 m_abbrev_table_holder
86de1d91
TT
7062 = abbrev_table::read (objfile, abbrev_section,
7063 m_new_cu->header.abbrev_sect_off);
dee91e82 7064
c0ab21c2
TT
7065 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7066 m_abbrev_table_holder.get ());
3e225074 7067 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7068}
7069
0018ea6f
DE
7070\f
7071/* Type Unit Groups.
dee91e82 7072
0018ea6f
DE
7073 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7074 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7075 so that all types coming from the same compilation (.o file) are grouped
7076 together. A future step could be to put the types in the same symtab as
7077 the CU the types ultimately came from. */
ff013f42 7078
f4dc4d17
DE
7079static hashval_t
7080hash_type_unit_group (const void *item)
7081{
9a3c8263
SM
7082 const struct type_unit_group *tu_group
7083 = (const struct type_unit_group *) item;
f4dc4d17 7084
094b34ac 7085 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7086}
348e048f
DE
7087
7088static int
f4dc4d17 7089eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7090{
9a3c8263
SM
7091 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7092 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7093
094b34ac 7094 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7095}
348e048f 7096
f4dc4d17
DE
7097/* Allocate a hash table for type unit groups. */
7098
eaa5fa8b 7099static htab_up
298e9637 7100allocate_type_unit_groups_table ()
f4dc4d17 7101{
eaa5fa8b
TT
7102 return htab_up (htab_create_alloc (3,
7103 hash_type_unit_group,
7104 eq_type_unit_group,
7105 NULL, xcalloc, xfree));
f4dc4d17 7106}
dee91e82 7107
f4dc4d17
DE
7108/* Type units that don't have DW_AT_stmt_list are grouped into their own
7109 partial symtabs. We combine several TUs per psymtab to not let the size
7110 of any one psymtab grow too big. */
7111#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7112#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7113
094b34ac 7114/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7115 Create the type_unit_group object used to hold one or more TUs. */
7116
7117static struct type_unit_group *
094b34ac 7118create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7119{
518817b3
SM
7120 struct dwarf2_per_objfile *dwarf2_per_objfile
7121 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7122 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7123 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7124 struct type_unit_group *tu_group;
f4dc4d17
DE
7125
7126 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7127 struct type_unit_group);
094b34ac 7128 per_cu = &tu_group->per_cu;
518817b3 7129 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7130
094b34ac
DE
7131 if (dwarf2_per_objfile->using_index)
7132 {
7133 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7134 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7135 }
7136 else
7137 {
9c541725 7138 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7139 dwarf2_psymtab *pst;
528e1572 7140 std::string name;
094b34ac
DE
7141
7142 /* Give the symtab a useful name for debug purposes. */
7143 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7144 name = string_printf ("<type_units_%d>",
7145 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7146 else
528e1572 7147 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7148
528e1572 7149 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7150 pst->anonymous = true;
094b34ac 7151 }
f4dc4d17 7152
094b34ac 7153 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7154 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7155
7156 return tu_group;
7157}
7158
094b34ac
DE
7159/* Look up the type_unit_group for type unit CU, and create it if necessary.
7160 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7161
7162static struct type_unit_group *
ff39bb5e 7163get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7164{
518817b3
SM
7165 struct dwarf2_per_objfile *dwarf2_per_objfile
7166 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7167 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7168 struct type_unit_group *tu_group;
7169 void **slot;
7170 unsigned int line_offset;
7171 struct type_unit_group type_unit_group_for_lookup;
7172
7173 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7174 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7175
7176 /* Do we need to create a new group, or can we use an existing one? */
7177
7178 if (stmt_list)
7179 {
7180 line_offset = DW_UNSND (stmt_list);
7181 ++tu_stats->nr_symtab_sharers;
7182 }
7183 else
7184 {
7185 /* Ugh, no stmt_list. Rare, but we have to handle it.
7186 We can do various things here like create one group per TU or
7187 spread them over multiple groups to split up the expansion work.
7188 To avoid worst case scenarios (too many groups or too large groups)
7189 we, umm, group them in bunches. */
7190 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7191 | (tu_stats->nr_stmt_less_type_units
7192 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7193 ++tu_stats->nr_stmt_less_type_units;
7194 }
7195
094b34ac 7196 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7197 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7198 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7199 &type_unit_group_for_lookup, INSERT);
7200 if (*slot != NULL)
7201 {
9a3c8263 7202 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7203 gdb_assert (tu_group != NULL);
7204 }
7205 else
7206 {
9c541725 7207 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7208 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7209 *slot = tu_group;
7210 ++tu_stats->nr_symtabs;
7211 }
7212
7213 return tu_group;
7214}
0018ea6f
DE
7215\f
7216/* Partial symbol tables. */
7217
7218/* Create a psymtab named NAME and assign it to PER_CU.
7219
7220 The caller must fill in the following details:
7221 dirname, textlow, texthigh. */
7222
891813be 7223static dwarf2_psymtab *
0018ea6f
DE
7224create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7225{
e3b94546 7226 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7227 dwarf2_psymtab *pst;
0018ea6f 7228
891813be 7229 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7230
6d94535f 7231 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7232
7233 /* This is the glue that links PST into GDB's symbol API. */
891813be 7234 pst->per_cu_data = per_cu;
0018ea6f
DE
7235 per_cu->v.psymtab = pst;
7236
7237 return pst;
7238}
7239
c0ab21c2 7240/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7241
7242static void
7243process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7244 const gdb_byte *info_ptr,
0018ea6f 7245 struct die_info *comp_unit_die,
c0ab21c2 7246 enum language pretend_language)
0018ea6f
DE
7247{
7248 struct dwarf2_cu *cu = reader->cu;
518817b3 7249 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7250 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7251 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7252 CORE_ADDR baseaddr;
7253 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7254 dwarf2_psymtab *pst;
3a2b436a 7255 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7256 const char *filename;
0018ea6f 7257
0018ea6f
DE
7258 gdb_assert (! per_cu->is_debug_types);
7259
c0ab21c2 7260 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7261
0018ea6f 7262 /* Allocate a new partial symbol table structure. */
2e927613
TV
7263 gdb::unique_xmalloc_ptr<char> debug_filename;
7264 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7265 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7266 if (filename == NULL)
0018ea6f 7267 filename = "";
2e927613
TV
7268 else if (strcmp (filename, artificial) == 0)
7269 {
7270 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7271 sect_offset_str (per_cu->sect_off),
7272 (char *) NULL));
2e927613
TV
7273 filename = debug_filename.get ();
7274 }
0018ea6f
DE
7275
7276 pst = create_partial_symtab (per_cu, filename);
7277
7278 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7279 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7280
b3b3bada 7281 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7282
7283 dwarf2_find_base_address (comp_unit_die, cu);
7284
7285 /* Possibly set the default values of LOWPC and HIGHPC from
7286 `DW_AT_ranges'. */
3a2b436a
JK
7287 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7288 &best_highpc, cu, pst);
7289 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7290 {
7291 CORE_ADDR low
7292 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7293 - baseaddr);
7294 CORE_ADDR high
7295 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7296 - baseaddr - 1);
7297 /* Store the contiguous range if it is not empty; it can be
7298 empty for CUs with no code. */
d320c2b5
TT
7299 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7300 low, high, pst);
79748972 7301 }
0018ea6f
DE
7302
7303 /* Check if comp unit has_children.
7304 If so, read the rest of the partial symbols from this comp unit.
7305 If not, there's no more debug_info for this comp unit. */
3e225074 7306 if (comp_unit_die->has_children)
0018ea6f
DE
7307 {
7308 struct partial_die_info *first_die;
7309 CORE_ADDR lowpc, highpc;
7310
7311 lowpc = ((CORE_ADDR) -1);
7312 highpc = ((CORE_ADDR) 0);
7313
7314 first_die = load_partial_dies (reader, info_ptr, 1);
7315
7316 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7317 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7318
7319 /* If we didn't find a lowpc, set it to highpc to avoid
7320 complaints from `maint check'. */
7321 if (lowpc == ((CORE_ADDR) -1))
7322 lowpc = highpc;
7323
7324 /* If the compilation unit didn't have an explicit address range,
7325 then use the information extracted from its child dies. */
e385593e 7326 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7327 {
7328 best_lowpc = lowpc;
7329 best_highpc = highpc;
7330 }
7331 }
4ae976d1 7332 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7333 best_lowpc + baseaddr)
7334 - baseaddr);
4ae976d1 7335 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7336 best_highpc + baseaddr)
7337 - baseaddr);
0018ea6f 7338
8763cede 7339 end_psymtab_common (objfile, pst);
0018ea6f 7340
ae640021 7341 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7342 {
7343 int i;
ae640021 7344 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7345
7346 /* Fill in 'dependencies' here; we fill in 'users' in a
7347 post-pass. */
7348 pst->number_of_dependencies = len;
a9342b62
TT
7349 pst->dependencies
7350 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7351 for (i = 0; i < len; ++i)
7352 {
7353 pst->dependencies[i]
7354 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7355 }
0018ea6f 7356
ae640021 7357 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7358 }
7359
7360 /* Get the list of files included in the current compilation unit,
7361 and build a psymtab for each of them. */
7362 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7363
b4f54984 7364 if (dwarf_read_debug)
b926417a
TT
7365 fprintf_unfiltered (gdb_stdlog,
7366 "Psymtab for %s unit @%s: %s - %s"
7367 ", %d global, %d static syms\n",
7368 per_cu->is_debug_types ? "type" : "comp",
7369 sect_offset_str (per_cu->sect_off),
7370 paddress (gdbarch, pst->text_low (objfile)),
7371 paddress (gdbarch, pst->text_high (objfile)),
7372 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7373}
7374
7375/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7376 Process compilation unit THIS_CU for a psymtab. */
7377
7378static void
7379process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7380 bool want_partial_unit,
b93601f3 7381 enum language pretend_language)
0018ea6f
DE
7382{
7383 /* If this compilation unit was already read in, free the
7384 cached copy in order to read it in again. This is
7385 necessary because we skipped some symbols when we first
7386 read in the compilation unit (see load_partial_dies).
7387 This problem could be avoided, but the benefit is unclear. */
7388 if (this_cu->cu != NULL)
7389 free_one_cached_comp_unit (this_cu);
7390
6751ebae 7391 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2
TT
7392
7393 if (reader.dummy_p)
f1902523 7394 {
c0ab21c2 7395 /* Nothing. */
f1902523 7396 }
c0ab21c2 7397 else if (this_cu->is_debug_types)
3e225074
TT
7398 build_type_psymtabs_reader (&reader, reader.info_ptr,
7399 reader.comp_unit_die);
135f5437
TT
7400 else if (want_partial_unit
7401 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7402 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7403 reader.comp_unit_die,
c0ab21c2 7404 pretend_language);
0018ea6f
DE
7405
7406 /* Age out any secondary CUs. */
ed2dc618 7407 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7408}
f4dc4d17
DE
7409
7410/* Reader function for build_type_psymtabs. */
7411
7412static void
7413build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7414 const gdb_byte *info_ptr,
3e225074 7415 struct die_info *type_unit_die)
f4dc4d17 7416{
ed2dc618 7417 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7418 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7419 struct objfile *objfile = dwarf2_per_objfile->objfile;
7420 struct dwarf2_cu *cu = reader->cu;
7421 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7422 struct signatured_type *sig_type;
f4dc4d17
DE
7423 struct type_unit_group *tu_group;
7424 struct attribute *attr;
7425 struct partial_die_info *first_die;
7426 CORE_ADDR lowpc, highpc;
891813be 7427 dwarf2_psymtab *pst;
f4dc4d17 7428
0186c6a7
DE
7429 gdb_assert (per_cu->is_debug_types);
7430 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7431
3e225074 7432 if (! type_unit_die->has_children)
f4dc4d17
DE
7433 return;
7434
7435 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7436 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7437
df07e2c7 7438 if (tu_group->tus == nullptr)
a8b3b8e9 7439 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7440 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7441
7442 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7443 pst = create_partial_symtab (per_cu, "");
6d94535f 7444 pst->anonymous = true;
f4dc4d17
DE
7445
7446 first_die = load_partial_dies (reader, info_ptr, 1);
7447
7448 lowpc = (CORE_ADDR) -1;
7449 highpc = (CORE_ADDR) 0;
7450 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7451
8763cede 7452 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7453}
7454
73051182
DE
7455/* Struct used to sort TUs by their abbreviation table offset. */
7456
7457struct tu_abbrev_offset
7458{
b2bdb8cf
SM
7459 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7460 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7461 {}
7462
7463 signatured_type *sig_type;
73051182
DE
7464 sect_offset abbrev_offset;
7465};
7466
484cf504 7467/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7468
484cf504
TT
7469static bool
7470sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7471 const struct tu_abbrev_offset &b)
73051182 7472{
484cf504 7473 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7474}
7475
7476/* Efficiently read all the type units.
7477 This does the bulk of the work for build_type_psymtabs.
7478
7479 The efficiency is because we sort TUs by the abbrev table they use and
7480 only read each abbrev table once. In one program there are 200K TUs
7481 sharing 8K abbrev tables.
7482
7483 The main purpose of this function is to support building the
7484 dwarf2_per_objfile->type_unit_groups table.
7485 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7486 can collapse the search space by grouping them by stmt_list.
7487 The savings can be significant, in the same program from above the 200K TUs
7488 share 8K stmt_list tables.
7489
7490 FUNC is expected to call get_type_unit_group, which will create the
7491 struct type_unit_group if necessary and add it to
7492 dwarf2_per_objfile->type_unit_groups. */
7493
7494static void
ed2dc618 7495build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7496{
73051182 7497 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7498 abbrev_table_up abbrev_table;
73051182 7499 sect_offset abbrev_offset;
73051182
DE
7500
7501 /* It's up to the caller to not call us multiple times. */
7502 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7503
b2bdb8cf 7504 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7505 return;
7506
7507 /* TUs typically share abbrev tables, and there can be way more TUs than
7508 abbrev tables. Sort by abbrev table to reduce the number of times we
7509 read each abbrev table in.
7510 Alternatives are to punt or to maintain a cache of abbrev tables.
7511 This is simpler and efficient enough for now.
7512
7513 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7514 symtab to use). Typically TUs with the same abbrev offset have the same
7515 stmt_list value too so in practice this should work well.
7516
7517 The basic algorithm here is:
7518
7519 sort TUs by abbrev table
7520 for each TU with same abbrev table:
7521 read abbrev table if first user
7522 read TU top level DIE
7523 [IWBN if DWO skeletons had DW_AT_stmt_list]
7524 call FUNC */
7525
b4f54984 7526 if (dwarf_read_debug)
73051182
DE
7527 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7528
7529 /* Sort in a separate table to maintain the order of all_type_units
7530 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7531 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7532 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7533
7534 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7535 sorted_by_abbrev.emplace_back
7536 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7537 sig_type->per_cu.section,
7538 sig_type->per_cu.sect_off));
73051182 7539
484cf504
TT
7540 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7541 sort_tu_by_abbrev_offset);
73051182 7542
9c541725 7543 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7544
b2bdb8cf 7545 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7546 {
73051182
DE
7547 /* Switch to the next abbrev table if necessary. */
7548 if (abbrev_table == NULL
b2bdb8cf 7549 || tu.abbrev_offset != abbrev_offset)
73051182 7550 {
b2bdb8cf 7551 abbrev_offset = tu.abbrev_offset;
73051182 7552 abbrev_table =
86de1d91
TT
7553 abbrev_table::read (dwarf2_per_objfile->objfile,
7554 &dwarf2_per_objfile->abbrev,
7555 abbrev_offset);
73051182
DE
7556 ++tu_stats->nr_uniq_abbrev_tables;
7557 }
7558
c0ab21c2 7559 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7560 0, false);
c0ab21c2
TT
7561 if (!reader.dummy_p)
7562 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7563 reader.comp_unit_die);
73051182 7564 }
6aa5f3a6 7565}
73051182 7566
6aa5f3a6
DE
7567/* Print collected type unit statistics. */
7568
7569static void
ed2dc618 7570print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7571{
7572 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7573
7574 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7575 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7576 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7577 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7578 tu_stats->nr_uniq_abbrev_tables);
7579 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7580 tu_stats->nr_symtabs);
7581 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7582 tu_stats->nr_symtab_sharers);
7583 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7584 tu_stats->nr_stmt_less_type_units);
7585 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7586 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7587}
7588
f4dc4d17
DE
7589/* Traversal function for build_type_psymtabs. */
7590
7591static int
7592build_type_psymtab_dependencies (void **slot, void *info)
7593{
ed2dc618
SM
7594 struct dwarf2_per_objfile *dwarf2_per_objfile
7595 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7596 struct objfile *objfile = dwarf2_per_objfile->objfile;
7597 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7598 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7599 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7600 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7601 int i;
7602
7603 gdb_assert (len > 0);
197400e8 7604 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7605
7606 pst->number_of_dependencies = len;
a9342b62 7607 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7608 for (i = 0; i < len; ++i)
f4dc4d17 7609 {
df07e2c7 7610 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7611 gdb_assert (iter->per_cu.is_debug_types);
7612 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7613 iter->type_unit_group = tu_group;
f4dc4d17
DE
7614 }
7615
df07e2c7
AB
7616 delete tu_group->tus;
7617 tu_group->tus = nullptr;
348e048f
DE
7618
7619 return 1;
7620}
7621
7622/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7623 Build partial symbol tables for the .debug_types comp-units. */
7624
7625static void
ed2dc618 7626build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7627{
ed2dc618 7628 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7629 return;
7630
ed2dc618 7631 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7632}
f4dc4d17 7633
6aa5f3a6
DE
7634/* Traversal function for process_skeletonless_type_unit.
7635 Read a TU in a DWO file and build partial symbols for it. */
7636
7637static int
7638process_skeletonless_type_unit (void **slot, void *info)
7639{
7640 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7641 struct dwarf2_per_objfile *dwarf2_per_objfile
7642 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7643 struct signatured_type find_entry, *entry;
7644
7645 /* If this TU doesn't exist in the global table, add it and read it in. */
7646
7647 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7648 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7649
7650 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7651 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7652 &find_entry, INSERT);
6aa5f3a6
DE
7653 /* If we've already seen this type there's nothing to do. What's happening
7654 is we're doing our own version of comdat-folding here. */
7655 if (*slot != NULL)
7656 return 1;
7657
7658 /* This does the job that create_all_type_units would have done for
7659 this TU. */
ed2dc618
SM
7660 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7661 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7662 *slot = entry;
7663
7664 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7665 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7666 if (!reader.dummy_p)
7667 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7668 reader.comp_unit_die);
6aa5f3a6
DE
7669
7670 return 1;
7671}
7672
7673/* Traversal function for process_skeletonless_type_units. */
7674
7675static int
7676process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7677{
7678 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7679
7680 if (dwo_file->tus != NULL)
b0b6a987
TT
7681 htab_traverse_noresize (dwo_file->tus.get (),
7682 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7683
7684 return 1;
7685}
7686
7687/* Scan all TUs of DWO files, verifying we've processed them.
7688 This is needed in case a TU was emitted without its skeleton.
7689 Note: This can't be done until we know what all the DWO files are. */
7690
7691static void
ed2dc618 7692process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7693{
7694 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7695 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7696 && dwarf2_per_objfile->dwo_files != NULL)
7697 {
51ac9db5 7698 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7699 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7700 dwarf2_per_objfile);
6aa5f3a6 7701 }
348e048f
DE
7702}
7703
ed2dc618 7704/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7705
7706static void
ed2dc618 7707set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7708{
b76e467d 7709 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7710 {
891813be 7711 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7712
36586728
TT
7713 if (pst == NULL)
7714 continue;
7715
b76e467d 7716 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7717 {
7718 /* Set the 'user' field only if it is not already set. */
7719 if (pst->dependencies[j]->user == NULL)
7720 pst->dependencies[j]->user = pst;
7721 }
7722 }
7723}
7724
93311388
DE
7725/* Build the partial symbol table by doing a quick pass through the
7726 .debug_info and .debug_abbrev sections. */
72bf9492 7727
93311388 7728static void
ed2dc618 7729dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7730{
ed2dc618 7731 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7732
b4f54984 7733 if (dwarf_read_debug)
45cfd468
DE
7734 {
7735 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7736 objfile_name (objfile));
45cfd468
DE
7737 }
7738
76935768
TT
7739 scoped_restore restore_reading_psyms
7740 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7741 true);
98bfdba5 7742
96b79293 7743 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7744
93311388
DE
7745 /* Any cached compilation units will be linked by the per-objfile
7746 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7747 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7748
ed2dc618 7749 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7750
ed2dc618 7751 create_all_comp_units (dwarf2_per_objfile);
c906108c 7752
60606b2c
TT
7753 /* Create a temporary address map on a temporary obstack. We later
7754 copy this to the final obstack. */
8268c778 7755 auto_obstack temp_obstack;
791afaa2
TT
7756
7757 scoped_restore save_psymtabs_addrmap
d320c2b5 7758 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7759 addrmap_create_mutable (&temp_obstack));
72bf9492 7760
b76e467d 7761 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7762 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7763
6aa5f3a6 7764 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7765 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7766
7767 /* Now that all TUs have been processed we can fill in the dependencies. */
7768 if (dwarf2_per_objfile->type_unit_groups != NULL)
7769 {
eaa5fa8b 7770 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7771 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7772 }
7773
b4f54984 7774 if (dwarf_read_debug)
ed2dc618 7775 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7776
ed2dc618 7777 set_partial_user (dwarf2_per_objfile);
95554aad 7778
d320c2b5
TT
7779 objfile->partial_symtabs->psymtabs_addrmap
7780 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7781 objfile->partial_symtabs->obstack ());
791afaa2
TT
7782 /* At this point we want to keep the address map. */
7783 save_psymtabs_addrmap.release ();
ff013f42 7784
b4f54984 7785 if (dwarf_read_debug)
45cfd468 7786 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7787 objfile_name (objfile));
ae038cb0
DJ
7788}
7789
dee91e82
DE
7790/* Load the partial DIEs for a secondary CU into memory.
7791 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7792
dee91e82
DE
7793static void
7794load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7795{
6751ebae 7796 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7797
7798 if (!reader.dummy_p)
7799 {
7800 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7801 language_minimal);
7802
7803 /* Check if comp unit has_children.
7804 If so, read the rest of the partial symbols from this comp unit.
7805 If not, there's no more debug_info for this comp unit. */
3e225074 7806 if (reader.comp_unit_die->has_children)
c0ab21c2 7807 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7808
7809 reader.keep ();
c0ab21c2 7810 }
ae038cb0
DJ
7811}
7812
ae038cb0 7813static void
ed2dc618 7814read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7815 struct dwarf2_section_info *section,
f1902523 7816 struct dwarf2_section_info *abbrev_section,
b76e467d 7817 unsigned int is_dwz)
ae038cb0 7818{
d521ce57 7819 const gdb_byte *info_ptr;
ed2dc618 7820 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7821
b4f54984 7822 if (dwarf_read_debug)
bf6af496 7823 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7824 section->get_name (),
7825 section->get_file_name ());
bf6af496 7826
96b79293 7827 section->read (objfile);
ae038cb0 7828
36586728 7829 info_ptr = section->buffer;
6e70227d 7830
36586728 7831 while (info_ptr < section->buffer + section->size)
ae038cb0 7832 {
ae038cb0 7833 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7834
9c541725 7835 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7836
f1902523 7837 comp_unit_head cu_header;
ed2dc618
SM
7838 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7839 abbrev_section, info_ptr,
7840 rcuh_kind::COMPILE);
ae038cb0
DJ
7841
7842 /* Save the compilation unit for later lookup. */
f1902523
JK
7843 if (cu_header.unit_type != DW_UT_type)
7844 {
7845 this_cu = XOBNEW (&objfile->objfile_obstack,
7846 struct dwarf2_per_cu_data);
7847 memset (this_cu, 0, sizeof (*this_cu));
7848 }
7849 else
7850 {
7851 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7852 struct signatured_type);
7853 memset (sig_type, 0, sizeof (*sig_type));
7854 sig_type->signature = cu_header.signature;
7855 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7856 this_cu = &sig_type->per_cu;
7857 }
7858 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7859 this_cu->sect_off = sect_off;
f1902523 7860 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7861 this_cu->is_dwz = is_dwz;
e3b94546 7862 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7863 this_cu->section = section;
ae038cb0 7864
b76e467d 7865 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7866
7867 info_ptr = info_ptr + this_cu->length;
7868 }
36586728
TT
7869}
7870
7871/* Create a list of all compilation units in OBJFILE.
7872 This is only done for -readnow and building partial symtabs. */
7873
7874static void
ed2dc618 7875create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7876{
b76e467d 7877 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7878 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7879 &dwarf2_per_objfile->abbrev, 0);
36586728 7880
b76e467d 7881 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7882 if (dwz != NULL)
ed2dc618 7883 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7884 1);
c906108c
SS
7885}
7886
5734ee8b 7887/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7888 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7889 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7890 DW_AT_ranges). See the comments of add_partial_subprogram on how
7891 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7892
72bf9492
DJ
7893static void
7894scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7895 CORE_ADDR *highpc, int set_addrmap,
7896 struct dwarf2_cu *cu)
c906108c 7897{
72bf9492 7898 struct partial_die_info *pdi;
c906108c 7899
91c24f0a
DC
7900 /* Now, march along the PDI's, descending into ones which have
7901 interesting children but skipping the children of the other ones,
7902 until we reach the end of the compilation unit. */
c906108c 7903
72bf9492 7904 pdi = first_die;
91c24f0a 7905
72bf9492
DJ
7906 while (pdi != NULL)
7907 {
52356b79 7908 pdi->fixup (cu);
c906108c 7909
f55ee35c 7910 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7911 children, so we need to look at them. Ditto for anonymous
7912 enums. */
933c6fe4 7913
72bf9492 7914 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7915 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7916 || pdi->tag == DW_TAG_imported_unit
7917 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7918 {
72bf9492 7919 switch (pdi->tag)
c906108c
SS
7920 {
7921 case DW_TAG_subprogram:
b1dc1806 7922 case DW_TAG_inlined_subroutine:
cdc07690 7923 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7924 break;
72929c62 7925 case DW_TAG_constant:
c906108c
SS
7926 case DW_TAG_variable:
7927 case DW_TAG_typedef:
91c24f0a 7928 case DW_TAG_union_type:
72bf9492 7929 if (!pdi->is_declaration)
63d06c5c 7930 {
72bf9492 7931 add_partial_symbol (pdi, cu);
63d06c5c
DC
7932 }
7933 break;
c906108c 7934 case DW_TAG_class_type:
680b30c7 7935 case DW_TAG_interface_type:
c906108c 7936 case DW_TAG_structure_type:
72bf9492 7937 if (!pdi->is_declaration)
c906108c 7938 {
72bf9492 7939 add_partial_symbol (pdi, cu);
c906108c 7940 }
b7fee5a3
KS
7941 if ((cu->language == language_rust
7942 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7943 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7944 set_addrmap, cu);
c906108c 7945 break;
91c24f0a 7946 case DW_TAG_enumeration_type:
72bf9492
DJ
7947 if (!pdi->is_declaration)
7948 add_partial_enumeration (pdi, cu);
c906108c
SS
7949 break;
7950 case DW_TAG_base_type:
a02abb62 7951 case DW_TAG_subrange_type:
c906108c 7952 /* File scope base type definitions are added to the partial
c5aa993b 7953 symbol table. */
72bf9492 7954 add_partial_symbol (pdi, cu);
c906108c 7955 break;
d9fa45fe 7956 case DW_TAG_namespace:
cdc07690 7957 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7958 break;
5d7cb8df 7959 case DW_TAG_module:
59c35742
AB
7960 if (!pdi->is_declaration)
7961 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7962 break;
95554aad
TT
7963 case DW_TAG_imported_unit:
7964 {
7965 struct dwarf2_per_cu_data *per_cu;
7966
f4dc4d17
DE
7967 /* For now we don't handle imported units in type units. */
7968 if (cu->per_cu->is_debug_types)
7969 {
7970 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7971 " supported in type units [in module %s]"),
518817b3 7972 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7973 }
7974
e3b94546
SM
7975 per_cu = dwarf2_find_containing_comp_unit
7976 (pdi->d.sect_off, pdi->is_dwz,
518817b3 7977 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
7978
7979 /* Go read the partial unit, if needed. */
7980 if (per_cu->v.psymtab == NULL)
135f5437 7981 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 7982
ae640021 7983 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7984 }
7985 break;
74921315
KS
7986 case DW_TAG_imported_declaration:
7987 add_partial_symbol (pdi, cu);
7988 break;
c906108c
SS
7989 default:
7990 break;
7991 }
7992 }
7993
72bf9492
DJ
7994 /* If the die has a sibling, skip to the sibling. */
7995
7996 pdi = pdi->die_sibling;
7997 }
7998}
7999
8000/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8001
72bf9492 8002 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8003 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8004 Enumerators are an exception; they use the scope of their parent
8005 enumeration type, i.e. the name of the enumeration type is not
8006 prepended to the enumerator.
91c24f0a 8007
72bf9492
DJ
8008 There are two complexities. One is DW_AT_specification; in this
8009 case "parent" means the parent of the target of the specification,
8010 instead of the direct parent of the DIE. The other is compilers
8011 which do not emit DW_TAG_namespace; in this case we try to guess
8012 the fully qualified name of structure types from their members'
8013 linkage names. This must be done using the DIE's children rather
8014 than the children of any DW_AT_specification target. We only need
8015 to do this for structures at the top level, i.e. if the target of
8016 any DW_AT_specification (if any; otherwise the DIE itself) does not
8017 have a parent. */
8018
8019/* Compute the scope prefix associated with PDI's parent, in
8020 compilation unit CU. The result will be allocated on CU's
8021 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8022 field. NULL is returned if no prefix is necessary. */
15d034d0 8023static const char *
72bf9492
DJ
8024partial_die_parent_scope (struct partial_die_info *pdi,
8025 struct dwarf2_cu *cu)
8026{
15d034d0 8027 const char *grandparent_scope;
72bf9492 8028 struct partial_die_info *parent, *real_pdi;
91c24f0a 8029
72bf9492
DJ
8030 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8031 then this means the parent of the specification DIE. */
8032
8033 real_pdi = pdi;
72bf9492 8034 while (real_pdi->has_specification)
fb816e8b 8035 {
122cf0f2
AB
8036 auto res = find_partial_die (real_pdi->spec_offset,
8037 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8038 real_pdi = res.pdi;
8039 cu = res.cu;
8040 }
72bf9492
DJ
8041
8042 parent = real_pdi->die_parent;
8043 if (parent == NULL)
8044 return NULL;
8045
8046 if (parent->scope_set)
8047 return parent->scope;
8048
52356b79 8049 parent->fixup (cu);
72bf9492 8050
10b3939b 8051 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8052
acebe513
UW
8053 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8054 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8055 Work around this problem here. */
8056 if (cu->language == language_cplus
6e70227d 8057 && parent->tag == DW_TAG_namespace
acebe513
UW
8058 && strcmp (parent->name, "::") == 0
8059 && grandparent_scope == NULL)
8060 {
8061 parent->scope = NULL;
8062 parent->scope_set = 1;
8063 return NULL;
8064 }
8065
0a4b0913 8066 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8067 if (pdi->tag == DW_TAG_enumerator)
8068 /* Enumerators should not get the name of the enumeration as a prefix. */
8069 parent->scope = grandparent_scope;
8070 else if (parent->tag == DW_TAG_namespace
f55ee35c 8071 || parent->tag == DW_TAG_module
72bf9492
DJ
8072 || parent->tag == DW_TAG_structure_type
8073 || parent->tag == DW_TAG_class_type
680b30c7 8074 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8075 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8076 || parent->tag == DW_TAG_enumeration_type
8077 || (cu->language == language_fortran
8078 && parent->tag == DW_TAG_subprogram
8079 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8080 {
8081 if (grandparent_scope == NULL)
8082 parent->scope = parent->name;
8083 else
3e43a32a
MS
8084 parent->scope = typename_concat (&cu->comp_unit_obstack,
8085 grandparent_scope,
f55ee35c 8086 parent->name, 0, cu);
72bf9492 8087 }
72bf9492
DJ
8088 else
8089 {
8090 /* FIXME drow/2004-04-01: What should we be doing with
8091 function-local names? For partial symbols, we should probably be
8092 ignoring them. */
fa9c3fa0
TT
8093 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8094 dwarf_tag_name (parent->tag),
8095 sect_offset_str (pdi->sect_off));
72bf9492 8096 parent->scope = grandparent_scope;
c906108c
SS
8097 }
8098
72bf9492
DJ
8099 parent->scope_set = 1;
8100 return parent->scope;
8101}
8102
8103/* Return the fully scoped name associated with PDI, from compilation unit
8104 CU. The result will be allocated with malloc. */
4568ecf9 8105
43816ebc 8106static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8107partial_die_full_name (struct partial_die_info *pdi,
8108 struct dwarf2_cu *cu)
8109{
15d034d0 8110 const char *parent_scope;
72bf9492 8111
98bfdba5
PA
8112 /* If this is a template instantiation, we can not work out the
8113 template arguments from partial DIEs. So, unfortunately, we have
8114 to go through the full DIEs. At least any work we do building
8115 types here will be reused if full symbols are loaded later. */
8116 if (pdi->has_template_arguments)
8117 {
52356b79 8118 pdi->fixup (cu);
98bfdba5
PA
8119
8120 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8121 {
8122 struct die_info *die;
8123 struct attribute attr;
8124 struct dwarf2_cu *ref_cu = cu;
8125
b64f50a1 8126 /* DW_FORM_ref_addr is using section offset. */
b4069958 8127 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8128 attr.form = DW_FORM_ref_addr;
9c541725 8129 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8130 die = follow_die_ref (NULL, &attr, &ref_cu);
8131
43816ebc 8132 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8133 }
8134 }
8135
72bf9492
DJ
8136 parent_scope = partial_die_parent_scope (pdi, cu);
8137 if (parent_scope == NULL)
8138 return NULL;
8139 else
43816ebc
TT
8140 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8141 pdi->name, 0, cu));
c906108c
SS
8142}
8143
8144static void
72bf9492 8145add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8146{
518817b3
SM
8147 struct dwarf2_per_objfile *dwarf2_per_objfile
8148 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8149 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8150 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8151 CORE_ADDR addr = 0;
15d034d0 8152 const char *actual_name = NULL;
e142c38c
DJ
8153 CORE_ADDR baseaddr;
8154
b3b3bada 8155 baseaddr = objfile->text_section_offset ();
c906108c 8156
43816ebc
TT
8157 gdb::unique_xmalloc_ptr<char> built_actual_name
8158 = partial_die_full_name (pdi, cu);
15d034d0 8159 if (built_actual_name != NULL)
43816ebc 8160 actual_name = built_actual_name.get ();
63d06c5c 8161
72bf9492
DJ
8162 if (actual_name == NULL)
8163 actual_name = pdi->name;
8164
c906108c
SS
8165 switch (pdi->tag)
8166 {
b1dc1806 8167 case DW_TAG_inlined_subroutine:
c906108c 8168 case DW_TAG_subprogram:
79748972
TT
8169 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8170 - baseaddr);
0a4b0913
AB
8171 if (pdi->is_external
8172 || cu->language == language_ada
8173 || (cu->language == language_fortran
8174 && pdi->die_parent != NULL
8175 && pdi->die_parent->tag == DW_TAG_subprogram))
8176 {
8177 /* Normally, only "external" DIEs are part of the global scope.
8178 But in Ada and Fortran, we want to be able to access nested
8179 procedures globally. So all Ada and Fortran subprograms are
8180 stored in the global scope. */
31edb802 8181 add_psymbol_to_list (actual_name,
15d034d0 8182 built_actual_name != NULL,
f47fb265 8183 VAR_DOMAIN, LOC_BLOCK,
79748972 8184 SECT_OFF_TEXT (objfile),
75aedd27 8185 psymbol_placement::GLOBAL,
79748972
TT
8186 addr,
8187 cu->language, objfile);
c906108c
SS
8188 }
8189 else
8190 {
31edb802 8191 add_psymbol_to_list (actual_name,
15d034d0 8192 built_actual_name != NULL,
f47fb265 8193 VAR_DOMAIN, LOC_BLOCK,
79748972 8194 SECT_OFF_TEXT (objfile),
75aedd27 8195 psymbol_placement::STATIC,
1762568f 8196 addr, cu->language, objfile);
c906108c 8197 }
0c1b455e
TT
8198
8199 if (pdi->main_subprogram && actual_name != NULL)
8200 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8201 break;
72929c62 8202 case DW_TAG_constant:
31edb802 8203 add_psymbol_to_list (actual_name,
75aedd27
TT
8204 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8205 -1, (pdi->is_external
8206 ? psymbol_placement::GLOBAL
8207 : psymbol_placement::STATIC),
8208 0, cu->language, objfile);
72929c62 8209 break;
c906108c 8210 case DW_TAG_variable:
95554aad
TT
8211 if (pdi->d.locdesc)
8212 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8213
95554aad 8214 if (pdi->d.locdesc
caac4577
JG
8215 && addr == 0
8216 && !dwarf2_per_objfile->has_section_at_zero)
8217 {
8218 /* A global or static variable may also have been stripped
8219 out by the linker if unused, in which case its address
8220 will be nullified; do not add such variables into partial
8221 symbol table then. */
8222 }
8223 else if (pdi->is_external)
c906108c
SS
8224 {
8225 /* Global Variable.
8226 Don't enter into the minimal symbol tables as there is
8227 a minimal symbol table entry from the ELF symbols already.
8228 Enter into partial symbol table if it has a location
8229 descriptor or a type.
8230 If the location descriptor is missing, new_symbol will create
8231 a LOC_UNRESOLVED symbol, the address of the variable will then
8232 be determined from the minimal symbol table whenever the variable
8233 is referenced.
8234 The address for the partial symbol table entry is not
8235 used by GDB, but it comes in handy for debugging partial symbol
8236 table building. */
8237
95554aad 8238 if (pdi->d.locdesc || pdi->has_type)
31edb802 8239 add_psymbol_to_list (actual_name,
15d034d0 8240 built_actual_name != NULL,
f47fb265 8241 VAR_DOMAIN, LOC_STATIC,
79748972 8242 SECT_OFF_TEXT (objfile),
75aedd27 8243 psymbol_placement::GLOBAL,
79748972 8244 addr, cu->language, objfile);
c906108c
SS
8245 }
8246 else
8247 {
ff908ebf
AW
8248 int has_loc = pdi->d.locdesc != NULL;
8249
8250 /* Static Variable. Skip symbols whose value we cannot know (those
8251 without location descriptors or constant values). */
8252 if (!has_loc && !pdi->has_const_value)
43816ebc 8253 return;
ff908ebf 8254
31edb802 8255 add_psymbol_to_list (actual_name,
15d034d0 8256 built_actual_name != NULL,
f47fb265 8257 VAR_DOMAIN, LOC_STATIC,
79748972 8258 SECT_OFF_TEXT (objfile),
75aedd27 8259 psymbol_placement::STATIC,
79748972 8260 has_loc ? addr : 0,
f47fb265 8261 cu->language, objfile);
c906108c
SS
8262 }
8263 break;
8264 case DW_TAG_typedef:
8265 case DW_TAG_base_type:
a02abb62 8266 case DW_TAG_subrange_type:
31edb802 8267 add_psymbol_to_list (actual_name,
15d034d0 8268 built_actual_name != NULL,
79748972 8269 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8270 psymbol_placement::STATIC,
1762568f 8271 0, cu->language, objfile);
c906108c 8272 break;
74921315 8273 case DW_TAG_imported_declaration:
72bf9492 8274 case DW_TAG_namespace:
31edb802 8275 add_psymbol_to_list (actual_name,
15d034d0 8276 built_actual_name != NULL,
79748972 8277 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8278 psymbol_placement::GLOBAL,
1762568f 8279 0, cu->language, objfile);
72bf9492 8280 break;
530e8392 8281 case DW_TAG_module:
a5fd13a9
BH
8282 /* With Fortran 77 there might be a "BLOCK DATA" module
8283 available without any name. If so, we skip the module as it
8284 doesn't bring any value. */
8285 if (actual_name != nullptr)
31edb802 8286 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8287 built_actual_name != NULL,
8288 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8289 psymbol_placement::GLOBAL,
8290 0, cu->language, objfile);
530e8392 8291 break;
c906108c 8292 case DW_TAG_class_type:
680b30c7 8293 case DW_TAG_interface_type:
c906108c
SS
8294 case DW_TAG_structure_type:
8295 case DW_TAG_union_type:
8296 case DW_TAG_enumeration_type:
fa4028e9
JB
8297 /* Skip external references. The DWARF standard says in the section
8298 about "Structure, Union, and Class Type Entries": "An incomplete
8299 structure, union or class type is represented by a structure,
8300 union or class entry that does not have a byte size attribute
8301 and that has a DW_AT_declaration attribute." */
8302 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8303 return;
fa4028e9 8304
63d06c5c
DC
8305 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8306 static vs. global. */
31edb802 8307 add_psymbol_to_list (actual_name,
15d034d0 8308 built_actual_name != NULL,
79748972 8309 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8310 cu->language == language_cplus
75aedd27
TT
8311 ? psymbol_placement::GLOBAL
8312 : psymbol_placement::STATIC,
1762568f 8313 0, cu->language, objfile);
c906108c 8314
c906108c
SS
8315 break;
8316 case DW_TAG_enumerator:
31edb802 8317 add_psymbol_to_list (actual_name,
15d034d0 8318 built_actual_name != NULL,
79748972 8319 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8320 cu->language == language_cplus
75aedd27
TT
8321 ? psymbol_placement::GLOBAL
8322 : psymbol_placement::STATIC,
1762568f 8323 0, cu->language, objfile);
c906108c
SS
8324 break;
8325 default:
8326 break;
8327 }
8328}
8329
5c4e30ca
DC
8330/* Read a partial die corresponding to a namespace; also, add a symbol
8331 corresponding to that namespace to the symbol table. NAMESPACE is
8332 the name of the enclosing namespace. */
91c24f0a 8333
72bf9492
DJ
8334static void
8335add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8336 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8337 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8338{
72bf9492 8339 /* Add a symbol for the namespace. */
e7c27a73 8340
72bf9492 8341 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8342
8343 /* Now scan partial symbols in that namespace. */
8344
91c24f0a 8345 if (pdi->has_children)
cdc07690 8346 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8347}
8348
5d7cb8df
JK
8349/* Read a partial die corresponding to a Fortran module. */
8350
8351static void
8352add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8353 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8354{
530e8392
KB
8355 /* Add a symbol for the namespace. */
8356
8357 add_partial_symbol (pdi, cu);
8358
f55ee35c 8359 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8360
8361 if (pdi->has_children)
cdc07690 8362 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8363}
8364
b1dc1806
XR
8365/* Read a partial die corresponding to a subprogram or an inlined
8366 subprogram and create a partial symbol for that subprogram.
8367 When the CU language allows it, this routine also defines a partial
8368 symbol for each nested subprogram that this subprogram contains.
8369 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8370 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8371
cdc07690
YQ
8372 PDI may also be a lexical block, in which case we simply search
8373 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8374 Again, this is only performed when the CU language allows this
8375 type of definitions. */
8376
8377static void
8378add_partial_subprogram (struct partial_die_info *pdi,
8379 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8380 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8381{
b1dc1806 8382 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8383 {
8384 if (pdi->has_pc_info)
8385 {
8386 if (pdi->lowpc < *lowpc)
8387 *lowpc = pdi->lowpc;
8388 if (pdi->highpc > *highpc)
8389 *highpc = pdi->highpc;
cdc07690 8390 if (set_addrmap)
5734ee8b 8391 {
518817b3 8392 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8393 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8394 CORE_ADDR baseaddr;
b926417a
TT
8395 CORE_ADDR this_highpc;
8396 CORE_ADDR this_lowpc;
5734ee8b 8397
b3b3bada 8398 baseaddr = objfile->text_section_offset ();
b926417a
TT
8399 this_lowpc
8400 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8401 pdi->lowpc + baseaddr)
8402 - baseaddr);
8403 this_highpc
8404 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8405 pdi->highpc + baseaddr)
8406 - baseaddr);
d320c2b5 8407 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8408 this_lowpc, this_highpc - 1,
9291a0cd 8409 cu->per_cu->v.psymtab);
5734ee8b 8410 }
481860b3
GB
8411 }
8412
8413 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8414 {
bc30ff58 8415 if (!pdi->is_declaration)
e8d05480
JB
8416 /* Ignore subprogram DIEs that do not have a name, they are
8417 illegal. Do not emit a complaint at this point, we will
8418 do so when we convert this psymtab into a symtab. */
8419 if (pdi->name)
8420 add_partial_symbol (pdi, cu);
bc30ff58
JB
8421 }
8422 }
6e70227d 8423
bc30ff58
JB
8424 if (! pdi->has_children)
8425 return;
8426
0a4b0913 8427 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8428 {
8429 pdi = pdi->die_child;
8430 while (pdi != NULL)
8431 {
52356b79 8432 pdi->fixup (cu);
bc30ff58 8433 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8434 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8435 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8436 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8437 pdi = pdi->die_sibling;
8438 }
8439 }
8440}
8441
91c24f0a
DC
8442/* Read a partial die corresponding to an enumeration type. */
8443
72bf9492
DJ
8444static void
8445add_partial_enumeration (struct partial_die_info *enum_pdi,
8446 struct dwarf2_cu *cu)
91c24f0a 8447{
72bf9492 8448 struct partial_die_info *pdi;
91c24f0a
DC
8449
8450 if (enum_pdi->name != NULL)
72bf9492
DJ
8451 add_partial_symbol (enum_pdi, cu);
8452
8453 pdi = enum_pdi->die_child;
8454 while (pdi)
91c24f0a 8455 {
72bf9492 8456 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8457 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8458 else
72bf9492
DJ
8459 add_partial_symbol (pdi, cu);
8460 pdi = pdi->die_sibling;
91c24f0a 8461 }
91c24f0a
DC
8462}
8463
6caca83c
CC
8464/* Return the initial uleb128 in the die at INFO_PTR. */
8465
8466static unsigned int
d521ce57 8467peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8468{
8469 unsigned int bytes_read;
8470
8471 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8472}
8473
685af9cd
TT
8474/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8475 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8476
4bb7a0a7
DJ
8477 Return the corresponding abbrev, or NULL if the number is zero (indicating
8478 an empty DIE). In either case *BYTES_READ will be set to the length of
8479 the initial number. */
8480
8481static struct abbrev_info *
685af9cd
TT
8482peek_die_abbrev (const die_reader_specs &reader,
8483 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8484{
685af9cd 8485 dwarf2_cu *cu = reader.cu;
518817b3 8486 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8487 unsigned int abbrev_number
8488 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8489
8490 if (abbrev_number == 0)
8491 return NULL;
8492
685af9cd 8493 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8494 if (!abbrev)
8495 {
422b9917 8496 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8497 " at offset %s [in module %s]"),
422b9917 8498 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8499 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8500 }
8501
8502 return abbrev;
8503}
8504
93311388
DE
8505/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8506 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8507 DIE. Any children of the skipped DIEs will also be skipped. */
8508
d521ce57
TT
8509static const gdb_byte *
8510skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8511{
4bb7a0a7
DJ
8512 while (1)
8513 {
685af9cd
TT
8514 unsigned int bytes_read;
8515 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8516
4bb7a0a7
DJ
8517 if (abbrev == NULL)
8518 return info_ptr + bytes_read;
8519 else
dee91e82 8520 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8521 }
8522}
8523
93311388
DE
8524/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8525 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8526 abbrev corresponding to that skipped uleb128 should be passed in
8527 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8528 children. */
8529
d521ce57
TT
8530static const gdb_byte *
8531skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8532 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8533{
8534 unsigned int bytes_read;
8535 struct attribute attr;
dee91e82
DE
8536 bfd *abfd = reader->abfd;
8537 struct dwarf2_cu *cu = reader->cu;
d521ce57 8538 const gdb_byte *buffer = reader->buffer;
f664829e 8539 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8540 unsigned int form, i;
8541
8542 for (i = 0; i < abbrev->num_attrs; i++)
8543 {
8544 /* The only abbrev we care about is DW_AT_sibling. */
8545 if (abbrev->attrs[i].name == DW_AT_sibling)
8546 {
18a8505e
AT
8547 bool ignored;
8548 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8549 &ignored);
4bb7a0a7 8550 if (attr.form == DW_FORM_ref_addr)
b98664d3 8551 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8552 else
b9502d3f 8553 {
9c541725
PA
8554 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8555 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8556
8557 if (sibling_ptr < info_ptr)
b98664d3 8558 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
8559 else if (sibling_ptr > reader->buffer_end)
8560 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8561 else
8562 return sibling_ptr;
8563 }
4bb7a0a7
DJ
8564 }
8565
8566 /* If it isn't DW_AT_sibling, skip this attribute. */
8567 form = abbrev->attrs[i].form;
8568 skip_attribute:
8569 switch (form)
8570 {
4bb7a0a7 8571 case DW_FORM_ref_addr:
ae411497
TT
8572 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8573 and later it is offset sized. */
8574 if (cu->header.version == 2)
8575 info_ptr += cu->header.addr_size;
8576 else
8577 info_ptr += cu->header.offset_size;
8578 break;
36586728
TT
8579 case DW_FORM_GNU_ref_alt:
8580 info_ptr += cu->header.offset_size;
8581 break;
ae411497 8582 case DW_FORM_addr:
4bb7a0a7
DJ
8583 info_ptr += cu->header.addr_size;
8584 break;
8585 case DW_FORM_data1:
8586 case DW_FORM_ref1:
8587 case DW_FORM_flag:
8fe0f950 8588 case DW_FORM_strx1:
4bb7a0a7
DJ
8589 info_ptr += 1;
8590 break;
2dc7f7b3 8591 case DW_FORM_flag_present:
43988095 8592 case DW_FORM_implicit_const:
2dc7f7b3 8593 break;
4bb7a0a7
DJ
8594 case DW_FORM_data2:
8595 case DW_FORM_ref2:
8fe0f950 8596 case DW_FORM_strx2:
4bb7a0a7
DJ
8597 info_ptr += 2;
8598 break;
8fe0f950
AT
8599 case DW_FORM_strx3:
8600 info_ptr += 3;
8601 break;
4bb7a0a7
DJ
8602 case DW_FORM_data4:
8603 case DW_FORM_ref4:
8fe0f950 8604 case DW_FORM_strx4:
4bb7a0a7
DJ
8605 info_ptr += 4;
8606 break;
8607 case DW_FORM_data8:
8608 case DW_FORM_ref8:
55f1336d 8609 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8610 info_ptr += 8;
8611 break;
0224619f
JK
8612 case DW_FORM_data16:
8613 info_ptr += 16;
8614 break;
4bb7a0a7 8615 case DW_FORM_string:
9b1c24c8 8616 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8617 info_ptr += bytes_read;
8618 break;
2dc7f7b3 8619 case DW_FORM_sec_offset:
4bb7a0a7 8620 case DW_FORM_strp:
36586728 8621 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8622 info_ptr += cu->header.offset_size;
8623 break;
2dc7f7b3 8624 case DW_FORM_exprloc:
4bb7a0a7
DJ
8625 case DW_FORM_block:
8626 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8627 info_ptr += bytes_read;
8628 break;
8629 case DW_FORM_block1:
8630 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8631 break;
8632 case DW_FORM_block2:
8633 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8634 break;
8635 case DW_FORM_block4:
8636 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8637 break;
336d760d 8638 case DW_FORM_addrx:
cf532bd1 8639 case DW_FORM_strx:
4bb7a0a7
DJ
8640 case DW_FORM_sdata:
8641 case DW_FORM_udata:
8642 case DW_FORM_ref_udata:
3019eac3
DE
8643 case DW_FORM_GNU_addr_index:
8644 case DW_FORM_GNU_str_index:
18a8505e 8645 case DW_FORM_rnglistx:
d521ce57 8646 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8647 break;
8648 case DW_FORM_indirect:
8649 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8650 info_ptr += bytes_read;
8651 /* We need to continue parsing from here, so just go back to
8652 the top. */
8653 goto skip_attribute;
8654
8655 default:
3e43a32a
MS
8656 error (_("Dwarf Error: Cannot handle %s "
8657 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8658 dwarf_form_name (form),
8659 bfd_get_filename (abfd));
8660 }
8661 }
8662
8663 if (abbrev->has_children)
dee91e82 8664 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8665 else
8666 return info_ptr;
8667}
8668
93311388 8669/* Locate ORIG_PDI's sibling.
dee91e82 8670 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8671
d521ce57 8672static const gdb_byte *
dee91e82
DE
8673locate_pdi_sibling (const struct die_reader_specs *reader,
8674 struct partial_die_info *orig_pdi,
d521ce57 8675 const gdb_byte *info_ptr)
91c24f0a
DC
8676{
8677 /* Do we know the sibling already? */
72bf9492 8678
91c24f0a
DC
8679 if (orig_pdi->sibling)
8680 return orig_pdi->sibling;
8681
8682 /* Are there any children to deal with? */
8683
8684 if (!orig_pdi->has_children)
8685 return info_ptr;
8686
4bb7a0a7 8687 /* Skip the children the long way. */
91c24f0a 8688
dee91e82 8689 return skip_children (reader, info_ptr);
91c24f0a
DC
8690}
8691
257e7a09 8692/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8693 not NULL. */
c906108c 8694
891813be
TT
8695void
8696dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8697{
ed2dc618
SM
8698 struct dwarf2_per_objfile *dwarf2_per_objfile
8699 = get_dwarf2_per_objfile (objfile);
8700
077cbab2
TT
8701 gdb_assert (!readin);
8702 /* If this psymtab is constructed from a debug-only objfile, the
8703 has_section_at_zero flag will not necessarily be correct. We
8704 can get the correct value for this flag by looking at the data
8705 associated with the (presumably stripped) associated objfile. */
8706 if (objfile->separate_debug_objfile_backlink)
c906108c 8707 {
077cbab2
TT
8708 struct dwarf2_per_objfile *dpo_backlink
8709 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8710
077cbab2
TT
8711 dwarf2_per_objfile->has_section_at_zero
8712 = dpo_backlink->has_section_at_zero;
8713 }
98bfdba5 8714
8566b89b 8715 expand_psymtab (objfile);
95554aad 8716
ed2dc618 8717 process_cu_includes (dwarf2_per_objfile);
c906108c 8718}
9cdd5dbd
DE
8719\f
8720/* Reading in full CUs. */
c906108c 8721
10b3939b
DJ
8722/* Add PER_CU to the queue. */
8723
8724static void
95554aad
TT
8725queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8726 enum language pretend_language)
10b3939b 8727{
10b3939b 8728 per_cu->queued = 1;
39856def 8729 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8730}
8731
89e63ee4
DE
8732/* If PER_CU is not yet queued, add it to the queue.
8733 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8734 dependency.
0907af0c 8735 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8736 meaning either PER_CU is already queued or it is already loaded.
8737
8738 N.B. There is an invariant here that if a CU is queued then it is loaded.
8739 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8740
8741static int
89e63ee4 8742maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8743 struct dwarf2_per_cu_data *per_cu,
8744 enum language pretend_language)
8745{
8746 /* We may arrive here during partial symbol reading, if we need full
8747 DIEs to process an unusual case (e.g. template arguments). Do
8748 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8749 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8750 {
8751 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8752 return 1;
8753 return 0;
8754 }
8755
8756 /* Mark the dependence relation so that we don't flush PER_CU
8757 too early. */
89e63ee4
DE
8758 if (dependent_cu != NULL)
8759 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8760
8761 /* If it's already on the queue, we have nothing to do. */
8762 if (per_cu->queued)
8763 return 0;
8764
8765 /* If the compilation unit is already loaded, just mark it as
8766 used. */
8767 if (per_cu->cu != NULL)
8768 {
8769 per_cu->cu->last_used = 0;
8770 return 0;
8771 }
8772
8773 /* Add it to the queue. */
8774 queue_comp_unit (per_cu, pretend_language);
8775
8776 return 1;
8777}
8778
10b3939b
DJ
8779/* Process the queue. */
8780
8781static void
ed2dc618 8782process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8783{
b4f54984 8784 if (dwarf_read_debug)
45cfd468
DE
8785 {
8786 fprintf_unfiltered (gdb_stdlog,
8787 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8788 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8789 }
8790
03dd20cc
DJ
8791 /* The queue starts out with one item, but following a DIE reference
8792 may load a new CU, adding it to the end of the queue. */
39856def 8793 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8794 {
39856def
TT
8795 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8796
cc12ce38 8797 if ((dwarf2_per_objfile->using_index
39856def
TT
8798 ? !item.per_cu->v.quick->compunit_symtab
8799 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8800 /* Skip dummy CUs. */
39856def 8801 && item.per_cu->cu != NULL)
f4dc4d17 8802 {
39856def 8803 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8804 unsigned int debug_print_threshold;
247f5c4f 8805 char buf[100];
f4dc4d17 8806
247f5c4f 8807 if (per_cu->is_debug_types)
f4dc4d17 8808 {
247f5c4f
DE
8809 struct signatured_type *sig_type =
8810 (struct signatured_type *) per_cu;
8811
9d8780f0 8812 sprintf (buf, "TU %s at offset %s",
73be47f5 8813 hex_string (sig_type->signature),
9d8780f0 8814 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8815 /* There can be 100s of TUs.
8816 Only print them in verbose mode. */
8817 debug_print_threshold = 2;
f4dc4d17 8818 }
247f5c4f 8819 else
73be47f5 8820 {
9d8780f0
SM
8821 sprintf (buf, "CU at offset %s",
8822 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8823 debug_print_threshold = 1;
8824 }
247f5c4f 8825
b4f54984 8826 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8827 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8828
8829 if (per_cu->is_debug_types)
39856def 8830 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8831 else
39856def 8832 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8833
b4f54984 8834 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8835 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8836 }
10b3939b 8837
39856def
TT
8838 item.per_cu->queued = 0;
8839 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8840 }
8841
b4f54984 8842 if (dwarf_read_debug)
45cfd468
DE
8843 {
8844 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8845 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8846 }
10b3939b
DJ
8847}
8848
10b3939b
DJ
8849/* Read in full symbols for PST, and anything it depends on. */
8850
8566b89b
TT
8851void
8852dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8853{
10b3939b 8854 struct dwarf2_per_cu_data *per_cu;
aaa75496 8855
8566b89b 8856 if (readin)
95554aad
TT
8857 return;
8858
0494dbec 8859 read_dependencies (objfile);
aaa75496 8860
8566b89b 8861 per_cu = per_cu_data;
10b3939b
DJ
8862
8863 if (per_cu == NULL)
aaa75496
JB
8864 {
8865 /* It's an include file, no symbols to read for it.
8866 Everything is in the parent symtab. */
8566b89b 8867 readin = true;
aaa75496
JB
8868 return;
8869 }
c906108c 8870
58f0c718 8871 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
8872}
8873
dee91e82
DE
8874/* Trivial hash function for die_info: the hash value of a DIE
8875 is its offset in .debug_info for this objfile. */
10b3939b 8876
dee91e82
DE
8877static hashval_t
8878die_hash (const void *item)
10b3939b 8879{
9a3c8263 8880 const struct die_info *die = (const struct die_info *) item;
6502dd73 8881
9c541725 8882 return to_underlying (die->sect_off);
dee91e82 8883}
63d06c5c 8884
dee91e82
DE
8885/* Trivial comparison function for die_info structures: two DIEs
8886 are equal if they have the same offset. */
98bfdba5 8887
dee91e82
DE
8888static int
8889die_eq (const void *item_lhs, const void *item_rhs)
8890{
9a3c8263
SM
8891 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8892 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8893
9c541725 8894 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8895}
c906108c 8896
c0ab21c2 8897/* Load the DIEs associated with PER_CU into memory. */
c906108c 8898
dee91e82 8899static void
c0ab21c2
TT
8900load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8901 bool skip_partial,
8902 enum language pretend_language)
dee91e82 8903{
c0ab21c2
TT
8904 gdb_assert (! this_cu->is_debug_types);
8905
6751ebae 8906 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8907 if (reader.dummy_p)
8908 return;
8909
8910 struct dwarf2_cu *cu = reader.cu;
8911 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8912
dee91e82
DE
8913 gdb_assert (cu->die_hash == NULL);
8914 cu->die_hash =
8915 htab_create_alloc_ex (cu->header.length / 12,
8916 die_hash,
8917 die_eq,
8918 NULL,
8919 &cu->comp_unit_obstack,
8920 hashtab_obstack_allocate,
8921 dummy_obstack_deallocate);
e142c38c 8922
3e225074 8923 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8924 reader.comp_unit_die->child
8925 = read_die_and_siblings (&reader, reader.info_ptr,
8926 &info_ptr, reader.comp_unit_die);
8927 cu->dies = reader.comp_unit_die;
dee91e82 8928 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8929
8930 /* We try not to read any attributes in this function, because not
9cdd5dbd 8931 all CUs needed for references have been loaded yet, and symbol
10b3939b 8932 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8933 or we won't be able to build types correctly.
8934 Similarly, if we do not read the producer, we can not apply
8935 producer-specific interpretation. */
c0ab21c2 8936 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8937
8938 reader.keep ();
10b3939b
DJ
8939}
8940
3da10d80
KS
8941/* Add a DIE to the delayed physname list. */
8942
8943static void
8944add_to_method_list (struct type *type, int fnfield_index, int index,
8945 const char *name, struct die_info *die,
8946 struct dwarf2_cu *cu)
8947{
8948 struct delayed_method_info mi;
8949 mi.type = type;
8950 mi.fnfield_index = fnfield_index;
8951 mi.index = index;
8952 mi.name = name;
8953 mi.die = die;
c89b44cd 8954 cu->method_list.push_back (mi);
3da10d80
KS
8955}
8956
3693fdb3
PA
8957/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8958 "const" / "volatile". If so, decrements LEN by the length of the
8959 modifier and return true. Otherwise return false. */
8960
8961template<size_t N>
8962static bool
8963check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8964{
8965 size_t mod_len = sizeof (mod) - 1;
8966 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8967 {
8968 len -= mod_len;
8969 return true;
8970 }
8971 return false;
8972}
8973
3da10d80
KS
8974/* Compute the physnames of any methods on the CU's method list.
8975
8976 The computation of method physnames is delayed in order to avoid the
8977 (bad) condition that one of the method's formal parameters is of an as yet
8978 incomplete type. */
8979
8980static void
8981compute_delayed_physnames (struct dwarf2_cu *cu)
8982{
3693fdb3 8983 /* Only C++ delays computing physnames. */
c89b44cd 8984 if (cu->method_list.empty ())
3693fdb3
PA
8985 return;
8986 gdb_assert (cu->language == language_cplus);
8987
52941706 8988 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8989 {
1d06ead6 8990 const char *physname;
3da10d80 8991 struct fn_fieldlist *fn_flp
c89b44cd
TT
8992 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8993 physname = dwarf2_physname (mi.name, mi.die, cu);
8994 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8995 = physname ? physname : "";
3693fdb3
PA
8996
8997 /* Since there's no tag to indicate whether a method is a
8998 const/volatile overload, extract that information out of the
8999 demangled name. */
9000 if (physname != NULL)
9001 {
9002 size_t len = strlen (physname);
9003
9004 while (1)
9005 {
9006 if (physname[len] == ')') /* shortcut */
9007 break;
9008 else if (check_modifier (physname, len, " const"))
c89b44cd 9009 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9010 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9011 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9012 else
9013 break;
9014 }
9015 }
3da10d80 9016 }
c89b44cd
TT
9017
9018 /* The list is no longer needed. */
9019 cu->method_list.clear ();
3da10d80
KS
9020}
9021
a766d390
DE
9022/* Go objects should be embedded in a DW_TAG_module DIE,
9023 and it's not clear if/how imported objects will appear.
9024 To keep Go support simple until that's worked out,
9025 go back through what we've read and create something usable.
9026 We could do this while processing each DIE, and feels kinda cleaner,
9027 but that way is more invasive.
9028 This is to, for example, allow the user to type "p var" or "b main"
9029 without having to specify the package name, and allow lookups
9030 of module.object to work in contexts that use the expression
9031 parser. */
9032
9033static void
9034fixup_go_packaging (struct dwarf2_cu *cu)
9035{
421d1616 9036 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9037 struct pending *list;
9038 int i;
9039
c24bdb02 9040 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9041 list != NULL;
9042 list = list->next)
a766d390
DE
9043 {
9044 for (i = 0; i < list->nsyms; ++i)
9045 {
9046 struct symbol *sym = list->symbol[i];
9047
c1b5c1eb 9048 if (sym->language () == language_go
a766d390
DE
9049 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9050 {
421d1616
TT
9051 gdb::unique_xmalloc_ptr<char> this_package_name
9052 (go_symbol_package_name (sym));
a766d390
DE
9053
9054 if (this_package_name == NULL)
9055 continue;
9056 if (package_name == NULL)
421d1616 9057 package_name = std::move (this_package_name);
a766d390
DE
9058 else
9059 {
518817b3
SM
9060 struct objfile *objfile
9061 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9062 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9063 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9064 (symbol_symtab (sym) != NULL
9065 ? symtab_to_filename_for_display
9066 (symbol_symtab (sym))
e3b94546 9067 : objfile_name (objfile)),
421d1616 9068 this_package_name.get (), package_name.get ());
a766d390
DE
9069 }
9070 }
9071 }
9072 }
9073
9074 if (package_name != NULL)
9075 {
518817b3 9076 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9077 const char *saved_package_name
421d1616 9078 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9079 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9080 saved_package_name);
a766d390
DE
9081 struct symbol *sym;
9082
e623cf5d 9083 sym = allocate_symbol (objfile);
d3ecddab 9084 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9085 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9086 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9087 e.g., "main" finds the "main" module and not C's main(). */
9088 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9089 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9090 SYMBOL_TYPE (sym) = type;
9091
c24bdb02 9092 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9093 }
9094}
9095
c9317f21
TT
9096/* Allocate a fully-qualified name consisting of the two parts on the
9097 obstack. */
9098
9099static const char *
9100rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9101{
9102 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9103}
9104
9105/* A helper that allocates a struct discriminant_info to attach to a
9106 union type. */
9107
9108static struct discriminant_info *
9109alloc_discriminant_info (struct type *type, int discriminant_index,
9110 int default_index)
9111{
9112 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9113 gdb_assert (discriminant_index == -1
9114 || (discriminant_index >= 0
9115 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9116 gdb_assert (default_index == -1
c7b15a66 9117 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9118
9119 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9120
9121 struct discriminant_info *disc
9122 = ((struct discriminant_info *)
9123 TYPE_ZALLOC (type,
9124 offsetof (struct discriminant_info, discriminants)
9125 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9126 disc->default_index = default_index;
9127 disc->discriminant_index = discriminant_index;
9128
9129 struct dynamic_prop prop;
9130 prop.kind = PROP_UNDEFINED;
9131 prop.data.baton = disc;
9132
9133 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9134
9135 return disc;
9136}
9137
9138/* Some versions of rustc emitted enums in an unusual way.
9139
9140 Ordinary enums were emitted as unions. The first element of each
9141 structure in the union was named "RUST$ENUM$DISR". This element
9142 held the discriminant.
9143
9144 These versions of Rust also implemented the "non-zero"
9145 optimization. When the enum had two values, and one is empty and
9146 the other holds a pointer that cannot be zero, the pointer is used
9147 as the discriminant, with a zero value meaning the empty variant.
9148 Here, the union's first member is of the form
9149 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9150 where the fieldnos are the indices of the fields that should be
9151 traversed in order to find the field (which may be several fields deep)
9152 and the variantname is the name of the variant of the case when the
9153 field is zero.
9154
9155 This function recognizes whether TYPE is of one of these forms,
9156 and, if so, smashes it to be a variant type. */
9157
9158static void
9159quirk_rust_enum (struct type *type, struct objfile *objfile)
9160{
9161 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9162
9163 /* We don't need to deal with empty enums. */
9164 if (TYPE_NFIELDS (type) == 0)
9165 return;
9166
9167#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9168 if (TYPE_NFIELDS (type) == 1
9169 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9170 {
9171 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9172
9173 /* Decode the field name to find the offset of the
9174 discriminant. */
9175 ULONGEST bit_offset = 0;
9176 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9177 while (name[0] >= '0' && name[0] <= '9')
9178 {
9179 char *tail;
9180 unsigned long index = strtoul (name, &tail, 10);
9181 name = tail;
9182 if (*name != '$'
9183 || index >= TYPE_NFIELDS (field_type)
9184 || (TYPE_FIELD_LOC_KIND (field_type, index)
9185 != FIELD_LOC_KIND_BITPOS))
9186 {
b98664d3 9187 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9188 "[in module %s]"),
9189 TYPE_FIELD_NAME (type, 0),
9190 objfile_name (objfile));
9191 return;
9192 }
9193 ++name;
9194
9195 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9196 field_type = TYPE_FIELD_TYPE (field_type, index);
9197 }
9198
9199 /* Make a union to hold the variants. */
9200 struct type *union_type = alloc_type (objfile);
9201 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9202 TYPE_NFIELDS (union_type) = 3;
9203 TYPE_FIELDS (union_type)
9204 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9205 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9206 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9207
9208 /* Put the discriminant must at index 0. */
9209 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9210 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9211 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9212 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9213
9214 /* The order of fields doesn't really matter, so put the real
9215 field at index 1 and the data-less field at index 2. */
9216 struct discriminant_info *disc
9217 = alloc_discriminant_info (union_type, 0, 1);
9218 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9219 TYPE_FIELD_NAME (union_type, 1)
9220 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9221 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9222 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9223 TYPE_FIELD_NAME (union_type, 1));
9224
9225 const char *dataless_name
9226 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9227 name);
9228 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9229 dataless_name);
9230 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9231 /* NAME points into the original discriminant name, which
9232 already has the correct lifetime. */
9233 TYPE_FIELD_NAME (union_type, 2) = name;
9234 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9235 disc->discriminants[2] = 0;
9236
9237 /* Smash this type to be a structure type. We have to do this
9238 because the type has already been recorded. */
9239 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9240 TYPE_NFIELDS (type) = 1;
9241 TYPE_FIELDS (type)
9242 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9243
9244 /* Install the variant part. */
9245 TYPE_FIELD_TYPE (type, 0) = union_type;
9246 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9247 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9248 }
77c2dba3
TT
9249 /* A union with a single anonymous field is probably an old-style
9250 univariant enum. */
9251 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9252 {
c9317f21
TT
9253 /* Smash this type to be a structure type. We have to do this
9254 because the type has already been recorded. */
9255 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9256
9257 /* Make a union to hold the variants. */
9258 struct type *union_type = alloc_type (objfile);
9259 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9260 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9261 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9262 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9263 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9264
9265 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9266 const char *variant_name
9267 = rust_last_path_segment (TYPE_NAME (field_type));
9268 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9269 TYPE_NAME (field_type)
9270 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9271 TYPE_NAME (type), variant_name);
c9317f21
TT
9272
9273 /* Install the union in the outer struct type. */
9274 TYPE_NFIELDS (type) = 1;
9275 TYPE_FIELDS (type)
9276 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9277 TYPE_FIELD_TYPE (type, 0) = union_type;
9278 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9279 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9280
9281 alloc_discriminant_info (union_type, -1, 0);
9282 }
9283 else
9284 {
9285 struct type *disr_type = nullptr;
9286 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9287 {
9288 disr_type = TYPE_FIELD_TYPE (type, i);
9289
a037790e
TT
9290 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9291 {
9292 /* All fields of a true enum will be structs. */
9293 return;
9294 }
9295 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9296 {
9297 /* Could be data-less variant, so keep going. */
a037790e 9298 disr_type = nullptr;
c9317f21
TT
9299 }
9300 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9301 "RUST$ENUM$DISR") != 0)
9302 {
9303 /* Not a Rust enum. */
9304 return;
9305 }
9306 else
9307 {
9308 /* Found one. */
9309 break;
9310 }
9311 }
9312
9313 /* If we got here without a discriminant, then it's probably
9314 just a union. */
9315 if (disr_type == nullptr)
9316 return;
9317
9318 /* Smash this type to be a structure type. We have to do this
9319 because the type has already been recorded. */
9320 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9321
9322 /* Make a union to hold the variants. */
9323 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9324 struct type *union_type = alloc_type (objfile);
9325 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9326 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9327 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9328 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9329 TYPE_FIELDS (union_type)
9330 = (struct field *) TYPE_ZALLOC (union_type,
9331 (TYPE_NFIELDS (union_type)
9332 * sizeof (struct field)));
9333
9334 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9335 TYPE_NFIELDS (type) * sizeof (struct field));
9336
9337 /* Install the discriminant at index 0 in the union. */
9338 TYPE_FIELD (union_type, 0) = *disr_field;
9339 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9340 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9341
9342 /* Install the union in the outer struct type. */
9343 TYPE_FIELD_TYPE (type, 0) = union_type;
9344 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9345 TYPE_NFIELDS (type) = 1;
9346
9347 /* Set the size and offset of the union type. */
9348 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9349
9350 /* We need a way to find the correct discriminant given a
9351 variant name. For convenience we build a map here. */
9352 struct type *enum_type = FIELD_TYPE (*disr_field);
9353 std::unordered_map<std::string, ULONGEST> discriminant_map;
9354 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9355 {
9356 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9357 {
9358 const char *name
9359 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9360 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9361 }
9362 }
9363
9364 int n_fields = TYPE_NFIELDS (union_type);
9365 struct discriminant_info *disc
9366 = alloc_discriminant_info (union_type, 0, -1);
9367 /* Skip the discriminant here. */
9368 for (int i = 1; i < n_fields; ++i)
9369 {
9370 /* Find the final word in the name of this variant's type.
9371 That name can be used to look up the correct
9372 discriminant. */
9373 const char *variant_name
9374 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9375 i)));
9376
9377 auto iter = discriminant_map.find (variant_name);
9378 if (iter != discriminant_map.end ())
9379 disc->discriminants[i] = iter->second;
9380
bedda9ac 9381 /* Remove the discriminant field, if it exists. */
c9317f21 9382 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9383 if (TYPE_NFIELDS (sub_type) > 0)
9384 {
9385 --TYPE_NFIELDS (sub_type);
9386 ++TYPE_FIELDS (sub_type);
9387 }
c9317f21
TT
9388 TYPE_FIELD_NAME (union_type, i) = variant_name;
9389 TYPE_NAME (sub_type)
9390 = rust_fully_qualify (&objfile->objfile_obstack,
9391 TYPE_NAME (type), variant_name);
9392 }
9393 }
9394}
9395
9396/* Rewrite some Rust unions to be structures with variants parts. */
9397
9398static void
9399rust_union_quirks (struct dwarf2_cu *cu)
9400{
9401 gdb_assert (cu->language == language_rust);
52941706
SM
9402 for (type *type_ : cu->rust_unions)
9403 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9404 /* We don't need this any more. */
9405 cu->rust_unions.clear ();
c9317f21
TT
9406}
9407
95554aad
TT
9408/* Return the symtab for PER_CU. This works properly regardless of
9409 whether we're using the index or psymtabs. */
9410
43f3e411
DE
9411static struct compunit_symtab *
9412get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9413{
ed2dc618 9414 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9415 ? per_cu->v.quick->compunit_symtab
9416 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9417}
9418
9419/* A helper function for computing the list of all symbol tables
9420 included by PER_CU. */
9421
9422static void
4c39bc03 9423recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9424 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9425 struct dwarf2_per_cu_data *per_cu,
43f3e411 9426 struct compunit_symtab *immediate_parent)
95554aad
TT
9427{
9428 void **slot;
43f3e411 9429 struct compunit_symtab *cust;
95554aad
TT
9430
9431 slot = htab_find_slot (all_children, per_cu, INSERT);
9432 if (*slot != NULL)
9433 {
9434 /* This inclusion and its children have been processed. */
9435 return;
9436 }
9437
9438 *slot = per_cu;
9439 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9440 cust = get_compunit_symtab (per_cu);
9441 if (cust != NULL)
ec94af83
DE
9442 {
9443 /* If this is a type unit only add its symbol table if we haven't
9444 seen it yet (type unit per_cu's can share symtabs). */
9445 if (per_cu->is_debug_types)
9446 {
43f3e411 9447 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9448 if (*slot == NULL)
9449 {
43f3e411 9450 *slot = cust;
4c39bc03 9451 result->push_back (cust);
43f3e411
DE
9452 if (cust->user == NULL)
9453 cust->user = immediate_parent;
ec94af83
DE
9454 }
9455 }
9456 else
f9125b6c 9457 {
4c39bc03 9458 result->push_back (cust);
43f3e411
DE
9459 if (cust->user == NULL)
9460 cust->user = immediate_parent;
f9125b6c 9461 }
ec94af83 9462 }
95554aad 9463
ae640021
AB
9464 if (!per_cu->imported_symtabs_empty ())
9465 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9466 {
9467 recursively_compute_inclusions (result, all_children,
9468 all_type_symtabs, ptr, cust);
9469 }
95554aad
TT
9470}
9471
43f3e411 9472/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9473 PER_CU. */
9474
9475static void
43f3e411 9476compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9477{
f4dc4d17
DE
9478 gdb_assert (! per_cu->is_debug_types);
9479
ae640021 9480 if (!per_cu->imported_symtabs_empty ())
95554aad 9481 {
ae640021 9482 int len;
4c39bc03 9483 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9484 htab_t all_children, all_type_symtabs;
43f3e411 9485 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9486
9487 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9488 if (cust == NULL)
95554aad
TT
9489 return;
9490
9491 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9492 NULL, xcalloc, xfree);
ec94af83
DE
9493 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9494 NULL, xcalloc, xfree);
95554aad 9495
ae640021 9496 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9497 {
9498 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9499 all_type_symtabs, ptr, cust);
ec94af83 9500 }
95554aad 9501
ec94af83 9502 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9503 len = result_symtabs.size ();
43f3e411 9504 cust->includes
ed2dc618 9505 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9506 struct compunit_symtab *, len + 1);
4c39bc03
TT
9507 memcpy (cust->includes, result_symtabs.data (),
9508 len * sizeof (compunit_symtab *));
43f3e411 9509 cust->includes[len] = NULL;
95554aad 9510
95554aad 9511 htab_delete (all_children);
ec94af83 9512 htab_delete (all_type_symtabs);
95554aad
TT
9513 }
9514}
9515
9516/* Compute the 'includes' field for the symtabs of all the CUs we just
9517 read. */
9518
9519static void
ed2dc618 9520process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9521{
71b73764 9522 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9523 {
9524 if (! iter->is_debug_types)
43f3e411 9525 compute_compunit_symtab_includes (iter);
f4dc4d17 9526 }
95554aad 9527
c5d0225d 9528 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9529}
9530
9cdd5dbd 9531/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9532 already been loaded into memory. */
9533
9534static void
95554aad
TT
9535process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9536 enum language pretend_language)
10b3939b 9537{
10b3939b 9538 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9539 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9540 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9541 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9542 CORE_ADDR lowpc, highpc;
43f3e411 9543 struct compunit_symtab *cust;
10b3939b 9544 CORE_ADDR baseaddr;
4359dff1 9545 struct block *static_block;
3e29f34a 9546 CORE_ADDR addr;
10b3939b 9547
b3b3bada 9548 baseaddr = objfile->text_section_offset ();
10b3939b 9549
c89b44cd
TT
9550 /* Clear the list here in case something was left over. */
9551 cu->method_list.clear ();
10b3939b 9552
95554aad
TT
9553 cu->language = pretend_language;
9554 cu->language_defn = language_def (cu->language);
9555
c906108c 9556 /* Do line number decoding in read_file_scope () */
10b3939b 9557 process_die (cu->dies, cu);
c906108c 9558
a766d390
DE
9559 /* For now fudge the Go package. */
9560 if (cu->language == language_go)
9561 fixup_go_packaging (cu);
9562
5f48f8f3 9563 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9564 should be complete, and it should now be safe to compute all of the
9565 physnames. */
9566 compute_delayed_physnames (cu);
3da10d80 9567
c9317f21
TT
9568 if (cu->language == language_rust)
9569 rust_union_quirks (cu);
9570
fae299cd
DC
9571 /* Some compilers don't define a DW_AT_high_pc attribute for the
9572 compilation unit. If the DW_AT_high_pc is missing, synthesize
9573 it, by scanning the DIE's below the compilation unit. */
10b3939b 9574 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9575
3e29f34a 9576 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9577 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9578
9579 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9580 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9581 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9582 addrmap to help ensure it has an accurate map of pc values belonging to
9583 this comp unit. */
9584 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9585
c24bdb02 9586 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9587 SECT_OFF_TEXT (objfile),
9588 0);
c906108c 9589
43f3e411 9590 if (cust != NULL)
c906108c 9591 {
df15bd07 9592 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9593
8be455d7
JK
9594 /* Set symtab language to language from DW_AT_language. If the
9595 compilation is from a C file generated by language preprocessors, do
9596 not set the language if it was already deduced by start_subfile. */
43f3e411 9597 if (!(cu->language == language_c
40e3ad0e 9598 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9599 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9600
9601 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9602 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9603 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9604 there were bugs in prologue debug info, fixed later in GCC-4.5
9605 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9606
9607 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9608 needed, it would be wrong due to missing DW_AT_producer there.
9609
9610 Still one can confuse GDB by using non-standard GCC compilation
9611 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9612 */
ab260dad 9613 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9614 cust->locations_valid = 1;
e0d00bc7
JK
9615
9616 if (gcc_4_minor >= 5)
43f3e411 9617 cust->epilogue_unwind_valid = 1;
96408a79 9618
43f3e411 9619 cust->call_site_htab = cu->call_site_htab;
c906108c 9620 }
9291a0cd
TT
9621
9622 if (dwarf2_per_objfile->using_index)
43f3e411 9623 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9624 else
9625 {
891813be 9626 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9627 pst->compunit_symtab = cust;
6d94535f 9628 pst->readin = true;
9291a0cd 9629 }
c906108c 9630
95554aad 9631 /* Push it for inclusion processing later. */
c5d0225d 9632 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9633
9634 /* Not needed any more. */
c24bdb02 9635 cu->reset_builder ();
f4dc4d17 9636}
45cfd468 9637
f4dc4d17
DE
9638/* Generate full symbol information for type unit PER_CU, whose DIEs have
9639 already been loaded into memory. */
9640
9641static void
9642process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9643 enum language pretend_language)
9644{
9645 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9646 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9647 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9648 struct compunit_symtab *cust;
0186c6a7
DE
9649 struct signatured_type *sig_type;
9650
9651 gdb_assert (per_cu->is_debug_types);
9652 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9653
c89b44cd
TT
9654 /* Clear the list here in case something was left over. */
9655 cu->method_list.clear ();
f4dc4d17 9656
f4dc4d17
DE
9657 cu->language = pretend_language;
9658 cu->language_defn = language_def (cu->language);
9659
9660 /* The symbol tables are set up in read_type_unit_scope. */
9661 process_die (cu->dies, cu);
9662
9663 /* For now fudge the Go package. */
9664 if (cu->language == language_go)
9665 fixup_go_packaging (cu);
9666
5f48f8f3 9667 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9668 should be complete, and it should now be safe to compute all of the
9669 physnames. */
9670 compute_delayed_physnames (cu);
f4dc4d17 9671
c9317f21
TT
9672 if (cu->language == language_rust)
9673 rust_union_quirks (cu);
9674
f4dc4d17
DE
9675 /* TUs share symbol tables.
9676 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9677 of it with end_expandable_symtab. Otherwise, complete the addition of
9678 this TU's symbols to the existing symtab. */
43f3e411 9679 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9680 {
c24bdb02
KS
9681 buildsym_compunit *builder = cu->get_builder ();
9682 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9683 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9684
43f3e411 9685 if (cust != NULL)
f4dc4d17
DE
9686 {
9687 /* Set symtab language to language from DW_AT_language. If the
9688 compilation is from a C file generated by language preprocessors,
9689 do not set the language if it was already deduced by
9690 start_subfile. */
43f3e411
DE
9691 if (!(cu->language == language_c
9692 && COMPUNIT_FILETABS (cust)->language != language_c))
9693 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9694 }
9695 }
9696 else
9697 {
c24bdb02 9698 cu->get_builder ()->augment_type_symtab ();
43f3e411 9699 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9700 }
9701
9702 if (dwarf2_per_objfile->using_index)
43f3e411 9703 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9704 else
9705 {
891813be 9706 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9707 pst->compunit_symtab = cust;
6d94535f 9708 pst->readin = true;
45cfd468 9709 }
804d2729
TT
9710
9711 /* Not needed any more. */
c24bdb02 9712 cu->reset_builder ();
c906108c
SS
9713}
9714
95554aad
TT
9715/* Process an imported unit DIE. */
9716
9717static void
9718process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9719{
9720 struct attribute *attr;
9721
f4dc4d17
DE
9722 /* For now we don't handle imported units in type units. */
9723 if (cu->per_cu->is_debug_types)
9724 {
9725 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9726 " supported in type units [in module %s]"),
518817b3 9727 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9728 }
9729
95554aad
TT
9730 attr = dwarf2_attr (die, DW_AT_import, cu);
9731 if (attr != NULL)
9732 {
9c541725
PA
9733 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9734 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9735 dwarf2_per_cu_data *per_cu
e3b94546 9736 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9737 cu->per_cu->dwarf2_per_objfile);
95554aad 9738
69d751e3 9739 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9740 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9741 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9742
ae640021 9743 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9744 }
9745}
9746
4c8aa72d
PA
9747/* RAII object that represents a process_die scope: i.e.,
9748 starts/finishes processing a DIE. */
9749class process_die_scope
adde2bff 9750{
4c8aa72d
PA
9751public:
9752 process_die_scope (die_info *die, dwarf2_cu *cu)
9753 : m_die (die), m_cu (cu)
9754 {
9755 /* We should only be processing DIEs not already in process. */
9756 gdb_assert (!m_die->in_process);
9757 m_die->in_process = true;
9758 }
8c3cb9fa 9759
4c8aa72d
PA
9760 ~process_die_scope ()
9761 {
9762 m_die->in_process = false;
9763
9764 /* If we're done processing the DIE for the CU that owns the line
9765 header, we don't need the line header anymore. */
9766 if (m_cu->line_header_die_owner == m_die)
9767 {
9768 delete m_cu->line_header;
9769 m_cu->line_header = NULL;
9770 m_cu->line_header_die_owner = NULL;
9771 }
9772 }
9773
9774private:
9775 die_info *m_die;
9776 dwarf2_cu *m_cu;
9777};
adde2bff 9778
c906108c
SS
9779/* Process a die and its children. */
9780
9781static void
e7c27a73 9782process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9783{
4c8aa72d 9784 process_die_scope scope (die, cu);
adde2bff 9785
c906108c
SS
9786 switch (die->tag)
9787 {
9788 case DW_TAG_padding:
9789 break;
9790 case DW_TAG_compile_unit:
95554aad 9791 case DW_TAG_partial_unit:
e7c27a73 9792 read_file_scope (die, cu);
c906108c 9793 break;
348e048f
DE
9794 case DW_TAG_type_unit:
9795 read_type_unit_scope (die, cu);
9796 break;
c906108c 9797 case DW_TAG_subprogram:
0a4b0913
AB
9798 /* Nested subprograms in Fortran get a prefix. */
9799 if (cu->language == language_fortran
9800 && die->parent != NULL
9801 && die->parent->tag == DW_TAG_subprogram)
9802 cu->processing_has_namespace_info = true;
9803 /* Fall through. */
c906108c 9804 case DW_TAG_inlined_subroutine:
edb3359d 9805 read_func_scope (die, cu);
c906108c
SS
9806 break;
9807 case DW_TAG_lexical_block:
14898363
L
9808 case DW_TAG_try_block:
9809 case DW_TAG_catch_block:
e7c27a73 9810 read_lexical_block_scope (die, cu);
c906108c 9811 break;
216f72a1 9812 case DW_TAG_call_site:
96408a79
SA
9813 case DW_TAG_GNU_call_site:
9814 read_call_site_scope (die, cu);
9815 break;
c906108c 9816 case DW_TAG_class_type:
680b30c7 9817 case DW_TAG_interface_type:
c906108c
SS
9818 case DW_TAG_structure_type:
9819 case DW_TAG_union_type:
134d01f1 9820 process_structure_scope (die, cu);
c906108c
SS
9821 break;
9822 case DW_TAG_enumeration_type:
134d01f1 9823 process_enumeration_scope (die, cu);
c906108c 9824 break;
134d01f1 9825
f792889a
DJ
9826 /* These dies have a type, but processing them does not create
9827 a symbol or recurse to process the children. Therefore we can
9828 read them on-demand through read_type_die. */
c906108c 9829 case DW_TAG_subroutine_type:
72019c9c 9830 case DW_TAG_set_type:
c906108c 9831 case DW_TAG_array_type:
c906108c 9832 case DW_TAG_pointer_type:
c906108c 9833 case DW_TAG_ptr_to_member_type:
c906108c 9834 case DW_TAG_reference_type:
4297a3f0 9835 case DW_TAG_rvalue_reference_type:
c906108c 9836 case DW_TAG_string_type:
c906108c 9837 break;
134d01f1 9838
c906108c 9839 case DW_TAG_base_type:
a02abb62 9840 case DW_TAG_subrange_type:
cb249c71 9841 case DW_TAG_typedef:
134d01f1
DJ
9842 /* Add a typedef symbol for the type definition, if it has a
9843 DW_AT_name. */
f792889a 9844 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9845 break;
c906108c 9846 case DW_TAG_common_block:
e7c27a73 9847 read_common_block (die, cu);
c906108c
SS
9848 break;
9849 case DW_TAG_common_inclusion:
9850 break;
d9fa45fe 9851 case DW_TAG_namespace:
9068261f 9852 cu->processing_has_namespace_info = true;
e7c27a73 9853 read_namespace (die, cu);
d9fa45fe 9854 break;
5d7cb8df 9855 case DW_TAG_module:
9068261f 9856 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9857 read_module (die, cu);
9858 break;
d9fa45fe 9859 case DW_TAG_imported_declaration:
9068261f 9860 cu->processing_has_namespace_info = true;
74921315
KS
9861 if (read_namespace_alias (die, cu))
9862 break;
86a73007
TT
9863 /* The declaration is not a global namespace alias. */
9864 /* Fall through. */
d9fa45fe 9865 case DW_TAG_imported_module:
9068261f 9866 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9867 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9868 || cu->language != language_fortran))
b98664d3 9869 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9870 dwarf_tag_name (die->tag));
9871 read_import_statement (die, cu);
d9fa45fe 9872 break;
95554aad
TT
9873
9874 case DW_TAG_imported_unit:
9875 process_imported_unit_die (die, cu);
9876 break;
9877
71a3c369
TT
9878 case DW_TAG_variable:
9879 read_variable (die, cu);
9880 break;
9881
c906108c 9882 default:
e7c27a73 9883 new_symbol (die, NULL, cu);
c906108c
SS
9884 break;
9885 }
9886}
ca69b9e6
DE
9887\f
9888/* DWARF name computation. */
c906108c 9889
94af9270
KS
9890/* A helper function for dwarf2_compute_name which determines whether DIE
9891 needs to have the name of the scope prepended to the name listed in the
9892 die. */
9893
9894static int
9895die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9896{
1c809c68
TT
9897 struct attribute *attr;
9898
94af9270
KS
9899 switch (die->tag)
9900 {
9901 case DW_TAG_namespace:
9902 case DW_TAG_typedef:
9903 case DW_TAG_class_type:
9904 case DW_TAG_interface_type:
9905 case DW_TAG_structure_type:
9906 case DW_TAG_union_type:
9907 case DW_TAG_enumeration_type:
9908 case DW_TAG_enumerator:
9909 case DW_TAG_subprogram:
08a76f8a 9910 case DW_TAG_inlined_subroutine:
94af9270 9911 case DW_TAG_member:
74921315 9912 case DW_TAG_imported_declaration:
94af9270
KS
9913 return 1;
9914
9915 case DW_TAG_variable:
c2b0a229 9916 case DW_TAG_constant:
94af9270
KS
9917 /* We only need to prefix "globally" visible variables. These include
9918 any variable marked with DW_AT_external or any variable that
9919 lives in a namespace. [Variables in anonymous namespaces
9920 require prefixing, but they are not DW_AT_external.] */
9921
9922 if (dwarf2_attr (die, DW_AT_specification, cu))
9923 {
9924 struct dwarf2_cu *spec_cu = cu;
9a619af0 9925
94af9270
KS
9926 return die_needs_namespace (die_specification (die, &spec_cu),
9927 spec_cu);
9928 }
9929
1c809c68 9930 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9931 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9932 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9933 return 0;
9934 /* A variable in a lexical block of some kind does not need a
9935 namespace, even though in C++ such variables may be external
9936 and have a mangled name. */
9937 if (die->parent->tag == DW_TAG_lexical_block
9938 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9939 || die->parent->tag == DW_TAG_catch_block
9940 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9941 return 0;
9942 return 1;
94af9270
KS
9943
9944 default:
9945 return 0;
9946 }
9947}
9948
73b9be8b
KS
9949/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9950 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9951 defined for the given DIE. */
9952
9953static struct attribute *
9954dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9955{
9956 struct attribute *attr;
9957
9958 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9959 if (attr == NULL)
9960 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9961
9962 return attr;
9963}
9964
9965/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9966 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9967 defined for the given DIE. */
9968
9969static const char *
9970dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9971{
9972 const char *linkage_name;
9973
9974 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9975 if (linkage_name == NULL)
9976 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9977
9978 return linkage_name;
9979}
9980
94af9270 9981/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9982 compute the physname for the object, which include a method's:
9c37b5ae 9983 - formal parameters (C++),
a766d390 9984 - receiver type (Go),
a766d390
DE
9985
9986 The term "physname" is a bit confusing.
9987 For C++, for example, it is the demangled name.
9988 For Go, for example, it's the mangled name.
94af9270 9989
af6b7be1
JB
9990 For Ada, return the DIE's linkage name rather than the fully qualified
9991 name. PHYSNAME is ignored..
9992
94af9270
KS
9993 The result is allocated on the objfile_obstack and canonicalized. */
9994
9995static const char *
15d034d0
TT
9996dwarf2_compute_name (const char *name,
9997 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9998 int physname)
9999{
518817b3 10000 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10001
94af9270
KS
10002 if (name == NULL)
10003 name = dwarf2_name (die, cu);
10004
2ee7123e
DE
10005 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10006 but otherwise compute it by typename_concat inside GDB.
10007 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10008 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10009 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10010 will set the demangled name to the result of dwarf2_full_name, and it is
10011 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10012 if (cu->language == language_ada
10013 || (cu->language == language_fortran && physname))
10014 {
10015 /* For Ada unit, we prefer the linkage name over the name, as
10016 the former contains the exported name, which the user expects
10017 to be able to reference. Ideally, we want the user to be able
10018 to reference this entity using either natural or linkage name,
10019 but we haven't started looking at this enhancement yet. */
73b9be8b 10020 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10021
2ee7123e
DE
10022 if (linkage_name != NULL)
10023 return linkage_name;
f55ee35c
JK
10024 }
10025
94af9270
KS
10026 /* These are the only languages we know how to qualify names in. */
10027 if (name != NULL
9c37b5ae 10028 && (cu->language == language_cplus
c44af4eb
TT
10029 || cu->language == language_fortran || cu->language == language_d
10030 || cu->language == language_rust))
94af9270
KS
10031 {
10032 if (die_needs_namespace (die, cu))
10033 {
0d5cff50 10034 const char *prefix;
34a68019 10035 const char *canonical_name = NULL;
94af9270 10036
d7e74731
PA
10037 string_file buf;
10038
94af9270 10039 prefix = determine_prefix (die, cu);
94af9270
KS
10040 if (*prefix != '\0')
10041 {
43816ebc
TT
10042 gdb::unique_xmalloc_ptr<char> prefixed_name
10043 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10044
43816ebc 10045 buf.puts (prefixed_name.get ());
94af9270
KS
10046 }
10047 else
d7e74731 10048 buf.puts (name);
94af9270 10049
98bfdba5
PA
10050 /* Template parameters may be specified in the DIE's DW_AT_name, or
10051 as children with DW_TAG_template_type_param or
10052 DW_TAG_value_type_param. If the latter, add them to the name
10053 here. If the name already has template parameters, then
10054 skip this step; some versions of GCC emit both, and
10055 it is more efficient to use the pre-computed name.
10056
10057 Something to keep in mind about this process: it is very
10058 unlikely, or in some cases downright impossible, to produce
10059 something that will match the mangled name of a function.
10060 If the definition of the function has the same debug info,
10061 we should be able to match up with it anyway. But fallbacks
10062 using the minimal symbol, for instance to find a method
10063 implemented in a stripped copy of libstdc++, will not work.
10064 If we do not have debug info for the definition, we will have to
10065 match them up some other way.
10066
10067 When we do name matching there is a related problem with function
10068 templates; two instantiated function templates are allowed to
10069 differ only by their return types, which we do not add here. */
10070
10071 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10072 {
10073 struct attribute *attr;
10074 struct die_info *child;
10075 int first = 1;
10076
10077 die->building_fullname = 1;
10078
10079 for (child = die->child; child != NULL; child = child->sibling)
10080 {
10081 struct type *type;
12df843f 10082 LONGEST value;
d521ce57 10083 const gdb_byte *bytes;
98bfdba5
PA
10084 struct dwarf2_locexpr_baton *baton;
10085 struct value *v;
10086
10087 if (child->tag != DW_TAG_template_type_param
10088 && child->tag != DW_TAG_template_value_param)
10089 continue;
10090
10091 if (first)
10092 {
d7e74731 10093 buf.puts ("<");
98bfdba5
PA
10094 first = 0;
10095 }
10096 else
d7e74731 10097 buf.puts (", ");
98bfdba5
PA
10098
10099 attr = dwarf2_attr (child, DW_AT_type, cu);
10100 if (attr == NULL)
10101 {
b98664d3 10102 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10103 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10104 continue;
10105 }
10106 type = die_type (child, cu);
10107
10108 if (child->tag == DW_TAG_template_type_param)
10109 {
c1ec8cea
TT
10110 c_print_type (type, "", &buf, -1, 0, cu->language,
10111 &type_print_raw_options);
98bfdba5
PA
10112 continue;
10113 }
10114
10115 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10116 if (attr == NULL)
10117 {
b98664d3 10118 complaint (_("template parameter missing "
3e43a32a 10119 "DW_AT_const_value"));
d7e74731 10120 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10121 continue;
10122 }
10123
10124 dwarf2_const_value_attr (attr, type, name,
10125 &cu->comp_unit_obstack, cu,
10126 &value, &bytes, &baton);
10127
10128 if (TYPE_NOSIGN (type))
10129 /* GDB prints characters as NUMBER 'CHAR'. If that's
10130 changed, this can use value_print instead. */
d7e74731 10131 c_printchar (value, type, &buf);
98bfdba5
PA
10132 else
10133 {
10134 struct value_print_options opts;
10135
10136 if (baton != NULL)
10137 v = dwarf2_evaluate_loc_desc (type, NULL,
10138 baton->data,
10139 baton->size,
10140 baton->per_cu);
10141 else if (bytes != NULL)
10142 {
10143 v = allocate_value (type);
10144 memcpy (value_contents_writeable (v), bytes,
10145 TYPE_LENGTH (type));
10146 }
10147 else
10148 v = value_from_longest (type, value);
10149
3e43a32a
MS
10150 /* Specify decimal so that we do not depend on
10151 the radix. */
98bfdba5
PA
10152 get_formatted_print_options (&opts, 'd');
10153 opts.raw = 1;
d7e74731 10154 value_print (v, &buf, &opts);
98bfdba5 10155 release_value (v);
98bfdba5
PA
10156 }
10157 }
10158
10159 die->building_fullname = 0;
10160
10161 if (!first)
10162 {
10163 /* Close the argument list, with a space if necessary
10164 (nested templates). */
d7e74731
PA
10165 if (!buf.empty () && buf.string ().back () == '>')
10166 buf.puts (" >");
98bfdba5 10167 else
d7e74731 10168 buf.puts (">");
98bfdba5
PA
10169 }
10170 }
10171
9c37b5ae 10172 /* For C++ methods, append formal parameter type
94af9270 10173 information, if PHYSNAME. */
6e70227d 10174
94af9270 10175 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10176 && cu->language == language_cplus)
94af9270
KS
10177 {
10178 struct type *type = read_type_die (die, cu);
10179
d7e74731 10180 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10181 &type_print_raw_options);
94af9270 10182
9c37b5ae 10183 if (cu->language == language_cplus)
94af9270 10184 {
60430eff
DJ
10185 /* Assume that an artificial first parameter is
10186 "this", but do not crash if it is not. RealView
10187 marks unnamed (and thus unused) parameters as
10188 artificial; there is no way to differentiate
10189 the two cases. */
94af9270
KS
10190 if (TYPE_NFIELDS (type) > 0
10191 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10192 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10193 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10194 0))))
d7e74731 10195 buf.puts (" const");
94af9270
KS
10196 }
10197 }
10198
d7e74731 10199 const std::string &intermediate_name = buf.string ();
94af9270
KS
10200
10201 if (cu->language == language_cplus)
34a68019 10202 canonical_name
322a8516 10203 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10204 &objfile->per_bfd->storage_obstack);
10205
10206 /* If we only computed INTERMEDIATE_NAME, or if
10207 INTERMEDIATE_NAME is already canonical, then we need to
10208 copy it to the appropriate obstack. */
322a8516 10209 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10210 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10211 intermediate_name);
34a68019
TT
10212 else
10213 name = canonical_name;
94af9270
KS
10214 }
10215 }
10216
10217 return name;
10218}
10219
0114d602
DJ
10220/* Return the fully qualified name of DIE, based on its DW_AT_name.
10221 If scope qualifiers are appropriate they will be added. The result
34a68019 10222 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10223 not have a name. NAME may either be from a previous call to
10224 dwarf2_name or NULL.
10225
9c37b5ae 10226 The output string will be canonicalized (if C++). */
0114d602
DJ
10227
10228static const char *
15d034d0 10229dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10230{
94af9270
KS
10231 return dwarf2_compute_name (name, die, cu, 0);
10232}
0114d602 10233
94af9270
KS
10234/* Construct a physname for the given DIE in CU. NAME may either be
10235 from a previous call to dwarf2_name or NULL. The result will be
10236 allocated on the objfile_objstack or NULL if the DIE does not have a
10237 name.
0114d602 10238
9c37b5ae 10239 The output string will be canonicalized (if C++). */
0114d602 10240
94af9270 10241static const char *
15d034d0 10242dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10243{
518817b3 10244 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10245 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10246 int need_copy = 1;
10247
10248 /* In this case dwarf2_compute_name is just a shortcut not building anything
10249 on its own. */
10250 if (!die_needs_namespace (die, cu))
10251 return dwarf2_compute_name (name, die, cu, 1);
10252
73b9be8b 10253 mangled = dw2_linkage_name (die, cu);
900e11f9 10254
e98c9e7c
TT
10255 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10256 See https://github.com/rust-lang/rust/issues/32925. */
10257 if (cu->language == language_rust && mangled != NULL
10258 && strchr (mangled, '{') != NULL)
10259 mangled = NULL;
10260
900e11f9
JK
10261 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10262 has computed. */
791afaa2 10263 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10264 if (mangled != NULL)
900e11f9 10265 {
900e11f9 10266
59cc4834
JB
10267 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10268 {
10269 /* Do nothing (do not demangle the symbol name). */
10270 }
10271 else if (cu->language == language_go)
a766d390 10272 {
5e2db402
TT
10273 /* This is a lie, but we already lie to the caller new_symbol.
10274 new_symbol assumes we return the mangled name.
a766d390 10275 This just undoes that lie until things are cleaned up. */
a766d390
DE
10276 }
10277 else
10278 {
0eb876f5
JB
10279 /* Use DMGL_RET_DROP for C++ template functions to suppress
10280 their return type. It is easier for GDB users to search
10281 for such functions as `name(params)' than `long name(params)'.
10282 In such case the minimal symbol names do not match the full
10283 symbol names but for template functions there is never a need
10284 to look up their definition from their declaration so
10285 the only disadvantage remains the minimal symbol variant
10286 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10287 demangled.reset (gdb_demangle (mangled,
10288 (DMGL_PARAMS | DMGL_ANSI
10289 | DMGL_RET_DROP)));
a766d390 10290 }
900e11f9 10291 if (demangled)
791afaa2 10292 canon = demangled.get ();
900e11f9
JK
10293 else
10294 {
10295 canon = mangled;
10296 need_copy = 0;
10297 }
10298 }
10299
10300 if (canon == NULL || check_physname)
10301 {
10302 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10303
10304 if (canon != NULL && strcmp (physname, canon) != 0)
10305 {
10306 /* It may not mean a bug in GDB. The compiler could also
10307 compute DW_AT_linkage_name incorrectly. But in such case
10308 GDB would need to be bug-to-bug compatible. */
10309
b98664d3 10310 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10311 "(from linkage <%s>) - DIE at %s [in module %s]"),
10312 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10313 objfile_name (objfile));
900e11f9
JK
10314
10315 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10316 is available here - over computed PHYSNAME. It is safer
10317 against both buggy GDB and buggy compilers. */
10318
10319 retval = canon;
10320 }
10321 else
10322 {
10323 retval = physname;
10324 need_copy = 0;
10325 }
10326 }
10327 else
10328 retval = canon;
10329
10330 if (need_copy)
021887d8 10331 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 10332
900e11f9 10333 return retval;
0114d602
DJ
10334}
10335
74921315
KS
10336/* Inspect DIE in CU for a namespace alias. If one exists, record
10337 a new symbol for it.
10338
10339 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10340
10341static int
10342read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10343{
10344 struct attribute *attr;
10345
10346 /* If the die does not have a name, this is not a namespace
10347 alias. */
10348 attr = dwarf2_attr (die, DW_AT_name, cu);
10349 if (attr != NULL)
10350 {
10351 int num;
10352 struct die_info *d = die;
10353 struct dwarf2_cu *imported_cu = cu;
10354
10355 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10356 keep inspecting DIEs until we hit the underlying import. */
10357#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10358 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10359 {
10360 attr = dwarf2_attr (d, DW_AT_import, cu);
10361 if (attr == NULL)
10362 break;
10363
10364 d = follow_die_ref (d, attr, &imported_cu);
10365 if (d->tag != DW_TAG_imported_declaration)
10366 break;
10367 }
10368
10369 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10370 {
b98664d3 10371 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10372 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10373 return 0;
10374 }
10375
10376 if (attr != NULL)
10377 {
10378 struct type *type;
9c541725 10379 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10380
9c541725 10381 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10382 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10383 {
10384 /* This declaration is a global namespace alias. Add
10385 a symbol for it whose type is the aliased namespace. */
10386 new_symbol (die, type, cu);
10387 return 1;
10388 }
10389 }
10390 }
10391
10392 return 0;
10393}
10394
22cee43f 10395/* Return the using directives repository (global or local?) to use in the
804d2729 10396 current context for CU.
22cee43f
PMR
10397
10398 For Ada, imported declarations can materialize renamings, which *may* be
10399 global. However it is impossible (for now?) in DWARF to distinguish
10400 "external" imported declarations and "static" ones. As all imported
10401 declarations seem to be static in all other languages, make them all CU-wide
10402 global only in Ada. */
10403
10404static struct using_direct **
804d2729 10405using_directives (struct dwarf2_cu *cu)
22cee43f 10406{
c24bdb02
KS
10407 if (cu->language == language_ada
10408 && cu->get_builder ()->outermost_context_p ())
10409 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10410 else
c24bdb02 10411 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10412}
10413
27aa8d6a
SW
10414/* Read the import statement specified by the given die and record it. */
10415
10416static void
10417read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10418{
518817b3 10419 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10420 struct attribute *import_attr;
32019081 10421 struct die_info *imported_die, *child_die;
de4affc9 10422 struct dwarf2_cu *imported_cu;
27aa8d6a 10423 const char *imported_name;
794684b6 10424 const char *imported_name_prefix;
13387711
SW
10425 const char *canonical_name;
10426 const char *import_alias;
10427 const char *imported_declaration = NULL;
794684b6 10428 const char *import_prefix;
eb1e02fd 10429 std::vector<const char *> excludes;
13387711 10430
27aa8d6a
SW
10431 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10432 if (import_attr == NULL)
10433 {
b98664d3 10434 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10435 dwarf_tag_name (die->tag));
10436 return;
10437 }
10438
de4affc9
CC
10439 imported_cu = cu;
10440 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10441 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10442 if (imported_name == NULL)
10443 {
10444 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10445
10446 The import in the following code:
10447 namespace A
10448 {
10449 typedef int B;
10450 }
10451
10452 int main ()
10453 {
10454 using A::B;
10455 B b;
10456 return b;
10457 }
10458
10459 ...
10460 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10461 <52> DW_AT_decl_file : 1
10462 <53> DW_AT_decl_line : 6
10463 <54> DW_AT_import : <0x75>
10464 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10465 <59> DW_AT_name : B
10466 <5b> DW_AT_decl_file : 1
10467 <5c> DW_AT_decl_line : 2
10468 <5d> DW_AT_type : <0x6e>
10469 ...
10470 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10471 <76> DW_AT_byte_size : 4
10472 <77> DW_AT_encoding : 5 (signed)
10473
10474 imports the wrong die ( 0x75 instead of 0x58 ).
10475 This case will be ignored until the gcc bug is fixed. */
10476 return;
10477 }
10478
82856980
SW
10479 /* Figure out the local name after import. */
10480 import_alias = dwarf2_name (die, cu);
27aa8d6a 10481
794684b6
SW
10482 /* Figure out where the statement is being imported to. */
10483 import_prefix = determine_prefix (die, cu);
10484
10485 /* Figure out what the scope of the imported die is and prepend it
10486 to the name of the imported die. */
de4affc9 10487 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10488
f55ee35c
JK
10489 if (imported_die->tag != DW_TAG_namespace
10490 && imported_die->tag != DW_TAG_module)
794684b6 10491 {
13387711
SW
10492 imported_declaration = imported_name;
10493 canonical_name = imported_name_prefix;
794684b6 10494 }
13387711 10495 else if (strlen (imported_name_prefix) > 0)
12aaed36 10496 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10497 imported_name_prefix,
10498 (cu->language == language_d ? "." : "::"),
10499 imported_name, (char *) NULL);
13387711
SW
10500 else
10501 canonical_name = imported_name;
794684b6 10502
32019081
JK
10503 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10504 for (child_die = die->child; child_die && child_die->tag;
10505 child_die = sibling_die (child_die))
10506 {
10507 /* DWARF-4: A Fortran use statement with a “rename list” may be
10508 represented by an imported module entry with an import attribute
10509 referring to the module and owned entries corresponding to those
10510 entities that are renamed as part of being imported. */
10511
10512 if (child_die->tag != DW_TAG_imported_declaration)
10513 {
b98664d3 10514 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10515 "- DIE at %s [in module %s]"),
10516 sect_offset_str (child_die->sect_off),
10517 objfile_name (objfile));
32019081
JK
10518 continue;
10519 }
10520
10521 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10522 if (import_attr == NULL)
10523 {
b98664d3 10524 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10525 dwarf_tag_name (child_die->tag));
10526 continue;
10527 }
10528
10529 imported_cu = cu;
10530 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10531 &imported_cu);
10532 imported_name = dwarf2_name (imported_die, imported_cu);
10533 if (imported_name == NULL)
10534 {
b98664d3 10535 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10536 "imported name - DIE at %s [in module %s]"),
10537 sect_offset_str (child_die->sect_off),
10538 objfile_name (objfile));
32019081
JK
10539 continue;
10540 }
10541
eb1e02fd 10542 excludes.push_back (imported_name);
32019081
JK
10543
10544 process_die (child_die, cu);
10545 }
10546
804d2729 10547 add_using_directive (using_directives (cu),
22cee43f
PMR
10548 import_prefix,
10549 canonical_name,
10550 import_alias,
10551 imported_declaration,
10552 excludes,
10553 0,
10554 &objfile->objfile_obstack);
27aa8d6a
SW
10555}
10556
5230b05a
WT
10557/* ICC<14 does not output the required DW_AT_declaration on incomplete
10558 types, but gives them a size of zero. Starting with version 14,
10559 ICC is compatible with GCC. */
10560
9068261f 10561static bool
5230b05a
WT
10562producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10563{
10564 if (!cu->checked_producer)
10565 check_producer (cu);
10566
10567 return cu->producer_is_icc_lt_14;
10568}
10569
eb77c9df
AB
10570/* ICC generates a DW_AT_type for C void functions. This was observed on
10571 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10572 which says that void functions should not have a DW_AT_type. */
10573
10574static bool
10575producer_is_icc (struct dwarf2_cu *cu)
10576{
10577 if (!cu->checked_producer)
10578 check_producer (cu);
10579
10580 return cu->producer_is_icc;
10581}
10582
1b80a9fa
JK
10583/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10584 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10585 this, it was first present in GCC release 4.3.0. */
10586
9068261f 10587static bool
1b80a9fa
JK
10588producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10589{
10590 if (!cu->checked_producer)
10591 check_producer (cu);
10592
10593 return cu->producer_is_gcc_lt_4_3;
10594}
10595
d721ba37
PA
10596static file_and_directory
10597find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10598{
d721ba37
PA
10599 file_and_directory res;
10600
9291a0cd
TT
10601 /* Find the filename. Do not use dwarf2_name here, since the filename
10602 is not a source language identifier. */
d721ba37
PA
10603 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10604 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10605
d721ba37
PA
10606 if (res.comp_dir == NULL
10607 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10608 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10609 {
d721ba37
PA
10610 res.comp_dir_storage = ldirname (res.name);
10611 if (!res.comp_dir_storage.empty ())
10612 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10613 }
d721ba37 10614 if (res.comp_dir != NULL)
9291a0cd
TT
10615 {
10616 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10617 directory, get rid of it. */
d721ba37 10618 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10619
d721ba37
PA
10620 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10621 res.comp_dir = cp + 1;
9291a0cd
TT
10622 }
10623
d721ba37
PA
10624 if (res.name == NULL)
10625 res.name = "<unknown>";
10626
10627 return res;
9291a0cd
TT
10628}
10629
f4dc4d17
DE
10630/* Handle DW_AT_stmt_list for a compilation unit.
10631 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10632 COMP_DIR is the compilation directory. LOWPC is passed to
10633 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10634
10635static void
10636handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10637 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10638{
518817b3
SM
10639 struct dwarf2_per_objfile *dwarf2_per_objfile
10640 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10641 struct attribute *attr;
527f3840
JK
10642 struct line_header line_header_local;
10643 hashval_t line_header_local_hash;
527f3840
JK
10644 void **slot;
10645 int decode_mapping;
2ab95328 10646
f4dc4d17
DE
10647 gdb_assert (! cu->per_cu->is_debug_types);
10648
2ab95328 10649 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10650 if (attr == NULL)
10651 return;
10652
9c541725 10653 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10654
10655 /* The line header hash table is only created if needed (it exists to
10656 prevent redundant reading of the line table for partial_units).
10657 If we're given a partial_unit, we'll need it. If we're given a
10658 compile_unit, then use the line header hash table if it's already
10659 created, but don't create one just yet. */
10660
10661 if (dwarf2_per_objfile->line_header_hash == NULL
10662 && die->tag == DW_TAG_partial_unit)
2ab95328 10663 {
527f3840 10664 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10665 .reset (htab_create_alloc (127, line_header_hash_voidp,
10666 line_header_eq_voidp,
10667 free_line_header_voidp,
10668 xcalloc, xfree));
527f3840 10669 }
2ab95328 10670
9c541725 10671 line_header_local.sect_off = line_offset;
527f3840
JK
10672 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10673 line_header_local_hash = line_header_hash (&line_header_local);
10674 if (dwarf2_per_objfile->line_header_hash != NULL)
10675 {
d15acc42 10676 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10677 &line_header_local,
10678 line_header_local_hash, NO_INSERT);
10679
10680 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10681 is not present in *SLOT (since if there is something in *SLOT then
10682 it will be for a partial_unit). */
10683 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10684 {
527f3840 10685 gdb_assert (*slot != NULL);
9a3c8263 10686 cu->line_header = (struct line_header *) *slot;
527f3840 10687 return;
dee91e82 10688 }
2ab95328 10689 }
527f3840
JK
10690
10691 /* dwarf_decode_line_header does not yet provide sufficient information.
10692 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10693 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10694 if (lh == NULL)
527f3840 10695 return;
4c8aa72d
PA
10696
10697 cu->line_header = lh.release ();
10698 cu->line_header_die_owner = die;
527f3840
JK
10699
10700 if (dwarf2_per_objfile->line_header_hash == NULL)
10701 slot = NULL;
10702 else
10703 {
d15acc42 10704 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10705 &line_header_local,
10706 line_header_local_hash, INSERT);
10707 gdb_assert (slot != NULL);
10708 }
10709 if (slot != NULL && *slot == NULL)
10710 {
10711 /* This newly decoded line number information unit will be owned
10712 by line_header_hash hash table. */
10713 *slot = cu->line_header;
4c8aa72d 10714 cu->line_header_die_owner = NULL;
527f3840
JK
10715 }
10716 else
10717 {
10718 /* We cannot free any current entry in (*slot) as that struct line_header
10719 may be already used by multiple CUs. Create only temporary decoded
10720 line_header for this CU - it may happen at most once for each line
10721 number information unit. And if we're not using line_header_hash
10722 then this is what we want as well. */
10723 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10724 }
10725 decode_mapping = (die->tag != DW_TAG_partial_unit);
10726 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10727 decode_mapping);
fff8551c 10728
2ab95328
TT
10729}
10730
95554aad 10731/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10732
c906108c 10733static void
e7c27a73 10734read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10735{
518817b3
SM
10736 struct dwarf2_per_objfile *dwarf2_per_objfile
10737 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10738 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10739 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10740 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10741 CORE_ADDR highpc = ((CORE_ADDR) 0);
10742 struct attribute *attr;
c906108c 10743 struct die_info *child_die;
e142c38c 10744 CORE_ADDR baseaddr;
6e70227d 10745
380618d6 10746 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10747 baseaddr = objfile->text_section_offset ();
c906108c 10748
fae299cd 10749 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10750
10751 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10752 from finish_block. */
2acceee2 10753 if (lowpc == ((CORE_ADDR) -1))
c906108c 10754 lowpc = highpc;
3e29f34a 10755 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10756
d721ba37 10757 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10758
f4b8a18d
KW
10759 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10760 standardised yet. As a workaround for the language detection we fall
10761 back to the DW_AT_producer string. */
10762 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10763 cu->language = language_opencl;
10764
3019eac3
DE
10765 /* Similar hack for Go. */
10766 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10767 set_cu_language (DW_LANG_Go, cu);
10768
c24bdb02 10769 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10770
10771 /* Decode line number information if present. We do this before
10772 processing child DIEs, so that the line header table is available
10773 for DW_AT_decl_file. */
d721ba37 10774 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10775
10776 /* Process all dies in compilation unit. */
10777 if (die->child != NULL)
10778 {
10779 child_die = die->child;
10780 while (child_die && child_die->tag)
10781 {
10782 process_die (child_die, cu);
10783 child_die = sibling_die (child_die);
10784 }
10785 }
10786
10787 /* Decode macro information, if present. Dwarf 2 macro information
10788 refers to information in the line number info statement program
10789 header, so we can only read it if we've read the header
10790 successfully. */
0af92d60
JK
10791 attr = dwarf2_attr (die, DW_AT_macros, cu);
10792 if (attr == NULL)
10793 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10794 if (attr && cu->line_header)
10795 {
10796 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10797 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10798
43f3e411 10799 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10800 }
10801 else
10802 {
10803 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10804 if (attr && cu->line_header)
10805 {
10806 unsigned int macro_offset = DW_UNSND (attr);
10807
43f3e411 10808 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10809 }
10810 }
3019eac3
DE
10811}
10812
c24bdb02
KS
10813void
10814dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10815{
f4dc4d17
DE
10816 struct type_unit_group *tu_group;
10817 int first_time;
3019eac3 10818 struct attribute *attr;
9c541725 10819 unsigned int i;
0186c6a7 10820 struct signatured_type *sig_type;
3019eac3 10821
f4dc4d17 10822 gdb_assert (per_cu->is_debug_types);
0186c6a7 10823 sig_type = (struct signatured_type *) per_cu;
3019eac3 10824
c24bdb02 10825 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10826
f4dc4d17 10827 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10828 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10829 if (sig_type->type_unit_group == NULL)
c24bdb02 10830 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10831 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10832
10833 /* If we've already processed this stmt_list there's no real need to
10834 do it again, we could fake it and just recreate the part we need
10835 (file name,index -> symtab mapping). If data shows this optimization
10836 is useful we can do it then. */
43f3e411 10837 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10838
10839 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10840 debug info. */
fff8551c 10841 line_header_up lh;
f4dc4d17 10842 if (attr != NULL)
3019eac3 10843 {
9c541725 10844 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10845 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10846 }
10847 if (lh == NULL)
10848 {
10849 if (first_time)
c24bdb02 10850 start_symtab ("", NULL, 0);
f4dc4d17
DE
10851 else
10852 {
10853 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10854 gdb_assert (m_builder == nullptr);
804d2729 10855 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10856 m_builder.reset (new struct buildsym_compunit
10857 (COMPUNIT_OBJFILE (cust), "",
10858 COMPUNIT_DIRNAME (cust),
10859 compunit_language (cust),
10860 0, cust));
f4dc4d17 10861 }
f4dc4d17 10862 return;
3019eac3
DE
10863 }
10864
c24bdb02
KS
10865 line_header = lh.release ();
10866 line_header_die_owner = die;
3019eac3 10867
f4dc4d17
DE
10868 if (first_time)
10869 {
c24bdb02 10870 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10871
1fd60fc0
DE
10872 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10873 still initializing it, and our caller (a few levels up)
10874 process_full_type_unit still needs to know if this is the first
10875 time. */
10876
4ac93832
TT
10877 tu_group->symtabs
10878 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10879 struct symtab *, line_header->file_names_size ());
3019eac3 10880
7ba99d21
AT
10881 auto &file_names = line_header->file_names ();
10882 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10883 {
7ba99d21 10884 file_entry &fe = file_names[i];
c24bdb02
KS
10885 dwarf2_start_subfile (this, fe.name,
10886 fe.include_dir (line_header));
10887 buildsym_compunit *b = get_builder ();
10888 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10889 {
4c8aa72d
PA
10890 /* NOTE: start_subfile will recognize when it's been
10891 passed a file it has already seen. So we can't
10892 assume there's a simple mapping from
10893 cu->line_header->file_names to subfiles, plus
10894 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10895 b->get_current_subfile ()->symtab
10896 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10897 }
10898
c24bdb02 10899 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10900 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10901 }
10902 }
10903 else
3019eac3 10904 {
c24bdb02 10905 gdb_assert (m_builder == nullptr);
804d2729 10906 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10907 m_builder.reset (new struct buildsym_compunit
10908 (COMPUNIT_OBJFILE (cust), "",
10909 COMPUNIT_DIRNAME (cust),
10910 compunit_language (cust),
10911 0, cust));
f4dc4d17 10912
7ba99d21
AT
10913 auto &file_names = line_header->file_names ();
10914 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10915 {
7ba99d21 10916 file_entry &fe = file_names[i];
4c8aa72d 10917 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10918 }
3019eac3
DE
10919 }
10920
f4dc4d17
DE
10921 /* The main symtab is allocated last. Type units don't have DW_AT_name
10922 so they don't have a "real" (so to speak) symtab anyway.
10923 There is later code that will assign the main symtab to all symbols
10924 that don't have one. We need to handle the case of a symbol with a
10925 missing symtab (DW_AT_decl_file) anyway. */
10926}
3019eac3 10927
f4dc4d17
DE
10928/* Process DW_TAG_type_unit.
10929 For TUs we want to skip the first top level sibling if it's not the
10930 actual type being defined by this TU. In this case the first top
10931 level sibling is there to provide context only. */
3019eac3 10932
f4dc4d17
DE
10933static void
10934read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10935{
10936 struct die_info *child_die;
3019eac3 10937
f4dc4d17
DE
10938 prepare_one_comp_unit (cu, die, language_minimal);
10939
10940 /* Initialize (or reinitialize) the machinery for building symtabs.
10941 We do this before processing child DIEs, so that the line header table
10942 is available for DW_AT_decl_file. */
c24bdb02 10943 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10944
10945 if (die->child != NULL)
10946 {
10947 child_die = die->child;
10948 while (child_die && child_die->tag)
10949 {
10950 process_die (child_die, cu);
10951 child_die = sibling_die (child_die);
10952 }
10953 }
3019eac3
DE
10954}
10955\f
80626a55
DE
10956/* DWO/DWP files.
10957
10958 http://gcc.gnu.org/wiki/DebugFission
10959 http://gcc.gnu.org/wiki/DebugFissionDWP
10960
10961 To simplify handling of both DWO files ("object" files with the DWARF info)
10962 and DWP files (a file with the DWOs packaged up into one file), we treat
10963 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10964
10965static hashval_t
10966hash_dwo_file (const void *item)
10967{
9a3c8263 10968 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10969 hashval_t hash;
3019eac3 10970
a2ce51a0
DE
10971 hash = htab_hash_string (dwo_file->dwo_name);
10972 if (dwo_file->comp_dir != NULL)
10973 hash += htab_hash_string (dwo_file->comp_dir);
10974 return hash;
3019eac3
DE
10975}
10976
10977static int
10978eq_dwo_file (const void *item_lhs, const void *item_rhs)
10979{
9a3c8263
SM
10980 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10981 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10982
a2ce51a0
DE
10983 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10984 return 0;
10985 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10986 return lhs->comp_dir == rhs->comp_dir;
10987 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10988}
10989
10990/* Allocate a hash table for DWO files. */
10991
51ac9db5 10992static htab_up
298e9637 10993allocate_dwo_file_hash_table ()
3019eac3 10994{
51ac9db5
SM
10995 auto delete_dwo_file = [] (void *item)
10996 {
10997 struct dwo_file *dwo_file = (struct dwo_file *) item;
10998
10999 delete dwo_file;
11000 };
11001
bc68fb19
TT
11002 return htab_up (htab_create_alloc (41,
11003 hash_dwo_file,
11004 eq_dwo_file,
11005 delete_dwo_file,
11006 xcalloc, xfree));
3019eac3
DE
11007}
11008
80626a55
DE
11009/* Lookup DWO file DWO_NAME. */
11010
11011static void **
ed2dc618
SM
11012lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11013 const char *dwo_name,
11014 const char *comp_dir)
80626a55
DE
11015{
11016 struct dwo_file find_entry;
11017 void **slot;
11018
11019 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11020 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11021
0ac5b59e
DE
11022 find_entry.dwo_name = dwo_name;
11023 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11024 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11025 INSERT);
80626a55
DE
11026
11027 return slot;
11028}
11029
3019eac3
DE
11030static hashval_t
11031hash_dwo_unit (const void *item)
11032{
9a3c8263 11033 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11034
11035 /* This drops the top 32 bits of the id, but is ok for a hash. */
11036 return dwo_unit->signature;
11037}
11038
11039static int
11040eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11041{
9a3c8263
SM
11042 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11043 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11044
11045 /* The signature is assumed to be unique within the DWO file.
11046 So while object file CU dwo_id's always have the value zero,
11047 that's OK, assuming each object file DWO file has only one CU,
11048 and that's the rule for now. */
11049 return lhs->signature == rhs->signature;
11050}
11051
11052/* Allocate a hash table for DWO CUs,TUs.
11053 There is one of these tables for each of CUs,TUs for each DWO file. */
11054
b0b6a987 11055static htab_up
298e9637 11056allocate_dwo_unit_table ()
3019eac3
DE
11057{
11058 /* Start out with a pretty small number.
11059 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11060 return htab_up (htab_create_alloc (3,
11061 hash_dwo_unit,
11062 eq_dwo_unit,
11063 NULL, xcalloc, xfree));
3019eac3
DE
11064}
11065
19c3d4c9 11066/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11067
11068static void
19c3d4c9
DE
11069create_dwo_cu_reader (const struct die_reader_specs *reader,
11070 const gdb_byte *info_ptr,
11071 struct die_info *comp_unit_die,
c0ab21c2
TT
11072 struct dwo_file *dwo_file,
11073 struct dwo_unit *dwo_unit)
3019eac3
DE
11074{
11075 struct dwarf2_cu *cu = reader->cu;
9c541725 11076 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11077 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11078
a084a2a6
AT
11079 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11080 if (!signature.has_value ())
3019eac3 11081 {
b98664d3 11082 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11083 " its dwo_id [in module %s]"),
9d8780f0 11084 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11085 return;
11086 }
11087
3019eac3 11088 dwo_unit->dwo_file = dwo_file;
a084a2a6 11089 dwo_unit->signature = *signature;
8a0459fd 11090 dwo_unit->section = section;
9c541725 11091 dwo_unit->sect_off = sect_off;
3019eac3
DE
11092 dwo_unit->length = cu->per_cu->length;
11093
b4f54984 11094 if (dwarf_read_debug)
9d8780f0
SM
11095 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11096 sect_offset_str (sect_off),
9c541725 11097 hex_string (dwo_unit->signature));
3019eac3
DE
11098}
11099
33c5cd75 11100/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11101 Note: This function processes DWO files only, not DWP files. */
3019eac3 11102
33c5cd75 11103static void
ed2dc618 11104create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11105 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11106 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11107{
11108 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11109 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11110
96b79293 11111 section.read (objfile);
33c5cd75 11112 info_ptr = section.buffer;
3019eac3
DE
11113
11114 if (info_ptr == NULL)
33c5cd75 11115 return;
3019eac3 11116
b4f54984 11117 if (dwarf_read_debug)
19c3d4c9
DE
11118 {
11119 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11120 section.get_name (),
11121 section.get_file_name ());
19c3d4c9 11122 }
3019eac3 11123
33c5cd75 11124 end_ptr = info_ptr + section.size;
3019eac3
DE
11125 while (info_ptr < end_ptr)
11126 {
11127 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11128 struct dwo_unit read_unit {};
33c5cd75
DB
11129 struct dwo_unit *dwo_unit;
11130 void **slot;
11131 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11132
11133 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11134 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11135 per_cu.is_debug_types = 0;
33c5cd75
DB
11136 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11137 per_cu.section = &section;
11138
c0ab21c2
TT
11139 cutu_reader reader (&per_cu, cu, &dwo_file);
11140 if (!reader.dummy_p)
11141 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11142 &dwo_file, &read_unit);
33c5cd75
DB
11143 info_ptr += per_cu.length;
11144
11145 // If the unit could not be parsed, skip it.
c0ab21c2 11146 if (read_unit.dwo_file == NULL)
33c5cd75 11147 continue;
3019eac3 11148
33c5cd75 11149 if (cus_htab == NULL)
298e9637 11150 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11151
33c5cd75 11152 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11153 *dwo_unit = read_unit;
b0b6a987 11154 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11155 gdb_assert (slot != NULL);
11156 if (*slot != NULL)
19c3d4c9 11157 {
33c5cd75
DB
11158 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11159 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11160
b98664d3 11161 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11162 " the entry at offset %s, signature %s"),
11163 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11164 hex_string (dwo_unit->signature));
19c3d4c9 11165 }
33c5cd75 11166 *slot = (void *)dwo_unit;
3019eac3 11167 }
3019eac3
DE
11168}
11169
80626a55
DE
11170/* DWP file .debug_{cu,tu}_index section format:
11171 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11172
d2415c6c
DE
11173 DWP Version 1:
11174
80626a55
DE
11175 Both index sections have the same format, and serve to map a 64-bit
11176 signature to a set of section numbers. Each section begins with a header,
11177 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11178 indexes, and a pool of 32-bit section numbers. The index sections will be
11179 aligned at 8-byte boundaries in the file.
11180
d2415c6c
DE
11181 The index section header consists of:
11182
11183 V, 32 bit version number
11184 -, 32 bits unused
11185 N, 32 bit number of compilation units or type units in the index
11186 M, 32 bit number of slots in the hash table
80626a55 11187
d2415c6c 11188 Numbers are recorded using the byte order of the application binary.
80626a55 11189
d2415c6c
DE
11190 The hash table begins at offset 16 in the section, and consists of an array
11191 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11192 order of the application binary). Unused slots in the hash table are 0.
11193 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11194
d2415c6c
DE
11195 The parallel table begins immediately after the hash table
11196 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11197 array of 32-bit indexes (using the byte order of the application binary),
11198 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11199 table contains a 32-bit index into the pool of section numbers. For unused
11200 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11201
73869dc2
DE
11202 The pool of section numbers begins immediately following the hash table
11203 (at offset 16 + 12 * M from the beginning of the section). The pool of
11204 section numbers consists of an array of 32-bit words (using the byte order
11205 of the application binary). Each item in the array is indexed starting
11206 from 0. The hash table entry provides the index of the first section
11207 number in the set. Additional section numbers in the set follow, and the
11208 set is terminated by a 0 entry (section number 0 is not used in ELF).
11209
11210 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11211 section must be the first entry in the set, and the .debug_abbrev.dwo must
11212 be the second entry. Other members of the set may follow in any order.
11213
11214 ---
11215
11216 DWP Version 2:
11217
11218 DWP Version 2 combines all the .debug_info, etc. sections into one,
11219 and the entries in the index tables are now offsets into these sections.
11220 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11221 section.
11222
11223 Index Section Contents:
11224 Header
11225 Hash Table of Signatures dwp_hash_table.hash_table
11226 Parallel Table of Indices dwp_hash_table.unit_table
11227 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11228 Table of Section Sizes dwp_hash_table.v2.sizes
11229
11230 The index section header consists of:
11231
11232 V, 32 bit version number
11233 L, 32 bit number of columns in the table of section offsets
11234 N, 32 bit number of compilation units or type units in the index
11235 M, 32 bit number of slots in the hash table
11236
11237 Numbers are recorded using the byte order of the application binary.
11238
11239 The hash table has the same format as version 1.
11240 The parallel table of indices has the same format as version 1,
11241 except that the entries are origin-1 indices into the table of sections
11242 offsets and the table of section sizes.
11243
11244 The table of offsets begins immediately following the parallel table
11245 (at offset 16 + 12 * M from the beginning of the section). The table is
11246 a two-dimensional array of 32-bit words (using the byte order of the
11247 application binary), with L columns and N+1 rows, in row-major order.
11248 Each row in the array is indexed starting from 0. The first row provides
11249 a key to the remaining rows: each column in this row provides an identifier
11250 for a debug section, and the offsets in the same column of subsequent rows
11251 refer to that section. The section identifiers are:
11252
11253 DW_SECT_INFO 1 .debug_info.dwo
11254 DW_SECT_TYPES 2 .debug_types.dwo
11255 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11256 DW_SECT_LINE 4 .debug_line.dwo
11257 DW_SECT_LOC 5 .debug_loc.dwo
11258 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11259 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11260 DW_SECT_MACRO 8 .debug_macro.dwo
11261
11262 The offsets provided by the CU and TU index sections are the base offsets
11263 for the contributions made by each CU or TU to the corresponding section
11264 in the package file. Each CU and TU header contains an abbrev_offset
11265 field, used to find the abbreviations table for that CU or TU within the
11266 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11267 be interpreted as relative to the base offset given in the index section.
11268 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11269 should be interpreted as relative to the base offset for .debug_line.dwo,
11270 and offsets into other debug sections obtained from DWARF attributes should
11271 also be interpreted as relative to the corresponding base offset.
11272
11273 The table of sizes begins immediately following the table of offsets.
11274 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11275 with L columns and N rows, in row-major order. Each row in the array is
11276 indexed starting from 1 (row 0 is shared by the two tables).
11277
11278 ---
11279
11280 Hash table lookup is handled the same in version 1 and 2:
11281
11282 We assume that N and M will not exceed 2^32 - 1.
11283 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11284
d2415c6c
DE
11285 Given a 64-bit compilation unit signature or a type signature S, an entry
11286 in the hash table is located as follows:
80626a55 11287
d2415c6c
DE
11288 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11289 the low-order k bits all set to 1.
80626a55 11290
d2415c6c 11291 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11292
d2415c6c
DE
11293 3) If the hash table entry at index H matches the signature, use that
11294 entry. If the hash table entry at index H is unused (all zeroes),
11295 terminate the search: the signature is not present in the table.
80626a55 11296
d2415c6c 11297 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11298
d2415c6c 11299 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11300 to stop at an unused slot or find the match. */
80626a55
DE
11301
11302/* Create a hash table to map DWO IDs to their CU/TU entry in
11303 .debug_{info,types}.dwo in DWP_FILE.
11304 Returns NULL if there isn't one.
11305 Note: This function processes DWP files only, not DWO files. */
11306
11307static struct dwp_hash_table *
ed2dc618
SM
11308create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11309 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11310{
11311 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11312 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11313 const gdb_byte *index_ptr, *index_end;
80626a55 11314 struct dwarf2_section_info *index;
73869dc2 11315 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11316 struct dwp_hash_table *htab;
11317
11318 if (is_debug_types)
11319 index = &dwp_file->sections.tu_index;
11320 else
11321 index = &dwp_file->sections.cu_index;
11322
96b79293 11323 if (index->empty ())
80626a55 11324 return NULL;
96b79293 11325 index->read (objfile);
80626a55
DE
11326
11327 index_ptr = index->buffer;
11328 index_end = index_ptr + index->size;
11329
11330 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11331 index_ptr += 4;
11332 if (version == 2)
11333 nr_columns = read_4_bytes (dbfd, index_ptr);
11334 else
11335 nr_columns = 0;
11336 index_ptr += 4;
80626a55
DE
11337 nr_units = read_4_bytes (dbfd, index_ptr);
11338 index_ptr += 4;
11339 nr_slots = read_4_bytes (dbfd, index_ptr);
11340 index_ptr += 4;
11341
73869dc2 11342 if (version != 1 && version != 2)
80626a55 11343 {
21aa081e 11344 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11345 " [in module %s]"),
21aa081e 11346 pulongest (version), dwp_file->name);
80626a55
DE
11347 }
11348 if (nr_slots != (nr_slots & -nr_slots))
11349 {
21aa081e 11350 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11351 " is not power of 2 [in module %s]"),
21aa081e 11352 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11353 }
11354
11355 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11356 htab->version = version;
11357 htab->nr_columns = nr_columns;
80626a55
DE
11358 htab->nr_units = nr_units;
11359 htab->nr_slots = nr_slots;
11360 htab->hash_table = index_ptr;
11361 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11362
11363 /* Exit early if the table is empty. */
11364 if (nr_slots == 0 || nr_units == 0
11365 || (version == 2 && nr_columns == 0))
11366 {
11367 /* All must be zero. */
11368 if (nr_slots != 0 || nr_units != 0
11369 || (version == 2 && nr_columns != 0))
11370 {
b98664d3 11371 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11372 " all zero [in modules %s]"),
11373 dwp_file->name);
11374 }
11375 return htab;
11376 }
11377
11378 if (version == 1)
11379 {
11380 htab->section_pool.v1.indices =
11381 htab->unit_table + sizeof (uint32_t) * nr_slots;
11382 /* It's harder to decide whether the section is too small in v1.
11383 V1 is deprecated anyway so we punt. */
11384 }
11385 else
11386 {
11387 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11388 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11389 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11390 /* Reverse map for error checking. */
11391 int ids_seen[DW_SECT_MAX + 1];
11392 int i;
11393
11394 if (nr_columns < 2)
11395 {
11396 error (_("Dwarf Error: bad DWP hash table, too few columns"
11397 " in section table [in module %s]"),
11398 dwp_file->name);
11399 }
11400 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11401 {
11402 error (_("Dwarf Error: bad DWP hash table, too many columns"
11403 " in section table [in module %s]"),
11404 dwp_file->name);
11405 }
04fd5eed
GB
11406 memset (ids, 255, sizeof_ids);
11407 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11408 for (i = 0; i < nr_columns; ++i)
11409 {
11410 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11411
11412 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11413 {
11414 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11415 " in section table [in module %s]"),
11416 id, dwp_file->name);
11417 }
11418 if (ids_seen[id] != -1)
11419 {
11420 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11421 " id %d in section table [in module %s]"),
11422 id, dwp_file->name);
11423 }
11424 ids_seen[id] = i;
11425 ids[i] = id;
11426 }
11427 /* Must have exactly one info or types section. */
11428 if (((ids_seen[DW_SECT_INFO] != -1)
11429 + (ids_seen[DW_SECT_TYPES] != -1))
11430 != 1)
11431 {
11432 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11433 " DWO info/types section [in module %s]"),
11434 dwp_file->name);
11435 }
11436 /* Must have an abbrev section. */
11437 if (ids_seen[DW_SECT_ABBREV] == -1)
11438 {
11439 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11440 " section [in module %s]"),
11441 dwp_file->name);
11442 }
11443 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11444 htab->section_pool.v2.sizes =
11445 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11446 * nr_units * nr_columns);
11447 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11448 * nr_units * nr_columns))
11449 > index_end)
11450 {
11451 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11452 " [in module %s]"),
11453 dwp_file->name);
11454 }
11455 }
80626a55
DE
11456
11457 return htab;
11458}
11459
11460/* Update SECTIONS with the data from SECTP.
11461
11462 This function is like the other "locate" section routines that are
11463 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11464 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11465
11466 The result is non-zero for success, or zero if an error was found. */
11467
11468static int
73869dc2
DE
11469locate_v1_virtual_dwo_sections (asection *sectp,
11470 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11471{
11472 const struct dwop_section_names *names = &dwop_section_names;
11473
11474 if (section_is_p (sectp->name, &names->abbrev_dwo))
11475 {
11476 /* There can be only one. */
049412e3 11477 if (sections->abbrev.s.section != NULL)
80626a55 11478 return 0;
049412e3 11479 sections->abbrev.s.section = sectp;
fd361982 11480 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11481 }
11482 else if (section_is_p (sectp->name, &names->info_dwo)
11483 || section_is_p (sectp->name, &names->types_dwo))
11484 {
11485 /* There can be only one. */
049412e3 11486 if (sections->info_or_types.s.section != NULL)
80626a55 11487 return 0;
049412e3 11488 sections->info_or_types.s.section = sectp;
fd361982 11489 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11490 }
11491 else if (section_is_p (sectp->name, &names->line_dwo))
11492 {
11493 /* There can be only one. */
049412e3 11494 if (sections->line.s.section != NULL)
80626a55 11495 return 0;
049412e3 11496 sections->line.s.section = sectp;
fd361982 11497 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11498 }
11499 else if (section_is_p (sectp->name, &names->loc_dwo))
11500 {
11501 /* There can be only one. */
049412e3 11502 if (sections->loc.s.section != NULL)
80626a55 11503 return 0;
049412e3 11504 sections->loc.s.section = sectp;
fd361982 11505 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11506 }
11507 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11508 {
11509 /* There can be only one. */
049412e3 11510 if (sections->macinfo.s.section != NULL)
80626a55 11511 return 0;
049412e3 11512 sections->macinfo.s.section = sectp;
fd361982 11513 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11514 }
11515 else if (section_is_p (sectp->name, &names->macro_dwo))
11516 {
11517 /* There can be only one. */
049412e3 11518 if (sections->macro.s.section != NULL)
80626a55 11519 return 0;
049412e3 11520 sections->macro.s.section = sectp;
fd361982 11521 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11522 }
11523 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11524 {
11525 /* There can be only one. */
049412e3 11526 if (sections->str_offsets.s.section != NULL)
80626a55 11527 return 0;
049412e3 11528 sections->str_offsets.s.section = sectp;
fd361982 11529 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11530 }
11531 else
11532 {
11533 /* No other kind of section is valid. */
11534 return 0;
11535 }
11536
11537 return 1;
11538}
11539
73869dc2
DE
11540/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11541 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11542 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11543 This is for DWP version 1 files. */
80626a55
DE
11544
11545static struct dwo_unit *
ed2dc618
SM
11546create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11547 struct dwp_file *dwp_file,
73869dc2
DE
11548 uint32_t unit_index,
11549 const char *comp_dir,
11550 ULONGEST signature, int is_debug_types)
80626a55
DE
11551{
11552 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11553 const struct dwp_hash_table *dwp_htab =
11554 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11555 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11556 const char *kind = is_debug_types ? "TU" : "CU";
11557 struct dwo_file *dwo_file;
11558 struct dwo_unit *dwo_unit;
73869dc2 11559 struct virtual_v1_dwo_sections sections;
80626a55 11560 void **dwo_file_slot;
80626a55
DE
11561 int i;
11562
73869dc2
DE
11563 gdb_assert (dwp_file->version == 1);
11564
b4f54984 11565 if (dwarf_read_debug)
80626a55 11566 {
73869dc2 11567 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11568 kind,
73869dc2 11569 pulongest (unit_index), hex_string (signature),
80626a55
DE
11570 dwp_file->name);
11571 }
11572
19ac8c2e 11573 /* Fetch the sections of this DWO unit.
80626a55
DE
11574 Put a limit on the number of sections we look for so that bad data
11575 doesn't cause us to loop forever. */
11576
73869dc2 11577#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11578 (1 /* .debug_info or .debug_types */ \
11579 + 1 /* .debug_abbrev */ \
11580 + 1 /* .debug_line */ \
11581 + 1 /* .debug_loc */ \
11582 + 1 /* .debug_str_offsets */ \
19ac8c2e 11583 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11584 + 1 /* trailing zero */)
11585
11586 memset (&sections, 0, sizeof (sections));
80626a55 11587
73869dc2 11588 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11589 {
11590 asection *sectp;
11591 uint32_t section_nr =
11592 read_4_bytes (dbfd,
73869dc2
DE
11593 dwp_htab->section_pool.v1.indices
11594 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11595
11596 if (section_nr == 0)
11597 break;
11598 if (section_nr >= dwp_file->num_sections)
11599 {
11600 error (_("Dwarf Error: bad DWP hash table, section number too large"
11601 " [in module %s]"),
11602 dwp_file->name);
11603 }
11604
11605 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11606 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11607 {
11608 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11609 " [in module %s]"),
11610 dwp_file->name);
11611 }
11612 }
11613
11614 if (i < 2
96b79293
TT
11615 || sections.info_or_types.empty ()
11616 || sections.abbrev.empty ())
80626a55
DE
11617 {
11618 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11619 " [in module %s]"),
11620 dwp_file->name);
11621 }
73869dc2 11622 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11623 {
11624 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11625 " [in module %s]"),
11626 dwp_file->name);
11627 }
11628
11629 /* It's easier for the rest of the code if we fake a struct dwo_file and
11630 have dwo_unit "live" in that. At least for now.
11631
11632 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11633 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11634 file, we can combine them back into a virtual DWO file to save space
11635 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11636 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11637
791afaa2
TT
11638 std::string virtual_dwo_name =
11639 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11640 sections.abbrev.get_id (),
11641 sections.line.get_id (),
11642 sections.loc.get_id (),
11643 sections.str_offsets.get_id ());
80626a55 11644 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11645 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11646 virtual_dwo_name.c_str (),
11647 comp_dir);
80626a55
DE
11648 /* Create one if necessary. */
11649 if (*dwo_file_slot == NULL)
11650 {
b4f54984 11651 if (dwarf_read_debug)
80626a55
DE
11652 {
11653 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11654 virtual_dwo_name.c_str ());
80626a55 11655 }
51ac9db5 11656 dwo_file = new struct dwo_file;
efba19b0
TT
11657 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11658 virtual_dwo_name);
0ac5b59e 11659 dwo_file->comp_dir = comp_dir;
80626a55
DE
11660 dwo_file->sections.abbrev = sections.abbrev;
11661 dwo_file->sections.line = sections.line;
11662 dwo_file->sections.loc = sections.loc;
11663 dwo_file->sections.macinfo = sections.macinfo;
11664 dwo_file->sections.macro = sections.macro;
11665 dwo_file->sections.str_offsets = sections.str_offsets;
11666 /* The "str" section is global to the entire DWP file. */
11667 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11668 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11669 there's no need to record it in dwo_file.
11670 Also, we can't simply record type sections in dwo_file because
11671 we record a pointer into the vector in dwo_unit. As we collect more
11672 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11673 for it, invalidating all copies of pointers into the previous
11674 contents. */
80626a55
DE
11675 *dwo_file_slot = dwo_file;
11676 }
11677 else
11678 {
b4f54984 11679 if (dwarf_read_debug)
80626a55
DE
11680 {
11681 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11682 virtual_dwo_name.c_str ());
80626a55 11683 }
9a3c8263 11684 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11685 }
80626a55
DE
11686
11687 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11688 dwo_unit->dwo_file = dwo_file;
11689 dwo_unit->signature = signature;
8d749320
SM
11690 dwo_unit->section =
11691 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11692 *dwo_unit->section = sections.info_or_types;
57d63ce2 11693 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11694
11695 return dwo_unit;
11696}
11697
73869dc2
DE
11698/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11699 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11700 piece within that section used by a TU/CU, return a virtual section
11701 of just that piece. */
11702
11703static struct dwarf2_section_info
ed2dc618
SM
11704create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11705 struct dwarf2_section_info *section,
73869dc2
DE
11706 bfd_size_type offset, bfd_size_type size)
11707{
11708 struct dwarf2_section_info result;
11709 asection *sectp;
11710
11711 gdb_assert (section != NULL);
11712 gdb_assert (!section->is_virtual);
11713
11714 memset (&result, 0, sizeof (result));
11715 result.s.containing_section = section;
dc4ccb6f 11716 result.is_virtual = true;
73869dc2
DE
11717
11718 if (size == 0)
11719 return result;
11720
96b79293 11721 sectp = section->get_bfd_section ();
73869dc2
DE
11722
11723 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11724 bounds of the real section. This is a pretty-rare event, so just
11725 flag an error (easier) instead of a warning and trying to cope. */
11726 if (sectp == NULL
fd361982 11727 || offset + size > bfd_section_size (sectp))
73869dc2 11728 {
73869dc2
DE
11729 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11730 " in section %s [in module %s]"),
fd361982 11731 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11732 objfile_name (dwarf2_per_objfile->objfile));
11733 }
11734
11735 result.virtual_offset = offset;
11736 result.size = size;
11737 return result;
11738}
11739
11740/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11741 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11742 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11743 This is for DWP version 2 files. */
11744
11745static struct dwo_unit *
ed2dc618
SM
11746create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11747 struct dwp_file *dwp_file,
73869dc2
DE
11748 uint32_t unit_index,
11749 const char *comp_dir,
11750 ULONGEST signature, int is_debug_types)
11751{
11752 struct objfile *objfile = dwarf2_per_objfile->objfile;
11753 const struct dwp_hash_table *dwp_htab =
11754 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11755 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11756 const char *kind = is_debug_types ? "TU" : "CU";
11757 struct dwo_file *dwo_file;
11758 struct dwo_unit *dwo_unit;
11759 struct virtual_v2_dwo_sections sections;
11760 void **dwo_file_slot;
73869dc2
DE
11761 int i;
11762
11763 gdb_assert (dwp_file->version == 2);
11764
b4f54984 11765 if (dwarf_read_debug)
73869dc2
DE
11766 {
11767 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11768 kind,
11769 pulongest (unit_index), hex_string (signature),
11770 dwp_file->name);
11771 }
11772
11773 /* Fetch the section offsets of this DWO unit. */
11774
11775 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11776
11777 for (i = 0; i < dwp_htab->nr_columns; ++i)
11778 {
11779 uint32_t offset = read_4_bytes (dbfd,
11780 dwp_htab->section_pool.v2.offsets
11781 + (((unit_index - 1) * dwp_htab->nr_columns
11782 + i)
11783 * sizeof (uint32_t)));
11784 uint32_t size = read_4_bytes (dbfd,
11785 dwp_htab->section_pool.v2.sizes
11786 + (((unit_index - 1) * dwp_htab->nr_columns
11787 + i)
11788 * sizeof (uint32_t)));
11789
11790 switch (dwp_htab->section_pool.v2.section_ids[i])
11791 {
11792 case DW_SECT_INFO:
11793 case DW_SECT_TYPES:
11794 sections.info_or_types_offset = offset;
11795 sections.info_or_types_size = size;
11796 break;
11797 case DW_SECT_ABBREV:
11798 sections.abbrev_offset = offset;
11799 sections.abbrev_size = size;
11800 break;
11801 case DW_SECT_LINE:
11802 sections.line_offset = offset;
11803 sections.line_size = size;
11804 break;
11805 case DW_SECT_LOC:
11806 sections.loc_offset = offset;
11807 sections.loc_size = size;
11808 break;
11809 case DW_SECT_STR_OFFSETS:
11810 sections.str_offsets_offset = offset;
11811 sections.str_offsets_size = size;
11812 break;
11813 case DW_SECT_MACINFO:
11814 sections.macinfo_offset = offset;
11815 sections.macinfo_size = size;
11816 break;
11817 case DW_SECT_MACRO:
11818 sections.macro_offset = offset;
11819 sections.macro_size = size;
11820 break;
11821 }
11822 }
11823
11824 /* It's easier for the rest of the code if we fake a struct dwo_file and
11825 have dwo_unit "live" in that. At least for now.
11826
11827 The DWP file can be made up of a random collection of CUs and TUs.
11828 However, for each CU + set of TUs that came from the same original DWO
11829 file, we can combine them back into a virtual DWO file to save space
11830 (fewer struct dwo_file objects to allocate). Remember that for really
11831 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11832
791afaa2
TT
11833 std::string virtual_dwo_name =
11834 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11835 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11836 (long) (sections.line_size ? sections.line_offset : 0),
11837 (long) (sections.loc_size ? sections.loc_offset : 0),
11838 (long) (sections.str_offsets_size
11839 ? sections.str_offsets_offset : 0));
73869dc2 11840 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11841 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11842 virtual_dwo_name.c_str (),
11843 comp_dir);
73869dc2
DE
11844 /* Create one if necessary. */
11845 if (*dwo_file_slot == NULL)
11846 {
b4f54984 11847 if (dwarf_read_debug)
73869dc2
DE
11848 {
11849 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11850 virtual_dwo_name.c_str ());
73869dc2 11851 }
51ac9db5 11852 dwo_file = new struct dwo_file;
efba19b0
TT
11853 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11854 virtual_dwo_name);
73869dc2
DE
11855 dwo_file->comp_dir = comp_dir;
11856 dwo_file->sections.abbrev =
ed2dc618 11857 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11858 sections.abbrev_offset, sections.abbrev_size);
11859 dwo_file->sections.line =
ed2dc618 11860 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11861 sections.line_offset, sections.line_size);
11862 dwo_file->sections.loc =
ed2dc618 11863 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11864 sections.loc_offset, sections.loc_size);
11865 dwo_file->sections.macinfo =
ed2dc618 11866 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11867 sections.macinfo_offset, sections.macinfo_size);
11868 dwo_file->sections.macro =
ed2dc618 11869 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11870 sections.macro_offset, sections.macro_size);
11871 dwo_file->sections.str_offsets =
ed2dc618
SM
11872 create_dwp_v2_section (dwarf2_per_objfile,
11873 &dwp_file->sections.str_offsets,
73869dc2
DE
11874 sections.str_offsets_offset,
11875 sections.str_offsets_size);
11876 /* The "str" section is global to the entire DWP file. */
11877 dwo_file->sections.str = dwp_file->sections.str;
11878 /* The info or types section is assigned below to dwo_unit,
11879 there's no need to record it in dwo_file.
11880 Also, we can't simply record type sections in dwo_file because
11881 we record a pointer into the vector in dwo_unit. As we collect more
11882 types we'll grow the vector and eventually have to reallocate space
11883 for it, invalidating all copies of pointers into the previous
11884 contents. */
11885 *dwo_file_slot = dwo_file;
11886 }
11887 else
11888 {
b4f54984 11889 if (dwarf_read_debug)
73869dc2
DE
11890 {
11891 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11892 virtual_dwo_name.c_str ());
73869dc2 11893 }
9a3c8263 11894 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11895 }
73869dc2
DE
11896
11897 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11898 dwo_unit->dwo_file = dwo_file;
11899 dwo_unit->signature = signature;
8d749320
SM
11900 dwo_unit->section =
11901 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11902 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11903 is_debug_types
73869dc2
DE
11904 ? &dwp_file->sections.types
11905 : &dwp_file->sections.info,
11906 sections.info_or_types_offset,
11907 sections.info_or_types_size);
11908 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11909
11910 return dwo_unit;
11911}
11912
57d63ce2
DE
11913/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11914 Returns NULL if the signature isn't found. */
80626a55
DE
11915
11916static struct dwo_unit *
ed2dc618
SM
11917lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11918 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11919 ULONGEST signature, int is_debug_types)
80626a55 11920{
57d63ce2
DE
11921 const struct dwp_hash_table *dwp_htab =
11922 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11923 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11924 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11925 uint32_t hash = signature & mask;
11926 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11927 unsigned int i;
11928 void **slot;
870f88f7 11929 struct dwo_unit find_dwo_cu;
80626a55
DE
11930
11931 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11932 find_dwo_cu.signature = signature;
19ac8c2e 11933 slot = htab_find_slot (is_debug_types
48b490f2
TT
11934 ? dwp_file->loaded_tus.get ()
11935 : dwp_file->loaded_cus.get (),
19ac8c2e 11936 &find_dwo_cu, INSERT);
80626a55
DE
11937
11938 if (*slot != NULL)
9a3c8263 11939 return (struct dwo_unit *) *slot;
80626a55
DE
11940
11941 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11942 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11943 {
11944 ULONGEST signature_in_table;
11945
11946 signature_in_table =
57d63ce2 11947 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11948 if (signature_in_table == signature)
11949 {
57d63ce2
DE
11950 uint32_t unit_index =
11951 read_4_bytes (dbfd,
11952 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11953
73869dc2
DE
11954 if (dwp_file->version == 1)
11955 {
ed2dc618
SM
11956 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11957 dwp_file, unit_index,
73869dc2
DE
11958 comp_dir, signature,
11959 is_debug_types);
11960 }
11961 else
11962 {
ed2dc618
SM
11963 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11964 dwp_file, unit_index,
73869dc2
DE
11965 comp_dir, signature,
11966 is_debug_types);
11967 }
9a3c8263 11968 return (struct dwo_unit *) *slot;
80626a55
DE
11969 }
11970 if (signature_in_table == 0)
11971 return NULL;
11972 hash = (hash + hash2) & mask;
11973 }
11974
11975 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11976 " [in module %s]"),
11977 dwp_file->name);
11978}
11979
ab5088bf 11980/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11981 Open the file specified by FILE_NAME and hand it off to BFD for
11982 preliminary analysis. Return a newly initialized bfd *, which
11983 includes a canonicalized copy of FILE_NAME.
80626a55 11984 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11985 SEARCH_CWD is true if the current directory is to be searched.
11986 It will be searched before debug-file-directory.
13aaf454
DE
11987 If successful, the file is added to the bfd include table of the
11988 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11989 If unable to find/open the file, return NULL.
3019eac3
DE
11990 NOTE: This function is derived from symfile_bfd_open. */
11991
192b62ce 11992static gdb_bfd_ref_ptr
ed2dc618
SM
11993try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11994 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11995{
24b9144d 11996 int desc;
9c02c129
DE
11997 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11998 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11999 to debug_file_directory. */
e0cc99a6 12000 const char *search_path;
9c02c129
DE
12001 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12002
e0cc99a6 12003 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12004 if (search_cwd)
12005 {
12006 if (*debug_file_directory != '\0')
e0cc99a6
TT
12007 {
12008 search_path_holder.reset (concat (".", dirname_separator_string,
12009 debug_file_directory,
12010 (char *) NULL));
12011 search_path = search_path_holder.get ();
12012 }
6ac97d4c 12013 else
e0cc99a6 12014 search_path = ".";
6ac97d4c 12015 }
9c02c129 12016 else
e0cc99a6 12017 search_path = debug_file_directory;
3019eac3 12018
24b9144d 12019 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12020 if (is_dwp)
12021 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12022
12023 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12024 desc = openp (search_path, flags, file_name,
3019eac3
DE
12025 O_RDONLY | O_BINARY, &absolute_name);
12026 if (desc < 0)
12027 return NULL;
12028
e0cc99a6
TT
12029 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12030 gnutarget, desc));
9c02c129
DE
12031 if (sym_bfd == NULL)
12032 return NULL;
192b62ce 12033 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12034
192b62ce
TT
12035 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12036 return NULL;
3019eac3 12037
13aaf454
DE
12038 /* Success. Record the bfd as having been included by the objfile's bfd.
12039 This is important because things like demangled_names_hash lives in the
12040 objfile's per_bfd space and may have references to things like symbol
12041 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12042 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12043
3019eac3
DE
12044 return sym_bfd;
12045}
12046
ab5088bf 12047/* Try to open DWO file FILE_NAME.
3019eac3
DE
12048 COMP_DIR is the DW_AT_comp_dir attribute.
12049 The result is the bfd handle of the file.
12050 If there is a problem finding or opening the file, return NULL.
12051 Upon success, the canonicalized path of the file is stored in the bfd,
12052 same as symfile_bfd_open. */
12053
192b62ce 12054static gdb_bfd_ref_ptr
ed2dc618
SM
12055open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12056 const char *file_name, const char *comp_dir)
3019eac3 12057{
80626a55 12058 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12059 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12060 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12061
12062 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12063
12064 if (comp_dir != NULL)
12065 {
43816ebc
TT
12066 gdb::unique_xmalloc_ptr<char> path_to_try
12067 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12068
12069 /* NOTE: If comp_dir is a relative path, this will also try the
12070 search path, which seems useful. */
ed2dc618 12071 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12072 path_to_try.get (),
ed2dc618 12073 0 /*is_dwp*/,
192b62ce 12074 1 /*search_cwd*/));
3019eac3
DE
12075 if (abfd != NULL)
12076 return abfd;
12077 }
12078
12079 /* That didn't work, try debug-file-directory, which, despite its name,
12080 is a list of paths. */
12081
12082 if (*debug_file_directory == '\0')
12083 return NULL;
12084
ed2dc618
SM
12085 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12086 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12087}
12088
80626a55
DE
12089/* This function is mapped across the sections and remembers the offset and
12090 size of each of the DWO debugging sections we are interested in. */
12091
12092static void
12093dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12094{
9a3c8263 12095 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12096 const struct dwop_section_names *names = &dwop_section_names;
12097
12098 if (section_is_p (sectp->name, &names->abbrev_dwo))
12099 {
049412e3 12100 dwo_sections->abbrev.s.section = sectp;
fd361982 12101 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12102 }
12103 else if (section_is_p (sectp->name, &names->info_dwo))
12104 {
049412e3 12105 dwo_sections->info.s.section = sectp;
fd361982 12106 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12107 }
12108 else if (section_is_p (sectp->name, &names->line_dwo))
12109 {
049412e3 12110 dwo_sections->line.s.section = sectp;
fd361982 12111 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12112 }
12113 else if (section_is_p (sectp->name, &names->loc_dwo))
12114 {
049412e3 12115 dwo_sections->loc.s.section = sectp;
fd361982 12116 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12117 }
12118 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12119 {
049412e3 12120 dwo_sections->macinfo.s.section = sectp;
fd361982 12121 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12122 }
12123 else if (section_is_p (sectp->name, &names->macro_dwo))
12124 {
049412e3 12125 dwo_sections->macro.s.section = sectp;
fd361982 12126 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12127 }
12128 else if (section_is_p (sectp->name, &names->str_dwo))
12129 {
049412e3 12130 dwo_sections->str.s.section = sectp;
fd361982 12131 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12132 }
12133 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12134 {
049412e3 12135 dwo_sections->str_offsets.s.section = sectp;
fd361982 12136 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12137 }
12138 else if (section_is_p (sectp->name, &names->types_dwo))
12139 {
12140 struct dwarf2_section_info type_section;
12141
12142 memset (&type_section, 0, sizeof (type_section));
049412e3 12143 type_section.s.section = sectp;
fd361982 12144 type_section.size = bfd_section_size (sectp);
fd5866f6 12145 dwo_sections->types.push_back (type_section);
80626a55
DE
12146 }
12147}
12148
ab5088bf 12149/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12150 by PER_CU. This is for the non-DWP case.
80626a55 12151 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12152
12153static struct dwo_file *
0ac5b59e
DE
12154open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12155 const char *dwo_name, const char *comp_dir)
3019eac3 12156{
ed2dc618 12157 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12158
fb1eb2f9 12159 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12160 if (dbfd == NULL)
12161 {
b4f54984 12162 if (dwarf_read_debug)
80626a55
DE
12163 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12164 return NULL;
12165 }
263db9a1 12166
51ac9db5 12167 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12168 dwo_file->dwo_name = dwo_name;
12169 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12170 dwo_file->dbfd = std::move (dbfd);
3019eac3 12171
fb1eb2f9 12172 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12173 &dwo_file->sections);
3019eac3 12174
18a8505e
AT
12175 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12176 dwo_file->sections.info, dwo_file->cus);
3019eac3 12177
263db9a1 12178 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12179 dwo_file->sections.types, dwo_file->tus);
3019eac3 12180
b4f54984 12181 if (dwarf_read_debug)
80626a55
DE
12182 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12183
263db9a1 12184 return dwo_file.release ();
3019eac3
DE
12185}
12186
80626a55 12187/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12188 size of each of the DWP debugging sections common to version 1 and 2 that
12189 we are interested in. */
3019eac3 12190
80626a55 12191static void
73869dc2
DE
12192dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12193 void *dwp_file_ptr)
3019eac3 12194{
9a3c8263 12195 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12196 const struct dwop_section_names *names = &dwop_section_names;
12197 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12198
80626a55 12199 /* Record the ELF section number for later lookup: this is what the
73869dc2 12200 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12201 gdb_assert (elf_section_nr < dwp_file->num_sections);
12202 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12203
80626a55
DE
12204 /* Look for specific sections that we need. */
12205 if (section_is_p (sectp->name, &names->str_dwo))
12206 {
049412e3 12207 dwp_file->sections.str.s.section = sectp;
fd361982 12208 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12209 }
12210 else if (section_is_p (sectp->name, &names->cu_index))
12211 {
049412e3 12212 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12213 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12214 }
12215 else if (section_is_p (sectp->name, &names->tu_index))
12216 {
049412e3 12217 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12218 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12219 }
12220}
3019eac3 12221
73869dc2
DE
12222/* This function is mapped across the sections and remembers the offset and
12223 size of each of the DWP version 2 debugging sections that we are interested
12224 in. This is split into a separate function because we don't know if we
12225 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12226
12227static void
12228dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12229{
9a3c8263 12230 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12231 const struct dwop_section_names *names = &dwop_section_names;
12232 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12233
12234 /* Record the ELF section number for later lookup: this is what the
12235 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12236 gdb_assert (elf_section_nr < dwp_file->num_sections);
12237 dwp_file->elf_sections[elf_section_nr] = sectp;
12238
12239 /* Look for specific sections that we need. */
12240 if (section_is_p (sectp->name, &names->abbrev_dwo))
12241 {
049412e3 12242 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12243 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12244 }
12245 else if (section_is_p (sectp->name, &names->info_dwo))
12246 {
049412e3 12247 dwp_file->sections.info.s.section = sectp;
fd361982 12248 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12249 }
12250 else if (section_is_p (sectp->name, &names->line_dwo))
12251 {
049412e3 12252 dwp_file->sections.line.s.section = sectp;
fd361982 12253 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12254 }
12255 else if (section_is_p (sectp->name, &names->loc_dwo))
12256 {
049412e3 12257 dwp_file->sections.loc.s.section = sectp;
fd361982 12258 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12259 }
12260 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12261 {
049412e3 12262 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12263 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12264 }
12265 else if (section_is_p (sectp->name, &names->macro_dwo))
12266 {
049412e3 12267 dwp_file->sections.macro.s.section = sectp;
fd361982 12268 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12269 }
12270 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12271 {
049412e3 12272 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12273 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12274 }
12275 else if (section_is_p (sectp->name, &names->types_dwo))
12276 {
049412e3 12277 dwp_file->sections.types.s.section = sectp;
fd361982 12278 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12279 }
12280}
12281
80626a55 12282/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12283
80626a55
DE
12284static hashval_t
12285hash_dwp_loaded_cutus (const void *item)
12286{
9a3c8263 12287 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12288
80626a55
DE
12289 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12290 return dwo_unit->signature;
3019eac3
DE
12291}
12292
80626a55 12293/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12294
80626a55
DE
12295static int
12296eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12297{
9a3c8263
SM
12298 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12299 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12300
80626a55
DE
12301 return dua->signature == dub->signature;
12302}
3019eac3 12303
80626a55 12304/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12305
48b490f2 12306static htab_up
298e9637 12307allocate_dwp_loaded_cutus_table ()
80626a55 12308{
48b490f2
TT
12309 return htab_up (htab_create_alloc (3,
12310 hash_dwp_loaded_cutus,
12311 eq_dwp_loaded_cutus,
12312 NULL, xcalloc, xfree));
80626a55 12313}
3019eac3 12314
ab5088bf
DE
12315/* Try to open DWP file FILE_NAME.
12316 The result is the bfd handle of the file.
12317 If there is a problem finding or opening the file, return NULL.
12318 Upon success, the canonicalized path of the file is stored in the bfd,
12319 same as symfile_bfd_open. */
12320
192b62ce 12321static gdb_bfd_ref_ptr
ed2dc618
SM
12322open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12323 const char *file_name)
ab5088bf 12324{
ed2dc618
SM
12325 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12326 1 /*is_dwp*/,
192b62ce 12327 1 /*search_cwd*/));
6ac97d4c
DE
12328 if (abfd != NULL)
12329 return abfd;
12330
12331 /* Work around upstream bug 15652.
12332 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12333 [Whether that's a "bug" is debatable, but it is getting in our way.]
12334 We have no real idea where the dwp file is, because gdb's realpath-ing
12335 of the executable's path may have discarded the needed info.
12336 [IWBN if the dwp file name was recorded in the executable, akin to
12337 .gnu_debuglink, but that doesn't exist yet.]
12338 Strip the directory from FILE_NAME and search again. */
12339 if (*debug_file_directory != '\0')
12340 {
12341 /* Don't implicitly search the current directory here.
12342 If the user wants to search "." to handle this case,
12343 it must be added to debug-file-directory. */
ed2dc618
SM
12344 return try_open_dwop_file (dwarf2_per_objfile,
12345 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12346 0 /*search_cwd*/);
12347 }
12348
12349 return NULL;
ab5088bf
DE
12350}
12351
80626a55
DE
12352/* Initialize the use of the DWP file for the current objfile.
12353 By convention the name of the DWP file is ${objfile}.dwp.
12354 The result is NULL if it can't be found. */
a766d390 12355
400174b1 12356static std::unique_ptr<struct dwp_file>
ed2dc618 12357open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12358{
12359 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12360
82bf32bc
JK
12361 /* Try to find first .dwp for the binary file before any symbolic links
12362 resolving. */
6c447423
DE
12363
12364 /* If the objfile is a debug file, find the name of the real binary
12365 file and get the name of dwp file from there. */
d721ba37 12366 std::string dwp_name;
6c447423
DE
12367 if (objfile->separate_debug_objfile_backlink != NULL)
12368 {
12369 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12370 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12371
d721ba37 12372 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12373 }
12374 else
d721ba37
PA
12375 dwp_name = objfile->original_name;
12376
12377 dwp_name += ".dwp";
80626a55 12378
ed2dc618 12379 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12380 if (dbfd == NULL
12381 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12382 {
12383 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12384 dwp_name = objfile_name (objfile);
12385 dwp_name += ".dwp";
ed2dc618 12386 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12387 }
12388
80626a55
DE
12389 if (dbfd == NULL)
12390 {
b4f54984 12391 if (dwarf_read_debug)
d721ba37 12392 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12393 return std::unique_ptr<dwp_file> ();
3019eac3 12394 }
400174b1
TT
12395
12396 const char *name = bfd_get_filename (dbfd.get ());
12397 std::unique_ptr<struct dwp_file> dwp_file
12398 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12399
0a0f4c01 12400 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12401 dwp_file->elf_sections =
12402 OBSTACK_CALLOC (&objfile->objfile_obstack,
12403 dwp_file->num_sections, asection *);
12404
400174b1
TT
12405 bfd_map_over_sections (dwp_file->dbfd.get (),
12406 dwarf2_locate_common_dwp_sections,
12407 dwp_file.get ());
80626a55 12408
400174b1
TT
12409 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12410 0);
80626a55 12411
400174b1
TT
12412 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12413 1);
80626a55 12414
73869dc2 12415 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12416 if (dwp_file->cus && dwp_file->tus
12417 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12418 {
12419 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12420 pretty bizarre. We use pulongest here because that's the established
4d65956b 12421 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12422 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12423 " TU version %s [in DWP file %s]"),
12424 pulongest (dwp_file->cus->version),
d721ba37 12425 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12426 }
08302ed2
DE
12427
12428 if (dwp_file->cus)
12429 dwp_file->version = dwp_file->cus->version;
12430 else if (dwp_file->tus)
12431 dwp_file->version = dwp_file->tus->version;
12432 else
12433 dwp_file->version = 2;
73869dc2
DE
12434
12435 if (dwp_file->version == 2)
400174b1
TT
12436 bfd_map_over_sections (dwp_file->dbfd.get (),
12437 dwarf2_locate_v2_dwp_sections,
12438 dwp_file.get ());
73869dc2 12439
298e9637
SM
12440 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12441 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12442
b4f54984 12443 if (dwarf_read_debug)
80626a55
DE
12444 {
12445 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12446 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12447 " %s CUs, %s TUs\n",
12448 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12449 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12450 }
12451
12452 return dwp_file;
3019eac3 12453}
c906108c 12454
ab5088bf
DE
12455/* Wrapper around open_and_init_dwp_file, only open it once. */
12456
12457static struct dwp_file *
ed2dc618 12458get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12459{
12460 if (! dwarf2_per_objfile->dwp_checked)
12461 {
ed2dc618
SM
12462 dwarf2_per_objfile->dwp_file
12463 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12464 dwarf2_per_objfile->dwp_checked = 1;
12465 }
400174b1 12466 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12467}
12468
80626a55
DE
12469/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12470 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12471 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12472 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12473 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12474
12475 This is called, for example, when wanting to read a variable with a
12476 complex location. Therefore we don't want to do file i/o for every call.
12477 Therefore we don't want to look for a DWO file on every call.
12478 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12479 then we check if we've already seen DWO_NAME, and only THEN do we check
12480 for a DWO file.
12481
1c658ad5 12482 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12483 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12484
3019eac3 12485static struct dwo_unit *
80626a55
DE
12486lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12487 const char *dwo_name, const char *comp_dir,
12488 ULONGEST signature, int is_debug_types)
3019eac3 12489{
ed2dc618 12490 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12491 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12492 const char *kind = is_debug_types ? "TU" : "CU";
12493 void **dwo_file_slot;
3019eac3 12494 struct dwo_file *dwo_file;
80626a55 12495 struct dwp_file *dwp_file;
cb1df416 12496
6a506a2d
DE
12497 /* First see if there's a DWP file.
12498 If we have a DWP file but didn't find the DWO inside it, don't
12499 look for the original DWO file. It makes gdb behave differently
12500 depending on whether one is debugging in the build tree. */
cf2c3c16 12501
ed2dc618 12502 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12503 if (dwp_file != NULL)
cf2c3c16 12504 {
80626a55
DE
12505 const struct dwp_hash_table *dwp_htab =
12506 is_debug_types ? dwp_file->tus : dwp_file->cus;
12507
12508 if (dwp_htab != NULL)
12509 {
12510 struct dwo_unit *dwo_cutu =
ed2dc618 12511 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12512 signature, is_debug_types);
80626a55
DE
12513
12514 if (dwo_cutu != NULL)
12515 {
b4f54984 12516 if (dwarf_read_debug)
80626a55
DE
12517 {
12518 fprintf_unfiltered (gdb_stdlog,
12519 "Virtual DWO %s %s found: @%s\n",
12520 kind, hex_string (signature),
12521 host_address_to_string (dwo_cutu));
12522 }
12523 return dwo_cutu;
12524 }
12525 }
12526 }
6a506a2d 12527 else
80626a55 12528 {
6a506a2d 12529 /* No DWP file, look for the DWO file. */
80626a55 12530
ed2dc618
SM
12531 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12532 dwo_name, comp_dir);
6a506a2d 12533 if (*dwo_file_slot == NULL)
80626a55 12534 {
6a506a2d
DE
12535 /* Read in the file and build a table of the CUs/TUs it contains. */
12536 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12537 }
6a506a2d 12538 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12539 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12540
6a506a2d 12541 if (dwo_file != NULL)
19c3d4c9 12542 {
6a506a2d
DE
12543 struct dwo_unit *dwo_cutu = NULL;
12544
12545 if (is_debug_types && dwo_file->tus)
12546 {
12547 struct dwo_unit find_dwo_cutu;
12548
12549 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12550 find_dwo_cutu.signature = signature;
9a3c8263 12551 dwo_cutu
b0b6a987
TT
12552 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12553 &find_dwo_cutu);
6a506a2d 12554 }
33c5cd75 12555 else if (!is_debug_types && dwo_file->cus)
80626a55 12556 {
33c5cd75
DB
12557 struct dwo_unit find_dwo_cutu;
12558
12559 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12560 find_dwo_cutu.signature = signature;
b0b6a987 12561 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12562 &find_dwo_cutu);
6a506a2d
DE
12563 }
12564
12565 if (dwo_cutu != NULL)
12566 {
b4f54984 12567 if (dwarf_read_debug)
6a506a2d
DE
12568 {
12569 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12570 kind, dwo_name, hex_string (signature),
12571 host_address_to_string (dwo_cutu));
12572 }
12573 return dwo_cutu;
80626a55
DE
12574 }
12575 }
2e276125 12576 }
9cdd5dbd 12577
80626a55
DE
12578 /* We didn't find it. This could mean a dwo_id mismatch, or
12579 someone deleted the DWO/DWP file, or the search path isn't set up
12580 correctly to find the file. */
12581
b4f54984 12582 if (dwarf_read_debug)
80626a55
DE
12583 {
12584 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12585 kind, dwo_name, hex_string (signature));
12586 }
3019eac3 12587
6656a72d
DE
12588 /* This is a warning and not a complaint because it can be caused by
12589 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12590 {
12591 /* Print the name of the DWP file if we looked there, helps the user
12592 better diagnose the problem. */
791afaa2 12593 std::string dwp_text;
43942612
DE
12594
12595 if (dwp_file != NULL)
791afaa2
TT
12596 dwp_text = string_printf (" [in DWP file %s]",
12597 lbasename (dwp_file->name));
43942612 12598
9d8780f0 12599 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12600 " [in module %s]"),
12601 kind, dwo_name, hex_string (signature),
791afaa2 12602 dwp_text.c_str (),
43942612 12603 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12604 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12605 }
3019eac3 12606 return NULL;
5fb290d7
DJ
12607}
12608
80626a55
DE
12609/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12610 See lookup_dwo_cutu_unit for details. */
12611
12612static struct dwo_unit *
12613lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12614 const char *dwo_name, const char *comp_dir,
12615 ULONGEST signature)
12616{
12617 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12618}
12619
12620/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12621 See lookup_dwo_cutu_unit for details. */
12622
12623static struct dwo_unit *
12624lookup_dwo_type_unit (struct signatured_type *this_tu,
12625 const char *dwo_name, const char *comp_dir)
12626{
12627 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12628}
12629
89e63ee4
DE
12630/* Traversal function for queue_and_load_all_dwo_tus. */
12631
12632static int
12633queue_and_load_dwo_tu (void **slot, void *info)
12634{
12635 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12636 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12637 ULONGEST signature = dwo_unit->signature;
12638 struct signatured_type *sig_type =
12639 lookup_dwo_signatured_type (per_cu->cu, signature);
12640
12641 if (sig_type != NULL)
12642 {
12643 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12644
12645 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12646 a real dependency of PER_CU on SIG_TYPE. That is detected later
12647 while processing PER_CU. */
12648 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12649 load_full_type_unit (sig_cu);
ae640021 12650 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12651 }
12652
12653 return 1;
12654}
12655
12656/* Queue all TUs contained in the DWO of PER_CU to be read in.
12657 The DWO may have the only definition of the type, though it may not be
12658 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12659 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12660
12661static void
12662queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12663{
12664 struct dwo_unit *dwo_unit;
12665 struct dwo_file *dwo_file;
12666
12667 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12668 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12669 gdb_assert (per_cu->cu != NULL);
12670
12671 dwo_unit = per_cu->cu->dwo_unit;
12672 gdb_assert (dwo_unit != NULL);
12673
12674 dwo_file = dwo_unit->dwo_file;
12675 if (dwo_file->tus != NULL)
b0b6a987
TT
12676 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12677 per_cu);
89e63ee4
DE
12678}
12679
3019eac3 12680/* Read in various DIEs. */
348e048f 12681
d389af10 12682/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12683 Inherit only the children of the DW_AT_abstract_origin DIE not being
12684 already referenced by DW_AT_abstract_origin from the children of the
12685 current DIE. */
d389af10
JK
12686
12687static void
12688inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12689{
12690 struct die_info *child_die;
791afaa2 12691 sect_offset *offsetp;
d389af10
JK
12692 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12693 struct die_info *origin_die;
12694 /* Iterator of the ORIGIN_DIE children. */
12695 struct die_info *origin_child_die;
d389af10 12696 struct attribute *attr;
cd02d79d
PA
12697 struct dwarf2_cu *origin_cu;
12698 struct pending **origin_previous_list_in_scope;
d389af10
JK
12699
12700 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12701 if (!attr)
12702 return;
12703
cd02d79d
PA
12704 /* Note that following die references may follow to a die in a
12705 different cu. */
12706
12707 origin_cu = cu;
12708 origin_die = follow_die_ref (die, attr, &origin_cu);
12709
12710 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12711 symbols in. */
12712 origin_previous_list_in_scope = origin_cu->list_in_scope;
12713 origin_cu->list_in_scope = cu->list_in_scope;
12714
edb3359d
DJ
12715 if (die->tag != origin_die->tag
12716 && !(die->tag == DW_TAG_inlined_subroutine
12717 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12718 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12719 sect_offset_str (die->sect_off),
12720 sect_offset_str (origin_die->sect_off));
d389af10 12721
791afaa2 12722 std::vector<sect_offset> offsets;
d389af10 12723
3ea89b92
PMR
12724 for (child_die = die->child;
12725 child_die && child_die->tag;
12726 child_die = sibling_die (child_die))
12727 {
12728 struct die_info *child_origin_die;
12729 struct dwarf2_cu *child_origin_cu;
12730
12731 /* We are trying to process concrete instance entries:
216f72a1 12732 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12733 it's not relevant to our analysis here. i.e. detecting DIEs that are
12734 present in the abstract instance but not referenced in the concrete
12735 one. */
216f72a1
JK
12736 if (child_die->tag == DW_TAG_call_site
12737 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12738 continue;
12739
c38f313d
DJ
12740 /* For each CHILD_DIE, find the corresponding child of
12741 ORIGIN_DIE. If there is more than one layer of
12742 DW_AT_abstract_origin, follow them all; there shouldn't be,
12743 but GCC versions at least through 4.4 generate this (GCC PR
12744 40573). */
3ea89b92
PMR
12745 child_origin_die = child_die;
12746 child_origin_cu = cu;
c38f313d
DJ
12747 while (1)
12748 {
cd02d79d
PA
12749 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12750 child_origin_cu);
c38f313d
DJ
12751 if (attr == NULL)
12752 break;
cd02d79d
PA
12753 child_origin_die = follow_die_ref (child_origin_die, attr,
12754 &child_origin_cu);
c38f313d
DJ
12755 }
12756
d389af10
JK
12757 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12758 counterpart may exist. */
c38f313d 12759 if (child_origin_die != child_die)
d389af10 12760 {
edb3359d
DJ
12761 if (child_die->tag != child_origin_die->tag
12762 && !(child_die->tag == DW_TAG_inlined_subroutine
12763 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12764 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12765 "different tags"),
9d8780f0
SM
12766 sect_offset_str (child_die->sect_off),
12767 sect_offset_str (child_origin_die->sect_off));
c38f313d 12768 if (child_origin_die->parent != origin_die)
b98664d3 12769 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12770 "different parents"),
9d8780f0
SM
12771 sect_offset_str (child_die->sect_off),
12772 sect_offset_str (child_origin_die->sect_off));
c38f313d 12773 else
791afaa2 12774 offsets.push_back (child_origin_die->sect_off);
d389af10 12775 }
d389af10 12776 }
791afaa2
TT
12777 std::sort (offsets.begin (), offsets.end ());
12778 sect_offset *offsets_end = offsets.data () + offsets.size ();
12779 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12780 if (offsetp[-1] == *offsetp)
b98664d3 12781 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12782 "to DIE %s as their abstract origin"),
12783 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12784
791afaa2 12785 offsetp = offsets.data ();
d389af10
JK
12786 origin_child_die = origin_die->child;
12787 while (origin_child_die && origin_child_die->tag)
12788 {
12789 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12790 while (offsetp < offsets_end
9c541725 12791 && *offsetp < origin_child_die->sect_off)
d389af10 12792 offsetp++;
b64f50a1 12793 if (offsetp >= offsets_end
9c541725 12794 || *offsetp > origin_child_die->sect_off)
d389af10 12795 {
adde2bff
DE
12796 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12797 Check whether we're already processing ORIGIN_CHILD_DIE.
12798 This can happen with mutually referenced abstract_origins.
12799 PR 16581. */
12800 if (!origin_child_die->in_process)
12801 process_die (origin_child_die, origin_cu);
d389af10
JK
12802 }
12803 origin_child_die = sibling_die (origin_child_die);
12804 }
cd02d79d 12805 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12806
12807 if (cu != origin_cu)
12808 compute_delayed_physnames (origin_cu);
d389af10
JK
12809}
12810
c906108c 12811static void
e7c27a73 12812read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12813{
518817b3 12814 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12815 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12816 struct context_stack *newobj;
c906108c
SS
12817 CORE_ADDR lowpc;
12818 CORE_ADDR highpc;
12819 struct die_info *child_die;
edb3359d 12820 struct attribute *attr, *call_line, *call_file;
15d034d0 12821 const char *name;
e142c38c 12822 CORE_ADDR baseaddr;
801e3a5b 12823 struct block *block;
edb3359d 12824 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12825 std::vector<struct symbol *> template_args;
34eaf542 12826 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12827
12828 if (inlined_func)
12829 {
12830 /* If we do not have call site information, we can't show the
12831 caller of this inlined function. That's too confusing, so
12832 only use the scope for local variables. */
12833 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12834 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12835 if (call_line == NULL || call_file == NULL)
12836 {
12837 read_lexical_block_scope (die, cu);
12838 return;
12839 }
12840 }
c906108c 12841
b3b3bada 12842 baseaddr = objfile->text_section_offset ();
e142c38c 12843
94af9270 12844 name = dwarf2_name (die, cu);
c906108c 12845
e8d05480
JB
12846 /* Ignore functions with missing or empty names. These are actually
12847 illegal according to the DWARF standard. */
12848 if (name == NULL)
12849 {
b98664d3 12850 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12851 sect_offset_str (die->sect_off));
e8d05480
JB
12852 return;
12853 }
12854
12855 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12856 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12857 <= PC_BOUNDS_INVALID)
e8d05480 12858 {
ae4d0c03
PM
12859 attr = dwarf2_attr (die, DW_AT_external, cu);
12860 if (!attr || !DW_UNSND (attr))
b98664d3 12861 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12862 "for subprogram DIE at %s"),
12863 sect_offset_str (die->sect_off));
e8d05480
JB
12864 return;
12865 }
c906108c 12866
3e29f34a
MR
12867 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12868 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12869
34eaf542
TT
12870 /* If we have any template arguments, then we must allocate a
12871 different sort of symbol. */
12872 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12873 {
12874 if (child_die->tag == DW_TAG_template_type_param
12875 || child_die->tag == DW_TAG_template_value_param)
12876 {
e623cf5d 12877 templ_func = allocate_template_symbol (objfile);
cf724bc9 12878 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12879 break;
12880 }
12881 }
12882
c24bdb02 12883 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12884 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12885 (struct symbol *) templ_func);
4c2df51b 12886
81873cc8 12887 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12888 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12889 cu->language);
12890
4cecd739
DJ
12891 /* If there is a location expression for DW_AT_frame_base, record
12892 it. */
e142c38c 12893 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12894 if (attr != nullptr)
fe978cb0 12895 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12896
63e43d3a
PMR
12897 /* If there is a location for the static link, record it. */
12898 newobj->static_link = NULL;
12899 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12900 if (attr != nullptr)
63e43d3a 12901 {
224c3ddb
SM
12902 newobj->static_link
12903 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12904 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12905 cu->per_cu->addr_type ());
63e43d3a
PMR
12906 }
12907
c24bdb02 12908 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12909
639d11d3 12910 if (die->child != NULL)
c906108c 12911 {
639d11d3 12912 child_die = die->child;
c906108c
SS
12913 while (child_die && child_die->tag)
12914 {
34eaf542
TT
12915 if (child_die->tag == DW_TAG_template_type_param
12916 || child_die->tag == DW_TAG_template_value_param)
12917 {
12918 struct symbol *arg = new_symbol (child_die, NULL, cu);
12919
f1078f66 12920 if (arg != NULL)
2f4732b0 12921 template_args.push_back (arg);
34eaf542
TT
12922 }
12923 else
12924 process_die (child_die, cu);
c906108c
SS
12925 child_die = sibling_die (child_die);
12926 }
12927 }
12928
d389af10
JK
12929 inherit_abstract_dies (die, cu);
12930
4a811a97
UW
12931 /* If we have a DW_AT_specification, we might need to import using
12932 directives from the context of the specification DIE. See the
12933 comment in determine_prefix. */
12934 if (cu->language == language_cplus
12935 && dwarf2_attr (die, DW_AT_specification, cu))
12936 {
12937 struct dwarf2_cu *spec_cu = cu;
12938 struct die_info *spec_die = die_specification (die, &spec_cu);
12939
12940 while (spec_die)
12941 {
12942 child_die = spec_die->child;
12943 while (child_die && child_die->tag)
12944 {
12945 if (child_die->tag == DW_TAG_imported_module)
12946 process_die (child_die, spec_cu);
12947 child_die = sibling_die (child_die);
12948 }
12949
12950 /* In some cases, GCC generates specification DIEs that
12951 themselves contain DW_AT_specification attributes. */
12952 spec_die = die_specification (spec_die, &spec_cu);
12953 }
12954 }
12955
c24bdb02 12956 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12957 /* Make a block for the local symbols within. */
c24bdb02 12958 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12959 cstk.static_link, lowpc, highpc);
801e3a5b 12960
df8a16a1 12961 /* For C++, set the block's scope. */
45280282
IB
12962 if ((cu->language == language_cplus
12963 || cu->language == language_fortran
c44af4eb
TT
12964 || cu->language == language_d
12965 || cu->language == language_rust)
4d4ec4e5 12966 && cu->processing_has_namespace_info)
195a3f6c
TT
12967 block_set_scope (block, determine_prefix (die, cu),
12968 &objfile->objfile_obstack);
df8a16a1 12969
801e3a5b
JB
12970 /* If we have address ranges, record them. */
12971 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12972
a60f3166 12973 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12974
34eaf542 12975 /* Attach template arguments to function. */
2f4732b0 12976 if (!template_args.empty ())
34eaf542
TT
12977 {
12978 gdb_assert (templ_func != NULL);
12979
2f4732b0 12980 templ_func->n_template_arguments = template_args.size ();
34eaf542 12981 templ_func->template_arguments
8d749320
SM
12982 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12983 templ_func->n_template_arguments);
34eaf542 12984 memcpy (templ_func->template_arguments,
2f4732b0 12985 template_args.data (),
34eaf542 12986 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
12987
12988 /* Make sure that the symtab is set on the new symbols. Even
12989 though they don't appear in this symtab directly, other parts
12990 of gdb assume that symbols do, and this is reasonably
12991 true. */
8634679f 12992 for (symbol *sym : template_args)
3e1d3d8c 12993 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
12994 }
12995
208d8187
JB
12996 /* In C++, we can have functions nested inside functions (e.g., when
12997 a function declares a class that has methods). This means that
12998 when we finish processing a function scope, we may need to go
12999 back to building a containing block's symbol lists. */
c24bdb02
KS
13000 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13001 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13002
921e78cf
JB
13003 /* If we've finished processing a top-level function, subsequent
13004 symbols go in the file symbol list. */
c24bdb02
KS
13005 if (cu->get_builder ()->outermost_context_p ())
13006 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13007}
13008
13009/* Process all the DIES contained within a lexical block scope. Start
13010 a new scope, process the dies, and then close the scope. */
13011
13012static void
e7c27a73 13013read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13014{
518817b3 13015 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13016 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13017 CORE_ADDR lowpc, highpc;
13018 struct die_info *child_die;
e142c38c
DJ
13019 CORE_ADDR baseaddr;
13020
b3b3bada 13021 baseaddr = objfile->text_section_offset ();
c906108c
SS
13022
13023 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13024 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13025 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13026 be nasty. Might be easier to properly extend generic blocks to
af34e669 13027 describe ranges. */
e385593e
JK
13028 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13029 {
13030 case PC_BOUNDS_NOT_PRESENT:
13031 /* DW_TAG_lexical_block has no attributes, process its children as if
13032 there was no wrapping by that DW_TAG_lexical_block.
13033 GCC does no longer produces such DWARF since GCC r224161. */
13034 for (child_die = die->child;
13035 child_die != NULL && child_die->tag;
13036 child_die = sibling_die (child_die))
13037 process_die (child_die, cu);
13038 return;
13039 case PC_BOUNDS_INVALID:
13040 return;
13041 }
3e29f34a
MR
13042 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13043 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13044
c24bdb02 13045 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13046 if (die->child != NULL)
c906108c 13047 {
639d11d3 13048 child_die = die->child;
c906108c
SS
13049 while (child_die && child_die->tag)
13050 {
e7c27a73 13051 process_die (child_die, cu);
c906108c
SS
13052 child_die = sibling_die (child_die);
13053 }
13054 }
3ea89b92 13055 inherit_abstract_dies (die, cu);
c24bdb02 13056 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13057
c24bdb02
KS
13058 if (*cu->get_builder ()->get_local_symbols () != NULL
13059 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13060 {
801e3a5b 13061 struct block *block
c24bdb02 13062 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13063 cstk.start_addr, highpc);
801e3a5b
JB
13064
13065 /* Note that recording ranges after traversing children, as we
13066 do here, means that recording a parent's ranges entails
13067 walking across all its children's ranges as they appear in
13068 the address map, which is quadratic behavior.
13069
13070 It would be nicer to record the parent's ranges before
13071 traversing its children, simply overriding whatever you find
13072 there. But since we don't even decide whether to create a
13073 block until after we've traversed its children, that's hard
13074 to do. */
13075 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13076 }
c24bdb02
KS
13077 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13078 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13079}
13080
216f72a1 13081/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13082
13083static void
13084read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13085{
518817b3 13086 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13087 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13088 CORE_ADDR pc, baseaddr;
13089 struct attribute *attr;
13090 struct call_site *call_site, call_site_local;
13091 void **slot;
13092 int nparams;
13093 struct die_info *child_die;
13094
b3b3bada 13095 baseaddr = objfile->text_section_offset ();
96408a79 13096
216f72a1
JK
13097 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13098 if (attr == NULL)
13099 {
13100 /* This was a pre-DWARF-5 GNU extension alias
13101 for DW_AT_call_return_pc. */
13102 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13103 }
96408a79
SA
13104 if (!attr)
13105 {
b98664d3 13106 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13107 "DIE %s [in module %s]"),
13108 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13109 return;
13110 }
cd6c91b4 13111 pc = attr->value_as_address () + baseaddr;
3e29f34a 13112 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13113
13114 if (cu->call_site_htab == NULL)
13115 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13116 NULL, &objfile->objfile_obstack,
13117 hashtab_obstack_allocate, NULL);
13118 call_site_local.pc = pc;
13119 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13120 if (*slot != NULL)
13121 {
b98664d3 13122 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13123 "DIE %s [in module %s]"),
13124 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13125 objfile_name (objfile));
96408a79
SA
13126 return;
13127 }
13128
13129 /* Count parameters at the caller. */
13130
13131 nparams = 0;
13132 for (child_die = die->child; child_die && child_die->tag;
13133 child_die = sibling_die (child_die))
13134 {
216f72a1
JK
13135 if (child_die->tag != DW_TAG_call_site_parameter
13136 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13137 {
b98664d3 13138 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13139 "DW_TAG_call_site child DIE %s [in module %s]"),
13140 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13141 objfile_name (objfile));
96408a79
SA
13142 continue;
13143 }
13144
13145 nparams++;
13146 }
13147
224c3ddb
SM
13148 call_site
13149 = ((struct call_site *)
13150 obstack_alloc (&objfile->objfile_obstack,
13151 sizeof (*call_site)
13152 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13153 *slot = call_site;
13154 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13155 call_site->pc = pc;
13156
216f72a1
JK
13157 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13158 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13159 {
13160 struct die_info *func_die;
13161
13162 /* Skip also over DW_TAG_inlined_subroutine. */
13163 for (func_die = die->parent;
13164 func_die && func_die->tag != DW_TAG_subprogram
13165 && func_die->tag != DW_TAG_subroutine_type;
13166 func_die = func_die->parent);
13167
216f72a1
JK
13168 /* DW_AT_call_all_calls is a superset
13169 of DW_AT_call_all_tail_calls. */
96408a79 13170 if (func_die
216f72a1 13171 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13172 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13173 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13174 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13175 {
13176 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13177 not complete. But keep CALL_SITE for look ups via call_site_htab,
13178 both the initial caller containing the real return address PC and
13179 the final callee containing the current PC of a chain of tail
13180 calls do not need to have the tail call list complete. But any
13181 function candidate for a virtual tail call frame searched via
13182 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13183 determined unambiguously. */
13184 }
13185 else
13186 {
13187 struct type *func_type = NULL;
13188
13189 if (func_die)
13190 func_type = get_die_type (func_die, cu);
13191 if (func_type != NULL)
13192 {
13193 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13194
13195 /* Enlist this call site to the function. */
13196 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13197 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13198 }
13199 else
b98664d3 13200 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13201 "DIE %s [in module %s]"),
13202 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13203 }
13204 }
13205
216f72a1
JK
13206 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13207 if (attr == NULL)
13208 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13209 if (attr == NULL)
13210 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13211 if (attr == NULL)
216f72a1
JK
13212 {
13213 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13214 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13215 }
96408a79 13216 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13217 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13218 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13219 else if (attr->form_is_block ())
96408a79
SA
13220 {
13221 struct dwarf2_locexpr_baton *dlbaton;
13222
8d749320 13223 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13224 dlbaton->data = DW_BLOCK (attr)->data;
13225 dlbaton->size = DW_BLOCK (attr)->size;
13226 dlbaton->per_cu = cu->per_cu;
13227
13228 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13229 }
cd6c91b4 13230 else if (attr->form_is_ref ())
96408a79 13231 {
96408a79
SA
13232 struct dwarf2_cu *target_cu = cu;
13233 struct die_info *target_die;
13234
ac9ec31b 13235 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13236 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13237 if (die_is_declaration (target_die, target_cu))
13238 {
7d45c7c3 13239 const char *target_physname;
9112db09
JK
13240
13241 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13242 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13243 if (target_physname == NULL)
9112db09 13244 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13245 if (target_physname == NULL)
b98664d3 13246 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13247 "physname, for referencing DIE %s [in module %s]"),
13248 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13249 else
7d455152 13250 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13251 }
13252 else
13253 {
13254 CORE_ADDR lowpc;
13255
13256 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13257 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13258 <= PC_BOUNDS_INVALID)
b98664d3 13259 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13260 "low pc, for referencing DIE %s [in module %s]"),
13261 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13262 else
3e29f34a
MR
13263 {
13264 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13265 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13266 }
96408a79
SA
13267 }
13268 }
13269 else
b98664d3 13270 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13271 "block nor reference, for DIE %s [in module %s]"),
13272 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13273
13274 call_site->per_cu = cu->per_cu;
13275
13276 for (child_die = die->child;
13277 child_die && child_die->tag;
13278 child_die = sibling_die (child_die))
13279 {
96408a79 13280 struct call_site_parameter *parameter;
1788b2d3 13281 struct attribute *loc, *origin;
96408a79 13282
216f72a1
JK
13283 if (child_die->tag != DW_TAG_call_site_parameter
13284 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13285 {
13286 /* Already printed the complaint above. */
13287 continue;
13288 }
13289
13290 gdb_assert (call_site->parameter_count < nparams);
13291 parameter = &call_site->parameter[call_site->parameter_count];
13292
1788b2d3
JK
13293 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13294 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13295 register is contained in DW_AT_call_value. */
96408a79 13296
24c5c679 13297 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13298 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13299 if (origin == NULL)
13300 {
13301 /* This was a pre-DWARF-5 GNU extension alias
13302 for DW_AT_call_parameter. */
13303 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13304 }
cd6c91b4 13305 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13306 {
1788b2d3 13307 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13308
13309 sect_offset sect_off
13310 = (sect_offset) dwarf2_get_ref_die_offset (origin);
4057dfde 13311 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13312 {
13313 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13314 binding can be done only inside one CU. Such referenced DIE
13315 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13316 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13317 "DW_TAG_call_site child DIE %s [in module %s]"),
13318 sect_offset_str (child_die->sect_off),
9c541725 13319 objfile_name (objfile));
d76b7dbc
JK
13320 continue;
13321 }
9c541725
PA
13322 parameter->u.param_cu_off
13323 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13324 }
4fc6c0d5 13325 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13326 {
b98664d3 13327 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13328 "DW_TAG_call_site child DIE %s [in module %s]"),
13329 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13330 continue;
13331 }
24c5c679 13332 else
96408a79 13333 {
24c5c679
JK
13334 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13335 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13336 if (parameter->u.dwarf_reg != -1)
13337 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13338 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13339 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13340 &parameter->u.fb_offset))
13341 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13342 else
13343 {
b98664d3 13344 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13345 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13346 "DW_TAG_call_site child DIE %s "
24c5c679 13347 "[in module %s]"),
9d8780f0 13348 sect_offset_str (child_die->sect_off),
9c541725 13349 objfile_name (objfile));
24c5c679
JK
13350 continue;
13351 }
96408a79
SA
13352 }
13353
216f72a1
JK
13354 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13355 if (attr == NULL)
13356 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13357 if (attr == NULL || !attr->form_is_block ())
96408a79 13358 {
b98664d3 13359 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13360 "DW_TAG_call_site child DIE %s [in module %s]"),
13361 sect_offset_str (child_die->sect_off),
9c541725 13362 objfile_name (objfile));
96408a79
SA
13363 continue;
13364 }
13365 parameter->value = DW_BLOCK (attr)->data;
13366 parameter->value_size = DW_BLOCK (attr)->size;
13367
13368 /* Parameters are not pre-cleared by memset above. */
13369 parameter->data_value = NULL;
13370 parameter->data_value_size = 0;
13371 call_site->parameter_count++;
13372
216f72a1
JK
13373 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13374 if (attr == NULL)
13375 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13376 if (attr != nullptr)
96408a79 13377 {
4fc6c0d5 13378 if (!attr->form_is_block ())
b98664d3 13379 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13380 "DW_TAG_call_site child DIE %s [in module %s]"),
13381 sect_offset_str (child_die->sect_off),
9c541725 13382 objfile_name (objfile));
96408a79
SA
13383 else
13384 {
13385 parameter->data_value = DW_BLOCK (attr)->data;
13386 parameter->data_value_size = DW_BLOCK (attr)->size;
13387 }
13388 }
13389 }
13390}
13391
71a3c369
TT
13392/* Helper function for read_variable. If DIE represents a virtual
13393 table, then return the type of the concrete object that is
13394 associated with the virtual table. Otherwise, return NULL. */
13395
13396static struct type *
13397rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13398{
13399 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13400 if (attr == NULL)
13401 return NULL;
13402
13403 /* Find the type DIE. */
13404 struct die_info *type_die = NULL;
13405 struct dwarf2_cu *type_cu = cu;
13406
cd6c91b4 13407 if (attr->form_is_ref ())
71a3c369
TT
13408 type_die = follow_die_ref (die, attr, &type_cu);
13409 if (type_die == NULL)
13410 return NULL;
13411
13412 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13413 return NULL;
13414 return die_containing_type (type_die, type_cu);
13415}
13416
13417/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13418
13419static void
13420read_variable (struct die_info *die, struct dwarf2_cu *cu)
13421{
13422 struct rust_vtable_symbol *storage = NULL;
13423
13424 if (cu->language == language_rust)
13425 {
13426 struct type *containing_type = rust_containing_type (die, cu);
13427
13428 if (containing_type != NULL)
13429 {
518817b3 13430 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13431
468c0cbb 13432 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13433 initialize_objfile_symbol (storage);
13434 storage->concrete_type = containing_type;
cf724bc9 13435 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13436 }
13437 }
13438
e4a62c65
TV
13439 struct symbol *res = new_symbol (die, NULL, cu, storage);
13440 struct attribute *abstract_origin
13441 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13442 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13443 if (res == NULL && loc && abstract_origin)
13444 {
13445 /* We have a variable without a name, but with a location and an abstract
13446 origin. This may be a concrete instance of an abstract variable
13447 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13448 later. */
13449 struct dwarf2_cu *origin_cu = cu;
13450 struct die_info *origin_die
13451 = follow_die_ref (die, abstract_origin, &origin_cu);
13452 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13453 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13454 }
71a3c369
TT
13455}
13456
43988095
JK
13457/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13458 reading .debug_rnglists.
13459 Callback's type should be:
13460 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13461 Return true if the attributes are present and valid, otherwise,
13462 return false. */
13463
13464template <typename Callback>
13465static bool
13466dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13467 Callback &&callback)
13468{
ed2dc618 13469 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13470 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13471 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13472 bfd *obfd = objfile->obfd;
43988095
JK
13473 /* Base address selection entry. */
13474 CORE_ADDR base;
13475 int found_base;
43988095 13476 const gdb_byte *buffer;
43988095
JK
13477 CORE_ADDR baseaddr;
13478 bool overflow = false;
13479
13480 found_base = cu->base_known;
13481 base = cu->base_address;
13482
96b79293 13483 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13484 if (offset >= dwarf2_per_objfile->rnglists.size)
13485 {
b98664d3 13486 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13487 offset);
13488 return false;
13489 }
13490 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13491
b3b3bada 13492 baseaddr = objfile->text_section_offset ();
43988095
JK
13493
13494 while (1)
13495 {
7814882a
JK
13496 /* Initialize it due to a false compiler warning. */
13497 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13498 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13499 + dwarf2_per_objfile->rnglists.size);
13500 unsigned int bytes_read;
13501
13502 if (buffer == buf_end)
13503 {
13504 overflow = true;
13505 break;
13506 }
13507 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13508 switch (rlet)
13509 {
13510 case DW_RLE_end_of_list:
13511 break;
13512 case DW_RLE_base_address:
13513 if (buffer + cu->header.addr_size > buf_end)
13514 {
13515 overflow = true;
13516 break;
13517 }
c8a7a66f 13518 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13519 found_base = 1;
13520 buffer += bytes_read;
13521 break;
13522 case DW_RLE_start_length:
13523 if (buffer + cu->header.addr_size > buf_end)
13524 {
13525 overflow = true;
13526 break;
13527 }
c8a7a66f
TT
13528 range_beginning = cu->header.read_address (obfd, buffer,
13529 &bytes_read);
43988095
JK
13530 buffer += bytes_read;
13531 range_end = (range_beginning
13532 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13533 buffer += bytes_read;
13534 if (buffer > buf_end)
13535 {
13536 overflow = true;
13537 break;
13538 }
13539 break;
13540 case DW_RLE_offset_pair:
13541 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13542 buffer += bytes_read;
13543 if (buffer > buf_end)
13544 {
13545 overflow = true;
13546 break;
13547 }
13548 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13549 buffer += bytes_read;
13550 if (buffer > buf_end)
13551 {
13552 overflow = true;
13553 break;
13554 }
13555 break;
13556 case DW_RLE_start_end:
13557 if (buffer + 2 * cu->header.addr_size > buf_end)
13558 {
13559 overflow = true;
13560 break;
13561 }
c8a7a66f
TT
13562 range_beginning = cu->header.read_address (obfd, buffer,
13563 &bytes_read);
43988095 13564 buffer += bytes_read;
c8a7a66f 13565 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13566 buffer += bytes_read;
13567 break;
13568 default:
b98664d3 13569 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13570 return false;
13571 }
13572 if (rlet == DW_RLE_end_of_list || overflow)
13573 break;
13574 if (rlet == DW_RLE_base_address)
13575 continue;
13576
13577 if (!found_base)
13578 {
13579 /* We have no valid base address for the ranges
13580 data. */
b98664d3 13581 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13582 return false;
13583 }
13584
13585 if (range_beginning > range_end)
13586 {
13587 /* Inverted range entries are invalid. */
b98664d3 13588 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13589 return false;
13590 }
13591
13592 /* Empty range entries have no effect. */
13593 if (range_beginning == range_end)
13594 continue;
13595
13596 range_beginning += base;
13597 range_end += base;
13598
13599 /* A not-uncommon case of bad debug info.
13600 Don't pollute the addrmap with bad data. */
13601 if (range_beginning + baseaddr == 0
13602 && !dwarf2_per_objfile->has_section_at_zero)
13603 {
b98664d3 13604 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13605 " [in module %s]"), objfile_name (objfile));
13606 continue;
13607 }
13608
13609 callback (range_beginning, range_end);
13610 }
13611
13612 if (overflow)
13613 {
b98664d3 13614 complaint (_("Offset %d is not terminated "
43988095
JK
13615 "for DW_AT_ranges attribute"),
13616 offset);
13617 return false;
13618 }
13619
13620 return true;
13621}
13622
13623/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13624 Callback's type should be:
13625 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13626 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13627
43988095 13628template <typename Callback>
43039443 13629static int
5f46c5a5 13630dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13631 Callback &&callback)
43039443 13632{
ed2dc618 13633 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13634 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13635 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13636 struct comp_unit_head *cu_header = &cu->header;
13637 bfd *obfd = objfile->obfd;
13638 unsigned int addr_size = cu_header->addr_size;
13639 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13640 /* Base address selection entry. */
13641 CORE_ADDR base;
13642 int found_base;
13643 unsigned int dummy;
d521ce57 13644 const gdb_byte *buffer;
ff013f42 13645 CORE_ADDR baseaddr;
43039443 13646
43988095
JK
13647 if (cu_header->version >= 5)
13648 return dwarf2_rnglists_process (offset, cu, callback);
13649
d00adf39
DE
13650 found_base = cu->base_known;
13651 base = cu->base_address;
43039443 13652
96b79293 13653 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13654 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13655 {
b98664d3 13656 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13657 offset);
13658 return 0;
13659 }
dce234bc 13660 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13661
b3b3bada 13662 baseaddr = objfile->text_section_offset ();
ff013f42 13663
43039443
JK
13664 while (1)
13665 {
13666 CORE_ADDR range_beginning, range_end;
13667
c8a7a66f 13668 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13669 buffer += addr_size;
c8a7a66f 13670 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13671 buffer += addr_size;
13672 offset += 2 * addr_size;
13673
13674 /* An end of list marker is a pair of zero addresses. */
13675 if (range_beginning == 0 && range_end == 0)
13676 /* Found the end of list entry. */
13677 break;
13678
13679 /* Each base address selection entry is a pair of 2 values.
13680 The first is the largest possible address, the second is
13681 the base address. Check for a base address here. */
13682 if ((range_beginning & mask) == mask)
13683 {
28d2bfb9
AB
13684 /* If we found the largest possible address, then we already
13685 have the base address in range_end. */
13686 base = range_end;
43039443
JK
13687 found_base = 1;
13688 continue;
13689 }
13690
13691 if (!found_base)
13692 {
13693 /* We have no valid base address for the ranges
13694 data. */
b98664d3 13695 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13696 return 0;
13697 }
13698
9277c30c
UW
13699 if (range_beginning > range_end)
13700 {
13701 /* Inverted range entries are invalid. */
b98664d3 13702 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13703 return 0;
13704 }
13705
13706 /* Empty range entries have no effect. */
13707 if (range_beginning == range_end)
13708 continue;
13709
43039443
JK
13710 range_beginning += base;
13711 range_end += base;
13712
01093045
DE
13713 /* A not-uncommon case of bad debug info.
13714 Don't pollute the addrmap with bad data. */
13715 if (range_beginning + baseaddr == 0
13716 && !dwarf2_per_objfile->has_section_at_zero)
13717 {
b98664d3 13718 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13719 " [in module %s]"), objfile_name (objfile));
01093045
DE
13720 continue;
13721 }
13722
5f46c5a5
JK
13723 callback (range_beginning, range_end);
13724 }
13725
13726 return 1;
13727}
13728
13729/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13730 Return 1 if the attributes are present and valid, otherwise, return 0.
13731 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13732
13733static int
13734dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13735 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13736 dwarf2_psymtab *ranges_pst)
5f46c5a5 13737{
518817b3 13738 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13739 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13740 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13741 int low_set = 0;
13742 CORE_ADDR low = 0;
13743 CORE_ADDR high = 0;
13744 int retval;
13745
13746 retval = dwarf2_ranges_process (offset, cu,
13747 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13748 {
9277c30c 13749 if (ranges_pst != NULL)
3e29f34a
MR
13750 {
13751 CORE_ADDR lowpc;
13752 CORE_ADDR highpc;
13753
79748972
TT
13754 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13755 range_beginning + baseaddr)
13756 - baseaddr);
13757 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13758 range_end + baseaddr)
13759 - baseaddr);
d320c2b5
TT
13760 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13761 lowpc, highpc - 1, ranges_pst);
3e29f34a 13762 }
ff013f42 13763
43039443
JK
13764 /* FIXME: This is recording everything as a low-high
13765 segment of consecutive addresses. We should have a
13766 data structure for discontiguous block ranges
13767 instead. */
13768 if (! low_set)
13769 {
13770 low = range_beginning;
13771 high = range_end;
13772 low_set = 1;
13773 }
13774 else
13775 {
13776 if (range_beginning < low)
13777 low = range_beginning;
13778 if (range_end > high)
13779 high = range_end;
13780 }
5f46c5a5
JK
13781 });
13782 if (!retval)
13783 return 0;
43039443
JK
13784
13785 if (! low_set)
13786 /* If the first entry is an end-of-list marker, the range
13787 describes an empty scope, i.e. no instructions. */
13788 return 0;
13789
13790 if (low_return)
13791 *low_return = low;
13792 if (high_return)
13793 *high_return = high;
13794 return 1;
13795}
13796
3a2b436a
JK
13797/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13798 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13799 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13800
3a2b436a 13801static enum pc_bounds_kind
af34e669 13802dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13803 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13804 dwarf2_psymtab *pst)
c906108c 13805{
518817b3
SM
13806 struct dwarf2_per_objfile *dwarf2_per_objfile
13807 = cu->per_cu->dwarf2_per_objfile;
c906108c 13808 struct attribute *attr;
91da1414 13809 struct attribute *attr_high;
af34e669
DJ
13810 CORE_ADDR low = 0;
13811 CORE_ADDR high = 0;
e385593e 13812 enum pc_bounds_kind ret;
c906108c 13813
91da1414
MW
13814 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13815 if (attr_high)
af34e669 13816 {
e142c38c 13817 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13818 if (attr != nullptr)
91da1414 13819 {
cd6c91b4
TT
13820 low = attr->value_as_address ();
13821 high = attr_high->value_as_address ();
13822 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13823 high += low;
91da1414 13824 }
af34e669
DJ
13825 else
13826 /* Found high w/o low attribute. */
e385593e 13827 return PC_BOUNDS_INVALID;
af34e669
DJ
13828
13829 /* Found consecutive range of addresses. */
3a2b436a 13830 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13831 }
c906108c 13832 else
af34e669 13833 {
e142c38c 13834 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13835 if (attr != NULL)
13836 {
18a8505e 13837 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13838 We take advantage of the fact that DW_AT_ranges does not appear
13839 in DW_TAG_compile_unit of DWO files. */
13840 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13841 unsigned int ranges_offset = (DW_UNSND (attr)
13842 + (need_ranges_base
13843 ? cu->ranges_base
13844 : 0));
2e3cf129 13845
af34e669 13846 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13847 .debug_ranges section. */
2e3cf129 13848 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13849 return PC_BOUNDS_INVALID;
43039443 13850 /* Found discontinuous range of addresses. */
3a2b436a 13851 ret = PC_BOUNDS_RANGES;
af34e669 13852 }
e385593e
JK
13853 else
13854 return PC_BOUNDS_NOT_PRESENT;
af34e669 13855 }
c906108c 13856
48fbe735 13857 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13858 if (high <= low)
e385593e 13859 return PC_BOUNDS_INVALID;
c906108c
SS
13860
13861 /* When using the GNU linker, .gnu.linkonce. sections are used to
13862 eliminate duplicate copies of functions and vtables and such.
13863 The linker will arbitrarily choose one and discard the others.
13864 The AT_*_pc values for such functions refer to local labels in
13865 these sections. If the section from that file was discarded, the
13866 labels are not in the output, so the relocs get a value of 0.
13867 If this is a discarded function, mark the pc bounds as invalid,
13868 so that GDB will ignore it. */
72dca2f5 13869 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13870 return PC_BOUNDS_INVALID;
c906108c
SS
13871
13872 *lowpc = low;
96408a79
SA
13873 if (highpc)
13874 *highpc = high;
af34e669 13875 return ret;
c906108c
SS
13876}
13877
b084d499
JB
13878/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13879 its low and high PC addresses. Do nothing if these addresses could not
13880 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13881 and HIGHPC to the high address if greater than HIGHPC. */
13882
13883static void
13884dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13885 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13886 struct dwarf2_cu *cu)
13887{
13888 CORE_ADDR low, high;
13889 struct die_info *child = die->child;
13890
e385593e 13891 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13892 {
325fac50
PA
13893 *lowpc = std::min (*lowpc, low);
13894 *highpc = std::max (*highpc, high);
b084d499
JB
13895 }
13896
13897 /* If the language does not allow nested subprograms (either inside
13898 subprograms or lexical blocks), we're done. */
13899 if (cu->language != language_ada)
13900 return;
6e70227d 13901
b084d499
JB
13902 /* Check all the children of the given DIE. If it contains nested
13903 subprograms, then check their pc bounds. Likewise, we need to
13904 check lexical blocks as well, as they may also contain subprogram
13905 definitions. */
13906 while (child && child->tag)
13907 {
13908 if (child->tag == DW_TAG_subprogram
13909 || child->tag == DW_TAG_lexical_block)
13910 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13911 child = sibling_die (child);
13912 }
13913}
13914
fae299cd
DC
13915/* Get the low and high pc's represented by the scope DIE, and store
13916 them in *LOWPC and *HIGHPC. If the correct values can't be
13917 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13918
13919static void
13920get_scope_pc_bounds (struct die_info *die,
13921 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13922 struct dwarf2_cu *cu)
13923{
13924 CORE_ADDR best_low = (CORE_ADDR) -1;
13925 CORE_ADDR best_high = (CORE_ADDR) 0;
13926 CORE_ADDR current_low, current_high;
13927
3a2b436a 13928 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13929 >= PC_BOUNDS_RANGES)
fae299cd
DC
13930 {
13931 best_low = current_low;
13932 best_high = current_high;
13933 }
13934 else
13935 {
13936 struct die_info *child = die->child;
13937
13938 while (child && child->tag)
13939 {
13940 switch (child->tag) {
13941 case DW_TAG_subprogram:
b084d499 13942 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13943 break;
13944 case DW_TAG_namespace:
f55ee35c 13945 case DW_TAG_module:
fae299cd
DC
13946 /* FIXME: carlton/2004-01-16: Should we do this for
13947 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13948 that current GCC's always emit the DIEs corresponding
13949 to definitions of methods of classes as children of a
13950 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13951 the DIEs giving the declarations, which could be
13952 anywhere). But I don't see any reason why the
13953 standards says that they have to be there. */
13954 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13955
13956 if (current_low != ((CORE_ADDR) -1))
13957 {
325fac50
PA
13958 best_low = std::min (best_low, current_low);
13959 best_high = std::max (best_high, current_high);
fae299cd
DC
13960 }
13961 break;
13962 default:
0963b4bd 13963 /* Ignore. */
fae299cd
DC
13964 break;
13965 }
13966
13967 child = sibling_die (child);
13968 }
13969 }
13970
13971 *lowpc = best_low;
13972 *highpc = best_high;
13973}
13974
801e3a5b
JB
13975/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13976 in DIE. */
380bca97 13977
801e3a5b
JB
13978static void
13979dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13980 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13981{
518817b3 13982 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13983 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13984 struct attribute *attr;
91da1414 13985 struct attribute *attr_high;
801e3a5b 13986
91da1414
MW
13987 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13988 if (attr_high)
801e3a5b 13989 {
801e3a5b 13990 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13991 if (attr != nullptr)
801e3a5b 13992 {
cd6c91b4
TT
13993 CORE_ADDR low = attr->value_as_address ();
13994 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 13995
cd6c91b4 13996 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13997 high += low;
9a619af0 13998
3e29f34a
MR
13999 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14000 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14001 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14002 }
14003 }
14004
14005 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14006 if (attr != nullptr)
801e3a5b 14007 {
18a8505e 14008 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14009 We take advantage of the fact that DW_AT_ranges does not appear
14010 in DW_TAG_compile_unit of DWO files. */
14011 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14012
14013 /* The value of the DW_AT_ranges attribute is the offset of the
14014 address range list in the .debug_ranges section. */
ab435259
DE
14015 unsigned long offset = (DW_UNSND (attr)
14016 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14017
2d5f09ec 14018 std::vector<blockrange> blockvec;
5f46c5a5
JK
14019 dwarf2_ranges_process (offset, cu,
14020 [&] (CORE_ADDR start, CORE_ADDR end)
14021 {
58fdfd2c
JK
14022 start += baseaddr;
14023 end += baseaddr;
5f46c5a5
JK
14024 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14025 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14026 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14027 blockvec.emplace_back (start, end);
5f46c5a5 14028 });
2d5f09ec
KB
14029
14030 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14031 }
14032}
14033
685b1105
JK
14034/* Check whether the producer field indicates either of GCC < 4.6, or the
14035 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14036
685b1105
JK
14037static void
14038check_producer (struct dwarf2_cu *cu)
60d5a603 14039{
38360086 14040 int major, minor;
60d5a603
JK
14041
14042 if (cu->producer == NULL)
14043 {
14044 /* For unknown compilers expect their behavior is DWARF version
14045 compliant.
14046
14047 GCC started to support .debug_types sections by -gdwarf-4 since
14048 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14049 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14050 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14051 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14052 }
b1ffba5a 14053 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14054 {
38360086
MW
14055 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14056 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14057 }
5230b05a 14058 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14059 {
14060 cu->producer_is_icc = true;
14061 cu->producer_is_icc_lt_14 = major < 14;
14062 }
c258c396
JD
14063 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14064 cu->producer_is_codewarrior = true;
685b1105
JK
14065 else
14066 {
14067 /* For other non-GCC compilers, expect their behavior is DWARF version
14068 compliant. */
60d5a603
JK
14069 }
14070
9068261f 14071 cu->checked_producer = true;
685b1105 14072}
ba919b58 14073
685b1105
JK
14074/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14075 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14076 during 4.6.0 experimental. */
14077
9068261f 14078static bool
685b1105
JK
14079producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14080{
14081 if (!cu->checked_producer)
14082 check_producer (cu);
14083
14084 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14085}
14086
c258c396
JD
14087
14088/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14089 with incorrect is_stmt attributes. */
14090
14091static bool
14092producer_is_codewarrior (struct dwarf2_cu *cu)
14093{
14094 if (!cu->checked_producer)
14095 check_producer (cu);
14096
14097 return cu->producer_is_codewarrior;
14098}
14099
405feb71 14100/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14101 DW_AT_accessibility. */
14102
14103static enum dwarf_access_attribute
14104dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14105{
14106 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14107 {
14108 /* The default DWARF 2 accessibility for members is public, the default
14109 accessibility for inheritance is private. */
14110
14111 if (die->tag != DW_TAG_inheritance)
14112 return DW_ACCESS_public;
14113 else
14114 return DW_ACCESS_private;
14115 }
14116 else
14117 {
14118 /* DWARF 3+ defines the default accessibility a different way. The same
14119 rules apply now for DW_TAG_inheritance as for the members and it only
14120 depends on the container kind. */
14121
14122 if (die->parent->tag == DW_TAG_class_type)
14123 return DW_ACCESS_private;
14124 else
14125 return DW_ACCESS_public;
14126 }
14127}
14128
74ac6d43
TT
14129/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14130 offset. If the attribute was not found return 0, otherwise return
14131 1. If it was found but could not properly be handled, set *OFFSET
14132 to 0. */
14133
14134static int
14135handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14136 LONGEST *offset)
14137{
14138 struct attribute *attr;
14139
14140 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14141 if (attr != NULL)
14142 {
14143 *offset = 0;
14144
14145 /* Note that we do not check for a section offset first here.
14146 This is because DW_AT_data_member_location is new in DWARF 4,
14147 so if we see it, we can assume that a constant form is really
14148 a constant and not a section offset. */
cd6c91b4 14149 if (attr->form_is_constant ())
74ac6d43 14150 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14151 else if (attr->form_is_section_offset ())
74ac6d43 14152 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14153 else if (attr->form_is_block ())
74ac6d43
TT
14154 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14155 else
14156 dwarf2_complex_location_expr_complaint ();
14157
14158 return 1;
14159 }
14160
14161 return 0;
14162}
14163
c906108c
SS
14164/* Add an aggregate field to the field list. */
14165
14166static void
107d2387 14167dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14168 struct dwarf2_cu *cu)
6e70227d 14169{
518817b3 14170 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14171 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14172 struct nextfield *new_field;
14173 struct attribute *attr;
14174 struct field *fp;
15d034d0 14175 const char *fieldname = "";
c906108c 14176
7d0ccb61
DJ
14177 if (die->tag == DW_TAG_inheritance)
14178 {
be2daae6
TT
14179 fip->baseclasses.emplace_back ();
14180 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14181 }
14182 else
14183 {
be2daae6
TT
14184 fip->fields.emplace_back ();
14185 new_field = &fip->fields.back ();
7d0ccb61 14186 }
be2daae6 14187
c906108c
SS
14188 fip->nfields++;
14189
e142c38c 14190 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14191 if (attr != nullptr)
c906108c 14192 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14193 else
14194 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14195 if (new_field->accessibility != DW_ACCESS_public)
14196 fip->non_public_fields = 1;
60d5a603 14197
e142c38c 14198 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14199 if (attr != nullptr)
c906108c 14200 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14201 else
14202 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14203
14204 fp = &new_field->field;
a9a9bd0f 14205
e142c38c 14206 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14207 {
74ac6d43
TT
14208 LONGEST offset;
14209
a9a9bd0f 14210 /* Data member other than a C++ static data member. */
6e70227d 14211
c906108c 14212 /* Get type of field. */
e7c27a73 14213 fp->type = die_type (die, cu);
c906108c 14214
d6a843b5 14215 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14216
c906108c 14217 /* Get bit size of field (zero if none). */
e142c38c 14218 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14219 if (attr != nullptr)
c906108c
SS
14220 {
14221 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14222 }
14223 else
14224 {
14225 FIELD_BITSIZE (*fp) = 0;
14226 }
14227
14228 /* Get bit offset of field. */
74ac6d43
TT
14229 if (handle_data_member_location (die, cu, &offset))
14230 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14231 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14232 if (attr != nullptr)
c906108c 14233 {
d5a22e77 14234 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14235 {
14236 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14237 additional bit offset from the MSB of the containing
14238 anonymous object to the MSB of the field. We don't
14239 have to do anything special since we don't need to
14240 know the size of the anonymous object. */
f41f5e61 14241 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14242 }
14243 else
14244 {
14245 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14246 MSB of the anonymous object, subtract off the number of
14247 bits from the MSB of the field to the MSB of the
14248 object, and then subtract off the number of bits of
14249 the field itself. The result is the bit offset of
14250 the LSB of the field. */
c906108c
SS
14251 int anonymous_size;
14252 int bit_offset = DW_UNSND (attr);
14253
e142c38c 14254 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14255 if (attr != nullptr)
c906108c
SS
14256 {
14257 /* The size of the anonymous object containing
14258 the bit field is explicit, so use the
14259 indicated size (in bytes). */
14260 anonymous_size = DW_UNSND (attr);
14261 }
14262 else
14263 {
14264 /* The size of the anonymous object containing
14265 the bit field must be inferred from the type
14266 attribute of the data member containing the
14267 bit field. */
14268 anonymous_size = TYPE_LENGTH (fp->type);
14269 }
f41f5e61
PA
14270 SET_FIELD_BITPOS (*fp,
14271 (FIELD_BITPOS (*fp)
14272 + anonymous_size * bits_per_byte
14273 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14274 }
14275 }
da5b30da
AA
14276 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14277 if (attr != NULL)
14278 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14279 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14280
14281 /* Get name of field. */
39cbfefa
DJ
14282 fieldname = dwarf2_name (die, cu);
14283 if (fieldname == NULL)
14284 fieldname = "";
d8151005
DJ
14285
14286 /* The name is already allocated along with this objfile, so we don't
14287 need to duplicate it for the type. */
14288 fp->name = fieldname;
c906108c
SS
14289
14290 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14291 pointer or virtual base class pointer) to private. */
e142c38c 14292 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14293 {
d48cc9dd 14294 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14295 new_field->accessibility = DW_ACCESS_private;
14296 fip->non_public_fields = 1;
14297 }
14298 }
a9a9bd0f 14299 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14300 {
a9a9bd0f
DC
14301 /* C++ static member. */
14302
14303 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14304 is a declaration, but all versions of G++ as of this writing
14305 (so through at least 3.2.1) incorrectly generate
14306 DW_TAG_variable tags. */
6e70227d 14307
ff355380 14308 const char *physname;
c906108c 14309
a9a9bd0f 14310 /* Get name of field. */
39cbfefa
DJ
14311 fieldname = dwarf2_name (die, cu);
14312 if (fieldname == NULL)
c906108c
SS
14313 return;
14314
254e6b9e 14315 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14316 if (attr
14317 /* Only create a symbol if this is an external value.
14318 new_symbol checks this and puts the value in the global symbol
14319 table, which we want. If it is not external, new_symbol
14320 will try to put the value in cu->list_in_scope which is wrong. */
14321 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14322 {
14323 /* A static const member, not much different than an enum as far as
14324 we're concerned, except that we can support more types. */
14325 new_symbol (die, NULL, cu);
14326 }
14327
2df3850c 14328 /* Get physical name. */
ff355380 14329 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14330
d8151005
DJ
14331 /* The name is already allocated along with this objfile, so we don't
14332 need to duplicate it for the type. */
14333 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14334 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14335 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14336 }
14337 else if (die->tag == DW_TAG_inheritance)
14338 {
74ac6d43 14339 LONGEST offset;
d4b96c9a 14340
74ac6d43
TT
14341 /* C++ base class field. */
14342 if (handle_data_member_location (die, cu, &offset))
14343 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14344 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14345 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14346 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14347 }
2ddeaf8a
TT
14348 else if (die->tag == DW_TAG_variant_part)
14349 {
14350 /* process_structure_scope will treat this DIE as a union. */
14351 process_structure_scope (die, cu);
14352
14353 /* The variant part is relative to the start of the enclosing
14354 structure. */
14355 SET_FIELD_BITPOS (*fp, 0);
14356 fp->type = get_die_type (die, cu);
14357 fp->artificial = 1;
14358 fp->name = "<<variant>>";
c8c81635
TT
14359
14360 /* Normally a DW_TAG_variant_part won't have a size, but our
14361 representation requires one, so set it to the maximum of the
489dbda6
TT
14362 child sizes, being sure to account for the offset at which
14363 each child is seen. */
c8c81635
TT
14364 if (TYPE_LENGTH (fp->type) == 0)
14365 {
14366 unsigned max = 0;
14367 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14368 {
14369 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14370 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14371 if (len > max)
14372 max = len;
14373 }
c8c81635
TT
14374 TYPE_LENGTH (fp->type) = max;
14375 }
2ddeaf8a
TT
14376 }
14377 else
14378 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14379}
14380
883fd55a
KS
14381/* Can the type given by DIE define another type? */
14382
14383static bool
14384type_can_define_types (const struct die_info *die)
14385{
14386 switch (die->tag)
14387 {
14388 case DW_TAG_typedef:
14389 case DW_TAG_class_type:
14390 case DW_TAG_structure_type:
14391 case DW_TAG_union_type:
14392 case DW_TAG_enumeration_type:
14393 return true;
14394
14395 default:
14396 return false;
14397 }
14398}
14399
14400/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14401
14402static void
883fd55a
KS
14403dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14404 struct dwarf2_cu *cu)
6e70227d 14405{
be2daae6
TT
14406 struct decl_field fp;
14407 memset (&fp, 0, sizeof (fp));
98751a41 14408
883fd55a 14409 gdb_assert (type_can_define_types (die));
98751a41 14410
883fd55a 14411 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14412 fp.name = dwarf2_name (die, cu);
14413 fp.type = read_type_die (die, cu);
98751a41 14414
c191a687
KS
14415 /* Save accessibility. */
14416 enum dwarf_access_attribute accessibility;
14417 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14418 if (attr != NULL)
14419 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14420 else
14421 accessibility = dwarf2_default_access_attribute (die, cu);
14422 switch (accessibility)
14423 {
14424 case DW_ACCESS_public:
14425 /* The assumed value if neither private nor protected. */
14426 break;
14427 case DW_ACCESS_private:
be2daae6 14428 fp.is_private = 1;
c191a687
KS
14429 break;
14430 case DW_ACCESS_protected:
be2daae6 14431 fp.is_protected = 1;
c191a687
KS
14432 break;
14433 default:
b98664d3 14434 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14435 }
14436
883fd55a 14437 if (die->tag == DW_TAG_typedef)
be2daae6 14438 fip->typedef_field_list.push_back (fp);
883fd55a 14439 else
be2daae6 14440 fip->nested_types_list.push_back (fp);
98751a41
JK
14441}
14442
c906108c
SS
14443/* Create the vector of fields, and attach it to the type. */
14444
14445static void
fba45db2 14446dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14447 struct dwarf2_cu *cu)
c906108c
SS
14448{
14449 int nfields = fip->nfields;
14450
14451 /* Record the field count, allocate space for the array of fields,
14452 and create blank accessibility bitfields if necessary. */
14453 TYPE_NFIELDS (type) = nfields;
14454 TYPE_FIELDS (type) = (struct field *)
be2daae6 14455 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14456
b4ba55a1 14457 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14458 {
14459 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14460
14461 TYPE_FIELD_PRIVATE_BITS (type) =
14462 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14463 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14464
14465 TYPE_FIELD_PROTECTED_BITS (type) =
14466 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14467 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14468
774b6a14
TT
14469 TYPE_FIELD_IGNORE_BITS (type) =
14470 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14471 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14472 }
14473
14474 /* If the type has baseclasses, allocate and clear a bit vector for
14475 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14476 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14477 {
be2daae6 14478 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14479 unsigned char *pointer;
c906108c
SS
14480
14481 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14482 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14483 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14484 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14485 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14486 }
14487
2ddeaf8a
TT
14488 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14489 {
14490 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14491
be2daae6 14492 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14493 {
be2daae6
TT
14494 struct nextfield &field = fip->fields[index];
14495
14496 if (field.variant.is_discriminant)
2ddeaf8a 14497 di->discriminant_index = index;
be2daae6 14498 else if (field.variant.default_branch)
2ddeaf8a
TT
14499 di->default_index = index;
14500 else
be2daae6 14501 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14502 }
14503 }
14504
be2daae6
TT
14505 /* Copy the saved-up fields into the field vector. */
14506 for (int i = 0; i < nfields; ++i)
c906108c 14507 {
be2daae6
TT
14508 struct nextfield &field
14509 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14510 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14511
be2daae6
TT
14512 TYPE_FIELD (type, i) = field.field;
14513 switch (field.accessibility)
c906108c 14514 {
c5aa993b 14515 case DW_ACCESS_private:
b4ba55a1 14516 if (cu->language != language_ada)
be2daae6 14517 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14518 break;
c906108c 14519
c5aa993b 14520 case DW_ACCESS_protected:
b4ba55a1 14521 if (cu->language != language_ada)
be2daae6 14522 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14523 break;
c906108c 14524
c5aa993b
JM
14525 case DW_ACCESS_public:
14526 break;
c906108c 14527
c5aa993b
JM
14528 default:
14529 /* Unknown accessibility. Complain and treat it as public. */
14530 {
b98664d3 14531 complaint (_("unsupported accessibility %d"),
be2daae6 14532 field.accessibility);
c5aa993b
JM
14533 }
14534 break;
c906108c 14535 }
be2daae6 14536 if (i < fip->baseclasses.size ())
c906108c 14537 {
be2daae6 14538 switch (field.virtuality)
c906108c 14539 {
c5aa993b
JM
14540 case DW_VIRTUALITY_virtual:
14541 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14542 if (cu->language == language_ada)
a73c6dcd 14543 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14544 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14545 break;
c906108c
SS
14546 }
14547 }
c906108c
SS
14548 }
14549}
14550
7d27a96d
TT
14551/* Return true if this member function is a constructor, false
14552 otherwise. */
14553
14554static int
14555dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14556{
14557 const char *fieldname;
fe978cb0 14558 const char *type_name;
7d27a96d
TT
14559 int len;
14560
14561 if (die->parent == NULL)
14562 return 0;
14563
14564 if (die->parent->tag != DW_TAG_structure_type
14565 && die->parent->tag != DW_TAG_union_type
14566 && die->parent->tag != DW_TAG_class_type)
14567 return 0;
14568
14569 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14570 type_name = dwarf2_name (die->parent, cu);
14571 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14572 return 0;
14573
14574 len = strlen (fieldname);
fe978cb0
PA
14575 return (strncmp (fieldname, type_name, len) == 0
14576 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14577}
14578
e35000a7
TBA
14579/* Check if the given VALUE is a recognized enum
14580 dwarf_defaulted_attribute constant according to DWARF5 spec,
14581 Table 7.24. */
14582
14583static bool
14584is_valid_DW_AT_defaulted (ULONGEST value)
14585{
14586 switch (value)
14587 {
14588 case DW_DEFAULTED_no:
14589 case DW_DEFAULTED_in_class:
14590 case DW_DEFAULTED_out_of_class:
14591 return true;
14592 }
14593
3142e908 14594 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14595 return false;
14596}
14597
c906108c
SS
14598/* Add a member function to the proper fieldlist. */
14599
14600static void
107d2387 14601dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14602 struct type *type, struct dwarf2_cu *cu)
c906108c 14603{
518817b3 14604 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14605 struct attribute *attr;
c906108c 14606 int i;
be2daae6 14607 struct fnfieldlist *flp = nullptr;
c906108c 14608 struct fn_field *fnp;
15d034d0 14609 const char *fieldname;
f792889a 14610 struct type *this_type;
60d5a603 14611 enum dwarf_access_attribute accessibility;
c906108c 14612
b4ba55a1 14613 if (cu->language == language_ada)
a73c6dcd 14614 error (_("unexpected member function in Ada type"));
b4ba55a1 14615
2df3850c 14616 /* Get name of member function. */
39cbfefa
DJ
14617 fieldname = dwarf2_name (die, cu);
14618 if (fieldname == NULL)
2df3850c 14619 return;
c906108c 14620
c906108c 14621 /* Look up member function name in fieldlist. */
be2daae6 14622 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14623 {
27bfe10e 14624 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14625 {
14626 flp = &fip->fnfieldlists[i];
14627 break;
14628 }
c906108c
SS
14629 }
14630
be2daae6
TT
14631 /* Create a new fnfieldlist if necessary. */
14632 if (flp == nullptr)
c906108c 14633 {
be2daae6
TT
14634 fip->fnfieldlists.emplace_back ();
14635 flp = &fip->fnfieldlists.back ();
c906108c 14636 flp->name = fieldname;
be2daae6 14637 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14638 }
14639
be2daae6
TT
14640 /* Create a new member function field and add it to the vector of
14641 fnfieldlists. */
14642 flp->fnfields.emplace_back ();
14643 fnp = &flp->fnfields.back ();
3da10d80
KS
14644
14645 /* Delay processing of the physname until later. */
9c37b5ae 14646 if (cu->language == language_cplus)
be2daae6
TT
14647 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14648 die, cu);
3da10d80
KS
14649 else
14650 {
1d06ead6 14651 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14652 fnp->physname = physname ? physname : "";
14653 }
14654
c906108c 14655 fnp->type = alloc_type (objfile);
f792889a
DJ
14656 this_type = read_type_die (die, cu);
14657 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14658 {
f792889a 14659 int nparams = TYPE_NFIELDS (this_type);
c906108c 14660
f792889a 14661 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14662 of the method itself (TYPE_CODE_METHOD). */
14663 smash_to_method_type (fnp->type, type,
f792889a
DJ
14664 TYPE_TARGET_TYPE (this_type),
14665 TYPE_FIELDS (this_type),
14666 TYPE_NFIELDS (this_type),
14667 TYPE_VARARGS (this_type));
c906108c
SS
14668
14669 /* Handle static member functions.
c5aa993b 14670 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14671 member functions. G++ helps GDB by marking the first
14672 parameter for non-static member functions (which is the this
14673 pointer) as artificial. We obtain this information from
14674 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14675 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14676 fnp->voffset = VOFFSET_STATIC;
14677 }
14678 else
b98664d3 14679 complaint (_("member function type missing for '%s'"),
3da10d80 14680 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14681
14682 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14683 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14684 fnp->fcontext = die_containing_type (die, cu);
c906108c 14685
3e43a32a
MS
14686 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14687 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14688
14689 /* Get accessibility. */
e142c38c 14690 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14691 if (attr != nullptr)
aead7601 14692 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14693 else
14694 accessibility = dwarf2_default_access_attribute (die, cu);
14695 switch (accessibility)
c906108c 14696 {
60d5a603
JK
14697 case DW_ACCESS_private:
14698 fnp->is_private = 1;
14699 break;
14700 case DW_ACCESS_protected:
14701 fnp->is_protected = 1;
14702 break;
c906108c
SS
14703 }
14704
b02dede2 14705 /* Check for artificial methods. */
e142c38c 14706 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14707 if (attr && DW_UNSND (attr) != 0)
14708 fnp->is_artificial = 1;
14709
e35000a7
TBA
14710 /* Check for defaulted methods. */
14711 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14712 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14713 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14714
14715 /* Check for deleted methods. */
14716 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14717 if (attr != nullptr && DW_UNSND (attr) != 0)
14718 fnp->is_deleted = 1;
14719
7d27a96d
TT
14720 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14721
0d564a31 14722 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14723 function. For older versions of GCC, this is an offset in the
14724 appropriate virtual table, as specified by DW_AT_containing_type.
14725 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14726 to the object address. */
14727
e142c38c 14728 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14729 if (attr != nullptr)
8e19ed76 14730 {
4fc6c0d5 14731 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14732 {
aec5aa8b
TT
14733 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14734 {
14735 /* Old-style GCC. */
14736 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14737 }
14738 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14739 || (DW_BLOCK (attr)->size > 1
14740 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14741 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14742 {
aec5aa8b
TT
14743 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14744 if ((fnp->voffset % cu->header.addr_size) != 0)
14745 dwarf2_complex_location_expr_complaint ();
14746 else
14747 fnp->voffset /= cu->header.addr_size;
14748 fnp->voffset += 2;
14749 }
14750 else
14751 dwarf2_complex_location_expr_complaint ();
14752
14753 if (!fnp->fcontext)
7e993ebf
KS
14754 {
14755 /* If there is no `this' field and no DW_AT_containing_type,
14756 we cannot actually find a base class context for the
14757 vtable! */
14758 if (TYPE_NFIELDS (this_type) == 0
14759 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14760 {
b98664d3 14761 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14762 "function \"%s\" (offset %s)"),
14763 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14764 }
14765 else
14766 {
14767 fnp->fcontext
14768 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14769 }
14770 }
aec5aa8b 14771 }
cd6c91b4 14772 else if (attr->form_is_section_offset ())
8e19ed76 14773 {
4d3c2250 14774 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14775 }
14776 else
14777 {
4d3c2250
KB
14778 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14779 fieldname);
8e19ed76 14780 }
0d564a31 14781 }
d48cc9dd
DJ
14782 else
14783 {
14784 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14785 if (attr && DW_UNSND (attr))
14786 {
14787 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14788 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14789 "but the vtable offset is not specified"),
9d8780f0 14790 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14791 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14792 TYPE_CPLUS_DYNAMIC (type) = 1;
14793 }
14794 }
c906108c
SS
14795}
14796
14797/* Create the vector of member function fields, and attach it to the type. */
14798
14799static void
fba45db2 14800dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14801 struct dwarf2_cu *cu)
c906108c 14802{
b4ba55a1 14803 if (cu->language == language_ada)
a73c6dcd 14804 error (_("unexpected member functions in Ada type"));
b4ba55a1 14805
c906108c
SS
14806 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14807 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14808 TYPE_ALLOC (type,
14809 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14810
be2daae6 14811 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14812 {
be2daae6 14813 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14814 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14815
be2daae6
TT
14816 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14817 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14818 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14819 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14820
14821 for (int k = 0; k < nf.fnfields.size (); ++k)
14822 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14823 }
14824
be2daae6 14825 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14826}
14827
1168df01
JB
14828/* Returns non-zero if NAME is the name of a vtable member in CU's
14829 language, zero otherwise. */
14830static int
14831is_vtable_name (const char *name, struct dwarf2_cu *cu)
14832{
14833 static const char vptr[] = "_vptr";
14834
9c37b5ae
TT
14835 /* Look for the C++ form of the vtable. */
14836 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14837 return 1;
14838
14839 return 0;
14840}
14841
c0dd20ea 14842/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14843 functions, with the ABI-specified layout. If TYPE describes
14844 such a structure, smash it into a member function type.
61049d3b
DJ
14845
14846 GCC shouldn't do this; it should just output pointer to member DIEs.
14847 This is GCC PR debug/28767. */
c0dd20ea 14848
0b92b5bb
TT
14849static void
14850quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14851{
09e2d7c7 14852 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14853
14854 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14855 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14856 return;
c0dd20ea
DJ
14857
14858 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14859 if (TYPE_FIELD_NAME (type, 0) == NULL
14860 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14861 || TYPE_FIELD_NAME (type, 1) == NULL
14862 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14863 return;
c0dd20ea
DJ
14864
14865 /* Find the type of the method. */
0b92b5bb 14866 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14867 if (pfn_type == NULL
14868 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14869 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14870 return;
c0dd20ea
DJ
14871
14872 /* Look for the "this" argument. */
14873 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14874 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14875 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14876 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14877 return;
c0dd20ea 14878
09e2d7c7 14879 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14880 new_type = alloc_type (objfile);
09e2d7c7 14881 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14882 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14883 TYPE_VARARGS (pfn_type));
0b92b5bb 14884 smash_to_methodptr_type (type, new_type);
c0dd20ea 14885}
1168df01 14886
2b4424c3
TT
14887/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14888 appropriate error checking and issuing complaints if there is a
14889 problem. */
14890
14891static ULONGEST
14892get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14893{
14894 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14895
14896 if (attr == nullptr)
14897 return 0;
14898
cd6c91b4 14899 if (!attr->form_is_constant ())
2b4424c3 14900 {
b98664d3 14901 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14902 " - DIE at %s [in module %s]"),
14903 sect_offset_str (die->sect_off),
14904 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14905 return 0;
14906 }
14907
14908 ULONGEST align;
14909 if (attr->form == DW_FORM_sdata)
14910 {
14911 LONGEST val = DW_SND (attr);
14912 if (val < 0)
14913 {
b98664d3 14914 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14915 " - DIE at %s [in module %s]"),
14916 sect_offset_str (die->sect_off),
14917 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14918 return 0;
14919 }
14920 align = val;
14921 }
14922 else
14923 align = DW_UNSND (attr);
14924
14925 if (align == 0)
14926 {
b98664d3 14927 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14928 " - DIE at %s [in module %s]"),
14929 sect_offset_str (die->sect_off),
14930 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14931 return 0;
14932 }
14933 if ((align & (align - 1)) != 0)
14934 {
b98664d3 14935 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14936 " - DIE at %s [in module %s]"),
14937 sect_offset_str (die->sect_off),
14938 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14939 return 0;
14940 }
14941
14942 return align;
14943}
14944
14945/* If the DIE has a DW_AT_alignment attribute, use its value to set
14946 the alignment for TYPE. */
14947
14948static void
14949maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14950 struct type *type)
14951{
14952 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14953 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14954 " - DIE at %s [in module %s]"),
14955 sect_offset_str (die->sect_off),
14956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14957}
685b1105 14958
e35000a7
TBA
14959/* Check if the given VALUE is a valid enum dwarf_calling_convention
14960 constant for a type, according to DWARF5 spec, Table 5.5. */
14961
14962static bool
14963is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14964{
14965 switch (value)
14966 {
14967 case DW_CC_normal:
14968 case DW_CC_pass_by_reference:
14969 case DW_CC_pass_by_value:
14970 return true;
14971
14972 default:
14973 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14974 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14975 return false;
14976 }
14977}
14978
d0922fcf
TBA
14979/* Check if the given VALUE is a valid enum dwarf_calling_convention
14980 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14981 also according to GNU-specific values (see include/dwarf2.h). */
14982
14983static bool
14984is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14985{
14986 switch (value)
14987 {
14988 case DW_CC_normal:
14989 case DW_CC_program:
14990 case DW_CC_nocall:
14991 return true;
14992
14993 case DW_CC_GNU_renesas_sh:
14994 case DW_CC_GNU_borland_fastcall_i386:
14995 case DW_CC_GDB_IBM_OpenCL:
14996 return true;
14997
14998 default:
14999 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15000 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15001 return false;
15002 }
15003}
15004
c906108c 15005/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15006 (definition) to create a type for the structure or union. Fill in
15007 the type's name and general properties; the members will not be
83655187
DE
15008 processed until process_structure_scope. A symbol table entry for
15009 the type will also not be done until process_structure_scope (assuming
15010 the type has a name).
c906108c 15011
c767944b
DJ
15012 NOTE: we need to call these functions regardless of whether or not the
15013 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15014 structure or union. This gets the type entered into our set of
83655187 15015 user defined types. */
c906108c 15016
f792889a 15017static struct type *
134d01f1 15018read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15019{
518817b3 15020 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15021 struct type *type;
15022 struct attribute *attr;
15d034d0 15023 const char *name;
c906108c 15024
348e048f
DE
15025 /* If the definition of this type lives in .debug_types, read that type.
15026 Don't follow DW_AT_specification though, that will take us back up
15027 the chain and we want to go down. */
45e58e77 15028 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15029 if (attr != nullptr)
348e048f 15030 {
ac9ec31b 15031 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15032
ac9ec31b 15033 /* The type's CU may not be the same as CU.
02142a6c 15034 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15035 return set_die_type (die, type, cu);
15036 }
15037
c0dd20ea 15038 type = alloc_type (objfile);
c906108c 15039 INIT_CPLUS_SPECIFIC (type);
93311388 15040
39cbfefa
DJ
15041 name = dwarf2_name (die, cu);
15042 if (name != NULL)
c906108c 15043 {
987504bb 15044 if (cu->language == language_cplus
c44af4eb
TT
15045 || cu->language == language_d
15046 || cu->language == language_rust)
63d06c5c 15047 {
15d034d0 15048 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15049
15050 /* dwarf2_full_name might have already finished building the DIE's
15051 type. If so, there is no need to continue. */
15052 if (get_die_type (die, cu) != NULL)
15053 return get_die_type (die, cu);
15054
e86ca25f 15055 TYPE_NAME (type) = full_name;
63d06c5c
DC
15056 }
15057 else
15058 {
d8151005
DJ
15059 /* The name is already allocated along with this objfile, so
15060 we don't need to duplicate it for the type. */
e86ca25f 15061 TYPE_NAME (type) = name;
63d06c5c 15062 }
c906108c
SS
15063 }
15064
15065 if (die->tag == DW_TAG_structure_type)
15066 {
15067 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15068 }
15069 else if (die->tag == DW_TAG_union_type)
15070 {
15071 TYPE_CODE (type) = TYPE_CODE_UNION;
15072 }
2ddeaf8a
TT
15073 else if (die->tag == DW_TAG_variant_part)
15074 {
15075 TYPE_CODE (type) = TYPE_CODE_UNION;
15076 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15077 }
c906108c
SS
15078 else
15079 {
4753d33b 15080 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15081 }
15082
0cc2414c
TT
15083 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15084 TYPE_DECLARED_CLASS (type) = 1;
15085
e35000a7
TBA
15086 /* Store the calling convention in the type if it's available in
15087 the die. Otherwise the calling convention remains set to
15088 the default value DW_CC_normal. */
15089 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15090 if (attr != nullptr
15091 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15092 {
15093 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15094 TYPE_CPLUS_CALLING_CONVENTION (type)
15095 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15096 }
15097
e142c38c 15098 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15099 if (attr != nullptr)
c906108c 15100 {
cd6c91b4 15101 if (attr->form_is_constant ())
155bfbd3
JB
15102 TYPE_LENGTH (type) = DW_UNSND (attr);
15103 else
15104 {
15105 /* For the moment, dynamic type sizes are not supported
15106 by GDB's struct type. The actual size is determined
15107 on-demand when resolving the type of a given object,
15108 so set the type's length to zero for now. Otherwise,
15109 we record an expression as the length, and that expression
15110 could lead to a very large value, which could eventually
15111 lead to us trying to allocate that much memory when creating
15112 a value of that type. */
15113 TYPE_LENGTH (type) = 0;
15114 }
c906108c
SS
15115 }
15116 else
15117 {
15118 TYPE_LENGTH (type) = 0;
15119 }
15120
2b4424c3
TT
15121 maybe_set_alignment (cu, die, type);
15122
5230b05a 15123 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15124 {
5230b05a
WT
15125 /* ICC<14 does not output the required DW_AT_declaration on
15126 incomplete types, but gives them a size of zero. */
422b1cb0 15127 TYPE_STUB (type) = 1;
685b1105
JK
15128 }
15129 else
15130 TYPE_STUB_SUPPORTED (type) = 1;
15131
dc718098 15132 if (die_is_declaration (die, cu))
876cecd0 15133 TYPE_STUB (type) = 1;
a6c727b2
DJ
15134 else if (attr == NULL && die->child == NULL
15135 && producer_is_realview (cu->producer))
15136 /* RealView does not output the required DW_AT_declaration
15137 on incomplete types. */
15138 TYPE_STUB (type) = 1;
dc718098 15139
c906108c
SS
15140 /* We need to add the type field to the die immediately so we don't
15141 infinitely recurse when dealing with pointers to the structure
0963b4bd 15142 type within the structure itself. */
1c379e20 15143 set_die_type (die, type, cu);
c906108c 15144
7e314c57
JK
15145 /* set_die_type should be already done. */
15146 set_descriptive_type (type, die, cu);
15147
c767944b
DJ
15148 return type;
15149}
15150
2ddeaf8a
TT
15151/* A helper for process_structure_scope that handles a single member
15152 DIE. */
15153
15154static void
15155handle_struct_member_die (struct die_info *child_die, struct type *type,
15156 struct field_info *fi,
15157 std::vector<struct symbol *> *template_args,
15158 struct dwarf2_cu *cu)
15159{
15160 if (child_die->tag == DW_TAG_member
15161 || child_die->tag == DW_TAG_variable
15162 || child_die->tag == DW_TAG_variant_part)
15163 {
15164 /* NOTE: carlton/2002-11-05: A C++ static data member
15165 should be a DW_TAG_member that is a declaration, but
15166 all versions of G++ as of this writing (so through at
15167 least 3.2.1) incorrectly generate DW_TAG_variable
15168 tags for them instead. */
15169 dwarf2_add_field (fi, child_die, cu);
15170 }
15171 else if (child_die->tag == DW_TAG_subprogram)
15172 {
15173 /* Rust doesn't have member functions in the C++ sense.
15174 However, it does emit ordinary functions as children
15175 of a struct DIE. */
15176 if (cu->language == language_rust)
15177 read_func_scope (child_die, cu);
15178 else
15179 {
15180 /* C++ member function. */
15181 dwarf2_add_member_fn (fi, child_die, type, cu);
15182 }
15183 }
15184 else if (child_die->tag == DW_TAG_inheritance)
15185 {
15186 /* C++ base class field. */
15187 dwarf2_add_field (fi, child_die, cu);
15188 }
15189 else if (type_can_define_types (child_die))
15190 dwarf2_add_type_defn (fi, child_die, cu);
15191 else if (child_die->tag == DW_TAG_template_type_param
15192 || child_die->tag == DW_TAG_template_value_param)
15193 {
15194 struct symbol *arg = new_symbol (child_die, NULL, cu);
15195
15196 if (arg != NULL)
15197 template_args->push_back (arg);
15198 }
15199 else if (child_die->tag == DW_TAG_variant)
15200 {
15201 /* In a variant we want to get the discriminant and also add a
15202 field for our sole member child. */
15203 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15204
bde09ab7 15205 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15206 variant_child != NULL;
15207 variant_child = sibling_die (variant_child))
15208 {
15209 if (variant_child->tag == DW_TAG_member)
15210 {
15211 handle_struct_member_die (variant_child, type, fi,
15212 template_args, cu);
15213 /* Only handle the one. */
15214 break;
15215 }
15216 }
15217
15218 /* We don't handle this but we might as well report it if we see
15219 it. */
15220 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15221 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15222 " - DIE at %s [in module %s]"),
15223 sect_offset_str (child_die->sect_off),
15224 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15225
15226 /* The first field was just added, so we can stash the
15227 discriminant there. */
be2daae6 15228 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15229 if (discr == NULL)
be2daae6 15230 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15231 else
be2daae6 15232 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15233 }
15234}
15235
c767944b
DJ
15236/* Finish creating a structure or union type, including filling in
15237 its members and creating a symbol for it. */
15238
15239static void
15240process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15241{
518817b3 15242 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15243 struct die_info *child_die;
c767944b
DJ
15244 struct type *type;
15245
15246 type = get_die_type (die, cu);
15247 if (type == NULL)
15248 type = read_structure_type (die, cu);
15249
2ddeaf8a
TT
15250 /* When reading a DW_TAG_variant_part, we need to notice when we
15251 read the discriminant member, so we can record it later in the
15252 discriminant_info. */
15253 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15254 sect_offset discr_offset {};
3e1d3d8c 15255 bool has_template_parameters = false;
2ddeaf8a
TT
15256
15257 if (is_variant_part)
15258 {
15259 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15260 if (discr == NULL)
15261 {
15262 /* Maybe it's a univariant form, an extension we support.
15263 In this case arrange not to check the offset. */
15264 is_variant_part = false;
15265 }
cd6c91b4 15266 else if (discr->form_is_ref ())
2ddeaf8a
TT
15267 {
15268 struct dwarf2_cu *target_cu = cu;
15269 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15270
15271 discr_offset = target_die->sect_off;
15272 }
15273 else
15274 {
b98664d3 15275 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15276 " - DIE at %s [in module %s]"),
15277 sect_offset_str (die->sect_off),
15278 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15279 is_variant_part = false;
15280 }
15281 }
15282
e142c38c 15283 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15284 {
15285 struct field_info fi;
2f4732b0 15286 std::vector<struct symbol *> template_args;
c906108c 15287
639d11d3 15288 child_die = die->child;
c906108c
SS
15289
15290 while (child_die && child_die->tag)
15291 {
2ddeaf8a 15292 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15293
2ddeaf8a 15294 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15295 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15296
c906108c
SS
15297 child_die = sibling_die (child_die);
15298 }
15299
34eaf542 15300 /* Attach template arguments to type. */
2f4732b0 15301 if (!template_args.empty ())
34eaf542 15302 {
3e1d3d8c 15303 has_template_parameters = true;
34eaf542 15304 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15305 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15306 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15307 = XOBNEWVEC (&objfile->objfile_obstack,
15308 struct symbol *,
15309 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15310 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15311 template_args.data (),
34eaf542
TT
15312 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15313 * sizeof (struct symbol *)));
34eaf542
TT
15314 }
15315
c906108c
SS
15316 /* Attach fields and member functions to the type. */
15317 if (fi.nfields)
e7c27a73 15318 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15319 if (!fi.fnfieldlists.empty ())
c906108c 15320 {
e7c27a73 15321 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15322
c5aa993b 15323 /* Get the type which refers to the base class (possibly this
c906108c 15324 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15325 class from the DW_AT_containing_type attribute. This use of
15326 DW_AT_containing_type is a GNU extension. */
c906108c 15327
e142c38c 15328 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15329 {
e7c27a73 15330 struct type *t = die_containing_type (die, cu);
c906108c 15331
ae6ae975 15332 set_type_vptr_basetype (type, t);
c906108c
SS
15333 if (type == t)
15334 {
c906108c
SS
15335 int i;
15336
15337 /* Our own class provides vtbl ptr. */
15338 for (i = TYPE_NFIELDS (t) - 1;
15339 i >= TYPE_N_BASECLASSES (t);
15340 --i)
15341 {
0d5cff50 15342 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15343
1168df01 15344 if (is_vtable_name (fieldname, cu))
c906108c 15345 {
ae6ae975 15346 set_type_vptr_fieldno (type, i);
c906108c
SS
15347 break;
15348 }
15349 }
15350
15351 /* Complain if virtual function table field not found. */
15352 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15353 complaint (_("virtual function table pointer "
3e43a32a 15354 "not found when defining class '%s'"),
e86ca25f 15355 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15356 }
15357 else
15358 {
ae6ae975 15359 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15360 }
15361 }
f6235d4c 15362 else if (cu->producer
61012eef 15363 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15364 {
15365 /* The IBM XLC compiler does not provide direct indication
15366 of the containing type, but the vtable pointer is
15367 always named __vfp. */
15368
15369 int i;
15370
15371 for (i = TYPE_NFIELDS (type) - 1;
15372 i >= TYPE_N_BASECLASSES (type);
15373 --i)
15374 {
15375 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15376 {
ae6ae975
DE
15377 set_type_vptr_fieldno (type, i);
15378 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15379 break;
15380 }
15381 }
15382 }
c906108c 15383 }
98751a41
JK
15384
15385 /* Copy fi.typedef_field_list linked list elements content into the
15386 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15387 if (!fi.typedef_field_list.empty ())
98751a41 15388 {
be2daae6 15389 int count = fi.typedef_field_list.size ();
98751a41 15390
a0d7a4ff 15391 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15392 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15393 = ((struct decl_field *)
be2daae6
TT
15394 TYPE_ALLOC (type,
15395 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15396 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15397
be2daae6
TT
15398 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15399 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15400 }
c767944b 15401
883fd55a
KS
15402 /* Copy fi.nested_types_list linked list elements content into the
15403 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15404 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15405 {
be2daae6 15406 int count = fi.nested_types_list.size ();
883fd55a
KS
15407
15408 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15409 TYPE_NESTED_TYPES_ARRAY (type)
15410 = ((struct decl_field *)
be2daae6
TT
15411 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15412 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15413
be2daae6
TT
15414 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15415 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15416 }
c906108c 15417 }
63d06c5c 15418
bb5ed363 15419 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15420 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15421 cu->rust_unions.push_back (type);
0b92b5bb 15422
90aeadfc
DC
15423 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15424 snapshots) has been known to create a die giving a declaration
15425 for a class that has, as a child, a die giving a definition for a
15426 nested class. So we have to process our children even if the
15427 current die is a declaration. Normally, of course, a declaration
15428 won't have any children at all. */
134d01f1 15429
ca040673
DE
15430 child_die = die->child;
15431
90aeadfc
DC
15432 while (child_die != NULL && child_die->tag)
15433 {
15434 if (child_die->tag == DW_TAG_member
15435 || child_die->tag == DW_TAG_variable
34eaf542
TT
15436 || child_die->tag == DW_TAG_inheritance
15437 || child_die->tag == DW_TAG_template_value_param
15438 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15439 {
90aeadfc 15440 /* Do nothing. */
134d01f1 15441 }
90aeadfc
DC
15442 else
15443 process_die (child_die, cu);
134d01f1 15444
90aeadfc 15445 child_die = sibling_die (child_die);
134d01f1
DJ
15446 }
15447
fa4028e9
JB
15448 /* Do not consider external references. According to the DWARF standard,
15449 these DIEs are identified by the fact that they have no byte_size
15450 attribute, and a declaration attribute. */
15451 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15452 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15453 {
15454 struct symbol *sym = new_symbol (die, type, cu);
15455
15456 if (has_template_parameters)
15457 {
a776957c
TT
15458 struct symtab *symtab;
15459 if (sym != nullptr)
15460 symtab = symbol_symtab (sym);
15461 else if (cu->line_header != nullptr)
15462 {
15463 /* Any related symtab will do. */
15464 symtab
7ba99d21 15465 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15466 }
15467 else
15468 {
15469 symtab = nullptr;
15470 complaint (_("could not find suitable "
15471 "symtab for template parameter"
15472 " - DIE at %s [in module %s]"),
15473 sect_offset_str (die->sect_off),
15474 objfile_name (objfile));
15475 }
15476
15477 if (symtab != nullptr)
15478 {
15479 /* Make sure that the symtab is set on the new symbols.
15480 Even though they don't appear in this symtab directly,
15481 other parts of gdb assume that symbols do, and this is
15482 reasonably true. */
15483 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15484 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15485 }
3e1d3d8c
TT
15486 }
15487 }
134d01f1
DJ
15488}
15489
55426c9d
JB
15490/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15491 update TYPE using some information only available in DIE's children. */
15492
15493static void
15494update_enumeration_type_from_children (struct die_info *die,
15495 struct type *type,
15496 struct dwarf2_cu *cu)
15497{
60f7655a 15498 struct die_info *child_die;
55426c9d
JB
15499 int unsigned_enum = 1;
15500 int flag_enum = 1;
55426c9d 15501
8268c778 15502 auto_obstack obstack;
55426c9d 15503
60f7655a
DE
15504 for (child_die = die->child;
15505 child_die != NULL && child_die->tag;
15506 child_die = sibling_die (child_die))
55426c9d
JB
15507 {
15508 struct attribute *attr;
15509 LONGEST value;
15510 const gdb_byte *bytes;
15511 struct dwarf2_locexpr_baton *baton;
15512 const char *name;
60f7655a 15513
55426c9d
JB
15514 if (child_die->tag != DW_TAG_enumerator)
15515 continue;
15516
15517 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15518 if (attr == NULL)
15519 continue;
15520
15521 name = dwarf2_name (child_die, cu);
15522 if (name == NULL)
15523 name = "<anonymous enumerator>";
15524
15525 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15526 &value, &bytes, &baton);
15527 if (value < 0)
15528 {
15529 unsigned_enum = 0;
15530 flag_enum = 0;
15531 }
55426c9d 15532 else
edd45eb0
SM
15533 {
15534 if (count_one_bits_ll (value) >= 2)
15535 flag_enum = 0;
edd45eb0 15536 }
55426c9d
JB
15537
15538 /* If we already know that the enum type is neither unsigned, nor
15539 a flag type, no need to look at the rest of the enumerates. */
15540 if (!unsigned_enum && !flag_enum)
15541 break;
55426c9d
JB
15542 }
15543
15544 if (unsigned_enum)
15545 TYPE_UNSIGNED (type) = 1;
15546 if (flag_enum)
15547 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15548}
15549
134d01f1
DJ
15550/* Given a DW_AT_enumeration_type die, set its type. We do not
15551 complete the type's fields yet, or create any symbols. */
c906108c 15552
f792889a 15553static struct type *
134d01f1 15554read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15555{
518817b3 15556 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15557 struct type *type;
c906108c 15558 struct attribute *attr;
0114d602 15559 const char *name;
134d01f1 15560
348e048f
DE
15561 /* If the definition of this type lives in .debug_types, read that type.
15562 Don't follow DW_AT_specification though, that will take us back up
15563 the chain and we want to go down. */
45e58e77 15564 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15565 if (attr != nullptr)
348e048f 15566 {
ac9ec31b 15567 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15568
ac9ec31b 15569 /* The type's CU may not be the same as CU.
02142a6c 15570 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15571 return set_die_type (die, type, cu);
15572 }
15573
c906108c
SS
15574 type = alloc_type (objfile);
15575
15576 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15577 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15578 if (name != NULL)
e86ca25f 15579 TYPE_NAME (type) = name;
c906108c 15580
0626fc76
TT
15581 attr = dwarf2_attr (die, DW_AT_type, cu);
15582 if (attr != NULL)
15583 {
15584 struct type *underlying_type = die_type (die, cu);
15585
15586 TYPE_TARGET_TYPE (type) = underlying_type;
15587 }
15588
e142c38c 15589 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15590 if (attr != nullptr)
c906108c
SS
15591 {
15592 TYPE_LENGTH (type) = DW_UNSND (attr);
15593 }
15594 else
15595 {
15596 TYPE_LENGTH (type) = 0;
15597 }
15598
2b4424c3
TT
15599 maybe_set_alignment (cu, die, type);
15600
137033e9
JB
15601 /* The enumeration DIE can be incomplete. In Ada, any type can be
15602 declared as private in the package spec, and then defined only
15603 inside the package body. Such types are known as Taft Amendment
15604 Types. When another package uses such a type, an incomplete DIE
15605 may be generated by the compiler. */
02eb380e 15606 if (die_is_declaration (die, cu))
876cecd0 15607 TYPE_STUB (type) = 1;
02eb380e 15608
0626fc76
TT
15609 /* Finish the creation of this type by using the enum's children.
15610 We must call this even when the underlying type has been provided
15611 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15612 update_enumeration_type_from_children (die, type, cu);
15613
0626fc76
TT
15614 /* If this type has an underlying type that is not a stub, then we
15615 may use its attributes. We always use the "unsigned" attribute
15616 in this situation, because ordinarily we guess whether the type
15617 is unsigned -- but the guess can be wrong and the underlying type
15618 can tell us the reality. However, we defer to a local size
15619 attribute if one exists, because this lets the compiler override
15620 the underlying type if needed. */
15621 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15622 {
15623 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15624 if (TYPE_LENGTH (type) == 0)
15625 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15626 if (TYPE_RAW_ALIGN (type) == 0
15627 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15628 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15629 }
15630
3d567982
TT
15631 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15632
f792889a 15633 return set_die_type (die, type, cu);
134d01f1
DJ
15634}
15635
15636/* Given a pointer to a die which begins an enumeration, process all
15637 the dies that define the members of the enumeration, and create the
15638 symbol for the enumeration type.
15639
15640 NOTE: We reverse the order of the element list. */
15641
15642static void
15643process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15644{
f792889a 15645 struct type *this_type;
134d01f1 15646
f792889a
DJ
15647 this_type = get_die_type (die, cu);
15648 if (this_type == NULL)
15649 this_type = read_enumeration_type (die, cu);
9dc481d3 15650
639d11d3 15651 if (die->child != NULL)
c906108c 15652 {
9dc481d3
DE
15653 struct die_info *child_die;
15654 struct symbol *sym;
43816ebc 15655 std::vector<struct field> fields;
15d034d0 15656 const char *name;
9dc481d3 15657
639d11d3 15658 child_die = die->child;
c906108c
SS
15659 while (child_die && child_die->tag)
15660 {
15661 if (child_die->tag != DW_TAG_enumerator)
15662 {
e7c27a73 15663 process_die (child_die, cu);
c906108c
SS
15664 }
15665 else
15666 {
39cbfefa
DJ
15667 name = dwarf2_name (child_die, cu);
15668 if (name)
c906108c 15669 {
f792889a 15670 sym = new_symbol (child_die, this_type, cu);
c906108c 15671
43816ebc
TT
15672 fields.emplace_back ();
15673 struct field &field = fields.back ();
c906108c 15674
43816ebc
TT
15675 FIELD_NAME (field) = sym->linkage_name ();
15676 FIELD_TYPE (field) = NULL;
15677 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15678 FIELD_BITSIZE (field) = 0;
c906108c
SS
15679 }
15680 }
15681
15682 child_die = sibling_die (child_die);
15683 }
15684
43816ebc 15685 if (!fields.empty ())
c906108c 15686 {
43816ebc 15687 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15688 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15689 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15690 memcpy (TYPE_FIELDS (this_type), fields.data (),
15691 sizeof (struct field) * fields.size ());
c906108c 15692 }
c906108c 15693 }
134d01f1 15694
6c83ed52
TT
15695 /* If we are reading an enum from a .debug_types unit, and the enum
15696 is a declaration, and the enum is not the signatured type in the
15697 unit, then we do not want to add a symbol for it. Adding a
15698 symbol would in some cases obscure the true definition of the
15699 enum, giving users an incomplete type when the definition is
15700 actually available. Note that we do not want to do this for all
15701 enums which are just declarations, because C++0x allows forward
15702 enum declarations. */
3019eac3 15703 if (cu->per_cu->is_debug_types
6c83ed52
TT
15704 && die_is_declaration (die, cu))
15705 {
52dc124a 15706 struct signatured_type *sig_type;
6c83ed52 15707
c0f78cd4 15708 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15709 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15710 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15711 return;
15712 }
15713
f792889a 15714 new_symbol (die, this_type, cu);
c906108c
SS
15715}
15716
15717/* Extract all information from a DW_TAG_array_type DIE and put it in
15718 the DIE's type field. For now, this only handles one dimensional
15719 arrays. */
15720
f792889a 15721static struct type *
e7c27a73 15722read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15723{
518817b3 15724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15725 struct die_info *child_die;
7e314c57 15726 struct type *type;
c906108c 15727 struct type *element_type, *range_type, *index_type;
c906108c 15728 struct attribute *attr;
15d034d0 15729 const char *name;
a405673c 15730 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15731 unsigned int bit_stride = 0;
c906108c 15732
e7c27a73 15733 element_type = die_type (die, cu);
c906108c 15734
7e314c57
JK
15735 /* The die_type call above may have already set the type for this DIE. */
15736 type = get_die_type (die, cu);
15737 if (type)
15738 return type;
15739
dc53a7ad
JB
15740 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15741 if (attr != NULL)
a405673c
JB
15742 {
15743 int stride_ok;
09ba997f 15744 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15745
15746 byte_stride_prop
15747 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15748 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15749 prop_type);
a405673c
JB
15750 if (!stride_ok)
15751 {
b98664d3 15752 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15753 " - DIE at %s [in module %s]"),
15754 sect_offset_str (die->sect_off),
518817b3 15755 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15756 /* Ignore this attribute. We will likely not be able to print
15757 arrays of this type correctly, but there is little we can do
15758 to help if we cannot read the attribute's value. */
15759 byte_stride_prop = NULL;
15760 }
15761 }
dc53a7ad
JB
15762
15763 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15764 if (attr != NULL)
15765 bit_stride = DW_UNSND (attr);
15766
c906108c
SS
15767 /* Irix 6.2 native cc creates array types without children for
15768 arrays with unspecified length. */
639d11d3 15769 if (die->child == NULL)
c906108c 15770 {
46bf5051 15771 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15772 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15773 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15774 byte_stride_prop, bit_stride);
f792889a 15775 return set_die_type (die, type, cu);
c906108c
SS
15776 }
15777
791afaa2 15778 std::vector<struct type *> range_types;
639d11d3 15779 child_die = die->child;
c906108c
SS
15780 while (child_die && child_die->tag)
15781 {
15782 if (child_die->tag == DW_TAG_subrange_type)
15783 {
f792889a 15784 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15785
f792889a 15786 if (child_type != NULL)
a02abb62 15787 {
0963b4bd
MS
15788 /* The range type was succesfully read. Save it for the
15789 array type creation. */
791afaa2 15790 range_types.push_back (child_type);
a02abb62 15791 }
c906108c
SS
15792 }
15793 child_die = sibling_die (child_die);
15794 }
15795
15796 /* Dwarf2 dimensions are output from left to right, create the
15797 necessary array types in backwards order. */
7ca2d3a3 15798
c906108c 15799 type = element_type;
7ca2d3a3
DL
15800
15801 if (read_array_order (die, cu) == DW_ORD_col_major)
15802 {
15803 int i = 0;
9a619af0 15804
791afaa2 15805 while (i < range_types.size ())
dc53a7ad 15806 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15807 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15808 }
15809 else
15810 {
791afaa2 15811 size_t ndim = range_types.size ();
7ca2d3a3 15812 while (ndim-- > 0)
dc53a7ad 15813 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15814 byte_stride_prop, bit_stride);
7ca2d3a3 15815 }
c906108c 15816
f5f8a009
EZ
15817 /* Understand Dwarf2 support for vector types (like they occur on
15818 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15819 array type. This is not part of the Dwarf2/3 standard yet, but a
15820 custom vendor extension. The main difference between a regular
15821 array and the vector variant is that vectors are passed by value
15822 to functions. */
e142c38c 15823 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15824 if (attr != nullptr)
ea37ba09 15825 make_vector_type (type);
f5f8a009 15826
dbc98a8b
KW
15827 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15828 implementation may choose to implement triple vectors using this
15829 attribute. */
15830 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15831 if (attr != nullptr)
dbc98a8b
KW
15832 {
15833 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15834 TYPE_LENGTH (type) = DW_UNSND (attr);
15835 else
b98664d3 15836 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15837 "than the total size of elements"));
dbc98a8b
KW
15838 }
15839
39cbfefa
DJ
15840 name = dwarf2_name (die, cu);
15841 if (name)
15842 TYPE_NAME (type) = name;
6e70227d 15843
2b4424c3
TT
15844 maybe_set_alignment (cu, die, type);
15845
0963b4bd 15846 /* Install the type in the die. */
7e314c57
JK
15847 set_die_type (die, type, cu);
15848
15849 /* set_die_type should be already done. */
b4ba55a1
JB
15850 set_descriptive_type (type, die, cu);
15851
7e314c57 15852 return type;
c906108c
SS
15853}
15854
7ca2d3a3 15855static enum dwarf_array_dim_ordering
6e70227d 15856read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15857{
15858 struct attribute *attr;
15859
15860 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15861
435d3d88 15862 if (attr != nullptr)
aead7601 15863 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15864
0963b4bd
MS
15865 /* GNU F77 is a special case, as at 08/2004 array type info is the
15866 opposite order to the dwarf2 specification, but data is still
15867 laid out as per normal fortran.
7ca2d3a3 15868
0963b4bd
MS
15869 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15870 version checking. */
7ca2d3a3 15871
905e0470
PM
15872 if (cu->language == language_fortran
15873 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15874 {
15875 return DW_ORD_row_major;
15876 }
15877
6e70227d 15878 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15879 {
15880 case array_column_major:
15881 return DW_ORD_col_major;
15882 case array_row_major:
15883 default:
15884 return DW_ORD_row_major;
15885 };
15886}
15887
72019c9c 15888/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15889 the DIE's type field. */
72019c9c 15890
f792889a 15891static struct type *
72019c9c
GM
15892read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15893{
7e314c57
JK
15894 struct type *domain_type, *set_type;
15895 struct attribute *attr;
f792889a 15896
7e314c57
JK
15897 domain_type = die_type (die, cu);
15898
15899 /* The die_type call above may have already set the type for this DIE. */
15900 set_type = get_die_type (die, cu);
15901 if (set_type)
15902 return set_type;
15903
15904 set_type = create_set_type (NULL, domain_type);
15905
15906 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15907 if (attr != nullptr)
d09039dd 15908 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15909
2b4424c3
TT
15910 maybe_set_alignment (cu, die, set_type);
15911
f792889a 15912 return set_die_type (die, set_type, cu);
72019c9c 15913}
7ca2d3a3 15914
0971de02
TT
15915/* A helper for read_common_block that creates a locexpr baton.
15916 SYM is the symbol which we are marking as computed.
15917 COMMON_DIE is the DIE for the common block.
15918 COMMON_LOC is the location expression attribute for the common
15919 block itself.
15920 MEMBER_LOC is the location expression attribute for the particular
15921 member of the common block that we are processing.
15922 CU is the CU from which the above come. */
15923
15924static void
15925mark_common_block_symbol_computed (struct symbol *sym,
15926 struct die_info *common_die,
15927 struct attribute *common_loc,
15928 struct attribute *member_loc,
15929 struct dwarf2_cu *cu)
15930{
518817b3
SM
15931 struct dwarf2_per_objfile *dwarf2_per_objfile
15932 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15933 struct objfile *objfile = dwarf2_per_objfile->objfile;
15934 struct dwarf2_locexpr_baton *baton;
15935 gdb_byte *ptr;
15936 unsigned int cu_off;
15937 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15938 LONGEST offset = 0;
15939
15940 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15941 gdb_assert (common_loc->form_is_block ());
15942 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15943 || member_loc->form_is_constant ());
0971de02 15944
8d749320 15945 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15946 baton->per_cu = cu->per_cu;
15947 gdb_assert (baton->per_cu);
15948
15949 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15950
cd6c91b4 15951 if (member_loc->form_is_constant ())
0971de02
TT
15952 {
15953 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15954 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15955 }
15956 else
15957 baton->size += DW_BLOCK (member_loc)->size;
15958
224c3ddb 15959 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15960 baton->data = ptr;
15961
15962 *ptr++ = DW_OP_call4;
9c541725 15963 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15964 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15965 ptr += 4;
15966
cd6c91b4 15967 if (member_loc->form_is_constant ())
0971de02
TT
15968 {
15969 *ptr++ = DW_OP_addr;
15970 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15971 ptr += cu->header.addr_size;
15972 }
15973 else
15974 {
15975 /* We have to copy the data here, because DW_OP_call4 will only
15976 use a DW_AT_location attribute. */
15977 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15978 ptr += DW_BLOCK (member_loc)->size;
15979 }
15980
15981 *ptr++ = DW_OP_plus;
15982 gdb_assert (ptr - baton->data == baton->size);
15983
0971de02 15984 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15985 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15986}
15987
4357ac6c
TT
15988/* Create appropriate locally-scoped variables for all the
15989 DW_TAG_common_block entries. Also create a struct common_block
15990 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 15991 is used to separate the common blocks name namespace from regular
4357ac6c 15992 variable names. */
c906108c
SS
15993
15994static void
e7c27a73 15995read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15996{
0971de02
TT
15997 struct attribute *attr;
15998
15999 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16000 if (attr != nullptr)
0971de02
TT
16001 {
16002 /* Support the .debug_loc offsets. */
4fc6c0d5 16003 if (attr->form_is_block ())
0971de02
TT
16004 {
16005 /* Ok. */
16006 }
cd6c91b4 16007 else if (attr->form_is_section_offset ())
0971de02
TT
16008 {
16009 dwarf2_complex_location_expr_complaint ();
16010 attr = NULL;
16011 }
16012 else
16013 {
16014 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16015 "common block member");
16016 attr = NULL;
16017 }
16018 }
16019
639d11d3 16020 if (die->child != NULL)
c906108c 16021 {
518817b3 16022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16023 struct die_info *child_die;
16024 size_t n_entries = 0, size;
16025 struct common_block *common_block;
16026 struct symbol *sym;
74ac6d43 16027
4357ac6c
TT
16028 for (child_die = die->child;
16029 child_die && child_die->tag;
16030 child_die = sibling_die (child_die))
16031 ++n_entries;
16032
16033 size = (sizeof (struct common_block)
16034 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16035 common_block
16036 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16037 size);
4357ac6c
TT
16038 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16039 common_block->n_entries = 0;
16040
16041 for (child_die = die->child;
16042 child_die && child_die->tag;
16043 child_die = sibling_die (child_die))
16044 {
16045 /* Create the symbol in the DW_TAG_common_block block in the current
16046 symbol scope. */
e7c27a73 16047 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16048 if (sym != NULL)
16049 {
16050 struct attribute *member_loc;
16051
16052 common_block->contents[common_block->n_entries++] = sym;
16053
16054 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16055 cu);
16056 if (member_loc)
16057 {
16058 /* GDB has handled this for a long time, but it is
16059 not specified by DWARF. It seems to have been
16060 emitted by gfortran at least as recently as:
16061 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16062 complaint (_("Variable in common block has "
0971de02 16063 "DW_AT_data_member_location "
9d8780f0
SM
16064 "- DIE at %s [in module %s]"),
16065 sect_offset_str (child_die->sect_off),
518817b3 16066 objfile_name (objfile));
0971de02 16067
cd6c91b4 16068 if (member_loc->form_is_section_offset ())
0971de02 16069 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16070 else if (member_loc->form_is_constant ()
4fc6c0d5 16071 || member_loc->form_is_block ())
0971de02 16072 {
435d3d88 16073 if (attr != nullptr)
0971de02
TT
16074 mark_common_block_symbol_computed (sym, die, attr,
16075 member_loc, cu);
16076 }
16077 else
16078 dwarf2_complex_location_expr_complaint ();
16079 }
16080 }
c906108c 16081 }
4357ac6c
TT
16082
16083 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16084 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16085 }
16086}
16087
0114d602 16088/* Create a type for a C++ namespace. */
d9fa45fe 16089
0114d602
DJ
16090static struct type *
16091read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16092{
518817b3 16093 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16094 const char *previous_prefix, *name;
9219021c 16095 int is_anonymous;
0114d602
DJ
16096 struct type *type;
16097
16098 /* For extensions, reuse the type of the original namespace. */
16099 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16100 {
16101 struct die_info *ext_die;
16102 struct dwarf2_cu *ext_cu = cu;
9a619af0 16103
0114d602
DJ
16104 ext_die = dwarf2_extension (die, &ext_cu);
16105 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16106
16107 /* EXT_CU may not be the same as CU.
02142a6c 16108 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16109 return set_die_type (die, type, cu);
16110 }
9219021c 16111
e142c38c 16112 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16113
16114 /* Now build the name of the current namespace. */
16115
0114d602
DJ
16116 previous_prefix = determine_prefix (die, cu);
16117 if (previous_prefix[0] != '\0')
16118 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16119 previous_prefix, name, 0, cu);
0114d602
DJ
16120
16121 /* Create the type. */
19f392bc 16122 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16123
60531b24 16124 return set_die_type (die, type, cu);
0114d602
DJ
16125}
16126
22cee43f 16127/* Read a namespace scope. */
0114d602
DJ
16128
16129static void
16130read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16131{
518817b3 16132 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16133 int is_anonymous;
9219021c 16134
5c4e30ca
DC
16135 /* Add a symbol associated to this if we haven't seen the namespace
16136 before. Also, add a using directive if it's an anonymous
16137 namespace. */
9219021c 16138
f2f0e013 16139 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16140 {
16141 struct type *type;
16142
0114d602 16143 type = read_type_die (die, cu);
e7c27a73 16144 new_symbol (die, type, cu);
5c4e30ca 16145
e8e80198 16146 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16147 if (is_anonymous)
0114d602
DJ
16148 {
16149 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16150
eb1e02fd 16151 std::vector<const char *> excludes;
804d2729 16152 add_using_directive (using_directives (cu),
22cee43f 16153 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16154 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16155 }
5c4e30ca 16156 }
9219021c 16157
639d11d3 16158 if (die->child != NULL)
d9fa45fe 16159 {
639d11d3 16160 struct die_info *child_die = die->child;
6e70227d 16161
d9fa45fe
DC
16162 while (child_die && child_die->tag)
16163 {
e7c27a73 16164 process_die (child_die, cu);
d9fa45fe
DC
16165 child_die = sibling_die (child_die);
16166 }
16167 }
38d518c9
EZ
16168}
16169
f55ee35c
JK
16170/* Read a Fortran module as type. This DIE can be only a declaration used for
16171 imported module. Still we need that type as local Fortran "use ... only"
16172 declaration imports depend on the created type in determine_prefix. */
16173
16174static struct type *
16175read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16176{
518817b3 16177 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16178 const char *module_name;
f55ee35c
JK
16179 struct type *type;
16180
16181 module_name = dwarf2_name (die, cu);
19f392bc 16182 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16183
f55ee35c
JK
16184 return set_die_type (die, type, cu);
16185}
16186
5d7cb8df
JK
16187/* Read a Fortran module. */
16188
16189static void
16190read_module (struct die_info *die, struct dwarf2_cu *cu)
16191{
16192 struct die_info *child_die = die->child;
530e8392
KB
16193 struct type *type;
16194
16195 type = read_type_die (die, cu);
16196 new_symbol (die, type, cu);
5d7cb8df 16197
5d7cb8df
JK
16198 while (child_die && child_die->tag)
16199 {
16200 process_die (child_die, cu);
16201 child_die = sibling_die (child_die);
16202 }
16203}
16204
38d518c9
EZ
16205/* Return the name of the namespace represented by DIE. Set
16206 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16207 namespace. */
16208
16209static const char *
e142c38c 16210namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16211{
16212 struct die_info *current_die;
16213 const char *name = NULL;
16214
16215 /* Loop through the extensions until we find a name. */
16216
16217 for (current_die = die;
16218 current_die != NULL;
f2f0e013 16219 current_die = dwarf2_extension (die, &cu))
38d518c9 16220 {
96553a0c
DE
16221 /* We don't use dwarf2_name here so that we can detect the absence
16222 of a name -> anonymous namespace. */
7d45c7c3 16223 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16224
38d518c9
EZ
16225 if (name != NULL)
16226 break;
16227 }
16228
16229 /* Is it an anonymous namespace? */
16230
16231 *is_anonymous = (name == NULL);
16232 if (*is_anonymous)
2b1dbab0 16233 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16234
16235 return name;
d9fa45fe
DC
16236}
16237
c906108c
SS
16238/* Extract all information from a DW_TAG_pointer_type DIE and add to
16239 the user defined type vector. */
16240
f792889a 16241static struct type *
e7c27a73 16242read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16243{
518817b3
SM
16244 struct gdbarch *gdbarch
16245 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16246 struct comp_unit_head *cu_header = &cu->header;
c906108c 16247 struct type *type;
8b2dbe47
KB
16248 struct attribute *attr_byte_size;
16249 struct attribute *attr_address_class;
16250 int byte_size, addr_class;
7e314c57
JK
16251 struct type *target_type;
16252
16253 target_type = die_type (die, cu);
c906108c 16254
7e314c57
JK
16255 /* The die_type call above may have already set the type for this DIE. */
16256 type = get_die_type (die, cu);
16257 if (type)
16258 return type;
16259
16260 type = lookup_pointer_type (target_type);
8b2dbe47 16261
e142c38c 16262 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16263 if (attr_byte_size)
16264 byte_size = DW_UNSND (attr_byte_size);
c906108c 16265 else
8b2dbe47
KB
16266 byte_size = cu_header->addr_size;
16267
e142c38c 16268 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16269 if (attr_address_class)
16270 addr_class = DW_UNSND (attr_address_class);
16271 else
16272 addr_class = DW_ADDR_none;
16273
2b4424c3
TT
16274 ULONGEST alignment = get_alignment (cu, die);
16275
16276 /* If the pointer size, alignment, or address class is different
16277 than the default, create a type variant marked as such and set
16278 the length accordingly. */
16279 if (TYPE_LENGTH (type) != byte_size
16280 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16281 && alignment != TYPE_RAW_ALIGN (type))
16282 || addr_class != DW_ADDR_none)
c906108c 16283 {
5e2b427d 16284 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16285 {
16286 int type_flags;
16287
849957d9 16288 type_flags = gdbarch_address_class_type_flags
5e2b427d 16289 (gdbarch, byte_size, addr_class);
876cecd0
TT
16290 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16291 == 0);
8b2dbe47
KB
16292 type = make_type_with_address_space (type, type_flags);
16293 }
16294 else if (TYPE_LENGTH (type) != byte_size)
16295 {
b98664d3 16296 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16297 }
2b4424c3
TT
16298 else if (TYPE_RAW_ALIGN (type) != alignment)
16299 {
b98664d3 16300 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16301 " - DIE at %s [in module %s]"),
16302 sect_offset_str (die->sect_off),
16303 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16304 }
6e70227d 16305 else
9a619af0
MS
16306 {
16307 /* Should we also complain about unhandled address classes? */
16308 }
c906108c 16309 }
8b2dbe47
KB
16310
16311 TYPE_LENGTH (type) = byte_size;
2b4424c3 16312 set_type_align (type, alignment);
f792889a 16313 return set_die_type (die, type, cu);
c906108c
SS
16314}
16315
16316/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16317 the user defined type vector. */
16318
f792889a 16319static struct type *
e7c27a73 16320read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16321{
16322 struct type *type;
16323 struct type *to_type;
16324 struct type *domain;
16325
e7c27a73
DJ
16326 to_type = die_type (die, cu);
16327 domain = die_containing_type (die, cu);
0d5de010 16328
7e314c57
JK
16329 /* The calls above may have already set the type for this DIE. */
16330 type = get_die_type (die, cu);
16331 if (type)
16332 return type;
16333
0d5de010
DJ
16334 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16335 type = lookup_methodptr_type (to_type);
7078baeb
TT
16336 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16337 {
518817b3
SM
16338 struct type *new_type
16339 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16340
16341 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16342 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16343 TYPE_VARARGS (to_type));
16344 type = lookup_methodptr_type (new_type);
16345 }
0d5de010
DJ
16346 else
16347 type = lookup_memberptr_type (to_type, domain);
c906108c 16348
f792889a 16349 return set_die_type (die, type, cu);
c906108c
SS
16350}
16351
4297a3f0 16352/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16353 the user defined type vector. */
16354
f792889a 16355static struct type *
4297a3f0
AV
16356read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16357 enum type_code refcode)
c906108c 16358{
e7c27a73 16359 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16360 struct type *type, *target_type;
c906108c
SS
16361 struct attribute *attr;
16362
4297a3f0
AV
16363 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16364
7e314c57
JK
16365 target_type = die_type (die, cu);
16366
16367 /* The die_type call above may have already set the type for this DIE. */
16368 type = get_die_type (die, cu);
16369 if (type)
16370 return type;
16371
4297a3f0 16372 type = lookup_reference_type (target_type, refcode);
e142c38c 16373 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16374 if (attr != nullptr)
c906108c
SS
16375 {
16376 TYPE_LENGTH (type) = DW_UNSND (attr);
16377 }
16378 else
16379 {
107d2387 16380 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16381 }
2b4424c3 16382 maybe_set_alignment (cu, die, type);
f792889a 16383 return set_die_type (die, type, cu);
c906108c
SS
16384}
16385
cf363f18
MW
16386/* Add the given cv-qualifiers to the element type of the array. GCC
16387 outputs DWARF type qualifiers that apply to an array, not the
16388 element type. But GDB relies on the array element type to carry
16389 the cv-qualifiers. This mimics section 6.7.3 of the C99
16390 specification. */
16391
16392static struct type *
16393add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16394 struct type *base_type, int cnst, int voltl)
16395{
16396 struct type *el_type, *inner_array;
16397
16398 base_type = copy_type (base_type);
16399 inner_array = base_type;
16400
16401 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16402 {
16403 TYPE_TARGET_TYPE (inner_array) =
16404 copy_type (TYPE_TARGET_TYPE (inner_array));
16405 inner_array = TYPE_TARGET_TYPE (inner_array);
16406 }
16407
16408 el_type = TYPE_TARGET_TYPE (inner_array);
16409 cnst |= TYPE_CONST (el_type);
16410 voltl |= TYPE_VOLATILE (el_type);
16411 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16412
16413 return set_die_type (die, base_type, cu);
16414}
16415
f792889a 16416static struct type *
e7c27a73 16417read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16418{
f792889a 16419 struct type *base_type, *cv_type;
c906108c 16420
e7c27a73 16421 base_type = die_type (die, cu);
7e314c57
JK
16422
16423 /* The die_type call above may have already set the type for this DIE. */
16424 cv_type = get_die_type (die, cu);
16425 if (cv_type)
16426 return cv_type;
16427
2f608a3a
KW
16428 /* In case the const qualifier is applied to an array type, the element type
16429 is so qualified, not the array type (section 6.7.3 of C99). */
16430 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16431 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16432
f792889a
DJ
16433 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16434 return set_die_type (die, cv_type, cu);
c906108c
SS
16435}
16436
f792889a 16437static struct type *
e7c27a73 16438read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16439{
f792889a 16440 struct type *base_type, *cv_type;
c906108c 16441
e7c27a73 16442 base_type = die_type (die, cu);
7e314c57
JK
16443
16444 /* The die_type call above may have already set the type for this DIE. */
16445 cv_type = get_die_type (die, cu);
16446 if (cv_type)
16447 return cv_type;
16448
cf363f18
MW
16449 /* In case the volatile qualifier is applied to an array type, the
16450 element type is so qualified, not the array type (section 6.7.3
16451 of C99). */
16452 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16453 return add_array_cv_type (die, cu, base_type, 0, 1);
16454
f792889a
DJ
16455 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16456 return set_die_type (die, cv_type, cu);
c906108c
SS
16457}
16458
06d66ee9
TT
16459/* Handle DW_TAG_restrict_type. */
16460
16461static struct type *
16462read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16463{
16464 struct type *base_type, *cv_type;
16465
16466 base_type = die_type (die, cu);
16467
16468 /* The die_type call above may have already set the type for this DIE. */
16469 cv_type = get_die_type (die, cu);
16470 if (cv_type)
16471 return cv_type;
16472
16473 cv_type = make_restrict_type (base_type);
16474 return set_die_type (die, cv_type, cu);
16475}
16476
a2c2acaf
MW
16477/* Handle DW_TAG_atomic_type. */
16478
16479static struct type *
16480read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16481{
16482 struct type *base_type, *cv_type;
16483
16484 base_type = die_type (die, cu);
16485
16486 /* The die_type call above may have already set the type for this DIE. */
16487 cv_type = get_die_type (die, cu);
16488 if (cv_type)
16489 return cv_type;
16490
16491 cv_type = make_atomic_type (base_type);
16492 return set_die_type (die, cv_type, cu);
16493}
16494
c906108c
SS
16495/* Extract all information from a DW_TAG_string_type DIE and add to
16496 the user defined type vector. It isn't really a user defined type,
16497 but it behaves like one, with other DIE's using an AT_user_def_type
16498 attribute to reference it. */
16499
f792889a 16500static struct type *
e7c27a73 16501read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16502{
518817b3 16503 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16504 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16505 struct type *type, *range_type, *index_type, *char_type;
16506 struct attribute *attr;
216a7e6b
AB
16507 struct dynamic_prop prop;
16508 bool length_is_constant = true;
16509 LONGEST length;
16510
16511 /* There are a couple of places where bit sizes might be made use of
16512 when parsing a DW_TAG_string_type, however, no producer that we know
16513 of make use of these. Handling bit sizes that are a multiple of the
16514 byte size is easy enough, but what about other bit sizes? Lets deal
16515 with that problem when we have to. Warn about these attributes being
16516 unsupported, then parse the type and ignore them like we always
16517 have. */
16518 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16519 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16520 {
16521 static bool warning_printed = false;
16522 if (!warning_printed)
16523 {
16524 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16525 "currently supported on DW_TAG_string_type."));
16526 warning_printed = true;
16527 }
16528 }
c906108c 16529
e142c38c 16530 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16531 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16532 {
16533 /* The string length describes the location at which the length of
16534 the string can be found. The size of the length field can be
16535 specified with one of the attributes below. */
16536 struct type *prop_type;
16537 struct attribute *len
16538 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16539 if (len == nullptr)
16540 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16541 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16542 {
16543 /* Pass 0 as the default as we know this attribute is constant
16544 and the default value will not be returned. */
16545 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
09ba997f 16546 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16547 }
16548 else
16549 {
16550 /* If the size is not specified then we assume it is the size of
16551 an address on this target. */
09ba997f 16552 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16553 }
16554
16555 /* Convert the attribute into a dynamic property. */
16556 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16557 length = 1;
16558 else
16559 length_is_constant = false;
16560 }
16561 else if (attr != nullptr)
16562 {
16563 /* This DW_AT_string_length just contains the length with no
16564 indirection. There's no need to create a dynamic property in this
16565 case. Pass 0 for the default value as we know it will not be
16566 returned in this case. */
16567 length = dwarf2_get_attr_constant_value (attr, 0);
16568 }
16569 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16570 {
216a7e6b
AB
16571 /* We don't currently support non-constant byte sizes for strings. */
16572 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16573 }
16574 else
16575 {
216a7e6b
AB
16576 /* Use 1 as a fallback length if we have nothing else. */
16577 length = 1;
c906108c 16578 }
6ccb9162 16579
46bf5051 16580 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16581 if (length_is_constant)
16582 range_type = create_static_range_type (NULL, index_type, 1, length);
16583 else
16584 {
16585 struct dynamic_prop low_bound;
16586
16587 low_bound.kind = PROP_CONST;
16588 low_bound.data.const_val = 1;
16589 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16590 }
3b7538c0
UW
16591 char_type = language_string_char_type (cu->language_defn, gdbarch);
16592 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16593
f792889a 16594 return set_die_type (die, type, cu);
c906108c
SS
16595}
16596
4d804846
JB
16597/* Assuming that DIE corresponds to a function, returns nonzero
16598 if the function is prototyped. */
16599
16600static int
16601prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16602{
16603 struct attribute *attr;
16604
16605 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16606 if (attr && (DW_UNSND (attr) != 0))
16607 return 1;
16608
16609 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16610 is only meaningful for C, but the concept also extends to other
4d804846
JB
16611 languages that allow unprototyped functions (Eg: Objective C).
16612 For all other languages, assume that functions are always
16613 prototyped. */
16614 if (cu->language != language_c
16615 && cu->language != language_objc
16616 && cu->language != language_opencl)
16617 return 1;
16618
16619 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16620 prototyped and unprototyped functions; default to prototyped,
16621 since that is more common in modern code (and RealView warns
16622 about unprototyped functions). */
16623 if (producer_is_realview (cu->producer))
16624 return 1;
16625
16626 return 0;
16627}
16628
c906108c
SS
16629/* Handle DIES due to C code like:
16630
16631 struct foo
c5aa993b
JM
16632 {
16633 int (*funcp)(int a, long l);
16634 int b;
16635 };
c906108c 16636
0963b4bd 16637 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16638
f792889a 16639static struct type *
e7c27a73 16640read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16641{
518817b3 16642 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16643 struct type *type; /* Type that this function returns. */
16644 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16645 struct attribute *attr;
16646
e7c27a73 16647 type = die_type (die, cu);
7e314c57
JK
16648
16649 /* The die_type call above may have already set the type for this DIE. */
16650 ftype = get_die_type (die, cu);
16651 if (ftype)
16652 return ftype;
16653
0c8b41f1 16654 ftype = lookup_function_type (type);
c906108c 16655
4d804846 16656 if (prototyped_function_p (die, cu))
a6c727b2 16657 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16658
c055b101
CV
16659 /* Store the calling convention in the type if it's available in
16660 the subroutine die. Otherwise set the calling convention to
16661 the default value DW_CC_normal. */
16662 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16663 if (attr != nullptr
16664 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16665 TYPE_CALLING_CONVENTION (ftype)
16666 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16667 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16668 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16669 else
16670 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16671
743649fd
MW
16672 /* Record whether the function returns normally to its caller or not
16673 if the DWARF producer set that information. */
16674 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16675 if (attr && (DW_UNSND (attr) != 0))
16676 TYPE_NO_RETURN (ftype) = 1;
16677
76c10ea2
GM
16678 /* We need to add the subroutine type to the die immediately so
16679 we don't infinitely recurse when dealing with parameters
0963b4bd 16680 declared as the same subroutine type. */
76c10ea2 16681 set_die_type (die, ftype, cu);
6e70227d 16682
639d11d3 16683 if (die->child != NULL)
c906108c 16684 {
bb5ed363 16685 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16686 struct die_info *child_die;
8072405b 16687 int nparams, iparams;
c906108c
SS
16688
16689 /* Count the number of parameters.
16690 FIXME: GDB currently ignores vararg functions, but knows about
16691 vararg member functions. */
8072405b 16692 nparams = 0;
639d11d3 16693 child_die = die->child;
c906108c
SS
16694 while (child_die && child_die->tag)
16695 {
16696 if (child_die->tag == DW_TAG_formal_parameter)
16697 nparams++;
16698 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16699 TYPE_VARARGS (ftype) = 1;
c906108c
SS
16700 child_die = sibling_die (child_die);
16701 }
16702
16703 /* Allocate storage for parameters and fill them in. */
16704 TYPE_NFIELDS (ftype) = nparams;
16705 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16706 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16707
8072405b
JK
16708 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16709 even if we error out during the parameters reading below. */
16710 for (iparams = 0; iparams < nparams; iparams++)
16711 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16712
16713 iparams = 0;
639d11d3 16714 child_die = die->child;
c906108c
SS
16715 while (child_die && child_die->tag)
16716 {
16717 if (child_die->tag == DW_TAG_formal_parameter)
16718 {
3ce3b1ba
PA
16719 struct type *arg_type;
16720
16721 /* DWARF version 2 has no clean way to discern C++
16722 static and non-static member functions. G++ helps
16723 GDB by marking the first parameter for non-static
16724 member functions (which is the this pointer) as
16725 artificial. We pass this information to
16726 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16727
16728 DWARF version 3 added DW_AT_object_pointer, which GCC
16729 4.5 does not yet generate. */
e142c38c 16730 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16731 if (attr != nullptr)
c906108c
SS
16732 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16733 else
9c37b5ae 16734 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16735 arg_type = die_type (child_die, cu);
16736
16737 /* RealView does not mark THIS as const, which the testsuite
16738 expects. GCC marks THIS as const in method definitions,
16739 but not in the class specifications (GCC PR 43053). */
16740 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16741 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16742 {
16743 int is_this = 0;
16744 struct dwarf2_cu *arg_cu = cu;
16745 const char *name = dwarf2_name (child_die, cu);
16746
16747 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16748 if (attr != nullptr)
3ce3b1ba
PA
16749 {
16750 /* If the compiler emits this, use it. */
16751 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16752 is_this = 1;
16753 }
16754 else if (name && strcmp (name, "this") == 0)
16755 /* Function definitions will have the argument names. */
16756 is_this = 1;
16757 else if (name == NULL && iparams == 0)
16758 /* Declarations may not have the names, so like
16759 elsewhere in GDB, assume an artificial first
16760 argument is "this". */
16761 is_this = 1;
16762
16763 if (is_this)
16764 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16765 arg_type, 0);
16766 }
16767
16768 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16769 iparams++;
16770 }
16771 child_die = sibling_die (child_die);
16772 }
16773 }
16774
76c10ea2 16775 return ftype;
c906108c
SS
16776}
16777
f792889a 16778static struct type *
e7c27a73 16779read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16780{
518817b3 16781 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16782 const char *name = NULL;
3c8e0968 16783 struct type *this_type, *target_type;
c906108c 16784
94af9270 16785 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16786 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16787 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16788 set_die_type (die, this_type, cu);
3c8e0968
DE
16789 target_type = die_type (die, cu);
16790 if (target_type != this_type)
16791 TYPE_TARGET_TYPE (this_type) = target_type;
16792 else
16793 {
16794 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16795 spec and cause infinite loops in GDB. */
b98664d3 16796 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16797 "- DIE at %s [in module %s]"),
16798 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16799 TYPE_TARGET_TYPE (this_type) = NULL;
16800 }
f792889a 16801 return this_type;
c906108c
SS
16802}
16803
9b790ce7
UW
16804/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16805 (which may be different from NAME) to the architecture back-end to allow
16806 it to guess the correct format if necessary. */
16807
16808static struct type *
16809dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16810 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16811{
16812 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16813 const struct floatformat **format;
16814 struct type *type;
16815
16816 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16817 if (format)
103a685e 16818 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16819 else
77b7c781 16820 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16821
16822 return type;
16823}
16824
eb77c9df
AB
16825/* Allocate an integer type of size BITS and name NAME. */
16826
16827static struct type *
16828dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16829 int bits, int unsigned_p, const char *name)
16830{
16831 struct type *type;
16832
16833 /* Versions of Intel's C Compiler generate an integer type called "void"
16834 instead of using DW_TAG_unspecified_type. This has been seen on
16835 at least versions 14, 17, and 18. */
35ee2dc2
AB
16836 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16837 && strcmp (name, "void") == 0)
eb77c9df
AB
16838 type = objfile_type (objfile)->builtin_void;
16839 else
16840 type = init_integer_type (objfile, bits, unsigned_p, name);
16841
16842 return type;
16843}
16844
8bdc1658
AB
16845/* Initialise and return a floating point type of size BITS suitable for
16846 use as a component of a complex number. The NAME_HINT is passed through
16847 when initialising the floating point type and is the name of the complex
16848 type.
16849
16850 As DWARF doesn't currently provide an explicit name for the components
16851 of a complex number, but it can be helpful to have these components
16852 named, we try to select a suitable name based on the size of the
16853 component. */
16854static struct type *
16855dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16856 struct objfile *objfile,
103a685e
TT
16857 int bits, const char *name_hint,
16858 enum bfd_endian byte_order)
8bdc1658
AB
16859{
16860 gdbarch *gdbarch = get_objfile_arch (objfile);
16861 struct type *tt = nullptr;
16862
35add35e
AB
16863 /* Try to find a suitable floating point builtin type of size BITS.
16864 We're going to use the name of this type as the name for the complex
16865 target type that we are about to create. */
1db455a7 16866 switch (cu->language)
8bdc1658 16867 {
1db455a7
AB
16868 case language_fortran:
16869 switch (bits)
16870 {
16871 case 32:
16872 tt = builtin_f_type (gdbarch)->builtin_real;
16873 break;
16874 case 64:
16875 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16876 break;
16877 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16878 case 128:
16879 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16880 break;
16881 }
8bdc1658 16882 break;
1db455a7
AB
16883 default:
16884 switch (bits)
16885 {
16886 case 32:
16887 tt = builtin_type (gdbarch)->builtin_float;
16888 break;
16889 case 64:
16890 tt = builtin_type (gdbarch)->builtin_double;
16891 break;
16892 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16893 case 128:
16894 tt = builtin_type (gdbarch)->builtin_long_double;
16895 break;
16896 }
8bdc1658
AB
16897 break;
16898 }
16899
35add35e
AB
16900 /* If the type we found doesn't match the size we were looking for, then
16901 pretend we didn't find a type at all, the complex target type we
16902 create will then be nameless. */
a12e5744 16903 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16904 tt = nullptr;
16905
8bdc1658 16906 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16907 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16908}
16909
c906108c
SS
16910/* Find a representation of a given base type and install
16911 it in the TYPE field of the die. */
16912
f792889a 16913static struct type *
e7c27a73 16914read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16915{
518817b3 16916 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16917 struct type *type;
16918 struct attribute *attr;
19f392bc 16919 int encoding = 0, bits = 0;
15d034d0 16920 const char *name;
34877895 16921 gdbarch *arch;
c906108c 16922
e142c38c 16923 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16924 if (attr != nullptr)
34877895 16925 encoding = DW_UNSND (attr);
e142c38c 16926 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16927 if (attr != nullptr)
34877895 16928 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16929 name = dwarf2_name (die, cu);
6ccb9162 16930 if (!name)
34877895 16931 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16932
16933 arch = get_objfile_arch (objfile);
16934 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16935
34877895
PJ
16936 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16937 if (attr)
103a685e
TT
16938 {
16939 int endianity = DW_UNSND (attr);
16940
16941 switch (endianity)
16942 {
16943 case DW_END_big:
16944 byte_order = BFD_ENDIAN_BIG;
16945 break;
16946 case DW_END_little:
16947 byte_order = BFD_ENDIAN_LITTLE;
16948 break;
16949 default:
16950 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16951 break;
16952 }
16953 }
6ccb9162
UW
16954
16955 switch (encoding)
c906108c 16956 {
6ccb9162
UW
16957 case DW_ATE_address:
16958 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16959 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16960 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16961 break;
16962 case DW_ATE_boolean:
19f392bc 16963 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16964 break;
16965 case DW_ATE_complex_float:
103a685e
TT
16966 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16967 byte_order);
19f392bc 16968 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16969 break;
16970 case DW_ATE_decimal_float:
19f392bc 16971 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16972 break;
16973 case DW_ATE_float:
103a685e 16974 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16975 break;
16976 case DW_ATE_signed:
eb77c9df 16977 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16978 break;
16979 case DW_ATE_unsigned:
3b2b8fea
TT
16980 if (cu->language == language_fortran
16981 && name
61012eef 16982 && startswith (name, "character("))
19f392bc
UW
16983 type = init_character_type (objfile, bits, 1, name);
16984 else
eb77c9df 16985 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
16986 break;
16987 case DW_ATE_signed_char:
6e70227d 16988 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16989 || cu->language == language_pascal
16990 || cu->language == language_fortran)
19f392bc
UW
16991 type = init_character_type (objfile, bits, 0, name);
16992 else
eb77c9df 16993 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16994 break;
16995 case DW_ATE_unsigned_char:
868a0084 16996 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16997 || cu->language == language_pascal
c44af4eb
TT
16998 || cu->language == language_fortran
16999 || cu->language == language_rust)
19f392bc
UW
17000 type = init_character_type (objfile, bits, 1, name);
17001 else
eb77c9df 17002 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17003 break;
75079b2b 17004 case DW_ATE_UTF:
53e710ac 17005 {
53e710ac
PA
17006 if (bits == 16)
17007 type = builtin_type (arch)->builtin_char16;
17008 else if (bits == 32)
17009 type = builtin_type (arch)->builtin_char32;
17010 else
17011 {
b98664d3 17012 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17013 bits);
eb77c9df 17014 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17015 }
17016 return set_die_type (die, type, cu);
17017 }
75079b2b
TT
17018 break;
17019
6ccb9162 17020 default:
b98664d3 17021 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17022 dwarf_type_encoding_name (encoding));
77b7c781 17023 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17024 break;
c906108c 17025 }
6ccb9162 17026
0114d602 17027 if (name && strcmp (name, "char") == 0)
876cecd0 17028 TYPE_NOSIGN (type) = 1;
0114d602 17029
2b4424c3
TT
17030 maybe_set_alignment (cu, die, type);
17031
103a685e 17032 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17033
f792889a 17034 return set_die_type (die, type, cu);
c906108c
SS
17035}
17036
80180f79
SA
17037/* Parse dwarf attribute if it's a block, reference or constant and put the
17038 resulting value of the attribute into struct bound_prop.
17039 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17040
17041static int
17042attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17043 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17044 struct type *default_type)
80180f79
SA
17045{
17046 struct dwarf2_property_baton *baton;
518817b3
SM
17047 struct obstack *obstack
17048 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17049
9a49df9d
AB
17050 gdb_assert (default_type != NULL);
17051
80180f79
SA
17052 if (attr == NULL || prop == NULL)
17053 return 0;
17054
4fc6c0d5 17055 if (attr->form_is_block ())
80180f79 17056 {
8d749320 17057 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17058 baton->property_type = default_type;
80180f79
SA
17059 baton->locexpr.per_cu = cu->per_cu;
17060 baton->locexpr.size = DW_BLOCK (attr)->size;
17061 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17062 switch (attr->name)
17063 {
17064 case DW_AT_string_length:
17065 baton->locexpr.is_reference = true;
17066 break;
17067 default:
17068 baton->locexpr.is_reference = false;
17069 break;
17070 }
80180f79
SA
17071 prop->data.baton = baton;
17072 prop->kind = PROP_LOCEXPR;
17073 gdb_assert (prop->data.baton != NULL);
17074 }
cd6c91b4 17075 else if (attr->form_is_ref ())
80180f79
SA
17076 {
17077 struct dwarf2_cu *target_cu = cu;
17078 struct die_info *target_die;
17079 struct attribute *target_attr;
17080
17081 target_die = follow_die_ref (die, attr, &target_cu);
17082 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17083 if (target_attr == NULL)
17084 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17085 target_cu);
80180f79
SA
17086 if (target_attr == NULL)
17087 return 0;
17088
df25ebbd 17089 switch (target_attr->name)
80180f79 17090 {
df25ebbd 17091 case DW_AT_location:
cd6c91b4 17092 if (target_attr->form_is_section_offset ())
df25ebbd 17093 {
8d749320 17094 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17095 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17096 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17097 prop->data.baton = baton;
17098 prop->kind = PROP_LOCLIST;
17099 gdb_assert (prop->data.baton != NULL);
17100 }
4fc6c0d5 17101 else if (target_attr->form_is_block ())
df25ebbd 17102 {
8d749320 17103 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17104 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17105 baton->locexpr.per_cu = cu->per_cu;
17106 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17107 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17108 baton->locexpr.is_reference = true;
df25ebbd
JB
17109 prop->data.baton = baton;
17110 prop->kind = PROP_LOCEXPR;
17111 gdb_assert (prop->data.baton != NULL);
17112 }
17113 else
17114 {
17115 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17116 "dynamic property");
17117 return 0;
17118 }
17119 break;
17120 case DW_AT_data_member_location:
17121 {
17122 LONGEST offset;
17123
17124 if (!handle_data_member_location (target_die, target_cu,
17125 &offset))
17126 return 0;
17127
8d749320 17128 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17129 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17130 target_cu);
df25ebbd
JB
17131 baton->offset_info.offset = offset;
17132 baton->offset_info.type = die_type (target_die, target_cu);
17133 prop->data.baton = baton;
17134 prop->kind = PROP_ADDR_OFFSET;
17135 break;
17136 }
80180f79
SA
17137 }
17138 }
cd6c91b4 17139 else if (attr->form_is_constant ())
80180f79
SA
17140 {
17141 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17142 prop->kind = PROP_CONST;
17143 }
17144 else
17145 {
17146 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17147 dwarf2_name (die, cu));
17148 return 0;
17149 }
17150
17151 return 1;
17152}
17153
09ba997f 17154/* See read.h. */
9a49df9d 17155
09ba997f
TT
17156struct type *
17157dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17158{
09ba997f 17159 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17160 struct type *int_type;
17161
17162 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17163#define TRY_TYPE(F) \
17164 int_type = (unsigned_p \
17165 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17166 : objfile_type (objfile)->builtin_ ## F); \
17167 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17168 return int_type
17169
17170 TRY_TYPE (char);
17171 TRY_TYPE (short);
17172 TRY_TYPE (int);
17173 TRY_TYPE (long);
17174 TRY_TYPE (long_long);
17175
17176#undef TRY_TYPE
17177
17178 gdb_assert_not_reached ("unable to find suitable integer type");
17179}
17180
09ba997f 17181/* See read.h. */
11a8b164 17182
09ba997f
TT
17183struct type *
17184dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17185{
09ba997f
TT
17186 int addr_size = this->addr_size ();
17187 return int_type (addr_size, unsigned_p);
11a8b164
AB
17188}
17189
b86352cf
AB
17190/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17191 present (which is valid) then compute the default type based on the
17192 compilation units address size. */
17193
17194static struct type *
17195read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17196{
17197 struct type *index_type = die_type (die, cu);
17198
17199 /* Dwarf-2 specifications explicitly allows to create subrange types
17200 without specifying a base type.
17201 In that case, the base type must be set to the type of
17202 the lower bound, upper bound or count, in that order, if any of these
17203 three attributes references an object that has a type.
17204 If no base type is found, the Dwarf-2 specifications say that
17205 a signed integer type of size equal to the size of an address should
17206 be used.
17207 For the following C code: `extern char gdb_int [];'
17208 GCC produces an empty range DIE.
17209 FIXME: muller/2010-05-28: Possible references to object for low bound,
17210 high bound or count are not yet handled by this code. */
17211 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17212 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17213
17214 return index_type;
17215}
17216
a02abb62
JB
17217/* Read the given DW_AT_subrange DIE. */
17218
f792889a 17219static struct type *
a02abb62
JB
17220read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17221{
4c9ad8c2 17222 struct type *base_type, *orig_base_type;
a02abb62
JB
17223 struct type *range_type;
17224 struct attribute *attr;
729efb13 17225 struct dynamic_prop low, high;
4fae6e18 17226 int low_default_is_valid;
c451ebe5 17227 int high_bound_is_count = 0;
15d034d0 17228 const char *name;
d359392f 17229 ULONGEST negative_mask;
e77813c8 17230
b86352cf
AB
17231 orig_base_type = read_subrange_index_type (die, cu);
17232
4c9ad8c2
TT
17233 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17234 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17235 creating the range type, but we use the result of check_typedef
17236 when examining properties of the type. */
17237 base_type = check_typedef (orig_base_type);
a02abb62 17238
7e314c57
JK
17239 /* The die_type call above may have already set the type for this DIE. */
17240 range_type = get_die_type (die, cu);
17241 if (range_type)
17242 return range_type;
17243
729efb13
SA
17244 low.kind = PROP_CONST;
17245 high.kind = PROP_CONST;
17246 high.data.const_val = 0;
17247
4fae6e18
JK
17248 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17249 omitting DW_AT_lower_bound. */
17250 switch (cu->language)
6e70227d 17251 {
4fae6e18
JK
17252 case language_c:
17253 case language_cplus:
729efb13 17254 low.data.const_val = 0;
4fae6e18
JK
17255 low_default_is_valid = 1;
17256 break;
17257 case language_fortran:
729efb13 17258 low.data.const_val = 1;
4fae6e18
JK
17259 low_default_is_valid = 1;
17260 break;
17261 case language_d:
4fae6e18 17262 case language_objc:
c44af4eb 17263 case language_rust:
729efb13 17264 low.data.const_val = 0;
4fae6e18
JK
17265 low_default_is_valid = (cu->header.version >= 4);
17266 break;
17267 case language_ada:
17268 case language_m2:
17269 case language_pascal:
729efb13 17270 low.data.const_val = 1;
4fae6e18
JK
17271 low_default_is_valid = (cu->header.version >= 4);
17272 break;
17273 default:
729efb13 17274 low.data.const_val = 0;
4fae6e18
JK
17275 low_default_is_valid = 0;
17276 break;
a02abb62
JB
17277 }
17278
e142c38c 17279 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17280 if (attr != nullptr)
9a49df9d 17281 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17282 else if (!low_default_is_valid)
b98664d3 17283 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17284 "- DIE at %s [in module %s]"),
17285 sect_offset_str (die->sect_off),
518817b3 17286 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17287
506f5c41
TV
17288 struct attribute *attr_ub, *attr_count;
17289 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17290 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17291 {
506f5c41 17292 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17293 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17294 {
c451ebe5
SA
17295 /* If bounds are constant do the final calculation here. */
17296 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17297 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17298 else
17299 high_bound_is_count = 1;
c2ff108b 17300 }
506f5c41
TV
17301 else
17302 {
17303 if (attr_ub != NULL)
17304 complaint (_("Unresolved DW_AT_upper_bound "
17305 "- DIE at %s [in module %s]"),
17306 sect_offset_str (die->sect_off),
17307 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17308 if (attr_count != NULL)
17309 complaint (_("Unresolved DW_AT_count "
17310 "- DIE at %s [in module %s]"),
17311 sect_offset_str (die->sect_off),
17312 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17313 }
e77813c8 17314 }
a02abb62 17315
4e962e74
TT
17316 LONGEST bias = 0;
17317 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17318 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17319 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17320
dbb9c2b1
JB
17321 /* Normally, the DWARF producers are expected to use a signed
17322 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17323 But this is unfortunately not always the case, as witnessed
17324 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17325 is used instead. To work around that ambiguity, we treat
17326 the bounds as signed, and thus sign-extend their values, when
17327 the base type is signed. */
6e70227d 17328 negative_mask =
d359392f 17329 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17330 if (low.kind == PROP_CONST
17331 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17332 low.data.const_val |= negative_mask;
17333 if (high.kind == PROP_CONST
17334 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17335 high.data.const_val |= negative_mask;
43bbcdc2 17336
5bbd8269
AB
17337 /* Check for bit and byte strides. */
17338 struct dynamic_prop byte_stride_prop;
17339 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17340 if (attr_byte_stride != nullptr)
17341 {
09ba997f 17342 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17343 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17344 prop_type);
17345 }
17346
17347 struct dynamic_prop bit_stride_prop;
17348 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17349 if (attr_bit_stride != nullptr)
17350 {
17351 /* It only makes sense to have either a bit or byte stride. */
17352 if (attr_byte_stride != nullptr)
17353 {
17354 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17355 "- DIE at %s [in module %s]"),
17356 sect_offset_str (die->sect_off),
17357 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17358 attr_bit_stride = nullptr;
17359 }
17360 else
17361 {
09ba997f 17362 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17363 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17364 prop_type);
17365 }
17366 }
17367
17368 if (attr_byte_stride != nullptr
17369 || attr_bit_stride != nullptr)
17370 {
17371 bool byte_stride_p = (attr_byte_stride != nullptr);
17372 struct dynamic_prop *stride
17373 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17374
17375 range_type
17376 = create_range_type_with_stride (NULL, orig_base_type, &low,
17377 &high, bias, stride, byte_stride_p);
17378 }
17379 else
17380 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17381
c451ebe5
SA
17382 if (high_bound_is_count)
17383 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17384
c2ff108b
JK
17385 /* Ada expects an empty array on no boundary attributes. */
17386 if (attr == NULL && cu->language != language_ada)
729efb13 17387 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17388
39cbfefa
DJ
17389 name = dwarf2_name (die, cu);
17390 if (name)
17391 TYPE_NAME (range_type) = name;
6e70227d 17392
e142c38c 17393 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17394 if (attr != nullptr)
a02abb62
JB
17395 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17396
2b4424c3
TT
17397 maybe_set_alignment (cu, die, range_type);
17398
7e314c57
JK
17399 set_die_type (die, range_type, cu);
17400
17401 /* set_die_type should be already done. */
b4ba55a1
JB
17402 set_descriptive_type (range_type, die, cu);
17403
7e314c57 17404 return range_type;
a02abb62 17405}
6e70227d 17406
f792889a 17407static struct type *
81a17f79
JB
17408read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17409{
17410 struct type *type;
81a17f79 17411
518817b3
SM
17412 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17413 NULL);
0114d602 17414 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17415
74a2f8ff 17416 /* In Ada, an unspecified type is typically used when the description
85102364 17417 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17418 such a type, we treat it as a stub, and try to resolve it later on,
17419 when needed. */
17420 if (cu->language == language_ada)
17421 TYPE_STUB (type) = 1;
17422
f792889a 17423 return set_die_type (die, type, cu);
81a17f79 17424}
a02abb62 17425
639d11d3
DC
17426/* Read a single die and all its descendents. Set the die's sibling
17427 field to NULL; set other fields in the die correctly, and set all
17428 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17429 location of the info_ptr after reading all of those dies. PARENT
17430 is the parent of the die in question. */
17431
17432static struct die_info *
dee91e82 17433read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17434 const gdb_byte *info_ptr,
17435 const gdb_byte **new_info_ptr,
dee91e82 17436 struct die_info *parent)
639d11d3
DC
17437{
17438 struct die_info *die;
d521ce57 17439 const gdb_byte *cur_ptr;
639d11d3 17440
3e225074 17441 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17442 if (die == NULL)
17443 {
17444 *new_info_ptr = cur_ptr;
17445 return NULL;
17446 }
93311388 17447 store_in_ref_table (die, reader->cu);
639d11d3 17448
3e225074 17449 if (die->has_children)
bf6af496 17450 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17451 else
17452 {
17453 die->child = NULL;
17454 *new_info_ptr = cur_ptr;
17455 }
17456
17457 die->sibling = NULL;
17458 die->parent = parent;
17459 return die;
17460}
17461
17462/* Read a die, all of its descendents, and all of its siblings; set
17463 all of the fields of all of the dies correctly. Arguments are as
17464 in read_die_and_children. */
17465
17466static struct die_info *
bf6af496 17467read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17468 const gdb_byte *info_ptr,
17469 const gdb_byte **new_info_ptr,
bf6af496 17470 struct die_info *parent)
639d11d3
DC
17471{
17472 struct die_info *first_die, *last_sibling;
d521ce57 17473 const gdb_byte *cur_ptr;
639d11d3 17474
c906108c 17475 cur_ptr = info_ptr;
639d11d3
DC
17476 first_die = last_sibling = NULL;
17477
17478 while (1)
c906108c 17479 {
639d11d3 17480 struct die_info *die
dee91e82 17481 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17482
1d325ec1 17483 if (die == NULL)
c906108c 17484 {
639d11d3
DC
17485 *new_info_ptr = cur_ptr;
17486 return first_die;
c906108c 17487 }
1d325ec1
DJ
17488
17489 if (!first_die)
17490 first_die = die;
c906108c 17491 else
1d325ec1
DJ
17492 last_sibling->sibling = die;
17493
17494 last_sibling = die;
c906108c 17495 }
c906108c
SS
17496}
17497
bf6af496
DE
17498/* Read a die, all of its descendents, and all of its siblings; set
17499 all of the fields of all of the dies correctly. Arguments are as
17500 in read_die_and_children.
17501 This the main entry point for reading a DIE and all its children. */
17502
17503static struct die_info *
17504read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17505 const gdb_byte *info_ptr,
17506 const gdb_byte **new_info_ptr,
bf6af496
DE
17507 struct die_info *parent)
17508{
17509 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17510 new_info_ptr, parent);
17511
b4f54984 17512 if (dwarf_die_debug)
bf6af496
DE
17513 {
17514 fprintf_unfiltered (gdb_stdlog,
17515 "Read die from %s@0x%x of %s:\n",
96b79293 17516 reader->die_section->get_name (),
bf6af496
DE
17517 (unsigned) (info_ptr - reader->die_section->buffer),
17518 bfd_get_filename (reader->abfd));
b4f54984 17519 dump_die (die, dwarf_die_debug);
bf6af496
DE
17520 }
17521
17522 return die;
17523}
17524
3019eac3
DE
17525/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17526 attributes.
17527 The caller is responsible for filling in the extra attributes
17528 and updating (*DIEP)->num_attrs.
17529 Set DIEP to point to a newly allocated die with its information,
3e225074 17530 except for its child, sibling, and parent fields. */
93311388 17531
d521ce57 17532static const gdb_byte *
3019eac3 17533read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17534 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17535 int num_extra_attrs)
93311388 17536{
b64f50a1 17537 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17538 struct abbrev_info *abbrev;
17539 struct die_info *die;
17540 struct dwarf2_cu *cu = reader->cu;
17541 bfd *abfd = reader->abfd;
17542
9c541725 17543 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17544 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17545 info_ptr += bytes_read;
17546 if (!abbrev_number)
17547 {
17548 *diep = NULL;
93311388
DE
17549 return info_ptr;
17550 }
17551
685af9cd 17552 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17553 if (!abbrev)
348e048f
DE
17554 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17555 abbrev_number,
17556 bfd_get_filename (abfd));
17557
3019eac3 17558 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17559 die->sect_off = sect_off;
93311388
DE
17560 die->tag = abbrev->tag;
17561 die->abbrev = abbrev_number;
3e225074 17562 die->has_children = abbrev->has_children;
93311388 17563
3019eac3
DE
17564 /* Make the result usable.
17565 The caller needs to update num_attrs after adding the extra
17566 attributes. */
93311388
DE
17567 die->num_attrs = abbrev->num_attrs;
17568
18a8505e 17569 std::vector<int> indexes_that_need_reprocess;
93311388 17570 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17571 {
17572 bool need_reprocess;
17573 info_ptr =
17574 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17575 info_ptr, &need_reprocess);
17576 if (need_reprocess)
17577 indexes_that_need_reprocess.push_back (i);
17578 }
17579
17580 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17581 if (attr != nullptr)
17582 cu->str_offsets_base = DW_UNSND (attr);
93311388 17583
18a8505e
AT
17584 auto maybe_addr_base = lookup_addr_base(die);
17585 if (maybe_addr_base.has_value ())
17586 cu->addr_base = *maybe_addr_base;
17587 for (int index : indexes_that_need_reprocess)
17588 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17589 *diep = die;
93311388
DE
17590 return info_ptr;
17591}
17592
3019eac3
DE
17593/* Read a die and all its attributes.
17594 Set DIEP to point to a newly allocated die with its information,
3e225074 17595 except for its child, sibling, and parent fields. */
3019eac3 17596
d521ce57 17597static const gdb_byte *
3019eac3 17598read_full_die (const struct die_reader_specs *reader,
3e225074 17599 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17600{
d521ce57 17601 const gdb_byte *result;
bf6af496 17602
3e225074 17603 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17604
b4f54984 17605 if (dwarf_die_debug)
bf6af496
DE
17606 {
17607 fprintf_unfiltered (gdb_stdlog,
17608 "Read die from %s@0x%x of %s:\n",
96b79293 17609 reader->die_section->get_name (),
bf6af496
DE
17610 (unsigned) (info_ptr - reader->die_section->buffer),
17611 bfd_get_filename (reader->abfd));
b4f54984 17612 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17613 }
17614
17615 return result;
3019eac3 17616}
433df2d4 17617\f
c906108c 17618
72bf9492
DJ
17619/* Returns nonzero if TAG represents a type that we might generate a partial
17620 symbol for. */
17621
17622static int
17623is_type_tag_for_partial (int tag)
17624{
17625 switch (tag)
17626 {
17627#if 0
17628 /* Some types that would be reasonable to generate partial symbols for,
17629 that we don't at present. */
17630 case DW_TAG_array_type:
17631 case DW_TAG_file_type:
17632 case DW_TAG_ptr_to_member_type:
17633 case DW_TAG_set_type:
17634 case DW_TAG_string_type:
17635 case DW_TAG_subroutine_type:
17636#endif
17637 case DW_TAG_base_type:
17638 case DW_TAG_class_type:
680b30c7 17639 case DW_TAG_interface_type:
72bf9492
DJ
17640 case DW_TAG_enumeration_type:
17641 case DW_TAG_structure_type:
17642 case DW_TAG_subrange_type:
17643 case DW_TAG_typedef:
17644 case DW_TAG_union_type:
17645 return 1;
17646 default:
17647 return 0;
17648 }
17649}
17650
17651/* Load all DIEs that are interesting for partial symbols into memory. */
17652
17653static struct partial_die_info *
dee91e82 17654load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17655 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17656{
dee91e82 17657 struct dwarf2_cu *cu = reader->cu;
518817b3 17658 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17659 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17660 unsigned int bytes_read;
5afb4e99 17661 unsigned int load_all = 0;
72bf9492
DJ
17662 int nesting_level = 1;
17663
17664 parent_die = NULL;
17665 last_die = NULL;
17666
7adf1e79
DE
17667 gdb_assert (cu->per_cu != NULL);
17668 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17669 load_all = 1;
17670
72bf9492
DJ
17671 cu->partial_dies
17672 = htab_create_alloc_ex (cu->header.length / 12,
17673 partial_die_hash,
17674 partial_die_eq,
17675 NULL,
17676 &cu->comp_unit_obstack,
17677 hashtab_obstack_allocate,
17678 dummy_obstack_deallocate);
17679
72bf9492
DJ
17680 while (1)
17681 {
685af9cd 17682 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17683
17684 /* A NULL abbrev means the end of a series of children. */
17685 if (abbrev == NULL)
17686 {
17687 if (--nesting_level == 0)
cd9983dd
YQ
17688 return first_die;
17689
72bf9492
DJ
17690 info_ptr += bytes_read;
17691 last_die = parent_die;
17692 parent_die = parent_die->die_parent;
17693 continue;
17694 }
17695
98bfdba5
PA
17696 /* Check for template arguments. We never save these; if
17697 they're seen, we just mark the parent, and go on our way. */
17698 if (parent_die != NULL
17699 && cu->language == language_cplus
17700 && (abbrev->tag == DW_TAG_template_type_param
17701 || abbrev->tag == DW_TAG_template_value_param))
17702 {
17703 parent_die->has_template_arguments = 1;
17704
17705 if (!load_all)
17706 {
17707 /* We don't need a partial DIE for the template argument. */
dee91e82 17708 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17709 continue;
17710 }
17711 }
17712
0d99eb77 17713 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17714 Skip their other children. */
17715 if (!load_all
17716 && cu->language == language_cplus
17717 && parent_die != NULL
17718 && parent_die->tag == DW_TAG_subprogram)
17719 {
dee91e82 17720 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17721 continue;
17722 }
17723
5afb4e99
DJ
17724 /* Check whether this DIE is interesting enough to save. Normally
17725 we would not be interested in members here, but there may be
17726 later variables referencing them via DW_AT_specification (for
17727 static members). */
17728 if (!load_all
17729 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17730 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17731 && abbrev->tag != DW_TAG_enumerator
17732 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17733 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17734 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17735 && abbrev->tag != DW_TAG_variable
5afb4e99 17736 && abbrev->tag != DW_TAG_namespace
f55ee35c 17737 && abbrev->tag != DW_TAG_module
95554aad 17738 && abbrev->tag != DW_TAG_member
74921315
KS
17739 && abbrev->tag != DW_TAG_imported_unit
17740 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17741 {
17742 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17743 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17744 continue;
17745 }
17746
6f06d47b
YQ
17747 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17748 abbrev);
cd9983dd 17749
48fbe735 17750 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17751
17752 /* This two-pass algorithm for processing partial symbols has a
17753 high cost in cache pressure. Thus, handle some simple cases
17754 here which cover the majority of C partial symbols. DIEs
17755 which neither have specification tags in them, nor could have
17756 specification tags elsewhere pointing at them, can simply be
17757 processed and discarded.
17758
17759 This segment is also optional; scan_partial_symbols and
17760 add_partial_symbol will handle these DIEs if we chain
17761 them in normally. When compilers which do not emit large
17762 quantities of duplicate debug information are more common,
17763 this code can probably be removed. */
17764
17765 /* Any complete simple types at the top level (pretty much all
17766 of them, for a language without namespaces), can be processed
17767 directly. */
17768 if (parent_die == NULL
cd9983dd
YQ
17769 && pdi.has_specification == 0
17770 && pdi.is_declaration == 0
17771 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17772 || pdi.tag == DW_TAG_base_type
17773 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17774 {
cd9983dd 17775 if (building_psymtab && pdi.name != NULL)
31edb802 17776 add_psymbol_to_list (pdi.name, false,
79748972 17777 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17778 psymbol_placement::STATIC,
1762568f 17779 0, cu->language, objfile);
cd9983dd 17780 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17781 continue;
17782 }
17783
d8228535
JK
17784 /* The exception for DW_TAG_typedef with has_children above is
17785 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17786 type_name_or_error will error on such types later.
d8228535
JK
17787
17788 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17789 it could not find the child DIEs referenced later, this is checked
17790 above. In correct DWARF DW_TAG_typedef should have no children. */
17791
cd9983dd 17792 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17793 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17794 "- DIE at %s [in module %s]"),
cd9983dd 17795 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17796
72bf9492
DJ
17797 /* If we're at the second level, and we're an enumerator, and
17798 our parent has no specification (meaning possibly lives in a
17799 namespace elsewhere), then we can add the partial symbol now
17800 instead of queueing it. */
cd9983dd 17801 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17802 && parent_die != NULL
17803 && parent_die->die_parent == NULL
17804 && parent_die->tag == DW_TAG_enumeration_type
17805 && parent_die->has_specification == 0)
17806 {
cd9983dd 17807 if (pdi.name == NULL)
b98664d3 17808 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17809 else if (building_psymtab)
31edb802 17810 add_psymbol_to_list (pdi.name, false,
79748972 17811 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17812 cu->language == language_cplus
75aedd27
TT
17813 ? psymbol_placement::GLOBAL
17814 : psymbol_placement::STATIC,
1762568f 17815 0, cu->language, objfile);
72bf9492 17816
cd9983dd 17817 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17818 continue;
17819 }
17820
cd9983dd 17821 struct partial_die_info *part_die
6f06d47b 17822 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17823
72bf9492
DJ
17824 /* We'll save this DIE so link it in. */
17825 part_die->die_parent = parent_die;
17826 part_die->die_sibling = NULL;
17827 part_die->die_child = NULL;
17828
17829 if (last_die && last_die == parent_die)
17830 last_die->die_child = part_die;
17831 else if (last_die)
17832 last_die->die_sibling = part_die;
17833
17834 last_die = part_die;
17835
17836 if (first_die == NULL)
17837 first_die = part_die;
17838
17839 /* Maybe add the DIE to the hash table. Not all DIEs that we
17840 find interesting need to be in the hash table, because we
17841 also have the parent/sibling/child chains; only those that we
17842 might refer to by offset later during partial symbol reading.
17843
17844 For now this means things that might have be the target of a
17845 DW_AT_specification, DW_AT_abstract_origin, or
17846 DW_AT_extension. DW_AT_extension will refer only to
17847 namespaces; DW_AT_abstract_origin refers to functions (and
17848 many things under the function DIE, but we do not recurse
17849 into function DIEs during partial symbol reading) and
17850 possibly variables as well; DW_AT_specification refers to
17851 declarations. Declarations ought to have the DW_AT_declaration
17852 flag. It happens that GCC forgets to put it in sometimes, but
17853 only for functions, not for types.
17854
17855 Adding more things than necessary to the hash table is harmless
17856 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17857 wasted time in find_partial_die, when we reread the compilation
17858 unit with load_all_dies set. */
72bf9492 17859
5afb4e99 17860 if (load_all
72929c62 17861 || abbrev->tag == DW_TAG_constant
5afb4e99 17862 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17863 || abbrev->tag == DW_TAG_variable
17864 || abbrev->tag == DW_TAG_namespace
17865 || part_die->is_declaration)
17866 {
17867 void **slot;
17868
17869 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17870 to_underlying (part_die->sect_off),
17871 INSERT);
72bf9492
DJ
17872 *slot = part_die;
17873 }
17874
72bf9492 17875 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17876 we have no reason to follow the children of structures; for other
98bfdba5
PA
17877 languages we have to, so that we can get at method physnames
17878 to infer fully qualified class names, for DW_AT_specification,
17879 and for C++ template arguments. For C++, we also look one level
17880 inside functions to find template arguments (if the name of the
17881 function does not already contain the template arguments).
bc30ff58 17882
0a4b0913
AB
17883 For Ada and Fortran, we need to scan the children of subprograms
17884 and lexical blocks as well because these languages allow the
17885 definition of nested entities that could be interesting for the
17886 debugger, such as nested subprograms for instance. */
72bf9492 17887 if (last_die->has_children
5afb4e99
DJ
17888 && (load_all
17889 || last_die->tag == DW_TAG_namespace
f55ee35c 17890 || last_die->tag == DW_TAG_module
72bf9492 17891 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17892 || (cu->language == language_cplus
17893 && last_die->tag == DW_TAG_subprogram
17894 && (last_die->name == NULL
17895 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17896 || (cu->language != language_c
17897 && (last_die->tag == DW_TAG_class_type
680b30c7 17898 || last_die->tag == DW_TAG_interface_type
72bf9492 17899 || last_die->tag == DW_TAG_structure_type
bc30ff58 17900 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17901 || ((cu->language == language_ada
17902 || cu->language == language_fortran)
bc30ff58
JB
17903 && (last_die->tag == DW_TAG_subprogram
17904 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17905 {
17906 nesting_level++;
17907 parent_die = last_die;
17908 continue;
17909 }
17910
17911 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17912 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17913
17914 /* Back to the top, do it again. */
17915 }
17916}
17917
6f06d47b
YQ
17918partial_die_info::partial_die_info (sect_offset sect_off_,
17919 struct abbrev_info *abbrev)
17920 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17921{
17922}
17923
35cc7ed7
YQ
17924/* Read a minimal amount of information into the minimal die structure.
17925 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17926
48fbe735
YQ
17927const gdb_byte *
17928partial_die_info::read (const struct die_reader_specs *reader,
17929 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17930{
dee91e82 17931 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17932 struct dwarf2_per_objfile *dwarf2_per_objfile
17933 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17934 unsigned int i;
c5aa993b 17935 int has_low_pc_attr = 0;
c906108c 17936 int has_high_pc_attr = 0;
91da1414 17937 int high_pc_relative = 0;
c906108c 17938
18a8505e 17939 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17940 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17941 {
18a8505e
AT
17942 bool need_reprocess;
17943 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17944 info_ptr, &need_reprocess);
17945 /* String and address offsets that need to do the reprocessing have
17946 already been read at this point, so there is no need to wait until
17947 the loop terminates to do the reprocessing. */
17948 if (need_reprocess)
17949 read_attribute_reprocess (reader, &attr_vec[i]);
17950 attribute &attr = attr_vec[i];
c906108c 17951 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17952 partial symbol table. */
c906108c
SS
17953 switch (attr.name)
17954 {
17955 case DW_AT_name:
48fbe735 17956 switch (tag)
71c25dea
TT
17957 {
17958 case DW_TAG_compile_unit:
95554aad 17959 case DW_TAG_partial_unit:
348e048f 17960 case DW_TAG_type_unit:
71c25dea
TT
17961 /* Compilation units have a DW_AT_name that is a filename, not
17962 a source language identifier. */
17963 case DW_TAG_enumeration_type:
17964 case DW_TAG_enumerator:
17965 /* These tags always have simple identifiers already; no need
17966 to canonicalize them. */
48fbe735 17967 name = DW_STRING (&attr);
71c25dea
TT
17968 break;
17969 default:
48fbe735
YQ
17970 {
17971 struct objfile *objfile = dwarf2_per_objfile->objfile;
17972
17973 name
17974 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17975 &objfile->per_bfd->storage_obstack);
17976 }
71c25dea
TT
17977 break;
17978 }
c906108c 17979 break;
31ef98ae 17980 case DW_AT_linkage_name:
c906108c 17981 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17982 /* Note that both forms of linkage name might appear. We
17983 assume they will be the same, and we only store the last
17984 one we see. */
48fbe735 17985 linkage_name = DW_STRING (&attr);
c906108c
SS
17986 break;
17987 case DW_AT_low_pc:
17988 has_low_pc_attr = 1;
cd6c91b4 17989 lowpc = attr.value_as_address ();
c906108c
SS
17990 break;
17991 case DW_AT_high_pc:
17992 has_high_pc_attr = 1;
cd6c91b4
TT
17993 highpc = attr.value_as_address ();
17994 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 17995 high_pc_relative = 1;
c906108c
SS
17996 break;
17997 case DW_AT_location:
0963b4bd 17998 /* Support the .debug_loc offsets. */
4fc6c0d5 17999 if (attr.form_is_block ())
8e19ed76 18000 {
48fbe735 18001 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18002 }
cd6c91b4 18003 else if (attr.form_is_section_offset ())
8e19ed76 18004 {
4d3c2250 18005 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18006 }
18007 else
18008 {
4d3c2250
KB
18009 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18010 "partial symbol information");
8e19ed76 18011 }
c906108c 18012 break;
c906108c 18013 case DW_AT_external:
48fbe735 18014 is_external = DW_UNSND (&attr);
c906108c
SS
18015 break;
18016 case DW_AT_declaration:
48fbe735 18017 is_declaration = DW_UNSND (&attr);
c906108c
SS
18018 break;
18019 case DW_AT_type:
48fbe735 18020 has_type = 1;
c906108c
SS
18021 break;
18022 case DW_AT_abstract_origin:
18023 case DW_AT_specification:
72bf9492 18024 case DW_AT_extension:
48fbe735
YQ
18025 has_specification = 1;
18026 spec_offset = dwarf2_get_ref_die_offset (&attr);
18027 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18028 || cu->per_cu->is_dwz);
c906108c
SS
18029 break;
18030 case DW_AT_sibling:
18031 /* Ignore absolute siblings, they might point outside of
18032 the current compile unit. */
18033 if (attr.form == DW_FORM_ref_addr)
b98664d3 18034 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18035 else
b9502d3f 18036 {
48fbe735 18037 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18038 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18039 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18040
18041 if (sibling_ptr < info_ptr)
b98664d3 18042 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18043 else if (sibling_ptr > reader->buffer_end)
18044 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18045 else
48fbe735 18046 sibling = sibling_ptr;
b9502d3f 18047 }
c906108c 18048 break;
fa4028e9 18049 case DW_AT_byte_size:
48fbe735 18050 has_byte_size = 1;
fa4028e9 18051 break;
ff908ebf 18052 case DW_AT_const_value:
48fbe735 18053 has_const_value = 1;
ff908ebf 18054 break;
68511cec
CES
18055 case DW_AT_calling_convention:
18056 /* DWARF doesn't provide a way to identify a program's source-level
18057 entry point. DW_AT_calling_convention attributes are only meant
18058 to describe functions' calling conventions.
18059
18060 However, because it's a necessary piece of information in
0c1b455e
TT
18061 Fortran, and before DWARF 4 DW_CC_program was the only
18062 piece of debugging information whose definition refers to
18063 a 'main program' at all, several compilers marked Fortran
18064 main programs with DW_CC_program --- even when those
18065 functions use the standard calling conventions.
18066
18067 Although DWARF now specifies a way to provide this
18068 information, we support this practice for backward
18069 compatibility. */
68511cec 18070 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18071 && cu->language == language_fortran)
48fbe735 18072 main_subprogram = 1;
68511cec 18073 break;
481860b3
GB
18074 case DW_AT_inline:
18075 if (DW_UNSND (&attr) == DW_INL_inlined
18076 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18077 may_be_inlined = 1;
481860b3 18078 break;
95554aad
TT
18079
18080 case DW_AT_import:
48fbe735 18081 if (tag == DW_TAG_imported_unit)
36586728 18082 {
48fbe735
YQ
18083 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18084 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18085 || cu->per_cu->is_dwz);
18086 }
95554aad
TT
18087 break;
18088
0c1b455e 18089 case DW_AT_main_subprogram:
48fbe735 18090 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18091 break;
18092
05caa1d2
TT
18093 case DW_AT_ranges:
18094 {
18095 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18096 but that requires a full DIE, so instead we just
18097 reimplement it. */
18098 int need_ranges_base = tag != DW_TAG_compile_unit;
18099 unsigned int ranges_offset = (DW_UNSND (&attr)
18100 + (need_ranges_base
18101 ? cu->ranges_base
18102 : 0));
18103
18104 /* Value of the DW_AT_ranges attribute is the offset in the
18105 .debug_ranges section. */
18106 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18107 nullptr))
18108 has_pc_info = 1;
18109 }
18110 break;
18111
c906108c
SS
18112 default:
18113 break;
18114 }
18115 }
18116
10d06d82
TT
18117 /* For Ada, if both the name and the linkage name appear, we prefer
18118 the latter. This lets "catch exception" work better, regardless
18119 of the order in which the name and linkage name were emitted.
18120 Really, though, this is just a workaround for the fact that gdb
18121 doesn't store both the name and the linkage name. */
18122 if (cu->language == language_ada && linkage_name != nullptr)
18123 name = linkage_name;
18124
91da1414 18125 if (high_pc_relative)
48fbe735 18126 highpc += lowpc;
91da1414 18127
9373cf26
JK
18128 if (has_low_pc_attr && has_high_pc_attr)
18129 {
18130 /* When using the GNU linker, .gnu.linkonce. sections are used to
18131 eliminate duplicate copies of functions and vtables and such.
18132 The linker will arbitrarily choose one and discard the others.
18133 The AT_*_pc values for such functions refer to local labels in
18134 these sections. If the section from that file was discarded, the
18135 labels are not in the output, so the relocs get a value of 0.
18136 If this is a discarded function, mark the pc bounds as invalid,
18137 so that GDB will ignore it. */
48fbe735 18138 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18139 {
48fbe735 18140 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18142
b98664d3 18143 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18144 "for DIE at %s [in module %s]"),
48fbe735
YQ
18145 paddress (gdbarch, lowpc),
18146 sect_offset_str (sect_off),
9d8780f0 18147 objfile_name (objfile));
9373cf26
JK
18148 }
18149 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18150 else if (lowpc >= highpc)
9373cf26 18151 {
48fbe735 18152 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18153 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18154
b98664d3 18155 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18156 "for DIE at %s [in module %s]"),
48fbe735
YQ
18157 paddress (gdbarch, lowpc),
18158 paddress (gdbarch, highpc),
18159 sect_offset_str (sect_off),
9c541725 18160 objfile_name (objfile));
9373cf26
JK
18161 }
18162 else
48fbe735 18163 has_pc_info = 1;
9373cf26 18164 }
85cbf3d3 18165
c906108c
SS
18166 return info_ptr;
18167}
18168
72bf9492
DJ
18169/* Find a cached partial DIE at OFFSET in CU. */
18170
d590ff25
YQ
18171struct partial_die_info *
18172dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18173{
18174 struct partial_die_info *lookup_die = NULL;
6f06d47b 18175 struct partial_die_info part_die (sect_off);
72bf9492 18176
9a3c8263 18177 lookup_die = ((struct partial_die_info *)
d590ff25 18178 htab_find_with_hash (partial_dies, &part_die,
9c541725 18179 to_underlying (sect_off)));
72bf9492 18180
72bf9492
DJ
18181 return lookup_die;
18182}
18183
348e048f
DE
18184/* Find a partial DIE at OFFSET, which may or may not be in CU,
18185 except in the case of .debug_types DIEs which do not reference
18186 outside their CU (they do however referencing other types via
55f1336d 18187 DW_FORM_ref_sig8). */
72bf9492 18188
122cf0f2 18189static const struct cu_partial_die_info
9c541725 18190find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18191{
518817b3
SM
18192 struct dwarf2_per_objfile *dwarf2_per_objfile
18193 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18194 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18195 struct dwarf2_per_cu_data *per_cu = NULL;
18196 struct partial_die_info *pd = NULL;
72bf9492 18197
36586728 18198 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18199 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18200 {
d590ff25 18201 pd = cu->find_partial_die (sect_off);
5afb4e99 18202 if (pd != NULL)
fb816e8b 18203 return { cu, pd };
0d99eb77
DE
18204 /* We missed recording what we needed.
18205 Load all dies and try again. */
18206 per_cu = cu->per_cu;
5afb4e99 18207 }
0d99eb77
DE
18208 else
18209 {
18210 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18211 if (cu->per_cu->is_debug_types)
0d99eb77 18212 {
9d8780f0
SM
18213 error (_("Dwarf Error: Type Unit at offset %s contains"
18214 " external reference to offset %s [in module %s].\n"),
18215 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18216 bfd_get_filename (objfile->obfd));
18217 }
9c541725 18218 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18219 dwarf2_per_objfile);
72bf9492 18220
0d99eb77
DE
18221 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18222 load_partial_comp_unit (per_cu);
ae038cb0 18223
0d99eb77 18224 per_cu->cu->last_used = 0;
d590ff25 18225 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18226 }
5afb4e99 18227
dee91e82
DE
18228 /* If we didn't find it, and not all dies have been loaded,
18229 load them all and try again. */
18230
5afb4e99
DJ
18231 if (pd == NULL && per_cu->load_all_dies == 0)
18232 {
5afb4e99 18233 per_cu->load_all_dies = 1;
fd820528
DE
18234
18235 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18236 THIS_CU->cu may already be in use. So we can't just free it and
18237 replace its DIEs with the ones we read in. Instead, we leave those
18238 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18239 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18240 set. */
dee91e82 18241 load_partial_comp_unit (per_cu);
5afb4e99 18242
d590ff25 18243 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18244 }
18245
18246 if (pd == NULL)
18247 internal_error (__FILE__, __LINE__,
9d8780f0 18248 _("could not find partial DIE %s "
3e43a32a 18249 "in cache [from module %s]\n"),
9d8780f0 18250 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18251 return { per_cu->cu, pd };
72bf9492
DJ
18252}
18253
abc72ce4
DE
18254/* See if we can figure out if the class lives in a namespace. We do
18255 this by looking for a member function; its demangled name will
18256 contain namespace info, if there is any. */
18257
18258static void
18259guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18260 struct dwarf2_cu *cu)
18261{
18262 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18263 what template types look like, because the demangler
18264 frequently doesn't give the same name as the debug info. We
18265 could fix this by only using the demangled name to get the
18266 prefix (but see comment in read_structure_type). */
18267
18268 struct partial_die_info *real_pdi;
18269 struct partial_die_info *child_pdi;
18270
18271 /* If this DIE (this DIE's specification, if any) has a parent, then
18272 we should not do this. We'll prepend the parent's fully qualified
18273 name when we create the partial symbol. */
18274
18275 real_pdi = struct_pdi;
18276 while (real_pdi->has_specification)
fb816e8b 18277 {
122cf0f2
AB
18278 auto res = find_partial_die (real_pdi->spec_offset,
18279 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18280 real_pdi = res.pdi;
18281 cu = res.cu;
18282 }
abc72ce4
DE
18283
18284 if (real_pdi->die_parent != NULL)
18285 return;
18286
18287 for (child_pdi = struct_pdi->die_child;
18288 child_pdi != NULL;
18289 child_pdi = child_pdi->die_sibling)
18290 {
18291 if (child_pdi->tag == DW_TAG_subprogram
18292 && child_pdi->linkage_name != NULL)
18293 {
43816ebc
TT
18294 gdb::unique_xmalloc_ptr<char> actual_class_name
18295 (language_class_name_from_physname (cu->language_defn,
18296 child_pdi->linkage_name));
abc72ce4
DE
18297 if (actual_class_name != NULL)
18298 {
518817b3 18299 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18300 struct_pdi->name
021887d8 18301 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 18302 actual_class_name.get ());
abc72ce4
DE
18303 }
18304 break;
18305 }
18306 }
18307}
18308
52356b79
YQ
18309void
18310partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18311{
abc72ce4
DE
18312 /* Once we've fixed up a die, there's no point in doing so again.
18313 This also avoids a memory leak if we were to call
18314 guess_partial_die_structure_name multiple times. */
52356b79 18315 if (fixup_called)
abc72ce4
DE
18316 return;
18317
72bf9492
DJ
18318 /* If we found a reference attribute and the DIE has no name, try
18319 to find a name in the referred to DIE. */
18320
52356b79 18321 if (name == NULL && has_specification)
72bf9492
DJ
18322 {
18323 struct partial_die_info *spec_die;
72bf9492 18324
122cf0f2 18325 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18326 spec_die = res.pdi;
18327 cu = res.cu;
72bf9492 18328
52356b79 18329 spec_die->fixup (cu);
72bf9492
DJ
18330
18331 if (spec_die->name)
18332 {
52356b79 18333 name = spec_die->name;
72bf9492
DJ
18334
18335 /* Copy DW_AT_external attribute if it is set. */
18336 if (spec_die->is_external)
52356b79 18337 is_external = spec_die->is_external;
72bf9492
DJ
18338 }
18339 }
18340
18341 /* Set default names for some unnamed DIEs. */
72bf9492 18342
52356b79
YQ
18343 if (name == NULL && tag == DW_TAG_namespace)
18344 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18345
abc72ce4
DE
18346 /* If there is no parent die to provide a namespace, and there are
18347 children, see if we can determine the namespace from their linkage
122d1940 18348 name. */
abc72ce4 18349 if (cu->language == language_cplus
fd5866f6 18350 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18351 && die_parent == NULL
18352 && has_children
18353 && (tag == DW_TAG_class_type
18354 || tag == DW_TAG_structure_type
18355 || tag == DW_TAG_union_type))
18356 guess_partial_die_structure_name (this, cu);
abc72ce4 18357
53832f31
TT
18358 /* GCC might emit a nameless struct or union that has a linkage
18359 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18360 if (name == NULL
18361 && (tag == DW_TAG_class_type
18362 || tag == DW_TAG_interface_type
18363 || tag == DW_TAG_structure_type
18364 || tag == DW_TAG_union_type)
18365 && linkage_name != NULL)
53832f31 18366 {
43816ebc
TT
18367 gdb::unique_xmalloc_ptr<char> demangled
18368 (gdb_demangle (linkage_name, DMGL_TYPES));
18369 if (demangled != nullptr)
53832f31 18370 {
96408a79
SA
18371 const char *base;
18372
18373 /* Strip any leading namespaces/classes, keep only the base name.
18374 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18375 base = strrchr (demangled.get (), ':');
18376 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18377 base++;
18378 else
43816ebc 18379 base = demangled.get ();
96408a79 18380
518817b3 18381 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 18382 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
18383 }
18384 }
18385
52356b79 18386 fixup_called = 1;
72bf9492
DJ
18387}
18388
18a8505e
AT
18389/* Process the attributes that had to be skipped in the first round. These
18390 attributes are the ones that need str_offsets_base or addr_base attributes.
18391 They could not have been processed in the first round, because at the time
18392 the values of str_offsets_base or addr_base may not have been known. */
18393void read_attribute_reprocess (const struct die_reader_specs *reader,
18394 struct attribute *attr)
18395{
18396 struct dwarf2_cu *cu = reader->cu;
18397 switch (attr->form)
18398 {
18399 case DW_FORM_addrx:
18400 case DW_FORM_GNU_addr_index:
18401 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18402 break;
18403 case DW_FORM_strx:
18404 case DW_FORM_strx1:
18405 case DW_FORM_strx2:
18406 case DW_FORM_strx3:
18407 case DW_FORM_strx4:
18408 case DW_FORM_GNU_str_index:
18409 {
18410 unsigned int str_index = DW_UNSND (attr);
18411 if (reader->dwo_file != NULL)
18412 {
18413 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18414 DW_STRING_IS_CANONICAL (attr) = 0;
18415 }
18416 else
18417 {
18418 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18419 DW_STRING_IS_CANONICAL (attr) = 0;
18420 }
18421 break;
18422 }
18423 default:
18424 gdb_assert_not_reached (_("Unexpected DWARF form."));
18425 }
18426}
18427
a8329558 18428/* Read an attribute value described by an attribute form. */
c906108c 18429
d521ce57 18430static const gdb_byte *
dee91e82
DE
18431read_attribute_value (const struct die_reader_specs *reader,
18432 struct attribute *attr, unsigned form,
18a8505e
AT
18433 LONGEST implicit_const, const gdb_byte *info_ptr,
18434 bool *need_reprocess)
c906108c 18435{
dee91e82 18436 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18437 struct dwarf2_per_objfile *dwarf2_per_objfile
18438 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18439 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18440 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18441 bfd *abfd = reader->abfd;
e7c27a73 18442 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18443 unsigned int bytes_read;
18444 struct dwarf_block *blk;
18a8505e 18445 *need_reprocess = false;
c906108c 18446
aead7601 18447 attr->form = (enum dwarf_form) form;
a8329558 18448 switch (form)
c906108c 18449 {
c906108c 18450 case DW_FORM_ref_addr:
ae411497 18451 if (cu->header.version == 2)
c8a7a66f
TT
18452 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18453 &bytes_read);
ae411497 18454 else
8266302d
TT
18455 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18456 &bytes_read);
ae411497
TT
18457 info_ptr += bytes_read;
18458 break;
36586728 18459 case DW_FORM_GNU_ref_alt:
8266302d 18460 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18461 info_ptr += bytes_read;
18462 break;
ae411497 18463 case DW_FORM_addr:
c8a7a66f 18464 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18465 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18466 info_ptr += bytes_read;
c906108c
SS
18467 break;
18468 case DW_FORM_block2:
7b5a2f43 18469 blk = dwarf_alloc_block (cu);
c906108c
SS
18470 blk->size = read_2_bytes (abfd, info_ptr);
18471 info_ptr += 2;
18472 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18473 info_ptr += blk->size;
18474 DW_BLOCK (attr) = blk;
18475 break;
18476 case DW_FORM_block4:
7b5a2f43 18477 blk = dwarf_alloc_block (cu);
c906108c
SS
18478 blk->size = read_4_bytes (abfd, info_ptr);
18479 info_ptr += 4;
18480 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18481 info_ptr += blk->size;
18482 DW_BLOCK (attr) = blk;
18483 break;
18484 case DW_FORM_data2:
18485 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18486 info_ptr += 2;
18487 break;
18488 case DW_FORM_data4:
18489 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18490 info_ptr += 4;
18491 break;
18492 case DW_FORM_data8:
18493 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18494 info_ptr += 8;
18495 break;
0224619f
JK
18496 case DW_FORM_data16:
18497 blk = dwarf_alloc_block (cu);
18498 blk->size = 16;
18499 blk->data = read_n_bytes (abfd, info_ptr, 16);
18500 info_ptr += 16;
18501 DW_BLOCK (attr) = blk;
18502 break;
2dc7f7b3 18503 case DW_FORM_sec_offset:
8266302d 18504 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18505 info_ptr += bytes_read;
18506 break;
c906108c 18507 case DW_FORM_string:
9b1c24c8 18508 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18509 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18510 info_ptr += bytes_read;
18511 break;
4bdf3d34 18512 case DW_FORM_strp:
36586728
TT
18513 if (!cu->per_cu->is_dwz)
18514 {
ed2dc618
SM
18515 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18516 abfd, info_ptr, cu_header,
36586728
TT
18517 &bytes_read);
18518 DW_STRING_IS_CANONICAL (attr) = 0;
18519 info_ptr += bytes_read;
18520 break;
18521 }
18522 /* FALLTHROUGH */
43988095
JK
18523 case DW_FORM_line_strp:
18524 if (!cu->per_cu->is_dwz)
18525 {
ed2dc618
SM
18526 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18527 abfd, info_ptr,
43988095
JK
18528 cu_header, &bytes_read);
18529 DW_STRING_IS_CANONICAL (attr) = 0;
18530 info_ptr += bytes_read;
18531 break;
18532 }
18533 /* FALLTHROUGH */
36586728
TT
18534 case DW_FORM_GNU_strp_alt:
18535 {
ed2dc618 18536 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18537 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18538 &bytes_read);
36586728 18539
ed2dc618
SM
18540 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18541 dwz, str_offset);
36586728
TT
18542 DW_STRING_IS_CANONICAL (attr) = 0;
18543 info_ptr += bytes_read;
18544 }
4bdf3d34 18545 break;
2dc7f7b3 18546 case DW_FORM_exprloc:
c906108c 18547 case DW_FORM_block:
7b5a2f43 18548 blk = dwarf_alloc_block (cu);
c906108c
SS
18549 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18550 info_ptr += bytes_read;
18551 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18552 info_ptr += blk->size;
18553 DW_BLOCK (attr) = blk;
18554 break;
18555 case DW_FORM_block1:
7b5a2f43 18556 blk = dwarf_alloc_block (cu);
c906108c
SS
18557 blk->size = read_1_byte (abfd, info_ptr);
18558 info_ptr += 1;
18559 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18560 info_ptr += blk->size;
18561 DW_BLOCK (attr) = blk;
18562 break;
18563 case DW_FORM_data1:
18564 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18565 info_ptr += 1;
18566 break;
18567 case DW_FORM_flag:
18568 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18569 info_ptr += 1;
18570 break;
2dc7f7b3
TT
18571 case DW_FORM_flag_present:
18572 DW_UNSND (attr) = 1;
18573 break;
c906108c
SS
18574 case DW_FORM_sdata:
18575 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18576 info_ptr += bytes_read;
18577 break;
18578 case DW_FORM_udata:
18a8505e 18579 case DW_FORM_rnglistx:
c906108c
SS
18580 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18581 info_ptr += bytes_read;
18582 break;
18583 case DW_FORM_ref1:
9c541725 18584 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18585 + read_1_byte (abfd, info_ptr));
c906108c
SS
18586 info_ptr += 1;
18587 break;
18588 case DW_FORM_ref2:
9c541725 18589 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18590 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18591 info_ptr += 2;
18592 break;
18593 case DW_FORM_ref4:
9c541725 18594 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18595 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18596 info_ptr += 4;
18597 break;
613e1657 18598 case DW_FORM_ref8:
9c541725 18599 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18600 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18601 info_ptr += 8;
18602 break;
55f1336d 18603 case DW_FORM_ref_sig8:
ac9ec31b 18604 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18605 info_ptr += 8;
18606 break;
c906108c 18607 case DW_FORM_ref_udata:
9c541725 18608 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18609 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18610 info_ptr += bytes_read;
18611 break;
c906108c 18612 case DW_FORM_indirect:
a8329558
KW
18613 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18614 info_ptr += bytes_read;
43988095
JK
18615 if (form == DW_FORM_implicit_const)
18616 {
18617 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18618 info_ptr += bytes_read;
18619 }
18620 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18621 info_ptr, need_reprocess);
43988095
JK
18622 break;
18623 case DW_FORM_implicit_const:
18624 DW_SND (attr) = implicit_const;
a8329558 18625 break;
336d760d 18626 case DW_FORM_addrx:
3019eac3 18627 case DW_FORM_GNU_addr_index:
18a8505e
AT
18628 *need_reprocess = true;
18629 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18630 info_ptr += bytes_read;
18631 break;
cf532bd1 18632 case DW_FORM_strx:
15f18d14
AT
18633 case DW_FORM_strx1:
18634 case DW_FORM_strx2:
18635 case DW_FORM_strx3:
18636 case DW_FORM_strx4:
3019eac3 18637 case DW_FORM_GNU_str_index:
3019eac3 18638 {
15f18d14
AT
18639 ULONGEST str_index;
18640 if (form == DW_FORM_strx1)
18641 {
18642 str_index = read_1_byte (abfd, info_ptr);
18643 info_ptr += 1;
18644 }
18645 else if (form == DW_FORM_strx2)
18646 {
18647 str_index = read_2_bytes (abfd, info_ptr);
18648 info_ptr += 2;
18649 }
18650 else if (form == DW_FORM_strx3)
18651 {
18652 str_index = read_3_bytes (abfd, info_ptr);
18653 info_ptr += 3;
18654 }
18655 else if (form == DW_FORM_strx4)
18656 {
18657 str_index = read_4_bytes (abfd, info_ptr);
18658 info_ptr += 4;
18659 }
18660 else
18661 {
18662 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18663 info_ptr += bytes_read;
18664 }
18a8505e
AT
18665 *need_reprocess = true;
18666 DW_UNSND (attr) = str_index;
18667 }
3019eac3 18668 break;
c906108c 18669 default:
8a3fe4f8 18670 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18671 dwarf_form_name (form),
18672 bfd_get_filename (abfd));
c906108c 18673 }
28e94949 18674
36586728 18675 /* Super hack. */
cd6c91b4 18676 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18677 attr->form = DW_FORM_GNU_ref_alt;
18678
28e94949
JB
18679 /* We have seen instances where the compiler tried to emit a byte
18680 size attribute of -1 which ended up being encoded as an unsigned
18681 0xffffffff. Although 0xffffffff is technically a valid size value,
18682 an object of this size seems pretty unlikely so we can relatively
18683 safely treat these cases as if the size attribute was invalid and
18684 treat them as zero by default. */
18685 if (attr->name == DW_AT_byte_size
18686 && form == DW_FORM_data4
18687 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18688 {
18689 complaint
b98664d3 18690 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18691 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18692 DW_UNSND (attr) = 0;
18693 }
28e94949 18694
c906108c
SS
18695 return info_ptr;
18696}
18697
a8329558
KW
18698/* Read an attribute described by an abbreviated attribute. */
18699
d521ce57 18700static const gdb_byte *
dee91e82
DE
18701read_attribute (const struct die_reader_specs *reader,
18702 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18703 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18704{
18705 attr->name = abbrev->name;
43988095 18706 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18707 abbrev->implicit_const, info_ptr,
18708 need_reprocess);
a8329558
KW
18709}
18710
c764a876
DE
18711/* Cover function for read_initial_length.
18712 Returns the length of the object at BUF, and stores the size of the
18713 initial length in *BYTES_READ and stores the size that offsets will be in
18714 *OFFSET_SIZE.
18715 If the initial length size is not equivalent to that specified in
18716 CU_HEADER then issue a complaint.
18717 This is useful when reading non-comp-unit headers. */
dd373385 18718
c764a876 18719static LONGEST
d521ce57 18720read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18721 const struct comp_unit_head *cu_header,
18722 unsigned int *bytes_read,
18723 unsigned int *offset_size)
18724{
18725 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18726
18727 gdb_assert (cu_header->initial_length_size == 4
18728 || cu_header->initial_length_size == 8
18729 || cu_header->initial_length_size == 12);
18730
18731 if (cu_header->initial_length_size != *bytes_read)
b98664d3 18732 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18733
c764a876 18734 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18735 return length;
613e1657
KB
18736}
18737
43988095
JK
18738/* Return pointer to string at section SECT offset STR_OFFSET with error
18739 reporting strings FORM_NAME and SECT_NAME. */
18740
d521ce57 18741static const char *
ed2dc618
SM
18742read_indirect_string_at_offset_from (struct objfile *objfile,
18743 bfd *abfd, LONGEST str_offset,
43988095
JK
18744 struct dwarf2_section_info *sect,
18745 const char *form_name,
18746 const char *sect_name)
18747{
96b79293 18748 sect->read (objfile);
43988095
JK
18749 if (sect->buffer == NULL)
18750 error (_("%s used without %s section [in module %s]"),
18751 form_name, sect_name, bfd_get_filename (abfd));
18752 if (str_offset >= sect->size)
18753 error (_("%s pointing outside of %s section [in module %s]"),
18754 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18755 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18756 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18757 return NULL;
43988095
JK
18758 return (const char *) (sect->buffer + str_offset);
18759}
18760
18761/* Return pointer to string at .debug_str offset STR_OFFSET. */
18762
18763static const char *
ed2dc618
SM
18764read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18765 bfd *abfd, LONGEST str_offset)
43988095 18766{
ed2dc618
SM
18767 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18768 abfd, str_offset,
43988095
JK
18769 &dwarf2_per_objfile->str,
18770 "DW_FORM_strp", ".debug_str");
18771}
18772
18773/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18774
18775static const char *
ed2dc618
SM
18776read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18777 bfd *abfd, LONGEST str_offset)
43988095 18778{
ed2dc618
SM
18779 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18780 abfd, str_offset,
43988095
JK
18781 &dwarf2_per_objfile->line_str,
18782 "DW_FORM_line_strp",
18783 ".debug_line_str");
c906108c
SS
18784}
18785
36586728
TT
18786/* Read a string at offset STR_OFFSET in the .debug_str section from
18787 the .dwz file DWZ. Throw an error if the offset is too large. If
18788 the string consists of a single NUL byte, return NULL; otherwise
18789 return a pointer to the string. */
18790
d521ce57 18791static const char *
ed2dc618
SM
18792read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18793 LONGEST str_offset)
36586728 18794{
96b79293 18795 dwz->str.read (objfile);
36586728
TT
18796
18797 if (dwz->str.buffer == NULL)
18798 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18799 "section [in module %s]"),
00f93c44 18800 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18801 if (str_offset >= dwz->str.size)
18802 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18803 ".debug_str section [in module %s]"),
00f93c44 18804 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18805 gdb_assert (HOST_CHAR_BIT == 8);
18806 if (dwz->str.buffer[str_offset] == '\0')
18807 return NULL;
d521ce57 18808 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
18809}
18810
43988095
JK
18811/* Return pointer to string at .debug_str offset as read from BUF.
18812 BUF is assumed to be in a compilation unit described by CU_HEADER.
18813 Return *BYTES_READ_PTR count of bytes read from BUF. */
18814
d521ce57 18815static const char *
ed2dc618
SM
18816read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18817 const gdb_byte *buf,
cf2c3c16
TT
18818 const struct comp_unit_head *cu_header,
18819 unsigned int *bytes_read_ptr)
18820{
8266302d 18821 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18822
ed2dc618 18823 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
18824}
18825
43988095
JK
18826/* Return pointer to string at .debug_line_str offset as read from BUF.
18827 BUF is assumed to be in a compilation unit described by CU_HEADER.
18828 Return *BYTES_READ_PTR count of bytes read from BUF. */
18829
18830static const char *
ed2dc618
SM
18831read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18832 bfd *abfd, const gdb_byte *buf,
43988095
JK
18833 const struct comp_unit_head *cu_header,
18834 unsigned int *bytes_read_ptr)
18835{
8266302d 18836 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18837
ed2dc618
SM
18838 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18839 str_offset);
43988095
JK
18840}
18841
3019eac3 18842/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18843 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18844 ADDR_SIZE is the size of addresses from the CU header. */
18845
18846static CORE_ADDR
ed2dc618 18847read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18848 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18849 int addr_size)
3019eac3
DE
18850{
18851 struct objfile *objfile = dwarf2_per_objfile->objfile;
18852 bfd *abfd = objfile->obfd;
18853 const gdb_byte *info_ptr;
18a8505e 18854 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18855
96b79293 18856 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18857 if (dwarf2_per_objfile->addr.buffer == NULL)
18858 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18859 objfile_name (objfile));
18a8505e
AT
18860 if (addr_base_or_zero + addr_index * addr_size
18861 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18862 error (_("DW_FORM_addr_index pointing outside of "
18863 ".debug_addr section [in module %s]"),
4262abfb 18864 objfile_name (objfile));
3019eac3 18865 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18866 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18867 if (addr_size == 4)
18868 return bfd_get_32 (abfd, info_ptr);
18869 else
18870 return bfd_get_64 (abfd, info_ptr);
18871}
18872
18873/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18874
18875static CORE_ADDR
18876read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18877{
518817b3
SM
18878 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18879 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18880}
18881
18882/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18883
18884static CORE_ADDR
d521ce57 18885read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18886 unsigned int *bytes_read)
18887{
518817b3 18888 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18889 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18890
18891 return read_addr_index (cu, addr_index);
18892}
18893
450a1bfc 18894/* See read.h. */
3019eac3
DE
18895
18896CORE_ADDR
450a1bfc 18897dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18898{
ed2dc618 18899 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18900 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18901 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18902 int addr_size;
18903
3019eac3
DE
18904 /* We need addr_base and addr_size.
18905 If we don't have PER_CU->cu, we have to get it.
18906 Nasty, but the alternative is storing the needed info in PER_CU,
18907 which at this point doesn't seem justified: it's not clear how frequently
18908 it would get used and it would increase the size of every PER_CU.
18909 Entry points like dwarf2_per_cu_addr_size do a similar thing
18910 so we're not in uncharted territory here.
18911 Alas we need to be a bit more complicated as addr_base is contained
18912 in the DIE.
18913
18914 We don't need to read the entire CU(/TU).
18915 We just need the header and top level die.
a1b64ce1 18916
3019eac3 18917 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18918 For now we skip this optimization. */
3019eac3
DE
18919
18920 if (cu != NULL)
18921 {
18922 addr_base = cu->addr_base;
18923 addr_size = cu->header.addr_size;
18924 }
18925 else
18926 {
6751ebae 18927 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18928 addr_base = reader.cu->addr_base;
18929 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18930 }
18931
ed2dc618
SM
18932 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18933 addr_size);
3019eac3
DE
18934}
18935
18a8505e
AT
18936/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18937 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18938 DWO file. */
3019eac3 18939
d521ce57 18940static const char *
18a8505e
AT
18941read_str_index (struct dwarf2_cu *cu,
18942 struct dwarf2_section_info *str_section,
18943 struct dwarf2_section_info *str_offsets_section,
18944 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18945{
518817b3
SM
18946 struct dwarf2_per_objfile *dwarf2_per_objfile
18947 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18948 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18949 const char *objf_name = objfile_name (objfile);
3019eac3 18950 bfd *abfd = objfile->obfd;
d521ce57 18951 const gdb_byte *info_ptr;
3019eac3 18952 ULONGEST str_offset;
cf532bd1 18953 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18954
96b79293
TT
18955 str_section->read (objfile);
18956 str_offsets_section->read (objfile);
73869dc2 18957 if (str_section->buffer == NULL)
18a8505e 18958 error (_("%s used without %s section"
9d8780f0 18959 " in CU at offset %s [in module %s]"),
96b79293 18960 form_name, str_section->get_name (),
18a8505e 18961 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18962 if (str_offsets_section->buffer == NULL)
18a8505e 18963 error (_("%s used without %s section"
9d8780f0 18964 " in CU at offset %s [in module %s]"),
96b79293 18965 form_name, str_section->get_name (),
18a8505e 18966 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18967 info_ptr = (str_offsets_section->buffer
18a8505e 18968 + str_offsets_base
3019eac3
DE
18969 + str_index * cu->header.offset_size);
18970 if (cu->header.offset_size == 4)
18971 str_offset = bfd_get_32 (abfd, info_ptr);
18972 else
18973 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18974 if (str_offset >= str_section->size)
57d63ce2 18975 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18976 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18977 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18978 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18979}
18980
18a8505e
AT
18981/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18982
18983static const char *
18984read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18985{
18986 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18987 ? reader->cu->header.addr_size : 0;
18988 return read_str_index (reader->cu,
18989 &reader->dwo_file->sections.str,
18990 &reader->dwo_file->sections.str_offsets,
18991 str_offsets_base, str_index);
18992}
18993
18994/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18995
18996static const char *
18997read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18998{
18999 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19000 const char *objf_name = objfile_name (objfile);
19001 static const char form_name[] = "DW_FORM_GNU_str_index";
19002 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19003
19004 if (!cu->str_offsets_base.has_value ())
19005 error (_("%s used in Fission stub without %s"
19006 " in CU at offset 0x%lx [in module %s]"),
19007 form_name, str_offsets_attr_name,
19008 (long) cu->header.offset_size, objf_name);
19009
19010 return read_str_index (cu,
19011 &cu->per_cu->dwarf2_per_objfile->str,
19012 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19013 *cu->str_offsets_base, str_index);
19014}
19015
3019eac3
DE
19016/* Return the length of an LEB128 number in BUF. */
19017
19018static int
19019leb128_size (const gdb_byte *buf)
19020{
19021 const gdb_byte *begin = buf;
19022 gdb_byte byte;
19023
19024 while (1)
19025 {
19026 byte = *buf++;
19027 if ((byte & 128) == 0)
19028 return buf - begin;
19029 }
19030}
19031
c906108c 19032static void
e142c38c 19033set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19034{
19035 switch (lang)
19036 {
19037 case DW_LANG_C89:
76bee0cc 19038 case DW_LANG_C99:
0cfd832f 19039 case DW_LANG_C11:
c906108c 19040 case DW_LANG_C:
d1be3247 19041 case DW_LANG_UPC:
e142c38c 19042 cu->language = language_c;
c906108c 19043 break;
9c37b5ae 19044 case DW_LANG_Java:
c906108c 19045 case DW_LANG_C_plus_plus:
0cfd832f
MW
19046 case DW_LANG_C_plus_plus_11:
19047 case DW_LANG_C_plus_plus_14:
e142c38c 19048 cu->language = language_cplus;
c906108c 19049 break;
6aecb9c2
JB
19050 case DW_LANG_D:
19051 cu->language = language_d;
19052 break;
c906108c
SS
19053 case DW_LANG_Fortran77:
19054 case DW_LANG_Fortran90:
b21b22e0 19055 case DW_LANG_Fortran95:
f7de9aab
MW
19056 case DW_LANG_Fortran03:
19057 case DW_LANG_Fortran08:
e142c38c 19058 cu->language = language_fortran;
c906108c 19059 break;
a766d390
DE
19060 case DW_LANG_Go:
19061 cu->language = language_go;
19062 break;
c906108c 19063 case DW_LANG_Mips_Assembler:
e142c38c 19064 cu->language = language_asm;
c906108c
SS
19065 break;
19066 case DW_LANG_Ada83:
8aaf0b47 19067 case DW_LANG_Ada95:
bc5f45f8
JB
19068 cu->language = language_ada;
19069 break;
72019c9c
GM
19070 case DW_LANG_Modula2:
19071 cu->language = language_m2;
19072 break;
fe8e67fd
PM
19073 case DW_LANG_Pascal83:
19074 cu->language = language_pascal;
19075 break;
22566fbd
DJ
19076 case DW_LANG_ObjC:
19077 cu->language = language_objc;
19078 break;
c44af4eb
TT
19079 case DW_LANG_Rust:
19080 case DW_LANG_Rust_old:
19081 cu->language = language_rust;
19082 break;
c906108c
SS
19083 case DW_LANG_Cobol74:
19084 case DW_LANG_Cobol85:
c906108c 19085 default:
e142c38c 19086 cu->language = language_minimal;
c906108c
SS
19087 break;
19088 }
e142c38c 19089 cu->language_defn = language_def (cu->language);
c906108c
SS
19090}
19091
19092/* Return the named attribute or NULL if not there. */
19093
19094static struct attribute *
e142c38c 19095dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19096{
a48e046c 19097 for (;;)
c906108c 19098 {
a48e046c
TT
19099 unsigned int i;
19100 struct attribute *spec = NULL;
19101
19102 for (i = 0; i < die->num_attrs; ++i)
19103 {
19104 if (die->attrs[i].name == name)
19105 return &die->attrs[i];
19106 if (die->attrs[i].name == DW_AT_specification
19107 || die->attrs[i].name == DW_AT_abstract_origin)
19108 spec = &die->attrs[i];
19109 }
19110
19111 if (!spec)
19112 break;
c906108c 19113
f2f0e013 19114 die = follow_die_ref (die, spec, &cu);
f2f0e013 19115 }
c5aa993b 19116
c906108c
SS
19117 return NULL;
19118}
19119
348e048f
DE
19120/* Return the named attribute or NULL if not there,
19121 but do not follow DW_AT_specification, etc.
19122 This is for use in contexts where we're reading .debug_types dies.
19123 Following DW_AT_specification, DW_AT_abstract_origin will take us
19124 back up the chain, and we want to go down. */
19125
19126static struct attribute *
45e58e77 19127dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19128{
19129 unsigned int i;
19130
19131 for (i = 0; i < die->num_attrs; ++i)
19132 if (die->attrs[i].name == name)
19133 return &die->attrs[i];
19134
19135 return NULL;
19136}
19137
7d45c7c3
KB
19138/* Return the string associated with a string-typed attribute, or NULL if it
19139 is either not found or is of an incorrect type. */
19140
19141static const char *
19142dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19143{
19144 struct attribute *attr;
19145 const char *str = NULL;
19146
19147 attr = dwarf2_attr (die, name, cu);
19148
19149 if (attr != NULL)
19150 {
43988095 19151 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19152 || attr->form == DW_FORM_string
cf532bd1 19153 || attr->form == DW_FORM_strx
8fe0f950
AT
19154 || attr->form == DW_FORM_strx1
19155 || attr->form == DW_FORM_strx2
19156 || attr->form == DW_FORM_strx3
19157 || attr->form == DW_FORM_strx4
b3340438 19158 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19159 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19160 str = DW_STRING (attr);
19161 else
b98664d3 19162 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19163 "DIE at %s in module %s"),
19164 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19165 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19166 }
19167
19168 return str;
19169}
19170
a084a2a6 19171/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19172 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19173static const char *
19174dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19175{
19176 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19177 if (dwo_name == nullptr)
19178 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19179 return dwo_name;
19180}
19181
05cf31d1
JB
19182/* Return non-zero iff the attribute NAME is defined for the given DIE,
19183 and holds a non-zero value. This function should only be used for
2dc7f7b3 19184 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19185
19186static int
19187dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19188{
19189 struct attribute *attr = dwarf2_attr (die, name, cu);
19190
19191 return (attr && DW_UNSND (attr));
19192}
19193
3ca72b44 19194static int
e142c38c 19195die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19196{
05cf31d1
JB
19197 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19198 which value is non-zero. However, we have to be careful with
19199 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19200 (via dwarf2_flag_true_p) follows this attribute. So we may
19201 end up accidently finding a declaration attribute that belongs
19202 to a different DIE referenced by the specification attribute,
19203 even though the given DIE does not have a declaration attribute. */
19204 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19205 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19206}
19207
63d06c5c 19208/* Return the die giving the specification for DIE, if there is
f2f0e013 19209 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19210 containing the return value on output. If there is no
19211 specification, but there is an abstract origin, that is
19212 returned. */
63d06c5c
DC
19213
19214static struct die_info *
f2f0e013 19215die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19216{
f2f0e013
DJ
19217 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19218 *spec_cu);
63d06c5c 19219
edb3359d
DJ
19220 if (spec_attr == NULL)
19221 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19222
63d06c5c
DC
19223 if (spec_attr == NULL)
19224 return NULL;
19225 else
f2f0e013 19226 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19227}
c906108c 19228
527f3840
JK
19229/* Stub for free_line_header to match void * callback types. */
19230
19231static void
19232free_line_header_voidp (void *arg)
19233{
9a3c8263 19234 struct line_header *lh = (struct line_header *) arg;
527f3840 19235
fff8551c 19236 delete lh;
527f3840
JK
19237}
19238
83769d0b 19239/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19240
19241static struct dwarf2_section_info *
19242get_debug_line_section (struct dwarf2_cu *cu)
19243{
19244 struct dwarf2_section_info *section;
518817b3
SM
19245 struct dwarf2_per_objfile *dwarf2_per_objfile
19246 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19247
19248 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19249 DWO file. */
19250 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19251 section = &cu->dwo_unit->dwo_file->sections.line;
19252 else if (cu->per_cu->is_dwz)
19253 {
ed2dc618 19254 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19255
19256 section = &dwz->line;
19257 }
19258 else
19259 section = &dwarf2_per_objfile->line;
19260
19261 return section;
19262}
19263
43988095
JK
19264/* Read directory or file name entry format, starting with byte of
19265 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19266 entries count and the entries themselves in the described entry
19267 format. */
19268
19269static void
ed2dc618
SM
19270read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19271 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19272 struct line_header *lh,
19273 const struct comp_unit_head *cu_header,
19274 void (*callback) (struct line_header *lh,
19275 const char *name,
ecfb656c 19276 dir_index d_index,
43988095
JK
19277 unsigned int mod_time,
19278 unsigned int length))
19279{
19280 gdb_byte format_count, formati;
19281 ULONGEST data_count, datai;
19282 const gdb_byte *buf = *bufp;
19283 const gdb_byte *format_header_data;
43988095
JK
19284 unsigned int bytes_read;
19285
19286 format_count = read_1_byte (abfd, buf);
19287 buf += 1;
19288 format_header_data = buf;
19289 for (formati = 0; formati < format_count; formati++)
19290 {
19291 read_unsigned_leb128 (abfd, buf, &bytes_read);
19292 buf += bytes_read;
19293 read_unsigned_leb128 (abfd, buf, &bytes_read);
19294 buf += bytes_read;
19295 }
19296
19297 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19298 buf += bytes_read;
19299 for (datai = 0; datai < data_count; datai++)
19300 {
19301 const gdb_byte *format = format_header_data;
19302 struct file_entry fe;
19303
43988095
JK
19304 for (formati = 0; formati < format_count; formati++)
19305 {
ecfb656c 19306 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19307 format += bytes_read;
43988095 19308
ecfb656c 19309 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19310 format += bytes_read;
ecfb656c
PA
19311
19312 gdb::optional<const char *> string;
19313 gdb::optional<unsigned int> uint;
19314
43988095
JK
19315 switch (form)
19316 {
19317 case DW_FORM_string:
ecfb656c 19318 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19319 buf += bytes_read;
19320 break;
19321
19322 case DW_FORM_line_strp:
ed2dc618
SM
19323 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19324 abfd, buf,
ecfb656c
PA
19325 cu_header,
19326 &bytes_read));
43988095
JK
19327 buf += bytes_read;
19328 break;
19329
19330 case DW_FORM_data1:
ecfb656c 19331 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19332 buf += 1;
19333 break;
19334
19335 case DW_FORM_data2:
ecfb656c 19336 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19337 buf += 2;
19338 break;
19339
19340 case DW_FORM_data4:
ecfb656c 19341 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19342 buf += 4;
19343 break;
19344
19345 case DW_FORM_data8:
ecfb656c 19346 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19347 buf += 8;
19348 break;
19349
7ba99d21
AT
19350 case DW_FORM_data16:
19351 /* This is used for MD5, but file_entry does not record MD5s. */
19352 buf += 16;
19353 break;
19354
43988095 19355 case DW_FORM_udata:
ecfb656c 19356 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19357 buf += bytes_read;
19358 break;
19359
19360 case DW_FORM_block:
19361 /* It is valid only for DW_LNCT_timestamp which is ignored by
19362 current GDB. */
19363 break;
19364 }
ecfb656c
PA
19365
19366 switch (content_type)
19367 {
19368 case DW_LNCT_path:
19369 if (string.has_value ())
19370 fe.name = *string;
19371 break;
19372 case DW_LNCT_directory_index:
19373 if (uint.has_value ())
19374 fe.d_index = (dir_index) *uint;
19375 break;
19376 case DW_LNCT_timestamp:
19377 if (uint.has_value ())
19378 fe.mod_time = *uint;
19379 break;
19380 case DW_LNCT_size:
19381 if (uint.has_value ())
19382 fe.length = *uint;
19383 break;
19384 case DW_LNCT_MD5:
19385 break;
19386 default:
b98664d3 19387 complaint (_("Unknown format content type %s"),
ecfb656c
PA
19388 pulongest (content_type));
19389 }
43988095
JK
19390 }
19391
ecfb656c 19392 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
19393 }
19394
19395 *bufp = buf;
19396}
19397
debd256d 19398/* Read the statement program header starting at OFFSET in
3019eac3 19399 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19400 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19401 Returns NULL if there is a problem reading the header, e.g., if it
19402 has a version we don't understand.
debd256d
JB
19403
19404 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19405 the returned object point into the dwarf line section buffer,
19406 and must not be freed. */
ae2de4f8 19407
fff8551c 19408static line_header_up
9c541725 19409dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19410{
d521ce57 19411 const gdb_byte *line_ptr;
c764a876 19412 unsigned int bytes_read, offset_size;
debd256d 19413 int i;
d521ce57 19414 const char *cur_dir, *cur_file;
3019eac3
DE
19415 struct dwarf2_section_info *section;
19416 bfd *abfd;
518817b3
SM
19417 struct dwarf2_per_objfile *dwarf2_per_objfile
19418 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19419
36586728 19420 section = get_debug_line_section (cu);
96b79293 19421 section->read (dwarf2_per_objfile->objfile);
3019eac3 19422 if (section->buffer == NULL)
debd256d 19423 {
3019eac3 19424 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19425 complaint (_("missing .debug_line.dwo section"));
3019eac3 19426 else
b98664d3 19427 complaint (_("missing .debug_line section"));
debd256d
JB
19428 return 0;
19429 }
19430
fceca515
DE
19431 /* We can't do this until we know the section is non-empty.
19432 Only then do we know we have such a section. */
96b79293 19433 abfd = section->get_bfd_owner ();
fceca515 19434
a738430d
MK
19435 /* Make sure that at least there's room for the total_length field.
19436 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 19437 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 19438 {
4d3c2250 19439 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19440 return 0;
19441 }
19442
fff8551c 19443 line_header_up lh (new line_header ());
debd256d 19444
9c541725 19445 lh->sect_off = sect_off;
527f3840
JK
19446 lh->offset_in_dwz = cu->per_cu->is_dwz;
19447
9c541725 19448 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 19449
a738430d 19450 /* Read in the header. */
6e70227d 19451 lh->total_length =
c764a876
DE
19452 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19453 &bytes_read, &offset_size);
debd256d 19454 line_ptr += bytes_read;
7ba99d21
AT
19455
19456 const gdb_byte *start_here = line_ptr;
19457
3019eac3 19458 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 19459 {
4d3c2250 19460 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19461 return 0;
19462 }
7ba99d21 19463 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
19464 lh->version = read_2_bytes (abfd, line_ptr);
19465 line_ptr += 2;
43988095 19466 if (lh->version > 5)
cd366ee8
DE
19467 {
19468 /* This is a version we don't understand. The format could have
19469 changed in ways we don't handle properly so just punt. */
b98664d3 19470 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
19471 return NULL;
19472 }
43988095
JK
19473 if (lh->version >= 5)
19474 {
19475 gdb_byte segment_selector_size;
19476
19477 /* Skip address size. */
19478 read_1_byte (abfd, line_ptr);
19479 line_ptr += 1;
19480
19481 segment_selector_size = read_1_byte (abfd, line_ptr);
19482 line_ptr += 1;
19483 if (segment_selector_size != 0)
19484 {
b98664d3 19485 complaint (_("unsupported segment selector size %u "
43988095
JK
19486 "in .debug_line section"),
19487 segment_selector_size);
19488 return NULL;
19489 }
19490 }
24aa364d 19491 lh->header_length = read_offset (abfd, line_ptr, offset_size);
c764a876 19492 line_ptr += offset_size;
7ba99d21 19493 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
19494 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19495 line_ptr += 1;
2dc7f7b3
TT
19496 if (lh->version >= 4)
19497 {
19498 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19499 line_ptr += 1;
19500 }
19501 else
19502 lh->maximum_ops_per_instruction = 1;
19503
19504 if (lh->maximum_ops_per_instruction == 0)
19505 {
19506 lh->maximum_ops_per_instruction = 1;
b98664d3 19507 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 19508 "in `.debug_line' section"));
2dc7f7b3
TT
19509 }
19510
debd256d
JB
19511 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19512 line_ptr += 1;
19513 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19514 line_ptr += 1;
19515 lh->line_range = read_1_byte (abfd, line_ptr);
19516 line_ptr += 1;
19517 lh->opcode_base = read_1_byte (abfd, line_ptr);
19518 line_ptr += 1;
fff8551c 19519 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
19520
19521 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19522 for (i = 1; i < lh->opcode_base; ++i)
19523 {
19524 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19525 line_ptr += 1;
19526 }
19527
43988095 19528 if (lh->version >= 5)
debd256d 19529 {
43988095 19530 /* Read directory table. */
ed2dc618
SM
19531 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19532 &cu->header,
b926417a 19533 [] (struct line_header *header, const char *name,
ecfb656c 19534 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19535 unsigned int length)
19536 {
b926417a 19537 header->add_include_dir (name);
fff8551c 19538 });
debd256d 19539
43988095 19540 /* Read file name table. */
ed2dc618
SM
19541 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19542 &cu->header,
b926417a 19543 [] (struct line_header *header, const char *name,
ecfb656c 19544 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19545 unsigned int length)
19546 {
b926417a 19547 header->add_file_name (name, d_index, mod_time, length);
fff8551c 19548 });
43988095
JK
19549 }
19550 else
debd256d 19551 {
43988095
JK
19552 /* Read directory table. */
19553 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19554 {
19555 line_ptr += bytes_read;
fff8551c 19556 lh->add_include_dir (cur_dir);
43988095 19557 }
debd256d
JB
19558 line_ptr += bytes_read;
19559
43988095
JK
19560 /* Read file name table. */
19561 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19562 {
ecfb656c
PA
19563 unsigned int mod_time, length;
19564 dir_index d_index;
43988095
JK
19565
19566 line_ptr += bytes_read;
ecfb656c 19567 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
19568 line_ptr += bytes_read;
19569 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19570 line_ptr += bytes_read;
19571 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19572 line_ptr += bytes_read;
19573
ecfb656c 19574 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
19575 }
19576 line_ptr += bytes_read;
debd256d 19577 }
debd256d 19578
3019eac3 19579 if (line_ptr > (section->buffer + section->size))
b98664d3 19580 complaint (_("line number info header doesn't "
3e43a32a 19581 "fit in `.debug_line' section"));
debd256d 19582
debd256d
JB
19583 return lh;
19584}
c906108c 19585
c6da4cef 19586/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19587 Return the file name of the psymtab for the given file_entry.
c6da4cef 19588 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19589 If space for the result is malloc'd, *NAME_HOLDER will be set.
19590 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19591
d521ce57 19592static const char *
7ba99d21 19593psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19594 const dwarf2_psymtab *pst,
c89b44cd
TT
19595 const char *comp_dir,
19596 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19597{
d521ce57
TT
19598 const char *include_name = fe.name;
19599 const char *include_name_to_compare = include_name;
72b9f47f 19600 const char *pst_filename;
c6da4cef
DE
19601 int file_is_pst;
19602
8c43009f 19603 const char *dir_name = fe.include_dir (lh);
c6da4cef 19604
c89b44cd 19605 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19606 if (!IS_ABSOLUTE_PATH (include_name)
19607 && (dir_name != NULL || comp_dir != NULL))
19608 {
19609 /* Avoid creating a duplicate psymtab for PST.
19610 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19611 Before we do the comparison, however, we need to account
19612 for DIR_NAME and COMP_DIR.
19613 First prepend dir_name (if non-NULL). If we still don't
19614 have an absolute path prepend comp_dir (if non-NULL).
19615 However, the directory we record in the include-file's
19616 psymtab does not contain COMP_DIR (to match the
19617 corresponding symtab(s)).
19618
19619 Example:
19620
19621 bash$ cd /tmp
19622 bash$ gcc -g ./hello.c
19623 include_name = "hello.c"
19624 dir_name = "."
19625 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19626 DW_AT_name = "./hello.c"
19627
19628 */
c6da4cef
DE
19629
19630 if (dir_name != NULL)
19631 {
c89b44cd
TT
19632 name_holder->reset (concat (dir_name, SLASH_STRING,
19633 include_name, (char *) NULL));
19634 include_name = name_holder->get ();
c6da4cef 19635 include_name_to_compare = include_name;
c6da4cef
DE
19636 }
19637 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19638 {
c89b44cd
TT
19639 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19640 include_name, (char *) NULL));
19641 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19642 }
19643 }
19644
19645 pst_filename = pst->filename;
c89b44cd 19646 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19647 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19648 {
c89b44cd
TT
19649 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19650 pst_filename, (char *) NULL));
19651 pst_filename = copied_name.get ();
c6da4cef
DE
19652 }
19653
1e3fad37 19654 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19655
c6da4cef
DE
19656 if (file_is_pst)
19657 return NULL;
19658 return include_name;
19659}
19660
d9b3de22
DE
19661/* State machine to track the state of the line number program. */
19662
6f77053d 19663class lnp_state_machine
d9b3de22 19664{
6f77053d
PA
19665public:
19666 /* Initialize a machine state for the start of a line number
19667 program. */
804d2729
TT
19668 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19669 bool record_lines_p);
6f77053d 19670
8c43009f
PA
19671 file_entry *current_file ()
19672 {
19673 /* lh->file_names is 0-based, but the file name numbers in the
19674 statement program are 1-based. */
6f77053d
PA
19675 return m_line_header->file_name_at (m_file);
19676 }
19677
19678 /* Record the line in the state machine. END_SEQUENCE is true if
19679 we're processing the end of a sequence. */
19680 void record_line (bool end_sequence);
19681
7ab6656f
OJ
19682 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19683 nop-out rest of the lines in this sequence. */
6f77053d
PA
19684 void check_line_address (struct dwarf2_cu *cu,
19685 const gdb_byte *line_ptr,
7ab6656f 19686 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19687
19688 void handle_set_discriminator (unsigned int discriminator)
19689 {
19690 m_discriminator = discriminator;
19691 m_line_has_non_zero_discriminator |= discriminator != 0;
19692 }
19693
19694 /* Handle DW_LNE_set_address. */
19695 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19696 {
19697 m_op_index = 0;
19698 address += baseaddr;
19699 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19700 }
19701
19702 /* Handle DW_LNS_advance_pc. */
19703 void handle_advance_pc (CORE_ADDR adjust);
19704
19705 /* Handle a special opcode. */
19706 void handle_special_opcode (unsigned char op_code);
19707
19708 /* Handle DW_LNS_advance_line. */
19709 void handle_advance_line (int line_delta)
19710 {
19711 advance_line (line_delta);
19712 }
19713
19714 /* Handle DW_LNS_set_file. */
19715 void handle_set_file (file_name_index file);
19716
19717 /* Handle DW_LNS_negate_stmt. */
19718 void handle_negate_stmt ()
19719 {
19720 m_is_stmt = !m_is_stmt;
19721 }
19722
19723 /* Handle DW_LNS_const_add_pc. */
19724 void handle_const_add_pc ();
19725
19726 /* Handle DW_LNS_fixed_advance_pc. */
19727 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19728 {
19729 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19730 m_op_index = 0;
19731 }
19732
19733 /* Handle DW_LNS_copy. */
19734 void handle_copy ()
19735 {
19736 record_line (false);
19737 m_discriminator = 0;
19738 }
19739
19740 /* Handle DW_LNE_end_sequence. */
19741 void handle_end_sequence ()
19742 {
804d2729 19743 m_currently_recording_lines = true;
6f77053d
PA
19744 }
19745
19746private:
19747 /* Advance the line by LINE_DELTA. */
19748 void advance_line (int line_delta)
19749 {
19750 m_line += line_delta;
19751
19752 if (line_delta != 0)
19753 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19754 }
19755
804d2729
TT
19756 struct dwarf2_cu *m_cu;
19757
6f77053d
PA
19758 gdbarch *m_gdbarch;
19759
19760 /* True if we're recording lines.
19761 Otherwise we're building partial symtabs and are just interested in
19762 finding include files mentioned by the line number program. */
19763 bool m_record_lines_p;
19764
8c43009f 19765 /* The line number header. */
6f77053d 19766 line_header *m_line_header;
8c43009f 19767
6f77053d
PA
19768 /* These are part of the standard DWARF line number state machine,
19769 and initialized according to the DWARF spec. */
d9b3de22 19770
6f77053d 19771 unsigned char m_op_index = 0;
7ba99d21
AT
19772 /* The line table index of the current file. */
19773 file_name_index m_file = 1;
6f77053d
PA
19774 unsigned int m_line = 1;
19775
19776 /* These are initialized in the constructor. */
19777
19778 CORE_ADDR m_address;
19779 bool m_is_stmt;
19780 unsigned int m_discriminator;
d9b3de22
DE
19781
19782 /* Additional bits of state we need to track. */
19783
19784 /* The last file that we called dwarf2_start_subfile for.
19785 This is only used for TLLs. */
6f77053d 19786 unsigned int m_last_file = 0;
d9b3de22 19787 /* The last file a line number was recorded for. */
6f77053d 19788 struct subfile *m_last_subfile = NULL;
d9b3de22 19789
804d2729
TT
19790 /* When true, record the lines we decode. */
19791 bool m_currently_recording_lines = false;
d9b3de22
DE
19792
19793 /* The last line number that was recorded, used to coalesce
19794 consecutive entries for the same line. This can happen, for
19795 example, when discriminators are present. PR 17276. */
6f77053d
PA
19796 unsigned int m_last_line = 0;
19797 bool m_line_has_non_zero_discriminator = false;
8c43009f 19798};
d9b3de22 19799
6f77053d
PA
19800void
19801lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19802{
19803 CORE_ADDR addr_adj = (((m_op_index + adjust)
19804 / m_line_header->maximum_ops_per_instruction)
19805 * m_line_header->minimum_instruction_length);
19806 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19807 m_op_index = ((m_op_index + adjust)
19808 % m_line_header->maximum_ops_per_instruction);
19809}
d9b3de22 19810
6f77053d
PA
19811void
19812lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19813{
6f77053d 19814 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19815 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19816 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19817 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19818 / m_line_header->maximum_ops_per_instruction)
19819 * m_line_header->minimum_instruction_length);
19820 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19821 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19822 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19823
258bf0ee 19824 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19825 advance_line (line_delta);
19826 record_line (false);
19827 m_discriminator = 0;
19828}
d9b3de22 19829
6f77053d
PA
19830void
19831lnp_state_machine::handle_set_file (file_name_index file)
19832{
19833 m_file = file;
19834
19835 const file_entry *fe = current_file ();
19836 if (fe == NULL)
19837 dwarf2_debug_line_missing_file_complaint ();
19838 else if (m_record_lines_p)
19839 {
19840 const char *dir = fe->include_dir (m_line_header);
19841
c24bdb02 19842 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19843 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19844 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19845 }
19846}
19847
19848void
19849lnp_state_machine::handle_const_add_pc ()
19850{
19851 CORE_ADDR adjust
19852 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19853
19854 CORE_ADDR addr_adj
19855 = (((m_op_index + adjust)
19856 / m_line_header->maximum_ops_per_instruction)
19857 * m_line_header->minimum_instruction_length);
19858
19859 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19860 m_op_index = ((m_op_index + adjust)
19861 % m_line_header->maximum_ops_per_instruction);
19862}
d9b3de22 19863
a05a36a5
DE
19864/* Return non-zero if we should add LINE to the line number table.
19865 LINE is the line to add, LAST_LINE is the last line that was added,
19866 LAST_SUBFILE is the subfile for LAST_LINE.
19867 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19868 had a non-zero discriminator.
19869
19870 We have to be careful in the presence of discriminators.
19871 E.g., for this line:
19872
19873 for (i = 0; i < 100000; i++);
19874
19875 clang can emit four line number entries for that one line,
19876 each with a different discriminator.
19877 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19878
19879 However, we want gdb to coalesce all four entries into one.
19880 Otherwise the user could stepi into the middle of the line and
19881 gdb would get confused about whether the pc really was in the
19882 middle of the line.
19883
19884 Things are further complicated by the fact that two consecutive
19885 line number entries for the same line is a heuristic used by gcc
19886 to denote the end of the prologue. So we can't just discard duplicate
19887 entries, we have to be selective about it. The heuristic we use is
19888 that we only collapse consecutive entries for the same line if at least
19889 one of those entries has a non-zero discriminator. PR 17276.
19890
19891 Note: Addresses in the line number state machine can never go backwards
19892 within one sequence, thus this coalescing is ok. */
19893
19894static int
804d2729
TT
19895dwarf_record_line_p (struct dwarf2_cu *cu,
19896 unsigned int line, unsigned int last_line,
a05a36a5
DE
19897 int line_has_non_zero_discriminator,
19898 struct subfile *last_subfile)
19899{
c24bdb02 19900 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19901 return 1;
19902 if (line != last_line)
19903 return 1;
19904 /* Same line for the same file that we've seen already.
19905 As a last check, for pr 17276, only record the line if the line
19906 has never had a non-zero discriminator. */
19907 if (!line_has_non_zero_discriminator)
19908 return 1;
19909 return 0;
19910}
19911
804d2729
TT
19912/* Use the CU's builder to record line number LINE beginning at
19913 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19914
19915static void
d9b3de22
DE
19916dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19917 unsigned int line, CORE_ADDR address,
804d2729 19918 struct dwarf2_cu *cu)
252a6764
DE
19919{
19920 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19921
27e0867f
DE
19922 if (dwarf_line_debug)
19923 {
19924 fprintf_unfiltered (gdb_stdlog,
19925 "Recording line %u, file %s, address %s\n",
19926 line, lbasename (subfile->name),
19927 paddress (gdbarch, address));
19928 }
19929
804d2729 19930 if (cu != nullptr)
c24bdb02 19931 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
19932}
19933
19934/* Subroutine of dwarf_decode_lines_1 to simplify it.
19935 Mark the end of a set of line number records.
d9b3de22 19936 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19937 If SUBFILE is NULL the request is ignored. */
19938
19939static void
19940dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19941 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19942{
27e0867f
DE
19943 if (subfile == NULL)
19944 return;
19945
19946 if (dwarf_line_debug)
19947 {
19948 fprintf_unfiltered (gdb_stdlog,
19949 "Finishing current line, file %s, address %s\n",
19950 lbasename (subfile->name),
19951 paddress (gdbarch, address));
19952 }
19953
804d2729 19954 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
19955}
19956
6f77053d
PA
19957void
19958lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19959{
d9b3de22
DE
19960 if (dwarf_line_debug)
19961 {
19962 fprintf_unfiltered (gdb_stdlog,
19963 "Processing actual line %u: file %u,"
94a72be7 19964 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19965 m_line, m_file,
6f77053d 19966 paddress (m_gdbarch, m_address),
94a72be7
AB
19967 m_is_stmt, m_discriminator,
19968 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19969 }
19970
6f77053d 19971 file_entry *fe = current_file ();
8c43009f
PA
19972
19973 if (fe == NULL)
d9b3de22
DE
19974 dwarf2_debug_line_missing_file_complaint ();
19975 /* For now we ignore lines not starting on an instruction boundary.
19976 But not when processing end_sequence for compatibility with the
19977 previous version of the code. */
6f77053d 19978 else if (m_op_index == 0 || end_sequence)
d9b3de22 19979 {
8c43009f 19980 fe->included_p = 1;
94a72be7
AB
19981 if (m_record_lines_p
19982 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 19983 {
c24bdb02 19984 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19985 || end_sequence)
d9b3de22 19986 {
804d2729
TT
19987 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19988 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19989 }
19990
19991 if (!end_sequence)
19992 {
804d2729 19993 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
19994 m_line_has_non_zero_discriminator,
19995 m_last_subfile))
d9b3de22 19996 {
c24bdb02 19997 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 19998 dwarf_record_line_1 (m_gdbarch,
c24bdb02 19999 builder->get_current_subfile (),
6f77053d 20000 m_line, m_address,
804d2729 20001 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20002 }
c24bdb02 20003 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20004 m_last_line = m_line;
d9b3de22
DE
20005 }
20006 }
20007 }
20008}
20009
804d2729
TT
20010lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20011 line_header *lh, bool record_lines_p)
d9b3de22 20012{
804d2729 20013 m_cu = cu;
6f77053d
PA
20014 m_gdbarch = arch;
20015 m_record_lines_p = record_lines_p;
20016 m_line_header = lh;
d9b3de22 20017
804d2729 20018 m_currently_recording_lines = true;
d9b3de22 20019
d9b3de22
DE
20020 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20021 was a line entry for it so that the backend has a chance to adjust it
20022 and also record it in case it needs it. This is currently used by MIPS
20023 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20024 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20025 m_is_stmt = lh->default_is_stmt;
20026 m_discriminator = 0;
252a6764
DE
20027}
20028
6f77053d
PA
20029void
20030lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20031 const gdb_byte *line_ptr,
7ab6656f 20032 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20033{
7ab6656f
OJ
20034 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20035 the pc range of the CU. However, we restrict the test to only ADDRESS
20036 values of zero to preserve GDB's previous behaviour which is to handle
20037 the specific case of a function being GC'd by the linker. */
924c2928 20038
7ab6656f 20039 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20040 {
20041 /* This line table is for a function which has been
20042 GCd by the linker. Ignore it. PR gdb/12528 */
20043
518817b3 20044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20045 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20046
b98664d3 20047 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20048 line_offset, objfile_name (objfile));
804d2729
TT
20049 m_currently_recording_lines = false;
20050 /* Note: m_currently_recording_lines is left as false until we see
20051 DW_LNE_end_sequence. */
924c2928
DE
20052 }
20053}
20054
f3f5162e 20055/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20056 Process the line number information in LH.
20057 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20058 program in order to set included_p for every referenced header. */
debd256d 20059
c906108c 20060static void
43f3e411
DE
20061dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20062 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20063{
d521ce57
TT
20064 const gdb_byte *line_ptr, *extended_end;
20065 const gdb_byte *line_end;
a8c50c1f 20066 unsigned int bytes_read, extended_len;
699ca60a 20067 unsigned char op_code, extended_op;
e142c38c 20068 CORE_ADDR baseaddr;
518817b3 20069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20070 bfd *abfd = objfile->obfd;
fbf65064 20071 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20072 /* True if we're recording line info (as opposed to building partial
20073 symtabs and just interested in finding include files mentioned by
20074 the line number program). */
20075 bool record_lines_p = !decode_for_pst_p;
e142c38c 20076
b3b3bada 20077 baseaddr = objfile->text_section_offset ();
c906108c 20078
debd256d
JB
20079 line_ptr = lh->statement_program_start;
20080 line_end = lh->statement_program_end;
c906108c
SS
20081
20082 /* Read the statement sequences until there's nothing left. */
20083 while (line_ptr < line_end)
20084 {
6f77053d
PA
20085 /* The DWARF line number program state machine. Reset the state
20086 machine at the start of each sequence. */
804d2729 20087 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20088 bool end_sequence = false;
d9b3de22 20089
8c43009f 20090 if (record_lines_p)
c906108c 20091 {
8c43009f
PA
20092 /* Start a subfile for the current file of the state
20093 machine. */
20094 const file_entry *fe = state_machine.current_file ();
20095
20096 if (fe != NULL)
804d2729 20097 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20098 }
20099
a738430d 20100 /* Decode the table. */
d9b3de22 20101 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20102 {
20103 op_code = read_1_byte (abfd, line_ptr);
20104 line_ptr += 1;
9aa1fe7e 20105
debd256d 20106 if (op_code >= lh->opcode_base)
6e70227d 20107 {
8e07a239 20108 /* Special opcode. */
6f77053d 20109 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20110 }
20111 else switch (op_code)
c906108c
SS
20112 {
20113 case DW_LNS_extended_op:
3e43a32a
MS
20114 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20115 &bytes_read);
473b7be6 20116 line_ptr += bytes_read;
a8c50c1f 20117 extended_end = line_ptr + extended_len;
c906108c
SS
20118 extended_op = read_1_byte (abfd, line_ptr);
20119 line_ptr += 1;
20120 switch (extended_op)
20121 {
20122 case DW_LNE_end_sequence:
6f77053d
PA
20123 state_machine.handle_end_sequence ();
20124 end_sequence = true;
c906108c
SS
20125 break;
20126 case DW_LNE_set_address:
d9b3de22
DE
20127 {
20128 CORE_ADDR address
c8a7a66f 20129 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20130 line_ptr += bytes_read;
6f77053d
PA
20131
20132 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20133 lowpc - baseaddr, address);
6f77053d 20134 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20135 }
c906108c
SS
20136 break;
20137 case DW_LNE_define_file:
debd256d 20138 {
d521ce57 20139 const char *cur_file;
ecfb656c
PA
20140 unsigned int mod_time, length;
20141 dir_index dindex;
6e70227d 20142
3e43a32a
MS
20143 cur_file = read_direct_string (abfd, line_ptr,
20144 &bytes_read);
debd256d 20145 line_ptr += bytes_read;
ecfb656c 20146 dindex = (dir_index)
debd256d
JB
20147 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20148 line_ptr += bytes_read;
20149 mod_time =
20150 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20151 line_ptr += bytes_read;
20152 length =
20153 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20154 line_ptr += bytes_read;
ecfb656c 20155 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20156 }
c906108c 20157 break;
d0c6ba3d 20158 case DW_LNE_set_discriminator:
6f77053d
PA
20159 {
20160 /* The discriminator is not interesting to the
20161 debugger; just ignore it. We still need to
20162 check its value though:
20163 if there are consecutive entries for the same
20164 (non-prologue) line we want to coalesce them.
20165 PR 17276. */
20166 unsigned int discr
20167 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20168 line_ptr += bytes_read;
20169
20170 state_machine.handle_set_discriminator (discr);
20171 }
d0c6ba3d 20172 break;
c906108c 20173 default:
b98664d3 20174 complaint (_("mangled .debug_line section"));
debd256d 20175 return;
c906108c 20176 }
a8c50c1f
DJ
20177 /* Make sure that we parsed the extended op correctly. If e.g.
20178 we expected a different address size than the producer used,
20179 we may have read the wrong number of bytes. */
20180 if (line_ptr != extended_end)
20181 {
b98664d3 20182 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20183 return;
20184 }
c906108c
SS
20185 break;
20186 case DW_LNS_copy:
6f77053d 20187 state_machine.handle_copy ();
c906108c
SS
20188 break;
20189 case DW_LNS_advance_pc:
2dc7f7b3
TT
20190 {
20191 CORE_ADDR adjust
20192 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20193 line_ptr += bytes_read;
6f77053d
PA
20194
20195 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20196 }
c906108c
SS
20197 break;
20198 case DW_LNS_advance_line:
a05a36a5
DE
20199 {
20200 int line_delta
20201 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20202 line_ptr += bytes_read;
6f77053d
PA
20203
20204 state_machine.handle_advance_line (line_delta);
a05a36a5 20205 }
c906108c
SS
20206 break;
20207 case DW_LNS_set_file:
d9b3de22 20208 {
6f77053d 20209 file_name_index file
ecfb656c
PA
20210 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20211 &bytes_read);
d9b3de22 20212 line_ptr += bytes_read;
8c43009f 20213
6f77053d 20214 state_machine.handle_set_file (file);
d9b3de22 20215 }
c906108c
SS
20216 break;
20217 case DW_LNS_set_column:
0ad93d4f 20218 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20219 line_ptr += bytes_read;
20220 break;
20221 case DW_LNS_negate_stmt:
6f77053d 20222 state_machine.handle_negate_stmt ();
c906108c
SS
20223 break;
20224 case DW_LNS_set_basic_block:
c906108c 20225 break;
c2c6d25f
JM
20226 /* Add to the address register of the state machine the
20227 address increment value corresponding to special opcode
a738430d
MK
20228 255. I.e., this value is scaled by the minimum
20229 instruction length since special opcode 255 would have
b021a221 20230 scaled the increment. */
c906108c 20231 case DW_LNS_const_add_pc:
6f77053d 20232 state_machine.handle_const_add_pc ();
c906108c
SS
20233 break;
20234 case DW_LNS_fixed_advance_pc:
3e29f34a 20235 {
6f77053d 20236 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20237 line_ptr += 2;
6f77053d
PA
20238
20239 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20240 }
c906108c 20241 break;
9aa1fe7e 20242 default:
a738430d
MK
20243 {
20244 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20245 int i;
a738430d 20246
debd256d 20247 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20248 {
20249 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20250 line_ptr += bytes_read;
20251 }
20252 }
c906108c
SS
20253 }
20254 }
d9b3de22
DE
20255
20256 if (!end_sequence)
20257 dwarf2_debug_line_missing_end_sequence_complaint ();
20258
20259 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20260 in which case we still finish recording the last line). */
6f77053d 20261 state_machine.record_line (true);
c906108c 20262 }
f3f5162e
DE
20263}
20264
20265/* Decode the Line Number Program (LNP) for the given line_header
20266 structure and CU. The actual information extracted and the type
20267 of structures created from the LNP depends on the value of PST.
20268
20269 1. If PST is NULL, then this procedure uses the data from the program
20270 to create all necessary symbol tables, and their linetables.
20271
20272 2. If PST is not NULL, this procedure reads the program to determine
20273 the list of files included by the unit represented by PST, and
20274 builds all the associated partial symbol tables.
20275
20276 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20277 It is used for relative paths in the line table.
20278 NOTE: When processing partial symtabs (pst != NULL),
20279 comp_dir == pst->dirname.
20280
20281 NOTE: It is important that psymtabs have the same file name (via strcmp)
20282 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20283 symtab we don't use it in the name of the psymtabs we create.
20284 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20285 A good testcase for this is mb-inline.exp.
20286
527f3840
JK
20287 LOWPC is the lowest address in CU (or 0 if not known).
20288
20289 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20290 for its PC<->lines mapping information. Otherwise only the filename
20291 table is read in. */
f3f5162e
DE
20292
20293static void
20294dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20295 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20296 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20297{
518817b3 20298 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20299 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20300
527f3840
JK
20301 if (decode_mapping)
20302 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20303
20304 if (decode_for_pst_p)
20305 {
aaa75496
JB
20306 /* Now that we're done scanning the Line Header Program, we can
20307 create the psymtab of each included file. */
7ba99d21
AT
20308 for (auto &file_entry : lh->file_names ())
20309 if (file_entry.included_p == 1)
aaa75496 20310 {
c89b44cd 20311 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20312 const char *include_name =
7ba99d21
AT
20313 psymtab_include_file_name (lh, file_entry, pst,
20314 comp_dir, &name_holder);
c6da4cef 20315 if (include_name != NULL)
aaa75496
JB
20316 dwarf2_create_include_psymtab (include_name, pst, objfile);
20317 }
20318 }
cb1df416
DJ
20319 else
20320 {
20321 /* Make sure a symtab is created for every file, even files
20322 which contain only variables (i.e. no code with associated
20323 line numbers). */
c24bdb02
KS
20324 buildsym_compunit *builder = cu->get_builder ();
20325 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20326
7ba99d21 20327 for (auto &fe : lh->file_names ())
cb1df416 20328 {
804d2729 20329 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20330 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20331 {
c24bdb02 20332 builder->get_current_subfile ()->symtab
804d2729 20333 = allocate_symtab (cust,
c24bdb02 20334 builder->get_current_subfile ()->name);
43f3e411 20335 }
c24bdb02 20336 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20337 }
20338 }
c906108c
SS
20339}
20340
20341/* Start a subfile for DWARF. FILENAME is the name of the file and
20342 DIRNAME the name of the source directory which contains FILENAME
4d663531 20343 or NULL if not known.
c906108c
SS
20344 This routine tries to keep line numbers from identical absolute and
20345 relative file names in a common subfile.
20346
20347 Using the `list' example from the GDB testsuite, which resides in
20348 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20349 of /srcdir/list0.c yields the following debugging information for list0.c:
20350
c5aa993b 20351 DW_AT_name: /srcdir/list0.c
4d663531 20352 DW_AT_comp_dir: /compdir
357e46e7 20353 files.files[0].name: list0.h
c5aa993b 20354 files.files[0].dir: /srcdir
357e46e7 20355 files.files[1].name: list0.c
c5aa993b 20356 files.files[1].dir: /srcdir
c906108c
SS
20357
20358 The line number information for list0.c has to end up in a single
4f1520fb
FR
20359 subfile, so that `break /srcdir/list0.c:1' works as expected.
20360 start_subfile will ensure that this happens provided that we pass the
20361 concatenation of files.files[1].dir and files.files[1].name as the
20362 subfile's name. */
c906108c
SS
20363
20364static void
804d2729
TT
20365dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20366 const char *dirname)
c906108c 20367{
43816ebc 20368 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20369
4d663531 20370 /* In order not to lose the line information directory,
4f1520fb
FR
20371 we concatenate it to the filename when it makes sense.
20372 Note that the Dwarf3 standard says (speaking of filenames in line
20373 information): ``The directory index is ignored for file names
20374 that represent full path names''. Thus ignoring dirname in the
20375 `else' branch below isn't an issue. */
c906108c 20376
d5166ae1 20377 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20378 {
43816ebc
TT
20379 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20380 filename = copy.get ();
d521ce57 20381 }
c906108c 20382
c24bdb02 20383 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20384}
20385
804d2729
TT
20386/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20387 buildsym_compunit constructor. */
f4dc4d17 20388
c24bdb02
KS
20389struct compunit_symtab *
20390dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20391 CORE_ADDR low_pc)
f4dc4d17 20392{
c24bdb02 20393 gdb_assert (m_builder == nullptr);
43f3e411 20394
c24bdb02
KS
20395 m_builder.reset (new struct buildsym_compunit
20396 (per_cu->dwarf2_per_objfile->objfile,
20397 name, comp_dir, language, low_pc));
93b8bea4 20398
c24bdb02 20399 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20400
c24bdb02
KS
20401 get_builder ()->record_debugformat ("DWARF 2");
20402 get_builder ()->record_producer (producer);
f4dc4d17 20403
c24bdb02 20404 processing_has_namespace_info = false;
43f3e411 20405
c24bdb02 20406 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20407}
20408
4c2df51b
DJ
20409static void
20410var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20411 struct dwarf2_cu *cu)
4c2df51b 20412{
518817b3 20413 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20414 struct comp_unit_head *cu_header = &cu->header;
20415
4c2df51b
DJ
20416 /* NOTE drow/2003-01-30: There used to be a comment and some special
20417 code here to turn a symbol with DW_AT_external and a
20418 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20419 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20420 with some versions of binutils) where shared libraries could have
20421 relocations against symbols in their debug information - the
20422 minimal symbol would have the right address, but the debug info
20423 would not. It's no longer necessary, because we will explicitly
20424 apply relocations when we read in the debug information now. */
20425
20426 /* A DW_AT_location attribute with no contents indicates that a
20427 variable has been optimized away. */
4fc6c0d5 20428 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20429 {
f1e6e072 20430 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20431 return;
20432 }
20433
20434 /* Handle one degenerate form of location expression specially, to
20435 preserve GDB's previous behavior when section offsets are
336d760d
AT
20436 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20437 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20438
4fc6c0d5 20439 if (attr->form_is_block ()
3019eac3
DE
20440 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20441 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20442 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20443 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20444 && (DW_BLOCK (attr)->size
20445 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20446 {
891d2f0b 20447 unsigned int dummy;
4c2df51b 20448
3019eac3 20449 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20450 SET_SYMBOL_VALUE_ADDRESS
20451 (sym, cu->header.read_address (objfile->obfd,
20452 DW_BLOCK (attr)->data + 1,
20453 &dummy));
3019eac3 20454 else
38583298
TT
20455 SET_SYMBOL_VALUE_ADDRESS
20456 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20457 &dummy));
f1e6e072 20458 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20459 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20460 SET_SYMBOL_VALUE_ADDRESS
20461 (sym,
20462 SYMBOL_VALUE_ADDRESS (sym)
20463 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20464 return;
20465 }
20466
20467 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20468 expression evaluator, and use LOC_COMPUTED only when necessary
20469 (i.e. when the value of a register or memory location is
20470 referenced, or a thread-local block, etc.). Then again, it might
20471 not be worthwhile. I'm assuming that it isn't unless performance
20472 or memory numbers show me otherwise. */
20473
f1e6e072 20474 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20475
f1e6e072 20476 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20477 cu->has_loclist = true;
4c2df51b
DJ
20478}
20479
c906108c
SS
20480/* Given a pointer to a DWARF information entry, figure out if we need
20481 to make a symbol table entry for it, and if so, create a new entry
20482 and return a pointer to it.
20483 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20484 used the passed type.
20485 If SPACE is not NULL, use it to hold the new symbol. If it is
20486 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20487
20488static struct symbol *
5e2db402
TT
20489new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20490 struct symbol *space)
c906108c 20491{
518817b3
SM
20492 struct dwarf2_per_objfile *dwarf2_per_objfile
20493 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20494 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20495 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20496 struct symbol *sym = NULL;
15d034d0 20497 const char *name;
c906108c
SS
20498 struct attribute *attr = NULL;
20499 struct attribute *attr2 = NULL;
e142c38c 20500 CORE_ADDR baseaddr;
e37fd15a
SW
20501 struct pending **list_to_add = NULL;
20502
edb3359d 20503 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20504
b3b3bada 20505 baseaddr = objfile->text_section_offset ();
c906108c 20506
94af9270 20507 name = dwarf2_name (die, cu);
c906108c
SS
20508 if (name)
20509 {
94af9270 20510 const char *linkagename;
34eaf542 20511 int suppress_add = 0;
94af9270 20512
34eaf542
TT
20513 if (space)
20514 sym = space;
20515 else
e623cf5d 20516 sym = allocate_symbol (objfile);
c906108c 20517 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20518
20519 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20520 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20521 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20522 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20523
f55ee35c
JK
20524 /* Fortran does not have mangling standard and the mangling does differ
20525 between gfortran, iFort etc. */
20526 if (cu->language == language_fortran
468c0cbb
CB
20527 && symbol_get_demangled_name (sym) == NULL)
20528 symbol_set_demangled_name (sym,
cfc594ee 20529 dwarf2_full_name (name, die, cu),
29df156d 20530 NULL);
f55ee35c 20531
c906108c 20532 /* Default assumptions.
c5aa993b 20533 Use the passed type or decode it from the die. */
176620f1 20534 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20535 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20536 if (type != NULL)
20537 SYMBOL_TYPE (sym) = type;
20538 else
e7c27a73 20539 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20540 attr = dwarf2_attr (die,
20541 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20542 cu);
435d3d88 20543 if (attr != nullptr)
c906108c
SS
20544 {
20545 SYMBOL_LINE (sym) = DW_UNSND (attr);
20546 }
cb1df416 20547
edb3359d
DJ
20548 attr = dwarf2_attr (die,
20549 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20550 cu);
435d3d88 20551 if (attr != nullptr)
cb1df416 20552 {
ecfb656c 20553 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20554 struct file_entry *fe;
9a619af0 20555
ecfb656c
PA
20556 if (cu->line_header != NULL)
20557 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20558 else
20559 fe = NULL;
20560
20561 if (fe == NULL)
b98664d3 20562 complaint (_("file index out of range"));
8c43009f
PA
20563 else
20564 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20565 }
20566
c906108c
SS
20567 switch (die->tag)
20568 {
20569 case DW_TAG_label:
e142c38c 20570 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20571 if (attr != nullptr)
3e29f34a
MR
20572 {
20573 CORE_ADDR addr;
20574
cd6c91b4 20575 addr = attr->value_as_address ();
3e29f34a 20576 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20577 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20578 }
0f5238ed
TT
20579 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20580 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20581 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20582 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20583 break;
20584 case DW_TAG_subprogram:
20585 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20586 finish_block. */
f1e6e072 20587 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20588 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20589 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20590 || cu->language == language_ada
20591 || cu->language == language_fortran)
c906108c 20592 {
2cfa0c8d 20593 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20594 Ada and Fortran subprograms, whether marked external or
20595 not, are always stored as a global symbol, because we want
20596 to be able to access them globally. For instance, we want
20597 to be able to break on a nested subprogram without having
20598 to specify the context. */
c24bdb02 20599 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20600 }
20601 else
20602 {
e37fd15a 20603 list_to_add = cu->list_in_scope;
c906108c
SS
20604 }
20605 break;
edb3359d
DJ
20606 case DW_TAG_inlined_subroutine:
20607 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20608 finish_block. */
f1e6e072 20609 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20610 SYMBOL_INLINED (sym) = 1;
481860b3 20611 list_to_add = cu->list_in_scope;
edb3359d 20612 break;
34eaf542
TT
20613 case DW_TAG_template_value_param:
20614 suppress_add = 1;
20615 /* Fall through. */
72929c62 20616 case DW_TAG_constant:
c906108c 20617 case DW_TAG_variable:
254e6b9e 20618 case DW_TAG_member:
0963b4bd
MS
20619 /* Compilation with minimal debug info may result in
20620 variables with missing type entries. Change the
20621 misleading `void' type to something sensible. */
c906108c 20622 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20623 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20624
e142c38c 20625 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20626 /* In the case of DW_TAG_member, we should only be called for
20627 static const members. */
20628 if (die->tag == DW_TAG_member)
20629 {
3863f96c
DE
20630 /* dwarf2_add_field uses die_is_declaration,
20631 so we do the same. */
254e6b9e
DE
20632 gdb_assert (die_is_declaration (die, cu));
20633 gdb_assert (attr);
20634 }
435d3d88 20635 if (attr != nullptr)
c906108c 20636 {
e7c27a73 20637 dwarf2_const_value (attr, sym, cu);
e142c38c 20638 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20639 if (!suppress_add)
34eaf542
TT
20640 {
20641 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20642 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20643 else
e37fd15a 20644 list_to_add = cu->list_in_scope;
34eaf542 20645 }
c906108c
SS
20646 break;
20647 }
e142c38c 20648 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20649 if (attr != nullptr)
c906108c 20650 {
e7c27a73 20651 var_decode_location (attr, sym, cu);
e142c38c 20652 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20653
20654 /* Fortran explicitly imports any global symbols to the local
20655 scope by DW_TAG_common_block. */
20656 if (cu->language == language_fortran && die->parent
20657 && die->parent->tag == DW_TAG_common_block)
20658 attr2 = NULL;
20659
caac4577
JG
20660 if (SYMBOL_CLASS (sym) == LOC_STATIC
20661 && SYMBOL_VALUE_ADDRESS (sym) == 0
20662 && !dwarf2_per_objfile->has_section_at_zero)
20663 {
20664 /* When a static variable is eliminated by the linker,
20665 the corresponding debug information is not stripped
20666 out, but the variable address is set to null;
20667 do not add such variables into symbol table. */
20668 }
20669 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20670 {
4b610737
TT
20671 if (SYMBOL_CLASS (sym) == LOC_STATIC
20672 && (objfile->flags & OBJF_MAINLINE) == 0
20673 && dwarf2_per_objfile->can_copy)
20674 {
20675 /* A global static variable might be subject to
20676 copy relocation. We first check for a local
20677 minsym, though, because maybe the symbol was
20678 marked hidden, in which case this would not
20679 apply. */
20680 bound_minimal_symbol found
20681 = (lookup_minimal_symbol_linkage
987012b8 20682 (sym->linkage_name (), objfile));
4b610737
TT
20683 if (found.minsym != nullptr)
20684 sym->maybe_copied = 1;
20685 }
f55ee35c 20686
1c809c68
TT
20687 /* A variable with DW_AT_external is never static,
20688 but it may be block-scoped. */
804d2729 20689 list_to_add
c24bdb02
KS
20690 = ((cu->list_in_scope
20691 == cu->get_builder ()->get_file_symbols ())
20692 ? cu->get_builder ()->get_global_symbols ()
804d2729 20693 : cu->list_in_scope);
1c809c68 20694 }
c906108c 20695 else
e37fd15a 20696 list_to_add = cu->list_in_scope;
c906108c
SS
20697 }
20698 else
20699 {
20700 /* We do not know the address of this symbol.
c5aa993b
JM
20701 If it is an external symbol and we have type information
20702 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20703 The address of the variable will then be determined from
20704 the minimal symbol table whenever the variable is
20705 referenced. */
e142c38c 20706 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20707
20708 /* Fortran explicitly imports any global symbols to the local
20709 scope by DW_TAG_common_block. */
20710 if (cu->language == language_fortran && die->parent
20711 && die->parent->tag == DW_TAG_common_block)
20712 {
20713 /* SYMBOL_CLASS doesn't matter here because
20714 read_common_block is going to reset it. */
20715 if (!suppress_add)
20716 list_to_add = cu->list_in_scope;
20717 }
20718 else if (attr2 && (DW_UNSND (attr2) != 0)
20719 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20720 {
0fe7935b
DJ
20721 /* A variable with DW_AT_external is never static, but it
20722 may be block-scoped. */
804d2729 20723 list_to_add
c24bdb02
KS
20724 = ((cu->list_in_scope
20725 == cu->get_builder ()->get_file_symbols ())
20726 ? cu->get_builder ()->get_global_symbols ()
804d2729 20727 : cu->list_in_scope);
0fe7935b 20728
f1e6e072 20729 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20730 }
442ddf59
JK
20731 else if (!die_is_declaration (die, cu))
20732 {
20733 /* Use the default LOC_OPTIMIZED_OUT class. */
20734 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20735 if (!suppress_add)
20736 list_to_add = cu->list_in_scope;
442ddf59 20737 }
c906108c
SS
20738 }
20739 break;
20740 case DW_TAG_formal_parameter:
a60f3166
TT
20741 {
20742 /* If we are inside a function, mark this as an argument. If
20743 not, we might be looking at an argument to an inlined function
20744 when we do not have enough information to show inlined frames;
20745 pretend it's a local variable in that case so that the user can
20746 still see it. */
804d2729 20747 struct context_stack *curr
c24bdb02 20748 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20749 if (curr != nullptr && curr->name != nullptr)
20750 SYMBOL_IS_ARGUMENT (sym) = 1;
20751 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20752 if (attr != nullptr)
a60f3166
TT
20753 {
20754 var_decode_location (attr, sym, cu);
20755 }
20756 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20757 if (attr != nullptr)
a60f3166
TT
20758 {
20759 dwarf2_const_value (attr, sym, cu);
20760 }
f346a30d 20761
a60f3166
TT
20762 list_to_add = cu->list_in_scope;
20763 }
c906108c
SS
20764 break;
20765 case DW_TAG_unspecified_parameters:
20766 /* From varargs functions; gdb doesn't seem to have any
20767 interest in this information, so just ignore it for now.
20768 (FIXME?) */
20769 break;
34eaf542
TT
20770 case DW_TAG_template_type_param:
20771 suppress_add = 1;
20772 /* Fall through. */
c906108c 20773 case DW_TAG_class_type:
680b30c7 20774 case DW_TAG_interface_type:
c906108c
SS
20775 case DW_TAG_structure_type:
20776 case DW_TAG_union_type:
72019c9c 20777 case DW_TAG_set_type:
c906108c 20778 case DW_TAG_enumeration_type:
f1e6e072 20779 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20780 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20781
63d06c5c 20782 {
9c37b5ae 20783 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20784 really ever be static objects: otherwise, if you try
20785 to, say, break of a class's method and you're in a file
20786 which doesn't mention that class, it won't work unless
20787 the check for all static symbols in lookup_symbol_aux
20788 saves you. See the OtherFileClass tests in
20789 gdb.c++/namespace.exp. */
20790
e37fd15a 20791 if (!suppress_add)
34eaf542 20792 {
c24bdb02 20793 buildsym_compunit *builder = cu->get_builder ();
804d2729 20794 list_to_add
c24bdb02 20795 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20796 && cu->language == language_cplus
c24bdb02 20797 ? builder->get_global_symbols ()
804d2729 20798 : cu->list_in_scope);
63d06c5c 20799
64382290 20800 /* The semantics of C++ state that "struct foo {
9c37b5ae 20801 ... }" also defines a typedef for "foo". */
64382290 20802 if (cu->language == language_cplus
45280282 20803 || cu->language == language_ada
c44af4eb
TT
20804 || cu->language == language_d
20805 || cu->language == language_rust)
64382290
TT
20806 {
20807 /* The symbol's name is already allocated along
20808 with this objfile, so we don't need to
20809 duplicate it for the type. */
20810 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20811 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20812 }
63d06c5c
DC
20813 }
20814 }
c906108c
SS
20815 break;
20816 case DW_TAG_typedef:
f1e6e072 20817 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20818 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20819 list_to_add = cu->list_in_scope;
63d06c5c 20820 break;
c906108c 20821 case DW_TAG_base_type:
a02abb62 20822 case DW_TAG_subrange_type:
f1e6e072 20823 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20824 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20825 list_to_add = cu->list_in_scope;
c906108c
SS
20826 break;
20827 case DW_TAG_enumerator:
e142c38c 20828 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20829 if (attr != nullptr)
c906108c 20830 {
e7c27a73 20831 dwarf2_const_value (attr, sym, cu);
c906108c 20832 }
63d06c5c
DC
20833 {
20834 /* NOTE: carlton/2003-11-10: See comment above in the
20835 DW_TAG_class_type, etc. block. */
20836
804d2729 20837 list_to_add
c24bdb02 20838 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20839 && cu->language == language_cplus
c24bdb02 20840 ? cu->get_builder ()->get_global_symbols ()
804d2729 20841 : cu->list_in_scope);
63d06c5c 20842 }
c906108c 20843 break;
74921315 20844 case DW_TAG_imported_declaration:
5c4e30ca 20845 case DW_TAG_namespace:
f1e6e072 20846 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20847 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20848 break;
530e8392
KB
20849 case DW_TAG_module:
20850 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20851 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20852 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20853 break;
4357ac6c 20854 case DW_TAG_common_block:
f1e6e072 20855 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20856 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20857 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20858 break;
c906108c
SS
20859 default:
20860 /* Not a tag we recognize. Hopefully we aren't processing
20861 trash data, but since we must specifically ignore things
20862 we don't recognize, there is nothing else we should do at
0963b4bd 20863 this point. */
b98664d3 20864 complaint (_("unsupported tag: '%s'"),
4d3c2250 20865 dwarf_tag_name (die->tag));
c906108c
SS
20866 break;
20867 }
df8a16a1 20868
e37fd15a
SW
20869 if (suppress_add)
20870 {
20871 sym->hash_next = objfile->template_symbols;
20872 objfile->template_symbols = sym;
20873 list_to_add = NULL;
20874 }
20875
20876 if (list_to_add != NULL)
d3cb6808 20877 add_symbol_to_list (sym, list_to_add);
e37fd15a 20878
df8a16a1
DJ
20879 /* For the benefit of old versions of GCC, check for anonymous
20880 namespaces based on the demangled name. */
4d4ec4e5 20881 if (!cu->processing_has_namespace_info
94af9270 20882 && cu->language == language_cplus)
c24bdb02 20883 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20884 }
20885 return (sym);
20886}
20887
98bfdba5
PA
20888/* Given an attr with a DW_FORM_dataN value in host byte order,
20889 zero-extend it as appropriate for the symbol's type. The DWARF
20890 standard (v4) is not entirely clear about the meaning of using
20891 DW_FORM_dataN for a constant with a signed type, where the type is
20892 wider than the data. The conclusion of a discussion on the DWARF
20893 list was that this is unspecified. We choose to always zero-extend
20894 because that is the interpretation long in use by GCC. */
c906108c 20895
98bfdba5 20896static gdb_byte *
ff39bb5e 20897dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20898 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20899{
518817b3 20900 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20901 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20902 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20903 LONGEST l = DW_UNSND (attr);
20904
20905 if (bits < sizeof (*value) * 8)
20906 {
20907 l &= ((LONGEST) 1 << bits) - 1;
20908 *value = l;
20909 }
20910 else if (bits == sizeof (*value) * 8)
20911 *value = l;
20912 else
20913 {
224c3ddb 20914 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20915 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20916 return bytes;
20917 }
20918
20919 return NULL;
20920}
20921
20922/* Read a constant value from an attribute. Either set *VALUE, or if
20923 the value does not fit in *VALUE, set *BYTES - either already
20924 allocated on the objfile obstack, or newly allocated on OBSTACK,
20925 or, set *BATON, if we translated the constant to a location
20926 expression. */
20927
20928static void
ff39bb5e 20929dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20930 const char *name, struct obstack *obstack,
20931 struct dwarf2_cu *cu,
d521ce57 20932 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20933 struct dwarf2_locexpr_baton **baton)
20934{
518817b3 20935 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20936 struct comp_unit_head *cu_header = &cu->header;
c906108c 20937 struct dwarf_block *blk;
98bfdba5
PA
20938 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20939 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20940
20941 *value = 0;
20942 *bytes = NULL;
20943 *baton = NULL;
c906108c
SS
20944
20945 switch (attr->form)
20946 {
20947 case DW_FORM_addr:
336d760d 20948 case DW_FORM_addrx:
3019eac3 20949 case DW_FORM_GNU_addr_index:
ac56253d 20950 {
ac56253d
TT
20951 gdb_byte *data;
20952
98bfdba5
PA
20953 if (TYPE_LENGTH (type) != cu_header->addr_size)
20954 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20955 cu_header->addr_size,
98bfdba5 20956 TYPE_LENGTH (type));
ac56253d
TT
20957 /* Symbols of this form are reasonably rare, so we just
20958 piggyback on the existing location code rather than writing
20959 a new implementation of symbol_computed_ops. */
8d749320 20960 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20961 (*baton)->per_cu = cu->per_cu;
20962 gdb_assert ((*baton)->per_cu);
ac56253d 20963
98bfdba5 20964 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20965 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20966 (*baton)->data = data;
ac56253d
TT
20967
20968 data[0] = DW_OP_addr;
20969 store_unsigned_integer (&data[1], cu_header->addr_size,
20970 byte_order, DW_ADDR (attr));
20971 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20972 }
c906108c 20973 break;
4ac36638 20974 case DW_FORM_string:
93b5768b 20975 case DW_FORM_strp:
cf532bd1 20976 case DW_FORM_strx:
3019eac3 20977 case DW_FORM_GNU_str_index:
36586728 20978 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20979 /* DW_STRING is already allocated on the objfile obstack, point
20980 directly to it. */
d521ce57 20981 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20982 break;
c906108c
SS
20983 case DW_FORM_block1:
20984 case DW_FORM_block2:
20985 case DW_FORM_block4:
20986 case DW_FORM_block:
2dc7f7b3 20987 case DW_FORM_exprloc:
0224619f 20988 case DW_FORM_data16:
c906108c 20989 blk = DW_BLOCK (attr);
98bfdba5
PA
20990 if (TYPE_LENGTH (type) != blk->size)
20991 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20992 TYPE_LENGTH (type));
20993 *bytes = blk->data;
c906108c 20994 break;
2df3850c
JM
20995
20996 /* The DW_AT_const_value attributes are supposed to carry the
20997 symbol's value "represented as it would be on the target
20998 architecture." By the time we get here, it's already been
20999 converted to host endianness, so we just need to sign- or
21000 zero-extend it as appropriate. */
21001 case DW_FORM_data1:
3aef2284 21002 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21003 break;
c906108c 21004 case DW_FORM_data2:
3aef2284 21005 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21006 break;
c906108c 21007 case DW_FORM_data4:
3aef2284 21008 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21009 break;
c906108c 21010 case DW_FORM_data8:
3aef2284 21011 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21012 break;
21013
c906108c 21014 case DW_FORM_sdata:
663c44ac 21015 case DW_FORM_implicit_const:
98bfdba5 21016 *value = DW_SND (attr);
2df3850c
JM
21017 break;
21018
c906108c 21019 case DW_FORM_udata:
98bfdba5 21020 *value = DW_UNSND (attr);
c906108c 21021 break;
2df3850c 21022
c906108c 21023 default:
b98664d3 21024 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21025 dwarf_form_name (attr->form));
98bfdba5 21026 *value = 0;
c906108c
SS
21027 break;
21028 }
21029}
21030
2df3850c 21031
98bfdba5
PA
21032/* Copy constant value from an attribute to a symbol. */
21033
2df3850c 21034static void
ff39bb5e 21035dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21036 struct dwarf2_cu *cu)
2df3850c 21037{
518817b3 21038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21039 LONGEST value;
d521ce57 21040 const gdb_byte *bytes;
98bfdba5 21041 struct dwarf2_locexpr_baton *baton;
2df3850c 21042
98bfdba5 21043 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21044 sym->print_name (),
98bfdba5
PA
21045 &objfile->objfile_obstack, cu,
21046 &value, &bytes, &baton);
2df3850c 21047
98bfdba5
PA
21048 if (baton != NULL)
21049 {
98bfdba5 21050 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21051 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21052 }
21053 else if (bytes != NULL)
21054 {
21055 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21056 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21057 }
21058 else
21059 {
21060 SYMBOL_VALUE (sym) = value;
f1e6e072 21061 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21062 }
2df3850c
JM
21063}
21064
c906108c
SS
21065/* Return the type of the die in question using its DW_AT_type attribute. */
21066
21067static struct type *
e7c27a73 21068die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21069{
c906108c 21070 struct attribute *type_attr;
c906108c 21071
e142c38c 21072 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21073 if (!type_attr)
21074 {
518817b3 21075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21076 /* A missing DW_AT_type represents a void type. */
518817b3 21077 return objfile_type (objfile)->builtin_void;
c906108c 21078 }
348e048f 21079
673bfd45 21080 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21081}
21082
b4ba55a1
JB
21083/* True iff CU's producer generates GNAT Ada auxiliary information
21084 that allows to find parallel types through that information instead
21085 of having to do expensive parallel lookups by type name. */
21086
21087static int
21088need_gnat_info (struct dwarf2_cu *cu)
21089{
de4cb04a
JB
21090 /* Assume that the Ada compiler was GNAT, which always produces
21091 the auxiliary information. */
21092 return (cu->language == language_ada);
b4ba55a1
JB
21093}
21094
b4ba55a1
JB
21095/* Return the auxiliary type of the die in question using its
21096 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21097 attribute is not present. */
21098
21099static struct type *
21100die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21101{
b4ba55a1 21102 struct attribute *type_attr;
b4ba55a1
JB
21103
21104 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21105 if (!type_attr)
21106 return NULL;
21107
673bfd45 21108 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21109}
21110
21111/* If DIE has a descriptive_type attribute, then set the TYPE's
21112 descriptive type accordingly. */
21113
21114static void
21115set_descriptive_type (struct type *type, struct die_info *die,
21116 struct dwarf2_cu *cu)
21117{
21118 struct type *descriptive_type = die_descriptive_type (die, cu);
21119
21120 if (descriptive_type)
21121 {
21122 ALLOCATE_GNAT_AUX_TYPE (type);
21123 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21124 }
21125}
21126
c906108c
SS
21127/* Return the containing type of the die in question using its
21128 DW_AT_containing_type attribute. */
21129
21130static struct type *
e7c27a73 21131die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21132{
c906108c 21133 struct attribute *type_attr;
518817b3 21134 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21135
e142c38c 21136 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21137 if (!type_attr)
21138 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21139 "[in module %s]"), objfile_name (objfile));
33ac96f0 21140
673bfd45 21141 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21142}
21143
ac9ec31b
DE
21144/* Return an error marker type to use for the ill formed type in DIE/CU. */
21145
21146static struct type *
21147build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21148{
518817b3
SM
21149 struct dwarf2_per_objfile *dwarf2_per_objfile
21150 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21151 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21152 char *saved;
ac9ec31b 21153
528e1572
SM
21154 std::string message
21155 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21156 objfile_name (objfile),
21157 sect_offset_str (cu->header.sect_off),
21158 sect_offset_str (die->sect_off));
efba19b0 21159 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21160
19f392bc 21161 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21162}
21163
673bfd45 21164/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21165 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21166 DW_AT_containing_type.
673bfd45
DE
21167 If there is no type substitute an error marker. */
21168
c906108c 21169static struct type *
ff39bb5e 21170lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21171 struct dwarf2_cu *cu)
c906108c 21172{
518817b3
SM
21173 struct dwarf2_per_objfile *dwarf2_per_objfile
21174 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21175 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21176 struct type *this_type;
21177
ac9ec31b
DE
21178 gdb_assert (attr->name == DW_AT_type
21179 || attr->name == DW_AT_GNAT_descriptive_type
21180 || attr->name == DW_AT_containing_type);
21181
673bfd45
DE
21182 /* First see if we have it cached. */
21183
36586728
TT
21184 if (attr->form == DW_FORM_GNU_ref_alt)
21185 {
21186 struct dwarf2_per_cu_data *per_cu;
9c541725 21187 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21188
ed2dc618
SM
21189 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21190 dwarf2_per_objfile);
9c541725 21191 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21192 }
cd6c91b4 21193 else if (attr->form_is_ref ())
673bfd45 21194 {
9c541725 21195 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21196
9c541725 21197 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21198 }
55f1336d 21199 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21200 {
ac9ec31b 21201 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21202
ac9ec31b 21203 return get_signatured_type (die, signature, cu);
673bfd45
DE
21204 }
21205 else
21206 {
b98664d3 21207 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21208 " at %s [in module %s]"),
21209 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21210 objfile_name (objfile));
ac9ec31b 21211 return build_error_marker_type (cu, die);
673bfd45
DE
21212 }
21213
21214 /* If not cached we need to read it in. */
21215
21216 if (this_type == NULL)
21217 {
ac9ec31b 21218 struct die_info *type_die = NULL;
673bfd45
DE
21219 struct dwarf2_cu *type_cu = cu;
21220
cd6c91b4 21221 if (attr->form_is_ref ())
ac9ec31b
DE
21222 type_die = follow_die_ref (die, attr, &type_cu);
21223 if (type_die == NULL)
21224 return build_error_marker_type (cu, die);
21225 /* If we find the type now, it's probably because the type came
3019eac3
DE
21226 from an inter-CU reference and the type's CU got expanded before
21227 ours. */
ac9ec31b 21228 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21229 }
21230
21231 /* If we still don't have a type use an error marker. */
21232
21233 if (this_type == NULL)
ac9ec31b 21234 return build_error_marker_type (cu, die);
673bfd45 21235
f792889a 21236 return this_type;
c906108c
SS
21237}
21238
673bfd45
DE
21239/* Return the type in DIE, CU.
21240 Returns NULL for invalid types.
21241
02142a6c 21242 This first does a lookup in die_type_hash,
673bfd45
DE
21243 and only reads the die in if necessary.
21244
21245 NOTE: This can be called when reading in partial or full symbols. */
21246
f792889a 21247static struct type *
e7c27a73 21248read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21249{
f792889a
DJ
21250 struct type *this_type;
21251
21252 this_type = get_die_type (die, cu);
21253 if (this_type)
21254 return this_type;
21255
673bfd45
DE
21256 return read_type_die_1 (die, cu);
21257}
21258
21259/* Read the type in DIE, CU.
21260 Returns NULL for invalid types. */
21261
21262static struct type *
21263read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21264{
21265 struct type *this_type = NULL;
21266
c906108c
SS
21267 switch (die->tag)
21268 {
21269 case DW_TAG_class_type:
680b30c7 21270 case DW_TAG_interface_type:
c906108c
SS
21271 case DW_TAG_structure_type:
21272 case DW_TAG_union_type:
f792889a 21273 this_type = read_structure_type (die, cu);
c906108c
SS
21274 break;
21275 case DW_TAG_enumeration_type:
f792889a 21276 this_type = read_enumeration_type (die, cu);
c906108c
SS
21277 break;
21278 case DW_TAG_subprogram:
21279 case DW_TAG_subroutine_type:
edb3359d 21280 case DW_TAG_inlined_subroutine:
f792889a 21281 this_type = read_subroutine_type (die, cu);
c906108c
SS
21282 break;
21283 case DW_TAG_array_type:
f792889a 21284 this_type = read_array_type (die, cu);
c906108c 21285 break;
72019c9c 21286 case DW_TAG_set_type:
f792889a 21287 this_type = read_set_type (die, cu);
72019c9c 21288 break;
c906108c 21289 case DW_TAG_pointer_type:
f792889a 21290 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21291 break;
21292 case DW_TAG_ptr_to_member_type:
f792889a 21293 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21294 break;
21295 case DW_TAG_reference_type:
4297a3f0
AV
21296 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21297 break;
21298 case DW_TAG_rvalue_reference_type:
21299 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21300 break;
21301 case DW_TAG_const_type:
f792889a 21302 this_type = read_tag_const_type (die, cu);
c906108c
SS
21303 break;
21304 case DW_TAG_volatile_type:
f792889a 21305 this_type = read_tag_volatile_type (die, cu);
c906108c 21306 break;
06d66ee9
TT
21307 case DW_TAG_restrict_type:
21308 this_type = read_tag_restrict_type (die, cu);
21309 break;
c906108c 21310 case DW_TAG_string_type:
f792889a 21311 this_type = read_tag_string_type (die, cu);
c906108c
SS
21312 break;
21313 case DW_TAG_typedef:
f792889a 21314 this_type = read_typedef (die, cu);
c906108c 21315 break;
a02abb62 21316 case DW_TAG_subrange_type:
f792889a 21317 this_type = read_subrange_type (die, cu);
a02abb62 21318 break;
c906108c 21319 case DW_TAG_base_type:
f792889a 21320 this_type = read_base_type (die, cu);
c906108c 21321 break;
81a17f79 21322 case DW_TAG_unspecified_type:
f792889a 21323 this_type = read_unspecified_type (die, cu);
81a17f79 21324 break;
0114d602
DJ
21325 case DW_TAG_namespace:
21326 this_type = read_namespace_type (die, cu);
21327 break;
f55ee35c
JK
21328 case DW_TAG_module:
21329 this_type = read_module_type (die, cu);
21330 break;
a2c2acaf
MW
21331 case DW_TAG_atomic_type:
21332 this_type = read_tag_atomic_type (die, cu);
21333 break;
c906108c 21334 default:
b98664d3 21335 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21336 dwarf_tag_name (die->tag));
c906108c
SS
21337 break;
21338 }
63d06c5c 21339
f792889a 21340 return this_type;
63d06c5c
DC
21341}
21342
abc72ce4
DE
21343/* See if we can figure out if the class lives in a namespace. We do
21344 this by looking for a member function; its demangled name will
21345 contain namespace info, if there is any.
21346 Return the computed name or NULL.
21347 Space for the result is allocated on the objfile's obstack.
21348 This is the full-die version of guess_partial_die_structure_name.
21349 In this case we know DIE has no useful parent. */
21350
43816ebc 21351static const char *
abc72ce4
DE
21352guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21353{
21354 struct die_info *spec_die;
21355 struct dwarf2_cu *spec_cu;
21356 struct die_info *child;
518817b3 21357 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21358
21359 spec_cu = cu;
21360 spec_die = die_specification (die, &spec_cu);
21361 if (spec_die != NULL)
21362 {
21363 die = spec_die;
21364 cu = spec_cu;
21365 }
21366
21367 for (child = die->child;
21368 child != NULL;
21369 child = child->sibling)
21370 {
21371 if (child->tag == DW_TAG_subprogram)
21372 {
73b9be8b 21373 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21374
7d45c7c3 21375 if (linkage_name != NULL)
abc72ce4 21376 {
43816ebc
TT
21377 gdb::unique_xmalloc_ptr<char> actual_name
21378 (language_class_name_from_physname (cu->language_defn,
21379 linkage_name));
21380 const char *name = NULL;
abc72ce4
DE
21381
21382 if (actual_name != NULL)
21383 {
15d034d0 21384 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21385
21386 if (die_name != NULL
43816ebc 21387 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21388 {
21389 /* Strip off the class name from the full name.
21390 We want the prefix. */
21391 int die_name_len = strlen (die_name);
43816ebc
TT
21392 int actual_name_len = strlen (actual_name.get ());
21393 const char *ptr = actual_name.get ();
abc72ce4
DE
21394
21395 /* Test for '::' as a sanity check. */
21396 if (actual_name_len > die_name_len + 2
43816ebc 21397 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21398 name = obstack_strndup (
e3b94546 21399 &objfile->per_bfd->storage_obstack,
43816ebc 21400 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21401 }
21402 }
abc72ce4
DE
21403 return name;
21404 }
21405 }
21406 }
21407
21408 return NULL;
21409}
21410
96408a79
SA
21411/* GCC might emit a nameless typedef that has a linkage name. Determine the
21412 prefix part in such case. See
21413 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21414
a121b7c1 21415static const char *
96408a79
SA
21416anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21417{
21418 struct attribute *attr;
e6a959d6 21419 const char *base;
96408a79
SA
21420
21421 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21422 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21423 return NULL;
21424
7d45c7c3 21425 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21426 return NULL;
21427
73b9be8b 21428 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21429 if (attr == NULL || DW_STRING (attr) == NULL)
21430 return NULL;
21431
21432 /* dwarf2_name had to be already called. */
21433 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21434
21435 /* Strip the base name, keep any leading namespaces/classes. */
21436 base = strrchr (DW_STRING (attr), ':');
21437 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21438 return "";
21439
518817b3 21440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21441 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21442 DW_STRING (attr),
21443 &base[-1] - DW_STRING (attr));
96408a79
SA
21444}
21445
fdde2d81 21446/* Return the name of the namespace/class that DIE is defined within,
0114d602 21447 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21448
0114d602
DJ
21449 For example, if we're within the method foo() in the following
21450 code:
21451
21452 namespace N {
21453 class C {
21454 void foo () {
21455 }
21456 };
21457 }
21458
21459 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21460
0d5cff50 21461static const char *
e142c38c 21462determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21463{
518817b3
SM
21464 struct dwarf2_per_objfile *dwarf2_per_objfile
21465 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21466 struct die_info *parent, *spec_die;
21467 struct dwarf2_cu *spec_cu;
21468 struct type *parent_type;
a121b7c1 21469 const char *retval;
63d06c5c 21470
9c37b5ae 21471 if (cu->language != language_cplus
c44af4eb
TT
21472 && cu->language != language_fortran && cu->language != language_d
21473 && cu->language != language_rust)
0114d602
DJ
21474 return "";
21475
96408a79
SA
21476 retval = anonymous_struct_prefix (die, cu);
21477 if (retval)
21478 return retval;
21479
0114d602
DJ
21480 /* We have to be careful in the presence of DW_AT_specification.
21481 For example, with GCC 3.4, given the code
21482
21483 namespace N {
21484 void foo() {
21485 // Definition of N::foo.
21486 }
21487 }
21488
21489 then we'll have a tree of DIEs like this:
21490
21491 1: DW_TAG_compile_unit
21492 2: DW_TAG_namespace // N
21493 3: DW_TAG_subprogram // declaration of N::foo
21494 4: DW_TAG_subprogram // definition of N::foo
21495 DW_AT_specification // refers to die #3
21496
21497 Thus, when processing die #4, we have to pretend that we're in
21498 the context of its DW_AT_specification, namely the contex of die
21499 #3. */
21500 spec_cu = cu;
21501 spec_die = die_specification (die, &spec_cu);
21502 if (spec_die == NULL)
21503 parent = die->parent;
21504 else
63d06c5c 21505 {
0114d602
DJ
21506 parent = spec_die->parent;
21507 cu = spec_cu;
63d06c5c 21508 }
0114d602
DJ
21509
21510 if (parent == NULL)
21511 return "";
98bfdba5
PA
21512 else if (parent->building_fullname)
21513 {
21514 const char *name;
21515 const char *parent_name;
21516
21517 /* It has been seen on RealView 2.2 built binaries,
21518 DW_TAG_template_type_param types actually _defined_ as
21519 children of the parent class:
21520
21521 enum E {};
21522 template class <class Enum> Class{};
21523 Class<enum E> class_e;
21524
21525 1: DW_TAG_class_type (Class)
21526 2: DW_TAG_enumeration_type (E)
21527 3: DW_TAG_enumerator (enum1:0)
21528 3: DW_TAG_enumerator (enum2:1)
21529 ...
21530 2: DW_TAG_template_type_param
21531 DW_AT_type DW_FORM_ref_udata (E)
21532
21533 Besides being broken debug info, it can put GDB into an
21534 infinite loop. Consider:
21535
21536 When we're building the full name for Class<E>, we'll start
21537 at Class, and go look over its template type parameters,
21538 finding E. We'll then try to build the full name of E, and
21539 reach here. We're now trying to build the full name of E,
21540 and look over the parent DIE for containing scope. In the
21541 broken case, if we followed the parent DIE of E, we'd again
21542 find Class, and once again go look at its template type
21543 arguments, etc., etc. Simply don't consider such parent die
21544 as source-level parent of this die (it can't be, the language
21545 doesn't allow it), and break the loop here. */
21546 name = dwarf2_name (die, cu);
21547 parent_name = dwarf2_name (parent, cu);
b98664d3 21548 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21549 name ? name : "<unknown>",
21550 parent_name ? parent_name : "<unknown>");
21551 return "";
21552 }
63d06c5c 21553 else
0114d602
DJ
21554 switch (parent->tag)
21555 {
63d06c5c 21556 case DW_TAG_namespace:
0114d602 21557 parent_type = read_type_die (parent, cu);
acebe513
UW
21558 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21559 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21560 Work around this problem here. */
21561 if (cu->language == language_cplus
e86ca25f 21562 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21563 return "";
0114d602 21564 /* We give a name to even anonymous namespaces. */
e86ca25f 21565 return TYPE_NAME (parent_type);
63d06c5c 21566 case DW_TAG_class_type:
680b30c7 21567 case DW_TAG_interface_type:
63d06c5c 21568 case DW_TAG_structure_type:
0114d602 21569 case DW_TAG_union_type:
f55ee35c 21570 case DW_TAG_module:
0114d602 21571 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21572 if (TYPE_NAME (parent_type) != NULL)
21573 return TYPE_NAME (parent_type);
0114d602
DJ
21574 else
21575 /* An anonymous structure is only allowed non-static data
21576 members; no typedefs, no member functions, et cetera.
21577 So it does not need a prefix. */
21578 return "";
abc72ce4 21579 case DW_TAG_compile_unit:
95554aad 21580 case DW_TAG_partial_unit:
abc72ce4
DE
21581 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21582 if (cu->language == language_cplus
fd5866f6 21583 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21584 && die->child != NULL
21585 && (die->tag == DW_TAG_class_type
21586 || die->tag == DW_TAG_structure_type
21587 || die->tag == DW_TAG_union_type))
21588 {
43816ebc 21589 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21590 if (name != NULL)
21591 return name;
21592 }
21593 return "";
0a4b0913
AB
21594 case DW_TAG_subprogram:
21595 /* Nested subroutines in Fortran get a prefix with the name
21596 of the parent's subroutine. */
21597 if (cu->language == language_fortran)
21598 {
21599 if ((die->tag == DW_TAG_subprogram)
21600 && (dwarf2_name (parent, cu) != NULL))
21601 return dwarf2_name (parent, cu);
21602 }
21603 return determine_prefix (parent, cu);
3d567982
TT
21604 case DW_TAG_enumeration_type:
21605 parent_type = read_type_die (parent, cu);
21606 if (TYPE_DECLARED_CLASS (parent_type))
21607 {
e86ca25f
TT
21608 if (TYPE_NAME (parent_type) != NULL)
21609 return TYPE_NAME (parent_type);
3d567982
TT
21610 return "";
21611 }
21612 /* Fall through. */
63d06c5c 21613 default:
8176b9b8 21614 return determine_prefix (parent, cu);
63d06c5c 21615 }
63d06c5c
DC
21616}
21617
3e43a32a
MS
21618/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21619 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21620 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21621 an obconcat, otherwise allocate storage for the result. The CU argument is
21622 used to determine the language and hence, the appropriate separator. */
987504bb 21623
f55ee35c 21624#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21625
21626static char *
f55ee35c
JK
21627typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21628 int physname, struct dwarf2_cu *cu)
63d06c5c 21629{
f55ee35c 21630 const char *lead = "";
5c315b68 21631 const char *sep;
63d06c5c 21632
3e43a32a
MS
21633 if (suffix == NULL || suffix[0] == '\0'
21634 || prefix == NULL || prefix[0] == '\0')
987504bb 21635 sep = "";
45280282
IB
21636 else if (cu->language == language_d)
21637 {
21638 /* For D, the 'main' function could be defined in any module, but it
21639 should never be prefixed. */
21640 if (strcmp (suffix, "D main") == 0)
21641 {
21642 prefix = "";
21643 sep = "";
21644 }
21645 else
21646 sep = ".";
21647 }
f55ee35c
JK
21648 else if (cu->language == language_fortran && physname)
21649 {
21650 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21651 DW_AT_MIPS_linkage_name is preferred and used instead. */
21652
21653 lead = "__";
21654 sep = "_MOD_";
21655 }
987504bb
JJ
21656 else
21657 sep = "::";
63d06c5c 21658
6dd47d34
DE
21659 if (prefix == NULL)
21660 prefix = "";
21661 if (suffix == NULL)
21662 suffix = "";
21663
987504bb
JJ
21664 if (obs == NULL)
21665 {
3e43a32a 21666 char *retval
224c3ddb
SM
21667 = ((char *)
21668 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21669
f55ee35c
JK
21670 strcpy (retval, lead);
21671 strcat (retval, prefix);
6dd47d34
DE
21672 strcat (retval, sep);
21673 strcat (retval, suffix);
63d06c5c
DC
21674 return retval;
21675 }
987504bb
JJ
21676 else
21677 {
21678 /* We have an obstack. */
f55ee35c 21679 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21680 }
63d06c5c
DC
21681}
21682
c906108c
SS
21683/* Return sibling of die, NULL if no sibling. */
21684
f9aca02d 21685static struct die_info *
fba45db2 21686sibling_die (struct die_info *die)
c906108c 21687{
639d11d3 21688 return die->sibling;
c906108c
SS
21689}
21690
71c25dea
TT
21691/* Get name of a die, return NULL if not found. */
21692
15d034d0
TT
21693static const char *
21694dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
21695 struct obstack *obstack)
21696{
21697 if (name && cu->language == language_cplus)
21698 {
2f408ecb 21699 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21700
2f408ecb 21701 if (!canon_name.empty ())
71c25dea 21702 {
2f408ecb 21703 if (canon_name != name)
efba19b0 21704 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
21705 }
21706 }
21707
21708 return name;
c906108c
SS
21709}
21710
96553a0c
DE
21711/* Get name of a die, return NULL if not found.
21712 Anonymous namespaces are converted to their magic string. */
9219021c 21713
15d034d0 21714static const char *
e142c38c 21715dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21716{
21717 struct attribute *attr;
518817b3 21718 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21719
e142c38c 21720 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21721 if ((!attr || !DW_STRING (attr))
96553a0c 21722 && die->tag != DW_TAG_namespace
53832f31
TT
21723 && die->tag != DW_TAG_class_type
21724 && die->tag != DW_TAG_interface_type
21725 && die->tag != DW_TAG_structure_type
21726 && die->tag != DW_TAG_union_type)
71c25dea
TT
21727 return NULL;
21728
21729 switch (die->tag)
21730 {
21731 case DW_TAG_compile_unit:
95554aad 21732 case DW_TAG_partial_unit:
71c25dea
TT
21733 /* Compilation units have a DW_AT_name that is a filename, not
21734 a source language identifier. */
21735 case DW_TAG_enumeration_type:
21736 case DW_TAG_enumerator:
21737 /* These tags always have simple identifiers already; no need
21738 to canonicalize them. */
21739 return DW_STRING (attr);
907af001 21740
96553a0c
DE
21741 case DW_TAG_namespace:
21742 if (attr != NULL && DW_STRING (attr) != NULL)
21743 return DW_STRING (attr);
21744 return CP_ANONYMOUS_NAMESPACE_STR;
21745
907af001
UW
21746 case DW_TAG_class_type:
21747 case DW_TAG_interface_type:
21748 case DW_TAG_structure_type:
21749 case DW_TAG_union_type:
21750 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21751 structures or unions. These were of the form "._%d" in GCC 4.1,
21752 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21753 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21754 if (attr && DW_STRING (attr)
61012eef
GB
21755 && (startswith (DW_STRING (attr), "._")
21756 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21757 return NULL;
53832f31
TT
21758
21759 /* GCC might emit a nameless typedef that has a linkage name. See
21760 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21761 if (!attr || DW_STRING (attr) == NULL)
21762 {
73b9be8b 21763 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21764 if (attr == NULL || DW_STRING (attr) == NULL)
21765 return NULL;
21766
df5c6c50
JK
21767 /* Avoid demangling DW_STRING (attr) the second time on a second
21768 call for the same DIE. */
21769 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21770 {
43816ebc
TT
21771 gdb::unique_xmalloc_ptr<char> demangled
21772 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21773 if (demangled == nullptr)
21774 return nullptr;
43816ebc 21775
e6a959d6 21776 const char *base;
96408a79 21777
53832f31 21778 /* FIXME: we already did this for the partial symbol... */
34a68019 21779 DW_STRING (attr)
021887d8 21780 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 21781 demangled.get ());
53832f31 21782 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
21783
21784 /* Strip any leading namespaces/classes, keep only the base name.
21785 DW_AT_name for named DIEs does not contain the prefixes. */
21786 base = strrchr (DW_STRING (attr), ':');
21787 if (base && base > DW_STRING (attr) && base[-1] == ':')
21788 return &base[1];
21789 else
21790 return DW_STRING (attr);
53832f31
TT
21791 }
21792 }
907af001
UW
21793 break;
21794
71c25dea 21795 default:
907af001
UW
21796 break;
21797 }
21798
21799 if (!DW_STRING_IS_CANONICAL (attr))
21800 {
21801 DW_STRING (attr)
21802 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 21803 &objfile->per_bfd->storage_obstack);
907af001 21804 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21805 }
907af001 21806 return DW_STRING (attr);
9219021c
DC
21807}
21808
21809/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21810 is none. *EXT_CU is the CU containing DIE on input, and the CU
21811 containing the return value on output. */
9219021c
DC
21812
21813static struct die_info *
f2f0e013 21814dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21815{
21816 struct attribute *attr;
9219021c 21817
f2f0e013 21818 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21819 if (attr == NULL)
21820 return NULL;
21821
f2f0e013 21822 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21823}
21824
fa9c3fa0
TT
21825/* A convenience function that returns an "unknown" DWARF name,
21826 including the value of V. STR is the name of the entity being
21827 printed, e.g., "TAG". */
21828
21829static const char *
21830dwarf_unknown (const char *str, unsigned v)
21831{
21832 char *cell = get_print_cell ();
21833 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21834 return cell;
21835}
21836
c906108c
SS
21837/* Convert a DIE tag into its string name. */
21838
f39c6ffd 21839static const char *
aa1ee363 21840dwarf_tag_name (unsigned tag)
c906108c 21841{
f39c6ffd
TT
21842 const char *name = get_DW_TAG_name (tag);
21843
21844 if (name == NULL)
fa9c3fa0 21845 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
21846
21847 return name;
c906108c
SS
21848}
21849
21850/* Convert a DWARF attribute code into its string name. */
21851
f39c6ffd 21852static const char *
aa1ee363 21853dwarf_attr_name (unsigned attr)
c906108c 21854{
f39c6ffd
TT
21855 const char *name;
21856
c764a876 21857#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21858 if (attr == DW_AT_MIPS_fde)
21859 return "DW_AT_MIPS_fde";
21860#else
21861 if (attr == DW_AT_HP_block_index)
21862 return "DW_AT_HP_block_index";
c764a876 21863#endif
f39c6ffd
TT
21864
21865 name = get_DW_AT_name (attr);
21866
21867 if (name == NULL)
fa9c3fa0 21868 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
21869
21870 return name;
c906108c
SS
21871}
21872
21873/* Convert a DWARF value form code into its string name. */
21874
f39c6ffd 21875static const char *
aa1ee363 21876dwarf_form_name (unsigned form)
c906108c 21877{
f39c6ffd
TT
21878 const char *name = get_DW_FORM_name (form);
21879
21880 if (name == NULL)
fa9c3fa0 21881 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
21882
21883 return name;
c906108c
SS
21884}
21885
a121b7c1 21886static const char *
fba45db2 21887dwarf_bool_name (unsigned mybool)
c906108c
SS
21888{
21889 if (mybool)
21890 return "TRUE";
21891 else
21892 return "FALSE";
21893}
21894
21895/* Convert a DWARF type code into its string name. */
21896
f39c6ffd 21897static const char *
aa1ee363 21898dwarf_type_encoding_name (unsigned enc)
c906108c 21899{
f39c6ffd 21900 const char *name = get_DW_ATE_name (enc);
c906108c 21901
f39c6ffd 21902 if (name == NULL)
fa9c3fa0 21903 return dwarf_unknown ("ATE", enc);
c906108c 21904
f39c6ffd 21905 return name;
c906108c 21906}
c906108c 21907
f9aca02d 21908static void
d97bc12b 21909dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21910{
21911 unsigned int i;
21912
d97bc12b 21913 print_spaces (indent, f);
9d8780f0 21914 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21915 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21916 sect_offset_str (die->sect_off));
d97bc12b
DE
21917
21918 if (die->parent != NULL)
21919 {
21920 print_spaces (indent, f);
9d8780f0
SM
21921 fprintf_unfiltered (f, " parent at offset: %s\n",
21922 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21923 }
21924
21925 print_spaces (indent, f);
21926 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21927 dwarf_bool_name (die->child != NULL));
c906108c 21928
d97bc12b
DE
21929 print_spaces (indent, f);
21930 fprintf_unfiltered (f, " attributes:\n");
21931
c906108c
SS
21932 for (i = 0; i < die->num_attrs; ++i)
21933 {
d97bc12b
DE
21934 print_spaces (indent, f);
21935 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21936 dwarf_attr_name (die->attrs[i].name),
21937 dwarf_form_name (die->attrs[i].form));
d97bc12b 21938
c906108c
SS
21939 switch (die->attrs[i].form)
21940 {
c906108c 21941 case DW_FORM_addr:
336d760d 21942 case DW_FORM_addrx:
3019eac3 21943 case DW_FORM_GNU_addr_index:
d97bc12b 21944 fprintf_unfiltered (f, "address: ");
5af949e3 21945 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21946 break;
21947 case DW_FORM_block2:
21948 case DW_FORM_block4:
21949 case DW_FORM_block:
21950 case DW_FORM_block1:
56eb65bd
SP
21951 fprintf_unfiltered (f, "block: size %s",
21952 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21953 break;
2dc7f7b3 21954 case DW_FORM_exprloc:
56eb65bd
SP
21955 fprintf_unfiltered (f, "expression: size %s",
21956 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21957 break;
0224619f
JK
21958 case DW_FORM_data16:
21959 fprintf_unfiltered (f, "constant of 16 bytes");
21960 break;
4568ecf9
DE
21961 case DW_FORM_ref_addr:
21962 fprintf_unfiltered (f, "ref address: ");
21963 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21964 break;
36586728
TT
21965 case DW_FORM_GNU_ref_alt:
21966 fprintf_unfiltered (f, "alt ref address: ");
21967 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21968 break;
10b3939b
DJ
21969 case DW_FORM_ref1:
21970 case DW_FORM_ref2:
21971 case DW_FORM_ref4:
4568ecf9
DE
21972 case DW_FORM_ref8:
21973 case DW_FORM_ref_udata:
d97bc12b 21974 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21975 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21976 break;
c906108c
SS
21977 case DW_FORM_data1:
21978 case DW_FORM_data2:
21979 case DW_FORM_data4:
ce5d95e1 21980 case DW_FORM_data8:
c906108c
SS
21981 case DW_FORM_udata:
21982 case DW_FORM_sdata:
43bbcdc2
PH
21983 fprintf_unfiltered (f, "constant: %s",
21984 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21985 break;
2dc7f7b3
TT
21986 case DW_FORM_sec_offset:
21987 fprintf_unfiltered (f, "section offset: %s",
21988 pulongest (DW_UNSND (&die->attrs[i])));
21989 break;
55f1336d 21990 case DW_FORM_ref_sig8:
ac9ec31b
DE
21991 fprintf_unfiltered (f, "signature: %s",
21992 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21993 break;
c906108c 21994 case DW_FORM_string:
4bdf3d34 21995 case DW_FORM_strp:
43988095 21996 case DW_FORM_line_strp:
cf532bd1 21997 case DW_FORM_strx:
3019eac3 21998 case DW_FORM_GNU_str_index:
36586728 21999 case DW_FORM_GNU_strp_alt:
8285870a 22000 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22001 DW_STRING (&die->attrs[i])
8285870a
JK
22002 ? DW_STRING (&die->attrs[i]) : "",
22003 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22004 break;
22005 case DW_FORM_flag:
22006 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22007 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22008 else
d97bc12b 22009 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22010 break;
2dc7f7b3
TT
22011 case DW_FORM_flag_present:
22012 fprintf_unfiltered (f, "flag: TRUE");
22013 break;
a8329558 22014 case DW_FORM_indirect:
0963b4bd
MS
22015 /* The reader will have reduced the indirect form to
22016 the "base form" so this form should not occur. */
5f48f8f3 22017 fprintf_unfiltered (f,
3e43a32a 22018 "unexpected attribute form: DW_FORM_indirect");
a8329558 22019 break;
663c44ac
JK
22020 case DW_FORM_implicit_const:
22021 fprintf_unfiltered (f, "constant: %s",
22022 plongest (DW_SND (&die->attrs[i])));
22023 break;
c906108c 22024 default:
d97bc12b 22025 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22026 die->attrs[i].form);
d97bc12b 22027 break;
c906108c 22028 }
d97bc12b 22029 fprintf_unfiltered (f, "\n");
c906108c
SS
22030 }
22031}
22032
f9aca02d 22033static void
d97bc12b 22034dump_die_for_error (struct die_info *die)
c906108c 22035{
d97bc12b
DE
22036 dump_die_shallow (gdb_stderr, 0, die);
22037}
22038
22039static void
22040dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22041{
22042 int indent = level * 4;
22043
22044 gdb_assert (die != NULL);
22045
22046 if (level >= max_level)
22047 return;
22048
22049 dump_die_shallow (f, indent, die);
22050
22051 if (die->child != NULL)
c906108c 22052 {
d97bc12b
DE
22053 print_spaces (indent, f);
22054 fprintf_unfiltered (f, " Children:");
22055 if (level + 1 < max_level)
22056 {
22057 fprintf_unfiltered (f, "\n");
22058 dump_die_1 (f, level + 1, max_level, die->child);
22059 }
22060 else
22061 {
3e43a32a
MS
22062 fprintf_unfiltered (f,
22063 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22064 }
22065 }
22066
22067 if (die->sibling != NULL && level > 0)
22068 {
22069 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22070 }
22071}
22072
d97bc12b
DE
22073/* This is called from the pdie macro in gdbinit.in.
22074 It's not static so gcc will keep a copy callable from gdb. */
22075
22076void
22077dump_die (struct die_info *die, int max_level)
22078{
22079 dump_die_1 (gdb_stdlog, 0, max_level, die);
22080}
22081
f9aca02d 22082static void
51545339 22083store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22084{
51545339 22085 void **slot;
c906108c 22086
9c541725
PA
22087 slot = htab_find_slot_with_hash (cu->die_hash, die,
22088 to_underlying (die->sect_off),
b64f50a1 22089 INSERT);
51545339
DJ
22090
22091 *slot = die;
c906108c
SS
22092}
22093
b64f50a1
JK
22094/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22095 required kind. */
22096
22097static sect_offset
ff39bb5e 22098dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22099{
cd6c91b4 22100 if (attr->form_is_ref ())
9c541725 22101 return (sect_offset) DW_UNSND (attr);
93311388 22102
b98664d3 22103 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22104 dwarf_form_name (attr->form));
9c541725 22105 return {};
c906108c
SS
22106}
22107
43bbcdc2
PH
22108/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22109 * the value held by the attribute is not constant. */
a02abb62 22110
43bbcdc2 22111static LONGEST
ff39bb5e 22112dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22113{
663c44ac 22114 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22115 return DW_SND (attr);
22116 else if (attr->form == DW_FORM_udata
22117 || attr->form == DW_FORM_data1
22118 || attr->form == DW_FORM_data2
22119 || attr->form == DW_FORM_data4
22120 || attr->form == DW_FORM_data8)
22121 return DW_UNSND (attr);
22122 else
22123 {
cd6c91b4 22124 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22125 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22126 dwarf_form_name (attr->form));
22127 return default_value;
22128 }
22129}
22130
348e048f
DE
22131/* Follow reference or signature attribute ATTR of SRC_DIE.
22132 On entry *REF_CU is the CU of SRC_DIE.
22133 On exit *REF_CU is the CU of the result. */
22134
22135static struct die_info *
ff39bb5e 22136follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22137 struct dwarf2_cu **ref_cu)
22138{
22139 struct die_info *die;
22140
cd6c91b4 22141 if (attr->form_is_ref ())
348e048f 22142 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22143 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22144 die = follow_die_sig (src_die, attr, ref_cu);
22145 else
22146 {
22147 dump_die_for_error (src_die);
22148 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22149 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22150 }
22151
22152 return die;
03dd20cc
DJ
22153}
22154
5c631832 22155/* Follow reference OFFSET.
673bfd45
DE
22156 On entry *REF_CU is the CU of the source die referencing OFFSET.
22157 On exit *REF_CU is the CU of the result.
22158 Returns NULL if OFFSET is invalid. */
f504f079 22159
f9aca02d 22160static struct die_info *
9c541725 22161follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22162 struct dwarf2_cu **ref_cu)
c906108c 22163{
10b3939b 22164 struct die_info temp_die;
f2f0e013 22165 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22166 struct dwarf2_per_objfile *dwarf2_per_objfile
22167 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22168
348e048f
DE
22169 gdb_assert (cu->per_cu != NULL);
22170
98bfdba5
PA
22171 target_cu = cu;
22172
3019eac3 22173 if (cu->per_cu->is_debug_types)
348e048f
DE
22174 {
22175 /* .debug_types CUs cannot reference anything outside their CU.
22176 If they need to, they have to reference a signatured type via
55f1336d 22177 DW_FORM_ref_sig8. */
4057dfde 22178 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22179 return NULL;
348e048f 22180 }
36586728 22181 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22182 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22183 {
22184 struct dwarf2_per_cu_data *per_cu;
9a619af0 22185
9c541725 22186 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22187 dwarf2_per_objfile);
03dd20cc
DJ
22188
22189 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22190 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22191 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22192
10b3939b
DJ
22193 target_cu = per_cu->cu;
22194 }
98bfdba5
PA
22195 else if (cu->dies == NULL)
22196 {
22197 /* We're loading full DIEs during partial symbol reading. */
22198 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22199 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22200 }
c906108c 22201
f2f0e013 22202 *ref_cu = target_cu;
9c541725 22203 temp_die.sect_off = sect_off;
c24bdb02
KS
22204
22205 if (target_cu != cu)
22206 target_cu->ancestor = cu;
22207
9a3c8263 22208 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22209 &temp_die,
22210 to_underlying (sect_off));
5c631832 22211}
10b3939b 22212
5c631832
JK
22213/* Follow reference attribute ATTR of SRC_DIE.
22214 On entry *REF_CU is the CU of SRC_DIE.
22215 On exit *REF_CU is the CU of the result. */
22216
22217static struct die_info *
ff39bb5e 22218follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22219 struct dwarf2_cu **ref_cu)
22220{
9c541725 22221 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22222 struct dwarf2_cu *cu = *ref_cu;
22223 struct die_info *die;
22224
9c541725 22225 die = follow_die_offset (sect_off,
36586728
TT
22226 (attr->form == DW_FORM_GNU_ref_alt
22227 || cu->per_cu->is_dwz),
22228 ref_cu);
5c631832 22229 if (!die)
9d8780f0
SM
22230 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22231 "at %s [in module %s]"),
22232 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22233 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22234
5c631832
JK
22235 return die;
22236}
22237
d4c9a4f8 22238/* See read.h. */
5c631832
JK
22239
22240struct dwarf2_locexpr_baton
9c541725 22241dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22242 dwarf2_per_cu_data *per_cu,
8b9737bf 22243 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22244 void *baton, bool resolve_abstract_p)
5c631832 22245{
918dd910 22246 struct dwarf2_cu *cu;
5c631832
JK
22247 struct die_info *die;
22248 struct attribute *attr;
22249 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22250 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22251 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22252
918dd910 22253 if (per_cu->cu == NULL)
58f0c718 22254 load_cu (per_cu, false);
918dd910 22255 cu = per_cu->cu;
cc12ce38
DE
22256 if (cu == NULL)
22257 {
22258 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22259 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22260 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22261 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22262 }
918dd910 22263
9c541725 22264 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22265 if (!die)
9d8780f0
SM
22266 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22267 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22268
22269 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22270 if (!attr && resolve_abstract_p
3360b6e7 22271 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22272 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22273 {
22274 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22275 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22276 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22277
3360b6e7
TV
22278 for (const auto &cand_off
22279 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22280 {
3360b6e7
TV
22281 struct dwarf2_cu *cand_cu = cu;
22282 struct die_info *cand
22283 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22284 if (!cand
22285 || !cand->parent
e4a62c65
TV
22286 || cand->parent->tag != DW_TAG_subprogram)
22287 continue;
22288
22289 CORE_ADDR pc_low, pc_high;
22290 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22291 if (pc_low == ((CORE_ADDR) -1))
22292 continue;
22293 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22294 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22295 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22296 continue;
22297
22298 die = cand;
22299 attr = dwarf2_attr (die, DW_AT_location, cu);
22300 break;
22301 }
22302 }
22303
5c631832
JK
22304 if (!attr)
22305 {
e103e986
JK
22306 /* DWARF: "If there is no such attribute, then there is no effect.".
22307 DATA is ignored if SIZE is 0. */
5c631832 22308
e103e986 22309 retval.data = NULL;
5c631832
JK
22310 retval.size = 0;
22311 }
cd6c91b4 22312 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22313 {
22314 struct dwarf2_loclist_baton loclist_baton;
22315 CORE_ADDR pc = (*get_frame_pc) (baton);
22316 size_t size;
22317
22318 fill_in_loclist_baton (cu, &loclist_baton, attr);
22319
22320 retval.data = dwarf2_find_location_expression (&loclist_baton,
22321 &size, pc);
22322 retval.size = size;
22323 }
5c631832
JK
22324 else
22325 {
4fc6c0d5 22326 if (!attr->form_is_block ())
9d8780f0 22327 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22328 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22329 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22330
22331 retval.data = DW_BLOCK (attr)->data;
22332 retval.size = DW_BLOCK (attr)->size;
22333 }
22334 retval.per_cu = cu->per_cu;
918dd910 22335
ed2dc618 22336 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22337
5c631832 22338 return retval;
348e048f
DE
22339}
22340
d4c9a4f8 22341/* See read.h. */
8b9737bf
TT
22342
22343struct dwarf2_locexpr_baton
22344dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22345 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22346 CORE_ADDR (*get_frame_pc) (void *baton),
22347 void *baton)
22348{
9c541725 22349 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22350
9c541725 22351 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22352}
22353
b6807d98
TT
22354/* Write a constant of a given type as target-ordered bytes into
22355 OBSTACK. */
22356
22357static const gdb_byte *
22358write_constant_as_bytes (struct obstack *obstack,
22359 enum bfd_endian byte_order,
22360 struct type *type,
22361 ULONGEST value,
22362 LONGEST *len)
22363{
22364 gdb_byte *result;
22365
22366 *len = TYPE_LENGTH (type);
224c3ddb 22367 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22368 store_unsigned_integer (result, *len, byte_order, value);
22369
22370 return result;
22371}
22372
d4c9a4f8 22373/* See read.h. */
b6807d98
TT
22374
22375const gdb_byte *
9c541725 22376dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22377 dwarf2_per_cu_data *per_cu,
22378 obstack *obstack,
b6807d98
TT
22379 LONGEST *len)
22380{
22381 struct dwarf2_cu *cu;
22382 struct die_info *die;
22383 struct attribute *attr;
22384 const gdb_byte *result = NULL;
22385 struct type *type;
22386 LONGEST value;
22387 enum bfd_endian byte_order;
e3b94546 22388 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22389
b6807d98 22390 if (per_cu->cu == NULL)
58f0c718 22391 load_cu (per_cu, false);
b6807d98 22392 cu = per_cu->cu;
cc12ce38
DE
22393 if (cu == NULL)
22394 {
22395 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22396 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22397 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22398 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22399 }
b6807d98 22400
9c541725 22401 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22402 if (!die)
9d8780f0
SM
22403 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22404 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22405
22406 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22407 if (attr == NULL)
22408 return NULL;
22409
e3b94546 22410 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22411 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22412
22413 switch (attr->form)
22414 {
22415 case DW_FORM_addr:
336d760d 22416 case DW_FORM_addrx:
b6807d98
TT
22417 case DW_FORM_GNU_addr_index:
22418 {
22419 gdb_byte *tem;
22420
22421 *len = cu->header.addr_size;
224c3ddb 22422 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22423 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22424 result = tem;
22425 }
22426 break;
22427 case DW_FORM_string:
22428 case DW_FORM_strp:
cf532bd1 22429 case DW_FORM_strx:
b6807d98
TT
22430 case DW_FORM_GNU_str_index:
22431 case DW_FORM_GNU_strp_alt:
22432 /* DW_STRING is already allocated on the objfile obstack, point
22433 directly to it. */
22434 result = (const gdb_byte *) DW_STRING (attr);
22435 *len = strlen (DW_STRING (attr));
22436 break;
22437 case DW_FORM_block1:
22438 case DW_FORM_block2:
22439 case DW_FORM_block4:
22440 case DW_FORM_block:
22441 case DW_FORM_exprloc:
0224619f 22442 case DW_FORM_data16:
b6807d98
TT
22443 result = DW_BLOCK (attr)->data;
22444 *len = DW_BLOCK (attr)->size;
22445 break;
22446
22447 /* The DW_AT_const_value attributes are supposed to carry the
22448 symbol's value "represented as it would be on the target
22449 architecture." By the time we get here, it's already been
22450 converted to host endianness, so we just need to sign- or
22451 zero-extend it as appropriate. */
22452 case DW_FORM_data1:
22453 type = die_type (die, cu);
22454 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22455 if (result == NULL)
22456 result = write_constant_as_bytes (obstack, byte_order,
22457 type, value, len);
22458 break;
22459 case DW_FORM_data2:
22460 type = die_type (die, cu);
22461 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22462 if (result == NULL)
22463 result = write_constant_as_bytes (obstack, byte_order,
22464 type, value, len);
22465 break;
22466 case DW_FORM_data4:
22467 type = die_type (die, cu);
22468 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22469 if (result == NULL)
22470 result = write_constant_as_bytes (obstack, byte_order,
22471 type, value, len);
22472 break;
22473 case DW_FORM_data8:
22474 type = die_type (die, cu);
22475 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22476 if (result == NULL)
22477 result = write_constant_as_bytes (obstack, byte_order,
22478 type, value, len);
22479 break;
22480
22481 case DW_FORM_sdata:
663c44ac 22482 case DW_FORM_implicit_const:
b6807d98
TT
22483 type = die_type (die, cu);
22484 result = write_constant_as_bytes (obstack, byte_order,
22485 type, DW_SND (attr), len);
22486 break;
22487
22488 case DW_FORM_udata:
22489 type = die_type (die, cu);
22490 result = write_constant_as_bytes (obstack, byte_order,
22491 type, DW_UNSND (attr), len);
22492 break;
22493
22494 default:
b98664d3 22495 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22496 dwarf_form_name (attr->form));
22497 break;
22498 }
22499
22500 return result;
22501}
22502
d4c9a4f8 22503/* See read.h. */
7942e96e
AA
22504
22505struct type *
9c541725 22506dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22507 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22508{
22509 struct dwarf2_cu *cu;
22510 struct die_info *die;
22511
7942e96e 22512 if (per_cu->cu == NULL)
58f0c718 22513 load_cu (per_cu, false);
7942e96e
AA
22514 cu = per_cu->cu;
22515 if (!cu)
22516 return NULL;
22517
9c541725 22518 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22519 if (!die)
22520 return NULL;
22521
22522 return die_type (die, cu);
22523}
22524
8cb5117c 22525/* See read.h. */
8a9b8146
TT
22526
22527struct type *
b64f50a1 22528dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22529 struct dwarf2_per_cu_data *per_cu)
22530{
9c541725 22531 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22532 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22533}
22534
ac9ec31b 22535/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22536 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22537 On exit *REF_CU is the CU of the result.
22538 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22539
22540static struct die_info *
ac9ec31b
DE
22541follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22542 struct dwarf2_cu **ref_cu)
348e048f 22543{
348e048f 22544 struct die_info temp_die;
c24bdb02 22545 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22546 struct die_info *die;
22547
ac9ec31b
DE
22548 /* While it might be nice to assert sig_type->type == NULL here,
22549 we can get here for DW_AT_imported_declaration where we need
22550 the DIE not the type. */
348e048f
DE
22551
22552 /* If necessary, add it to the queue and load its DIEs. */
22553
95554aad 22554 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22555 read_signatured_type (sig_type);
348e048f 22556
348e048f 22557 sig_cu = sig_type->per_cu.cu;
69d751e3 22558 gdb_assert (sig_cu != NULL);
9c541725
PA
22559 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22560 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22561 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22562 to_underlying (temp_die.sect_off));
348e048f
DE
22563 if (die)
22564 {
ed2dc618 22565 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22566 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22567
796a7ff8
DE
22568 /* For .gdb_index version 7 keep track of included TUs.
22569 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22570 if (dwarf2_per_objfile->index_table != NULL
22571 && dwarf2_per_objfile->index_table->version <= 7)
22572 {
ae640021 22573 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22574 }
22575
348e048f 22576 *ref_cu = sig_cu;
c24bdb02
KS
22577 if (sig_cu != cu)
22578 sig_cu->ancestor = cu;
22579
348e048f
DE
22580 return die;
22581 }
22582
ac9ec31b
DE
22583 return NULL;
22584}
22585
22586/* Follow signatured type referenced by ATTR in SRC_DIE.
22587 On entry *REF_CU is the CU of SRC_DIE.
22588 On exit *REF_CU is the CU of the result.
22589 The result is the DIE of the type.
22590 If the referenced type cannot be found an error is thrown. */
22591
22592static struct die_info *
ff39bb5e 22593follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22594 struct dwarf2_cu **ref_cu)
22595{
22596 ULONGEST signature = DW_SIGNATURE (attr);
22597 struct signatured_type *sig_type;
22598 struct die_info *die;
22599
22600 gdb_assert (attr->form == DW_FORM_ref_sig8);
22601
a2ce51a0 22602 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22603 /* sig_type will be NULL if the signatured type is missing from
22604 the debug info. */
22605 if (sig_type == NULL)
22606 {
22607 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22608 " from DIE at %s [in module %s]"),
22609 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22610 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22611 }
22612
22613 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22614 if (die == NULL)
22615 {
22616 dump_die_for_error (src_die);
22617 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22618 " from DIE at %s [in module %s]"),
22619 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22620 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22621 }
22622
22623 return die;
22624}
22625
22626/* Get the type specified by SIGNATURE referenced in DIE/CU,
22627 reading in and processing the type unit if necessary. */
22628
22629static struct type *
22630get_signatured_type (struct die_info *die, ULONGEST signature,
22631 struct dwarf2_cu *cu)
22632{
518817b3
SM
22633 struct dwarf2_per_objfile *dwarf2_per_objfile
22634 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22635 struct signatured_type *sig_type;
22636 struct dwarf2_cu *type_cu;
22637 struct die_info *type_die;
22638 struct type *type;
22639
a2ce51a0 22640 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22641 /* sig_type will be NULL if the signatured type is missing from
22642 the debug info. */
22643 if (sig_type == NULL)
22644 {
b98664d3 22645 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22646 " from DIE at %s [in module %s]"),
22647 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22648 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22649 return build_error_marker_type (cu, die);
22650 }
22651
22652 /* If we already know the type we're done. */
22653 if (sig_type->type != NULL)
22654 return sig_type->type;
22655
22656 type_cu = cu;
22657 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22658 if (type_die != NULL)
22659 {
22660 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22661 is created. This is important, for example, because for c++ classes
22662 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22663 type = read_type_die (type_die, type_cu);
22664 if (type == NULL)
22665 {
b98664d3 22666 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22667 " referenced from DIE at %s [in module %s]"),
22668 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22669 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22670 type = build_error_marker_type (cu, die);
22671 }
22672 }
22673 else
22674 {
b98664d3 22675 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22676 " from DIE at %s [in module %s]"),
22677 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22678 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22679 type = build_error_marker_type (cu, die);
22680 }
22681 sig_type->type = type;
22682
22683 return type;
22684}
22685
22686/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22687 reading in and processing the type unit if necessary. */
22688
22689static struct type *
ff39bb5e 22690get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22691 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22692{
22693 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22694 if (attr->form_is_ref ())
ac9ec31b
DE
22695 {
22696 struct dwarf2_cu *type_cu = cu;
22697 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22698
22699 return read_type_die (type_die, type_cu);
22700 }
22701 else if (attr->form == DW_FORM_ref_sig8)
22702 {
22703 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22704 }
22705 else
22706 {
518817b3
SM
22707 struct dwarf2_per_objfile *dwarf2_per_objfile
22708 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22709
b98664d3 22710 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22711 " at %s [in module %s]"),
22712 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22713 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22714 return build_error_marker_type (cu, die);
22715 }
348e048f
DE
22716}
22717
e5fe5e75 22718/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22719
22720static void
e5fe5e75 22721load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22722{
52dc124a 22723 struct signatured_type *sig_type;
348e048f 22724
f4dc4d17 22725 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22726 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22727
6721b2ec
DE
22728 /* We have the per_cu, but we need the signatured_type.
22729 Fortunately this is an easy translation. */
22730 gdb_assert (per_cu->is_debug_types);
22731 sig_type = (struct signatured_type *) per_cu;
348e048f 22732
6721b2ec 22733 gdb_assert (per_cu->cu == NULL);
348e048f 22734
52dc124a 22735 read_signatured_type (sig_type);
348e048f 22736
6721b2ec 22737 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22738}
22739
3019eac3
DE
22740/* Read in a signatured type and build its CU and DIEs.
22741 If the type is a stub for the real type in a DWO file,
22742 read in the real type from the DWO file as well. */
dee91e82
DE
22743
22744static void
22745read_signatured_type (struct signatured_type *sig_type)
22746{
22747 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22748
3019eac3 22749 gdb_assert (per_cu->is_debug_types);
dee91e82 22750 gdb_assert (per_cu->cu == NULL);
348e048f 22751
6751ebae 22752 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22753
22754 if (!reader.dummy_p)
22755 {
22756 struct dwarf2_cu *cu = reader.cu;
22757 const gdb_byte *info_ptr = reader.info_ptr;
22758
22759 gdb_assert (cu->die_hash == NULL);
22760 cu->die_hash =
22761 htab_create_alloc_ex (cu->header.length / 12,
22762 die_hash,
22763 die_eq,
22764 NULL,
22765 &cu->comp_unit_obstack,
22766 hashtab_obstack_allocate,
22767 dummy_obstack_deallocate);
22768
3e225074 22769 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22770 reader.comp_unit_die->child
22771 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22772 reader.comp_unit_die);
22773 cu->dies = reader.comp_unit_die;
22774 /* comp_unit_die is not stored in die_hash, no need. */
22775
22776 /* We try not to read any attributes in this function, because
22777 not all CUs needed for references have been loaded yet, and
22778 symbol table processing isn't initialized. But we have to
22779 set the CU language, or we won't be able to build types
22780 correctly. Similarly, if we do not read the producer, we can
22781 not apply producer-specific interpretation. */
22782 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22783
22784 reader.keep ();
c0ab21c2
TT
22785 }
22786
7ee85ab1 22787 sig_type->per_cu.tu_read = 1;
c906108c
SS
22788}
22789
c906108c
SS
22790/* Decode simple location descriptions.
22791 Given a pointer to a dwarf block that defines a location, compute
22792 the location and return the value.
22793
4cecd739
DJ
22794 NOTE drow/2003-11-18: This function is called in two situations
22795 now: for the address of static or global variables (partial symbols
22796 only) and for offsets into structures which are expected to be
22797 (more or less) constant. The partial symbol case should go away,
22798 and only the constant case should remain. That will let this
22799 function complain more accurately. A few special modes are allowed
22800 without complaint for global variables (for instance, global
22801 register values and thread-local values).
c906108c
SS
22802
22803 A location description containing no operations indicates that the
4cecd739 22804 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22805 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22806 callers will only want a very basic result and this can become a
21ae7a4d
JK
22807 complaint.
22808
22809 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22810
22811static CORE_ADDR
e7c27a73 22812decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22813{
518817b3 22814 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22815 size_t i;
22816 size_t size = blk->size;
d521ce57 22817 const gdb_byte *data = blk->data;
21ae7a4d
JK
22818 CORE_ADDR stack[64];
22819 int stacki;
22820 unsigned int bytes_read, unsnd;
22821 gdb_byte op;
c906108c 22822
21ae7a4d
JK
22823 i = 0;
22824 stacki = 0;
22825 stack[stacki] = 0;
22826 stack[++stacki] = 0;
22827
22828 while (i < size)
22829 {
22830 op = data[i++];
22831 switch (op)
22832 {
22833 case DW_OP_lit0:
22834 case DW_OP_lit1:
22835 case DW_OP_lit2:
22836 case DW_OP_lit3:
22837 case DW_OP_lit4:
22838 case DW_OP_lit5:
22839 case DW_OP_lit6:
22840 case DW_OP_lit7:
22841 case DW_OP_lit8:
22842 case DW_OP_lit9:
22843 case DW_OP_lit10:
22844 case DW_OP_lit11:
22845 case DW_OP_lit12:
22846 case DW_OP_lit13:
22847 case DW_OP_lit14:
22848 case DW_OP_lit15:
22849 case DW_OP_lit16:
22850 case DW_OP_lit17:
22851 case DW_OP_lit18:
22852 case DW_OP_lit19:
22853 case DW_OP_lit20:
22854 case DW_OP_lit21:
22855 case DW_OP_lit22:
22856 case DW_OP_lit23:
22857 case DW_OP_lit24:
22858 case DW_OP_lit25:
22859 case DW_OP_lit26:
22860 case DW_OP_lit27:
22861 case DW_OP_lit28:
22862 case DW_OP_lit29:
22863 case DW_OP_lit30:
22864 case DW_OP_lit31:
22865 stack[++stacki] = op - DW_OP_lit0;
22866 break;
f1bea926 22867
21ae7a4d
JK
22868 case DW_OP_reg0:
22869 case DW_OP_reg1:
22870 case DW_OP_reg2:
22871 case DW_OP_reg3:
22872 case DW_OP_reg4:
22873 case DW_OP_reg5:
22874 case DW_OP_reg6:
22875 case DW_OP_reg7:
22876 case DW_OP_reg8:
22877 case DW_OP_reg9:
22878 case DW_OP_reg10:
22879 case DW_OP_reg11:
22880 case DW_OP_reg12:
22881 case DW_OP_reg13:
22882 case DW_OP_reg14:
22883 case DW_OP_reg15:
22884 case DW_OP_reg16:
22885 case DW_OP_reg17:
22886 case DW_OP_reg18:
22887 case DW_OP_reg19:
22888 case DW_OP_reg20:
22889 case DW_OP_reg21:
22890 case DW_OP_reg22:
22891 case DW_OP_reg23:
22892 case DW_OP_reg24:
22893 case DW_OP_reg25:
22894 case DW_OP_reg26:
22895 case DW_OP_reg27:
22896 case DW_OP_reg28:
22897 case DW_OP_reg29:
22898 case DW_OP_reg30:
22899 case DW_OP_reg31:
22900 stack[++stacki] = op - DW_OP_reg0;
22901 if (i < size)
22902 dwarf2_complex_location_expr_complaint ();
22903 break;
c906108c 22904
21ae7a4d
JK
22905 case DW_OP_regx:
22906 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22907 i += bytes_read;
22908 stack[++stacki] = unsnd;
22909 if (i < size)
22910 dwarf2_complex_location_expr_complaint ();
22911 break;
c906108c 22912
21ae7a4d 22913 case DW_OP_addr:
c8a7a66f
TT
22914 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22915 &bytes_read);
21ae7a4d
JK
22916 i += bytes_read;
22917 break;
d53d4ac5 22918
21ae7a4d
JK
22919 case DW_OP_const1u:
22920 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22921 i += 1;
22922 break;
22923
22924 case DW_OP_const1s:
22925 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22926 i += 1;
22927 break;
22928
22929 case DW_OP_const2u:
22930 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22931 i += 2;
22932 break;
22933
22934 case DW_OP_const2s:
22935 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22936 i += 2;
22937 break;
d53d4ac5 22938
21ae7a4d
JK
22939 case DW_OP_const4u:
22940 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22941 i += 4;
22942 break;
22943
22944 case DW_OP_const4s:
22945 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22946 i += 4;
22947 break;
22948
585861ea
JK
22949 case DW_OP_const8u:
22950 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22951 i += 8;
22952 break;
22953
21ae7a4d
JK
22954 case DW_OP_constu:
22955 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22956 &bytes_read);
22957 i += bytes_read;
22958 break;
22959
22960 case DW_OP_consts:
22961 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22962 i += bytes_read;
22963 break;
22964
22965 case DW_OP_dup:
22966 stack[stacki + 1] = stack[stacki];
22967 stacki++;
22968 break;
22969
22970 case DW_OP_plus:
22971 stack[stacki - 1] += stack[stacki];
22972 stacki--;
22973 break;
22974
22975 case DW_OP_plus_uconst:
22976 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22977 &bytes_read);
22978 i += bytes_read;
22979 break;
22980
22981 case DW_OP_minus:
22982 stack[stacki - 1] -= stack[stacki];
22983 stacki--;
22984 break;
22985
22986 case DW_OP_deref:
22987 /* If we're not the last op, then we definitely can't encode
22988 this using GDB's address_class enum. This is valid for partial
22989 global symbols, although the variable's address will be bogus
22990 in the psymtab. */
22991 if (i < size)
22992 dwarf2_complex_location_expr_complaint ();
22993 break;
22994
22995 case DW_OP_GNU_push_tls_address:
4aa4e28b 22996 case DW_OP_form_tls_address:
21ae7a4d
JK
22997 /* The top of the stack has the offset from the beginning
22998 of the thread control block at which the variable is located. */
22999 /* Nothing should follow this operator, so the top of stack would
23000 be returned. */
23001 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23002 address will be bogus in the psymtab. Make it always at least
23003 non-zero to not look as a variable garbage collected by linker
23004 which have DW_OP_addr 0. */
21ae7a4d
JK
23005 if (i < size)
23006 dwarf2_complex_location_expr_complaint ();
585861ea 23007 stack[stacki]++;
21ae7a4d
JK
23008 break;
23009
23010 case DW_OP_GNU_uninit:
23011 break;
23012
336d760d 23013 case DW_OP_addrx:
3019eac3 23014 case DW_OP_GNU_addr_index:
49f6c839 23015 case DW_OP_GNU_const_index:
3019eac3
DE
23016 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23017 &bytes_read);
23018 i += bytes_read;
23019 break;
23020
21ae7a4d
JK
23021 default:
23022 {
f39c6ffd 23023 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23024
23025 if (name)
b98664d3 23026 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23027 name);
23028 else
b98664d3 23029 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23030 op);
23031 }
23032
23033 return (stack[stacki]);
d53d4ac5 23034 }
3c6e0cb3 23035
21ae7a4d
JK
23036 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23037 outside of the allocated space. Also enforce minimum>0. */
23038 if (stacki >= ARRAY_SIZE (stack) - 1)
23039 {
b98664d3 23040 complaint (_("location description stack overflow"));
21ae7a4d
JK
23041 return 0;
23042 }
23043
23044 if (stacki <= 0)
23045 {
b98664d3 23046 complaint (_("location description stack underflow"));
21ae7a4d
JK
23047 return 0;
23048 }
23049 }
23050 return (stack[stacki]);
c906108c
SS
23051}
23052
23053/* memory allocation interface */
23054
c906108c 23055static struct dwarf_block *
7b5a2f43 23056dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23057{
8d749320 23058 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23059}
23060
c906108c 23061static struct die_info *
b60c80d6 23062dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23063{
23064 struct die_info *die;
b60c80d6
DJ
23065 size_t size = sizeof (struct die_info);
23066
23067 if (num_attrs > 1)
23068 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23069
b60c80d6 23070 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23071 memset (die, 0, sizeof (struct die_info));
23072 return (die);
23073}
2e276125
JB
23074
23075\f
23076/* Macro support. */
23077
2e276125 23078static struct macro_source_file *
804d2729
TT
23079macro_start_file (struct dwarf2_cu *cu,
23080 int file, int line,
2e276125 23081 struct macro_source_file *current_file,
43f3e411 23082 struct line_header *lh)
2e276125 23083{
233d95b5 23084 /* File name relative to the compilation directory of this source file. */
03075812 23085 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
2e276125 23086
2e276125 23087 if (! current_file)
abc9d0dc 23088 {
fc474241
DE
23089 /* Note: We don't create a macro table for this compilation unit
23090 at all until we actually get a filename. */
c24bdb02 23091 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 23092
abc9d0dc
TT
23093 /* If we have no current file, then this must be the start_file
23094 directive for the compilation unit's main source file. */
03075812 23095 current_file = macro_set_main (macro_table, file_name.get ());
fc474241 23096 macro_define_special (macro_table);
abc9d0dc 23097 }
2e276125 23098 else
03075812 23099 current_file = macro_include (current_file, line, file_name.get ());
6e70227d 23100
2e276125
JB
23101 return current_file;
23102}
23103
2e276125
JB
23104static const char *
23105consume_improper_spaces (const char *p, const char *body)
23106{
23107 if (*p == ' ')
23108 {
b98664d3 23109 complaint (_("macro definition contains spaces "
3e43a32a 23110 "in formal argument list:\n`%s'"),
4d3c2250 23111 body);
2e276125
JB
23112
23113 while (*p == ' ')
23114 p++;
23115 }
23116
23117 return p;
23118}
23119
23120
23121static void
23122parse_macro_definition (struct macro_source_file *file, int line,
23123 const char *body)
23124{
23125 const char *p;
23126
23127 /* The body string takes one of two forms. For object-like macro
23128 definitions, it should be:
23129
23130 <macro name> " " <definition>
23131
23132 For function-like macro definitions, it should be:
23133
23134 <macro name> "() " <definition>
23135 or
23136 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23137
23138 Spaces may appear only where explicitly indicated, and in the
23139 <definition>.
23140
23141 The Dwarf 2 spec says that an object-like macro's name is always
23142 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23143 the space when the macro's definition is the empty string.
2e276125
JB
23144
23145 The Dwarf 2 spec says that there should be no spaces between the
23146 formal arguments in a function-like macro's formal argument list,
23147 but versions of GCC around March 2002 include spaces after the
23148 commas. */
23149
23150
23151 /* Find the extent of the macro name. The macro name is terminated
23152 by either a space or null character (for an object-like macro) or
23153 an opening paren (for a function-like macro). */
23154 for (p = body; *p; p++)
23155 if (*p == ' ' || *p == '(')
23156 break;
23157
23158 if (*p == ' ' || *p == '\0')
23159 {
23160 /* It's an object-like macro. */
23161 int name_len = p - body;
456e800a 23162 std::string name (body, name_len);
2e276125
JB
23163 const char *replacement;
23164
23165 if (*p == ' ')
23166 replacement = body + name_len + 1;
23167 else
23168 {
4d3c2250 23169 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23170 replacement = body + name_len;
23171 }
6e70227d 23172
456e800a 23173 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23174 }
23175 else if (*p == '(')
23176 {
23177 /* It's a function-like macro. */
456e800a 23178 std::string name (body, p - body);
2e276125
JB
23179 int argc = 0;
23180 int argv_size = 1;
8d749320 23181 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23182
23183 p++;
23184
23185 p = consume_improper_spaces (p, body);
23186
23187 /* Parse the formal argument list. */
23188 while (*p && *p != ')')
23189 {
23190 /* Find the extent of the current argument name. */
23191 const char *arg_start = p;
23192
23193 while (*p && *p != ',' && *p != ')' && *p != ' ')
23194 p++;
23195
23196 if (! *p || p == arg_start)
4d3c2250 23197 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23198 else
23199 {
23200 /* Make sure argv has room for the new argument. */
23201 if (argc >= argv_size)
23202 {
23203 argv_size *= 2;
224c3ddb 23204 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23205 }
23206
3f8a7804 23207 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23208 }
23209
23210 p = consume_improper_spaces (p, body);
23211
23212 /* Consume the comma, if present. */
23213 if (*p == ',')
23214 {
23215 p++;
23216
23217 p = consume_improper_spaces (p, body);
23218 }
23219 }
23220
23221 if (*p == ')')
23222 {
23223 p++;
23224
23225 if (*p == ' ')
23226 /* Perfectly formed definition, no complaints. */
456e800a 23227 macro_define_function (file, line, name.c_str (),
6e70227d 23228 argc, (const char **) argv,
2e276125
JB
23229 p + 1);
23230 else if (*p == '\0')
23231 {
23232 /* Complain, but do define it. */
4d3c2250 23233 dwarf2_macro_malformed_definition_complaint (body);
456e800a 23234 macro_define_function (file, line, name.c_str (),
6e70227d 23235 argc, (const char **) argv,
2e276125
JB
23236 p);
23237 }
23238 else
23239 /* Just complain. */
4d3c2250 23240 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23241 }
23242 else
23243 /* Just complain. */
4d3c2250 23244 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23245
2e276125
JB
23246 {
23247 int i;
23248
23249 for (i = 0; i < argc; i++)
23250 xfree (argv[i]);
23251 }
23252 xfree (argv);
23253 }
23254 else
4d3c2250 23255 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23256}
23257
cf2c3c16
TT
23258/* Skip some bytes from BYTES according to the form given in FORM.
23259 Returns the new pointer. */
2e276125 23260
d521ce57
TT
23261static const gdb_byte *
23262skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23263 enum dwarf_form form,
23264 unsigned int offset_size,
23265 struct dwarf2_section_info *section)
2e276125 23266{
cf2c3c16 23267 unsigned int bytes_read;
2e276125 23268
cf2c3c16 23269 switch (form)
2e276125 23270 {
cf2c3c16
TT
23271 case DW_FORM_data1:
23272 case DW_FORM_flag:
23273 ++bytes;
23274 break;
23275
23276 case DW_FORM_data2:
23277 bytes += 2;
23278 break;
23279
23280 case DW_FORM_data4:
23281 bytes += 4;
23282 break;
23283
23284 case DW_FORM_data8:
23285 bytes += 8;
23286 break;
23287
0224619f
JK
23288 case DW_FORM_data16:
23289 bytes += 16;
23290 break;
23291
cf2c3c16
TT
23292 case DW_FORM_string:
23293 read_direct_string (abfd, bytes, &bytes_read);
23294 bytes += bytes_read;
23295 break;
23296
23297 case DW_FORM_sec_offset:
23298 case DW_FORM_strp:
36586728 23299 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23300 bytes += offset_size;
23301 break;
23302
23303 case DW_FORM_block:
23304 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23305 bytes += bytes_read;
23306 break;
23307
23308 case DW_FORM_block1:
23309 bytes += 1 + read_1_byte (abfd, bytes);
23310 break;
23311 case DW_FORM_block2:
23312 bytes += 2 + read_2_bytes (abfd, bytes);
23313 break;
23314 case DW_FORM_block4:
23315 bytes += 4 + read_4_bytes (abfd, bytes);
23316 break;
23317
336d760d 23318 case DW_FORM_addrx:
cf2c3c16 23319 case DW_FORM_sdata:
cf532bd1 23320 case DW_FORM_strx:
cf2c3c16 23321 case DW_FORM_udata:
3019eac3
DE
23322 case DW_FORM_GNU_addr_index:
23323 case DW_FORM_GNU_str_index:
d521ce57 23324 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23325 if (bytes == NULL)
23326 {
23327 dwarf2_section_buffer_overflow_complaint (section);
23328 return NULL;
23329 }
cf2c3c16
TT
23330 break;
23331
663c44ac
JK
23332 case DW_FORM_implicit_const:
23333 break;
23334
cf2c3c16
TT
23335 default:
23336 {
b98664d3 23337 complaint (_("invalid form 0x%x in `%s'"),
96b79293 23338 form, section->get_name ());
cf2c3c16
TT
23339 return NULL;
23340 }
2e276125
JB
23341 }
23342
cf2c3c16
TT
23343 return bytes;
23344}
757a13d0 23345
cf2c3c16
TT
23346/* A helper for dwarf_decode_macros that handles skipping an unknown
23347 opcode. Returns an updated pointer to the macro data buffer; or,
23348 on error, issues a complaint and returns NULL. */
757a13d0 23349
d521ce57 23350static const gdb_byte *
cf2c3c16 23351skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23352 const gdb_byte **opcode_definitions,
23353 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23354 bfd *abfd,
23355 unsigned int offset_size,
23356 struct dwarf2_section_info *section)
23357{
23358 unsigned int bytes_read, i;
23359 unsigned long arg;
d521ce57 23360 const gdb_byte *defn;
2e276125 23361
cf2c3c16 23362 if (opcode_definitions[opcode] == NULL)
2e276125 23363 {
b98664d3 23364 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23365 opcode);
23366 return NULL;
23367 }
2e276125 23368
cf2c3c16
TT
23369 defn = opcode_definitions[opcode];
23370 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23371 defn += bytes_read;
2e276125 23372
cf2c3c16
TT
23373 for (i = 0; i < arg; ++i)
23374 {
aead7601
SM
23375 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23376 (enum dwarf_form) defn[i], offset_size,
f664829e 23377 section);
cf2c3c16
TT
23378 if (mac_ptr == NULL)
23379 {
23380 /* skip_form_bytes already issued the complaint. */
23381 return NULL;
23382 }
23383 }
757a13d0 23384
cf2c3c16
TT
23385 return mac_ptr;
23386}
757a13d0 23387
cf2c3c16
TT
23388/* A helper function which parses the header of a macro section.
23389 If the macro section is the extended (for now called "GNU") type,
23390 then this updates *OFFSET_SIZE. Returns a pointer to just after
23391 the header, or issues a complaint and returns NULL on error. */
757a13d0 23392
d521ce57
TT
23393static const gdb_byte *
23394dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 23395 bfd *abfd,
d521ce57 23396 const gdb_byte *mac_ptr,
cf2c3c16
TT
23397 unsigned int *offset_size,
23398 int section_is_gnu)
23399{
23400 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 23401
cf2c3c16
TT
23402 if (section_is_gnu)
23403 {
23404 unsigned int version, flags;
757a13d0 23405
cf2c3c16 23406 version = read_2_bytes (abfd, mac_ptr);
0af92d60 23407 if (version != 4 && version != 5)
cf2c3c16 23408 {
b98664d3 23409 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
23410 version);
23411 return NULL;
23412 }
23413 mac_ptr += 2;
757a13d0 23414
cf2c3c16
TT
23415 flags = read_1_byte (abfd, mac_ptr);
23416 ++mac_ptr;
23417 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 23418
cf2c3c16
TT
23419 if ((flags & 2) != 0)
23420 /* We don't need the line table offset. */
23421 mac_ptr += *offset_size;
757a13d0 23422
cf2c3c16
TT
23423 /* Vendor opcode descriptions. */
23424 if ((flags & 4) != 0)
23425 {
23426 unsigned int i, count;
757a13d0 23427
cf2c3c16
TT
23428 count = read_1_byte (abfd, mac_ptr);
23429 ++mac_ptr;
23430 for (i = 0; i < count; ++i)
23431 {
23432 unsigned int opcode, bytes_read;
23433 unsigned long arg;
23434
23435 opcode = read_1_byte (abfd, mac_ptr);
23436 ++mac_ptr;
23437 opcode_definitions[opcode] = mac_ptr;
23438 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23439 mac_ptr += bytes_read;
23440 mac_ptr += arg;
23441 }
757a13d0 23442 }
cf2c3c16 23443 }
757a13d0 23444
cf2c3c16
TT
23445 return mac_ptr;
23446}
757a13d0 23447
cf2c3c16 23448/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 23449 including DW_MACRO_import. */
cf2c3c16
TT
23450
23451static void
804d2729 23452dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 23453 bfd *abfd,
d521ce57 23454 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 23455 struct macro_source_file *current_file,
43f3e411 23456 struct line_header *lh,
cf2c3c16 23457 struct dwarf2_section_info *section,
36586728 23458 int section_is_gnu, int section_is_dwz,
cf2c3c16 23459 unsigned int offset_size,
8fc3fc34 23460 htab_t include_hash)
cf2c3c16 23461{
804d2729
TT
23462 struct dwarf2_per_objfile *dwarf2_per_objfile
23463 = cu->per_cu->dwarf2_per_objfile;
4d663531 23464 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
23465 enum dwarf_macro_record_type macinfo_type;
23466 int at_commandline;
d521ce57 23467 const gdb_byte *opcode_definitions[256];
757a13d0 23468
cf2c3c16
TT
23469 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23470 &offset_size, section_is_gnu);
23471 if (mac_ptr == NULL)
23472 {
23473 /* We already issued a complaint. */
23474 return;
23475 }
757a13d0
JK
23476
23477 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23478 GDB is still reading the definitions from command line. First
23479 DW_MACINFO_start_file will need to be ignored as it was already executed
23480 to create CURRENT_FILE for the main source holding also the command line
23481 definitions. On first met DW_MACINFO_start_file this flag is reset to
23482 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23483
23484 at_commandline = 1;
23485
23486 do
23487 {
23488 /* Do we at least have room for a macinfo type byte? */
23489 if (mac_ptr >= mac_end)
23490 {
f664829e 23491 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
23492 break;
23493 }
23494
aead7601 23495 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
23496 mac_ptr++;
23497
cf2c3c16
TT
23498 /* Note that we rely on the fact that the corresponding GNU and
23499 DWARF constants are the same. */
132448f8
SM
23500 DIAGNOSTIC_PUSH
23501 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
23502 switch (macinfo_type)
23503 {
23504 /* A zero macinfo type indicates the end of the macro
23505 information. */
23506 case 0:
23507 break;
2e276125 23508
0af92d60
JK
23509 case DW_MACRO_define:
23510 case DW_MACRO_undef:
23511 case DW_MACRO_define_strp:
23512 case DW_MACRO_undef_strp:
23513 case DW_MACRO_define_sup:
23514 case DW_MACRO_undef_sup:
2e276125 23515 {
891d2f0b 23516 unsigned int bytes_read;
2e276125 23517 int line;
d521ce57 23518 const char *body;
cf2c3c16 23519 int is_define;
2e276125 23520
cf2c3c16
TT
23521 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23522 mac_ptr += bytes_read;
23523
0af92d60
JK
23524 if (macinfo_type == DW_MACRO_define
23525 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
23526 {
23527 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23528 mac_ptr += bytes_read;
23529 }
23530 else
23531 {
23532 LONGEST str_offset;
23533
24aa364d 23534 str_offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16 23535 mac_ptr += offset_size;
2e276125 23536
0af92d60
JK
23537 if (macinfo_type == DW_MACRO_define_sup
23538 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 23539 || section_is_dwz)
36586728 23540 {
ed2dc618
SM
23541 struct dwz_file *dwz
23542 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 23543
ed2dc618
SM
23544 body = read_indirect_string_from_dwz (objfile,
23545 dwz, str_offset);
36586728
TT
23546 }
23547 else
ed2dc618
SM
23548 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23549 abfd, str_offset);
cf2c3c16
TT
23550 }
23551
0af92d60
JK
23552 is_define = (macinfo_type == DW_MACRO_define
23553 || macinfo_type == DW_MACRO_define_strp
23554 || macinfo_type == DW_MACRO_define_sup);
2e276125 23555 if (! current_file)
757a13d0
JK
23556 {
23557 /* DWARF violation as no main source is present. */
b98664d3 23558 complaint (_("debug info with no main source gives macro %s "
757a13d0 23559 "on line %d: %s"),
cf2c3c16
TT
23560 is_define ? _("definition") : _("undefinition"),
23561 line, body);
757a13d0
JK
23562 break;
23563 }
3e43a32a
MS
23564 if ((line == 0 && !at_commandline)
23565 || (line != 0 && at_commandline))
b98664d3 23566 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 23567 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 23568 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
23569 line == 0 ? _("zero") : _("non-zero"), line, body);
23570
955b06fa 23571 if (body == NULL)
7bede828 23572 {
955b06fa
SDJ
23573 /* Fedora's rpm-build's "debugedit" binary
23574 corrupted .debug_macro sections.
23575
23576 For more info, see
23577 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23578 complaint (_("debug info gives %s invalid macro %s "
23579 "without body (corrupted?) at line %d "
23580 "on file %s"),
23581 at_commandline ? _("command-line") : _("in-file"),
23582 is_define ? _("definition") : _("undefinition"),
23583 line, current_file->filename);
7bede828 23584 }
955b06fa
SDJ
23585 else if (is_define)
23586 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
23587 else
23588 {
0af92d60
JK
23589 gdb_assert (macinfo_type == DW_MACRO_undef
23590 || macinfo_type == DW_MACRO_undef_strp
23591 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
23592 macro_undef (current_file, line, body);
23593 }
2e276125
JB
23594 }
23595 break;
23596
0af92d60 23597 case DW_MACRO_start_file:
2e276125 23598 {
891d2f0b 23599 unsigned int bytes_read;
2e276125
JB
23600 int line, file;
23601
23602 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23603 mac_ptr += bytes_read;
23604 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23605 mac_ptr += bytes_read;
23606
3e43a32a
MS
23607 if ((line == 0 && !at_commandline)
23608 || (line != 0 && at_commandline))
b98664d3 23609 complaint (_("debug info gives source %d included "
757a13d0
JK
23610 "from %s at %s line %d"),
23611 file, at_commandline ? _("command-line") : _("file"),
23612 line == 0 ? _("zero") : _("non-zero"), line);
23613
23614 if (at_commandline)
23615 {
0af92d60 23616 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23617 pass one. */
757a13d0
JK
23618 at_commandline = 0;
23619 }
23620 else
804d2729
TT
23621 current_file = macro_start_file (cu, file, line, current_file,
23622 lh);
2e276125
JB
23623 }
23624 break;
23625
0af92d60 23626 case DW_MACRO_end_file:
2e276125 23627 if (! current_file)
b98664d3 23628 complaint (_("macro debug info has an unmatched "
3e43a32a 23629 "`close_file' directive"));
2e276125
JB
23630 else
23631 {
23632 current_file = current_file->included_by;
23633 if (! current_file)
23634 {
cf2c3c16 23635 enum dwarf_macro_record_type next_type;
2e276125
JB
23636
23637 /* GCC circa March 2002 doesn't produce the zero
23638 type byte marking the end of the compilation
23639 unit. Complain if it's not there, but exit no
23640 matter what. */
23641
23642 /* Do we at least have room for a macinfo type byte? */
23643 if (mac_ptr >= mac_end)
23644 {
f664829e 23645 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23646 return;
23647 }
23648
23649 /* We don't increment mac_ptr here, so this is just
23650 a look-ahead. */
aead7601
SM
23651 next_type
23652 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23653 mac_ptr);
2e276125 23654 if (next_type != 0)
b98664d3 23655 complaint (_("no terminating 0-type entry for "
3e43a32a 23656 "macros in `.debug_macinfo' section"));
2e276125
JB
23657
23658 return;
23659 }
23660 }
23661 break;
23662
0af92d60
JK
23663 case DW_MACRO_import:
23664 case DW_MACRO_import_sup:
cf2c3c16
TT
23665 {
23666 LONGEST offset;
8fc3fc34 23667 void **slot;
a036ba48
TT
23668 bfd *include_bfd = abfd;
23669 struct dwarf2_section_info *include_section = section;
d521ce57 23670 const gdb_byte *include_mac_end = mac_end;
a036ba48 23671 int is_dwz = section_is_dwz;
d521ce57 23672 const gdb_byte *new_mac_ptr;
cf2c3c16 23673
24aa364d 23674 offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16
TT
23675 mac_ptr += offset_size;
23676
0af92d60 23677 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 23678 {
ed2dc618 23679 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 23680
96b79293 23681 dwz->macro.read (objfile);
a036ba48 23682
a036ba48 23683 include_section = &dwz->macro;
96b79293 23684 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
23685 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23686 is_dwz = 1;
23687 }
23688
23689 new_mac_ptr = include_section->buffer + offset;
23690 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23691
8fc3fc34
TT
23692 if (*slot != NULL)
23693 {
23694 /* This has actually happened; see
23695 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 23696 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
23697 ".debug_macro section"));
23698 }
23699 else
23700 {
d521ce57 23701 *slot = (void *) new_mac_ptr;
36586728 23702
804d2729 23703 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 23704 include_mac_end, current_file, lh,
36586728 23705 section, section_is_gnu, is_dwz,
4d663531 23706 offset_size, include_hash);
8fc3fc34 23707
d521ce57 23708 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23709 }
cf2c3c16
TT
23710 }
23711 break;
23712
2e276125 23713 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23714 if (!section_is_gnu)
23715 {
23716 unsigned int bytes_read;
2e276125 23717
ac298888
TT
23718 /* This reads the constant, but since we don't recognize
23719 any vendor extensions, we ignore it. */
23720 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23721 mac_ptr += bytes_read;
23722 read_direct_string (abfd, mac_ptr, &bytes_read);
23723 mac_ptr += bytes_read;
2e276125 23724
cf2c3c16
TT
23725 /* We don't recognize any vendor extensions. */
23726 break;
23727 }
23728 /* FALLTHROUGH */
23729
23730 default:
23731 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23732 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23733 section);
23734 if (mac_ptr == NULL)
23735 return;
23736 break;
2e276125 23737 }
132448f8 23738 DIAGNOSTIC_POP
757a13d0 23739 } while (macinfo_type != 0);
2e276125 23740}
8e19ed76 23741
cf2c3c16 23742static void
09262596 23743dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 23744 int section_is_gnu)
cf2c3c16 23745{
518817b3
SM
23746 struct dwarf2_per_objfile *dwarf2_per_objfile
23747 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23748 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
23749 struct line_header *lh = cu->line_header;
23750 bfd *abfd;
d521ce57 23751 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23752 struct macro_source_file *current_file = 0;
23753 enum dwarf_macro_record_type macinfo_type;
23754 unsigned int offset_size = cu->header.offset_size;
d521ce57 23755 const gdb_byte *opcode_definitions[256];
8fc3fc34 23756 void **slot;
09262596
DE
23757 struct dwarf2_section_info *section;
23758 const char *section_name;
23759
23760 if (cu->dwo_unit != NULL)
23761 {
23762 if (section_is_gnu)
23763 {
23764 section = &cu->dwo_unit->dwo_file->sections.macro;
23765 section_name = ".debug_macro.dwo";
23766 }
23767 else
23768 {
23769 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23770 section_name = ".debug_macinfo.dwo";
23771 }
23772 }
23773 else
23774 {
23775 if (section_is_gnu)
23776 {
23777 section = &dwarf2_per_objfile->macro;
23778 section_name = ".debug_macro";
23779 }
23780 else
23781 {
23782 section = &dwarf2_per_objfile->macinfo;
23783 section_name = ".debug_macinfo";
23784 }
23785 }
cf2c3c16 23786
96b79293 23787 section->read (objfile);
cf2c3c16
TT
23788 if (section->buffer == NULL)
23789 {
b98664d3 23790 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
23791 return;
23792 }
96b79293 23793 abfd = section->get_bfd_owner ();
cf2c3c16
TT
23794
23795 /* First pass: Find the name of the base filename.
23796 This filename is needed in order to process all macros whose definition
23797 (or undefinition) comes from the command line. These macros are defined
23798 before the first DW_MACINFO_start_file entry, and yet still need to be
23799 associated to the base file.
23800
23801 To determine the base file name, we scan the macro definitions until we
23802 reach the first DW_MACINFO_start_file entry. We then initialize
23803 CURRENT_FILE accordingly so that any macro definition found before the
23804 first DW_MACINFO_start_file can still be associated to the base file. */
23805
23806 mac_ptr = section->buffer + offset;
23807 mac_end = section->buffer + section->size;
23808
23809 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23810 &offset_size, section_is_gnu);
23811 if (mac_ptr == NULL)
23812 {
23813 /* We already issued a complaint. */
23814 return;
23815 }
23816
23817 do
23818 {
23819 /* Do we at least have room for a macinfo type byte? */
23820 if (mac_ptr >= mac_end)
23821 {
23822 /* Complaint is printed during the second pass as GDB will probably
23823 stop the first pass earlier upon finding
23824 DW_MACINFO_start_file. */
23825 break;
23826 }
23827
aead7601 23828 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23829 mac_ptr++;
23830
23831 /* Note that we rely on the fact that the corresponding GNU and
23832 DWARF constants are the same. */
132448f8
SM
23833 DIAGNOSTIC_PUSH
23834 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
23835 switch (macinfo_type)
23836 {
23837 /* A zero macinfo type indicates the end of the macro
23838 information. */
23839 case 0:
23840 break;
23841
0af92d60
JK
23842 case DW_MACRO_define:
23843 case DW_MACRO_undef:
cf2c3c16
TT
23844 /* Only skip the data by MAC_PTR. */
23845 {
23846 unsigned int bytes_read;
23847
23848 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23849 mac_ptr += bytes_read;
23850 read_direct_string (abfd, mac_ptr, &bytes_read);
23851 mac_ptr += bytes_read;
23852 }
23853 break;
23854
0af92d60 23855 case DW_MACRO_start_file:
cf2c3c16
TT
23856 {
23857 unsigned int bytes_read;
23858 int line, file;
23859
23860 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23861 mac_ptr += bytes_read;
23862 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23863 mac_ptr += bytes_read;
23864
804d2729 23865 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
23866 }
23867 break;
23868
0af92d60 23869 case DW_MACRO_end_file:
cf2c3c16
TT
23870 /* No data to skip by MAC_PTR. */
23871 break;
23872
0af92d60
JK
23873 case DW_MACRO_define_strp:
23874 case DW_MACRO_undef_strp:
23875 case DW_MACRO_define_sup:
23876 case DW_MACRO_undef_sup:
cf2c3c16
TT
23877 {
23878 unsigned int bytes_read;
23879
23880 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23881 mac_ptr += bytes_read;
23882 mac_ptr += offset_size;
23883 }
23884 break;
23885
0af92d60
JK
23886 case DW_MACRO_import:
23887 case DW_MACRO_import_sup:
cf2c3c16 23888 /* Note that, according to the spec, a transparent include
0af92d60 23889 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23890 skip this opcode. */
23891 mac_ptr += offset_size;
23892 break;
23893
23894 case DW_MACINFO_vendor_ext:
23895 /* Only skip the data by MAC_PTR. */
23896 if (!section_is_gnu)
23897 {
23898 unsigned int bytes_read;
23899
23900 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23901 mac_ptr += bytes_read;
23902 read_direct_string (abfd, mac_ptr, &bytes_read);
23903 mac_ptr += bytes_read;
23904 }
23905 /* FALLTHROUGH */
23906
23907 default:
23908 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23909 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23910 section);
23911 if (mac_ptr == NULL)
23912 return;
23913 break;
23914 }
132448f8 23915 DIAGNOSTIC_POP
cf2c3c16
TT
23916 } while (macinfo_type != 0 && current_file == NULL);
23917
23918 /* Second pass: Process all entries.
23919
23920 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23921 command-line macro definitions/undefinitions. This flag is unset when we
23922 reach the first DW_MACINFO_start_file entry. */
23923
fc4007c9
TT
23924 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23925 htab_eq_pointer,
23926 NULL, xcalloc, xfree));
8fc3fc34 23927 mac_ptr = section->buffer + offset;
fc4007c9 23928 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23929 *slot = (void *) mac_ptr;
804d2729 23930 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 23931 current_file, lh, section,
fc4007c9
TT
23932 section_is_gnu, 0, offset_size,
23933 include_hash.get ());
cf2c3c16
TT
23934}
23935
3019eac3
DE
23936/* Return the .debug_loc section to use for CU.
23937 For DWO files use .debug_loc.dwo. */
23938
23939static struct dwarf2_section_info *
23940cu_debug_loc_section (struct dwarf2_cu *cu)
23941{
518817b3
SM
23942 struct dwarf2_per_objfile *dwarf2_per_objfile
23943 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23944
3019eac3 23945 if (cu->dwo_unit)
43988095
JK
23946 {
23947 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23948
43988095
JK
23949 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23950 }
23951 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23952 : &dwarf2_per_objfile->loc);
3019eac3
DE
23953}
23954
8cf6f0b1
TT
23955/* A helper function that fills in a dwarf2_loclist_baton. */
23956
23957static void
23958fill_in_loclist_baton (struct dwarf2_cu *cu,
23959 struct dwarf2_loclist_baton *baton,
ff39bb5e 23960 const struct attribute *attr)
8cf6f0b1 23961{
518817b3
SM
23962 struct dwarf2_per_objfile *dwarf2_per_objfile
23963 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23964 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23965
96b79293 23966 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23967
23968 baton->per_cu = cu->per_cu;
23969 gdb_assert (baton->per_cu);
23970 /* We don't know how long the location list is, but make sure we
23971 don't run off the edge of the section. */
3019eac3
DE
23972 baton->size = section->size - DW_UNSND (attr);
23973 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23974 baton->base_address = cu->base_address;
f664829e 23975 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23976}
23977
4c2df51b 23978static void
ff39bb5e 23979dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23980 struct dwarf2_cu *cu, int is_block)
4c2df51b 23981{
518817b3
SM
23982 struct dwarf2_per_objfile *dwarf2_per_objfile
23983 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23984 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23985 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23986
cd6c91b4 23987 if (attr->form_is_section_offset ()
3019eac3 23988 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23989 the section. If so, fall through to the complaint in the
23990 other branch. */
2c7d5afc 23991 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23992 {
0d53c4c4 23993 struct dwarf2_loclist_baton *baton;
4c2df51b 23994
8d749320 23995 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23996
8cf6f0b1 23997 fill_in_loclist_baton (cu, baton, attr);
be391dca 23998
d00adf39 23999 if (cu->base_known == 0)
b98664d3 24000 complaint (_("Location list used without "
3e43a32a 24001 "specifying the CU base address."));
4c2df51b 24002
f1e6e072
TT
24003 SYMBOL_ACLASS_INDEX (sym) = (is_block
24004 ? dwarf2_loclist_block_index
24005 : dwarf2_loclist_index);
0d53c4c4
DJ
24006 SYMBOL_LOCATION_BATON (sym) = baton;
24007 }
24008 else
24009 {
24010 struct dwarf2_locexpr_baton *baton;
24011
8d749320 24012 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24013 baton->per_cu = cu->per_cu;
24014 gdb_assert (baton->per_cu);
0d53c4c4 24015
4fc6c0d5 24016 if (attr->form_is_block ())
0d53c4c4
DJ
24017 {
24018 /* Note that we're just copying the block's data pointer
24019 here, not the actual data. We're still pointing into the
6502dd73
DJ
24020 info_buffer for SYM's objfile; right now we never release
24021 that buffer, but when we do clean up properly this may
24022 need to change. */
0d53c4c4
DJ
24023 baton->size = DW_BLOCK (attr)->size;
24024 baton->data = DW_BLOCK (attr)->data;
24025 }
24026 else
24027 {
24028 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24029 sym->natural_name ());
0d53c4c4 24030 baton->size = 0;
0d53c4c4 24031 }
6e70227d 24032
f1e6e072
TT
24033 SYMBOL_ACLASS_INDEX (sym) = (is_block
24034 ? dwarf2_locexpr_block_index
24035 : dwarf2_locexpr_index);
0d53c4c4
DJ
24036 SYMBOL_LOCATION_BATON (sym) = baton;
24037 }
4c2df51b 24038}
6502dd73 24039
09ba997f 24040/* See read.h. */
ae0d2f24
UW
24041
24042struct objfile *
09ba997f 24043dwarf2_per_cu_data::objfile () const
ae0d2f24 24044{
09ba997f 24045 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
24046
24047 /* Return the master objfile, so that we can report and look up the
24048 correct file containing this variable. */
24049 if (objfile->separate_debug_objfile_backlink)
24050 objfile = objfile->separate_debug_objfile_backlink;
24051
24052 return objfile;
24053}
24054
96408a79
SA
24055/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24056 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24057 CU_HEADERP first. */
24058
24059static const struct comp_unit_head *
24060per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 24061 const struct dwarf2_per_cu_data *per_cu)
96408a79 24062{
d521ce57 24063 const gdb_byte *info_ptr;
96408a79
SA
24064
24065 if (per_cu->cu)
24066 return &per_cu->cu->header;
24067
9c541725 24068 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24069
24070 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24071 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24072 rcuh_kind::COMPILE);
96408a79
SA
24073
24074 return cu_headerp;
24075}
24076
09ba997f 24077/* See read.h. */
ae0d2f24 24078
98714339 24079int
09ba997f 24080dwarf2_per_cu_data::addr_size () const
ae0d2f24 24081{
96408a79
SA
24082 struct comp_unit_head cu_header_local;
24083 const struct comp_unit_head *cu_headerp;
c471e790 24084
09ba997f 24085 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24086
24087 return cu_headerp->addr_size;
ae0d2f24
UW
24088}
24089
09ba997f 24090/* See read.h. */
9eae7c52
TT
24091
24092int
09ba997f 24093dwarf2_per_cu_data::offset_size () const
9eae7c52 24094{
96408a79
SA
24095 struct comp_unit_head cu_header_local;
24096 const struct comp_unit_head *cu_headerp;
9c6c53f7 24097
09ba997f 24098 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24099
24100 return cu_headerp->offset_size;
24101}
24102
09ba997f 24103/* See read.h. */
96408a79
SA
24104
24105int
09ba997f 24106dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
24107{
24108 struct comp_unit_head cu_header_local;
24109 const struct comp_unit_head *cu_headerp;
24110
09ba997f 24111 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24112
24113 if (cu_headerp->version == 2)
24114 return cu_headerp->addr_size;
24115 else
24116 return cu_headerp->offset_size;
181cebd4
JK
24117}
24118
09ba997f 24119/* See read.h. */
9aa1f1e3
TT
24120
24121CORE_ADDR
09ba997f 24122dwarf2_per_cu_data::text_offset () const
9aa1f1e3 24123{
09ba997f
TT
24124 struct objfile *objfile = dwarf2_per_objfile->objfile;
24125
24126 return objfile->text_section_offset ();
9aa1f1e3
TT
24127}
24128
09ba997f
TT
24129/* See read.h. */
24130
24131struct type *
24132dwarf2_per_cu_data::addr_type () const
9a49df9d 24133{
09ba997f 24134 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
24135 struct type *void_type = objfile_type (objfile)->builtin_void;
24136 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 24137 int addr_size = this->addr_size ();
9a49df9d
AB
24138
24139 if (TYPE_LENGTH (addr_type) == addr_size)
24140 return addr_type;
24141
09ba997f 24142 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
24143 return addr_type;
24144}
24145
22b6cd70
TT
24146/* A helper function for dwarf2_find_containing_comp_unit that returns
24147 the index of the result, and that searches a vector. It will
24148 return a result even if the offset in question does not actually
24149 occur in any CU. This is separate so that it can be unit
24150 tested. */
ae038cb0 24151
22b6cd70
TT
24152static int
24153dwarf2_find_containing_comp_unit
24154 (sect_offset sect_off,
24155 unsigned int offset_in_dwz,
24156 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24157{
ae038cb0
DJ
24158 int low, high;
24159
ae038cb0 24160 low = 0;
22b6cd70 24161 high = all_comp_units.size () - 1;
ae038cb0
DJ
24162 while (high > low)
24163 {
36586728 24164 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24165 int mid = low + (high - low) / 2;
9a619af0 24166
22b6cd70 24167 mid_cu = all_comp_units[mid];
36586728 24168 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24169 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24170 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24171 high = mid;
24172 else
24173 low = mid + 1;
24174 }
24175 gdb_assert (low == high);
22b6cd70
TT
24176 return low;
24177}
24178
24179/* Locate the .debug_info compilation unit from CU's objfile which contains
24180 the DIE at OFFSET. Raises an error on failure. */
24181
24182static struct dwarf2_per_cu_data *
24183dwarf2_find_containing_comp_unit (sect_offset sect_off,
24184 unsigned int offset_in_dwz,
24185 struct dwarf2_per_objfile *dwarf2_per_objfile)
24186{
24187 int low
24188 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24189 dwarf2_per_objfile->all_comp_units);
24190 struct dwarf2_per_cu_data *this_cu
24191 = dwarf2_per_objfile->all_comp_units[low];
24192
45b8ae0c 24193 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24194 {
36586728 24195 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24196 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24197 "offset %s [in module %s]"),
24198 sect_offset_str (sect_off),
ed2dc618 24199 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24200
9c541725
PA
24201 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24202 <= sect_off);
ae038cb0
DJ
24203 return dwarf2_per_objfile->all_comp_units[low-1];
24204 }
24205 else
24206 {
b76e467d 24207 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24208 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24209 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24210 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24211 return this_cu;
24212 }
24213}
24214
22b6cd70
TT
24215#if GDB_SELF_TEST
24216
24217namespace selftests {
24218namespace find_containing_comp_unit {
24219
24220static void
24221run_test ()
24222{
24223 struct dwarf2_per_cu_data one {};
24224 struct dwarf2_per_cu_data two {};
24225 struct dwarf2_per_cu_data three {};
24226 struct dwarf2_per_cu_data four {};
24227
24228 one.length = 5;
24229 two.sect_off = sect_offset (one.length);
24230 two.length = 7;
24231
24232 three.length = 5;
24233 three.is_dwz = 1;
24234 four.sect_off = sect_offset (three.length);
24235 four.length = 7;
24236 four.is_dwz = 1;
24237
24238 std::vector<dwarf2_per_cu_data *> units;
24239 units.push_back (&one);
24240 units.push_back (&two);
24241 units.push_back (&three);
24242 units.push_back (&four);
24243
24244 int result;
24245
24246 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24247 SELF_CHECK (units[result] == &one);
24248 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24249 SELF_CHECK (units[result] == &one);
24250 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24251 SELF_CHECK (units[result] == &two);
24252
24253 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24254 SELF_CHECK (units[result] == &three);
24255 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24256 SELF_CHECK (units[result] == &three);
24257 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24258 SELF_CHECK (units[result] == &four);
24259}
24260
24261}
24262}
24263
24264#endif /* GDB_SELF_TEST */
24265
23745b47 24266/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24267
fcd3b13d
SM
24268dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24269 : per_cu (per_cu_),
9068261f
AB
24270 mark (false),
24271 has_loclist (false),
24272 checked_producer (false),
24273 producer_is_gxx_lt_4_6 (false),
24274 producer_is_gcc_lt_4_3 (false),
eb77c9df 24275 producer_is_icc (false),
9068261f 24276 producer_is_icc_lt_14 (false),
c258c396 24277 producer_is_codewarrior (false),
9068261f 24278 processing_has_namespace_info (false)
93311388 24279{
fcd3b13d
SM
24280 per_cu->cu = this;
24281}
24282
24283/* Destroy a dwarf2_cu. */
24284
24285dwarf2_cu::~dwarf2_cu ()
24286{
24287 per_cu->cu = NULL;
9816fde3
JK
24288}
24289
24290/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24291
24292static void
95554aad
TT
24293prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24294 enum language pretend_language)
9816fde3
JK
24295{
24296 struct attribute *attr;
24297
24298 /* Set the language we're debugging. */
24299 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24300 if (attr != nullptr)
9816fde3
JK
24301 set_cu_language (DW_UNSND (attr), cu);
24302 else
9cded63f 24303 {
95554aad 24304 cu->language = pretend_language;
9cded63f
TT
24305 cu->language_defn = language_def (cu->language);
24306 }
dee91e82 24307
7d45c7c3 24308 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24309}
24310
ae038cb0
DJ
24311/* Increase the age counter on each cached compilation unit, and free
24312 any that are too old. */
24313
24314static void
ed2dc618 24315age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24316{
24317 struct dwarf2_per_cu_data *per_cu, **last_chain;
24318
24319 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24320 per_cu = dwarf2_per_objfile->read_in_chain;
24321 while (per_cu != NULL)
24322 {
24323 per_cu->cu->last_used ++;
b4f54984 24324 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24325 dwarf2_mark (per_cu->cu);
24326 per_cu = per_cu->cu->read_in_chain;
24327 }
24328
24329 per_cu = dwarf2_per_objfile->read_in_chain;
24330 last_chain = &dwarf2_per_objfile->read_in_chain;
24331 while (per_cu != NULL)
24332 {
24333 struct dwarf2_per_cu_data *next_cu;
24334
24335 next_cu = per_cu->cu->read_in_chain;
24336
24337 if (!per_cu->cu->mark)
24338 {
fcd3b13d 24339 delete per_cu->cu;
ae038cb0
DJ
24340 *last_chain = next_cu;
24341 }
24342 else
24343 last_chain = &per_cu->cu->read_in_chain;
24344
24345 per_cu = next_cu;
24346 }
24347}
24348
24349/* Remove a single compilation unit from the cache. */
24350
24351static void
dee91e82 24352free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24353{
24354 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24355 struct dwarf2_per_objfile *dwarf2_per_objfile
24356 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24357
24358 per_cu = dwarf2_per_objfile->read_in_chain;
24359 last_chain = &dwarf2_per_objfile->read_in_chain;
24360 while (per_cu != NULL)
24361 {
24362 struct dwarf2_per_cu_data *next_cu;
24363
24364 next_cu = per_cu->cu->read_in_chain;
24365
dee91e82 24366 if (per_cu == target_per_cu)
ae038cb0 24367 {
fcd3b13d 24368 delete per_cu->cu;
dee91e82 24369 per_cu->cu = NULL;
ae038cb0
DJ
24370 *last_chain = next_cu;
24371 break;
24372 }
24373 else
24374 last_chain = &per_cu->cu->read_in_chain;
24375
24376 per_cu = next_cu;
24377 }
24378}
24379
dee91e82
DE
24380/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24381 We store these in a hash table separate from the DIEs, and preserve them
24382 when the DIEs are flushed out of cache.
24383
24384 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24385 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24386 or the type may come from a DWO file. Furthermore, while it's more logical
24387 to use per_cu->section+offset, with Fission the section with the data is in
24388 the DWO file but we don't know that section at the point we need it.
24389 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24390 because we can enter the lookup routine, get_die_type_at_offset, from
24391 outside this file, and thus won't necessarily have PER_CU->cu.
24392 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24393
dee91e82 24394struct dwarf2_per_cu_offset_and_type
1c379e20 24395{
dee91e82 24396 const struct dwarf2_per_cu_data *per_cu;
9c541725 24397 sect_offset sect_off;
1c379e20
DJ
24398 struct type *type;
24399};
24400
dee91e82 24401/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24402
24403static hashval_t
dee91e82 24404per_cu_offset_and_type_hash (const void *item)
1c379e20 24405{
9a3c8263
SM
24406 const struct dwarf2_per_cu_offset_and_type *ofs
24407 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24408
9c541725 24409 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24410}
24411
dee91e82 24412/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24413
24414static int
dee91e82 24415per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24416{
9a3c8263
SM
24417 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24418 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24419 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24420 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24421
dee91e82 24422 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24423 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24424}
24425
24426/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24427 table if necessary. For convenience, return TYPE.
24428
24429 The DIEs reading must have careful ordering to:
85102364 24430 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24431 reading current DIE.
24432 * Not trying to dereference contents of still incompletely read in types
24433 while reading in other DIEs.
24434 * Enable referencing still incompletely read in types just by a pointer to
24435 the type without accessing its fields.
24436
24437 Therefore caller should follow these rules:
24438 * Try to fetch any prerequisite types we may need to build this DIE type
24439 before building the type and calling set_die_type.
e71ec853 24440 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24441 possible before fetching more types to complete the current type.
24442 * Make the type as complete as possible before fetching more types. */
1c379e20 24443
f792889a 24444static struct type *
1c379e20
DJ
24445set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24446{
518817b3
SM
24447 struct dwarf2_per_objfile *dwarf2_per_objfile
24448 = cu->per_cu->dwarf2_per_objfile;
dee91e82 24449 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 24450 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
24451 struct attribute *attr;
24452 struct dynamic_prop prop;
1c379e20 24453
b4ba55a1
JB
24454 /* For Ada types, make sure that the gnat-specific data is always
24455 initialized (if not already set). There are a few types where
24456 we should not be doing so, because the type-specific area is
24457 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24458 where the type-specific area is used to store the floatformat).
24459 But this is not a problem, because the gnat-specific information
24460 is actually not needed for these types. */
24461 if (need_gnat_info (cu)
24462 && TYPE_CODE (type) != TYPE_CODE_FUNC
24463 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
24464 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24465 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24466 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
24467 && !HAVE_GNAT_AUX_INFO (type))
24468 INIT_GNAT_SPECIFIC (type);
24469
3f2f83dd
KB
24470 /* Read DW_AT_allocated and set in type. */
24471 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 24472 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24473 {
09ba997f 24474 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24475 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24476 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
24477 }
24478 else if (attr != NULL)
24479 {
b98664d3 24480 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 24481 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24482 sect_offset_str (die->sect_off));
3f2f83dd
KB
24483 }
24484
24485 /* Read DW_AT_associated and set in type. */
24486 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 24487 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24488 {
09ba997f 24489 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24490 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24491 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
24492 }
24493 else if (attr != NULL)
24494 {
b98664d3 24495 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 24496 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24497 sect_offset_str (die->sect_off));
3f2f83dd
KB
24498 }
24499
3cdcd0ce
JB
24500 /* Read DW_AT_data_location and set in type. */
24501 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 24502 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 24503 cu->per_cu->addr_type ()))
50a82047 24504 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 24505
dee91e82 24506 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
24507 dwarf2_per_objfile->die_type_hash
24508 = htab_up (htab_create_alloc (127,
24509 per_cu_offset_and_type_hash,
24510 per_cu_offset_and_type_eq,
24511 NULL, xcalloc, xfree));
1c379e20 24512
dee91e82 24513 ofs.per_cu = cu->per_cu;
9c541725 24514 ofs.sect_off = die->sect_off;
1c379e20 24515 ofs.type = type;
dee91e82 24516 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 24517 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24518 if (*slot)
b98664d3 24519 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24520 sect_offset_str (die->sect_off));
8d749320
SM
24521 *slot = XOBNEW (&objfile->objfile_obstack,
24522 struct dwarf2_per_cu_offset_and_type);
1c379e20 24523 **slot = ofs;
f792889a 24524 return type;
1c379e20
DJ
24525}
24526
9c541725 24527/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24528 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24529
24530static struct type *
9c541725 24531get_die_type_at_offset (sect_offset sect_off,
673bfd45 24532 struct dwarf2_per_cu_data *per_cu)
1c379e20 24533{
dee91e82 24534 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 24535 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 24536
dee91e82 24537 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24538 return NULL;
1c379e20 24539
dee91e82 24540 ofs.per_cu = per_cu;
9c541725 24541 ofs.sect_off = sect_off;
9a3c8263 24542 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 24543 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24544 if (slot)
24545 return slot->type;
24546 else
24547 return NULL;
24548}
24549
02142a6c 24550/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24551 or return NULL if DIE does not have a saved type. */
24552
24553static struct type *
24554get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24555{
9c541725 24556 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24557}
24558
10b3939b
DJ
24559/* Add a dependence relationship from CU to REF_PER_CU. */
24560
24561static void
24562dwarf2_add_dependence (struct dwarf2_cu *cu,
24563 struct dwarf2_per_cu_data *ref_per_cu)
24564{
24565 void **slot;
24566
24567 if (cu->dependencies == NULL)
24568 cu->dependencies
24569 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24570 NULL, &cu->comp_unit_obstack,
24571 hashtab_obstack_allocate,
24572 dummy_obstack_deallocate);
24573
24574 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24575 if (*slot == NULL)
24576 *slot = ref_per_cu;
24577}
1c379e20 24578
f504f079
DE
24579/* Subroutine of dwarf2_mark to pass to htab_traverse.
24580 Set the mark field in every compilation unit in the
ae038cb0
DJ
24581 cache that we must keep because we are keeping CU. */
24582
10b3939b
DJ
24583static int
24584dwarf2_mark_helper (void **slot, void *data)
24585{
24586 struct dwarf2_per_cu_data *per_cu;
24587
24588 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24589
24590 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24591 reading of the chain. As such dependencies remain valid it is not much
24592 useful to track and undo them during QUIT cleanups. */
24593 if (per_cu->cu == NULL)
24594 return 1;
24595
10b3939b
DJ
24596 if (per_cu->cu->mark)
24597 return 1;
9068261f 24598 per_cu->cu->mark = true;
10b3939b
DJ
24599
24600 if (per_cu->cu->dependencies != NULL)
24601 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24602
24603 return 1;
24604}
24605
f504f079
DE
24606/* Set the mark field in CU and in every other compilation unit in the
24607 cache that we must keep because we are keeping CU. */
24608
ae038cb0
DJ
24609static void
24610dwarf2_mark (struct dwarf2_cu *cu)
24611{
24612 if (cu->mark)
24613 return;
9068261f 24614 cu->mark = true;
10b3939b
DJ
24615 if (cu->dependencies != NULL)
24616 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24617}
24618
24619static void
24620dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24621{
24622 while (per_cu)
24623 {
9068261f 24624 per_cu->cu->mark = false;
ae038cb0
DJ
24625 per_cu = per_cu->cu->read_in_chain;
24626 }
72bf9492
DJ
24627}
24628
72bf9492
DJ
24629/* Trivial hash function for partial_die_info: the hash value of a DIE
24630 is its offset in .debug_info for this objfile. */
24631
24632static hashval_t
24633partial_die_hash (const void *item)
24634{
9a3c8263
SM
24635 const struct partial_die_info *part_die
24636 = (const struct partial_die_info *) item;
9a619af0 24637
9c541725 24638 return to_underlying (part_die->sect_off);
72bf9492
DJ
24639}
24640
24641/* Trivial comparison function for partial_die_info structures: two DIEs
24642 are equal if they have the same offset. */
24643
24644static int
24645partial_die_eq (const void *item_lhs, const void *item_rhs)
24646{
9a3c8263
SM
24647 const struct partial_die_info *part_die_lhs
24648 = (const struct partial_die_info *) item_lhs;
24649 const struct partial_die_info *part_die_rhs
24650 = (const struct partial_die_info *) item_rhs;
9a619af0 24651
9c541725 24652 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24653}
24654
3c3bb058
AB
24655struct cmd_list_element *set_dwarf_cmdlist;
24656struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24657
24658static void
981a3fb3 24659set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24660{
b4f54984 24661 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24662 gdb_stdout);
ae038cb0
DJ
24663}
24664
24665static void
981a3fb3 24666show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24667{
b4f54984 24668 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24669}
24670
9291a0cd 24671static void
cd4fb1b2
SM
24672show_check_physname (struct ui_file *file, int from_tty,
24673 struct cmd_list_element *c, const char *value)
9291a0cd 24674{
cd4fb1b2
SM
24675 fprintf_filtered (file,
24676 _("Whether to check \"physname\" is %s.\n"),
24677 value);
9291a0cd
TT
24678}
24679
6c265988 24680void _initialize_dwarf2_read ();
cd4fb1b2 24681void
6c265988 24682_initialize_dwarf2_read ()
9291a0cd 24683{
cd4fb1b2
SM
24684 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24685Set DWARF specific variables.\n\
590042fc 24686Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
24687 &set_dwarf_cmdlist, "maintenance set dwarf ",
24688 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24689
cd4fb1b2 24690 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
24691Show DWARF specific variables.\n\
24692Show DWARF variables such as the cache size."),
cd4fb1b2
SM
24693 &show_dwarf_cmdlist, "maintenance show dwarf ",
24694 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24695
cd4fb1b2
SM
24696 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24697 &dwarf_max_cache_age, _("\
24698Set the upper bound on the age of cached DWARF compilation units."), _("\
24699Show the upper bound on the age of cached DWARF compilation units."), _("\
24700A higher limit means that cached compilation units will be stored\n\
24701in memory longer, and more total memory will be used. Zero disables\n\
24702caching, which can slow down startup."),
24703 NULL,
24704 show_dwarf_max_cache_age,
24705 &set_dwarf_cmdlist,
24706 &show_dwarf_cmdlist);
156942c7 24707
cd4fb1b2
SM
24708 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24709Set debugging of the DWARF reader."), _("\
24710Show debugging of the DWARF reader."), _("\
24711When enabled (non-zero), debugging messages are printed during DWARF\n\
24712reading and symtab expansion. A value of 1 (one) provides basic\n\
24713information. A value greater than 1 provides more verbose information."),
24714 NULL,
24715 NULL,
24716 &setdebuglist, &showdebuglist);
9291a0cd 24717
cd4fb1b2
SM
24718 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24719Set debugging of the DWARF DIE reader."), _("\
24720Show debugging of the DWARF DIE reader."), _("\
24721When enabled (non-zero), DIEs are dumped after they are read in.\n\
24722The value is the maximum depth to print."),
24723 NULL,
24724 NULL,
24725 &setdebuglist, &showdebuglist);
9291a0cd 24726
cd4fb1b2
SM
24727 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24728Set debugging of the dwarf line reader."), _("\
24729Show debugging of the dwarf line reader."), _("\
24730When enabled (non-zero), line number entries are dumped as they are read in.\n\
24731A value of 1 (one) provides basic information.\n\
24732A value greater than 1 provides more verbose information."),
24733 NULL,
24734 NULL,
24735 &setdebuglist, &showdebuglist);
437afbb8 24736
cd4fb1b2
SM
24737 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24738Set cross-checking of \"physname\" code against demangler."), _("\
24739Show cross-checking of \"physname\" code against demangler."), _("\
24740When enabled, GDB's internal \"physname\" code is checked against\n\
24741the demangler."),
24742 NULL, show_check_physname,
24743 &setdebuglist, &showdebuglist);
900e11f9 24744
e615022a
DE
24745 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24746 no_class, &use_deprecated_index_sections, _("\
24747Set whether to use deprecated gdb_index sections."), _("\
24748Show whether to use deprecated gdb_index sections."), _("\
24749When enabled, deprecated .gdb_index sections are used anyway.\n\
24750Normally they are ignored either because of a missing feature or\n\
24751performance issue.\n\
24752Warning: This option must be enabled before gdb reads the file."),
24753 NULL,
24754 NULL,
24755 &setlist, &showlist);
24756
f1e6e072
TT
24757 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24758 &dwarf2_locexpr_funcs);
24759 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24760 &dwarf2_loclist_funcs);
24761
24762 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24763 &dwarf2_block_frame_base_locexpr_funcs);
24764 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24765 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24766
24767#if GDB_SELF_TEST
24768 selftests::register_test ("dw2_expand_symtabs_matching",
24769 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24770 selftests::register_test ("dwarf2_find_containing_comp_unit",
24771 selftests::find_containing_comp_unit::run_test);
c62446b1 24772#endif
6502dd73 24773}
This page took 5.247225 seconds and 4 git commands to generate.