Remove field_info::nfields member
[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
85102364 1132 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1133 int non_public_fields = 0;
c906108c 1134
c5aa993b
JM
1135 /* Member function fieldlist array, contains name of possibly overloaded
1136 member function, number of overloaded member functions and a pointer
1137 to the head of the member function field chain. */
be2daae6 1138 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1139
1140 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1141 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1142 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1143
1144 /* Nested types defined by this class and the number of elements in this
1145 list. */
be2daae6 1146 std::vector<struct decl_field> nested_types_list;
317f7127
TT
1147
1148 /* Return the total number of fields (including baseclasses). */
1149 int nfields () const
1150 {
1151 return fields.size () + baseclasses.size ();
1152 }
c5aa993b 1153 };
c906108c 1154
ae038cb0
DJ
1155/* Loaded secondary compilation units are kept in memory until they
1156 have not been referenced for the processing of this many
1157 compilation units. Set this to zero to disable caching. Cache
1158 sizes of up to at least twenty will improve startup time for
1159 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1160static int dwarf_max_cache_age = 5;
920d2a44 1161static void
b4f54984
DE
1162show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1163 struct cmd_list_element *c, const char *value)
920d2a44 1164{
3e43a32a 1165 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1166 "DWARF compilation units is %s.\n"),
920d2a44
AC
1167 value);
1168}
4390d890 1169\f
c906108c
SS
1170/* local function prototypes */
1171
918dd910
JK
1172static void dwarf2_find_base_address (struct die_info *die,
1173 struct dwarf2_cu *cu);
1174
891813be 1175static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1176 (struct dwarf2_per_cu_data *per_cu, const char *name);
1177
f1902523
JK
1178static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1179 const gdb_byte *info_ptr,
3e225074 1180 struct die_info *type_unit_die);
f1902523 1181
ed2dc618
SM
1182static void dwarf2_build_psymtabs_hard
1183 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1184
72bf9492
DJ
1185static void scan_partial_symbols (struct partial_die_info *,
1186 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1187 int, struct dwarf2_cu *);
c906108c 1188
72bf9492
DJ
1189static void add_partial_symbol (struct partial_die_info *,
1190 struct dwarf2_cu *);
63d06c5c 1191
72bf9492
DJ
1192static void add_partial_namespace (struct partial_die_info *pdi,
1193 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1194 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1195
5d7cb8df 1196static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1197 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1198 struct dwarf2_cu *cu);
1199
72bf9492
DJ
1200static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1201 struct dwarf2_cu *cu);
91c24f0a 1202
bc30ff58
JB
1203static void add_partial_subprogram (struct partial_die_info *pdi,
1204 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1205 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1206
d521ce57 1207static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1208
dee91e82 1209static struct partial_die_info *load_partial_dies
d521ce57 1210 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1211
fb816e8b
TV
1212/* A pair of partial_die_info and compilation unit. */
1213struct cu_partial_die_info
1214{
1215 /* The compilation unit of the partial_die_info. */
1216 struct dwarf2_cu *cu;
1217 /* A partial_die_info. */
1218 struct partial_die_info *pdi;
122cf0f2
AB
1219
1220 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1221 : cu (cu),
1222 pdi (pdi)
405feb71 1223 { /* Nothing. */ }
122cf0f2
AB
1224
1225private:
1226 cu_partial_die_info () = delete;
fb816e8b
TV
1227};
1228
122cf0f2
AB
1229static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1230 struct dwarf2_cu *);
72bf9492 1231
d521ce57
TT
1232static const gdb_byte *read_attribute (const struct die_reader_specs *,
1233 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1234 const gdb_byte *, bool *need_reprocess);
1235
1236static void read_attribute_reprocess (const struct die_reader_specs *reader,
1237 struct attribute *attr);
1238
1239static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1240
c764a876 1241static LONGEST read_checked_initial_length_and_offset
d521ce57 1242 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1243 unsigned int *, unsigned int *);
613e1657 1244
ed2dc618
SM
1245static sect_offset read_abbrev_offset
1246 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1247 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1248
ed2dc618
SM
1249static const char *read_indirect_string
1250 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1251 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1252
ed2dc618
SM
1253static const char *read_indirect_line_string
1254 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1255 const struct comp_unit_head *, unsigned int *);
36586728 1256
ed2dc618
SM
1257static const char *read_indirect_string_at_offset
1258 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1259 LONGEST str_offset);
927aa2e7 1260
ed2dc618
SM
1261static const char *read_indirect_string_from_dwz
1262 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1263
d521ce57
TT
1264static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1265 const gdb_byte *,
3019eac3
DE
1266 unsigned int *);
1267
18a8505e
AT
1268static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1269 ULONGEST str_index);
1270
1271static const char *read_stub_str_index (struct dwarf2_cu *cu,
1272 ULONGEST str_index);
3019eac3 1273
e142c38c 1274static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1275
e142c38c
DJ
1276static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1277 struct dwarf2_cu *);
c906108c 1278
348e048f 1279static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1280 unsigned int);
348e048f 1281
7d45c7c3
KB
1282static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1283 struct dwarf2_cu *cu);
1284
a084a2a6
AT
1285static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1286
05cf31d1
JB
1287static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1288 struct dwarf2_cu *cu);
1289
e142c38c 1290static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1291
e142c38c 1292static struct die_info *die_specification (struct die_info *die,
f2f0e013 1293 struct dwarf2_cu **);
63d06c5c 1294
9c541725 1295static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1296 struct dwarf2_cu *cu);
debd256d 1297
f3f5162e 1298static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1299 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1300 CORE_ADDR, int decode_mapping);
c906108c 1301
804d2729
TT
1302static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1303 const char *);
c906108c 1304
a14ed312 1305static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1306 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1307
ff39bb5e 1308static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1309 struct dwarf2_cu *);
c906108c 1310
ff39bb5e 1311static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1312 struct type *type,
1313 const char *name,
1314 struct obstack *obstack,
12df843f 1315 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1316 const gdb_byte **bytes,
98bfdba5 1317 struct dwarf2_locexpr_baton **baton);
2df3850c 1318
e7c27a73 1319static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1320
b4ba55a1
JB
1321static int need_gnat_info (struct dwarf2_cu *);
1322
3e43a32a
MS
1323static struct type *die_descriptive_type (struct die_info *,
1324 struct dwarf2_cu *);
b4ba55a1
JB
1325
1326static void set_descriptive_type (struct type *, struct die_info *,
1327 struct dwarf2_cu *);
1328
e7c27a73
DJ
1329static struct type *die_containing_type (struct die_info *,
1330 struct dwarf2_cu *);
c906108c 1331
ff39bb5e 1332static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1333 struct dwarf2_cu *);
c906108c 1334
f792889a 1335static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1336
673bfd45
DE
1337static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1338
0d5cff50 1339static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1340
6e70227d 1341static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1342 const char *suffix, int physname,
1343 struct dwarf2_cu *cu);
63d06c5c 1344
e7c27a73 1345static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1346
348e048f
DE
1347static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1348
e7c27a73 1349static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1350
e7c27a73 1351static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1352
96408a79
SA
1353static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1354
71a3c369
TT
1355static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1356
ff013f42 1357static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1358 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1359
3a2b436a 1360/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1361 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1362enum pc_bounds_kind
1363{
e385593e 1364 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1365 PC_BOUNDS_NOT_PRESENT,
1366
e385593e
JK
1367 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1368 were present but they do not form a valid range of PC addresses. */
1369 PC_BOUNDS_INVALID,
1370
3a2b436a
JK
1371 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1372 PC_BOUNDS_RANGES,
1373
1374 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1375 PC_BOUNDS_HIGH_LOW,
1376};
1377
1378static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1379 CORE_ADDR *, CORE_ADDR *,
1380 struct dwarf2_cu *,
891813be 1381 dwarf2_psymtab *);
c906108c 1382
fae299cd
DC
1383static void get_scope_pc_bounds (struct die_info *,
1384 CORE_ADDR *, CORE_ADDR *,
1385 struct dwarf2_cu *);
1386
801e3a5b
JB
1387static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1388 CORE_ADDR, struct dwarf2_cu *);
1389
a14ed312 1390static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1391 struct dwarf2_cu *);
c906108c 1392
a14ed312 1393static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1394 struct type *, struct dwarf2_cu *);
c906108c 1395
a14ed312 1396static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1397 struct die_info *, struct type *,
e7c27a73 1398 struct dwarf2_cu *);
c906108c 1399
a14ed312 1400static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1401 struct type *,
1402 struct dwarf2_cu *);
c906108c 1403
134d01f1 1404static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1405
e7c27a73 1406static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1407
e7c27a73 1408static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1409
5d7cb8df
JK
1410static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1411
804d2729 1412static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1413
27aa8d6a
SW
1414static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1415
74921315
KS
1416static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1417
f55ee35c
JK
1418static struct type *read_module_type (struct die_info *die,
1419 struct dwarf2_cu *cu);
1420
38d518c9 1421static const char *namespace_name (struct die_info *die,
e142c38c 1422 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1423
134d01f1 1424static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1425
e7c27a73 1426static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1427
6e70227d 1428static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1429 struct dwarf2_cu *);
1430
bf6af496 1431static struct die_info *read_die_and_siblings_1
d521ce57 1432 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1433 struct die_info *);
639d11d3 1434
dee91e82 1435static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1436 const gdb_byte *info_ptr,
1437 const gdb_byte **new_info_ptr,
639d11d3
DC
1438 struct die_info *parent);
1439
d521ce57
TT
1440static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1441 struct die_info **, const gdb_byte *,
3e225074 1442 int);
3019eac3 1443
d521ce57 1444static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1445 struct die_info **, const gdb_byte *);
93311388 1446
e7c27a73 1447static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1448
15d034d0
TT
1449static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1450 struct obstack *);
71c25dea 1451
15d034d0 1452static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1453
15d034d0 1454static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1455 struct die_info *die,
1456 struct dwarf2_cu *cu);
1457
ca69b9e6
DE
1458static const char *dwarf2_physname (const char *name, struct die_info *die,
1459 struct dwarf2_cu *cu);
1460
e142c38c 1461static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1462 struct dwarf2_cu **);
9219021c 1463
f39c6ffd 1464static const char *dwarf_tag_name (unsigned int);
c906108c 1465
f39c6ffd 1466static const char *dwarf_attr_name (unsigned int);
c906108c 1467
f39c6ffd 1468static const char *dwarf_form_name (unsigned int);
c906108c 1469
a121b7c1 1470static const char *dwarf_bool_name (unsigned int);
c906108c 1471
f39c6ffd 1472static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1473
f9aca02d 1474static struct die_info *sibling_die (struct die_info *);
c906108c 1475
d97bc12b
DE
1476static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1477
1478static void dump_die_for_error (struct die_info *);
1479
1480static void dump_die_1 (struct ui_file *, int level, int max_level,
1481 struct die_info *);
c906108c 1482
d97bc12b 1483/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1484
51545339 1485static void store_in_ref_table (struct die_info *,
10b3939b 1486 struct dwarf2_cu *);
c906108c 1487
ff39bb5e 1488static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1489
ff39bb5e 1490static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1491
348e048f 1492static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1493 const struct attribute *,
348e048f
DE
1494 struct dwarf2_cu **);
1495
10b3939b 1496static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1497 const struct attribute *,
f2f0e013 1498 struct dwarf2_cu **);
c906108c 1499
348e048f 1500static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1501 const struct attribute *,
348e048f
DE
1502 struct dwarf2_cu **);
1503
ac9ec31b
DE
1504static struct type *get_signatured_type (struct die_info *, ULONGEST,
1505 struct dwarf2_cu *);
1506
1507static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1508 const struct attribute *,
ac9ec31b
DE
1509 struct dwarf2_cu *);
1510
e5fe5e75 1511static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1512
52dc124a 1513static void read_signatured_type (struct signatured_type *);
348e048f 1514
63e43d3a
PMR
1515static int attr_to_dynamic_prop (const struct attribute *attr,
1516 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1517 struct dynamic_prop *prop, struct type *type);
63e43d3a 1518
c906108c
SS
1519/* memory allocation interface */
1520
7b5a2f43 1521static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1522
b60c80d6 1523static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1524
43f3e411 1525static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1526
8cf6f0b1
TT
1527static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1528 struct dwarf2_loclist_baton *baton,
ff39bb5e 1529 const struct attribute *attr);
8cf6f0b1 1530
ff39bb5e 1531static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1532 struct symbol *sym,
f1e6e072
TT
1533 struct dwarf2_cu *cu,
1534 int is_block);
4c2df51b 1535
d521ce57
TT
1536static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1537 const gdb_byte *info_ptr,
1538 struct abbrev_info *abbrev);
4bb7a0a7 1539
72bf9492
DJ
1540static hashval_t partial_die_hash (const void *item);
1541
1542static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1543
ae038cb0 1544static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1545 (sect_offset sect_off, unsigned int offset_in_dwz,
1546 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1547
9816fde3 1548static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1549 struct die_info *comp_unit_die,
1550 enum language pretend_language);
93311388 1551
ed2dc618 1552static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1553
dee91e82 1554static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1555
f792889a
DJ
1556static struct type *set_die_type (struct die_info *, struct type *,
1557 struct dwarf2_cu *);
1c379e20 1558
ed2dc618 1559static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1560
ed2dc618 1561static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1562
58f0c718 1563static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1564 enum language);
10b3939b 1565
95554aad
TT
1566static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1567 enum language);
10b3939b 1568
f4dc4d17
DE
1569static void process_full_type_unit (struct dwarf2_per_cu_data *,
1570 enum language);
1571
10b3939b
DJ
1572static void dwarf2_add_dependence (struct dwarf2_cu *,
1573 struct dwarf2_per_cu_data *);
1574
ae038cb0
DJ
1575static void dwarf2_mark (struct dwarf2_cu *);
1576
1577static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1578
b64f50a1 1579static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1580 struct dwarf2_per_cu_data *);
673bfd45 1581
f792889a 1582static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1583
95554aad
TT
1584static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1585 enum language pretend_language);
1586
ed2dc618 1587static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1588
b303c6f6
AB
1589/* Class, the destructor of which frees all allocated queue entries. This
1590 will only have work to do if an error was thrown while processing the
1591 dwarf. If no error was thrown then the queue entries should have all
1592 been processed, and freed, as we went along. */
1593
1594class dwarf2_queue_guard
1595{
1596public:
39856def
TT
1597 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1598 : m_per_objfile (per_objfile)
1599 {
1600 }
b303c6f6
AB
1601
1602 /* Free any entries remaining on the queue. There should only be
1603 entries left if we hit an error while processing the dwarf. */
1604 ~dwarf2_queue_guard ()
1605 {
39856def
TT
1606 /* Ensure that no memory is allocated by the queue. */
1607 std::queue<dwarf2_queue_item> empty;
1608 std::swap (m_per_objfile->queue, empty);
1609 }
b303c6f6 1610
39856def 1611 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1612
39856def
TT
1613private:
1614 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1615};
1616
39856def
TT
1617dwarf2_queue_item::~dwarf2_queue_item ()
1618{
1619 /* Anything still marked queued is likely to be in an
1620 inconsistent state, so discard it. */
1621 if (per_cu->queued)
1622 {
1623 if (per_cu->cu != NULL)
1624 free_one_cached_comp_unit (per_cu);
1625 per_cu->queued = 0;
1626 }
1627}
1628
d721ba37
PA
1629/* The return type of find_file_and_directory. Note, the enclosed
1630 string pointers are only valid while this object is valid. */
1631
1632struct file_and_directory
1633{
1634 /* The filename. This is never NULL. */
1635 const char *name;
1636
1637 /* The compilation directory. NULL if not known. If we needed to
1638 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1639 points directly to the DW_AT_comp_dir string attribute owned by
1640 the obstack that owns the DIE. */
1641 const char *comp_dir;
1642
1643 /* If we needed to build a new string for comp_dir, this is what
1644 owns the storage. */
1645 std::string comp_dir_storage;
1646};
1647
1648static file_and_directory find_file_and_directory (struct die_info *die,
1649 struct dwarf2_cu *cu);
9291a0cd 1650
298e9637 1651static htab_up allocate_signatured_type_table ();
1fd400ff 1652
298e9637 1653static htab_up allocate_dwo_unit_table ();
3019eac3 1654
57d63ce2 1655static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1656 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1657 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1658 ULONGEST signature, int is_debug_types);
a2ce51a0 1659
ed2dc618
SM
1660static struct dwp_file *get_dwp_file
1661 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1662
3019eac3 1663static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1664 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1665
1666static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1667 (struct signatured_type *, const char *, const char *);
3019eac3 1668
89e63ee4
DE
1669static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1670
263db9a1
TT
1671/* A unique pointer to a dwo_file. */
1672
51ac9db5 1673typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1674
ed2dc618 1675static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1676
1b80a9fa 1677static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1678
1679static void free_line_header_voidp (void *arg);
4390d890
DE
1680\f
1681/* Various complaints about symbol reading that don't abort the process. */
1682
1683static void
1684dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1685{
b98664d3 1686 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1687}
1688
1689static void
1690dwarf2_debug_line_missing_file_complaint (void)
1691{
b98664d3 1692 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1693}
1694
1695static void
1696dwarf2_debug_line_missing_end_sequence_complaint (void)
1697{
b98664d3 1698 complaint (_(".debug_line section has line "
4390d890
DE
1699 "program sequence without an end"));
1700}
1701
1702static void
1703dwarf2_complex_location_expr_complaint (void)
1704{
b98664d3 1705 complaint (_("location expression too complex"));
4390d890
DE
1706}
1707
1708static void
1709dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1710 int arg3)
1711{
b98664d3 1712 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1713 arg1, arg2, arg3);
1714}
1715
1716static void
1717dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1718{
b98664d3 1719 complaint (_("debug info runs off end of %s section"
4390d890 1720 " [in module %s]"),
96b79293
TT
1721 section->get_name (),
1722 section->get_file_name ());
4390d890 1723}
1b80a9fa 1724
4390d890
DE
1725static void
1726dwarf2_macro_malformed_definition_complaint (const char *arg1)
1727{
b98664d3 1728 complaint (_("macro debug info contains a "
4390d890
DE
1729 "malformed macro definition:\n`%s'"),
1730 arg1);
1731}
1732
1733static void
1734dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1735{
b98664d3 1736 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1737 arg1, arg2);
1738}
527f3840
JK
1739
1740/* Hash function for line_header_hash. */
1741
1742static hashval_t
1743line_header_hash (const struct line_header *ofs)
1744{
9c541725 1745 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1746}
1747
1748/* Hash function for htab_create_alloc_ex for line_header_hash. */
1749
1750static hashval_t
1751line_header_hash_voidp (const void *item)
1752{
9a3c8263 1753 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1754
1755 return line_header_hash (ofs);
1756}
1757
1758/* Equality function for line_header_hash. */
1759
1760static int
1761line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1762{
9a3c8263
SM
1763 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1764 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1765
9c541725 1766 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1767 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1768}
1769
4390d890 1770\f
9291a0cd 1771
330cdd98
PA
1772/* See declaration. */
1773
1774dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1775 const dwarf2_debug_sections *names,
1776 bool can_copy_)
1777 : objfile (objfile_),
1778 can_copy (can_copy_)
330cdd98
PA
1779{
1780 if (names == NULL)
1781 names = &dwarf2_elf_names;
1782
1783 bfd *obfd = objfile->obfd;
1784
1785 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1786 locate_sections (obfd, sec, *names);
1787}
1788
1789dwarf2_per_objfile::~dwarf2_per_objfile ()
1790{
1791 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1792 free_cached_comp_units ();
1793
b76e467d 1794 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1795 per_cu->imported_symtabs_free ();
fc8e7e75 1796
b2bdb8cf 1797 for (signatured_type *sig_type : all_type_units)
ae640021 1798 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1799
330cdd98
PA
1800 /* Everything else should be on the objfile obstack. */
1801}
1802
1803/* See declaration. */
1804
1805void
1806dwarf2_per_objfile::free_cached_comp_units ()
1807{
1808 dwarf2_per_cu_data *per_cu = read_in_chain;
1809 dwarf2_per_cu_data **last_chain = &read_in_chain;
1810 while (per_cu != NULL)
1811 {
1812 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1813
fcd3b13d 1814 delete per_cu->cu;
330cdd98
PA
1815 *last_chain = next_cu;
1816 per_cu = next_cu;
1817 }
1818}
1819
11ed8cad
TT
1820/* A helper class that calls free_cached_comp_units on
1821 destruction. */
1822
1823class free_cached_comp_units
1824{
1825public:
1826
1827 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1828 : m_per_objfile (per_objfile)
1829 {
1830 }
1831
1832 ~free_cached_comp_units ()
1833 {
1834 m_per_objfile->free_cached_comp_units ();
1835 }
1836
1837 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1838
1839private:
1840
1841 dwarf2_per_objfile *m_per_objfile;
1842};
1843
c906108c 1844/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1845 information and return true if we have enough to do something.
1846 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1847 ELF names are used. CAN_COPY is true for formats where symbol
1848 interposition is possible and so symbol values must follow copy
1849 relocation rules. */
c906108c
SS
1850
1851int
251d32d9 1852dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1853 const struct dwarf2_debug_sections *names,
1854 bool can_copy)
c906108c 1855{
97cbe998
SDJ
1856 if (objfile->flags & OBJF_READNEVER)
1857 return 0;
1858
ed2dc618
SM
1859 struct dwarf2_per_objfile *dwarf2_per_objfile
1860 = get_dwarf2_per_objfile (objfile);
1861
1862 if (dwarf2_per_objfile == NULL)
5bfd760d 1863 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1864 names,
1865 can_copy);
5bfd760d 1866
73869dc2 1867 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1868 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1869 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1870 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1871}
1872
251d32d9
TG
1873/* When loading sections, we look either for uncompressed section or for
1874 compressed section names. */
233a11ab
CS
1875
1876static int
251d32d9
TG
1877section_is_p (const char *section_name,
1878 const struct dwarf2_section_names *names)
233a11ab 1879{
251d32d9
TG
1880 if (names->normal != NULL
1881 && strcmp (section_name, names->normal) == 0)
1882 return 1;
1883 if (names->compressed != NULL
1884 && strcmp (section_name, names->compressed) == 0)
1885 return 1;
1886 return 0;
233a11ab
CS
1887}
1888
330cdd98 1889/* See declaration. */
c906108c 1890
330cdd98
PA
1891void
1892dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1893 const dwarf2_debug_sections &names)
c906108c 1894{
fd361982 1895 flagword aflag = bfd_section_flags (sectp);
251d32d9 1896
dc7650b8
JK
1897 if ((aflag & SEC_HAS_CONTENTS) == 0)
1898 {
1899 }
950b7495
KS
1900 else if (elf_section_data (sectp)->this_hdr.sh_size
1901 > bfd_get_file_size (abfd))
1902 {
1903 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1904 warning (_("Discarding section %s which has a section size (%s"
1905 ") larger than the file size [in module %s]"),
1906 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1907 bfd_get_filename (abfd));
1908 }
330cdd98 1909 else if (section_is_p (sectp->name, &names.info))
c906108c 1910 {
330cdd98 1911 this->info.s.section = sectp;
fd361982 1912 this->info.size = bfd_section_size (sectp);
c906108c 1913 }
330cdd98 1914 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1915 {
330cdd98 1916 this->abbrev.s.section = sectp;
fd361982 1917 this->abbrev.size = bfd_section_size (sectp);
c906108c 1918 }
330cdd98 1919 else if (section_is_p (sectp->name, &names.line))
c906108c 1920 {
330cdd98 1921 this->line.s.section = sectp;
fd361982 1922 this->line.size = bfd_section_size (sectp);
c906108c 1923 }
330cdd98 1924 else if (section_is_p (sectp->name, &names.loc))
c906108c 1925 {
330cdd98 1926 this->loc.s.section = sectp;
fd361982 1927 this->loc.size = bfd_section_size (sectp);
c906108c 1928 }
330cdd98 1929 else if (section_is_p (sectp->name, &names.loclists))
43988095 1930 {
330cdd98 1931 this->loclists.s.section = sectp;
fd361982 1932 this->loclists.size = bfd_section_size (sectp);
43988095 1933 }
330cdd98 1934 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1935 {
330cdd98 1936 this->macinfo.s.section = sectp;
fd361982 1937 this->macinfo.size = bfd_section_size (sectp);
c906108c 1938 }
330cdd98 1939 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1940 {
330cdd98 1941 this->macro.s.section = sectp;
fd361982 1942 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1943 }
330cdd98 1944 else if (section_is_p (sectp->name, &names.str))
c906108c 1945 {
330cdd98 1946 this->str.s.section = sectp;
fd361982 1947 this->str.size = bfd_section_size (sectp);
c906108c 1948 }
18a8505e
AT
1949 else if (section_is_p (sectp->name, &names.str_offsets))
1950 {
1951 this->str_offsets.s.section = sectp;
1952 this->str_offsets.size = bfd_section_size (sectp);
1953 }
330cdd98 1954 else if (section_is_p (sectp->name, &names.line_str))
43988095 1955 {
330cdd98 1956 this->line_str.s.section = sectp;
fd361982 1957 this->line_str.size = bfd_section_size (sectp);
43988095 1958 }
330cdd98 1959 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1960 {
330cdd98 1961 this->addr.s.section = sectp;
fd361982 1962 this->addr.size = bfd_section_size (sectp);
3019eac3 1963 }
330cdd98 1964 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1965 {
330cdd98 1966 this->frame.s.section = sectp;
fd361982 1967 this->frame.size = bfd_section_size (sectp);
b6af0555 1968 }
330cdd98 1969 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1970 {
330cdd98 1971 this->eh_frame.s.section = sectp;
fd361982 1972 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1973 }
330cdd98 1974 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1975 {
330cdd98 1976 this->ranges.s.section = sectp;
fd361982 1977 this->ranges.size = bfd_section_size (sectp);
af34e669 1978 }
330cdd98 1979 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1980 {
330cdd98 1981 this->rnglists.s.section = sectp;
fd361982 1982 this->rnglists.size = bfd_section_size (sectp);
43988095 1983 }
330cdd98 1984 else if (section_is_p (sectp->name, &names.types))
348e048f 1985 {
8b70b953
TT
1986 struct dwarf2_section_info type_section;
1987
1988 memset (&type_section, 0, sizeof (type_section));
049412e3 1989 type_section.s.section = sectp;
fd361982 1990 type_section.size = bfd_section_size (sectp);
8b70b953 1991
fd5866f6 1992 this->types.push_back (type_section);
348e048f 1993 }
330cdd98 1994 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1995 {
330cdd98 1996 this->gdb_index.s.section = sectp;
fd361982 1997 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1998 }
927aa2e7
JK
1999 else if (section_is_p (sectp->name, &names.debug_names))
2000 {
2001 this->debug_names.s.section = sectp;
fd361982 2002 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2003 }
2004 else if (section_is_p (sectp->name, &names.debug_aranges))
2005 {
2006 this->debug_aranges.s.section = sectp;
fd361982 2007 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2008 }
dce234bc 2009
fd361982
AM
2010 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2011 && bfd_section_vma (sectp) == 0)
330cdd98 2012 this->has_section_at_zero = true;
c906108c
SS
2013}
2014
dce234bc 2015/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2016 SECTION_NAME. */
af34e669 2017
dce234bc 2018void
3017a003
TG
2019dwarf2_get_section_info (struct objfile *objfile,
2020 enum dwarf2_section_enum sect,
d521ce57 2021 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2022 bfd_size_type *sizep)
2023{
5bfd760d 2024 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2025 struct dwarf2_section_info *info;
a3b2a86b
TT
2026
2027 /* We may see an objfile without any DWARF, in which case we just
2028 return nothing. */
2029 if (data == NULL)
2030 {
2031 *sectp = NULL;
2032 *bufp = NULL;
2033 *sizep = 0;
2034 return;
2035 }
3017a003
TG
2036 switch (sect)
2037 {
2038 case DWARF2_DEBUG_FRAME:
2039 info = &data->frame;
2040 break;
2041 case DWARF2_EH_FRAME:
2042 info = &data->eh_frame;
2043 break;
2044 default:
2045 gdb_assert_not_reached ("unexpected section");
2046 }
dce234bc 2047
96b79293 2048 info->read (objfile);
dce234bc 2049
96b79293 2050 *sectp = info->get_bfd_section ();
dce234bc
PP
2051 *bufp = info->buffer;
2052 *sizep = info->size;
2053}
2054
36586728
TT
2055/* A helper function to find the sections for a .dwz file. */
2056
2057static void
2058locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2059{
9a3c8263 2060 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2061
2062 /* Note that we only support the standard ELF names, because .dwz
2063 is ELF-only (at the time of writing). */
2064 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2065 {
049412e3 2066 dwz_file->abbrev.s.section = sectp;
fd361982 2067 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2068 }
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2070 {
049412e3 2071 dwz_file->info.s.section = sectp;
fd361982 2072 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2073 }
2074 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2075 {
049412e3 2076 dwz_file->str.s.section = sectp;
fd361982 2077 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2078 }
2079 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2080 {
049412e3 2081 dwz_file->line.s.section = sectp;
fd361982 2082 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2083 }
2084 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2085 {
049412e3 2086 dwz_file->macro.s.section = sectp;
fd361982 2087 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2088 }
2ec9a5e0
TT
2089 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2090 {
049412e3 2091 dwz_file->gdb_index.s.section = sectp;
fd361982 2092 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2093 }
927aa2e7
JK
2094 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2095 {
2096 dwz_file->debug_names.s.section = sectp;
fd361982 2097 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2098 }
36586728
TT
2099}
2100
c4973306 2101/* See dwarf2read.h. */
36586728 2102
c4973306 2103struct dwz_file *
ed2dc618 2104dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2105{
36586728 2106 const char *filename;
acd13123 2107 bfd_size_type buildid_len_arg;
dc294be5
TT
2108 size_t buildid_len;
2109 bfd_byte *buildid;
36586728
TT
2110
2111 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2112 return dwarf2_per_objfile->dwz_file.get ();
36586728 2113
4db1a1dc 2114 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2115 gdb::unique_xmalloc_ptr<char> data
2116 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2117 &buildid_len_arg, &buildid));
4db1a1dc
TT
2118 if (data == NULL)
2119 {
2120 if (bfd_get_error () == bfd_error_no_error)
2121 return NULL;
2122 error (_("could not read '.gnu_debugaltlink' section: %s"),
2123 bfd_errmsg (bfd_get_error ()));
2124 }
791afaa2
TT
2125
2126 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2127
acd13123
TT
2128 buildid_len = (size_t) buildid_len_arg;
2129
791afaa2 2130 filename = data.get ();
d721ba37
PA
2131
2132 std::string abs_storage;
36586728
TT
2133 if (!IS_ABSOLUTE_PATH (filename))
2134 {
14278e1f
TT
2135 gdb::unique_xmalloc_ptr<char> abs
2136 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2137
14278e1f 2138 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2139 filename = abs_storage.c_str ();
36586728
TT
2140 }
2141
dc294be5
TT
2142 /* First try the file name given in the section. If that doesn't
2143 work, try to use the build-id instead. */
192b62ce 2144 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2145 if (dwz_bfd != NULL)
36586728 2146 {
192b62ce 2147 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2148 dwz_bfd.reset (nullptr);
36586728
TT
2149 }
2150
dc294be5
TT
2151 if (dwz_bfd == NULL)
2152 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2153
0d79cdc4
AM
2154 if (dwz_bfd == nullptr)
2155 {
2156 gdb::unique_xmalloc_ptr<char> alt_filename;
2157 const char *origname = dwarf2_per_objfile->objfile->original_name;
2158
2159 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2160 buildid_len,
2161 origname,
2162 &alt_filename));
2163
2164 if (fd.get () >= 0)
2165 {
2166 /* File successfully retrieved from server. */
2167 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2168
2169 if (dwz_bfd == nullptr)
2170 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2171 alt_filename.get ());
2172 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2173 dwz_bfd.reset (nullptr);
2174 }
2175 }
2176
dc294be5
TT
2177 if (dwz_bfd == NULL)
2178 error (_("could not find '.gnu_debugaltlink' file for %s"),
2179 objfile_name (dwarf2_per_objfile->objfile));
2180
7ff8cb8c
TT
2181 std::unique_ptr<struct dwz_file> result
2182 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2183
7ff8cb8c
TT
2184 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2185 result.get ());
36586728 2186
7ff8cb8c
TT
2187 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2188 result->dwz_bfd.get ());
2189 dwarf2_per_objfile->dwz_file = std::move (result);
2190 return dwarf2_per_objfile->dwz_file.get ();
36586728 2191}
9291a0cd 2192\f
7b9f3c50
DE
2193/* DWARF quick_symbols_functions support. */
2194
2195/* TUs can share .debug_line entries, and there can be a lot more TUs than
2196 unique line tables, so we maintain a separate table of all .debug_line
2197 derived entries to support the sharing.
2198 All the quick functions need is the list of file names. We discard the
2199 line_header when we're done and don't need to record it here. */
2200struct quick_file_names
2201{
094b34ac
DE
2202 /* The data used to construct the hash key. */
2203 struct stmt_list_hash hash;
7b9f3c50
DE
2204
2205 /* The number of entries in file_names, real_names. */
2206 unsigned int num_file_names;
2207
2208 /* The file names from the line table, after being run through
2209 file_full_name. */
2210 const char **file_names;
2211
2212 /* The file names from the line table after being run through
2213 gdb_realpath. These are computed lazily. */
2214 const char **real_names;
2215};
2216
2217/* When using the index (and thus not using psymtabs), each CU has an
2218 object of this type. This is used to hold information needed by
2219 the various "quick" methods. */
2220struct dwarf2_per_cu_quick_data
2221{
2222 /* The file table. This can be NULL if there was no file table
2223 or it's currently not read in.
2224 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2225 struct quick_file_names *file_names;
2226
2227 /* The corresponding symbol table. This is NULL if symbols for this
2228 CU have not yet been read. */
43f3e411 2229 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2230
2231 /* A temporary mark bit used when iterating over all CUs in
2232 expand_symtabs_matching. */
2233 unsigned int mark : 1;
2234
2235 /* True if we've tried to read the file table and found there isn't one.
2236 There will be no point in trying to read it again next time. */
2237 unsigned int no_file_data : 1;
2238};
2239
094b34ac
DE
2240/* Utility hash function for a stmt_list_hash. */
2241
2242static hashval_t
2243hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2244{
2245 hashval_t v = 0;
2246
2247 if (stmt_list_hash->dwo_unit != NULL)
2248 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2249 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2250 return v;
2251}
2252
2253/* Utility equality function for a stmt_list_hash. */
2254
2255static int
2256eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2257 const struct stmt_list_hash *rhs)
2258{
2259 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2260 return 0;
2261 if (lhs->dwo_unit != NULL
2262 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2263 return 0;
2264
9c541725 2265 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2266}
2267
7b9f3c50
DE
2268/* Hash function for a quick_file_names. */
2269
2270static hashval_t
2271hash_file_name_entry (const void *e)
2272{
9a3c8263
SM
2273 const struct quick_file_names *file_data
2274 = (const struct quick_file_names *) e;
7b9f3c50 2275
094b34ac 2276 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2277}
2278
2279/* Equality function for a quick_file_names. */
2280
2281static int
2282eq_file_name_entry (const void *a, const void *b)
2283{
9a3c8263
SM
2284 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2285 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2286
094b34ac 2287 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2288}
2289
2290/* Delete function for a quick_file_names. */
2291
2292static void
2293delete_file_name_entry (void *e)
2294{
9a3c8263 2295 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2296 int i;
2297
2298 for (i = 0; i < file_data->num_file_names; ++i)
2299 {
2300 xfree ((void*) file_data->file_names[i]);
2301 if (file_data->real_names)
2302 xfree ((void*) file_data->real_names[i]);
2303 }
2304
2305 /* The space for the struct itself lives on objfile_obstack,
2306 so we don't free it here. */
2307}
2308
2309/* Create a quick_file_names hash table. */
2310
5895093f 2311static htab_up
7b9f3c50
DE
2312create_quick_file_names_table (unsigned int nr_initial_entries)
2313{
5895093f
TT
2314 return htab_up (htab_create_alloc (nr_initial_entries,
2315 hash_file_name_entry, eq_file_name_entry,
2316 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2317}
9291a0cd 2318
918dd910
JK
2319/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2320 have to be created afterwards. You should call age_cached_comp_units after
2321 processing PER_CU->CU. dw2_setup must have been already called. */
2322
2323static void
58f0c718 2324load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2325{
3019eac3 2326 if (per_cu->is_debug_types)
e5fe5e75 2327 load_full_type_unit (per_cu);
918dd910 2328 else
58f0c718 2329 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2330
cc12ce38
DE
2331 if (per_cu->cu == NULL)
2332 return; /* Dummy CU. */
2dc860c0
DE
2333
2334 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2335}
2336
a0f42c21 2337/* Read in the symbols for PER_CU. */
2fdf6df6 2338
9291a0cd 2339static void
58f0c718 2340dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2341{
ed2dc618 2342 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2343
f4dc4d17
DE
2344 /* Skip type_unit_groups, reading the type units they contain
2345 is handled elsewhere. */
197400e8 2346 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2347 return;
2348
b303c6f6
AB
2349 /* The destructor of dwarf2_queue_guard frees any entries left on
2350 the queue. After this point we're guaranteed to leave this function
2351 with the dwarf queue empty. */
39856def 2352 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2353
95554aad 2354 if (dwarf2_per_objfile->using_index
43f3e411 2355 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2356 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2357 {
2358 queue_comp_unit (per_cu, language_minimal);
58f0c718 2359 load_cu (per_cu, skip_partial);
89e63ee4
DE
2360
2361 /* If we just loaded a CU from a DWO, and we're working with an index
2362 that may badly handle TUs, load all the TUs in that DWO as well.
2363 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2364 if (!per_cu->is_debug_types
cc12ce38 2365 && per_cu->cu != NULL
89e63ee4
DE
2366 && per_cu->cu->dwo_unit != NULL
2367 && dwarf2_per_objfile->index_table != NULL
2368 && dwarf2_per_objfile->index_table->version <= 7
2369 /* DWP files aren't supported yet. */
ed2dc618 2370 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2371 queue_and_load_all_dwo_tus (per_cu);
95554aad 2372 }
9291a0cd 2373
ed2dc618 2374 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2375
2376 /* Age the cache, releasing compilation units that have not
2377 been used recently. */
ed2dc618 2378 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2379}
2380
2381/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2382 the objfile from which this CU came. Returns the resulting symbol
2383 table. */
2fdf6df6 2384
43f3e411 2385static struct compunit_symtab *
58f0c718 2386dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2387{
ed2dc618
SM
2388 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2389
95554aad 2390 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2391 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2392 {
11ed8cad 2393 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2394 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2395 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2396 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2397 }
f194fefb 2398
43f3e411 2399 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2400}
2401
ff4c9fec 2402/* See declaration. */
f4dc4d17 2403
ff4c9fec
SM
2404dwarf2_per_cu_data *
2405dwarf2_per_objfile::get_cutu (int index)
2406{
b76e467d 2407 if (index >= this->all_comp_units.size ())
ff4c9fec 2408 {
b76e467d 2409 index -= this->all_comp_units.size ();
b2bdb8cf 2410 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2411 return &this->all_type_units[index]->per_cu;
2412 }
f4dc4d17 2413
ff4c9fec
SM
2414 return this->all_comp_units[index];
2415}
f4dc4d17 2416
ff4c9fec 2417/* See declaration. */
2fdf6df6 2418
ff4c9fec
SM
2419dwarf2_per_cu_data *
2420dwarf2_per_objfile::get_cu (int index)
1fd400ff 2421{
b76e467d 2422 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2423
ff4c9fec 2424 return this->all_comp_units[index];
f4dc4d17
DE
2425}
2426
ff4c9fec 2427/* See declaration. */
f4dc4d17 2428
ff4c9fec
SM
2429signatured_type *
2430dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2431{
b2bdb8cf 2432 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2433
ff4c9fec 2434 return this->all_type_units[index];
1fd400ff
TT
2435}
2436
4b514bc8
JK
2437/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2438 objfile_obstack, and constructed with the specified field
2439 values. */
2440
2441static dwarf2_per_cu_data *
ed2dc618 2442create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2443 struct dwarf2_section_info *section,
2444 int is_dwz,
2445 sect_offset sect_off, ULONGEST length)
2446{
ed2dc618 2447 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2448 dwarf2_per_cu_data *the_cu
2449 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2450 struct dwarf2_per_cu_data);
2451 the_cu->sect_off = sect_off;
2452 the_cu->length = length;
e3b94546 2453 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2454 the_cu->section = section;
2455 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2456 struct dwarf2_per_cu_quick_data);
2457 the_cu->is_dwz = is_dwz;
2458 return the_cu;
2459}
2460
2ec9a5e0
TT
2461/* A helper for create_cus_from_index that handles a given list of
2462 CUs. */
2fdf6df6 2463
74a0d9f6 2464static void
12359b5e 2465create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2466 const gdb_byte *cu_list, offset_type n_elements,
2467 struct dwarf2_section_info *section,
b76e467d 2468 int is_dwz)
9291a0cd 2469{
12359b5e 2470 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2471 {
74a0d9f6 2472 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2473
2474 sect_offset sect_off
2475 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2476 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2477 cu_list += 2 * 8;
2478
b76e467d 2479 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2480 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2481 sect_off, length);
b76e467d 2482 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2483 }
9291a0cd
TT
2484}
2485
2ec9a5e0 2486/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2487 the CU objects for this objfile. */
2ec9a5e0 2488
74a0d9f6 2489static void
12359b5e 2490create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2491 const gdb_byte *cu_list, offset_type cu_list_elements,
2492 const gdb_byte *dwz_list, offset_type dwz_elements)
2493{
b76e467d
SM
2494 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2495 dwarf2_per_objfile->all_comp_units.reserve
2496 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2497
12359b5e 2498 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2499 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2500
2501 if (dwz_elements == 0)
74a0d9f6 2502 return;
2ec9a5e0 2503
12359b5e
SM
2504 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2505 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2506 &dwz->info, 1);
2ec9a5e0
TT
2507}
2508
1fd400ff 2509/* Create the signatured type hash table from the index. */
673bfd45 2510
74a0d9f6 2511static void
12359b5e
SM
2512create_signatured_type_table_from_index
2513 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2514 struct dwarf2_section_info *section,
2515 const gdb_byte *bytes,
2516 offset_type elements)
1fd400ff 2517{
12359b5e 2518 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2519
b2bdb8cf
SM
2520 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2521 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2522
298e9637 2523 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2524
12359b5e 2525 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2526 {
52dc124a 2527 struct signatured_type *sig_type;
9c541725 2528 ULONGEST signature;
1fd400ff 2529 void **slot;
9c541725 2530 cu_offset type_offset_in_tu;
1fd400ff 2531
74a0d9f6 2532 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2533 sect_offset sect_off
2534 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2535 type_offset_in_tu
2536 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2537 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2538 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2539 bytes += 3 * 8;
2540
52dc124a 2541 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2542 struct signatured_type);
52dc124a 2543 sig_type->signature = signature;
9c541725 2544 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2545 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2546 sig_type->per_cu.section = section;
9c541725 2547 sig_type->per_cu.sect_off = sect_off;
e3b94546 2548 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2549 sig_type->per_cu.v.quick
1fd400ff
TT
2550 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2551 struct dwarf2_per_cu_quick_data);
2552
b0b6a987 2553 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2554 *slot = sig_type;
1fd400ff 2555
b2bdb8cf 2556 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2557 }
2558
b0b6a987 2559 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2560}
2561
927aa2e7
JK
2562/* Create the signatured type hash table from .debug_names. */
2563
2564static void
2565create_signatured_type_table_from_debug_names
ed2dc618 2566 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2567 const mapped_debug_names &map,
2568 struct dwarf2_section_info *section,
2569 struct dwarf2_section_info *abbrev_section)
2570{
ed2dc618
SM
2571 struct objfile *objfile = dwarf2_per_objfile->objfile;
2572
96b79293
TT
2573 section->read (objfile);
2574 abbrev_section->read (objfile);
927aa2e7 2575
b2bdb8cf
SM
2576 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2577 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2578
298e9637 2579 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2580
2581 for (uint32_t i = 0; i < map.tu_count; ++i)
2582 {
2583 struct signatured_type *sig_type;
927aa2e7 2584 void **slot;
927aa2e7
JK
2585
2586 sect_offset sect_off
2587 = (sect_offset) (extract_unsigned_integer
2588 (map.tu_table_reordered + i * map.offset_size,
2589 map.offset_size,
2590 map.dwarf5_byte_order));
2591
2592 comp_unit_head cu_header;
ed2dc618
SM
2593 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2594 abbrev_section,
927aa2e7
JK
2595 section->buffer + to_underlying (sect_off),
2596 rcuh_kind::TYPE);
2597
2598 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2599 struct signatured_type);
2600 sig_type->signature = cu_header.signature;
2601 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2602 sig_type->per_cu.is_debug_types = 1;
2603 sig_type->per_cu.section = section;
2604 sig_type->per_cu.sect_off = sect_off;
e3b94546 2605 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2606 sig_type->per_cu.v.quick
2607 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2608 struct dwarf2_per_cu_quick_data);
2609
b0b6a987 2610 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2611 *slot = sig_type;
2612
b2bdb8cf 2613 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2614 }
2615
b0b6a987 2616 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2617}
2618
9291a0cd
TT
2619/* Read the address map data from the mapped index, and use it to
2620 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2621
9291a0cd 2622static void
ed2dc618
SM
2623create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2624 struct mapped_index *index)
9291a0cd 2625{
ed2dc618 2626 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2628 const gdb_byte *iter, *end;
9291a0cd 2629 struct addrmap *mutable_map;
9291a0cd
TT
2630 CORE_ADDR baseaddr;
2631
8268c778
PA
2632 auto_obstack temp_obstack;
2633
9291a0cd
TT
2634 mutable_map = addrmap_create_mutable (&temp_obstack);
2635
f00a2de2
PA
2636 iter = index->address_table.data ();
2637 end = iter + index->address_table.size ();
9291a0cd 2638
b3b3bada 2639 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2640
2641 while (iter < end)
2642 {
2643 ULONGEST hi, lo, cu_index;
2644 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2645 iter += 8;
2646 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2647 iter += 8;
2648 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2649 iter += 4;
f652bce2 2650
24a55014 2651 if (lo > hi)
f652bce2 2652 {
b98664d3 2653 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2654 hex_string (lo), hex_string (hi));
24a55014 2655 continue;
f652bce2 2656 }
24a55014 2657
b76e467d 2658 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2659 {
b98664d3 2660 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2661 (unsigned) cu_index);
24a55014 2662 continue;
f652bce2 2663 }
24a55014 2664
79748972
TT
2665 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2666 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2667 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2668 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2669 }
2670
d320c2b5 2671 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2672 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2673}
2674
927aa2e7
JK
2675/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2676 populate the objfile's psymtabs_addrmap. */
2677
2678static void
ed2dc618 2679create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2680 struct dwarf2_section_info *section)
2681{
ed2dc618 2682 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2683 bfd *abfd = objfile->obfd;
2684 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2685 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2686
2687 auto_obstack temp_obstack;
2688 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2689
2690 std::unordered_map<sect_offset,
2691 dwarf2_per_cu_data *,
2692 gdb::hash_enum<sect_offset>>
2693 debug_info_offset_to_per_cu;
b76e467d 2694 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2695 {
927aa2e7
JK
2696 const auto insertpair
2697 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2698 if (!insertpair.second)
2699 {
2700 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2701 "debug_info_offset %s, ignoring .debug_aranges."),
2702 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2703 return;
2704 }
2705 }
2706
96b79293 2707 section->read (objfile);
927aa2e7
JK
2708
2709 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2710
2711 const gdb_byte *addr = section->buffer;
2712
2713 while (addr < section->buffer + section->size)
2714 {
2715 const gdb_byte *const entry_addr = addr;
2716 unsigned int bytes_read;
2717
2718 const LONGEST entry_length = read_initial_length (abfd, addr,
2719 &bytes_read);
2720 addr += bytes_read;
2721
2722 const gdb_byte *const entry_end = addr + entry_length;
2723 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2724 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2725 if (addr + entry_length > section->buffer + section->size)
2726 {
47e3f474 2727 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2728 "length %s exceeds section length %s, "
2729 "ignoring .debug_aranges."),
47e3f474
TV
2730 objfile_name (objfile),
2731 plongest (entry_addr - section->buffer),
927aa2e7
JK
2732 plongest (bytes_read + entry_length),
2733 pulongest (section->size));
2734 return;
2735 }
2736
2737 /* The version number. */
2738 const uint16_t version = read_2_bytes (abfd, addr);
2739 addr += 2;
2740 if (version != 2)
2741 {
47e3f474 2742 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2743 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2744 objfile_name (objfile),
2745 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2746 return;
2747 }
2748
2749 const uint64_t debug_info_offset
2750 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2751 addr += offset_size;
2752 const auto per_cu_it
2753 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2754 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2755 {
47e3f474 2756 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2757 "debug_info_offset %s does not exists, "
2758 "ignoring .debug_aranges."),
47e3f474
TV
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
927aa2e7
JK
2761 pulongest (debug_info_offset));
2762 return;
2763 }
2764 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2765
2766 const uint8_t address_size = *addr++;
2767 if (address_size < 1 || address_size > 8)
2768 {
47e3f474 2769 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2770 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2771 objfile_name (objfile),
2772 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2773 return;
2774 }
2775
2776 const uint8_t segment_selector_size = *addr++;
2777 if (segment_selector_size != 0)
2778 {
47e3f474 2779 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2780 "segment_selector_size %u is not supported, "
2781 "ignoring .debug_aranges."),
47e3f474
TV
2782 objfile_name (objfile),
2783 plongest (entry_addr - section->buffer),
927aa2e7
JK
2784 segment_selector_size);
2785 return;
2786 }
2787
2788 /* Must pad to an alignment boundary that is twice the address
2789 size. It is undocumented by the DWARF standard but GCC does
2790 use it. */
2791 for (size_t padding = ((-(addr - section->buffer))
2792 & (2 * address_size - 1));
2793 padding > 0; padding--)
2794 if (*addr++ != 0)
2795 {
47e3f474 2796 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2797 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2798 objfile_name (objfile),
2799 plongest (entry_addr - section->buffer));
927aa2e7
JK
2800 return;
2801 }
2802
2803 for (;;)
2804 {
2805 if (addr + 2 * address_size > entry_end)
2806 {
47e3f474 2807 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2808 "address list is not properly terminated, "
2809 "ignoring .debug_aranges."),
47e3f474
TV
2810 objfile_name (objfile),
2811 plongest (entry_addr - section->buffer));
927aa2e7
JK
2812 return;
2813 }
2814 ULONGEST start = extract_unsigned_integer (addr, address_size,
2815 dwarf5_byte_order);
2816 addr += address_size;
2817 ULONGEST length = extract_unsigned_integer (addr, address_size,
2818 dwarf5_byte_order);
2819 addr += address_size;
2820 if (start == 0 && length == 0)
2821 break;
2822 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2823 {
2824 /* Symbol was eliminated due to a COMDAT group. */
2825 continue;
2826 }
2827 ULONGEST end = start + length;
79748972
TT
2828 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2829 - baseaddr);
2830 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2831 - baseaddr);
927aa2e7
JK
2832 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2833 }
2834 }
2835
d320c2b5 2836 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2837 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2838}
2839
9291a0cd
TT
2840/* Find a slot in the mapped index INDEX for the object named NAME.
2841 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2842 constant pool and return true. If NAME cannot be found, return
2843 false. */
2fdf6df6 2844
109483d9 2845static bool
9291a0cd
TT
2846find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2847 offset_type **vec_out)
2848{
0cf03b49 2849 offset_type hash;
9291a0cd 2850 offset_type slot, step;
559a7a62 2851 int (*cmp) (const char *, const char *);
9291a0cd 2852
791afaa2 2853 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2854 if (current_language->la_language == language_cplus
45280282
IB
2855 || current_language->la_language == language_fortran
2856 || current_language->la_language == language_d)
0cf03b49
JK
2857 {
2858 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2859 not contain any. */
a8719064 2860
72998fb3 2861 if (strchr (name, '(') != NULL)
0cf03b49 2862 {
109483d9 2863 without_params = cp_remove_params (name);
0cf03b49 2864
72998fb3 2865 if (without_params != NULL)
791afaa2 2866 name = without_params.get ();
0cf03b49
JK
2867 }
2868 }
2869
559a7a62 2870 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2871 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2872 simulate our NAME being searched is also lowercased. */
2873 hash = mapped_index_string_hash ((index->version == 4
2874 && case_sensitivity == case_sensitive_off
2875 ? 5 : index->version),
2876 name);
2877
f00a2de2
PA
2878 slot = hash & (index->symbol_table.size () - 1);
2879 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2880 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2881
2882 for (;;)
2883 {
9291a0cd 2884 const char *str;
f00a2de2
PA
2885
2886 const auto &bucket = index->symbol_table[slot];
2887 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2888 return false;
9291a0cd 2889
f00a2de2 2890 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2891 if (!cmp (name, str))
9291a0cd
TT
2892 {
2893 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2894 + MAYBE_SWAP (bucket.vec));
109483d9 2895 return true;
9291a0cd
TT
2896 }
2897
f00a2de2 2898 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2899 }
2900}
2901
4485a1c1
SM
2902/* A helper function that reads the .gdb_index from BUFFER and fills
2903 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2904 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2905 ok to use deprecated sections.
2906
2907 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2908 out parameters that are filled in with information about the CU and
2909 TU lists in the section.
2910
4485a1c1 2911 Returns true if all went well, false otherwise. */
2fdf6df6 2912
d33bc52e 2913static bool
4485a1c1
SM
2914read_gdb_index_from_buffer (struct objfile *objfile,
2915 const char *filename,
2916 bool deprecated_ok,
2917 gdb::array_view<const gdb_byte> buffer,
2918 struct mapped_index *map,
2919 const gdb_byte **cu_list,
2920 offset_type *cu_list_elements,
2921 const gdb_byte **types_list,
2922 offset_type *types_list_elements)
2923{
2924 const gdb_byte *addr = &buffer[0];
82430852 2925
9291a0cd 2926 /* Version check. */
4485a1c1 2927 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2928 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2929 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2930 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2931 indices. */
831adc1f 2932 if (version < 4)
481860b3
GB
2933 {
2934 static int warning_printed = 0;
2935 if (!warning_printed)
2936 {
2937 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2938 filename);
481860b3
GB
2939 warning_printed = 1;
2940 }
2941 return 0;
2942 }
2943 /* Index version 4 uses a different hash function than index version
2944 5 and later.
2945
2946 Versions earlier than 6 did not emit psymbols for inlined
2947 functions. Using these files will cause GDB not to be able to
2948 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2949 indices unless the user has done
2950 "set use-deprecated-index-sections on". */
2ec9a5e0 2951 if (version < 6 && !deprecated_ok)
481860b3
GB
2952 {
2953 static int warning_printed = 0;
2954 if (!warning_printed)
2955 {
e615022a
DE
2956 warning (_("\
2957Skipping deprecated .gdb_index section in %s.\n\
2958Do \"set use-deprecated-index-sections on\" before the file is read\n\
2959to use the section anyway."),
2ec9a5e0 2960 filename);
481860b3
GB
2961 warning_printed = 1;
2962 }
2963 return 0;
2964 }
796a7ff8 2965 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2966 of the TU (for symbols coming from TUs),
2967 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2968 Plus gold-generated indices can have duplicate entries for global symbols,
2969 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2970 These are just performance bugs, and we can't distinguish gdb-generated
2971 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2972
481860b3 2973 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2974 longer backward compatible. */
796a7ff8 2975 if (version > 8)
594e8718 2976 return 0;
9291a0cd 2977
559a7a62 2978 map->version = version;
9291a0cd 2979
4485a1c1 2980 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2981
4485a1c1 2982 int i = 0;
2ec9a5e0
TT
2983 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2984 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2985 / 8);
1fd400ff
TT
2986 ++i;
2987
2ec9a5e0
TT
2988 *types_list = addr + MAYBE_SWAP (metadata[i]);
2989 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2990 - MAYBE_SWAP (metadata[i]))
2991 / 8);
987d643c 2992 ++i;
1fd400ff 2993
f00a2de2
PA
2994 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2995 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2996 map->address_table
2997 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2998 ++i;
2999
f00a2de2
PA
3000 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3001 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3002 map->symbol_table
3003 = gdb::array_view<mapped_index::symbol_table_slot>
3004 ((mapped_index::symbol_table_slot *) symbol_table,
3005 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3006
f00a2de2 3007 ++i;
f9d83a0b 3008 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3009
2ec9a5e0
TT
3010 return 1;
3011}
3012
4485a1c1
SM
3013/* Callback types for dwarf2_read_gdb_index. */
3014
3015typedef gdb::function_view
3016 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3017 get_gdb_index_contents_ftype;
3018typedef gdb::function_view
3019 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3020 get_gdb_index_contents_dwz_ftype;
3021
927aa2e7 3022/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3023 elements of all the CUs and return 1. Otherwise, return 0. */
3024
3025static int
4485a1c1
SM
3026dwarf2_read_gdb_index
3027 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3028 get_gdb_index_contents_ftype get_gdb_index_contents,
3029 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3030{
2ec9a5e0
TT
3031 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3032 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3033 struct dwz_file *dwz;
12359b5e 3034 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3035
4485a1c1
SM
3036 gdb::array_view<const gdb_byte> main_index_contents
3037 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3038
3039 if (main_index_contents.empty ())
3040 return 0;
3041
3063847f 3042 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3043 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3044 use_deprecated_index_sections,
3045 main_index_contents, map.get (), &cu_list,
3046 &cu_list_elements, &types_list,
3047 &types_list_elements))
2ec9a5e0
TT
3048 return 0;
3049
0fefef59 3050 /* Don't use the index if it's empty. */
3063847f 3051 if (map->symbol_table.empty ())
0fefef59
DE
3052 return 0;
3053
2ec9a5e0
TT
3054 /* If there is a .dwz file, read it so we can get its CU list as
3055 well. */
ed2dc618 3056 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3057 if (dwz != NULL)
2ec9a5e0 3058 {
2ec9a5e0
TT
3059 struct mapped_index dwz_map;
3060 const gdb_byte *dwz_types_ignore;
3061 offset_type dwz_types_elements_ignore;
3062
4485a1c1
SM
3063 gdb::array_view<const gdb_byte> dwz_index_content
3064 = get_gdb_index_contents_dwz (objfile, dwz);
3065
3066 if (dwz_index_content.empty ())
3067 return 0;
3068
3069 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3070 bfd_get_filename (dwz->dwz_bfd.get ()),
3071 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3072 &dwz_list, &dwz_list_elements,
3073 &dwz_types_ignore,
3074 &dwz_types_elements_ignore))
2ec9a5e0
TT
3075 {
3076 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3077 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3078 return 0;
3079 }
3080 }
3081
12359b5e
SM
3082 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3083 dwz_list, dwz_list_elements);
1fd400ff 3084
8b70b953
TT
3085 if (types_list_elements)
3086 {
8b70b953
TT
3087 /* We can only handle a single .debug_types when we have an
3088 index. */
fd5866f6 3089 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3090 return 0;
3091
fd5866f6 3092 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3093
12359b5e
SM
3094 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3095 types_list, types_list_elements);
8b70b953 3096 }
9291a0cd 3097
3063847f 3098 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3099
3063847f 3100 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3101 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3102 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3103 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3104
3105 return 1;
3106}
3107
dee91e82 3108/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3109
dee91e82
DE
3110static void
3111dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3112 const gdb_byte *info_ptr,
3e225074 3113 struct die_info *comp_unit_die)
9291a0cd 3114{
dee91e82 3115 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3116 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3117 struct dwarf2_per_objfile *dwarf2_per_objfile
3118 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3119 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3120 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3121 struct attribute *attr;
7b9f3c50
DE
3122 void **slot;
3123 struct quick_file_names *qfn;
9291a0cd 3124
0186c6a7
DE
3125 gdb_assert (! this_cu->is_debug_types);
3126
07261596
TT
3127 /* Our callers never want to match partial units -- instead they
3128 will match the enclosing full CU. */
3129 if (comp_unit_die->tag == DW_TAG_partial_unit)
3130 {
3131 this_cu->v.quick->no_file_data = 1;
3132 return;
3133 }
3134
0186c6a7 3135 lh_cu = this_cu;
7b9f3c50 3136 slot = NULL;
dee91e82 3137
fff8551c 3138 line_header_up lh;
9c541725 3139 sect_offset line_offset {};
fff8551c 3140
dee91e82 3141 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3142 if (attr != nullptr)
9291a0cd 3143 {
7b9f3c50
DE
3144 struct quick_file_names find_entry;
3145
9c541725 3146 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3147
3148 /* We may have already read in this line header (TU line header sharing).
3149 If we have we're done. */
094b34ac 3150 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3151 find_entry.hash.line_sect_off = line_offset;
5895093f 3152 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3153 &find_entry, INSERT);
3154 if (*slot != NULL)
3155 {
9a3c8263 3156 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3157 return;
7b9f3c50
DE
3158 }
3159
3019eac3 3160 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3161 }
3162 if (lh == NULL)
3163 {
094b34ac 3164 lh_cu->v.quick->no_file_data = 1;
dee91e82 3165 return;
9291a0cd
TT
3166 }
3167
8d749320 3168 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3169 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3170 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3171 gdb_assert (slot != NULL);
3172 *slot = qfn;
9291a0cd 3173
d721ba37 3174 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3175
aa391654
TT
3176 int offset = 0;
3177 if (strcmp (fnd.name, "<unknown>") != 0)
3178 ++offset;
3179
7ba99d21 3180 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3181 qfn->file_names =
aa391654
TT
3182 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3183 if (offset != 0)
3184 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3185 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3186 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3187 fnd.comp_dir).release ();
7b9f3c50 3188 qfn->real_names = NULL;
9291a0cd 3189
094b34ac 3190 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3191}
3192
3193/* A helper for the "quick" functions which attempts to read the line
3194 table for THIS_CU. */
3195
3196static struct quick_file_names *
e4a48d9d 3197dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3198{
0186c6a7
DE
3199 /* This should never be called for TUs. */
3200 gdb_assert (! this_cu->is_debug_types);
3201 /* Nor type unit groups. */
197400e8 3202 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3203
dee91e82
DE
3204 if (this_cu->v.quick->file_names != NULL)
3205 return this_cu->v.quick->file_names;
3206 /* If we know there is no line data, no point in looking again. */
3207 if (this_cu->v.quick->no_file_data)
3208 return NULL;
3209
c0ab21c2
TT
3210 cutu_reader reader (this_cu);
3211 if (!reader.dummy_p)
3e225074 3212 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3213
3214 if (this_cu->v.quick->no_file_data)
3215 return NULL;
3216 return this_cu->v.quick->file_names;
9291a0cd
TT
3217}
3218
3219/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3220 real path for a given file name from the line table. */
2fdf6df6 3221
9291a0cd 3222static const char *
7b9f3c50
DE
3223dw2_get_real_path (struct objfile *objfile,
3224 struct quick_file_names *qfn, int index)
9291a0cd 3225{
7b9f3c50
DE
3226 if (qfn->real_names == NULL)
3227 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3228 qfn->num_file_names, const char *);
9291a0cd 3229
7b9f3c50 3230 if (qfn->real_names[index] == NULL)
14278e1f 3231 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3232
7b9f3c50 3233 return qfn->real_names[index];
9291a0cd
TT
3234}
3235
3236static struct symtab *
3237dw2_find_last_source_symtab (struct objfile *objfile)
3238{
ed2dc618
SM
3239 struct dwarf2_per_objfile *dwarf2_per_objfile
3240 = get_dwarf2_per_objfile (objfile);
b76e467d 3241 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3242 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3243
43f3e411
DE
3244 if (cust == NULL)
3245 return NULL;
ed2dc618 3246
43f3e411 3247 return compunit_primary_filetab (cust);
9291a0cd
TT
3248}
3249
7b9f3c50
DE
3250/* Traversal function for dw2_forget_cached_source_info. */
3251
3252static int
3253dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3254{
7b9f3c50 3255 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3256
7b9f3c50 3257 if (file_data->real_names)
9291a0cd 3258 {
7b9f3c50 3259 int i;
9291a0cd 3260
7b9f3c50 3261 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3262 {
7b9f3c50
DE
3263 xfree ((void*) file_data->real_names[i]);
3264 file_data->real_names[i] = NULL;
9291a0cd
TT
3265 }
3266 }
7b9f3c50
DE
3267
3268 return 1;
3269}
3270
3271static void
3272dw2_forget_cached_source_info (struct objfile *objfile)
3273{
ed2dc618
SM
3274 struct dwarf2_per_objfile *dwarf2_per_objfile
3275 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3276
5895093f 3277 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3278 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3279}
3280
f8eba3c6
TT
3281/* Helper function for dw2_map_symtabs_matching_filename that expands
3282 the symtabs and calls the iterator. */
3283
3284static int
3285dw2_map_expand_apply (struct objfile *objfile,
3286 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3287 const char *name, const char *real_path,
14bc53a8 3288 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3289{
43f3e411 3290 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3291
3292 /* Don't visit already-expanded CUs. */
43f3e411 3293 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3294 return 0;
3295
3296 /* This may expand more than one symtab, and we want to iterate over
3297 all of them. */
58f0c718 3298 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3299
14bc53a8
PA
3300 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3301 last_made, callback);
f8eba3c6
TT
3302}
3303
3304/* Implementation of the map_symtabs_matching_filename method. */
3305
14bc53a8
PA
3306static bool
3307dw2_map_symtabs_matching_filename
3308 (struct objfile *objfile, const char *name, const char *real_path,
3309 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3310{
c011a4f4 3311 const char *name_basename = lbasename (name);
ed2dc618
SM
3312 struct dwarf2_per_objfile *dwarf2_per_objfile
3313 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3314
848e3e78
DE
3315 /* The rule is CUs specify all the files, including those used by
3316 any TU, so there's no need to scan TUs here. */
f4dc4d17 3317
b76e467d 3318 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3319 {
3d7bb9d9 3320 /* We only need to look at symtabs not already expanded. */
43f3e411 3321 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3322 continue;
3323
b76e467d 3324 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3325 if (file_data == NULL)
9291a0cd
TT
3326 continue;
3327
b76e467d 3328 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3329 {
7b9f3c50 3330 const char *this_name = file_data->file_names[j];
da235a7c 3331 const char *this_real_name;
9291a0cd 3332
af529f8f 3333 if (compare_filenames_for_search (this_name, name))
9291a0cd 3334 {
f5b95b50 3335 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3336 callback))
3337 return true;
288e77a7 3338 continue;
4aac40c8 3339 }
9291a0cd 3340
c011a4f4
DE
3341 /* Before we invoke realpath, which can get expensive when many
3342 files are involved, do a quick comparison of the basenames. */
3343 if (! basenames_may_differ
3344 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3345 continue;
3346
da235a7c
JK
3347 this_real_name = dw2_get_real_path (objfile, file_data, j);
3348 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3349 {
da235a7c 3350 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3351 callback))
3352 return true;
288e77a7 3353 continue;
da235a7c 3354 }
9291a0cd 3355
da235a7c
JK
3356 if (real_path != NULL)
3357 {
af529f8f
JK
3358 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3359 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3360 if (this_real_name != NULL
af529f8f 3361 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3362 {
f5b95b50 3363 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3364 callback))
3365 return true;
288e77a7 3366 continue;
9291a0cd
TT
3367 }
3368 }
3369 }
3370 }
3371
14bc53a8 3372 return false;
9291a0cd
TT
3373}
3374
da51c347
DE
3375/* Struct used to manage iterating over all CUs looking for a symbol. */
3376
3377struct dw2_symtab_iterator
9291a0cd 3378{
ed2dc618
SM
3379 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3380 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3381 /* If set, only look for symbols that match that block. Valid values are
3382 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3383 gdb::optional<block_enum> block_index;
da51c347
DE
3384 /* The kind of symbol we're looking for. */
3385 domain_enum domain;
3386 /* The list of CUs from the index entry of the symbol,
3387 or NULL if not found. */
3388 offset_type *vec;
3389 /* The next element in VEC to look at. */
3390 int next;
3391 /* The number of elements in VEC, or zero if there is no match. */
3392 int length;
8943b874
DE
3393 /* Have we seen a global version of the symbol?
3394 If so we can ignore all further global instances.
3395 This is to work around gold/15646, inefficient gold-generated
3396 indices. */
3397 int global_seen;
da51c347 3398};
9291a0cd 3399
2b79f376 3400/* Initialize the index symtab iterator ITER. */
2fdf6df6 3401
9291a0cd 3402static void
da51c347 3403dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3404 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3405 gdb::optional<block_enum> block_index,
da51c347
DE
3406 domain_enum domain,
3407 const char *name)
3408{
ed2dc618 3409 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3410 iter->block_index = block_index;
3411 iter->domain = domain;
3412 iter->next = 0;
8943b874 3413 iter->global_seen = 0;
da51c347 3414
3063847f 3415 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3416
3417 /* index is NULL if OBJF_READNOW. */
3418 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3419 iter->length = MAYBE_SWAP (*iter->vec);
3420 else
3421 {
3422 iter->vec = NULL;
3423 iter->length = 0;
3424 }
3425}
3426
3427/* Return the next matching CU or NULL if there are no more. */
3428
3429static struct dwarf2_per_cu_data *
3430dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3431{
ed2dc618
SM
3432 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3433
da51c347
DE
3434 for ( ; iter->next < iter->length; ++iter->next)
3435 {
3436 offset_type cu_index_and_attrs =
3437 MAYBE_SWAP (iter->vec[iter->next + 1]);
3438 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3439 gdb_index_symbol_kind symbol_kind =
3440 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3441 /* Only check the symbol attributes if they're present.
3442 Indices prior to version 7 don't record them,
3443 and indices >= 7 may elide them for certain symbols
3444 (gold does this). */
3445 int attrs_valid =
ed2dc618 3446 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3447 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3448
3190f0c6 3449 /* Don't crash on bad data. */
b76e467d 3450 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3451 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3452 {
b98664d3 3453 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3454 " [in module %s]"),
3455 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3456 continue;
3457 }
3458
ff4c9fec 3459 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3460
da51c347 3461 /* Skip if already read in. */
43f3e411 3462 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3463 continue;
3464
8943b874
DE
3465 /* Check static vs global. */
3466 if (attrs_valid)
3467 {
2b79f376
SM
3468 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3469
3470 if (iter->block_index.has_value ())
3471 {
3472 bool want_static = *iter->block_index == STATIC_BLOCK;
3473
3474 if (is_static != want_static)
3475 continue;
3476 }
3477
8943b874
DE
3478 /* Work around gold/15646. */
3479 if (!is_static && iter->global_seen)
3480 continue;
3481 if (!is_static)
3482 iter->global_seen = 1;
3483 }
da51c347
DE
3484
3485 /* Only check the symbol's kind if it has one. */
3486 if (attrs_valid)
3487 {
3488 switch (iter->domain)
3489 {
3490 case VAR_DOMAIN:
3491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3492 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3493 /* Some types are also in VAR_DOMAIN. */
3494 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3495 continue;
3496 break;
3497 case STRUCT_DOMAIN:
3498 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3499 continue;
3500 break;
3501 case LABEL_DOMAIN:
3502 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3503 continue;
3504 break;
59c35742
AB
3505 case MODULE_DOMAIN:
3506 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3507 continue;
3508 break;
da51c347
DE
3509 default:
3510 break;
3511 }
3512 }
3513
3514 ++iter->next;
3515 return per_cu;
3516 }
3517
3518 return NULL;
3519}
3520
43f3e411 3521static struct compunit_symtab *
c7f839cb 3522dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3523 const char *name, domain_enum domain)
9291a0cd 3524{
43f3e411 3525 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3526 struct dwarf2_per_objfile *dwarf2_per_objfile
3527 = get_dwarf2_per_objfile (objfile);
9291a0cd 3528
b5ec771e
PA
3529 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3530
ed2dc618
SM
3531 struct dw2_symtab_iterator iter;
3532 struct dwarf2_per_cu_data *per_cu;
da51c347 3533
2b79f376 3534 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3535
ed2dc618
SM
3536 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3537 {
3538 struct symbol *sym, *with_opaque = NULL;
58f0c718 3539 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3540 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3541 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3542
ed2dc618
SM
3543 sym = block_find_symbol (block, name, domain,
3544 block_find_non_opaque_type_preferred,
3545 &with_opaque);
b2e2f908 3546
ed2dc618
SM
3547 /* Some caution must be observed with overloaded functions
3548 and methods, since the index will not contain any overload
3549 information (but NAME might contain it). */
da51c347 3550
ed2dc618
SM
3551 if (sym != NULL
3552 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3553 return stab;
3554 if (with_opaque != NULL
3555 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3556 stab_best = stab;
da51c347 3557
ed2dc618 3558 /* Keep looking through other CUs. */
9291a0cd 3559 }
9291a0cd 3560
da51c347 3561 return stab_best;
9291a0cd
TT
3562}
3563
3564static void
3565dw2_print_stats (struct objfile *objfile)
3566{
ed2dc618
SM
3567 struct dwarf2_per_objfile *dwarf2_per_objfile
3568 = get_dwarf2_per_objfile (objfile);
b76e467d 3569 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3570 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3571 int count = 0;
9291a0cd 3572
ed2dc618 3573 for (int i = 0; i < total; ++i)
9291a0cd 3574 {
ff4c9fec 3575 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3576
43f3e411 3577 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3578 ++count;
3579 }
e4a48d9d 3580 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3581 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3582}
3583
779bd270
DE
3584/* This dumps minimal information about the index.
3585 It is called via "mt print objfiles".
3586 One use is to verify .gdb_index has been loaded by the
3587 gdb.dwarf2/gdb-index.exp testcase. */
3588
9291a0cd
TT
3589static void
3590dw2_dump (struct objfile *objfile)
3591{
ed2dc618
SM
3592 struct dwarf2_per_objfile *dwarf2_per_objfile
3593 = get_dwarf2_per_objfile (objfile);
3594
779bd270
DE
3595 gdb_assert (dwarf2_per_objfile->using_index);
3596 printf_filtered (".gdb_index:");
3597 if (dwarf2_per_objfile->index_table != NULL)
3598 {
3599 printf_filtered (" version %d\n",
3600 dwarf2_per_objfile->index_table->version);
3601 }
3602 else
3603 printf_filtered (" faked for \"readnow\"\n");
3604 printf_filtered ("\n");
9291a0cd
TT
3605}
3606
9291a0cd
TT
3607static void
3608dw2_expand_symtabs_for_function (struct objfile *objfile,
3609 const char *func_name)
3610{
ed2dc618
SM
3611 struct dwarf2_per_objfile *dwarf2_per_objfile
3612 = get_dwarf2_per_objfile (objfile);
da51c347 3613
ed2dc618
SM
3614 struct dw2_symtab_iterator iter;
3615 struct dwarf2_per_cu_data *per_cu;
da51c347 3616
2b79f376 3617 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3618
ed2dc618 3619 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3620 dw2_instantiate_symtab (per_cu, false);
da51c347 3621
9291a0cd
TT
3622}
3623
3624static void
3625dw2_expand_all_symtabs (struct objfile *objfile)
3626{
ed2dc618
SM
3627 struct dwarf2_per_objfile *dwarf2_per_objfile
3628 = get_dwarf2_per_objfile (objfile);
b76e467d 3629 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3630 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3631
ed2dc618 3632 for (int i = 0; i < total_units; ++i)
9291a0cd 3633 {
ff4c9fec 3634 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3635
58f0c718
TT
3636 /* We don't want to directly expand a partial CU, because if we
3637 read it with the wrong language, then assertion failures can
3638 be triggered later on. See PR symtab/23010. So, tell
3639 dw2_instantiate_symtab to skip partial CUs -- any important
3640 partial CU will be read via DW_TAG_imported_unit anyway. */
3641 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3642 }
3643}
3644
3645static void
652a8996
JK
3646dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3647 const char *fullname)
9291a0cd 3648{
ed2dc618
SM
3649 struct dwarf2_per_objfile *dwarf2_per_objfile
3650 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3651
3652 /* We don't need to consider type units here.
3653 This is only called for examining code, e.g. expand_line_sal.
3654 There can be an order of magnitude (or more) more type units
3655 than comp units, and we avoid them if we can. */
3656
b76e467d 3657 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3658 {
3d7bb9d9 3659 /* We only need to look at symtabs not already expanded. */
43f3e411 3660 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3661 continue;
3662
b76e467d 3663 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3664 if (file_data == NULL)
9291a0cd
TT
3665 continue;
3666
b76e467d 3667 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3668 {
652a8996
JK
3669 const char *this_fullname = file_data->file_names[j];
3670
3671 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3672 {
58f0c718 3673 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3674 break;
3675 }
3676 }
3677 }
3678}
3679
9291a0cd 3680static void
199b4314
TT
3681dw2_map_matching_symbols
3682 (struct objfile *objfile,
b054970d 3683 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3684 int global,
3685 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3686 symbol_compare_ftype *ordered_compare)
9291a0cd 3687{
40658b94 3688 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3689 current language is Ada for a non-Ada objfile using GNU index. As Ada
3690 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3691}
3692
e1ef7d7a
PA
3693/* Starting from a search name, return the string that finds the upper
3694 bound of all strings that start with SEARCH_NAME in a sorted name
3695 list. Returns the empty string to indicate that the upper bound is
3696 the end of the list. */
3697
3698static std::string
3699make_sort_after_prefix_name (const char *search_name)
3700{
3701 /* When looking to complete "func", we find the upper bound of all
3702 symbols that start with "func" by looking for where we'd insert
3703 the closest string that would follow "func" in lexicographical
3704 order. Usually, that's "func"-with-last-character-incremented,
3705 i.e. "fund". Mind non-ASCII characters, though. Usually those
3706 will be UTF-8 multi-byte sequences, but we can't be certain.
3707 Especially mind the 0xff character, which is a valid character in
3708 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3709 rule out compilers allowing it in identifiers. Note that
3710 conveniently, strcmp/strcasecmp are specified to compare
3711 characters interpreted as unsigned char. So what we do is treat
3712 the whole string as a base 256 number composed of a sequence of
3713 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3714 to 0, and carries 1 to the following more-significant position.
3715 If the very first character in SEARCH_NAME ends up incremented
3716 and carries/overflows, then the upper bound is the end of the
3717 list. The string after the empty string is also the empty
3718 string.
3719
3720 Some examples of this operation:
3721
3722 SEARCH_NAME => "+1" RESULT
3723
3724 "abc" => "abd"
3725 "ab\xff" => "ac"
3726 "\xff" "a" "\xff" => "\xff" "b"
3727 "\xff" => ""
3728 "\xff\xff" => ""
3729 "" => ""
3730
3731 Then, with these symbols for example:
3732
3733 func
3734 func1
3735 fund
3736
3737 completing "func" looks for symbols between "func" and
3738 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3739 which finds "func" and "func1", but not "fund".
3740
3741 And with:
3742
3743 funcÿ (Latin1 'ÿ' [0xff])
3744 funcÿ1
3745 fund
3746
3747 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3748 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3749
3750 And with:
3751
3752 ÿÿ (Latin1 'ÿ' [0xff])
3753 ÿÿ1
3754
3755 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3756 the end of the list.
3757 */
3758 std::string after = search_name;
3759 while (!after.empty () && (unsigned char) after.back () == 0xff)
3760 after.pop_back ();
3761 if (!after.empty ())
3762 after.back () = (unsigned char) after.back () + 1;
3763 return after;
3764}
3765
5c58de74 3766/* See declaration. */
61d96d7e 3767
5c58de74
PA
3768std::pair<std::vector<name_component>::const_iterator,
3769 std::vector<name_component>::const_iterator>
44ed8f3e 3770mapped_index_base::find_name_components_bounds
3b00ef10 3771 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3772{
5c58de74
PA
3773 auto *name_cmp
3774 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3775
3b00ef10
TT
3776 const char *lang_name
3777 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3778
3f563c84
PA
3779 /* Comparison function object for lower_bound that matches against a
3780 given symbol name. */
3781 auto lookup_compare_lower = [&] (const name_component &elem,
3782 const char *name)
3783 {
5c58de74 3784 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3785 const char *elem_name = elem_qualified + elem.name_offset;
3786 return name_cmp (elem_name, name) < 0;
3787 };
3788
3789 /* Comparison function object for upper_bound that matches against a
3790 given symbol name. */
3791 auto lookup_compare_upper = [&] (const char *name,
3792 const name_component &elem)
3793 {
5c58de74 3794 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3795 const char *elem_name = elem_qualified + elem.name_offset;
3796 return name_cmp (name, elem_name) < 0;
3797 };
3798
5c58de74
PA
3799 auto begin = this->name_components.begin ();
3800 auto end = this->name_components.end ();
3f563c84
PA
3801
3802 /* Find the lower bound. */
3803 auto lower = [&] ()
3804 {
3b00ef10 3805 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3806 return begin;
3807 else
3b00ef10 3808 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3809 } ();
3810
3811 /* Find the upper bound. */
3812 auto upper = [&] ()
3813 {
5c58de74 3814 if (lookup_name_without_params.completion_mode ())
3f563c84 3815 {
e1ef7d7a
PA
3816 /* In completion mode, we want UPPER to point past all
3817 symbols names that have the same prefix. I.e., with
3818 these symbols, and completing "func":
3819
3820 function << lower bound
3821 function1
3822 other_function << upper bound
3823
3824 We find the upper bound by looking for the insertion
3825 point of "func"-with-last-character-incremented,
3826 i.e. "fund". */
3b00ef10 3827 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3828 if (after.empty ())
3f563c84 3829 return end;
e6b2f5ef
PA
3830 return std::lower_bound (lower, end, after.c_str (),
3831 lookup_compare_lower);
3f563c84
PA
3832 }
3833 else
3b00ef10 3834 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3835 } ();
3836
5c58de74
PA
3837 return {lower, upper};
3838}
3839
3840/* See declaration. */
3841
3842void
44ed8f3e 3843mapped_index_base::build_name_components ()
5c58de74
PA
3844{
3845 if (!this->name_components.empty ())
3846 return;
3847
3848 this->name_components_casing = case_sensitivity;
3849 auto *name_cmp
3850 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3851
3852 /* The code below only knows how to break apart components of C++
3853 symbol names (and other languages that use '::' as
3b00ef10 3854 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3855 auto count = this->symbol_name_count ();
3856 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3857 {
44ed8f3e 3858 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3859 continue;
3860
3861 const char *name = this->symbol_name_at (idx);
3862
3863 /* Add each name component to the name component table. */
3864 unsigned int previous_len = 0;
3b00ef10
TT
3865
3866 if (strstr (name, "::") != nullptr)
3867 {
3868 for (unsigned int current_len = cp_find_first_component (name);
3869 name[current_len] != '\0';
3870 current_len += cp_find_first_component (name + current_len))
3871 {
3872 gdb_assert (name[current_len] == ':');
3873 this->name_components.push_back ({previous_len, idx});
3874 /* Skip the '::'. */
3875 current_len += 2;
3876 previous_len = current_len;
3877 }
3878 }
3879 else
5c58de74 3880 {
3b00ef10
TT
3881 /* Handle the Ada encoded (aka mangled) form here. */
3882 for (const char *iter = strstr (name, "__");
3883 iter != nullptr;
3884 iter = strstr (iter, "__"))
3885 {
3886 this->name_components.push_back ({previous_len, idx});
3887 iter += 2;
3888 previous_len = iter - name;
3889 }
5c58de74 3890 }
3b00ef10 3891
5c58de74
PA
3892 this->name_components.push_back ({previous_len, idx});
3893 }
3894
3895 /* Sort name_components elements by name. */
3896 auto name_comp_compare = [&] (const name_component &left,
3897 const name_component &right)
3898 {
3899 const char *left_qualified = this->symbol_name_at (left.idx);
3900 const char *right_qualified = this->symbol_name_at (right.idx);
3901
3902 const char *left_name = left_qualified + left.name_offset;
3903 const char *right_name = right_qualified + right.name_offset;
3904
3905 return name_cmp (left_name, right_name) < 0;
3906 };
3907
3908 std::sort (this->name_components.begin (),
3909 this->name_components.end (),
3910 name_comp_compare);
3911}
3912
3913/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3914 mapped_index_base instead of the containing objfile. This is split
3915 to a separate function in order to be able to unit test the
3916 name_components matching using a mock mapped_index_base. For each
5c58de74 3917 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3918 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3919
3920static void
3921dw2_expand_symtabs_matching_symbol
44ed8f3e 3922 (mapped_index_base &index,
5c58de74
PA
3923 const lookup_name_info &lookup_name_in,
3924 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3925 enum search_domain kind,
3b00ef10 3926 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3927{
3928 lookup_name_info lookup_name_without_params
3929 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3930
3931 /* Build the symbol name component sorted vector, if we haven't
3932 yet. */
3933 index.build_name_components ();
3934
3f563c84
PA
3935 /* The same symbol may appear more than once in the range though.
3936 E.g., if we're looking for symbols that complete "w", and we have
3937 a symbol named "w1::w2", we'll find the two name components for
3938 that same symbol in the range. To be sure we only call the
3939 callback once per symbol, we first collect the symbol name
3940 indexes that matched in a temporary vector and ignore
3941 duplicates. */
3942 std::vector<offset_type> matches;
3f563c84 3943
3b00ef10
TT
3944 struct name_and_matcher
3945 {
3946 symbol_name_matcher_ftype *matcher;
3947 const std::string &name;
3948
3949 bool operator== (const name_and_matcher &other) const
3f563c84 3950 {
3b00ef10
TT
3951 return matcher == other.matcher && name == other.name;
3952 }
3953 };
3954
3955 /* A vector holding all the different symbol name matchers, for all
3956 languages. */
3957 std::vector<name_and_matcher> matchers;
3958
3959 for (int i = 0; i < nr_languages; i++)
3960 {
3961 enum language lang_e = (enum language) i;
3962
3963 const language_defn *lang = language_def (lang_e);
3964 symbol_name_matcher_ftype *name_matcher
3965 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3966
3b00ef10
TT
3967 name_and_matcher key {
3968 name_matcher,
3969 lookup_name_without_params.language_lookup_name (lang_e)
3970 };
3971
3972 /* Don't insert the same comparison routine more than once.
3973 Note that we do this linear walk. This is not a problem in
3974 practice because the number of supported languages is
3975 low. */
3976 if (std::find (matchers.begin (), matchers.end (), key)
3977 != matchers.end ())
9291a0cd 3978 continue;
3b00ef10
TT
3979 matchers.push_back (std::move (key));
3980
3981 auto bounds
3982 = index.find_name_components_bounds (lookup_name_without_params,
3983 lang_e);
3984
3985 /* Now for each symbol name in range, check to see if we have a name
3986 match, and if so, call the MATCH_CALLBACK callback. */
3987
3988 for (; bounds.first != bounds.second; ++bounds.first)
3989 {
3990 const char *qualified = index.symbol_name_at (bounds.first->idx);
3991
3992 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3993 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3994 continue;
9291a0cd 3995
3b00ef10
TT
3996 matches.push_back (bounds.first->idx);
3997 }
3f563c84
PA
3998 }
3999
4000 std::sort (matches.begin (), matches.end ());
4001
4002 /* Finally call the callback, once per match. */
4003 ULONGEST prev = -1;
4004 for (offset_type idx : matches)
4005 {
4006 if (prev != idx)
4007 {
3b00ef10
TT
4008 if (!match_callback (idx))
4009 break;
3f563c84
PA
4010 prev = idx;
4011 }
4012 }
4013
4014 /* Above we use a type wider than idx's for 'prev', since 0 and
4015 (offset_type)-1 are both possible values. */
4016 static_assert (sizeof (prev) > sizeof (offset_type), "");
4017}
4018
c62446b1
PA
4019#if GDB_SELF_TEST
4020
4021namespace selftests { namespace dw2_expand_symtabs_matching {
4022
a3c5fafd
PA
4023/* A mock .gdb_index/.debug_names-like name index table, enough to
4024 exercise dw2_expand_symtabs_matching_symbol, which works with the
4025 mapped_index_base interface. Builds an index from the symbol list
4026 passed as parameter to the constructor. */
4027class mock_mapped_index : public mapped_index_base
c62446b1
PA
4028{
4029public:
a3c5fafd
PA
4030 mock_mapped_index (gdb::array_view<const char *> symbols)
4031 : m_symbol_table (symbols)
c62446b1
PA
4032 {}
4033
a3c5fafd 4034 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4035
a3c5fafd 4036 /* Return the number of names in the symbol table. */
632e107b 4037 size_t symbol_name_count () const override
c62446b1 4038 {
a3c5fafd 4039 return m_symbol_table.size ();
c62446b1
PA
4040 }
4041
a3c5fafd 4042 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4043 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4044 {
4045 return m_symbol_table[idx];
4046 }
c62446b1 4047
a3c5fafd
PA
4048private:
4049 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4050};
4051
4052/* Convenience function that converts a NULL pointer to a "<null>"
4053 string, to pass to print routines. */
4054
4055static const char *
4056string_or_null (const char *str)
4057{
4058 return str != NULL ? str : "<null>";
4059}
4060
4061/* Check if a lookup_name_info built from
4062 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4063 index. EXPECTED_LIST is the list of expected matches, in expected
4064 matching order. If no match expected, then an empty list is
4065 specified. Returns true on success. On failure prints a warning
4066 indicating the file:line that failed, and returns false. */
4067
4068static bool
4069check_match (const char *file, int line,
4070 mock_mapped_index &mock_index,
4071 const char *name, symbol_name_match_type match_type,
4072 bool completion_mode,
4073 std::initializer_list<const char *> expected_list)
4074{
4075 lookup_name_info lookup_name (name, match_type, completion_mode);
4076
4077 bool matched = true;
4078
4079 auto mismatch = [&] (const char *expected_str,
4080 const char *got)
4081 {
4082 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4083 "expected=\"%s\", got=\"%s\"\n"),
4084 file, line,
4085 (match_type == symbol_name_match_type::FULL
4086 ? "FULL" : "WILD"),
4087 name, string_or_null (expected_str), string_or_null (got));
4088 matched = false;
4089 };
4090
4091 auto expected_it = expected_list.begin ();
4092 auto expected_end = expected_list.end ();
4093
a3c5fafd 4094 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4095 NULL, ALL_DOMAIN,
4096 [&] (offset_type idx)
4097 {
a3c5fafd 4098 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4099 const char *expected_str
4100 = expected_it == expected_end ? NULL : *expected_it++;
4101
4102 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4103 mismatch (expected_str, matched_name);
3b00ef10 4104 return true;
c62446b1
PA
4105 });
4106
4107 const char *expected_str
4108 = expected_it == expected_end ? NULL : *expected_it++;
4109 if (expected_str != NULL)
4110 mismatch (expected_str, NULL);
4111
4112 return matched;
4113}
4114
4115/* The symbols added to the mock mapped_index for testing (in
4116 canonical form). */
4117static const char *test_symbols[] = {
4118 "function",
4119 "std::bar",
4120 "std::zfunction",
4121 "std::zfunction2",
4122 "w1::w2",
4123 "ns::foo<char*>",
4124 "ns::foo<int>",
4125 "ns::foo<long>",
a20714ff
PA
4126 "ns2::tmpl<int>::foo2",
4127 "(anonymous namespace)::A::B::C",
c62446b1 4128
e1ef7d7a
PA
4129 /* These are used to check that the increment-last-char in the
4130 matching algorithm for completion doesn't match "t1_fund" when
4131 completing "t1_func". */
4132 "t1_func",
4133 "t1_func1",
4134 "t1_fund",
4135 "t1_fund1",
4136
4137 /* A UTF-8 name with multi-byte sequences to make sure that
4138 cp-name-parser understands this as a single identifier ("função"
4139 is "function" in PT). */
4140 u8"u8função",
4141
4142 /* \377 (0xff) is Latin1 'ÿ'. */
4143 "yfunc\377",
4144
4145 /* \377 (0xff) is Latin1 'ÿ'. */
4146 "\377",
4147 "\377\377123",
4148
c62446b1
PA
4149 /* A name with all sorts of complications. Starts with "z" to make
4150 it easier for the completion tests below. */
4151#define Z_SYM_NAME \
4152 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4153 "::tuple<(anonymous namespace)::ui*, " \
4154 "std::default_delete<(anonymous namespace)::ui>, void>"
4155
4156 Z_SYM_NAME
4157};
4158
a3c5fafd
PA
4159/* Returns true if the mapped_index_base::find_name_component_bounds
4160 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4161 in completion mode. */
5c58de74
PA
4162
4163static bool
a3c5fafd 4164check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4165 const char *search_name,
4166 gdb::array_view<const char *> expected_syms)
4167{
4168 lookup_name_info lookup_name (search_name,
4169 symbol_name_match_type::FULL, true);
4170
3b00ef10
TT
4171 auto bounds = index.find_name_components_bounds (lookup_name,
4172 language_cplus);
5c58de74
PA
4173
4174 size_t distance = std::distance (bounds.first, bounds.second);
4175 if (distance != expected_syms.size ())
4176 return false;
4177
4178 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4179 {
4180 auto nc_elem = bounds.first + exp_elem;
4181 const char *qualified = index.symbol_name_at (nc_elem->idx);
4182 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4183 return false;
4184 }
4185
4186 return true;
4187}
4188
4189/* Test the lower-level mapped_index::find_name_component_bounds
4190 method. */
4191
c62446b1 4192static void
5c58de74
PA
4193test_mapped_index_find_name_component_bounds ()
4194{
4195 mock_mapped_index mock_index (test_symbols);
4196
a3c5fafd 4197 mock_index.build_name_components ();
5c58de74
PA
4198
4199 /* Test the lower-level mapped_index::find_name_component_bounds
4200 method in completion mode. */
4201 {
4202 static const char *expected_syms[] = {
4203 "t1_func",
4204 "t1_func1",
5c58de74
PA
4205 };
4206
a3c5fafd 4207 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4208 "t1_func", expected_syms));
4209 }
4210
4211 /* Check that the increment-last-char in the name matching algorithm
4212 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4213 {
4214 static const char *expected_syms1[] = {
4215 "\377",
4216 "\377\377123",
4217 };
a3c5fafd 4218 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4219 "\377", expected_syms1));
4220
4221 static const char *expected_syms2[] = {
4222 "\377\377123",
4223 };
a3c5fafd 4224 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4225 "\377\377", expected_syms2));
4226 }
4227}
4228
4229/* Test dw2_expand_symtabs_matching_symbol. */
4230
4231static void
4232test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4233{
4234 mock_mapped_index mock_index (test_symbols);
4235
4236 /* We let all tests run until the end even if some fails, for debug
4237 convenience. */
4238 bool any_mismatch = false;
4239
4240 /* Create the expected symbols list (an initializer_list). Needed
4241 because lists have commas, and we need to pass them to CHECK,
4242 which is a macro. */
4243#define EXPECT(...) { __VA_ARGS__ }
4244
4245 /* Wrapper for check_match that passes down the current
4246 __FILE__/__LINE__. */
4247#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4248 any_mismatch |= !check_match (__FILE__, __LINE__, \
4249 mock_index, \
4250 NAME, MATCH_TYPE, COMPLETION_MODE, \
4251 EXPECTED_LIST)
4252
4253 /* Identity checks. */
4254 for (const char *sym : test_symbols)
4255 {
4256 /* Should be able to match all existing symbols. */
4257 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4258 EXPECT (sym));
4259
4260 /* Should be able to match all existing symbols with
4261 parameters. */
4262 std::string with_params = std::string (sym) + "(int)";
4263 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4264 EXPECT (sym));
4265
4266 /* Should be able to match all existing symbols with
4267 parameters and qualifiers. */
4268 with_params = std::string (sym) + " ( int ) const";
4269 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4270 EXPECT (sym));
4271
4272 /* This should really find sym, but cp-name-parser.y doesn't
4273 know about lvalue/rvalue qualifiers yet. */
4274 with_params = std::string (sym) + " ( int ) &&";
4275 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4276 {});
4277 }
4278
e1ef7d7a
PA
4279 /* Check that the name matching algorithm for completion doesn't get
4280 confused with Latin1 'ÿ' / 0xff. */
4281 {
4282 static const char str[] = "\377";
4283 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4284 EXPECT ("\377", "\377\377123"));
4285 }
4286
4287 /* Check that the increment-last-char in the matching algorithm for
4288 completion doesn't match "t1_fund" when completing "t1_func". */
4289 {
4290 static const char str[] = "t1_func";
4291 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4292 EXPECT ("t1_func", "t1_func1"));
4293 }
4294
c62446b1
PA
4295 /* Check that completion mode works at each prefix of the expected
4296 symbol name. */
4297 {
4298 static const char str[] = "function(int)";
4299 size_t len = strlen (str);
4300 std::string lookup;
4301
4302 for (size_t i = 1; i < len; i++)
4303 {
4304 lookup.assign (str, i);
4305 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4306 EXPECT ("function"));
4307 }
4308 }
4309
4310 /* While "w" is a prefix of both components, the match function
4311 should still only be called once. */
4312 {
4313 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4314 EXPECT ("w1::w2"));
a20714ff
PA
4315 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4316 EXPECT ("w1::w2"));
c62446b1
PA
4317 }
4318
4319 /* Same, with a "complicated" symbol. */
4320 {
4321 static const char str[] = Z_SYM_NAME;
4322 size_t len = strlen (str);
4323 std::string lookup;
4324
4325 for (size_t i = 1; i < len; i++)
4326 {
4327 lookup.assign (str, i);
4328 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4329 EXPECT (Z_SYM_NAME));
4330 }
4331 }
4332
4333 /* In FULL mode, an incomplete symbol doesn't match. */
4334 {
4335 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4336 {});
4337 }
4338
4339 /* A complete symbol with parameters matches any overload, since the
4340 index has no overload info. */
4341 {
4342 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4343 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4344 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4345 EXPECT ("std::zfunction", "std::zfunction2"));
4346 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4347 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4348 }
4349
4350 /* Check that whitespace is ignored appropriately. A symbol with a
4351 template argument list. */
4352 {
4353 static const char expected[] = "ns::foo<int>";
4354 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4355 EXPECT (expected));
a20714ff
PA
4356 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4357 EXPECT (expected));
c62446b1
PA
4358 }
4359
4360 /* Check that whitespace is ignored appropriately. A symbol with a
4361 template argument list that includes a pointer. */
4362 {
4363 static const char expected[] = "ns::foo<char*>";
4364 /* Try both completion and non-completion modes. */
4365 static const bool completion_mode[2] = {false, true};
4366 for (size_t i = 0; i < 2; i++)
4367 {
4368 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4369 completion_mode[i], EXPECT (expected));
a20714ff
PA
4370 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4371 completion_mode[i], EXPECT (expected));
c62446b1
PA
4372
4373 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4374 completion_mode[i], EXPECT (expected));
a20714ff
PA
4375 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4376 completion_mode[i], EXPECT (expected));
c62446b1
PA
4377 }
4378 }
4379
4380 {
4381 /* Check method qualifiers are ignored. */
4382 static const char expected[] = "ns::foo<char*>";
4383 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4384 symbol_name_match_type::FULL, true, EXPECT (expected));
4385 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4386 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4387 CHECK_MATCH ("foo < char * > ( int ) const",
4388 symbol_name_match_type::WILD, true, EXPECT (expected));
4389 CHECK_MATCH ("foo < char * > ( int ) &&",
4390 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4391 }
4392
4393 /* Test lookup names that don't match anything. */
4394 {
a20714ff
PA
4395 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4396 {});
4397
c62446b1
PA
4398 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4399 {});
4400 }
4401
a20714ff
PA
4402 /* Some wild matching tests, exercising "(anonymous namespace)",
4403 which should not be confused with a parameter list. */
4404 {
4405 static const char *syms[] = {
4406 "A::B::C",
4407 "B::C",
4408 "C",
4409 "A :: B :: C ( int )",
4410 "B :: C ( int )",
4411 "C ( int )",
4412 };
4413
4414 for (const char *s : syms)
4415 {
4416 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4417 EXPECT ("(anonymous namespace)::A::B::C"));
4418 }
4419 }
4420
4421 {
4422 static const char expected[] = "ns2::tmpl<int>::foo2";
4423 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4424 EXPECT (expected));
4425 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4426 EXPECT (expected));
4427 }
4428
c62446b1
PA
4429 SELF_CHECK (!any_mismatch);
4430
4431#undef EXPECT
4432#undef CHECK_MATCH
4433}
4434
5c58de74
PA
4435static void
4436run_test ()
4437{
4438 test_mapped_index_find_name_component_bounds ();
4439 test_dw2_expand_symtabs_matching_symbol ();
4440}
4441
c62446b1
PA
4442}} // namespace selftests::dw2_expand_symtabs_matching
4443
4444#endif /* GDB_SELF_TEST */
4445
4b514bc8
JK
4446/* If FILE_MATCHER is NULL or if PER_CU has
4447 dwarf2_per_cu_quick_data::MARK set (see
4448 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4449 EXPANSION_NOTIFY on it. */
4450
4451static void
4452dw2_expand_symtabs_matching_one
4453 (struct dwarf2_per_cu_data *per_cu,
4454 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4455 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4456{
4457 if (file_matcher == NULL || per_cu->v.quick->mark)
4458 {
4459 bool symtab_was_null
4460 = (per_cu->v.quick->compunit_symtab == NULL);
4461
58f0c718 4462 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4463
4464 if (expansion_notify != NULL
4465 && symtab_was_null
4466 && per_cu->v.quick->compunit_symtab != NULL)
4467 expansion_notify (per_cu->v.quick->compunit_symtab);
4468 }
4469}
4470
3f563c84
PA
4471/* Helper for dw2_expand_matching symtabs. Called on each symbol
4472 matched, to expand corresponding CUs that were marked. IDX is the
4473 index of the symbol name that matched. */
4474
4475static void
4476dw2_expand_marked_cus
ed2dc618 4477 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4478 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4479 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4480 search_domain kind)
4481{
3f563c84
PA
4482 offset_type *vec, vec_len, vec_idx;
4483 bool global_seen = false;
ed2dc618 4484 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4485
61920122 4486 vec = (offset_type *) (index.constant_pool
f00a2de2 4487 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4488 vec_len = MAYBE_SWAP (vec[0]);
4489 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4490 {
61920122
PA
4491 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4492 /* This value is only valid for index versions >= 7. */
4493 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4494 gdb_index_symbol_kind symbol_kind =
4495 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4496 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4497 /* Only check the symbol attributes if they're present.
4498 Indices prior to version 7 don't record them,
4499 and indices >= 7 may elide them for certain symbols
4500 (gold does this). */
4501 int attrs_valid =
4502 (index.version >= 7
4503 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4504
4505 /* Work around gold/15646. */
4506 if (attrs_valid)
9291a0cd 4507 {
61920122
PA
4508 if (!is_static && global_seen)
4509 continue;
4510 if (!is_static)
4511 global_seen = true;
4512 }
3190f0c6 4513
61920122
PA
4514 /* Only check the symbol's kind if it has one. */
4515 if (attrs_valid)
4516 {
4517 switch (kind)
8943b874 4518 {
61920122
PA
4519 case VARIABLES_DOMAIN:
4520 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4521 continue;
4522 break;
4523 case FUNCTIONS_DOMAIN:
4524 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4525 continue;
61920122
PA
4526 break;
4527 case TYPES_DOMAIN:
4528 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4529 continue;
4530 break;
59c35742
AB
4531 case MODULES_DOMAIN:
4532 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4533 continue;
4534 break;
61920122
PA
4535 default:
4536 break;
8943b874 4537 }
61920122 4538 }
8943b874 4539
61920122 4540 /* Don't crash on bad data. */
b76e467d 4541 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4542 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4543 {
b98664d3 4544 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4545 " [in module %s]"),
4546 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4547 continue;
4548 }
4549
ff4c9fec 4550 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4551 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4552 expansion_notify);
61920122
PA
4553 }
4554}
4555
4b514bc8
JK
4556/* If FILE_MATCHER is non-NULL, set all the
4557 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4558 that match FILE_MATCHER. */
4559
61920122 4560static void
4b514bc8 4561dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4562 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4563 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4564{
4b514bc8 4565 if (file_matcher == NULL)
61920122
PA
4566 return;
4567
4b514bc8
JK
4568 objfile *const objfile = dwarf2_per_objfile->objfile;
4569
4570 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4571 htab_eq_pointer,
4572 NULL, xcalloc, xfree));
4573 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4574 htab_eq_pointer,
4575 NULL, xcalloc, xfree));
61920122 4576
4b514bc8
JK
4577 /* The rule is CUs specify all the files, including those used by
4578 any TU, so there's no need to scan TUs here. */
61920122 4579
b76e467d 4580 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4581 {
927aa2e7
JK
4582 QUIT;
4583
4584 per_cu->v.quick->mark = 0;
4585
4586 /* We only need to look at symtabs not already expanded. */
4587 if (per_cu->v.quick->compunit_symtab)
4588 continue;
4589
b76e467d 4590 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4591 if (file_data == NULL)
4592 continue;
4593
4594 if (htab_find (visited_not_found.get (), file_data) != NULL)
4595 continue;
4596 else if (htab_find (visited_found.get (), file_data) != NULL)
4597 {
4598 per_cu->v.quick->mark = 1;
4599 continue;
4600 }
4601
b76e467d 4602 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4603 {
4604 const char *this_real_name;
4605
4606 if (file_matcher (file_data->file_names[j], false))
4607 {
4608 per_cu->v.quick->mark = 1;
4609 break;
4610 }
4611
4612 /* Before we invoke realpath, which can get expensive when many
4613 files are involved, do a quick comparison of the basenames. */
4614 if (!basenames_may_differ
4615 && !file_matcher (lbasename (file_data->file_names[j]),
4616 true))
4617 continue;
4618
4619 this_real_name = dw2_get_real_path (objfile, file_data, j);
4620 if (file_matcher (this_real_name, false))
4621 {
4622 per_cu->v.quick->mark = 1;
4623 break;
4624 }
4625 }
4626
b76e467d
SM
4627 void **slot = htab_find_slot (per_cu->v.quick->mark
4628 ? visited_found.get ()
4629 : visited_not_found.get (),
4630 file_data, INSERT);
927aa2e7
JK
4631 *slot = file_data;
4632 }
4633}
4634
4635static void
4636dw2_expand_symtabs_matching
4637 (struct objfile *objfile,
4638 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4639 const lookup_name_info &lookup_name,
4640 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4641 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4642 enum search_domain kind)
4643{
ed2dc618
SM
4644 struct dwarf2_per_objfile *dwarf2_per_objfile
4645 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4646
4647 /* index_table is NULL if OBJF_READNOW. */
4648 if (!dwarf2_per_objfile->index_table)
4649 return;
4650
ed2dc618 4651 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4652
4653 mapped_index &index = *dwarf2_per_objfile->index_table;
4654
4655 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4656 symbol_matcher,
4657 kind, [&] (offset_type idx)
4658 {
ed2dc618 4659 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4660 expansion_notify, kind);
3b00ef10 4661 return true;
927aa2e7
JK
4662 });
4663}
4664
4665/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4666 symtab. */
4667
4668static struct compunit_symtab *
4669recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4670 CORE_ADDR pc)
4671{
4672 int i;
4673
4674 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4675 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4676 return cust;
4677
4678 if (cust->includes == NULL)
4679 return NULL;
4680
4681 for (i = 0; cust->includes[i]; ++i)
4682 {
4683 struct compunit_symtab *s = cust->includes[i];
4684
4685 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4686 if (s != NULL)
4687 return s;
4688 }
4689
4690 return NULL;
4691}
4692
4693static struct compunit_symtab *
4694dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4695 struct bound_minimal_symbol msymbol,
4696 CORE_ADDR pc,
4697 struct obj_section *section,
4698 int warn_if_readin)
4699{
4700 struct dwarf2_per_cu_data *data;
4701 struct compunit_symtab *result;
4702
d320c2b5 4703 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4704 return NULL;
4705
b3b3bada 4706 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4707 data = (struct dwarf2_per_cu_data *) addrmap_find
4708 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4709 if (!data)
4710 return NULL;
4711
4712 if (warn_if_readin && data->v.quick->compunit_symtab)
4713 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4714 paddress (get_objfile_arch (objfile), pc));
4715
4716 result
58f0c718
TT
4717 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4718 false),
927aa2e7
JK
4719 pc);
4720 gdb_assert (result != NULL);
4721 return result;
4722}
4723
4724static void
4725dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4726 void *data, int need_fullname)
4727{
ed2dc618
SM
4728 struct dwarf2_per_objfile *dwarf2_per_objfile
4729 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4730
4731 if (!dwarf2_per_objfile->filenames_cache)
4732 {
4733 dwarf2_per_objfile->filenames_cache.emplace ();
4734
4735 htab_up visited (htab_create_alloc (10,
4736 htab_hash_pointer, htab_eq_pointer,
4737 NULL, xcalloc, xfree));
4738
4739 /* The rule is CUs specify all the files, including those used
4740 by any TU, so there's no need to scan TUs here. We can
4741 ignore file names coming from already-expanded CUs. */
4742
b76e467d 4743 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4744 {
927aa2e7
JK
4745 if (per_cu->v.quick->compunit_symtab)
4746 {
4747 void **slot = htab_find_slot (visited.get (),
4748 per_cu->v.quick->file_names,
4749 INSERT);
4750
4751 *slot = per_cu->v.quick->file_names;
4752 }
4753 }
4754
b76e467d 4755 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4756 {
927aa2e7
JK
4757 /* We only need to look at symtabs not already expanded. */
4758 if (per_cu->v.quick->compunit_symtab)
4759 continue;
4760
b76e467d 4761 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4762 if (file_data == NULL)
4763 continue;
4764
b76e467d 4765 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4766 if (*slot)
4767 {
4768 /* Already visited. */
4769 continue;
4770 }
4771 *slot = file_data;
4772
4773 for (int j = 0; j < file_data->num_file_names; ++j)
4774 {
4775 const char *filename = file_data->file_names[j];
4776 dwarf2_per_objfile->filenames_cache->seen (filename);
4777 }
4778 }
4779 }
4780
4781 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4782 {
4783 gdb::unique_xmalloc_ptr<char> this_real_name;
4784
4785 if (need_fullname)
4786 this_real_name = gdb_realpath (filename);
4787 (*fun) (filename, this_real_name.get (), data);
4788 });
4789}
4790
4791static int
4792dw2_has_symbols (struct objfile *objfile)
4793{
4794 return 1;
4795}
4796
4797const struct quick_symbol_functions dwarf2_gdb_index_functions =
4798{
4799 dw2_has_symbols,
4800 dw2_find_last_source_symtab,
4801 dw2_forget_cached_source_info,
4802 dw2_map_symtabs_matching_filename,
4803 dw2_lookup_symbol,
4804 dw2_print_stats,
4805 dw2_dump,
927aa2e7
JK
4806 dw2_expand_symtabs_for_function,
4807 dw2_expand_all_symtabs,
4808 dw2_expand_symtabs_with_fullname,
4809 dw2_map_matching_symbols,
4810 dw2_expand_symtabs_matching,
4811 dw2_find_pc_sect_compunit_symtab,
4812 NULL,
4813 dw2_map_symbol_filenames
4814};
4815
4816/* DWARF-5 debug_names reader. */
4817
4818/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4819static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4820
4821/* A helper function that reads the .debug_names section in SECTION
4822 and fills in MAP. FILENAME is the name of the file containing the
4823 section; it is used for error reporting.
4824
4825 Returns true if all went well, false otherwise. */
4826
4827static bool
4828read_debug_names_from_section (struct objfile *objfile,
4829 const char *filename,
4830 struct dwarf2_section_info *section,
4831 mapped_debug_names &map)
4832{
96b79293 4833 if (section->empty ())
927aa2e7
JK
4834 return false;
4835
4836 /* Older elfutils strip versions could keep the section in the main
4837 executable while splitting it for the separate debug info file. */
96b79293 4838 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4839 return false;
4840
96b79293 4841 section->read (objfile);
927aa2e7
JK
4842
4843 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4844
4845 const gdb_byte *addr = section->buffer;
4846
96b79293 4847 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4848
4849 unsigned int bytes_read;
4850 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4851 addr += bytes_read;
4852
4853 map.dwarf5_is_dwarf64 = bytes_read != 4;
4854 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4855 if (bytes_read + length != section->size)
4856 {
4857 /* There may be multiple per-CU indices. */
4858 warning (_("Section .debug_names in %s length %s does not match "
4859 "section length %s, ignoring .debug_names."),
4860 filename, plongest (bytes_read + length),
4861 pulongest (section->size));
4862 return false;
4863 }
4864
4865 /* The version number. */
4866 uint16_t version = read_2_bytes (abfd, addr);
4867 addr += 2;
4868 if (version != 5)
4869 {
4870 warning (_("Section .debug_names in %s has unsupported version %d, "
4871 "ignoring .debug_names."),
4872 filename, version);
4873 return false;
4874 }
4875
4876 /* Padding. */
4877 uint16_t padding = read_2_bytes (abfd, addr);
4878 addr += 2;
4879 if (padding != 0)
4880 {
4881 warning (_("Section .debug_names in %s has unsupported padding %d, "
4882 "ignoring .debug_names."),
4883 filename, padding);
4884 return false;
4885 }
4886
4887 /* comp_unit_count - The number of CUs in the CU list. */
4888 map.cu_count = read_4_bytes (abfd, addr);
4889 addr += 4;
4890
4891 /* local_type_unit_count - The number of TUs in the local TU
4892 list. */
4893 map.tu_count = read_4_bytes (abfd, addr);
4894 addr += 4;
4895
4896 /* foreign_type_unit_count - The number of TUs in the foreign TU
4897 list. */
4898 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4899 addr += 4;
4900 if (foreign_tu_count != 0)
4901 {
4902 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4903 "ignoring .debug_names."),
4904 filename, static_cast<unsigned long> (foreign_tu_count));
4905 return false;
4906 }
4907
4908 /* bucket_count - The number of hash buckets in the hash lookup
4909 table. */
4910 map.bucket_count = read_4_bytes (abfd, addr);
4911 addr += 4;
4912
4913 /* name_count - The number of unique names in the index. */
4914 map.name_count = read_4_bytes (abfd, addr);
4915 addr += 4;
4916
4917 /* abbrev_table_size - The size in bytes of the abbreviations
4918 table. */
4919 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4920 addr += 4;
4921
4922 /* augmentation_string_size - The size in bytes of the augmentation
4923 string. This value is rounded up to a multiple of 4. */
4924 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4925 addr += 4;
4926 map.augmentation_is_gdb = ((augmentation_string_size
4927 == sizeof (dwarf5_augmentation))
4928 && memcmp (addr, dwarf5_augmentation,
4929 sizeof (dwarf5_augmentation)) == 0);
4930 augmentation_string_size += (-augmentation_string_size) & 3;
4931 addr += augmentation_string_size;
4932
4933 /* List of CUs */
4934 map.cu_table_reordered = addr;
4935 addr += map.cu_count * map.offset_size;
4936
4937 /* List of Local TUs */
4938 map.tu_table_reordered = addr;
4939 addr += map.tu_count * map.offset_size;
4940
4941 /* Hash Lookup Table */
4942 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4943 addr += map.bucket_count * 4;
4944 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4945 addr += map.name_count * 4;
4946
4947 /* Name Table */
4948 map.name_table_string_offs_reordered = addr;
4949 addr += map.name_count * map.offset_size;
4950 map.name_table_entry_offs_reordered = addr;
4951 addr += map.name_count * map.offset_size;
4952
4953 const gdb_byte *abbrev_table_start = addr;
4954 for (;;)
4955 {
927aa2e7
JK
4956 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4957 addr += bytes_read;
4958 if (index_num == 0)
4959 break;
4960
4961 const auto insertpair
4962 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4963 if (!insertpair.second)
4964 {
4965 warning (_("Section .debug_names in %s has duplicate index %s, "
4966 "ignoring .debug_names."),
4967 filename, pulongest (index_num));
4968 return false;
4969 }
4970 mapped_debug_names::index_val &indexval = insertpair.first->second;
4971 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4972 addr += bytes_read;
4973
4974 for (;;)
4975 {
4976 mapped_debug_names::index_val::attr attr;
4977 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4978 addr += bytes_read;
4979 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4980 addr += bytes_read;
4981 if (attr.form == DW_FORM_implicit_const)
4982 {
4983 attr.implicit_const = read_signed_leb128 (abfd, addr,
4984 &bytes_read);
4985 addr += bytes_read;
4986 }
4987 if (attr.dw_idx == 0 && attr.form == 0)
4988 break;
4989 indexval.attr_vec.push_back (std::move (attr));
4990 }
4991 }
4992 if (addr != abbrev_table_start + abbrev_table_size)
4993 {
4994 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4995 "of size %s vs. written as %u, ignoring .debug_names."),
4996 filename, plongest (addr - abbrev_table_start),
4997 abbrev_table_size);
927aa2e7
JK
4998 return false;
4999 }
5000 map.entry_pool = addr;
5001
5002 return true;
5003}
5004
5005/* A helper for create_cus_from_debug_names that handles the MAP's CU
5006 list. */
5007
5008static void
ed2dc618 5009create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5010 const mapped_debug_names &map,
5011 dwarf2_section_info &section,
b76e467d 5012 bool is_dwz)
927aa2e7
JK
5013{
5014 sect_offset sect_off_prev;
5015 for (uint32_t i = 0; i <= map.cu_count; ++i)
5016 {
5017 sect_offset sect_off_next;
5018 if (i < map.cu_count)
5019 {
5020 sect_off_next
5021 = (sect_offset) (extract_unsigned_integer
5022 (map.cu_table_reordered + i * map.offset_size,
5023 map.offset_size,
5024 map.dwarf5_byte_order));
5025 }
5026 else
5027 sect_off_next = (sect_offset) section.size;
5028 if (i >= 1)
5029 {
5030 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5031 dwarf2_per_cu_data *per_cu
ed2dc618 5032 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5033 sect_off_prev, length);
b76e467d 5034 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5035 }
5036 sect_off_prev = sect_off_next;
5037 }
5038}
5039
5040/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5041 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5042
5043static void
ed2dc618 5044create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5045 const mapped_debug_names &map,
5046 const mapped_debug_names &dwz_map)
5047{
b76e467d
SM
5048 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5049 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5050
ed2dc618
SM
5051 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5052 dwarf2_per_objfile->info,
b76e467d 5053 false /* is_dwz */);
927aa2e7
JK
5054
5055 if (dwz_map.cu_count == 0)
5056 return;
5057
ed2dc618
SM
5058 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5059 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5060 true /* is_dwz */);
927aa2e7
JK
5061}
5062
5063/* Read .debug_names. If everything went ok, initialize the "quick"
5064 elements of all the CUs and return true. Otherwise, return false. */
5065
5066static bool
ed2dc618 5067dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5068{
22ca247e
TT
5069 std::unique_ptr<mapped_debug_names> map
5070 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5071 mapped_debug_names dwz_map (dwarf2_per_objfile);
5072 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5073
5074 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5075 &dwarf2_per_objfile->debug_names,
22ca247e 5076 *map))
927aa2e7
JK
5077 return false;
5078
5079 /* Don't use the index if it's empty. */
22ca247e 5080 if (map->name_count == 0)
927aa2e7
JK
5081 return false;
5082
5083 /* If there is a .dwz file, read it so we can get its CU list as
5084 well. */
ed2dc618 5085 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5086 if (dwz != NULL)
5087 {
5088 if (!read_debug_names_from_section (objfile,
00f93c44 5089 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5090 &dwz->debug_names, dwz_map))
5091 {
5092 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5093 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5094 return false;
5095 }
5096 }
5097
22ca247e 5098 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5099
22ca247e 5100 if (map->tu_count != 0)
927aa2e7
JK
5101 {
5102 /* We can only handle a single .debug_types when we have an
5103 index. */
fd5866f6 5104 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5105 return false;
5106
fd5866f6 5107 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5108
5109 create_signatured_type_table_from_debug_names
22ca247e 5110 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5111 }
5112
ed2dc618
SM
5113 create_addrmap_from_aranges (dwarf2_per_objfile,
5114 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5115
22ca247e 5116 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5117 dwarf2_per_objfile->using_index = 1;
5118 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5119 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5120
5121 return true;
5122}
5123
927aa2e7
JK
5124/* Type used to manage iterating over all CUs looking for a symbol for
5125 .debug_names. */
5126
5127class dw2_debug_names_iterator
5128{
5129public:
927aa2e7 5130 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5131 gdb::optional<block_enum> block_index,
5132 domain_enum domain,
927aa2e7 5133 const char *name)
2b79f376 5134 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5135 m_addr (find_vec_in_debug_names (map, name))
5136 {}
5137
5138 dw2_debug_names_iterator (const mapped_debug_names &map,
5139 search_domain search, uint32_t namei)
5140 : m_map (map),
5141 m_search (search),
5142 m_addr (find_vec_in_debug_names (map, namei))
5143 {}
5144
3b00ef10
TT
5145 dw2_debug_names_iterator (const mapped_debug_names &map,
5146 block_enum block_index, domain_enum domain,
5147 uint32_t namei)
5148 : m_map (map), m_block_index (block_index), m_domain (domain),
5149 m_addr (find_vec_in_debug_names (map, namei))
5150 {}
5151
927aa2e7
JK
5152 /* Return the next matching CU or NULL if there are no more. */
5153 dwarf2_per_cu_data *next ();
5154
5155private:
5156 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5157 const char *name);
5158 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5159 uint32_t namei);
5160
5161 /* The internalized form of .debug_names. */
5162 const mapped_debug_names &m_map;
5163
2b79f376
SM
5164 /* If set, only look for symbols that match that block. Valid values are
5165 GLOBAL_BLOCK and STATIC_BLOCK. */
5166 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5167
5168 /* The kind of symbol we're looking for. */
5169 const domain_enum m_domain = UNDEF_DOMAIN;
5170 const search_domain m_search = ALL_DOMAIN;
5171
5172 /* The list of CUs from the index entry of the symbol, or NULL if
5173 not found. */
5174 const gdb_byte *m_addr;
5175};
5176
5177const char *
5178mapped_debug_names::namei_to_name (uint32_t namei) const
5179{
5180 const ULONGEST namei_string_offs
5181 = extract_unsigned_integer ((name_table_string_offs_reordered
5182 + namei * offset_size),
5183 offset_size,
5184 dwarf5_byte_order);
5185 return read_indirect_string_at_offset
ed2dc618 5186 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5187}
5188
5189/* Find a slot in .debug_names for the object named NAME. If NAME is
5190 found, return pointer to its pool data. If NAME cannot be found,
5191 return NULL. */
5192
5193const gdb_byte *
5194dw2_debug_names_iterator::find_vec_in_debug_names
5195 (const mapped_debug_names &map, const char *name)
5196{
5197 int (*cmp) (const char *, const char *);
5198
54ee4252 5199 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5200 if (current_language->la_language == language_cplus
5201 || current_language->la_language == language_fortran
5202 || current_language->la_language == language_d)
5203 {
5204 /* NAME is already canonical. Drop any qualifiers as
5205 .debug_names does not contain any. */
5206
5207 if (strchr (name, '(') != NULL)
5208 {
54ee4252 5209 without_params = cp_remove_params (name);
927aa2e7 5210 if (without_params != NULL)
54ee4252 5211 name = without_params.get ();
927aa2e7
JK
5212 }
5213 }
5214
5215 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5216
5217 const uint32_t full_hash = dwarf5_djb_hash (name);
5218 uint32_t namei
5219 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5220 (map.bucket_table_reordered
5221 + (full_hash % map.bucket_count)), 4,
5222 map.dwarf5_byte_order);
5223 if (namei == 0)
5224 return NULL;
5225 --namei;
5226 if (namei >= map.name_count)
5227 {
b98664d3 5228 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5229 "[in module %s]"),
5230 namei, map.name_count,
ed2dc618 5231 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5232 return NULL;
5233 }
5234
5235 for (;;)
5236 {
5237 const uint32_t namei_full_hash
5238 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5239 (map.hash_table_reordered + namei), 4,
5240 map.dwarf5_byte_order);
5241 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5242 return NULL;
5243
5244 if (full_hash == namei_full_hash)
5245 {
5246 const char *const namei_string = map.namei_to_name (namei);
5247
5248#if 0 /* An expensive sanity check. */
5249 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5250 {
b98664d3 5251 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5252 "[in module %s]"),
5253 namei, objfile_name (dwarf2_per_objfile->objfile));
5254 return NULL;
5255 }
5256#endif
5257
5258 if (cmp (namei_string, name) == 0)
5259 {
5260 const ULONGEST namei_entry_offs
5261 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5262 + namei * map.offset_size),
5263 map.offset_size, map.dwarf5_byte_order);
5264 return map.entry_pool + namei_entry_offs;
5265 }
5266 }
5267
5268 ++namei;
5269 if (namei >= map.name_count)
5270 return NULL;
5271 }
5272}
5273
5274const gdb_byte *
5275dw2_debug_names_iterator::find_vec_in_debug_names
5276 (const mapped_debug_names &map, uint32_t namei)
5277{
5278 if (namei >= map.name_count)
5279 {
b98664d3 5280 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5281 "[in module %s]"),
5282 namei, map.name_count,
ed2dc618 5283 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5284 return NULL;
5285 }
5286
5287 const ULONGEST namei_entry_offs
5288 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5289 + namei * map.offset_size),
5290 map.offset_size, map.dwarf5_byte_order);
5291 return map.entry_pool + namei_entry_offs;
5292}
5293
5294/* See dw2_debug_names_iterator. */
5295
5296dwarf2_per_cu_data *
5297dw2_debug_names_iterator::next ()
5298{
5299 if (m_addr == NULL)
5300 return NULL;
5301
ed2dc618
SM
5302 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5303 struct objfile *objfile = dwarf2_per_objfile->objfile;
5304 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5305
5306 again:
5307
5308 unsigned int bytes_read;
5309 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5310 m_addr += bytes_read;
5311 if (abbrev == 0)
5312 return NULL;
5313
5314 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5315 if (indexval_it == m_map.abbrev_map.cend ())
5316 {
b98664d3 5317 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5318 "[in module %s]"),
ed2dc618 5319 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5320 return NULL;
5321 }
5322 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5323 enum class symbol_linkage {
5324 unknown,
5325 static_,
5326 extern_,
23c13d42 5327 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5328 dwarf2_per_cu_data *per_cu = NULL;
5329 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5330 {
5331 ULONGEST ull;
5332 switch (attr.form)
5333 {
5334 case DW_FORM_implicit_const:
5335 ull = attr.implicit_const;
5336 break;
5337 case DW_FORM_flag_present:
5338 ull = 1;
5339 break;
5340 case DW_FORM_udata:
5341 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5342 m_addr += bytes_read;
5343 break;
5344 default:
b98664d3 5345 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5346 dwarf_form_name (attr.form),
ed2dc618 5347 objfile_name (objfile));
927aa2e7
JK
5348 return NULL;
5349 }
5350 switch (attr.dw_idx)
5351 {
5352 case DW_IDX_compile_unit:
5353 /* Don't crash on bad data. */
b76e467d 5354 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5355 {
b98664d3 5356 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5357 " [in module %s]"),
5358 pulongest (ull),
5359 objfile_name (dwarf2_per_objfile->objfile));
5360 continue;
5361 }
ff4c9fec 5362 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5363 break;
8af5c486
JK
5364 case DW_IDX_type_unit:
5365 /* Don't crash on bad data. */
b2bdb8cf 5366 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5367 {
b98664d3 5368 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5369 " [in module %s]"),
5370 pulongest (ull),
5371 objfile_name (dwarf2_per_objfile->objfile));
5372 continue;
5373 }
ff4c9fec 5374 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5375 break;
927aa2e7
JK
5376 case DW_IDX_GNU_internal:
5377 if (!m_map.augmentation_is_gdb)
5378 break;
23c13d42 5379 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5380 break;
5381 case DW_IDX_GNU_external:
5382 if (!m_map.augmentation_is_gdb)
5383 break;
23c13d42 5384 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5385 break;
5386 }
5387 }
5388
5389 /* Skip if already read in. */
5390 if (per_cu->v.quick->compunit_symtab)
5391 goto again;
5392
5393 /* Check static vs global. */
23c13d42 5394 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5395 {
2b79f376 5396 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5397 const bool symbol_is_static =
5398 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5399 if (want_static != symbol_is_static)
2b79f376 5400 goto again;
927aa2e7
JK
5401 }
5402
5403 /* Match dw2_symtab_iter_next, symbol_kind
5404 and debug_names::psymbol_tag. */
5405 switch (m_domain)
5406 {
5407 case VAR_DOMAIN:
5408 switch (indexval.dwarf_tag)
5409 {
5410 case DW_TAG_variable:
5411 case DW_TAG_subprogram:
5412 /* Some types are also in VAR_DOMAIN. */
5413 case DW_TAG_typedef:
5414 case DW_TAG_structure_type:
5415 break;
5416 default:
5417 goto again;
5418 }
5419 break;
5420 case STRUCT_DOMAIN:
5421 switch (indexval.dwarf_tag)
5422 {
5423 case DW_TAG_typedef:
5424 case DW_TAG_structure_type:
5425 break;
5426 default:
5427 goto again;
5428 }
5429 break;
5430 case LABEL_DOMAIN:
5431 switch (indexval.dwarf_tag)
5432 {
5433 case 0:
5434 case DW_TAG_variable:
5435 break;
5436 default:
5437 goto again;
5438 }
5439 break;
59c35742
AB
5440 case MODULE_DOMAIN:
5441 switch (indexval.dwarf_tag)
5442 {
5443 case DW_TAG_module:
5444 break;
5445 default:
5446 goto again;
5447 }
5448 break;
927aa2e7
JK
5449 default:
5450 break;
5451 }
5452
5453 /* Match dw2_expand_symtabs_matching, symbol_kind and
5454 debug_names::psymbol_tag. */
5455 switch (m_search)
4b514bc8 5456 {
927aa2e7
JK
5457 case VARIABLES_DOMAIN:
5458 switch (indexval.dwarf_tag)
4b514bc8 5459 {
927aa2e7
JK
5460 case DW_TAG_variable:
5461 break;
5462 default:
5463 goto again;
4b514bc8 5464 }
927aa2e7
JK
5465 break;
5466 case FUNCTIONS_DOMAIN:
5467 switch (indexval.dwarf_tag)
4b514bc8 5468 {
927aa2e7
JK
5469 case DW_TAG_subprogram:
5470 break;
5471 default:
5472 goto again;
4b514bc8 5473 }
927aa2e7
JK
5474 break;
5475 case TYPES_DOMAIN:
5476 switch (indexval.dwarf_tag)
5477 {
5478 case DW_TAG_typedef:
5479 case DW_TAG_structure_type:
5480 break;
5481 default:
5482 goto again;
5483 }
5484 break;
59c35742
AB
5485 case MODULES_DOMAIN:
5486 switch (indexval.dwarf_tag)
5487 {
5488 case DW_TAG_module:
5489 break;
5490 default:
5491 goto again;
5492 }
927aa2e7
JK
5493 default:
5494 break;
4b514bc8 5495 }
927aa2e7
JK
5496
5497 return per_cu;
4b514bc8 5498}
61920122 5499
927aa2e7 5500static struct compunit_symtab *
c7f839cb 5501dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5502 const char *name, domain_enum domain)
4b514bc8 5503{
ed2dc618
SM
5504 struct dwarf2_per_objfile *dwarf2_per_objfile
5505 = get_dwarf2_per_objfile (objfile);
61920122 5506
927aa2e7
JK
5507 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5508 if (!mapp)
61920122 5509 {
927aa2e7
JK
5510 /* index is NULL if OBJF_READNOW. */
5511 return NULL;
5512 }
5513 const auto &map = *mapp;
9291a0cd 5514
2b79f376 5515 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5516
927aa2e7
JK
5517 struct compunit_symtab *stab_best = NULL;
5518 struct dwarf2_per_cu_data *per_cu;
5519 while ((per_cu = iter.next ()) != NULL)
5520 {
5521 struct symbol *sym, *with_opaque = NULL;
58f0c718 5522 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5523 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5524 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5525
927aa2e7
JK
5526 sym = block_find_symbol (block, name, domain,
5527 block_find_non_opaque_type_preferred,
5528 &with_opaque);
9703b513 5529
927aa2e7
JK
5530 /* Some caution must be observed with overloaded functions and
5531 methods, since the index will not contain any overload
5532 information (but NAME might contain it). */
a3ec0bb1 5533
927aa2e7 5534 if (sym != NULL
987012b8 5535 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5536 return stab;
5537 if (with_opaque != NULL
987012b8 5538 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5539 stab_best = stab;
9703b513 5540
927aa2e7 5541 /* Keep looking through other CUs. */
9703b513
TT
5542 }
5543
927aa2e7 5544 return stab_best;
9703b513
TT
5545}
5546
927aa2e7
JK
5547/* This dumps minimal information about .debug_names. It is called
5548 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5549 uses this to verify that .debug_names has been loaded. */
9291a0cd 5550
927aa2e7
JK
5551static void
5552dw2_debug_names_dump (struct objfile *objfile)
5553{
ed2dc618
SM
5554 struct dwarf2_per_objfile *dwarf2_per_objfile
5555 = get_dwarf2_per_objfile (objfile);
5556
927aa2e7
JK
5557 gdb_assert (dwarf2_per_objfile->using_index);
5558 printf_filtered (".debug_names:");
5559 if (dwarf2_per_objfile->debug_names_table)
5560 printf_filtered (" exists\n");
5561 else
5562 printf_filtered (" faked for \"readnow\"\n");
5563 printf_filtered ("\n");
9291a0cd
TT
5564}
5565
9291a0cd 5566static void
927aa2e7
JK
5567dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5568 const char *func_name)
9291a0cd 5569{
ed2dc618
SM
5570 struct dwarf2_per_objfile *dwarf2_per_objfile
5571 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5572
927aa2e7
JK
5573 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5574 if (dwarf2_per_objfile->debug_names_table)
24c79950 5575 {
927aa2e7 5576 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5577
2b79f376 5578 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5579
927aa2e7
JK
5580 struct dwarf2_per_cu_data *per_cu;
5581 while ((per_cu = iter.next ()) != NULL)
58f0c718 5582 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5583 }
5584}
24c79950 5585
3b00ef10
TT
5586static void
5587dw2_debug_names_map_matching_symbols
5588 (struct objfile *objfile,
5589 const lookup_name_info &name, domain_enum domain,
5590 int global,
5591 gdb::function_view<symbol_found_callback_ftype> callback,
5592 symbol_compare_ftype *ordered_compare)
5593{
5594 struct dwarf2_per_objfile *dwarf2_per_objfile
5595 = get_dwarf2_per_objfile (objfile);
5596
5597 /* debug_names_table is NULL if OBJF_READNOW. */
5598 if (!dwarf2_per_objfile->debug_names_table)
5599 return;
5600
5601 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5602 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5603
5604 const char *match_name = name.ada ().lookup_name ().c_str ();
5605 auto matcher = [&] (const char *symname)
5606 {
5607 if (ordered_compare == nullptr)
5608 return true;
5609 return ordered_compare (symname, match_name) == 0;
5610 };
5611
5612 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5613 [&] (offset_type namei)
5614 {
5615 /* The name was matched, now expand corresponding CUs that were
5616 marked. */
5617 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5618
5619 struct dwarf2_per_cu_data *per_cu;
5620 while ((per_cu = iter.next ()) != NULL)
5621 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5622 return true;
5623 });
5624
5625 /* It's a shame we couldn't do this inside the
5626 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5627 that have already been expanded. Instead, this loop matches what
5628 the psymtab code does. */
5629 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5630 {
5631 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5632 if (cust != nullptr)
5633 {
5634 const struct block *block
5635 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5636 if (!iterate_over_symbols_terminated (block, name,
5637 domain, callback))
5638 break;
5639 }
5640 }
5641}
5642
927aa2e7
JK
5643static void
5644dw2_debug_names_expand_symtabs_matching
5645 (struct objfile *objfile,
5646 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5647 const lookup_name_info &lookup_name,
5648 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5649 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5650 enum search_domain kind)
5651{
ed2dc618
SM
5652 struct dwarf2_per_objfile *dwarf2_per_objfile
5653 = get_dwarf2_per_objfile (objfile);
9291a0cd 5654
927aa2e7
JK
5655 /* debug_names_table is NULL if OBJF_READNOW. */
5656 if (!dwarf2_per_objfile->debug_names_table)
5657 return;
9291a0cd 5658
ed2dc618 5659 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5660
44ed8f3e 5661 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5662
44ed8f3e
PA
5663 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5664 symbol_matcher,
5665 kind, [&] (offset_type namei)
927aa2e7 5666 {
927aa2e7
JK
5667 /* The name was matched, now expand corresponding CUs that were
5668 marked. */
5669 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5670
927aa2e7
JK
5671 struct dwarf2_per_cu_data *per_cu;
5672 while ((per_cu = iter.next ()) != NULL)
5673 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5674 expansion_notify);
3b00ef10 5675 return true;
44ed8f3e 5676 });
9291a0cd
TT
5677}
5678
927aa2e7 5679const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5680{
5681 dw2_has_symbols,
5682 dw2_find_last_source_symtab,
5683 dw2_forget_cached_source_info,
f8eba3c6 5684 dw2_map_symtabs_matching_filename,
927aa2e7 5685 dw2_debug_names_lookup_symbol,
9291a0cd 5686 dw2_print_stats,
927aa2e7 5687 dw2_debug_names_dump,
927aa2e7 5688 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5689 dw2_expand_all_symtabs,
652a8996 5690 dw2_expand_symtabs_with_fullname,
3b00ef10 5691 dw2_debug_names_map_matching_symbols,
927aa2e7 5692 dw2_debug_names_expand_symtabs_matching,
43f3e411 5693 dw2_find_pc_sect_compunit_symtab,
71a3c369 5694 NULL,
9291a0cd
TT
5695 dw2_map_symbol_filenames
5696};
5697
4485a1c1
SM
5698/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5699 to either a dwarf2_per_objfile or dwz_file object. */
5700
5701template <typename T>
5702static gdb::array_view<const gdb_byte>
5703get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5704{
5705 dwarf2_section_info *section = &section_owner->gdb_index;
5706
96b79293 5707 if (section->empty ())
4485a1c1
SM
5708 return {};
5709
5710 /* Older elfutils strip versions could keep the section in the main
5711 executable while splitting it for the separate debug info file. */
96b79293 5712 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5713 return {};
5714
96b79293 5715 section->read (obj);
4485a1c1 5716
8bebfcda
PA
5717 /* dwarf2_section_info::size is a bfd_size_type, while
5718 gdb::array_view works with size_t. On 32-bit hosts, with
5719 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5720 is 32-bit. So we need an explicit narrowing conversion here.
5721 This is fine, because it's impossible to allocate or mmap an
5722 array/buffer larger than what size_t can represent. */
5723 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5724}
5725
87d6a7aa
SM
5726/* Lookup the index cache for the contents of the index associated to
5727 DWARF2_OBJ. */
5728
5729static gdb::array_view<const gdb_byte>
5730get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5731{
5732 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5733 if (build_id == nullptr)
5734 return {};
5735
5736 return global_index_cache.lookup_gdb_index (build_id,
5737 &dwarf2_obj->index_cache_res);
5738}
5739
5740/* Same as the above, but for DWZ. */
5741
5742static gdb::array_view<const gdb_byte>
5743get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5744{
5745 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5746 if (build_id == nullptr)
5747 return {};
5748
5749 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5750}
5751
3c0aa29a 5752/* See symfile.h. */
9291a0cd 5753
3c0aa29a
PA
5754bool
5755dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5756{
ed2dc618
SM
5757 struct dwarf2_per_objfile *dwarf2_per_objfile
5758 = get_dwarf2_per_objfile (objfile);
5759
9291a0cd
TT
5760 /* If we're about to read full symbols, don't bother with the
5761 indices. In this case we also don't care if some other debug
5762 format is making psymtabs, because they are all about to be
5763 expanded anyway. */
5764 if ((objfile->flags & OBJF_READNOW))
5765 {
9291a0cd 5766 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5767 create_all_comp_units (dwarf2_per_objfile);
5768 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5769 dwarf2_per_objfile->quick_file_names_table
5770 = create_quick_file_names_table
5771 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5772
b76e467d 5773 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5774 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5775 {
ff4c9fec 5776 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5777
e254ef6a
DE
5778 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5779 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5780 }
5781
5782 /* Return 1 so that gdb sees the "quick" functions. However,
5783 these functions will be no-ops because we will have expanded
5784 all symtabs. */
3c0aa29a
PA
5785 *index_kind = dw_index_kind::GDB_INDEX;
5786 return true;
9291a0cd
TT
5787 }
5788
ed2dc618 5789 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5790 {
5791 *index_kind = dw_index_kind::DEBUG_NAMES;
5792 return true;
5793 }
927aa2e7 5794
4485a1c1
SM
5795 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5796 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5797 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5798 {
5799 *index_kind = dw_index_kind::GDB_INDEX;
5800 return true;
5801 }
9291a0cd 5802
87d6a7aa
SM
5803 /* ... otherwise, try to find the index in the index cache. */
5804 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5805 get_gdb_index_contents_from_cache,
5806 get_gdb_index_contents_from_cache_dwz))
5807 {
5808 global_index_cache.hit ();
5809 *index_kind = dw_index_kind::GDB_INDEX;
5810 return true;
5811 }
5812
5813 global_index_cache.miss ();
3c0aa29a 5814 return false;
9291a0cd
TT
5815}
5816
5817\f
5818
dce234bc
PP
5819/* Build a partial symbol table. */
5820
5821void
f29dff0a 5822dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5823{
ed2dc618
SM
5824 struct dwarf2_per_objfile *dwarf2_per_objfile
5825 = get_dwarf2_per_objfile (objfile);
c9bf0622 5826
6eee24ce 5827 init_psymbol_list (objfile, 1024);
c906108c 5828
a70b8144 5829 try
c9bf0622
TT
5830 {
5831 /* This isn't really ideal: all the data we allocate on the
5832 objfile's obstack is still uselessly kept around. However,
5833 freeing it seems unsafe. */
906768f9 5834 psymtab_discarder psymtabs (objfile);
ed2dc618 5835 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5836 psymtabs.keep ();
87d6a7aa
SM
5837
5838 /* (maybe) store an index in the cache. */
5839 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5840 }
230d2906 5841 catch (const gdb_exception_error &except)
492d29ea
PA
5842 {
5843 exception_print (gdb_stderr, except);
5844 }
c906108c 5845}
c906108c 5846
3b80fe9b
DE
5847/* Find the base address of the compilation unit for range lists and
5848 location lists. It will normally be specified by DW_AT_low_pc.
5849 In DWARF-3 draft 4, the base address could be overridden by
5850 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5851 compilation units with discontinuous ranges. */
5852
5853static void
5854dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5855{
5856 struct attribute *attr;
5857
5858 cu->base_known = 0;
5859 cu->base_address = 0;
5860
5861 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5862 if (attr != nullptr)
3b80fe9b 5863 {
cd6c91b4 5864 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5865 cu->base_known = 1;
5866 }
5867 else
5868 {
5869 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5870 if (attr != nullptr)
3b80fe9b 5871 {
cd6c91b4 5872 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5873 cu->base_known = 1;
5874 }
5875 }
5876}
5877
36586728
TT
5878/* Helper function that returns the proper abbrev section for
5879 THIS_CU. */
5880
5881static struct dwarf2_section_info *
5882get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5883{
5884 struct dwarf2_section_info *abbrev;
ed2dc618 5885 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5886
5887 if (this_cu->is_dwz)
ed2dc618 5888 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5889 else
5890 abbrev = &dwarf2_per_objfile->abbrev;
5891
5892 return abbrev;
5893}
5894
f4dc4d17
DE
5895/* Fetch the abbreviation table offset from a comp or type unit header. */
5896
5897static sect_offset
ed2dc618
SM
5898read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5899 struct dwarf2_section_info *section,
9c541725 5900 sect_offset sect_off)
f4dc4d17 5901{
96b79293 5902 bfd *abfd = section->get_bfd_owner ();
d521ce57 5903 const gdb_byte *info_ptr;
ac298888 5904 unsigned int initial_length_size, offset_size;
43988095 5905 uint16_t version;
f4dc4d17 5906
96b79293 5907 section->read (dwarf2_per_objfile->objfile);
9c541725 5908 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5909 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5910 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5911 info_ptr += initial_length_size;
5912
5913 version = read_2_bytes (abfd, info_ptr);
5914 info_ptr += 2;
5915 if (version >= 5)
5916 {
5917 /* Skip unit type and address size. */
5918 info_ptr += 2;
5919 }
5920
24aa364d 5921 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5922}
5923
b83470bf
TT
5924/* A partial symtab that is used only for include files. */
5925struct dwarf2_include_psymtab : public partial_symtab
5926{
5927 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5928 : partial_symtab (filename, objfile)
5929 {
5930 }
5931
5932 void read_symtab (struct objfile *objfile) override
5933 {
5934 expand_psymtab (objfile);
5935 }
5936
5937 void expand_psymtab (struct objfile *objfile) override
5938 {
5939 if (m_readin)
5940 return;
5941 /* It's an include file, no symbols to read for it.
5942 Everything is in the parent symtab. */
5943 read_dependencies (objfile);
5944 m_readin = true;
5945 }
5946
5947 bool readin_p () const override
5948 {
5949 return m_readin;
5950 }
5951
5952 struct compunit_symtab *get_compunit_symtab () const override
5953 {
5954 return nullptr;
5955 }
5956
5957private:
5958
5959 bool m_readin = false;
5960};
5961
aaa75496
JB
5962/* Allocate a new partial symtab for file named NAME and mark this new
5963 partial symtab as being an include of PST. */
5964
5965static void
891813be 5966dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5967 struct objfile *objfile)
5968{
b83470bf 5969 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 5970
fbd9ab74
JK
5971 if (!IS_ABSOLUTE_PATH (subpst->filename))
5972 {
5973 /* It shares objfile->objfile_obstack. */
5974 subpst->dirname = pst->dirname;
5975 }
5976
a9342b62 5977 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5978 subpst->dependencies[0] = pst;
5979 subpst->number_of_dependencies = 1;
aaa75496
JB
5980}
5981
5982/* Read the Line Number Program data and extract the list of files
5983 included by the source file represented by PST. Build an include
d85a05f0 5984 partial symtab for each of these included files. */
aaa75496
JB
5985
5986static void
5987dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5988 struct die_info *die,
891813be 5989 dwarf2_psymtab *pst)
aaa75496 5990{
fff8551c 5991 line_header_up lh;
d85a05f0 5992 struct attribute *attr;
aaa75496 5993
d85a05f0 5994 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5995 if (attr != nullptr)
9c541725 5996 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5997 if (lh == NULL)
5998 return; /* No linetable, so no includes. */
5999
79748972
TT
6000 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6001 that we pass in the raw text_low here; that is ok because we're
6002 only decoding the line table to make include partial symtabs, and
6003 so the addresses aren't really used. */
4ae976d1 6004 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6005 pst->raw_text_low (), 1);
aaa75496
JB
6006}
6007
348e048f 6008static hashval_t
52dc124a 6009hash_signatured_type (const void *item)
348e048f 6010{
9a3c8263
SM
6011 const struct signatured_type *sig_type
6012 = (const struct signatured_type *) item;
9a619af0 6013
348e048f 6014 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6015 return sig_type->signature;
348e048f
DE
6016}
6017
6018static int
52dc124a 6019eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6020{
9a3c8263
SM
6021 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6022 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6023
348e048f
DE
6024 return lhs->signature == rhs->signature;
6025}
6026
1fd400ff
TT
6027/* Allocate a hash table for signatured types. */
6028
b0b6a987 6029static htab_up
298e9637 6030allocate_signatured_type_table ()
1fd400ff 6031{
b0b6a987
TT
6032 return htab_up (htab_create_alloc (41,
6033 hash_signatured_type,
6034 eq_signatured_type,
6035 NULL, xcalloc, xfree));
1fd400ff
TT
6036}
6037
d467dd73 6038/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6039
6040static int
d467dd73 6041add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6042{
9a3c8263 6043 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6044 std::vector<signatured_type *> *all_type_units
6045 = (std::vector<signatured_type *> *) datum;
1fd400ff 6046
b2bdb8cf 6047 all_type_units->push_back (sigt);
1fd400ff
TT
6048
6049 return 1;
6050}
6051
78d4d2c5 6052/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6053 and fill them into TYPES_HTAB. It will process only type units,
6054 therefore DW_UT_type. */
c88ee1f0 6055
78d4d2c5 6056static void
ed2dc618
SM
6057create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6058 struct dwo_file *dwo_file,
b0b6a987 6059 dwarf2_section_info *section, htab_up &types_htab,
43988095 6060 rcuh_kind section_kind)
348e048f 6061{
3019eac3 6062 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6063 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6064 bfd *abfd;
6065 const gdb_byte *info_ptr, *end_ptr;
348e048f 6066
4bdcc0c1
DE
6067 abbrev_section = (dwo_file != NULL
6068 ? &dwo_file->sections.abbrev
6069 : &dwarf2_per_objfile->abbrev);
6070
b4f54984 6071 if (dwarf_read_debug)
43988095 6072 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6073 section->get_name (),
6074 abbrev_section->get_file_name ());
09406207 6075
96b79293 6076 section->read (objfile);
78d4d2c5 6077 info_ptr = section->buffer;
348e048f 6078
78d4d2c5
JK
6079 if (info_ptr == NULL)
6080 return;
348e048f 6081
78d4d2c5
JK
6082 /* We can't set abfd until now because the section may be empty or
6083 not present, in which case the bfd is unknown. */
96b79293 6084 abfd = section->get_bfd_owner ();
348e048f 6085
c0ab21c2
TT
6086 /* We don't use cutu_reader here because we don't need to read
6087 any dies: the signature is in the header. */
3019eac3 6088
78d4d2c5
JK
6089 end_ptr = info_ptr + section->size;
6090 while (info_ptr < end_ptr)
6091 {
78d4d2c5
JK
6092 struct signatured_type *sig_type;
6093 struct dwo_unit *dwo_tu;
6094 void **slot;
6095 const gdb_byte *ptr = info_ptr;
6096 struct comp_unit_head header;
6097 unsigned int length;
8b70b953 6098
9c541725 6099 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6100
a49dd8dd
JK
6101 /* Initialize it due to a false compiler warning. */
6102 header.signature = -1;
9c541725 6103 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6104
78d4d2c5
JK
6105 /* We need to read the type's signature in order to build the hash
6106 table, but we don't need anything else just yet. */
348e048f 6107
ed2dc618 6108 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6109 abbrev_section, ptr, section_kind);
348e048f 6110
4057dfde 6111 length = header.get_length ();
6caca83c 6112
78d4d2c5
JK
6113 /* Skip dummy type units. */
6114 if (ptr >= info_ptr + length
43988095
JK
6115 || peek_abbrev_code (abfd, ptr) == 0
6116 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6117 {
6118 info_ptr += length;
6119 continue;
6120 }
dee91e82 6121
78d4d2c5
JK
6122 if (types_htab == NULL)
6123 {
6124 if (dwo_file)
298e9637 6125 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6126 else
298e9637 6127 types_htab = allocate_signatured_type_table ();
78d4d2c5 6128 }
8b70b953 6129
78d4d2c5
JK
6130 if (dwo_file)
6131 {
6132 sig_type = NULL;
6133 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6134 struct dwo_unit);
6135 dwo_tu->dwo_file = dwo_file;
43988095 6136 dwo_tu->signature = header.signature;
9c541725 6137 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6138 dwo_tu->section = section;
9c541725 6139 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6140 dwo_tu->length = length;
6141 }
6142 else
6143 {
6144 /* N.B.: type_offset is not usable if this type uses a DWO file.
6145 The real type_offset is in the DWO file. */
6146 dwo_tu = NULL;
6147 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6148 struct signatured_type);
43988095 6149 sig_type->signature = header.signature;
9c541725 6150 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6151 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6152 sig_type->per_cu.is_debug_types = 1;
6153 sig_type->per_cu.section = section;
9c541725 6154 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6155 sig_type->per_cu.length = length;
6156 }
6157
b0b6a987 6158 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6159 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6160 INSERT);
6161 gdb_assert (slot != NULL);
6162 if (*slot != NULL)
6163 {
9c541725 6164 sect_offset dup_sect_off;
0349ea22 6165
3019eac3
DE
6166 if (dwo_file)
6167 {
78d4d2c5
JK
6168 const struct dwo_unit *dup_tu
6169 = (const struct dwo_unit *) *slot;
6170
9c541725 6171 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6172 }
6173 else
6174 {
78d4d2c5
JK
6175 const struct signatured_type *dup_tu
6176 = (const struct signatured_type *) *slot;
6177
9c541725 6178 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6179 }
8b70b953 6180
b98664d3 6181 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6182 " the entry at offset %s, signature %s"),
6183 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6184 hex_string (header.signature));
78d4d2c5
JK
6185 }
6186 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6187
78d4d2c5 6188 if (dwarf_read_debug > 1)
9d8780f0
SM
6189 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6190 sect_offset_str (sect_off),
43988095 6191 hex_string (header.signature));
3019eac3 6192
78d4d2c5
JK
6193 info_ptr += length;
6194 }
6195}
3019eac3 6196
78d4d2c5
JK
6197/* Create the hash table of all entries in the .debug_types
6198 (or .debug_types.dwo) section(s).
6199 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6200 otherwise it is NULL.
b3c8eb43 6201
78d4d2c5 6202 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6203
78d4d2c5 6204 Note: This function processes DWO files only, not DWP files. */
348e048f 6205
78d4d2c5 6206static void
ed2dc618
SM
6207create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6208 struct dwo_file *dwo_file,
fd5866f6 6209 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6210 htab_up &types_htab)
78d4d2c5 6211{
fd5866f6
SM
6212 for (dwarf2_section_info &section : type_sections)
6213 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6214 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6215}
6216
6217/* Create the hash table of all entries in the .debug_types section,
6218 and initialize all_type_units.
6219 The result is zero if there is an error (e.g. missing .debug_types section),
6220 otherwise non-zero. */
6221
6222static int
ed2dc618 6223create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6224{
b0b6a987 6225 htab_up types_htab;
3019eac3 6226
ed2dc618
SM
6227 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6228 &dwarf2_per_objfile->info, types_htab,
43988095 6229 rcuh_kind::COMPILE);
ed2dc618
SM
6230 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6231 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6232 if (types_htab == NULL)
6233 {
6234 dwarf2_per_objfile->signatured_types = NULL;
6235 return 0;
6236 }
6237
b0b6a987 6238 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6239
b2bdb8cf 6240 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6241 dwarf2_per_objfile->all_type_units.reserve
6242 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6243
b0b6a987
TT
6244 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6245 add_signatured_type_cu_to_table,
b2bdb8cf 6246 &dwarf2_per_objfile->all_type_units);
1fd400ff 6247
348e048f
DE
6248 return 1;
6249}
6250
6aa5f3a6
DE
6251/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6252 If SLOT is non-NULL, it is the entry to use in the hash table.
6253 Otherwise we find one. */
6254
6255static struct signatured_type *
ed2dc618
SM
6256add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6257 void **slot)
6aa5f3a6
DE
6258{
6259 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6260
b2bdb8cf
SM
6261 if (dwarf2_per_objfile->all_type_units.size ()
6262 == dwarf2_per_objfile->all_type_units.capacity ())
6263 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6264
b2bdb8cf
SM
6265 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6266 struct signatured_type);
6267
6268 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6269 sig_type->signature = sig;
6270 sig_type->per_cu.is_debug_types = 1;
6271 if (dwarf2_per_objfile->using_index)
6272 {
6273 sig_type->per_cu.v.quick =
6274 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6275 struct dwarf2_per_cu_quick_data);
6276 }
6277
6278 if (slot == NULL)
6279 {
b0b6a987 6280 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6281 sig_type, INSERT);
6282 }
6283 gdb_assert (*slot == NULL);
6284 *slot = sig_type;
6285 /* The rest of sig_type must be filled in by the caller. */
6286 return sig_type;
6287}
6288
a2ce51a0
DE
6289/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6290 Fill in SIG_ENTRY with DWO_ENTRY. */
6291
6292static void
ed2dc618 6293fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6294 struct signatured_type *sig_entry,
6295 struct dwo_unit *dwo_entry)
6296{
7ee85ab1 6297 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6298 gdb_assert (! sig_entry->per_cu.queued);
6299 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6300 if (dwarf2_per_objfile->using_index)
6301 {
6302 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6303 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6304 }
6305 else
6306 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6307 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6308 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6309 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6310 gdb_assert (sig_entry->dwo_unit == NULL);
6311
6312 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6313 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6314 sig_entry->per_cu.length = dwo_entry->length;
6315 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6316 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6317 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6318 sig_entry->dwo_unit = dwo_entry;
6319}
6320
6321/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6322 If we haven't read the TU yet, create the signatured_type data structure
6323 for a TU to be read in directly from a DWO file, bypassing the stub.
6324 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6325 using .gdb_index, then when reading a CU we want to stay in the DWO file
6326 containing that CU. Otherwise we could end up reading several other DWO
6327 files (due to comdat folding) to process the transitive closure of all the
6328 mentioned TUs, and that can be slow. The current DWO file will have every
6329 type signature that it needs.
a2ce51a0
DE
6330 We only do this for .gdb_index because in the psymtab case we already have
6331 to read all the DWOs to build the type unit groups. */
6332
6333static struct signatured_type *
6334lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6335{
518817b3
SM
6336 struct dwarf2_per_objfile *dwarf2_per_objfile
6337 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6338 struct dwo_file *dwo_file;
6339 struct dwo_unit find_dwo_entry, *dwo_entry;
6340 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6341 void **slot;
a2ce51a0
DE
6342
6343 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6344
6aa5f3a6
DE
6345 /* If TU skeletons have been removed then we may not have read in any
6346 TUs yet. */
6347 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6348 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6349
6350 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6351 Use the global signatured_types array to do our own comdat-folding
6352 of types. If this is the first time we're reading this TU, and
6353 the TU has an entry in .gdb_index, replace the recorded data from
6354 .gdb_index with this TU. */
a2ce51a0 6355
a2ce51a0 6356 find_sig_entry.signature = sig;
b0b6a987 6357 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6358 &find_sig_entry, INSERT);
9a3c8263 6359 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6360
6361 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6362 read. Don't reassign the global entry to point to this DWO if that's
6363 the case. Also note that if the TU is already being read, it may not
6364 have come from a DWO, the program may be a mix of Fission-compiled
6365 code and non-Fission-compiled code. */
6366
6367 /* Have we already tried to read this TU?
6368 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6369 needn't exist in the global table yet). */
6370 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6371 return sig_entry;
6372
6aa5f3a6
DE
6373 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6374 dwo_unit of the TU itself. */
6375 dwo_file = cu->dwo_unit->dwo_file;
6376
a2ce51a0
DE
6377 /* Ok, this is the first time we're reading this TU. */
6378 if (dwo_file->tus == NULL)
6379 return NULL;
6380 find_dwo_entry.signature = sig;
b0b6a987
TT
6381 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6382 &find_dwo_entry);
a2ce51a0
DE
6383 if (dwo_entry == NULL)
6384 return NULL;
6385
6aa5f3a6
DE
6386 /* If the global table doesn't have an entry for this TU, add one. */
6387 if (sig_entry == NULL)
ed2dc618 6388 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6389
ed2dc618 6390 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6391 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6392 return sig_entry;
6393}
6394
a2ce51a0
DE
6395/* Subroutine of lookup_signatured_type.
6396 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6397 then try the DWP file. If the TU stub (skeleton) has been removed then
6398 it won't be in .gdb_index. */
a2ce51a0
DE
6399
6400static struct signatured_type *
6401lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6402{
518817b3
SM
6403 struct dwarf2_per_objfile *dwarf2_per_objfile
6404 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6405 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6406 struct dwo_unit *dwo_entry;
6407 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6408 void **slot;
a2ce51a0
DE
6409
6410 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6411 gdb_assert (dwp_file != NULL);
6412
6aa5f3a6
DE
6413 /* If TU skeletons have been removed then we may not have read in any
6414 TUs yet. */
6415 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6416 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6417
6aa5f3a6 6418 find_sig_entry.signature = sig;
b0b6a987 6419 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6420 &find_sig_entry, INSERT);
9a3c8263 6421 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6422
6423 /* Have we already tried to read this TU?
6424 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6425 needn't exist in the global table yet). */
6426 if (sig_entry != NULL)
6427 return sig_entry;
6428
a2ce51a0
DE
6429 if (dwp_file->tus == NULL)
6430 return NULL;
ed2dc618 6431 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6432 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6433 if (dwo_entry == NULL)
6434 return NULL;
6435
ed2dc618
SM
6436 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6437 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6438
a2ce51a0
DE
6439 return sig_entry;
6440}
6441
380bca97 6442/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6443 Returns NULL if signature SIG is not present in the table.
6444 It is up to the caller to complain about this. */
348e048f
DE
6445
6446static struct signatured_type *
a2ce51a0 6447lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6448{
518817b3
SM
6449 struct dwarf2_per_objfile *dwarf2_per_objfile
6450 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6451
a2ce51a0
DE
6452 if (cu->dwo_unit
6453 && dwarf2_per_objfile->using_index)
6454 {
6455 /* We're in a DWO/DWP file, and we're using .gdb_index.
6456 These cases require special processing. */
ed2dc618 6457 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6458 return lookup_dwo_signatured_type (cu, sig);
6459 else
6460 return lookup_dwp_signatured_type (cu, sig);
6461 }
6462 else
6463 {
6464 struct signatured_type find_entry, *entry;
348e048f 6465
a2ce51a0
DE
6466 if (dwarf2_per_objfile->signatured_types == NULL)
6467 return NULL;
6468 find_entry.signature = sig;
9a3c8263 6469 entry = ((struct signatured_type *)
b0b6a987
TT
6470 htab_find (dwarf2_per_objfile->signatured_types.get (),
6471 &find_entry));
a2ce51a0
DE
6472 return entry;
6473 }
348e048f 6474}
18a8505e
AT
6475
6476/* Return the address base of the compile unit, which, if exists, is stored
6477 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6478static gdb::optional<ULONGEST>
6479lookup_addr_base (struct die_info *comp_unit_die)
6480{
6481 struct attribute *attr;
6482 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6483 if (attr == nullptr)
6484 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6485 if (attr == nullptr)
6486 return gdb::optional<ULONGEST> ();
6487 return DW_UNSND (attr);
6488}
6489
6490/* Return range lists base of the compile unit, which, if exists, is stored
6491 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6492static ULONGEST
6493lookup_ranges_base (struct die_info *comp_unit_die)
6494{
6495 struct attribute *attr;
6496 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6497 if (attr == nullptr)
6498 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6499 if (attr == nullptr)
6500 return 0;
6501 return DW_UNSND (attr);
6502}
6503
42e7ad6c 6504/* Low level DIE reading support. */
348e048f 6505
d85a05f0
DJ
6506/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6507
6508static void
6509init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6510 struct dwarf2_cu *cu,
3019eac3 6511 struct dwarf2_section_info *section,
685af9cd
TT
6512 struct dwo_file *dwo_file,
6513 struct abbrev_table *abbrev_table)
d85a05f0 6514{
fceca515 6515 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6516 reader->abfd = section->get_bfd_owner ();
d85a05f0 6517 reader->cu = cu;
3019eac3 6518 reader->dwo_file = dwo_file;
dee91e82
DE
6519 reader->die_section = section;
6520 reader->buffer = section->buffer;
f664829e 6521 reader->buffer_end = section->buffer + section->size;
685af9cd 6522 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6523}
6524
c0ab21c2 6525/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6526 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6527 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6528 already.
6529
6530 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6531 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6532 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6533 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6534 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6535 STUB_COMP_DIR may be non-NULL.
3e225074 6536 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6537 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6538 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6539 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6540 kept around for at least as long as *RESULT_READER.
6541
b0c7bfa9
DE
6542 The result is non-zero if a valid (non-dummy) DIE was found. */
6543
6544static int
6545read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6546 struct dwo_unit *dwo_unit,
b0c7bfa9 6547 struct die_info *stub_comp_unit_die,
a2ce51a0 6548 const char *stub_comp_dir,
b0c7bfa9 6549 struct die_reader_specs *result_reader,
d521ce57 6550 const gdb_byte **result_info_ptr,
b0c7bfa9 6551 struct die_info **result_comp_unit_die,
685af9cd 6552 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6553{
ed2dc618 6554 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6555 struct objfile *objfile = dwarf2_per_objfile->objfile;
6556 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6557 bfd *abfd;
d521ce57 6558 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6559 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6560 int i,num_extra_attrs;
6561 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6562 struct die_info *comp_unit_die;
6563
b0aeadb3
DE
6564 /* At most one of these may be provided. */
6565 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6566
b0c7bfa9
DE
6567 /* These attributes aren't processed until later:
6568 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6569 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6570 referenced later. However, these attributes are found in the stub
6571 which we won't have later. In order to not impose this complication
6572 on the rest of the code, we read them here and copy them to the
6573 DWO CU/TU die. */
b0c7bfa9
DE
6574
6575 stmt_list = NULL;
6576 low_pc = NULL;
6577 high_pc = NULL;
6578 ranges = NULL;
6579 comp_dir = NULL;
6580
6581 if (stub_comp_unit_die != NULL)
6582 {
6583 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6584 DWO file. */
6585 if (! this_cu->is_debug_types)
6586 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6587 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6588 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6589 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6590 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6591
18a8505e 6592 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6593
18a8505e
AT
6594 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6595 here (if needed). We need the value before we can process
6596 DW_AT_ranges. */
6597 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6598 }
a2ce51a0
DE
6599 else if (stub_comp_dir != NULL)
6600 {
6601 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6602 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6603 comp_dir->name = DW_AT_comp_dir;
6604 comp_dir->form = DW_FORM_string;
6605 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6606 DW_STRING (comp_dir) = stub_comp_dir;
6607 }
b0c7bfa9
DE
6608
6609 /* Set up for reading the DWO CU/TU. */
6610 cu->dwo_unit = dwo_unit;
685af9cd 6611 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6612 section->read (objfile);
6613 abfd = section->get_bfd_owner ();
9c541725
PA
6614 begin_info_ptr = info_ptr = (section->buffer
6615 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6616 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6617
6618 if (this_cu->is_debug_types)
6619 {
b0c7bfa9
DE
6620 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6621
ed2dc618
SM
6622 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6623 &cu->header, section,
b0c7bfa9 6624 dwo_abbrev_section,
43988095 6625 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6626 /* This is not an assert because it can be caused by bad debug info. */
43988095 6627 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6628 {
6629 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6630 " TU at offset %s [in module %s]"),
a2ce51a0 6631 hex_string (sig_type->signature),
43988095 6632 hex_string (cu->header.signature),
9d8780f0 6633 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6634 bfd_get_filename (abfd));
6635 }
9c541725 6636 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6637 /* For DWOs coming from DWP files, we don't know the CU length
6638 nor the type's offset in the TU until now. */
4057dfde 6639 dwo_unit->length = cu->header.get_length ();
9c541725 6640 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6641
6642 /* Establish the type offset that can be used to lookup the type.
6643 For DWO files, we don't know it until now. */
9c541725
PA
6644 sig_type->type_offset_in_section
6645 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6646 }
6647 else
6648 {
ed2dc618
SM
6649 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6650 &cu->header, section,
b0c7bfa9 6651 dwo_abbrev_section,
43988095 6652 info_ptr, rcuh_kind::COMPILE);
9c541725 6653 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6654 /* For DWOs coming from DWP files, we don't know the CU length
6655 until now. */
4057dfde 6656 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6657 }
6658
685af9cd 6659 *result_dwo_abbrev_table
86de1d91
TT
6660 = abbrev_table::read (objfile, dwo_abbrev_section,
6661 cu->header.abbrev_sect_off);
685af9cd
TT
6662 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6663 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6664
6665 /* Read in the die, but leave space to copy over the attributes
6666 from the stub. This has the benefit of simplifying the rest of
6667 the code - all the work to maintain the illusion of a single
6668 DW_TAG_{compile,type}_unit DIE is done here. */
6669 num_extra_attrs = ((stmt_list != NULL)
6670 + (low_pc != NULL)
6671 + (high_pc != NULL)
6672 + (ranges != NULL)
6673 + (comp_dir != NULL));
6674 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6675 num_extra_attrs);
b0c7bfa9
DE
6676
6677 /* Copy over the attributes from the stub to the DIE we just read in. */
6678 comp_unit_die = *result_comp_unit_die;
6679 i = comp_unit_die->num_attrs;
6680 if (stmt_list != NULL)
6681 comp_unit_die->attrs[i++] = *stmt_list;
6682 if (low_pc != NULL)
6683 comp_unit_die->attrs[i++] = *low_pc;
6684 if (high_pc != NULL)
6685 comp_unit_die->attrs[i++] = *high_pc;
6686 if (ranges != NULL)
6687 comp_unit_die->attrs[i++] = *ranges;
6688 if (comp_dir != NULL)
6689 comp_unit_die->attrs[i++] = *comp_dir;
6690 comp_unit_die->num_attrs += num_extra_attrs;
6691
b4f54984 6692 if (dwarf_die_debug)
bf6af496
DE
6693 {
6694 fprintf_unfiltered (gdb_stdlog,
6695 "Read die from %s@0x%x of %s:\n",
96b79293 6696 section->get_name (),
bf6af496
DE
6697 (unsigned) (begin_info_ptr - section->buffer),
6698 bfd_get_filename (abfd));
b4f54984 6699 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6700 }
6701
b0c7bfa9
DE
6702 /* Skip dummy compilation units. */
6703 if (info_ptr >= begin_info_ptr + dwo_unit->length
6704 || peek_abbrev_code (abfd, info_ptr) == 0)
6705 return 0;
6706
6707 *result_info_ptr = info_ptr;
6708 return 1;
6709}
6710
a084a2a6
AT
6711/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6712 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6713 signature is part of the header. */
6714static gdb::optional<ULONGEST>
6715lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6716{
6717 if (cu->header.version >= 5)
6718 return cu->header.signature;
6719 struct attribute *attr;
6720 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6721 if (attr == nullptr)
6722 return gdb::optional<ULONGEST> ();
6723 return DW_UNSND (attr);
6724}
6725
c0ab21c2 6726/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6727 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6728 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6729
6730static struct dwo_unit *
6731lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6732 struct die_info *comp_unit_die,
6733 const char *dwo_name)
b0c7bfa9
DE
6734{
6735 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6736 struct dwo_unit *dwo_unit;
c0ab21c2 6737 const char *comp_dir;
b0c7bfa9 6738
a2ce51a0
DE
6739 gdb_assert (cu != NULL);
6740
b0c7bfa9 6741 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6742 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6743 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6744
6745 if (this_cu->is_debug_types)
6746 {
6747 struct signatured_type *sig_type;
6748
6749 /* Since this_cu is the first member of struct signatured_type,
6750 we can go from a pointer to one to a pointer to the other. */
6751 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6752 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6753 }
6754 else
6755 {
a084a2a6
AT
6756 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6757 if (!signature.has_value ())
b0c7bfa9
DE
6758 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6759 " [in module %s]"),
e3b94546 6760 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6761 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6762 *signature);
b0c7bfa9
DE
6763 }
6764
b0c7bfa9
DE
6765 return dwo_unit;
6766}
6767
c0ab21c2 6768/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6769 See it for a description of the parameters.
fcd3b13d 6770 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6771
c0ab21c2
TT
6772void
6773cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6774 int use_existing_cu)
a2ce51a0 6775{
a2ce51a0 6776 struct signatured_type *sig_type;
a2ce51a0 6777 struct die_reader_specs reader;
a2ce51a0
DE
6778
6779 /* Verify we can do the following downcast, and that we have the
6780 data we need. */
6781 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6782 sig_type = (struct signatured_type *) this_cu;
6783 gdb_assert (sig_type->dwo_unit != NULL);
6784
6aa5f3a6
DE
6785 if (use_existing_cu && this_cu->cu != NULL)
6786 {
6787 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6788 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6789 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6790 }
6791 else
6792 {
6793 /* If !use_existing_cu, this_cu->cu must be NULL. */
6794 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6795 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6796 }
6797
6798 /* A future optimization, if needed, would be to use an existing
6799 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6800 could share abbrev tables. */
a2ce51a0
DE
6801
6802 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6803 NULL /* stub_comp_unit_die */,
6804 sig_type->dwo_unit->dwo_file->comp_dir,
6805 &reader, &info_ptr,
3e225074 6806 &comp_unit_die,
c0ab21c2 6807 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6808 {
6809 /* Dummy die. */
c0ab21c2 6810 dummy_p = true;
a2ce51a0 6811 }
a2ce51a0
DE
6812}
6813
fd820528 6814/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6815 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6816
f4dc4d17
DE
6817 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6818 Otherwise the table specified in the comp unit header is read in and used.
6819 This is an optimization for when we already have the abbrev table.
6820
dee91e82 6821 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6822 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6823
c0ab21c2
TT
6824cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6825 struct abbrev_table *abbrev_table,
6751ebae 6826 int use_existing_cu,
c0ab21c2
TT
6827 bool skip_partial)
6828 : die_reader_specs {},
6751ebae 6829 m_this_cu (this_cu)
c906108c 6830{
ed2dc618 6831 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6832 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6833 struct dwarf2_section_info *section = this_cu->section;
96b79293 6834 bfd *abfd = section->get_bfd_owner ();
dee91e82 6835 struct dwarf2_cu *cu;
c0ab21c2 6836 const gdb_byte *begin_info_ptr;
dee91e82 6837 struct signatured_type *sig_type = NULL;
4bdcc0c1 6838 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6839 /* Non-zero if CU currently points to a DWO file and we need to
6840 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6841 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6842 int rereading_dwo_cu = 0;
c906108c 6843
b4f54984 6844 if (dwarf_die_debug)
9d8780f0 6845 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6846 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6847 sect_offset_str (this_cu->sect_off));
09406207 6848
a2ce51a0
DE
6849 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6850 file (instead of going through the stub), short-circuit all of this. */
6851 if (this_cu->reading_dwo_directly)
6852 {
6853 /* Narrow down the scope of possibilities to have to understand. */
6854 gdb_assert (this_cu->is_debug_types);
6855 gdb_assert (abbrev_table == NULL);
6751ebae 6856 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6857 return;
6858 }
6859
dee91e82 6860 /* This is cheap if the section is already read in. */
96b79293 6861 section->read (objfile);
dee91e82 6862
9c541725 6863 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6864
6865 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6866
6867 if (use_existing_cu && this_cu->cu != NULL)
6868 {
6869 cu = this_cu->cu;
42e7ad6c
DE
6870 /* If this CU is from a DWO file we need to start over, we need to
6871 refetch the attributes from the skeleton CU.
6872 This could be optimized by retrieving those attributes from when we
6873 were here the first time: the previous comp_unit_die was stored in
6874 comp_unit_obstack. But there's no data yet that we need this
6875 optimization. */
6876 if (cu->dwo_unit != NULL)
6877 rereading_dwo_cu = 1;
dee91e82
DE
6878 }
6879 else
6880 {
6881 /* If !use_existing_cu, this_cu->cu must be NULL. */
6882 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6883 m_new_cu.reset (new dwarf2_cu (this_cu));
6884 cu = m_new_cu.get ();
42e7ad6c 6885 }
dee91e82 6886
b0c7bfa9 6887 /* Get the header. */
9c541725 6888 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6889 {
6890 /* We already have the header, there's no need to read it in again. */
9c541725 6891 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6892 }
6893 else
6894 {
3019eac3 6895 if (this_cu->is_debug_types)
dee91e82 6896 {
ed2dc618
SM
6897 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6898 &cu->header, section,
4bdcc0c1 6899 abbrev_section, info_ptr,
43988095 6900 rcuh_kind::TYPE);
dee91e82 6901
42e7ad6c
DE
6902 /* Since per_cu is the first member of struct signatured_type,
6903 we can go from a pointer to one to a pointer to the other. */
6904 sig_type = (struct signatured_type *) this_cu;
43988095 6905 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6906 gdb_assert (sig_type->type_offset_in_tu
6907 == cu->header.type_cu_offset_in_tu);
6908 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6909
42e7ad6c
DE
6910 /* LENGTH has not been set yet for type units if we're
6911 using .gdb_index. */
4057dfde 6912 this_cu->length = cu->header.get_length ();
3019eac3
DE
6913
6914 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6915 sig_type->type_offset_in_section =
6916 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6917
6918 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6919 }
6920 else
6921 {
ed2dc618
SM
6922 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6923 &cu->header, section,
4bdcc0c1 6924 abbrev_section,
43988095
JK
6925 info_ptr,
6926 rcuh_kind::COMPILE);
dee91e82 6927
9c541725 6928 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6929 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6930 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6931 }
6932 }
10b3939b 6933
6caca83c 6934 /* Skip dummy compilation units. */
dee91e82 6935 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6936 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6937 {
6938 dummy_p = true;
6939 return;
6940 }
6caca83c 6941
433df2d4
DE
6942 /* If we don't have them yet, read the abbrevs for this compilation unit.
6943 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6944 done. */
f4dc4d17 6945 if (abbrev_table != NULL)
685af9cd
TT
6946 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6947 else
f4dc4d17 6948 {
c0ab21c2 6949 m_abbrev_table_holder
86de1d91
TT
6950 = abbrev_table::read (objfile, abbrev_section,
6951 cu->header.abbrev_sect_off);
c0ab21c2 6952 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6953 }
af703f96 6954
dee91e82 6955 /* Read the top level CU/TU die. */
c0ab21c2 6956 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6957 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6958
58f0c718 6959 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6960 {
6961 dummy_p = true;
6962 return;
6963 }
58f0c718 6964
b0c7bfa9 6965 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6966 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6967 table from the DWO file and pass the ownership over to us. It will be
6968 referenced from READER, so we must make sure to free it after we're done
6969 with READER.
6970
b0c7bfa9
DE
6971 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6972 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6973 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6974 if (dwo_name != nullptr)
3019eac3 6975 {
3019eac3 6976 struct dwo_unit *dwo_unit;
b0c7bfa9 6977 struct die_info *dwo_comp_unit_die;
3019eac3 6978
3e225074 6979 if (comp_unit_die->has_children)
6a506a2d 6980 {
b98664d3 6981 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6982 " has children (offset %s) [in module %s]"),
6983 sect_offset_str (this_cu->sect_off),
6984 bfd_get_filename (abfd));
6a506a2d 6985 }
c0ab21c2 6986 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6987 if (dwo_unit != NULL)
3019eac3 6988 {
6a506a2d 6989 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6990 comp_unit_die, NULL,
c0ab21c2 6991 this, &info_ptr,
3e225074 6992 &dwo_comp_unit_die,
c0ab21c2 6993 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6994 {
6995 /* Dummy die. */
c0ab21c2 6996 dummy_p = true;
6a506a2d
DE
6997 return;
6998 }
6999 comp_unit_die = dwo_comp_unit_die;
7000 }
7001 else
7002 {
7003 /* Yikes, we couldn't find the rest of the DIE, we only have
7004 the stub. A complaint has already been logged. There's
7005 not much more we can do except pass on the stub DIE to
7006 die_reader_func. We don't want to throw an error on bad
7007 debug info. */
3019eac3
DE
7008 }
7009 }
c0ab21c2 7010}
3019eac3 7011
6751ebae
TT
7012void
7013cutu_reader::keep ()
c0ab21c2 7014{
b0c7bfa9 7015 /* Done, clean up. */
6751ebae
TT
7016 gdb_assert (!dummy_p);
7017 if (m_new_cu != NULL)
348e048f 7018 {
c0ab21c2
TT
7019 struct dwarf2_per_objfile *dwarf2_per_objfile
7020 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7021 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7022 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7023 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7024 /* The chain owns it now. */
c0ab21c2 7025 m_new_cu.release ();
348e048f 7026 }
dee91e82
DE
7027}
7028
18a8505e
AT
7029/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7030 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7031 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7032
7033 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7034 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7035
7036 We fill in THIS_CU->length.
7037
dee91e82 7038 THIS_CU->cu is always freed when done.
3019eac3 7039 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7040 to care whether it refers to the "main" CU or the DWO CU.
7041
7042 When parent_cu is passed, it is used to provide a default value for
7043 str_offsets_base and addr_base from the parent. */
dee91e82 7044
c0ab21c2
TT
7045cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7046 struct dwarf2_cu *parent_cu,
7047 struct dwo_file *dwo_file)
7048 : die_reader_specs {},
7049 m_this_cu (this_cu)
dee91e82 7050{
ed2dc618 7051 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7052 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7053 struct dwarf2_section_info *section = this_cu->section;
96b79293 7054 bfd *abfd = section->get_bfd_owner ();
33e80786 7055 struct dwarf2_section_info *abbrev_section;
d521ce57 7056 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7057
b4f54984 7058 if (dwarf_die_debug)
9d8780f0 7059 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7060 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7061 sect_offset_str (this_cu->sect_off));
09406207 7062
dee91e82
DE
7063 gdb_assert (this_cu->cu == NULL);
7064
33e80786
DE
7065 abbrev_section = (dwo_file != NULL
7066 ? &dwo_file->sections.abbrev
7067 : get_abbrev_section_for_cu (this_cu));
7068
dee91e82 7069 /* This is cheap if the section is already read in. */
96b79293 7070 section->read (objfile);
dee91e82 7071
c0ab21c2 7072 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7073
9c541725 7074 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7075 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7076 &m_new_cu->header, section,
4bdcc0c1 7077 abbrev_section, info_ptr,
43988095
JK
7078 (this_cu->is_debug_types
7079 ? rcuh_kind::TYPE
7080 : rcuh_kind::COMPILE));
dee91e82 7081
18a8505e
AT
7082 if (parent_cu != nullptr)
7083 {
c0ab21c2
TT
7084 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7085 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7086 }
4057dfde 7087 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7088
7089 /* Skip dummy compilation units. */
7090 if (info_ptr >= begin_info_ptr + this_cu->length
7091 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7092 {
7093 dummy_p = true;
7094 return;
7095 }
72bf9492 7096
c0ab21c2 7097 m_abbrev_table_holder
86de1d91
TT
7098 = abbrev_table::read (objfile, abbrev_section,
7099 m_new_cu->header.abbrev_sect_off);
dee91e82 7100
c0ab21c2
TT
7101 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7102 m_abbrev_table_holder.get ());
3e225074 7103 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7104}
7105
0018ea6f
DE
7106\f
7107/* Type Unit Groups.
dee91e82 7108
0018ea6f
DE
7109 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7110 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7111 so that all types coming from the same compilation (.o file) are grouped
7112 together. A future step could be to put the types in the same symtab as
7113 the CU the types ultimately came from. */
ff013f42 7114
f4dc4d17
DE
7115static hashval_t
7116hash_type_unit_group (const void *item)
7117{
9a3c8263
SM
7118 const struct type_unit_group *tu_group
7119 = (const struct type_unit_group *) item;
f4dc4d17 7120
094b34ac 7121 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7122}
348e048f
DE
7123
7124static int
f4dc4d17 7125eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7126{
9a3c8263
SM
7127 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7128 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7129
094b34ac 7130 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7131}
348e048f 7132
f4dc4d17
DE
7133/* Allocate a hash table for type unit groups. */
7134
eaa5fa8b 7135static htab_up
298e9637 7136allocate_type_unit_groups_table ()
f4dc4d17 7137{
eaa5fa8b
TT
7138 return htab_up (htab_create_alloc (3,
7139 hash_type_unit_group,
7140 eq_type_unit_group,
7141 NULL, xcalloc, xfree));
f4dc4d17 7142}
dee91e82 7143
f4dc4d17
DE
7144/* Type units that don't have DW_AT_stmt_list are grouped into their own
7145 partial symtabs. We combine several TUs per psymtab to not let the size
7146 of any one psymtab grow too big. */
7147#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7148#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7149
094b34ac 7150/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7151 Create the type_unit_group object used to hold one or more TUs. */
7152
7153static struct type_unit_group *
094b34ac 7154create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7155{
518817b3
SM
7156 struct dwarf2_per_objfile *dwarf2_per_objfile
7157 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7158 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7159 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7160 struct type_unit_group *tu_group;
f4dc4d17
DE
7161
7162 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7163 struct type_unit_group);
094b34ac 7164 per_cu = &tu_group->per_cu;
518817b3 7165 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7166
094b34ac
DE
7167 if (dwarf2_per_objfile->using_index)
7168 {
7169 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7170 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7171 }
7172 else
7173 {
9c541725 7174 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7175 dwarf2_psymtab *pst;
528e1572 7176 std::string name;
094b34ac
DE
7177
7178 /* Give the symtab a useful name for debug purposes. */
7179 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7180 name = string_printf ("<type_units_%d>",
7181 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7182 else
528e1572 7183 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7184
528e1572 7185 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7186 pst->anonymous = true;
094b34ac 7187 }
f4dc4d17 7188
094b34ac 7189 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7190 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7191
7192 return tu_group;
7193}
7194
094b34ac
DE
7195/* Look up the type_unit_group for type unit CU, and create it if necessary.
7196 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7197
7198static struct type_unit_group *
ff39bb5e 7199get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7200{
518817b3
SM
7201 struct dwarf2_per_objfile *dwarf2_per_objfile
7202 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7203 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7204 struct type_unit_group *tu_group;
7205 void **slot;
7206 unsigned int line_offset;
7207 struct type_unit_group type_unit_group_for_lookup;
7208
7209 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7210 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7211
7212 /* Do we need to create a new group, or can we use an existing one? */
7213
7214 if (stmt_list)
7215 {
7216 line_offset = DW_UNSND (stmt_list);
7217 ++tu_stats->nr_symtab_sharers;
7218 }
7219 else
7220 {
7221 /* Ugh, no stmt_list. Rare, but we have to handle it.
7222 We can do various things here like create one group per TU or
7223 spread them over multiple groups to split up the expansion work.
7224 To avoid worst case scenarios (too many groups or too large groups)
7225 we, umm, group them in bunches. */
7226 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7227 | (tu_stats->nr_stmt_less_type_units
7228 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7229 ++tu_stats->nr_stmt_less_type_units;
7230 }
7231
094b34ac 7232 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7233 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7234 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7235 &type_unit_group_for_lookup, INSERT);
7236 if (*slot != NULL)
7237 {
9a3c8263 7238 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7239 gdb_assert (tu_group != NULL);
7240 }
7241 else
7242 {
9c541725 7243 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7244 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7245 *slot = tu_group;
7246 ++tu_stats->nr_symtabs;
7247 }
7248
7249 return tu_group;
7250}
0018ea6f
DE
7251\f
7252/* Partial symbol tables. */
7253
7254/* Create a psymtab named NAME and assign it to PER_CU.
7255
7256 The caller must fill in the following details:
7257 dirname, textlow, texthigh. */
7258
891813be 7259static dwarf2_psymtab *
0018ea6f
DE
7260create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7261{
e3b94546 7262 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7263 dwarf2_psymtab *pst;
0018ea6f 7264
891813be 7265 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7266
6d94535f 7267 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7268
7269 /* This is the glue that links PST into GDB's symbol API. */
891813be 7270 pst->per_cu_data = per_cu;
0018ea6f
DE
7271 per_cu->v.psymtab = pst;
7272
7273 return pst;
7274}
7275
c0ab21c2 7276/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7277
7278static void
7279process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7280 const gdb_byte *info_ptr,
0018ea6f 7281 struct die_info *comp_unit_die,
c0ab21c2 7282 enum language pretend_language)
0018ea6f
DE
7283{
7284 struct dwarf2_cu *cu = reader->cu;
518817b3 7285 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7286 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7287 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7288 CORE_ADDR baseaddr;
7289 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7290 dwarf2_psymtab *pst;
3a2b436a 7291 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7292 const char *filename;
0018ea6f 7293
0018ea6f
DE
7294 gdb_assert (! per_cu->is_debug_types);
7295
c0ab21c2 7296 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7297
0018ea6f 7298 /* Allocate a new partial symbol table structure. */
2e927613
TV
7299 gdb::unique_xmalloc_ptr<char> debug_filename;
7300 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7301 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7302 if (filename == NULL)
0018ea6f 7303 filename = "";
2e927613
TV
7304 else if (strcmp (filename, artificial) == 0)
7305 {
7306 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7307 sect_offset_str (per_cu->sect_off),
7308 (char *) NULL));
2e927613
TV
7309 filename = debug_filename.get ();
7310 }
0018ea6f
DE
7311
7312 pst = create_partial_symtab (per_cu, filename);
7313
7314 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7315 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7316
b3b3bada 7317 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7318
7319 dwarf2_find_base_address (comp_unit_die, cu);
7320
7321 /* Possibly set the default values of LOWPC and HIGHPC from
7322 `DW_AT_ranges'. */
3a2b436a
JK
7323 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7324 &best_highpc, cu, pst);
7325 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7326 {
7327 CORE_ADDR low
7328 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7329 - baseaddr);
7330 CORE_ADDR high
7331 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7332 - baseaddr - 1);
7333 /* Store the contiguous range if it is not empty; it can be
7334 empty for CUs with no code. */
d320c2b5
TT
7335 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7336 low, high, pst);
79748972 7337 }
0018ea6f
DE
7338
7339 /* Check if comp unit has_children.
7340 If so, read the rest of the partial symbols from this comp unit.
7341 If not, there's no more debug_info for this comp unit. */
3e225074 7342 if (comp_unit_die->has_children)
0018ea6f
DE
7343 {
7344 struct partial_die_info *first_die;
7345 CORE_ADDR lowpc, highpc;
7346
7347 lowpc = ((CORE_ADDR) -1);
7348 highpc = ((CORE_ADDR) 0);
7349
7350 first_die = load_partial_dies (reader, info_ptr, 1);
7351
7352 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7353 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7354
7355 /* If we didn't find a lowpc, set it to highpc to avoid
7356 complaints from `maint check'. */
7357 if (lowpc == ((CORE_ADDR) -1))
7358 lowpc = highpc;
7359
7360 /* If the compilation unit didn't have an explicit address range,
7361 then use the information extracted from its child dies. */
e385593e 7362 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7363 {
7364 best_lowpc = lowpc;
7365 best_highpc = highpc;
7366 }
7367 }
4ae976d1 7368 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7369 best_lowpc + baseaddr)
7370 - baseaddr);
4ae976d1 7371 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7372 best_highpc + baseaddr)
7373 - baseaddr);
0018ea6f 7374
8763cede 7375 end_psymtab_common (objfile, pst);
0018ea6f 7376
ae640021 7377 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7378 {
7379 int i;
ae640021 7380 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7381
7382 /* Fill in 'dependencies' here; we fill in 'users' in a
7383 post-pass. */
7384 pst->number_of_dependencies = len;
a9342b62
TT
7385 pst->dependencies
7386 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7387 for (i = 0; i < len; ++i)
7388 {
7389 pst->dependencies[i]
7390 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7391 }
0018ea6f 7392
ae640021 7393 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7394 }
7395
7396 /* Get the list of files included in the current compilation unit,
7397 and build a psymtab for each of them. */
7398 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7399
b4f54984 7400 if (dwarf_read_debug)
b926417a
TT
7401 fprintf_unfiltered (gdb_stdlog,
7402 "Psymtab for %s unit @%s: %s - %s"
7403 ", %d global, %d static syms\n",
7404 per_cu->is_debug_types ? "type" : "comp",
7405 sect_offset_str (per_cu->sect_off),
7406 paddress (gdbarch, pst->text_low (objfile)),
7407 paddress (gdbarch, pst->text_high (objfile)),
7408 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7409}
7410
7411/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7412 Process compilation unit THIS_CU for a psymtab. */
7413
7414static void
7415process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7416 bool want_partial_unit,
b93601f3 7417 enum language pretend_language)
0018ea6f
DE
7418{
7419 /* If this compilation unit was already read in, free the
7420 cached copy in order to read it in again. This is
7421 necessary because we skipped some symbols when we first
7422 read in the compilation unit (see load_partial_dies).
7423 This problem could be avoided, but the benefit is unclear. */
7424 if (this_cu->cu != NULL)
7425 free_one_cached_comp_unit (this_cu);
7426
6751ebae 7427 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2
TT
7428
7429 if (reader.dummy_p)
f1902523 7430 {
c0ab21c2 7431 /* Nothing. */
f1902523 7432 }
c0ab21c2 7433 else if (this_cu->is_debug_types)
3e225074
TT
7434 build_type_psymtabs_reader (&reader, reader.info_ptr,
7435 reader.comp_unit_die);
135f5437
TT
7436 else if (want_partial_unit
7437 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7438 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7439 reader.comp_unit_die,
c0ab21c2 7440 pretend_language);
0018ea6f
DE
7441
7442 /* Age out any secondary CUs. */
ed2dc618 7443 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7444}
f4dc4d17
DE
7445
7446/* Reader function for build_type_psymtabs. */
7447
7448static void
7449build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7450 const gdb_byte *info_ptr,
3e225074 7451 struct die_info *type_unit_die)
f4dc4d17 7452{
ed2dc618 7453 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7454 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7455 struct objfile *objfile = dwarf2_per_objfile->objfile;
7456 struct dwarf2_cu *cu = reader->cu;
7457 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7458 struct signatured_type *sig_type;
f4dc4d17
DE
7459 struct type_unit_group *tu_group;
7460 struct attribute *attr;
7461 struct partial_die_info *first_die;
7462 CORE_ADDR lowpc, highpc;
891813be 7463 dwarf2_psymtab *pst;
f4dc4d17 7464
0186c6a7
DE
7465 gdb_assert (per_cu->is_debug_types);
7466 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7467
3e225074 7468 if (! type_unit_die->has_children)
f4dc4d17
DE
7469 return;
7470
7471 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7472 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7473
df07e2c7 7474 if (tu_group->tus == nullptr)
a8b3b8e9 7475 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7476 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7477
7478 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7479 pst = create_partial_symtab (per_cu, "");
6d94535f 7480 pst->anonymous = true;
f4dc4d17
DE
7481
7482 first_die = load_partial_dies (reader, info_ptr, 1);
7483
7484 lowpc = (CORE_ADDR) -1;
7485 highpc = (CORE_ADDR) 0;
7486 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7487
8763cede 7488 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7489}
7490
73051182
DE
7491/* Struct used to sort TUs by their abbreviation table offset. */
7492
7493struct tu_abbrev_offset
7494{
b2bdb8cf
SM
7495 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7496 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7497 {}
7498
7499 signatured_type *sig_type;
73051182
DE
7500 sect_offset abbrev_offset;
7501};
7502
484cf504 7503/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7504
484cf504
TT
7505static bool
7506sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7507 const struct tu_abbrev_offset &b)
73051182 7508{
484cf504 7509 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7510}
7511
7512/* Efficiently read all the type units.
7513 This does the bulk of the work for build_type_psymtabs.
7514
7515 The efficiency is because we sort TUs by the abbrev table they use and
7516 only read each abbrev table once. In one program there are 200K TUs
7517 sharing 8K abbrev tables.
7518
7519 The main purpose of this function is to support building the
7520 dwarf2_per_objfile->type_unit_groups table.
7521 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7522 can collapse the search space by grouping them by stmt_list.
7523 The savings can be significant, in the same program from above the 200K TUs
7524 share 8K stmt_list tables.
7525
7526 FUNC is expected to call get_type_unit_group, which will create the
7527 struct type_unit_group if necessary and add it to
7528 dwarf2_per_objfile->type_unit_groups. */
7529
7530static void
ed2dc618 7531build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7532{
73051182 7533 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7534 abbrev_table_up abbrev_table;
73051182 7535 sect_offset abbrev_offset;
73051182
DE
7536
7537 /* It's up to the caller to not call us multiple times. */
7538 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7539
b2bdb8cf 7540 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7541 return;
7542
7543 /* TUs typically share abbrev tables, and there can be way more TUs than
7544 abbrev tables. Sort by abbrev table to reduce the number of times we
7545 read each abbrev table in.
7546 Alternatives are to punt or to maintain a cache of abbrev tables.
7547 This is simpler and efficient enough for now.
7548
7549 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7550 symtab to use). Typically TUs with the same abbrev offset have the same
7551 stmt_list value too so in practice this should work well.
7552
7553 The basic algorithm here is:
7554
7555 sort TUs by abbrev table
7556 for each TU with same abbrev table:
7557 read abbrev table if first user
7558 read TU top level DIE
7559 [IWBN if DWO skeletons had DW_AT_stmt_list]
7560 call FUNC */
7561
b4f54984 7562 if (dwarf_read_debug)
73051182
DE
7563 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7564
7565 /* Sort in a separate table to maintain the order of all_type_units
7566 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7567 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7568 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7569
7570 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7571 sorted_by_abbrev.emplace_back
7572 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7573 sig_type->per_cu.section,
7574 sig_type->per_cu.sect_off));
73051182 7575
484cf504
TT
7576 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7577 sort_tu_by_abbrev_offset);
73051182 7578
9c541725 7579 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7580
b2bdb8cf 7581 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7582 {
73051182
DE
7583 /* Switch to the next abbrev table if necessary. */
7584 if (abbrev_table == NULL
b2bdb8cf 7585 || tu.abbrev_offset != abbrev_offset)
73051182 7586 {
b2bdb8cf 7587 abbrev_offset = tu.abbrev_offset;
73051182 7588 abbrev_table =
86de1d91
TT
7589 abbrev_table::read (dwarf2_per_objfile->objfile,
7590 &dwarf2_per_objfile->abbrev,
7591 abbrev_offset);
73051182
DE
7592 ++tu_stats->nr_uniq_abbrev_tables;
7593 }
7594
c0ab21c2 7595 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7596 0, false);
c0ab21c2
TT
7597 if (!reader.dummy_p)
7598 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7599 reader.comp_unit_die);
73051182 7600 }
6aa5f3a6 7601}
73051182 7602
6aa5f3a6
DE
7603/* Print collected type unit statistics. */
7604
7605static void
ed2dc618 7606print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7607{
7608 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7609
7610 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7611 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7612 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7613 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7614 tu_stats->nr_uniq_abbrev_tables);
7615 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7616 tu_stats->nr_symtabs);
7617 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7618 tu_stats->nr_symtab_sharers);
7619 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7620 tu_stats->nr_stmt_less_type_units);
7621 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7622 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7623}
7624
f4dc4d17
DE
7625/* Traversal function for build_type_psymtabs. */
7626
7627static int
7628build_type_psymtab_dependencies (void **slot, void *info)
7629{
ed2dc618
SM
7630 struct dwarf2_per_objfile *dwarf2_per_objfile
7631 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7632 struct objfile *objfile = dwarf2_per_objfile->objfile;
7633 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7634 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7635 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7636 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7637 int i;
7638
7639 gdb_assert (len > 0);
197400e8 7640 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7641
7642 pst->number_of_dependencies = len;
a9342b62 7643 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7644 for (i = 0; i < len; ++i)
f4dc4d17 7645 {
df07e2c7 7646 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7647 gdb_assert (iter->per_cu.is_debug_types);
7648 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7649 iter->type_unit_group = tu_group;
f4dc4d17
DE
7650 }
7651
df07e2c7
AB
7652 delete tu_group->tus;
7653 tu_group->tus = nullptr;
348e048f
DE
7654
7655 return 1;
7656}
7657
7658/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7659 Build partial symbol tables for the .debug_types comp-units. */
7660
7661static void
ed2dc618 7662build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7663{
ed2dc618 7664 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7665 return;
7666
ed2dc618 7667 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7668}
f4dc4d17 7669
6aa5f3a6
DE
7670/* Traversal function for process_skeletonless_type_unit.
7671 Read a TU in a DWO file and build partial symbols for it. */
7672
7673static int
7674process_skeletonless_type_unit (void **slot, void *info)
7675{
7676 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7677 struct dwarf2_per_objfile *dwarf2_per_objfile
7678 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7679 struct signatured_type find_entry, *entry;
7680
7681 /* If this TU doesn't exist in the global table, add it and read it in. */
7682
7683 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7684 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7685
7686 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7687 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7688 &find_entry, INSERT);
6aa5f3a6
DE
7689 /* If we've already seen this type there's nothing to do. What's happening
7690 is we're doing our own version of comdat-folding here. */
7691 if (*slot != NULL)
7692 return 1;
7693
7694 /* This does the job that create_all_type_units would have done for
7695 this TU. */
ed2dc618
SM
7696 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7697 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7698 *slot = entry;
7699
7700 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7701 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7702 if (!reader.dummy_p)
7703 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7704 reader.comp_unit_die);
6aa5f3a6
DE
7705
7706 return 1;
7707}
7708
7709/* Traversal function for process_skeletonless_type_units. */
7710
7711static int
7712process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7713{
7714 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7715
7716 if (dwo_file->tus != NULL)
b0b6a987
TT
7717 htab_traverse_noresize (dwo_file->tus.get (),
7718 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7719
7720 return 1;
7721}
7722
7723/* Scan all TUs of DWO files, verifying we've processed them.
7724 This is needed in case a TU was emitted without its skeleton.
7725 Note: This can't be done until we know what all the DWO files are. */
7726
7727static void
ed2dc618 7728process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7729{
7730 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7731 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7732 && dwarf2_per_objfile->dwo_files != NULL)
7733 {
51ac9db5 7734 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7735 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7736 dwarf2_per_objfile);
6aa5f3a6 7737 }
348e048f
DE
7738}
7739
ed2dc618 7740/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7741
7742static void
ed2dc618 7743set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7744{
b76e467d 7745 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7746 {
891813be 7747 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7748
36586728
TT
7749 if (pst == NULL)
7750 continue;
7751
b76e467d 7752 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7753 {
7754 /* Set the 'user' field only if it is not already set. */
7755 if (pst->dependencies[j]->user == NULL)
7756 pst->dependencies[j]->user = pst;
7757 }
7758 }
7759}
7760
93311388
DE
7761/* Build the partial symbol table by doing a quick pass through the
7762 .debug_info and .debug_abbrev sections. */
72bf9492 7763
93311388 7764static void
ed2dc618 7765dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7766{
ed2dc618 7767 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7768
b4f54984 7769 if (dwarf_read_debug)
45cfd468
DE
7770 {
7771 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7772 objfile_name (objfile));
45cfd468
DE
7773 }
7774
76935768
TT
7775 scoped_restore restore_reading_psyms
7776 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7777 true);
98bfdba5 7778
96b79293 7779 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7780
93311388
DE
7781 /* Any cached compilation units will be linked by the per-objfile
7782 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7783 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7784
ed2dc618 7785 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7786
ed2dc618 7787 create_all_comp_units (dwarf2_per_objfile);
c906108c 7788
60606b2c
TT
7789 /* Create a temporary address map on a temporary obstack. We later
7790 copy this to the final obstack. */
8268c778 7791 auto_obstack temp_obstack;
791afaa2
TT
7792
7793 scoped_restore save_psymtabs_addrmap
d320c2b5 7794 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7795 addrmap_create_mutable (&temp_obstack));
72bf9492 7796
b76e467d 7797 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7798 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7799
6aa5f3a6 7800 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7801 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7802
7803 /* Now that all TUs have been processed we can fill in the dependencies. */
7804 if (dwarf2_per_objfile->type_unit_groups != NULL)
7805 {
eaa5fa8b 7806 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7807 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7808 }
7809
b4f54984 7810 if (dwarf_read_debug)
ed2dc618 7811 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7812
ed2dc618 7813 set_partial_user (dwarf2_per_objfile);
95554aad 7814
d320c2b5
TT
7815 objfile->partial_symtabs->psymtabs_addrmap
7816 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7817 objfile->partial_symtabs->obstack ());
791afaa2
TT
7818 /* At this point we want to keep the address map. */
7819 save_psymtabs_addrmap.release ();
ff013f42 7820
b4f54984 7821 if (dwarf_read_debug)
45cfd468 7822 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7823 objfile_name (objfile));
ae038cb0
DJ
7824}
7825
dee91e82
DE
7826/* Load the partial DIEs for a secondary CU into memory.
7827 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7828
dee91e82
DE
7829static void
7830load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7831{
6751ebae 7832 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7833
7834 if (!reader.dummy_p)
7835 {
7836 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7837 language_minimal);
7838
7839 /* Check if comp unit has_children.
7840 If so, read the rest of the partial symbols from this comp unit.
7841 If not, there's no more debug_info for this comp unit. */
3e225074 7842 if (reader.comp_unit_die->has_children)
c0ab21c2 7843 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7844
7845 reader.keep ();
c0ab21c2 7846 }
ae038cb0
DJ
7847}
7848
ae038cb0 7849static void
ed2dc618 7850read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7851 struct dwarf2_section_info *section,
f1902523 7852 struct dwarf2_section_info *abbrev_section,
b76e467d 7853 unsigned int is_dwz)
ae038cb0 7854{
d521ce57 7855 const gdb_byte *info_ptr;
ed2dc618 7856 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7857
b4f54984 7858 if (dwarf_read_debug)
bf6af496 7859 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7860 section->get_name (),
7861 section->get_file_name ());
bf6af496 7862
96b79293 7863 section->read (objfile);
ae038cb0 7864
36586728 7865 info_ptr = section->buffer;
6e70227d 7866
36586728 7867 while (info_ptr < section->buffer + section->size)
ae038cb0 7868 {
ae038cb0 7869 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7870
9c541725 7871 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7872
f1902523 7873 comp_unit_head cu_header;
ed2dc618
SM
7874 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7875 abbrev_section, info_ptr,
7876 rcuh_kind::COMPILE);
ae038cb0
DJ
7877
7878 /* Save the compilation unit for later lookup. */
f1902523
JK
7879 if (cu_header.unit_type != DW_UT_type)
7880 {
7881 this_cu = XOBNEW (&objfile->objfile_obstack,
7882 struct dwarf2_per_cu_data);
7883 memset (this_cu, 0, sizeof (*this_cu));
7884 }
7885 else
7886 {
7887 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7888 struct signatured_type);
7889 memset (sig_type, 0, sizeof (*sig_type));
7890 sig_type->signature = cu_header.signature;
7891 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7892 this_cu = &sig_type->per_cu;
7893 }
7894 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7895 this_cu->sect_off = sect_off;
f1902523 7896 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7897 this_cu->is_dwz = is_dwz;
e3b94546 7898 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7899 this_cu->section = section;
ae038cb0 7900
b76e467d 7901 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7902
7903 info_ptr = info_ptr + this_cu->length;
7904 }
36586728
TT
7905}
7906
7907/* Create a list of all compilation units in OBJFILE.
7908 This is only done for -readnow and building partial symtabs. */
7909
7910static void
ed2dc618 7911create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7912{
b76e467d 7913 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7914 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7915 &dwarf2_per_objfile->abbrev, 0);
36586728 7916
b76e467d 7917 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7918 if (dwz != NULL)
ed2dc618 7919 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7920 1);
c906108c
SS
7921}
7922
5734ee8b 7923/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7924 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7925 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7926 DW_AT_ranges). See the comments of add_partial_subprogram on how
7927 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7928
72bf9492
DJ
7929static void
7930scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7931 CORE_ADDR *highpc, int set_addrmap,
7932 struct dwarf2_cu *cu)
c906108c 7933{
72bf9492 7934 struct partial_die_info *pdi;
c906108c 7935
91c24f0a
DC
7936 /* Now, march along the PDI's, descending into ones which have
7937 interesting children but skipping the children of the other ones,
7938 until we reach the end of the compilation unit. */
c906108c 7939
72bf9492 7940 pdi = first_die;
91c24f0a 7941
72bf9492
DJ
7942 while (pdi != NULL)
7943 {
52356b79 7944 pdi->fixup (cu);
c906108c 7945
f55ee35c 7946 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7947 children, so we need to look at them. Ditto for anonymous
7948 enums. */
933c6fe4 7949
72bf9492 7950 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7951 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7952 || pdi->tag == DW_TAG_imported_unit
7953 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7954 {
72bf9492 7955 switch (pdi->tag)
c906108c
SS
7956 {
7957 case DW_TAG_subprogram:
b1dc1806 7958 case DW_TAG_inlined_subroutine:
cdc07690 7959 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7960 break;
72929c62 7961 case DW_TAG_constant:
c906108c
SS
7962 case DW_TAG_variable:
7963 case DW_TAG_typedef:
91c24f0a 7964 case DW_TAG_union_type:
72bf9492 7965 if (!pdi->is_declaration)
63d06c5c 7966 {
72bf9492 7967 add_partial_symbol (pdi, cu);
63d06c5c
DC
7968 }
7969 break;
c906108c 7970 case DW_TAG_class_type:
680b30c7 7971 case DW_TAG_interface_type:
c906108c 7972 case DW_TAG_structure_type:
72bf9492 7973 if (!pdi->is_declaration)
c906108c 7974 {
72bf9492 7975 add_partial_symbol (pdi, cu);
c906108c 7976 }
b7fee5a3
KS
7977 if ((cu->language == language_rust
7978 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7979 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7980 set_addrmap, cu);
c906108c 7981 break;
91c24f0a 7982 case DW_TAG_enumeration_type:
72bf9492
DJ
7983 if (!pdi->is_declaration)
7984 add_partial_enumeration (pdi, cu);
c906108c
SS
7985 break;
7986 case DW_TAG_base_type:
a02abb62 7987 case DW_TAG_subrange_type:
c906108c 7988 /* File scope base type definitions are added to the partial
c5aa993b 7989 symbol table. */
72bf9492 7990 add_partial_symbol (pdi, cu);
c906108c 7991 break;
d9fa45fe 7992 case DW_TAG_namespace:
cdc07690 7993 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7994 break;
5d7cb8df 7995 case DW_TAG_module:
59c35742
AB
7996 if (!pdi->is_declaration)
7997 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7998 break;
95554aad
TT
7999 case DW_TAG_imported_unit:
8000 {
8001 struct dwarf2_per_cu_data *per_cu;
8002
f4dc4d17
DE
8003 /* For now we don't handle imported units in type units. */
8004 if (cu->per_cu->is_debug_types)
8005 {
8006 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8007 " supported in type units [in module %s]"),
518817b3 8008 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8009 }
8010
e3b94546
SM
8011 per_cu = dwarf2_find_containing_comp_unit
8012 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8013 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8014
8015 /* Go read the partial unit, if needed. */
8016 if (per_cu->v.psymtab == NULL)
135f5437 8017 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8018
ae640021 8019 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8020 }
8021 break;
74921315
KS
8022 case DW_TAG_imported_declaration:
8023 add_partial_symbol (pdi, cu);
8024 break;
c906108c
SS
8025 default:
8026 break;
8027 }
8028 }
8029
72bf9492
DJ
8030 /* If the die has a sibling, skip to the sibling. */
8031
8032 pdi = pdi->die_sibling;
8033 }
8034}
8035
8036/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8037
72bf9492 8038 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8039 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8040 Enumerators are an exception; they use the scope of their parent
8041 enumeration type, i.e. the name of the enumeration type is not
8042 prepended to the enumerator.
91c24f0a 8043
72bf9492
DJ
8044 There are two complexities. One is DW_AT_specification; in this
8045 case "parent" means the parent of the target of the specification,
8046 instead of the direct parent of the DIE. The other is compilers
8047 which do not emit DW_TAG_namespace; in this case we try to guess
8048 the fully qualified name of structure types from their members'
8049 linkage names. This must be done using the DIE's children rather
8050 than the children of any DW_AT_specification target. We only need
8051 to do this for structures at the top level, i.e. if the target of
8052 any DW_AT_specification (if any; otherwise the DIE itself) does not
8053 have a parent. */
8054
8055/* Compute the scope prefix associated with PDI's parent, in
8056 compilation unit CU. The result will be allocated on CU's
8057 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8058 field. NULL is returned if no prefix is necessary. */
15d034d0 8059static const char *
72bf9492
DJ
8060partial_die_parent_scope (struct partial_die_info *pdi,
8061 struct dwarf2_cu *cu)
8062{
15d034d0 8063 const char *grandparent_scope;
72bf9492 8064 struct partial_die_info *parent, *real_pdi;
91c24f0a 8065
72bf9492
DJ
8066 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8067 then this means the parent of the specification DIE. */
8068
8069 real_pdi = pdi;
72bf9492 8070 while (real_pdi->has_specification)
fb816e8b 8071 {
122cf0f2
AB
8072 auto res = find_partial_die (real_pdi->spec_offset,
8073 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8074 real_pdi = res.pdi;
8075 cu = res.cu;
8076 }
72bf9492
DJ
8077
8078 parent = real_pdi->die_parent;
8079 if (parent == NULL)
8080 return NULL;
8081
8082 if (parent->scope_set)
8083 return parent->scope;
8084
52356b79 8085 parent->fixup (cu);
72bf9492 8086
10b3939b 8087 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8088
acebe513
UW
8089 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8090 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8091 Work around this problem here. */
8092 if (cu->language == language_cplus
6e70227d 8093 && parent->tag == DW_TAG_namespace
acebe513
UW
8094 && strcmp (parent->name, "::") == 0
8095 && grandparent_scope == NULL)
8096 {
8097 parent->scope = NULL;
8098 parent->scope_set = 1;
8099 return NULL;
8100 }
8101
0a4b0913 8102 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8103 if (pdi->tag == DW_TAG_enumerator)
8104 /* Enumerators should not get the name of the enumeration as a prefix. */
8105 parent->scope = grandparent_scope;
8106 else if (parent->tag == DW_TAG_namespace
f55ee35c 8107 || parent->tag == DW_TAG_module
72bf9492
DJ
8108 || parent->tag == DW_TAG_structure_type
8109 || parent->tag == DW_TAG_class_type
680b30c7 8110 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8111 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8112 || parent->tag == DW_TAG_enumeration_type
8113 || (cu->language == language_fortran
8114 && parent->tag == DW_TAG_subprogram
8115 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8116 {
8117 if (grandparent_scope == NULL)
8118 parent->scope = parent->name;
8119 else
3e43a32a
MS
8120 parent->scope = typename_concat (&cu->comp_unit_obstack,
8121 grandparent_scope,
f55ee35c 8122 parent->name, 0, cu);
72bf9492 8123 }
72bf9492
DJ
8124 else
8125 {
8126 /* FIXME drow/2004-04-01: What should we be doing with
8127 function-local names? For partial symbols, we should probably be
8128 ignoring them. */
fa9c3fa0
TT
8129 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8130 dwarf_tag_name (parent->tag),
8131 sect_offset_str (pdi->sect_off));
72bf9492 8132 parent->scope = grandparent_scope;
c906108c
SS
8133 }
8134
72bf9492
DJ
8135 parent->scope_set = 1;
8136 return parent->scope;
8137}
8138
8139/* Return the fully scoped name associated with PDI, from compilation unit
8140 CU. The result will be allocated with malloc. */
4568ecf9 8141
43816ebc 8142static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8143partial_die_full_name (struct partial_die_info *pdi,
8144 struct dwarf2_cu *cu)
8145{
15d034d0 8146 const char *parent_scope;
72bf9492 8147
98bfdba5
PA
8148 /* If this is a template instantiation, we can not work out the
8149 template arguments from partial DIEs. So, unfortunately, we have
8150 to go through the full DIEs. At least any work we do building
8151 types here will be reused if full symbols are loaded later. */
8152 if (pdi->has_template_arguments)
8153 {
52356b79 8154 pdi->fixup (cu);
98bfdba5
PA
8155
8156 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8157 {
8158 struct die_info *die;
8159 struct attribute attr;
8160 struct dwarf2_cu *ref_cu = cu;
8161
b64f50a1 8162 /* DW_FORM_ref_addr is using section offset. */
b4069958 8163 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8164 attr.form = DW_FORM_ref_addr;
9c541725 8165 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8166 die = follow_die_ref (NULL, &attr, &ref_cu);
8167
43816ebc 8168 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8169 }
8170 }
8171
72bf9492
DJ
8172 parent_scope = partial_die_parent_scope (pdi, cu);
8173 if (parent_scope == NULL)
8174 return NULL;
8175 else
43816ebc
TT
8176 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8177 pdi->name, 0, cu));
c906108c
SS
8178}
8179
8180static void
72bf9492 8181add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8182{
518817b3
SM
8183 struct dwarf2_per_objfile *dwarf2_per_objfile
8184 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8185 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8186 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8187 CORE_ADDR addr = 0;
15d034d0 8188 const char *actual_name = NULL;
e142c38c
DJ
8189 CORE_ADDR baseaddr;
8190
b3b3bada 8191 baseaddr = objfile->text_section_offset ();
c906108c 8192
43816ebc
TT
8193 gdb::unique_xmalloc_ptr<char> built_actual_name
8194 = partial_die_full_name (pdi, cu);
15d034d0 8195 if (built_actual_name != NULL)
43816ebc 8196 actual_name = built_actual_name.get ();
63d06c5c 8197
72bf9492
DJ
8198 if (actual_name == NULL)
8199 actual_name = pdi->name;
8200
c906108c
SS
8201 switch (pdi->tag)
8202 {
b1dc1806 8203 case DW_TAG_inlined_subroutine:
c906108c 8204 case DW_TAG_subprogram:
79748972
TT
8205 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8206 - baseaddr);
0a4b0913
AB
8207 if (pdi->is_external
8208 || cu->language == language_ada
8209 || (cu->language == language_fortran
8210 && pdi->die_parent != NULL
8211 && pdi->die_parent->tag == DW_TAG_subprogram))
8212 {
8213 /* Normally, only "external" DIEs are part of the global scope.
8214 But in Ada and Fortran, we want to be able to access nested
8215 procedures globally. So all Ada and Fortran subprograms are
8216 stored in the global scope. */
31edb802 8217 add_psymbol_to_list (actual_name,
15d034d0 8218 built_actual_name != NULL,
f47fb265 8219 VAR_DOMAIN, LOC_BLOCK,
79748972 8220 SECT_OFF_TEXT (objfile),
75aedd27 8221 psymbol_placement::GLOBAL,
79748972
TT
8222 addr,
8223 cu->language, objfile);
c906108c
SS
8224 }
8225 else
8226 {
31edb802 8227 add_psymbol_to_list (actual_name,
15d034d0 8228 built_actual_name != NULL,
f47fb265 8229 VAR_DOMAIN, LOC_BLOCK,
79748972 8230 SECT_OFF_TEXT (objfile),
75aedd27 8231 psymbol_placement::STATIC,
1762568f 8232 addr, cu->language, objfile);
c906108c 8233 }
0c1b455e
TT
8234
8235 if (pdi->main_subprogram && actual_name != NULL)
8236 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8237 break;
72929c62 8238 case DW_TAG_constant:
31edb802 8239 add_psymbol_to_list (actual_name,
75aedd27
TT
8240 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8241 -1, (pdi->is_external
8242 ? psymbol_placement::GLOBAL
8243 : psymbol_placement::STATIC),
8244 0, cu->language, objfile);
72929c62 8245 break;
c906108c 8246 case DW_TAG_variable:
95554aad
TT
8247 if (pdi->d.locdesc)
8248 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8249
95554aad 8250 if (pdi->d.locdesc
caac4577
JG
8251 && addr == 0
8252 && !dwarf2_per_objfile->has_section_at_zero)
8253 {
8254 /* A global or static variable may also have been stripped
8255 out by the linker if unused, in which case its address
8256 will be nullified; do not add such variables into partial
8257 symbol table then. */
8258 }
8259 else if (pdi->is_external)
c906108c
SS
8260 {
8261 /* Global Variable.
8262 Don't enter into the minimal symbol tables as there is
8263 a minimal symbol table entry from the ELF symbols already.
8264 Enter into partial symbol table if it has a location
8265 descriptor or a type.
8266 If the location descriptor is missing, new_symbol will create
8267 a LOC_UNRESOLVED symbol, the address of the variable will then
8268 be determined from the minimal symbol table whenever the variable
8269 is referenced.
8270 The address for the partial symbol table entry is not
8271 used by GDB, but it comes in handy for debugging partial symbol
8272 table building. */
8273
95554aad 8274 if (pdi->d.locdesc || pdi->has_type)
31edb802 8275 add_psymbol_to_list (actual_name,
15d034d0 8276 built_actual_name != NULL,
f47fb265 8277 VAR_DOMAIN, LOC_STATIC,
79748972 8278 SECT_OFF_TEXT (objfile),
75aedd27 8279 psymbol_placement::GLOBAL,
79748972 8280 addr, cu->language, objfile);
c906108c
SS
8281 }
8282 else
8283 {
ff908ebf
AW
8284 int has_loc = pdi->d.locdesc != NULL;
8285
8286 /* Static Variable. Skip symbols whose value we cannot know (those
8287 without location descriptors or constant values). */
8288 if (!has_loc && !pdi->has_const_value)
43816ebc 8289 return;
ff908ebf 8290
31edb802 8291 add_psymbol_to_list (actual_name,
15d034d0 8292 built_actual_name != NULL,
f47fb265 8293 VAR_DOMAIN, LOC_STATIC,
79748972 8294 SECT_OFF_TEXT (objfile),
75aedd27 8295 psymbol_placement::STATIC,
79748972 8296 has_loc ? addr : 0,
f47fb265 8297 cu->language, objfile);
c906108c
SS
8298 }
8299 break;
8300 case DW_TAG_typedef:
8301 case DW_TAG_base_type:
a02abb62 8302 case DW_TAG_subrange_type:
31edb802 8303 add_psymbol_to_list (actual_name,
15d034d0 8304 built_actual_name != NULL,
79748972 8305 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8306 psymbol_placement::STATIC,
1762568f 8307 0, cu->language, objfile);
c906108c 8308 break;
74921315 8309 case DW_TAG_imported_declaration:
72bf9492 8310 case DW_TAG_namespace:
31edb802 8311 add_psymbol_to_list (actual_name,
15d034d0 8312 built_actual_name != NULL,
79748972 8313 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8314 psymbol_placement::GLOBAL,
1762568f 8315 0, cu->language, objfile);
72bf9492 8316 break;
530e8392 8317 case DW_TAG_module:
a5fd13a9
BH
8318 /* With Fortran 77 there might be a "BLOCK DATA" module
8319 available without any name. If so, we skip the module as it
8320 doesn't bring any value. */
8321 if (actual_name != nullptr)
31edb802 8322 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8323 built_actual_name != NULL,
8324 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8325 psymbol_placement::GLOBAL,
8326 0, cu->language, objfile);
530e8392 8327 break;
c906108c 8328 case DW_TAG_class_type:
680b30c7 8329 case DW_TAG_interface_type:
c906108c
SS
8330 case DW_TAG_structure_type:
8331 case DW_TAG_union_type:
8332 case DW_TAG_enumeration_type:
fa4028e9
JB
8333 /* Skip external references. The DWARF standard says in the section
8334 about "Structure, Union, and Class Type Entries": "An incomplete
8335 structure, union or class type is represented by a structure,
8336 union or class entry that does not have a byte size attribute
8337 and that has a DW_AT_declaration attribute." */
8338 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8339 return;
fa4028e9 8340
63d06c5c
DC
8341 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8342 static vs. global. */
31edb802 8343 add_psymbol_to_list (actual_name,
15d034d0 8344 built_actual_name != NULL,
79748972 8345 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8346 cu->language == language_cplus
75aedd27
TT
8347 ? psymbol_placement::GLOBAL
8348 : psymbol_placement::STATIC,
1762568f 8349 0, cu->language, objfile);
c906108c 8350
c906108c
SS
8351 break;
8352 case DW_TAG_enumerator:
31edb802 8353 add_psymbol_to_list (actual_name,
15d034d0 8354 built_actual_name != NULL,
79748972 8355 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8356 cu->language == language_cplus
75aedd27
TT
8357 ? psymbol_placement::GLOBAL
8358 : psymbol_placement::STATIC,
1762568f 8359 0, cu->language, objfile);
c906108c
SS
8360 break;
8361 default:
8362 break;
8363 }
8364}
8365
5c4e30ca
DC
8366/* Read a partial die corresponding to a namespace; also, add a symbol
8367 corresponding to that namespace to the symbol table. NAMESPACE is
8368 the name of the enclosing namespace. */
91c24f0a 8369
72bf9492
DJ
8370static void
8371add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8372 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8373 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8374{
72bf9492 8375 /* Add a symbol for the namespace. */
e7c27a73 8376
72bf9492 8377 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8378
8379 /* Now scan partial symbols in that namespace. */
8380
91c24f0a 8381 if (pdi->has_children)
cdc07690 8382 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8383}
8384
5d7cb8df
JK
8385/* Read a partial die corresponding to a Fortran module. */
8386
8387static void
8388add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8389 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8390{
530e8392
KB
8391 /* Add a symbol for the namespace. */
8392
8393 add_partial_symbol (pdi, cu);
8394
f55ee35c 8395 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8396
8397 if (pdi->has_children)
cdc07690 8398 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8399}
8400
b1dc1806
XR
8401/* Read a partial die corresponding to a subprogram or an inlined
8402 subprogram and create a partial symbol for that subprogram.
8403 When the CU language allows it, this routine also defines a partial
8404 symbol for each nested subprogram that this subprogram contains.
8405 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8406 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8407
cdc07690
YQ
8408 PDI may also be a lexical block, in which case we simply search
8409 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8410 Again, this is only performed when the CU language allows this
8411 type of definitions. */
8412
8413static void
8414add_partial_subprogram (struct partial_die_info *pdi,
8415 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8416 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8417{
b1dc1806 8418 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8419 {
8420 if (pdi->has_pc_info)
8421 {
8422 if (pdi->lowpc < *lowpc)
8423 *lowpc = pdi->lowpc;
8424 if (pdi->highpc > *highpc)
8425 *highpc = pdi->highpc;
cdc07690 8426 if (set_addrmap)
5734ee8b 8427 {
518817b3 8428 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8429 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8430 CORE_ADDR baseaddr;
b926417a
TT
8431 CORE_ADDR this_highpc;
8432 CORE_ADDR this_lowpc;
5734ee8b 8433
b3b3bada 8434 baseaddr = objfile->text_section_offset ();
b926417a
TT
8435 this_lowpc
8436 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8437 pdi->lowpc + baseaddr)
8438 - baseaddr);
8439 this_highpc
8440 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8441 pdi->highpc + baseaddr)
8442 - baseaddr);
d320c2b5 8443 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8444 this_lowpc, this_highpc - 1,
9291a0cd 8445 cu->per_cu->v.psymtab);
5734ee8b 8446 }
481860b3
GB
8447 }
8448
8449 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8450 {
bc30ff58 8451 if (!pdi->is_declaration)
e8d05480
JB
8452 /* Ignore subprogram DIEs that do not have a name, they are
8453 illegal. Do not emit a complaint at this point, we will
8454 do so when we convert this psymtab into a symtab. */
8455 if (pdi->name)
8456 add_partial_symbol (pdi, cu);
bc30ff58
JB
8457 }
8458 }
6e70227d 8459
bc30ff58
JB
8460 if (! pdi->has_children)
8461 return;
8462
0a4b0913 8463 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8464 {
8465 pdi = pdi->die_child;
8466 while (pdi != NULL)
8467 {
52356b79 8468 pdi->fixup (cu);
bc30ff58 8469 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8470 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8471 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8472 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8473 pdi = pdi->die_sibling;
8474 }
8475 }
8476}
8477
91c24f0a
DC
8478/* Read a partial die corresponding to an enumeration type. */
8479
72bf9492
DJ
8480static void
8481add_partial_enumeration (struct partial_die_info *enum_pdi,
8482 struct dwarf2_cu *cu)
91c24f0a 8483{
72bf9492 8484 struct partial_die_info *pdi;
91c24f0a
DC
8485
8486 if (enum_pdi->name != NULL)
72bf9492
DJ
8487 add_partial_symbol (enum_pdi, cu);
8488
8489 pdi = enum_pdi->die_child;
8490 while (pdi)
91c24f0a 8491 {
72bf9492 8492 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8493 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8494 else
72bf9492
DJ
8495 add_partial_symbol (pdi, cu);
8496 pdi = pdi->die_sibling;
91c24f0a 8497 }
91c24f0a
DC
8498}
8499
6caca83c
CC
8500/* Return the initial uleb128 in the die at INFO_PTR. */
8501
8502static unsigned int
d521ce57 8503peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8504{
8505 unsigned int bytes_read;
8506
8507 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8508}
8509
685af9cd
TT
8510/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8511 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8512
4bb7a0a7
DJ
8513 Return the corresponding abbrev, or NULL if the number is zero (indicating
8514 an empty DIE). In either case *BYTES_READ will be set to the length of
8515 the initial number. */
8516
8517static struct abbrev_info *
685af9cd
TT
8518peek_die_abbrev (const die_reader_specs &reader,
8519 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8520{
685af9cd 8521 dwarf2_cu *cu = reader.cu;
518817b3 8522 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8523 unsigned int abbrev_number
8524 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8525
8526 if (abbrev_number == 0)
8527 return NULL;
8528
685af9cd 8529 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8530 if (!abbrev)
8531 {
422b9917 8532 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8533 " at offset %s [in module %s]"),
422b9917 8534 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8535 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8536 }
8537
8538 return abbrev;
8539}
8540
93311388
DE
8541/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8542 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8543 DIE. Any children of the skipped DIEs will also be skipped. */
8544
d521ce57
TT
8545static const gdb_byte *
8546skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8547{
4bb7a0a7
DJ
8548 while (1)
8549 {
685af9cd
TT
8550 unsigned int bytes_read;
8551 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8552
4bb7a0a7
DJ
8553 if (abbrev == NULL)
8554 return info_ptr + bytes_read;
8555 else
dee91e82 8556 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8557 }
8558}
8559
93311388
DE
8560/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8561 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8562 abbrev corresponding to that skipped uleb128 should be passed in
8563 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8564 children. */
8565
d521ce57
TT
8566static const gdb_byte *
8567skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8568 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8569{
8570 unsigned int bytes_read;
8571 struct attribute attr;
dee91e82
DE
8572 bfd *abfd = reader->abfd;
8573 struct dwarf2_cu *cu = reader->cu;
d521ce57 8574 const gdb_byte *buffer = reader->buffer;
f664829e 8575 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8576 unsigned int form, i;
8577
8578 for (i = 0; i < abbrev->num_attrs; i++)
8579 {
8580 /* The only abbrev we care about is DW_AT_sibling. */
8581 if (abbrev->attrs[i].name == DW_AT_sibling)
8582 {
18a8505e
AT
8583 bool ignored;
8584 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8585 &ignored);
4bb7a0a7 8586 if (attr.form == DW_FORM_ref_addr)
b98664d3 8587 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8588 else
b9502d3f 8589 {
9c541725
PA
8590 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8591 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8592
8593 if (sibling_ptr < info_ptr)
b98664d3 8594 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
8595 else if (sibling_ptr > reader->buffer_end)
8596 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8597 else
8598 return sibling_ptr;
8599 }
4bb7a0a7
DJ
8600 }
8601
8602 /* If it isn't DW_AT_sibling, skip this attribute. */
8603 form = abbrev->attrs[i].form;
8604 skip_attribute:
8605 switch (form)
8606 {
4bb7a0a7 8607 case DW_FORM_ref_addr:
ae411497
TT
8608 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8609 and later it is offset sized. */
8610 if (cu->header.version == 2)
8611 info_ptr += cu->header.addr_size;
8612 else
8613 info_ptr += cu->header.offset_size;
8614 break;
36586728
TT
8615 case DW_FORM_GNU_ref_alt:
8616 info_ptr += cu->header.offset_size;
8617 break;
ae411497 8618 case DW_FORM_addr:
4bb7a0a7
DJ
8619 info_ptr += cu->header.addr_size;
8620 break;
8621 case DW_FORM_data1:
8622 case DW_FORM_ref1:
8623 case DW_FORM_flag:
8fe0f950 8624 case DW_FORM_strx1:
4bb7a0a7
DJ
8625 info_ptr += 1;
8626 break;
2dc7f7b3 8627 case DW_FORM_flag_present:
43988095 8628 case DW_FORM_implicit_const:
2dc7f7b3 8629 break;
4bb7a0a7
DJ
8630 case DW_FORM_data2:
8631 case DW_FORM_ref2:
8fe0f950 8632 case DW_FORM_strx2:
4bb7a0a7
DJ
8633 info_ptr += 2;
8634 break;
8fe0f950
AT
8635 case DW_FORM_strx3:
8636 info_ptr += 3;
8637 break;
4bb7a0a7
DJ
8638 case DW_FORM_data4:
8639 case DW_FORM_ref4:
8fe0f950 8640 case DW_FORM_strx4:
4bb7a0a7
DJ
8641 info_ptr += 4;
8642 break;
8643 case DW_FORM_data8:
8644 case DW_FORM_ref8:
55f1336d 8645 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8646 info_ptr += 8;
8647 break;
0224619f
JK
8648 case DW_FORM_data16:
8649 info_ptr += 16;
8650 break;
4bb7a0a7 8651 case DW_FORM_string:
9b1c24c8 8652 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8653 info_ptr += bytes_read;
8654 break;
2dc7f7b3 8655 case DW_FORM_sec_offset:
4bb7a0a7 8656 case DW_FORM_strp:
36586728 8657 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8658 info_ptr += cu->header.offset_size;
8659 break;
2dc7f7b3 8660 case DW_FORM_exprloc:
4bb7a0a7
DJ
8661 case DW_FORM_block:
8662 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8663 info_ptr += bytes_read;
8664 break;
8665 case DW_FORM_block1:
8666 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8667 break;
8668 case DW_FORM_block2:
8669 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8670 break;
8671 case DW_FORM_block4:
8672 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8673 break;
336d760d 8674 case DW_FORM_addrx:
cf532bd1 8675 case DW_FORM_strx:
4bb7a0a7
DJ
8676 case DW_FORM_sdata:
8677 case DW_FORM_udata:
8678 case DW_FORM_ref_udata:
3019eac3
DE
8679 case DW_FORM_GNU_addr_index:
8680 case DW_FORM_GNU_str_index:
18a8505e 8681 case DW_FORM_rnglistx:
d521ce57 8682 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8683 break;
8684 case DW_FORM_indirect:
8685 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8686 info_ptr += bytes_read;
8687 /* We need to continue parsing from here, so just go back to
8688 the top. */
8689 goto skip_attribute;
8690
8691 default:
3e43a32a
MS
8692 error (_("Dwarf Error: Cannot handle %s "
8693 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8694 dwarf_form_name (form),
8695 bfd_get_filename (abfd));
8696 }
8697 }
8698
8699 if (abbrev->has_children)
dee91e82 8700 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8701 else
8702 return info_ptr;
8703}
8704
93311388 8705/* Locate ORIG_PDI's sibling.
dee91e82 8706 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8707
d521ce57 8708static const gdb_byte *
dee91e82
DE
8709locate_pdi_sibling (const struct die_reader_specs *reader,
8710 struct partial_die_info *orig_pdi,
d521ce57 8711 const gdb_byte *info_ptr)
91c24f0a
DC
8712{
8713 /* Do we know the sibling already? */
72bf9492 8714
91c24f0a
DC
8715 if (orig_pdi->sibling)
8716 return orig_pdi->sibling;
8717
8718 /* Are there any children to deal with? */
8719
8720 if (!orig_pdi->has_children)
8721 return info_ptr;
8722
4bb7a0a7 8723 /* Skip the children the long way. */
91c24f0a 8724
dee91e82 8725 return skip_children (reader, info_ptr);
91c24f0a
DC
8726}
8727
257e7a09 8728/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8729 not NULL. */
c906108c 8730
891813be
TT
8731void
8732dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8733{
ed2dc618
SM
8734 struct dwarf2_per_objfile *dwarf2_per_objfile
8735 = get_dwarf2_per_objfile (objfile);
8736
077cbab2
TT
8737 gdb_assert (!readin);
8738 /* If this psymtab is constructed from a debug-only objfile, the
8739 has_section_at_zero flag will not necessarily be correct. We
8740 can get the correct value for this flag by looking at the data
8741 associated with the (presumably stripped) associated objfile. */
8742 if (objfile->separate_debug_objfile_backlink)
c906108c 8743 {
077cbab2
TT
8744 struct dwarf2_per_objfile *dpo_backlink
8745 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8746
077cbab2
TT
8747 dwarf2_per_objfile->has_section_at_zero
8748 = dpo_backlink->has_section_at_zero;
8749 }
98bfdba5 8750
8566b89b 8751 expand_psymtab (objfile);
95554aad 8752
ed2dc618 8753 process_cu_includes (dwarf2_per_objfile);
c906108c 8754}
9cdd5dbd
DE
8755\f
8756/* Reading in full CUs. */
c906108c 8757
10b3939b
DJ
8758/* Add PER_CU to the queue. */
8759
8760static void
95554aad
TT
8761queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8762 enum language pretend_language)
10b3939b 8763{
10b3939b 8764 per_cu->queued = 1;
39856def 8765 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8766}
8767
89e63ee4
DE
8768/* If PER_CU is not yet queued, add it to the queue.
8769 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8770 dependency.
0907af0c 8771 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8772 meaning either PER_CU is already queued or it is already loaded.
8773
8774 N.B. There is an invariant here that if a CU is queued then it is loaded.
8775 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8776
8777static int
89e63ee4 8778maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8779 struct dwarf2_per_cu_data *per_cu,
8780 enum language pretend_language)
8781{
8782 /* We may arrive here during partial symbol reading, if we need full
8783 DIEs to process an unusual case (e.g. template arguments). Do
8784 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8785 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8786 {
8787 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8788 return 1;
8789 return 0;
8790 }
8791
8792 /* Mark the dependence relation so that we don't flush PER_CU
8793 too early. */
89e63ee4
DE
8794 if (dependent_cu != NULL)
8795 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8796
8797 /* If it's already on the queue, we have nothing to do. */
8798 if (per_cu->queued)
8799 return 0;
8800
8801 /* If the compilation unit is already loaded, just mark it as
8802 used. */
8803 if (per_cu->cu != NULL)
8804 {
8805 per_cu->cu->last_used = 0;
8806 return 0;
8807 }
8808
8809 /* Add it to the queue. */
8810 queue_comp_unit (per_cu, pretend_language);
8811
8812 return 1;
8813}
8814
10b3939b
DJ
8815/* Process the queue. */
8816
8817static void
ed2dc618 8818process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8819{
b4f54984 8820 if (dwarf_read_debug)
45cfd468
DE
8821 {
8822 fprintf_unfiltered (gdb_stdlog,
8823 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8824 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8825 }
8826
03dd20cc
DJ
8827 /* The queue starts out with one item, but following a DIE reference
8828 may load a new CU, adding it to the end of the queue. */
39856def 8829 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8830 {
39856def
TT
8831 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8832
cc12ce38 8833 if ((dwarf2_per_objfile->using_index
39856def
TT
8834 ? !item.per_cu->v.quick->compunit_symtab
8835 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8836 /* Skip dummy CUs. */
39856def 8837 && item.per_cu->cu != NULL)
f4dc4d17 8838 {
39856def 8839 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8840 unsigned int debug_print_threshold;
247f5c4f 8841 char buf[100];
f4dc4d17 8842
247f5c4f 8843 if (per_cu->is_debug_types)
f4dc4d17 8844 {
247f5c4f
DE
8845 struct signatured_type *sig_type =
8846 (struct signatured_type *) per_cu;
8847
9d8780f0 8848 sprintf (buf, "TU %s at offset %s",
73be47f5 8849 hex_string (sig_type->signature),
9d8780f0 8850 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8851 /* There can be 100s of TUs.
8852 Only print them in verbose mode. */
8853 debug_print_threshold = 2;
f4dc4d17 8854 }
247f5c4f 8855 else
73be47f5 8856 {
9d8780f0
SM
8857 sprintf (buf, "CU at offset %s",
8858 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8859 debug_print_threshold = 1;
8860 }
247f5c4f 8861
b4f54984 8862 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8863 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8864
8865 if (per_cu->is_debug_types)
39856def 8866 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8867 else
39856def 8868 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8869
b4f54984 8870 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8871 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8872 }
10b3939b 8873
39856def
TT
8874 item.per_cu->queued = 0;
8875 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8876 }
8877
b4f54984 8878 if (dwarf_read_debug)
45cfd468
DE
8879 {
8880 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8881 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8882 }
10b3939b
DJ
8883}
8884
10b3939b
DJ
8885/* Read in full symbols for PST, and anything it depends on. */
8886
8566b89b
TT
8887void
8888dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8889{
8566b89b 8890 if (readin)
95554aad
TT
8891 return;
8892
0494dbec 8893 read_dependencies (objfile);
aaa75496 8894
b83470bf
TT
8895 dw2_do_instantiate_symtab (per_cu_data, false);
8896 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8897}
8898
dee91e82
DE
8899/* Trivial hash function for die_info: the hash value of a DIE
8900 is its offset in .debug_info for this objfile. */
10b3939b 8901
dee91e82
DE
8902static hashval_t
8903die_hash (const void *item)
10b3939b 8904{
9a3c8263 8905 const struct die_info *die = (const struct die_info *) item;
6502dd73 8906
9c541725 8907 return to_underlying (die->sect_off);
dee91e82 8908}
63d06c5c 8909
dee91e82
DE
8910/* Trivial comparison function for die_info structures: two DIEs
8911 are equal if they have the same offset. */
98bfdba5 8912
dee91e82
DE
8913static int
8914die_eq (const void *item_lhs, const void *item_rhs)
8915{
9a3c8263
SM
8916 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8917 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8918
9c541725 8919 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8920}
c906108c 8921
c0ab21c2 8922/* Load the DIEs associated with PER_CU into memory. */
c906108c 8923
dee91e82 8924static void
c0ab21c2
TT
8925load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8926 bool skip_partial,
8927 enum language pretend_language)
dee91e82 8928{
c0ab21c2
TT
8929 gdb_assert (! this_cu->is_debug_types);
8930
6751ebae 8931 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8932 if (reader.dummy_p)
8933 return;
8934
8935 struct dwarf2_cu *cu = reader.cu;
8936 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8937
dee91e82
DE
8938 gdb_assert (cu->die_hash == NULL);
8939 cu->die_hash =
8940 htab_create_alloc_ex (cu->header.length / 12,
8941 die_hash,
8942 die_eq,
8943 NULL,
8944 &cu->comp_unit_obstack,
8945 hashtab_obstack_allocate,
8946 dummy_obstack_deallocate);
e142c38c 8947
3e225074 8948 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8949 reader.comp_unit_die->child
8950 = read_die_and_siblings (&reader, reader.info_ptr,
8951 &info_ptr, reader.comp_unit_die);
8952 cu->dies = reader.comp_unit_die;
dee91e82 8953 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8954
8955 /* We try not to read any attributes in this function, because not
9cdd5dbd 8956 all CUs needed for references have been loaded yet, and symbol
10b3939b 8957 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8958 or we won't be able to build types correctly.
8959 Similarly, if we do not read the producer, we can not apply
8960 producer-specific interpretation. */
c0ab21c2 8961 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8962
8963 reader.keep ();
10b3939b
DJ
8964}
8965
3da10d80
KS
8966/* Add a DIE to the delayed physname list. */
8967
8968static void
8969add_to_method_list (struct type *type, int fnfield_index, int index,
8970 const char *name, struct die_info *die,
8971 struct dwarf2_cu *cu)
8972{
8973 struct delayed_method_info mi;
8974 mi.type = type;
8975 mi.fnfield_index = fnfield_index;
8976 mi.index = index;
8977 mi.name = name;
8978 mi.die = die;
c89b44cd 8979 cu->method_list.push_back (mi);
3da10d80
KS
8980}
8981
3693fdb3
PA
8982/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8983 "const" / "volatile". If so, decrements LEN by the length of the
8984 modifier and return true. Otherwise return false. */
8985
8986template<size_t N>
8987static bool
8988check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8989{
8990 size_t mod_len = sizeof (mod) - 1;
8991 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8992 {
8993 len -= mod_len;
8994 return true;
8995 }
8996 return false;
8997}
8998
3da10d80
KS
8999/* Compute the physnames of any methods on the CU's method list.
9000
9001 The computation of method physnames is delayed in order to avoid the
9002 (bad) condition that one of the method's formal parameters is of an as yet
9003 incomplete type. */
9004
9005static void
9006compute_delayed_physnames (struct dwarf2_cu *cu)
9007{
3693fdb3 9008 /* Only C++ delays computing physnames. */
c89b44cd 9009 if (cu->method_list.empty ())
3693fdb3
PA
9010 return;
9011 gdb_assert (cu->language == language_cplus);
9012
52941706 9013 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9014 {
1d06ead6 9015 const char *physname;
3da10d80 9016 struct fn_fieldlist *fn_flp
c89b44cd
TT
9017 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9018 physname = dwarf2_physname (mi.name, mi.die, cu);
9019 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9020 = physname ? physname : "";
3693fdb3
PA
9021
9022 /* Since there's no tag to indicate whether a method is a
9023 const/volatile overload, extract that information out of the
9024 demangled name. */
9025 if (physname != NULL)
9026 {
9027 size_t len = strlen (physname);
9028
9029 while (1)
9030 {
9031 if (physname[len] == ')') /* shortcut */
9032 break;
9033 else if (check_modifier (physname, len, " const"))
c89b44cd 9034 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9035 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9036 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9037 else
9038 break;
9039 }
9040 }
3da10d80 9041 }
c89b44cd
TT
9042
9043 /* The list is no longer needed. */
9044 cu->method_list.clear ();
3da10d80
KS
9045}
9046
a766d390
DE
9047/* Go objects should be embedded in a DW_TAG_module DIE,
9048 and it's not clear if/how imported objects will appear.
9049 To keep Go support simple until that's worked out,
9050 go back through what we've read and create something usable.
9051 We could do this while processing each DIE, and feels kinda cleaner,
9052 but that way is more invasive.
9053 This is to, for example, allow the user to type "p var" or "b main"
9054 without having to specify the package name, and allow lookups
9055 of module.object to work in contexts that use the expression
9056 parser. */
9057
9058static void
9059fixup_go_packaging (struct dwarf2_cu *cu)
9060{
421d1616 9061 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9062 struct pending *list;
9063 int i;
9064
c24bdb02 9065 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9066 list != NULL;
9067 list = list->next)
a766d390
DE
9068 {
9069 for (i = 0; i < list->nsyms; ++i)
9070 {
9071 struct symbol *sym = list->symbol[i];
9072
c1b5c1eb 9073 if (sym->language () == language_go
a766d390
DE
9074 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9075 {
421d1616
TT
9076 gdb::unique_xmalloc_ptr<char> this_package_name
9077 (go_symbol_package_name (sym));
a766d390
DE
9078
9079 if (this_package_name == NULL)
9080 continue;
9081 if (package_name == NULL)
421d1616 9082 package_name = std::move (this_package_name);
a766d390
DE
9083 else
9084 {
518817b3
SM
9085 struct objfile *objfile
9086 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9087 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9088 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9089 (symbol_symtab (sym) != NULL
9090 ? symtab_to_filename_for_display
9091 (symbol_symtab (sym))
e3b94546 9092 : objfile_name (objfile)),
421d1616 9093 this_package_name.get (), package_name.get ());
a766d390
DE
9094 }
9095 }
9096 }
9097 }
9098
9099 if (package_name != NULL)
9100 {
518817b3 9101 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9102 const char *saved_package_name
421d1616 9103 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9104 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9105 saved_package_name);
a766d390
DE
9106 struct symbol *sym;
9107
e623cf5d 9108 sym = allocate_symbol (objfile);
d3ecddab 9109 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9110 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9111 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9112 e.g., "main" finds the "main" module and not C's main(). */
9113 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9114 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9115 SYMBOL_TYPE (sym) = type;
9116
c24bdb02 9117 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9118 }
9119}
9120
c9317f21
TT
9121/* Allocate a fully-qualified name consisting of the two parts on the
9122 obstack. */
9123
9124static const char *
9125rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9126{
9127 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9128}
9129
9130/* A helper that allocates a struct discriminant_info to attach to a
9131 union type. */
9132
9133static struct discriminant_info *
9134alloc_discriminant_info (struct type *type, int discriminant_index,
9135 int default_index)
9136{
9137 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9138 gdb_assert (discriminant_index == -1
9139 || (discriminant_index >= 0
9140 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9141 gdb_assert (default_index == -1
c7b15a66 9142 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9143
9144 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9145
9146 struct discriminant_info *disc
9147 = ((struct discriminant_info *)
9148 TYPE_ZALLOC (type,
9149 offsetof (struct discriminant_info, discriminants)
9150 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9151 disc->default_index = default_index;
9152 disc->discriminant_index = discriminant_index;
9153
9154 struct dynamic_prop prop;
9155 prop.kind = PROP_UNDEFINED;
9156 prop.data.baton = disc;
9157
9158 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9159
9160 return disc;
9161}
9162
9163/* Some versions of rustc emitted enums in an unusual way.
9164
9165 Ordinary enums were emitted as unions. The first element of each
9166 structure in the union was named "RUST$ENUM$DISR". This element
9167 held the discriminant.
9168
9169 These versions of Rust also implemented the "non-zero"
9170 optimization. When the enum had two values, and one is empty and
9171 the other holds a pointer that cannot be zero, the pointer is used
9172 as the discriminant, with a zero value meaning the empty variant.
9173 Here, the union's first member is of the form
9174 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9175 where the fieldnos are the indices of the fields that should be
9176 traversed in order to find the field (which may be several fields deep)
9177 and the variantname is the name of the variant of the case when the
9178 field is zero.
9179
9180 This function recognizes whether TYPE is of one of these forms,
9181 and, if so, smashes it to be a variant type. */
9182
9183static void
9184quirk_rust_enum (struct type *type, struct objfile *objfile)
9185{
9186 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9187
9188 /* We don't need to deal with empty enums. */
9189 if (TYPE_NFIELDS (type) == 0)
9190 return;
9191
9192#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9193 if (TYPE_NFIELDS (type) == 1
9194 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9195 {
9196 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9197
9198 /* Decode the field name to find the offset of the
9199 discriminant. */
9200 ULONGEST bit_offset = 0;
9201 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9202 while (name[0] >= '0' && name[0] <= '9')
9203 {
9204 char *tail;
9205 unsigned long index = strtoul (name, &tail, 10);
9206 name = tail;
9207 if (*name != '$'
9208 || index >= TYPE_NFIELDS (field_type)
9209 || (TYPE_FIELD_LOC_KIND (field_type, index)
9210 != FIELD_LOC_KIND_BITPOS))
9211 {
b98664d3 9212 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9213 "[in module %s]"),
9214 TYPE_FIELD_NAME (type, 0),
9215 objfile_name (objfile));
9216 return;
9217 }
9218 ++name;
9219
9220 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9221 field_type = TYPE_FIELD_TYPE (field_type, index);
9222 }
9223
9224 /* Make a union to hold the variants. */
9225 struct type *union_type = alloc_type (objfile);
9226 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9227 TYPE_NFIELDS (union_type) = 3;
9228 TYPE_FIELDS (union_type)
9229 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9230 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9231 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9232
9233 /* Put the discriminant must at index 0. */
9234 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9235 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9236 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9237 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9238
9239 /* The order of fields doesn't really matter, so put the real
9240 field at index 1 and the data-less field at index 2. */
9241 struct discriminant_info *disc
9242 = alloc_discriminant_info (union_type, 0, 1);
9243 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9244 TYPE_FIELD_NAME (union_type, 1)
9245 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9246 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9247 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9248 TYPE_FIELD_NAME (union_type, 1));
9249
9250 const char *dataless_name
9251 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9252 name);
9253 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9254 dataless_name);
9255 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9256 /* NAME points into the original discriminant name, which
9257 already has the correct lifetime. */
9258 TYPE_FIELD_NAME (union_type, 2) = name;
9259 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9260 disc->discriminants[2] = 0;
9261
9262 /* Smash this type to be a structure type. We have to do this
9263 because the type has already been recorded. */
9264 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9265 TYPE_NFIELDS (type) = 1;
9266 TYPE_FIELDS (type)
9267 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9268
9269 /* Install the variant part. */
9270 TYPE_FIELD_TYPE (type, 0) = union_type;
9271 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9272 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9273 }
77c2dba3
TT
9274 /* A union with a single anonymous field is probably an old-style
9275 univariant enum. */
9276 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9277 {
c9317f21
TT
9278 /* Smash this type to be a structure type. We have to do this
9279 because the type has already been recorded. */
9280 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9281
9282 /* Make a union to hold the variants. */
9283 struct type *union_type = alloc_type (objfile);
9284 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9285 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9286 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9287 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9288 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9289
9290 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9291 const char *variant_name
9292 = rust_last_path_segment (TYPE_NAME (field_type));
9293 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9294 TYPE_NAME (field_type)
9295 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9296 TYPE_NAME (type), variant_name);
c9317f21
TT
9297
9298 /* Install the union in the outer struct type. */
9299 TYPE_NFIELDS (type) = 1;
9300 TYPE_FIELDS (type)
9301 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9302 TYPE_FIELD_TYPE (type, 0) = union_type;
9303 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9304 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9305
9306 alloc_discriminant_info (union_type, -1, 0);
9307 }
9308 else
9309 {
9310 struct type *disr_type = nullptr;
9311 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9312 {
9313 disr_type = TYPE_FIELD_TYPE (type, i);
9314
a037790e
TT
9315 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9316 {
9317 /* All fields of a true enum will be structs. */
9318 return;
9319 }
9320 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9321 {
9322 /* Could be data-less variant, so keep going. */
a037790e 9323 disr_type = nullptr;
c9317f21
TT
9324 }
9325 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9326 "RUST$ENUM$DISR") != 0)
9327 {
9328 /* Not a Rust enum. */
9329 return;
9330 }
9331 else
9332 {
9333 /* Found one. */
9334 break;
9335 }
9336 }
9337
9338 /* If we got here without a discriminant, then it's probably
9339 just a union. */
9340 if (disr_type == nullptr)
9341 return;
9342
9343 /* Smash this type to be a structure type. We have to do this
9344 because the type has already been recorded. */
9345 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9346
9347 /* Make a union to hold the variants. */
9348 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9349 struct type *union_type = alloc_type (objfile);
9350 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9351 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9352 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9353 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9354 TYPE_FIELDS (union_type)
9355 = (struct field *) TYPE_ZALLOC (union_type,
9356 (TYPE_NFIELDS (union_type)
9357 * sizeof (struct field)));
9358
9359 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9360 TYPE_NFIELDS (type) * sizeof (struct field));
9361
9362 /* Install the discriminant at index 0 in the union. */
9363 TYPE_FIELD (union_type, 0) = *disr_field;
9364 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9365 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9366
9367 /* Install the union in the outer struct type. */
9368 TYPE_FIELD_TYPE (type, 0) = union_type;
9369 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9370 TYPE_NFIELDS (type) = 1;
9371
9372 /* Set the size and offset of the union type. */
9373 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9374
9375 /* We need a way to find the correct discriminant given a
9376 variant name. For convenience we build a map here. */
9377 struct type *enum_type = FIELD_TYPE (*disr_field);
9378 std::unordered_map<std::string, ULONGEST> discriminant_map;
9379 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9380 {
9381 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9382 {
9383 const char *name
9384 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9385 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9386 }
9387 }
9388
9389 int n_fields = TYPE_NFIELDS (union_type);
9390 struct discriminant_info *disc
9391 = alloc_discriminant_info (union_type, 0, -1);
9392 /* Skip the discriminant here. */
9393 for (int i = 1; i < n_fields; ++i)
9394 {
9395 /* Find the final word in the name of this variant's type.
9396 That name can be used to look up the correct
9397 discriminant. */
9398 const char *variant_name
9399 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9400 i)));
9401
9402 auto iter = discriminant_map.find (variant_name);
9403 if (iter != discriminant_map.end ())
9404 disc->discriminants[i] = iter->second;
9405
bedda9ac 9406 /* Remove the discriminant field, if it exists. */
c9317f21 9407 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9408 if (TYPE_NFIELDS (sub_type) > 0)
9409 {
9410 --TYPE_NFIELDS (sub_type);
9411 ++TYPE_FIELDS (sub_type);
9412 }
c9317f21
TT
9413 TYPE_FIELD_NAME (union_type, i) = variant_name;
9414 TYPE_NAME (sub_type)
9415 = rust_fully_qualify (&objfile->objfile_obstack,
9416 TYPE_NAME (type), variant_name);
9417 }
9418 }
9419}
9420
9421/* Rewrite some Rust unions to be structures with variants parts. */
9422
9423static void
9424rust_union_quirks (struct dwarf2_cu *cu)
9425{
9426 gdb_assert (cu->language == language_rust);
52941706
SM
9427 for (type *type_ : cu->rust_unions)
9428 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9429 /* We don't need this any more. */
9430 cu->rust_unions.clear ();
c9317f21
TT
9431}
9432
95554aad
TT
9433/* Return the symtab for PER_CU. This works properly regardless of
9434 whether we're using the index or psymtabs. */
9435
43f3e411
DE
9436static struct compunit_symtab *
9437get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9438{
ed2dc618 9439 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9440 ? per_cu->v.quick->compunit_symtab
9441 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9442}
9443
9444/* A helper function for computing the list of all symbol tables
9445 included by PER_CU. */
9446
9447static void
4c39bc03 9448recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9449 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9450 struct dwarf2_per_cu_data *per_cu,
43f3e411 9451 struct compunit_symtab *immediate_parent)
95554aad
TT
9452{
9453 void **slot;
43f3e411 9454 struct compunit_symtab *cust;
95554aad
TT
9455
9456 slot = htab_find_slot (all_children, per_cu, INSERT);
9457 if (*slot != NULL)
9458 {
9459 /* This inclusion and its children have been processed. */
9460 return;
9461 }
9462
9463 *slot = per_cu;
9464 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9465 cust = get_compunit_symtab (per_cu);
9466 if (cust != NULL)
ec94af83
DE
9467 {
9468 /* If this is a type unit only add its symbol table if we haven't
9469 seen it yet (type unit per_cu's can share symtabs). */
9470 if (per_cu->is_debug_types)
9471 {
43f3e411 9472 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9473 if (*slot == NULL)
9474 {
43f3e411 9475 *slot = cust;
4c39bc03 9476 result->push_back (cust);
43f3e411
DE
9477 if (cust->user == NULL)
9478 cust->user = immediate_parent;
ec94af83
DE
9479 }
9480 }
9481 else
f9125b6c 9482 {
4c39bc03 9483 result->push_back (cust);
43f3e411
DE
9484 if (cust->user == NULL)
9485 cust->user = immediate_parent;
f9125b6c 9486 }
ec94af83 9487 }
95554aad 9488
ae640021
AB
9489 if (!per_cu->imported_symtabs_empty ())
9490 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9491 {
9492 recursively_compute_inclusions (result, all_children,
9493 all_type_symtabs, ptr, cust);
9494 }
95554aad
TT
9495}
9496
43f3e411 9497/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9498 PER_CU. */
9499
9500static void
43f3e411 9501compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9502{
f4dc4d17
DE
9503 gdb_assert (! per_cu->is_debug_types);
9504
ae640021 9505 if (!per_cu->imported_symtabs_empty ())
95554aad 9506 {
ae640021 9507 int len;
4c39bc03 9508 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9509 htab_t all_children, all_type_symtabs;
43f3e411 9510 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9511
9512 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9513 if (cust == NULL)
95554aad
TT
9514 return;
9515
9516 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9517 NULL, xcalloc, xfree);
ec94af83
DE
9518 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9519 NULL, xcalloc, xfree);
95554aad 9520
ae640021 9521 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9522 {
9523 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9524 all_type_symtabs, ptr, cust);
ec94af83 9525 }
95554aad 9526
ec94af83 9527 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9528 len = result_symtabs.size ();
43f3e411 9529 cust->includes
ed2dc618 9530 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9531 struct compunit_symtab *, len + 1);
4c39bc03
TT
9532 memcpy (cust->includes, result_symtabs.data (),
9533 len * sizeof (compunit_symtab *));
43f3e411 9534 cust->includes[len] = NULL;
95554aad 9535
95554aad 9536 htab_delete (all_children);
ec94af83 9537 htab_delete (all_type_symtabs);
95554aad
TT
9538 }
9539}
9540
9541/* Compute the 'includes' field for the symtabs of all the CUs we just
9542 read. */
9543
9544static void
ed2dc618 9545process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9546{
71b73764 9547 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9548 {
9549 if (! iter->is_debug_types)
43f3e411 9550 compute_compunit_symtab_includes (iter);
f4dc4d17 9551 }
95554aad 9552
c5d0225d 9553 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9554}
9555
9cdd5dbd 9556/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9557 already been loaded into memory. */
9558
9559static void
95554aad
TT
9560process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9561 enum language pretend_language)
10b3939b 9562{
10b3939b 9563 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9564 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9565 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9566 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9567 CORE_ADDR lowpc, highpc;
43f3e411 9568 struct compunit_symtab *cust;
10b3939b 9569 CORE_ADDR baseaddr;
4359dff1 9570 struct block *static_block;
3e29f34a 9571 CORE_ADDR addr;
10b3939b 9572
b3b3bada 9573 baseaddr = objfile->text_section_offset ();
10b3939b 9574
c89b44cd
TT
9575 /* Clear the list here in case something was left over. */
9576 cu->method_list.clear ();
10b3939b 9577
95554aad
TT
9578 cu->language = pretend_language;
9579 cu->language_defn = language_def (cu->language);
9580
c906108c 9581 /* Do line number decoding in read_file_scope () */
10b3939b 9582 process_die (cu->dies, cu);
c906108c 9583
a766d390
DE
9584 /* For now fudge the Go package. */
9585 if (cu->language == language_go)
9586 fixup_go_packaging (cu);
9587
5f48f8f3 9588 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9589 should be complete, and it should now be safe to compute all of the
9590 physnames. */
9591 compute_delayed_physnames (cu);
3da10d80 9592
c9317f21
TT
9593 if (cu->language == language_rust)
9594 rust_union_quirks (cu);
9595
fae299cd
DC
9596 /* Some compilers don't define a DW_AT_high_pc attribute for the
9597 compilation unit. If the DW_AT_high_pc is missing, synthesize
9598 it, by scanning the DIE's below the compilation unit. */
10b3939b 9599 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9600
3e29f34a 9601 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9602 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9603
9604 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9605 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9606 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9607 addrmap to help ensure it has an accurate map of pc values belonging to
9608 this comp unit. */
9609 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9610
c24bdb02 9611 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9612 SECT_OFF_TEXT (objfile),
9613 0);
c906108c 9614
43f3e411 9615 if (cust != NULL)
c906108c 9616 {
df15bd07 9617 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9618
8be455d7
JK
9619 /* Set symtab language to language from DW_AT_language. If the
9620 compilation is from a C file generated by language preprocessors, do
9621 not set the language if it was already deduced by start_subfile. */
43f3e411 9622 if (!(cu->language == language_c
40e3ad0e 9623 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9624 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9625
9626 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9627 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9628 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9629 there were bugs in prologue debug info, fixed later in GCC-4.5
9630 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9631
9632 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9633 needed, it would be wrong due to missing DW_AT_producer there.
9634
9635 Still one can confuse GDB by using non-standard GCC compilation
9636 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9637 */
ab260dad 9638 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9639 cust->locations_valid = 1;
e0d00bc7
JK
9640
9641 if (gcc_4_minor >= 5)
43f3e411 9642 cust->epilogue_unwind_valid = 1;
96408a79 9643
43f3e411 9644 cust->call_site_htab = cu->call_site_htab;
c906108c 9645 }
9291a0cd
TT
9646
9647 if (dwarf2_per_objfile->using_index)
43f3e411 9648 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9649 else
9650 {
891813be 9651 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9652 pst->compunit_symtab = cust;
6d94535f 9653 pst->readin = true;
9291a0cd 9654 }
c906108c 9655
95554aad 9656 /* Push it for inclusion processing later. */
c5d0225d 9657 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9658
9659 /* Not needed any more. */
c24bdb02 9660 cu->reset_builder ();
f4dc4d17 9661}
45cfd468 9662
f4dc4d17
DE
9663/* Generate full symbol information for type unit PER_CU, whose DIEs have
9664 already been loaded into memory. */
9665
9666static void
9667process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9668 enum language pretend_language)
9669{
9670 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9671 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9672 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9673 struct compunit_symtab *cust;
0186c6a7
DE
9674 struct signatured_type *sig_type;
9675
9676 gdb_assert (per_cu->is_debug_types);
9677 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9678
c89b44cd
TT
9679 /* Clear the list here in case something was left over. */
9680 cu->method_list.clear ();
f4dc4d17 9681
f4dc4d17
DE
9682 cu->language = pretend_language;
9683 cu->language_defn = language_def (cu->language);
9684
9685 /* The symbol tables are set up in read_type_unit_scope. */
9686 process_die (cu->dies, cu);
9687
9688 /* For now fudge the Go package. */
9689 if (cu->language == language_go)
9690 fixup_go_packaging (cu);
9691
5f48f8f3 9692 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9693 should be complete, and it should now be safe to compute all of the
9694 physnames. */
9695 compute_delayed_physnames (cu);
f4dc4d17 9696
c9317f21
TT
9697 if (cu->language == language_rust)
9698 rust_union_quirks (cu);
9699
f4dc4d17
DE
9700 /* TUs share symbol tables.
9701 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9702 of it with end_expandable_symtab. Otherwise, complete the addition of
9703 this TU's symbols to the existing symtab. */
43f3e411 9704 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9705 {
c24bdb02
KS
9706 buildsym_compunit *builder = cu->get_builder ();
9707 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9708 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9709
43f3e411 9710 if (cust != NULL)
f4dc4d17
DE
9711 {
9712 /* Set symtab language to language from DW_AT_language. If the
9713 compilation is from a C file generated by language preprocessors,
9714 do not set the language if it was already deduced by
9715 start_subfile. */
43f3e411
DE
9716 if (!(cu->language == language_c
9717 && COMPUNIT_FILETABS (cust)->language != language_c))
9718 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9719 }
9720 }
9721 else
9722 {
c24bdb02 9723 cu->get_builder ()->augment_type_symtab ();
43f3e411 9724 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9725 }
9726
9727 if (dwarf2_per_objfile->using_index)
43f3e411 9728 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9729 else
9730 {
891813be 9731 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9732 pst->compunit_symtab = cust;
6d94535f 9733 pst->readin = true;
45cfd468 9734 }
804d2729
TT
9735
9736 /* Not needed any more. */
c24bdb02 9737 cu->reset_builder ();
c906108c
SS
9738}
9739
95554aad
TT
9740/* Process an imported unit DIE. */
9741
9742static void
9743process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9744{
9745 struct attribute *attr;
9746
f4dc4d17
DE
9747 /* For now we don't handle imported units in type units. */
9748 if (cu->per_cu->is_debug_types)
9749 {
9750 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9751 " supported in type units [in module %s]"),
518817b3 9752 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9753 }
9754
95554aad
TT
9755 attr = dwarf2_attr (die, DW_AT_import, cu);
9756 if (attr != NULL)
9757 {
9c541725
PA
9758 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9759 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9760 dwarf2_per_cu_data *per_cu
e3b94546 9761 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9762 cu->per_cu->dwarf2_per_objfile);
95554aad 9763
69d751e3 9764 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9765 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9766 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9767
ae640021 9768 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9769 }
9770}
9771
4c8aa72d
PA
9772/* RAII object that represents a process_die scope: i.e.,
9773 starts/finishes processing a DIE. */
9774class process_die_scope
adde2bff 9775{
4c8aa72d
PA
9776public:
9777 process_die_scope (die_info *die, dwarf2_cu *cu)
9778 : m_die (die), m_cu (cu)
9779 {
9780 /* We should only be processing DIEs not already in process. */
9781 gdb_assert (!m_die->in_process);
9782 m_die->in_process = true;
9783 }
8c3cb9fa 9784
4c8aa72d
PA
9785 ~process_die_scope ()
9786 {
9787 m_die->in_process = false;
9788
9789 /* If we're done processing the DIE for the CU that owns the line
9790 header, we don't need the line header anymore. */
9791 if (m_cu->line_header_die_owner == m_die)
9792 {
9793 delete m_cu->line_header;
9794 m_cu->line_header = NULL;
9795 m_cu->line_header_die_owner = NULL;
9796 }
9797 }
9798
9799private:
9800 die_info *m_die;
9801 dwarf2_cu *m_cu;
9802};
adde2bff 9803
c906108c
SS
9804/* Process a die and its children. */
9805
9806static void
e7c27a73 9807process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9808{
4c8aa72d 9809 process_die_scope scope (die, cu);
adde2bff 9810
c906108c
SS
9811 switch (die->tag)
9812 {
9813 case DW_TAG_padding:
9814 break;
9815 case DW_TAG_compile_unit:
95554aad 9816 case DW_TAG_partial_unit:
e7c27a73 9817 read_file_scope (die, cu);
c906108c 9818 break;
348e048f
DE
9819 case DW_TAG_type_unit:
9820 read_type_unit_scope (die, cu);
9821 break;
c906108c 9822 case DW_TAG_subprogram:
0a4b0913
AB
9823 /* Nested subprograms in Fortran get a prefix. */
9824 if (cu->language == language_fortran
9825 && die->parent != NULL
9826 && die->parent->tag == DW_TAG_subprogram)
9827 cu->processing_has_namespace_info = true;
9828 /* Fall through. */
c906108c 9829 case DW_TAG_inlined_subroutine:
edb3359d 9830 read_func_scope (die, cu);
c906108c
SS
9831 break;
9832 case DW_TAG_lexical_block:
14898363
L
9833 case DW_TAG_try_block:
9834 case DW_TAG_catch_block:
e7c27a73 9835 read_lexical_block_scope (die, cu);
c906108c 9836 break;
216f72a1 9837 case DW_TAG_call_site:
96408a79
SA
9838 case DW_TAG_GNU_call_site:
9839 read_call_site_scope (die, cu);
9840 break;
c906108c 9841 case DW_TAG_class_type:
680b30c7 9842 case DW_TAG_interface_type:
c906108c
SS
9843 case DW_TAG_structure_type:
9844 case DW_TAG_union_type:
134d01f1 9845 process_structure_scope (die, cu);
c906108c
SS
9846 break;
9847 case DW_TAG_enumeration_type:
134d01f1 9848 process_enumeration_scope (die, cu);
c906108c 9849 break;
134d01f1 9850
f792889a
DJ
9851 /* These dies have a type, but processing them does not create
9852 a symbol or recurse to process the children. Therefore we can
9853 read them on-demand through read_type_die. */
c906108c 9854 case DW_TAG_subroutine_type:
72019c9c 9855 case DW_TAG_set_type:
c906108c 9856 case DW_TAG_array_type:
c906108c 9857 case DW_TAG_pointer_type:
c906108c 9858 case DW_TAG_ptr_to_member_type:
c906108c 9859 case DW_TAG_reference_type:
4297a3f0 9860 case DW_TAG_rvalue_reference_type:
c906108c 9861 case DW_TAG_string_type:
c906108c 9862 break;
134d01f1 9863
c906108c 9864 case DW_TAG_base_type:
a02abb62 9865 case DW_TAG_subrange_type:
cb249c71 9866 case DW_TAG_typedef:
134d01f1
DJ
9867 /* Add a typedef symbol for the type definition, if it has a
9868 DW_AT_name. */
f792889a 9869 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9870 break;
c906108c 9871 case DW_TAG_common_block:
e7c27a73 9872 read_common_block (die, cu);
c906108c
SS
9873 break;
9874 case DW_TAG_common_inclusion:
9875 break;
d9fa45fe 9876 case DW_TAG_namespace:
9068261f 9877 cu->processing_has_namespace_info = true;
e7c27a73 9878 read_namespace (die, cu);
d9fa45fe 9879 break;
5d7cb8df 9880 case DW_TAG_module:
9068261f 9881 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9882 read_module (die, cu);
9883 break;
d9fa45fe 9884 case DW_TAG_imported_declaration:
9068261f 9885 cu->processing_has_namespace_info = true;
74921315
KS
9886 if (read_namespace_alias (die, cu))
9887 break;
86a73007
TT
9888 /* The declaration is not a global namespace alias. */
9889 /* Fall through. */
d9fa45fe 9890 case DW_TAG_imported_module:
9068261f 9891 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9892 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9893 || cu->language != language_fortran))
b98664d3 9894 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9895 dwarf_tag_name (die->tag));
9896 read_import_statement (die, cu);
d9fa45fe 9897 break;
95554aad
TT
9898
9899 case DW_TAG_imported_unit:
9900 process_imported_unit_die (die, cu);
9901 break;
9902
71a3c369
TT
9903 case DW_TAG_variable:
9904 read_variable (die, cu);
9905 break;
9906
c906108c 9907 default:
e7c27a73 9908 new_symbol (die, NULL, cu);
c906108c
SS
9909 break;
9910 }
9911}
ca69b9e6
DE
9912\f
9913/* DWARF name computation. */
c906108c 9914
94af9270
KS
9915/* A helper function for dwarf2_compute_name which determines whether DIE
9916 needs to have the name of the scope prepended to the name listed in the
9917 die. */
9918
9919static int
9920die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9921{
1c809c68
TT
9922 struct attribute *attr;
9923
94af9270
KS
9924 switch (die->tag)
9925 {
9926 case DW_TAG_namespace:
9927 case DW_TAG_typedef:
9928 case DW_TAG_class_type:
9929 case DW_TAG_interface_type:
9930 case DW_TAG_structure_type:
9931 case DW_TAG_union_type:
9932 case DW_TAG_enumeration_type:
9933 case DW_TAG_enumerator:
9934 case DW_TAG_subprogram:
08a76f8a 9935 case DW_TAG_inlined_subroutine:
94af9270 9936 case DW_TAG_member:
74921315 9937 case DW_TAG_imported_declaration:
94af9270
KS
9938 return 1;
9939
9940 case DW_TAG_variable:
c2b0a229 9941 case DW_TAG_constant:
94af9270
KS
9942 /* We only need to prefix "globally" visible variables. These include
9943 any variable marked with DW_AT_external or any variable that
9944 lives in a namespace. [Variables in anonymous namespaces
9945 require prefixing, but they are not DW_AT_external.] */
9946
9947 if (dwarf2_attr (die, DW_AT_specification, cu))
9948 {
9949 struct dwarf2_cu *spec_cu = cu;
9a619af0 9950
94af9270
KS
9951 return die_needs_namespace (die_specification (die, &spec_cu),
9952 spec_cu);
9953 }
9954
1c809c68 9955 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9956 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9957 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9958 return 0;
9959 /* A variable in a lexical block of some kind does not need a
9960 namespace, even though in C++ such variables may be external
9961 and have a mangled name. */
9962 if (die->parent->tag == DW_TAG_lexical_block
9963 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9964 || die->parent->tag == DW_TAG_catch_block
9965 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9966 return 0;
9967 return 1;
94af9270
KS
9968
9969 default:
9970 return 0;
9971 }
9972}
9973
73b9be8b
KS
9974/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9975 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9976 defined for the given DIE. */
9977
9978static struct attribute *
9979dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9980{
9981 struct attribute *attr;
9982
9983 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9984 if (attr == NULL)
9985 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9986
9987 return attr;
9988}
9989
9990/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9991 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9992 defined for the given DIE. */
9993
9994static const char *
9995dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9996{
9997 const char *linkage_name;
9998
9999 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10000 if (linkage_name == NULL)
10001 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10002
10003 return linkage_name;
10004}
10005
94af9270 10006/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10007 compute the physname for the object, which include a method's:
9c37b5ae 10008 - formal parameters (C++),
a766d390 10009 - receiver type (Go),
a766d390
DE
10010
10011 The term "physname" is a bit confusing.
10012 For C++, for example, it is the demangled name.
10013 For Go, for example, it's the mangled name.
94af9270 10014
af6b7be1
JB
10015 For Ada, return the DIE's linkage name rather than the fully qualified
10016 name. PHYSNAME is ignored..
10017
94af9270
KS
10018 The result is allocated on the objfile_obstack and canonicalized. */
10019
10020static const char *
15d034d0
TT
10021dwarf2_compute_name (const char *name,
10022 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10023 int physname)
10024{
518817b3 10025 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10026
94af9270
KS
10027 if (name == NULL)
10028 name = dwarf2_name (die, cu);
10029
2ee7123e
DE
10030 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10031 but otherwise compute it by typename_concat inside GDB.
10032 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10033 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10034 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10035 will set the demangled name to the result of dwarf2_full_name, and it is
10036 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10037 if (cu->language == language_ada
10038 || (cu->language == language_fortran && physname))
10039 {
10040 /* For Ada unit, we prefer the linkage name over the name, as
10041 the former contains the exported name, which the user expects
10042 to be able to reference. Ideally, we want the user to be able
10043 to reference this entity using either natural or linkage name,
10044 but we haven't started looking at this enhancement yet. */
73b9be8b 10045 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10046
2ee7123e
DE
10047 if (linkage_name != NULL)
10048 return linkage_name;
f55ee35c
JK
10049 }
10050
94af9270
KS
10051 /* These are the only languages we know how to qualify names in. */
10052 if (name != NULL
9c37b5ae 10053 && (cu->language == language_cplus
c44af4eb
TT
10054 || cu->language == language_fortran || cu->language == language_d
10055 || cu->language == language_rust))
94af9270
KS
10056 {
10057 if (die_needs_namespace (die, cu))
10058 {
0d5cff50 10059 const char *prefix;
34a68019 10060 const char *canonical_name = NULL;
94af9270 10061
d7e74731
PA
10062 string_file buf;
10063
94af9270 10064 prefix = determine_prefix (die, cu);
94af9270
KS
10065 if (*prefix != '\0')
10066 {
43816ebc
TT
10067 gdb::unique_xmalloc_ptr<char> prefixed_name
10068 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10069
43816ebc 10070 buf.puts (prefixed_name.get ());
94af9270
KS
10071 }
10072 else
d7e74731 10073 buf.puts (name);
94af9270 10074
98bfdba5
PA
10075 /* Template parameters may be specified in the DIE's DW_AT_name, or
10076 as children with DW_TAG_template_type_param or
10077 DW_TAG_value_type_param. If the latter, add them to the name
10078 here. If the name already has template parameters, then
10079 skip this step; some versions of GCC emit both, and
10080 it is more efficient to use the pre-computed name.
10081
10082 Something to keep in mind about this process: it is very
10083 unlikely, or in some cases downright impossible, to produce
10084 something that will match the mangled name of a function.
10085 If the definition of the function has the same debug info,
10086 we should be able to match up with it anyway. But fallbacks
10087 using the minimal symbol, for instance to find a method
10088 implemented in a stripped copy of libstdc++, will not work.
10089 If we do not have debug info for the definition, we will have to
10090 match them up some other way.
10091
10092 When we do name matching there is a related problem with function
10093 templates; two instantiated function templates are allowed to
10094 differ only by their return types, which we do not add here. */
10095
10096 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10097 {
10098 struct attribute *attr;
10099 struct die_info *child;
10100 int first = 1;
10101
10102 die->building_fullname = 1;
10103
10104 for (child = die->child; child != NULL; child = child->sibling)
10105 {
10106 struct type *type;
12df843f 10107 LONGEST value;
d521ce57 10108 const gdb_byte *bytes;
98bfdba5
PA
10109 struct dwarf2_locexpr_baton *baton;
10110 struct value *v;
10111
10112 if (child->tag != DW_TAG_template_type_param
10113 && child->tag != DW_TAG_template_value_param)
10114 continue;
10115
10116 if (first)
10117 {
d7e74731 10118 buf.puts ("<");
98bfdba5
PA
10119 first = 0;
10120 }
10121 else
d7e74731 10122 buf.puts (", ");
98bfdba5
PA
10123
10124 attr = dwarf2_attr (child, DW_AT_type, cu);
10125 if (attr == NULL)
10126 {
b98664d3 10127 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10128 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10129 continue;
10130 }
10131 type = die_type (child, cu);
10132
10133 if (child->tag == DW_TAG_template_type_param)
10134 {
c1ec8cea
TT
10135 c_print_type (type, "", &buf, -1, 0, cu->language,
10136 &type_print_raw_options);
98bfdba5
PA
10137 continue;
10138 }
10139
10140 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10141 if (attr == NULL)
10142 {
b98664d3 10143 complaint (_("template parameter missing "
3e43a32a 10144 "DW_AT_const_value"));
d7e74731 10145 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10146 continue;
10147 }
10148
10149 dwarf2_const_value_attr (attr, type, name,
10150 &cu->comp_unit_obstack, cu,
10151 &value, &bytes, &baton);
10152
10153 if (TYPE_NOSIGN (type))
10154 /* GDB prints characters as NUMBER 'CHAR'. If that's
10155 changed, this can use value_print instead. */
d7e74731 10156 c_printchar (value, type, &buf);
98bfdba5
PA
10157 else
10158 {
10159 struct value_print_options opts;
10160
10161 if (baton != NULL)
10162 v = dwarf2_evaluate_loc_desc (type, NULL,
10163 baton->data,
10164 baton->size,
10165 baton->per_cu);
10166 else if (bytes != NULL)
10167 {
10168 v = allocate_value (type);
10169 memcpy (value_contents_writeable (v), bytes,
10170 TYPE_LENGTH (type));
10171 }
10172 else
10173 v = value_from_longest (type, value);
10174
3e43a32a
MS
10175 /* Specify decimal so that we do not depend on
10176 the radix. */
98bfdba5
PA
10177 get_formatted_print_options (&opts, 'd');
10178 opts.raw = 1;
d7e74731 10179 value_print (v, &buf, &opts);
98bfdba5 10180 release_value (v);
98bfdba5
PA
10181 }
10182 }
10183
10184 die->building_fullname = 0;
10185
10186 if (!first)
10187 {
10188 /* Close the argument list, with a space if necessary
10189 (nested templates). */
d7e74731
PA
10190 if (!buf.empty () && buf.string ().back () == '>')
10191 buf.puts (" >");
98bfdba5 10192 else
d7e74731 10193 buf.puts (">");
98bfdba5
PA
10194 }
10195 }
10196
9c37b5ae 10197 /* For C++ methods, append formal parameter type
94af9270 10198 information, if PHYSNAME. */
6e70227d 10199
94af9270 10200 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10201 && cu->language == language_cplus)
94af9270
KS
10202 {
10203 struct type *type = read_type_die (die, cu);
10204
d7e74731 10205 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10206 &type_print_raw_options);
94af9270 10207
9c37b5ae 10208 if (cu->language == language_cplus)
94af9270 10209 {
60430eff
DJ
10210 /* Assume that an artificial first parameter is
10211 "this", but do not crash if it is not. RealView
10212 marks unnamed (and thus unused) parameters as
10213 artificial; there is no way to differentiate
10214 the two cases. */
94af9270
KS
10215 if (TYPE_NFIELDS (type) > 0
10216 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10217 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10218 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10219 0))))
d7e74731 10220 buf.puts (" const");
94af9270
KS
10221 }
10222 }
10223
d7e74731 10224 const std::string &intermediate_name = buf.string ();
94af9270
KS
10225
10226 if (cu->language == language_cplus)
34a68019 10227 canonical_name
322a8516 10228 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10229 &objfile->per_bfd->storage_obstack);
10230
10231 /* If we only computed INTERMEDIATE_NAME, or if
10232 INTERMEDIATE_NAME is already canonical, then we need to
10233 copy it to the appropriate obstack. */
322a8516 10234 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10235 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10236 intermediate_name);
34a68019
TT
10237 else
10238 name = canonical_name;
94af9270
KS
10239 }
10240 }
10241
10242 return name;
10243}
10244
0114d602
DJ
10245/* Return the fully qualified name of DIE, based on its DW_AT_name.
10246 If scope qualifiers are appropriate they will be added. The result
34a68019 10247 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10248 not have a name. NAME may either be from a previous call to
10249 dwarf2_name or NULL.
10250
9c37b5ae 10251 The output string will be canonicalized (if C++). */
0114d602
DJ
10252
10253static const char *
15d034d0 10254dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10255{
94af9270
KS
10256 return dwarf2_compute_name (name, die, cu, 0);
10257}
0114d602 10258
94af9270
KS
10259/* Construct a physname for the given DIE in CU. NAME may either be
10260 from a previous call to dwarf2_name or NULL. The result will be
10261 allocated on the objfile_objstack or NULL if the DIE does not have a
10262 name.
0114d602 10263
9c37b5ae 10264 The output string will be canonicalized (if C++). */
0114d602 10265
94af9270 10266static const char *
15d034d0 10267dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10268{
518817b3 10269 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10270 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10271 int need_copy = 1;
10272
10273 /* In this case dwarf2_compute_name is just a shortcut not building anything
10274 on its own. */
10275 if (!die_needs_namespace (die, cu))
10276 return dwarf2_compute_name (name, die, cu, 1);
10277
73b9be8b 10278 mangled = dw2_linkage_name (die, cu);
900e11f9 10279
e98c9e7c
TT
10280 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10281 See https://github.com/rust-lang/rust/issues/32925. */
10282 if (cu->language == language_rust && mangled != NULL
10283 && strchr (mangled, '{') != NULL)
10284 mangled = NULL;
10285
900e11f9
JK
10286 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10287 has computed. */
791afaa2 10288 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10289 if (mangled != NULL)
900e11f9 10290 {
900e11f9 10291
59cc4834
JB
10292 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10293 {
10294 /* Do nothing (do not demangle the symbol name). */
10295 }
10296 else if (cu->language == language_go)
a766d390 10297 {
5e2db402
TT
10298 /* This is a lie, but we already lie to the caller new_symbol.
10299 new_symbol assumes we return the mangled name.
a766d390 10300 This just undoes that lie until things are cleaned up. */
a766d390
DE
10301 }
10302 else
10303 {
0eb876f5
JB
10304 /* Use DMGL_RET_DROP for C++ template functions to suppress
10305 their return type. It is easier for GDB users to search
10306 for such functions as `name(params)' than `long name(params)'.
10307 In such case the minimal symbol names do not match the full
10308 symbol names but for template functions there is never a need
10309 to look up their definition from their declaration so
10310 the only disadvantage remains the minimal symbol variant
10311 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10312 demangled.reset (gdb_demangle (mangled,
10313 (DMGL_PARAMS | DMGL_ANSI
10314 | DMGL_RET_DROP)));
a766d390 10315 }
900e11f9 10316 if (demangled)
791afaa2 10317 canon = demangled.get ();
900e11f9
JK
10318 else
10319 {
10320 canon = mangled;
10321 need_copy = 0;
10322 }
10323 }
10324
10325 if (canon == NULL || check_physname)
10326 {
10327 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10328
10329 if (canon != NULL && strcmp (physname, canon) != 0)
10330 {
10331 /* It may not mean a bug in GDB. The compiler could also
10332 compute DW_AT_linkage_name incorrectly. But in such case
10333 GDB would need to be bug-to-bug compatible. */
10334
b98664d3 10335 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10336 "(from linkage <%s>) - DIE at %s [in module %s]"),
10337 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10338 objfile_name (objfile));
900e11f9
JK
10339
10340 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10341 is available here - over computed PHYSNAME. It is safer
10342 against both buggy GDB and buggy compilers. */
10343
10344 retval = canon;
10345 }
10346 else
10347 {
10348 retval = physname;
10349 need_copy = 0;
10350 }
10351 }
10352 else
10353 retval = canon;
10354
10355 if (need_copy)
021887d8 10356 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 10357
900e11f9 10358 return retval;
0114d602
DJ
10359}
10360
74921315
KS
10361/* Inspect DIE in CU for a namespace alias. If one exists, record
10362 a new symbol for it.
10363
10364 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10365
10366static int
10367read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10368{
10369 struct attribute *attr;
10370
10371 /* If the die does not have a name, this is not a namespace
10372 alias. */
10373 attr = dwarf2_attr (die, DW_AT_name, cu);
10374 if (attr != NULL)
10375 {
10376 int num;
10377 struct die_info *d = die;
10378 struct dwarf2_cu *imported_cu = cu;
10379
10380 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10381 keep inspecting DIEs until we hit the underlying import. */
10382#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10383 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10384 {
10385 attr = dwarf2_attr (d, DW_AT_import, cu);
10386 if (attr == NULL)
10387 break;
10388
10389 d = follow_die_ref (d, attr, &imported_cu);
10390 if (d->tag != DW_TAG_imported_declaration)
10391 break;
10392 }
10393
10394 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10395 {
b98664d3 10396 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10397 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10398 return 0;
10399 }
10400
10401 if (attr != NULL)
10402 {
10403 struct type *type;
9c541725 10404 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10405
9c541725 10406 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10407 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10408 {
10409 /* This declaration is a global namespace alias. Add
10410 a symbol for it whose type is the aliased namespace. */
10411 new_symbol (die, type, cu);
10412 return 1;
10413 }
10414 }
10415 }
10416
10417 return 0;
10418}
10419
22cee43f 10420/* Return the using directives repository (global or local?) to use in the
804d2729 10421 current context for CU.
22cee43f
PMR
10422
10423 For Ada, imported declarations can materialize renamings, which *may* be
10424 global. However it is impossible (for now?) in DWARF to distinguish
10425 "external" imported declarations and "static" ones. As all imported
10426 declarations seem to be static in all other languages, make them all CU-wide
10427 global only in Ada. */
10428
10429static struct using_direct **
804d2729 10430using_directives (struct dwarf2_cu *cu)
22cee43f 10431{
c24bdb02
KS
10432 if (cu->language == language_ada
10433 && cu->get_builder ()->outermost_context_p ())
10434 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10435 else
c24bdb02 10436 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10437}
10438
27aa8d6a
SW
10439/* Read the import statement specified by the given die and record it. */
10440
10441static void
10442read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10443{
518817b3 10444 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10445 struct attribute *import_attr;
32019081 10446 struct die_info *imported_die, *child_die;
de4affc9 10447 struct dwarf2_cu *imported_cu;
27aa8d6a 10448 const char *imported_name;
794684b6 10449 const char *imported_name_prefix;
13387711
SW
10450 const char *canonical_name;
10451 const char *import_alias;
10452 const char *imported_declaration = NULL;
794684b6 10453 const char *import_prefix;
eb1e02fd 10454 std::vector<const char *> excludes;
13387711 10455
27aa8d6a
SW
10456 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10457 if (import_attr == NULL)
10458 {
b98664d3 10459 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10460 dwarf_tag_name (die->tag));
10461 return;
10462 }
10463
de4affc9
CC
10464 imported_cu = cu;
10465 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10466 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10467 if (imported_name == NULL)
10468 {
10469 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10470
10471 The import in the following code:
10472 namespace A
10473 {
10474 typedef int B;
10475 }
10476
10477 int main ()
10478 {
10479 using A::B;
10480 B b;
10481 return b;
10482 }
10483
10484 ...
10485 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10486 <52> DW_AT_decl_file : 1
10487 <53> DW_AT_decl_line : 6
10488 <54> DW_AT_import : <0x75>
10489 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10490 <59> DW_AT_name : B
10491 <5b> DW_AT_decl_file : 1
10492 <5c> DW_AT_decl_line : 2
10493 <5d> DW_AT_type : <0x6e>
10494 ...
10495 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10496 <76> DW_AT_byte_size : 4
10497 <77> DW_AT_encoding : 5 (signed)
10498
10499 imports the wrong die ( 0x75 instead of 0x58 ).
10500 This case will be ignored until the gcc bug is fixed. */
10501 return;
10502 }
10503
82856980
SW
10504 /* Figure out the local name after import. */
10505 import_alias = dwarf2_name (die, cu);
27aa8d6a 10506
794684b6
SW
10507 /* Figure out where the statement is being imported to. */
10508 import_prefix = determine_prefix (die, cu);
10509
10510 /* Figure out what the scope of the imported die is and prepend it
10511 to the name of the imported die. */
de4affc9 10512 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10513
f55ee35c
JK
10514 if (imported_die->tag != DW_TAG_namespace
10515 && imported_die->tag != DW_TAG_module)
794684b6 10516 {
13387711
SW
10517 imported_declaration = imported_name;
10518 canonical_name = imported_name_prefix;
794684b6 10519 }
13387711 10520 else if (strlen (imported_name_prefix) > 0)
12aaed36 10521 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10522 imported_name_prefix,
10523 (cu->language == language_d ? "." : "::"),
10524 imported_name, (char *) NULL);
13387711
SW
10525 else
10526 canonical_name = imported_name;
794684b6 10527
32019081
JK
10528 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10529 for (child_die = die->child; child_die && child_die->tag;
10530 child_die = sibling_die (child_die))
10531 {
10532 /* DWARF-4: A Fortran use statement with a “rename list” may be
10533 represented by an imported module entry with an import attribute
10534 referring to the module and owned entries corresponding to those
10535 entities that are renamed as part of being imported. */
10536
10537 if (child_die->tag != DW_TAG_imported_declaration)
10538 {
b98664d3 10539 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10540 "- DIE at %s [in module %s]"),
10541 sect_offset_str (child_die->sect_off),
10542 objfile_name (objfile));
32019081
JK
10543 continue;
10544 }
10545
10546 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10547 if (import_attr == NULL)
10548 {
b98664d3 10549 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10550 dwarf_tag_name (child_die->tag));
10551 continue;
10552 }
10553
10554 imported_cu = cu;
10555 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10556 &imported_cu);
10557 imported_name = dwarf2_name (imported_die, imported_cu);
10558 if (imported_name == NULL)
10559 {
b98664d3 10560 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10561 "imported name - DIE at %s [in module %s]"),
10562 sect_offset_str (child_die->sect_off),
10563 objfile_name (objfile));
32019081
JK
10564 continue;
10565 }
10566
eb1e02fd 10567 excludes.push_back (imported_name);
32019081
JK
10568
10569 process_die (child_die, cu);
10570 }
10571
804d2729 10572 add_using_directive (using_directives (cu),
22cee43f
PMR
10573 import_prefix,
10574 canonical_name,
10575 import_alias,
10576 imported_declaration,
10577 excludes,
10578 0,
10579 &objfile->objfile_obstack);
27aa8d6a
SW
10580}
10581
5230b05a
WT
10582/* ICC<14 does not output the required DW_AT_declaration on incomplete
10583 types, but gives them a size of zero. Starting with version 14,
10584 ICC is compatible with GCC. */
10585
9068261f 10586static bool
5230b05a
WT
10587producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10588{
10589 if (!cu->checked_producer)
10590 check_producer (cu);
10591
10592 return cu->producer_is_icc_lt_14;
10593}
10594
eb77c9df
AB
10595/* ICC generates a DW_AT_type for C void functions. This was observed on
10596 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10597 which says that void functions should not have a DW_AT_type. */
10598
10599static bool
10600producer_is_icc (struct dwarf2_cu *cu)
10601{
10602 if (!cu->checked_producer)
10603 check_producer (cu);
10604
10605 return cu->producer_is_icc;
10606}
10607
1b80a9fa
JK
10608/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10609 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10610 this, it was first present in GCC release 4.3.0. */
10611
9068261f 10612static bool
1b80a9fa
JK
10613producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10614{
10615 if (!cu->checked_producer)
10616 check_producer (cu);
10617
10618 return cu->producer_is_gcc_lt_4_3;
10619}
10620
d721ba37
PA
10621static file_and_directory
10622find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10623{
d721ba37
PA
10624 file_and_directory res;
10625
9291a0cd
TT
10626 /* Find the filename. Do not use dwarf2_name here, since the filename
10627 is not a source language identifier. */
d721ba37
PA
10628 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10629 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10630
d721ba37
PA
10631 if (res.comp_dir == NULL
10632 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10633 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10634 {
d721ba37
PA
10635 res.comp_dir_storage = ldirname (res.name);
10636 if (!res.comp_dir_storage.empty ())
10637 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10638 }
d721ba37 10639 if (res.comp_dir != NULL)
9291a0cd
TT
10640 {
10641 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10642 directory, get rid of it. */
d721ba37 10643 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10644
d721ba37
PA
10645 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10646 res.comp_dir = cp + 1;
9291a0cd
TT
10647 }
10648
d721ba37
PA
10649 if (res.name == NULL)
10650 res.name = "<unknown>";
10651
10652 return res;
9291a0cd
TT
10653}
10654
f4dc4d17
DE
10655/* Handle DW_AT_stmt_list for a compilation unit.
10656 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10657 COMP_DIR is the compilation directory. LOWPC is passed to
10658 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10659
10660static void
10661handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10662 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10663{
518817b3
SM
10664 struct dwarf2_per_objfile *dwarf2_per_objfile
10665 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10666 struct attribute *attr;
527f3840
JK
10667 struct line_header line_header_local;
10668 hashval_t line_header_local_hash;
527f3840
JK
10669 void **slot;
10670 int decode_mapping;
2ab95328 10671
f4dc4d17
DE
10672 gdb_assert (! cu->per_cu->is_debug_types);
10673
2ab95328 10674 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10675 if (attr == NULL)
10676 return;
10677
9c541725 10678 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10679
10680 /* The line header hash table is only created if needed (it exists to
10681 prevent redundant reading of the line table for partial_units).
10682 If we're given a partial_unit, we'll need it. If we're given a
10683 compile_unit, then use the line header hash table if it's already
10684 created, but don't create one just yet. */
10685
10686 if (dwarf2_per_objfile->line_header_hash == NULL
10687 && die->tag == DW_TAG_partial_unit)
2ab95328 10688 {
527f3840 10689 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10690 .reset (htab_create_alloc (127, line_header_hash_voidp,
10691 line_header_eq_voidp,
10692 free_line_header_voidp,
10693 xcalloc, xfree));
527f3840 10694 }
2ab95328 10695
9c541725 10696 line_header_local.sect_off = line_offset;
527f3840
JK
10697 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10698 line_header_local_hash = line_header_hash (&line_header_local);
10699 if (dwarf2_per_objfile->line_header_hash != NULL)
10700 {
d15acc42 10701 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10702 &line_header_local,
10703 line_header_local_hash, NO_INSERT);
10704
10705 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10706 is not present in *SLOT (since if there is something in *SLOT then
10707 it will be for a partial_unit). */
10708 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10709 {
527f3840 10710 gdb_assert (*slot != NULL);
9a3c8263 10711 cu->line_header = (struct line_header *) *slot;
527f3840 10712 return;
dee91e82 10713 }
2ab95328 10714 }
527f3840
JK
10715
10716 /* dwarf_decode_line_header does not yet provide sufficient information.
10717 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10718 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10719 if (lh == NULL)
527f3840 10720 return;
4c8aa72d
PA
10721
10722 cu->line_header = lh.release ();
10723 cu->line_header_die_owner = die;
527f3840
JK
10724
10725 if (dwarf2_per_objfile->line_header_hash == NULL)
10726 slot = NULL;
10727 else
10728 {
d15acc42 10729 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10730 &line_header_local,
10731 line_header_local_hash, INSERT);
10732 gdb_assert (slot != NULL);
10733 }
10734 if (slot != NULL && *slot == NULL)
10735 {
10736 /* This newly decoded line number information unit will be owned
10737 by line_header_hash hash table. */
10738 *slot = cu->line_header;
4c8aa72d 10739 cu->line_header_die_owner = NULL;
527f3840
JK
10740 }
10741 else
10742 {
10743 /* We cannot free any current entry in (*slot) as that struct line_header
10744 may be already used by multiple CUs. Create only temporary decoded
10745 line_header for this CU - it may happen at most once for each line
10746 number information unit. And if we're not using line_header_hash
10747 then this is what we want as well. */
10748 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10749 }
10750 decode_mapping = (die->tag != DW_TAG_partial_unit);
10751 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10752 decode_mapping);
fff8551c 10753
2ab95328
TT
10754}
10755
95554aad 10756/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10757
c906108c 10758static void
e7c27a73 10759read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10760{
518817b3
SM
10761 struct dwarf2_per_objfile *dwarf2_per_objfile
10762 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10763 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10764 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10765 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10766 CORE_ADDR highpc = ((CORE_ADDR) 0);
10767 struct attribute *attr;
c906108c 10768 struct die_info *child_die;
e142c38c 10769 CORE_ADDR baseaddr;
6e70227d 10770
380618d6 10771 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10772 baseaddr = objfile->text_section_offset ();
c906108c 10773
fae299cd 10774 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10775
10776 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10777 from finish_block. */
2acceee2 10778 if (lowpc == ((CORE_ADDR) -1))
c906108c 10779 lowpc = highpc;
3e29f34a 10780 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10781
d721ba37 10782 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10783
f4b8a18d
KW
10784 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10785 standardised yet. As a workaround for the language detection we fall
10786 back to the DW_AT_producer string. */
10787 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10788 cu->language = language_opencl;
10789
3019eac3
DE
10790 /* Similar hack for Go. */
10791 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10792 set_cu_language (DW_LANG_Go, cu);
10793
c24bdb02 10794 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10795
10796 /* Decode line number information if present. We do this before
10797 processing child DIEs, so that the line header table is available
10798 for DW_AT_decl_file. */
d721ba37 10799 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10800
10801 /* Process all dies in compilation unit. */
10802 if (die->child != NULL)
10803 {
10804 child_die = die->child;
10805 while (child_die && child_die->tag)
10806 {
10807 process_die (child_die, cu);
10808 child_die = sibling_die (child_die);
10809 }
10810 }
10811
10812 /* Decode macro information, if present. Dwarf 2 macro information
10813 refers to information in the line number info statement program
10814 header, so we can only read it if we've read the header
10815 successfully. */
0af92d60
JK
10816 attr = dwarf2_attr (die, DW_AT_macros, cu);
10817 if (attr == NULL)
10818 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10819 if (attr && cu->line_header)
10820 {
10821 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10822 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10823
43f3e411 10824 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10825 }
10826 else
10827 {
10828 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10829 if (attr && cu->line_header)
10830 {
10831 unsigned int macro_offset = DW_UNSND (attr);
10832
43f3e411 10833 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10834 }
10835 }
3019eac3
DE
10836}
10837
c24bdb02
KS
10838void
10839dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10840{
f4dc4d17
DE
10841 struct type_unit_group *tu_group;
10842 int first_time;
3019eac3 10843 struct attribute *attr;
9c541725 10844 unsigned int i;
0186c6a7 10845 struct signatured_type *sig_type;
3019eac3 10846
f4dc4d17 10847 gdb_assert (per_cu->is_debug_types);
0186c6a7 10848 sig_type = (struct signatured_type *) per_cu;
3019eac3 10849
c24bdb02 10850 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10851
f4dc4d17 10852 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10853 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10854 if (sig_type->type_unit_group == NULL)
c24bdb02 10855 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10856 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10857
10858 /* If we've already processed this stmt_list there's no real need to
10859 do it again, we could fake it and just recreate the part we need
10860 (file name,index -> symtab mapping). If data shows this optimization
10861 is useful we can do it then. */
43f3e411 10862 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10863
10864 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10865 debug info. */
fff8551c 10866 line_header_up lh;
f4dc4d17 10867 if (attr != NULL)
3019eac3 10868 {
9c541725 10869 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10870 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10871 }
10872 if (lh == NULL)
10873 {
10874 if (first_time)
c24bdb02 10875 start_symtab ("", NULL, 0);
f4dc4d17
DE
10876 else
10877 {
10878 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10879 gdb_assert (m_builder == nullptr);
804d2729 10880 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10881 m_builder.reset (new struct buildsym_compunit
10882 (COMPUNIT_OBJFILE (cust), "",
10883 COMPUNIT_DIRNAME (cust),
10884 compunit_language (cust),
10885 0, cust));
f4dc4d17 10886 }
f4dc4d17 10887 return;
3019eac3
DE
10888 }
10889
c24bdb02
KS
10890 line_header = lh.release ();
10891 line_header_die_owner = die;
3019eac3 10892
f4dc4d17
DE
10893 if (first_time)
10894 {
c24bdb02 10895 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10896
1fd60fc0
DE
10897 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10898 still initializing it, and our caller (a few levels up)
10899 process_full_type_unit still needs to know if this is the first
10900 time. */
10901
4ac93832
TT
10902 tu_group->symtabs
10903 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10904 struct symtab *, line_header->file_names_size ());
3019eac3 10905
7ba99d21
AT
10906 auto &file_names = line_header->file_names ();
10907 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10908 {
7ba99d21 10909 file_entry &fe = file_names[i];
c24bdb02
KS
10910 dwarf2_start_subfile (this, fe.name,
10911 fe.include_dir (line_header));
10912 buildsym_compunit *b = get_builder ();
10913 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10914 {
4c8aa72d
PA
10915 /* NOTE: start_subfile will recognize when it's been
10916 passed a file it has already seen. So we can't
10917 assume there's a simple mapping from
10918 cu->line_header->file_names to subfiles, plus
10919 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10920 b->get_current_subfile ()->symtab
10921 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10922 }
10923
c24bdb02 10924 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10925 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10926 }
10927 }
10928 else
3019eac3 10929 {
c24bdb02 10930 gdb_assert (m_builder == nullptr);
804d2729 10931 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10932 m_builder.reset (new struct buildsym_compunit
10933 (COMPUNIT_OBJFILE (cust), "",
10934 COMPUNIT_DIRNAME (cust),
10935 compunit_language (cust),
10936 0, cust));
f4dc4d17 10937
7ba99d21
AT
10938 auto &file_names = line_header->file_names ();
10939 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10940 {
7ba99d21 10941 file_entry &fe = file_names[i];
4c8aa72d 10942 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10943 }
3019eac3
DE
10944 }
10945
f4dc4d17
DE
10946 /* The main symtab is allocated last. Type units don't have DW_AT_name
10947 so they don't have a "real" (so to speak) symtab anyway.
10948 There is later code that will assign the main symtab to all symbols
10949 that don't have one. We need to handle the case of a symbol with a
10950 missing symtab (DW_AT_decl_file) anyway. */
10951}
3019eac3 10952
f4dc4d17
DE
10953/* Process DW_TAG_type_unit.
10954 For TUs we want to skip the first top level sibling if it's not the
10955 actual type being defined by this TU. In this case the first top
10956 level sibling is there to provide context only. */
3019eac3 10957
f4dc4d17
DE
10958static void
10959read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10960{
10961 struct die_info *child_die;
3019eac3 10962
f4dc4d17
DE
10963 prepare_one_comp_unit (cu, die, language_minimal);
10964
10965 /* Initialize (or reinitialize) the machinery for building symtabs.
10966 We do this before processing child DIEs, so that the line header table
10967 is available for DW_AT_decl_file. */
c24bdb02 10968 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10969
10970 if (die->child != NULL)
10971 {
10972 child_die = die->child;
10973 while (child_die && child_die->tag)
10974 {
10975 process_die (child_die, cu);
10976 child_die = sibling_die (child_die);
10977 }
10978 }
3019eac3
DE
10979}
10980\f
80626a55
DE
10981/* DWO/DWP files.
10982
10983 http://gcc.gnu.org/wiki/DebugFission
10984 http://gcc.gnu.org/wiki/DebugFissionDWP
10985
10986 To simplify handling of both DWO files ("object" files with the DWARF info)
10987 and DWP files (a file with the DWOs packaged up into one file), we treat
10988 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10989
10990static hashval_t
10991hash_dwo_file (const void *item)
10992{
9a3c8263 10993 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10994 hashval_t hash;
3019eac3 10995
a2ce51a0
DE
10996 hash = htab_hash_string (dwo_file->dwo_name);
10997 if (dwo_file->comp_dir != NULL)
10998 hash += htab_hash_string (dwo_file->comp_dir);
10999 return hash;
3019eac3
DE
11000}
11001
11002static int
11003eq_dwo_file (const void *item_lhs, const void *item_rhs)
11004{
9a3c8263
SM
11005 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11006 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11007
a2ce51a0
DE
11008 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11009 return 0;
11010 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11011 return lhs->comp_dir == rhs->comp_dir;
11012 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11013}
11014
11015/* Allocate a hash table for DWO files. */
11016
51ac9db5 11017static htab_up
298e9637 11018allocate_dwo_file_hash_table ()
3019eac3 11019{
51ac9db5
SM
11020 auto delete_dwo_file = [] (void *item)
11021 {
11022 struct dwo_file *dwo_file = (struct dwo_file *) item;
11023
11024 delete dwo_file;
11025 };
11026
bc68fb19
TT
11027 return htab_up (htab_create_alloc (41,
11028 hash_dwo_file,
11029 eq_dwo_file,
11030 delete_dwo_file,
11031 xcalloc, xfree));
3019eac3
DE
11032}
11033
80626a55
DE
11034/* Lookup DWO file DWO_NAME. */
11035
11036static void **
ed2dc618
SM
11037lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11038 const char *dwo_name,
11039 const char *comp_dir)
80626a55
DE
11040{
11041 struct dwo_file find_entry;
11042 void **slot;
11043
11044 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11045 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11046
0ac5b59e
DE
11047 find_entry.dwo_name = dwo_name;
11048 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11049 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11050 INSERT);
80626a55
DE
11051
11052 return slot;
11053}
11054
3019eac3
DE
11055static hashval_t
11056hash_dwo_unit (const void *item)
11057{
9a3c8263 11058 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11059
11060 /* This drops the top 32 bits of the id, but is ok for a hash. */
11061 return dwo_unit->signature;
11062}
11063
11064static int
11065eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11066{
9a3c8263
SM
11067 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11068 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11069
11070 /* The signature is assumed to be unique within the DWO file.
11071 So while object file CU dwo_id's always have the value zero,
11072 that's OK, assuming each object file DWO file has only one CU,
11073 and that's the rule for now. */
11074 return lhs->signature == rhs->signature;
11075}
11076
11077/* Allocate a hash table for DWO CUs,TUs.
11078 There is one of these tables for each of CUs,TUs for each DWO file. */
11079
b0b6a987 11080static htab_up
298e9637 11081allocate_dwo_unit_table ()
3019eac3
DE
11082{
11083 /* Start out with a pretty small number.
11084 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11085 return htab_up (htab_create_alloc (3,
11086 hash_dwo_unit,
11087 eq_dwo_unit,
11088 NULL, xcalloc, xfree));
3019eac3
DE
11089}
11090
19c3d4c9 11091/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11092
11093static void
19c3d4c9
DE
11094create_dwo_cu_reader (const struct die_reader_specs *reader,
11095 const gdb_byte *info_ptr,
11096 struct die_info *comp_unit_die,
c0ab21c2
TT
11097 struct dwo_file *dwo_file,
11098 struct dwo_unit *dwo_unit)
3019eac3
DE
11099{
11100 struct dwarf2_cu *cu = reader->cu;
9c541725 11101 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11102 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11103
a084a2a6
AT
11104 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11105 if (!signature.has_value ())
3019eac3 11106 {
b98664d3 11107 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11108 " its dwo_id [in module %s]"),
9d8780f0 11109 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11110 return;
11111 }
11112
3019eac3 11113 dwo_unit->dwo_file = dwo_file;
a084a2a6 11114 dwo_unit->signature = *signature;
8a0459fd 11115 dwo_unit->section = section;
9c541725 11116 dwo_unit->sect_off = sect_off;
3019eac3
DE
11117 dwo_unit->length = cu->per_cu->length;
11118
b4f54984 11119 if (dwarf_read_debug)
9d8780f0
SM
11120 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11121 sect_offset_str (sect_off),
9c541725 11122 hex_string (dwo_unit->signature));
3019eac3
DE
11123}
11124
33c5cd75 11125/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11126 Note: This function processes DWO files only, not DWP files. */
3019eac3 11127
33c5cd75 11128static void
ed2dc618 11129create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11130 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11131 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11132{
11133 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11134 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11135
96b79293 11136 section.read (objfile);
33c5cd75 11137 info_ptr = section.buffer;
3019eac3
DE
11138
11139 if (info_ptr == NULL)
33c5cd75 11140 return;
3019eac3 11141
b4f54984 11142 if (dwarf_read_debug)
19c3d4c9
DE
11143 {
11144 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11145 section.get_name (),
11146 section.get_file_name ());
19c3d4c9 11147 }
3019eac3 11148
33c5cd75 11149 end_ptr = info_ptr + section.size;
3019eac3
DE
11150 while (info_ptr < end_ptr)
11151 {
11152 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11153 struct dwo_unit read_unit {};
33c5cd75
DB
11154 struct dwo_unit *dwo_unit;
11155 void **slot;
11156 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11157
11158 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11159 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11160 per_cu.is_debug_types = 0;
33c5cd75
DB
11161 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11162 per_cu.section = &section;
11163
c0ab21c2
TT
11164 cutu_reader reader (&per_cu, cu, &dwo_file);
11165 if (!reader.dummy_p)
11166 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11167 &dwo_file, &read_unit);
33c5cd75
DB
11168 info_ptr += per_cu.length;
11169
11170 // If the unit could not be parsed, skip it.
c0ab21c2 11171 if (read_unit.dwo_file == NULL)
33c5cd75 11172 continue;
3019eac3 11173
33c5cd75 11174 if (cus_htab == NULL)
298e9637 11175 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11176
33c5cd75 11177 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11178 *dwo_unit = read_unit;
b0b6a987 11179 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11180 gdb_assert (slot != NULL);
11181 if (*slot != NULL)
19c3d4c9 11182 {
33c5cd75
DB
11183 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11184 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11185
b98664d3 11186 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11187 " the entry at offset %s, signature %s"),
11188 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11189 hex_string (dwo_unit->signature));
19c3d4c9 11190 }
33c5cd75 11191 *slot = (void *)dwo_unit;
3019eac3 11192 }
3019eac3
DE
11193}
11194
80626a55
DE
11195/* DWP file .debug_{cu,tu}_index section format:
11196 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11197
d2415c6c
DE
11198 DWP Version 1:
11199
80626a55
DE
11200 Both index sections have the same format, and serve to map a 64-bit
11201 signature to a set of section numbers. Each section begins with a header,
11202 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11203 indexes, and a pool of 32-bit section numbers. The index sections will be
11204 aligned at 8-byte boundaries in the file.
11205
d2415c6c
DE
11206 The index section header consists of:
11207
11208 V, 32 bit version number
11209 -, 32 bits unused
11210 N, 32 bit number of compilation units or type units in the index
11211 M, 32 bit number of slots in the hash table
80626a55 11212
d2415c6c 11213 Numbers are recorded using the byte order of the application binary.
80626a55 11214
d2415c6c
DE
11215 The hash table begins at offset 16 in the section, and consists of an array
11216 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11217 order of the application binary). Unused slots in the hash table are 0.
11218 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11219
d2415c6c
DE
11220 The parallel table begins immediately after the hash table
11221 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11222 array of 32-bit indexes (using the byte order of the application binary),
11223 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11224 table contains a 32-bit index into the pool of section numbers. For unused
11225 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11226
73869dc2
DE
11227 The pool of section numbers begins immediately following the hash table
11228 (at offset 16 + 12 * M from the beginning of the section). The pool of
11229 section numbers consists of an array of 32-bit words (using the byte order
11230 of the application binary). Each item in the array is indexed starting
11231 from 0. The hash table entry provides the index of the first section
11232 number in the set. Additional section numbers in the set follow, and the
11233 set is terminated by a 0 entry (section number 0 is not used in ELF).
11234
11235 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11236 section must be the first entry in the set, and the .debug_abbrev.dwo must
11237 be the second entry. Other members of the set may follow in any order.
11238
11239 ---
11240
11241 DWP Version 2:
11242
11243 DWP Version 2 combines all the .debug_info, etc. sections into one,
11244 and the entries in the index tables are now offsets into these sections.
11245 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11246 section.
11247
11248 Index Section Contents:
11249 Header
11250 Hash Table of Signatures dwp_hash_table.hash_table
11251 Parallel Table of Indices dwp_hash_table.unit_table
11252 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11253 Table of Section Sizes dwp_hash_table.v2.sizes
11254
11255 The index section header consists of:
11256
11257 V, 32 bit version number
11258 L, 32 bit number of columns in the table of section offsets
11259 N, 32 bit number of compilation units or type units in the index
11260 M, 32 bit number of slots in the hash table
11261
11262 Numbers are recorded using the byte order of the application binary.
11263
11264 The hash table has the same format as version 1.
11265 The parallel table of indices has the same format as version 1,
11266 except that the entries are origin-1 indices into the table of sections
11267 offsets and the table of section sizes.
11268
11269 The table of offsets begins immediately following the parallel table
11270 (at offset 16 + 12 * M from the beginning of the section). The table is
11271 a two-dimensional array of 32-bit words (using the byte order of the
11272 application binary), with L columns and N+1 rows, in row-major order.
11273 Each row in the array is indexed starting from 0. The first row provides
11274 a key to the remaining rows: each column in this row provides an identifier
11275 for a debug section, and the offsets in the same column of subsequent rows
11276 refer to that section. The section identifiers are:
11277
11278 DW_SECT_INFO 1 .debug_info.dwo
11279 DW_SECT_TYPES 2 .debug_types.dwo
11280 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11281 DW_SECT_LINE 4 .debug_line.dwo
11282 DW_SECT_LOC 5 .debug_loc.dwo
11283 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11284 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11285 DW_SECT_MACRO 8 .debug_macro.dwo
11286
11287 The offsets provided by the CU and TU index sections are the base offsets
11288 for the contributions made by each CU or TU to the corresponding section
11289 in the package file. Each CU and TU header contains an abbrev_offset
11290 field, used to find the abbreviations table for that CU or TU within the
11291 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11292 be interpreted as relative to the base offset given in the index section.
11293 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11294 should be interpreted as relative to the base offset for .debug_line.dwo,
11295 and offsets into other debug sections obtained from DWARF attributes should
11296 also be interpreted as relative to the corresponding base offset.
11297
11298 The table of sizes begins immediately following the table of offsets.
11299 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11300 with L columns and N rows, in row-major order. Each row in the array is
11301 indexed starting from 1 (row 0 is shared by the two tables).
11302
11303 ---
11304
11305 Hash table lookup is handled the same in version 1 and 2:
11306
11307 We assume that N and M will not exceed 2^32 - 1.
11308 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11309
d2415c6c
DE
11310 Given a 64-bit compilation unit signature or a type signature S, an entry
11311 in the hash table is located as follows:
80626a55 11312
d2415c6c
DE
11313 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11314 the low-order k bits all set to 1.
80626a55 11315
d2415c6c 11316 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11317
d2415c6c
DE
11318 3) If the hash table entry at index H matches the signature, use that
11319 entry. If the hash table entry at index H is unused (all zeroes),
11320 terminate the search: the signature is not present in the table.
80626a55 11321
d2415c6c 11322 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11323
d2415c6c 11324 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11325 to stop at an unused slot or find the match. */
80626a55
DE
11326
11327/* Create a hash table to map DWO IDs to their CU/TU entry in
11328 .debug_{info,types}.dwo in DWP_FILE.
11329 Returns NULL if there isn't one.
11330 Note: This function processes DWP files only, not DWO files. */
11331
11332static struct dwp_hash_table *
ed2dc618
SM
11333create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11334 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11335{
11336 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11337 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11338 const gdb_byte *index_ptr, *index_end;
80626a55 11339 struct dwarf2_section_info *index;
73869dc2 11340 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11341 struct dwp_hash_table *htab;
11342
11343 if (is_debug_types)
11344 index = &dwp_file->sections.tu_index;
11345 else
11346 index = &dwp_file->sections.cu_index;
11347
96b79293 11348 if (index->empty ())
80626a55 11349 return NULL;
96b79293 11350 index->read (objfile);
80626a55
DE
11351
11352 index_ptr = index->buffer;
11353 index_end = index_ptr + index->size;
11354
11355 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11356 index_ptr += 4;
11357 if (version == 2)
11358 nr_columns = read_4_bytes (dbfd, index_ptr);
11359 else
11360 nr_columns = 0;
11361 index_ptr += 4;
80626a55
DE
11362 nr_units = read_4_bytes (dbfd, index_ptr);
11363 index_ptr += 4;
11364 nr_slots = read_4_bytes (dbfd, index_ptr);
11365 index_ptr += 4;
11366
73869dc2 11367 if (version != 1 && version != 2)
80626a55 11368 {
21aa081e 11369 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11370 " [in module %s]"),
21aa081e 11371 pulongest (version), dwp_file->name);
80626a55
DE
11372 }
11373 if (nr_slots != (nr_slots & -nr_slots))
11374 {
21aa081e 11375 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11376 " is not power of 2 [in module %s]"),
21aa081e 11377 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11378 }
11379
11380 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11381 htab->version = version;
11382 htab->nr_columns = nr_columns;
80626a55
DE
11383 htab->nr_units = nr_units;
11384 htab->nr_slots = nr_slots;
11385 htab->hash_table = index_ptr;
11386 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11387
11388 /* Exit early if the table is empty. */
11389 if (nr_slots == 0 || nr_units == 0
11390 || (version == 2 && nr_columns == 0))
11391 {
11392 /* All must be zero. */
11393 if (nr_slots != 0 || nr_units != 0
11394 || (version == 2 && nr_columns != 0))
11395 {
b98664d3 11396 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11397 " all zero [in modules %s]"),
11398 dwp_file->name);
11399 }
11400 return htab;
11401 }
11402
11403 if (version == 1)
11404 {
11405 htab->section_pool.v1.indices =
11406 htab->unit_table + sizeof (uint32_t) * nr_slots;
11407 /* It's harder to decide whether the section is too small in v1.
11408 V1 is deprecated anyway so we punt. */
11409 }
11410 else
11411 {
11412 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11413 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11414 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11415 /* Reverse map for error checking. */
11416 int ids_seen[DW_SECT_MAX + 1];
11417 int i;
11418
11419 if (nr_columns < 2)
11420 {
11421 error (_("Dwarf Error: bad DWP hash table, too few columns"
11422 " in section table [in module %s]"),
11423 dwp_file->name);
11424 }
11425 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11426 {
11427 error (_("Dwarf Error: bad DWP hash table, too many columns"
11428 " in section table [in module %s]"),
11429 dwp_file->name);
11430 }
04fd5eed
GB
11431 memset (ids, 255, sizeof_ids);
11432 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11433 for (i = 0; i < nr_columns; ++i)
11434 {
11435 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11436
11437 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11438 {
11439 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11440 " in section table [in module %s]"),
11441 id, dwp_file->name);
11442 }
11443 if (ids_seen[id] != -1)
11444 {
11445 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11446 " id %d in section table [in module %s]"),
11447 id, dwp_file->name);
11448 }
11449 ids_seen[id] = i;
11450 ids[i] = id;
11451 }
11452 /* Must have exactly one info or types section. */
11453 if (((ids_seen[DW_SECT_INFO] != -1)
11454 + (ids_seen[DW_SECT_TYPES] != -1))
11455 != 1)
11456 {
11457 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11458 " DWO info/types section [in module %s]"),
11459 dwp_file->name);
11460 }
11461 /* Must have an abbrev section. */
11462 if (ids_seen[DW_SECT_ABBREV] == -1)
11463 {
11464 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11465 " section [in module %s]"),
11466 dwp_file->name);
11467 }
11468 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11469 htab->section_pool.v2.sizes =
11470 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11471 * nr_units * nr_columns);
11472 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11473 * nr_units * nr_columns))
11474 > index_end)
11475 {
11476 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11477 " [in module %s]"),
11478 dwp_file->name);
11479 }
11480 }
80626a55
DE
11481
11482 return htab;
11483}
11484
11485/* Update SECTIONS with the data from SECTP.
11486
11487 This function is like the other "locate" section routines that are
11488 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11489 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11490
11491 The result is non-zero for success, or zero if an error was found. */
11492
11493static int
73869dc2
DE
11494locate_v1_virtual_dwo_sections (asection *sectp,
11495 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11496{
11497 const struct dwop_section_names *names = &dwop_section_names;
11498
11499 if (section_is_p (sectp->name, &names->abbrev_dwo))
11500 {
11501 /* There can be only one. */
049412e3 11502 if (sections->abbrev.s.section != NULL)
80626a55 11503 return 0;
049412e3 11504 sections->abbrev.s.section = sectp;
fd361982 11505 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11506 }
11507 else if (section_is_p (sectp->name, &names->info_dwo)
11508 || section_is_p (sectp->name, &names->types_dwo))
11509 {
11510 /* There can be only one. */
049412e3 11511 if (sections->info_or_types.s.section != NULL)
80626a55 11512 return 0;
049412e3 11513 sections->info_or_types.s.section = sectp;
fd361982 11514 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11515 }
11516 else if (section_is_p (sectp->name, &names->line_dwo))
11517 {
11518 /* There can be only one. */
049412e3 11519 if (sections->line.s.section != NULL)
80626a55 11520 return 0;
049412e3 11521 sections->line.s.section = sectp;
fd361982 11522 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11523 }
11524 else if (section_is_p (sectp->name, &names->loc_dwo))
11525 {
11526 /* There can be only one. */
049412e3 11527 if (sections->loc.s.section != NULL)
80626a55 11528 return 0;
049412e3 11529 sections->loc.s.section = sectp;
fd361982 11530 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11531 }
11532 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11533 {
11534 /* There can be only one. */
049412e3 11535 if (sections->macinfo.s.section != NULL)
80626a55 11536 return 0;
049412e3 11537 sections->macinfo.s.section = sectp;
fd361982 11538 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11539 }
11540 else if (section_is_p (sectp->name, &names->macro_dwo))
11541 {
11542 /* There can be only one. */
049412e3 11543 if (sections->macro.s.section != NULL)
80626a55 11544 return 0;
049412e3 11545 sections->macro.s.section = sectp;
fd361982 11546 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11547 }
11548 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11549 {
11550 /* There can be only one. */
049412e3 11551 if (sections->str_offsets.s.section != NULL)
80626a55 11552 return 0;
049412e3 11553 sections->str_offsets.s.section = sectp;
fd361982 11554 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11555 }
11556 else
11557 {
11558 /* No other kind of section is valid. */
11559 return 0;
11560 }
11561
11562 return 1;
11563}
11564
73869dc2
DE
11565/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11566 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11567 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11568 This is for DWP version 1 files. */
80626a55
DE
11569
11570static struct dwo_unit *
ed2dc618
SM
11571create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11572 struct dwp_file *dwp_file,
73869dc2
DE
11573 uint32_t unit_index,
11574 const char *comp_dir,
11575 ULONGEST signature, int is_debug_types)
80626a55
DE
11576{
11577 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11578 const struct dwp_hash_table *dwp_htab =
11579 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11580 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11581 const char *kind = is_debug_types ? "TU" : "CU";
11582 struct dwo_file *dwo_file;
11583 struct dwo_unit *dwo_unit;
73869dc2 11584 struct virtual_v1_dwo_sections sections;
80626a55 11585 void **dwo_file_slot;
80626a55
DE
11586 int i;
11587
73869dc2
DE
11588 gdb_assert (dwp_file->version == 1);
11589
b4f54984 11590 if (dwarf_read_debug)
80626a55 11591 {
73869dc2 11592 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11593 kind,
73869dc2 11594 pulongest (unit_index), hex_string (signature),
80626a55
DE
11595 dwp_file->name);
11596 }
11597
19ac8c2e 11598 /* Fetch the sections of this DWO unit.
80626a55
DE
11599 Put a limit on the number of sections we look for so that bad data
11600 doesn't cause us to loop forever. */
11601
73869dc2 11602#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11603 (1 /* .debug_info or .debug_types */ \
11604 + 1 /* .debug_abbrev */ \
11605 + 1 /* .debug_line */ \
11606 + 1 /* .debug_loc */ \
11607 + 1 /* .debug_str_offsets */ \
19ac8c2e 11608 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11609 + 1 /* trailing zero */)
11610
11611 memset (&sections, 0, sizeof (sections));
80626a55 11612
73869dc2 11613 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11614 {
11615 asection *sectp;
11616 uint32_t section_nr =
11617 read_4_bytes (dbfd,
73869dc2
DE
11618 dwp_htab->section_pool.v1.indices
11619 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11620
11621 if (section_nr == 0)
11622 break;
11623 if (section_nr >= dwp_file->num_sections)
11624 {
11625 error (_("Dwarf Error: bad DWP hash table, section number too large"
11626 " [in module %s]"),
11627 dwp_file->name);
11628 }
11629
11630 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11631 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11632 {
11633 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11634 " [in module %s]"),
11635 dwp_file->name);
11636 }
11637 }
11638
11639 if (i < 2
96b79293
TT
11640 || sections.info_or_types.empty ()
11641 || sections.abbrev.empty ())
80626a55
DE
11642 {
11643 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11644 " [in module %s]"),
11645 dwp_file->name);
11646 }
73869dc2 11647 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11648 {
11649 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11650 " [in module %s]"),
11651 dwp_file->name);
11652 }
11653
11654 /* It's easier for the rest of the code if we fake a struct dwo_file and
11655 have dwo_unit "live" in that. At least for now.
11656
11657 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11658 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11659 file, we can combine them back into a virtual DWO file to save space
11660 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11661 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11662
791afaa2
TT
11663 std::string virtual_dwo_name =
11664 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11665 sections.abbrev.get_id (),
11666 sections.line.get_id (),
11667 sections.loc.get_id (),
11668 sections.str_offsets.get_id ());
80626a55 11669 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11670 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11671 virtual_dwo_name.c_str (),
11672 comp_dir);
80626a55
DE
11673 /* Create one if necessary. */
11674 if (*dwo_file_slot == NULL)
11675 {
b4f54984 11676 if (dwarf_read_debug)
80626a55
DE
11677 {
11678 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11679 virtual_dwo_name.c_str ());
80626a55 11680 }
51ac9db5 11681 dwo_file = new struct dwo_file;
efba19b0
TT
11682 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11683 virtual_dwo_name);
0ac5b59e 11684 dwo_file->comp_dir = comp_dir;
80626a55
DE
11685 dwo_file->sections.abbrev = sections.abbrev;
11686 dwo_file->sections.line = sections.line;
11687 dwo_file->sections.loc = sections.loc;
11688 dwo_file->sections.macinfo = sections.macinfo;
11689 dwo_file->sections.macro = sections.macro;
11690 dwo_file->sections.str_offsets = sections.str_offsets;
11691 /* The "str" section is global to the entire DWP file. */
11692 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11693 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11694 there's no need to record it in dwo_file.
11695 Also, we can't simply record type sections in dwo_file because
11696 we record a pointer into the vector in dwo_unit. As we collect more
11697 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11698 for it, invalidating all copies of pointers into the previous
11699 contents. */
80626a55
DE
11700 *dwo_file_slot = dwo_file;
11701 }
11702 else
11703 {
b4f54984 11704 if (dwarf_read_debug)
80626a55
DE
11705 {
11706 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11707 virtual_dwo_name.c_str ());
80626a55 11708 }
9a3c8263 11709 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11710 }
80626a55
DE
11711
11712 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11713 dwo_unit->dwo_file = dwo_file;
11714 dwo_unit->signature = signature;
8d749320
SM
11715 dwo_unit->section =
11716 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11717 *dwo_unit->section = sections.info_or_types;
57d63ce2 11718 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11719
11720 return dwo_unit;
11721}
11722
73869dc2
DE
11723/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11724 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11725 piece within that section used by a TU/CU, return a virtual section
11726 of just that piece. */
11727
11728static struct dwarf2_section_info
ed2dc618
SM
11729create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11730 struct dwarf2_section_info *section,
73869dc2
DE
11731 bfd_size_type offset, bfd_size_type size)
11732{
11733 struct dwarf2_section_info result;
11734 asection *sectp;
11735
11736 gdb_assert (section != NULL);
11737 gdb_assert (!section->is_virtual);
11738
11739 memset (&result, 0, sizeof (result));
11740 result.s.containing_section = section;
dc4ccb6f 11741 result.is_virtual = true;
73869dc2
DE
11742
11743 if (size == 0)
11744 return result;
11745
96b79293 11746 sectp = section->get_bfd_section ();
73869dc2
DE
11747
11748 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11749 bounds of the real section. This is a pretty-rare event, so just
11750 flag an error (easier) instead of a warning and trying to cope. */
11751 if (sectp == NULL
fd361982 11752 || offset + size > bfd_section_size (sectp))
73869dc2 11753 {
73869dc2
DE
11754 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11755 " in section %s [in module %s]"),
fd361982 11756 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11757 objfile_name (dwarf2_per_objfile->objfile));
11758 }
11759
11760 result.virtual_offset = offset;
11761 result.size = size;
11762 return result;
11763}
11764
11765/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11766 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11767 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11768 This is for DWP version 2 files. */
11769
11770static struct dwo_unit *
ed2dc618
SM
11771create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11772 struct dwp_file *dwp_file,
73869dc2
DE
11773 uint32_t unit_index,
11774 const char *comp_dir,
11775 ULONGEST signature, int is_debug_types)
11776{
11777 struct objfile *objfile = dwarf2_per_objfile->objfile;
11778 const struct dwp_hash_table *dwp_htab =
11779 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11780 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11781 const char *kind = is_debug_types ? "TU" : "CU";
11782 struct dwo_file *dwo_file;
11783 struct dwo_unit *dwo_unit;
11784 struct virtual_v2_dwo_sections sections;
11785 void **dwo_file_slot;
73869dc2
DE
11786 int i;
11787
11788 gdb_assert (dwp_file->version == 2);
11789
b4f54984 11790 if (dwarf_read_debug)
73869dc2
DE
11791 {
11792 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11793 kind,
11794 pulongest (unit_index), hex_string (signature),
11795 dwp_file->name);
11796 }
11797
11798 /* Fetch the section offsets of this DWO unit. */
11799
11800 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11801
11802 for (i = 0; i < dwp_htab->nr_columns; ++i)
11803 {
11804 uint32_t offset = read_4_bytes (dbfd,
11805 dwp_htab->section_pool.v2.offsets
11806 + (((unit_index - 1) * dwp_htab->nr_columns
11807 + i)
11808 * sizeof (uint32_t)));
11809 uint32_t size = read_4_bytes (dbfd,
11810 dwp_htab->section_pool.v2.sizes
11811 + (((unit_index - 1) * dwp_htab->nr_columns
11812 + i)
11813 * sizeof (uint32_t)));
11814
11815 switch (dwp_htab->section_pool.v2.section_ids[i])
11816 {
11817 case DW_SECT_INFO:
11818 case DW_SECT_TYPES:
11819 sections.info_or_types_offset = offset;
11820 sections.info_or_types_size = size;
11821 break;
11822 case DW_SECT_ABBREV:
11823 sections.abbrev_offset = offset;
11824 sections.abbrev_size = size;
11825 break;
11826 case DW_SECT_LINE:
11827 sections.line_offset = offset;
11828 sections.line_size = size;
11829 break;
11830 case DW_SECT_LOC:
11831 sections.loc_offset = offset;
11832 sections.loc_size = size;
11833 break;
11834 case DW_SECT_STR_OFFSETS:
11835 sections.str_offsets_offset = offset;
11836 sections.str_offsets_size = size;
11837 break;
11838 case DW_SECT_MACINFO:
11839 sections.macinfo_offset = offset;
11840 sections.macinfo_size = size;
11841 break;
11842 case DW_SECT_MACRO:
11843 sections.macro_offset = offset;
11844 sections.macro_size = size;
11845 break;
11846 }
11847 }
11848
11849 /* It's easier for the rest of the code if we fake a struct dwo_file and
11850 have dwo_unit "live" in that. At least for now.
11851
11852 The DWP file can be made up of a random collection of CUs and TUs.
11853 However, for each CU + set of TUs that came from the same original DWO
11854 file, we can combine them back into a virtual DWO file to save space
11855 (fewer struct dwo_file objects to allocate). Remember that for really
11856 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11857
791afaa2
TT
11858 std::string virtual_dwo_name =
11859 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11860 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11861 (long) (sections.line_size ? sections.line_offset : 0),
11862 (long) (sections.loc_size ? sections.loc_offset : 0),
11863 (long) (sections.str_offsets_size
11864 ? sections.str_offsets_offset : 0));
73869dc2 11865 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11866 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11867 virtual_dwo_name.c_str (),
11868 comp_dir);
73869dc2
DE
11869 /* Create one if necessary. */
11870 if (*dwo_file_slot == NULL)
11871 {
b4f54984 11872 if (dwarf_read_debug)
73869dc2
DE
11873 {
11874 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11875 virtual_dwo_name.c_str ());
73869dc2 11876 }
51ac9db5 11877 dwo_file = new struct dwo_file;
efba19b0
TT
11878 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11879 virtual_dwo_name);
73869dc2
DE
11880 dwo_file->comp_dir = comp_dir;
11881 dwo_file->sections.abbrev =
ed2dc618 11882 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11883 sections.abbrev_offset, sections.abbrev_size);
11884 dwo_file->sections.line =
ed2dc618 11885 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11886 sections.line_offset, sections.line_size);
11887 dwo_file->sections.loc =
ed2dc618 11888 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11889 sections.loc_offset, sections.loc_size);
11890 dwo_file->sections.macinfo =
ed2dc618 11891 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11892 sections.macinfo_offset, sections.macinfo_size);
11893 dwo_file->sections.macro =
ed2dc618 11894 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11895 sections.macro_offset, sections.macro_size);
11896 dwo_file->sections.str_offsets =
ed2dc618
SM
11897 create_dwp_v2_section (dwarf2_per_objfile,
11898 &dwp_file->sections.str_offsets,
73869dc2
DE
11899 sections.str_offsets_offset,
11900 sections.str_offsets_size);
11901 /* The "str" section is global to the entire DWP file. */
11902 dwo_file->sections.str = dwp_file->sections.str;
11903 /* The info or types section is assigned below to dwo_unit,
11904 there's no need to record it in dwo_file.
11905 Also, we can't simply record type sections in dwo_file because
11906 we record a pointer into the vector in dwo_unit. As we collect more
11907 types we'll grow the vector and eventually have to reallocate space
11908 for it, invalidating all copies of pointers into the previous
11909 contents. */
11910 *dwo_file_slot = dwo_file;
11911 }
11912 else
11913 {
b4f54984 11914 if (dwarf_read_debug)
73869dc2
DE
11915 {
11916 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11917 virtual_dwo_name.c_str ());
73869dc2 11918 }
9a3c8263 11919 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11920 }
73869dc2
DE
11921
11922 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11923 dwo_unit->dwo_file = dwo_file;
11924 dwo_unit->signature = signature;
8d749320
SM
11925 dwo_unit->section =
11926 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11927 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11928 is_debug_types
73869dc2
DE
11929 ? &dwp_file->sections.types
11930 : &dwp_file->sections.info,
11931 sections.info_or_types_offset,
11932 sections.info_or_types_size);
11933 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11934
11935 return dwo_unit;
11936}
11937
57d63ce2
DE
11938/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11939 Returns NULL if the signature isn't found. */
80626a55
DE
11940
11941static struct dwo_unit *
ed2dc618
SM
11942lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11943 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11944 ULONGEST signature, int is_debug_types)
80626a55 11945{
57d63ce2
DE
11946 const struct dwp_hash_table *dwp_htab =
11947 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11948 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11949 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11950 uint32_t hash = signature & mask;
11951 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11952 unsigned int i;
11953 void **slot;
870f88f7 11954 struct dwo_unit find_dwo_cu;
80626a55
DE
11955
11956 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11957 find_dwo_cu.signature = signature;
19ac8c2e 11958 slot = htab_find_slot (is_debug_types
48b490f2
TT
11959 ? dwp_file->loaded_tus.get ()
11960 : dwp_file->loaded_cus.get (),
19ac8c2e 11961 &find_dwo_cu, INSERT);
80626a55
DE
11962
11963 if (*slot != NULL)
9a3c8263 11964 return (struct dwo_unit *) *slot;
80626a55
DE
11965
11966 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11967 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11968 {
11969 ULONGEST signature_in_table;
11970
11971 signature_in_table =
57d63ce2 11972 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11973 if (signature_in_table == signature)
11974 {
57d63ce2
DE
11975 uint32_t unit_index =
11976 read_4_bytes (dbfd,
11977 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11978
73869dc2
DE
11979 if (dwp_file->version == 1)
11980 {
ed2dc618
SM
11981 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11982 dwp_file, unit_index,
73869dc2
DE
11983 comp_dir, signature,
11984 is_debug_types);
11985 }
11986 else
11987 {
ed2dc618
SM
11988 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11989 dwp_file, unit_index,
73869dc2
DE
11990 comp_dir, signature,
11991 is_debug_types);
11992 }
9a3c8263 11993 return (struct dwo_unit *) *slot;
80626a55
DE
11994 }
11995 if (signature_in_table == 0)
11996 return NULL;
11997 hash = (hash + hash2) & mask;
11998 }
11999
12000 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12001 " [in module %s]"),
12002 dwp_file->name);
12003}
12004
ab5088bf 12005/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12006 Open the file specified by FILE_NAME and hand it off to BFD for
12007 preliminary analysis. Return a newly initialized bfd *, which
12008 includes a canonicalized copy of FILE_NAME.
80626a55 12009 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12010 SEARCH_CWD is true if the current directory is to be searched.
12011 It will be searched before debug-file-directory.
13aaf454
DE
12012 If successful, the file is added to the bfd include table of the
12013 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12014 If unable to find/open the file, return NULL.
3019eac3
DE
12015 NOTE: This function is derived from symfile_bfd_open. */
12016
192b62ce 12017static gdb_bfd_ref_ptr
ed2dc618
SM
12018try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12019 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12020{
24b9144d 12021 int desc;
9c02c129
DE
12022 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12023 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12024 to debug_file_directory. */
e0cc99a6 12025 const char *search_path;
9c02c129
DE
12026 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12027
e0cc99a6 12028 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12029 if (search_cwd)
12030 {
12031 if (*debug_file_directory != '\0')
e0cc99a6
TT
12032 {
12033 search_path_holder.reset (concat (".", dirname_separator_string,
12034 debug_file_directory,
12035 (char *) NULL));
12036 search_path = search_path_holder.get ();
12037 }
6ac97d4c 12038 else
e0cc99a6 12039 search_path = ".";
6ac97d4c 12040 }
9c02c129 12041 else
e0cc99a6 12042 search_path = debug_file_directory;
3019eac3 12043
24b9144d 12044 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12045 if (is_dwp)
12046 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12047
12048 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12049 desc = openp (search_path, flags, file_name,
3019eac3
DE
12050 O_RDONLY | O_BINARY, &absolute_name);
12051 if (desc < 0)
12052 return NULL;
12053
e0cc99a6
TT
12054 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12055 gnutarget, desc));
9c02c129
DE
12056 if (sym_bfd == NULL)
12057 return NULL;
192b62ce 12058 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12059
192b62ce
TT
12060 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12061 return NULL;
3019eac3 12062
13aaf454
DE
12063 /* Success. Record the bfd as having been included by the objfile's bfd.
12064 This is important because things like demangled_names_hash lives in the
12065 objfile's per_bfd space and may have references to things like symbol
12066 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12067 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12068
3019eac3
DE
12069 return sym_bfd;
12070}
12071
ab5088bf 12072/* Try to open DWO file FILE_NAME.
3019eac3
DE
12073 COMP_DIR is the DW_AT_comp_dir attribute.
12074 The result is the bfd handle of the file.
12075 If there is a problem finding or opening the file, return NULL.
12076 Upon success, the canonicalized path of the file is stored in the bfd,
12077 same as symfile_bfd_open. */
12078
192b62ce 12079static gdb_bfd_ref_ptr
ed2dc618
SM
12080open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12081 const char *file_name, const char *comp_dir)
3019eac3 12082{
80626a55 12083 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12084 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12085 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12086
12087 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12088
12089 if (comp_dir != NULL)
12090 {
43816ebc
TT
12091 gdb::unique_xmalloc_ptr<char> path_to_try
12092 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12093
12094 /* NOTE: If comp_dir is a relative path, this will also try the
12095 search path, which seems useful. */
ed2dc618 12096 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12097 path_to_try.get (),
ed2dc618 12098 0 /*is_dwp*/,
192b62ce 12099 1 /*search_cwd*/));
3019eac3
DE
12100 if (abfd != NULL)
12101 return abfd;
12102 }
12103
12104 /* That didn't work, try debug-file-directory, which, despite its name,
12105 is a list of paths. */
12106
12107 if (*debug_file_directory == '\0')
12108 return NULL;
12109
ed2dc618
SM
12110 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12111 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12112}
12113
80626a55
DE
12114/* This function is mapped across the sections and remembers the offset and
12115 size of each of the DWO debugging sections we are interested in. */
12116
12117static void
12118dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12119{
9a3c8263 12120 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12121 const struct dwop_section_names *names = &dwop_section_names;
12122
12123 if (section_is_p (sectp->name, &names->abbrev_dwo))
12124 {
049412e3 12125 dwo_sections->abbrev.s.section = sectp;
fd361982 12126 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12127 }
12128 else if (section_is_p (sectp->name, &names->info_dwo))
12129 {
049412e3 12130 dwo_sections->info.s.section = sectp;
fd361982 12131 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12132 }
12133 else if (section_is_p (sectp->name, &names->line_dwo))
12134 {
049412e3 12135 dwo_sections->line.s.section = sectp;
fd361982 12136 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12137 }
12138 else if (section_is_p (sectp->name, &names->loc_dwo))
12139 {
049412e3 12140 dwo_sections->loc.s.section = sectp;
fd361982 12141 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12142 }
12143 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12144 {
049412e3 12145 dwo_sections->macinfo.s.section = sectp;
fd361982 12146 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12147 }
12148 else if (section_is_p (sectp->name, &names->macro_dwo))
12149 {
049412e3 12150 dwo_sections->macro.s.section = sectp;
fd361982 12151 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12152 }
12153 else if (section_is_p (sectp->name, &names->str_dwo))
12154 {
049412e3 12155 dwo_sections->str.s.section = sectp;
fd361982 12156 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12157 }
12158 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12159 {
049412e3 12160 dwo_sections->str_offsets.s.section = sectp;
fd361982 12161 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12162 }
12163 else if (section_is_p (sectp->name, &names->types_dwo))
12164 {
12165 struct dwarf2_section_info type_section;
12166
12167 memset (&type_section, 0, sizeof (type_section));
049412e3 12168 type_section.s.section = sectp;
fd361982 12169 type_section.size = bfd_section_size (sectp);
fd5866f6 12170 dwo_sections->types.push_back (type_section);
80626a55
DE
12171 }
12172}
12173
ab5088bf 12174/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12175 by PER_CU. This is for the non-DWP case.
80626a55 12176 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12177
12178static struct dwo_file *
0ac5b59e
DE
12179open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12180 const char *dwo_name, const char *comp_dir)
3019eac3 12181{
ed2dc618 12182 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12183
fb1eb2f9 12184 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12185 if (dbfd == NULL)
12186 {
b4f54984 12187 if (dwarf_read_debug)
80626a55
DE
12188 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12189 return NULL;
12190 }
263db9a1 12191
51ac9db5 12192 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12193 dwo_file->dwo_name = dwo_name;
12194 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12195 dwo_file->dbfd = std::move (dbfd);
3019eac3 12196
fb1eb2f9 12197 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12198 &dwo_file->sections);
3019eac3 12199
18a8505e
AT
12200 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12201 dwo_file->sections.info, dwo_file->cus);
3019eac3 12202
263db9a1 12203 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12204 dwo_file->sections.types, dwo_file->tus);
3019eac3 12205
b4f54984 12206 if (dwarf_read_debug)
80626a55
DE
12207 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12208
263db9a1 12209 return dwo_file.release ();
3019eac3
DE
12210}
12211
80626a55 12212/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12213 size of each of the DWP debugging sections common to version 1 and 2 that
12214 we are interested in. */
3019eac3 12215
80626a55 12216static void
73869dc2
DE
12217dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12218 void *dwp_file_ptr)
3019eac3 12219{
9a3c8263 12220 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12221 const struct dwop_section_names *names = &dwop_section_names;
12222 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12223
80626a55 12224 /* Record the ELF section number for later lookup: this is what the
73869dc2 12225 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12226 gdb_assert (elf_section_nr < dwp_file->num_sections);
12227 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12228
80626a55
DE
12229 /* Look for specific sections that we need. */
12230 if (section_is_p (sectp->name, &names->str_dwo))
12231 {
049412e3 12232 dwp_file->sections.str.s.section = sectp;
fd361982 12233 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12234 }
12235 else if (section_is_p (sectp->name, &names->cu_index))
12236 {
049412e3 12237 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12238 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12239 }
12240 else if (section_is_p (sectp->name, &names->tu_index))
12241 {
049412e3 12242 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12243 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12244 }
12245}
3019eac3 12246
73869dc2
DE
12247/* This function is mapped across the sections and remembers the offset and
12248 size of each of the DWP version 2 debugging sections that we are interested
12249 in. This is split into a separate function because we don't know if we
12250 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12251
12252static void
12253dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12254{
9a3c8263 12255 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12256 const struct dwop_section_names *names = &dwop_section_names;
12257 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12258
12259 /* Record the ELF section number for later lookup: this is what the
12260 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12261 gdb_assert (elf_section_nr < dwp_file->num_sections);
12262 dwp_file->elf_sections[elf_section_nr] = sectp;
12263
12264 /* Look for specific sections that we need. */
12265 if (section_is_p (sectp->name, &names->abbrev_dwo))
12266 {
049412e3 12267 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12268 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12269 }
12270 else if (section_is_p (sectp->name, &names->info_dwo))
12271 {
049412e3 12272 dwp_file->sections.info.s.section = sectp;
fd361982 12273 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12274 }
12275 else if (section_is_p (sectp->name, &names->line_dwo))
12276 {
049412e3 12277 dwp_file->sections.line.s.section = sectp;
fd361982 12278 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12279 }
12280 else if (section_is_p (sectp->name, &names->loc_dwo))
12281 {
049412e3 12282 dwp_file->sections.loc.s.section = sectp;
fd361982 12283 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12284 }
12285 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12286 {
049412e3 12287 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12288 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12289 }
12290 else if (section_is_p (sectp->name, &names->macro_dwo))
12291 {
049412e3 12292 dwp_file->sections.macro.s.section = sectp;
fd361982 12293 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12294 }
12295 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12296 {
049412e3 12297 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12298 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12299 }
12300 else if (section_is_p (sectp->name, &names->types_dwo))
12301 {
049412e3 12302 dwp_file->sections.types.s.section = sectp;
fd361982 12303 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12304 }
12305}
12306
80626a55 12307/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12308
80626a55
DE
12309static hashval_t
12310hash_dwp_loaded_cutus (const void *item)
12311{
9a3c8263 12312 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12313
80626a55
DE
12314 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12315 return dwo_unit->signature;
3019eac3
DE
12316}
12317
80626a55 12318/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12319
80626a55
DE
12320static int
12321eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12322{
9a3c8263
SM
12323 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12324 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12325
80626a55
DE
12326 return dua->signature == dub->signature;
12327}
3019eac3 12328
80626a55 12329/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12330
48b490f2 12331static htab_up
298e9637 12332allocate_dwp_loaded_cutus_table ()
80626a55 12333{
48b490f2
TT
12334 return htab_up (htab_create_alloc (3,
12335 hash_dwp_loaded_cutus,
12336 eq_dwp_loaded_cutus,
12337 NULL, xcalloc, xfree));
80626a55 12338}
3019eac3 12339
ab5088bf
DE
12340/* Try to open DWP file FILE_NAME.
12341 The result is the bfd handle of the file.
12342 If there is a problem finding or opening the file, return NULL.
12343 Upon success, the canonicalized path of the file is stored in the bfd,
12344 same as symfile_bfd_open. */
12345
192b62ce 12346static gdb_bfd_ref_ptr
ed2dc618
SM
12347open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12348 const char *file_name)
ab5088bf 12349{
ed2dc618
SM
12350 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12351 1 /*is_dwp*/,
192b62ce 12352 1 /*search_cwd*/));
6ac97d4c
DE
12353 if (abfd != NULL)
12354 return abfd;
12355
12356 /* Work around upstream bug 15652.
12357 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12358 [Whether that's a "bug" is debatable, but it is getting in our way.]
12359 We have no real idea where the dwp file is, because gdb's realpath-ing
12360 of the executable's path may have discarded the needed info.
12361 [IWBN if the dwp file name was recorded in the executable, akin to
12362 .gnu_debuglink, but that doesn't exist yet.]
12363 Strip the directory from FILE_NAME and search again. */
12364 if (*debug_file_directory != '\0')
12365 {
12366 /* Don't implicitly search the current directory here.
12367 If the user wants to search "." to handle this case,
12368 it must be added to debug-file-directory. */
ed2dc618
SM
12369 return try_open_dwop_file (dwarf2_per_objfile,
12370 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12371 0 /*search_cwd*/);
12372 }
12373
12374 return NULL;
ab5088bf
DE
12375}
12376
80626a55
DE
12377/* Initialize the use of the DWP file for the current objfile.
12378 By convention the name of the DWP file is ${objfile}.dwp.
12379 The result is NULL if it can't be found. */
a766d390 12380
400174b1 12381static std::unique_ptr<struct dwp_file>
ed2dc618 12382open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12383{
12384 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12385
82bf32bc
JK
12386 /* Try to find first .dwp for the binary file before any symbolic links
12387 resolving. */
6c447423
DE
12388
12389 /* If the objfile is a debug file, find the name of the real binary
12390 file and get the name of dwp file from there. */
d721ba37 12391 std::string dwp_name;
6c447423
DE
12392 if (objfile->separate_debug_objfile_backlink != NULL)
12393 {
12394 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12395 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12396
d721ba37 12397 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12398 }
12399 else
d721ba37
PA
12400 dwp_name = objfile->original_name;
12401
12402 dwp_name += ".dwp";
80626a55 12403
ed2dc618 12404 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12405 if (dbfd == NULL
12406 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12407 {
12408 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12409 dwp_name = objfile_name (objfile);
12410 dwp_name += ".dwp";
ed2dc618 12411 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12412 }
12413
80626a55
DE
12414 if (dbfd == NULL)
12415 {
b4f54984 12416 if (dwarf_read_debug)
d721ba37 12417 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12418 return std::unique_ptr<dwp_file> ();
3019eac3 12419 }
400174b1
TT
12420
12421 const char *name = bfd_get_filename (dbfd.get ());
12422 std::unique_ptr<struct dwp_file> dwp_file
12423 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12424
0a0f4c01 12425 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12426 dwp_file->elf_sections =
12427 OBSTACK_CALLOC (&objfile->objfile_obstack,
12428 dwp_file->num_sections, asection *);
12429
400174b1
TT
12430 bfd_map_over_sections (dwp_file->dbfd.get (),
12431 dwarf2_locate_common_dwp_sections,
12432 dwp_file.get ());
80626a55 12433
400174b1
TT
12434 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12435 0);
80626a55 12436
400174b1
TT
12437 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12438 1);
80626a55 12439
73869dc2 12440 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12441 if (dwp_file->cus && dwp_file->tus
12442 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12443 {
12444 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12445 pretty bizarre. We use pulongest here because that's the established
4d65956b 12446 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12447 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12448 " TU version %s [in DWP file %s]"),
12449 pulongest (dwp_file->cus->version),
d721ba37 12450 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12451 }
08302ed2
DE
12452
12453 if (dwp_file->cus)
12454 dwp_file->version = dwp_file->cus->version;
12455 else if (dwp_file->tus)
12456 dwp_file->version = dwp_file->tus->version;
12457 else
12458 dwp_file->version = 2;
73869dc2
DE
12459
12460 if (dwp_file->version == 2)
400174b1
TT
12461 bfd_map_over_sections (dwp_file->dbfd.get (),
12462 dwarf2_locate_v2_dwp_sections,
12463 dwp_file.get ());
73869dc2 12464
298e9637
SM
12465 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12466 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12467
b4f54984 12468 if (dwarf_read_debug)
80626a55
DE
12469 {
12470 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12471 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12472 " %s CUs, %s TUs\n",
12473 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12474 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12475 }
12476
12477 return dwp_file;
3019eac3 12478}
c906108c 12479
ab5088bf
DE
12480/* Wrapper around open_and_init_dwp_file, only open it once. */
12481
12482static struct dwp_file *
ed2dc618 12483get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12484{
12485 if (! dwarf2_per_objfile->dwp_checked)
12486 {
ed2dc618
SM
12487 dwarf2_per_objfile->dwp_file
12488 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12489 dwarf2_per_objfile->dwp_checked = 1;
12490 }
400174b1 12491 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12492}
12493
80626a55
DE
12494/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12495 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12496 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12497 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12498 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12499
12500 This is called, for example, when wanting to read a variable with a
12501 complex location. Therefore we don't want to do file i/o for every call.
12502 Therefore we don't want to look for a DWO file on every call.
12503 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12504 then we check if we've already seen DWO_NAME, and only THEN do we check
12505 for a DWO file.
12506
1c658ad5 12507 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12508 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12509
3019eac3 12510static struct dwo_unit *
80626a55
DE
12511lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12512 const char *dwo_name, const char *comp_dir,
12513 ULONGEST signature, int is_debug_types)
3019eac3 12514{
ed2dc618 12515 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12516 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12517 const char *kind = is_debug_types ? "TU" : "CU";
12518 void **dwo_file_slot;
3019eac3 12519 struct dwo_file *dwo_file;
80626a55 12520 struct dwp_file *dwp_file;
cb1df416 12521
6a506a2d
DE
12522 /* First see if there's a DWP file.
12523 If we have a DWP file but didn't find the DWO inside it, don't
12524 look for the original DWO file. It makes gdb behave differently
12525 depending on whether one is debugging in the build tree. */
cf2c3c16 12526
ed2dc618 12527 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12528 if (dwp_file != NULL)
cf2c3c16 12529 {
80626a55
DE
12530 const struct dwp_hash_table *dwp_htab =
12531 is_debug_types ? dwp_file->tus : dwp_file->cus;
12532
12533 if (dwp_htab != NULL)
12534 {
12535 struct dwo_unit *dwo_cutu =
ed2dc618 12536 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12537 signature, is_debug_types);
80626a55
DE
12538
12539 if (dwo_cutu != NULL)
12540 {
b4f54984 12541 if (dwarf_read_debug)
80626a55
DE
12542 {
12543 fprintf_unfiltered (gdb_stdlog,
12544 "Virtual DWO %s %s found: @%s\n",
12545 kind, hex_string (signature),
12546 host_address_to_string (dwo_cutu));
12547 }
12548 return dwo_cutu;
12549 }
12550 }
12551 }
6a506a2d 12552 else
80626a55 12553 {
6a506a2d 12554 /* No DWP file, look for the DWO file. */
80626a55 12555
ed2dc618
SM
12556 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12557 dwo_name, comp_dir);
6a506a2d 12558 if (*dwo_file_slot == NULL)
80626a55 12559 {
6a506a2d
DE
12560 /* Read in the file and build a table of the CUs/TUs it contains. */
12561 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12562 }
6a506a2d 12563 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12564 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12565
6a506a2d 12566 if (dwo_file != NULL)
19c3d4c9 12567 {
6a506a2d
DE
12568 struct dwo_unit *dwo_cutu = NULL;
12569
12570 if (is_debug_types && dwo_file->tus)
12571 {
12572 struct dwo_unit find_dwo_cutu;
12573
12574 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12575 find_dwo_cutu.signature = signature;
9a3c8263 12576 dwo_cutu
b0b6a987
TT
12577 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12578 &find_dwo_cutu);
6a506a2d 12579 }
33c5cd75 12580 else if (!is_debug_types && dwo_file->cus)
80626a55 12581 {
33c5cd75
DB
12582 struct dwo_unit find_dwo_cutu;
12583
12584 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12585 find_dwo_cutu.signature = signature;
b0b6a987 12586 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12587 &find_dwo_cutu);
6a506a2d
DE
12588 }
12589
12590 if (dwo_cutu != NULL)
12591 {
b4f54984 12592 if (dwarf_read_debug)
6a506a2d
DE
12593 {
12594 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12595 kind, dwo_name, hex_string (signature),
12596 host_address_to_string (dwo_cutu));
12597 }
12598 return dwo_cutu;
80626a55
DE
12599 }
12600 }
2e276125 12601 }
9cdd5dbd 12602
80626a55
DE
12603 /* We didn't find it. This could mean a dwo_id mismatch, or
12604 someone deleted the DWO/DWP file, or the search path isn't set up
12605 correctly to find the file. */
12606
b4f54984 12607 if (dwarf_read_debug)
80626a55
DE
12608 {
12609 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12610 kind, dwo_name, hex_string (signature));
12611 }
3019eac3 12612
6656a72d
DE
12613 /* This is a warning and not a complaint because it can be caused by
12614 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12615 {
12616 /* Print the name of the DWP file if we looked there, helps the user
12617 better diagnose the problem. */
791afaa2 12618 std::string dwp_text;
43942612
DE
12619
12620 if (dwp_file != NULL)
791afaa2
TT
12621 dwp_text = string_printf (" [in DWP file %s]",
12622 lbasename (dwp_file->name));
43942612 12623
9d8780f0 12624 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12625 " [in module %s]"),
12626 kind, dwo_name, hex_string (signature),
791afaa2 12627 dwp_text.c_str (),
43942612 12628 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12629 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12630 }
3019eac3 12631 return NULL;
5fb290d7
DJ
12632}
12633
80626a55
DE
12634/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12635 See lookup_dwo_cutu_unit for details. */
12636
12637static struct dwo_unit *
12638lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12639 const char *dwo_name, const char *comp_dir,
12640 ULONGEST signature)
12641{
12642 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12643}
12644
12645/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12646 See lookup_dwo_cutu_unit for details. */
12647
12648static struct dwo_unit *
12649lookup_dwo_type_unit (struct signatured_type *this_tu,
12650 const char *dwo_name, const char *comp_dir)
12651{
12652 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12653}
12654
89e63ee4
DE
12655/* Traversal function for queue_and_load_all_dwo_tus. */
12656
12657static int
12658queue_and_load_dwo_tu (void **slot, void *info)
12659{
12660 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12661 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12662 ULONGEST signature = dwo_unit->signature;
12663 struct signatured_type *sig_type =
12664 lookup_dwo_signatured_type (per_cu->cu, signature);
12665
12666 if (sig_type != NULL)
12667 {
12668 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12669
12670 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12671 a real dependency of PER_CU on SIG_TYPE. That is detected later
12672 while processing PER_CU. */
12673 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12674 load_full_type_unit (sig_cu);
ae640021 12675 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12676 }
12677
12678 return 1;
12679}
12680
12681/* Queue all TUs contained in the DWO of PER_CU to be read in.
12682 The DWO may have the only definition of the type, though it may not be
12683 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12684 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12685
12686static void
12687queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12688{
12689 struct dwo_unit *dwo_unit;
12690 struct dwo_file *dwo_file;
12691
12692 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12693 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12694 gdb_assert (per_cu->cu != NULL);
12695
12696 dwo_unit = per_cu->cu->dwo_unit;
12697 gdb_assert (dwo_unit != NULL);
12698
12699 dwo_file = dwo_unit->dwo_file;
12700 if (dwo_file->tus != NULL)
b0b6a987
TT
12701 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12702 per_cu);
89e63ee4
DE
12703}
12704
3019eac3 12705/* Read in various DIEs. */
348e048f 12706
d389af10 12707/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12708 Inherit only the children of the DW_AT_abstract_origin DIE not being
12709 already referenced by DW_AT_abstract_origin from the children of the
12710 current DIE. */
d389af10
JK
12711
12712static void
12713inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12714{
12715 struct die_info *child_die;
791afaa2 12716 sect_offset *offsetp;
d389af10
JK
12717 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12718 struct die_info *origin_die;
12719 /* Iterator of the ORIGIN_DIE children. */
12720 struct die_info *origin_child_die;
d389af10 12721 struct attribute *attr;
cd02d79d
PA
12722 struct dwarf2_cu *origin_cu;
12723 struct pending **origin_previous_list_in_scope;
d389af10
JK
12724
12725 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12726 if (!attr)
12727 return;
12728
cd02d79d
PA
12729 /* Note that following die references may follow to a die in a
12730 different cu. */
12731
12732 origin_cu = cu;
12733 origin_die = follow_die_ref (die, attr, &origin_cu);
12734
12735 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12736 symbols in. */
12737 origin_previous_list_in_scope = origin_cu->list_in_scope;
12738 origin_cu->list_in_scope = cu->list_in_scope;
12739
edb3359d
DJ
12740 if (die->tag != origin_die->tag
12741 && !(die->tag == DW_TAG_inlined_subroutine
12742 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12743 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12744 sect_offset_str (die->sect_off),
12745 sect_offset_str (origin_die->sect_off));
d389af10 12746
791afaa2 12747 std::vector<sect_offset> offsets;
d389af10 12748
3ea89b92
PMR
12749 for (child_die = die->child;
12750 child_die && child_die->tag;
12751 child_die = sibling_die (child_die))
12752 {
12753 struct die_info *child_origin_die;
12754 struct dwarf2_cu *child_origin_cu;
12755
12756 /* We are trying to process concrete instance entries:
216f72a1 12757 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12758 it's not relevant to our analysis here. i.e. detecting DIEs that are
12759 present in the abstract instance but not referenced in the concrete
12760 one. */
216f72a1
JK
12761 if (child_die->tag == DW_TAG_call_site
12762 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12763 continue;
12764
c38f313d
DJ
12765 /* For each CHILD_DIE, find the corresponding child of
12766 ORIGIN_DIE. If there is more than one layer of
12767 DW_AT_abstract_origin, follow them all; there shouldn't be,
12768 but GCC versions at least through 4.4 generate this (GCC PR
12769 40573). */
3ea89b92
PMR
12770 child_origin_die = child_die;
12771 child_origin_cu = cu;
c38f313d
DJ
12772 while (1)
12773 {
cd02d79d
PA
12774 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12775 child_origin_cu);
c38f313d
DJ
12776 if (attr == NULL)
12777 break;
cd02d79d
PA
12778 child_origin_die = follow_die_ref (child_origin_die, attr,
12779 &child_origin_cu);
c38f313d
DJ
12780 }
12781
d389af10
JK
12782 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12783 counterpart may exist. */
c38f313d 12784 if (child_origin_die != child_die)
d389af10 12785 {
edb3359d
DJ
12786 if (child_die->tag != child_origin_die->tag
12787 && !(child_die->tag == DW_TAG_inlined_subroutine
12788 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12789 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12790 "different tags"),
9d8780f0
SM
12791 sect_offset_str (child_die->sect_off),
12792 sect_offset_str (child_origin_die->sect_off));
c38f313d 12793 if (child_origin_die->parent != origin_die)
b98664d3 12794 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12795 "different parents"),
9d8780f0
SM
12796 sect_offset_str (child_die->sect_off),
12797 sect_offset_str (child_origin_die->sect_off));
c38f313d 12798 else
791afaa2 12799 offsets.push_back (child_origin_die->sect_off);
d389af10 12800 }
d389af10 12801 }
791afaa2
TT
12802 std::sort (offsets.begin (), offsets.end ());
12803 sect_offset *offsets_end = offsets.data () + offsets.size ();
12804 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12805 if (offsetp[-1] == *offsetp)
b98664d3 12806 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12807 "to DIE %s as their abstract origin"),
12808 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12809
791afaa2 12810 offsetp = offsets.data ();
d389af10
JK
12811 origin_child_die = origin_die->child;
12812 while (origin_child_die && origin_child_die->tag)
12813 {
12814 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12815 while (offsetp < offsets_end
9c541725 12816 && *offsetp < origin_child_die->sect_off)
d389af10 12817 offsetp++;
b64f50a1 12818 if (offsetp >= offsets_end
9c541725 12819 || *offsetp > origin_child_die->sect_off)
d389af10 12820 {
adde2bff
DE
12821 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12822 Check whether we're already processing ORIGIN_CHILD_DIE.
12823 This can happen with mutually referenced abstract_origins.
12824 PR 16581. */
12825 if (!origin_child_die->in_process)
12826 process_die (origin_child_die, origin_cu);
d389af10
JK
12827 }
12828 origin_child_die = sibling_die (origin_child_die);
12829 }
cd02d79d 12830 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12831
12832 if (cu != origin_cu)
12833 compute_delayed_physnames (origin_cu);
d389af10
JK
12834}
12835
c906108c 12836static void
e7c27a73 12837read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12838{
518817b3 12839 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12840 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12841 struct context_stack *newobj;
c906108c
SS
12842 CORE_ADDR lowpc;
12843 CORE_ADDR highpc;
12844 struct die_info *child_die;
edb3359d 12845 struct attribute *attr, *call_line, *call_file;
15d034d0 12846 const char *name;
e142c38c 12847 CORE_ADDR baseaddr;
801e3a5b 12848 struct block *block;
edb3359d 12849 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12850 std::vector<struct symbol *> template_args;
34eaf542 12851 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12852
12853 if (inlined_func)
12854 {
12855 /* If we do not have call site information, we can't show the
12856 caller of this inlined function. That's too confusing, so
12857 only use the scope for local variables. */
12858 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12859 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12860 if (call_line == NULL || call_file == NULL)
12861 {
12862 read_lexical_block_scope (die, cu);
12863 return;
12864 }
12865 }
c906108c 12866
b3b3bada 12867 baseaddr = objfile->text_section_offset ();
e142c38c 12868
94af9270 12869 name = dwarf2_name (die, cu);
c906108c 12870
e8d05480
JB
12871 /* Ignore functions with missing or empty names. These are actually
12872 illegal according to the DWARF standard. */
12873 if (name == NULL)
12874 {
b98664d3 12875 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12876 sect_offset_str (die->sect_off));
e8d05480
JB
12877 return;
12878 }
12879
12880 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12881 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12882 <= PC_BOUNDS_INVALID)
e8d05480 12883 {
ae4d0c03
PM
12884 attr = dwarf2_attr (die, DW_AT_external, cu);
12885 if (!attr || !DW_UNSND (attr))
b98664d3 12886 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12887 "for subprogram DIE at %s"),
12888 sect_offset_str (die->sect_off));
e8d05480
JB
12889 return;
12890 }
c906108c 12891
3e29f34a
MR
12892 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12893 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12894
34eaf542
TT
12895 /* If we have any template arguments, then we must allocate a
12896 different sort of symbol. */
12897 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12898 {
12899 if (child_die->tag == DW_TAG_template_type_param
12900 || child_die->tag == DW_TAG_template_value_param)
12901 {
e623cf5d 12902 templ_func = allocate_template_symbol (objfile);
cf724bc9 12903 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12904 break;
12905 }
12906 }
12907
c24bdb02 12908 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12909 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12910 (struct symbol *) templ_func);
4c2df51b 12911
81873cc8 12912 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12913 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12914 cu->language);
12915
4cecd739
DJ
12916 /* If there is a location expression for DW_AT_frame_base, record
12917 it. */
e142c38c 12918 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12919 if (attr != nullptr)
fe978cb0 12920 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12921
63e43d3a
PMR
12922 /* If there is a location for the static link, record it. */
12923 newobj->static_link = NULL;
12924 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12925 if (attr != nullptr)
63e43d3a 12926 {
224c3ddb
SM
12927 newobj->static_link
12928 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12929 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12930 cu->per_cu->addr_type ());
63e43d3a
PMR
12931 }
12932
c24bdb02 12933 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12934
639d11d3 12935 if (die->child != NULL)
c906108c 12936 {
639d11d3 12937 child_die = die->child;
c906108c
SS
12938 while (child_die && child_die->tag)
12939 {
34eaf542
TT
12940 if (child_die->tag == DW_TAG_template_type_param
12941 || child_die->tag == DW_TAG_template_value_param)
12942 {
12943 struct symbol *arg = new_symbol (child_die, NULL, cu);
12944
f1078f66 12945 if (arg != NULL)
2f4732b0 12946 template_args.push_back (arg);
34eaf542
TT
12947 }
12948 else
12949 process_die (child_die, cu);
c906108c
SS
12950 child_die = sibling_die (child_die);
12951 }
12952 }
12953
d389af10
JK
12954 inherit_abstract_dies (die, cu);
12955
4a811a97
UW
12956 /* If we have a DW_AT_specification, we might need to import using
12957 directives from the context of the specification DIE. See the
12958 comment in determine_prefix. */
12959 if (cu->language == language_cplus
12960 && dwarf2_attr (die, DW_AT_specification, cu))
12961 {
12962 struct dwarf2_cu *spec_cu = cu;
12963 struct die_info *spec_die = die_specification (die, &spec_cu);
12964
12965 while (spec_die)
12966 {
12967 child_die = spec_die->child;
12968 while (child_die && child_die->tag)
12969 {
12970 if (child_die->tag == DW_TAG_imported_module)
12971 process_die (child_die, spec_cu);
12972 child_die = sibling_die (child_die);
12973 }
12974
12975 /* In some cases, GCC generates specification DIEs that
12976 themselves contain DW_AT_specification attributes. */
12977 spec_die = die_specification (spec_die, &spec_cu);
12978 }
12979 }
12980
c24bdb02 12981 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12982 /* Make a block for the local symbols within. */
c24bdb02 12983 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12984 cstk.static_link, lowpc, highpc);
801e3a5b 12985
df8a16a1 12986 /* For C++, set the block's scope. */
45280282
IB
12987 if ((cu->language == language_cplus
12988 || cu->language == language_fortran
c44af4eb
TT
12989 || cu->language == language_d
12990 || cu->language == language_rust)
4d4ec4e5 12991 && cu->processing_has_namespace_info)
195a3f6c
TT
12992 block_set_scope (block, determine_prefix (die, cu),
12993 &objfile->objfile_obstack);
df8a16a1 12994
801e3a5b
JB
12995 /* If we have address ranges, record them. */
12996 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12997
a60f3166 12998 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12999
34eaf542 13000 /* Attach template arguments to function. */
2f4732b0 13001 if (!template_args.empty ())
34eaf542
TT
13002 {
13003 gdb_assert (templ_func != NULL);
13004
2f4732b0 13005 templ_func->n_template_arguments = template_args.size ();
34eaf542 13006 templ_func->template_arguments
8d749320
SM
13007 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13008 templ_func->n_template_arguments);
34eaf542 13009 memcpy (templ_func->template_arguments,
2f4732b0 13010 template_args.data (),
34eaf542 13011 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13012
13013 /* Make sure that the symtab is set on the new symbols. Even
13014 though they don't appear in this symtab directly, other parts
13015 of gdb assume that symbols do, and this is reasonably
13016 true. */
8634679f 13017 for (symbol *sym : template_args)
3e1d3d8c 13018 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13019 }
13020
208d8187
JB
13021 /* In C++, we can have functions nested inside functions (e.g., when
13022 a function declares a class that has methods). This means that
13023 when we finish processing a function scope, we may need to go
13024 back to building a containing block's symbol lists. */
c24bdb02
KS
13025 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13026 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13027
921e78cf
JB
13028 /* If we've finished processing a top-level function, subsequent
13029 symbols go in the file symbol list. */
c24bdb02
KS
13030 if (cu->get_builder ()->outermost_context_p ())
13031 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13032}
13033
13034/* Process all the DIES contained within a lexical block scope. Start
13035 a new scope, process the dies, and then close the scope. */
13036
13037static void
e7c27a73 13038read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13039{
518817b3 13040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13041 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13042 CORE_ADDR lowpc, highpc;
13043 struct die_info *child_die;
e142c38c
DJ
13044 CORE_ADDR baseaddr;
13045
b3b3bada 13046 baseaddr = objfile->text_section_offset ();
c906108c
SS
13047
13048 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13049 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13050 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13051 be nasty. Might be easier to properly extend generic blocks to
af34e669 13052 describe ranges. */
e385593e
JK
13053 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13054 {
13055 case PC_BOUNDS_NOT_PRESENT:
13056 /* DW_TAG_lexical_block has no attributes, process its children as if
13057 there was no wrapping by that DW_TAG_lexical_block.
13058 GCC does no longer produces such DWARF since GCC r224161. */
13059 for (child_die = die->child;
13060 child_die != NULL && child_die->tag;
13061 child_die = sibling_die (child_die))
13062 process_die (child_die, cu);
13063 return;
13064 case PC_BOUNDS_INVALID:
13065 return;
13066 }
3e29f34a
MR
13067 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13068 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13069
c24bdb02 13070 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13071 if (die->child != NULL)
c906108c 13072 {
639d11d3 13073 child_die = die->child;
c906108c
SS
13074 while (child_die && child_die->tag)
13075 {
e7c27a73 13076 process_die (child_die, cu);
c906108c
SS
13077 child_die = sibling_die (child_die);
13078 }
13079 }
3ea89b92 13080 inherit_abstract_dies (die, cu);
c24bdb02 13081 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13082
c24bdb02
KS
13083 if (*cu->get_builder ()->get_local_symbols () != NULL
13084 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13085 {
801e3a5b 13086 struct block *block
c24bdb02 13087 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13088 cstk.start_addr, highpc);
801e3a5b
JB
13089
13090 /* Note that recording ranges after traversing children, as we
13091 do here, means that recording a parent's ranges entails
13092 walking across all its children's ranges as they appear in
13093 the address map, which is quadratic behavior.
13094
13095 It would be nicer to record the parent's ranges before
13096 traversing its children, simply overriding whatever you find
13097 there. But since we don't even decide whether to create a
13098 block until after we've traversed its children, that's hard
13099 to do. */
13100 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13101 }
c24bdb02
KS
13102 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13103 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13104}
13105
216f72a1 13106/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13107
13108static void
13109read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13110{
518817b3 13111 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13112 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13113 CORE_ADDR pc, baseaddr;
13114 struct attribute *attr;
13115 struct call_site *call_site, call_site_local;
13116 void **slot;
13117 int nparams;
13118 struct die_info *child_die;
13119
b3b3bada 13120 baseaddr = objfile->text_section_offset ();
96408a79 13121
216f72a1
JK
13122 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13123 if (attr == NULL)
13124 {
13125 /* This was a pre-DWARF-5 GNU extension alias
13126 for DW_AT_call_return_pc. */
13127 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13128 }
96408a79
SA
13129 if (!attr)
13130 {
b98664d3 13131 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13132 "DIE %s [in module %s]"),
13133 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13134 return;
13135 }
cd6c91b4 13136 pc = attr->value_as_address () + baseaddr;
3e29f34a 13137 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13138
13139 if (cu->call_site_htab == NULL)
13140 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13141 NULL, &objfile->objfile_obstack,
13142 hashtab_obstack_allocate, NULL);
13143 call_site_local.pc = pc;
13144 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13145 if (*slot != NULL)
13146 {
b98664d3 13147 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13148 "DIE %s [in module %s]"),
13149 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13150 objfile_name (objfile));
96408a79
SA
13151 return;
13152 }
13153
13154 /* Count parameters at the caller. */
13155
13156 nparams = 0;
13157 for (child_die = die->child; child_die && child_die->tag;
13158 child_die = sibling_die (child_die))
13159 {
216f72a1
JK
13160 if (child_die->tag != DW_TAG_call_site_parameter
13161 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13162 {
b98664d3 13163 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13164 "DW_TAG_call_site child DIE %s [in module %s]"),
13165 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13166 objfile_name (objfile));
96408a79
SA
13167 continue;
13168 }
13169
13170 nparams++;
13171 }
13172
224c3ddb
SM
13173 call_site
13174 = ((struct call_site *)
13175 obstack_alloc (&objfile->objfile_obstack,
13176 sizeof (*call_site)
13177 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13178 *slot = call_site;
13179 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13180 call_site->pc = pc;
13181
216f72a1
JK
13182 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13183 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13184 {
13185 struct die_info *func_die;
13186
13187 /* Skip also over DW_TAG_inlined_subroutine. */
13188 for (func_die = die->parent;
13189 func_die && func_die->tag != DW_TAG_subprogram
13190 && func_die->tag != DW_TAG_subroutine_type;
13191 func_die = func_die->parent);
13192
216f72a1
JK
13193 /* DW_AT_call_all_calls is a superset
13194 of DW_AT_call_all_tail_calls. */
96408a79 13195 if (func_die
216f72a1 13196 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13197 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13198 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13199 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13200 {
13201 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13202 not complete. But keep CALL_SITE for look ups via call_site_htab,
13203 both the initial caller containing the real return address PC and
13204 the final callee containing the current PC of a chain of tail
13205 calls do not need to have the tail call list complete. But any
13206 function candidate for a virtual tail call frame searched via
13207 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13208 determined unambiguously. */
13209 }
13210 else
13211 {
13212 struct type *func_type = NULL;
13213
13214 if (func_die)
13215 func_type = get_die_type (func_die, cu);
13216 if (func_type != NULL)
13217 {
13218 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13219
13220 /* Enlist this call site to the function. */
13221 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13222 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13223 }
13224 else
b98664d3 13225 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13226 "DIE %s [in module %s]"),
13227 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13228 }
13229 }
13230
216f72a1
JK
13231 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13232 if (attr == NULL)
13233 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13234 if (attr == NULL)
13235 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13236 if (attr == NULL)
216f72a1
JK
13237 {
13238 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13239 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13240 }
96408a79 13241 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13242 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13243 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13244 else if (attr->form_is_block ())
96408a79
SA
13245 {
13246 struct dwarf2_locexpr_baton *dlbaton;
13247
8d749320 13248 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13249 dlbaton->data = DW_BLOCK (attr)->data;
13250 dlbaton->size = DW_BLOCK (attr)->size;
13251 dlbaton->per_cu = cu->per_cu;
13252
13253 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13254 }
cd6c91b4 13255 else if (attr->form_is_ref ())
96408a79 13256 {
96408a79
SA
13257 struct dwarf2_cu *target_cu = cu;
13258 struct die_info *target_die;
13259
ac9ec31b 13260 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13261 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13262 if (die_is_declaration (target_die, target_cu))
13263 {
7d45c7c3 13264 const char *target_physname;
9112db09
JK
13265
13266 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13267 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13268 if (target_physname == NULL)
9112db09 13269 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13270 if (target_physname == NULL)
b98664d3 13271 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13272 "physname, for referencing DIE %s [in module %s]"),
13273 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13274 else
7d455152 13275 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13276 }
13277 else
13278 {
13279 CORE_ADDR lowpc;
13280
13281 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13282 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13283 <= PC_BOUNDS_INVALID)
b98664d3 13284 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13285 "low pc, for referencing DIE %s [in module %s]"),
13286 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13287 else
3e29f34a
MR
13288 {
13289 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13290 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13291 }
96408a79
SA
13292 }
13293 }
13294 else
b98664d3 13295 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13296 "block nor reference, for DIE %s [in module %s]"),
13297 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13298
13299 call_site->per_cu = cu->per_cu;
13300
13301 for (child_die = die->child;
13302 child_die && child_die->tag;
13303 child_die = sibling_die (child_die))
13304 {
96408a79 13305 struct call_site_parameter *parameter;
1788b2d3 13306 struct attribute *loc, *origin;
96408a79 13307
216f72a1
JK
13308 if (child_die->tag != DW_TAG_call_site_parameter
13309 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13310 {
13311 /* Already printed the complaint above. */
13312 continue;
13313 }
13314
13315 gdb_assert (call_site->parameter_count < nparams);
13316 parameter = &call_site->parameter[call_site->parameter_count];
13317
1788b2d3
JK
13318 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13319 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13320 register is contained in DW_AT_call_value. */
96408a79 13321
24c5c679 13322 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13323 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13324 if (origin == NULL)
13325 {
13326 /* This was a pre-DWARF-5 GNU extension alias
13327 for DW_AT_call_parameter. */
13328 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13329 }
cd6c91b4 13330 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13331 {
1788b2d3 13332 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13333
13334 sect_offset sect_off
13335 = (sect_offset) dwarf2_get_ref_die_offset (origin);
4057dfde 13336 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13337 {
13338 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13339 binding can be done only inside one CU. Such referenced DIE
13340 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13341 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13342 "DW_TAG_call_site child DIE %s [in module %s]"),
13343 sect_offset_str (child_die->sect_off),
9c541725 13344 objfile_name (objfile));
d76b7dbc
JK
13345 continue;
13346 }
9c541725
PA
13347 parameter->u.param_cu_off
13348 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13349 }
4fc6c0d5 13350 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13351 {
b98664d3 13352 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13353 "DW_TAG_call_site child DIE %s [in module %s]"),
13354 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13355 continue;
13356 }
24c5c679 13357 else
96408a79 13358 {
24c5c679
JK
13359 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13360 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13361 if (parameter->u.dwarf_reg != -1)
13362 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13363 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13364 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13365 &parameter->u.fb_offset))
13366 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13367 else
13368 {
b98664d3 13369 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13370 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13371 "DW_TAG_call_site child DIE %s "
24c5c679 13372 "[in module %s]"),
9d8780f0 13373 sect_offset_str (child_die->sect_off),
9c541725 13374 objfile_name (objfile));
24c5c679
JK
13375 continue;
13376 }
96408a79
SA
13377 }
13378
216f72a1
JK
13379 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13380 if (attr == NULL)
13381 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13382 if (attr == NULL || !attr->form_is_block ())
96408a79 13383 {
b98664d3 13384 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13385 "DW_TAG_call_site child DIE %s [in module %s]"),
13386 sect_offset_str (child_die->sect_off),
9c541725 13387 objfile_name (objfile));
96408a79
SA
13388 continue;
13389 }
13390 parameter->value = DW_BLOCK (attr)->data;
13391 parameter->value_size = DW_BLOCK (attr)->size;
13392
13393 /* Parameters are not pre-cleared by memset above. */
13394 parameter->data_value = NULL;
13395 parameter->data_value_size = 0;
13396 call_site->parameter_count++;
13397
216f72a1
JK
13398 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13399 if (attr == NULL)
13400 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13401 if (attr != nullptr)
96408a79 13402 {
4fc6c0d5 13403 if (!attr->form_is_block ())
b98664d3 13404 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13405 "DW_TAG_call_site child DIE %s [in module %s]"),
13406 sect_offset_str (child_die->sect_off),
9c541725 13407 objfile_name (objfile));
96408a79
SA
13408 else
13409 {
13410 parameter->data_value = DW_BLOCK (attr)->data;
13411 parameter->data_value_size = DW_BLOCK (attr)->size;
13412 }
13413 }
13414 }
13415}
13416
71a3c369
TT
13417/* Helper function for read_variable. If DIE represents a virtual
13418 table, then return the type of the concrete object that is
13419 associated with the virtual table. Otherwise, return NULL. */
13420
13421static struct type *
13422rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13423{
13424 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13425 if (attr == NULL)
13426 return NULL;
13427
13428 /* Find the type DIE. */
13429 struct die_info *type_die = NULL;
13430 struct dwarf2_cu *type_cu = cu;
13431
cd6c91b4 13432 if (attr->form_is_ref ())
71a3c369
TT
13433 type_die = follow_die_ref (die, attr, &type_cu);
13434 if (type_die == NULL)
13435 return NULL;
13436
13437 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13438 return NULL;
13439 return die_containing_type (type_die, type_cu);
13440}
13441
13442/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13443
13444static void
13445read_variable (struct die_info *die, struct dwarf2_cu *cu)
13446{
13447 struct rust_vtable_symbol *storage = NULL;
13448
13449 if (cu->language == language_rust)
13450 {
13451 struct type *containing_type = rust_containing_type (die, cu);
13452
13453 if (containing_type != NULL)
13454 {
518817b3 13455 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13456
468c0cbb 13457 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13458 initialize_objfile_symbol (storage);
13459 storage->concrete_type = containing_type;
cf724bc9 13460 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13461 }
13462 }
13463
e4a62c65
TV
13464 struct symbol *res = new_symbol (die, NULL, cu, storage);
13465 struct attribute *abstract_origin
13466 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13467 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13468 if (res == NULL && loc && abstract_origin)
13469 {
13470 /* We have a variable without a name, but with a location and an abstract
13471 origin. This may be a concrete instance of an abstract variable
13472 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13473 later. */
13474 struct dwarf2_cu *origin_cu = cu;
13475 struct die_info *origin_die
13476 = follow_die_ref (die, abstract_origin, &origin_cu);
13477 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13478 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13479 }
71a3c369
TT
13480}
13481
43988095
JK
13482/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13483 reading .debug_rnglists.
13484 Callback's type should be:
13485 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13486 Return true if the attributes are present and valid, otherwise,
13487 return false. */
13488
13489template <typename Callback>
13490static bool
13491dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13492 Callback &&callback)
13493{
ed2dc618 13494 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13495 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13496 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13497 bfd *obfd = objfile->obfd;
43988095
JK
13498 /* Base address selection entry. */
13499 CORE_ADDR base;
13500 int found_base;
43988095 13501 const gdb_byte *buffer;
43988095
JK
13502 CORE_ADDR baseaddr;
13503 bool overflow = false;
13504
13505 found_base = cu->base_known;
13506 base = cu->base_address;
13507
96b79293 13508 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13509 if (offset >= dwarf2_per_objfile->rnglists.size)
13510 {
b98664d3 13511 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13512 offset);
13513 return false;
13514 }
13515 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13516
b3b3bada 13517 baseaddr = objfile->text_section_offset ();
43988095
JK
13518
13519 while (1)
13520 {
7814882a
JK
13521 /* Initialize it due to a false compiler warning. */
13522 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13523 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13524 + dwarf2_per_objfile->rnglists.size);
13525 unsigned int bytes_read;
13526
13527 if (buffer == buf_end)
13528 {
13529 overflow = true;
13530 break;
13531 }
13532 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13533 switch (rlet)
13534 {
13535 case DW_RLE_end_of_list:
13536 break;
13537 case DW_RLE_base_address:
13538 if (buffer + cu->header.addr_size > buf_end)
13539 {
13540 overflow = true;
13541 break;
13542 }
c8a7a66f 13543 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13544 found_base = 1;
13545 buffer += bytes_read;
13546 break;
13547 case DW_RLE_start_length:
13548 if (buffer + cu->header.addr_size > buf_end)
13549 {
13550 overflow = true;
13551 break;
13552 }
c8a7a66f
TT
13553 range_beginning = cu->header.read_address (obfd, buffer,
13554 &bytes_read);
43988095
JK
13555 buffer += bytes_read;
13556 range_end = (range_beginning
13557 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13558 buffer += bytes_read;
13559 if (buffer > buf_end)
13560 {
13561 overflow = true;
13562 break;
13563 }
13564 break;
13565 case DW_RLE_offset_pair:
13566 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13567 buffer += bytes_read;
13568 if (buffer > buf_end)
13569 {
13570 overflow = true;
13571 break;
13572 }
13573 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13574 buffer += bytes_read;
13575 if (buffer > buf_end)
13576 {
13577 overflow = true;
13578 break;
13579 }
13580 break;
13581 case DW_RLE_start_end:
13582 if (buffer + 2 * cu->header.addr_size > buf_end)
13583 {
13584 overflow = true;
13585 break;
13586 }
c8a7a66f
TT
13587 range_beginning = cu->header.read_address (obfd, buffer,
13588 &bytes_read);
43988095 13589 buffer += bytes_read;
c8a7a66f 13590 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13591 buffer += bytes_read;
13592 break;
13593 default:
b98664d3 13594 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13595 return false;
13596 }
13597 if (rlet == DW_RLE_end_of_list || overflow)
13598 break;
13599 if (rlet == DW_RLE_base_address)
13600 continue;
13601
13602 if (!found_base)
13603 {
13604 /* We have no valid base address for the ranges
13605 data. */
b98664d3 13606 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13607 return false;
13608 }
13609
13610 if (range_beginning > range_end)
13611 {
13612 /* Inverted range entries are invalid. */
b98664d3 13613 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13614 return false;
13615 }
13616
13617 /* Empty range entries have no effect. */
13618 if (range_beginning == range_end)
13619 continue;
13620
13621 range_beginning += base;
13622 range_end += base;
13623
13624 /* A not-uncommon case of bad debug info.
13625 Don't pollute the addrmap with bad data. */
13626 if (range_beginning + baseaddr == 0
13627 && !dwarf2_per_objfile->has_section_at_zero)
13628 {
b98664d3 13629 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13630 " [in module %s]"), objfile_name (objfile));
13631 continue;
13632 }
13633
13634 callback (range_beginning, range_end);
13635 }
13636
13637 if (overflow)
13638 {
b98664d3 13639 complaint (_("Offset %d is not terminated "
43988095
JK
13640 "for DW_AT_ranges attribute"),
13641 offset);
13642 return false;
13643 }
13644
13645 return true;
13646}
13647
13648/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13649 Callback's type should be:
13650 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13651 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13652
43988095 13653template <typename Callback>
43039443 13654static int
5f46c5a5 13655dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13656 Callback &&callback)
43039443 13657{
ed2dc618 13658 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13659 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13660 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13661 struct comp_unit_head *cu_header = &cu->header;
13662 bfd *obfd = objfile->obfd;
13663 unsigned int addr_size = cu_header->addr_size;
13664 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13665 /* Base address selection entry. */
13666 CORE_ADDR base;
13667 int found_base;
13668 unsigned int dummy;
d521ce57 13669 const gdb_byte *buffer;
ff013f42 13670 CORE_ADDR baseaddr;
43039443 13671
43988095
JK
13672 if (cu_header->version >= 5)
13673 return dwarf2_rnglists_process (offset, cu, callback);
13674
d00adf39
DE
13675 found_base = cu->base_known;
13676 base = cu->base_address;
43039443 13677
96b79293 13678 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13679 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13680 {
b98664d3 13681 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13682 offset);
13683 return 0;
13684 }
dce234bc 13685 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13686
b3b3bada 13687 baseaddr = objfile->text_section_offset ();
ff013f42 13688
43039443
JK
13689 while (1)
13690 {
13691 CORE_ADDR range_beginning, range_end;
13692
c8a7a66f 13693 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13694 buffer += addr_size;
c8a7a66f 13695 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13696 buffer += addr_size;
13697 offset += 2 * addr_size;
13698
13699 /* An end of list marker is a pair of zero addresses. */
13700 if (range_beginning == 0 && range_end == 0)
13701 /* Found the end of list entry. */
13702 break;
13703
13704 /* Each base address selection entry is a pair of 2 values.
13705 The first is the largest possible address, the second is
13706 the base address. Check for a base address here. */
13707 if ((range_beginning & mask) == mask)
13708 {
28d2bfb9
AB
13709 /* If we found the largest possible address, then we already
13710 have the base address in range_end. */
13711 base = range_end;
43039443
JK
13712 found_base = 1;
13713 continue;
13714 }
13715
13716 if (!found_base)
13717 {
13718 /* We have no valid base address for the ranges
13719 data. */
b98664d3 13720 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13721 return 0;
13722 }
13723
9277c30c
UW
13724 if (range_beginning > range_end)
13725 {
13726 /* Inverted range entries are invalid. */
b98664d3 13727 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13728 return 0;
13729 }
13730
13731 /* Empty range entries have no effect. */
13732 if (range_beginning == range_end)
13733 continue;
13734
43039443
JK
13735 range_beginning += base;
13736 range_end += base;
13737
01093045
DE
13738 /* A not-uncommon case of bad debug info.
13739 Don't pollute the addrmap with bad data. */
13740 if (range_beginning + baseaddr == 0
13741 && !dwarf2_per_objfile->has_section_at_zero)
13742 {
b98664d3 13743 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13744 " [in module %s]"), objfile_name (objfile));
01093045
DE
13745 continue;
13746 }
13747
5f46c5a5
JK
13748 callback (range_beginning, range_end);
13749 }
13750
13751 return 1;
13752}
13753
13754/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13755 Return 1 if the attributes are present and valid, otherwise, return 0.
13756 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13757
13758static int
13759dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13760 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13761 dwarf2_psymtab *ranges_pst)
5f46c5a5 13762{
518817b3 13763 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13764 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13765 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13766 int low_set = 0;
13767 CORE_ADDR low = 0;
13768 CORE_ADDR high = 0;
13769 int retval;
13770
13771 retval = dwarf2_ranges_process (offset, cu,
13772 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13773 {
9277c30c 13774 if (ranges_pst != NULL)
3e29f34a
MR
13775 {
13776 CORE_ADDR lowpc;
13777 CORE_ADDR highpc;
13778
79748972
TT
13779 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13780 range_beginning + baseaddr)
13781 - baseaddr);
13782 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13783 range_end + baseaddr)
13784 - baseaddr);
d320c2b5
TT
13785 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13786 lowpc, highpc - 1, ranges_pst);
3e29f34a 13787 }
ff013f42 13788
43039443
JK
13789 /* FIXME: This is recording everything as a low-high
13790 segment of consecutive addresses. We should have a
13791 data structure for discontiguous block ranges
13792 instead. */
13793 if (! low_set)
13794 {
13795 low = range_beginning;
13796 high = range_end;
13797 low_set = 1;
13798 }
13799 else
13800 {
13801 if (range_beginning < low)
13802 low = range_beginning;
13803 if (range_end > high)
13804 high = range_end;
13805 }
5f46c5a5
JK
13806 });
13807 if (!retval)
13808 return 0;
43039443
JK
13809
13810 if (! low_set)
13811 /* If the first entry is an end-of-list marker, the range
13812 describes an empty scope, i.e. no instructions. */
13813 return 0;
13814
13815 if (low_return)
13816 *low_return = low;
13817 if (high_return)
13818 *high_return = high;
13819 return 1;
13820}
13821
3a2b436a
JK
13822/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13823 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13824 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13825
3a2b436a 13826static enum pc_bounds_kind
af34e669 13827dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13828 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13829 dwarf2_psymtab *pst)
c906108c 13830{
518817b3
SM
13831 struct dwarf2_per_objfile *dwarf2_per_objfile
13832 = cu->per_cu->dwarf2_per_objfile;
c906108c 13833 struct attribute *attr;
91da1414 13834 struct attribute *attr_high;
af34e669
DJ
13835 CORE_ADDR low = 0;
13836 CORE_ADDR high = 0;
e385593e 13837 enum pc_bounds_kind ret;
c906108c 13838
91da1414
MW
13839 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13840 if (attr_high)
af34e669 13841 {
e142c38c 13842 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13843 if (attr != nullptr)
91da1414 13844 {
cd6c91b4
TT
13845 low = attr->value_as_address ();
13846 high = attr_high->value_as_address ();
13847 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13848 high += low;
91da1414 13849 }
af34e669
DJ
13850 else
13851 /* Found high w/o low attribute. */
e385593e 13852 return PC_BOUNDS_INVALID;
af34e669
DJ
13853
13854 /* Found consecutive range of addresses. */
3a2b436a 13855 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13856 }
c906108c 13857 else
af34e669 13858 {
e142c38c 13859 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13860 if (attr != NULL)
13861 {
18a8505e 13862 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13863 We take advantage of the fact that DW_AT_ranges does not appear
13864 in DW_TAG_compile_unit of DWO files. */
13865 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13866 unsigned int ranges_offset = (DW_UNSND (attr)
13867 + (need_ranges_base
13868 ? cu->ranges_base
13869 : 0));
2e3cf129 13870
af34e669 13871 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13872 .debug_ranges section. */
2e3cf129 13873 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13874 return PC_BOUNDS_INVALID;
43039443 13875 /* Found discontinuous range of addresses. */
3a2b436a 13876 ret = PC_BOUNDS_RANGES;
af34e669 13877 }
e385593e
JK
13878 else
13879 return PC_BOUNDS_NOT_PRESENT;
af34e669 13880 }
c906108c 13881
48fbe735 13882 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13883 if (high <= low)
e385593e 13884 return PC_BOUNDS_INVALID;
c906108c
SS
13885
13886 /* When using the GNU linker, .gnu.linkonce. sections are used to
13887 eliminate duplicate copies of functions and vtables and such.
13888 The linker will arbitrarily choose one and discard the others.
13889 The AT_*_pc values for such functions refer to local labels in
13890 these sections. If the section from that file was discarded, the
13891 labels are not in the output, so the relocs get a value of 0.
13892 If this is a discarded function, mark the pc bounds as invalid,
13893 so that GDB will ignore it. */
72dca2f5 13894 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13895 return PC_BOUNDS_INVALID;
c906108c
SS
13896
13897 *lowpc = low;
96408a79
SA
13898 if (highpc)
13899 *highpc = high;
af34e669 13900 return ret;
c906108c
SS
13901}
13902
b084d499
JB
13903/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13904 its low and high PC addresses. Do nothing if these addresses could not
13905 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13906 and HIGHPC to the high address if greater than HIGHPC. */
13907
13908static void
13909dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13910 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13911 struct dwarf2_cu *cu)
13912{
13913 CORE_ADDR low, high;
13914 struct die_info *child = die->child;
13915
e385593e 13916 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13917 {
325fac50
PA
13918 *lowpc = std::min (*lowpc, low);
13919 *highpc = std::max (*highpc, high);
b084d499
JB
13920 }
13921
13922 /* If the language does not allow nested subprograms (either inside
13923 subprograms or lexical blocks), we're done. */
13924 if (cu->language != language_ada)
13925 return;
6e70227d 13926
b084d499
JB
13927 /* Check all the children of the given DIE. If it contains nested
13928 subprograms, then check their pc bounds. Likewise, we need to
13929 check lexical blocks as well, as they may also contain subprogram
13930 definitions. */
13931 while (child && child->tag)
13932 {
13933 if (child->tag == DW_TAG_subprogram
13934 || child->tag == DW_TAG_lexical_block)
13935 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13936 child = sibling_die (child);
13937 }
13938}
13939
fae299cd
DC
13940/* Get the low and high pc's represented by the scope DIE, and store
13941 them in *LOWPC and *HIGHPC. If the correct values can't be
13942 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13943
13944static void
13945get_scope_pc_bounds (struct die_info *die,
13946 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13947 struct dwarf2_cu *cu)
13948{
13949 CORE_ADDR best_low = (CORE_ADDR) -1;
13950 CORE_ADDR best_high = (CORE_ADDR) 0;
13951 CORE_ADDR current_low, current_high;
13952
3a2b436a 13953 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13954 >= PC_BOUNDS_RANGES)
fae299cd
DC
13955 {
13956 best_low = current_low;
13957 best_high = current_high;
13958 }
13959 else
13960 {
13961 struct die_info *child = die->child;
13962
13963 while (child && child->tag)
13964 {
13965 switch (child->tag) {
13966 case DW_TAG_subprogram:
b084d499 13967 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13968 break;
13969 case DW_TAG_namespace:
f55ee35c 13970 case DW_TAG_module:
fae299cd
DC
13971 /* FIXME: carlton/2004-01-16: Should we do this for
13972 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13973 that current GCC's always emit the DIEs corresponding
13974 to definitions of methods of classes as children of a
13975 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13976 the DIEs giving the declarations, which could be
13977 anywhere). But I don't see any reason why the
13978 standards says that they have to be there. */
13979 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13980
13981 if (current_low != ((CORE_ADDR) -1))
13982 {
325fac50
PA
13983 best_low = std::min (best_low, current_low);
13984 best_high = std::max (best_high, current_high);
fae299cd
DC
13985 }
13986 break;
13987 default:
0963b4bd 13988 /* Ignore. */
fae299cd
DC
13989 break;
13990 }
13991
13992 child = sibling_die (child);
13993 }
13994 }
13995
13996 *lowpc = best_low;
13997 *highpc = best_high;
13998}
13999
801e3a5b
JB
14000/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14001 in DIE. */
380bca97 14002
801e3a5b
JB
14003static void
14004dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14005 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14006{
518817b3 14007 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14008 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14009 struct attribute *attr;
91da1414 14010 struct attribute *attr_high;
801e3a5b 14011
91da1414
MW
14012 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14013 if (attr_high)
801e3a5b 14014 {
801e3a5b 14015 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14016 if (attr != nullptr)
801e3a5b 14017 {
cd6c91b4
TT
14018 CORE_ADDR low = attr->value_as_address ();
14019 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14020
cd6c91b4 14021 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14022 high += low;
9a619af0 14023
3e29f34a
MR
14024 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14025 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14026 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14027 }
14028 }
14029
14030 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14031 if (attr != nullptr)
801e3a5b 14032 {
18a8505e 14033 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14034 We take advantage of the fact that DW_AT_ranges does not appear
14035 in DW_TAG_compile_unit of DWO files. */
14036 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14037
14038 /* The value of the DW_AT_ranges attribute is the offset of the
14039 address range list in the .debug_ranges section. */
ab435259
DE
14040 unsigned long offset = (DW_UNSND (attr)
14041 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14042
2d5f09ec 14043 std::vector<blockrange> blockvec;
5f46c5a5
JK
14044 dwarf2_ranges_process (offset, cu,
14045 [&] (CORE_ADDR start, CORE_ADDR end)
14046 {
58fdfd2c
JK
14047 start += baseaddr;
14048 end += baseaddr;
5f46c5a5
JK
14049 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14050 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14051 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14052 blockvec.emplace_back (start, end);
5f46c5a5 14053 });
2d5f09ec
KB
14054
14055 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14056 }
14057}
14058
685b1105
JK
14059/* Check whether the producer field indicates either of GCC < 4.6, or the
14060 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14061
685b1105
JK
14062static void
14063check_producer (struct dwarf2_cu *cu)
60d5a603 14064{
38360086 14065 int major, minor;
60d5a603
JK
14066
14067 if (cu->producer == NULL)
14068 {
14069 /* For unknown compilers expect their behavior is DWARF version
14070 compliant.
14071
14072 GCC started to support .debug_types sections by -gdwarf-4 since
14073 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14074 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14075 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14076 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14077 }
b1ffba5a 14078 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14079 {
38360086
MW
14080 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14081 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14082 }
5230b05a 14083 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14084 {
14085 cu->producer_is_icc = true;
14086 cu->producer_is_icc_lt_14 = major < 14;
14087 }
c258c396
JD
14088 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14089 cu->producer_is_codewarrior = true;
685b1105
JK
14090 else
14091 {
14092 /* For other non-GCC compilers, expect their behavior is DWARF version
14093 compliant. */
60d5a603
JK
14094 }
14095
9068261f 14096 cu->checked_producer = true;
685b1105 14097}
ba919b58 14098
685b1105
JK
14099/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14100 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14101 during 4.6.0 experimental. */
14102
9068261f 14103static bool
685b1105
JK
14104producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14105{
14106 if (!cu->checked_producer)
14107 check_producer (cu);
14108
14109 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14110}
14111
c258c396
JD
14112
14113/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14114 with incorrect is_stmt attributes. */
14115
14116static bool
14117producer_is_codewarrior (struct dwarf2_cu *cu)
14118{
14119 if (!cu->checked_producer)
14120 check_producer (cu);
14121
14122 return cu->producer_is_codewarrior;
14123}
14124
405feb71 14125/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14126 DW_AT_accessibility. */
14127
14128static enum dwarf_access_attribute
14129dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14130{
14131 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14132 {
14133 /* The default DWARF 2 accessibility for members is public, the default
14134 accessibility for inheritance is private. */
14135
14136 if (die->tag != DW_TAG_inheritance)
14137 return DW_ACCESS_public;
14138 else
14139 return DW_ACCESS_private;
14140 }
14141 else
14142 {
14143 /* DWARF 3+ defines the default accessibility a different way. The same
14144 rules apply now for DW_TAG_inheritance as for the members and it only
14145 depends on the container kind. */
14146
14147 if (die->parent->tag == DW_TAG_class_type)
14148 return DW_ACCESS_private;
14149 else
14150 return DW_ACCESS_public;
14151 }
14152}
14153
74ac6d43
TT
14154/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14155 offset. If the attribute was not found return 0, otherwise return
14156 1. If it was found but could not properly be handled, set *OFFSET
14157 to 0. */
14158
14159static int
14160handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14161 LONGEST *offset)
14162{
14163 struct attribute *attr;
14164
14165 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14166 if (attr != NULL)
14167 {
14168 *offset = 0;
14169
14170 /* Note that we do not check for a section offset first here.
14171 This is because DW_AT_data_member_location is new in DWARF 4,
14172 so if we see it, we can assume that a constant form is really
14173 a constant and not a section offset. */
cd6c91b4 14174 if (attr->form_is_constant ())
74ac6d43 14175 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14176 else if (attr->form_is_section_offset ())
74ac6d43 14177 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14178 else if (attr->form_is_block ())
74ac6d43
TT
14179 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14180 else
14181 dwarf2_complex_location_expr_complaint ();
14182
14183 return 1;
14184 }
14185
14186 return 0;
14187}
14188
c906108c
SS
14189/* Add an aggregate field to the field list. */
14190
14191static void
107d2387 14192dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14193 struct dwarf2_cu *cu)
6e70227d 14194{
518817b3 14195 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14196 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14197 struct nextfield *new_field;
14198 struct attribute *attr;
14199 struct field *fp;
15d034d0 14200 const char *fieldname = "";
c906108c 14201
7d0ccb61
DJ
14202 if (die->tag == DW_TAG_inheritance)
14203 {
be2daae6
TT
14204 fip->baseclasses.emplace_back ();
14205 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14206 }
14207 else
14208 {
be2daae6
TT
14209 fip->fields.emplace_back ();
14210 new_field = &fip->fields.back ();
7d0ccb61 14211 }
be2daae6 14212
e142c38c 14213 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14214 if (attr != nullptr)
c906108c 14215 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14216 else
14217 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14218 if (new_field->accessibility != DW_ACCESS_public)
14219 fip->non_public_fields = 1;
60d5a603 14220
e142c38c 14221 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14222 if (attr != nullptr)
c906108c 14223 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14224 else
14225 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14226
14227 fp = &new_field->field;
a9a9bd0f 14228
e142c38c 14229 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14230 {
74ac6d43
TT
14231 LONGEST offset;
14232
a9a9bd0f 14233 /* Data member other than a C++ static data member. */
6e70227d 14234
c906108c 14235 /* Get type of field. */
e7c27a73 14236 fp->type = die_type (die, cu);
c906108c 14237
d6a843b5 14238 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14239
c906108c 14240 /* Get bit size of field (zero if none). */
e142c38c 14241 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14242 if (attr != nullptr)
c906108c
SS
14243 {
14244 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14245 }
14246 else
14247 {
14248 FIELD_BITSIZE (*fp) = 0;
14249 }
14250
14251 /* Get bit offset of field. */
74ac6d43
TT
14252 if (handle_data_member_location (die, cu, &offset))
14253 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14254 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14255 if (attr != nullptr)
c906108c 14256 {
d5a22e77 14257 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14258 {
14259 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14260 additional bit offset from the MSB of the containing
14261 anonymous object to the MSB of the field. We don't
14262 have to do anything special since we don't need to
14263 know the size of the anonymous object. */
f41f5e61 14264 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14265 }
14266 else
14267 {
14268 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14269 MSB of the anonymous object, subtract off the number of
14270 bits from the MSB of the field to the MSB of the
14271 object, and then subtract off the number of bits of
14272 the field itself. The result is the bit offset of
14273 the LSB of the field. */
c906108c
SS
14274 int anonymous_size;
14275 int bit_offset = DW_UNSND (attr);
14276
e142c38c 14277 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14278 if (attr != nullptr)
c906108c
SS
14279 {
14280 /* The size of the anonymous object containing
14281 the bit field is explicit, so use the
14282 indicated size (in bytes). */
14283 anonymous_size = DW_UNSND (attr);
14284 }
14285 else
14286 {
14287 /* The size of the anonymous object containing
14288 the bit field must be inferred from the type
14289 attribute of the data member containing the
14290 bit field. */
14291 anonymous_size = TYPE_LENGTH (fp->type);
14292 }
f41f5e61
PA
14293 SET_FIELD_BITPOS (*fp,
14294 (FIELD_BITPOS (*fp)
14295 + anonymous_size * bits_per_byte
14296 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14297 }
14298 }
da5b30da
AA
14299 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14300 if (attr != NULL)
14301 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14302 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14303
14304 /* Get name of field. */
39cbfefa
DJ
14305 fieldname = dwarf2_name (die, cu);
14306 if (fieldname == NULL)
14307 fieldname = "";
d8151005
DJ
14308
14309 /* The name is already allocated along with this objfile, so we don't
14310 need to duplicate it for the type. */
14311 fp->name = fieldname;
c906108c
SS
14312
14313 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14314 pointer or virtual base class pointer) to private. */
e142c38c 14315 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14316 {
d48cc9dd 14317 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14318 new_field->accessibility = DW_ACCESS_private;
14319 fip->non_public_fields = 1;
14320 }
14321 }
a9a9bd0f 14322 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14323 {
a9a9bd0f
DC
14324 /* C++ static member. */
14325
14326 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14327 is a declaration, but all versions of G++ as of this writing
14328 (so through at least 3.2.1) incorrectly generate
14329 DW_TAG_variable tags. */
6e70227d 14330
ff355380 14331 const char *physname;
c906108c 14332
a9a9bd0f 14333 /* Get name of field. */
39cbfefa
DJ
14334 fieldname = dwarf2_name (die, cu);
14335 if (fieldname == NULL)
c906108c
SS
14336 return;
14337
254e6b9e 14338 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14339 if (attr
14340 /* Only create a symbol if this is an external value.
14341 new_symbol checks this and puts the value in the global symbol
14342 table, which we want. If it is not external, new_symbol
14343 will try to put the value in cu->list_in_scope which is wrong. */
14344 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14345 {
14346 /* A static const member, not much different than an enum as far as
14347 we're concerned, except that we can support more types. */
14348 new_symbol (die, NULL, cu);
14349 }
14350
2df3850c 14351 /* Get physical name. */
ff355380 14352 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14353
d8151005
DJ
14354 /* The name is already allocated along with this objfile, so we don't
14355 need to duplicate it for the type. */
14356 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14357 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14358 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14359 }
14360 else if (die->tag == DW_TAG_inheritance)
14361 {
74ac6d43 14362 LONGEST offset;
d4b96c9a 14363
74ac6d43
TT
14364 /* C++ base class field. */
14365 if (handle_data_member_location (die, cu, &offset))
14366 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14367 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14368 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14369 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14370 }
2ddeaf8a
TT
14371 else if (die->tag == DW_TAG_variant_part)
14372 {
14373 /* process_structure_scope will treat this DIE as a union. */
14374 process_structure_scope (die, cu);
14375
14376 /* The variant part is relative to the start of the enclosing
14377 structure. */
14378 SET_FIELD_BITPOS (*fp, 0);
14379 fp->type = get_die_type (die, cu);
14380 fp->artificial = 1;
14381 fp->name = "<<variant>>";
c8c81635
TT
14382
14383 /* Normally a DW_TAG_variant_part won't have a size, but our
14384 representation requires one, so set it to the maximum of the
489dbda6
TT
14385 child sizes, being sure to account for the offset at which
14386 each child is seen. */
c8c81635
TT
14387 if (TYPE_LENGTH (fp->type) == 0)
14388 {
14389 unsigned max = 0;
14390 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14391 {
14392 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14393 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14394 if (len > max)
14395 max = len;
14396 }
c8c81635
TT
14397 TYPE_LENGTH (fp->type) = max;
14398 }
2ddeaf8a
TT
14399 }
14400 else
14401 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14402}
14403
883fd55a
KS
14404/* Can the type given by DIE define another type? */
14405
14406static bool
14407type_can_define_types (const struct die_info *die)
14408{
14409 switch (die->tag)
14410 {
14411 case DW_TAG_typedef:
14412 case DW_TAG_class_type:
14413 case DW_TAG_structure_type:
14414 case DW_TAG_union_type:
14415 case DW_TAG_enumeration_type:
14416 return true;
14417
14418 default:
14419 return false;
14420 }
14421}
14422
14423/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14424
14425static void
883fd55a
KS
14426dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14427 struct dwarf2_cu *cu)
6e70227d 14428{
be2daae6
TT
14429 struct decl_field fp;
14430 memset (&fp, 0, sizeof (fp));
98751a41 14431
883fd55a 14432 gdb_assert (type_can_define_types (die));
98751a41 14433
883fd55a 14434 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14435 fp.name = dwarf2_name (die, cu);
14436 fp.type = read_type_die (die, cu);
98751a41 14437
c191a687
KS
14438 /* Save accessibility. */
14439 enum dwarf_access_attribute accessibility;
14440 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14441 if (attr != NULL)
14442 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14443 else
14444 accessibility = dwarf2_default_access_attribute (die, cu);
14445 switch (accessibility)
14446 {
14447 case DW_ACCESS_public:
14448 /* The assumed value if neither private nor protected. */
14449 break;
14450 case DW_ACCESS_private:
be2daae6 14451 fp.is_private = 1;
c191a687
KS
14452 break;
14453 case DW_ACCESS_protected:
be2daae6 14454 fp.is_protected = 1;
c191a687
KS
14455 break;
14456 default:
b98664d3 14457 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14458 }
14459
883fd55a 14460 if (die->tag == DW_TAG_typedef)
be2daae6 14461 fip->typedef_field_list.push_back (fp);
883fd55a 14462 else
be2daae6 14463 fip->nested_types_list.push_back (fp);
98751a41
JK
14464}
14465
c906108c
SS
14466/* Create the vector of fields, and attach it to the type. */
14467
14468static void
fba45db2 14469dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14470 struct dwarf2_cu *cu)
c906108c 14471{
317f7127 14472 int nfields = fip->nfields ();
c906108c
SS
14473
14474 /* Record the field count, allocate space for the array of fields,
14475 and create blank accessibility bitfields if necessary. */
14476 TYPE_NFIELDS (type) = nfields;
14477 TYPE_FIELDS (type) = (struct field *)
be2daae6 14478 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14479
b4ba55a1 14480 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14481 {
14482 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14483
14484 TYPE_FIELD_PRIVATE_BITS (type) =
14485 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14486 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14487
14488 TYPE_FIELD_PROTECTED_BITS (type) =
14489 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14490 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14491
774b6a14
TT
14492 TYPE_FIELD_IGNORE_BITS (type) =
14493 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14494 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14495 }
14496
14497 /* If the type has baseclasses, allocate and clear a bit vector for
14498 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14499 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14500 {
be2daae6 14501 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14502 unsigned char *pointer;
c906108c
SS
14503
14504 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14505 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14506 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14507 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14508 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14509 }
14510
2ddeaf8a
TT
14511 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14512 {
14513 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14514
be2daae6 14515 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14516 {
be2daae6
TT
14517 struct nextfield &field = fip->fields[index];
14518
14519 if (field.variant.is_discriminant)
2ddeaf8a 14520 di->discriminant_index = index;
be2daae6 14521 else if (field.variant.default_branch)
2ddeaf8a
TT
14522 di->default_index = index;
14523 else
be2daae6 14524 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14525 }
14526 }
14527
be2daae6
TT
14528 /* Copy the saved-up fields into the field vector. */
14529 for (int i = 0; i < nfields; ++i)
c906108c 14530 {
be2daae6
TT
14531 struct nextfield &field
14532 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14533 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14534
be2daae6
TT
14535 TYPE_FIELD (type, i) = field.field;
14536 switch (field.accessibility)
c906108c 14537 {
c5aa993b 14538 case DW_ACCESS_private:
b4ba55a1 14539 if (cu->language != language_ada)
be2daae6 14540 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14541 break;
c906108c 14542
c5aa993b 14543 case DW_ACCESS_protected:
b4ba55a1 14544 if (cu->language != language_ada)
be2daae6 14545 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14546 break;
c906108c 14547
c5aa993b
JM
14548 case DW_ACCESS_public:
14549 break;
c906108c 14550
c5aa993b
JM
14551 default:
14552 /* Unknown accessibility. Complain and treat it as public. */
14553 {
b98664d3 14554 complaint (_("unsupported accessibility %d"),
be2daae6 14555 field.accessibility);
c5aa993b
JM
14556 }
14557 break;
c906108c 14558 }
be2daae6 14559 if (i < fip->baseclasses.size ())
c906108c 14560 {
be2daae6 14561 switch (field.virtuality)
c906108c 14562 {
c5aa993b
JM
14563 case DW_VIRTUALITY_virtual:
14564 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14565 if (cu->language == language_ada)
a73c6dcd 14566 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14567 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14568 break;
c906108c
SS
14569 }
14570 }
c906108c
SS
14571 }
14572}
14573
7d27a96d
TT
14574/* Return true if this member function is a constructor, false
14575 otherwise. */
14576
14577static int
14578dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14579{
14580 const char *fieldname;
fe978cb0 14581 const char *type_name;
7d27a96d
TT
14582 int len;
14583
14584 if (die->parent == NULL)
14585 return 0;
14586
14587 if (die->parent->tag != DW_TAG_structure_type
14588 && die->parent->tag != DW_TAG_union_type
14589 && die->parent->tag != DW_TAG_class_type)
14590 return 0;
14591
14592 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14593 type_name = dwarf2_name (die->parent, cu);
14594 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14595 return 0;
14596
14597 len = strlen (fieldname);
fe978cb0
PA
14598 return (strncmp (fieldname, type_name, len) == 0
14599 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14600}
14601
e35000a7
TBA
14602/* Check if the given VALUE is a recognized enum
14603 dwarf_defaulted_attribute constant according to DWARF5 spec,
14604 Table 7.24. */
14605
14606static bool
14607is_valid_DW_AT_defaulted (ULONGEST value)
14608{
14609 switch (value)
14610 {
14611 case DW_DEFAULTED_no:
14612 case DW_DEFAULTED_in_class:
14613 case DW_DEFAULTED_out_of_class:
14614 return true;
14615 }
14616
3142e908 14617 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14618 return false;
14619}
14620
c906108c
SS
14621/* Add a member function to the proper fieldlist. */
14622
14623static void
107d2387 14624dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14625 struct type *type, struct dwarf2_cu *cu)
c906108c 14626{
518817b3 14627 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14628 struct attribute *attr;
c906108c 14629 int i;
be2daae6 14630 struct fnfieldlist *flp = nullptr;
c906108c 14631 struct fn_field *fnp;
15d034d0 14632 const char *fieldname;
f792889a 14633 struct type *this_type;
60d5a603 14634 enum dwarf_access_attribute accessibility;
c906108c 14635
b4ba55a1 14636 if (cu->language == language_ada)
a73c6dcd 14637 error (_("unexpected member function in Ada type"));
b4ba55a1 14638
2df3850c 14639 /* Get name of member function. */
39cbfefa
DJ
14640 fieldname = dwarf2_name (die, cu);
14641 if (fieldname == NULL)
2df3850c 14642 return;
c906108c 14643
c906108c 14644 /* Look up member function name in fieldlist. */
be2daae6 14645 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14646 {
27bfe10e 14647 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14648 {
14649 flp = &fip->fnfieldlists[i];
14650 break;
14651 }
c906108c
SS
14652 }
14653
be2daae6
TT
14654 /* Create a new fnfieldlist if necessary. */
14655 if (flp == nullptr)
c906108c 14656 {
be2daae6
TT
14657 fip->fnfieldlists.emplace_back ();
14658 flp = &fip->fnfieldlists.back ();
c906108c 14659 flp->name = fieldname;
be2daae6 14660 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14661 }
14662
be2daae6
TT
14663 /* Create a new member function field and add it to the vector of
14664 fnfieldlists. */
14665 flp->fnfields.emplace_back ();
14666 fnp = &flp->fnfields.back ();
3da10d80
KS
14667
14668 /* Delay processing of the physname until later. */
9c37b5ae 14669 if (cu->language == language_cplus)
be2daae6
TT
14670 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14671 die, cu);
3da10d80
KS
14672 else
14673 {
1d06ead6 14674 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14675 fnp->physname = physname ? physname : "";
14676 }
14677
c906108c 14678 fnp->type = alloc_type (objfile);
f792889a
DJ
14679 this_type = read_type_die (die, cu);
14680 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14681 {
f792889a 14682 int nparams = TYPE_NFIELDS (this_type);
c906108c 14683
f792889a 14684 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14685 of the method itself (TYPE_CODE_METHOD). */
14686 smash_to_method_type (fnp->type, type,
f792889a
DJ
14687 TYPE_TARGET_TYPE (this_type),
14688 TYPE_FIELDS (this_type),
14689 TYPE_NFIELDS (this_type),
14690 TYPE_VARARGS (this_type));
c906108c
SS
14691
14692 /* Handle static member functions.
c5aa993b 14693 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14694 member functions. G++ helps GDB by marking the first
14695 parameter for non-static member functions (which is the this
14696 pointer) as artificial. We obtain this information from
14697 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14698 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14699 fnp->voffset = VOFFSET_STATIC;
14700 }
14701 else
b98664d3 14702 complaint (_("member function type missing for '%s'"),
3da10d80 14703 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14704
14705 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14706 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14707 fnp->fcontext = die_containing_type (die, cu);
c906108c 14708
3e43a32a
MS
14709 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14710 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14711
14712 /* Get accessibility. */
e142c38c 14713 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14714 if (attr != nullptr)
aead7601 14715 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14716 else
14717 accessibility = dwarf2_default_access_attribute (die, cu);
14718 switch (accessibility)
c906108c 14719 {
60d5a603
JK
14720 case DW_ACCESS_private:
14721 fnp->is_private = 1;
14722 break;
14723 case DW_ACCESS_protected:
14724 fnp->is_protected = 1;
14725 break;
c906108c
SS
14726 }
14727
b02dede2 14728 /* Check for artificial methods. */
e142c38c 14729 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14730 if (attr && DW_UNSND (attr) != 0)
14731 fnp->is_artificial = 1;
14732
e35000a7
TBA
14733 /* Check for defaulted methods. */
14734 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14735 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14736 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14737
14738 /* Check for deleted methods. */
14739 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14740 if (attr != nullptr && DW_UNSND (attr) != 0)
14741 fnp->is_deleted = 1;
14742
7d27a96d
TT
14743 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14744
0d564a31 14745 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14746 function. For older versions of GCC, this is an offset in the
14747 appropriate virtual table, as specified by DW_AT_containing_type.
14748 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14749 to the object address. */
14750
e142c38c 14751 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14752 if (attr != nullptr)
8e19ed76 14753 {
4fc6c0d5 14754 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14755 {
aec5aa8b
TT
14756 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14757 {
14758 /* Old-style GCC. */
14759 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14760 }
14761 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14762 || (DW_BLOCK (attr)->size > 1
14763 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14764 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14765 {
aec5aa8b
TT
14766 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14767 if ((fnp->voffset % cu->header.addr_size) != 0)
14768 dwarf2_complex_location_expr_complaint ();
14769 else
14770 fnp->voffset /= cu->header.addr_size;
14771 fnp->voffset += 2;
14772 }
14773 else
14774 dwarf2_complex_location_expr_complaint ();
14775
14776 if (!fnp->fcontext)
7e993ebf
KS
14777 {
14778 /* If there is no `this' field and no DW_AT_containing_type,
14779 we cannot actually find a base class context for the
14780 vtable! */
14781 if (TYPE_NFIELDS (this_type) == 0
14782 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14783 {
b98664d3 14784 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14785 "function \"%s\" (offset %s)"),
14786 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14787 }
14788 else
14789 {
14790 fnp->fcontext
14791 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14792 }
14793 }
aec5aa8b 14794 }
cd6c91b4 14795 else if (attr->form_is_section_offset ())
8e19ed76 14796 {
4d3c2250 14797 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14798 }
14799 else
14800 {
4d3c2250
KB
14801 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14802 fieldname);
8e19ed76 14803 }
0d564a31 14804 }
d48cc9dd
DJ
14805 else
14806 {
14807 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14808 if (attr && DW_UNSND (attr))
14809 {
14810 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14811 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14812 "but the vtable offset is not specified"),
9d8780f0 14813 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14814 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14815 TYPE_CPLUS_DYNAMIC (type) = 1;
14816 }
14817 }
c906108c
SS
14818}
14819
14820/* Create the vector of member function fields, and attach it to the type. */
14821
14822static void
fba45db2 14823dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14824 struct dwarf2_cu *cu)
c906108c 14825{
b4ba55a1 14826 if (cu->language == language_ada)
a73c6dcd 14827 error (_("unexpected member functions in Ada type"));
b4ba55a1 14828
c906108c
SS
14829 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14830 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14831 TYPE_ALLOC (type,
14832 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14833
be2daae6 14834 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14835 {
be2daae6 14836 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14837 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14838
be2daae6
TT
14839 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14840 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14841 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14842 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14843
14844 for (int k = 0; k < nf.fnfields.size (); ++k)
14845 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14846 }
14847
be2daae6 14848 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14849}
14850
1168df01
JB
14851/* Returns non-zero if NAME is the name of a vtable member in CU's
14852 language, zero otherwise. */
14853static int
14854is_vtable_name (const char *name, struct dwarf2_cu *cu)
14855{
14856 static const char vptr[] = "_vptr";
14857
9c37b5ae
TT
14858 /* Look for the C++ form of the vtable. */
14859 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14860 return 1;
14861
14862 return 0;
14863}
14864
c0dd20ea 14865/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14866 functions, with the ABI-specified layout. If TYPE describes
14867 such a structure, smash it into a member function type.
61049d3b
DJ
14868
14869 GCC shouldn't do this; it should just output pointer to member DIEs.
14870 This is GCC PR debug/28767. */
c0dd20ea 14871
0b92b5bb
TT
14872static void
14873quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14874{
09e2d7c7 14875 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14876
14877 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14878 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14879 return;
c0dd20ea
DJ
14880
14881 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14882 if (TYPE_FIELD_NAME (type, 0) == NULL
14883 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14884 || TYPE_FIELD_NAME (type, 1) == NULL
14885 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14886 return;
c0dd20ea
DJ
14887
14888 /* Find the type of the method. */
0b92b5bb 14889 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14890 if (pfn_type == NULL
14891 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14892 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14893 return;
c0dd20ea
DJ
14894
14895 /* Look for the "this" argument. */
14896 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14897 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14898 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14899 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14900 return;
c0dd20ea 14901
09e2d7c7 14902 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14903 new_type = alloc_type (objfile);
09e2d7c7 14904 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14905 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14906 TYPE_VARARGS (pfn_type));
0b92b5bb 14907 smash_to_methodptr_type (type, new_type);
c0dd20ea 14908}
1168df01 14909
2b4424c3
TT
14910/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14911 appropriate error checking and issuing complaints if there is a
14912 problem. */
14913
14914static ULONGEST
14915get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14916{
14917 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14918
14919 if (attr == nullptr)
14920 return 0;
14921
cd6c91b4 14922 if (!attr->form_is_constant ())
2b4424c3 14923 {
b98664d3 14924 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14925 " - DIE at %s [in module %s]"),
14926 sect_offset_str (die->sect_off),
14927 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14928 return 0;
14929 }
14930
14931 ULONGEST align;
14932 if (attr->form == DW_FORM_sdata)
14933 {
14934 LONGEST val = DW_SND (attr);
14935 if (val < 0)
14936 {
b98664d3 14937 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14938 " - DIE at %s [in module %s]"),
14939 sect_offset_str (die->sect_off),
14940 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14941 return 0;
14942 }
14943 align = val;
14944 }
14945 else
14946 align = DW_UNSND (attr);
14947
14948 if (align == 0)
14949 {
b98664d3 14950 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14951 " - DIE at %s [in module %s]"),
14952 sect_offset_str (die->sect_off),
14953 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14954 return 0;
14955 }
14956 if ((align & (align - 1)) != 0)
14957 {
b98664d3 14958 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14959 " - DIE at %s [in module %s]"),
14960 sect_offset_str (die->sect_off),
14961 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14962 return 0;
14963 }
14964
14965 return align;
14966}
14967
14968/* If the DIE has a DW_AT_alignment attribute, use its value to set
14969 the alignment for TYPE. */
14970
14971static void
14972maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14973 struct type *type)
14974{
14975 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14976 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14977 " - DIE at %s [in module %s]"),
14978 sect_offset_str (die->sect_off),
14979 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14980}
685b1105 14981
e35000a7
TBA
14982/* Check if the given VALUE is a valid enum dwarf_calling_convention
14983 constant for a type, according to DWARF5 spec, Table 5.5. */
14984
14985static bool
14986is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14987{
14988 switch (value)
14989 {
14990 case DW_CC_normal:
14991 case DW_CC_pass_by_reference:
14992 case DW_CC_pass_by_value:
14993 return true;
14994
14995 default:
14996 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14997 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14998 return false;
14999 }
15000}
15001
d0922fcf
TBA
15002/* Check if the given VALUE is a valid enum dwarf_calling_convention
15003 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15004 also according to GNU-specific values (see include/dwarf2.h). */
15005
15006static bool
15007is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15008{
15009 switch (value)
15010 {
15011 case DW_CC_normal:
15012 case DW_CC_program:
15013 case DW_CC_nocall:
15014 return true;
15015
15016 case DW_CC_GNU_renesas_sh:
15017 case DW_CC_GNU_borland_fastcall_i386:
15018 case DW_CC_GDB_IBM_OpenCL:
15019 return true;
15020
15021 default:
15022 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15023 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15024 return false;
15025 }
15026}
15027
c906108c 15028/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15029 (definition) to create a type for the structure or union. Fill in
15030 the type's name and general properties; the members will not be
83655187
DE
15031 processed until process_structure_scope. A symbol table entry for
15032 the type will also not be done until process_structure_scope (assuming
15033 the type has a name).
c906108c 15034
c767944b
DJ
15035 NOTE: we need to call these functions regardless of whether or not the
15036 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15037 structure or union. This gets the type entered into our set of
83655187 15038 user defined types. */
c906108c 15039
f792889a 15040static struct type *
134d01f1 15041read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15042{
518817b3 15043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15044 struct type *type;
15045 struct attribute *attr;
15d034d0 15046 const char *name;
c906108c 15047
348e048f
DE
15048 /* If the definition of this type lives in .debug_types, read that type.
15049 Don't follow DW_AT_specification though, that will take us back up
15050 the chain and we want to go down. */
45e58e77 15051 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15052 if (attr != nullptr)
348e048f 15053 {
ac9ec31b 15054 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15055
ac9ec31b 15056 /* The type's CU may not be the same as CU.
02142a6c 15057 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15058 return set_die_type (die, type, cu);
15059 }
15060
c0dd20ea 15061 type = alloc_type (objfile);
c906108c 15062 INIT_CPLUS_SPECIFIC (type);
93311388 15063
39cbfefa
DJ
15064 name = dwarf2_name (die, cu);
15065 if (name != NULL)
c906108c 15066 {
987504bb 15067 if (cu->language == language_cplus
c44af4eb
TT
15068 || cu->language == language_d
15069 || cu->language == language_rust)
63d06c5c 15070 {
15d034d0 15071 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15072
15073 /* dwarf2_full_name might have already finished building the DIE's
15074 type. If so, there is no need to continue. */
15075 if (get_die_type (die, cu) != NULL)
15076 return get_die_type (die, cu);
15077
e86ca25f 15078 TYPE_NAME (type) = full_name;
63d06c5c
DC
15079 }
15080 else
15081 {
d8151005
DJ
15082 /* The name is already allocated along with this objfile, so
15083 we don't need to duplicate it for the type. */
e86ca25f 15084 TYPE_NAME (type) = name;
63d06c5c 15085 }
c906108c
SS
15086 }
15087
15088 if (die->tag == DW_TAG_structure_type)
15089 {
15090 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15091 }
15092 else if (die->tag == DW_TAG_union_type)
15093 {
15094 TYPE_CODE (type) = TYPE_CODE_UNION;
15095 }
2ddeaf8a
TT
15096 else if (die->tag == DW_TAG_variant_part)
15097 {
15098 TYPE_CODE (type) = TYPE_CODE_UNION;
15099 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15100 }
c906108c
SS
15101 else
15102 {
4753d33b 15103 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15104 }
15105
0cc2414c
TT
15106 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15107 TYPE_DECLARED_CLASS (type) = 1;
15108
e35000a7
TBA
15109 /* Store the calling convention in the type if it's available in
15110 the die. Otherwise the calling convention remains set to
15111 the default value DW_CC_normal. */
15112 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15113 if (attr != nullptr
15114 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15115 {
15116 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15117 TYPE_CPLUS_CALLING_CONVENTION (type)
15118 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15119 }
15120
e142c38c 15121 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15122 if (attr != nullptr)
c906108c 15123 {
cd6c91b4 15124 if (attr->form_is_constant ())
155bfbd3
JB
15125 TYPE_LENGTH (type) = DW_UNSND (attr);
15126 else
15127 {
15128 /* For the moment, dynamic type sizes are not supported
15129 by GDB's struct type. The actual size is determined
15130 on-demand when resolving the type of a given object,
15131 so set the type's length to zero for now. Otherwise,
15132 we record an expression as the length, and that expression
15133 could lead to a very large value, which could eventually
15134 lead to us trying to allocate that much memory when creating
15135 a value of that type. */
15136 TYPE_LENGTH (type) = 0;
15137 }
c906108c
SS
15138 }
15139 else
15140 {
15141 TYPE_LENGTH (type) = 0;
15142 }
15143
2b4424c3
TT
15144 maybe_set_alignment (cu, die, type);
15145
5230b05a 15146 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15147 {
5230b05a
WT
15148 /* ICC<14 does not output the required DW_AT_declaration on
15149 incomplete types, but gives them a size of zero. */
422b1cb0 15150 TYPE_STUB (type) = 1;
685b1105
JK
15151 }
15152 else
15153 TYPE_STUB_SUPPORTED (type) = 1;
15154
dc718098 15155 if (die_is_declaration (die, cu))
876cecd0 15156 TYPE_STUB (type) = 1;
a6c727b2
DJ
15157 else if (attr == NULL && die->child == NULL
15158 && producer_is_realview (cu->producer))
15159 /* RealView does not output the required DW_AT_declaration
15160 on incomplete types. */
15161 TYPE_STUB (type) = 1;
dc718098 15162
c906108c
SS
15163 /* We need to add the type field to the die immediately so we don't
15164 infinitely recurse when dealing with pointers to the structure
0963b4bd 15165 type within the structure itself. */
1c379e20 15166 set_die_type (die, type, cu);
c906108c 15167
7e314c57
JK
15168 /* set_die_type should be already done. */
15169 set_descriptive_type (type, die, cu);
15170
c767944b
DJ
15171 return type;
15172}
15173
2ddeaf8a
TT
15174/* A helper for process_structure_scope that handles a single member
15175 DIE. */
15176
15177static void
15178handle_struct_member_die (struct die_info *child_die, struct type *type,
15179 struct field_info *fi,
15180 std::vector<struct symbol *> *template_args,
15181 struct dwarf2_cu *cu)
15182{
15183 if (child_die->tag == DW_TAG_member
15184 || child_die->tag == DW_TAG_variable
15185 || child_die->tag == DW_TAG_variant_part)
15186 {
15187 /* NOTE: carlton/2002-11-05: A C++ static data member
15188 should be a DW_TAG_member that is a declaration, but
15189 all versions of G++ as of this writing (so through at
15190 least 3.2.1) incorrectly generate DW_TAG_variable
15191 tags for them instead. */
15192 dwarf2_add_field (fi, child_die, cu);
15193 }
15194 else if (child_die->tag == DW_TAG_subprogram)
15195 {
15196 /* Rust doesn't have member functions in the C++ sense.
15197 However, it does emit ordinary functions as children
15198 of a struct DIE. */
15199 if (cu->language == language_rust)
15200 read_func_scope (child_die, cu);
15201 else
15202 {
15203 /* C++ member function. */
15204 dwarf2_add_member_fn (fi, child_die, type, cu);
15205 }
15206 }
15207 else if (child_die->tag == DW_TAG_inheritance)
15208 {
15209 /* C++ base class field. */
15210 dwarf2_add_field (fi, child_die, cu);
15211 }
15212 else if (type_can_define_types (child_die))
15213 dwarf2_add_type_defn (fi, child_die, cu);
15214 else if (child_die->tag == DW_TAG_template_type_param
15215 || child_die->tag == DW_TAG_template_value_param)
15216 {
15217 struct symbol *arg = new_symbol (child_die, NULL, cu);
15218
15219 if (arg != NULL)
15220 template_args->push_back (arg);
15221 }
15222 else if (child_die->tag == DW_TAG_variant)
15223 {
15224 /* In a variant we want to get the discriminant and also add a
15225 field for our sole member child. */
15226 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15227
bde09ab7 15228 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15229 variant_child != NULL;
15230 variant_child = sibling_die (variant_child))
15231 {
15232 if (variant_child->tag == DW_TAG_member)
15233 {
15234 handle_struct_member_die (variant_child, type, fi,
15235 template_args, cu);
15236 /* Only handle the one. */
15237 break;
15238 }
15239 }
15240
15241 /* We don't handle this but we might as well report it if we see
15242 it. */
15243 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15244 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15245 " - DIE at %s [in module %s]"),
15246 sect_offset_str (child_die->sect_off),
15247 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15248
15249 /* The first field was just added, so we can stash the
15250 discriminant there. */
be2daae6 15251 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15252 if (discr == NULL)
be2daae6 15253 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15254 else
be2daae6 15255 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15256 }
15257}
15258
c767944b
DJ
15259/* Finish creating a structure or union type, including filling in
15260 its members and creating a symbol for it. */
15261
15262static void
15263process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15264{
518817b3 15265 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15266 struct die_info *child_die;
c767944b
DJ
15267 struct type *type;
15268
15269 type = get_die_type (die, cu);
15270 if (type == NULL)
15271 type = read_structure_type (die, cu);
15272
2ddeaf8a
TT
15273 /* When reading a DW_TAG_variant_part, we need to notice when we
15274 read the discriminant member, so we can record it later in the
15275 discriminant_info. */
15276 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15277 sect_offset discr_offset {};
3e1d3d8c 15278 bool has_template_parameters = false;
2ddeaf8a
TT
15279
15280 if (is_variant_part)
15281 {
15282 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15283 if (discr == NULL)
15284 {
15285 /* Maybe it's a univariant form, an extension we support.
15286 In this case arrange not to check the offset. */
15287 is_variant_part = false;
15288 }
cd6c91b4 15289 else if (discr->form_is_ref ())
2ddeaf8a
TT
15290 {
15291 struct dwarf2_cu *target_cu = cu;
15292 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15293
15294 discr_offset = target_die->sect_off;
15295 }
15296 else
15297 {
b98664d3 15298 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15299 " - DIE at %s [in module %s]"),
15300 sect_offset_str (die->sect_off),
15301 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15302 is_variant_part = false;
15303 }
15304 }
15305
e142c38c 15306 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15307 {
15308 struct field_info fi;
2f4732b0 15309 std::vector<struct symbol *> template_args;
c906108c 15310
639d11d3 15311 child_die = die->child;
c906108c
SS
15312
15313 while (child_die && child_die->tag)
15314 {
2ddeaf8a 15315 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15316
2ddeaf8a 15317 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15318 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15319
c906108c
SS
15320 child_die = sibling_die (child_die);
15321 }
15322
34eaf542 15323 /* Attach template arguments to type. */
2f4732b0 15324 if (!template_args.empty ())
34eaf542 15325 {
3e1d3d8c 15326 has_template_parameters = true;
34eaf542 15327 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15328 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15329 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15330 = XOBNEWVEC (&objfile->objfile_obstack,
15331 struct symbol *,
15332 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15333 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15334 template_args.data (),
34eaf542
TT
15335 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15336 * sizeof (struct symbol *)));
34eaf542
TT
15337 }
15338
c906108c 15339 /* Attach fields and member functions to the type. */
317f7127 15340 if (fi.nfields () > 0)
e7c27a73 15341 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15342 if (!fi.fnfieldlists.empty ())
c906108c 15343 {
e7c27a73 15344 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15345
c5aa993b 15346 /* Get the type which refers to the base class (possibly this
c906108c 15347 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15348 class from the DW_AT_containing_type attribute. This use of
15349 DW_AT_containing_type is a GNU extension. */
c906108c 15350
e142c38c 15351 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15352 {
e7c27a73 15353 struct type *t = die_containing_type (die, cu);
c906108c 15354
ae6ae975 15355 set_type_vptr_basetype (type, t);
c906108c
SS
15356 if (type == t)
15357 {
c906108c
SS
15358 int i;
15359
15360 /* Our own class provides vtbl ptr. */
15361 for (i = TYPE_NFIELDS (t) - 1;
15362 i >= TYPE_N_BASECLASSES (t);
15363 --i)
15364 {
0d5cff50 15365 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15366
1168df01 15367 if (is_vtable_name (fieldname, cu))
c906108c 15368 {
ae6ae975 15369 set_type_vptr_fieldno (type, i);
c906108c
SS
15370 break;
15371 }
15372 }
15373
15374 /* Complain if virtual function table field not found. */
15375 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15376 complaint (_("virtual function table pointer "
3e43a32a 15377 "not found when defining class '%s'"),
e86ca25f 15378 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15379 }
15380 else
15381 {
ae6ae975 15382 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15383 }
15384 }
f6235d4c 15385 else if (cu->producer
61012eef 15386 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15387 {
15388 /* The IBM XLC compiler does not provide direct indication
15389 of the containing type, but the vtable pointer is
15390 always named __vfp. */
15391
15392 int i;
15393
15394 for (i = TYPE_NFIELDS (type) - 1;
15395 i >= TYPE_N_BASECLASSES (type);
15396 --i)
15397 {
15398 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15399 {
ae6ae975
DE
15400 set_type_vptr_fieldno (type, i);
15401 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15402 break;
15403 }
15404 }
15405 }
c906108c 15406 }
98751a41
JK
15407
15408 /* Copy fi.typedef_field_list linked list elements content into the
15409 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15410 if (!fi.typedef_field_list.empty ())
98751a41 15411 {
be2daae6 15412 int count = fi.typedef_field_list.size ();
98751a41 15413
a0d7a4ff 15414 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15415 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15416 = ((struct decl_field *)
be2daae6
TT
15417 TYPE_ALLOC (type,
15418 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15419 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15420
be2daae6
TT
15421 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15422 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15423 }
c767944b 15424
883fd55a
KS
15425 /* Copy fi.nested_types_list linked list elements content into the
15426 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15427 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15428 {
be2daae6 15429 int count = fi.nested_types_list.size ();
883fd55a
KS
15430
15431 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15432 TYPE_NESTED_TYPES_ARRAY (type)
15433 = ((struct decl_field *)
be2daae6
TT
15434 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15435 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15436
be2daae6
TT
15437 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15438 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15439 }
c906108c 15440 }
63d06c5c 15441
bb5ed363 15442 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15443 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15444 cu->rust_unions.push_back (type);
0b92b5bb 15445
90aeadfc
DC
15446 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15447 snapshots) has been known to create a die giving a declaration
15448 for a class that has, as a child, a die giving a definition for a
15449 nested class. So we have to process our children even if the
15450 current die is a declaration. Normally, of course, a declaration
15451 won't have any children at all. */
134d01f1 15452
ca040673
DE
15453 child_die = die->child;
15454
90aeadfc
DC
15455 while (child_die != NULL && child_die->tag)
15456 {
15457 if (child_die->tag == DW_TAG_member
15458 || child_die->tag == DW_TAG_variable
34eaf542
TT
15459 || child_die->tag == DW_TAG_inheritance
15460 || child_die->tag == DW_TAG_template_value_param
15461 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15462 {
90aeadfc 15463 /* Do nothing. */
134d01f1 15464 }
90aeadfc
DC
15465 else
15466 process_die (child_die, cu);
134d01f1 15467
90aeadfc 15468 child_die = sibling_die (child_die);
134d01f1
DJ
15469 }
15470
fa4028e9
JB
15471 /* Do not consider external references. According to the DWARF standard,
15472 these DIEs are identified by the fact that they have no byte_size
15473 attribute, and a declaration attribute. */
15474 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15475 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15476 {
15477 struct symbol *sym = new_symbol (die, type, cu);
15478
15479 if (has_template_parameters)
15480 {
a776957c
TT
15481 struct symtab *symtab;
15482 if (sym != nullptr)
15483 symtab = symbol_symtab (sym);
15484 else if (cu->line_header != nullptr)
15485 {
15486 /* Any related symtab will do. */
15487 symtab
7ba99d21 15488 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15489 }
15490 else
15491 {
15492 symtab = nullptr;
15493 complaint (_("could not find suitable "
15494 "symtab for template parameter"
15495 " - DIE at %s [in module %s]"),
15496 sect_offset_str (die->sect_off),
15497 objfile_name (objfile));
15498 }
15499
15500 if (symtab != nullptr)
15501 {
15502 /* Make sure that the symtab is set on the new symbols.
15503 Even though they don't appear in this symtab directly,
15504 other parts of gdb assume that symbols do, and this is
15505 reasonably true. */
15506 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15507 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15508 }
3e1d3d8c
TT
15509 }
15510 }
134d01f1
DJ
15511}
15512
55426c9d
JB
15513/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15514 update TYPE using some information only available in DIE's children. */
15515
15516static void
15517update_enumeration_type_from_children (struct die_info *die,
15518 struct type *type,
15519 struct dwarf2_cu *cu)
15520{
60f7655a 15521 struct die_info *child_die;
55426c9d
JB
15522 int unsigned_enum = 1;
15523 int flag_enum = 1;
55426c9d 15524
8268c778 15525 auto_obstack obstack;
55426c9d 15526
60f7655a
DE
15527 for (child_die = die->child;
15528 child_die != NULL && child_die->tag;
15529 child_die = sibling_die (child_die))
55426c9d
JB
15530 {
15531 struct attribute *attr;
15532 LONGEST value;
15533 const gdb_byte *bytes;
15534 struct dwarf2_locexpr_baton *baton;
15535 const char *name;
60f7655a 15536
55426c9d
JB
15537 if (child_die->tag != DW_TAG_enumerator)
15538 continue;
15539
15540 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15541 if (attr == NULL)
15542 continue;
15543
15544 name = dwarf2_name (child_die, cu);
15545 if (name == NULL)
15546 name = "<anonymous enumerator>";
15547
15548 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15549 &value, &bytes, &baton);
15550 if (value < 0)
15551 {
15552 unsigned_enum = 0;
15553 flag_enum = 0;
15554 }
55426c9d 15555 else
edd45eb0
SM
15556 {
15557 if (count_one_bits_ll (value) >= 2)
15558 flag_enum = 0;
edd45eb0 15559 }
55426c9d
JB
15560
15561 /* If we already know that the enum type is neither unsigned, nor
15562 a flag type, no need to look at the rest of the enumerates. */
15563 if (!unsigned_enum && !flag_enum)
15564 break;
55426c9d
JB
15565 }
15566
15567 if (unsigned_enum)
15568 TYPE_UNSIGNED (type) = 1;
15569 if (flag_enum)
15570 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15571}
15572
134d01f1
DJ
15573/* Given a DW_AT_enumeration_type die, set its type. We do not
15574 complete the type's fields yet, or create any symbols. */
c906108c 15575
f792889a 15576static struct type *
134d01f1 15577read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15578{
518817b3 15579 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15580 struct type *type;
c906108c 15581 struct attribute *attr;
0114d602 15582 const char *name;
134d01f1 15583
348e048f
DE
15584 /* If the definition of this type lives in .debug_types, read that type.
15585 Don't follow DW_AT_specification though, that will take us back up
15586 the chain and we want to go down. */
45e58e77 15587 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15588 if (attr != nullptr)
348e048f 15589 {
ac9ec31b 15590 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15591
ac9ec31b 15592 /* The type's CU may not be the same as CU.
02142a6c 15593 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15594 return set_die_type (die, type, cu);
15595 }
15596
c906108c
SS
15597 type = alloc_type (objfile);
15598
15599 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15600 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15601 if (name != NULL)
e86ca25f 15602 TYPE_NAME (type) = name;
c906108c 15603
0626fc76
TT
15604 attr = dwarf2_attr (die, DW_AT_type, cu);
15605 if (attr != NULL)
15606 {
15607 struct type *underlying_type = die_type (die, cu);
15608
15609 TYPE_TARGET_TYPE (type) = underlying_type;
15610 }
15611
e142c38c 15612 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15613 if (attr != nullptr)
c906108c
SS
15614 {
15615 TYPE_LENGTH (type) = DW_UNSND (attr);
15616 }
15617 else
15618 {
15619 TYPE_LENGTH (type) = 0;
15620 }
15621
2b4424c3
TT
15622 maybe_set_alignment (cu, die, type);
15623
137033e9
JB
15624 /* The enumeration DIE can be incomplete. In Ada, any type can be
15625 declared as private in the package spec, and then defined only
15626 inside the package body. Such types are known as Taft Amendment
15627 Types. When another package uses such a type, an incomplete DIE
15628 may be generated by the compiler. */
02eb380e 15629 if (die_is_declaration (die, cu))
876cecd0 15630 TYPE_STUB (type) = 1;
02eb380e 15631
0626fc76
TT
15632 /* Finish the creation of this type by using the enum's children.
15633 We must call this even when the underlying type has been provided
15634 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15635 update_enumeration_type_from_children (die, type, cu);
15636
0626fc76
TT
15637 /* If this type has an underlying type that is not a stub, then we
15638 may use its attributes. We always use the "unsigned" attribute
15639 in this situation, because ordinarily we guess whether the type
15640 is unsigned -- but the guess can be wrong and the underlying type
15641 can tell us the reality. However, we defer to a local size
15642 attribute if one exists, because this lets the compiler override
15643 the underlying type if needed. */
15644 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15645 {
15646 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15647 if (TYPE_LENGTH (type) == 0)
15648 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15649 if (TYPE_RAW_ALIGN (type) == 0
15650 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15651 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15652 }
15653
3d567982
TT
15654 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15655
f792889a 15656 return set_die_type (die, type, cu);
134d01f1
DJ
15657}
15658
15659/* Given a pointer to a die which begins an enumeration, process all
15660 the dies that define the members of the enumeration, and create the
15661 symbol for the enumeration type.
15662
15663 NOTE: We reverse the order of the element list. */
15664
15665static void
15666process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15667{
f792889a 15668 struct type *this_type;
134d01f1 15669
f792889a
DJ
15670 this_type = get_die_type (die, cu);
15671 if (this_type == NULL)
15672 this_type = read_enumeration_type (die, cu);
9dc481d3 15673
639d11d3 15674 if (die->child != NULL)
c906108c 15675 {
9dc481d3
DE
15676 struct die_info *child_die;
15677 struct symbol *sym;
43816ebc 15678 std::vector<struct field> fields;
15d034d0 15679 const char *name;
9dc481d3 15680
639d11d3 15681 child_die = die->child;
c906108c
SS
15682 while (child_die && child_die->tag)
15683 {
15684 if (child_die->tag != DW_TAG_enumerator)
15685 {
e7c27a73 15686 process_die (child_die, cu);
c906108c
SS
15687 }
15688 else
15689 {
39cbfefa
DJ
15690 name = dwarf2_name (child_die, cu);
15691 if (name)
c906108c 15692 {
f792889a 15693 sym = new_symbol (child_die, this_type, cu);
c906108c 15694
43816ebc
TT
15695 fields.emplace_back ();
15696 struct field &field = fields.back ();
c906108c 15697
43816ebc
TT
15698 FIELD_NAME (field) = sym->linkage_name ();
15699 FIELD_TYPE (field) = NULL;
15700 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15701 FIELD_BITSIZE (field) = 0;
c906108c
SS
15702 }
15703 }
15704
15705 child_die = sibling_die (child_die);
15706 }
15707
43816ebc 15708 if (!fields.empty ())
c906108c 15709 {
43816ebc 15710 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15711 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15712 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15713 memcpy (TYPE_FIELDS (this_type), fields.data (),
15714 sizeof (struct field) * fields.size ());
c906108c 15715 }
c906108c 15716 }
134d01f1 15717
6c83ed52
TT
15718 /* If we are reading an enum from a .debug_types unit, and the enum
15719 is a declaration, and the enum is not the signatured type in the
15720 unit, then we do not want to add a symbol for it. Adding a
15721 symbol would in some cases obscure the true definition of the
15722 enum, giving users an incomplete type when the definition is
15723 actually available. Note that we do not want to do this for all
15724 enums which are just declarations, because C++0x allows forward
15725 enum declarations. */
3019eac3 15726 if (cu->per_cu->is_debug_types
6c83ed52
TT
15727 && die_is_declaration (die, cu))
15728 {
52dc124a 15729 struct signatured_type *sig_type;
6c83ed52 15730
c0f78cd4 15731 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15732 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15733 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15734 return;
15735 }
15736
f792889a 15737 new_symbol (die, this_type, cu);
c906108c
SS
15738}
15739
15740/* Extract all information from a DW_TAG_array_type DIE and put it in
15741 the DIE's type field. For now, this only handles one dimensional
15742 arrays. */
15743
f792889a 15744static struct type *
e7c27a73 15745read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15746{
518817b3 15747 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15748 struct die_info *child_die;
7e314c57 15749 struct type *type;
c906108c 15750 struct type *element_type, *range_type, *index_type;
c906108c 15751 struct attribute *attr;
15d034d0 15752 const char *name;
a405673c 15753 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15754 unsigned int bit_stride = 0;
c906108c 15755
e7c27a73 15756 element_type = die_type (die, cu);
c906108c 15757
7e314c57
JK
15758 /* The die_type call above may have already set the type for this DIE. */
15759 type = get_die_type (die, cu);
15760 if (type)
15761 return type;
15762
dc53a7ad
JB
15763 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15764 if (attr != NULL)
a405673c
JB
15765 {
15766 int stride_ok;
09ba997f 15767 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15768
15769 byte_stride_prop
15770 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15771 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15772 prop_type);
a405673c
JB
15773 if (!stride_ok)
15774 {
b98664d3 15775 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15776 " - DIE at %s [in module %s]"),
15777 sect_offset_str (die->sect_off),
518817b3 15778 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15779 /* Ignore this attribute. We will likely not be able to print
15780 arrays of this type correctly, but there is little we can do
15781 to help if we cannot read the attribute's value. */
15782 byte_stride_prop = NULL;
15783 }
15784 }
dc53a7ad
JB
15785
15786 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15787 if (attr != NULL)
15788 bit_stride = DW_UNSND (attr);
15789
c906108c
SS
15790 /* Irix 6.2 native cc creates array types without children for
15791 arrays with unspecified length. */
639d11d3 15792 if (die->child == NULL)
c906108c 15793 {
46bf5051 15794 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15795 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15796 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15797 byte_stride_prop, bit_stride);
f792889a 15798 return set_die_type (die, type, cu);
c906108c
SS
15799 }
15800
791afaa2 15801 std::vector<struct type *> range_types;
639d11d3 15802 child_die = die->child;
c906108c
SS
15803 while (child_die && child_die->tag)
15804 {
15805 if (child_die->tag == DW_TAG_subrange_type)
15806 {
f792889a 15807 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15808
f792889a 15809 if (child_type != NULL)
a02abb62 15810 {
0963b4bd
MS
15811 /* The range type was succesfully read. Save it for the
15812 array type creation. */
791afaa2 15813 range_types.push_back (child_type);
a02abb62 15814 }
c906108c
SS
15815 }
15816 child_die = sibling_die (child_die);
15817 }
15818
15819 /* Dwarf2 dimensions are output from left to right, create the
15820 necessary array types in backwards order. */
7ca2d3a3 15821
c906108c 15822 type = element_type;
7ca2d3a3
DL
15823
15824 if (read_array_order (die, cu) == DW_ORD_col_major)
15825 {
15826 int i = 0;
9a619af0 15827
791afaa2 15828 while (i < range_types.size ())
dc53a7ad 15829 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15830 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15831 }
15832 else
15833 {
791afaa2 15834 size_t ndim = range_types.size ();
7ca2d3a3 15835 while (ndim-- > 0)
dc53a7ad 15836 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15837 byte_stride_prop, bit_stride);
7ca2d3a3 15838 }
c906108c 15839
f5f8a009
EZ
15840 /* Understand Dwarf2 support for vector types (like they occur on
15841 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15842 array type. This is not part of the Dwarf2/3 standard yet, but a
15843 custom vendor extension. The main difference between a regular
15844 array and the vector variant is that vectors are passed by value
15845 to functions. */
e142c38c 15846 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15847 if (attr != nullptr)
ea37ba09 15848 make_vector_type (type);
f5f8a009 15849
dbc98a8b
KW
15850 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15851 implementation may choose to implement triple vectors using this
15852 attribute. */
15853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15854 if (attr != nullptr)
dbc98a8b
KW
15855 {
15856 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15857 TYPE_LENGTH (type) = DW_UNSND (attr);
15858 else
b98664d3 15859 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15860 "than the total size of elements"));
dbc98a8b
KW
15861 }
15862
39cbfefa
DJ
15863 name = dwarf2_name (die, cu);
15864 if (name)
15865 TYPE_NAME (type) = name;
6e70227d 15866
2b4424c3
TT
15867 maybe_set_alignment (cu, die, type);
15868
0963b4bd 15869 /* Install the type in the die. */
7e314c57
JK
15870 set_die_type (die, type, cu);
15871
15872 /* set_die_type should be already done. */
b4ba55a1
JB
15873 set_descriptive_type (type, die, cu);
15874
7e314c57 15875 return type;
c906108c
SS
15876}
15877
7ca2d3a3 15878static enum dwarf_array_dim_ordering
6e70227d 15879read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15880{
15881 struct attribute *attr;
15882
15883 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15884
435d3d88 15885 if (attr != nullptr)
aead7601 15886 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15887
0963b4bd
MS
15888 /* GNU F77 is a special case, as at 08/2004 array type info is the
15889 opposite order to the dwarf2 specification, but data is still
15890 laid out as per normal fortran.
7ca2d3a3 15891
0963b4bd
MS
15892 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15893 version checking. */
7ca2d3a3 15894
905e0470
PM
15895 if (cu->language == language_fortran
15896 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15897 {
15898 return DW_ORD_row_major;
15899 }
15900
6e70227d 15901 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15902 {
15903 case array_column_major:
15904 return DW_ORD_col_major;
15905 case array_row_major:
15906 default:
15907 return DW_ORD_row_major;
15908 };
15909}
15910
72019c9c 15911/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15912 the DIE's type field. */
72019c9c 15913
f792889a 15914static struct type *
72019c9c
GM
15915read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15916{
7e314c57
JK
15917 struct type *domain_type, *set_type;
15918 struct attribute *attr;
f792889a 15919
7e314c57
JK
15920 domain_type = die_type (die, cu);
15921
15922 /* The die_type call above may have already set the type for this DIE. */
15923 set_type = get_die_type (die, cu);
15924 if (set_type)
15925 return set_type;
15926
15927 set_type = create_set_type (NULL, domain_type);
15928
15929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15930 if (attr != nullptr)
d09039dd 15931 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15932
2b4424c3
TT
15933 maybe_set_alignment (cu, die, set_type);
15934
f792889a 15935 return set_die_type (die, set_type, cu);
72019c9c 15936}
7ca2d3a3 15937
0971de02
TT
15938/* A helper for read_common_block that creates a locexpr baton.
15939 SYM is the symbol which we are marking as computed.
15940 COMMON_DIE is the DIE for the common block.
15941 COMMON_LOC is the location expression attribute for the common
15942 block itself.
15943 MEMBER_LOC is the location expression attribute for the particular
15944 member of the common block that we are processing.
15945 CU is the CU from which the above come. */
15946
15947static void
15948mark_common_block_symbol_computed (struct symbol *sym,
15949 struct die_info *common_die,
15950 struct attribute *common_loc,
15951 struct attribute *member_loc,
15952 struct dwarf2_cu *cu)
15953{
518817b3
SM
15954 struct dwarf2_per_objfile *dwarf2_per_objfile
15955 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15956 struct objfile *objfile = dwarf2_per_objfile->objfile;
15957 struct dwarf2_locexpr_baton *baton;
15958 gdb_byte *ptr;
15959 unsigned int cu_off;
15960 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15961 LONGEST offset = 0;
15962
15963 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15964 gdb_assert (common_loc->form_is_block ());
15965 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15966 || member_loc->form_is_constant ());
0971de02 15967
8d749320 15968 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15969 baton->per_cu = cu->per_cu;
15970 gdb_assert (baton->per_cu);
15971
15972 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15973
cd6c91b4 15974 if (member_loc->form_is_constant ())
0971de02
TT
15975 {
15976 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15977 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15978 }
15979 else
15980 baton->size += DW_BLOCK (member_loc)->size;
15981
224c3ddb 15982 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15983 baton->data = ptr;
15984
15985 *ptr++ = DW_OP_call4;
9c541725 15986 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15987 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15988 ptr += 4;
15989
cd6c91b4 15990 if (member_loc->form_is_constant ())
0971de02
TT
15991 {
15992 *ptr++ = DW_OP_addr;
15993 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15994 ptr += cu->header.addr_size;
15995 }
15996 else
15997 {
15998 /* We have to copy the data here, because DW_OP_call4 will only
15999 use a DW_AT_location attribute. */
16000 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16001 ptr += DW_BLOCK (member_loc)->size;
16002 }
16003
16004 *ptr++ = DW_OP_plus;
16005 gdb_assert (ptr - baton->data == baton->size);
16006
0971de02 16007 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16008 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16009}
16010
4357ac6c
TT
16011/* Create appropriate locally-scoped variables for all the
16012 DW_TAG_common_block entries. Also create a struct common_block
16013 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16014 is used to separate the common blocks name namespace from regular
4357ac6c 16015 variable names. */
c906108c
SS
16016
16017static void
e7c27a73 16018read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16019{
0971de02
TT
16020 struct attribute *attr;
16021
16022 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16023 if (attr != nullptr)
0971de02
TT
16024 {
16025 /* Support the .debug_loc offsets. */
4fc6c0d5 16026 if (attr->form_is_block ())
0971de02
TT
16027 {
16028 /* Ok. */
16029 }
cd6c91b4 16030 else if (attr->form_is_section_offset ())
0971de02
TT
16031 {
16032 dwarf2_complex_location_expr_complaint ();
16033 attr = NULL;
16034 }
16035 else
16036 {
16037 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16038 "common block member");
16039 attr = NULL;
16040 }
16041 }
16042
639d11d3 16043 if (die->child != NULL)
c906108c 16044 {
518817b3 16045 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16046 struct die_info *child_die;
16047 size_t n_entries = 0, size;
16048 struct common_block *common_block;
16049 struct symbol *sym;
74ac6d43 16050
4357ac6c
TT
16051 for (child_die = die->child;
16052 child_die && child_die->tag;
16053 child_die = sibling_die (child_die))
16054 ++n_entries;
16055
16056 size = (sizeof (struct common_block)
16057 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16058 common_block
16059 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16060 size);
4357ac6c
TT
16061 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16062 common_block->n_entries = 0;
16063
16064 for (child_die = die->child;
16065 child_die && child_die->tag;
16066 child_die = sibling_die (child_die))
16067 {
16068 /* Create the symbol in the DW_TAG_common_block block in the current
16069 symbol scope. */
e7c27a73 16070 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16071 if (sym != NULL)
16072 {
16073 struct attribute *member_loc;
16074
16075 common_block->contents[common_block->n_entries++] = sym;
16076
16077 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16078 cu);
16079 if (member_loc)
16080 {
16081 /* GDB has handled this for a long time, but it is
16082 not specified by DWARF. It seems to have been
16083 emitted by gfortran at least as recently as:
16084 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16085 complaint (_("Variable in common block has "
0971de02 16086 "DW_AT_data_member_location "
9d8780f0
SM
16087 "- DIE at %s [in module %s]"),
16088 sect_offset_str (child_die->sect_off),
518817b3 16089 objfile_name (objfile));
0971de02 16090
cd6c91b4 16091 if (member_loc->form_is_section_offset ())
0971de02 16092 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16093 else if (member_loc->form_is_constant ()
4fc6c0d5 16094 || member_loc->form_is_block ())
0971de02 16095 {
435d3d88 16096 if (attr != nullptr)
0971de02
TT
16097 mark_common_block_symbol_computed (sym, die, attr,
16098 member_loc, cu);
16099 }
16100 else
16101 dwarf2_complex_location_expr_complaint ();
16102 }
16103 }
c906108c 16104 }
4357ac6c
TT
16105
16106 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16107 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16108 }
16109}
16110
0114d602 16111/* Create a type for a C++ namespace. */
d9fa45fe 16112
0114d602
DJ
16113static struct type *
16114read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16115{
518817b3 16116 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16117 const char *previous_prefix, *name;
9219021c 16118 int is_anonymous;
0114d602
DJ
16119 struct type *type;
16120
16121 /* For extensions, reuse the type of the original namespace. */
16122 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16123 {
16124 struct die_info *ext_die;
16125 struct dwarf2_cu *ext_cu = cu;
9a619af0 16126
0114d602
DJ
16127 ext_die = dwarf2_extension (die, &ext_cu);
16128 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16129
16130 /* EXT_CU may not be the same as CU.
02142a6c 16131 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16132 return set_die_type (die, type, cu);
16133 }
9219021c 16134
e142c38c 16135 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16136
16137 /* Now build the name of the current namespace. */
16138
0114d602
DJ
16139 previous_prefix = determine_prefix (die, cu);
16140 if (previous_prefix[0] != '\0')
16141 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16142 previous_prefix, name, 0, cu);
0114d602
DJ
16143
16144 /* Create the type. */
19f392bc 16145 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16146
60531b24 16147 return set_die_type (die, type, cu);
0114d602
DJ
16148}
16149
22cee43f 16150/* Read a namespace scope. */
0114d602
DJ
16151
16152static void
16153read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16154{
518817b3 16155 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16156 int is_anonymous;
9219021c 16157
5c4e30ca
DC
16158 /* Add a symbol associated to this if we haven't seen the namespace
16159 before. Also, add a using directive if it's an anonymous
16160 namespace. */
9219021c 16161
f2f0e013 16162 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16163 {
16164 struct type *type;
16165
0114d602 16166 type = read_type_die (die, cu);
e7c27a73 16167 new_symbol (die, type, cu);
5c4e30ca 16168
e8e80198 16169 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16170 if (is_anonymous)
0114d602
DJ
16171 {
16172 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16173
eb1e02fd 16174 std::vector<const char *> excludes;
804d2729 16175 add_using_directive (using_directives (cu),
22cee43f 16176 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16177 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16178 }
5c4e30ca 16179 }
9219021c 16180
639d11d3 16181 if (die->child != NULL)
d9fa45fe 16182 {
639d11d3 16183 struct die_info *child_die = die->child;
6e70227d 16184
d9fa45fe
DC
16185 while (child_die && child_die->tag)
16186 {
e7c27a73 16187 process_die (child_die, cu);
d9fa45fe
DC
16188 child_die = sibling_die (child_die);
16189 }
16190 }
38d518c9
EZ
16191}
16192
f55ee35c
JK
16193/* Read a Fortran module as type. This DIE can be only a declaration used for
16194 imported module. Still we need that type as local Fortran "use ... only"
16195 declaration imports depend on the created type in determine_prefix. */
16196
16197static struct type *
16198read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16199{
518817b3 16200 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16201 const char *module_name;
f55ee35c
JK
16202 struct type *type;
16203
16204 module_name = dwarf2_name (die, cu);
19f392bc 16205 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16206
f55ee35c
JK
16207 return set_die_type (die, type, cu);
16208}
16209
5d7cb8df
JK
16210/* Read a Fortran module. */
16211
16212static void
16213read_module (struct die_info *die, struct dwarf2_cu *cu)
16214{
16215 struct die_info *child_die = die->child;
530e8392
KB
16216 struct type *type;
16217
16218 type = read_type_die (die, cu);
16219 new_symbol (die, type, cu);
5d7cb8df 16220
5d7cb8df
JK
16221 while (child_die && child_die->tag)
16222 {
16223 process_die (child_die, cu);
16224 child_die = sibling_die (child_die);
16225 }
16226}
16227
38d518c9
EZ
16228/* Return the name of the namespace represented by DIE. Set
16229 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16230 namespace. */
16231
16232static const char *
e142c38c 16233namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16234{
16235 struct die_info *current_die;
16236 const char *name = NULL;
16237
16238 /* Loop through the extensions until we find a name. */
16239
16240 for (current_die = die;
16241 current_die != NULL;
f2f0e013 16242 current_die = dwarf2_extension (die, &cu))
38d518c9 16243 {
96553a0c
DE
16244 /* We don't use dwarf2_name here so that we can detect the absence
16245 of a name -> anonymous namespace. */
7d45c7c3 16246 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16247
38d518c9
EZ
16248 if (name != NULL)
16249 break;
16250 }
16251
16252 /* Is it an anonymous namespace? */
16253
16254 *is_anonymous = (name == NULL);
16255 if (*is_anonymous)
2b1dbab0 16256 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16257
16258 return name;
d9fa45fe
DC
16259}
16260
c906108c
SS
16261/* Extract all information from a DW_TAG_pointer_type DIE and add to
16262 the user defined type vector. */
16263
f792889a 16264static struct type *
e7c27a73 16265read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16266{
518817b3
SM
16267 struct gdbarch *gdbarch
16268 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16269 struct comp_unit_head *cu_header = &cu->header;
c906108c 16270 struct type *type;
8b2dbe47
KB
16271 struct attribute *attr_byte_size;
16272 struct attribute *attr_address_class;
16273 int byte_size, addr_class;
7e314c57
JK
16274 struct type *target_type;
16275
16276 target_type = die_type (die, cu);
c906108c 16277
7e314c57
JK
16278 /* The die_type call above may have already set the type for this DIE. */
16279 type = get_die_type (die, cu);
16280 if (type)
16281 return type;
16282
16283 type = lookup_pointer_type (target_type);
8b2dbe47 16284
e142c38c 16285 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16286 if (attr_byte_size)
16287 byte_size = DW_UNSND (attr_byte_size);
c906108c 16288 else
8b2dbe47
KB
16289 byte_size = cu_header->addr_size;
16290
e142c38c 16291 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16292 if (attr_address_class)
16293 addr_class = DW_UNSND (attr_address_class);
16294 else
16295 addr_class = DW_ADDR_none;
16296
2b4424c3
TT
16297 ULONGEST alignment = get_alignment (cu, die);
16298
16299 /* If the pointer size, alignment, or address class is different
16300 than the default, create a type variant marked as such and set
16301 the length accordingly. */
16302 if (TYPE_LENGTH (type) != byte_size
16303 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16304 && alignment != TYPE_RAW_ALIGN (type))
16305 || addr_class != DW_ADDR_none)
c906108c 16306 {
5e2b427d 16307 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16308 {
16309 int type_flags;
16310
849957d9 16311 type_flags = gdbarch_address_class_type_flags
5e2b427d 16312 (gdbarch, byte_size, addr_class);
876cecd0
TT
16313 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16314 == 0);
8b2dbe47
KB
16315 type = make_type_with_address_space (type, type_flags);
16316 }
16317 else if (TYPE_LENGTH (type) != byte_size)
16318 {
b98664d3 16319 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16320 }
2b4424c3
TT
16321 else if (TYPE_RAW_ALIGN (type) != alignment)
16322 {
b98664d3 16323 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16324 " - DIE at %s [in module %s]"),
16325 sect_offset_str (die->sect_off),
16326 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16327 }
6e70227d 16328 else
9a619af0
MS
16329 {
16330 /* Should we also complain about unhandled address classes? */
16331 }
c906108c 16332 }
8b2dbe47
KB
16333
16334 TYPE_LENGTH (type) = byte_size;
2b4424c3 16335 set_type_align (type, alignment);
f792889a 16336 return set_die_type (die, type, cu);
c906108c
SS
16337}
16338
16339/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16340 the user defined type vector. */
16341
f792889a 16342static struct type *
e7c27a73 16343read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16344{
16345 struct type *type;
16346 struct type *to_type;
16347 struct type *domain;
16348
e7c27a73
DJ
16349 to_type = die_type (die, cu);
16350 domain = die_containing_type (die, cu);
0d5de010 16351
7e314c57
JK
16352 /* The calls above may have already set the type for this DIE. */
16353 type = get_die_type (die, cu);
16354 if (type)
16355 return type;
16356
0d5de010
DJ
16357 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16358 type = lookup_methodptr_type (to_type);
7078baeb
TT
16359 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16360 {
518817b3
SM
16361 struct type *new_type
16362 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16363
16364 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16365 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16366 TYPE_VARARGS (to_type));
16367 type = lookup_methodptr_type (new_type);
16368 }
0d5de010
DJ
16369 else
16370 type = lookup_memberptr_type (to_type, domain);
c906108c 16371
f792889a 16372 return set_die_type (die, type, cu);
c906108c
SS
16373}
16374
4297a3f0 16375/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16376 the user defined type vector. */
16377
f792889a 16378static struct type *
4297a3f0
AV
16379read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16380 enum type_code refcode)
c906108c 16381{
e7c27a73 16382 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16383 struct type *type, *target_type;
c906108c
SS
16384 struct attribute *attr;
16385
4297a3f0
AV
16386 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16387
7e314c57
JK
16388 target_type = die_type (die, cu);
16389
16390 /* The die_type call above may have already set the type for this DIE. */
16391 type = get_die_type (die, cu);
16392 if (type)
16393 return type;
16394
4297a3f0 16395 type = lookup_reference_type (target_type, refcode);
e142c38c 16396 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16397 if (attr != nullptr)
c906108c
SS
16398 {
16399 TYPE_LENGTH (type) = DW_UNSND (attr);
16400 }
16401 else
16402 {
107d2387 16403 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16404 }
2b4424c3 16405 maybe_set_alignment (cu, die, type);
f792889a 16406 return set_die_type (die, type, cu);
c906108c
SS
16407}
16408
cf363f18
MW
16409/* Add the given cv-qualifiers to the element type of the array. GCC
16410 outputs DWARF type qualifiers that apply to an array, not the
16411 element type. But GDB relies on the array element type to carry
16412 the cv-qualifiers. This mimics section 6.7.3 of the C99
16413 specification. */
16414
16415static struct type *
16416add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16417 struct type *base_type, int cnst, int voltl)
16418{
16419 struct type *el_type, *inner_array;
16420
16421 base_type = copy_type (base_type);
16422 inner_array = base_type;
16423
16424 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16425 {
16426 TYPE_TARGET_TYPE (inner_array) =
16427 copy_type (TYPE_TARGET_TYPE (inner_array));
16428 inner_array = TYPE_TARGET_TYPE (inner_array);
16429 }
16430
16431 el_type = TYPE_TARGET_TYPE (inner_array);
16432 cnst |= TYPE_CONST (el_type);
16433 voltl |= TYPE_VOLATILE (el_type);
16434 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16435
16436 return set_die_type (die, base_type, cu);
16437}
16438
f792889a 16439static struct type *
e7c27a73 16440read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16441{
f792889a 16442 struct type *base_type, *cv_type;
c906108c 16443
e7c27a73 16444 base_type = die_type (die, cu);
7e314c57
JK
16445
16446 /* The die_type call above may have already set the type for this DIE. */
16447 cv_type = get_die_type (die, cu);
16448 if (cv_type)
16449 return cv_type;
16450
2f608a3a
KW
16451 /* In case the const qualifier is applied to an array type, the element type
16452 is so qualified, not the array type (section 6.7.3 of C99). */
16453 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16454 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16455
f792889a
DJ
16456 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16457 return set_die_type (die, cv_type, cu);
c906108c
SS
16458}
16459
f792889a 16460static struct type *
e7c27a73 16461read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16462{
f792889a 16463 struct type *base_type, *cv_type;
c906108c 16464
e7c27a73 16465 base_type = die_type (die, cu);
7e314c57
JK
16466
16467 /* The die_type call above may have already set the type for this DIE. */
16468 cv_type = get_die_type (die, cu);
16469 if (cv_type)
16470 return cv_type;
16471
cf363f18
MW
16472 /* In case the volatile qualifier is applied to an array type, the
16473 element type is so qualified, not the array type (section 6.7.3
16474 of C99). */
16475 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16476 return add_array_cv_type (die, cu, base_type, 0, 1);
16477
f792889a
DJ
16478 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16479 return set_die_type (die, cv_type, cu);
c906108c
SS
16480}
16481
06d66ee9
TT
16482/* Handle DW_TAG_restrict_type. */
16483
16484static struct type *
16485read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16486{
16487 struct type *base_type, *cv_type;
16488
16489 base_type = die_type (die, cu);
16490
16491 /* The die_type call above may have already set the type for this DIE. */
16492 cv_type = get_die_type (die, cu);
16493 if (cv_type)
16494 return cv_type;
16495
16496 cv_type = make_restrict_type (base_type);
16497 return set_die_type (die, cv_type, cu);
16498}
16499
a2c2acaf
MW
16500/* Handle DW_TAG_atomic_type. */
16501
16502static struct type *
16503read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16504{
16505 struct type *base_type, *cv_type;
16506
16507 base_type = die_type (die, cu);
16508
16509 /* The die_type call above may have already set the type for this DIE. */
16510 cv_type = get_die_type (die, cu);
16511 if (cv_type)
16512 return cv_type;
16513
16514 cv_type = make_atomic_type (base_type);
16515 return set_die_type (die, cv_type, cu);
16516}
16517
c906108c
SS
16518/* Extract all information from a DW_TAG_string_type DIE and add to
16519 the user defined type vector. It isn't really a user defined type,
16520 but it behaves like one, with other DIE's using an AT_user_def_type
16521 attribute to reference it. */
16522
f792889a 16523static struct type *
e7c27a73 16524read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16525{
518817b3 16526 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16527 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16528 struct type *type, *range_type, *index_type, *char_type;
16529 struct attribute *attr;
216a7e6b
AB
16530 struct dynamic_prop prop;
16531 bool length_is_constant = true;
16532 LONGEST length;
16533
16534 /* There are a couple of places where bit sizes might be made use of
16535 when parsing a DW_TAG_string_type, however, no producer that we know
16536 of make use of these. Handling bit sizes that are a multiple of the
16537 byte size is easy enough, but what about other bit sizes? Lets deal
16538 with that problem when we have to. Warn about these attributes being
16539 unsupported, then parse the type and ignore them like we always
16540 have. */
16541 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16542 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16543 {
16544 static bool warning_printed = false;
16545 if (!warning_printed)
16546 {
16547 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16548 "currently supported on DW_TAG_string_type."));
16549 warning_printed = true;
16550 }
16551 }
c906108c 16552
e142c38c 16553 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16554 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16555 {
16556 /* The string length describes the location at which the length of
16557 the string can be found. The size of the length field can be
16558 specified with one of the attributes below. */
16559 struct type *prop_type;
16560 struct attribute *len
16561 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16562 if (len == nullptr)
16563 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16564 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16565 {
16566 /* Pass 0 as the default as we know this attribute is constant
16567 and the default value will not be returned. */
16568 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
09ba997f 16569 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16570 }
16571 else
16572 {
16573 /* If the size is not specified then we assume it is the size of
16574 an address on this target. */
09ba997f 16575 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16576 }
16577
16578 /* Convert the attribute into a dynamic property. */
16579 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16580 length = 1;
16581 else
16582 length_is_constant = false;
16583 }
16584 else if (attr != nullptr)
16585 {
16586 /* This DW_AT_string_length just contains the length with no
16587 indirection. There's no need to create a dynamic property in this
16588 case. Pass 0 for the default value as we know it will not be
16589 returned in this case. */
16590 length = dwarf2_get_attr_constant_value (attr, 0);
16591 }
16592 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16593 {
216a7e6b
AB
16594 /* We don't currently support non-constant byte sizes for strings. */
16595 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16596 }
16597 else
16598 {
216a7e6b
AB
16599 /* Use 1 as a fallback length if we have nothing else. */
16600 length = 1;
c906108c 16601 }
6ccb9162 16602
46bf5051 16603 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16604 if (length_is_constant)
16605 range_type = create_static_range_type (NULL, index_type, 1, length);
16606 else
16607 {
16608 struct dynamic_prop low_bound;
16609
16610 low_bound.kind = PROP_CONST;
16611 low_bound.data.const_val = 1;
16612 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16613 }
3b7538c0
UW
16614 char_type = language_string_char_type (cu->language_defn, gdbarch);
16615 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16616
f792889a 16617 return set_die_type (die, type, cu);
c906108c
SS
16618}
16619
4d804846
JB
16620/* Assuming that DIE corresponds to a function, returns nonzero
16621 if the function is prototyped. */
16622
16623static int
16624prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16625{
16626 struct attribute *attr;
16627
16628 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16629 if (attr && (DW_UNSND (attr) != 0))
16630 return 1;
16631
16632 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16633 is only meaningful for C, but the concept also extends to other
4d804846
JB
16634 languages that allow unprototyped functions (Eg: Objective C).
16635 For all other languages, assume that functions are always
16636 prototyped. */
16637 if (cu->language != language_c
16638 && cu->language != language_objc
16639 && cu->language != language_opencl)
16640 return 1;
16641
16642 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16643 prototyped and unprototyped functions; default to prototyped,
16644 since that is more common in modern code (and RealView warns
16645 about unprototyped functions). */
16646 if (producer_is_realview (cu->producer))
16647 return 1;
16648
16649 return 0;
16650}
16651
c906108c
SS
16652/* Handle DIES due to C code like:
16653
16654 struct foo
c5aa993b
JM
16655 {
16656 int (*funcp)(int a, long l);
16657 int b;
16658 };
c906108c 16659
0963b4bd 16660 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16661
f792889a 16662static struct type *
e7c27a73 16663read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16664{
518817b3 16665 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16666 struct type *type; /* Type that this function returns. */
16667 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16668 struct attribute *attr;
16669
e7c27a73 16670 type = die_type (die, cu);
7e314c57
JK
16671
16672 /* The die_type call above may have already set the type for this DIE. */
16673 ftype = get_die_type (die, cu);
16674 if (ftype)
16675 return ftype;
16676
0c8b41f1 16677 ftype = lookup_function_type (type);
c906108c 16678
4d804846 16679 if (prototyped_function_p (die, cu))
a6c727b2 16680 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16681
c055b101
CV
16682 /* Store the calling convention in the type if it's available in
16683 the subroutine die. Otherwise set the calling convention to
16684 the default value DW_CC_normal. */
16685 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16686 if (attr != nullptr
16687 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16688 TYPE_CALLING_CONVENTION (ftype)
16689 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16690 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16691 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16692 else
16693 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16694
743649fd
MW
16695 /* Record whether the function returns normally to its caller or not
16696 if the DWARF producer set that information. */
16697 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16698 if (attr && (DW_UNSND (attr) != 0))
16699 TYPE_NO_RETURN (ftype) = 1;
16700
76c10ea2
GM
16701 /* We need to add the subroutine type to the die immediately so
16702 we don't infinitely recurse when dealing with parameters
0963b4bd 16703 declared as the same subroutine type. */
76c10ea2 16704 set_die_type (die, ftype, cu);
6e70227d 16705
639d11d3 16706 if (die->child != NULL)
c906108c 16707 {
bb5ed363 16708 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16709 struct die_info *child_die;
8072405b 16710 int nparams, iparams;
c906108c
SS
16711
16712 /* Count the number of parameters.
16713 FIXME: GDB currently ignores vararg functions, but knows about
16714 vararg member functions. */
8072405b 16715 nparams = 0;
639d11d3 16716 child_die = die->child;
c906108c
SS
16717 while (child_die && child_die->tag)
16718 {
16719 if (child_die->tag == DW_TAG_formal_parameter)
16720 nparams++;
16721 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16722 TYPE_VARARGS (ftype) = 1;
c906108c
SS
16723 child_die = sibling_die (child_die);
16724 }
16725
16726 /* Allocate storage for parameters and fill them in. */
16727 TYPE_NFIELDS (ftype) = nparams;
16728 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16729 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16730
8072405b
JK
16731 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16732 even if we error out during the parameters reading below. */
16733 for (iparams = 0; iparams < nparams; iparams++)
16734 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16735
16736 iparams = 0;
639d11d3 16737 child_die = die->child;
c906108c
SS
16738 while (child_die && child_die->tag)
16739 {
16740 if (child_die->tag == DW_TAG_formal_parameter)
16741 {
3ce3b1ba
PA
16742 struct type *arg_type;
16743
16744 /* DWARF version 2 has no clean way to discern C++
16745 static and non-static member functions. G++ helps
16746 GDB by marking the first parameter for non-static
16747 member functions (which is the this pointer) as
16748 artificial. We pass this information to
16749 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16750
16751 DWARF version 3 added DW_AT_object_pointer, which GCC
16752 4.5 does not yet generate. */
e142c38c 16753 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16754 if (attr != nullptr)
c906108c
SS
16755 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16756 else
9c37b5ae 16757 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16758 arg_type = die_type (child_die, cu);
16759
16760 /* RealView does not mark THIS as const, which the testsuite
16761 expects. GCC marks THIS as const in method definitions,
16762 but not in the class specifications (GCC PR 43053). */
16763 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16764 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16765 {
16766 int is_this = 0;
16767 struct dwarf2_cu *arg_cu = cu;
16768 const char *name = dwarf2_name (child_die, cu);
16769
16770 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16771 if (attr != nullptr)
3ce3b1ba
PA
16772 {
16773 /* If the compiler emits this, use it. */
16774 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16775 is_this = 1;
16776 }
16777 else if (name && strcmp (name, "this") == 0)
16778 /* Function definitions will have the argument names. */
16779 is_this = 1;
16780 else if (name == NULL && iparams == 0)
16781 /* Declarations may not have the names, so like
16782 elsewhere in GDB, assume an artificial first
16783 argument is "this". */
16784 is_this = 1;
16785
16786 if (is_this)
16787 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16788 arg_type, 0);
16789 }
16790
16791 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16792 iparams++;
16793 }
16794 child_die = sibling_die (child_die);
16795 }
16796 }
16797
76c10ea2 16798 return ftype;
c906108c
SS
16799}
16800
f792889a 16801static struct type *
e7c27a73 16802read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16803{
518817b3 16804 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16805 const char *name = NULL;
3c8e0968 16806 struct type *this_type, *target_type;
c906108c 16807
94af9270 16808 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16809 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16810 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16811 set_die_type (die, this_type, cu);
3c8e0968
DE
16812 target_type = die_type (die, cu);
16813 if (target_type != this_type)
16814 TYPE_TARGET_TYPE (this_type) = target_type;
16815 else
16816 {
16817 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16818 spec and cause infinite loops in GDB. */
b98664d3 16819 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16820 "- DIE at %s [in module %s]"),
16821 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16822 TYPE_TARGET_TYPE (this_type) = NULL;
16823 }
f792889a 16824 return this_type;
c906108c
SS
16825}
16826
9b790ce7
UW
16827/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16828 (which may be different from NAME) to the architecture back-end to allow
16829 it to guess the correct format if necessary. */
16830
16831static struct type *
16832dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16833 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16834{
16835 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16836 const struct floatformat **format;
16837 struct type *type;
16838
16839 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16840 if (format)
103a685e 16841 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16842 else
77b7c781 16843 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16844
16845 return type;
16846}
16847
eb77c9df
AB
16848/* Allocate an integer type of size BITS and name NAME. */
16849
16850static struct type *
16851dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16852 int bits, int unsigned_p, const char *name)
16853{
16854 struct type *type;
16855
16856 /* Versions of Intel's C Compiler generate an integer type called "void"
16857 instead of using DW_TAG_unspecified_type. This has been seen on
16858 at least versions 14, 17, and 18. */
35ee2dc2
AB
16859 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16860 && strcmp (name, "void") == 0)
eb77c9df
AB
16861 type = objfile_type (objfile)->builtin_void;
16862 else
16863 type = init_integer_type (objfile, bits, unsigned_p, name);
16864
16865 return type;
16866}
16867
8bdc1658
AB
16868/* Initialise and return a floating point type of size BITS suitable for
16869 use as a component of a complex number. The NAME_HINT is passed through
16870 when initialising the floating point type and is the name of the complex
16871 type.
16872
16873 As DWARF doesn't currently provide an explicit name for the components
16874 of a complex number, but it can be helpful to have these components
16875 named, we try to select a suitable name based on the size of the
16876 component. */
16877static struct type *
16878dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16879 struct objfile *objfile,
103a685e
TT
16880 int bits, const char *name_hint,
16881 enum bfd_endian byte_order)
8bdc1658
AB
16882{
16883 gdbarch *gdbarch = get_objfile_arch (objfile);
16884 struct type *tt = nullptr;
16885
35add35e
AB
16886 /* Try to find a suitable floating point builtin type of size BITS.
16887 We're going to use the name of this type as the name for the complex
16888 target type that we are about to create. */
1db455a7 16889 switch (cu->language)
8bdc1658 16890 {
1db455a7
AB
16891 case language_fortran:
16892 switch (bits)
16893 {
16894 case 32:
16895 tt = builtin_f_type (gdbarch)->builtin_real;
16896 break;
16897 case 64:
16898 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16899 break;
16900 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16901 case 128:
16902 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16903 break;
16904 }
8bdc1658 16905 break;
1db455a7
AB
16906 default:
16907 switch (bits)
16908 {
16909 case 32:
16910 tt = builtin_type (gdbarch)->builtin_float;
16911 break;
16912 case 64:
16913 tt = builtin_type (gdbarch)->builtin_double;
16914 break;
16915 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16916 case 128:
16917 tt = builtin_type (gdbarch)->builtin_long_double;
16918 break;
16919 }
8bdc1658
AB
16920 break;
16921 }
16922
35add35e
AB
16923 /* If the type we found doesn't match the size we were looking for, then
16924 pretend we didn't find a type at all, the complex target type we
16925 create will then be nameless. */
a12e5744 16926 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16927 tt = nullptr;
16928
8bdc1658 16929 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16930 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16931}
16932
c906108c
SS
16933/* Find a representation of a given base type and install
16934 it in the TYPE field of the die. */
16935
f792889a 16936static struct type *
e7c27a73 16937read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16938{
518817b3 16939 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16940 struct type *type;
16941 struct attribute *attr;
19f392bc 16942 int encoding = 0, bits = 0;
15d034d0 16943 const char *name;
34877895 16944 gdbarch *arch;
c906108c 16945
e142c38c 16946 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16947 if (attr != nullptr)
34877895 16948 encoding = DW_UNSND (attr);
e142c38c 16949 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16950 if (attr != nullptr)
34877895 16951 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16952 name = dwarf2_name (die, cu);
6ccb9162 16953 if (!name)
34877895 16954 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16955
16956 arch = get_objfile_arch (objfile);
16957 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16958
34877895
PJ
16959 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16960 if (attr)
103a685e
TT
16961 {
16962 int endianity = DW_UNSND (attr);
16963
16964 switch (endianity)
16965 {
16966 case DW_END_big:
16967 byte_order = BFD_ENDIAN_BIG;
16968 break;
16969 case DW_END_little:
16970 byte_order = BFD_ENDIAN_LITTLE;
16971 break;
16972 default:
16973 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16974 break;
16975 }
16976 }
6ccb9162
UW
16977
16978 switch (encoding)
c906108c 16979 {
6ccb9162
UW
16980 case DW_ATE_address:
16981 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16982 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16983 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16984 break;
16985 case DW_ATE_boolean:
19f392bc 16986 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16987 break;
16988 case DW_ATE_complex_float:
103a685e
TT
16989 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16990 byte_order);
19f392bc 16991 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16992 break;
16993 case DW_ATE_decimal_float:
19f392bc 16994 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16995 break;
16996 case DW_ATE_float:
103a685e 16997 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16998 break;
16999 case DW_ATE_signed:
eb77c9df 17000 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17001 break;
17002 case DW_ATE_unsigned:
3b2b8fea
TT
17003 if (cu->language == language_fortran
17004 && name
61012eef 17005 && startswith (name, "character("))
19f392bc
UW
17006 type = init_character_type (objfile, bits, 1, name);
17007 else
eb77c9df 17008 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17009 break;
17010 case DW_ATE_signed_char:
6e70227d 17011 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17012 || cu->language == language_pascal
17013 || cu->language == language_fortran)
19f392bc
UW
17014 type = init_character_type (objfile, bits, 0, name);
17015 else
eb77c9df 17016 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17017 break;
17018 case DW_ATE_unsigned_char:
868a0084 17019 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17020 || cu->language == language_pascal
c44af4eb
TT
17021 || cu->language == language_fortran
17022 || cu->language == language_rust)
19f392bc
UW
17023 type = init_character_type (objfile, bits, 1, name);
17024 else
eb77c9df 17025 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17026 break;
75079b2b 17027 case DW_ATE_UTF:
53e710ac 17028 {
53e710ac
PA
17029 if (bits == 16)
17030 type = builtin_type (arch)->builtin_char16;
17031 else if (bits == 32)
17032 type = builtin_type (arch)->builtin_char32;
17033 else
17034 {
b98664d3 17035 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17036 bits);
eb77c9df 17037 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17038 }
17039 return set_die_type (die, type, cu);
17040 }
75079b2b
TT
17041 break;
17042
6ccb9162 17043 default:
b98664d3 17044 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17045 dwarf_type_encoding_name (encoding));
77b7c781 17046 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17047 break;
c906108c 17048 }
6ccb9162 17049
0114d602 17050 if (name && strcmp (name, "char") == 0)
876cecd0 17051 TYPE_NOSIGN (type) = 1;
0114d602 17052
2b4424c3
TT
17053 maybe_set_alignment (cu, die, type);
17054
103a685e 17055 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17056
f792889a 17057 return set_die_type (die, type, cu);
c906108c
SS
17058}
17059
80180f79
SA
17060/* Parse dwarf attribute if it's a block, reference or constant and put the
17061 resulting value of the attribute into struct bound_prop.
17062 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17063
17064static int
17065attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17066 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17067 struct type *default_type)
80180f79
SA
17068{
17069 struct dwarf2_property_baton *baton;
518817b3
SM
17070 struct obstack *obstack
17071 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17072
9a49df9d
AB
17073 gdb_assert (default_type != NULL);
17074
80180f79
SA
17075 if (attr == NULL || prop == NULL)
17076 return 0;
17077
4fc6c0d5 17078 if (attr->form_is_block ())
80180f79 17079 {
8d749320 17080 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17081 baton->property_type = default_type;
80180f79
SA
17082 baton->locexpr.per_cu = cu->per_cu;
17083 baton->locexpr.size = DW_BLOCK (attr)->size;
17084 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17085 switch (attr->name)
17086 {
17087 case DW_AT_string_length:
17088 baton->locexpr.is_reference = true;
17089 break;
17090 default:
17091 baton->locexpr.is_reference = false;
17092 break;
17093 }
80180f79
SA
17094 prop->data.baton = baton;
17095 prop->kind = PROP_LOCEXPR;
17096 gdb_assert (prop->data.baton != NULL);
17097 }
cd6c91b4 17098 else if (attr->form_is_ref ())
80180f79
SA
17099 {
17100 struct dwarf2_cu *target_cu = cu;
17101 struct die_info *target_die;
17102 struct attribute *target_attr;
17103
17104 target_die = follow_die_ref (die, attr, &target_cu);
17105 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17106 if (target_attr == NULL)
17107 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17108 target_cu);
80180f79
SA
17109 if (target_attr == NULL)
17110 return 0;
17111
df25ebbd 17112 switch (target_attr->name)
80180f79 17113 {
df25ebbd 17114 case DW_AT_location:
cd6c91b4 17115 if (target_attr->form_is_section_offset ())
df25ebbd 17116 {
8d749320 17117 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17118 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17119 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17120 prop->data.baton = baton;
17121 prop->kind = PROP_LOCLIST;
17122 gdb_assert (prop->data.baton != NULL);
17123 }
4fc6c0d5 17124 else if (target_attr->form_is_block ())
df25ebbd 17125 {
8d749320 17126 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17127 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17128 baton->locexpr.per_cu = cu->per_cu;
17129 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17130 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17131 baton->locexpr.is_reference = true;
df25ebbd
JB
17132 prop->data.baton = baton;
17133 prop->kind = PROP_LOCEXPR;
17134 gdb_assert (prop->data.baton != NULL);
17135 }
17136 else
17137 {
17138 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17139 "dynamic property");
17140 return 0;
17141 }
17142 break;
17143 case DW_AT_data_member_location:
17144 {
17145 LONGEST offset;
17146
17147 if (!handle_data_member_location (target_die, target_cu,
17148 &offset))
17149 return 0;
17150
8d749320 17151 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17152 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17153 target_cu);
df25ebbd
JB
17154 baton->offset_info.offset = offset;
17155 baton->offset_info.type = die_type (target_die, target_cu);
17156 prop->data.baton = baton;
17157 prop->kind = PROP_ADDR_OFFSET;
17158 break;
17159 }
80180f79
SA
17160 }
17161 }
cd6c91b4 17162 else if (attr->form_is_constant ())
80180f79
SA
17163 {
17164 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17165 prop->kind = PROP_CONST;
17166 }
17167 else
17168 {
17169 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17170 dwarf2_name (die, cu));
17171 return 0;
17172 }
17173
17174 return 1;
17175}
17176
09ba997f 17177/* See read.h. */
9a49df9d 17178
09ba997f
TT
17179struct type *
17180dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17181{
09ba997f 17182 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17183 struct type *int_type;
17184
17185 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17186#define TRY_TYPE(F) \
17187 int_type = (unsigned_p \
17188 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17189 : objfile_type (objfile)->builtin_ ## F); \
17190 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17191 return int_type
17192
17193 TRY_TYPE (char);
17194 TRY_TYPE (short);
17195 TRY_TYPE (int);
17196 TRY_TYPE (long);
17197 TRY_TYPE (long_long);
17198
17199#undef TRY_TYPE
17200
17201 gdb_assert_not_reached ("unable to find suitable integer type");
17202}
17203
09ba997f 17204/* See read.h. */
11a8b164 17205
09ba997f
TT
17206struct type *
17207dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17208{
09ba997f
TT
17209 int addr_size = this->addr_size ();
17210 return int_type (addr_size, unsigned_p);
11a8b164
AB
17211}
17212
b86352cf
AB
17213/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17214 present (which is valid) then compute the default type based on the
17215 compilation units address size. */
17216
17217static struct type *
17218read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17219{
17220 struct type *index_type = die_type (die, cu);
17221
17222 /* Dwarf-2 specifications explicitly allows to create subrange types
17223 without specifying a base type.
17224 In that case, the base type must be set to the type of
17225 the lower bound, upper bound or count, in that order, if any of these
17226 three attributes references an object that has a type.
17227 If no base type is found, the Dwarf-2 specifications say that
17228 a signed integer type of size equal to the size of an address should
17229 be used.
17230 For the following C code: `extern char gdb_int [];'
17231 GCC produces an empty range DIE.
17232 FIXME: muller/2010-05-28: Possible references to object for low bound,
17233 high bound or count are not yet handled by this code. */
17234 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17235 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17236
17237 return index_type;
17238}
17239
a02abb62
JB
17240/* Read the given DW_AT_subrange DIE. */
17241
f792889a 17242static struct type *
a02abb62
JB
17243read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17244{
4c9ad8c2 17245 struct type *base_type, *orig_base_type;
a02abb62
JB
17246 struct type *range_type;
17247 struct attribute *attr;
729efb13 17248 struct dynamic_prop low, high;
4fae6e18 17249 int low_default_is_valid;
c451ebe5 17250 int high_bound_is_count = 0;
15d034d0 17251 const char *name;
d359392f 17252 ULONGEST negative_mask;
e77813c8 17253
b86352cf
AB
17254 orig_base_type = read_subrange_index_type (die, cu);
17255
4c9ad8c2
TT
17256 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17257 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17258 creating the range type, but we use the result of check_typedef
17259 when examining properties of the type. */
17260 base_type = check_typedef (orig_base_type);
a02abb62 17261
7e314c57
JK
17262 /* The die_type call above may have already set the type for this DIE. */
17263 range_type = get_die_type (die, cu);
17264 if (range_type)
17265 return range_type;
17266
729efb13
SA
17267 low.kind = PROP_CONST;
17268 high.kind = PROP_CONST;
17269 high.data.const_val = 0;
17270
4fae6e18
JK
17271 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17272 omitting DW_AT_lower_bound. */
17273 switch (cu->language)
6e70227d 17274 {
4fae6e18
JK
17275 case language_c:
17276 case language_cplus:
729efb13 17277 low.data.const_val = 0;
4fae6e18
JK
17278 low_default_is_valid = 1;
17279 break;
17280 case language_fortran:
729efb13 17281 low.data.const_val = 1;
4fae6e18
JK
17282 low_default_is_valid = 1;
17283 break;
17284 case language_d:
4fae6e18 17285 case language_objc:
c44af4eb 17286 case language_rust:
729efb13 17287 low.data.const_val = 0;
4fae6e18
JK
17288 low_default_is_valid = (cu->header.version >= 4);
17289 break;
17290 case language_ada:
17291 case language_m2:
17292 case language_pascal:
729efb13 17293 low.data.const_val = 1;
4fae6e18
JK
17294 low_default_is_valid = (cu->header.version >= 4);
17295 break;
17296 default:
729efb13 17297 low.data.const_val = 0;
4fae6e18
JK
17298 low_default_is_valid = 0;
17299 break;
a02abb62
JB
17300 }
17301
e142c38c 17302 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17303 if (attr != nullptr)
9a49df9d 17304 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17305 else if (!low_default_is_valid)
b98664d3 17306 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17307 "- DIE at %s [in module %s]"),
17308 sect_offset_str (die->sect_off),
518817b3 17309 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17310
506f5c41
TV
17311 struct attribute *attr_ub, *attr_count;
17312 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17313 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17314 {
506f5c41 17315 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17316 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17317 {
c451ebe5
SA
17318 /* If bounds are constant do the final calculation here. */
17319 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17320 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17321 else
17322 high_bound_is_count = 1;
c2ff108b 17323 }
506f5c41
TV
17324 else
17325 {
17326 if (attr_ub != NULL)
17327 complaint (_("Unresolved DW_AT_upper_bound "
17328 "- DIE at %s [in module %s]"),
17329 sect_offset_str (die->sect_off),
17330 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17331 if (attr_count != NULL)
17332 complaint (_("Unresolved DW_AT_count "
17333 "- DIE at %s [in module %s]"),
17334 sect_offset_str (die->sect_off),
17335 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17336 }
e77813c8 17337 }
a02abb62 17338
4e962e74
TT
17339 LONGEST bias = 0;
17340 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17341 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17342 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17343
dbb9c2b1
JB
17344 /* Normally, the DWARF producers are expected to use a signed
17345 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17346 But this is unfortunately not always the case, as witnessed
17347 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17348 is used instead. To work around that ambiguity, we treat
17349 the bounds as signed, and thus sign-extend their values, when
17350 the base type is signed. */
6e70227d 17351 negative_mask =
d359392f 17352 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17353 if (low.kind == PROP_CONST
17354 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17355 low.data.const_val |= negative_mask;
17356 if (high.kind == PROP_CONST
17357 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17358 high.data.const_val |= negative_mask;
43bbcdc2 17359
5bbd8269
AB
17360 /* Check for bit and byte strides. */
17361 struct dynamic_prop byte_stride_prop;
17362 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17363 if (attr_byte_stride != nullptr)
17364 {
09ba997f 17365 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17366 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17367 prop_type);
17368 }
17369
17370 struct dynamic_prop bit_stride_prop;
17371 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17372 if (attr_bit_stride != nullptr)
17373 {
17374 /* It only makes sense to have either a bit or byte stride. */
17375 if (attr_byte_stride != nullptr)
17376 {
17377 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17378 "- DIE at %s [in module %s]"),
17379 sect_offset_str (die->sect_off),
17380 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17381 attr_bit_stride = nullptr;
17382 }
17383 else
17384 {
09ba997f 17385 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17386 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17387 prop_type);
17388 }
17389 }
17390
17391 if (attr_byte_stride != nullptr
17392 || attr_bit_stride != nullptr)
17393 {
17394 bool byte_stride_p = (attr_byte_stride != nullptr);
17395 struct dynamic_prop *stride
17396 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17397
17398 range_type
17399 = create_range_type_with_stride (NULL, orig_base_type, &low,
17400 &high, bias, stride, byte_stride_p);
17401 }
17402 else
17403 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17404
c451ebe5
SA
17405 if (high_bound_is_count)
17406 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17407
c2ff108b
JK
17408 /* Ada expects an empty array on no boundary attributes. */
17409 if (attr == NULL && cu->language != language_ada)
729efb13 17410 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17411
39cbfefa
DJ
17412 name = dwarf2_name (die, cu);
17413 if (name)
17414 TYPE_NAME (range_type) = name;
6e70227d 17415
e142c38c 17416 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17417 if (attr != nullptr)
a02abb62
JB
17418 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17419
2b4424c3
TT
17420 maybe_set_alignment (cu, die, range_type);
17421
7e314c57
JK
17422 set_die_type (die, range_type, cu);
17423
17424 /* set_die_type should be already done. */
b4ba55a1
JB
17425 set_descriptive_type (range_type, die, cu);
17426
7e314c57 17427 return range_type;
a02abb62 17428}
6e70227d 17429
f792889a 17430static struct type *
81a17f79
JB
17431read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17432{
17433 struct type *type;
81a17f79 17434
518817b3
SM
17435 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17436 NULL);
0114d602 17437 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17438
74a2f8ff 17439 /* In Ada, an unspecified type is typically used when the description
85102364 17440 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17441 such a type, we treat it as a stub, and try to resolve it later on,
17442 when needed. */
17443 if (cu->language == language_ada)
17444 TYPE_STUB (type) = 1;
17445
f792889a 17446 return set_die_type (die, type, cu);
81a17f79 17447}
a02abb62 17448
639d11d3
DC
17449/* Read a single die and all its descendents. Set the die's sibling
17450 field to NULL; set other fields in the die correctly, and set all
17451 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17452 location of the info_ptr after reading all of those dies. PARENT
17453 is the parent of the die in question. */
17454
17455static struct die_info *
dee91e82 17456read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17457 const gdb_byte *info_ptr,
17458 const gdb_byte **new_info_ptr,
dee91e82 17459 struct die_info *parent)
639d11d3
DC
17460{
17461 struct die_info *die;
d521ce57 17462 const gdb_byte *cur_ptr;
639d11d3 17463
3e225074 17464 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17465 if (die == NULL)
17466 {
17467 *new_info_ptr = cur_ptr;
17468 return NULL;
17469 }
93311388 17470 store_in_ref_table (die, reader->cu);
639d11d3 17471
3e225074 17472 if (die->has_children)
bf6af496 17473 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17474 else
17475 {
17476 die->child = NULL;
17477 *new_info_ptr = cur_ptr;
17478 }
17479
17480 die->sibling = NULL;
17481 die->parent = parent;
17482 return die;
17483}
17484
17485/* Read a die, all of its descendents, and all of its siblings; set
17486 all of the fields of all of the dies correctly. Arguments are as
17487 in read_die_and_children. */
17488
17489static struct die_info *
bf6af496 17490read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17491 const gdb_byte *info_ptr,
17492 const gdb_byte **new_info_ptr,
bf6af496 17493 struct die_info *parent)
639d11d3
DC
17494{
17495 struct die_info *first_die, *last_sibling;
d521ce57 17496 const gdb_byte *cur_ptr;
639d11d3 17497
c906108c 17498 cur_ptr = info_ptr;
639d11d3
DC
17499 first_die = last_sibling = NULL;
17500
17501 while (1)
c906108c 17502 {
639d11d3 17503 struct die_info *die
dee91e82 17504 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17505
1d325ec1 17506 if (die == NULL)
c906108c 17507 {
639d11d3
DC
17508 *new_info_ptr = cur_ptr;
17509 return first_die;
c906108c 17510 }
1d325ec1
DJ
17511
17512 if (!first_die)
17513 first_die = die;
c906108c 17514 else
1d325ec1
DJ
17515 last_sibling->sibling = die;
17516
17517 last_sibling = die;
c906108c 17518 }
c906108c
SS
17519}
17520
bf6af496
DE
17521/* Read a die, all of its descendents, and all of its siblings; set
17522 all of the fields of all of the dies correctly. Arguments are as
17523 in read_die_and_children.
17524 This the main entry point for reading a DIE and all its children. */
17525
17526static struct die_info *
17527read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17528 const gdb_byte *info_ptr,
17529 const gdb_byte **new_info_ptr,
bf6af496
DE
17530 struct die_info *parent)
17531{
17532 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17533 new_info_ptr, parent);
17534
b4f54984 17535 if (dwarf_die_debug)
bf6af496
DE
17536 {
17537 fprintf_unfiltered (gdb_stdlog,
17538 "Read die from %s@0x%x of %s:\n",
96b79293 17539 reader->die_section->get_name (),
bf6af496
DE
17540 (unsigned) (info_ptr - reader->die_section->buffer),
17541 bfd_get_filename (reader->abfd));
b4f54984 17542 dump_die (die, dwarf_die_debug);
bf6af496
DE
17543 }
17544
17545 return die;
17546}
17547
3019eac3
DE
17548/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17549 attributes.
17550 The caller is responsible for filling in the extra attributes
17551 and updating (*DIEP)->num_attrs.
17552 Set DIEP to point to a newly allocated die with its information,
3e225074 17553 except for its child, sibling, and parent fields. */
93311388 17554
d521ce57 17555static const gdb_byte *
3019eac3 17556read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17557 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17558 int num_extra_attrs)
93311388 17559{
b64f50a1 17560 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17561 struct abbrev_info *abbrev;
17562 struct die_info *die;
17563 struct dwarf2_cu *cu = reader->cu;
17564 bfd *abfd = reader->abfd;
17565
9c541725 17566 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17567 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17568 info_ptr += bytes_read;
17569 if (!abbrev_number)
17570 {
17571 *diep = NULL;
93311388
DE
17572 return info_ptr;
17573 }
17574
685af9cd 17575 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17576 if (!abbrev)
348e048f
DE
17577 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17578 abbrev_number,
17579 bfd_get_filename (abfd));
17580
3019eac3 17581 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17582 die->sect_off = sect_off;
93311388
DE
17583 die->tag = abbrev->tag;
17584 die->abbrev = abbrev_number;
3e225074 17585 die->has_children = abbrev->has_children;
93311388 17586
3019eac3
DE
17587 /* Make the result usable.
17588 The caller needs to update num_attrs after adding the extra
17589 attributes. */
93311388
DE
17590 die->num_attrs = abbrev->num_attrs;
17591
18a8505e 17592 std::vector<int> indexes_that_need_reprocess;
93311388 17593 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17594 {
17595 bool need_reprocess;
17596 info_ptr =
17597 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17598 info_ptr, &need_reprocess);
17599 if (need_reprocess)
17600 indexes_that_need_reprocess.push_back (i);
17601 }
17602
17603 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17604 if (attr != nullptr)
17605 cu->str_offsets_base = DW_UNSND (attr);
93311388 17606
18a8505e
AT
17607 auto maybe_addr_base = lookup_addr_base(die);
17608 if (maybe_addr_base.has_value ())
17609 cu->addr_base = *maybe_addr_base;
17610 for (int index : indexes_that_need_reprocess)
17611 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17612 *diep = die;
93311388
DE
17613 return info_ptr;
17614}
17615
3019eac3
DE
17616/* Read a die and all its attributes.
17617 Set DIEP to point to a newly allocated die with its information,
3e225074 17618 except for its child, sibling, and parent fields. */
3019eac3 17619
d521ce57 17620static const gdb_byte *
3019eac3 17621read_full_die (const struct die_reader_specs *reader,
3e225074 17622 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17623{
d521ce57 17624 const gdb_byte *result;
bf6af496 17625
3e225074 17626 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17627
b4f54984 17628 if (dwarf_die_debug)
bf6af496
DE
17629 {
17630 fprintf_unfiltered (gdb_stdlog,
17631 "Read die from %s@0x%x of %s:\n",
96b79293 17632 reader->die_section->get_name (),
bf6af496
DE
17633 (unsigned) (info_ptr - reader->die_section->buffer),
17634 bfd_get_filename (reader->abfd));
b4f54984 17635 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17636 }
17637
17638 return result;
3019eac3 17639}
433df2d4 17640\f
c906108c 17641
72bf9492
DJ
17642/* Returns nonzero if TAG represents a type that we might generate a partial
17643 symbol for. */
17644
17645static int
17646is_type_tag_for_partial (int tag)
17647{
17648 switch (tag)
17649 {
17650#if 0
17651 /* Some types that would be reasonable to generate partial symbols for,
17652 that we don't at present. */
17653 case DW_TAG_array_type:
17654 case DW_TAG_file_type:
17655 case DW_TAG_ptr_to_member_type:
17656 case DW_TAG_set_type:
17657 case DW_TAG_string_type:
17658 case DW_TAG_subroutine_type:
17659#endif
17660 case DW_TAG_base_type:
17661 case DW_TAG_class_type:
680b30c7 17662 case DW_TAG_interface_type:
72bf9492
DJ
17663 case DW_TAG_enumeration_type:
17664 case DW_TAG_structure_type:
17665 case DW_TAG_subrange_type:
17666 case DW_TAG_typedef:
17667 case DW_TAG_union_type:
17668 return 1;
17669 default:
17670 return 0;
17671 }
17672}
17673
17674/* Load all DIEs that are interesting for partial symbols into memory. */
17675
17676static struct partial_die_info *
dee91e82 17677load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17678 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17679{
dee91e82 17680 struct dwarf2_cu *cu = reader->cu;
518817b3 17681 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17682 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17683 unsigned int bytes_read;
5afb4e99 17684 unsigned int load_all = 0;
72bf9492
DJ
17685 int nesting_level = 1;
17686
17687 parent_die = NULL;
17688 last_die = NULL;
17689
7adf1e79
DE
17690 gdb_assert (cu->per_cu != NULL);
17691 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17692 load_all = 1;
17693
72bf9492
DJ
17694 cu->partial_dies
17695 = htab_create_alloc_ex (cu->header.length / 12,
17696 partial_die_hash,
17697 partial_die_eq,
17698 NULL,
17699 &cu->comp_unit_obstack,
17700 hashtab_obstack_allocate,
17701 dummy_obstack_deallocate);
17702
72bf9492
DJ
17703 while (1)
17704 {
685af9cd 17705 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17706
17707 /* A NULL abbrev means the end of a series of children. */
17708 if (abbrev == NULL)
17709 {
17710 if (--nesting_level == 0)
cd9983dd
YQ
17711 return first_die;
17712
72bf9492
DJ
17713 info_ptr += bytes_read;
17714 last_die = parent_die;
17715 parent_die = parent_die->die_parent;
17716 continue;
17717 }
17718
98bfdba5
PA
17719 /* Check for template arguments. We never save these; if
17720 they're seen, we just mark the parent, and go on our way. */
17721 if (parent_die != NULL
17722 && cu->language == language_cplus
17723 && (abbrev->tag == DW_TAG_template_type_param
17724 || abbrev->tag == DW_TAG_template_value_param))
17725 {
17726 parent_die->has_template_arguments = 1;
17727
17728 if (!load_all)
17729 {
17730 /* We don't need a partial DIE for the template argument. */
dee91e82 17731 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17732 continue;
17733 }
17734 }
17735
0d99eb77 17736 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17737 Skip their other children. */
17738 if (!load_all
17739 && cu->language == language_cplus
17740 && parent_die != NULL
17741 && parent_die->tag == DW_TAG_subprogram)
17742 {
dee91e82 17743 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17744 continue;
17745 }
17746
5afb4e99
DJ
17747 /* Check whether this DIE is interesting enough to save. Normally
17748 we would not be interested in members here, but there may be
17749 later variables referencing them via DW_AT_specification (for
17750 static members). */
17751 if (!load_all
17752 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17753 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17754 && abbrev->tag != DW_TAG_enumerator
17755 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17756 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17757 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17758 && abbrev->tag != DW_TAG_variable
5afb4e99 17759 && abbrev->tag != DW_TAG_namespace
f55ee35c 17760 && abbrev->tag != DW_TAG_module
95554aad 17761 && abbrev->tag != DW_TAG_member
74921315
KS
17762 && abbrev->tag != DW_TAG_imported_unit
17763 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17764 {
17765 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17766 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17767 continue;
17768 }
17769
6f06d47b
YQ
17770 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17771 abbrev);
cd9983dd 17772
48fbe735 17773 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17774
17775 /* This two-pass algorithm for processing partial symbols has a
17776 high cost in cache pressure. Thus, handle some simple cases
17777 here which cover the majority of C partial symbols. DIEs
17778 which neither have specification tags in them, nor could have
17779 specification tags elsewhere pointing at them, can simply be
17780 processed and discarded.
17781
17782 This segment is also optional; scan_partial_symbols and
17783 add_partial_symbol will handle these DIEs if we chain
17784 them in normally. When compilers which do not emit large
17785 quantities of duplicate debug information are more common,
17786 this code can probably be removed. */
17787
17788 /* Any complete simple types at the top level (pretty much all
17789 of them, for a language without namespaces), can be processed
17790 directly. */
17791 if (parent_die == NULL
cd9983dd
YQ
17792 && pdi.has_specification == 0
17793 && pdi.is_declaration == 0
17794 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17795 || pdi.tag == DW_TAG_base_type
17796 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17797 {
cd9983dd 17798 if (building_psymtab && pdi.name != NULL)
31edb802 17799 add_psymbol_to_list (pdi.name, false,
79748972 17800 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17801 psymbol_placement::STATIC,
1762568f 17802 0, cu->language, objfile);
cd9983dd 17803 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17804 continue;
17805 }
17806
d8228535
JK
17807 /* The exception for DW_TAG_typedef with has_children above is
17808 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17809 type_name_or_error will error on such types later.
d8228535
JK
17810
17811 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17812 it could not find the child DIEs referenced later, this is checked
17813 above. In correct DWARF DW_TAG_typedef should have no children. */
17814
cd9983dd 17815 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17816 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17817 "- DIE at %s [in module %s]"),
cd9983dd 17818 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17819
72bf9492
DJ
17820 /* If we're at the second level, and we're an enumerator, and
17821 our parent has no specification (meaning possibly lives in a
17822 namespace elsewhere), then we can add the partial symbol now
17823 instead of queueing it. */
cd9983dd 17824 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17825 && parent_die != NULL
17826 && parent_die->die_parent == NULL
17827 && parent_die->tag == DW_TAG_enumeration_type
17828 && parent_die->has_specification == 0)
17829 {
cd9983dd 17830 if (pdi.name == NULL)
b98664d3 17831 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17832 else if (building_psymtab)
31edb802 17833 add_psymbol_to_list (pdi.name, false,
79748972 17834 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17835 cu->language == language_cplus
75aedd27
TT
17836 ? psymbol_placement::GLOBAL
17837 : psymbol_placement::STATIC,
1762568f 17838 0, cu->language, objfile);
72bf9492 17839
cd9983dd 17840 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17841 continue;
17842 }
17843
cd9983dd 17844 struct partial_die_info *part_die
6f06d47b 17845 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17846
72bf9492
DJ
17847 /* We'll save this DIE so link it in. */
17848 part_die->die_parent = parent_die;
17849 part_die->die_sibling = NULL;
17850 part_die->die_child = NULL;
17851
17852 if (last_die && last_die == parent_die)
17853 last_die->die_child = part_die;
17854 else if (last_die)
17855 last_die->die_sibling = part_die;
17856
17857 last_die = part_die;
17858
17859 if (first_die == NULL)
17860 first_die = part_die;
17861
17862 /* Maybe add the DIE to the hash table. Not all DIEs that we
17863 find interesting need to be in the hash table, because we
17864 also have the parent/sibling/child chains; only those that we
17865 might refer to by offset later during partial symbol reading.
17866
17867 For now this means things that might have be the target of a
17868 DW_AT_specification, DW_AT_abstract_origin, or
17869 DW_AT_extension. DW_AT_extension will refer only to
17870 namespaces; DW_AT_abstract_origin refers to functions (and
17871 many things under the function DIE, but we do not recurse
17872 into function DIEs during partial symbol reading) and
17873 possibly variables as well; DW_AT_specification refers to
17874 declarations. Declarations ought to have the DW_AT_declaration
17875 flag. It happens that GCC forgets to put it in sometimes, but
17876 only for functions, not for types.
17877
17878 Adding more things than necessary to the hash table is harmless
17879 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17880 wasted time in find_partial_die, when we reread the compilation
17881 unit with load_all_dies set. */
72bf9492 17882
5afb4e99 17883 if (load_all
72929c62 17884 || abbrev->tag == DW_TAG_constant
5afb4e99 17885 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17886 || abbrev->tag == DW_TAG_variable
17887 || abbrev->tag == DW_TAG_namespace
17888 || part_die->is_declaration)
17889 {
17890 void **slot;
17891
17892 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17893 to_underlying (part_die->sect_off),
17894 INSERT);
72bf9492
DJ
17895 *slot = part_die;
17896 }
17897
72bf9492 17898 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17899 we have no reason to follow the children of structures; for other
98bfdba5
PA
17900 languages we have to, so that we can get at method physnames
17901 to infer fully qualified class names, for DW_AT_specification,
17902 and for C++ template arguments. For C++, we also look one level
17903 inside functions to find template arguments (if the name of the
17904 function does not already contain the template arguments).
bc30ff58 17905
0a4b0913
AB
17906 For Ada and Fortran, we need to scan the children of subprograms
17907 and lexical blocks as well because these languages allow the
17908 definition of nested entities that could be interesting for the
17909 debugger, such as nested subprograms for instance. */
72bf9492 17910 if (last_die->has_children
5afb4e99
DJ
17911 && (load_all
17912 || last_die->tag == DW_TAG_namespace
f55ee35c 17913 || last_die->tag == DW_TAG_module
72bf9492 17914 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17915 || (cu->language == language_cplus
17916 && last_die->tag == DW_TAG_subprogram
17917 && (last_die->name == NULL
17918 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17919 || (cu->language != language_c
17920 && (last_die->tag == DW_TAG_class_type
680b30c7 17921 || last_die->tag == DW_TAG_interface_type
72bf9492 17922 || last_die->tag == DW_TAG_structure_type
bc30ff58 17923 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17924 || ((cu->language == language_ada
17925 || cu->language == language_fortran)
bc30ff58
JB
17926 && (last_die->tag == DW_TAG_subprogram
17927 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17928 {
17929 nesting_level++;
17930 parent_die = last_die;
17931 continue;
17932 }
17933
17934 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17935 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17936
17937 /* Back to the top, do it again. */
17938 }
17939}
17940
6f06d47b
YQ
17941partial_die_info::partial_die_info (sect_offset sect_off_,
17942 struct abbrev_info *abbrev)
17943 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17944{
17945}
17946
35cc7ed7
YQ
17947/* Read a minimal amount of information into the minimal die structure.
17948 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17949
48fbe735
YQ
17950const gdb_byte *
17951partial_die_info::read (const struct die_reader_specs *reader,
17952 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17953{
dee91e82 17954 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17955 struct dwarf2_per_objfile *dwarf2_per_objfile
17956 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17957 unsigned int i;
c5aa993b 17958 int has_low_pc_attr = 0;
c906108c 17959 int has_high_pc_attr = 0;
91da1414 17960 int high_pc_relative = 0;
c906108c 17961
18a8505e 17962 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17963 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17964 {
18a8505e
AT
17965 bool need_reprocess;
17966 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17967 info_ptr, &need_reprocess);
17968 /* String and address offsets that need to do the reprocessing have
17969 already been read at this point, so there is no need to wait until
17970 the loop terminates to do the reprocessing. */
17971 if (need_reprocess)
17972 read_attribute_reprocess (reader, &attr_vec[i]);
17973 attribute &attr = attr_vec[i];
c906108c 17974 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17975 partial symbol table. */
c906108c
SS
17976 switch (attr.name)
17977 {
17978 case DW_AT_name:
48fbe735 17979 switch (tag)
71c25dea
TT
17980 {
17981 case DW_TAG_compile_unit:
95554aad 17982 case DW_TAG_partial_unit:
348e048f 17983 case DW_TAG_type_unit:
71c25dea
TT
17984 /* Compilation units have a DW_AT_name that is a filename, not
17985 a source language identifier. */
17986 case DW_TAG_enumeration_type:
17987 case DW_TAG_enumerator:
17988 /* These tags always have simple identifiers already; no need
17989 to canonicalize them. */
48fbe735 17990 name = DW_STRING (&attr);
71c25dea
TT
17991 break;
17992 default:
48fbe735
YQ
17993 {
17994 struct objfile *objfile = dwarf2_per_objfile->objfile;
17995
17996 name
17997 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17998 &objfile->per_bfd->storage_obstack);
17999 }
71c25dea
TT
18000 break;
18001 }
c906108c 18002 break;
31ef98ae 18003 case DW_AT_linkage_name:
c906108c 18004 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18005 /* Note that both forms of linkage name might appear. We
18006 assume they will be the same, and we only store the last
18007 one we see. */
48fbe735 18008 linkage_name = DW_STRING (&attr);
c906108c
SS
18009 break;
18010 case DW_AT_low_pc:
18011 has_low_pc_attr = 1;
cd6c91b4 18012 lowpc = attr.value_as_address ();
c906108c
SS
18013 break;
18014 case DW_AT_high_pc:
18015 has_high_pc_attr = 1;
cd6c91b4
TT
18016 highpc = attr.value_as_address ();
18017 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18018 high_pc_relative = 1;
c906108c
SS
18019 break;
18020 case DW_AT_location:
0963b4bd 18021 /* Support the .debug_loc offsets. */
4fc6c0d5 18022 if (attr.form_is_block ())
8e19ed76 18023 {
48fbe735 18024 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18025 }
cd6c91b4 18026 else if (attr.form_is_section_offset ())
8e19ed76 18027 {
4d3c2250 18028 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18029 }
18030 else
18031 {
4d3c2250
KB
18032 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18033 "partial symbol information");
8e19ed76 18034 }
c906108c 18035 break;
c906108c 18036 case DW_AT_external:
48fbe735 18037 is_external = DW_UNSND (&attr);
c906108c
SS
18038 break;
18039 case DW_AT_declaration:
48fbe735 18040 is_declaration = DW_UNSND (&attr);
c906108c
SS
18041 break;
18042 case DW_AT_type:
48fbe735 18043 has_type = 1;
c906108c
SS
18044 break;
18045 case DW_AT_abstract_origin:
18046 case DW_AT_specification:
72bf9492 18047 case DW_AT_extension:
48fbe735
YQ
18048 has_specification = 1;
18049 spec_offset = dwarf2_get_ref_die_offset (&attr);
18050 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18051 || cu->per_cu->is_dwz);
c906108c
SS
18052 break;
18053 case DW_AT_sibling:
18054 /* Ignore absolute siblings, they might point outside of
18055 the current compile unit. */
18056 if (attr.form == DW_FORM_ref_addr)
b98664d3 18057 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18058 else
b9502d3f 18059 {
48fbe735 18060 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18061 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18062 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18063
18064 if (sibling_ptr < info_ptr)
b98664d3 18065 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18066 else if (sibling_ptr > reader->buffer_end)
18067 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18068 else
48fbe735 18069 sibling = sibling_ptr;
b9502d3f 18070 }
c906108c 18071 break;
fa4028e9 18072 case DW_AT_byte_size:
48fbe735 18073 has_byte_size = 1;
fa4028e9 18074 break;
ff908ebf 18075 case DW_AT_const_value:
48fbe735 18076 has_const_value = 1;
ff908ebf 18077 break;
68511cec
CES
18078 case DW_AT_calling_convention:
18079 /* DWARF doesn't provide a way to identify a program's source-level
18080 entry point. DW_AT_calling_convention attributes are only meant
18081 to describe functions' calling conventions.
18082
18083 However, because it's a necessary piece of information in
0c1b455e
TT
18084 Fortran, and before DWARF 4 DW_CC_program was the only
18085 piece of debugging information whose definition refers to
18086 a 'main program' at all, several compilers marked Fortran
18087 main programs with DW_CC_program --- even when those
18088 functions use the standard calling conventions.
18089
18090 Although DWARF now specifies a way to provide this
18091 information, we support this practice for backward
18092 compatibility. */
68511cec 18093 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18094 && cu->language == language_fortran)
48fbe735 18095 main_subprogram = 1;
68511cec 18096 break;
481860b3
GB
18097 case DW_AT_inline:
18098 if (DW_UNSND (&attr) == DW_INL_inlined
18099 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18100 may_be_inlined = 1;
481860b3 18101 break;
95554aad
TT
18102
18103 case DW_AT_import:
48fbe735 18104 if (tag == DW_TAG_imported_unit)
36586728 18105 {
48fbe735
YQ
18106 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18107 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18108 || cu->per_cu->is_dwz);
18109 }
95554aad
TT
18110 break;
18111
0c1b455e 18112 case DW_AT_main_subprogram:
48fbe735 18113 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18114 break;
18115
05caa1d2
TT
18116 case DW_AT_ranges:
18117 {
18118 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18119 but that requires a full DIE, so instead we just
18120 reimplement it. */
18121 int need_ranges_base = tag != DW_TAG_compile_unit;
18122 unsigned int ranges_offset = (DW_UNSND (&attr)
18123 + (need_ranges_base
18124 ? cu->ranges_base
18125 : 0));
18126
18127 /* Value of the DW_AT_ranges attribute is the offset in the
18128 .debug_ranges section. */
18129 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18130 nullptr))
18131 has_pc_info = 1;
18132 }
18133 break;
18134
c906108c
SS
18135 default:
18136 break;
18137 }
18138 }
18139
10d06d82
TT
18140 /* For Ada, if both the name and the linkage name appear, we prefer
18141 the latter. This lets "catch exception" work better, regardless
18142 of the order in which the name and linkage name were emitted.
18143 Really, though, this is just a workaround for the fact that gdb
18144 doesn't store both the name and the linkage name. */
18145 if (cu->language == language_ada && linkage_name != nullptr)
18146 name = linkage_name;
18147
91da1414 18148 if (high_pc_relative)
48fbe735 18149 highpc += lowpc;
91da1414 18150
9373cf26
JK
18151 if (has_low_pc_attr && has_high_pc_attr)
18152 {
18153 /* When using the GNU linker, .gnu.linkonce. sections are used to
18154 eliminate duplicate copies of functions and vtables and such.
18155 The linker will arbitrarily choose one and discard the others.
18156 The AT_*_pc values for such functions refer to local labels in
18157 these sections. If the section from that file was discarded, the
18158 labels are not in the output, so the relocs get a value of 0.
18159 If this is a discarded function, mark the pc bounds as invalid,
18160 so that GDB will ignore it. */
48fbe735 18161 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18162 {
48fbe735 18163 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18164 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18165
b98664d3 18166 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18167 "for DIE at %s [in module %s]"),
48fbe735
YQ
18168 paddress (gdbarch, lowpc),
18169 sect_offset_str (sect_off),
9d8780f0 18170 objfile_name (objfile));
9373cf26
JK
18171 }
18172 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18173 else if (lowpc >= highpc)
9373cf26 18174 {
48fbe735 18175 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18176 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18177
b98664d3 18178 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18179 "for DIE at %s [in module %s]"),
48fbe735
YQ
18180 paddress (gdbarch, lowpc),
18181 paddress (gdbarch, highpc),
18182 sect_offset_str (sect_off),
9c541725 18183 objfile_name (objfile));
9373cf26
JK
18184 }
18185 else
48fbe735 18186 has_pc_info = 1;
9373cf26 18187 }
85cbf3d3 18188
c906108c
SS
18189 return info_ptr;
18190}
18191
72bf9492
DJ
18192/* Find a cached partial DIE at OFFSET in CU. */
18193
d590ff25
YQ
18194struct partial_die_info *
18195dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18196{
18197 struct partial_die_info *lookup_die = NULL;
6f06d47b 18198 struct partial_die_info part_die (sect_off);
72bf9492 18199
9a3c8263 18200 lookup_die = ((struct partial_die_info *)
d590ff25 18201 htab_find_with_hash (partial_dies, &part_die,
9c541725 18202 to_underlying (sect_off)));
72bf9492 18203
72bf9492
DJ
18204 return lookup_die;
18205}
18206
348e048f
DE
18207/* Find a partial DIE at OFFSET, which may or may not be in CU,
18208 except in the case of .debug_types DIEs which do not reference
18209 outside their CU (they do however referencing other types via
55f1336d 18210 DW_FORM_ref_sig8). */
72bf9492 18211
122cf0f2 18212static const struct cu_partial_die_info
9c541725 18213find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18214{
518817b3
SM
18215 struct dwarf2_per_objfile *dwarf2_per_objfile
18216 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18217 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18218 struct dwarf2_per_cu_data *per_cu = NULL;
18219 struct partial_die_info *pd = NULL;
72bf9492 18220
36586728 18221 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18222 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18223 {
d590ff25 18224 pd = cu->find_partial_die (sect_off);
5afb4e99 18225 if (pd != NULL)
fb816e8b 18226 return { cu, pd };
0d99eb77
DE
18227 /* We missed recording what we needed.
18228 Load all dies and try again. */
18229 per_cu = cu->per_cu;
5afb4e99 18230 }
0d99eb77
DE
18231 else
18232 {
18233 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18234 if (cu->per_cu->is_debug_types)
0d99eb77 18235 {
9d8780f0
SM
18236 error (_("Dwarf Error: Type Unit at offset %s contains"
18237 " external reference to offset %s [in module %s].\n"),
18238 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18239 bfd_get_filename (objfile->obfd));
18240 }
9c541725 18241 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18242 dwarf2_per_objfile);
72bf9492 18243
0d99eb77
DE
18244 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18245 load_partial_comp_unit (per_cu);
ae038cb0 18246
0d99eb77 18247 per_cu->cu->last_used = 0;
d590ff25 18248 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18249 }
5afb4e99 18250
dee91e82
DE
18251 /* If we didn't find it, and not all dies have been loaded,
18252 load them all and try again. */
18253
5afb4e99
DJ
18254 if (pd == NULL && per_cu->load_all_dies == 0)
18255 {
5afb4e99 18256 per_cu->load_all_dies = 1;
fd820528
DE
18257
18258 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18259 THIS_CU->cu may already be in use. So we can't just free it and
18260 replace its DIEs with the ones we read in. Instead, we leave those
18261 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18262 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18263 set. */
dee91e82 18264 load_partial_comp_unit (per_cu);
5afb4e99 18265
d590ff25 18266 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18267 }
18268
18269 if (pd == NULL)
18270 internal_error (__FILE__, __LINE__,
9d8780f0 18271 _("could not find partial DIE %s "
3e43a32a 18272 "in cache [from module %s]\n"),
9d8780f0 18273 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18274 return { per_cu->cu, pd };
72bf9492
DJ
18275}
18276
abc72ce4
DE
18277/* See if we can figure out if the class lives in a namespace. We do
18278 this by looking for a member function; its demangled name will
18279 contain namespace info, if there is any. */
18280
18281static void
18282guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18283 struct dwarf2_cu *cu)
18284{
18285 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18286 what template types look like, because the demangler
18287 frequently doesn't give the same name as the debug info. We
18288 could fix this by only using the demangled name to get the
18289 prefix (but see comment in read_structure_type). */
18290
18291 struct partial_die_info *real_pdi;
18292 struct partial_die_info *child_pdi;
18293
18294 /* If this DIE (this DIE's specification, if any) has a parent, then
18295 we should not do this. We'll prepend the parent's fully qualified
18296 name when we create the partial symbol. */
18297
18298 real_pdi = struct_pdi;
18299 while (real_pdi->has_specification)
fb816e8b 18300 {
122cf0f2
AB
18301 auto res = find_partial_die (real_pdi->spec_offset,
18302 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18303 real_pdi = res.pdi;
18304 cu = res.cu;
18305 }
abc72ce4
DE
18306
18307 if (real_pdi->die_parent != NULL)
18308 return;
18309
18310 for (child_pdi = struct_pdi->die_child;
18311 child_pdi != NULL;
18312 child_pdi = child_pdi->die_sibling)
18313 {
18314 if (child_pdi->tag == DW_TAG_subprogram
18315 && child_pdi->linkage_name != NULL)
18316 {
43816ebc
TT
18317 gdb::unique_xmalloc_ptr<char> actual_class_name
18318 (language_class_name_from_physname (cu->language_defn,
18319 child_pdi->linkage_name));
abc72ce4
DE
18320 if (actual_class_name != NULL)
18321 {
518817b3 18322 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18323 struct_pdi->name
021887d8 18324 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 18325 actual_class_name.get ());
abc72ce4
DE
18326 }
18327 break;
18328 }
18329 }
18330}
18331
52356b79
YQ
18332void
18333partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18334{
abc72ce4
DE
18335 /* Once we've fixed up a die, there's no point in doing so again.
18336 This also avoids a memory leak if we were to call
18337 guess_partial_die_structure_name multiple times. */
52356b79 18338 if (fixup_called)
abc72ce4
DE
18339 return;
18340
72bf9492
DJ
18341 /* If we found a reference attribute and the DIE has no name, try
18342 to find a name in the referred to DIE. */
18343
52356b79 18344 if (name == NULL && has_specification)
72bf9492
DJ
18345 {
18346 struct partial_die_info *spec_die;
72bf9492 18347
122cf0f2 18348 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18349 spec_die = res.pdi;
18350 cu = res.cu;
72bf9492 18351
52356b79 18352 spec_die->fixup (cu);
72bf9492
DJ
18353
18354 if (spec_die->name)
18355 {
52356b79 18356 name = spec_die->name;
72bf9492
DJ
18357
18358 /* Copy DW_AT_external attribute if it is set. */
18359 if (spec_die->is_external)
52356b79 18360 is_external = spec_die->is_external;
72bf9492
DJ
18361 }
18362 }
18363
18364 /* Set default names for some unnamed DIEs. */
72bf9492 18365
52356b79
YQ
18366 if (name == NULL && tag == DW_TAG_namespace)
18367 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18368
abc72ce4
DE
18369 /* If there is no parent die to provide a namespace, and there are
18370 children, see if we can determine the namespace from their linkage
122d1940 18371 name. */
abc72ce4 18372 if (cu->language == language_cplus
fd5866f6 18373 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18374 && die_parent == NULL
18375 && has_children
18376 && (tag == DW_TAG_class_type
18377 || tag == DW_TAG_structure_type
18378 || tag == DW_TAG_union_type))
18379 guess_partial_die_structure_name (this, cu);
abc72ce4 18380
53832f31
TT
18381 /* GCC might emit a nameless struct or union that has a linkage
18382 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18383 if (name == NULL
18384 && (tag == DW_TAG_class_type
18385 || tag == DW_TAG_interface_type
18386 || tag == DW_TAG_structure_type
18387 || tag == DW_TAG_union_type)
18388 && linkage_name != NULL)
53832f31 18389 {
43816ebc
TT
18390 gdb::unique_xmalloc_ptr<char> demangled
18391 (gdb_demangle (linkage_name, DMGL_TYPES));
18392 if (demangled != nullptr)
53832f31 18393 {
96408a79
SA
18394 const char *base;
18395
18396 /* Strip any leading namespaces/classes, keep only the base name.
18397 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18398 base = strrchr (demangled.get (), ':');
18399 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18400 base++;
18401 else
43816ebc 18402 base = demangled.get ();
96408a79 18403
518817b3 18404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 18405 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
18406 }
18407 }
18408
52356b79 18409 fixup_called = 1;
72bf9492
DJ
18410}
18411
18a8505e
AT
18412/* Process the attributes that had to be skipped in the first round. These
18413 attributes are the ones that need str_offsets_base or addr_base attributes.
18414 They could not have been processed in the first round, because at the time
18415 the values of str_offsets_base or addr_base may not have been known. */
18416void read_attribute_reprocess (const struct die_reader_specs *reader,
18417 struct attribute *attr)
18418{
18419 struct dwarf2_cu *cu = reader->cu;
18420 switch (attr->form)
18421 {
18422 case DW_FORM_addrx:
18423 case DW_FORM_GNU_addr_index:
18424 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18425 break;
18426 case DW_FORM_strx:
18427 case DW_FORM_strx1:
18428 case DW_FORM_strx2:
18429 case DW_FORM_strx3:
18430 case DW_FORM_strx4:
18431 case DW_FORM_GNU_str_index:
18432 {
18433 unsigned int str_index = DW_UNSND (attr);
18434 if (reader->dwo_file != NULL)
18435 {
18436 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18437 DW_STRING_IS_CANONICAL (attr) = 0;
18438 }
18439 else
18440 {
18441 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18442 DW_STRING_IS_CANONICAL (attr) = 0;
18443 }
18444 break;
18445 }
18446 default:
18447 gdb_assert_not_reached (_("Unexpected DWARF form."));
18448 }
18449}
18450
a8329558 18451/* Read an attribute value described by an attribute form. */
c906108c 18452
d521ce57 18453static const gdb_byte *
dee91e82
DE
18454read_attribute_value (const struct die_reader_specs *reader,
18455 struct attribute *attr, unsigned form,
18a8505e
AT
18456 LONGEST implicit_const, const gdb_byte *info_ptr,
18457 bool *need_reprocess)
c906108c 18458{
dee91e82 18459 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18460 struct dwarf2_per_objfile *dwarf2_per_objfile
18461 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18462 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18464 bfd *abfd = reader->abfd;
e7c27a73 18465 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18466 unsigned int bytes_read;
18467 struct dwarf_block *blk;
18a8505e 18468 *need_reprocess = false;
c906108c 18469
aead7601 18470 attr->form = (enum dwarf_form) form;
a8329558 18471 switch (form)
c906108c 18472 {
c906108c 18473 case DW_FORM_ref_addr:
ae411497 18474 if (cu->header.version == 2)
c8a7a66f
TT
18475 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18476 &bytes_read);
ae411497 18477 else
8266302d
TT
18478 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18479 &bytes_read);
ae411497
TT
18480 info_ptr += bytes_read;
18481 break;
36586728 18482 case DW_FORM_GNU_ref_alt:
8266302d 18483 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18484 info_ptr += bytes_read;
18485 break;
ae411497 18486 case DW_FORM_addr:
c8a7a66f 18487 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18488 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18489 info_ptr += bytes_read;
c906108c
SS
18490 break;
18491 case DW_FORM_block2:
7b5a2f43 18492 blk = dwarf_alloc_block (cu);
c906108c
SS
18493 blk->size = read_2_bytes (abfd, info_ptr);
18494 info_ptr += 2;
18495 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18496 info_ptr += blk->size;
18497 DW_BLOCK (attr) = blk;
18498 break;
18499 case DW_FORM_block4:
7b5a2f43 18500 blk = dwarf_alloc_block (cu);
c906108c
SS
18501 blk->size = read_4_bytes (abfd, info_ptr);
18502 info_ptr += 4;
18503 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18504 info_ptr += blk->size;
18505 DW_BLOCK (attr) = blk;
18506 break;
18507 case DW_FORM_data2:
18508 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18509 info_ptr += 2;
18510 break;
18511 case DW_FORM_data4:
18512 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18513 info_ptr += 4;
18514 break;
18515 case DW_FORM_data8:
18516 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18517 info_ptr += 8;
18518 break;
0224619f
JK
18519 case DW_FORM_data16:
18520 blk = dwarf_alloc_block (cu);
18521 blk->size = 16;
18522 blk->data = read_n_bytes (abfd, info_ptr, 16);
18523 info_ptr += 16;
18524 DW_BLOCK (attr) = blk;
18525 break;
2dc7f7b3 18526 case DW_FORM_sec_offset:
8266302d 18527 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18528 info_ptr += bytes_read;
18529 break;
c906108c 18530 case DW_FORM_string:
9b1c24c8 18531 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18532 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18533 info_ptr += bytes_read;
18534 break;
4bdf3d34 18535 case DW_FORM_strp:
36586728
TT
18536 if (!cu->per_cu->is_dwz)
18537 {
ed2dc618
SM
18538 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18539 abfd, info_ptr, cu_header,
36586728
TT
18540 &bytes_read);
18541 DW_STRING_IS_CANONICAL (attr) = 0;
18542 info_ptr += bytes_read;
18543 break;
18544 }
18545 /* FALLTHROUGH */
43988095
JK
18546 case DW_FORM_line_strp:
18547 if (!cu->per_cu->is_dwz)
18548 {
ed2dc618
SM
18549 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18550 abfd, info_ptr,
43988095
JK
18551 cu_header, &bytes_read);
18552 DW_STRING_IS_CANONICAL (attr) = 0;
18553 info_ptr += bytes_read;
18554 break;
18555 }
18556 /* FALLTHROUGH */
36586728
TT
18557 case DW_FORM_GNU_strp_alt:
18558 {
ed2dc618 18559 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18560 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18561 &bytes_read);
36586728 18562
ed2dc618
SM
18563 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18564 dwz, str_offset);
36586728
TT
18565 DW_STRING_IS_CANONICAL (attr) = 0;
18566 info_ptr += bytes_read;
18567 }
4bdf3d34 18568 break;
2dc7f7b3 18569 case DW_FORM_exprloc:
c906108c 18570 case DW_FORM_block:
7b5a2f43 18571 blk = dwarf_alloc_block (cu);
c906108c
SS
18572 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18573 info_ptr += bytes_read;
18574 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18575 info_ptr += blk->size;
18576 DW_BLOCK (attr) = blk;
18577 break;
18578 case DW_FORM_block1:
7b5a2f43 18579 blk = dwarf_alloc_block (cu);
c906108c
SS
18580 blk->size = read_1_byte (abfd, info_ptr);
18581 info_ptr += 1;
18582 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18583 info_ptr += blk->size;
18584 DW_BLOCK (attr) = blk;
18585 break;
18586 case DW_FORM_data1:
18587 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18588 info_ptr += 1;
18589 break;
18590 case DW_FORM_flag:
18591 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18592 info_ptr += 1;
18593 break;
2dc7f7b3
TT
18594 case DW_FORM_flag_present:
18595 DW_UNSND (attr) = 1;
18596 break;
c906108c
SS
18597 case DW_FORM_sdata:
18598 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18599 info_ptr += bytes_read;
18600 break;
18601 case DW_FORM_udata:
18a8505e 18602 case DW_FORM_rnglistx:
c906108c
SS
18603 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18604 info_ptr += bytes_read;
18605 break;
18606 case DW_FORM_ref1:
9c541725 18607 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18608 + read_1_byte (abfd, info_ptr));
c906108c
SS
18609 info_ptr += 1;
18610 break;
18611 case DW_FORM_ref2:
9c541725 18612 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18613 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18614 info_ptr += 2;
18615 break;
18616 case DW_FORM_ref4:
9c541725 18617 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18618 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18619 info_ptr += 4;
18620 break;
613e1657 18621 case DW_FORM_ref8:
9c541725 18622 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18623 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18624 info_ptr += 8;
18625 break;
55f1336d 18626 case DW_FORM_ref_sig8:
ac9ec31b 18627 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18628 info_ptr += 8;
18629 break;
c906108c 18630 case DW_FORM_ref_udata:
9c541725 18631 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18632 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18633 info_ptr += bytes_read;
18634 break;
c906108c 18635 case DW_FORM_indirect:
a8329558
KW
18636 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18637 info_ptr += bytes_read;
43988095
JK
18638 if (form == DW_FORM_implicit_const)
18639 {
18640 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18641 info_ptr += bytes_read;
18642 }
18643 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18644 info_ptr, need_reprocess);
43988095
JK
18645 break;
18646 case DW_FORM_implicit_const:
18647 DW_SND (attr) = implicit_const;
a8329558 18648 break;
336d760d 18649 case DW_FORM_addrx:
3019eac3 18650 case DW_FORM_GNU_addr_index:
18a8505e
AT
18651 *need_reprocess = true;
18652 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18653 info_ptr += bytes_read;
18654 break;
cf532bd1 18655 case DW_FORM_strx:
15f18d14
AT
18656 case DW_FORM_strx1:
18657 case DW_FORM_strx2:
18658 case DW_FORM_strx3:
18659 case DW_FORM_strx4:
3019eac3 18660 case DW_FORM_GNU_str_index:
3019eac3 18661 {
15f18d14
AT
18662 ULONGEST str_index;
18663 if (form == DW_FORM_strx1)
18664 {
18665 str_index = read_1_byte (abfd, info_ptr);
18666 info_ptr += 1;
18667 }
18668 else if (form == DW_FORM_strx2)
18669 {
18670 str_index = read_2_bytes (abfd, info_ptr);
18671 info_ptr += 2;
18672 }
18673 else if (form == DW_FORM_strx3)
18674 {
18675 str_index = read_3_bytes (abfd, info_ptr);
18676 info_ptr += 3;
18677 }
18678 else if (form == DW_FORM_strx4)
18679 {
18680 str_index = read_4_bytes (abfd, info_ptr);
18681 info_ptr += 4;
18682 }
18683 else
18684 {
18685 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18686 info_ptr += bytes_read;
18687 }
18a8505e
AT
18688 *need_reprocess = true;
18689 DW_UNSND (attr) = str_index;
18690 }
3019eac3 18691 break;
c906108c 18692 default:
8a3fe4f8 18693 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18694 dwarf_form_name (form),
18695 bfd_get_filename (abfd));
c906108c 18696 }
28e94949 18697
36586728 18698 /* Super hack. */
cd6c91b4 18699 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18700 attr->form = DW_FORM_GNU_ref_alt;
18701
28e94949
JB
18702 /* We have seen instances where the compiler tried to emit a byte
18703 size attribute of -1 which ended up being encoded as an unsigned
18704 0xffffffff. Although 0xffffffff is technically a valid size value,
18705 an object of this size seems pretty unlikely so we can relatively
18706 safely treat these cases as if the size attribute was invalid and
18707 treat them as zero by default. */
18708 if (attr->name == DW_AT_byte_size
18709 && form == DW_FORM_data4
18710 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18711 {
18712 complaint
b98664d3 18713 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18714 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18715 DW_UNSND (attr) = 0;
18716 }
28e94949 18717
c906108c
SS
18718 return info_ptr;
18719}
18720
a8329558
KW
18721/* Read an attribute described by an abbreviated attribute. */
18722
d521ce57 18723static const gdb_byte *
dee91e82
DE
18724read_attribute (const struct die_reader_specs *reader,
18725 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18726 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18727{
18728 attr->name = abbrev->name;
43988095 18729 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18730 abbrev->implicit_const, info_ptr,
18731 need_reprocess);
a8329558
KW
18732}
18733
c764a876
DE
18734/* Cover function for read_initial_length.
18735 Returns the length of the object at BUF, and stores the size of the
18736 initial length in *BYTES_READ and stores the size that offsets will be in
18737 *OFFSET_SIZE.
18738 If the initial length size is not equivalent to that specified in
18739 CU_HEADER then issue a complaint.
18740 This is useful when reading non-comp-unit headers. */
dd373385 18741
c764a876 18742static LONGEST
d521ce57 18743read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18744 const struct comp_unit_head *cu_header,
18745 unsigned int *bytes_read,
18746 unsigned int *offset_size)
18747{
18748 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18749
18750 gdb_assert (cu_header->initial_length_size == 4
18751 || cu_header->initial_length_size == 8
18752 || cu_header->initial_length_size == 12);
18753
18754 if (cu_header->initial_length_size != *bytes_read)
b98664d3 18755 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18756
c764a876 18757 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18758 return length;
613e1657
KB
18759}
18760
43988095
JK
18761/* Return pointer to string at section SECT offset STR_OFFSET with error
18762 reporting strings FORM_NAME and SECT_NAME. */
18763
d521ce57 18764static const char *
ed2dc618
SM
18765read_indirect_string_at_offset_from (struct objfile *objfile,
18766 bfd *abfd, LONGEST str_offset,
43988095
JK
18767 struct dwarf2_section_info *sect,
18768 const char *form_name,
18769 const char *sect_name)
18770{
96b79293 18771 sect->read (objfile);
43988095
JK
18772 if (sect->buffer == NULL)
18773 error (_("%s used without %s section [in module %s]"),
18774 form_name, sect_name, bfd_get_filename (abfd));
18775 if (str_offset >= sect->size)
18776 error (_("%s pointing outside of %s section [in module %s]"),
18777 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18778 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18779 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18780 return NULL;
43988095
JK
18781 return (const char *) (sect->buffer + str_offset);
18782}
18783
18784/* Return pointer to string at .debug_str offset STR_OFFSET. */
18785
18786static const char *
ed2dc618
SM
18787read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18788 bfd *abfd, LONGEST str_offset)
43988095 18789{
ed2dc618
SM
18790 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18791 abfd, str_offset,
43988095
JK
18792 &dwarf2_per_objfile->str,
18793 "DW_FORM_strp", ".debug_str");
18794}
18795
18796/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18797
18798static const char *
ed2dc618
SM
18799read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18800 bfd *abfd, LONGEST str_offset)
43988095 18801{
ed2dc618
SM
18802 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18803 abfd, str_offset,
43988095
JK
18804 &dwarf2_per_objfile->line_str,
18805 "DW_FORM_line_strp",
18806 ".debug_line_str");
c906108c
SS
18807}
18808
36586728
TT
18809/* Read a string at offset STR_OFFSET in the .debug_str section from
18810 the .dwz file DWZ. Throw an error if the offset is too large. If
18811 the string consists of a single NUL byte, return NULL; otherwise
18812 return a pointer to the string. */
18813
d521ce57 18814static const char *
ed2dc618
SM
18815read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18816 LONGEST str_offset)
36586728 18817{
96b79293 18818 dwz->str.read (objfile);
36586728
TT
18819
18820 if (dwz->str.buffer == NULL)
18821 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18822 "section [in module %s]"),
00f93c44 18823 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18824 if (str_offset >= dwz->str.size)
18825 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18826 ".debug_str section [in module %s]"),
00f93c44 18827 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18828 gdb_assert (HOST_CHAR_BIT == 8);
18829 if (dwz->str.buffer[str_offset] == '\0')
18830 return NULL;
d521ce57 18831 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
18832}
18833
43988095
JK
18834/* Return pointer to string at .debug_str offset as read from BUF.
18835 BUF is assumed to be in a compilation unit described by CU_HEADER.
18836 Return *BYTES_READ_PTR count of bytes read from BUF. */
18837
d521ce57 18838static const char *
ed2dc618
SM
18839read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18840 const gdb_byte *buf,
cf2c3c16
TT
18841 const struct comp_unit_head *cu_header,
18842 unsigned int *bytes_read_ptr)
18843{
8266302d 18844 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18845
ed2dc618 18846 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
18847}
18848
43988095
JK
18849/* Return pointer to string at .debug_line_str offset as read from BUF.
18850 BUF is assumed to be in a compilation unit described by CU_HEADER.
18851 Return *BYTES_READ_PTR count of bytes read from BUF. */
18852
18853static const char *
ed2dc618
SM
18854read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18855 bfd *abfd, const gdb_byte *buf,
43988095
JK
18856 const struct comp_unit_head *cu_header,
18857 unsigned int *bytes_read_ptr)
18858{
8266302d 18859 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18860
ed2dc618
SM
18861 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18862 str_offset);
43988095
JK
18863}
18864
3019eac3 18865/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18866 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18867 ADDR_SIZE is the size of addresses from the CU header. */
18868
18869static CORE_ADDR
ed2dc618 18870read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18871 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18872 int addr_size)
3019eac3
DE
18873{
18874 struct objfile *objfile = dwarf2_per_objfile->objfile;
18875 bfd *abfd = objfile->obfd;
18876 const gdb_byte *info_ptr;
18a8505e 18877 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18878
96b79293 18879 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18880 if (dwarf2_per_objfile->addr.buffer == NULL)
18881 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18882 objfile_name (objfile));
18a8505e
AT
18883 if (addr_base_or_zero + addr_index * addr_size
18884 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18885 error (_("DW_FORM_addr_index pointing outside of "
18886 ".debug_addr section [in module %s]"),
4262abfb 18887 objfile_name (objfile));
3019eac3 18888 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18889 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18890 if (addr_size == 4)
18891 return bfd_get_32 (abfd, info_ptr);
18892 else
18893 return bfd_get_64 (abfd, info_ptr);
18894}
18895
18896/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18897
18898static CORE_ADDR
18899read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18900{
518817b3
SM
18901 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18902 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18903}
18904
18905/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18906
18907static CORE_ADDR
d521ce57 18908read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18909 unsigned int *bytes_read)
18910{
518817b3 18911 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18912 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18913
18914 return read_addr_index (cu, addr_index);
18915}
18916
450a1bfc 18917/* See read.h. */
3019eac3
DE
18918
18919CORE_ADDR
450a1bfc 18920dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18921{
ed2dc618 18922 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18923 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18924 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18925 int addr_size;
18926
3019eac3
DE
18927 /* We need addr_base and addr_size.
18928 If we don't have PER_CU->cu, we have to get it.
18929 Nasty, but the alternative is storing the needed info in PER_CU,
18930 which at this point doesn't seem justified: it's not clear how frequently
18931 it would get used and it would increase the size of every PER_CU.
18932 Entry points like dwarf2_per_cu_addr_size do a similar thing
18933 so we're not in uncharted territory here.
18934 Alas we need to be a bit more complicated as addr_base is contained
18935 in the DIE.
18936
18937 We don't need to read the entire CU(/TU).
18938 We just need the header and top level die.
a1b64ce1 18939
3019eac3 18940 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18941 For now we skip this optimization. */
3019eac3
DE
18942
18943 if (cu != NULL)
18944 {
18945 addr_base = cu->addr_base;
18946 addr_size = cu->header.addr_size;
18947 }
18948 else
18949 {
6751ebae 18950 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18951 addr_base = reader.cu->addr_base;
18952 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18953 }
18954
ed2dc618
SM
18955 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18956 addr_size);
3019eac3
DE
18957}
18958
18a8505e
AT
18959/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18960 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18961 DWO file. */
3019eac3 18962
d521ce57 18963static const char *
18a8505e
AT
18964read_str_index (struct dwarf2_cu *cu,
18965 struct dwarf2_section_info *str_section,
18966 struct dwarf2_section_info *str_offsets_section,
18967 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18968{
518817b3
SM
18969 struct dwarf2_per_objfile *dwarf2_per_objfile
18970 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18971 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18972 const char *objf_name = objfile_name (objfile);
3019eac3 18973 bfd *abfd = objfile->obfd;
d521ce57 18974 const gdb_byte *info_ptr;
3019eac3 18975 ULONGEST str_offset;
cf532bd1 18976 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18977
96b79293
TT
18978 str_section->read (objfile);
18979 str_offsets_section->read (objfile);
73869dc2 18980 if (str_section->buffer == NULL)
18a8505e 18981 error (_("%s used without %s section"
9d8780f0 18982 " in CU at offset %s [in module %s]"),
96b79293 18983 form_name, str_section->get_name (),
18a8505e 18984 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18985 if (str_offsets_section->buffer == NULL)
18a8505e 18986 error (_("%s used without %s section"
9d8780f0 18987 " in CU at offset %s [in module %s]"),
96b79293 18988 form_name, str_section->get_name (),
18a8505e 18989 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18990 info_ptr = (str_offsets_section->buffer
18a8505e 18991 + str_offsets_base
3019eac3
DE
18992 + str_index * cu->header.offset_size);
18993 if (cu->header.offset_size == 4)
18994 str_offset = bfd_get_32 (abfd, info_ptr);
18995 else
18996 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18997 if (str_offset >= str_section->size)
57d63ce2 18998 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18999 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19000 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19001 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19002}
19003
18a8505e
AT
19004/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19005
19006static const char *
19007read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19008{
19009 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19010 ? reader->cu->header.addr_size : 0;
19011 return read_str_index (reader->cu,
19012 &reader->dwo_file->sections.str,
19013 &reader->dwo_file->sections.str_offsets,
19014 str_offsets_base, str_index);
19015}
19016
19017/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19018
19019static const char *
19020read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19021{
19022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19023 const char *objf_name = objfile_name (objfile);
19024 static const char form_name[] = "DW_FORM_GNU_str_index";
19025 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19026
19027 if (!cu->str_offsets_base.has_value ())
19028 error (_("%s used in Fission stub without %s"
19029 " in CU at offset 0x%lx [in module %s]"),
19030 form_name, str_offsets_attr_name,
19031 (long) cu->header.offset_size, objf_name);
19032
19033 return read_str_index (cu,
19034 &cu->per_cu->dwarf2_per_objfile->str,
19035 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19036 *cu->str_offsets_base, str_index);
19037}
19038
3019eac3
DE
19039/* Return the length of an LEB128 number in BUF. */
19040
19041static int
19042leb128_size (const gdb_byte *buf)
19043{
19044 const gdb_byte *begin = buf;
19045 gdb_byte byte;
19046
19047 while (1)
19048 {
19049 byte = *buf++;
19050 if ((byte & 128) == 0)
19051 return buf - begin;
19052 }
19053}
19054
c906108c 19055static void
e142c38c 19056set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19057{
19058 switch (lang)
19059 {
19060 case DW_LANG_C89:
76bee0cc 19061 case DW_LANG_C99:
0cfd832f 19062 case DW_LANG_C11:
c906108c 19063 case DW_LANG_C:
d1be3247 19064 case DW_LANG_UPC:
e142c38c 19065 cu->language = language_c;
c906108c 19066 break;
9c37b5ae 19067 case DW_LANG_Java:
c906108c 19068 case DW_LANG_C_plus_plus:
0cfd832f
MW
19069 case DW_LANG_C_plus_plus_11:
19070 case DW_LANG_C_plus_plus_14:
e142c38c 19071 cu->language = language_cplus;
c906108c 19072 break;
6aecb9c2
JB
19073 case DW_LANG_D:
19074 cu->language = language_d;
19075 break;
c906108c
SS
19076 case DW_LANG_Fortran77:
19077 case DW_LANG_Fortran90:
b21b22e0 19078 case DW_LANG_Fortran95:
f7de9aab
MW
19079 case DW_LANG_Fortran03:
19080 case DW_LANG_Fortran08:
e142c38c 19081 cu->language = language_fortran;
c906108c 19082 break;
a766d390
DE
19083 case DW_LANG_Go:
19084 cu->language = language_go;
19085 break;
c906108c 19086 case DW_LANG_Mips_Assembler:
e142c38c 19087 cu->language = language_asm;
c906108c
SS
19088 break;
19089 case DW_LANG_Ada83:
8aaf0b47 19090 case DW_LANG_Ada95:
bc5f45f8
JB
19091 cu->language = language_ada;
19092 break;
72019c9c
GM
19093 case DW_LANG_Modula2:
19094 cu->language = language_m2;
19095 break;
fe8e67fd
PM
19096 case DW_LANG_Pascal83:
19097 cu->language = language_pascal;
19098 break;
22566fbd
DJ
19099 case DW_LANG_ObjC:
19100 cu->language = language_objc;
19101 break;
c44af4eb
TT
19102 case DW_LANG_Rust:
19103 case DW_LANG_Rust_old:
19104 cu->language = language_rust;
19105 break;
c906108c
SS
19106 case DW_LANG_Cobol74:
19107 case DW_LANG_Cobol85:
c906108c 19108 default:
e142c38c 19109 cu->language = language_minimal;
c906108c
SS
19110 break;
19111 }
e142c38c 19112 cu->language_defn = language_def (cu->language);
c906108c
SS
19113}
19114
19115/* Return the named attribute or NULL if not there. */
19116
19117static struct attribute *
e142c38c 19118dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19119{
a48e046c 19120 for (;;)
c906108c 19121 {
a48e046c
TT
19122 unsigned int i;
19123 struct attribute *spec = NULL;
19124
19125 for (i = 0; i < die->num_attrs; ++i)
19126 {
19127 if (die->attrs[i].name == name)
19128 return &die->attrs[i];
19129 if (die->attrs[i].name == DW_AT_specification
19130 || die->attrs[i].name == DW_AT_abstract_origin)
19131 spec = &die->attrs[i];
19132 }
19133
19134 if (!spec)
19135 break;
c906108c 19136
f2f0e013 19137 die = follow_die_ref (die, spec, &cu);
f2f0e013 19138 }
c5aa993b 19139
c906108c
SS
19140 return NULL;
19141}
19142
348e048f
DE
19143/* Return the named attribute or NULL if not there,
19144 but do not follow DW_AT_specification, etc.
19145 This is for use in contexts where we're reading .debug_types dies.
19146 Following DW_AT_specification, DW_AT_abstract_origin will take us
19147 back up the chain, and we want to go down. */
19148
19149static struct attribute *
45e58e77 19150dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19151{
19152 unsigned int i;
19153
19154 for (i = 0; i < die->num_attrs; ++i)
19155 if (die->attrs[i].name == name)
19156 return &die->attrs[i];
19157
19158 return NULL;
19159}
19160
7d45c7c3
KB
19161/* Return the string associated with a string-typed attribute, or NULL if it
19162 is either not found or is of an incorrect type. */
19163
19164static const char *
19165dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19166{
19167 struct attribute *attr;
19168 const char *str = NULL;
19169
19170 attr = dwarf2_attr (die, name, cu);
19171
19172 if (attr != NULL)
19173 {
43988095 19174 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19175 || attr->form == DW_FORM_string
cf532bd1 19176 || attr->form == DW_FORM_strx
8fe0f950
AT
19177 || attr->form == DW_FORM_strx1
19178 || attr->form == DW_FORM_strx2
19179 || attr->form == DW_FORM_strx3
19180 || attr->form == DW_FORM_strx4
b3340438 19181 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19182 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19183 str = DW_STRING (attr);
19184 else
b98664d3 19185 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19186 "DIE at %s in module %s"),
19187 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19188 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19189 }
19190
19191 return str;
19192}
19193
a084a2a6 19194/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19195 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19196static const char *
19197dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19198{
19199 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19200 if (dwo_name == nullptr)
19201 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19202 return dwo_name;
19203}
19204
05cf31d1
JB
19205/* Return non-zero iff the attribute NAME is defined for the given DIE,
19206 and holds a non-zero value. This function should only be used for
2dc7f7b3 19207 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19208
19209static int
19210dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19211{
19212 struct attribute *attr = dwarf2_attr (die, name, cu);
19213
19214 return (attr && DW_UNSND (attr));
19215}
19216
3ca72b44 19217static int
e142c38c 19218die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19219{
05cf31d1
JB
19220 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19221 which value is non-zero. However, we have to be careful with
19222 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19223 (via dwarf2_flag_true_p) follows this attribute. So we may
19224 end up accidently finding a declaration attribute that belongs
19225 to a different DIE referenced by the specification attribute,
19226 even though the given DIE does not have a declaration attribute. */
19227 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19228 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19229}
19230
63d06c5c 19231/* Return the die giving the specification for DIE, if there is
f2f0e013 19232 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19233 containing the return value on output. If there is no
19234 specification, but there is an abstract origin, that is
19235 returned. */
63d06c5c
DC
19236
19237static struct die_info *
f2f0e013 19238die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19239{
f2f0e013
DJ
19240 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19241 *spec_cu);
63d06c5c 19242
edb3359d
DJ
19243 if (spec_attr == NULL)
19244 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19245
63d06c5c
DC
19246 if (spec_attr == NULL)
19247 return NULL;
19248 else
f2f0e013 19249 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19250}
c906108c 19251
527f3840
JK
19252/* Stub for free_line_header to match void * callback types. */
19253
19254static void
19255free_line_header_voidp (void *arg)
19256{
9a3c8263 19257 struct line_header *lh = (struct line_header *) arg;
527f3840 19258
fff8551c 19259 delete lh;
527f3840
JK
19260}
19261
83769d0b 19262/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19263
19264static struct dwarf2_section_info *
19265get_debug_line_section (struct dwarf2_cu *cu)
19266{
19267 struct dwarf2_section_info *section;
518817b3
SM
19268 struct dwarf2_per_objfile *dwarf2_per_objfile
19269 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19270
19271 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19272 DWO file. */
19273 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19274 section = &cu->dwo_unit->dwo_file->sections.line;
19275 else if (cu->per_cu->is_dwz)
19276 {
ed2dc618 19277 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19278
19279 section = &dwz->line;
19280 }
19281 else
19282 section = &dwarf2_per_objfile->line;
19283
19284 return section;
19285}
19286
43988095
JK
19287/* Read directory or file name entry format, starting with byte of
19288 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19289 entries count and the entries themselves in the described entry
19290 format. */
19291
19292static void
ed2dc618
SM
19293read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19294 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19295 struct line_header *lh,
19296 const struct comp_unit_head *cu_header,
19297 void (*callback) (struct line_header *lh,
19298 const char *name,
ecfb656c 19299 dir_index d_index,
43988095
JK
19300 unsigned int mod_time,
19301 unsigned int length))
19302{
19303 gdb_byte format_count, formati;
19304 ULONGEST data_count, datai;
19305 const gdb_byte *buf = *bufp;
19306 const gdb_byte *format_header_data;
43988095
JK
19307 unsigned int bytes_read;
19308
19309 format_count = read_1_byte (abfd, buf);
19310 buf += 1;
19311 format_header_data = buf;
19312 for (formati = 0; formati < format_count; formati++)
19313 {
19314 read_unsigned_leb128 (abfd, buf, &bytes_read);
19315 buf += bytes_read;
19316 read_unsigned_leb128 (abfd, buf, &bytes_read);
19317 buf += bytes_read;
19318 }
19319
19320 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19321 buf += bytes_read;
19322 for (datai = 0; datai < data_count; datai++)
19323 {
19324 const gdb_byte *format = format_header_data;
19325 struct file_entry fe;
19326
43988095
JK
19327 for (formati = 0; formati < format_count; formati++)
19328 {
ecfb656c 19329 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19330 format += bytes_read;
43988095 19331
ecfb656c 19332 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19333 format += bytes_read;
ecfb656c
PA
19334
19335 gdb::optional<const char *> string;
19336 gdb::optional<unsigned int> uint;
19337
43988095
JK
19338 switch (form)
19339 {
19340 case DW_FORM_string:
ecfb656c 19341 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19342 buf += bytes_read;
19343 break;
19344
19345 case DW_FORM_line_strp:
ed2dc618
SM
19346 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19347 abfd, buf,
ecfb656c
PA
19348 cu_header,
19349 &bytes_read));
43988095
JK
19350 buf += bytes_read;
19351 break;
19352
19353 case DW_FORM_data1:
ecfb656c 19354 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19355 buf += 1;
19356 break;
19357
19358 case DW_FORM_data2:
ecfb656c 19359 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19360 buf += 2;
19361 break;
19362
19363 case DW_FORM_data4:
ecfb656c 19364 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19365 buf += 4;
19366 break;
19367
19368 case DW_FORM_data8:
ecfb656c 19369 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19370 buf += 8;
19371 break;
19372
7ba99d21
AT
19373 case DW_FORM_data16:
19374 /* This is used for MD5, but file_entry does not record MD5s. */
19375 buf += 16;
19376 break;
19377
43988095 19378 case DW_FORM_udata:
ecfb656c 19379 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19380 buf += bytes_read;
19381 break;
19382
19383 case DW_FORM_block:
19384 /* It is valid only for DW_LNCT_timestamp which is ignored by
19385 current GDB. */
19386 break;
19387 }
ecfb656c
PA
19388
19389 switch (content_type)
19390 {
19391 case DW_LNCT_path:
19392 if (string.has_value ())
19393 fe.name = *string;
19394 break;
19395 case DW_LNCT_directory_index:
19396 if (uint.has_value ())
19397 fe.d_index = (dir_index) *uint;
19398 break;
19399 case DW_LNCT_timestamp:
19400 if (uint.has_value ())
19401 fe.mod_time = *uint;
19402 break;
19403 case DW_LNCT_size:
19404 if (uint.has_value ())
19405 fe.length = *uint;
19406 break;
19407 case DW_LNCT_MD5:
19408 break;
19409 default:
b98664d3 19410 complaint (_("Unknown format content type %s"),
ecfb656c
PA
19411 pulongest (content_type));
19412 }
43988095
JK
19413 }
19414
ecfb656c 19415 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
19416 }
19417
19418 *bufp = buf;
19419}
19420
debd256d 19421/* Read the statement program header starting at OFFSET in
3019eac3 19422 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19423 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19424 Returns NULL if there is a problem reading the header, e.g., if it
19425 has a version we don't understand.
debd256d
JB
19426
19427 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19428 the returned object point into the dwarf line section buffer,
19429 and must not be freed. */
ae2de4f8 19430
fff8551c 19431static line_header_up
9c541725 19432dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19433{
d521ce57 19434 const gdb_byte *line_ptr;
c764a876 19435 unsigned int bytes_read, offset_size;
debd256d 19436 int i;
d521ce57 19437 const char *cur_dir, *cur_file;
3019eac3
DE
19438 struct dwarf2_section_info *section;
19439 bfd *abfd;
518817b3
SM
19440 struct dwarf2_per_objfile *dwarf2_per_objfile
19441 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19442
36586728 19443 section = get_debug_line_section (cu);
96b79293 19444 section->read (dwarf2_per_objfile->objfile);
3019eac3 19445 if (section->buffer == NULL)
debd256d 19446 {
3019eac3 19447 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19448 complaint (_("missing .debug_line.dwo section"));
3019eac3 19449 else
b98664d3 19450 complaint (_("missing .debug_line section"));
debd256d
JB
19451 return 0;
19452 }
19453
fceca515
DE
19454 /* We can't do this until we know the section is non-empty.
19455 Only then do we know we have such a section. */
96b79293 19456 abfd = section->get_bfd_owner ();
fceca515 19457
a738430d
MK
19458 /* Make sure that at least there's room for the total_length field.
19459 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 19460 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 19461 {
4d3c2250 19462 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19463 return 0;
19464 }
19465
fff8551c 19466 line_header_up lh (new line_header ());
debd256d 19467
9c541725 19468 lh->sect_off = sect_off;
527f3840
JK
19469 lh->offset_in_dwz = cu->per_cu->is_dwz;
19470
9c541725 19471 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 19472
a738430d 19473 /* Read in the header. */
6e70227d 19474 lh->total_length =
c764a876
DE
19475 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19476 &bytes_read, &offset_size);
debd256d 19477 line_ptr += bytes_read;
7ba99d21
AT
19478
19479 const gdb_byte *start_here = line_ptr;
19480
3019eac3 19481 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 19482 {
4d3c2250 19483 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19484 return 0;
19485 }
7ba99d21 19486 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
19487 lh->version = read_2_bytes (abfd, line_ptr);
19488 line_ptr += 2;
43988095 19489 if (lh->version > 5)
cd366ee8
DE
19490 {
19491 /* This is a version we don't understand. The format could have
19492 changed in ways we don't handle properly so just punt. */
b98664d3 19493 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
19494 return NULL;
19495 }
43988095
JK
19496 if (lh->version >= 5)
19497 {
19498 gdb_byte segment_selector_size;
19499
19500 /* Skip address size. */
19501 read_1_byte (abfd, line_ptr);
19502 line_ptr += 1;
19503
19504 segment_selector_size = read_1_byte (abfd, line_ptr);
19505 line_ptr += 1;
19506 if (segment_selector_size != 0)
19507 {
b98664d3 19508 complaint (_("unsupported segment selector size %u "
43988095
JK
19509 "in .debug_line section"),
19510 segment_selector_size);
19511 return NULL;
19512 }
19513 }
24aa364d 19514 lh->header_length = read_offset (abfd, line_ptr, offset_size);
c764a876 19515 line_ptr += offset_size;
7ba99d21 19516 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
19517 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19518 line_ptr += 1;
2dc7f7b3
TT
19519 if (lh->version >= 4)
19520 {
19521 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19522 line_ptr += 1;
19523 }
19524 else
19525 lh->maximum_ops_per_instruction = 1;
19526
19527 if (lh->maximum_ops_per_instruction == 0)
19528 {
19529 lh->maximum_ops_per_instruction = 1;
b98664d3 19530 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 19531 "in `.debug_line' section"));
2dc7f7b3
TT
19532 }
19533
debd256d
JB
19534 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19535 line_ptr += 1;
19536 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19537 line_ptr += 1;
19538 lh->line_range = read_1_byte (abfd, line_ptr);
19539 line_ptr += 1;
19540 lh->opcode_base = read_1_byte (abfd, line_ptr);
19541 line_ptr += 1;
fff8551c 19542 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
19543
19544 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19545 for (i = 1; i < lh->opcode_base; ++i)
19546 {
19547 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19548 line_ptr += 1;
19549 }
19550
43988095 19551 if (lh->version >= 5)
debd256d 19552 {
43988095 19553 /* Read directory table. */
ed2dc618
SM
19554 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19555 &cu->header,
b926417a 19556 [] (struct line_header *header, const char *name,
ecfb656c 19557 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19558 unsigned int length)
19559 {
b926417a 19560 header->add_include_dir (name);
fff8551c 19561 });
debd256d 19562
43988095 19563 /* Read file name table. */
ed2dc618
SM
19564 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19565 &cu->header,
b926417a 19566 [] (struct line_header *header, const char *name,
ecfb656c 19567 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19568 unsigned int length)
19569 {
b926417a 19570 header->add_file_name (name, d_index, mod_time, length);
fff8551c 19571 });
43988095
JK
19572 }
19573 else
debd256d 19574 {
43988095
JK
19575 /* Read directory table. */
19576 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19577 {
19578 line_ptr += bytes_read;
fff8551c 19579 lh->add_include_dir (cur_dir);
43988095 19580 }
debd256d
JB
19581 line_ptr += bytes_read;
19582
43988095
JK
19583 /* Read file name table. */
19584 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19585 {
ecfb656c
PA
19586 unsigned int mod_time, length;
19587 dir_index d_index;
43988095
JK
19588
19589 line_ptr += bytes_read;
ecfb656c 19590 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
19591 line_ptr += bytes_read;
19592 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19593 line_ptr += bytes_read;
19594 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19595 line_ptr += bytes_read;
19596
ecfb656c 19597 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
19598 }
19599 line_ptr += bytes_read;
debd256d 19600 }
debd256d 19601
3019eac3 19602 if (line_ptr > (section->buffer + section->size))
b98664d3 19603 complaint (_("line number info header doesn't "
3e43a32a 19604 "fit in `.debug_line' section"));
debd256d 19605
debd256d
JB
19606 return lh;
19607}
c906108c 19608
c6da4cef 19609/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19610 Return the file name of the psymtab for the given file_entry.
c6da4cef 19611 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19612 If space for the result is malloc'd, *NAME_HOLDER will be set.
19613 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19614
d521ce57 19615static const char *
7ba99d21 19616psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19617 const dwarf2_psymtab *pst,
c89b44cd
TT
19618 const char *comp_dir,
19619 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19620{
d521ce57
TT
19621 const char *include_name = fe.name;
19622 const char *include_name_to_compare = include_name;
72b9f47f 19623 const char *pst_filename;
c6da4cef
DE
19624 int file_is_pst;
19625
8c43009f 19626 const char *dir_name = fe.include_dir (lh);
c6da4cef 19627
c89b44cd 19628 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19629 if (!IS_ABSOLUTE_PATH (include_name)
19630 && (dir_name != NULL || comp_dir != NULL))
19631 {
19632 /* Avoid creating a duplicate psymtab for PST.
19633 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19634 Before we do the comparison, however, we need to account
19635 for DIR_NAME and COMP_DIR.
19636 First prepend dir_name (if non-NULL). If we still don't
19637 have an absolute path prepend comp_dir (if non-NULL).
19638 However, the directory we record in the include-file's
19639 psymtab does not contain COMP_DIR (to match the
19640 corresponding symtab(s)).
19641
19642 Example:
19643
19644 bash$ cd /tmp
19645 bash$ gcc -g ./hello.c
19646 include_name = "hello.c"
19647 dir_name = "."
19648 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19649 DW_AT_name = "./hello.c"
19650
19651 */
c6da4cef
DE
19652
19653 if (dir_name != NULL)
19654 {
c89b44cd
TT
19655 name_holder->reset (concat (dir_name, SLASH_STRING,
19656 include_name, (char *) NULL));
19657 include_name = name_holder->get ();
c6da4cef 19658 include_name_to_compare = include_name;
c6da4cef
DE
19659 }
19660 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19661 {
c89b44cd
TT
19662 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19663 include_name, (char *) NULL));
19664 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19665 }
19666 }
19667
19668 pst_filename = pst->filename;
c89b44cd 19669 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19670 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19671 {
c89b44cd
TT
19672 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19673 pst_filename, (char *) NULL));
19674 pst_filename = copied_name.get ();
c6da4cef
DE
19675 }
19676
1e3fad37 19677 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19678
c6da4cef
DE
19679 if (file_is_pst)
19680 return NULL;
19681 return include_name;
19682}
19683
d9b3de22
DE
19684/* State machine to track the state of the line number program. */
19685
6f77053d 19686class lnp_state_machine
d9b3de22 19687{
6f77053d
PA
19688public:
19689 /* Initialize a machine state for the start of a line number
19690 program. */
804d2729
TT
19691 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19692 bool record_lines_p);
6f77053d 19693
8c43009f
PA
19694 file_entry *current_file ()
19695 {
19696 /* lh->file_names is 0-based, but the file name numbers in the
19697 statement program are 1-based. */
6f77053d
PA
19698 return m_line_header->file_name_at (m_file);
19699 }
19700
19701 /* Record the line in the state machine. END_SEQUENCE is true if
19702 we're processing the end of a sequence. */
19703 void record_line (bool end_sequence);
19704
7ab6656f
OJ
19705 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19706 nop-out rest of the lines in this sequence. */
6f77053d
PA
19707 void check_line_address (struct dwarf2_cu *cu,
19708 const gdb_byte *line_ptr,
7ab6656f 19709 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19710
19711 void handle_set_discriminator (unsigned int discriminator)
19712 {
19713 m_discriminator = discriminator;
19714 m_line_has_non_zero_discriminator |= discriminator != 0;
19715 }
19716
19717 /* Handle DW_LNE_set_address. */
19718 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19719 {
19720 m_op_index = 0;
19721 address += baseaddr;
19722 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19723 }
19724
19725 /* Handle DW_LNS_advance_pc. */
19726 void handle_advance_pc (CORE_ADDR adjust);
19727
19728 /* Handle a special opcode. */
19729 void handle_special_opcode (unsigned char op_code);
19730
19731 /* Handle DW_LNS_advance_line. */
19732 void handle_advance_line (int line_delta)
19733 {
19734 advance_line (line_delta);
19735 }
19736
19737 /* Handle DW_LNS_set_file. */
19738 void handle_set_file (file_name_index file);
19739
19740 /* Handle DW_LNS_negate_stmt. */
19741 void handle_negate_stmt ()
19742 {
19743 m_is_stmt = !m_is_stmt;
19744 }
19745
19746 /* Handle DW_LNS_const_add_pc. */
19747 void handle_const_add_pc ();
19748
19749 /* Handle DW_LNS_fixed_advance_pc. */
19750 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19751 {
19752 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19753 m_op_index = 0;
19754 }
19755
19756 /* Handle DW_LNS_copy. */
19757 void handle_copy ()
19758 {
19759 record_line (false);
19760 m_discriminator = 0;
19761 }
19762
19763 /* Handle DW_LNE_end_sequence. */
19764 void handle_end_sequence ()
19765 {
804d2729 19766 m_currently_recording_lines = true;
6f77053d
PA
19767 }
19768
19769private:
19770 /* Advance the line by LINE_DELTA. */
19771 void advance_line (int line_delta)
19772 {
19773 m_line += line_delta;
19774
19775 if (line_delta != 0)
19776 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19777 }
19778
804d2729
TT
19779 struct dwarf2_cu *m_cu;
19780
6f77053d
PA
19781 gdbarch *m_gdbarch;
19782
19783 /* True if we're recording lines.
19784 Otherwise we're building partial symtabs and are just interested in
19785 finding include files mentioned by the line number program. */
19786 bool m_record_lines_p;
19787
8c43009f 19788 /* The line number header. */
6f77053d 19789 line_header *m_line_header;
8c43009f 19790
6f77053d
PA
19791 /* These are part of the standard DWARF line number state machine,
19792 and initialized according to the DWARF spec. */
d9b3de22 19793
6f77053d 19794 unsigned char m_op_index = 0;
7ba99d21
AT
19795 /* The line table index of the current file. */
19796 file_name_index m_file = 1;
6f77053d
PA
19797 unsigned int m_line = 1;
19798
19799 /* These are initialized in the constructor. */
19800
19801 CORE_ADDR m_address;
19802 bool m_is_stmt;
19803 unsigned int m_discriminator;
d9b3de22
DE
19804
19805 /* Additional bits of state we need to track. */
19806
19807 /* The last file that we called dwarf2_start_subfile for.
19808 This is only used for TLLs. */
6f77053d 19809 unsigned int m_last_file = 0;
d9b3de22 19810 /* The last file a line number was recorded for. */
6f77053d 19811 struct subfile *m_last_subfile = NULL;
d9b3de22 19812
804d2729
TT
19813 /* When true, record the lines we decode. */
19814 bool m_currently_recording_lines = false;
d9b3de22
DE
19815
19816 /* The last line number that was recorded, used to coalesce
19817 consecutive entries for the same line. This can happen, for
19818 example, when discriminators are present. PR 17276. */
6f77053d
PA
19819 unsigned int m_last_line = 0;
19820 bool m_line_has_non_zero_discriminator = false;
8c43009f 19821};
d9b3de22 19822
6f77053d
PA
19823void
19824lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19825{
19826 CORE_ADDR addr_adj = (((m_op_index + adjust)
19827 / m_line_header->maximum_ops_per_instruction)
19828 * m_line_header->minimum_instruction_length);
19829 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19830 m_op_index = ((m_op_index + adjust)
19831 % m_line_header->maximum_ops_per_instruction);
19832}
d9b3de22 19833
6f77053d
PA
19834void
19835lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19836{
6f77053d 19837 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19838 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19839 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19840 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19841 / m_line_header->maximum_ops_per_instruction)
19842 * m_line_header->minimum_instruction_length);
19843 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19844 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19845 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19846
258bf0ee 19847 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19848 advance_line (line_delta);
19849 record_line (false);
19850 m_discriminator = 0;
19851}
d9b3de22 19852
6f77053d
PA
19853void
19854lnp_state_machine::handle_set_file (file_name_index file)
19855{
19856 m_file = file;
19857
19858 const file_entry *fe = current_file ();
19859 if (fe == NULL)
19860 dwarf2_debug_line_missing_file_complaint ();
19861 else if (m_record_lines_p)
19862 {
19863 const char *dir = fe->include_dir (m_line_header);
19864
c24bdb02 19865 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19866 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19867 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19868 }
19869}
19870
19871void
19872lnp_state_machine::handle_const_add_pc ()
19873{
19874 CORE_ADDR adjust
19875 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19876
19877 CORE_ADDR addr_adj
19878 = (((m_op_index + adjust)
19879 / m_line_header->maximum_ops_per_instruction)
19880 * m_line_header->minimum_instruction_length);
19881
19882 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19883 m_op_index = ((m_op_index + adjust)
19884 % m_line_header->maximum_ops_per_instruction);
19885}
d9b3de22 19886
a05a36a5
DE
19887/* Return non-zero if we should add LINE to the line number table.
19888 LINE is the line to add, LAST_LINE is the last line that was added,
19889 LAST_SUBFILE is the subfile for LAST_LINE.
19890 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19891 had a non-zero discriminator.
19892
19893 We have to be careful in the presence of discriminators.
19894 E.g., for this line:
19895
19896 for (i = 0; i < 100000; i++);
19897
19898 clang can emit four line number entries for that one line,
19899 each with a different discriminator.
19900 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19901
19902 However, we want gdb to coalesce all four entries into one.
19903 Otherwise the user could stepi into the middle of the line and
19904 gdb would get confused about whether the pc really was in the
19905 middle of the line.
19906
19907 Things are further complicated by the fact that two consecutive
19908 line number entries for the same line is a heuristic used by gcc
19909 to denote the end of the prologue. So we can't just discard duplicate
19910 entries, we have to be selective about it. The heuristic we use is
19911 that we only collapse consecutive entries for the same line if at least
19912 one of those entries has a non-zero discriminator. PR 17276.
19913
19914 Note: Addresses in the line number state machine can never go backwards
19915 within one sequence, thus this coalescing is ok. */
19916
19917static int
804d2729
TT
19918dwarf_record_line_p (struct dwarf2_cu *cu,
19919 unsigned int line, unsigned int last_line,
a05a36a5
DE
19920 int line_has_non_zero_discriminator,
19921 struct subfile *last_subfile)
19922{
c24bdb02 19923 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19924 return 1;
19925 if (line != last_line)
19926 return 1;
19927 /* Same line for the same file that we've seen already.
19928 As a last check, for pr 17276, only record the line if the line
19929 has never had a non-zero discriminator. */
19930 if (!line_has_non_zero_discriminator)
19931 return 1;
19932 return 0;
19933}
19934
804d2729
TT
19935/* Use the CU's builder to record line number LINE beginning at
19936 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19937
19938static void
d9b3de22
DE
19939dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19940 unsigned int line, CORE_ADDR address,
804d2729 19941 struct dwarf2_cu *cu)
252a6764
DE
19942{
19943 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19944
27e0867f
DE
19945 if (dwarf_line_debug)
19946 {
19947 fprintf_unfiltered (gdb_stdlog,
19948 "Recording line %u, file %s, address %s\n",
19949 line, lbasename (subfile->name),
19950 paddress (gdbarch, address));
19951 }
19952
804d2729 19953 if (cu != nullptr)
c24bdb02 19954 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
19955}
19956
19957/* Subroutine of dwarf_decode_lines_1 to simplify it.
19958 Mark the end of a set of line number records.
d9b3de22 19959 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19960 If SUBFILE is NULL the request is ignored. */
19961
19962static void
19963dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19964 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19965{
27e0867f
DE
19966 if (subfile == NULL)
19967 return;
19968
19969 if (dwarf_line_debug)
19970 {
19971 fprintf_unfiltered (gdb_stdlog,
19972 "Finishing current line, file %s, address %s\n",
19973 lbasename (subfile->name),
19974 paddress (gdbarch, address));
19975 }
19976
804d2729 19977 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
19978}
19979
6f77053d
PA
19980void
19981lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19982{
d9b3de22
DE
19983 if (dwarf_line_debug)
19984 {
19985 fprintf_unfiltered (gdb_stdlog,
19986 "Processing actual line %u: file %u,"
94a72be7 19987 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19988 m_line, m_file,
6f77053d 19989 paddress (m_gdbarch, m_address),
94a72be7
AB
19990 m_is_stmt, m_discriminator,
19991 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19992 }
19993
6f77053d 19994 file_entry *fe = current_file ();
8c43009f
PA
19995
19996 if (fe == NULL)
d9b3de22
DE
19997 dwarf2_debug_line_missing_file_complaint ();
19998 /* For now we ignore lines not starting on an instruction boundary.
19999 But not when processing end_sequence for compatibility with the
20000 previous version of the code. */
6f77053d 20001 else if (m_op_index == 0 || end_sequence)
d9b3de22 20002 {
8c43009f 20003 fe->included_p = 1;
94a72be7
AB
20004 if (m_record_lines_p
20005 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 20006 {
c24bdb02 20007 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20008 || end_sequence)
d9b3de22 20009 {
804d2729
TT
20010 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20011 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20012 }
20013
20014 if (!end_sequence)
20015 {
804d2729 20016 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20017 m_line_has_non_zero_discriminator,
20018 m_last_subfile))
d9b3de22 20019 {
c24bdb02 20020 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20021 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20022 builder->get_current_subfile (),
6f77053d 20023 m_line, m_address,
804d2729 20024 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20025 }
c24bdb02 20026 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20027 m_last_line = m_line;
d9b3de22
DE
20028 }
20029 }
20030 }
20031}
20032
804d2729
TT
20033lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20034 line_header *lh, bool record_lines_p)
d9b3de22 20035{
804d2729 20036 m_cu = cu;
6f77053d
PA
20037 m_gdbarch = arch;
20038 m_record_lines_p = record_lines_p;
20039 m_line_header = lh;
d9b3de22 20040
804d2729 20041 m_currently_recording_lines = true;
d9b3de22 20042
d9b3de22
DE
20043 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20044 was a line entry for it so that the backend has a chance to adjust it
20045 and also record it in case it needs it. This is currently used by MIPS
20046 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20047 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20048 m_is_stmt = lh->default_is_stmt;
20049 m_discriminator = 0;
252a6764
DE
20050}
20051
6f77053d
PA
20052void
20053lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20054 const gdb_byte *line_ptr,
7ab6656f 20055 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20056{
7ab6656f
OJ
20057 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20058 the pc range of the CU. However, we restrict the test to only ADDRESS
20059 values of zero to preserve GDB's previous behaviour which is to handle
20060 the specific case of a function being GC'd by the linker. */
924c2928 20061
7ab6656f 20062 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20063 {
20064 /* This line table is for a function which has been
20065 GCd by the linker. Ignore it. PR gdb/12528 */
20066
518817b3 20067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20068 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20069
b98664d3 20070 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20071 line_offset, objfile_name (objfile));
804d2729
TT
20072 m_currently_recording_lines = false;
20073 /* Note: m_currently_recording_lines is left as false until we see
20074 DW_LNE_end_sequence. */
924c2928
DE
20075 }
20076}
20077
f3f5162e 20078/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20079 Process the line number information in LH.
20080 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20081 program in order to set included_p for every referenced header. */
debd256d 20082
c906108c 20083static void
43f3e411
DE
20084dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20085 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20086{
d521ce57
TT
20087 const gdb_byte *line_ptr, *extended_end;
20088 const gdb_byte *line_end;
a8c50c1f 20089 unsigned int bytes_read, extended_len;
699ca60a 20090 unsigned char op_code, extended_op;
e142c38c 20091 CORE_ADDR baseaddr;
518817b3 20092 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20093 bfd *abfd = objfile->obfd;
fbf65064 20094 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20095 /* True if we're recording line info (as opposed to building partial
20096 symtabs and just interested in finding include files mentioned by
20097 the line number program). */
20098 bool record_lines_p = !decode_for_pst_p;
e142c38c 20099
b3b3bada 20100 baseaddr = objfile->text_section_offset ();
c906108c 20101
debd256d
JB
20102 line_ptr = lh->statement_program_start;
20103 line_end = lh->statement_program_end;
c906108c
SS
20104
20105 /* Read the statement sequences until there's nothing left. */
20106 while (line_ptr < line_end)
20107 {
6f77053d
PA
20108 /* The DWARF line number program state machine. Reset the state
20109 machine at the start of each sequence. */
804d2729 20110 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20111 bool end_sequence = false;
d9b3de22 20112
8c43009f 20113 if (record_lines_p)
c906108c 20114 {
8c43009f
PA
20115 /* Start a subfile for the current file of the state
20116 machine. */
20117 const file_entry *fe = state_machine.current_file ();
20118
20119 if (fe != NULL)
804d2729 20120 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20121 }
20122
a738430d 20123 /* Decode the table. */
d9b3de22 20124 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20125 {
20126 op_code = read_1_byte (abfd, line_ptr);
20127 line_ptr += 1;
9aa1fe7e 20128
debd256d 20129 if (op_code >= lh->opcode_base)
6e70227d 20130 {
8e07a239 20131 /* Special opcode. */
6f77053d 20132 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20133 }
20134 else switch (op_code)
c906108c
SS
20135 {
20136 case DW_LNS_extended_op:
3e43a32a
MS
20137 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20138 &bytes_read);
473b7be6 20139 line_ptr += bytes_read;
a8c50c1f 20140 extended_end = line_ptr + extended_len;
c906108c
SS
20141 extended_op = read_1_byte (abfd, line_ptr);
20142 line_ptr += 1;
20143 switch (extended_op)
20144 {
20145 case DW_LNE_end_sequence:
6f77053d
PA
20146 state_machine.handle_end_sequence ();
20147 end_sequence = true;
c906108c
SS
20148 break;
20149 case DW_LNE_set_address:
d9b3de22
DE
20150 {
20151 CORE_ADDR address
c8a7a66f 20152 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20153 line_ptr += bytes_read;
6f77053d
PA
20154
20155 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20156 lowpc - baseaddr, address);
6f77053d 20157 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20158 }
c906108c
SS
20159 break;
20160 case DW_LNE_define_file:
debd256d 20161 {
d521ce57 20162 const char *cur_file;
ecfb656c
PA
20163 unsigned int mod_time, length;
20164 dir_index dindex;
6e70227d 20165
3e43a32a
MS
20166 cur_file = read_direct_string (abfd, line_ptr,
20167 &bytes_read);
debd256d 20168 line_ptr += bytes_read;
ecfb656c 20169 dindex = (dir_index)
debd256d
JB
20170 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20171 line_ptr += bytes_read;
20172 mod_time =
20173 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20174 line_ptr += bytes_read;
20175 length =
20176 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20177 line_ptr += bytes_read;
ecfb656c 20178 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20179 }
c906108c 20180 break;
d0c6ba3d 20181 case DW_LNE_set_discriminator:
6f77053d
PA
20182 {
20183 /* The discriminator is not interesting to the
20184 debugger; just ignore it. We still need to
20185 check its value though:
20186 if there are consecutive entries for the same
20187 (non-prologue) line we want to coalesce them.
20188 PR 17276. */
20189 unsigned int discr
20190 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20191 line_ptr += bytes_read;
20192
20193 state_machine.handle_set_discriminator (discr);
20194 }
d0c6ba3d 20195 break;
c906108c 20196 default:
b98664d3 20197 complaint (_("mangled .debug_line section"));
debd256d 20198 return;
c906108c 20199 }
a8c50c1f
DJ
20200 /* Make sure that we parsed the extended op correctly. If e.g.
20201 we expected a different address size than the producer used,
20202 we may have read the wrong number of bytes. */
20203 if (line_ptr != extended_end)
20204 {
b98664d3 20205 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20206 return;
20207 }
c906108c
SS
20208 break;
20209 case DW_LNS_copy:
6f77053d 20210 state_machine.handle_copy ();
c906108c
SS
20211 break;
20212 case DW_LNS_advance_pc:
2dc7f7b3
TT
20213 {
20214 CORE_ADDR adjust
20215 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20216 line_ptr += bytes_read;
6f77053d
PA
20217
20218 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20219 }
c906108c
SS
20220 break;
20221 case DW_LNS_advance_line:
a05a36a5
DE
20222 {
20223 int line_delta
20224 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20225 line_ptr += bytes_read;
6f77053d
PA
20226
20227 state_machine.handle_advance_line (line_delta);
a05a36a5 20228 }
c906108c
SS
20229 break;
20230 case DW_LNS_set_file:
d9b3de22 20231 {
6f77053d 20232 file_name_index file
ecfb656c
PA
20233 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20234 &bytes_read);
d9b3de22 20235 line_ptr += bytes_read;
8c43009f 20236
6f77053d 20237 state_machine.handle_set_file (file);
d9b3de22 20238 }
c906108c
SS
20239 break;
20240 case DW_LNS_set_column:
0ad93d4f 20241 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20242 line_ptr += bytes_read;
20243 break;
20244 case DW_LNS_negate_stmt:
6f77053d 20245 state_machine.handle_negate_stmt ();
c906108c
SS
20246 break;
20247 case DW_LNS_set_basic_block:
c906108c 20248 break;
c2c6d25f
JM
20249 /* Add to the address register of the state machine the
20250 address increment value corresponding to special opcode
a738430d
MK
20251 255. I.e., this value is scaled by the minimum
20252 instruction length since special opcode 255 would have
b021a221 20253 scaled the increment. */
c906108c 20254 case DW_LNS_const_add_pc:
6f77053d 20255 state_machine.handle_const_add_pc ();
c906108c
SS
20256 break;
20257 case DW_LNS_fixed_advance_pc:
3e29f34a 20258 {
6f77053d 20259 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20260 line_ptr += 2;
6f77053d
PA
20261
20262 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20263 }
c906108c 20264 break;
9aa1fe7e 20265 default:
a738430d
MK
20266 {
20267 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20268 int i;
a738430d 20269
debd256d 20270 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20271 {
20272 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20273 line_ptr += bytes_read;
20274 }
20275 }
c906108c
SS
20276 }
20277 }
d9b3de22
DE
20278
20279 if (!end_sequence)
20280 dwarf2_debug_line_missing_end_sequence_complaint ();
20281
20282 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20283 in which case we still finish recording the last line). */
6f77053d 20284 state_machine.record_line (true);
c906108c 20285 }
f3f5162e
DE
20286}
20287
20288/* Decode the Line Number Program (LNP) for the given line_header
20289 structure and CU. The actual information extracted and the type
20290 of structures created from the LNP depends on the value of PST.
20291
20292 1. If PST is NULL, then this procedure uses the data from the program
20293 to create all necessary symbol tables, and their linetables.
20294
20295 2. If PST is not NULL, this procedure reads the program to determine
20296 the list of files included by the unit represented by PST, and
20297 builds all the associated partial symbol tables.
20298
20299 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20300 It is used for relative paths in the line table.
20301 NOTE: When processing partial symtabs (pst != NULL),
20302 comp_dir == pst->dirname.
20303
20304 NOTE: It is important that psymtabs have the same file name (via strcmp)
20305 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20306 symtab we don't use it in the name of the psymtabs we create.
20307 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20308 A good testcase for this is mb-inline.exp.
20309
527f3840
JK
20310 LOWPC is the lowest address in CU (or 0 if not known).
20311
20312 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20313 for its PC<->lines mapping information. Otherwise only the filename
20314 table is read in. */
f3f5162e
DE
20315
20316static void
20317dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20318 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20319 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20320{
518817b3 20321 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20322 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20323
527f3840
JK
20324 if (decode_mapping)
20325 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20326
20327 if (decode_for_pst_p)
20328 {
aaa75496
JB
20329 /* Now that we're done scanning the Line Header Program, we can
20330 create the psymtab of each included file. */
7ba99d21
AT
20331 for (auto &file_entry : lh->file_names ())
20332 if (file_entry.included_p == 1)
aaa75496 20333 {
c89b44cd 20334 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20335 const char *include_name =
7ba99d21
AT
20336 psymtab_include_file_name (lh, file_entry, pst,
20337 comp_dir, &name_holder);
c6da4cef 20338 if (include_name != NULL)
aaa75496
JB
20339 dwarf2_create_include_psymtab (include_name, pst, objfile);
20340 }
20341 }
cb1df416
DJ
20342 else
20343 {
20344 /* Make sure a symtab is created for every file, even files
20345 which contain only variables (i.e. no code with associated
20346 line numbers). */
c24bdb02
KS
20347 buildsym_compunit *builder = cu->get_builder ();
20348 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20349
7ba99d21 20350 for (auto &fe : lh->file_names ())
cb1df416 20351 {
804d2729 20352 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20353 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20354 {
c24bdb02 20355 builder->get_current_subfile ()->symtab
804d2729 20356 = allocate_symtab (cust,
c24bdb02 20357 builder->get_current_subfile ()->name);
43f3e411 20358 }
c24bdb02 20359 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20360 }
20361 }
c906108c
SS
20362}
20363
20364/* Start a subfile for DWARF. FILENAME is the name of the file and
20365 DIRNAME the name of the source directory which contains FILENAME
4d663531 20366 or NULL if not known.
c906108c
SS
20367 This routine tries to keep line numbers from identical absolute and
20368 relative file names in a common subfile.
20369
20370 Using the `list' example from the GDB testsuite, which resides in
20371 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20372 of /srcdir/list0.c yields the following debugging information for list0.c:
20373
c5aa993b 20374 DW_AT_name: /srcdir/list0.c
4d663531 20375 DW_AT_comp_dir: /compdir
357e46e7 20376 files.files[0].name: list0.h
c5aa993b 20377 files.files[0].dir: /srcdir
357e46e7 20378 files.files[1].name: list0.c
c5aa993b 20379 files.files[1].dir: /srcdir
c906108c
SS
20380
20381 The line number information for list0.c has to end up in a single
4f1520fb
FR
20382 subfile, so that `break /srcdir/list0.c:1' works as expected.
20383 start_subfile will ensure that this happens provided that we pass the
20384 concatenation of files.files[1].dir and files.files[1].name as the
20385 subfile's name. */
c906108c
SS
20386
20387static void
804d2729
TT
20388dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20389 const char *dirname)
c906108c 20390{
43816ebc 20391 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20392
4d663531 20393 /* In order not to lose the line information directory,
4f1520fb
FR
20394 we concatenate it to the filename when it makes sense.
20395 Note that the Dwarf3 standard says (speaking of filenames in line
20396 information): ``The directory index is ignored for file names
20397 that represent full path names''. Thus ignoring dirname in the
20398 `else' branch below isn't an issue. */
c906108c 20399
d5166ae1 20400 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20401 {
43816ebc
TT
20402 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20403 filename = copy.get ();
d521ce57 20404 }
c906108c 20405
c24bdb02 20406 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20407}
20408
804d2729
TT
20409/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20410 buildsym_compunit constructor. */
f4dc4d17 20411
c24bdb02
KS
20412struct compunit_symtab *
20413dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20414 CORE_ADDR low_pc)
f4dc4d17 20415{
c24bdb02 20416 gdb_assert (m_builder == nullptr);
43f3e411 20417
c24bdb02
KS
20418 m_builder.reset (new struct buildsym_compunit
20419 (per_cu->dwarf2_per_objfile->objfile,
20420 name, comp_dir, language, low_pc));
93b8bea4 20421
c24bdb02 20422 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20423
c24bdb02
KS
20424 get_builder ()->record_debugformat ("DWARF 2");
20425 get_builder ()->record_producer (producer);
f4dc4d17 20426
c24bdb02 20427 processing_has_namespace_info = false;
43f3e411 20428
c24bdb02 20429 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20430}
20431
4c2df51b
DJ
20432static void
20433var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20434 struct dwarf2_cu *cu)
4c2df51b 20435{
518817b3 20436 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20437 struct comp_unit_head *cu_header = &cu->header;
20438
4c2df51b
DJ
20439 /* NOTE drow/2003-01-30: There used to be a comment and some special
20440 code here to turn a symbol with DW_AT_external and a
20441 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20442 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20443 with some versions of binutils) where shared libraries could have
20444 relocations against symbols in their debug information - the
20445 minimal symbol would have the right address, but the debug info
20446 would not. It's no longer necessary, because we will explicitly
20447 apply relocations when we read in the debug information now. */
20448
20449 /* A DW_AT_location attribute with no contents indicates that a
20450 variable has been optimized away. */
4fc6c0d5 20451 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20452 {
f1e6e072 20453 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20454 return;
20455 }
20456
20457 /* Handle one degenerate form of location expression specially, to
20458 preserve GDB's previous behavior when section offsets are
336d760d
AT
20459 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20460 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20461
4fc6c0d5 20462 if (attr->form_is_block ()
3019eac3
DE
20463 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20464 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20465 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20466 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20467 && (DW_BLOCK (attr)->size
20468 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20469 {
891d2f0b 20470 unsigned int dummy;
4c2df51b 20471
3019eac3 20472 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20473 SET_SYMBOL_VALUE_ADDRESS
20474 (sym, cu->header.read_address (objfile->obfd,
20475 DW_BLOCK (attr)->data + 1,
20476 &dummy));
3019eac3 20477 else
38583298
TT
20478 SET_SYMBOL_VALUE_ADDRESS
20479 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20480 &dummy));
f1e6e072 20481 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20482 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20483 SET_SYMBOL_VALUE_ADDRESS
20484 (sym,
20485 SYMBOL_VALUE_ADDRESS (sym)
20486 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20487 return;
20488 }
20489
20490 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20491 expression evaluator, and use LOC_COMPUTED only when necessary
20492 (i.e. when the value of a register or memory location is
20493 referenced, or a thread-local block, etc.). Then again, it might
20494 not be worthwhile. I'm assuming that it isn't unless performance
20495 or memory numbers show me otherwise. */
20496
f1e6e072 20497 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20498
f1e6e072 20499 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20500 cu->has_loclist = true;
4c2df51b
DJ
20501}
20502
c906108c
SS
20503/* Given a pointer to a DWARF information entry, figure out if we need
20504 to make a symbol table entry for it, and if so, create a new entry
20505 and return a pointer to it.
20506 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20507 used the passed type.
20508 If SPACE is not NULL, use it to hold the new symbol. If it is
20509 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20510
20511static struct symbol *
5e2db402
TT
20512new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20513 struct symbol *space)
c906108c 20514{
518817b3
SM
20515 struct dwarf2_per_objfile *dwarf2_per_objfile
20516 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20517 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20518 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20519 struct symbol *sym = NULL;
15d034d0 20520 const char *name;
c906108c
SS
20521 struct attribute *attr = NULL;
20522 struct attribute *attr2 = NULL;
e142c38c 20523 CORE_ADDR baseaddr;
e37fd15a
SW
20524 struct pending **list_to_add = NULL;
20525
edb3359d 20526 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20527
b3b3bada 20528 baseaddr = objfile->text_section_offset ();
c906108c 20529
94af9270 20530 name = dwarf2_name (die, cu);
c906108c
SS
20531 if (name)
20532 {
94af9270 20533 const char *linkagename;
34eaf542 20534 int suppress_add = 0;
94af9270 20535
34eaf542
TT
20536 if (space)
20537 sym = space;
20538 else
e623cf5d 20539 sym = allocate_symbol (objfile);
c906108c 20540 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20541
20542 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20543 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20544 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20545 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20546
f55ee35c
JK
20547 /* Fortran does not have mangling standard and the mangling does differ
20548 between gfortran, iFort etc. */
20549 if (cu->language == language_fortran
468c0cbb
CB
20550 && symbol_get_demangled_name (sym) == NULL)
20551 symbol_set_demangled_name (sym,
cfc594ee 20552 dwarf2_full_name (name, die, cu),
29df156d 20553 NULL);
f55ee35c 20554
c906108c 20555 /* Default assumptions.
c5aa993b 20556 Use the passed type or decode it from the die. */
176620f1 20557 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20558 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20559 if (type != NULL)
20560 SYMBOL_TYPE (sym) = type;
20561 else
e7c27a73 20562 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20563 attr = dwarf2_attr (die,
20564 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20565 cu);
435d3d88 20566 if (attr != nullptr)
c906108c
SS
20567 {
20568 SYMBOL_LINE (sym) = DW_UNSND (attr);
20569 }
cb1df416 20570
edb3359d
DJ
20571 attr = dwarf2_attr (die,
20572 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20573 cu);
435d3d88 20574 if (attr != nullptr)
cb1df416 20575 {
ecfb656c 20576 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20577 struct file_entry *fe;
9a619af0 20578
ecfb656c
PA
20579 if (cu->line_header != NULL)
20580 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20581 else
20582 fe = NULL;
20583
20584 if (fe == NULL)
b98664d3 20585 complaint (_("file index out of range"));
8c43009f
PA
20586 else
20587 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20588 }
20589
c906108c
SS
20590 switch (die->tag)
20591 {
20592 case DW_TAG_label:
e142c38c 20593 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20594 if (attr != nullptr)
3e29f34a
MR
20595 {
20596 CORE_ADDR addr;
20597
cd6c91b4 20598 addr = attr->value_as_address ();
3e29f34a 20599 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20600 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20601 }
0f5238ed
TT
20602 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20603 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20604 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20605 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20606 break;
20607 case DW_TAG_subprogram:
20608 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20609 finish_block. */
f1e6e072 20610 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20611 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20612 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20613 || cu->language == language_ada
20614 || cu->language == language_fortran)
c906108c 20615 {
2cfa0c8d 20616 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20617 Ada and Fortran subprograms, whether marked external or
20618 not, are always stored as a global symbol, because we want
20619 to be able to access them globally. For instance, we want
20620 to be able to break on a nested subprogram without having
20621 to specify the context. */
c24bdb02 20622 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20623 }
20624 else
20625 {
e37fd15a 20626 list_to_add = cu->list_in_scope;
c906108c
SS
20627 }
20628 break;
edb3359d
DJ
20629 case DW_TAG_inlined_subroutine:
20630 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20631 finish_block. */
f1e6e072 20632 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20633 SYMBOL_INLINED (sym) = 1;
481860b3 20634 list_to_add = cu->list_in_scope;
edb3359d 20635 break;
34eaf542
TT
20636 case DW_TAG_template_value_param:
20637 suppress_add = 1;
20638 /* Fall through. */
72929c62 20639 case DW_TAG_constant:
c906108c 20640 case DW_TAG_variable:
254e6b9e 20641 case DW_TAG_member:
0963b4bd
MS
20642 /* Compilation with minimal debug info may result in
20643 variables with missing type entries. Change the
20644 misleading `void' type to something sensible. */
c906108c 20645 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20646 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20647
e142c38c 20648 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20649 /* In the case of DW_TAG_member, we should only be called for
20650 static const members. */
20651 if (die->tag == DW_TAG_member)
20652 {
3863f96c
DE
20653 /* dwarf2_add_field uses die_is_declaration,
20654 so we do the same. */
254e6b9e
DE
20655 gdb_assert (die_is_declaration (die, cu));
20656 gdb_assert (attr);
20657 }
435d3d88 20658 if (attr != nullptr)
c906108c 20659 {
e7c27a73 20660 dwarf2_const_value (attr, sym, cu);
e142c38c 20661 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20662 if (!suppress_add)
34eaf542
TT
20663 {
20664 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20665 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20666 else
e37fd15a 20667 list_to_add = cu->list_in_scope;
34eaf542 20668 }
c906108c
SS
20669 break;
20670 }
e142c38c 20671 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20672 if (attr != nullptr)
c906108c 20673 {
e7c27a73 20674 var_decode_location (attr, sym, cu);
e142c38c 20675 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20676
20677 /* Fortran explicitly imports any global symbols to the local
20678 scope by DW_TAG_common_block. */
20679 if (cu->language == language_fortran && die->parent
20680 && die->parent->tag == DW_TAG_common_block)
20681 attr2 = NULL;
20682
caac4577
JG
20683 if (SYMBOL_CLASS (sym) == LOC_STATIC
20684 && SYMBOL_VALUE_ADDRESS (sym) == 0
20685 && !dwarf2_per_objfile->has_section_at_zero)
20686 {
20687 /* When a static variable is eliminated by the linker,
20688 the corresponding debug information is not stripped
20689 out, but the variable address is set to null;
20690 do not add such variables into symbol table. */
20691 }
20692 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20693 {
4b610737
TT
20694 if (SYMBOL_CLASS (sym) == LOC_STATIC
20695 && (objfile->flags & OBJF_MAINLINE) == 0
20696 && dwarf2_per_objfile->can_copy)
20697 {
20698 /* A global static variable might be subject to
20699 copy relocation. We first check for a local
20700 minsym, though, because maybe the symbol was
20701 marked hidden, in which case this would not
20702 apply. */
20703 bound_minimal_symbol found
20704 = (lookup_minimal_symbol_linkage
987012b8 20705 (sym->linkage_name (), objfile));
4b610737
TT
20706 if (found.minsym != nullptr)
20707 sym->maybe_copied = 1;
20708 }
f55ee35c 20709
1c809c68
TT
20710 /* A variable with DW_AT_external is never static,
20711 but it may be block-scoped. */
804d2729 20712 list_to_add
c24bdb02
KS
20713 = ((cu->list_in_scope
20714 == cu->get_builder ()->get_file_symbols ())
20715 ? cu->get_builder ()->get_global_symbols ()
804d2729 20716 : cu->list_in_scope);
1c809c68 20717 }
c906108c 20718 else
e37fd15a 20719 list_to_add = cu->list_in_scope;
c906108c
SS
20720 }
20721 else
20722 {
20723 /* We do not know the address of this symbol.
c5aa993b
JM
20724 If it is an external symbol and we have type information
20725 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20726 The address of the variable will then be determined from
20727 the minimal symbol table whenever the variable is
20728 referenced. */
e142c38c 20729 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20730
20731 /* Fortran explicitly imports any global symbols to the local
20732 scope by DW_TAG_common_block. */
20733 if (cu->language == language_fortran && die->parent
20734 && die->parent->tag == DW_TAG_common_block)
20735 {
20736 /* SYMBOL_CLASS doesn't matter here because
20737 read_common_block is going to reset it. */
20738 if (!suppress_add)
20739 list_to_add = cu->list_in_scope;
20740 }
20741 else if (attr2 && (DW_UNSND (attr2) != 0)
20742 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20743 {
0fe7935b
DJ
20744 /* A variable with DW_AT_external is never static, but it
20745 may be block-scoped. */
804d2729 20746 list_to_add
c24bdb02
KS
20747 = ((cu->list_in_scope
20748 == cu->get_builder ()->get_file_symbols ())
20749 ? cu->get_builder ()->get_global_symbols ()
804d2729 20750 : cu->list_in_scope);
0fe7935b 20751
f1e6e072 20752 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20753 }
442ddf59
JK
20754 else if (!die_is_declaration (die, cu))
20755 {
20756 /* Use the default LOC_OPTIMIZED_OUT class. */
20757 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20758 if (!suppress_add)
20759 list_to_add = cu->list_in_scope;
442ddf59 20760 }
c906108c
SS
20761 }
20762 break;
20763 case DW_TAG_formal_parameter:
a60f3166
TT
20764 {
20765 /* If we are inside a function, mark this as an argument. If
20766 not, we might be looking at an argument to an inlined function
20767 when we do not have enough information to show inlined frames;
20768 pretend it's a local variable in that case so that the user can
20769 still see it. */
804d2729 20770 struct context_stack *curr
c24bdb02 20771 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20772 if (curr != nullptr && curr->name != nullptr)
20773 SYMBOL_IS_ARGUMENT (sym) = 1;
20774 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20775 if (attr != nullptr)
a60f3166
TT
20776 {
20777 var_decode_location (attr, sym, cu);
20778 }
20779 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20780 if (attr != nullptr)
a60f3166
TT
20781 {
20782 dwarf2_const_value (attr, sym, cu);
20783 }
f346a30d 20784
a60f3166
TT
20785 list_to_add = cu->list_in_scope;
20786 }
c906108c
SS
20787 break;
20788 case DW_TAG_unspecified_parameters:
20789 /* From varargs functions; gdb doesn't seem to have any
20790 interest in this information, so just ignore it for now.
20791 (FIXME?) */
20792 break;
34eaf542
TT
20793 case DW_TAG_template_type_param:
20794 suppress_add = 1;
20795 /* Fall through. */
c906108c 20796 case DW_TAG_class_type:
680b30c7 20797 case DW_TAG_interface_type:
c906108c
SS
20798 case DW_TAG_structure_type:
20799 case DW_TAG_union_type:
72019c9c 20800 case DW_TAG_set_type:
c906108c 20801 case DW_TAG_enumeration_type:
f1e6e072 20802 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20803 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20804
63d06c5c 20805 {
9c37b5ae 20806 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20807 really ever be static objects: otherwise, if you try
20808 to, say, break of a class's method and you're in a file
20809 which doesn't mention that class, it won't work unless
20810 the check for all static symbols in lookup_symbol_aux
20811 saves you. See the OtherFileClass tests in
20812 gdb.c++/namespace.exp. */
20813
e37fd15a 20814 if (!suppress_add)
34eaf542 20815 {
c24bdb02 20816 buildsym_compunit *builder = cu->get_builder ();
804d2729 20817 list_to_add
c24bdb02 20818 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20819 && cu->language == language_cplus
c24bdb02 20820 ? builder->get_global_symbols ()
804d2729 20821 : cu->list_in_scope);
63d06c5c 20822
64382290 20823 /* The semantics of C++ state that "struct foo {
9c37b5ae 20824 ... }" also defines a typedef for "foo". */
64382290 20825 if (cu->language == language_cplus
45280282 20826 || cu->language == language_ada
c44af4eb
TT
20827 || cu->language == language_d
20828 || cu->language == language_rust)
64382290
TT
20829 {
20830 /* The symbol's name is already allocated along
20831 with this objfile, so we don't need to
20832 duplicate it for the type. */
20833 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20834 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20835 }
63d06c5c
DC
20836 }
20837 }
c906108c
SS
20838 break;
20839 case DW_TAG_typedef:
f1e6e072 20840 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20841 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20842 list_to_add = cu->list_in_scope;
63d06c5c 20843 break;
c906108c 20844 case DW_TAG_base_type:
a02abb62 20845 case DW_TAG_subrange_type:
f1e6e072 20846 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20847 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20848 list_to_add = cu->list_in_scope;
c906108c
SS
20849 break;
20850 case DW_TAG_enumerator:
e142c38c 20851 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20852 if (attr != nullptr)
c906108c 20853 {
e7c27a73 20854 dwarf2_const_value (attr, sym, cu);
c906108c 20855 }
63d06c5c
DC
20856 {
20857 /* NOTE: carlton/2003-11-10: See comment above in the
20858 DW_TAG_class_type, etc. block. */
20859
804d2729 20860 list_to_add
c24bdb02 20861 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20862 && cu->language == language_cplus
c24bdb02 20863 ? cu->get_builder ()->get_global_symbols ()
804d2729 20864 : cu->list_in_scope);
63d06c5c 20865 }
c906108c 20866 break;
74921315 20867 case DW_TAG_imported_declaration:
5c4e30ca 20868 case DW_TAG_namespace:
f1e6e072 20869 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20870 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20871 break;
530e8392
KB
20872 case DW_TAG_module:
20873 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20874 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20875 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20876 break;
4357ac6c 20877 case DW_TAG_common_block:
f1e6e072 20878 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20879 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20880 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20881 break;
c906108c
SS
20882 default:
20883 /* Not a tag we recognize. Hopefully we aren't processing
20884 trash data, but since we must specifically ignore things
20885 we don't recognize, there is nothing else we should do at
0963b4bd 20886 this point. */
b98664d3 20887 complaint (_("unsupported tag: '%s'"),
4d3c2250 20888 dwarf_tag_name (die->tag));
c906108c
SS
20889 break;
20890 }
df8a16a1 20891
e37fd15a
SW
20892 if (suppress_add)
20893 {
20894 sym->hash_next = objfile->template_symbols;
20895 objfile->template_symbols = sym;
20896 list_to_add = NULL;
20897 }
20898
20899 if (list_to_add != NULL)
d3cb6808 20900 add_symbol_to_list (sym, list_to_add);
e37fd15a 20901
df8a16a1
DJ
20902 /* For the benefit of old versions of GCC, check for anonymous
20903 namespaces based on the demangled name. */
4d4ec4e5 20904 if (!cu->processing_has_namespace_info
94af9270 20905 && cu->language == language_cplus)
c24bdb02 20906 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20907 }
20908 return (sym);
20909}
20910
98bfdba5
PA
20911/* Given an attr with a DW_FORM_dataN value in host byte order,
20912 zero-extend it as appropriate for the symbol's type. The DWARF
20913 standard (v4) is not entirely clear about the meaning of using
20914 DW_FORM_dataN for a constant with a signed type, where the type is
20915 wider than the data. The conclusion of a discussion on the DWARF
20916 list was that this is unspecified. We choose to always zero-extend
20917 because that is the interpretation long in use by GCC. */
c906108c 20918
98bfdba5 20919static gdb_byte *
ff39bb5e 20920dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20921 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20922{
518817b3 20923 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20924 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20925 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20926 LONGEST l = DW_UNSND (attr);
20927
20928 if (bits < sizeof (*value) * 8)
20929 {
20930 l &= ((LONGEST) 1 << bits) - 1;
20931 *value = l;
20932 }
20933 else if (bits == sizeof (*value) * 8)
20934 *value = l;
20935 else
20936 {
224c3ddb 20937 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20938 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20939 return bytes;
20940 }
20941
20942 return NULL;
20943}
20944
20945/* Read a constant value from an attribute. Either set *VALUE, or if
20946 the value does not fit in *VALUE, set *BYTES - either already
20947 allocated on the objfile obstack, or newly allocated on OBSTACK,
20948 or, set *BATON, if we translated the constant to a location
20949 expression. */
20950
20951static void
ff39bb5e 20952dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20953 const char *name, struct obstack *obstack,
20954 struct dwarf2_cu *cu,
d521ce57 20955 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20956 struct dwarf2_locexpr_baton **baton)
20957{
518817b3 20958 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20959 struct comp_unit_head *cu_header = &cu->header;
c906108c 20960 struct dwarf_block *blk;
98bfdba5
PA
20961 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20962 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20963
20964 *value = 0;
20965 *bytes = NULL;
20966 *baton = NULL;
c906108c
SS
20967
20968 switch (attr->form)
20969 {
20970 case DW_FORM_addr:
336d760d 20971 case DW_FORM_addrx:
3019eac3 20972 case DW_FORM_GNU_addr_index:
ac56253d 20973 {
ac56253d
TT
20974 gdb_byte *data;
20975
98bfdba5
PA
20976 if (TYPE_LENGTH (type) != cu_header->addr_size)
20977 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20978 cu_header->addr_size,
98bfdba5 20979 TYPE_LENGTH (type));
ac56253d
TT
20980 /* Symbols of this form are reasonably rare, so we just
20981 piggyback on the existing location code rather than writing
20982 a new implementation of symbol_computed_ops. */
8d749320 20983 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20984 (*baton)->per_cu = cu->per_cu;
20985 gdb_assert ((*baton)->per_cu);
ac56253d 20986
98bfdba5 20987 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20988 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20989 (*baton)->data = data;
ac56253d
TT
20990
20991 data[0] = DW_OP_addr;
20992 store_unsigned_integer (&data[1], cu_header->addr_size,
20993 byte_order, DW_ADDR (attr));
20994 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20995 }
c906108c 20996 break;
4ac36638 20997 case DW_FORM_string:
93b5768b 20998 case DW_FORM_strp:
cf532bd1 20999 case DW_FORM_strx:
3019eac3 21000 case DW_FORM_GNU_str_index:
36586728 21001 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21002 /* DW_STRING is already allocated on the objfile obstack, point
21003 directly to it. */
d521ce57 21004 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21005 break;
c906108c
SS
21006 case DW_FORM_block1:
21007 case DW_FORM_block2:
21008 case DW_FORM_block4:
21009 case DW_FORM_block:
2dc7f7b3 21010 case DW_FORM_exprloc:
0224619f 21011 case DW_FORM_data16:
c906108c 21012 blk = DW_BLOCK (attr);
98bfdba5
PA
21013 if (TYPE_LENGTH (type) != blk->size)
21014 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21015 TYPE_LENGTH (type));
21016 *bytes = blk->data;
c906108c 21017 break;
2df3850c
JM
21018
21019 /* The DW_AT_const_value attributes are supposed to carry the
21020 symbol's value "represented as it would be on the target
21021 architecture." By the time we get here, it's already been
21022 converted to host endianness, so we just need to sign- or
21023 zero-extend it as appropriate. */
21024 case DW_FORM_data1:
3aef2284 21025 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21026 break;
c906108c 21027 case DW_FORM_data2:
3aef2284 21028 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21029 break;
c906108c 21030 case DW_FORM_data4:
3aef2284 21031 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21032 break;
c906108c 21033 case DW_FORM_data8:
3aef2284 21034 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21035 break;
21036
c906108c 21037 case DW_FORM_sdata:
663c44ac 21038 case DW_FORM_implicit_const:
98bfdba5 21039 *value = DW_SND (attr);
2df3850c
JM
21040 break;
21041
c906108c 21042 case DW_FORM_udata:
98bfdba5 21043 *value = DW_UNSND (attr);
c906108c 21044 break;
2df3850c 21045
c906108c 21046 default:
b98664d3 21047 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21048 dwarf_form_name (attr->form));
98bfdba5 21049 *value = 0;
c906108c
SS
21050 break;
21051 }
21052}
21053
2df3850c 21054
98bfdba5
PA
21055/* Copy constant value from an attribute to a symbol. */
21056
2df3850c 21057static void
ff39bb5e 21058dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21059 struct dwarf2_cu *cu)
2df3850c 21060{
518817b3 21061 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21062 LONGEST value;
d521ce57 21063 const gdb_byte *bytes;
98bfdba5 21064 struct dwarf2_locexpr_baton *baton;
2df3850c 21065
98bfdba5 21066 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21067 sym->print_name (),
98bfdba5
PA
21068 &objfile->objfile_obstack, cu,
21069 &value, &bytes, &baton);
2df3850c 21070
98bfdba5
PA
21071 if (baton != NULL)
21072 {
98bfdba5 21073 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21074 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21075 }
21076 else if (bytes != NULL)
21077 {
21078 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21079 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21080 }
21081 else
21082 {
21083 SYMBOL_VALUE (sym) = value;
f1e6e072 21084 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21085 }
2df3850c
JM
21086}
21087
c906108c
SS
21088/* Return the type of the die in question using its DW_AT_type attribute. */
21089
21090static struct type *
e7c27a73 21091die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21092{
c906108c 21093 struct attribute *type_attr;
c906108c 21094
e142c38c 21095 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21096 if (!type_attr)
21097 {
518817b3 21098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21099 /* A missing DW_AT_type represents a void type. */
518817b3 21100 return objfile_type (objfile)->builtin_void;
c906108c 21101 }
348e048f 21102
673bfd45 21103 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21104}
21105
b4ba55a1
JB
21106/* True iff CU's producer generates GNAT Ada auxiliary information
21107 that allows to find parallel types through that information instead
21108 of having to do expensive parallel lookups by type name. */
21109
21110static int
21111need_gnat_info (struct dwarf2_cu *cu)
21112{
de4cb04a
JB
21113 /* Assume that the Ada compiler was GNAT, which always produces
21114 the auxiliary information. */
21115 return (cu->language == language_ada);
b4ba55a1
JB
21116}
21117
b4ba55a1
JB
21118/* Return the auxiliary type of the die in question using its
21119 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21120 attribute is not present. */
21121
21122static struct type *
21123die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21124{
b4ba55a1 21125 struct attribute *type_attr;
b4ba55a1
JB
21126
21127 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21128 if (!type_attr)
21129 return NULL;
21130
673bfd45 21131 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21132}
21133
21134/* If DIE has a descriptive_type attribute, then set the TYPE's
21135 descriptive type accordingly. */
21136
21137static void
21138set_descriptive_type (struct type *type, struct die_info *die,
21139 struct dwarf2_cu *cu)
21140{
21141 struct type *descriptive_type = die_descriptive_type (die, cu);
21142
21143 if (descriptive_type)
21144 {
21145 ALLOCATE_GNAT_AUX_TYPE (type);
21146 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21147 }
21148}
21149
c906108c
SS
21150/* Return the containing type of the die in question using its
21151 DW_AT_containing_type attribute. */
21152
21153static struct type *
e7c27a73 21154die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21155{
c906108c 21156 struct attribute *type_attr;
518817b3 21157 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21158
e142c38c 21159 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21160 if (!type_attr)
21161 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21162 "[in module %s]"), objfile_name (objfile));
33ac96f0 21163
673bfd45 21164 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21165}
21166
ac9ec31b
DE
21167/* Return an error marker type to use for the ill formed type in DIE/CU. */
21168
21169static struct type *
21170build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21171{
518817b3
SM
21172 struct dwarf2_per_objfile *dwarf2_per_objfile
21173 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21174 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21175 char *saved;
ac9ec31b 21176
528e1572
SM
21177 std::string message
21178 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21179 objfile_name (objfile),
21180 sect_offset_str (cu->header.sect_off),
21181 sect_offset_str (die->sect_off));
efba19b0 21182 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21183
19f392bc 21184 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21185}
21186
673bfd45 21187/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21188 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21189 DW_AT_containing_type.
673bfd45
DE
21190 If there is no type substitute an error marker. */
21191
c906108c 21192static struct type *
ff39bb5e 21193lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21194 struct dwarf2_cu *cu)
c906108c 21195{
518817b3
SM
21196 struct dwarf2_per_objfile *dwarf2_per_objfile
21197 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21198 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21199 struct type *this_type;
21200
ac9ec31b
DE
21201 gdb_assert (attr->name == DW_AT_type
21202 || attr->name == DW_AT_GNAT_descriptive_type
21203 || attr->name == DW_AT_containing_type);
21204
673bfd45
DE
21205 /* First see if we have it cached. */
21206
36586728
TT
21207 if (attr->form == DW_FORM_GNU_ref_alt)
21208 {
21209 struct dwarf2_per_cu_data *per_cu;
9c541725 21210 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21211
ed2dc618
SM
21212 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21213 dwarf2_per_objfile);
9c541725 21214 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21215 }
cd6c91b4 21216 else if (attr->form_is_ref ())
673bfd45 21217 {
9c541725 21218 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21219
9c541725 21220 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21221 }
55f1336d 21222 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21223 {
ac9ec31b 21224 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21225
ac9ec31b 21226 return get_signatured_type (die, signature, cu);
673bfd45
DE
21227 }
21228 else
21229 {
b98664d3 21230 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21231 " at %s [in module %s]"),
21232 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21233 objfile_name (objfile));
ac9ec31b 21234 return build_error_marker_type (cu, die);
673bfd45
DE
21235 }
21236
21237 /* If not cached we need to read it in. */
21238
21239 if (this_type == NULL)
21240 {
ac9ec31b 21241 struct die_info *type_die = NULL;
673bfd45
DE
21242 struct dwarf2_cu *type_cu = cu;
21243
cd6c91b4 21244 if (attr->form_is_ref ())
ac9ec31b
DE
21245 type_die = follow_die_ref (die, attr, &type_cu);
21246 if (type_die == NULL)
21247 return build_error_marker_type (cu, die);
21248 /* If we find the type now, it's probably because the type came
3019eac3
DE
21249 from an inter-CU reference and the type's CU got expanded before
21250 ours. */
ac9ec31b 21251 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21252 }
21253
21254 /* If we still don't have a type use an error marker. */
21255
21256 if (this_type == NULL)
ac9ec31b 21257 return build_error_marker_type (cu, die);
673bfd45 21258
f792889a 21259 return this_type;
c906108c
SS
21260}
21261
673bfd45
DE
21262/* Return the type in DIE, CU.
21263 Returns NULL for invalid types.
21264
02142a6c 21265 This first does a lookup in die_type_hash,
673bfd45
DE
21266 and only reads the die in if necessary.
21267
21268 NOTE: This can be called when reading in partial or full symbols. */
21269
f792889a 21270static struct type *
e7c27a73 21271read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21272{
f792889a
DJ
21273 struct type *this_type;
21274
21275 this_type = get_die_type (die, cu);
21276 if (this_type)
21277 return this_type;
21278
673bfd45
DE
21279 return read_type_die_1 (die, cu);
21280}
21281
21282/* Read the type in DIE, CU.
21283 Returns NULL for invalid types. */
21284
21285static struct type *
21286read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21287{
21288 struct type *this_type = NULL;
21289
c906108c
SS
21290 switch (die->tag)
21291 {
21292 case DW_TAG_class_type:
680b30c7 21293 case DW_TAG_interface_type:
c906108c
SS
21294 case DW_TAG_structure_type:
21295 case DW_TAG_union_type:
f792889a 21296 this_type = read_structure_type (die, cu);
c906108c
SS
21297 break;
21298 case DW_TAG_enumeration_type:
f792889a 21299 this_type = read_enumeration_type (die, cu);
c906108c
SS
21300 break;
21301 case DW_TAG_subprogram:
21302 case DW_TAG_subroutine_type:
edb3359d 21303 case DW_TAG_inlined_subroutine:
f792889a 21304 this_type = read_subroutine_type (die, cu);
c906108c
SS
21305 break;
21306 case DW_TAG_array_type:
f792889a 21307 this_type = read_array_type (die, cu);
c906108c 21308 break;
72019c9c 21309 case DW_TAG_set_type:
f792889a 21310 this_type = read_set_type (die, cu);
72019c9c 21311 break;
c906108c 21312 case DW_TAG_pointer_type:
f792889a 21313 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21314 break;
21315 case DW_TAG_ptr_to_member_type:
f792889a 21316 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21317 break;
21318 case DW_TAG_reference_type:
4297a3f0
AV
21319 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21320 break;
21321 case DW_TAG_rvalue_reference_type:
21322 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21323 break;
21324 case DW_TAG_const_type:
f792889a 21325 this_type = read_tag_const_type (die, cu);
c906108c
SS
21326 break;
21327 case DW_TAG_volatile_type:
f792889a 21328 this_type = read_tag_volatile_type (die, cu);
c906108c 21329 break;
06d66ee9
TT
21330 case DW_TAG_restrict_type:
21331 this_type = read_tag_restrict_type (die, cu);
21332 break;
c906108c 21333 case DW_TAG_string_type:
f792889a 21334 this_type = read_tag_string_type (die, cu);
c906108c
SS
21335 break;
21336 case DW_TAG_typedef:
f792889a 21337 this_type = read_typedef (die, cu);
c906108c 21338 break;
a02abb62 21339 case DW_TAG_subrange_type:
f792889a 21340 this_type = read_subrange_type (die, cu);
a02abb62 21341 break;
c906108c 21342 case DW_TAG_base_type:
f792889a 21343 this_type = read_base_type (die, cu);
c906108c 21344 break;
81a17f79 21345 case DW_TAG_unspecified_type:
f792889a 21346 this_type = read_unspecified_type (die, cu);
81a17f79 21347 break;
0114d602
DJ
21348 case DW_TAG_namespace:
21349 this_type = read_namespace_type (die, cu);
21350 break;
f55ee35c
JK
21351 case DW_TAG_module:
21352 this_type = read_module_type (die, cu);
21353 break;
a2c2acaf
MW
21354 case DW_TAG_atomic_type:
21355 this_type = read_tag_atomic_type (die, cu);
21356 break;
c906108c 21357 default:
b98664d3 21358 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21359 dwarf_tag_name (die->tag));
c906108c
SS
21360 break;
21361 }
63d06c5c 21362
f792889a 21363 return this_type;
63d06c5c
DC
21364}
21365
abc72ce4
DE
21366/* See if we can figure out if the class lives in a namespace. We do
21367 this by looking for a member function; its demangled name will
21368 contain namespace info, if there is any.
21369 Return the computed name or NULL.
21370 Space for the result is allocated on the objfile's obstack.
21371 This is the full-die version of guess_partial_die_structure_name.
21372 In this case we know DIE has no useful parent. */
21373
43816ebc 21374static const char *
abc72ce4
DE
21375guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21376{
21377 struct die_info *spec_die;
21378 struct dwarf2_cu *spec_cu;
21379 struct die_info *child;
518817b3 21380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21381
21382 spec_cu = cu;
21383 spec_die = die_specification (die, &spec_cu);
21384 if (spec_die != NULL)
21385 {
21386 die = spec_die;
21387 cu = spec_cu;
21388 }
21389
21390 for (child = die->child;
21391 child != NULL;
21392 child = child->sibling)
21393 {
21394 if (child->tag == DW_TAG_subprogram)
21395 {
73b9be8b 21396 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21397
7d45c7c3 21398 if (linkage_name != NULL)
abc72ce4 21399 {
43816ebc
TT
21400 gdb::unique_xmalloc_ptr<char> actual_name
21401 (language_class_name_from_physname (cu->language_defn,
21402 linkage_name));
21403 const char *name = NULL;
abc72ce4
DE
21404
21405 if (actual_name != NULL)
21406 {
15d034d0 21407 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21408
21409 if (die_name != NULL
43816ebc 21410 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21411 {
21412 /* Strip off the class name from the full name.
21413 We want the prefix. */
21414 int die_name_len = strlen (die_name);
43816ebc
TT
21415 int actual_name_len = strlen (actual_name.get ());
21416 const char *ptr = actual_name.get ();
abc72ce4
DE
21417
21418 /* Test for '::' as a sanity check. */
21419 if (actual_name_len > die_name_len + 2
43816ebc 21420 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21421 name = obstack_strndup (
e3b94546 21422 &objfile->per_bfd->storage_obstack,
43816ebc 21423 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21424 }
21425 }
abc72ce4
DE
21426 return name;
21427 }
21428 }
21429 }
21430
21431 return NULL;
21432}
21433
96408a79
SA
21434/* GCC might emit a nameless typedef that has a linkage name. Determine the
21435 prefix part in such case. See
21436 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21437
a121b7c1 21438static const char *
96408a79
SA
21439anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21440{
21441 struct attribute *attr;
e6a959d6 21442 const char *base;
96408a79
SA
21443
21444 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21445 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21446 return NULL;
21447
7d45c7c3 21448 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21449 return NULL;
21450
73b9be8b 21451 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21452 if (attr == NULL || DW_STRING (attr) == NULL)
21453 return NULL;
21454
21455 /* dwarf2_name had to be already called. */
21456 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21457
21458 /* Strip the base name, keep any leading namespaces/classes. */
21459 base = strrchr (DW_STRING (attr), ':');
21460 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21461 return "";
21462
518817b3 21463 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21464 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21465 DW_STRING (attr),
21466 &base[-1] - DW_STRING (attr));
96408a79
SA
21467}
21468
fdde2d81 21469/* Return the name of the namespace/class that DIE is defined within,
0114d602 21470 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21471
0114d602
DJ
21472 For example, if we're within the method foo() in the following
21473 code:
21474
21475 namespace N {
21476 class C {
21477 void foo () {
21478 }
21479 };
21480 }
21481
21482 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21483
0d5cff50 21484static const char *
e142c38c 21485determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21486{
518817b3
SM
21487 struct dwarf2_per_objfile *dwarf2_per_objfile
21488 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21489 struct die_info *parent, *spec_die;
21490 struct dwarf2_cu *spec_cu;
21491 struct type *parent_type;
a121b7c1 21492 const char *retval;
63d06c5c 21493
9c37b5ae 21494 if (cu->language != language_cplus
c44af4eb
TT
21495 && cu->language != language_fortran && cu->language != language_d
21496 && cu->language != language_rust)
0114d602
DJ
21497 return "";
21498
96408a79
SA
21499 retval = anonymous_struct_prefix (die, cu);
21500 if (retval)
21501 return retval;
21502
0114d602
DJ
21503 /* We have to be careful in the presence of DW_AT_specification.
21504 For example, with GCC 3.4, given the code
21505
21506 namespace N {
21507 void foo() {
21508 // Definition of N::foo.
21509 }
21510 }
21511
21512 then we'll have a tree of DIEs like this:
21513
21514 1: DW_TAG_compile_unit
21515 2: DW_TAG_namespace // N
21516 3: DW_TAG_subprogram // declaration of N::foo
21517 4: DW_TAG_subprogram // definition of N::foo
21518 DW_AT_specification // refers to die #3
21519
21520 Thus, when processing die #4, we have to pretend that we're in
21521 the context of its DW_AT_specification, namely the contex of die
21522 #3. */
21523 spec_cu = cu;
21524 spec_die = die_specification (die, &spec_cu);
21525 if (spec_die == NULL)
21526 parent = die->parent;
21527 else
63d06c5c 21528 {
0114d602
DJ
21529 parent = spec_die->parent;
21530 cu = spec_cu;
63d06c5c 21531 }
0114d602
DJ
21532
21533 if (parent == NULL)
21534 return "";
98bfdba5
PA
21535 else if (parent->building_fullname)
21536 {
21537 const char *name;
21538 const char *parent_name;
21539
21540 /* It has been seen on RealView 2.2 built binaries,
21541 DW_TAG_template_type_param types actually _defined_ as
21542 children of the parent class:
21543
21544 enum E {};
21545 template class <class Enum> Class{};
21546 Class<enum E> class_e;
21547
21548 1: DW_TAG_class_type (Class)
21549 2: DW_TAG_enumeration_type (E)
21550 3: DW_TAG_enumerator (enum1:0)
21551 3: DW_TAG_enumerator (enum2:1)
21552 ...
21553 2: DW_TAG_template_type_param
21554 DW_AT_type DW_FORM_ref_udata (E)
21555
21556 Besides being broken debug info, it can put GDB into an
21557 infinite loop. Consider:
21558
21559 When we're building the full name for Class<E>, we'll start
21560 at Class, and go look over its template type parameters,
21561 finding E. We'll then try to build the full name of E, and
21562 reach here. We're now trying to build the full name of E,
21563 and look over the parent DIE for containing scope. In the
21564 broken case, if we followed the parent DIE of E, we'd again
21565 find Class, and once again go look at its template type
21566 arguments, etc., etc. Simply don't consider such parent die
21567 as source-level parent of this die (it can't be, the language
21568 doesn't allow it), and break the loop here. */
21569 name = dwarf2_name (die, cu);
21570 parent_name = dwarf2_name (parent, cu);
b98664d3 21571 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21572 name ? name : "<unknown>",
21573 parent_name ? parent_name : "<unknown>");
21574 return "";
21575 }
63d06c5c 21576 else
0114d602
DJ
21577 switch (parent->tag)
21578 {
63d06c5c 21579 case DW_TAG_namespace:
0114d602 21580 parent_type = read_type_die (parent, cu);
acebe513
UW
21581 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21582 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21583 Work around this problem here. */
21584 if (cu->language == language_cplus
e86ca25f 21585 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21586 return "";
0114d602 21587 /* We give a name to even anonymous namespaces. */
e86ca25f 21588 return TYPE_NAME (parent_type);
63d06c5c 21589 case DW_TAG_class_type:
680b30c7 21590 case DW_TAG_interface_type:
63d06c5c 21591 case DW_TAG_structure_type:
0114d602 21592 case DW_TAG_union_type:
f55ee35c 21593 case DW_TAG_module:
0114d602 21594 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21595 if (TYPE_NAME (parent_type) != NULL)
21596 return TYPE_NAME (parent_type);
0114d602
DJ
21597 else
21598 /* An anonymous structure is only allowed non-static data
21599 members; no typedefs, no member functions, et cetera.
21600 So it does not need a prefix. */
21601 return "";
abc72ce4 21602 case DW_TAG_compile_unit:
95554aad 21603 case DW_TAG_partial_unit:
abc72ce4
DE
21604 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21605 if (cu->language == language_cplus
fd5866f6 21606 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21607 && die->child != NULL
21608 && (die->tag == DW_TAG_class_type
21609 || die->tag == DW_TAG_structure_type
21610 || die->tag == DW_TAG_union_type))
21611 {
43816ebc 21612 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21613 if (name != NULL)
21614 return name;
21615 }
21616 return "";
0a4b0913
AB
21617 case DW_TAG_subprogram:
21618 /* Nested subroutines in Fortran get a prefix with the name
21619 of the parent's subroutine. */
21620 if (cu->language == language_fortran)
21621 {
21622 if ((die->tag == DW_TAG_subprogram)
21623 && (dwarf2_name (parent, cu) != NULL))
21624 return dwarf2_name (parent, cu);
21625 }
21626 return determine_prefix (parent, cu);
3d567982
TT
21627 case DW_TAG_enumeration_type:
21628 parent_type = read_type_die (parent, cu);
21629 if (TYPE_DECLARED_CLASS (parent_type))
21630 {
e86ca25f
TT
21631 if (TYPE_NAME (parent_type) != NULL)
21632 return TYPE_NAME (parent_type);
3d567982
TT
21633 return "";
21634 }
21635 /* Fall through. */
63d06c5c 21636 default:
8176b9b8 21637 return determine_prefix (parent, cu);
63d06c5c 21638 }
63d06c5c
DC
21639}
21640
3e43a32a
MS
21641/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21642 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21643 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21644 an obconcat, otherwise allocate storage for the result. The CU argument is
21645 used to determine the language and hence, the appropriate separator. */
987504bb 21646
f55ee35c 21647#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21648
21649static char *
f55ee35c
JK
21650typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21651 int physname, struct dwarf2_cu *cu)
63d06c5c 21652{
f55ee35c 21653 const char *lead = "";
5c315b68 21654 const char *sep;
63d06c5c 21655
3e43a32a
MS
21656 if (suffix == NULL || suffix[0] == '\0'
21657 || prefix == NULL || prefix[0] == '\0')
987504bb 21658 sep = "";
45280282
IB
21659 else if (cu->language == language_d)
21660 {
21661 /* For D, the 'main' function could be defined in any module, but it
21662 should never be prefixed. */
21663 if (strcmp (suffix, "D main") == 0)
21664 {
21665 prefix = "";
21666 sep = "";
21667 }
21668 else
21669 sep = ".";
21670 }
f55ee35c
JK
21671 else if (cu->language == language_fortran && physname)
21672 {
21673 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21674 DW_AT_MIPS_linkage_name is preferred and used instead. */
21675
21676 lead = "__";
21677 sep = "_MOD_";
21678 }
987504bb
JJ
21679 else
21680 sep = "::";
63d06c5c 21681
6dd47d34
DE
21682 if (prefix == NULL)
21683 prefix = "";
21684 if (suffix == NULL)
21685 suffix = "";
21686
987504bb
JJ
21687 if (obs == NULL)
21688 {
3e43a32a 21689 char *retval
224c3ddb
SM
21690 = ((char *)
21691 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21692
f55ee35c
JK
21693 strcpy (retval, lead);
21694 strcat (retval, prefix);
6dd47d34
DE
21695 strcat (retval, sep);
21696 strcat (retval, suffix);
63d06c5c
DC
21697 return retval;
21698 }
987504bb
JJ
21699 else
21700 {
21701 /* We have an obstack. */
f55ee35c 21702 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21703 }
63d06c5c
DC
21704}
21705
c906108c
SS
21706/* Return sibling of die, NULL if no sibling. */
21707
f9aca02d 21708static struct die_info *
fba45db2 21709sibling_die (struct die_info *die)
c906108c 21710{
639d11d3 21711 return die->sibling;
c906108c
SS
21712}
21713
71c25dea
TT
21714/* Get name of a die, return NULL if not found. */
21715
15d034d0
TT
21716static const char *
21717dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
21718 struct obstack *obstack)
21719{
21720 if (name && cu->language == language_cplus)
21721 {
2f408ecb 21722 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21723
2f408ecb 21724 if (!canon_name.empty ())
71c25dea 21725 {
2f408ecb 21726 if (canon_name != name)
efba19b0 21727 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
21728 }
21729 }
21730
21731 return name;
c906108c
SS
21732}
21733
96553a0c
DE
21734/* Get name of a die, return NULL if not found.
21735 Anonymous namespaces are converted to their magic string. */
9219021c 21736
15d034d0 21737static const char *
e142c38c 21738dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21739{
21740 struct attribute *attr;
518817b3 21741 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21742
e142c38c 21743 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21744 if ((!attr || !DW_STRING (attr))
96553a0c 21745 && die->tag != DW_TAG_namespace
53832f31
TT
21746 && die->tag != DW_TAG_class_type
21747 && die->tag != DW_TAG_interface_type
21748 && die->tag != DW_TAG_structure_type
21749 && die->tag != DW_TAG_union_type)
71c25dea
TT
21750 return NULL;
21751
21752 switch (die->tag)
21753 {
21754 case DW_TAG_compile_unit:
95554aad 21755 case DW_TAG_partial_unit:
71c25dea
TT
21756 /* Compilation units have a DW_AT_name that is a filename, not
21757 a source language identifier. */
21758 case DW_TAG_enumeration_type:
21759 case DW_TAG_enumerator:
21760 /* These tags always have simple identifiers already; no need
21761 to canonicalize them. */
21762 return DW_STRING (attr);
907af001 21763
96553a0c
DE
21764 case DW_TAG_namespace:
21765 if (attr != NULL && DW_STRING (attr) != NULL)
21766 return DW_STRING (attr);
21767 return CP_ANONYMOUS_NAMESPACE_STR;
21768
907af001
UW
21769 case DW_TAG_class_type:
21770 case DW_TAG_interface_type:
21771 case DW_TAG_structure_type:
21772 case DW_TAG_union_type:
21773 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21774 structures or unions. These were of the form "._%d" in GCC 4.1,
21775 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21776 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21777 if (attr && DW_STRING (attr)
61012eef
GB
21778 && (startswith (DW_STRING (attr), "._")
21779 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21780 return NULL;
53832f31
TT
21781
21782 /* GCC might emit a nameless typedef that has a linkage name. See
21783 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21784 if (!attr || DW_STRING (attr) == NULL)
21785 {
73b9be8b 21786 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21787 if (attr == NULL || DW_STRING (attr) == NULL)
21788 return NULL;
21789
df5c6c50
JK
21790 /* Avoid demangling DW_STRING (attr) the second time on a second
21791 call for the same DIE. */
21792 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21793 {
43816ebc
TT
21794 gdb::unique_xmalloc_ptr<char> demangled
21795 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21796 if (demangled == nullptr)
21797 return nullptr;
43816ebc 21798
e6a959d6 21799 const char *base;
96408a79 21800
53832f31 21801 /* FIXME: we already did this for the partial symbol... */
34a68019 21802 DW_STRING (attr)
021887d8 21803 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 21804 demangled.get ());
53832f31 21805 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
21806
21807 /* Strip any leading namespaces/classes, keep only the base name.
21808 DW_AT_name for named DIEs does not contain the prefixes. */
21809 base = strrchr (DW_STRING (attr), ':');
21810 if (base && base > DW_STRING (attr) && base[-1] == ':')
21811 return &base[1];
21812 else
21813 return DW_STRING (attr);
53832f31
TT
21814 }
21815 }
907af001
UW
21816 break;
21817
71c25dea 21818 default:
907af001
UW
21819 break;
21820 }
21821
21822 if (!DW_STRING_IS_CANONICAL (attr))
21823 {
21824 DW_STRING (attr)
21825 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 21826 &objfile->per_bfd->storage_obstack);
907af001 21827 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21828 }
907af001 21829 return DW_STRING (attr);
9219021c
DC
21830}
21831
21832/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21833 is none. *EXT_CU is the CU containing DIE on input, and the CU
21834 containing the return value on output. */
9219021c
DC
21835
21836static struct die_info *
f2f0e013 21837dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21838{
21839 struct attribute *attr;
9219021c 21840
f2f0e013 21841 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21842 if (attr == NULL)
21843 return NULL;
21844
f2f0e013 21845 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21846}
21847
fa9c3fa0
TT
21848/* A convenience function that returns an "unknown" DWARF name,
21849 including the value of V. STR is the name of the entity being
21850 printed, e.g., "TAG". */
21851
21852static const char *
21853dwarf_unknown (const char *str, unsigned v)
21854{
21855 char *cell = get_print_cell ();
21856 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21857 return cell;
21858}
21859
c906108c
SS
21860/* Convert a DIE tag into its string name. */
21861
f39c6ffd 21862static const char *
aa1ee363 21863dwarf_tag_name (unsigned tag)
c906108c 21864{
f39c6ffd
TT
21865 const char *name = get_DW_TAG_name (tag);
21866
21867 if (name == NULL)
fa9c3fa0 21868 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
21869
21870 return name;
c906108c
SS
21871}
21872
21873/* Convert a DWARF attribute code into its string name. */
21874
f39c6ffd 21875static const char *
aa1ee363 21876dwarf_attr_name (unsigned attr)
c906108c 21877{
f39c6ffd
TT
21878 const char *name;
21879
c764a876 21880#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21881 if (attr == DW_AT_MIPS_fde)
21882 return "DW_AT_MIPS_fde";
21883#else
21884 if (attr == DW_AT_HP_block_index)
21885 return "DW_AT_HP_block_index";
c764a876 21886#endif
f39c6ffd
TT
21887
21888 name = get_DW_AT_name (attr);
21889
21890 if (name == NULL)
fa9c3fa0 21891 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
21892
21893 return name;
c906108c
SS
21894}
21895
21896/* Convert a DWARF value form code into its string name. */
21897
f39c6ffd 21898static const char *
aa1ee363 21899dwarf_form_name (unsigned form)
c906108c 21900{
f39c6ffd
TT
21901 const char *name = get_DW_FORM_name (form);
21902
21903 if (name == NULL)
fa9c3fa0 21904 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
21905
21906 return name;
c906108c
SS
21907}
21908
a121b7c1 21909static const char *
fba45db2 21910dwarf_bool_name (unsigned mybool)
c906108c
SS
21911{
21912 if (mybool)
21913 return "TRUE";
21914 else
21915 return "FALSE";
21916}
21917
21918/* Convert a DWARF type code into its string name. */
21919
f39c6ffd 21920static const char *
aa1ee363 21921dwarf_type_encoding_name (unsigned enc)
c906108c 21922{
f39c6ffd 21923 const char *name = get_DW_ATE_name (enc);
c906108c 21924
f39c6ffd 21925 if (name == NULL)
fa9c3fa0 21926 return dwarf_unknown ("ATE", enc);
c906108c 21927
f39c6ffd 21928 return name;
c906108c 21929}
c906108c 21930
f9aca02d 21931static void
d97bc12b 21932dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21933{
21934 unsigned int i;
21935
d97bc12b 21936 print_spaces (indent, f);
9d8780f0 21937 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21938 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21939 sect_offset_str (die->sect_off));
d97bc12b
DE
21940
21941 if (die->parent != NULL)
21942 {
21943 print_spaces (indent, f);
9d8780f0
SM
21944 fprintf_unfiltered (f, " parent at offset: %s\n",
21945 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21946 }
21947
21948 print_spaces (indent, f);
21949 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21950 dwarf_bool_name (die->child != NULL));
c906108c 21951
d97bc12b
DE
21952 print_spaces (indent, f);
21953 fprintf_unfiltered (f, " attributes:\n");
21954
c906108c
SS
21955 for (i = 0; i < die->num_attrs; ++i)
21956 {
d97bc12b
DE
21957 print_spaces (indent, f);
21958 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21959 dwarf_attr_name (die->attrs[i].name),
21960 dwarf_form_name (die->attrs[i].form));
d97bc12b 21961
c906108c
SS
21962 switch (die->attrs[i].form)
21963 {
c906108c 21964 case DW_FORM_addr:
336d760d 21965 case DW_FORM_addrx:
3019eac3 21966 case DW_FORM_GNU_addr_index:
d97bc12b 21967 fprintf_unfiltered (f, "address: ");
5af949e3 21968 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21969 break;
21970 case DW_FORM_block2:
21971 case DW_FORM_block4:
21972 case DW_FORM_block:
21973 case DW_FORM_block1:
56eb65bd
SP
21974 fprintf_unfiltered (f, "block: size %s",
21975 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21976 break;
2dc7f7b3 21977 case DW_FORM_exprloc:
56eb65bd
SP
21978 fprintf_unfiltered (f, "expression: size %s",
21979 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21980 break;
0224619f
JK
21981 case DW_FORM_data16:
21982 fprintf_unfiltered (f, "constant of 16 bytes");
21983 break;
4568ecf9
DE
21984 case DW_FORM_ref_addr:
21985 fprintf_unfiltered (f, "ref address: ");
21986 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21987 break;
36586728
TT
21988 case DW_FORM_GNU_ref_alt:
21989 fprintf_unfiltered (f, "alt ref address: ");
21990 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21991 break;
10b3939b
DJ
21992 case DW_FORM_ref1:
21993 case DW_FORM_ref2:
21994 case DW_FORM_ref4:
4568ecf9
DE
21995 case DW_FORM_ref8:
21996 case DW_FORM_ref_udata:
d97bc12b 21997 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21998 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21999 break;
c906108c
SS
22000 case DW_FORM_data1:
22001 case DW_FORM_data2:
22002 case DW_FORM_data4:
ce5d95e1 22003 case DW_FORM_data8:
c906108c
SS
22004 case DW_FORM_udata:
22005 case DW_FORM_sdata:
43bbcdc2
PH
22006 fprintf_unfiltered (f, "constant: %s",
22007 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22008 break;
2dc7f7b3
TT
22009 case DW_FORM_sec_offset:
22010 fprintf_unfiltered (f, "section offset: %s",
22011 pulongest (DW_UNSND (&die->attrs[i])));
22012 break;
55f1336d 22013 case DW_FORM_ref_sig8:
ac9ec31b
DE
22014 fprintf_unfiltered (f, "signature: %s",
22015 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22016 break;
c906108c 22017 case DW_FORM_string:
4bdf3d34 22018 case DW_FORM_strp:
43988095 22019 case DW_FORM_line_strp:
cf532bd1 22020 case DW_FORM_strx:
3019eac3 22021 case DW_FORM_GNU_str_index:
36586728 22022 case DW_FORM_GNU_strp_alt:
8285870a 22023 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22024 DW_STRING (&die->attrs[i])
8285870a
JK
22025 ? DW_STRING (&die->attrs[i]) : "",
22026 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22027 break;
22028 case DW_FORM_flag:
22029 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22030 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22031 else
d97bc12b 22032 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22033 break;
2dc7f7b3
TT
22034 case DW_FORM_flag_present:
22035 fprintf_unfiltered (f, "flag: TRUE");
22036 break;
a8329558 22037 case DW_FORM_indirect:
0963b4bd
MS
22038 /* The reader will have reduced the indirect form to
22039 the "base form" so this form should not occur. */
5f48f8f3 22040 fprintf_unfiltered (f,
3e43a32a 22041 "unexpected attribute form: DW_FORM_indirect");
a8329558 22042 break;
663c44ac
JK
22043 case DW_FORM_implicit_const:
22044 fprintf_unfiltered (f, "constant: %s",
22045 plongest (DW_SND (&die->attrs[i])));
22046 break;
c906108c 22047 default:
d97bc12b 22048 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22049 die->attrs[i].form);
d97bc12b 22050 break;
c906108c 22051 }
d97bc12b 22052 fprintf_unfiltered (f, "\n");
c906108c
SS
22053 }
22054}
22055
f9aca02d 22056static void
d97bc12b 22057dump_die_for_error (struct die_info *die)
c906108c 22058{
d97bc12b
DE
22059 dump_die_shallow (gdb_stderr, 0, die);
22060}
22061
22062static void
22063dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22064{
22065 int indent = level * 4;
22066
22067 gdb_assert (die != NULL);
22068
22069 if (level >= max_level)
22070 return;
22071
22072 dump_die_shallow (f, indent, die);
22073
22074 if (die->child != NULL)
c906108c 22075 {
d97bc12b
DE
22076 print_spaces (indent, f);
22077 fprintf_unfiltered (f, " Children:");
22078 if (level + 1 < max_level)
22079 {
22080 fprintf_unfiltered (f, "\n");
22081 dump_die_1 (f, level + 1, max_level, die->child);
22082 }
22083 else
22084 {
3e43a32a
MS
22085 fprintf_unfiltered (f,
22086 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22087 }
22088 }
22089
22090 if (die->sibling != NULL && level > 0)
22091 {
22092 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22093 }
22094}
22095
d97bc12b
DE
22096/* This is called from the pdie macro in gdbinit.in.
22097 It's not static so gcc will keep a copy callable from gdb. */
22098
22099void
22100dump_die (struct die_info *die, int max_level)
22101{
22102 dump_die_1 (gdb_stdlog, 0, max_level, die);
22103}
22104
f9aca02d 22105static void
51545339 22106store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22107{
51545339 22108 void **slot;
c906108c 22109
9c541725
PA
22110 slot = htab_find_slot_with_hash (cu->die_hash, die,
22111 to_underlying (die->sect_off),
b64f50a1 22112 INSERT);
51545339
DJ
22113
22114 *slot = die;
c906108c
SS
22115}
22116
b64f50a1
JK
22117/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22118 required kind. */
22119
22120static sect_offset
ff39bb5e 22121dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22122{
cd6c91b4 22123 if (attr->form_is_ref ())
9c541725 22124 return (sect_offset) DW_UNSND (attr);
93311388 22125
b98664d3 22126 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22127 dwarf_form_name (attr->form));
9c541725 22128 return {};
c906108c
SS
22129}
22130
43bbcdc2
PH
22131/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22132 * the value held by the attribute is not constant. */
a02abb62 22133
43bbcdc2 22134static LONGEST
ff39bb5e 22135dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22136{
663c44ac 22137 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22138 return DW_SND (attr);
22139 else if (attr->form == DW_FORM_udata
22140 || attr->form == DW_FORM_data1
22141 || attr->form == DW_FORM_data2
22142 || attr->form == DW_FORM_data4
22143 || attr->form == DW_FORM_data8)
22144 return DW_UNSND (attr);
22145 else
22146 {
cd6c91b4 22147 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22148 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22149 dwarf_form_name (attr->form));
22150 return default_value;
22151 }
22152}
22153
348e048f
DE
22154/* Follow reference or signature attribute ATTR of SRC_DIE.
22155 On entry *REF_CU is the CU of SRC_DIE.
22156 On exit *REF_CU is the CU of the result. */
22157
22158static struct die_info *
ff39bb5e 22159follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22160 struct dwarf2_cu **ref_cu)
22161{
22162 struct die_info *die;
22163
cd6c91b4 22164 if (attr->form_is_ref ())
348e048f 22165 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22166 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22167 die = follow_die_sig (src_die, attr, ref_cu);
22168 else
22169 {
22170 dump_die_for_error (src_die);
22171 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22172 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22173 }
22174
22175 return die;
03dd20cc
DJ
22176}
22177
5c631832 22178/* Follow reference OFFSET.
673bfd45
DE
22179 On entry *REF_CU is the CU of the source die referencing OFFSET.
22180 On exit *REF_CU is the CU of the result.
22181 Returns NULL if OFFSET is invalid. */
f504f079 22182
f9aca02d 22183static struct die_info *
9c541725 22184follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22185 struct dwarf2_cu **ref_cu)
c906108c 22186{
10b3939b 22187 struct die_info temp_die;
f2f0e013 22188 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22189 struct dwarf2_per_objfile *dwarf2_per_objfile
22190 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22191
348e048f
DE
22192 gdb_assert (cu->per_cu != NULL);
22193
98bfdba5
PA
22194 target_cu = cu;
22195
3019eac3 22196 if (cu->per_cu->is_debug_types)
348e048f
DE
22197 {
22198 /* .debug_types CUs cannot reference anything outside their CU.
22199 If they need to, they have to reference a signatured type via
55f1336d 22200 DW_FORM_ref_sig8. */
4057dfde 22201 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22202 return NULL;
348e048f 22203 }
36586728 22204 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22205 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22206 {
22207 struct dwarf2_per_cu_data *per_cu;
9a619af0 22208
9c541725 22209 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22210 dwarf2_per_objfile);
03dd20cc
DJ
22211
22212 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22213 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22214 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22215
10b3939b
DJ
22216 target_cu = per_cu->cu;
22217 }
98bfdba5
PA
22218 else if (cu->dies == NULL)
22219 {
22220 /* We're loading full DIEs during partial symbol reading. */
22221 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22222 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22223 }
c906108c 22224
f2f0e013 22225 *ref_cu = target_cu;
9c541725 22226 temp_die.sect_off = sect_off;
c24bdb02
KS
22227
22228 if (target_cu != cu)
22229 target_cu->ancestor = cu;
22230
9a3c8263 22231 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22232 &temp_die,
22233 to_underlying (sect_off));
5c631832 22234}
10b3939b 22235
5c631832
JK
22236/* Follow reference attribute ATTR of SRC_DIE.
22237 On entry *REF_CU is the CU of SRC_DIE.
22238 On exit *REF_CU is the CU of the result. */
22239
22240static struct die_info *
ff39bb5e 22241follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22242 struct dwarf2_cu **ref_cu)
22243{
9c541725 22244 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22245 struct dwarf2_cu *cu = *ref_cu;
22246 struct die_info *die;
22247
9c541725 22248 die = follow_die_offset (sect_off,
36586728
TT
22249 (attr->form == DW_FORM_GNU_ref_alt
22250 || cu->per_cu->is_dwz),
22251 ref_cu);
5c631832 22252 if (!die)
9d8780f0
SM
22253 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22254 "at %s [in module %s]"),
22255 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22256 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22257
5c631832
JK
22258 return die;
22259}
22260
d4c9a4f8 22261/* See read.h. */
5c631832
JK
22262
22263struct dwarf2_locexpr_baton
9c541725 22264dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22265 dwarf2_per_cu_data *per_cu,
8b9737bf 22266 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22267 void *baton, bool resolve_abstract_p)
5c631832 22268{
918dd910 22269 struct dwarf2_cu *cu;
5c631832
JK
22270 struct die_info *die;
22271 struct attribute *attr;
22272 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22273 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22274 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22275
918dd910 22276 if (per_cu->cu == NULL)
58f0c718 22277 load_cu (per_cu, false);
918dd910 22278 cu = per_cu->cu;
cc12ce38
DE
22279 if (cu == NULL)
22280 {
22281 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22282 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22283 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22284 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22285 }
918dd910 22286
9c541725 22287 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22288 if (!die)
9d8780f0
SM
22289 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22290 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22291
22292 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22293 if (!attr && resolve_abstract_p
3360b6e7 22294 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22295 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22296 {
22297 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22298 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22299 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22300
3360b6e7
TV
22301 for (const auto &cand_off
22302 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22303 {
3360b6e7
TV
22304 struct dwarf2_cu *cand_cu = cu;
22305 struct die_info *cand
22306 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22307 if (!cand
22308 || !cand->parent
e4a62c65
TV
22309 || cand->parent->tag != DW_TAG_subprogram)
22310 continue;
22311
22312 CORE_ADDR pc_low, pc_high;
22313 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22314 if (pc_low == ((CORE_ADDR) -1))
22315 continue;
22316 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22317 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22318 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22319 continue;
22320
22321 die = cand;
22322 attr = dwarf2_attr (die, DW_AT_location, cu);
22323 break;
22324 }
22325 }
22326
5c631832
JK
22327 if (!attr)
22328 {
e103e986
JK
22329 /* DWARF: "If there is no such attribute, then there is no effect.".
22330 DATA is ignored if SIZE is 0. */
5c631832 22331
e103e986 22332 retval.data = NULL;
5c631832
JK
22333 retval.size = 0;
22334 }
cd6c91b4 22335 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22336 {
22337 struct dwarf2_loclist_baton loclist_baton;
22338 CORE_ADDR pc = (*get_frame_pc) (baton);
22339 size_t size;
22340
22341 fill_in_loclist_baton (cu, &loclist_baton, attr);
22342
22343 retval.data = dwarf2_find_location_expression (&loclist_baton,
22344 &size, pc);
22345 retval.size = size;
22346 }
5c631832
JK
22347 else
22348 {
4fc6c0d5 22349 if (!attr->form_is_block ())
9d8780f0 22350 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22351 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22352 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22353
22354 retval.data = DW_BLOCK (attr)->data;
22355 retval.size = DW_BLOCK (attr)->size;
22356 }
22357 retval.per_cu = cu->per_cu;
918dd910 22358
ed2dc618 22359 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22360
5c631832 22361 return retval;
348e048f
DE
22362}
22363
d4c9a4f8 22364/* See read.h. */
8b9737bf
TT
22365
22366struct dwarf2_locexpr_baton
22367dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22368 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22369 CORE_ADDR (*get_frame_pc) (void *baton),
22370 void *baton)
22371{
9c541725 22372 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22373
9c541725 22374 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22375}
22376
b6807d98
TT
22377/* Write a constant of a given type as target-ordered bytes into
22378 OBSTACK. */
22379
22380static const gdb_byte *
22381write_constant_as_bytes (struct obstack *obstack,
22382 enum bfd_endian byte_order,
22383 struct type *type,
22384 ULONGEST value,
22385 LONGEST *len)
22386{
22387 gdb_byte *result;
22388
22389 *len = TYPE_LENGTH (type);
224c3ddb 22390 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22391 store_unsigned_integer (result, *len, byte_order, value);
22392
22393 return result;
22394}
22395
d4c9a4f8 22396/* See read.h. */
b6807d98
TT
22397
22398const gdb_byte *
9c541725 22399dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22400 dwarf2_per_cu_data *per_cu,
22401 obstack *obstack,
b6807d98
TT
22402 LONGEST *len)
22403{
22404 struct dwarf2_cu *cu;
22405 struct die_info *die;
22406 struct attribute *attr;
22407 const gdb_byte *result = NULL;
22408 struct type *type;
22409 LONGEST value;
22410 enum bfd_endian byte_order;
e3b94546 22411 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22412
b6807d98 22413 if (per_cu->cu == NULL)
58f0c718 22414 load_cu (per_cu, false);
b6807d98 22415 cu = per_cu->cu;
cc12ce38
DE
22416 if (cu == NULL)
22417 {
22418 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22419 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22420 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22421 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22422 }
b6807d98 22423
9c541725 22424 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22425 if (!die)
9d8780f0
SM
22426 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22427 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22428
22429 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22430 if (attr == NULL)
22431 return NULL;
22432
e3b94546 22433 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22434 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22435
22436 switch (attr->form)
22437 {
22438 case DW_FORM_addr:
336d760d 22439 case DW_FORM_addrx:
b6807d98
TT
22440 case DW_FORM_GNU_addr_index:
22441 {
22442 gdb_byte *tem;
22443
22444 *len = cu->header.addr_size;
224c3ddb 22445 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22446 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22447 result = tem;
22448 }
22449 break;
22450 case DW_FORM_string:
22451 case DW_FORM_strp:
cf532bd1 22452 case DW_FORM_strx:
b6807d98
TT
22453 case DW_FORM_GNU_str_index:
22454 case DW_FORM_GNU_strp_alt:
22455 /* DW_STRING is already allocated on the objfile obstack, point
22456 directly to it. */
22457 result = (const gdb_byte *) DW_STRING (attr);
22458 *len = strlen (DW_STRING (attr));
22459 break;
22460 case DW_FORM_block1:
22461 case DW_FORM_block2:
22462 case DW_FORM_block4:
22463 case DW_FORM_block:
22464 case DW_FORM_exprloc:
0224619f 22465 case DW_FORM_data16:
b6807d98
TT
22466 result = DW_BLOCK (attr)->data;
22467 *len = DW_BLOCK (attr)->size;
22468 break;
22469
22470 /* The DW_AT_const_value attributes are supposed to carry the
22471 symbol's value "represented as it would be on the target
22472 architecture." By the time we get here, it's already been
22473 converted to host endianness, so we just need to sign- or
22474 zero-extend it as appropriate. */
22475 case DW_FORM_data1:
22476 type = die_type (die, cu);
22477 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22478 if (result == NULL)
22479 result = write_constant_as_bytes (obstack, byte_order,
22480 type, value, len);
22481 break;
22482 case DW_FORM_data2:
22483 type = die_type (die, cu);
22484 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22485 if (result == NULL)
22486 result = write_constant_as_bytes (obstack, byte_order,
22487 type, value, len);
22488 break;
22489 case DW_FORM_data4:
22490 type = die_type (die, cu);
22491 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22492 if (result == NULL)
22493 result = write_constant_as_bytes (obstack, byte_order,
22494 type, value, len);
22495 break;
22496 case DW_FORM_data8:
22497 type = die_type (die, cu);
22498 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22499 if (result == NULL)
22500 result = write_constant_as_bytes (obstack, byte_order,
22501 type, value, len);
22502 break;
22503
22504 case DW_FORM_sdata:
663c44ac 22505 case DW_FORM_implicit_const:
b6807d98
TT
22506 type = die_type (die, cu);
22507 result = write_constant_as_bytes (obstack, byte_order,
22508 type, DW_SND (attr), len);
22509 break;
22510
22511 case DW_FORM_udata:
22512 type = die_type (die, cu);
22513 result = write_constant_as_bytes (obstack, byte_order,
22514 type, DW_UNSND (attr), len);
22515 break;
22516
22517 default:
b98664d3 22518 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22519 dwarf_form_name (attr->form));
22520 break;
22521 }
22522
22523 return result;
22524}
22525
d4c9a4f8 22526/* See read.h. */
7942e96e
AA
22527
22528struct type *
9c541725 22529dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22530 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22531{
22532 struct dwarf2_cu *cu;
22533 struct die_info *die;
22534
7942e96e 22535 if (per_cu->cu == NULL)
58f0c718 22536 load_cu (per_cu, false);
7942e96e
AA
22537 cu = per_cu->cu;
22538 if (!cu)
22539 return NULL;
22540
9c541725 22541 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22542 if (!die)
22543 return NULL;
22544
22545 return die_type (die, cu);
22546}
22547
8cb5117c 22548/* See read.h. */
8a9b8146
TT
22549
22550struct type *
b64f50a1 22551dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22552 struct dwarf2_per_cu_data *per_cu)
22553{
9c541725 22554 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22555 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22556}
22557
ac9ec31b 22558/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22559 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22560 On exit *REF_CU is the CU of the result.
22561 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22562
22563static struct die_info *
ac9ec31b
DE
22564follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22565 struct dwarf2_cu **ref_cu)
348e048f 22566{
348e048f 22567 struct die_info temp_die;
c24bdb02 22568 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22569 struct die_info *die;
22570
ac9ec31b
DE
22571 /* While it might be nice to assert sig_type->type == NULL here,
22572 we can get here for DW_AT_imported_declaration where we need
22573 the DIE not the type. */
348e048f
DE
22574
22575 /* If necessary, add it to the queue and load its DIEs. */
22576
95554aad 22577 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22578 read_signatured_type (sig_type);
348e048f 22579
348e048f 22580 sig_cu = sig_type->per_cu.cu;
69d751e3 22581 gdb_assert (sig_cu != NULL);
9c541725
PA
22582 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22583 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22584 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22585 to_underlying (temp_die.sect_off));
348e048f
DE
22586 if (die)
22587 {
ed2dc618 22588 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22589 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22590
796a7ff8
DE
22591 /* For .gdb_index version 7 keep track of included TUs.
22592 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22593 if (dwarf2_per_objfile->index_table != NULL
22594 && dwarf2_per_objfile->index_table->version <= 7)
22595 {
ae640021 22596 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22597 }
22598
348e048f 22599 *ref_cu = sig_cu;
c24bdb02
KS
22600 if (sig_cu != cu)
22601 sig_cu->ancestor = cu;
22602
348e048f
DE
22603 return die;
22604 }
22605
ac9ec31b
DE
22606 return NULL;
22607}
22608
22609/* Follow signatured type referenced by ATTR in SRC_DIE.
22610 On entry *REF_CU is the CU of SRC_DIE.
22611 On exit *REF_CU is the CU of the result.
22612 The result is the DIE of the type.
22613 If the referenced type cannot be found an error is thrown. */
22614
22615static struct die_info *
ff39bb5e 22616follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22617 struct dwarf2_cu **ref_cu)
22618{
22619 ULONGEST signature = DW_SIGNATURE (attr);
22620 struct signatured_type *sig_type;
22621 struct die_info *die;
22622
22623 gdb_assert (attr->form == DW_FORM_ref_sig8);
22624
a2ce51a0 22625 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22626 /* sig_type will be NULL if the signatured type is missing from
22627 the debug info. */
22628 if (sig_type == NULL)
22629 {
22630 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22631 " from DIE at %s [in module %s]"),
22632 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22633 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22634 }
22635
22636 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22637 if (die == NULL)
22638 {
22639 dump_die_for_error (src_die);
22640 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22641 " from DIE at %s [in module %s]"),
22642 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22643 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22644 }
22645
22646 return die;
22647}
22648
22649/* Get the type specified by SIGNATURE referenced in DIE/CU,
22650 reading in and processing the type unit if necessary. */
22651
22652static struct type *
22653get_signatured_type (struct die_info *die, ULONGEST signature,
22654 struct dwarf2_cu *cu)
22655{
518817b3
SM
22656 struct dwarf2_per_objfile *dwarf2_per_objfile
22657 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22658 struct signatured_type *sig_type;
22659 struct dwarf2_cu *type_cu;
22660 struct die_info *type_die;
22661 struct type *type;
22662
a2ce51a0 22663 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22664 /* sig_type will be NULL if the signatured type is missing from
22665 the debug info. */
22666 if (sig_type == NULL)
22667 {
b98664d3 22668 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22669 " from DIE at %s [in module %s]"),
22670 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22671 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22672 return build_error_marker_type (cu, die);
22673 }
22674
22675 /* If we already know the type we're done. */
22676 if (sig_type->type != NULL)
22677 return sig_type->type;
22678
22679 type_cu = cu;
22680 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22681 if (type_die != NULL)
22682 {
22683 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22684 is created. This is important, for example, because for c++ classes
22685 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22686 type = read_type_die (type_die, type_cu);
22687 if (type == NULL)
22688 {
b98664d3 22689 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22690 " referenced from DIE at %s [in module %s]"),
22691 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22692 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22693 type = build_error_marker_type (cu, die);
22694 }
22695 }
22696 else
22697 {
b98664d3 22698 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22699 " from DIE at %s [in module %s]"),
22700 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22701 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22702 type = build_error_marker_type (cu, die);
22703 }
22704 sig_type->type = type;
22705
22706 return type;
22707}
22708
22709/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22710 reading in and processing the type unit if necessary. */
22711
22712static struct type *
ff39bb5e 22713get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22714 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22715{
22716 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22717 if (attr->form_is_ref ())
ac9ec31b
DE
22718 {
22719 struct dwarf2_cu *type_cu = cu;
22720 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22721
22722 return read_type_die (type_die, type_cu);
22723 }
22724 else if (attr->form == DW_FORM_ref_sig8)
22725 {
22726 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22727 }
22728 else
22729 {
518817b3
SM
22730 struct dwarf2_per_objfile *dwarf2_per_objfile
22731 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22732
b98664d3 22733 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22734 " at %s [in module %s]"),
22735 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22736 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22737 return build_error_marker_type (cu, die);
22738 }
348e048f
DE
22739}
22740
e5fe5e75 22741/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22742
22743static void
e5fe5e75 22744load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22745{
52dc124a 22746 struct signatured_type *sig_type;
348e048f 22747
f4dc4d17 22748 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22749 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22750
6721b2ec
DE
22751 /* We have the per_cu, but we need the signatured_type.
22752 Fortunately this is an easy translation. */
22753 gdb_assert (per_cu->is_debug_types);
22754 sig_type = (struct signatured_type *) per_cu;
348e048f 22755
6721b2ec 22756 gdb_assert (per_cu->cu == NULL);
348e048f 22757
52dc124a 22758 read_signatured_type (sig_type);
348e048f 22759
6721b2ec 22760 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22761}
22762
3019eac3
DE
22763/* Read in a signatured type and build its CU and DIEs.
22764 If the type is a stub for the real type in a DWO file,
22765 read in the real type from the DWO file as well. */
dee91e82
DE
22766
22767static void
22768read_signatured_type (struct signatured_type *sig_type)
22769{
22770 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22771
3019eac3 22772 gdb_assert (per_cu->is_debug_types);
dee91e82 22773 gdb_assert (per_cu->cu == NULL);
348e048f 22774
6751ebae 22775 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22776
22777 if (!reader.dummy_p)
22778 {
22779 struct dwarf2_cu *cu = reader.cu;
22780 const gdb_byte *info_ptr = reader.info_ptr;
22781
22782 gdb_assert (cu->die_hash == NULL);
22783 cu->die_hash =
22784 htab_create_alloc_ex (cu->header.length / 12,
22785 die_hash,
22786 die_eq,
22787 NULL,
22788 &cu->comp_unit_obstack,
22789 hashtab_obstack_allocate,
22790 dummy_obstack_deallocate);
22791
3e225074 22792 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22793 reader.comp_unit_die->child
22794 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22795 reader.comp_unit_die);
22796 cu->dies = reader.comp_unit_die;
22797 /* comp_unit_die is not stored in die_hash, no need. */
22798
22799 /* We try not to read any attributes in this function, because
22800 not all CUs needed for references have been loaded yet, and
22801 symbol table processing isn't initialized. But we have to
22802 set the CU language, or we won't be able to build types
22803 correctly. Similarly, if we do not read the producer, we can
22804 not apply producer-specific interpretation. */
22805 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22806
22807 reader.keep ();
c0ab21c2
TT
22808 }
22809
7ee85ab1 22810 sig_type->per_cu.tu_read = 1;
c906108c
SS
22811}
22812
c906108c
SS
22813/* Decode simple location descriptions.
22814 Given a pointer to a dwarf block that defines a location, compute
22815 the location and return the value.
22816
4cecd739
DJ
22817 NOTE drow/2003-11-18: This function is called in two situations
22818 now: for the address of static or global variables (partial symbols
22819 only) and for offsets into structures which are expected to be
22820 (more or less) constant. The partial symbol case should go away,
22821 and only the constant case should remain. That will let this
22822 function complain more accurately. A few special modes are allowed
22823 without complaint for global variables (for instance, global
22824 register values and thread-local values).
c906108c
SS
22825
22826 A location description containing no operations indicates that the
4cecd739 22827 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22828 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22829 callers will only want a very basic result and this can become a
21ae7a4d
JK
22830 complaint.
22831
22832 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22833
22834static CORE_ADDR
e7c27a73 22835decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22836{
518817b3 22837 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22838 size_t i;
22839 size_t size = blk->size;
d521ce57 22840 const gdb_byte *data = blk->data;
21ae7a4d
JK
22841 CORE_ADDR stack[64];
22842 int stacki;
22843 unsigned int bytes_read, unsnd;
22844 gdb_byte op;
c906108c 22845
21ae7a4d
JK
22846 i = 0;
22847 stacki = 0;
22848 stack[stacki] = 0;
22849 stack[++stacki] = 0;
22850
22851 while (i < size)
22852 {
22853 op = data[i++];
22854 switch (op)
22855 {
22856 case DW_OP_lit0:
22857 case DW_OP_lit1:
22858 case DW_OP_lit2:
22859 case DW_OP_lit3:
22860 case DW_OP_lit4:
22861 case DW_OP_lit5:
22862 case DW_OP_lit6:
22863 case DW_OP_lit7:
22864 case DW_OP_lit8:
22865 case DW_OP_lit9:
22866 case DW_OP_lit10:
22867 case DW_OP_lit11:
22868 case DW_OP_lit12:
22869 case DW_OP_lit13:
22870 case DW_OP_lit14:
22871 case DW_OP_lit15:
22872 case DW_OP_lit16:
22873 case DW_OP_lit17:
22874 case DW_OP_lit18:
22875 case DW_OP_lit19:
22876 case DW_OP_lit20:
22877 case DW_OP_lit21:
22878 case DW_OP_lit22:
22879 case DW_OP_lit23:
22880 case DW_OP_lit24:
22881 case DW_OP_lit25:
22882 case DW_OP_lit26:
22883 case DW_OP_lit27:
22884 case DW_OP_lit28:
22885 case DW_OP_lit29:
22886 case DW_OP_lit30:
22887 case DW_OP_lit31:
22888 stack[++stacki] = op - DW_OP_lit0;
22889 break;
f1bea926 22890
21ae7a4d
JK
22891 case DW_OP_reg0:
22892 case DW_OP_reg1:
22893 case DW_OP_reg2:
22894 case DW_OP_reg3:
22895 case DW_OP_reg4:
22896 case DW_OP_reg5:
22897 case DW_OP_reg6:
22898 case DW_OP_reg7:
22899 case DW_OP_reg8:
22900 case DW_OP_reg9:
22901 case DW_OP_reg10:
22902 case DW_OP_reg11:
22903 case DW_OP_reg12:
22904 case DW_OP_reg13:
22905 case DW_OP_reg14:
22906 case DW_OP_reg15:
22907 case DW_OP_reg16:
22908 case DW_OP_reg17:
22909 case DW_OP_reg18:
22910 case DW_OP_reg19:
22911 case DW_OP_reg20:
22912 case DW_OP_reg21:
22913 case DW_OP_reg22:
22914 case DW_OP_reg23:
22915 case DW_OP_reg24:
22916 case DW_OP_reg25:
22917 case DW_OP_reg26:
22918 case DW_OP_reg27:
22919 case DW_OP_reg28:
22920 case DW_OP_reg29:
22921 case DW_OP_reg30:
22922 case DW_OP_reg31:
22923 stack[++stacki] = op - DW_OP_reg0;
22924 if (i < size)
22925 dwarf2_complex_location_expr_complaint ();
22926 break;
c906108c 22927
21ae7a4d
JK
22928 case DW_OP_regx:
22929 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22930 i += bytes_read;
22931 stack[++stacki] = unsnd;
22932 if (i < size)
22933 dwarf2_complex_location_expr_complaint ();
22934 break;
c906108c 22935
21ae7a4d 22936 case DW_OP_addr:
c8a7a66f
TT
22937 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22938 &bytes_read);
21ae7a4d
JK
22939 i += bytes_read;
22940 break;
d53d4ac5 22941
21ae7a4d
JK
22942 case DW_OP_const1u:
22943 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22944 i += 1;
22945 break;
22946
22947 case DW_OP_const1s:
22948 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22949 i += 1;
22950 break;
22951
22952 case DW_OP_const2u:
22953 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22954 i += 2;
22955 break;
22956
22957 case DW_OP_const2s:
22958 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22959 i += 2;
22960 break;
d53d4ac5 22961
21ae7a4d
JK
22962 case DW_OP_const4u:
22963 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22964 i += 4;
22965 break;
22966
22967 case DW_OP_const4s:
22968 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22969 i += 4;
22970 break;
22971
585861ea
JK
22972 case DW_OP_const8u:
22973 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22974 i += 8;
22975 break;
22976
21ae7a4d
JK
22977 case DW_OP_constu:
22978 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22979 &bytes_read);
22980 i += bytes_read;
22981 break;
22982
22983 case DW_OP_consts:
22984 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22985 i += bytes_read;
22986 break;
22987
22988 case DW_OP_dup:
22989 stack[stacki + 1] = stack[stacki];
22990 stacki++;
22991 break;
22992
22993 case DW_OP_plus:
22994 stack[stacki - 1] += stack[stacki];
22995 stacki--;
22996 break;
22997
22998 case DW_OP_plus_uconst:
22999 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23000 &bytes_read);
23001 i += bytes_read;
23002 break;
23003
23004 case DW_OP_minus:
23005 stack[stacki - 1] -= stack[stacki];
23006 stacki--;
23007 break;
23008
23009 case DW_OP_deref:
23010 /* If we're not the last op, then we definitely can't encode
23011 this using GDB's address_class enum. This is valid for partial
23012 global symbols, although the variable's address will be bogus
23013 in the psymtab. */
23014 if (i < size)
23015 dwarf2_complex_location_expr_complaint ();
23016 break;
23017
23018 case DW_OP_GNU_push_tls_address:
4aa4e28b 23019 case DW_OP_form_tls_address:
21ae7a4d
JK
23020 /* The top of the stack has the offset from the beginning
23021 of the thread control block at which the variable is located. */
23022 /* Nothing should follow this operator, so the top of stack would
23023 be returned. */
23024 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23025 address will be bogus in the psymtab. Make it always at least
23026 non-zero to not look as a variable garbage collected by linker
23027 which have DW_OP_addr 0. */
21ae7a4d
JK
23028 if (i < size)
23029 dwarf2_complex_location_expr_complaint ();
585861ea 23030 stack[stacki]++;
21ae7a4d
JK
23031 break;
23032
23033 case DW_OP_GNU_uninit:
23034 break;
23035
336d760d 23036 case DW_OP_addrx:
3019eac3 23037 case DW_OP_GNU_addr_index:
49f6c839 23038 case DW_OP_GNU_const_index:
3019eac3
DE
23039 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23040 &bytes_read);
23041 i += bytes_read;
23042 break;
23043
21ae7a4d
JK
23044 default:
23045 {
f39c6ffd 23046 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23047
23048 if (name)
b98664d3 23049 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23050 name);
23051 else
b98664d3 23052 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23053 op);
23054 }
23055
23056 return (stack[stacki]);
d53d4ac5 23057 }
3c6e0cb3 23058
21ae7a4d
JK
23059 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23060 outside of the allocated space. Also enforce minimum>0. */
23061 if (stacki >= ARRAY_SIZE (stack) - 1)
23062 {
b98664d3 23063 complaint (_("location description stack overflow"));
21ae7a4d
JK
23064 return 0;
23065 }
23066
23067 if (stacki <= 0)
23068 {
b98664d3 23069 complaint (_("location description stack underflow"));
21ae7a4d
JK
23070 return 0;
23071 }
23072 }
23073 return (stack[stacki]);
c906108c
SS
23074}
23075
23076/* memory allocation interface */
23077
c906108c 23078static struct dwarf_block *
7b5a2f43 23079dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23080{
8d749320 23081 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23082}
23083
c906108c 23084static struct die_info *
b60c80d6 23085dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23086{
23087 struct die_info *die;
b60c80d6
DJ
23088 size_t size = sizeof (struct die_info);
23089
23090 if (num_attrs > 1)
23091 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23092
b60c80d6 23093 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23094 memset (die, 0, sizeof (struct die_info));
23095 return (die);
23096}
2e276125
JB
23097
23098\f
23099/* Macro support. */
23100
2e276125 23101static struct macro_source_file *
804d2729
TT
23102macro_start_file (struct dwarf2_cu *cu,
23103 int file, int line,
2e276125 23104 struct macro_source_file *current_file,
43f3e411 23105 struct line_header *lh)
2e276125 23106{
233d95b5 23107 /* File name relative to the compilation directory of this source file. */
03075812 23108 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
2e276125 23109
2e276125 23110 if (! current_file)
abc9d0dc 23111 {
fc474241
DE
23112 /* Note: We don't create a macro table for this compilation unit
23113 at all until we actually get a filename. */
c24bdb02 23114 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 23115
abc9d0dc
TT
23116 /* If we have no current file, then this must be the start_file
23117 directive for the compilation unit's main source file. */
03075812 23118 current_file = macro_set_main (macro_table, file_name.get ());
fc474241 23119 macro_define_special (macro_table);
abc9d0dc 23120 }
2e276125 23121 else
03075812 23122 current_file = macro_include (current_file, line, file_name.get ());
6e70227d 23123
2e276125
JB
23124 return current_file;
23125}
23126
2e276125
JB
23127static const char *
23128consume_improper_spaces (const char *p, const char *body)
23129{
23130 if (*p == ' ')
23131 {
b98664d3 23132 complaint (_("macro definition contains spaces "
3e43a32a 23133 "in formal argument list:\n`%s'"),
4d3c2250 23134 body);
2e276125
JB
23135
23136 while (*p == ' ')
23137 p++;
23138 }
23139
23140 return p;
23141}
23142
23143
23144static void
23145parse_macro_definition (struct macro_source_file *file, int line,
23146 const char *body)
23147{
23148 const char *p;
23149
23150 /* The body string takes one of two forms. For object-like macro
23151 definitions, it should be:
23152
23153 <macro name> " " <definition>
23154
23155 For function-like macro definitions, it should be:
23156
23157 <macro name> "() " <definition>
23158 or
23159 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23160
23161 Spaces may appear only where explicitly indicated, and in the
23162 <definition>.
23163
23164 The Dwarf 2 spec says that an object-like macro's name is always
23165 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23166 the space when the macro's definition is the empty string.
2e276125
JB
23167
23168 The Dwarf 2 spec says that there should be no spaces between the
23169 formal arguments in a function-like macro's formal argument list,
23170 but versions of GCC around March 2002 include spaces after the
23171 commas. */
23172
23173
23174 /* Find the extent of the macro name. The macro name is terminated
23175 by either a space or null character (for an object-like macro) or
23176 an opening paren (for a function-like macro). */
23177 for (p = body; *p; p++)
23178 if (*p == ' ' || *p == '(')
23179 break;
23180
23181 if (*p == ' ' || *p == '\0')
23182 {
23183 /* It's an object-like macro. */
23184 int name_len = p - body;
456e800a 23185 std::string name (body, name_len);
2e276125
JB
23186 const char *replacement;
23187
23188 if (*p == ' ')
23189 replacement = body + name_len + 1;
23190 else
23191 {
4d3c2250 23192 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23193 replacement = body + name_len;
23194 }
6e70227d 23195
456e800a 23196 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23197 }
23198 else if (*p == '(')
23199 {
23200 /* It's a function-like macro. */
456e800a 23201 std::string name (body, p - body);
2e276125
JB
23202 int argc = 0;
23203 int argv_size = 1;
8d749320 23204 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23205
23206 p++;
23207
23208 p = consume_improper_spaces (p, body);
23209
23210 /* Parse the formal argument list. */
23211 while (*p && *p != ')')
23212 {
23213 /* Find the extent of the current argument name. */
23214 const char *arg_start = p;
23215
23216 while (*p && *p != ',' && *p != ')' && *p != ' ')
23217 p++;
23218
23219 if (! *p || p == arg_start)
4d3c2250 23220 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23221 else
23222 {
23223 /* Make sure argv has room for the new argument. */
23224 if (argc >= argv_size)
23225 {
23226 argv_size *= 2;
224c3ddb 23227 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23228 }
23229
3f8a7804 23230 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23231 }
23232
23233 p = consume_improper_spaces (p, body);
23234
23235 /* Consume the comma, if present. */
23236 if (*p == ',')
23237 {
23238 p++;
23239
23240 p = consume_improper_spaces (p, body);
23241 }
23242 }
23243
23244 if (*p == ')')
23245 {
23246 p++;
23247
23248 if (*p == ' ')
23249 /* Perfectly formed definition, no complaints. */
456e800a 23250 macro_define_function (file, line, name.c_str (),
6e70227d 23251 argc, (const char **) argv,
2e276125
JB
23252 p + 1);
23253 else if (*p == '\0')
23254 {
23255 /* Complain, but do define it. */
4d3c2250 23256 dwarf2_macro_malformed_definition_complaint (body);
456e800a 23257 macro_define_function (file, line, name.c_str (),
6e70227d 23258 argc, (const char **) argv,
2e276125
JB
23259 p);
23260 }
23261 else
23262 /* Just complain. */
4d3c2250 23263 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23264 }
23265 else
23266 /* Just complain. */
4d3c2250 23267 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23268
2e276125
JB
23269 {
23270 int i;
23271
23272 for (i = 0; i < argc; i++)
23273 xfree (argv[i]);
23274 }
23275 xfree (argv);
23276 }
23277 else
4d3c2250 23278 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23279}
23280
cf2c3c16
TT
23281/* Skip some bytes from BYTES according to the form given in FORM.
23282 Returns the new pointer. */
2e276125 23283
d521ce57
TT
23284static const gdb_byte *
23285skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23286 enum dwarf_form form,
23287 unsigned int offset_size,
23288 struct dwarf2_section_info *section)
2e276125 23289{
cf2c3c16 23290 unsigned int bytes_read;
2e276125 23291
cf2c3c16 23292 switch (form)
2e276125 23293 {
cf2c3c16
TT
23294 case DW_FORM_data1:
23295 case DW_FORM_flag:
23296 ++bytes;
23297 break;
23298
23299 case DW_FORM_data2:
23300 bytes += 2;
23301 break;
23302
23303 case DW_FORM_data4:
23304 bytes += 4;
23305 break;
23306
23307 case DW_FORM_data8:
23308 bytes += 8;
23309 break;
23310
0224619f
JK
23311 case DW_FORM_data16:
23312 bytes += 16;
23313 break;
23314
cf2c3c16
TT
23315 case DW_FORM_string:
23316 read_direct_string (abfd, bytes, &bytes_read);
23317 bytes += bytes_read;
23318 break;
23319
23320 case DW_FORM_sec_offset:
23321 case DW_FORM_strp:
36586728 23322 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23323 bytes += offset_size;
23324 break;
23325
23326 case DW_FORM_block:
23327 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23328 bytes += bytes_read;
23329 break;
23330
23331 case DW_FORM_block1:
23332 bytes += 1 + read_1_byte (abfd, bytes);
23333 break;
23334 case DW_FORM_block2:
23335 bytes += 2 + read_2_bytes (abfd, bytes);
23336 break;
23337 case DW_FORM_block4:
23338 bytes += 4 + read_4_bytes (abfd, bytes);
23339 break;
23340
336d760d 23341 case DW_FORM_addrx:
cf2c3c16 23342 case DW_FORM_sdata:
cf532bd1 23343 case DW_FORM_strx:
cf2c3c16 23344 case DW_FORM_udata:
3019eac3
DE
23345 case DW_FORM_GNU_addr_index:
23346 case DW_FORM_GNU_str_index:
d521ce57 23347 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23348 if (bytes == NULL)
23349 {
23350 dwarf2_section_buffer_overflow_complaint (section);
23351 return NULL;
23352 }
cf2c3c16
TT
23353 break;
23354
663c44ac
JK
23355 case DW_FORM_implicit_const:
23356 break;
23357
cf2c3c16
TT
23358 default:
23359 {
b98664d3 23360 complaint (_("invalid form 0x%x in `%s'"),
96b79293 23361 form, section->get_name ());
cf2c3c16
TT
23362 return NULL;
23363 }
2e276125
JB
23364 }
23365
cf2c3c16
TT
23366 return bytes;
23367}
757a13d0 23368
cf2c3c16
TT
23369/* A helper for dwarf_decode_macros that handles skipping an unknown
23370 opcode. Returns an updated pointer to the macro data buffer; or,
23371 on error, issues a complaint and returns NULL. */
757a13d0 23372
d521ce57 23373static const gdb_byte *
cf2c3c16 23374skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23375 const gdb_byte **opcode_definitions,
23376 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23377 bfd *abfd,
23378 unsigned int offset_size,
23379 struct dwarf2_section_info *section)
23380{
23381 unsigned int bytes_read, i;
23382 unsigned long arg;
d521ce57 23383 const gdb_byte *defn;
2e276125 23384
cf2c3c16 23385 if (opcode_definitions[opcode] == NULL)
2e276125 23386 {
b98664d3 23387 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23388 opcode);
23389 return NULL;
23390 }
2e276125 23391
cf2c3c16
TT
23392 defn = opcode_definitions[opcode];
23393 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23394 defn += bytes_read;
2e276125 23395
cf2c3c16
TT
23396 for (i = 0; i < arg; ++i)
23397 {
aead7601
SM
23398 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23399 (enum dwarf_form) defn[i], offset_size,
f664829e 23400 section);
cf2c3c16
TT
23401 if (mac_ptr == NULL)
23402 {
23403 /* skip_form_bytes already issued the complaint. */
23404 return NULL;
23405 }
23406 }
757a13d0 23407
cf2c3c16
TT
23408 return mac_ptr;
23409}
757a13d0 23410
cf2c3c16
TT
23411/* A helper function which parses the header of a macro section.
23412 If the macro section is the extended (for now called "GNU") type,
23413 then this updates *OFFSET_SIZE. Returns a pointer to just after
23414 the header, or issues a complaint and returns NULL on error. */
757a13d0 23415
d521ce57
TT
23416static const gdb_byte *
23417dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 23418 bfd *abfd,
d521ce57 23419 const gdb_byte *mac_ptr,
cf2c3c16
TT
23420 unsigned int *offset_size,
23421 int section_is_gnu)
23422{
23423 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 23424
cf2c3c16
TT
23425 if (section_is_gnu)
23426 {
23427 unsigned int version, flags;
757a13d0 23428
cf2c3c16 23429 version = read_2_bytes (abfd, mac_ptr);
0af92d60 23430 if (version != 4 && version != 5)
cf2c3c16 23431 {
b98664d3 23432 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
23433 version);
23434 return NULL;
23435 }
23436 mac_ptr += 2;
757a13d0 23437
cf2c3c16
TT
23438 flags = read_1_byte (abfd, mac_ptr);
23439 ++mac_ptr;
23440 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 23441
cf2c3c16
TT
23442 if ((flags & 2) != 0)
23443 /* We don't need the line table offset. */
23444 mac_ptr += *offset_size;
757a13d0 23445
cf2c3c16
TT
23446 /* Vendor opcode descriptions. */
23447 if ((flags & 4) != 0)
23448 {
23449 unsigned int i, count;
757a13d0 23450
cf2c3c16
TT
23451 count = read_1_byte (abfd, mac_ptr);
23452 ++mac_ptr;
23453 for (i = 0; i < count; ++i)
23454 {
23455 unsigned int opcode, bytes_read;
23456 unsigned long arg;
23457
23458 opcode = read_1_byte (abfd, mac_ptr);
23459 ++mac_ptr;
23460 opcode_definitions[opcode] = mac_ptr;
23461 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23462 mac_ptr += bytes_read;
23463 mac_ptr += arg;
23464 }
757a13d0 23465 }
cf2c3c16 23466 }
757a13d0 23467
cf2c3c16
TT
23468 return mac_ptr;
23469}
757a13d0 23470
cf2c3c16 23471/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 23472 including DW_MACRO_import. */
cf2c3c16
TT
23473
23474static void
804d2729 23475dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 23476 bfd *abfd,
d521ce57 23477 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 23478 struct macro_source_file *current_file,
43f3e411 23479 struct line_header *lh,
cf2c3c16 23480 struct dwarf2_section_info *section,
36586728 23481 int section_is_gnu, int section_is_dwz,
cf2c3c16 23482 unsigned int offset_size,
8fc3fc34 23483 htab_t include_hash)
cf2c3c16 23484{
804d2729
TT
23485 struct dwarf2_per_objfile *dwarf2_per_objfile
23486 = cu->per_cu->dwarf2_per_objfile;
4d663531 23487 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
23488 enum dwarf_macro_record_type macinfo_type;
23489 int at_commandline;
d521ce57 23490 const gdb_byte *opcode_definitions[256];
757a13d0 23491
cf2c3c16
TT
23492 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23493 &offset_size, section_is_gnu);
23494 if (mac_ptr == NULL)
23495 {
23496 /* We already issued a complaint. */
23497 return;
23498 }
757a13d0
JK
23499
23500 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23501 GDB is still reading the definitions from command line. First
23502 DW_MACINFO_start_file will need to be ignored as it was already executed
23503 to create CURRENT_FILE for the main source holding also the command line
23504 definitions. On first met DW_MACINFO_start_file this flag is reset to
23505 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23506
23507 at_commandline = 1;
23508
23509 do
23510 {
23511 /* Do we at least have room for a macinfo type byte? */
23512 if (mac_ptr >= mac_end)
23513 {
f664829e 23514 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
23515 break;
23516 }
23517
aead7601 23518 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
23519 mac_ptr++;
23520
cf2c3c16
TT
23521 /* Note that we rely on the fact that the corresponding GNU and
23522 DWARF constants are the same. */
132448f8
SM
23523 DIAGNOSTIC_PUSH
23524 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
23525 switch (macinfo_type)
23526 {
23527 /* A zero macinfo type indicates the end of the macro
23528 information. */
23529 case 0:
23530 break;
2e276125 23531
0af92d60
JK
23532 case DW_MACRO_define:
23533 case DW_MACRO_undef:
23534 case DW_MACRO_define_strp:
23535 case DW_MACRO_undef_strp:
23536 case DW_MACRO_define_sup:
23537 case DW_MACRO_undef_sup:
2e276125 23538 {
891d2f0b 23539 unsigned int bytes_read;
2e276125 23540 int line;
d521ce57 23541 const char *body;
cf2c3c16 23542 int is_define;
2e276125 23543
cf2c3c16
TT
23544 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23545 mac_ptr += bytes_read;
23546
0af92d60
JK
23547 if (macinfo_type == DW_MACRO_define
23548 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
23549 {
23550 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23551 mac_ptr += bytes_read;
23552 }
23553 else
23554 {
23555 LONGEST str_offset;
23556
24aa364d 23557 str_offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16 23558 mac_ptr += offset_size;
2e276125 23559
0af92d60
JK
23560 if (macinfo_type == DW_MACRO_define_sup
23561 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 23562 || section_is_dwz)
36586728 23563 {
ed2dc618
SM
23564 struct dwz_file *dwz
23565 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 23566
ed2dc618
SM
23567 body = read_indirect_string_from_dwz (objfile,
23568 dwz, str_offset);
36586728
TT
23569 }
23570 else
ed2dc618
SM
23571 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23572 abfd, str_offset);
cf2c3c16
TT
23573 }
23574
0af92d60
JK
23575 is_define = (macinfo_type == DW_MACRO_define
23576 || macinfo_type == DW_MACRO_define_strp
23577 || macinfo_type == DW_MACRO_define_sup);
2e276125 23578 if (! current_file)
757a13d0
JK
23579 {
23580 /* DWARF violation as no main source is present. */
b98664d3 23581 complaint (_("debug info with no main source gives macro %s "
757a13d0 23582 "on line %d: %s"),
cf2c3c16
TT
23583 is_define ? _("definition") : _("undefinition"),
23584 line, body);
757a13d0
JK
23585 break;
23586 }
3e43a32a
MS
23587 if ((line == 0 && !at_commandline)
23588 || (line != 0 && at_commandline))
b98664d3 23589 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 23590 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 23591 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
23592 line == 0 ? _("zero") : _("non-zero"), line, body);
23593
955b06fa 23594 if (body == NULL)
7bede828 23595 {
955b06fa
SDJ
23596 /* Fedora's rpm-build's "debugedit" binary
23597 corrupted .debug_macro sections.
23598
23599 For more info, see
23600 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23601 complaint (_("debug info gives %s invalid macro %s "
23602 "without body (corrupted?) at line %d "
23603 "on file %s"),
23604 at_commandline ? _("command-line") : _("in-file"),
23605 is_define ? _("definition") : _("undefinition"),
23606 line, current_file->filename);
7bede828 23607 }
955b06fa
SDJ
23608 else if (is_define)
23609 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
23610 else
23611 {
0af92d60
JK
23612 gdb_assert (macinfo_type == DW_MACRO_undef
23613 || macinfo_type == DW_MACRO_undef_strp
23614 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
23615 macro_undef (current_file, line, body);
23616 }
2e276125
JB
23617 }
23618 break;
23619
0af92d60 23620 case DW_MACRO_start_file:
2e276125 23621 {
891d2f0b 23622 unsigned int bytes_read;
2e276125
JB
23623 int line, file;
23624
23625 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23626 mac_ptr += bytes_read;
23627 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23628 mac_ptr += bytes_read;
23629
3e43a32a
MS
23630 if ((line == 0 && !at_commandline)
23631 || (line != 0 && at_commandline))
b98664d3 23632 complaint (_("debug info gives source %d included "
757a13d0
JK
23633 "from %s at %s line %d"),
23634 file, at_commandline ? _("command-line") : _("file"),
23635 line == 0 ? _("zero") : _("non-zero"), line);
23636
23637 if (at_commandline)
23638 {
0af92d60 23639 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23640 pass one. */
757a13d0
JK
23641 at_commandline = 0;
23642 }
23643 else
804d2729
TT
23644 current_file = macro_start_file (cu, file, line, current_file,
23645 lh);
2e276125
JB
23646 }
23647 break;
23648
0af92d60 23649 case DW_MACRO_end_file:
2e276125 23650 if (! current_file)
b98664d3 23651 complaint (_("macro debug info has an unmatched "
3e43a32a 23652 "`close_file' directive"));
2e276125
JB
23653 else
23654 {
23655 current_file = current_file->included_by;
23656 if (! current_file)
23657 {
cf2c3c16 23658 enum dwarf_macro_record_type next_type;
2e276125
JB
23659
23660 /* GCC circa March 2002 doesn't produce the zero
23661 type byte marking the end of the compilation
23662 unit. Complain if it's not there, but exit no
23663 matter what. */
23664
23665 /* Do we at least have room for a macinfo type byte? */
23666 if (mac_ptr >= mac_end)
23667 {
f664829e 23668 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23669 return;
23670 }
23671
23672 /* We don't increment mac_ptr here, so this is just
23673 a look-ahead. */
aead7601
SM
23674 next_type
23675 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23676 mac_ptr);
2e276125 23677 if (next_type != 0)
b98664d3 23678 complaint (_("no terminating 0-type entry for "
3e43a32a 23679 "macros in `.debug_macinfo' section"));
2e276125
JB
23680
23681 return;
23682 }
23683 }
23684 break;
23685
0af92d60
JK
23686 case DW_MACRO_import:
23687 case DW_MACRO_import_sup:
cf2c3c16
TT
23688 {
23689 LONGEST offset;
8fc3fc34 23690 void **slot;
a036ba48
TT
23691 bfd *include_bfd = abfd;
23692 struct dwarf2_section_info *include_section = section;
d521ce57 23693 const gdb_byte *include_mac_end = mac_end;
a036ba48 23694 int is_dwz = section_is_dwz;
d521ce57 23695 const gdb_byte *new_mac_ptr;
cf2c3c16 23696
24aa364d 23697 offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16
TT
23698 mac_ptr += offset_size;
23699
0af92d60 23700 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 23701 {
ed2dc618 23702 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 23703
96b79293 23704 dwz->macro.read (objfile);
a036ba48 23705
a036ba48 23706 include_section = &dwz->macro;
96b79293 23707 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
23708 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23709 is_dwz = 1;
23710 }
23711
23712 new_mac_ptr = include_section->buffer + offset;
23713 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23714
8fc3fc34
TT
23715 if (*slot != NULL)
23716 {
23717 /* This has actually happened; see
23718 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 23719 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
23720 ".debug_macro section"));
23721 }
23722 else
23723 {
d521ce57 23724 *slot = (void *) new_mac_ptr;
36586728 23725
804d2729 23726 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 23727 include_mac_end, current_file, lh,
36586728 23728 section, section_is_gnu, is_dwz,
4d663531 23729 offset_size, include_hash);
8fc3fc34 23730
d521ce57 23731 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23732 }
cf2c3c16
TT
23733 }
23734 break;
23735
2e276125 23736 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23737 if (!section_is_gnu)
23738 {
23739 unsigned int bytes_read;
2e276125 23740
ac298888
TT
23741 /* This reads the constant, but since we don't recognize
23742 any vendor extensions, we ignore it. */
23743 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23744 mac_ptr += bytes_read;
23745 read_direct_string (abfd, mac_ptr, &bytes_read);
23746 mac_ptr += bytes_read;
2e276125 23747
cf2c3c16
TT
23748 /* We don't recognize any vendor extensions. */
23749 break;
23750 }
23751 /* FALLTHROUGH */
23752
23753 default:
23754 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23755 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23756 section);
23757 if (mac_ptr == NULL)
23758 return;
23759 break;
2e276125 23760 }
132448f8 23761 DIAGNOSTIC_POP
757a13d0 23762 } while (macinfo_type != 0);
2e276125 23763}
8e19ed76 23764
cf2c3c16 23765static void
09262596 23766dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 23767 int section_is_gnu)
cf2c3c16 23768{
518817b3
SM
23769 struct dwarf2_per_objfile *dwarf2_per_objfile
23770 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23771 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
23772 struct line_header *lh = cu->line_header;
23773 bfd *abfd;
d521ce57 23774 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23775 struct macro_source_file *current_file = 0;
23776 enum dwarf_macro_record_type macinfo_type;
23777 unsigned int offset_size = cu->header.offset_size;
d521ce57 23778 const gdb_byte *opcode_definitions[256];
8fc3fc34 23779 void **slot;
09262596
DE
23780 struct dwarf2_section_info *section;
23781 const char *section_name;
23782
23783 if (cu->dwo_unit != NULL)
23784 {
23785 if (section_is_gnu)
23786 {
23787 section = &cu->dwo_unit->dwo_file->sections.macro;
23788 section_name = ".debug_macro.dwo";
23789 }
23790 else
23791 {
23792 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23793 section_name = ".debug_macinfo.dwo";
23794 }
23795 }
23796 else
23797 {
23798 if (section_is_gnu)
23799 {
23800 section = &dwarf2_per_objfile->macro;
23801 section_name = ".debug_macro";
23802 }
23803 else
23804 {
23805 section = &dwarf2_per_objfile->macinfo;
23806 section_name = ".debug_macinfo";
23807 }
23808 }
cf2c3c16 23809
96b79293 23810 section->read (objfile);
cf2c3c16
TT
23811 if (section->buffer == NULL)
23812 {
b98664d3 23813 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
23814 return;
23815 }
96b79293 23816 abfd = section->get_bfd_owner ();
cf2c3c16
TT
23817
23818 /* First pass: Find the name of the base filename.
23819 This filename is needed in order to process all macros whose definition
23820 (or undefinition) comes from the command line. These macros are defined
23821 before the first DW_MACINFO_start_file entry, and yet still need to be
23822 associated to the base file.
23823
23824 To determine the base file name, we scan the macro definitions until we
23825 reach the first DW_MACINFO_start_file entry. We then initialize
23826 CURRENT_FILE accordingly so that any macro definition found before the
23827 first DW_MACINFO_start_file can still be associated to the base file. */
23828
23829 mac_ptr = section->buffer + offset;
23830 mac_end = section->buffer + section->size;
23831
23832 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23833 &offset_size, section_is_gnu);
23834 if (mac_ptr == NULL)
23835 {
23836 /* We already issued a complaint. */
23837 return;
23838 }
23839
23840 do
23841 {
23842 /* Do we at least have room for a macinfo type byte? */
23843 if (mac_ptr >= mac_end)
23844 {
23845 /* Complaint is printed during the second pass as GDB will probably
23846 stop the first pass earlier upon finding
23847 DW_MACINFO_start_file. */
23848 break;
23849 }
23850
aead7601 23851 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23852 mac_ptr++;
23853
23854 /* Note that we rely on the fact that the corresponding GNU and
23855 DWARF constants are the same. */
132448f8
SM
23856 DIAGNOSTIC_PUSH
23857 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
23858 switch (macinfo_type)
23859 {
23860 /* A zero macinfo type indicates the end of the macro
23861 information. */
23862 case 0:
23863 break;
23864
0af92d60
JK
23865 case DW_MACRO_define:
23866 case DW_MACRO_undef:
cf2c3c16
TT
23867 /* Only skip the data by MAC_PTR. */
23868 {
23869 unsigned int bytes_read;
23870
23871 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23872 mac_ptr += bytes_read;
23873 read_direct_string (abfd, mac_ptr, &bytes_read);
23874 mac_ptr += bytes_read;
23875 }
23876 break;
23877
0af92d60 23878 case DW_MACRO_start_file:
cf2c3c16
TT
23879 {
23880 unsigned int bytes_read;
23881 int line, file;
23882
23883 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23884 mac_ptr += bytes_read;
23885 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23886 mac_ptr += bytes_read;
23887
804d2729 23888 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
23889 }
23890 break;
23891
0af92d60 23892 case DW_MACRO_end_file:
cf2c3c16
TT
23893 /* No data to skip by MAC_PTR. */
23894 break;
23895
0af92d60
JK
23896 case DW_MACRO_define_strp:
23897 case DW_MACRO_undef_strp:
23898 case DW_MACRO_define_sup:
23899 case DW_MACRO_undef_sup:
cf2c3c16
TT
23900 {
23901 unsigned int bytes_read;
23902
23903 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23904 mac_ptr += bytes_read;
23905 mac_ptr += offset_size;
23906 }
23907 break;
23908
0af92d60
JK
23909 case DW_MACRO_import:
23910 case DW_MACRO_import_sup:
cf2c3c16 23911 /* Note that, according to the spec, a transparent include
0af92d60 23912 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23913 skip this opcode. */
23914 mac_ptr += offset_size;
23915 break;
23916
23917 case DW_MACINFO_vendor_ext:
23918 /* Only skip the data by MAC_PTR. */
23919 if (!section_is_gnu)
23920 {
23921 unsigned int bytes_read;
23922
23923 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23924 mac_ptr += bytes_read;
23925 read_direct_string (abfd, mac_ptr, &bytes_read);
23926 mac_ptr += bytes_read;
23927 }
23928 /* FALLTHROUGH */
23929
23930 default:
23931 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23932 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23933 section);
23934 if (mac_ptr == NULL)
23935 return;
23936 break;
23937 }
132448f8 23938 DIAGNOSTIC_POP
cf2c3c16
TT
23939 } while (macinfo_type != 0 && current_file == NULL);
23940
23941 /* Second pass: Process all entries.
23942
23943 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23944 command-line macro definitions/undefinitions. This flag is unset when we
23945 reach the first DW_MACINFO_start_file entry. */
23946
fc4007c9
TT
23947 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23948 htab_eq_pointer,
23949 NULL, xcalloc, xfree));
8fc3fc34 23950 mac_ptr = section->buffer + offset;
fc4007c9 23951 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23952 *slot = (void *) mac_ptr;
804d2729 23953 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 23954 current_file, lh, section,
fc4007c9
TT
23955 section_is_gnu, 0, offset_size,
23956 include_hash.get ());
cf2c3c16
TT
23957}
23958
3019eac3
DE
23959/* Return the .debug_loc section to use for CU.
23960 For DWO files use .debug_loc.dwo. */
23961
23962static struct dwarf2_section_info *
23963cu_debug_loc_section (struct dwarf2_cu *cu)
23964{
518817b3
SM
23965 struct dwarf2_per_objfile *dwarf2_per_objfile
23966 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23967
3019eac3 23968 if (cu->dwo_unit)
43988095
JK
23969 {
23970 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23971
43988095
JK
23972 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23973 }
23974 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23975 : &dwarf2_per_objfile->loc);
3019eac3
DE
23976}
23977
8cf6f0b1
TT
23978/* A helper function that fills in a dwarf2_loclist_baton. */
23979
23980static void
23981fill_in_loclist_baton (struct dwarf2_cu *cu,
23982 struct dwarf2_loclist_baton *baton,
ff39bb5e 23983 const struct attribute *attr)
8cf6f0b1 23984{
518817b3
SM
23985 struct dwarf2_per_objfile *dwarf2_per_objfile
23986 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23987 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23988
96b79293 23989 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23990
23991 baton->per_cu = cu->per_cu;
23992 gdb_assert (baton->per_cu);
23993 /* We don't know how long the location list is, but make sure we
23994 don't run off the edge of the section. */
3019eac3
DE
23995 baton->size = section->size - DW_UNSND (attr);
23996 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23997 baton->base_address = cu->base_address;
f664829e 23998 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23999}
24000
4c2df51b 24001static void
ff39bb5e 24002dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24003 struct dwarf2_cu *cu, int is_block)
4c2df51b 24004{
518817b3
SM
24005 struct dwarf2_per_objfile *dwarf2_per_objfile
24006 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24007 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24008 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24009
cd6c91b4 24010 if (attr->form_is_section_offset ()
3019eac3 24011 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24012 the section. If so, fall through to the complaint in the
24013 other branch. */
2c7d5afc 24014 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 24015 {
0d53c4c4 24016 struct dwarf2_loclist_baton *baton;
4c2df51b 24017
8d749320 24018 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24019
8cf6f0b1 24020 fill_in_loclist_baton (cu, baton, attr);
be391dca 24021
d00adf39 24022 if (cu->base_known == 0)
b98664d3 24023 complaint (_("Location list used without "
3e43a32a 24024 "specifying the CU base address."));
4c2df51b 24025
f1e6e072
TT
24026 SYMBOL_ACLASS_INDEX (sym) = (is_block
24027 ? dwarf2_loclist_block_index
24028 : dwarf2_loclist_index);
0d53c4c4
DJ
24029 SYMBOL_LOCATION_BATON (sym) = baton;
24030 }
24031 else
24032 {
24033 struct dwarf2_locexpr_baton *baton;
24034
8d749320 24035 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24036 baton->per_cu = cu->per_cu;
24037 gdb_assert (baton->per_cu);
0d53c4c4 24038
4fc6c0d5 24039 if (attr->form_is_block ())
0d53c4c4
DJ
24040 {
24041 /* Note that we're just copying the block's data pointer
24042 here, not the actual data. We're still pointing into the
6502dd73
DJ
24043 info_buffer for SYM's objfile; right now we never release
24044 that buffer, but when we do clean up properly this may
24045 need to change. */
0d53c4c4
DJ
24046 baton->size = DW_BLOCK (attr)->size;
24047 baton->data = DW_BLOCK (attr)->data;
24048 }
24049 else
24050 {
24051 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24052 sym->natural_name ());
0d53c4c4 24053 baton->size = 0;
0d53c4c4 24054 }
6e70227d 24055
f1e6e072
TT
24056 SYMBOL_ACLASS_INDEX (sym) = (is_block
24057 ? dwarf2_locexpr_block_index
24058 : dwarf2_locexpr_index);
0d53c4c4
DJ
24059 SYMBOL_LOCATION_BATON (sym) = baton;
24060 }
4c2df51b 24061}
6502dd73 24062
09ba997f 24063/* See read.h. */
ae0d2f24
UW
24064
24065struct objfile *
09ba997f 24066dwarf2_per_cu_data::objfile () const
ae0d2f24 24067{
09ba997f 24068 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
24069
24070 /* Return the master objfile, so that we can report and look up the
24071 correct file containing this variable. */
24072 if (objfile->separate_debug_objfile_backlink)
24073 objfile = objfile->separate_debug_objfile_backlink;
24074
24075 return objfile;
24076}
24077
96408a79
SA
24078/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24079 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24080 CU_HEADERP first. */
24081
24082static const struct comp_unit_head *
24083per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 24084 const struct dwarf2_per_cu_data *per_cu)
96408a79 24085{
d521ce57 24086 const gdb_byte *info_ptr;
96408a79
SA
24087
24088 if (per_cu->cu)
24089 return &per_cu->cu->header;
24090
9c541725 24091 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24092
24093 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24094 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24095 rcuh_kind::COMPILE);
96408a79
SA
24096
24097 return cu_headerp;
24098}
24099
09ba997f 24100/* See read.h. */
ae0d2f24 24101
98714339 24102int
09ba997f 24103dwarf2_per_cu_data::addr_size () const
ae0d2f24 24104{
96408a79
SA
24105 struct comp_unit_head cu_header_local;
24106 const struct comp_unit_head *cu_headerp;
c471e790 24107
09ba997f 24108 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24109
24110 return cu_headerp->addr_size;
ae0d2f24
UW
24111}
24112
09ba997f 24113/* See read.h. */
9eae7c52
TT
24114
24115int
09ba997f 24116dwarf2_per_cu_data::offset_size () const
9eae7c52 24117{
96408a79
SA
24118 struct comp_unit_head cu_header_local;
24119 const struct comp_unit_head *cu_headerp;
9c6c53f7 24120
09ba997f 24121 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24122
24123 return cu_headerp->offset_size;
24124}
24125
09ba997f 24126/* See read.h. */
96408a79
SA
24127
24128int
09ba997f 24129dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
24130{
24131 struct comp_unit_head cu_header_local;
24132 const struct comp_unit_head *cu_headerp;
24133
09ba997f 24134 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24135
24136 if (cu_headerp->version == 2)
24137 return cu_headerp->addr_size;
24138 else
24139 return cu_headerp->offset_size;
181cebd4
JK
24140}
24141
09ba997f 24142/* See read.h. */
9aa1f1e3
TT
24143
24144CORE_ADDR
09ba997f 24145dwarf2_per_cu_data::text_offset () const
9aa1f1e3 24146{
09ba997f
TT
24147 struct objfile *objfile = dwarf2_per_objfile->objfile;
24148
24149 return objfile->text_section_offset ();
9aa1f1e3
TT
24150}
24151
09ba997f
TT
24152/* See read.h. */
24153
24154struct type *
24155dwarf2_per_cu_data::addr_type () const
9a49df9d 24156{
09ba997f 24157 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
24158 struct type *void_type = objfile_type (objfile)->builtin_void;
24159 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 24160 int addr_size = this->addr_size ();
9a49df9d
AB
24161
24162 if (TYPE_LENGTH (addr_type) == addr_size)
24163 return addr_type;
24164
09ba997f 24165 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
24166 return addr_type;
24167}
24168
22b6cd70
TT
24169/* A helper function for dwarf2_find_containing_comp_unit that returns
24170 the index of the result, and that searches a vector. It will
24171 return a result even if the offset in question does not actually
24172 occur in any CU. This is separate so that it can be unit
24173 tested. */
ae038cb0 24174
22b6cd70
TT
24175static int
24176dwarf2_find_containing_comp_unit
24177 (sect_offset sect_off,
24178 unsigned int offset_in_dwz,
24179 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24180{
ae038cb0
DJ
24181 int low, high;
24182
ae038cb0 24183 low = 0;
22b6cd70 24184 high = all_comp_units.size () - 1;
ae038cb0
DJ
24185 while (high > low)
24186 {
36586728 24187 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24188 int mid = low + (high - low) / 2;
9a619af0 24189
22b6cd70 24190 mid_cu = all_comp_units[mid];
36586728 24191 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24192 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24193 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24194 high = mid;
24195 else
24196 low = mid + 1;
24197 }
24198 gdb_assert (low == high);
22b6cd70
TT
24199 return low;
24200}
24201
24202/* Locate the .debug_info compilation unit from CU's objfile which contains
24203 the DIE at OFFSET. Raises an error on failure. */
24204
24205static struct dwarf2_per_cu_data *
24206dwarf2_find_containing_comp_unit (sect_offset sect_off,
24207 unsigned int offset_in_dwz,
24208 struct dwarf2_per_objfile *dwarf2_per_objfile)
24209{
24210 int low
24211 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24212 dwarf2_per_objfile->all_comp_units);
24213 struct dwarf2_per_cu_data *this_cu
24214 = dwarf2_per_objfile->all_comp_units[low];
24215
45b8ae0c 24216 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24217 {
36586728 24218 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24219 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24220 "offset %s [in module %s]"),
24221 sect_offset_str (sect_off),
ed2dc618 24222 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24223
9c541725
PA
24224 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24225 <= sect_off);
ae038cb0
DJ
24226 return dwarf2_per_objfile->all_comp_units[low-1];
24227 }
24228 else
24229 {
b76e467d 24230 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24231 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24232 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24233 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24234 return this_cu;
24235 }
24236}
24237
22b6cd70
TT
24238#if GDB_SELF_TEST
24239
24240namespace selftests {
24241namespace find_containing_comp_unit {
24242
24243static void
24244run_test ()
24245{
24246 struct dwarf2_per_cu_data one {};
24247 struct dwarf2_per_cu_data two {};
24248 struct dwarf2_per_cu_data three {};
24249 struct dwarf2_per_cu_data four {};
24250
24251 one.length = 5;
24252 two.sect_off = sect_offset (one.length);
24253 two.length = 7;
24254
24255 three.length = 5;
24256 three.is_dwz = 1;
24257 four.sect_off = sect_offset (three.length);
24258 four.length = 7;
24259 four.is_dwz = 1;
24260
24261 std::vector<dwarf2_per_cu_data *> units;
24262 units.push_back (&one);
24263 units.push_back (&two);
24264 units.push_back (&three);
24265 units.push_back (&four);
24266
24267 int result;
24268
24269 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24270 SELF_CHECK (units[result] == &one);
24271 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24272 SELF_CHECK (units[result] == &one);
24273 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24274 SELF_CHECK (units[result] == &two);
24275
24276 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24277 SELF_CHECK (units[result] == &three);
24278 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24279 SELF_CHECK (units[result] == &three);
24280 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24281 SELF_CHECK (units[result] == &four);
24282}
24283
24284}
24285}
24286
24287#endif /* GDB_SELF_TEST */
24288
23745b47 24289/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24290
fcd3b13d
SM
24291dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24292 : per_cu (per_cu_),
9068261f
AB
24293 mark (false),
24294 has_loclist (false),
24295 checked_producer (false),
24296 producer_is_gxx_lt_4_6 (false),
24297 producer_is_gcc_lt_4_3 (false),
eb77c9df 24298 producer_is_icc (false),
9068261f 24299 producer_is_icc_lt_14 (false),
c258c396 24300 producer_is_codewarrior (false),
9068261f 24301 processing_has_namespace_info (false)
93311388 24302{
fcd3b13d
SM
24303 per_cu->cu = this;
24304}
24305
24306/* Destroy a dwarf2_cu. */
24307
24308dwarf2_cu::~dwarf2_cu ()
24309{
24310 per_cu->cu = NULL;
9816fde3
JK
24311}
24312
24313/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24314
24315static void
95554aad
TT
24316prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24317 enum language pretend_language)
9816fde3
JK
24318{
24319 struct attribute *attr;
24320
24321 /* Set the language we're debugging. */
24322 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24323 if (attr != nullptr)
9816fde3
JK
24324 set_cu_language (DW_UNSND (attr), cu);
24325 else
9cded63f 24326 {
95554aad 24327 cu->language = pretend_language;
9cded63f
TT
24328 cu->language_defn = language_def (cu->language);
24329 }
dee91e82 24330
7d45c7c3 24331 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24332}
24333
ae038cb0
DJ
24334/* Increase the age counter on each cached compilation unit, and free
24335 any that are too old. */
24336
24337static void
ed2dc618 24338age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24339{
24340 struct dwarf2_per_cu_data *per_cu, **last_chain;
24341
24342 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24343 per_cu = dwarf2_per_objfile->read_in_chain;
24344 while (per_cu != NULL)
24345 {
24346 per_cu->cu->last_used ++;
b4f54984 24347 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24348 dwarf2_mark (per_cu->cu);
24349 per_cu = per_cu->cu->read_in_chain;
24350 }
24351
24352 per_cu = dwarf2_per_objfile->read_in_chain;
24353 last_chain = &dwarf2_per_objfile->read_in_chain;
24354 while (per_cu != NULL)
24355 {
24356 struct dwarf2_per_cu_data *next_cu;
24357
24358 next_cu = per_cu->cu->read_in_chain;
24359
24360 if (!per_cu->cu->mark)
24361 {
fcd3b13d 24362 delete per_cu->cu;
ae038cb0
DJ
24363 *last_chain = next_cu;
24364 }
24365 else
24366 last_chain = &per_cu->cu->read_in_chain;
24367
24368 per_cu = next_cu;
24369 }
24370}
24371
24372/* Remove a single compilation unit from the cache. */
24373
24374static void
dee91e82 24375free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24376{
24377 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24378 struct dwarf2_per_objfile *dwarf2_per_objfile
24379 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24380
24381 per_cu = dwarf2_per_objfile->read_in_chain;
24382 last_chain = &dwarf2_per_objfile->read_in_chain;
24383 while (per_cu != NULL)
24384 {
24385 struct dwarf2_per_cu_data *next_cu;
24386
24387 next_cu = per_cu->cu->read_in_chain;
24388
dee91e82 24389 if (per_cu == target_per_cu)
ae038cb0 24390 {
fcd3b13d 24391 delete per_cu->cu;
dee91e82 24392 per_cu->cu = NULL;
ae038cb0
DJ
24393 *last_chain = next_cu;
24394 break;
24395 }
24396 else
24397 last_chain = &per_cu->cu->read_in_chain;
24398
24399 per_cu = next_cu;
24400 }
24401}
24402
dee91e82
DE
24403/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24404 We store these in a hash table separate from the DIEs, and preserve them
24405 when the DIEs are flushed out of cache.
24406
24407 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24408 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24409 or the type may come from a DWO file. Furthermore, while it's more logical
24410 to use per_cu->section+offset, with Fission the section with the data is in
24411 the DWO file but we don't know that section at the point we need it.
24412 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24413 because we can enter the lookup routine, get_die_type_at_offset, from
24414 outside this file, and thus won't necessarily have PER_CU->cu.
24415 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24416
dee91e82 24417struct dwarf2_per_cu_offset_and_type
1c379e20 24418{
dee91e82 24419 const struct dwarf2_per_cu_data *per_cu;
9c541725 24420 sect_offset sect_off;
1c379e20
DJ
24421 struct type *type;
24422};
24423
dee91e82 24424/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24425
24426static hashval_t
dee91e82 24427per_cu_offset_and_type_hash (const void *item)
1c379e20 24428{
9a3c8263
SM
24429 const struct dwarf2_per_cu_offset_and_type *ofs
24430 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24431
9c541725 24432 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24433}
24434
dee91e82 24435/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24436
24437static int
dee91e82 24438per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24439{
9a3c8263
SM
24440 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24441 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24442 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24443 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24444
dee91e82 24445 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24446 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24447}
24448
24449/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24450 table if necessary. For convenience, return TYPE.
24451
24452 The DIEs reading must have careful ordering to:
85102364 24453 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24454 reading current DIE.
24455 * Not trying to dereference contents of still incompletely read in types
24456 while reading in other DIEs.
24457 * Enable referencing still incompletely read in types just by a pointer to
24458 the type without accessing its fields.
24459
24460 Therefore caller should follow these rules:
24461 * Try to fetch any prerequisite types we may need to build this DIE type
24462 before building the type and calling set_die_type.
e71ec853 24463 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24464 possible before fetching more types to complete the current type.
24465 * Make the type as complete as possible before fetching more types. */
1c379e20 24466
f792889a 24467static struct type *
1c379e20
DJ
24468set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24469{
518817b3
SM
24470 struct dwarf2_per_objfile *dwarf2_per_objfile
24471 = cu->per_cu->dwarf2_per_objfile;
dee91e82 24472 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 24473 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
24474 struct attribute *attr;
24475 struct dynamic_prop prop;
1c379e20 24476
b4ba55a1
JB
24477 /* For Ada types, make sure that the gnat-specific data is always
24478 initialized (if not already set). There are a few types where
24479 we should not be doing so, because the type-specific area is
24480 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24481 where the type-specific area is used to store the floatformat).
24482 But this is not a problem, because the gnat-specific information
24483 is actually not needed for these types. */
24484 if (need_gnat_info (cu)
24485 && TYPE_CODE (type) != TYPE_CODE_FUNC
24486 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
24487 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24488 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24489 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
24490 && !HAVE_GNAT_AUX_INFO (type))
24491 INIT_GNAT_SPECIFIC (type);
24492
3f2f83dd
KB
24493 /* Read DW_AT_allocated and set in type. */
24494 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 24495 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24496 {
09ba997f 24497 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24498 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24499 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
24500 }
24501 else if (attr != NULL)
24502 {
b98664d3 24503 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 24504 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24505 sect_offset_str (die->sect_off));
3f2f83dd
KB
24506 }
24507
24508 /* Read DW_AT_associated and set in type. */
24509 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 24510 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24511 {
09ba997f 24512 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24513 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24514 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
24515 }
24516 else if (attr != NULL)
24517 {
b98664d3 24518 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 24519 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24520 sect_offset_str (die->sect_off));
3f2f83dd
KB
24521 }
24522
3cdcd0ce
JB
24523 /* Read DW_AT_data_location and set in type. */
24524 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 24525 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 24526 cu->per_cu->addr_type ()))
50a82047 24527 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 24528
dee91e82 24529 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
24530 dwarf2_per_objfile->die_type_hash
24531 = htab_up (htab_create_alloc (127,
24532 per_cu_offset_and_type_hash,
24533 per_cu_offset_and_type_eq,
24534 NULL, xcalloc, xfree));
1c379e20 24535
dee91e82 24536 ofs.per_cu = cu->per_cu;
9c541725 24537 ofs.sect_off = die->sect_off;
1c379e20 24538 ofs.type = type;
dee91e82 24539 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 24540 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24541 if (*slot)
b98664d3 24542 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24543 sect_offset_str (die->sect_off));
8d749320
SM
24544 *slot = XOBNEW (&objfile->objfile_obstack,
24545 struct dwarf2_per_cu_offset_and_type);
1c379e20 24546 **slot = ofs;
f792889a 24547 return type;
1c379e20
DJ
24548}
24549
9c541725 24550/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24551 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24552
24553static struct type *
9c541725 24554get_die_type_at_offset (sect_offset sect_off,
673bfd45 24555 struct dwarf2_per_cu_data *per_cu)
1c379e20 24556{
dee91e82 24557 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 24558 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 24559
dee91e82 24560 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24561 return NULL;
1c379e20 24562
dee91e82 24563 ofs.per_cu = per_cu;
9c541725 24564 ofs.sect_off = sect_off;
9a3c8263 24565 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 24566 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24567 if (slot)
24568 return slot->type;
24569 else
24570 return NULL;
24571}
24572
02142a6c 24573/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24574 or return NULL if DIE does not have a saved type. */
24575
24576static struct type *
24577get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24578{
9c541725 24579 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24580}
24581
10b3939b
DJ
24582/* Add a dependence relationship from CU to REF_PER_CU. */
24583
24584static void
24585dwarf2_add_dependence (struct dwarf2_cu *cu,
24586 struct dwarf2_per_cu_data *ref_per_cu)
24587{
24588 void **slot;
24589
24590 if (cu->dependencies == NULL)
24591 cu->dependencies
24592 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24593 NULL, &cu->comp_unit_obstack,
24594 hashtab_obstack_allocate,
24595 dummy_obstack_deallocate);
24596
24597 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24598 if (*slot == NULL)
24599 *slot = ref_per_cu;
24600}
1c379e20 24601
f504f079
DE
24602/* Subroutine of dwarf2_mark to pass to htab_traverse.
24603 Set the mark field in every compilation unit in the
ae038cb0
DJ
24604 cache that we must keep because we are keeping CU. */
24605
10b3939b
DJ
24606static int
24607dwarf2_mark_helper (void **slot, void *data)
24608{
24609 struct dwarf2_per_cu_data *per_cu;
24610
24611 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24612
24613 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24614 reading of the chain. As such dependencies remain valid it is not much
24615 useful to track and undo them during QUIT cleanups. */
24616 if (per_cu->cu == NULL)
24617 return 1;
24618
10b3939b
DJ
24619 if (per_cu->cu->mark)
24620 return 1;
9068261f 24621 per_cu->cu->mark = true;
10b3939b
DJ
24622
24623 if (per_cu->cu->dependencies != NULL)
24624 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24625
24626 return 1;
24627}
24628
f504f079
DE
24629/* Set the mark field in CU and in every other compilation unit in the
24630 cache that we must keep because we are keeping CU. */
24631
ae038cb0
DJ
24632static void
24633dwarf2_mark (struct dwarf2_cu *cu)
24634{
24635 if (cu->mark)
24636 return;
9068261f 24637 cu->mark = true;
10b3939b
DJ
24638 if (cu->dependencies != NULL)
24639 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24640}
24641
24642static void
24643dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24644{
24645 while (per_cu)
24646 {
9068261f 24647 per_cu->cu->mark = false;
ae038cb0
DJ
24648 per_cu = per_cu->cu->read_in_chain;
24649 }
72bf9492
DJ
24650}
24651
72bf9492
DJ
24652/* Trivial hash function for partial_die_info: the hash value of a DIE
24653 is its offset in .debug_info for this objfile. */
24654
24655static hashval_t
24656partial_die_hash (const void *item)
24657{
9a3c8263
SM
24658 const struct partial_die_info *part_die
24659 = (const struct partial_die_info *) item;
9a619af0 24660
9c541725 24661 return to_underlying (part_die->sect_off);
72bf9492
DJ
24662}
24663
24664/* Trivial comparison function for partial_die_info structures: two DIEs
24665 are equal if they have the same offset. */
24666
24667static int
24668partial_die_eq (const void *item_lhs, const void *item_rhs)
24669{
9a3c8263
SM
24670 const struct partial_die_info *part_die_lhs
24671 = (const struct partial_die_info *) item_lhs;
24672 const struct partial_die_info *part_die_rhs
24673 = (const struct partial_die_info *) item_rhs;
9a619af0 24674
9c541725 24675 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24676}
24677
3c3bb058
AB
24678struct cmd_list_element *set_dwarf_cmdlist;
24679struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24680
24681static void
981a3fb3 24682set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24683{
b4f54984 24684 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24685 gdb_stdout);
ae038cb0
DJ
24686}
24687
24688static void
981a3fb3 24689show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24690{
b4f54984 24691 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24692}
24693
9291a0cd 24694static void
cd4fb1b2
SM
24695show_check_physname (struct ui_file *file, int from_tty,
24696 struct cmd_list_element *c, const char *value)
9291a0cd 24697{
cd4fb1b2
SM
24698 fprintf_filtered (file,
24699 _("Whether to check \"physname\" is %s.\n"),
24700 value);
9291a0cd
TT
24701}
24702
6c265988 24703void _initialize_dwarf2_read ();
cd4fb1b2 24704void
6c265988 24705_initialize_dwarf2_read ()
9291a0cd 24706{
cd4fb1b2
SM
24707 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24708Set DWARF specific variables.\n\
590042fc 24709Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
24710 &set_dwarf_cmdlist, "maintenance set dwarf ",
24711 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24712
cd4fb1b2 24713 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
24714Show DWARF specific variables.\n\
24715Show DWARF variables such as the cache size."),
cd4fb1b2
SM
24716 &show_dwarf_cmdlist, "maintenance show dwarf ",
24717 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24718
cd4fb1b2
SM
24719 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24720 &dwarf_max_cache_age, _("\
24721Set the upper bound on the age of cached DWARF compilation units."), _("\
24722Show the upper bound on the age of cached DWARF compilation units."), _("\
24723A higher limit means that cached compilation units will be stored\n\
24724in memory longer, and more total memory will be used. Zero disables\n\
24725caching, which can slow down startup."),
24726 NULL,
24727 show_dwarf_max_cache_age,
24728 &set_dwarf_cmdlist,
24729 &show_dwarf_cmdlist);
156942c7 24730
cd4fb1b2
SM
24731 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24732Set debugging of the DWARF reader."), _("\
24733Show debugging of the DWARF reader."), _("\
24734When enabled (non-zero), debugging messages are printed during DWARF\n\
24735reading and symtab expansion. A value of 1 (one) provides basic\n\
24736information. A value greater than 1 provides more verbose information."),
24737 NULL,
24738 NULL,
24739 &setdebuglist, &showdebuglist);
9291a0cd 24740
cd4fb1b2
SM
24741 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24742Set debugging of the DWARF DIE reader."), _("\
24743Show debugging of the DWARF DIE reader."), _("\
24744When enabled (non-zero), DIEs are dumped after they are read in.\n\
24745The value is the maximum depth to print."),
24746 NULL,
24747 NULL,
24748 &setdebuglist, &showdebuglist);
9291a0cd 24749
cd4fb1b2
SM
24750 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24751Set debugging of the dwarf line reader."), _("\
24752Show debugging of the dwarf line reader."), _("\
24753When enabled (non-zero), line number entries are dumped as they are read in.\n\
24754A value of 1 (one) provides basic information.\n\
24755A value greater than 1 provides more verbose information."),
24756 NULL,
24757 NULL,
24758 &setdebuglist, &showdebuglist);
437afbb8 24759
cd4fb1b2
SM
24760 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24761Set cross-checking of \"physname\" code against demangler."), _("\
24762Show cross-checking of \"physname\" code against demangler."), _("\
24763When enabled, GDB's internal \"physname\" code is checked against\n\
24764the demangler."),
24765 NULL, show_check_physname,
24766 &setdebuglist, &showdebuglist);
900e11f9 24767
e615022a
DE
24768 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24769 no_class, &use_deprecated_index_sections, _("\
24770Set whether to use deprecated gdb_index sections."), _("\
24771Show whether to use deprecated gdb_index sections."), _("\
24772When enabled, deprecated .gdb_index sections are used anyway.\n\
24773Normally they are ignored either because of a missing feature or\n\
24774performance issue.\n\
24775Warning: This option must be enabled before gdb reads the file."),
24776 NULL,
24777 NULL,
24778 &setlist, &showlist);
24779
f1e6e072
TT
24780 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24781 &dwarf2_locexpr_funcs);
24782 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24783 &dwarf2_loclist_funcs);
24784
24785 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24786 &dwarf2_block_frame_base_locexpr_funcs);
24787 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24788 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24789
24790#if GDB_SELF_TEST
24791 selftests::register_test ("dw2_expand_symtabs_matching",
24792 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24793 selftests::register_test ("dwarf2_find_containing_comp_unit",
24794 selftests::find_containing_comp_unit::run_test);
c62446b1 24795#endif
6502dd73 24796}
This page took 5.057822 seconds and 4 git commands to generate.