Specialize partial_symtab for DWARF include files
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
4de283e4
TT
40#include "bfd.h"
41#include "elf-bfd.h"
42#include "symtab.h"
43#include "gdbtypes.h"
44#include "objfiles.h"
d55e5aa6 45#include "dwarf2.h"
4de283e4
TT
46#include "buildsym.h"
47#include "demangle.h"
48#include "gdb-demangle.h"
4de283e4
TT
49#include "filenames.h" /* for DOSish file names */
50#include "macrotab.h"
51#include "language.h"
52#include "complaints.h"
82ca8957
TT
53#include "dwarf2/expr.h"
54#include "dwarf2/loc.h"
4de283e4
TT
55#include "cp-support.h"
56#include "hashtab.h"
57#include "command.h"
d55e5aa6 58#include "gdbcmd.h"
4de283e4
TT
59#include "block.h"
60#include "addrmap.h"
61#include "typeprint.h"
62#include "psympriv.h"
4de283e4 63#include "c-lang.h"
d55e5aa6 64#include "go-lang.h"
4de283e4
TT
65#include "valprint.h"
66#include "gdbcore.h" /* for gnutarget */
67#include "gdb/gdb-index.h"
4de283e4
TT
68#include "gdb_bfd.h"
69#include "f-lang.h"
70#include "source.h"
4de283e4 71#include "build-id.h"
d55e5aa6 72#include "namespace.h"
268a13a5
TT
73#include "gdbsupport/function-view.h"
74#include "gdbsupport/gdb_optional.h"
75#include "gdbsupport/underlying.h"
268a13a5 76#include "gdbsupport/hash_enum.h"
4de283e4 77#include "filename-seen-cache.h"
b32b108a 78#include "producer.h"
4de283e4 79#include <fcntl.h>
4de283e4 80#include <algorithm>
4de283e4 81#include <unordered_map>
268a13a5 82#include "gdbsupport/selftest.h"
c9317f21 83#include "rust-lang.h"
268a13a5 84#include "gdbsupport/pathstuff.h"
edd45eb0 85#include "count-one-bits.h"
0d79cdc4 86#include "debuginfod-support.h"
437afbb8 87
73be47f5
DE
88/* When == 1, print basic high level tracing messages.
89 When > 1, be more verbose.
b4f54984
DE
90 This is in contrast to the low level DIE reading of dwarf_die_debug. */
91static unsigned int dwarf_read_debug = 0;
45cfd468 92
d97bc12b 93/* When non-zero, dump DIEs after they are read in. */
b4f54984 94static unsigned int dwarf_die_debug = 0;
d97bc12b 95
27e0867f 96/* When non-zero, dump line number entries as they are read in. */
8fdd972c 97unsigned int dwarf_line_debug = 0;
27e0867f 98
491144b5
CB
99/* When true, cross-check physname against demangler. */
100static bool check_physname = false;
900e11f9 101
491144b5
CB
102/* When true, do not reject deprecated .gdb_index sections. */
103static bool use_deprecated_index_sections = false;
481860b3 104
5bfd760d 105static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 106
f1e6e072
TT
107/* The "aclass" indices for various kinds of computed DWARF symbols. */
108
109static int dwarf2_locexpr_index;
110static int dwarf2_loclist_index;
111static int dwarf2_locexpr_block_index;
112static int dwarf2_loclist_block_index;
113
3f563c84
PA
114/* An index into a (C++) symbol name component in a symbol name as
115 recorded in the mapped_index's symbol table. For each C++ symbol
116 in the symbol table, we record one entry for the start of each
117 component in the symbol in a table of name components, and then
118 sort the table, in order to be able to binary search symbol names,
119 ignoring leading namespaces, both completion and regular look up.
120 For example, for symbol "A::B::C", we'll have an entry that points
121 to "A::B::C", another that points to "B::C", and another for "C".
122 Note that function symbols in GDB index have no parameter
123 information, just the function/method names. You can convert a
124 name_component to a "const char *" using the
125 'mapped_index::symbol_name_at(offset_type)' method. */
126
127struct name_component
128{
129 /* Offset in the symbol name where the component starts. Stored as
130 a (32-bit) offset instead of a pointer to save memory and improve
131 locality on 64-bit architectures. */
132 offset_type name_offset;
133
134 /* The symbol's index in the symbol and constant pool tables of a
135 mapped_index. */
136 offset_type idx;
137};
138
44ed8f3e
PA
139/* Base class containing bits shared by both .gdb_index and
140 .debug_name indexes. */
141
142struct mapped_index_base
143{
22ca247e
TT
144 mapped_index_base () = default;
145 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
146
44ed8f3e
PA
147 /* The name_component table (a sorted vector). See name_component's
148 description above. */
149 std::vector<name_component> name_components;
150
151 /* How NAME_COMPONENTS is sorted. */
152 enum case_sensitivity name_components_casing;
153
154 /* Return the number of names in the symbol table. */
155 virtual size_t symbol_name_count () const = 0;
156
157 /* Get the name of the symbol at IDX in the symbol table. */
158 virtual const char *symbol_name_at (offset_type idx) const = 0;
159
160 /* Return whether the name at IDX in the symbol table should be
161 ignored. */
162 virtual bool symbol_name_slot_invalid (offset_type idx) const
163 {
164 return false;
165 }
166
167 /* Build the symbol name component sorted vector, if we haven't
168 yet. */
169 void build_name_components ();
170
171 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
172 possible matches for LN_NO_PARAMS in the name component
173 vector. */
174 std::pair<std::vector<name_component>::const_iterator,
175 std::vector<name_component>::const_iterator>
3b00ef10
TT
176 find_name_components_bounds (const lookup_name_info &ln_no_params,
177 enum language lang) const;
44ed8f3e
PA
178
179 /* Prevent deleting/destroying via a base class pointer. */
180protected:
181 ~mapped_index_base() = default;
182};
183
9291a0cd
TT
184/* A description of the mapped index. The file format is described in
185 a comment by the code that writes the index. */
fc898b42 186struct mapped_index final : public mapped_index_base
9291a0cd 187{
f00a2de2
PA
188 /* A slot/bucket in the symbol table hash. */
189 struct symbol_table_slot
190 {
191 const offset_type name;
192 const offset_type vec;
193 };
194
559a7a62 195 /* Index data format version. */
3063847f 196 int version = 0;
559a7a62 197
f00a2de2
PA
198 /* The address table data. */
199 gdb::array_view<const gdb_byte> address_table;
b11b1f88 200
3876f04e 201 /* The symbol table, implemented as a hash table. */
f00a2de2 202 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 203
9291a0cd 204 /* A pointer to the constant pool. */
3063847f 205 const char *constant_pool = nullptr;
3f563c84 206
44ed8f3e
PA
207 bool symbol_name_slot_invalid (offset_type idx) const override
208 {
209 const auto &bucket = this->symbol_table[idx];
9ab08412 210 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 211 }
5c58de74 212
3f563c84
PA
213 /* Convenience method to get at the name of the symbol at IDX in the
214 symbol table. */
44ed8f3e 215 const char *symbol_name_at (offset_type idx) const override
f00a2de2 216 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 217
44ed8f3e
PA
218 size_t symbol_name_count () const override
219 { return this->symbol_table.size (); }
9291a0cd
TT
220};
221
927aa2e7
JK
222/* A description of the mapped .debug_names.
223 Uninitialized map has CU_COUNT 0. */
fc898b42 224struct mapped_debug_names final : public mapped_index_base
927aa2e7 225{
ed2dc618
SM
226 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
227 : dwarf2_per_objfile (dwarf2_per_objfile_)
228 {}
229
230 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
231 bfd_endian dwarf5_byte_order;
232 bool dwarf5_is_dwarf64;
233 bool augmentation_is_gdb;
234 uint8_t offset_size;
235 uint32_t cu_count = 0;
236 uint32_t tu_count, bucket_count, name_count;
237 const gdb_byte *cu_table_reordered, *tu_table_reordered;
238 const uint32_t *bucket_table_reordered, *hash_table_reordered;
239 const gdb_byte *name_table_string_offs_reordered;
240 const gdb_byte *name_table_entry_offs_reordered;
241 const gdb_byte *entry_pool;
242
243 struct index_val
244 {
245 ULONGEST dwarf_tag;
246 struct attr
247 {
248 /* Attribute name DW_IDX_*. */
249 ULONGEST dw_idx;
250
251 /* Attribute form DW_FORM_*. */
252 ULONGEST form;
253
254 /* Value if FORM is DW_FORM_implicit_const. */
255 LONGEST implicit_const;
256 };
257 std::vector<attr> attr_vec;
258 };
259
260 std::unordered_map<ULONGEST, index_val> abbrev_map;
261
262 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
263
264 /* Implementation of the mapped_index_base virtual interface, for
265 the name_components cache. */
266
267 const char *symbol_name_at (offset_type idx) const override
268 { return namei_to_name (idx); }
269
270 size_t symbol_name_count () const override
271 { return this->name_count; }
927aa2e7
JK
272};
273
cd4fb1b2 274/* See dwarf2read.h. */
ed2dc618 275
cd4fb1b2 276dwarf2_per_objfile *
ed2dc618
SM
277get_dwarf2_per_objfile (struct objfile *objfile)
278{
5bfd760d 279 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 280}
c906108c 281
251d32d9 282/* Default names of the debugging sections. */
c906108c 283
233a11ab
CS
284/* Note that if the debugging section has been compressed, it might
285 have a name like .zdebug_info. */
286
9cdd5dbd
DE
287static const struct dwarf2_debug_sections dwarf2_elf_names =
288{
251d32d9
TG
289 { ".debug_info", ".zdebug_info" },
290 { ".debug_abbrev", ".zdebug_abbrev" },
291 { ".debug_line", ".zdebug_line" },
292 { ".debug_loc", ".zdebug_loc" },
43988095 293 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 294 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 295 { ".debug_macro", ".zdebug_macro" },
251d32d9 296 { ".debug_str", ".zdebug_str" },
18a8505e 297 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 298 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 299 { ".debug_ranges", ".zdebug_ranges" },
43988095 300 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 301 { ".debug_types", ".zdebug_types" },
3019eac3 302 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
303 { ".debug_frame", ".zdebug_frame" },
304 { ".eh_frame", NULL },
24d3216f 305 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
306 { ".debug_names", ".zdebug_names" },
307 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 308 23
251d32d9 309};
c906108c 310
80626a55 311/* List of DWO/DWP sections. */
3019eac3 312
80626a55 313static const struct dwop_section_names
3019eac3
DE
314{
315 struct dwarf2_section_names abbrev_dwo;
316 struct dwarf2_section_names info_dwo;
317 struct dwarf2_section_names line_dwo;
318 struct dwarf2_section_names loc_dwo;
43988095 319 struct dwarf2_section_names loclists_dwo;
09262596
DE
320 struct dwarf2_section_names macinfo_dwo;
321 struct dwarf2_section_names macro_dwo;
3019eac3
DE
322 struct dwarf2_section_names str_dwo;
323 struct dwarf2_section_names str_offsets_dwo;
324 struct dwarf2_section_names types_dwo;
80626a55
DE
325 struct dwarf2_section_names cu_index;
326 struct dwarf2_section_names tu_index;
3019eac3 327}
80626a55 328dwop_section_names =
3019eac3
DE
329{
330 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
331 { ".debug_info.dwo", ".zdebug_info.dwo" },
332 { ".debug_line.dwo", ".zdebug_line.dwo" },
333 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 334 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
335 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
336 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
337 { ".debug_str.dwo", ".zdebug_str.dwo" },
338 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
339 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
340 { ".debug_cu_index", ".zdebug_cu_index" },
341 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
342};
343
c906108c
SS
344/* local data types */
345
3da10d80
KS
346/* Type used for delaying computation of method physnames.
347 See comments for compute_delayed_physnames. */
348struct delayed_method_info
349{
350 /* The type to which the method is attached, i.e., its parent class. */
351 struct type *type;
352
353 /* The index of the method in the type's function fieldlists. */
354 int fnfield_index;
355
356 /* The index of the method in the fieldlist. */
357 int index;
358
359 /* The name of the DIE. */
360 const char *name;
361
362 /* The DIE associated with this method. */
363 struct die_info *die;
364};
365
e7c27a73
DJ
366/* Internal state when decoding a particular compilation unit. */
367struct dwarf2_cu
368{
fcd3b13d
SM
369 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
370 ~dwarf2_cu ();
371
372 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
373
c24bdb02
KS
374 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
375 Create the set of symtabs used by this TU, or if this TU is sharing
376 symtabs with another TU and the symtabs have already been created
377 then restore those symtabs in the line header.
378 We don't need the pc/line-number mapping for type units. */
379 void setup_type_unit_groups (struct die_info *die);
380
381 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
382 buildsym_compunit constructor. */
383 struct compunit_symtab *start_symtab (const char *name,
384 const char *comp_dir,
385 CORE_ADDR low_pc);
386
387 /* Reset the builder. */
388 void reset_builder () { m_builder.reset (); }
389
d00adf39 390 /* The header of the compilation unit. */
fcd3b13d 391 struct comp_unit_head header {};
e142c38c 392
d00adf39 393 /* Base address of this compilation unit. */
fcd3b13d 394 CORE_ADDR base_address = 0;
d00adf39
DE
395
396 /* Non-zero if base_address has been set. */
fcd3b13d 397 int base_known = 0;
d00adf39 398
e142c38c 399 /* The language we are debugging. */
fcd3b13d
SM
400 enum language language = language_unknown;
401 const struct language_defn *language_defn = nullptr;
e142c38c 402
fcd3b13d 403 const char *producer = nullptr;
b0f35d58 404
c24bdb02 405private:
804d2729
TT
406 /* The symtab builder for this CU. This is only non-NULL when full
407 symbols are being read. */
c24bdb02 408 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 409
c24bdb02 410public:
e142c38c
DJ
411 /* The generic symbol table building routines have separate lists for
412 file scope symbols and all all other scopes (local scopes). So
413 we need to select the right one to pass to add_symbol_to_list().
414 We do it by keeping a pointer to the correct list in list_in_scope.
415
416 FIXME: The original dwarf code just treated the file scope as the
417 first local scope, and all other local scopes as nested local
418 scopes, and worked fine. Check to see if we really need to
419 distinguish these in buildsym.c. */
fcd3b13d 420 struct pending **list_in_scope = nullptr;
e142c38c 421
b64f50a1
JK
422 /* Hash table holding all the loaded partial DIEs
423 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 424 htab_t partial_dies = nullptr;
72bf9492
DJ
425
426 /* Storage for things with the same lifetime as this read-in compilation
427 unit, including partial DIEs. */
fcd3b13d 428 auto_obstack comp_unit_obstack;
72bf9492 429
ae038cb0
DJ
430 /* When multiple dwarf2_cu structures are living in memory, this field
431 chains them all together, so that they can be released efficiently.
432 We will probably also want a generation counter so that most-recently-used
433 compilation units are cached... */
fcd3b13d 434 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 435
69d751e3 436 /* Backlink to our per_cu entry. */
ae038cb0
DJ
437 struct dwarf2_per_cu_data *per_cu;
438
439 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 440 int last_used = 0;
ae038cb0 441
b64f50a1
JK
442 /* A hash table of DIE cu_offset for following references with
443 die_info->offset.sect_off as hash. */
fcd3b13d 444 htab_t die_hash = nullptr;
10b3939b
DJ
445
446 /* Full DIEs if read in. */
fcd3b13d 447 struct die_info *dies = nullptr;
10b3939b
DJ
448
449 /* A set of pointers to dwarf2_per_cu_data objects for compilation
450 units referenced by this one. Only set during full symbol processing;
451 partial symbol tables do not have dependencies. */
fcd3b13d 452 htab_t dependencies = nullptr;
10b3939b 453
cb1df416 454 /* Header data from the line table, during full symbol processing. */
fcd3b13d 455 struct line_header *line_header = nullptr;
4c8aa72d
PA
456 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
457 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
458 this is the DW_TAG_compile_unit die for this CU. We'll hold on
459 to the line header as long as this DIE is being processed. See
460 process_die_scope. */
fcd3b13d 461 die_info *line_header_die_owner = nullptr;
cb1df416 462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
c89b44cd 465 std::vector<delayed_method_info> method_list;
3da10d80 466
96408a79 467 /* To be copied to symtab->call_site_htab. */
fcd3b13d 468 htab_t call_site_htab = nullptr;
96408a79 469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
18a8505e 474 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
fcd3b13d 479 struct dwo_unit *dwo_unit = nullptr;
3019eac3 480
18a8505e 481 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 482 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 483 gdb::optional<ULONGEST> addr_base;
3019eac3 484
18a8505e 485 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 486 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 487 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 492 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 494 ULONGEST ranges_base = 0;
2e3cf129 495
c9317f21
TT
496 /* When reading debug info generated by older versions of rustc, we
497 have to rewrite some union types to be struct types with a
498 variant part. This rewriting must be done after the CU is fully
499 read in, because otherwise at the point of rewriting some struct
500 type might not have been fully processed. So, we keep a list of
501 all such types here and process them after expansion. */
502 std::vector<struct type *> rust_unions;
503
18a8505e
AT
504 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
505 files, the value is implicitly zero. For DWARF 5 version DWO files, the
506 value is often implicit and is the size of the header of
507 .debug_str_offsets section (8 or 4, depending on the address size). */
508 gdb::optional<ULONGEST> str_offsets_base;
509
ae038cb0 510 /* Mark used when releasing cached dies. */
9068261f 511 bool mark : 1;
ae038cb0 512
8be455d7
JK
513 /* This CU references .debug_loc. See the symtab->locations_valid field.
514 This test is imperfect as there may exist optimized debug code not using
515 any location list and still facing inlining issues if handled as
516 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 517 bool has_loclist : 1;
ba919b58 518
9068261f 519 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
520 if all the producer_is_* fields are valid. This information is cached
521 because profiling CU expansion showed excessive time spent in
522 producer_is_gxx_lt_4_6. */
9068261f
AB
523 bool checked_producer : 1;
524 bool producer_is_gxx_lt_4_6 : 1;
525 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 526 bool producer_is_icc : 1;
9068261f 527 bool producer_is_icc_lt_14 : 1;
c258c396 528 bool producer_is_codewarrior : 1;
4d4ec4e5 529
9068261f 530 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
531 debugging info for C++ namespaces. GCC 3.3.x did not produce
532 this information, but later versions do. */
533
9068261f 534 bool processing_has_namespace_info : 1;
d590ff25
YQ
535
536 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
537
538 /* If this CU was inherited by another CU (via specification,
539 abstract_origin, etc), this is the ancestor CU. */
540 dwarf2_cu *ancestor;
541
542 /* Get the buildsym_compunit for this CU. */
543 buildsym_compunit *get_builder ()
544 {
545 /* If this CU has a builder associated with it, use that. */
546 if (m_builder != nullptr)
547 return m_builder.get ();
548
549 /* Otherwise, search ancestors for a valid builder. */
550 if (ancestor != nullptr)
551 return ancestor->get_builder ();
552
553 return nullptr;
554 }
e7c27a73
DJ
555};
556
094b34ac
DE
557/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
558 This includes type_unit_group and quick_file_names. */
559
560struct stmt_list_hash
561{
562 /* The DWO unit this table is from or NULL if there is none. */
563 struct dwo_unit *dwo_unit;
564
565 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 566 sect_offset line_sect_off;
094b34ac
DE
567};
568
f4dc4d17
DE
569/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
570 an object of this type. */
571
572struct type_unit_group
573{
0186c6a7 574 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
575 To simplify things we create an artificial CU that "includes" all the
576 type units using this stmt_list so that the rest of the code still has
197400e8 577 a "per_cu" handle on the symtab. */
094b34ac
DE
578 struct dwarf2_per_cu_data per_cu;
579
0186c6a7
DE
580 /* The TUs that share this DW_AT_stmt_list entry.
581 This is added to while parsing type units to build partial symtabs,
582 and is deleted afterwards and not used again. */
a8b3b8e9 583 std::vector<signatured_type *> *tus;
f4dc4d17 584
43f3e411 585 /* The compunit symtab.
094b34ac 586 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
587 so we create an essentially anonymous symtab as the compunit symtab. */
588 struct compunit_symtab *compunit_symtab;
f4dc4d17 589
094b34ac
DE
590 /* The data used to construct the hash key. */
591 struct stmt_list_hash hash;
f4dc4d17 592
f4dc4d17
DE
593 /* The symbol tables for this TU (obtained from the files listed in
594 DW_AT_stmt_list).
595 WARNING: The order of entries here must match the order of entries
596 in the line header. After the first TU using this type_unit_group, the
597 line header for the subsequent TUs is recreated from this. This is done
598 because we need to use the same symtabs for each TU using the same
599 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
600 there's no guarantee the line header doesn't have duplicate entries. */
601 struct symtab **symtabs;
602};
603
73869dc2 604/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
605
606struct dwo_sections
607{
608 struct dwarf2_section_info abbrev;
3019eac3
DE
609 struct dwarf2_section_info line;
610 struct dwarf2_section_info loc;
43988095 611 struct dwarf2_section_info loclists;
09262596
DE
612 struct dwarf2_section_info macinfo;
613 struct dwarf2_section_info macro;
3019eac3
DE
614 struct dwarf2_section_info str;
615 struct dwarf2_section_info str_offsets;
80626a55
DE
616 /* In the case of a virtual DWO file, these two are unused. */
617 struct dwarf2_section_info info;
fd5866f6 618 std::vector<dwarf2_section_info> types;
3019eac3
DE
619};
620
c88ee1f0 621/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
622
623struct dwo_unit
624{
625 /* Backlink to the containing struct dwo_file. */
626 struct dwo_file *dwo_file;
627
628 /* The "id" that distinguishes this CU/TU.
629 .debug_info calls this "dwo_id", .debug_types calls this "signature".
630 Since signatures came first, we stick with it for consistency. */
631 ULONGEST signature;
632
633 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 634 struct dwarf2_section_info *section;
3019eac3 635
9c541725
PA
636 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
637 sect_offset sect_off;
3019eac3
DE
638 unsigned int length;
639
640 /* For types, offset in the type's DIE of the type defined by this TU. */
641 cu_offset type_offset_in_tu;
642};
643
73869dc2
DE
644/* include/dwarf2.h defines the DWP section codes.
645 It defines a max value but it doesn't define a min value, which we
646 use for error checking, so provide one. */
647
648enum dwp_v2_section_ids
649{
650 DW_SECT_MIN = 1
651};
652
80626a55 653/* Data for one DWO file.
57d63ce2
DE
654
655 This includes virtual DWO files (a virtual DWO file is a DWO file as it
656 appears in a DWP file). DWP files don't really have DWO files per se -
657 comdat folding of types "loses" the DWO file they came from, and from
658 a high level view DWP files appear to contain a mass of random types.
659 However, to maintain consistency with the non-DWP case we pretend DWP
660 files contain virtual DWO files, and we assign each TU with one virtual
661 DWO file (generally based on the line and abbrev section offsets -
662 a heuristic that seems to work in practice). */
3019eac3
DE
663
664struct dwo_file
665{
51ac9db5
SM
666 dwo_file () = default;
667 DISABLE_COPY_AND_ASSIGN (dwo_file);
668
18a8505e 669 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
670 For virtual DWO files the name is constructed from the section offsets
671 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
672 from related CU+TUs. */
51ac9db5 673 const char *dwo_name = nullptr;
0ac5b59e
DE
674
675 /* The DW_AT_comp_dir attribute. */
51ac9db5 676 const char *comp_dir = nullptr;
3019eac3 677
80626a55
DE
678 /* The bfd, when the file is open. Otherwise this is NULL.
679 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 680 gdb_bfd_ref_ptr dbfd;
3019eac3 681
73869dc2
DE
682 /* The sections that make up this DWO file.
683 Remember that for virtual DWO files in DWP V2, these are virtual
684 sections (for lack of a better name). */
51ac9db5 685 struct dwo_sections sections {};
3019eac3 686
33c5cd75
DB
687 /* The CUs in the file.
688 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
689 an extension to handle LLVM's Link Time Optimization output (where
690 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 691 htab_up cus;
3019eac3
DE
692
693 /* Table of TUs in the file.
694 Each element is a struct dwo_unit. */
b0b6a987 695 htab_up tus;
3019eac3
DE
696};
697
80626a55
DE
698/* These sections are what may appear in a DWP file. */
699
700struct dwp_sections
701{
73869dc2 702 /* These are used by both DWP version 1 and 2. */
80626a55
DE
703 struct dwarf2_section_info str;
704 struct dwarf2_section_info cu_index;
705 struct dwarf2_section_info tu_index;
73869dc2
DE
706
707 /* These are only used by DWP version 2 files.
708 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
709 sections are referenced by section number, and are not recorded here.
710 In DWP version 2 there is at most one copy of all these sections, each
711 section being (effectively) comprised of the concatenation of all of the
712 individual sections that exist in the version 1 format.
713 To keep the code simple we treat each of these concatenated pieces as a
714 section itself (a virtual section?). */
715 struct dwarf2_section_info abbrev;
716 struct dwarf2_section_info info;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
721 struct dwarf2_section_info str_offsets;
722 struct dwarf2_section_info types;
80626a55
DE
723};
724
73869dc2
DE
725/* These sections are what may appear in a virtual DWO file in DWP version 1.
726 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 727
73869dc2 728struct virtual_v1_dwo_sections
80626a55
DE
729{
730 struct dwarf2_section_info abbrev;
731 struct dwarf2_section_info line;
732 struct dwarf2_section_info loc;
733 struct dwarf2_section_info macinfo;
734 struct dwarf2_section_info macro;
735 struct dwarf2_section_info str_offsets;
736 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 737 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
738 struct dwarf2_section_info info_or_types;
739};
740
73869dc2
DE
741/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
742 In version 2, the sections of the DWO files are concatenated together
743 and stored in one section of that name. Thus each ELF section contains
744 several "virtual" sections. */
745
746struct virtual_v2_dwo_sections
747{
748 bfd_size_type abbrev_offset;
749 bfd_size_type abbrev_size;
750
751 bfd_size_type line_offset;
752 bfd_size_type line_size;
753
754 bfd_size_type loc_offset;
755 bfd_size_type loc_size;
756
757 bfd_size_type macinfo_offset;
758 bfd_size_type macinfo_size;
759
760 bfd_size_type macro_offset;
761 bfd_size_type macro_size;
762
763 bfd_size_type str_offsets_offset;
764 bfd_size_type str_offsets_size;
765
766 /* Each DWP hash table entry records one CU or one TU.
767 That is recorded here, and copied to dwo_unit.section. */
768 bfd_size_type info_or_types_offset;
769 bfd_size_type info_or_types_size;
770};
771
80626a55
DE
772/* Contents of DWP hash tables. */
773
774struct dwp_hash_table
775{
73869dc2 776 uint32_t version, nr_columns;
80626a55 777 uint32_t nr_units, nr_slots;
73869dc2
DE
778 const gdb_byte *hash_table, *unit_table;
779 union
780 {
781 struct
782 {
783 const gdb_byte *indices;
784 } v1;
785 struct
786 {
787 /* This is indexed by column number and gives the id of the section
788 in that column. */
789#define MAX_NR_V2_DWO_SECTIONS \
790 (1 /* .debug_info or .debug_types */ \
791 + 1 /* .debug_abbrev */ \
792 + 1 /* .debug_line */ \
793 + 1 /* .debug_loc */ \
794 + 1 /* .debug_str_offsets */ \
795 + 1 /* .debug_macro or .debug_macinfo */)
796 int section_ids[MAX_NR_V2_DWO_SECTIONS];
797 const gdb_byte *offsets;
798 const gdb_byte *sizes;
799 } v2;
800 } section_pool;
80626a55
DE
801};
802
803/* Data for one DWP file. */
804
805struct dwp_file
806{
400174b1
TT
807 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
808 : name (name_),
809 dbfd (std::move (abfd))
810 {
811 }
812
80626a55
DE
813 /* Name of the file. */
814 const char *name;
815
73869dc2 816 /* File format version. */
400174b1 817 int version = 0;
73869dc2 818
93417882 819 /* The bfd. */
400174b1 820 gdb_bfd_ref_ptr dbfd;
80626a55
DE
821
822 /* Section info for this file. */
400174b1 823 struct dwp_sections sections {};
80626a55 824
57d63ce2 825 /* Table of CUs in the file. */
400174b1 826 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
827
828 /* Table of TUs in the file. */
400174b1 829 const struct dwp_hash_table *tus = nullptr;
80626a55 830
19ac8c2e 831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
832 htab_up loaded_cus;
833 htab_up loaded_tus;
80626a55 834
73869dc2
DE
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
400174b1
TT
837 unsigned int num_sections = 0;
838 asection **elf_sections = nullptr;
80626a55
DE
839};
840
0963b4bd
MS
841/* Struct used to pass misc. parameters to read_die_and_children, et
842 al. which are used for both .debug_info and .debug_types dies.
843 All parameters here are unchanging for the life of the call. This
dee91e82 844 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
845
846struct die_reader_specs
847{
a32a8923 848 /* The bfd of die_section. */
93311388
DE
849 bfd* abfd;
850
851 /* The CU of the DIE we are parsing. */
852 struct dwarf2_cu *cu;
853
80626a55 854 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
855 struct dwo_file *dwo_file;
856
dee91e82 857 /* The section the die comes from.
3019eac3 858 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
859 struct dwarf2_section_info *die_section;
860
861 /* die_section->buffer. */
d521ce57 862 const gdb_byte *buffer;
f664829e
DE
863
864 /* The end of the buffer. */
865 const gdb_byte *buffer_end;
a2ce51a0 866
685af9cd
TT
867 /* The abbreviation table to use when reading the DIEs. */
868 struct abbrev_table *abbrev_table;
93311388
DE
869};
870
c0ab21c2
TT
871/* A subclass of die_reader_specs that holds storage and has complex
872 constructor and destructor behavior. */
873
874class cutu_reader : public die_reader_specs
875{
876public:
877
878 cutu_reader (struct dwarf2_per_cu_data *this_cu,
879 struct abbrev_table *abbrev_table,
6751ebae 880 int use_existing_cu,
c0ab21c2
TT
881 bool skip_partial);
882
883 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
884 struct dwarf2_cu *parent_cu = nullptr,
885 struct dwo_file *dwo_file = nullptr);
886
c0ab21c2
TT
887 DISABLE_COPY_AND_ASSIGN (cutu_reader);
888
889 const gdb_byte *info_ptr = nullptr;
890 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
891 bool dummy_p = false;
892
6751ebae
TT
893 /* Release the new CU, putting it on the chain. This cannot be done
894 for dummy CUs. */
895 void keep ();
896
c0ab21c2
TT
897private:
898 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 899 int use_existing_cu);
c0ab21c2
TT
900
901 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
902 std::unique_ptr<dwarf2_cu> m_new_cu;
903
904 /* The ordinary abbreviation table. */
905 abbrev_table_up m_abbrev_table_holder;
906
907 /* The DWO abbreviation table. */
908 abbrev_table_up m_dwo_abbrev_table;
909};
dee91e82 910
c906108c 911/* When we construct a partial symbol table entry we only
0963b4bd 912 need this much information. */
6f06d47b 913struct partial_die_info : public allocate_on_obstack
c906108c 914 {
6f06d47b
YQ
915 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
916
917 /* Disable assign but still keep copy ctor, which is needed
918 load_partial_dies. */
919 partial_die_info& operator=(const partial_die_info& rhs) = delete;
920
52356b79
YQ
921 /* Adjust the partial die before generating a symbol for it. This
922 function may set the is_external flag or change the DIE's
923 name. */
924 void fixup (struct dwarf2_cu *cu);
925
48fbe735
YQ
926 /* Read a minimal amount of information into the minimal die
927 structure. */
928 const gdb_byte *read (const struct die_reader_specs *reader,
929 const struct abbrev_info &abbrev,
930 const gdb_byte *info_ptr);
931
72bf9492 932 /* Offset of this DIE. */
6f06d47b 933 const sect_offset sect_off;
72bf9492
DJ
934
935 /* DWARF-2 tag for this DIE. */
6f06d47b 936 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 937
72bf9492 938 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
939 const unsigned int has_children : 1;
940
72bf9492
DJ
941 unsigned int is_external : 1;
942 unsigned int is_declaration : 1;
943 unsigned int has_type : 1;
944 unsigned int has_specification : 1;
945 unsigned int has_pc_info : 1;
481860b3 946 unsigned int may_be_inlined : 1;
72bf9492 947
0c1b455e
TT
948 /* This DIE has been marked DW_AT_main_subprogram. */
949 unsigned int main_subprogram : 1;
950
72bf9492
DJ
951 /* Flag set if the SCOPE field of this structure has been
952 computed. */
953 unsigned int scope_set : 1;
954
fa4028e9
JB
955 /* Flag set if the DIE has a byte_size attribute. */
956 unsigned int has_byte_size : 1;
957
ff908ebf
AW
958 /* Flag set if the DIE has a DW_AT_const_value attribute. */
959 unsigned int has_const_value : 1;
960
98bfdba5
PA
961 /* Flag set if any of the DIE's children are template arguments. */
962 unsigned int has_template_arguments : 1;
963
52356b79 964 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
965 unsigned int fixup_called : 1;
966
36586728
TT
967 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
968 unsigned int is_dwz : 1;
969
970 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
971 unsigned int spec_is_dwz : 1;
972
72bf9492 973 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 974 sometimes a default name for unnamed DIEs. */
6f06d47b 975 const char *name = nullptr;
72bf9492 976
abc72ce4 977 /* The linkage name, if present. */
6f06d47b 978 const char *linkage_name = nullptr;
abc72ce4 979
72bf9492
DJ
980 /* The scope to prepend to our children. This is generally
981 allocated on the comp_unit_obstack, so will disappear
982 when this compilation unit leaves the cache. */
6f06d47b 983 const char *scope = nullptr;
72bf9492 984
95554aad
TT
985 /* Some data associated with the partial DIE. The tag determines
986 which field is live. */
987 union
988 {
989 /* The location description associated with this DIE, if any. */
990 struct dwarf_block *locdesc;
991 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 992 sect_offset sect_off;
6f06d47b 993 } d {};
72bf9492
DJ
994
995 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
996 CORE_ADDR lowpc = 0;
997 CORE_ADDR highpc = 0;
72bf9492 998
93311388 999 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1000 DW_AT_sibling, if any. */
48fbe735
YQ
1001 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1002 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1003 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1004
1005 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1006 DW_AT_specification (or DW_AT_abstract_origin or
1007 DW_AT_extension). */
6f06d47b 1008 sect_offset spec_offset {};
72bf9492
DJ
1009
1010 /* Pointers to this DIE's parent, first child, and next sibling,
1011 if any. */
6f06d47b
YQ
1012 struct partial_die_info *die_parent = nullptr;
1013 struct partial_die_info *die_child = nullptr;
1014 struct partial_die_info *die_sibling = nullptr;
1015
1016 friend struct partial_die_info *
1017 dwarf2_cu::find_partial_die (sect_offset sect_off);
1018
1019 private:
1020 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1021 partial_die_info (sect_offset sect_off)
1022 : partial_die_info (sect_off, DW_TAG_padding, 0)
1023 {
1024 }
1025
1026 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1027 int has_children_)
1028 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1029 {
1030 is_external = 0;
1031 is_declaration = 0;
1032 has_type = 0;
1033 has_specification = 0;
1034 has_pc_info = 0;
1035 may_be_inlined = 0;
1036 main_subprogram = 0;
1037 scope_set = 0;
1038 has_byte_size = 0;
1039 has_const_value = 0;
1040 has_template_arguments = 0;
1041 fixup_called = 0;
1042 is_dwz = 0;
1043 spec_is_dwz = 0;
1044 }
c906108c
SS
1045 };
1046
0963b4bd 1047/* This data structure holds a complete die structure. */
c906108c
SS
1048struct die_info
1049 {
76815b17
DE
1050 /* DWARF-2 tag for this DIE. */
1051 ENUM_BITFIELD(dwarf_tag) tag : 16;
1052
1053 /* Number of attributes */
98bfdba5
PA
1054 unsigned char num_attrs;
1055
1056 /* True if we're presently building the full type name for the
1057 type derived from this DIE. */
1058 unsigned char building_fullname : 1;
76815b17 1059
adde2bff
DE
1060 /* True if this die is in process. PR 16581. */
1061 unsigned char in_process : 1;
1062
3e225074
TT
1063 /* True if this DIE has children. */
1064 unsigned char has_children : 1;
1065
76815b17
DE
1066 /* Abbrev number */
1067 unsigned int abbrev;
1068
93311388 1069 /* Offset in .debug_info or .debug_types section. */
9c541725 1070 sect_offset sect_off;
78ba4af6
JB
1071
1072 /* The dies in a compilation unit form an n-ary tree. PARENT
1073 points to this die's parent; CHILD points to the first child of
1074 this node; and all the children of a given node are chained
4950bc1c 1075 together via their SIBLING fields. */
639d11d3
DC
1076 struct die_info *child; /* Its first child, if any. */
1077 struct die_info *sibling; /* Its next sibling, if any. */
1078 struct die_info *parent; /* Its parent, if any. */
c906108c 1079
b60c80d6
DJ
1080 /* An array of attributes, with NUM_ATTRS elements. There may be
1081 zero, but it's not common and zero-sized arrays are not
1082 sufficiently portable C. */
1083 struct attribute attrs[1];
c906108c
SS
1084 };
1085
c906108c
SS
1086/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1087 but this would require a corresponding change in unpack_field_as_long
1088 and friends. */
1089static int bits_per_byte = 8;
1090
2ddeaf8a
TT
1091/* When reading a variant or variant part, we track a bit more
1092 information about the field, and store it in an object of this
1093 type. */
1094
1095struct variant_field
1096{
1097 /* If we see a DW_TAG_variant, then this will be the discriminant
1098 value. */
1099 ULONGEST discriminant_value;
1100 /* If we see a DW_TAG_variant, then this will be set if this is the
1101 default branch. */
1102 bool default_branch;
1103 /* While reading a DW_TAG_variant_part, this will be set if this
1104 field is the discriminant. */
1105 bool is_discriminant;
1106};
1107
52059ffd
TT
1108struct nextfield
1109{
be2daae6
TT
1110 int accessibility = 0;
1111 int virtuality = 0;
2ddeaf8a 1112 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1113 struct variant_field variant {};
1114 struct field field {};
52059ffd
TT
1115};
1116
1117struct fnfieldlist
1118{
be2daae6
TT
1119 const char *name = nullptr;
1120 std::vector<struct fn_field> fnfields;
52059ffd
TT
1121};
1122
c906108c
SS
1123/* The routines that read and process dies for a C struct or C++ class
1124 pass lists of data member fields and lists of member function fields
1125 in an instance of a field_info structure, as defined below. */
1126struct field_info
c5aa993b 1127 {
0963b4bd 1128 /* List of data member and baseclasses fields. */
be2daae6
TT
1129 std::vector<struct nextfield> fields;
1130 std::vector<struct nextfield> baseclasses;
c906108c 1131
7d0ccb61 1132 /* Number of fields (including baseclasses). */
be2daae6 1133 int nfields = 0;
c906108c 1134
85102364 1135 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1136 int non_public_fields = 0;
c906108c 1137
c5aa993b
JM
1138 /* Member function fieldlist array, contains name of possibly overloaded
1139 member function, number of overloaded member functions and a pointer
1140 to the head of the member function field chain. */
be2daae6 1141 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1142
1143 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1144 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1145 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1146
1147 /* Nested types defined by this class and the number of elements in this
1148 list. */
be2daae6 1149 std::vector<struct decl_field> nested_types_list;
c5aa993b 1150 };
c906108c 1151
ae038cb0
DJ
1152/* Loaded secondary compilation units are kept in memory until they
1153 have not been referenced for the processing of this many
1154 compilation units. Set this to zero to disable caching. Cache
1155 sizes of up to at least twenty will improve startup time for
1156 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1157static int dwarf_max_cache_age = 5;
920d2a44 1158static void
b4f54984
DE
1159show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1160 struct cmd_list_element *c, const char *value)
920d2a44 1161{
3e43a32a 1162 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1163 "DWARF compilation units is %s.\n"),
920d2a44
AC
1164 value);
1165}
4390d890 1166\f
c906108c
SS
1167/* local function prototypes */
1168
918dd910
JK
1169static void dwarf2_find_base_address (struct die_info *die,
1170 struct dwarf2_cu *cu);
1171
891813be 1172static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1173 (struct dwarf2_per_cu_data *per_cu, const char *name);
1174
f1902523
JK
1175static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1176 const gdb_byte *info_ptr,
3e225074 1177 struct die_info *type_unit_die);
f1902523 1178
ed2dc618
SM
1179static void dwarf2_build_psymtabs_hard
1180 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1181
72bf9492
DJ
1182static void scan_partial_symbols (struct partial_die_info *,
1183 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1184 int, struct dwarf2_cu *);
c906108c 1185
72bf9492
DJ
1186static void add_partial_symbol (struct partial_die_info *,
1187 struct dwarf2_cu *);
63d06c5c 1188
72bf9492
DJ
1189static void add_partial_namespace (struct partial_die_info *pdi,
1190 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1191 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1192
5d7cb8df 1193static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1194 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1195 struct dwarf2_cu *cu);
1196
72bf9492
DJ
1197static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1198 struct dwarf2_cu *cu);
91c24f0a 1199
bc30ff58
JB
1200static void add_partial_subprogram (struct partial_die_info *pdi,
1201 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1202 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1203
d521ce57 1204static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1205
dee91e82 1206static struct partial_die_info *load_partial_dies
d521ce57 1207 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1208
fb816e8b
TV
1209/* A pair of partial_die_info and compilation unit. */
1210struct cu_partial_die_info
1211{
1212 /* The compilation unit of the partial_die_info. */
1213 struct dwarf2_cu *cu;
1214 /* A partial_die_info. */
1215 struct partial_die_info *pdi;
122cf0f2
AB
1216
1217 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1218 : cu (cu),
1219 pdi (pdi)
405feb71 1220 { /* Nothing. */ }
122cf0f2
AB
1221
1222private:
1223 cu_partial_die_info () = delete;
fb816e8b
TV
1224};
1225
122cf0f2
AB
1226static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1227 struct dwarf2_cu *);
72bf9492 1228
d521ce57
TT
1229static const gdb_byte *read_attribute (const struct die_reader_specs *,
1230 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1231 const gdb_byte *, bool *need_reprocess);
1232
1233static void read_attribute_reprocess (const struct die_reader_specs *reader,
1234 struct attribute *attr);
1235
1236static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1237
c764a876 1238static LONGEST read_checked_initial_length_and_offset
d521ce57 1239 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1240 unsigned int *, unsigned int *);
613e1657 1241
ed2dc618
SM
1242static sect_offset read_abbrev_offset
1243 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1244 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1245
ed2dc618
SM
1246static const char *read_indirect_string
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1248 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1249
ed2dc618
SM
1250static const char *read_indirect_line_string
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1252 const struct comp_unit_head *, unsigned int *);
36586728 1253
ed2dc618
SM
1254static const char *read_indirect_string_at_offset
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1256 LONGEST str_offset);
927aa2e7 1257
ed2dc618
SM
1258static const char *read_indirect_string_from_dwz
1259 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1260
d521ce57
TT
1261static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1262 const gdb_byte *,
3019eac3
DE
1263 unsigned int *);
1264
18a8505e
AT
1265static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1266 ULONGEST str_index);
1267
1268static const char *read_stub_str_index (struct dwarf2_cu *cu,
1269 ULONGEST str_index);
3019eac3 1270
e142c38c 1271static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1272
e142c38c
DJ
1273static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1274 struct dwarf2_cu *);
c906108c 1275
348e048f 1276static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1277 unsigned int);
348e048f 1278
7d45c7c3
KB
1279static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1280 struct dwarf2_cu *cu);
1281
a084a2a6
AT
1282static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1283
05cf31d1
JB
1284static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1285 struct dwarf2_cu *cu);
1286
e142c38c 1287static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1288
e142c38c 1289static struct die_info *die_specification (struct die_info *die,
f2f0e013 1290 struct dwarf2_cu **);
63d06c5c 1291
9c541725 1292static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1293 struct dwarf2_cu *cu);
debd256d 1294
f3f5162e 1295static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1296 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1297 CORE_ADDR, int decode_mapping);
c906108c 1298
804d2729
TT
1299static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1300 const char *);
c906108c 1301
a14ed312 1302static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1303 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1304
ff39bb5e 1305static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1306 struct dwarf2_cu *);
c906108c 1307
ff39bb5e 1308static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1309 struct type *type,
1310 const char *name,
1311 struct obstack *obstack,
12df843f 1312 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1313 const gdb_byte **bytes,
98bfdba5 1314 struct dwarf2_locexpr_baton **baton);
2df3850c 1315
e7c27a73 1316static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1317
b4ba55a1
JB
1318static int need_gnat_info (struct dwarf2_cu *);
1319
3e43a32a
MS
1320static struct type *die_descriptive_type (struct die_info *,
1321 struct dwarf2_cu *);
b4ba55a1
JB
1322
1323static void set_descriptive_type (struct type *, struct die_info *,
1324 struct dwarf2_cu *);
1325
e7c27a73
DJ
1326static struct type *die_containing_type (struct die_info *,
1327 struct dwarf2_cu *);
c906108c 1328
ff39bb5e 1329static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1330 struct dwarf2_cu *);
c906108c 1331
f792889a 1332static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1333
673bfd45
DE
1334static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1335
0d5cff50 1336static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1337
6e70227d 1338static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1339 const char *suffix, int physname,
1340 struct dwarf2_cu *cu);
63d06c5c 1341
e7c27a73 1342static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1343
348e048f
DE
1344static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1345
e7c27a73 1346static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1347
e7c27a73 1348static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1349
96408a79
SA
1350static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1351
71a3c369
TT
1352static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1353
ff013f42 1354static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1355 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1356
3a2b436a 1357/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1358 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1359enum pc_bounds_kind
1360{
e385593e 1361 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1362 PC_BOUNDS_NOT_PRESENT,
1363
e385593e
JK
1364 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1365 were present but they do not form a valid range of PC addresses. */
1366 PC_BOUNDS_INVALID,
1367
3a2b436a
JK
1368 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1369 PC_BOUNDS_RANGES,
1370
1371 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1372 PC_BOUNDS_HIGH_LOW,
1373};
1374
1375static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1376 CORE_ADDR *, CORE_ADDR *,
1377 struct dwarf2_cu *,
891813be 1378 dwarf2_psymtab *);
c906108c 1379
fae299cd
DC
1380static void get_scope_pc_bounds (struct die_info *,
1381 CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *);
1383
801e3a5b
JB
1384static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1385 CORE_ADDR, struct dwarf2_cu *);
1386
a14ed312 1387static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1388 struct dwarf2_cu *);
c906108c 1389
a14ed312 1390static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1391 struct type *, struct dwarf2_cu *);
c906108c 1392
a14ed312 1393static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1394 struct die_info *, struct type *,
e7c27a73 1395 struct dwarf2_cu *);
c906108c 1396
a14ed312 1397static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1398 struct type *,
1399 struct dwarf2_cu *);
c906108c 1400
134d01f1 1401static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1402
e7c27a73 1403static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1404
e7c27a73 1405static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1406
5d7cb8df
JK
1407static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1408
804d2729 1409static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1410
27aa8d6a
SW
1411static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1412
74921315
KS
1413static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1414
f55ee35c
JK
1415static struct type *read_module_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
38d518c9 1418static const char *namespace_name (struct die_info *die,
e142c38c 1419 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1420
134d01f1 1421static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1422
e7c27a73 1423static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1424
6e70227d 1425static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1426 struct dwarf2_cu *);
1427
bf6af496 1428static struct die_info *read_die_and_siblings_1
d521ce57 1429 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1430 struct die_info *);
639d11d3 1431
dee91e82 1432static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1433 const gdb_byte *info_ptr,
1434 const gdb_byte **new_info_ptr,
639d11d3
DC
1435 struct die_info *parent);
1436
d521ce57
TT
1437static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1438 struct die_info **, const gdb_byte *,
3e225074 1439 int);
3019eac3 1440
d521ce57 1441static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1442 struct die_info **, const gdb_byte *);
93311388 1443
e7c27a73 1444static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1445
15d034d0
TT
1446static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1447 struct obstack *);
71c25dea 1448
15d034d0 1449static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1450
15d034d0 1451static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1452 struct die_info *die,
1453 struct dwarf2_cu *cu);
1454
ca69b9e6
DE
1455static const char *dwarf2_physname (const char *name, struct die_info *die,
1456 struct dwarf2_cu *cu);
1457
e142c38c 1458static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1459 struct dwarf2_cu **);
9219021c 1460
f39c6ffd 1461static const char *dwarf_tag_name (unsigned int);
c906108c 1462
f39c6ffd 1463static const char *dwarf_attr_name (unsigned int);
c906108c 1464
f39c6ffd 1465static const char *dwarf_form_name (unsigned int);
c906108c 1466
a121b7c1 1467static const char *dwarf_bool_name (unsigned int);
c906108c 1468
f39c6ffd 1469static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1470
f9aca02d 1471static struct die_info *sibling_die (struct die_info *);
c906108c 1472
d97bc12b
DE
1473static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1474
1475static void dump_die_for_error (struct die_info *);
1476
1477static void dump_die_1 (struct ui_file *, int level, int max_level,
1478 struct die_info *);
c906108c 1479
d97bc12b 1480/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1481
51545339 1482static void store_in_ref_table (struct die_info *,
10b3939b 1483 struct dwarf2_cu *);
c906108c 1484
ff39bb5e 1485static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1486
ff39bb5e 1487static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1488
348e048f 1489static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1490 const struct attribute *,
348e048f
DE
1491 struct dwarf2_cu **);
1492
10b3939b 1493static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1494 const struct attribute *,
f2f0e013 1495 struct dwarf2_cu **);
c906108c 1496
348e048f 1497static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1498 const struct attribute *,
348e048f
DE
1499 struct dwarf2_cu **);
1500
ac9ec31b
DE
1501static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1505 const struct attribute *,
ac9ec31b
DE
1506 struct dwarf2_cu *);
1507
e5fe5e75 1508static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1509
52dc124a 1510static void read_signatured_type (struct signatured_type *);
348e048f 1511
63e43d3a
PMR
1512static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1514 struct dynamic_prop *prop, struct type *type);
63e43d3a 1515
c906108c
SS
1516/* memory allocation interface */
1517
7b5a2f43 1518static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1519
b60c80d6 1520static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1521
43f3e411 1522static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1523
8cf6f0b1
TT
1524static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
ff39bb5e 1526 const struct attribute *attr);
8cf6f0b1 1527
ff39bb5e 1528static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1529 struct symbol *sym,
f1e6e072
TT
1530 struct dwarf2_cu *cu,
1531 int is_block);
4c2df51b 1532
d521ce57
TT
1533static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
4bb7a0a7 1536
72bf9492
DJ
1537static hashval_t partial_die_hash (const void *item);
1538
1539static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
ae038cb0 1541static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1544
9816fde3 1545static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
93311388 1548
ed2dc618 1549static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1550
dee91e82 1551static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1552
f792889a
DJ
1553static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1c379e20 1555
ed2dc618 1556static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1557
ed2dc618 1558static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1559
58f0c718 1560static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1561 enum language);
10b3939b 1562
95554aad
TT
1563static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
10b3939b 1565
f4dc4d17
DE
1566static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
10b3939b
DJ
1569static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
ae038cb0
DJ
1572static void dwarf2_mark (struct dwarf2_cu *);
1573
1574static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
b64f50a1 1576static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1577 struct dwarf2_per_cu_data *);
673bfd45 1578
f792889a 1579static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1580
95554aad
TT
1581static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
ed2dc618 1584static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1585
b303c6f6
AB
1586/* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591class dwarf2_queue_guard
1592{
1593public:
39856def
TT
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
b303c6f6
AB
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
39856def
TT
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->queue, empty);
1606 }
b303c6f6 1607
39856def 1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1609
39856def
TT
1610private:
1611 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1612};
1613
39856def
TT
1614dwarf2_queue_item::~dwarf2_queue_item ()
1615{
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624}
1625
d721ba37
PA
1626/* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629struct file_and_directory
1630{
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643};
1644
1645static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
9291a0cd 1647
298e9637 1648static htab_up allocate_signatured_type_table ();
1fd400ff 1649
298e9637 1650static htab_up allocate_dwo_unit_table ();
3019eac3 1651
57d63ce2 1652static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1655 ULONGEST signature, int is_debug_types);
a2ce51a0 1656
ed2dc618
SM
1657static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1659
3019eac3 1660static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1662
1663static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1664 (struct signatured_type *, const char *, const char *);
3019eac3 1665
89e63ee4
DE
1666static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
263db9a1
TT
1668/* A unique pointer to a dwo_file. */
1669
51ac9db5 1670typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1671
ed2dc618 1672static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1673
1b80a9fa 1674static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1675
1676static void free_line_header_voidp (void *arg);
4390d890
DE
1677\f
1678/* Various complaints about symbol reading that don't abort the process. */
1679
1680static void
1681dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1682{
b98664d3 1683 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1684}
1685
1686static void
1687dwarf2_debug_line_missing_file_complaint (void)
1688{
b98664d3 1689 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1690}
1691
1692static void
1693dwarf2_debug_line_missing_end_sequence_complaint (void)
1694{
b98664d3 1695 complaint (_(".debug_line section has line "
4390d890
DE
1696 "program sequence without an end"));
1697}
1698
1699static void
1700dwarf2_complex_location_expr_complaint (void)
1701{
b98664d3 1702 complaint (_("location expression too complex"));
4390d890
DE
1703}
1704
1705static void
1706dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1707 int arg3)
1708{
b98664d3 1709 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1710 arg1, arg2, arg3);
1711}
1712
1713static void
1714dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1715{
b98664d3 1716 complaint (_("debug info runs off end of %s section"
4390d890 1717 " [in module %s]"),
96b79293
TT
1718 section->get_name (),
1719 section->get_file_name ());
4390d890 1720}
1b80a9fa 1721
4390d890
DE
1722static void
1723dwarf2_macro_malformed_definition_complaint (const char *arg1)
1724{
b98664d3 1725 complaint (_("macro debug info contains a "
4390d890
DE
1726 "malformed macro definition:\n`%s'"),
1727 arg1);
1728}
1729
1730static void
1731dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1732{
b98664d3 1733 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1734 arg1, arg2);
1735}
527f3840
JK
1736
1737/* Hash function for line_header_hash. */
1738
1739static hashval_t
1740line_header_hash (const struct line_header *ofs)
1741{
9c541725 1742 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1743}
1744
1745/* Hash function for htab_create_alloc_ex for line_header_hash. */
1746
1747static hashval_t
1748line_header_hash_voidp (const void *item)
1749{
9a3c8263 1750 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1751
1752 return line_header_hash (ofs);
1753}
1754
1755/* Equality function for line_header_hash. */
1756
1757static int
1758line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1759{
9a3c8263
SM
1760 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1761 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1762
9c541725 1763 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1764 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1765}
1766
4390d890 1767\f
9291a0cd 1768
330cdd98
PA
1769/* See declaration. */
1770
1771dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1772 const dwarf2_debug_sections *names,
1773 bool can_copy_)
1774 : objfile (objfile_),
1775 can_copy (can_copy_)
330cdd98
PA
1776{
1777 if (names == NULL)
1778 names = &dwarf2_elf_names;
1779
1780 bfd *obfd = objfile->obfd;
1781
1782 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1783 locate_sections (obfd, sec, *names);
1784}
1785
1786dwarf2_per_objfile::~dwarf2_per_objfile ()
1787{
1788 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1789 free_cached_comp_units ();
1790
b76e467d 1791 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1792 per_cu->imported_symtabs_free ();
fc8e7e75 1793
b2bdb8cf 1794 for (signatured_type *sig_type : all_type_units)
ae640021 1795 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1796
330cdd98
PA
1797 /* Everything else should be on the objfile obstack. */
1798}
1799
1800/* See declaration. */
1801
1802void
1803dwarf2_per_objfile::free_cached_comp_units ()
1804{
1805 dwarf2_per_cu_data *per_cu = read_in_chain;
1806 dwarf2_per_cu_data **last_chain = &read_in_chain;
1807 while (per_cu != NULL)
1808 {
1809 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1810
fcd3b13d 1811 delete per_cu->cu;
330cdd98
PA
1812 *last_chain = next_cu;
1813 per_cu = next_cu;
1814 }
1815}
1816
11ed8cad
TT
1817/* A helper class that calls free_cached_comp_units on
1818 destruction. */
1819
1820class free_cached_comp_units
1821{
1822public:
1823
1824 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1825 : m_per_objfile (per_objfile)
1826 {
1827 }
1828
1829 ~free_cached_comp_units ()
1830 {
1831 m_per_objfile->free_cached_comp_units ();
1832 }
1833
1834 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1835
1836private:
1837
1838 dwarf2_per_objfile *m_per_objfile;
1839};
1840
c906108c 1841/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1842 information and return true if we have enough to do something.
1843 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1844 ELF names are used. CAN_COPY is true for formats where symbol
1845 interposition is possible and so symbol values must follow copy
1846 relocation rules. */
c906108c
SS
1847
1848int
251d32d9 1849dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1850 const struct dwarf2_debug_sections *names,
1851 bool can_copy)
c906108c 1852{
97cbe998
SDJ
1853 if (objfile->flags & OBJF_READNEVER)
1854 return 0;
1855
ed2dc618
SM
1856 struct dwarf2_per_objfile *dwarf2_per_objfile
1857 = get_dwarf2_per_objfile (objfile);
1858
1859 if (dwarf2_per_objfile == NULL)
5bfd760d 1860 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1861 names,
1862 can_copy);
5bfd760d 1863
73869dc2 1864 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1865 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1866 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1867 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1868}
1869
251d32d9
TG
1870/* When loading sections, we look either for uncompressed section or for
1871 compressed section names. */
233a11ab
CS
1872
1873static int
251d32d9
TG
1874section_is_p (const char *section_name,
1875 const struct dwarf2_section_names *names)
233a11ab 1876{
251d32d9
TG
1877 if (names->normal != NULL
1878 && strcmp (section_name, names->normal) == 0)
1879 return 1;
1880 if (names->compressed != NULL
1881 && strcmp (section_name, names->compressed) == 0)
1882 return 1;
1883 return 0;
233a11ab
CS
1884}
1885
330cdd98 1886/* See declaration. */
c906108c 1887
330cdd98
PA
1888void
1889dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1890 const dwarf2_debug_sections &names)
c906108c 1891{
fd361982 1892 flagword aflag = bfd_section_flags (sectp);
251d32d9 1893
dc7650b8
JK
1894 if ((aflag & SEC_HAS_CONTENTS) == 0)
1895 {
1896 }
950b7495
KS
1897 else if (elf_section_data (sectp)->this_hdr.sh_size
1898 > bfd_get_file_size (abfd))
1899 {
1900 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1901 warning (_("Discarding section %s which has a section size (%s"
1902 ") larger than the file size [in module %s]"),
1903 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1904 bfd_get_filename (abfd));
1905 }
330cdd98 1906 else if (section_is_p (sectp->name, &names.info))
c906108c 1907 {
330cdd98 1908 this->info.s.section = sectp;
fd361982 1909 this->info.size = bfd_section_size (sectp);
c906108c 1910 }
330cdd98 1911 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1912 {
330cdd98 1913 this->abbrev.s.section = sectp;
fd361982 1914 this->abbrev.size = bfd_section_size (sectp);
c906108c 1915 }
330cdd98 1916 else if (section_is_p (sectp->name, &names.line))
c906108c 1917 {
330cdd98 1918 this->line.s.section = sectp;
fd361982 1919 this->line.size = bfd_section_size (sectp);
c906108c 1920 }
330cdd98 1921 else if (section_is_p (sectp->name, &names.loc))
c906108c 1922 {
330cdd98 1923 this->loc.s.section = sectp;
fd361982 1924 this->loc.size = bfd_section_size (sectp);
c906108c 1925 }
330cdd98 1926 else if (section_is_p (sectp->name, &names.loclists))
43988095 1927 {
330cdd98 1928 this->loclists.s.section = sectp;
fd361982 1929 this->loclists.size = bfd_section_size (sectp);
43988095 1930 }
330cdd98 1931 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1932 {
330cdd98 1933 this->macinfo.s.section = sectp;
fd361982 1934 this->macinfo.size = bfd_section_size (sectp);
c906108c 1935 }
330cdd98 1936 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1937 {
330cdd98 1938 this->macro.s.section = sectp;
fd361982 1939 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1940 }
330cdd98 1941 else if (section_is_p (sectp->name, &names.str))
c906108c 1942 {
330cdd98 1943 this->str.s.section = sectp;
fd361982 1944 this->str.size = bfd_section_size (sectp);
c906108c 1945 }
18a8505e
AT
1946 else if (section_is_p (sectp->name, &names.str_offsets))
1947 {
1948 this->str_offsets.s.section = sectp;
1949 this->str_offsets.size = bfd_section_size (sectp);
1950 }
330cdd98 1951 else if (section_is_p (sectp->name, &names.line_str))
43988095 1952 {
330cdd98 1953 this->line_str.s.section = sectp;
fd361982 1954 this->line_str.size = bfd_section_size (sectp);
43988095 1955 }
330cdd98 1956 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1957 {
330cdd98 1958 this->addr.s.section = sectp;
fd361982 1959 this->addr.size = bfd_section_size (sectp);
3019eac3 1960 }
330cdd98 1961 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1962 {
330cdd98 1963 this->frame.s.section = sectp;
fd361982 1964 this->frame.size = bfd_section_size (sectp);
b6af0555 1965 }
330cdd98 1966 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1967 {
330cdd98 1968 this->eh_frame.s.section = sectp;
fd361982 1969 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1970 }
330cdd98 1971 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1972 {
330cdd98 1973 this->ranges.s.section = sectp;
fd361982 1974 this->ranges.size = bfd_section_size (sectp);
af34e669 1975 }
330cdd98 1976 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1977 {
330cdd98 1978 this->rnglists.s.section = sectp;
fd361982 1979 this->rnglists.size = bfd_section_size (sectp);
43988095 1980 }
330cdd98 1981 else if (section_is_p (sectp->name, &names.types))
348e048f 1982 {
8b70b953
TT
1983 struct dwarf2_section_info type_section;
1984
1985 memset (&type_section, 0, sizeof (type_section));
049412e3 1986 type_section.s.section = sectp;
fd361982 1987 type_section.size = bfd_section_size (sectp);
8b70b953 1988
fd5866f6 1989 this->types.push_back (type_section);
348e048f 1990 }
330cdd98 1991 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1992 {
330cdd98 1993 this->gdb_index.s.section = sectp;
fd361982 1994 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1995 }
927aa2e7
JK
1996 else if (section_is_p (sectp->name, &names.debug_names))
1997 {
1998 this->debug_names.s.section = sectp;
fd361982 1999 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2000 }
2001 else if (section_is_p (sectp->name, &names.debug_aranges))
2002 {
2003 this->debug_aranges.s.section = sectp;
fd361982 2004 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2005 }
dce234bc 2006
fd361982
AM
2007 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2008 && bfd_section_vma (sectp) == 0)
330cdd98 2009 this->has_section_at_zero = true;
c906108c
SS
2010}
2011
dce234bc 2012/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2013 SECTION_NAME. */
af34e669 2014
dce234bc 2015void
3017a003
TG
2016dwarf2_get_section_info (struct objfile *objfile,
2017 enum dwarf2_section_enum sect,
d521ce57 2018 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2019 bfd_size_type *sizep)
2020{
5bfd760d 2021 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2022 struct dwarf2_section_info *info;
a3b2a86b
TT
2023
2024 /* We may see an objfile without any DWARF, in which case we just
2025 return nothing. */
2026 if (data == NULL)
2027 {
2028 *sectp = NULL;
2029 *bufp = NULL;
2030 *sizep = 0;
2031 return;
2032 }
3017a003
TG
2033 switch (sect)
2034 {
2035 case DWARF2_DEBUG_FRAME:
2036 info = &data->frame;
2037 break;
2038 case DWARF2_EH_FRAME:
2039 info = &data->eh_frame;
2040 break;
2041 default:
2042 gdb_assert_not_reached ("unexpected section");
2043 }
dce234bc 2044
96b79293 2045 info->read (objfile);
dce234bc 2046
96b79293 2047 *sectp = info->get_bfd_section ();
dce234bc
PP
2048 *bufp = info->buffer;
2049 *sizep = info->size;
2050}
2051
36586728
TT
2052/* A helper function to find the sections for a .dwz file. */
2053
2054static void
2055locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2056{
9a3c8263 2057 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2058
2059 /* Note that we only support the standard ELF names, because .dwz
2060 is ELF-only (at the time of writing). */
2061 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2062 {
049412e3 2063 dwz_file->abbrev.s.section = sectp;
fd361982 2064 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2065 }
2066 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2067 {
049412e3 2068 dwz_file->info.s.section = sectp;
fd361982 2069 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2070 }
2071 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2072 {
049412e3 2073 dwz_file->str.s.section = sectp;
fd361982 2074 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2075 }
2076 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2077 {
049412e3 2078 dwz_file->line.s.section = sectp;
fd361982 2079 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2080 }
2081 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2082 {
049412e3 2083 dwz_file->macro.s.section = sectp;
fd361982 2084 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2085 }
2ec9a5e0
TT
2086 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2087 {
049412e3 2088 dwz_file->gdb_index.s.section = sectp;
fd361982 2089 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2090 }
927aa2e7
JK
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2092 {
2093 dwz_file->debug_names.s.section = sectp;
fd361982 2094 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2095 }
36586728
TT
2096}
2097
c4973306 2098/* See dwarf2read.h. */
36586728 2099
c4973306 2100struct dwz_file *
ed2dc618 2101dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2102{
36586728 2103 const char *filename;
acd13123 2104 bfd_size_type buildid_len_arg;
dc294be5
TT
2105 size_t buildid_len;
2106 bfd_byte *buildid;
36586728
TT
2107
2108 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2109 return dwarf2_per_objfile->dwz_file.get ();
36586728 2110
4db1a1dc 2111 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2112 gdb::unique_xmalloc_ptr<char> data
2113 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2114 &buildid_len_arg, &buildid));
4db1a1dc
TT
2115 if (data == NULL)
2116 {
2117 if (bfd_get_error () == bfd_error_no_error)
2118 return NULL;
2119 error (_("could not read '.gnu_debugaltlink' section: %s"),
2120 bfd_errmsg (bfd_get_error ()));
2121 }
791afaa2
TT
2122
2123 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2124
acd13123
TT
2125 buildid_len = (size_t) buildid_len_arg;
2126
791afaa2 2127 filename = data.get ();
d721ba37
PA
2128
2129 std::string abs_storage;
36586728
TT
2130 if (!IS_ABSOLUTE_PATH (filename))
2131 {
14278e1f
TT
2132 gdb::unique_xmalloc_ptr<char> abs
2133 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2134
14278e1f 2135 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2136 filename = abs_storage.c_str ();
36586728
TT
2137 }
2138
dc294be5
TT
2139 /* First try the file name given in the section. If that doesn't
2140 work, try to use the build-id instead. */
192b62ce 2141 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2142 if (dwz_bfd != NULL)
36586728 2143 {
192b62ce 2144 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2145 dwz_bfd.reset (nullptr);
36586728
TT
2146 }
2147
dc294be5
TT
2148 if (dwz_bfd == NULL)
2149 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2150
0d79cdc4
AM
2151 if (dwz_bfd == nullptr)
2152 {
2153 gdb::unique_xmalloc_ptr<char> alt_filename;
2154 const char *origname = dwarf2_per_objfile->objfile->original_name;
2155
2156 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2157 buildid_len,
2158 origname,
2159 &alt_filename));
2160
2161 if (fd.get () >= 0)
2162 {
2163 /* File successfully retrieved from server. */
2164 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2165
2166 if (dwz_bfd == nullptr)
2167 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2168 alt_filename.get ());
2169 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2170 dwz_bfd.reset (nullptr);
2171 }
2172 }
2173
dc294be5
TT
2174 if (dwz_bfd == NULL)
2175 error (_("could not find '.gnu_debugaltlink' file for %s"),
2176 objfile_name (dwarf2_per_objfile->objfile));
2177
7ff8cb8c
TT
2178 std::unique_ptr<struct dwz_file> result
2179 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2180
7ff8cb8c
TT
2181 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2182 result.get ());
36586728 2183
7ff8cb8c
TT
2184 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2185 result->dwz_bfd.get ());
2186 dwarf2_per_objfile->dwz_file = std::move (result);
2187 return dwarf2_per_objfile->dwz_file.get ();
36586728 2188}
9291a0cd 2189\f
7b9f3c50
DE
2190/* DWARF quick_symbols_functions support. */
2191
2192/* TUs can share .debug_line entries, and there can be a lot more TUs than
2193 unique line tables, so we maintain a separate table of all .debug_line
2194 derived entries to support the sharing.
2195 All the quick functions need is the list of file names. We discard the
2196 line_header when we're done and don't need to record it here. */
2197struct quick_file_names
2198{
094b34ac
DE
2199 /* The data used to construct the hash key. */
2200 struct stmt_list_hash hash;
7b9f3c50
DE
2201
2202 /* The number of entries in file_names, real_names. */
2203 unsigned int num_file_names;
2204
2205 /* The file names from the line table, after being run through
2206 file_full_name. */
2207 const char **file_names;
2208
2209 /* The file names from the line table after being run through
2210 gdb_realpath. These are computed lazily. */
2211 const char **real_names;
2212};
2213
2214/* When using the index (and thus not using psymtabs), each CU has an
2215 object of this type. This is used to hold information needed by
2216 the various "quick" methods. */
2217struct dwarf2_per_cu_quick_data
2218{
2219 /* The file table. This can be NULL if there was no file table
2220 or it's currently not read in.
2221 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2222 struct quick_file_names *file_names;
2223
2224 /* The corresponding symbol table. This is NULL if symbols for this
2225 CU have not yet been read. */
43f3e411 2226 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2227
2228 /* A temporary mark bit used when iterating over all CUs in
2229 expand_symtabs_matching. */
2230 unsigned int mark : 1;
2231
2232 /* True if we've tried to read the file table and found there isn't one.
2233 There will be no point in trying to read it again next time. */
2234 unsigned int no_file_data : 1;
2235};
2236
094b34ac
DE
2237/* Utility hash function for a stmt_list_hash. */
2238
2239static hashval_t
2240hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2241{
2242 hashval_t v = 0;
2243
2244 if (stmt_list_hash->dwo_unit != NULL)
2245 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2246 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2247 return v;
2248}
2249
2250/* Utility equality function for a stmt_list_hash. */
2251
2252static int
2253eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2254 const struct stmt_list_hash *rhs)
2255{
2256 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2257 return 0;
2258 if (lhs->dwo_unit != NULL
2259 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2260 return 0;
2261
9c541725 2262 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2263}
2264
7b9f3c50
DE
2265/* Hash function for a quick_file_names. */
2266
2267static hashval_t
2268hash_file_name_entry (const void *e)
2269{
9a3c8263
SM
2270 const struct quick_file_names *file_data
2271 = (const struct quick_file_names *) e;
7b9f3c50 2272
094b34ac 2273 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2274}
2275
2276/* Equality function for a quick_file_names. */
2277
2278static int
2279eq_file_name_entry (const void *a, const void *b)
2280{
9a3c8263
SM
2281 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2282 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2283
094b34ac 2284 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2285}
2286
2287/* Delete function for a quick_file_names. */
2288
2289static void
2290delete_file_name_entry (void *e)
2291{
9a3c8263 2292 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2293 int i;
2294
2295 for (i = 0; i < file_data->num_file_names; ++i)
2296 {
2297 xfree ((void*) file_data->file_names[i]);
2298 if (file_data->real_names)
2299 xfree ((void*) file_data->real_names[i]);
2300 }
2301
2302 /* The space for the struct itself lives on objfile_obstack,
2303 so we don't free it here. */
2304}
2305
2306/* Create a quick_file_names hash table. */
2307
5895093f 2308static htab_up
7b9f3c50
DE
2309create_quick_file_names_table (unsigned int nr_initial_entries)
2310{
5895093f
TT
2311 return htab_up (htab_create_alloc (nr_initial_entries,
2312 hash_file_name_entry, eq_file_name_entry,
2313 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2314}
9291a0cd 2315
918dd910
JK
2316/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2317 have to be created afterwards. You should call age_cached_comp_units after
2318 processing PER_CU->CU. dw2_setup must have been already called. */
2319
2320static void
58f0c718 2321load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2322{
3019eac3 2323 if (per_cu->is_debug_types)
e5fe5e75 2324 load_full_type_unit (per_cu);
918dd910 2325 else
58f0c718 2326 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2327
cc12ce38
DE
2328 if (per_cu->cu == NULL)
2329 return; /* Dummy CU. */
2dc860c0
DE
2330
2331 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2332}
2333
a0f42c21 2334/* Read in the symbols for PER_CU. */
2fdf6df6 2335
9291a0cd 2336static void
58f0c718 2337dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2338{
ed2dc618 2339 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2340
f4dc4d17
DE
2341 /* Skip type_unit_groups, reading the type units they contain
2342 is handled elsewhere. */
197400e8 2343 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2344 return;
2345
b303c6f6
AB
2346 /* The destructor of dwarf2_queue_guard frees any entries left on
2347 the queue. After this point we're guaranteed to leave this function
2348 with the dwarf queue empty. */
39856def 2349 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2350
95554aad 2351 if (dwarf2_per_objfile->using_index
43f3e411 2352 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2353 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2354 {
2355 queue_comp_unit (per_cu, language_minimal);
58f0c718 2356 load_cu (per_cu, skip_partial);
89e63ee4
DE
2357
2358 /* If we just loaded a CU from a DWO, and we're working with an index
2359 that may badly handle TUs, load all the TUs in that DWO as well.
2360 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2361 if (!per_cu->is_debug_types
cc12ce38 2362 && per_cu->cu != NULL
89e63ee4
DE
2363 && per_cu->cu->dwo_unit != NULL
2364 && dwarf2_per_objfile->index_table != NULL
2365 && dwarf2_per_objfile->index_table->version <= 7
2366 /* DWP files aren't supported yet. */
ed2dc618 2367 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2368 queue_and_load_all_dwo_tus (per_cu);
95554aad 2369 }
9291a0cd 2370
ed2dc618 2371 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2372
2373 /* Age the cache, releasing compilation units that have not
2374 been used recently. */
ed2dc618 2375 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2376}
2377
2378/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2379 the objfile from which this CU came. Returns the resulting symbol
2380 table. */
2fdf6df6 2381
43f3e411 2382static struct compunit_symtab *
58f0c718 2383dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2384{
ed2dc618
SM
2385 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2386
95554aad 2387 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2388 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2389 {
11ed8cad 2390 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2391 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2392 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2393 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2394 }
f194fefb 2395
43f3e411 2396 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2397}
2398
ff4c9fec 2399/* See declaration. */
f4dc4d17 2400
ff4c9fec
SM
2401dwarf2_per_cu_data *
2402dwarf2_per_objfile::get_cutu (int index)
2403{
b76e467d 2404 if (index >= this->all_comp_units.size ())
ff4c9fec 2405 {
b76e467d 2406 index -= this->all_comp_units.size ();
b2bdb8cf 2407 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2408 return &this->all_type_units[index]->per_cu;
2409 }
f4dc4d17 2410
ff4c9fec
SM
2411 return this->all_comp_units[index];
2412}
f4dc4d17 2413
ff4c9fec 2414/* See declaration. */
2fdf6df6 2415
ff4c9fec
SM
2416dwarf2_per_cu_data *
2417dwarf2_per_objfile::get_cu (int index)
1fd400ff 2418{
b76e467d 2419 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2420
ff4c9fec 2421 return this->all_comp_units[index];
f4dc4d17
DE
2422}
2423
ff4c9fec 2424/* See declaration. */
f4dc4d17 2425
ff4c9fec
SM
2426signatured_type *
2427dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2428{
b2bdb8cf 2429 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2430
ff4c9fec 2431 return this->all_type_units[index];
1fd400ff
TT
2432}
2433
4b514bc8
JK
2434/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2435 objfile_obstack, and constructed with the specified field
2436 values. */
2437
2438static dwarf2_per_cu_data *
ed2dc618 2439create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2440 struct dwarf2_section_info *section,
2441 int is_dwz,
2442 sect_offset sect_off, ULONGEST length)
2443{
ed2dc618 2444 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2445 dwarf2_per_cu_data *the_cu
2446 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2447 struct dwarf2_per_cu_data);
2448 the_cu->sect_off = sect_off;
2449 the_cu->length = length;
e3b94546 2450 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2451 the_cu->section = section;
2452 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2453 struct dwarf2_per_cu_quick_data);
2454 the_cu->is_dwz = is_dwz;
2455 return the_cu;
2456}
2457
2ec9a5e0
TT
2458/* A helper for create_cus_from_index that handles a given list of
2459 CUs. */
2fdf6df6 2460
74a0d9f6 2461static void
12359b5e 2462create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2463 const gdb_byte *cu_list, offset_type n_elements,
2464 struct dwarf2_section_info *section,
b76e467d 2465 int is_dwz)
9291a0cd 2466{
12359b5e 2467 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2468 {
74a0d9f6 2469 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2470
2471 sect_offset sect_off
2472 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2473 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2474 cu_list += 2 * 8;
2475
b76e467d 2476 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2477 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2478 sect_off, length);
b76e467d 2479 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2480 }
9291a0cd
TT
2481}
2482
2ec9a5e0 2483/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2484 the CU objects for this objfile. */
2ec9a5e0 2485
74a0d9f6 2486static void
12359b5e 2487create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2488 const gdb_byte *cu_list, offset_type cu_list_elements,
2489 const gdb_byte *dwz_list, offset_type dwz_elements)
2490{
b76e467d
SM
2491 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2492 dwarf2_per_objfile->all_comp_units.reserve
2493 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2494
12359b5e 2495 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2496 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2497
2498 if (dwz_elements == 0)
74a0d9f6 2499 return;
2ec9a5e0 2500
12359b5e
SM
2501 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2502 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2503 &dwz->info, 1);
2ec9a5e0
TT
2504}
2505
1fd400ff 2506/* Create the signatured type hash table from the index. */
673bfd45 2507
74a0d9f6 2508static void
12359b5e
SM
2509create_signatured_type_table_from_index
2510 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2511 struct dwarf2_section_info *section,
2512 const gdb_byte *bytes,
2513 offset_type elements)
1fd400ff 2514{
12359b5e 2515 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2516
b2bdb8cf
SM
2517 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2518 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2519
298e9637 2520 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2521
12359b5e 2522 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2523 {
52dc124a 2524 struct signatured_type *sig_type;
9c541725 2525 ULONGEST signature;
1fd400ff 2526 void **slot;
9c541725 2527 cu_offset type_offset_in_tu;
1fd400ff 2528
74a0d9f6 2529 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2530 sect_offset sect_off
2531 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2532 type_offset_in_tu
2533 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2534 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2535 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2536 bytes += 3 * 8;
2537
52dc124a 2538 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2539 struct signatured_type);
52dc124a 2540 sig_type->signature = signature;
9c541725 2541 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2542 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2543 sig_type->per_cu.section = section;
9c541725 2544 sig_type->per_cu.sect_off = sect_off;
e3b94546 2545 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2546 sig_type->per_cu.v.quick
1fd400ff
TT
2547 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2548 struct dwarf2_per_cu_quick_data);
2549
b0b6a987 2550 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2551 *slot = sig_type;
1fd400ff 2552
b2bdb8cf 2553 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2554 }
2555
b0b6a987 2556 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2557}
2558
927aa2e7
JK
2559/* Create the signatured type hash table from .debug_names. */
2560
2561static void
2562create_signatured_type_table_from_debug_names
ed2dc618 2563 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2564 const mapped_debug_names &map,
2565 struct dwarf2_section_info *section,
2566 struct dwarf2_section_info *abbrev_section)
2567{
ed2dc618
SM
2568 struct objfile *objfile = dwarf2_per_objfile->objfile;
2569
96b79293
TT
2570 section->read (objfile);
2571 abbrev_section->read (objfile);
927aa2e7 2572
b2bdb8cf
SM
2573 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2574 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2575
298e9637 2576 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2577
2578 for (uint32_t i = 0; i < map.tu_count; ++i)
2579 {
2580 struct signatured_type *sig_type;
927aa2e7 2581 void **slot;
927aa2e7
JK
2582
2583 sect_offset sect_off
2584 = (sect_offset) (extract_unsigned_integer
2585 (map.tu_table_reordered + i * map.offset_size,
2586 map.offset_size,
2587 map.dwarf5_byte_order));
2588
2589 comp_unit_head cu_header;
ed2dc618
SM
2590 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2591 abbrev_section,
927aa2e7
JK
2592 section->buffer + to_underlying (sect_off),
2593 rcuh_kind::TYPE);
2594
2595 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2596 struct signatured_type);
2597 sig_type->signature = cu_header.signature;
2598 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2599 sig_type->per_cu.is_debug_types = 1;
2600 sig_type->per_cu.section = section;
2601 sig_type->per_cu.sect_off = sect_off;
e3b94546 2602 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2603 sig_type->per_cu.v.quick
2604 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2605 struct dwarf2_per_cu_quick_data);
2606
b0b6a987 2607 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2608 *slot = sig_type;
2609
b2bdb8cf 2610 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2611 }
2612
b0b6a987 2613 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2614}
2615
9291a0cd
TT
2616/* Read the address map data from the mapped index, and use it to
2617 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2618
9291a0cd 2619static void
ed2dc618
SM
2620create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2621 struct mapped_index *index)
9291a0cd 2622{
ed2dc618 2623 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2624 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2625 const gdb_byte *iter, *end;
9291a0cd 2626 struct addrmap *mutable_map;
9291a0cd
TT
2627 CORE_ADDR baseaddr;
2628
8268c778
PA
2629 auto_obstack temp_obstack;
2630
9291a0cd
TT
2631 mutable_map = addrmap_create_mutable (&temp_obstack);
2632
f00a2de2
PA
2633 iter = index->address_table.data ();
2634 end = iter + index->address_table.size ();
9291a0cd 2635
b3b3bada 2636 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2637
2638 while (iter < end)
2639 {
2640 ULONGEST hi, lo, cu_index;
2641 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2642 iter += 8;
2643 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2644 iter += 8;
2645 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2646 iter += 4;
f652bce2 2647
24a55014 2648 if (lo > hi)
f652bce2 2649 {
b98664d3 2650 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2651 hex_string (lo), hex_string (hi));
24a55014 2652 continue;
f652bce2 2653 }
24a55014 2654
b76e467d 2655 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2656 {
b98664d3 2657 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2658 (unsigned) cu_index);
24a55014 2659 continue;
f652bce2 2660 }
24a55014 2661
79748972
TT
2662 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2663 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2664 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2665 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2666 }
2667
d320c2b5 2668 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2669 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2670}
2671
927aa2e7
JK
2672/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2673 populate the objfile's psymtabs_addrmap. */
2674
2675static void
ed2dc618 2676create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2677 struct dwarf2_section_info *section)
2678{
ed2dc618 2679 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2680 bfd *abfd = objfile->obfd;
2681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2682 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2683
2684 auto_obstack temp_obstack;
2685 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2686
2687 std::unordered_map<sect_offset,
2688 dwarf2_per_cu_data *,
2689 gdb::hash_enum<sect_offset>>
2690 debug_info_offset_to_per_cu;
b76e467d 2691 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2692 {
927aa2e7
JK
2693 const auto insertpair
2694 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2695 if (!insertpair.second)
2696 {
2697 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2698 "debug_info_offset %s, ignoring .debug_aranges."),
2699 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2700 return;
2701 }
2702 }
2703
96b79293 2704 section->read (objfile);
927aa2e7
JK
2705
2706 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2707
2708 const gdb_byte *addr = section->buffer;
2709
2710 while (addr < section->buffer + section->size)
2711 {
2712 const gdb_byte *const entry_addr = addr;
2713 unsigned int bytes_read;
2714
2715 const LONGEST entry_length = read_initial_length (abfd, addr,
2716 &bytes_read);
2717 addr += bytes_read;
2718
2719 const gdb_byte *const entry_end = addr + entry_length;
2720 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2721 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2722 if (addr + entry_length > section->buffer + section->size)
2723 {
47e3f474 2724 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2725 "length %s exceeds section length %s, "
2726 "ignoring .debug_aranges."),
47e3f474
TV
2727 objfile_name (objfile),
2728 plongest (entry_addr - section->buffer),
927aa2e7
JK
2729 plongest (bytes_read + entry_length),
2730 pulongest (section->size));
2731 return;
2732 }
2733
2734 /* The version number. */
2735 const uint16_t version = read_2_bytes (abfd, addr);
2736 addr += 2;
2737 if (version != 2)
2738 {
47e3f474 2739 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2740 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2743 return;
2744 }
2745
2746 const uint64_t debug_info_offset
2747 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2748 addr += offset_size;
2749 const auto per_cu_it
2750 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2751 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2752 {
47e3f474 2753 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2754 "debug_info_offset %s does not exists, "
2755 "ignoring .debug_aranges."),
47e3f474
TV
2756 objfile_name (objfile),
2757 plongest (entry_addr - section->buffer),
927aa2e7
JK
2758 pulongest (debug_info_offset));
2759 return;
2760 }
2761 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2762
2763 const uint8_t address_size = *addr++;
2764 if (address_size < 1 || address_size > 8)
2765 {
47e3f474 2766 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2767 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2768 objfile_name (objfile),
2769 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2770 return;
2771 }
2772
2773 const uint8_t segment_selector_size = *addr++;
2774 if (segment_selector_size != 0)
2775 {
47e3f474 2776 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2777 "segment_selector_size %u is not supported, "
2778 "ignoring .debug_aranges."),
47e3f474
TV
2779 objfile_name (objfile),
2780 plongest (entry_addr - section->buffer),
927aa2e7
JK
2781 segment_selector_size);
2782 return;
2783 }
2784
2785 /* Must pad to an alignment boundary that is twice the address
2786 size. It is undocumented by the DWARF standard but GCC does
2787 use it. */
2788 for (size_t padding = ((-(addr - section->buffer))
2789 & (2 * address_size - 1));
2790 padding > 0; padding--)
2791 if (*addr++ != 0)
2792 {
47e3f474 2793 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2794 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2795 objfile_name (objfile),
2796 plongest (entry_addr - section->buffer));
927aa2e7
JK
2797 return;
2798 }
2799
2800 for (;;)
2801 {
2802 if (addr + 2 * address_size > entry_end)
2803 {
47e3f474 2804 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2805 "address list is not properly terminated, "
2806 "ignoring .debug_aranges."),
47e3f474
TV
2807 objfile_name (objfile),
2808 plongest (entry_addr - section->buffer));
927aa2e7
JK
2809 return;
2810 }
2811 ULONGEST start = extract_unsigned_integer (addr, address_size,
2812 dwarf5_byte_order);
2813 addr += address_size;
2814 ULONGEST length = extract_unsigned_integer (addr, address_size,
2815 dwarf5_byte_order);
2816 addr += address_size;
2817 if (start == 0 && length == 0)
2818 break;
2819 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2820 {
2821 /* Symbol was eliminated due to a COMDAT group. */
2822 continue;
2823 }
2824 ULONGEST end = start + length;
79748972
TT
2825 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2826 - baseaddr);
2827 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2828 - baseaddr);
927aa2e7
JK
2829 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2830 }
2831 }
2832
d320c2b5 2833 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2834 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2835}
2836
9291a0cd
TT
2837/* Find a slot in the mapped index INDEX for the object named NAME.
2838 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2839 constant pool and return true. If NAME cannot be found, return
2840 false. */
2fdf6df6 2841
109483d9 2842static bool
9291a0cd
TT
2843find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2844 offset_type **vec_out)
2845{
0cf03b49 2846 offset_type hash;
9291a0cd 2847 offset_type slot, step;
559a7a62 2848 int (*cmp) (const char *, const char *);
9291a0cd 2849
791afaa2 2850 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2851 if (current_language->la_language == language_cplus
45280282
IB
2852 || current_language->la_language == language_fortran
2853 || current_language->la_language == language_d)
0cf03b49
JK
2854 {
2855 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2856 not contain any. */
a8719064 2857
72998fb3 2858 if (strchr (name, '(') != NULL)
0cf03b49 2859 {
109483d9 2860 without_params = cp_remove_params (name);
0cf03b49 2861
72998fb3 2862 if (without_params != NULL)
791afaa2 2863 name = without_params.get ();
0cf03b49
JK
2864 }
2865 }
2866
559a7a62 2867 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2868 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2869 simulate our NAME being searched is also lowercased. */
2870 hash = mapped_index_string_hash ((index->version == 4
2871 && case_sensitivity == case_sensitive_off
2872 ? 5 : index->version),
2873 name);
2874
f00a2de2
PA
2875 slot = hash & (index->symbol_table.size () - 1);
2876 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2877 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2878
2879 for (;;)
2880 {
9291a0cd 2881 const char *str;
f00a2de2
PA
2882
2883 const auto &bucket = index->symbol_table[slot];
2884 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2885 return false;
9291a0cd 2886
f00a2de2 2887 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2888 if (!cmp (name, str))
9291a0cd
TT
2889 {
2890 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2891 + MAYBE_SWAP (bucket.vec));
109483d9 2892 return true;
9291a0cd
TT
2893 }
2894
f00a2de2 2895 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2896 }
2897}
2898
4485a1c1
SM
2899/* A helper function that reads the .gdb_index from BUFFER and fills
2900 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2901 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2902 ok to use deprecated sections.
2903
2904 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2905 out parameters that are filled in with information about the CU and
2906 TU lists in the section.
2907
4485a1c1 2908 Returns true if all went well, false otherwise. */
2fdf6df6 2909
d33bc52e 2910static bool
4485a1c1
SM
2911read_gdb_index_from_buffer (struct objfile *objfile,
2912 const char *filename,
2913 bool deprecated_ok,
2914 gdb::array_view<const gdb_byte> buffer,
2915 struct mapped_index *map,
2916 const gdb_byte **cu_list,
2917 offset_type *cu_list_elements,
2918 const gdb_byte **types_list,
2919 offset_type *types_list_elements)
2920{
2921 const gdb_byte *addr = &buffer[0];
82430852 2922
9291a0cd 2923 /* Version check. */
4485a1c1 2924 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2925 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2926 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2927 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2928 indices. */
831adc1f 2929 if (version < 4)
481860b3
GB
2930 {
2931 static int warning_printed = 0;
2932 if (!warning_printed)
2933 {
2934 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2935 filename);
481860b3
GB
2936 warning_printed = 1;
2937 }
2938 return 0;
2939 }
2940 /* Index version 4 uses a different hash function than index version
2941 5 and later.
2942
2943 Versions earlier than 6 did not emit psymbols for inlined
2944 functions. Using these files will cause GDB not to be able to
2945 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2946 indices unless the user has done
2947 "set use-deprecated-index-sections on". */
2ec9a5e0 2948 if (version < 6 && !deprecated_ok)
481860b3
GB
2949 {
2950 static int warning_printed = 0;
2951 if (!warning_printed)
2952 {
e615022a
DE
2953 warning (_("\
2954Skipping deprecated .gdb_index section in %s.\n\
2955Do \"set use-deprecated-index-sections on\" before the file is read\n\
2956to use the section anyway."),
2ec9a5e0 2957 filename);
481860b3
GB
2958 warning_printed = 1;
2959 }
2960 return 0;
2961 }
796a7ff8 2962 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2963 of the TU (for symbols coming from TUs),
2964 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2965 Plus gold-generated indices can have duplicate entries for global symbols,
2966 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2967 These are just performance bugs, and we can't distinguish gdb-generated
2968 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2969
481860b3 2970 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2971 longer backward compatible. */
796a7ff8 2972 if (version > 8)
594e8718 2973 return 0;
9291a0cd 2974
559a7a62 2975 map->version = version;
9291a0cd 2976
4485a1c1 2977 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2978
4485a1c1 2979 int i = 0;
2ec9a5e0
TT
2980 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2981 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2982 / 8);
1fd400ff
TT
2983 ++i;
2984
2ec9a5e0
TT
2985 *types_list = addr + MAYBE_SWAP (metadata[i]);
2986 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2987 - MAYBE_SWAP (metadata[i]))
2988 / 8);
987d643c 2989 ++i;
1fd400ff 2990
f00a2de2
PA
2991 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2992 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2993 map->address_table
2994 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2995 ++i;
2996
f00a2de2
PA
2997 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2998 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2999 map->symbol_table
3000 = gdb::array_view<mapped_index::symbol_table_slot>
3001 ((mapped_index::symbol_table_slot *) symbol_table,
3002 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3003
f00a2de2 3004 ++i;
f9d83a0b 3005 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3006
2ec9a5e0
TT
3007 return 1;
3008}
3009
4485a1c1
SM
3010/* Callback types for dwarf2_read_gdb_index. */
3011
3012typedef gdb::function_view
3013 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3014 get_gdb_index_contents_ftype;
3015typedef gdb::function_view
3016 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3017 get_gdb_index_contents_dwz_ftype;
3018
927aa2e7 3019/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3020 elements of all the CUs and return 1. Otherwise, return 0. */
3021
3022static int
4485a1c1
SM
3023dwarf2_read_gdb_index
3024 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3025 get_gdb_index_contents_ftype get_gdb_index_contents,
3026 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3027{
2ec9a5e0
TT
3028 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3029 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3030 struct dwz_file *dwz;
12359b5e 3031 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3032
4485a1c1
SM
3033 gdb::array_view<const gdb_byte> main_index_contents
3034 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3035
3036 if (main_index_contents.empty ())
3037 return 0;
3038
3063847f 3039 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3040 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3041 use_deprecated_index_sections,
3042 main_index_contents, map.get (), &cu_list,
3043 &cu_list_elements, &types_list,
3044 &types_list_elements))
2ec9a5e0
TT
3045 return 0;
3046
0fefef59 3047 /* Don't use the index if it's empty. */
3063847f 3048 if (map->symbol_table.empty ())
0fefef59
DE
3049 return 0;
3050
2ec9a5e0
TT
3051 /* If there is a .dwz file, read it so we can get its CU list as
3052 well. */
ed2dc618 3053 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3054 if (dwz != NULL)
2ec9a5e0 3055 {
2ec9a5e0
TT
3056 struct mapped_index dwz_map;
3057 const gdb_byte *dwz_types_ignore;
3058 offset_type dwz_types_elements_ignore;
3059
4485a1c1
SM
3060 gdb::array_view<const gdb_byte> dwz_index_content
3061 = get_gdb_index_contents_dwz (objfile, dwz);
3062
3063 if (dwz_index_content.empty ())
3064 return 0;
3065
3066 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3067 bfd_get_filename (dwz->dwz_bfd.get ()),
3068 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3069 &dwz_list, &dwz_list_elements,
3070 &dwz_types_ignore,
3071 &dwz_types_elements_ignore))
2ec9a5e0
TT
3072 {
3073 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3074 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3075 return 0;
3076 }
3077 }
3078
12359b5e
SM
3079 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3080 dwz_list, dwz_list_elements);
1fd400ff 3081
8b70b953
TT
3082 if (types_list_elements)
3083 {
8b70b953
TT
3084 /* We can only handle a single .debug_types when we have an
3085 index. */
fd5866f6 3086 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3087 return 0;
3088
fd5866f6 3089 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3090
12359b5e
SM
3091 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3092 types_list, types_list_elements);
8b70b953 3093 }
9291a0cd 3094
3063847f 3095 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3096
3063847f 3097 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3098 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3099 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3100 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3101
3102 return 1;
3103}
3104
dee91e82 3105/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3106
dee91e82
DE
3107static void
3108dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3109 const gdb_byte *info_ptr,
3e225074 3110 struct die_info *comp_unit_die)
9291a0cd 3111{
dee91e82 3112 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3113 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3114 struct dwarf2_per_objfile *dwarf2_per_objfile
3115 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3116 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3117 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3118 struct attribute *attr;
7b9f3c50
DE
3119 void **slot;
3120 struct quick_file_names *qfn;
9291a0cd 3121
0186c6a7
DE
3122 gdb_assert (! this_cu->is_debug_types);
3123
07261596
TT
3124 /* Our callers never want to match partial units -- instead they
3125 will match the enclosing full CU. */
3126 if (comp_unit_die->tag == DW_TAG_partial_unit)
3127 {
3128 this_cu->v.quick->no_file_data = 1;
3129 return;
3130 }
3131
0186c6a7 3132 lh_cu = this_cu;
7b9f3c50 3133 slot = NULL;
dee91e82 3134
fff8551c 3135 line_header_up lh;
9c541725 3136 sect_offset line_offset {};
fff8551c 3137
dee91e82 3138 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3139 if (attr != nullptr)
9291a0cd 3140 {
7b9f3c50
DE
3141 struct quick_file_names find_entry;
3142
9c541725 3143 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3144
3145 /* We may have already read in this line header (TU line header sharing).
3146 If we have we're done. */
094b34ac 3147 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3148 find_entry.hash.line_sect_off = line_offset;
5895093f 3149 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3150 &find_entry, INSERT);
3151 if (*slot != NULL)
3152 {
9a3c8263 3153 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3154 return;
7b9f3c50
DE
3155 }
3156
3019eac3 3157 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3158 }
3159 if (lh == NULL)
3160 {
094b34ac 3161 lh_cu->v.quick->no_file_data = 1;
dee91e82 3162 return;
9291a0cd
TT
3163 }
3164
8d749320 3165 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3166 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3167 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3168 gdb_assert (slot != NULL);
3169 *slot = qfn;
9291a0cd 3170
d721ba37 3171 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3172
aa391654
TT
3173 int offset = 0;
3174 if (strcmp (fnd.name, "<unknown>") != 0)
3175 ++offset;
3176
7ba99d21 3177 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3178 qfn->file_names =
aa391654
TT
3179 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3180 if (offset != 0)
3181 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3182 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3183 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3184 fnd.comp_dir).release ();
7b9f3c50 3185 qfn->real_names = NULL;
9291a0cd 3186
094b34ac 3187 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3188}
3189
3190/* A helper for the "quick" functions which attempts to read the line
3191 table for THIS_CU. */
3192
3193static struct quick_file_names *
e4a48d9d 3194dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3195{
0186c6a7
DE
3196 /* This should never be called for TUs. */
3197 gdb_assert (! this_cu->is_debug_types);
3198 /* Nor type unit groups. */
197400e8 3199 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3200
dee91e82
DE
3201 if (this_cu->v.quick->file_names != NULL)
3202 return this_cu->v.quick->file_names;
3203 /* If we know there is no line data, no point in looking again. */
3204 if (this_cu->v.quick->no_file_data)
3205 return NULL;
3206
c0ab21c2
TT
3207 cutu_reader reader (this_cu);
3208 if (!reader.dummy_p)
3e225074 3209 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3210
3211 if (this_cu->v.quick->no_file_data)
3212 return NULL;
3213 return this_cu->v.quick->file_names;
9291a0cd
TT
3214}
3215
3216/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3217 real path for a given file name from the line table. */
2fdf6df6 3218
9291a0cd 3219static const char *
7b9f3c50
DE
3220dw2_get_real_path (struct objfile *objfile,
3221 struct quick_file_names *qfn, int index)
9291a0cd 3222{
7b9f3c50
DE
3223 if (qfn->real_names == NULL)
3224 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3225 qfn->num_file_names, const char *);
9291a0cd 3226
7b9f3c50 3227 if (qfn->real_names[index] == NULL)
14278e1f 3228 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3229
7b9f3c50 3230 return qfn->real_names[index];
9291a0cd
TT
3231}
3232
3233static struct symtab *
3234dw2_find_last_source_symtab (struct objfile *objfile)
3235{
ed2dc618
SM
3236 struct dwarf2_per_objfile *dwarf2_per_objfile
3237 = get_dwarf2_per_objfile (objfile);
b76e467d 3238 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3239 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3240
43f3e411
DE
3241 if (cust == NULL)
3242 return NULL;
ed2dc618 3243
43f3e411 3244 return compunit_primary_filetab (cust);
9291a0cd
TT
3245}
3246
7b9f3c50
DE
3247/* Traversal function for dw2_forget_cached_source_info. */
3248
3249static int
3250dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3251{
7b9f3c50 3252 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3253
7b9f3c50 3254 if (file_data->real_names)
9291a0cd 3255 {
7b9f3c50 3256 int i;
9291a0cd 3257
7b9f3c50 3258 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3259 {
7b9f3c50
DE
3260 xfree ((void*) file_data->real_names[i]);
3261 file_data->real_names[i] = NULL;
9291a0cd
TT
3262 }
3263 }
7b9f3c50
DE
3264
3265 return 1;
3266}
3267
3268static void
3269dw2_forget_cached_source_info (struct objfile *objfile)
3270{
ed2dc618
SM
3271 struct dwarf2_per_objfile *dwarf2_per_objfile
3272 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3273
5895093f 3274 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3275 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3276}
3277
f8eba3c6
TT
3278/* Helper function for dw2_map_symtabs_matching_filename that expands
3279 the symtabs and calls the iterator. */
3280
3281static int
3282dw2_map_expand_apply (struct objfile *objfile,
3283 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3284 const char *name, const char *real_path,
14bc53a8 3285 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3286{
43f3e411 3287 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3288
3289 /* Don't visit already-expanded CUs. */
43f3e411 3290 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3291 return 0;
3292
3293 /* This may expand more than one symtab, and we want to iterate over
3294 all of them. */
58f0c718 3295 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3296
14bc53a8
PA
3297 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3298 last_made, callback);
f8eba3c6
TT
3299}
3300
3301/* Implementation of the map_symtabs_matching_filename method. */
3302
14bc53a8
PA
3303static bool
3304dw2_map_symtabs_matching_filename
3305 (struct objfile *objfile, const char *name, const char *real_path,
3306 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3307{
c011a4f4 3308 const char *name_basename = lbasename (name);
ed2dc618
SM
3309 struct dwarf2_per_objfile *dwarf2_per_objfile
3310 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3311
848e3e78
DE
3312 /* The rule is CUs specify all the files, including those used by
3313 any TU, so there's no need to scan TUs here. */
f4dc4d17 3314
b76e467d 3315 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3316 {
3d7bb9d9 3317 /* We only need to look at symtabs not already expanded. */
43f3e411 3318 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3319 continue;
3320
b76e467d 3321 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3322 if (file_data == NULL)
9291a0cd
TT
3323 continue;
3324
b76e467d 3325 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3326 {
7b9f3c50 3327 const char *this_name = file_data->file_names[j];
da235a7c 3328 const char *this_real_name;
9291a0cd 3329
af529f8f 3330 if (compare_filenames_for_search (this_name, name))
9291a0cd 3331 {
f5b95b50 3332 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3333 callback))
3334 return true;
288e77a7 3335 continue;
4aac40c8 3336 }
9291a0cd 3337
c011a4f4
DE
3338 /* Before we invoke realpath, which can get expensive when many
3339 files are involved, do a quick comparison of the basenames. */
3340 if (! basenames_may_differ
3341 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3342 continue;
3343
da235a7c
JK
3344 this_real_name = dw2_get_real_path (objfile, file_data, j);
3345 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3346 {
da235a7c 3347 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3348 callback))
3349 return true;
288e77a7 3350 continue;
da235a7c 3351 }
9291a0cd 3352
da235a7c
JK
3353 if (real_path != NULL)
3354 {
af529f8f
JK
3355 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3356 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3357 if (this_real_name != NULL
af529f8f 3358 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3359 {
f5b95b50 3360 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3361 callback))
3362 return true;
288e77a7 3363 continue;
9291a0cd
TT
3364 }
3365 }
3366 }
3367 }
3368
14bc53a8 3369 return false;
9291a0cd
TT
3370}
3371
da51c347
DE
3372/* Struct used to manage iterating over all CUs looking for a symbol. */
3373
3374struct dw2_symtab_iterator
9291a0cd 3375{
ed2dc618
SM
3376 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3377 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3378 /* If set, only look for symbols that match that block. Valid values are
3379 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3380 gdb::optional<block_enum> block_index;
da51c347
DE
3381 /* The kind of symbol we're looking for. */
3382 domain_enum domain;
3383 /* The list of CUs from the index entry of the symbol,
3384 or NULL if not found. */
3385 offset_type *vec;
3386 /* The next element in VEC to look at. */
3387 int next;
3388 /* The number of elements in VEC, or zero if there is no match. */
3389 int length;
8943b874
DE
3390 /* Have we seen a global version of the symbol?
3391 If so we can ignore all further global instances.
3392 This is to work around gold/15646, inefficient gold-generated
3393 indices. */
3394 int global_seen;
da51c347 3395};
9291a0cd 3396
2b79f376 3397/* Initialize the index symtab iterator ITER. */
2fdf6df6 3398
9291a0cd 3399static void
da51c347 3400dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3401 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3402 gdb::optional<block_enum> block_index,
da51c347
DE
3403 domain_enum domain,
3404 const char *name)
3405{
ed2dc618 3406 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3407 iter->block_index = block_index;
3408 iter->domain = domain;
3409 iter->next = 0;
8943b874 3410 iter->global_seen = 0;
da51c347 3411
3063847f 3412 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3413
3414 /* index is NULL if OBJF_READNOW. */
3415 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3416 iter->length = MAYBE_SWAP (*iter->vec);
3417 else
3418 {
3419 iter->vec = NULL;
3420 iter->length = 0;
3421 }
3422}
3423
3424/* Return the next matching CU or NULL if there are no more. */
3425
3426static struct dwarf2_per_cu_data *
3427dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3428{
ed2dc618
SM
3429 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3430
da51c347
DE
3431 for ( ; iter->next < iter->length; ++iter->next)
3432 {
3433 offset_type cu_index_and_attrs =
3434 MAYBE_SWAP (iter->vec[iter->next + 1]);
3435 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3436 gdb_index_symbol_kind symbol_kind =
3437 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3438 /* Only check the symbol attributes if they're present.
3439 Indices prior to version 7 don't record them,
3440 and indices >= 7 may elide them for certain symbols
3441 (gold does this). */
3442 int attrs_valid =
ed2dc618 3443 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3444 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3445
3190f0c6 3446 /* Don't crash on bad data. */
b76e467d 3447 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3448 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3449 {
b98664d3 3450 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3451 " [in module %s]"),
3452 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3453 continue;
3454 }
3455
ff4c9fec 3456 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3457
da51c347 3458 /* Skip if already read in. */
43f3e411 3459 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3460 continue;
3461
8943b874
DE
3462 /* Check static vs global. */
3463 if (attrs_valid)
3464 {
2b79f376
SM
3465 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3466
3467 if (iter->block_index.has_value ())
3468 {
3469 bool want_static = *iter->block_index == STATIC_BLOCK;
3470
3471 if (is_static != want_static)
3472 continue;
3473 }
3474
8943b874
DE
3475 /* Work around gold/15646. */
3476 if (!is_static && iter->global_seen)
3477 continue;
3478 if (!is_static)
3479 iter->global_seen = 1;
3480 }
da51c347
DE
3481
3482 /* Only check the symbol's kind if it has one. */
3483 if (attrs_valid)
3484 {
3485 switch (iter->domain)
3486 {
3487 case VAR_DOMAIN:
3488 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3489 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3490 /* Some types are also in VAR_DOMAIN. */
3491 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3492 continue;
3493 break;
3494 case STRUCT_DOMAIN:
3495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3496 continue;
3497 break;
3498 case LABEL_DOMAIN:
3499 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3500 continue;
3501 break;
59c35742
AB
3502 case MODULE_DOMAIN:
3503 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3504 continue;
3505 break;
da51c347
DE
3506 default:
3507 break;
3508 }
3509 }
3510
3511 ++iter->next;
3512 return per_cu;
3513 }
3514
3515 return NULL;
3516}
3517
43f3e411 3518static struct compunit_symtab *
c7f839cb 3519dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3520 const char *name, domain_enum domain)
9291a0cd 3521{
43f3e411 3522 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3523 struct dwarf2_per_objfile *dwarf2_per_objfile
3524 = get_dwarf2_per_objfile (objfile);
9291a0cd 3525
b5ec771e
PA
3526 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3527
ed2dc618
SM
3528 struct dw2_symtab_iterator iter;
3529 struct dwarf2_per_cu_data *per_cu;
da51c347 3530
2b79f376 3531 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3532
ed2dc618
SM
3533 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3534 {
3535 struct symbol *sym, *with_opaque = NULL;
58f0c718 3536 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3537 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3538 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3539
ed2dc618
SM
3540 sym = block_find_symbol (block, name, domain,
3541 block_find_non_opaque_type_preferred,
3542 &with_opaque);
b2e2f908 3543
ed2dc618
SM
3544 /* Some caution must be observed with overloaded functions
3545 and methods, since the index will not contain any overload
3546 information (but NAME might contain it). */
da51c347 3547
ed2dc618
SM
3548 if (sym != NULL
3549 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3550 return stab;
3551 if (with_opaque != NULL
3552 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3553 stab_best = stab;
da51c347 3554
ed2dc618 3555 /* Keep looking through other CUs. */
9291a0cd 3556 }
9291a0cd 3557
da51c347 3558 return stab_best;
9291a0cd
TT
3559}
3560
3561static void
3562dw2_print_stats (struct objfile *objfile)
3563{
ed2dc618
SM
3564 struct dwarf2_per_objfile *dwarf2_per_objfile
3565 = get_dwarf2_per_objfile (objfile);
b76e467d 3566 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3567 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3568 int count = 0;
9291a0cd 3569
ed2dc618 3570 for (int i = 0; i < total; ++i)
9291a0cd 3571 {
ff4c9fec 3572 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3573
43f3e411 3574 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3575 ++count;
3576 }
e4a48d9d 3577 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3578 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3579}
3580
779bd270
DE
3581/* This dumps minimal information about the index.
3582 It is called via "mt print objfiles".
3583 One use is to verify .gdb_index has been loaded by the
3584 gdb.dwarf2/gdb-index.exp testcase. */
3585
9291a0cd
TT
3586static void
3587dw2_dump (struct objfile *objfile)
3588{
ed2dc618
SM
3589 struct dwarf2_per_objfile *dwarf2_per_objfile
3590 = get_dwarf2_per_objfile (objfile);
3591
779bd270
DE
3592 gdb_assert (dwarf2_per_objfile->using_index);
3593 printf_filtered (".gdb_index:");
3594 if (dwarf2_per_objfile->index_table != NULL)
3595 {
3596 printf_filtered (" version %d\n",
3597 dwarf2_per_objfile->index_table->version);
3598 }
3599 else
3600 printf_filtered (" faked for \"readnow\"\n");
3601 printf_filtered ("\n");
9291a0cd
TT
3602}
3603
9291a0cd
TT
3604static void
3605dw2_expand_symtabs_for_function (struct objfile *objfile,
3606 const char *func_name)
3607{
ed2dc618
SM
3608 struct dwarf2_per_objfile *dwarf2_per_objfile
3609 = get_dwarf2_per_objfile (objfile);
da51c347 3610
ed2dc618
SM
3611 struct dw2_symtab_iterator iter;
3612 struct dwarf2_per_cu_data *per_cu;
da51c347 3613
2b79f376 3614 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3615
ed2dc618 3616 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3617 dw2_instantiate_symtab (per_cu, false);
da51c347 3618
9291a0cd
TT
3619}
3620
3621static void
3622dw2_expand_all_symtabs (struct objfile *objfile)
3623{
ed2dc618
SM
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
b76e467d 3626 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3627 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3628
ed2dc618 3629 for (int i = 0; i < total_units; ++i)
9291a0cd 3630 {
ff4c9fec 3631 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3632
58f0c718
TT
3633 /* We don't want to directly expand a partial CU, because if we
3634 read it with the wrong language, then assertion failures can
3635 be triggered later on. See PR symtab/23010. So, tell
3636 dw2_instantiate_symtab to skip partial CUs -- any important
3637 partial CU will be read via DW_TAG_imported_unit anyway. */
3638 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3639 }
3640}
3641
3642static void
652a8996
JK
3643dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3644 const char *fullname)
9291a0cd 3645{
ed2dc618
SM
3646 struct dwarf2_per_objfile *dwarf2_per_objfile
3647 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3648
3649 /* We don't need to consider type units here.
3650 This is only called for examining code, e.g. expand_line_sal.
3651 There can be an order of magnitude (or more) more type units
3652 than comp units, and we avoid them if we can. */
3653
b76e467d 3654 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3655 {
3d7bb9d9 3656 /* We only need to look at symtabs not already expanded. */
43f3e411 3657 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3658 continue;
3659
b76e467d 3660 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3661 if (file_data == NULL)
9291a0cd
TT
3662 continue;
3663
b76e467d 3664 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3665 {
652a8996
JK
3666 const char *this_fullname = file_data->file_names[j];
3667
3668 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3669 {
58f0c718 3670 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3671 break;
3672 }
3673 }
3674 }
3675}
3676
9291a0cd 3677static void
199b4314
TT
3678dw2_map_matching_symbols
3679 (struct objfile *objfile,
b054970d 3680 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3681 int global,
3682 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3683 symbol_compare_ftype *ordered_compare)
9291a0cd 3684{
40658b94 3685 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3686 current language is Ada for a non-Ada objfile using GNU index. As Ada
3687 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3688}
3689
e1ef7d7a
PA
3690/* Starting from a search name, return the string that finds the upper
3691 bound of all strings that start with SEARCH_NAME in a sorted name
3692 list. Returns the empty string to indicate that the upper bound is
3693 the end of the list. */
3694
3695static std::string
3696make_sort_after_prefix_name (const char *search_name)
3697{
3698 /* When looking to complete "func", we find the upper bound of all
3699 symbols that start with "func" by looking for where we'd insert
3700 the closest string that would follow "func" in lexicographical
3701 order. Usually, that's "func"-with-last-character-incremented,
3702 i.e. "fund". Mind non-ASCII characters, though. Usually those
3703 will be UTF-8 multi-byte sequences, but we can't be certain.
3704 Especially mind the 0xff character, which is a valid character in
3705 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3706 rule out compilers allowing it in identifiers. Note that
3707 conveniently, strcmp/strcasecmp are specified to compare
3708 characters interpreted as unsigned char. So what we do is treat
3709 the whole string as a base 256 number composed of a sequence of
3710 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3711 to 0, and carries 1 to the following more-significant position.
3712 If the very first character in SEARCH_NAME ends up incremented
3713 and carries/overflows, then the upper bound is the end of the
3714 list. The string after the empty string is also the empty
3715 string.
3716
3717 Some examples of this operation:
3718
3719 SEARCH_NAME => "+1" RESULT
3720
3721 "abc" => "abd"
3722 "ab\xff" => "ac"
3723 "\xff" "a" "\xff" => "\xff" "b"
3724 "\xff" => ""
3725 "\xff\xff" => ""
3726 "" => ""
3727
3728 Then, with these symbols for example:
3729
3730 func
3731 func1
3732 fund
3733
3734 completing "func" looks for symbols between "func" and
3735 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3736 which finds "func" and "func1", but not "fund".
3737
3738 And with:
3739
3740 funcÿ (Latin1 'ÿ' [0xff])
3741 funcÿ1
3742 fund
3743
3744 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3745 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3746
3747 And with:
3748
3749 ÿÿ (Latin1 'ÿ' [0xff])
3750 ÿÿ1
3751
3752 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3753 the end of the list.
3754 */
3755 std::string after = search_name;
3756 while (!after.empty () && (unsigned char) after.back () == 0xff)
3757 after.pop_back ();
3758 if (!after.empty ())
3759 after.back () = (unsigned char) after.back () + 1;
3760 return after;
3761}
3762
5c58de74 3763/* See declaration. */
61d96d7e 3764
5c58de74
PA
3765std::pair<std::vector<name_component>::const_iterator,
3766 std::vector<name_component>::const_iterator>
44ed8f3e 3767mapped_index_base::find_name_components_bounds
3b00ef10 3768 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3769{
5c58de74
PA
3770 auto *name_cmp
3771 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3772
3b00ef10
TT
3773 const char *lang_name
3774 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3775
3f563c84
PA
3776 /* Comparison function object for lower_bound that matches against a
3777 given symbol name. */
3778 auto lookup_compare_lower = [&] (const name_component &elem,
3779 const char *name)
3780 {
5c58de74 3781 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3782 const char *elem_name = elem_qualified + elem.name_offset;
3783 return name_cmp (elem_name, name) < 0;
3784 };
3785
3786 /* Comparison function object for upper_bound that matches against a
3787 given symbol name. */
3788 auto lookup_compare_upper = [&] (const char *name,
3789 const name_component &elem)
3790 {
5c58de74 3791 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3792 const char *elem_name = elem_qualified + elem.name_offset;
3793 return name_cmp (name, elem_name) < 0;
3794 };
3795
5c58de74
PA
3796 auto begin = this->name_components.begin ();
3797 auto end = this->name_components.end ();
3f563c84
PA
3798
3799 /* Find the lower bound. */
3800 auto lower = [&] ()
3801 {
3b00ef10 3802 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3803 return begin;
3804 else
3b00ef10 3805 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3806 } ();
3807
3808 /* Find the upper bound. */
3809 auto upper = [&] ()
3810 {
5c58de74 3811 if (lookup_name_without_params.completion_mode ())
3f563c84 3812 {
e1ef7d7a
PA
3813 /* In completion mode, we want UPPER to point past all
3814 symbols names that have the same prefix. I.e., with
3815 these symbols, and completing "func":
3816
3817 function << lower bound
3818 function1
3819 other_function << upper bound
3820
3821 We find the upper bound by looking for the insertion
3822 point of "func"-with-last-character-incremented,
3823 i.e. "fund". */
3b00ef10 3824 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3825 if (after.empty ())
3f563c84 3826 return end;
e6b2f5ef
PA
3827 return std::lower_bound (lower, end, after.c_str (),
3828 lookup_compare_lower);
3f563c84
PA
3829 }
3830 else
3b00ef10 3831 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3832 } ();
3833
5c58de74
PA
3834 return {lower, upper};
3835}
3836
3837/* See declaration. */
3838
3839void
44ed8f3e 3840mapped_index_base::build_name_components ()
5c58de74
PA
3841{
3842 if (!this->name_components.empty ())
3843 return;
3844
3845 this->name_components_casing = case_sensitivity;
3846 auto *name_cmp
3847 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3848
3849 /* The code below only knows how to break apart components of C++
3850 symbol names (and other languages that use '::' as
3b00ef10 3851 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3852 auto count = this->symbol_name_count ();
3853 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3854 {
44ed8f3e 3855 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3856 continue;
3857
3858 const char *name = this->symbol_name_at (idx);
3859
3860 /* Add each name component to the name component table. */
3861 unsigned int previous_len = 0;
3b00ef10
TT
3862
3863 if (strstr (name, "::") != nullptr)
3864 {
3865 for (unsigned int current_len = cp_find_first_component (name);
3866 name[current_len] != '\0';
3867 current_len += cp_find_first_component (name + current_len))
3868 {
3869 gdb_assert (name[current_len] == ':');
3870 this->name_components.push_back ({previous_len, idx});
3871 /* Skip the '::'. */
3872 current_len += 2;
3873 previous_len = current_len;
3874 }
3875 }
3876 else
5c58de74 3877 {
3b00ef10
TT
3878 /* Handle the Ada encoded (aka mangled) form here. */
3879 for (const char *iter = strstr (name, "__");
3880 iter != nullptr;
3881 iter = strstr (iter, "__"))
3882 {
3883 this->name_components.push_back ({previous_len, idx});
3884 iter += 2;
3885 previous_len = iter - name;
3886 }
5c58de74 3887 }
3b00ef10 3888
5c58de74
PA
3889 this->name_components.push_back ({previous_len, idx});
3890 }
3891
3892 /* Sort name_components elements by name. */
3893 auto name_comp_compare = [&] (const name_component &left,
3894 const name_component &right)
3895 {
3896 const char *left_qualified = this->symbol_name_at (left.idx);
3897 const char *right_qualified = this->symbol_name_at (right.idx);
3898
3899 const char *left_name = left_qualified + left.name_offset;
3900 const char *right_name = right_qualified + right.name_offset;
3901
3902 return name_cmp (left_name, right_name) < 0;
3903 };
3904
3905 std::sort (this->name_components.begin (),
3906 this->name_components.end (),
3907 name_comp_compare);
3908}
3909
3910/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3911 mapped_index_base instead of the containing objfile. This is split
3912 to a separate function in order to be able to unit test the
3913 name_components matching using a mock mapped_index_base. For each
5c58de74 3914 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3915 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3916
3917static void
3918dw2_expand_symtabs_matching_symbol
44ed8f3e 3919 (mapped_index_base &index,
5c58de74
PA
3920 const lookup_name_info &lookup_name_in,
3921 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3922 enum search_domain kind,
3b00ef10 3923 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3924{
3925 lookup_name_info lookup_name_without_params
3926 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3927
3928 /* Build the symbol name component sorted vector, if we haven't
3929 yet. */
3930 index.build_name_components ();
3931
3f563c84
PA
3932 /* The same symbol may appear more than once in the range though.
3933 E.g., if we're looking for symbols that complete "w", and we have
3934 a symbol named "w1::w2", we'll find the two name components for
3935 that same symbol in the range. To be sure we only call the
3936 callback once per symbol, we first collect the symbol name
3937 indexes that matched in a temporary vector and ignore
3938 duplicates. */
3939 std::vector<offset_type> matches;
3f563c84 3940
3b00ef10
TT
3941 struct name_and_matcher
3942 {
3943 symbol_name_matcher_ftype *matcher;
3944 const std::string &name;
3945
3946 bool operator== (const name_and_matcher &other) const
3f563c84 3947 {
3b00ef10
TT
3948 return matcher == other.matcher && name == other.name;
3949 }
3950 };
3951
3952 /* A vector holding all the different symbol name matchers, for all
3953 languages. */
3954 std::vector<name_and_matcher> matchers;
3955
3956 for (int i = 0; i < nr_languages; i++)
3957 {
3958 enum language lang_e = (enum language) i;
3959
3960 const language_defn *lang = language_def (lang_e);
3961 symbol_name_matcher_ftype *name_matcher
3962 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3963
3b00ef10
TT
3964 name_and_matcher key {
3965 name_matcher,
3966 lookup_name_without_params.language_lookup_name (lang_e)
3967 };
3968
3969 /* Don't insert the same comparison routine more than once.
3970 Note that we do this linear walk. This is not a problem in
3971 practice because the number of supported languages is
3972 low. */
3973 if (std::find (matchers.begin (), matchers.end (), key)
3974 != matchers.end ())
9291a0cd 3975 continue;
3b00ef10
TT
3976 matchers.push_back (std::move (key));
3977
3978 auto bounds
3979 = index.find_name_components_bounds (lookup_name_without_params,
3980 lang_e);
3981
3982 /* Now for each symbol name in range, check to see if we have a name
3983 match, and if so, call the MATCH_CALLBACK callback. */
3984
3985 for (; bounds.first != bounds.second; ++bounds.first)
3986 {
3987 const char *qualified = index.symbol_name_at (bounds.first->idx);
3988
3989 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3990 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3991 continue;
9291a0cd 3992
3b00ef10
TT
3993 matches.push_back (bounds.first->idx);
3994 }
3f563c84
PA
3995 }
3996
3997 std::sort (matches.begin (), matches.end ());
3998
3999 /* Finally call the callback, once per match. */
4000 ULONGEST prev = -1;
4001 for (offset_type idx : matches)
4002 {
4003 if (prev != idx)
4004 {
3b00ef10
TT
4005 if (!match_callback (idx))
4006 break;
3f563c84
PA
4007 prev = idx;
4008 }
4009 }
4010
4011 /* Above we use a type wider than idx's for 'prev', since 0 and
4012 (offset_type)-1 are both possible values. */
4013 static_assert (sizeof (prev) > sizeof (offset_type), "");
4014}
4015
c62446b1
PA
4016#if GDB_SELF_TEST
4017
4018namespace selftests { namespace dw2_expand_symtabs_matching {
4019
a3c5fafd
PA
4020/* A mock .gdb_index/.debug_names-like name index table, enough to
4021 exercise dw2_expand_symtabs_matching_symbol, which works with the
4022 mapped_index_base interface. Builds an index from the symbol list
4023 passed as parameter to the constructor. */
4024class mock_mapped_index : public mapped_index_base
c62446b1
PA
4025{
4026public:
a3c5fafd
PA
4027 mock_mapped_index (gdb::array_view<const char *> symbols)
4028 : m_symbol_table (symbols)
c62446b1
PA
4029 {}
4030
a3c5fafd 4031 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4032
a3c5fafd 4033 /* Return the number of names in the symbol table. */
632e107b 4034 size_t symbol_name_count () const override
c62446b1 4035 {
a3c5fafd 4036 return m_symbol_table.size ();
c62446b1
PA
4037 }
4038
a3c5fafd 4039 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4040 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4041 {
4042 return m_symbol_table[idx];
4043 }
c62446b1 4044
a3c5fafd
PA
4045private:
4046 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4047};
4048
4049/* Convenience function that converts a NULL pointer to a "<null>"
4050 string, to pass to print routines. */
4051
4052static const char *
4053string_or_null (const char *str)
4054{
4055 return str != NULL ? str : "<null>";
4056}
4057
4058/* Check if a lookup_name_info built from
4059 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4060 index. EXPECTED_LIST is the list of expected matches, in expected
4061 matching order. If no match expected, then an empty list is
4062 specified. Returns true on success. On failure prints a warning
4063 indicating the file:line that failed, and returns false. */
4064
4065static bool
4066check_match (const char *file, int line,
4067 mock_mapped_index &mock_index,
4068 const char *name, symbol_name_match_type match_type,
4069 bool completion_mode,
4070 std::initializer_list<const char *> expected_list)
4071{
4072 lookup_name_info lookup_name (name, match_type, completion_mode);
4073
4074 bool matched = true;
4075
4076 auto mismatch = [&] (const char *expected_str,
4077 const char *got)
4078 {
4079 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4080 "expected=\"%s\", got=\"%s\"\n"),
4081 file, line,
4082 (match_type == symbol_name_match_type::FULL
4083 ? "FULL" : "WILD"),
4084 name, string_or_null (expected_str), string_or_null (got));
4085 matched = false;
4086 };
4087
4088 auto expected_it = expected_list.begin ();
4089 auto expected_end = expected_list.end ();
4090
a3c5fafd 4091 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4092 NULL, ALL_DOMAIN,
4093 [&] (offset_type idx)
4094 {
a3c5fafd 4095 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4096 const char *expected_str
4097 = expected_it == expected_end ? NULL : *expected_it++;
4098
4099 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4100 mismatch (expected_str, matched_name);
3b00ef10 4101 return true;
c62446b1
PA
4102 });
4103
4104 const char *expected_str
4105 = expected_it == expected_end ? NULL : *expected_it++;
4106 if (expected_str != NULL)
4107 mismatch (expected_str, NULL);
4108
4109 return matched;
4110}
4111
4112/* The symbols added to the mock mapped_index for testing (in
4113 canonical form). */
4114static const char *test_symbols[] = {
4115 "function",
4116 "std::bar",
4117 "std::zfunction",
4118 "std::zfunction2",
4119 "w1::w2",
4120 "ns::foo<char*>",
4121 "ns::foo<int>",
4122 "ns::foo<long>",
a20714ff
PA
4123 "ns2::tmpl<int>::foo2",
4124 "(anonymous namespace)::A::B::C",
c62446b1 4125
e1ef7d7a
PA
4126 /* These are used to check that the increment-last-char in the
4127 matching algorithm for completion doesn't match "t1_fund" when
4128 completing "t1_func". */
4129 "t1_func",
4130 "t1_func1",
4131 "t1_fund",
4132 "t1_fund1",
4133
4134 /* A UTF-8 name with multi-byte sequences to make sure that
4135 cp-name-parser understands this as a single identifier ("função"
4136 is "function" in PT). */
4137 u8"u8função",
4138
4139 /* \377 (0xff) is Latin1 'ÿ'. */
4140 "yfunc\377",
4141
4142 /* \377 (0xff) is Latin1 'ÿ'. */
4143 "\377",
4144 "\377\377123",
4145
c62446b1
PA
4146 /* A name with all sorts of complications. Starts with "z" to make
4147 it easier for the completion tests below. */
4148#define Z_SYM_NAME \
4149 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4150 "::tuple<(anonymous namespace)::ui*, " \
4151 "std::default_delete<(anonymous namespace)::ui>, void>"
4152
4153 Z_SYM_NAME
4154};
4155
a3c5fafd
PA
4156/* Returns true if the mapped_index_base::find_name_component_bounds
4157 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4158 in completion mode. */
5c58de74
PA
4159
4160static bool
a3c5fafd 4161check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4162 const char *search_name,
4163 gdb::array_view<const char *> expected_syms)
4164{
4165 lookup_name_info lookup_name (search_name,
4166 symbol_name_match_type::FULL, true);
4167
3b00ef10
TT
4168 auto bounds = index.find_name_components_bounds (lookup_name,
4169 language_cplus);
5c58de74
PA
4170
4171 size_t distance = std::distance (bounds.first, bounds.second);
4172 if (distance != expected_syms.size ())
4173 return false;
4174
4175 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4176 {
4177 auto nc_elem = bounds.first + exp_elem;
4178 const char *qualified = index.symbol_name_at (nc_elem->idx);
4179 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4180 return false;
4181 }
4182
4183 return true;
4184}
4185
4186/* Test the lower-level mapped_index::find_name_component_bounds
4187 method. */
4188
c62446b1 4189static void
5c58de74
PA
4190test_mapped_index_find_name_component_bounds ()
4191{
4192 mock_mapped_index mock_index (test_symbols);
4193
a3c5fafd 4194 mock_index.build_name_components ();
5c58de74
PA
4195
4196 /* Test the lower-level mapped_index::find_name_component_bounds
4197 method in completion mode. */
4198 {
4199 static const char *expected_syms[] = {
4200 "t1_func",
4201 "t1_func1",
5c58de74
PA
4202 };
4203
a3c5fafd 4204 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4205 "t1_func", expected_syms));
4206 }
4207
4208 /* Check that the increment-last-char in the name matching algorithm
4209 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4210 {
4211 static const char *expected_syms1[] = {
4212 "\377",
4213 "\377\377123",
4214 };
a3c5fafd 4215 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4216 "\377", expected_syms1));
4217
4218 static const char *expected_syms2[] = {
4219 "\377\377123",
4220 };
a3c5fafd 4221 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4222 "\377\377", expected_syms2));
4223 }
4224}
4225
4226/* Test dw2_expand_symtabs_matching_symbol. */
4227
4228static void
4229test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4230{
4231 mock_mapped_index mock_index (test_symbols);
4232
4233 /* We let all tests run until the end even if some fails, for debug
4234 convenience. */
4235 bool any_mismatch = false;
4236
4237 /* Create the expected symbols list (an initializer_list). Needed
4238 because lists have commas, and we need to pass them to CHECK,
4239 which is a macro. */
4240#define EXPECT(...) { __VA_ARGS__ }
4241
4242 /* Wrapper for check_match that passes down the current
4243 __FILE__/__LINE__. */
4244#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4245 any_mismatch |= !check_match (__FILE__, __LINE__, \
4246 mock_index, \
4247 NAME, MATCH_TYPE, COMPLETION_MODE, \
4248 EXPECTED_LIST)
4249
4250 /* Identity checks. */
4251 for (const char *sym : test_symbols)
4252 {
4253 /* Should be able to match all existing symbols. */
4254 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4255 EXPECT (sym));
4256
4257 /* Should be able to match all existing symbols with
4258 parameters. */
4259 std::string with_params = std::string (sym) + "(int)";
4260 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4261 EXPECT (sym));
4262
4263 /* Should be able to match all existing symbols with
4264 parameters and qualifiers. */
4265 with_params = std::string (sym) + " ( int ) const";
4266 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4267 EXPECT (sym));
4268
4269 /* This should really find sym, but cp-name-parser.y doesn't
4270 know about lvalue/rvalue qualifiers yet. */
4271 with_params = std::string (sym) + " ( int ) &&";
4272 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4273 {});
4274 }
4275
e1ef7d7a
PA
4276 /* Check that the name matching algorithm for completion doesn't get
4277 confused with Latin1 'ÿ' / 0xff. */
4278 {
4279 static const char str[] = "\377";
4280 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4281 EXPECT ("\377", "\377\377123"));
4282 }
4283
4284 /* Check that the increment-last-char in the matching algorithm for
4285 completion doesn't match "t1_fund" when completing "t1_func". */
4286 {
4287 static const char str[] = "t1_func";
4288 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4289 EXPECT ("t1_func", "t1_func1"));
4290 }
4291
c62446b1
PA
4292 /* Check that completion mode works at each prefix of the expected
4293 symbol name. */
4294 {
4295 static const char str[] = "function(int)";
4296 size_t len = strlen (str);
4297 std::string lookup;
4298
4299 for (size_t i = 1; i < len; i++)
4300 {
4301 lookup.assign (str, i);
4302 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4303 EXPECT ("function"));
4304 }
4305 }
4306
4307 /* While "w" is a prefix of both components, the match function
4308 should still only be called once. */
4309 {
4310 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4311 EXPECT ("w1::w2"));
a20714ff
PA
4312 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4313 EXPECT ("w1::w2"));
c62446b1
PA
4314 }
4315
4316 /* Same, with a "complicated" symbol. */
4317 {
4318 static const char str[] = Z_SYM_NAME;
4319 size_t len = strlen (str);
4320 std::string lookup;
4321
4322 for (size_t i = 1; i < len; i++)
4323 {
4324 lookup.assign (str, i);
4325 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4326 EXPECT (Z_SYM_NAME));
4327 }
4328 }
4329
4330 /* In FULL mode, an incomplete symbol doesn't match. */
4331 {
4332 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4333 {});
4334 }
4335
4336 /* A complete symbol with parameters matches any overload, since the
4337 index has no overload info. */
4338 {
4339 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4340 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4341 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4342 EXPECT ("std::zfunction", "std::zfunction2"));
4343 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4344 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4345 }
4346
4347 /* Check that whitespace is ignored appropriately. A symbol with a
4348 template argument list. */
4349 {
4350 static const char expected[] = "ns::foo<int>";
4351 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4352 EXPECT (expected));
a20714ff
PA
4353 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4354 EXPECT (expected));
c62446b1
PA
4355 }
4356
4357 /* Check that whitespace is ignored appropriately. A symbol with a
4358 template argument list that includes a pointer. */
4359 {
4360 static const char expected[] = "ns::foo<char*>";
4361 /* Try both completion and non-completion modes. */
4362 static const bool completion_mode[2] = {false, true};
4363 for (size_t i = 0; i < 2; i++)
4364 {
4365 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4366 completion_mode[i], EXPECT (expected));
a20714ff
PA
4367 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4368 completion_mode[i], EXPECT (expected));
c62446b1
PA
4369
4370 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4371 completion_mode[i], EXPECT (expected));
a20714ff
PA
4372 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4373 completion_mode[i], EXPECT (expected));
c62446b1
PA
4374 }
4375 }
4376
4377 {
4378 /* Check method qualifiers are ignored. */
4379 static const char expected[] = "ns::foo<char*>";
4380 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4381 symbol_name_match_type::FULL, true, EXPECT (expected));
4382 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4383 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4384 CHECK_MATCH ("foo < char * > ( int ) const",
4385 symbol_name_match_type::WILD, true, EXPECT (expected));
4386 CHECK_MATCH ("foo < char * > ( int ) &&",
4387 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4388 }
4389
4390 /* Test lookup names that don't match anything. */
4391 {
a20714ff
PA
4392 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4393 {});
4394
c62446b1
PA
4395 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4396 {});
4397 }
4398
a20714ff
PA
4399 /* Some wild matching tests, exercising "(anonymous namespace)",
4400 which should not be confused with a parameter list. */
4401 {
4402 static const char *syms[] = {
4403 "A::B::C",
4404 "B::C",
4405 "C",
4406 "A :: B :: C ( int )",
4407 "B :: C ( int )",
4408 "C ( int )",
4409 };
4410
4411 for (const char *s : syms)
4412 {
4413 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4414 EXPECT ("(anonymous namespace)::A::B::C"));
4415 }
4416 }
4417
4418 {
4419 static const char expected[] = "ns2::tmpl<int>::foo2";
4420 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4421 EXPECT (expected));
4422 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4423 EXPECT (expected));
4424 }
4425
c62446b1
PA
4426 SELF_CHECK (!any_mismatch);
4427
4428#undef EXPECT
4429#undef CHECK_MATCH
4430}
4431
5c58de74
PA
4432static void
4433run_test ()
4434{
4435 test_mapped_index_find_name_component_bounds ();
4436 test_dw2_expand_symtabs_matching_symbol ();
4437}
4438
c62446b1
PA
4439}} // namespace selftests::dw2_expand_symtabs_matching
4440
4441#endif /* GDB_SELF_TEST */
4442
4b514bc8
JK
4443/* If FILE_MATCHER is NULL or if PER_CU has
4444 dwarf2_per_cu_quick_data::MARK set (see
4445 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4446 EXPANSION_NOTIFY on it. */
4447
4448static void
4449dw2_expand_symtabs_matching_one
4450 (struct dwarf2_per_cu_data *per_cu,
4451 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4452 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4453{
4454 if (file_matcher == NULL || per_cu->v.quick->mark)
4455 {
4456 bool symtab_was_null
4457 = (per_cu->v.quick->compunit_symtab == NULL);
4458
58f0c718 4459 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4460
4461 if (expansion_notify != NULL
4462 && symtab_was_null
4463 && per_cu->v.quick->compunit_symtab != NULL)
4464 expansion_notify (per_cu->v.quick->compunit_symtab);
4465 }
4466}
4467
3f563c84
PA
4468/* Helper for dw2_expand_matching symtabs. Called on each symbol
4469 matched, to expand corresponding CUs that were marked. IDX is the
4470 index of the symbol name that matched. */
4471
4472static void
4473dw2_expand_marked_cus
ed2dc618 4474 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4475 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4476 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4477 search_domain kind)
4478{
3f563c84
PA
4479 offset_type *vec, vec_len, vec_idx;
4480 bool global_seen = false;
ed2dc618 4481 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4482
61920122 4483 vec = (offset_type *) (index.constant_pool
f00a2de2 4484 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4485 vec_len = MAYBE_SWAP (vec[0]);
4486 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4487 {
61920122
PA
4488 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4489 /* This value is only valid for index versions >= 7. */
4490 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4491 gdb_index_symbol_kind symbol_kind =
4492 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4493 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4494 /* Only check the symbol attributes if they're present.
4495 Indices prior to version 7 don't record them,
4496 and indices >= 7 may elide them for certain symbols
4497 (gold does this). */
4498 int attrs_valid =
4499 (index.version >= 7
4500 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4501
4502 /* Work around gold/15646. */
4503 if (attrs_valid)
9291a0cd 4504 {
61920122
PA
4505 if (!is_static && global_seen)
4506 continue;
4507 if (!is_static)
4508 global_seen = true;
4509 }
3190f0c6 4510
61920122
PA
4511 /* Only check the symbol's kind if it has one. */
4512 if (attrs_valid)
4513 {
4514 switch (kind)
8943b874 4515 {
61920122
PA
4516 case VARIABLES_DOMAIN:
4517 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4518 continue;
4519 break;
4520 case FUNCTIONS_DOMAIN:
4521 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4522 continue;
61920122
PA
4523 break;
4524 case TYPES_DOMAIN:
4525 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4526 continue;
4527 break;
59c35742
AB
4528 case MODULES_DOMAIN:
4529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4530 continue;
4531 break;
61920122
PA
4532 default:
4533 break;
8943b874 4534 }
61920122 4535 }
8943b874 4536
61920122 4537 /* Don't crash on bad data. */
b76e467d 4538 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4539 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4540 {
b98664d3 4541 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4542 " [in module %s]"),
4543 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4544 continue;
4545 }
4546
ff4c9fec 4547 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4548 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4549 expansion_notify);
61920122
PA
4550 }
4551}
4552
4b514bc8
JK
4553/* If FILE_MATCHER is non-NULL, set all the
4554 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4555 that match FILE_MATCHER. */
4556
61920122 4557static void
4b514bc8 4558dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4559 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4560 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4561{
4b514bc8 4562 if (file_matcher == NULL)
61920122
PA
4563 return;
4564
4b514bc8
JK
4565 objfile *const objfile = dwarf2_per_objfile->objfile;
4566
4567 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4568 htab_eq_pointer,
4569 NULL, xcalloc, xfree));
4570 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4571 htab_eq_pointer,
4572 NULL, xcalloc, xfree));
61920122 4573
4b514bc8
JK
4574 /* The rule is CUs specify all the files, including those used by
4575 any TU, so there's no need to scan TUs here. */
61920122 4576
b76e467d 4577 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4578 {
927aa2e7
JK
4579 QUIT;
4580
4581 per_cu->v.quick->mark = 0;
4582
4583 /* We only need to look at symtabs not already expanded. */
4584 if (per_cu->v.quick->compunit_symtab)
4585 continue;
4586
b76e467d 4587 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4588 if (file_data == NULL)
4589 continue;
4590
4591 if (htab_find (visited_not_found.get (), file_data) != NULL)
4592 continue;
4593 else if (htab_find (visited_found.get (), file_data) != NULL)
4594 {
4595 per_cu->v.quick->mark = 1;
4596 continue;
4597 }
4598
b76e467d 4599 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4600 {
4601 const char *this_real_name;
4602
4603 if (file_matcher (file_data->file_names[j], false))
4604 {
4605 per_cu->v.quick->mark = 1;
4606 break;
4607 }
4608
4609 /* Before we invoke realpath, which can get expensive when many
4610 files are involved, do a quick comparison of the basenames. */
4611 if (!basenames_may_differ
4612 && !file_matcher (lbasename (file_data->file_names[j]),
4613 true))
4614 continue;
4615
4616 this_real_name = dw2_get_real_path (objfile, file_data, j);
4617 if (file_matcher (this_real_name, false))
4618 {
4619 per_cu->v.quick->mark = 1;
4620 break;
4621 }
4622 }
4623
b76e467d
SM
4624 void **slot = htab_find_slot (per_cu->v.quick->mark
4625 ? visited_found.get ()
4626 : visited_not_found.get (),
4627 file_data, INSERT);
927aa2e7
JK
4628 *slot = file_data;
4629 }
4630}
4631
4632static void
4633dw2_expand_symtabs_matching
4634 (struct objfile *objfile,
4635 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4636 const lookup_name_info &lookup_name,
4637 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4638 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4639 enum search_domain kind)
4640{
ed2dc618
SM
4641 struct dwarf2_per_objfile *dwarf2_per_objfile
4642 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4643
4644 /* index_table is NULL if OBJF_READNOW. */
4645 if (!dwarf2_per_objfile->index_table)
4646 return;
4647
ed2dc618 4648 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4649
4650 mapped_index &index = *dwarf2_per_objfile->index_table;
4651
4652 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4653 symbol_matcher,
4654 kind, [&] (offset_type idx)
4655 {
ed2dc618 4656 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4657 expansion_notify, kind);
3b00ef10 4658 return true;
927aa2e7
JK
4659 });
4660}
4661
4662/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4663 symtab. */
4664
4665static struct compunit_symtab *
4666recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4667 CORE_ADDR pc)
4668{
4669 int i;
4670
4671 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4672 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4673 return cust;
4674
4675 if (cust->includes == NULL)
4676 return NULL;
4677
4678 for (i = 0; cust->includes[i]; ++i)
4679 {
4680 struct compunit_symtab *s = cust->includes[i];
4681
4682 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4683 if (s != NULL)
4684 return s;
4685 }
4686
4687 return NULL;
4688}
4689
4690static struct compunit_symtab *
4691dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4692 struct bound_minimal_symbol msymbol,
4693 CORE_ADDR pc,
4694 struct obj_section *section,
4695 int warn_if_readin)
4696{
4697 struct dwarf2_per_cu_data *data;
4698 struct compunit_symtab *result;
4699
d320c2b5 4700 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4701 return NULL;
4702
b3b3bada 4703 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4704 data = (struct dwarf2_per_cu_data *) addrmap_find
4705 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4706 if (!data)
4707 return NULL;
4708
4709 if (warn_if_readin && data->v.quick->compunit_symtab)
4710 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4711 paddress (get_objfile_arch (objfile), pc));
4712
4713 result
58f0c718
TT
4714 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4715 false),
927aa2e7
JK
4716 pc);
4717 gdb_assert (result != NULL);
4718 return result;
4719}
4720
4721static void
4722dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4723 void *data, int need_fullname)
4724{
ed2dc618
SM
4725 struct dwarf2_per_objfile *dwarf2_per_objfile
4726 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4727
4728 if (!dwarf2_per_objfile->filenames_cache)
4729 {
4730 dwarf2_per_objfile->filenames_cache.emplace ();
4731
4732 htab_up visited (htab_create_alloc (10,
4733 htab_hash_pointer, htab_eq_pointer,
4734 NULL, xcalloc, xfree));
4735
4736 /* The rule is CUs specify all the files, including those used
4737 by any TU, so there's no need to scan TUs here. We can
4738 ignore file names coming from already-expanded CUs. */
4739
b76e467d 4740 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4741 {
927aa2e7
JK
4742 if (per_cu->v.quick->compunit_symtab)
4743 {
4744 void **slot = htab_find_slot (visited.get (),
4745 per_cu->v.quick->file_names,
4746 INSERT);
4747
4748 *slot = per_cu->v.quick->file_names;
4749 }
4750 }
4751
b76e467d 4752 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4753 {
927aa2e7
JK
4754 /* We only need to look at symtabs not already expanded. */
4755 if (per_cu->v.quick->compunit_symtab)
4756 continue;
4757
b76e467d 4758 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4759 if (file_data == NULL)
4760 continue;
4761
b76e467d 4762 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4763 if (*slot)
4764 {
4765 /* Already visited. */
4766 continue;
4767 }
4768 *slot = file_data;
4769
4770 for (int j = 0; j < file_data->num_file_names; ++j)
4771 {
4772 const char *filename = file_data->file_names[j];
4773 dwarf2_per_objfile->filenames_cache->seen (filename);
4774 }
4775 }
4776 }
4777
4778 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4779 {
4780 gdb::unique_xmalloc_ptr<char> this_real_name;
4781
4782 if (need_fullname)
4783 this_real_name = gdb_realpath (filename);
4784 (*fun) (filename, this_real_name.get (), data);
4785 });
4786}
4787
4788static int
4789dw2_has_symbols (struct objfile *objfile)
4790{
4791 return 1;
4792}
4793
4794const struct quick_symbol_functions dwarf2_gdb_index_functions =
4795{
4796 dw2_has_symbols,
4797 dw2_find_last_source_symtab,
4798 dw2_forget_cached_source_info,
4799 dw2_map_symtabs_matching_filename,
4800 dw2_lookup_symbol,
4801 dw2_print_stats,
4802 dw2_dump,
927aa2e7
JK
4803 dw2_expand_symtabs_for_function,
4804 dw2_expand_all_symtabs,
4805 dw2_expand_symtabs_with_fullname,
4806 dw2_map_matching_symbols,
4807 dw2_expand_symtabs_matching,
4808 dw2_find_pc_sect_compunit_symtab,
4809 NULL,
4810 dw2_map_symbol_filenames
4811};
4812
4813/* DWARF-5 debug_names reader. */
4814
4815/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4816static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4817
4818/* A helper function that reads the .debug_names section in SECTION
4819 and fills in MAP. FILENAME is the name of the file containing the
4820 section; it is used for error reporting.
4821
4822 Returns true if all went well, false otherwise. */
4823
4824static bool
4825read_debug_names_from_section (struct objfile *objfile,
4826 const char *filename,
4827 struct dwarf2_section_info *section,
4828 mapped_debug_names &map)
4829{
96b79293 4830 if (section->empty ())
927aa2e7
JK
4831 return false;
4832
4833 /* Older elfutils strip versions could keep the section in the main
4834 executable while splitting it for the separate debug info file. */
96b79293 4835 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4836 return false;
4837
96b79293 4838 section->read (objfile);
927aa2e7
JK
4839
4840 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4841
4842 const gdb_byte *addr = section->buffer;
4843
96b79293 4844 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4845
4846 unsigned int bytes_read;
4847 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4848 addr += bytes_read;
4849
4850 map.dwarf5_is_dwarf64 = bytes_read != 4;
4851 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4852 if (bytes_read + length != section->size)
4853 {
4854 /* There may be multiple per-CU indices. */
4855 warning (_("Section .debug_names in %s length %s does not match "
4856 "section length %s, ignoring .debug_names."),
4857 filename, plongest (bytes_read + length),
4858 pulongest (section->size));
4859 return false;
4860 }
4861
4862 /* The version number. */
4863 uint16_t version = read_2_bytes (abfd, addr);
4864 addr += 2;
4865 if (version != 5)
4866 {
4867 warning (_("Section .debug_names in %s has unsupported version %d, "
4868 "ignoring .debug_names."),
4869 filename, version);
4870 return false;
4871 }
4872
4873 /* Padding. */
4874 uint16_t padding = read_2_bytes (abfd, addr);
4875 addr += 2;
4876 if (padding != 0)
4877 {
4878 warning (_("Section .debug_names in %s has unsupported padding %d, "
4879 "ignoring .debug_names."),
4880 filename, padding);
4881 return false;
4882 }
4883
4884 /* comp_unit_count - The number of CUs in the CU list. */
4885 map.cu_count = read_4_bytes (abfd, addr);
4886 addr += 4;
4887
4888 /* local_type_unit_count - The number of TUs in the local TU
4889 list. */
4890 map.tu_count = read_4_bytes (abfd, addr);
4891 addr += 4;
4892
4893 /* foreign_type_unit_count - The number of TUs in the foreign TU
4894 list. */
4895 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4896 addr += 4;
4897 if (foreign_tu_count != 0)
4898 {
4899 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4900 "ignoring .debug_names."),
4901 filename, static_cast<unsigned long> (foreign_tu_count));
4902 return false;
4903 }
4904
4905 /* bucket_count - The number of hash buckets in the hash lookup
4906 table. */
4907 map.bucket_count = read_4_bytes (abfd, addr);
4908 addr += 4;
4909
4910 /* name_count - The number of unique names in the index. */
4911 map.name_count = read_4_bytes (abfd, addr);
4912 addr += 4;
4913
4914 /* abbrev_table_size - The size in bytes of the abbreviations
4915 table. */
4916 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4917 addr += 4;
4918
4919 /* augmentation_string_size - The size in bytes of the augmentation
4920 string. This value is rounded up to a multiple of 4. */
4921 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4922 addr += 4;
4923 map.augmentation_is_gdb = ((augmentation_string_size
4924 == sizeof (dwarf5_augmentation))
4925 && memcmp (addr, dwarf5_augmentation,
4926 sizeof (dwarf5_augmentation)) == 0);
4927 augmentation_string_size += (-augmentation_string_size) & 3;
4928 addr += augmentation_string_size;
4929
4930 /* List of CUs */
4931 map.cu_table_reordered = addr;
4932 addr += map.cu_count * map.offset_size;
4933
4934 /* List of Local TUs */
4935 map.tu_table_reordered = addr;
4936 addr += map.tu_count * map.offset_size;
4937
4938 /* Hash Lookup Table */
4939 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4940 addr += map.bucket_count * 4;
4941 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4942 addr += map.name_count * 4;
4943
4944 /* Name Table */
4945 map.name_table_string_offs_reordered = addr;
4946 addr += map.name_count * map.offset_size;
4947 map.name_table_entry_offs_reordered = addr;
4948 addr += map.name_count * map.offset_size;
4949
4950 const gdb_byte *abbrev_table_start = addr;
4951 for (;;)
4952 {
927aa2e7
JK
4953 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4954 addr += bytes_read;
4955 if (index_num == 0)
4956 break;
4957
4958 const auto insertpair
4959 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4960 if (!insertpair.second)
4961 {
4962 warning (_("Section .debug_names in %s has duplicate index %s, "
4963 "ignoring .debug_names."),
4964 filename, pulongest (index_num));
4965 return false;
4966 }
4967 mapped_debug_names::index_val &indexval = insertpair.first->second;
4968 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4969 addr += bytes_read;
4970
4971 for (;;)
4972 {
4973 mapped_debug_names::index_val::attr attr;
4974 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4975 addr += bytes_read;
4976 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4977 addr += bytes_read;
4978 if (attr.form == DW_FORM_implicit_const)
4979 {
4980 attr.implicit_const = read_signed_leb128 (abfd, addr,
4981 &bytes_read);
4982 addr += bytes_read;
4983 }
4984 if (attr.dw_idx == 0 && attr.form == 0)
4985 break;
4986 indexval.attr_vec.push_back (std::move (attr));
4987 }
4988 }
4989 if (addr != abbrev_table_start + abbrev_table_size)
4990 {
4991 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4992 "of size %s vs. written as %u, ignoring .debug_names."),
4993 filename, plongest (addr - abbrev_table_start),
4994 abbrev_table_size);
927aa2e7
JK
4995 return false;
4996 }
4997 map.entry_pool = addr;
4998
4999 return true;
5000}
5001
5002/* A helper for create_cus_from_debug_names that handles the MAP's CU
5003 list. */
5004
5005static void
ed2dc618 5006create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5007 const mapped_debug_names &map,
5008 dwarf2_section_info &section,
b76e467d 5009 bool is_dwz)
927aa2e7
JK
5010{
5011 sect_offset sect_off_prev;
5012 for (uint32_t i = 0; i <= map.cu_count; ++i)
5013 {
5014 sect_offset sect_off_next;
5015 if (i < map.cu_count)
5016 {
5017 sect_off_next
5018 = (sect_offset) (extract_unsigned_integer
5019 (map.cu_table_reordered + i * map.offset_size,
5020 map.offset_size,
5021 map.dwarf5_byte_order));
5022 }
5023 else
5024 sect_off_next = (sect_offset) section.size;
5025 if (i >= 1)
5026 {
5027 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5028 dwarf2_per_cu_data *per_cu
ed2dc618 5029 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5030 sect_off_prev, length);
b76e467d 5031 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5032 }
5033 sect_off_prev = sect_off_next;
5034 }
5035}
5036
5037/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5038 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5039
5040static void
ed2dc618 5041create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5042 const mapped_debug_names &map,
5043 const mapped_debug_names &dwz_map)
5044{
b76e467d
SM
5045 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5046 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5047
ed2dc618
SM
5048 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5049 dwarf2_per_objfile->info,
b76e467d 5050 false /* is_dwz */);
927aa2e7
JK
5051
5052 if (dwz_map.cu_count == 0)
5053 return;
5054
ed2dc618
SM
5055 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5056 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5057 true /* is_dwz */);
927aa2e7
JK
5058}
5059
5060/* Read .debug_names. If everything went ok, initialize the "quick"
5061 elements of all the CUs and return true. Otherwise, return false. */
5062
5063static bool
ed2dc618 5064dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5065{
22ca247e
TT
5066 std::unique_ptr<mapped_debug_names> map
5067 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5068 mapped_debug_names dwz_map (dwarf2_per_objfile);
5069 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5070
5071 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5072 &dwarf2_per_objfile->debug_names,
22ca247e 5073 *map))
927aa2e7
JK
5074 return false;
5075
5076 /* Don't use the index if it's empty. */
22ca247e 5077 if (map->name_count == 0)
927aa2e7
JK
5078 return false;
5079
5080 /* If there is a .dwz file, read it so we can get its CU list as
5081 well. */
ed2dc618 5082 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5083 if (dwz != NULL)
5084 {
5085 if (!read_debug_names_from_section (objfile,
00f93c44 5086 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5087 &dwz->debug_names, dwz_map))
5088 {
5089 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5090 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5091 return false;
5092 }
5093 }
5094
22ca247e 5095 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5096
22ca247e 5097 if (map->tu_count != 0)
927aa2e7
JK
5098 {
5099 /* We can only handle a single .debug_types when we have an
5100 index. */
fd5866f6 5101 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5102 return false;
5103
fd5866f6 5104 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5105
5106 create_signatured_type_table_from_debug_names
22ca247e 5107 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5108 }
5109
ed2dc618
SM
5110 create_addrmap_from_aranges (dwarf2_per_objfile,
5111 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5112
22ca247e 5113 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5114 dwarf2_per_objfile->using_index = 1;
5115 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5116 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5117
5118 return true;
5119}
5120
927aa2e7
JK
5121/* Type used to manage iterating over all CUs looking for a symbol for
5122 .debug_names. */
5123
5124class dw2_debug_names_iterator
5125{
5126public:
927aa2e7 5127 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5128 gdb::optional<block_enum> block_index,
5129 domain_enum domain,
927aa2e7 5130 const char *name)
2b79f376 5131 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5132 m_addr (find_vec_in_debug_names (map, name))
5133 {}
5134
5135 dw2_debug_names_iterator (const mapped_debug_names &map,
5136 search_domain search, uint32_t namei)
5137 : m_map (map),
5138 m_search (search),
5139 m_addr (find_vec_in_debug_names (map, namei))
5140 {}
5141
3b00ef10
TT
5142 dw2_debug_names_iterator (const mapped_debug_names &map,
5143 block_enum block_index, domain_enum domain,
5144 uint32_t namei)
5145 : m_map (map), m_block_index (block_index), m_domain (domain),
5146 m_addr (find_vec_in_debug_names (map, namei))
5147 {}
5148
927aa2e7
JK
5149 /* Return the next matching CU or NULL if there are no more. */
5150 dwarf2_per_cu_data *next ();
5151
5152private:
5153 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5154 const char *name);
5155 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5156 uint32_t namei);
5157
5158 /* The internalized form of .debug_names. */
5159 const mapped_debug_names &m_map;
5160
2b79f376
SM
5161 /* If set, only look for symbols that match that block. Valid values are
5162 GLOBAL_BLOCK and STATIC_BLOCK. */
5163 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5164
5165 /* The kind of symbol we're looking for. */
5166 const domain_enum m_domain = UNDEF_DOMAIN;
5167 const search_domain m_search = ALL_DOMAIN;
5168
5169 /* The list of CUs from the index entry of the symbol, or NULL if
5170 not found. */
5171 const gdb_byte *m_addr;
5172};
5173
5174const char *
5175mapped_debug_names::namei_to_name (uint32_t namei) const
5176{
5177 const ULONGEST namei_string_offs
5178 = extract_unsigned_integer ((name_table_string_offs_reordered
5179 + namei * offset_size),
5180 offset_size,
5181 dwarf5_byte_order);
5182 return read_indirect_string_at_offset
ed2dc618 5183 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5184}
5185
5186/* Find a slot in .debug_names for the object named NAME. If NAME is
5187 found, return pointer to its pool data. If NAME cannot be found,
5188 return NULL. */
5189
5190const gdb_byte *
5191dw2_debug_names_iterator::find_vec_in_debug_names
5192 (const mapped_debug_names &map, const char *name)
5193{
5194 int (*cmp) (const char *, const char *);
5195
54ee4252 5196 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5197 if (current_language->la_language == language_cplus
5198 || current_language->la_language == language_fortran
5199 || current_language->la_language == language_d)
5200 {
5201 /* NAME is already canonical. Drop any qualifiers as
5202 .debug_names does not contain any. */
5203
5204 if (strchr (name, '(') != NULL)
5205 {
54ee4252 5206 without_params = cp_remove_params (name);
927aa2e7 5207 if (without_params != NULL)
54ee4252 5208 name = without_params.get ();
927aa2e7
JK
5209 }
5210 }
5211
5212 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5213
5214 const uint32_t full_hash = dwarf5_djb_hash (name);
5215 uint32_t namei
5216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5217 (map.bucket_table_reordered
5218 + (full_hash % map.bucket_count)), 4,
5219 map.dwarf5_byte_order);
5220 if (namei == 0)
5221 return NULL;
5222 --namei;
5223 if (namei >= map.name_count)
5224 {
b98664d3 5225 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5226 "[in module %s]"),
5227 namei, map.name_count,
ed2dc618 5228 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5229 return NULL;
5230 }
5231
5232 for (;;)
5233 {
5234 const uint32_t namei_full_hash
5235 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5236 (map.hash_table_reordered + namei), 4,
5237 map.dwarf5_byte_order);
5238 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5239 return NULL;
5240
5241 if (full_hash == namei_full_hash)
5242 {
5243 const char *const namei_string = map.namei_to_name (namei);
5244
5245#if 0 /* An expensive sanity check. */
5246 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5247 {
b98664d3 5248 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5249 "[in module %s]"),
5250 namei, objfile_name (dwarf2_per_objfile->objfile));
5251 return NULL;
5252 }
5253#endif
5254
5255 if (cmp (namei_string, name) == 0)
5256 {
5257 const ULONGEST namei_entry_offs
5258 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5259 + namei * map.offset_size),
5260 map.offset_size, map.dwarf5_byte_order);
5261 return map.entry_pool + namei_entry_offs;
5262 }
5263 }
5264
5265 ++namei;
5266 if (namei >= map.name_count)
5267 return NULL;
5268 }
5269}
5270
5271const gdb_byte *
5272dw2_debug_names_iterator::find_vec_in_debug_names
5273 (const mapped_debug_names &map, uint32_t namei)
5274{
5275 if (namei >= map.name_count)
5276 {
b98664d3 5277 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5278 "[in module %s]"),
5279 namei, map.name_count,
ed2dc618 5280 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5281 return NULL;
5282 }
5283
5284 const ULONGEST namei_entry_offs
5285 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5286 + namei * map.offset_size),
5287 map.offset_size, map.dwarf5_byte_order);
5288 return map.entry_pool + namei_entry_offs;
5289}
5290
5291/* See dw2_debug_names_iterator. */
5292
5293dwarf2_per_cu_data *
5294dw2_debug_names_iterator::next ()
5295{
5296 if (m_addr == NULL)
5297 return NULL;
5298
ed2dc618
SM
5299 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5300 struct objfile *objfile = dwarf2_per_objfile->objfile;
5301 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5302
5303 again:
5304
5305 unsigned int bytes_read;
5306 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5307 m_addr += bytes_read;
5308 if (abbrev == 0)
5309 return NULL;
5310
5311 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5312 if (indexval_it == m_map.abbrev_map.cend ())
5313 {
b98664d3 5314 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5315 "[in module %s]"),
ed2dc618 5316 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5317 return NULL;
5318 }
5319 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5320 enum class symbol_linkage {
5321 unknown,
5322 static_,
5323 extern_,
23c13d42 5324 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5325 dwarf2_per_cu_data *per_cu = NULL;
5326 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5327 {
5328 ULONGEST ull;
5329 switch (attr.form)
5330 {
5331 case DW_FORM_implicit_const:
5332 ull = attr.implicit_const;
5333 break;
5334 case DW_FORM_flag_present:
5335 ull = 1;
5336 break;
5337 case DW_FORM_udata:
5338 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5339 m_addr += bytes_read;
5340 break;
5341 default:
b98664d3 5342 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5343 dwarf_form_name (attr.form),
ed2dc618 5344 objfile_name (objfile));
927aa2e7
JK
5345 return NULL;
5346 }
5347 switch (attr.dw_idx)
5348 {
5349 case DW_IDX_compile_unit:
5350 /* Don't crash on bad data. */
b76e467d 5351 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5352 {
b98664d3 5353 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5354 " [in module %s]"),
5355 pulongest (ull),
5356 objfile_name (dwarf2_per_objfile->objfile));
5357 continue;
5358 }
ff4c9fec 5359 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5360 break;
8af5c486
JK
5361 case DW_IDX_type_unit:
5362 /* Don't crash on bad data. */
b2bdb8cf 5363 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5364 {
b98664d3 5365 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5366 " [in module %s]"),
5367 pulongest (ull),
5368 objfile_name (dwarf2_per_objfile->objfile));
5369 continue;
5370 }
ff4c9fec 5371 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5372 break;
927aa2e7
JK
5373 case DW_IDX_GNU_internal:
5374 if (!m_map.augmentation_is_gdb)
5375 break;
23c13d42 5376 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5377 break;
5378 case DW_IDX_GNU_external:
5379 if (!m_map.augmentation_is_gdb)
5380 break;
23c13d42 5381 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5382 break;
5383 }
5384 }
5385
5386 /* Skip if already read in. */
5387 if (per_cu->v.quick->compunit_symtab)
5388 goto again;
5389
5390 /* Check static vs global. */
23c13d42 5391 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5392 {
2b79f376 5393 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5394 const bool symbol_is_static =
5395 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5396 if (want_static != symbol_is_static)
2b79f376 5397 goto again;
927aa2e7
JK
5398 }
5399
5400 /* Match dw2_symtab_iter_next, symbol_kind
5401 and debug_names::psymbol_tag. */
5402 switch (m_domain)
5403 {
5404 case VAR_DOMAIN:
5405 switch (indexval.dwarf_tag)
5406 {
5407 case DW_TAG_variable:
5408 case DW_TAG_subprogram:
5409 /* Some types are also in VAR_DOMAIN. */
5410 case DW_TAG_typedef:
5411 case DW_TAG_structure_type:
5412 break;
5413 default:
5414 goto again;
5415 }
5416 break;
5417 case STRUCT_DOMAIN:
5418 switch (indexval.dwarf_tag)
5419 {
5420 case DW_TAG_typedef:
5421 case DW_TAG_structure_type:
5422 break;
5423 default:
5424 goto again;
5425 }
5426 break;
5427 case LABEL_DOMAIN:
5428 switch (indexval.dwarf_tag)
5429 {
5430 case 0:
5431 case DW_TAG_variable:
5432 break;
5433 default:
5434 goto again;
5435 }
5436 break;
59c35742
AB
5437 case MODULE_DOMAIN:
5438 switch (indexval.dwarf_tag)
5439 {
5440 case DW_TAG_module:
5441 break;
5442 default:
5443 goto again;
5444 }
5445 break;
927aa2e7
JK
5446 default:
5447 break;
5448 }
5449
5450 /* Match dw2_expand_symtabs_matching, symbol_kind and
5451 debug_names::psymbol_tag. */
5452 switch (m_search)
4b514bc8 5453 {
927aa2e7
JK
5454 case VARIABLES_DOMAIN:
5455 switch (indexval.dwarf_tag)
4b514bc8 5456 {
927aa2e7
JK
5457 case DW_TAG_variable:
5458 break;
5459 default:
5460 goto again;
4b514bc8 5461 }
927aa2e7
JK
5462 break;
5463 case FUNCTIONS_DOMAIN:
5464 switch (indexval.dwarf_tag)
4b514bc8 5465 {
927aa2e7
JK
5466 case DW_TAG_subprogram:
5467 break;
5468 default:
5469 goto again;
4b514bc8 5470 }
927aa2e7
JK
5471 break;
5472 case TYPES_DOMAIN:
5473 switch (indexval.dwarf_tag)
5474 {
5475 case DW_TAG_typedef:
5476 case DW_TAG_structure_type:
5477 break;
5478 default:
5479 goto again;
5480 }
5481 break;
59c35742
AB
5482 case MODULES_DOMAIN:
5483 switch (indexval.dwarf_tag)
5484 {
5485 case DW_TAG_module:
5486 break;
5487 default:
5488 goto again;
5489 }
927aa2e7
JK
5490 default:
5491 break;
4b514bc8 5492 }
927aa2e7
JK
5493
5494 return per_cu;
4b514bc8 5495}
61920122 5496
927aa2e7 5497static struct compunit_symtab *
c7f839cb 5498dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5499 const char *name, domain_enum domain)
4b514bc8 5500{
ed2dc618
SM
5501 struct dwarf2_per_objfile *dwarf2_per_objfile
5502 = get_dwarf2_per_objfile (objfile);
61920122 5503
927aa2e7
JK
5504 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5505 if (!mapp)
61920122 5506 {
927aa2e7
JK
5507 /* index is NULL if OBJF_READNOW. */
5508 return NULL;
5509 }
5510 const auto &map = *mapp;
9291a0cd 5511
2b79f376 5512 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5513
927aa2e7
JK
5514 struct compunit_symtab *stab_best = NULL;
5515 struct dwarf2_per_cu_data *per_cu;
5516 while ((per_cu = iter.next ()) != NULL)
5517 {
5518 struct symbol *sym, *with_opaque = NULL;
58f0c718 5519 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5520 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5521 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5522
927aa2e7
JK
5523 sym = block_find_symbol (block, name, domain,
5524 block_find_non_opaque_type_preferred,
5525 &with_opaque);
9703b513 5526
927aa2e7
JK
5527 /* Some caution must be observed with overloaded functions and
5528 methods, since the index will not contain any overload
5529 information (but NAME might contain it). */
a3ec0bb1 5530
927aa2e7 5531 if (sym != NULL
987012b8 5532 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5533 return stab;
5534 if (with_opaque != NULL
987012b8 5535 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5536 stab_best = stab;
9703b513 5537
927aa2e7 5538 /* Keep looking through other CUs. */
9703b513
TT
5539 }
5540
927aa2e7 5541 return stab_best;
9703b513
TT
5542}
5543
927aa2e7
JK
5544/* This dumps minimal information about .debug_names. It is called
5545 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5546 uses this to verify that .debug_names has been loaded. */
9291a0cd 5547
927aa2e7
JK
5548static void
5549dw2_debug_names_dump (struct objfile *objfile)
5550{
ed2dc618
SM
5551 struct dwarf2_per_objfile *dwarf2_per_objfile
5552 = get_dwarf2_per_objfile (objfile);
5553
927aa2e7
JK
5554 gdb_assert (dwarf2_per_objfile->using_index);
5555 printf_filtered (".debug_names:");
5556 if (dwarf2_per_objfile->debug_names_table)
5557 printf_filtered (" exists\n");
5558 else
5559 printf_filtered (" faked for \"readnow\"\n");
5560 printf_filtered ("\n");
9291a0cd
TT
5561}
5562
9291a0cd 5563static void
927aa2e7
JK
5564dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5565 const char *func_name)
9291a0cd 5566{
ed2dc618
SM
5567 struct dwarf2_per_objfile *dwarf2_per_objfile
5568 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5569
927aa2e7
JK
5570 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5571 if (dwarf2_per_objfile->debug_names_table)
24c79950 5572 {
927aa2e7 5573 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5574
2b79f376 5575 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5576
927aa2e7
JK
5577 struct dwarf2_per_cu_data *per_cu;
5578 while ((per_cu = iter.next ()) != NULL)
58f0c718 5579 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5580 }
5581}
24c79950 5582
3b00ef10
TT
5583static void
5584dw2_debug_names_map_matching_symbols
5585 (struct objfile *objfile,
5586 const lookup_name_info &name, domain_enum domain,
5587 int global,
5588 gdb::function_view<symbol_found_callback_ftype> callback,
5589 symbol_compare_ftype *ordered_compare)
5590{
5591 struct dwarf2_per_objfile *dwarf2_per_objfile
5592 = get_dwarf2_per_objfile (objfile);
5593
5594 /* debug_names_table is NULL if OBJF_READNOW. */
5595 if (!dwarf2_per_objfile->debug_names_table)
5596 return;
5597
5598 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5599 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5600
5601 const char *match_name = name.ada ().lookup_name ().c_str ();
5602 auto matcher = [&] (const char *symname)
5603 {
5604 if (ordered_compare == nullptr)
5605 return true;
5606 return ordered_compare (symname, match_name) == 0;
5607 };
5608
5609 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5610 [&] (offset_type namei)
5611 {
5612 /* The name was matched, now expand corresponding CUs that were
5613 marked. */
5614 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5615
5616 struct dwarf2_per_cu_data *per_cu;
5617 while ((per_cu = iter.next ()) != NULL)
5618 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5619 return true;
5620 });
5621
5622 /* It's a shame we couldn't do this inside the
5623 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5624 that have already been expanded. Instead, this loop matches what
5625 the psymtab code does. */
5626 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5627 {
5628 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5629 if (cust != nullptr)
5630 {
5631 const struct block *block
5632 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5633 if (!iterate_over_symbols_terminated (block, name,
5634 domain, callback))
5635 break;
5636 }
5637 }
5638}
5639
927aa2e7
JK
5640static void
5641dw2_debug_names_expand_symtabs_matching
5642 (struct objfile *objfile,
5643 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5644 const lookup_name_info &lookup_name,
5645 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5646 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5647 enum search_domain kind)
5648{
ed2dc618
SM
5649 struct dwarf2_per_objfile *dwarf2_per_objfile
5650 = get_dwarf2_per_objfile (objfile);
9291a0cd 5651
927aa2e7
JK
5652 /* debug_names_table is NULL if OBJF_READNOW. */
5653 if (!dwarf2_per_objfile->debug_names_table)
5654 return;
9291a0cd 5655
ed2dc618 5656 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5657
44ed8f3e 5658 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5659
44ed8f3e
PA
5660 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5661 symbol_matcher,
5662 kind, [&] (offset_type namei)
927aa2e7 5663 {
927aa2e7
JK
5664 /* The name was matched, now expand corresponding CUs that were
5665 marked. */
5666 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5667
927aa2e7
JK
5668 struct dwarf2_per_cu_data *per_cu;
5669 while ((per_cu = iter.next ()) != NULL)
5670 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5671 expansion_notify);
3b00ef10 5672 return true;
44ed8f3e 5673 });
9291a0cd
TT
5674}
5675
927aa2e7 5676const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5677{
5678 dw2_has_symbols,
5679 dw2_find_last_source_symtab,
5680 dw2_forget_cached_source_info,
f8eba3c6 5681 dw2_map_symtabs_matching_filename,
927aa2e7 5682 dw2_debug_names_lookup_symbol,
9291a0cd 5683 dw2_print_stats,
927aa2e7 5684 dw2_debug_names_dump,
927aa2e7 5685 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5686 dw2_expand_all_symtabs,
652a8996 5687 dw2_expand_symtabs_with_fullname,
3b00ef10 5688 dw2_debug_names_map_matching_symbols,
927aa2e7 5689 dw2_debug_names_expand_symtabs_matching,
43f3e411 5690 dw2_find_pc_sect_compunit_symtab,
71a3c369 5691 NULL,
9291a0cd
TT
5692 dw2_map_symbol_filenames
5693};
5694
4485a1c1
SM
5695/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5696 to either a dwarf2_per_objfile or dwz_file object. */
5697
5698template <typename T>
5699static gdb::array_view<const gdb_byte>
5700get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5701{
5702 dwarf2_section_info *section = &section_owner->gdb_index;
5703
96b79293 5704 if (section->empty ())
4485a1c1
SM
5705 return {};
5706
5707 /* Older elfutils strip versions could keep the section in the main
5708 executable while splitting it for the separate debug info file. */
96b79293 5709 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5710 return {};
5711
96b79293 5712 section->read (obj);
4485a1c1 5713
8bebfcda
PA
5714 /* dwarf2_section_info::size is a bfd_size_type, while
5715 gdb::array_view works with size_t. On 32-bit hosts, with
5716 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5717 is 32-bit. So we need an explicit narrowing conversion here.
5718 This is fine, because it's impossible to allocate or mmap an
5719 array/buffer larger than what size_t can represent. */
5720 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5721}
5722
87d6a7aa
SM
5723/* Lookup the index cache for the contents of the index associated to
5724 DWARF2_OBJ. */
5725
5726static gdb::array_view<const gdb_byte>
5727get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5728{
5729 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5730 if (build_id == nullptr)
5731 return {};
5732
5733 return global_index_cache.lookup_gdb_index (build_id,
5734 &dwarf2_obj->index_cache_res);
5735}
5736
5737/* Same as the above, but for DWZ. */
5738
5739static gdb::array_view<const gdb_byte>
5740get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5741{
5742 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5743 if (build_id == nullptr)
5744 return {};
5745
5746 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5747}
5748
3c0aa29a 5749/* See symfile.h. */
9291a0cd 5750
3c0aa29a
PA
5751bool
5752dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5753{
ed2dc618
SM
5754 struct dwarf2_per_objfile *dwarf2_per_objfile
5755 = get_dwarf2_per_objfile (objfile);
5756
9291a0cd
TT
5757 /* If we're about to read full symbols, don't bother with the
5758 indices. In this case we also don't care if some other debug
5759 format is making psymtabs, because they are all about to be
5760 expanded anyway. */
5761 if ((objfile->flags & OBJF_READNOW))
5762 {
9291a0cd 5763 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5764 create_all_comp_units (dwarf2_per_objfile);
5765 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5766 dwarf2_per_objfile->quick_file_names_table
5767 = create_quick_file_names_table
5768 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5769
b76e467d 5770 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5771 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5772 {
ff4c9fec 5773 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5774
e254ef6a
DE
5775 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5776 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5777 }
5778
5779 /* Return 1 so that gdb sees the "quick" functions. However,
5780 these functions will be no-ops because we will have expanded
5781 all symtabs. */
3c0aa29a
PA
5782 *index_kind = dw_index_kind::GDB_INDEX;
5783 return true;
9291a0cd
TT
5784 }
5785
ed2dc618 5786 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5787 {
5788 *index_kind = dw_index_kind::DEBUG_NAMES;
5789 return true;
5790 }
927aa2e7 5791
4485a1c1
SM
5792 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5793 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5794 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5795 {
5796 *index_kind = dw_index_kind::GDB_INDEX;
5797 return true;
5798 }
9291a0cd 5799
87d6a7aa
SM
5800 /* ... otherwise, try to find the index in the index cache. */
5801 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5802 get_gdb_index_contents_from_cache,
5803 get_gdb_index_contents_from_cache_dwz))
5804 {
5805 global_index_cache.hit ();
5806 *index_kind = dw_index_kind::GDB_INDEX;
5807 return true;
5808 }
5809
5810 global_index_cache.miss ();
3c0aa29a 5811 return false;
9291a0cd
TT
5812}
5813
5814\f
5815
dce234bc
PP
5816/* Build a partial symbol table. */
5817
5818void
f29dff0a 5819dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5820{
ed2dc618
SM
5821 struct dwarf2_per_objfile *dwarf2_per_objfile
5822 = get_dwarf2_per_objfile (objfile);
c9bf0622 5823
6eee24ce 5824 init_psymbol_list (objfile, 1024);
c906108c 5825
a70b8144 5826 try
c9bf0622
TT
5827 {
5828 /* This isn't really ideal: all the data we allocate on the
5829 objfile's obstack is still uselessly kept around. However,
5830 freeing it seems unsafe. */
906768f9 5831 psymtab_discarder psymtabs (objfile);
ed2dc618 5832 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5833 psymtabs.keep ();
87d6a7aa
SM
5834
5835 /* (maybe) store an index in the cache. */
5836 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5837 }
230d2906 5838 catch (const gdb_exception_error &except)
492d29ea
PA
5839 {
5840 exception_print (gdb_stderr, except);
5841 }
c906108c 5842}
c906108c 5843
3b80fe9b
DE
5844/* Find the base address of the compilation unit for range lists and
5845 location lists. It will normally be specified by DW_AT_low_pc.
5846 In DWARF-3 draft 4, the base address could be overridden by
5847 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5848 compilation units with discontinuous ranges. */
5849
5850static void
5851dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5852{
5853 struct attribute *attr;
5854
5855 cu->base_known = 0;
5856 cu->base_address = 0;
5857
5858 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5859 if (attr != nullptr)
3b80fe9b 5860 {
cd6c91b4 5861 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5862 cu->base_known = 1;
5863 }
5864 else
5865 {
5866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5867 if (attr != nullptr)
3b80fe9b 5868 {
cd6c91b4 5869 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5870 cu->base_known = 1;
5871 }
5872 }
5873}
5874
36586728
TT
5875/* Helper function that returns the proper abbrev section for
5876 THIS_CU. */
5877
5878static struct dwarf2_section_info *
5879get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5880{
5881 struct dwarf2_section_info *abbrev;
ed2dc618 5882 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5883
5884 if (this_cu->is_dwz)
ed2dc618 5885 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5886 else
5887 abbrev = &dwarf2_per_objfile->abbrev;
5888
5889 return abbrev;
5890}
5891
f4dc4d17
DE
5892/* Fetch the abbreviation table offset from a comp or type unit header. */
5893
5894static sect_offset
ed2dc618
SM
5895read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5896 struct dwarf2_section_info *section,
9c541725 5897 sect_offset sect_off)
f4dc4d17 5898{
96b79293 5899 bfd *abfd = section->get_bfd_owner ();
d521ce57 5900 const gdb_byte *info_ptr;
ac298888 5901 unsigned int initial_length_size, offset_size;
43988095 5902 uint16_t version;
f4dc4d17 5903
96b79293 5904 section->read (dwarf2_per_objfile->objfile);
9c541725 5905 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5906 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5907 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5908 info_ptr += initial_length_size;
5909
5910 version = read_2_bytes (abfd, info_ptr);
5911 info_ptr += 2;
5912 if (version >= 5)
5913 {
5914 /* Skip unit type and address size. */
5915 info_ptr += 2;
5916 }
5917
24aa364d 5918 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5919}
5920
b83470bf
TT
5921/* A partial symtab that is used only for include files. */
5922struct dwarf2_include_psymtab : public partial_symtab
5923{
5924 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5925 : partial_symtab (filename, objfile)
5926 {
5927 }
5928
5929 void read_symtab (struct objfile *objfile) override
5930 {
5931 expand_psymtab (objfile);
5932 }
5933
5934 void expand_psymtab (struct objfile *objfile) override
5935 {
5936 if (m_readin)
5937 return;
5938 /* It's an include file, no symbols to read for it.
5939 Everything is in the parent symtab. */
5940 read_dependencies (objfile);
5941 m_readin = true;
5942 }
5943
5944 bool readin_p () const override
5945 {
5946 return m_readin;
5947 }
5948
5949 struct compunit_symtab *get_compunit_symtab () const override
5950 {
5951 return nullptr;
5952 }
5953
5954private:
5955
5956 bool m_readin = false;
5957};
5958
aaa75496
JB
5959/* Allocate a new partial symtab for file named NAME and mark this new
5960 partial symtab as being an include of PST. */
5961
5962static void
891813be 5963dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5964 struct objfile *objfile)
5965{
b83470bf 5966 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 5967
fbd9ab74
JK
5968 if (!IS_ABSOLUTE_PATH (subpst->filename))
5969 {
5970 /* It shares objfile->objfile_obstack. */
5971 subpst->dirname = pst->dirname;
5972 }
5973
a9342b62 5974 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5975 subpst->dependencies[0] = pst;
5976 subpst->number_of_dependencies = 1;
aaa75496
JB
5977}
5978
5979/* Read the Line Number Program data and extract the list of files
5980 included by the source file represented by PST. Build an include
d85a05f0 5981 partial symtab for each of these included files. */
aaa75496
JB
5982
5983static void
5984dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5985 struct die_info *die,
891813be 5986 dwarf2_psymtab *pst)
aaa75496 5987{
fff8551c 5988 line_header_up lh;
d85a05f0 5989 struct attribute *attr;
aaa75496 5990
d85a05f0 5991 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5992 if (attr != nullptr)
9c541725 5993 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5994 if (lh == NULL)
5995 return; /* No linetable, so no includes. */
5996
79748972
TT
5997 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5998 that we pass in the raw text_low here; that is ok because we're
5999 only decoding the line table to make include partial symtabs, and
6000 so the addresses aren't really used. */
4ae976d1 6001 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6002 pst->raw_text_low (), 1);
aaa75496
JB
6003}
6004
348e048f 6005static hashval_t
52dc124a 6006hash_signatured_type (const void *item)
348e048f 6007{
9a3c8263
SM
6008 const struct signatured_type *sig_type
6009 = (const struct signatured_type *) item;
9a619af0 6010
348e048f 6011 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6012 return sig_type->signature;
348e048f
DE
6013}
6014
6015static int
52dc124a 6016eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6017{
9a3c8263
SM
6018 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6019 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6020
348e048f
DE
6021 return lhs->signature == rhs->signature;
6022}
6023
1fd400ff
TT
6024/* Allocate a hash table for signatured types. */
6025
b0b6a987 6026static htab_up
298e9637 6027allocate_signatured_type_table ()
1fd400ff 6028{
b0b6a987
TT
6029 return htab_up (htab_create_alloc (41,
6030 hash_signatured_type,
6031 eq_signatured_type,
6032 NULL, xcalloc, xfree));
1fd400ff
TT
6033}
6034
d467dd73 6035/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6036
6037static int
d467dd73 6038add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6039{
9a3c8263 6040 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6041 std::vector<signatured_type *> *all_type_units
6042 = (std::vector<signatured_type *> *) datum;
1fd400ff 6043
b2bdb8cf 6044 all_type_units->push_back (sigt);
1fd400ff
TT
6045
6046 return 1;
6047}
6048
78d4d2c5 6049/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6050 and fill them into TYPES_HTAB. It will process only type units,
6051 therefore DW_UT_type. */
c88ee1f0 6052
78d4d2c5 6053static void
ed2dc618
SM
6054create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6055 struct dwo_file *dwo_file,
b0b6a987 6056 dwarf2_section_info *section, htab_up &types_htab,
43988095 6057 rcuh_kind section_kind)
348e048f 6058{
3019eac3 6059 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6060 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6061 bfd *abfd;
6062 const gdb_byte *info_ptr, *end_ptr;
348e048f 6063
4bdcc0c1
DE
6064 abbrev_section = (dwo_file != NULL
6065 ? &dwo_file->sections.abbrev
6066 : &dwarf2_per_objfile->abbrev);
6067
b4f54984 6068 if (dwarf_read_debug)
43988095 6069 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6070 section->get_name (),
6071 abbrev_section->get_file_name ());
09406207 6072
96b79293 6073 section->read (objfile);
78d4d2c5 6074 info_ptr = section->buffer;
348e048f 6075
78d4d2c5
JK
6076 if (info_ptr == NULL)
6077 return;
348e048f 6078
78d4d2c5
JK
6079 /* We can't set abfd until now because the section may be empty or
6080 not present, in which case the bfd is unknown. */
96b79293 6081 abfd = section->get_bfd_owner ();
348e048f 6082
c0ab21c2
TT
6083 /* We don't use cutu_reader here because we don't need to read
6084 any dies: the signature is in the header. */
3019eac3 6085
78d4d2c5
JK
6086 end_ptr = info_ptr + section->size;
6087 while (info_ptr < end_ptr)
6088 {
78d4d2c5
JK
6089 struct signatured_type *sig_type;
6090 struct dwo_unit *dwo_tu;
6091 void **slot;
6092 const gdb_byte *ptr = info_ptr;
6093 struct comp_unit_head header;
6094 unsigned int length;
8b70b953 6095
9c541725 6096 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6097
a49dd8dd
JK
6098 /* Initialize it due to a false compiler warning. */
6099 header.signature = -1;
9c541725 6100 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6101
78d4d2c5
JK
6102 /* We need to read the type's signature in order to build the hash
6103 table, but we don't need anything else just yet. */
348e048f 6104
ed2dc618 6105 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6106 abbrev_section, ptr, section_kind);
348e048f 6107
4057dfde 6108 length = header.get_length ();
6caca83c 6109
78d4d2c5
JK
6110 /* Skip dummy type units. */
6111 if (ptr >= info_ptr + length
43988095
JK
6112 || peek_abbrev_code (abfd, ptr) == 0
6113 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6114 {
6115 info_ptr += length;
6116 continue;
6117 }
dee91e82 6118
78d4d2c5
JK
6119 if (types_htab == NULL)
6120 {
6121 if (dwo_file)
298e9637 6122 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6123 else
298e9637 6124 types_htab = allocate_signatured_type_table ();
78d4d2c5 6125 }
8b70b953 6126
78d4d2c5
JK
6127 if (dwo_file)
6128 {
6129 sig_type = NULL;
6130 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6131 struct dwo_unit);
6132 dwo_tu->dwo_file = dwo_file;
43988095 6133 dwo_tu->signature = header.signature;
9c541725 6134 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6135 dwo_tu->section = section;
9c541725 6136 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6137 dwo_tu->length = length;
6138 }
6139 else
6140 {
6141 /* N.B.: type_offset is not usable if this type uses a DWO file.
6142 The real type_offset is in the DWO file. */
6143 dwo_tu = NULL;
6144 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6145 struct signatured_type);
43988095 6146 sig_type->signature = header.signature;
9c541725 6147 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6148 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6149 sig_type->per_cu.is_debug_types = 1;
6150 sig_type->per_cu.section = section;
9c541725 6151 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6152 sig_type->per_cu.length = length;
6153 }
6154
b0b6a987 6155 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6156 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6157 INSERT);
6158 gdb_assert (slot != NULL);
6159 if (*slot != NULL)
6160 {
9c541725 6161 sect_offset dup_sect_off;
0349ea22 6162
3019eac3
DE
6163 if (dwo_file)
6164 {
78d4d2c5
JK
6165 const struct dwo_unit *dup_tu
6166 = (const struct dwo_unit *) *slot;
6167
9c541725 6168 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6169 }
6170 else
6171 {
78d4d2c5
JK
6172 const struct signatured_type *dup_tu
6173 = (const struct signatured_type *) *slot;
6174
9c541725 6175 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6176 }
8b70b953 6177
b98664d3 6178 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6179 " the entry at offset %s, signature %s"),
6180 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6181 hex_string (header.signature));
78d4d2c5
JK
6182 }
6183 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6184
78d4d2c5 6185 if (dwarf_read_debug > 1)
9d8780f0
SM
6186 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6187 sect_offset_str (sect_off),
43988095 6188 hex_string (header.signature));
3019eac3 6189
78d4d2c5
JK
6190 info_ptr += length;
6191 }
6192}
3019eac3 6193
78d4d2c5
JK
6194/* Create the hash table of all entries in the .debug_types
6195 (or .debug_types.dwo) section(s).
6196 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6197 otherwise it is NULL.
b3c8eb43 6198
78d4d2c5 6199 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6200
78d4d2c5 6201 Note: This function processes DWO files only, not DWP files. */
348e048f 6202
78d4d2c5 6203static void
ed2dc618
SM
6204create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6205 struct dwo_file *dwo_file,
fd5866f6 6206 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6207 htab_up &types_htab)
78d4d2c5 6208{
fd5866f6
SM
6209 for (dwarf2_section_info &section : type_sections)
6210 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6211 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6212}
6213
6214/* Create the hash table of all entries in the .debug_types section,
6215 and initialize all_type_units.
6216 The result is zero if there is an error (e.g. missing .debug_types section),
6217 otherwise non-zero. */
6218
6219static int
ed2dc618 6220create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6221{
b0b6a987 6222 htab_up types_htab;
3019eac3 6223
ed2dc618
SM
6224 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6225 &dwarf2_per_objfile->info, types_htab,
43988095 6226 rcuh_kind::COMPILE);
ed2dc618
SM
6227 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6228 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6229 if (types_htab == NULL)
6230 {
6231 dwarf2_per_objfile->signatured_types = NULL;
6232 return 0;
6233 }
6234
b0b6a987 6235 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6236
b2bdb8cf 6237 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6238 dwarf2_per_objfile->all_type_units.reserve
6239 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6240
b0b6a987
TT
6241 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6242 add_signatured_type_cu_to_table,
b2bdb8cf 6243 &dwarf2_per_objfile->all_type_units);
1fd400ff 6244
348e048f
DE
6245 return 1;
6246}
6247
6aa5f3a6
DE
6248/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6249 If SLOT is non-NULL, it is the entry to use in the hash table.
6250 Otherwise we find one. */
6251
6252static struct signatured_type *
ed2dc618
SM
6253add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6254 void **slot)
6aa5f3a6
DE
6255{
6256 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6257
b2bdb8cf
SM
6258 if (dwarf2_per_objfile->all_type_units.size ()
6259 == dwarf2_per_objfile->all_type_units.capacity ())
6260 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6261
b2bdb8cf
SM
6262 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6263 struct signatured_type);
6264
6265 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6266 sig_type->signature = sig;
6267 sig_type->per_cu.is_debug_types = 1;
6268 if (dwarf2_per_objfile->using_index)
6269 {
6270 sig_type->per_cu.v.quick =
6271 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6272 struct dwarf2_per_cu_quick_data);
6273 }
6274
6275 if (slot == NULL)
6276 {
b0b6a987 6277 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6278 sig_type, INSERT);
6279 }
6280 gdb_assert (*slot == NULL);
6281 *slot = sig_type;
6282 /* The rest of sig_type must be filled in by the caller. */
6283 return sig_type;
6284}
6285
a2ce51a0
DE
6286/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6287 Fill in SIG_ENTRY with DWO_ENTRY. */
6288
6289static void
ed2dc618 6290fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6291 struct signatured_type *sig_entry,
6292 struct dwo_unit *dwo_entry)
6293{
7ee85ab1 6294 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6295 gdb_assert (! sig_entry->per_cu.queued);
6296 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6297 if (dwarf2_per_objfile->using_index)
6298 {
6299 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6300 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6301 }
6302 else
6303 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6304 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6305 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6306 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6307 gdb_assert (sig_entry->dwo_unit == NULL);
6308
6309 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6310 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6311 sig_entry->per_cu.length = dwo_entry->length;
6312 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6313 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6314 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6315 sig_entry->dwo_unit = dwo_entry;
6316}
6317
6318/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6319 If we haven't read the TU yet, create the signatured_type data structure
6320 for a TU to be read in directly from a DWO file, bypassing the stub.
6321 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6322 using .gdb_index, then when reading a CU we want to stay in the DWO file
6323 containing that CU. Otherwise we could end up reading several other DWO
6324 files (due to comdat folding) to process the transitive closure of all the
6325 mentioned TUs, and that can be slow. The current DWO file will have every
6326 type signature that it needs.
a2ce51a0
DE
6327 We only do this for .gdb_index because in the psymtab case we already have
6328 to read all the DWOs to build the type unit groups. */
6329
6330static struct signatured_type *
6331lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6332{
518817b3
SM
6333 struct dwarf2_per_objfile *dwarf2_per_objfile
6334 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6335 struct dwo_file *dwo_file;
6336 struct dwo_unit find_dwo_entry, *dwo_entry;
6337 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6338 void **slot;
a2ce51a0
DE
6339
6340 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6341
6aa5f3a6
DE
6342 /* If TU skeletons have been removed then we may not have read in any
6343 TUs yet. */
6344 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6345 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6346
6347 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6348 Use the global signatured_types array to do our own comdat-folding
6349 of types. If this is the first time we're reading this TU, and
6350 the TU has an entry in .gdb_index, replace the recorded data from
6351 .gdb_index with this TU. */
a2ce51a0 6352
a2ce51a0 6353 find_sig_entry.signature = sig;
b0b6a987 6354 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6355 &find_sig_entry, INSERT);
9a3c8263 6356 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6357
6358 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6359 read. Don't reassign the global entry to point to this DWO if that's
6360 the case. Also note that if the TU is already being read, it may not
6361 have come from a DWO, the program may be a mix of Fission-compiled
6362 code and non-Fission-compiled code. */
6363
6364 /* Have we already tried to read this TU?
6365 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6366 needn't exist in the global table yet). */
6367 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6368 return sig_entry;
6369
6aa5f3a6
DE
6370 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6371 dwo_unit of the TU itself. */
6372 dwo_file = cu->dwo_unit->dwo_file;
6373
a2ce51a0
DE
6374 /* Ok, this is the first time we're reading this TU. */
6375 if (dwo_file->tus == NULL)
6376 return NULL;
6377 find_dwo_entry.signature = sig;
b0b6a987
TT
6378 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6379 &find_dwo_entry);
a2ce51a0
DE
6380 if (dwo_entry == NULL)
6381 return NULL;
6382
6aa5f3a6
DE
6383 /* If the global table doesn't have an entry for this TU, add one. */
6384 if (sig_entry == NULL)
ed2dc618 6385 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6386
ed2dc618 6387 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6388 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6389 return sig_entry;
6390}
6391
a2ce51a0
DE
6392/* Subroutine of lookup_signatured_type.
6393 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6394 then try the DWP file. If the TU stub (skeleton) has been removed then
6395 it won't be in .gdb_index. */
a2ce51a0
DE
6396
6397static struct signatured_type *
6398lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6399{
518817b3
SM
6400 struct dwarf2_per_objfile *dwarf2_per_objfile
6401 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6402 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6403 struct dwo_unit *dwo_entry;
6404 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6405 void **slot;
a2ce51a0
DE
6406
6407 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6408 gdb_assert (dwp_file != NULL);
6409
6aa5f3a6
DE
6410 /* If TU skeletons have been removed then we may not have read in any
6411 TUs yet. */
6412 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6413 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6414
6aa5f3a6 6415 find_sig_entry.signature = sig;
b0b6a987 6416 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6417 &find_sig_entry, INSERT);
9a3c8263 6418 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6419
6420 /* Have we already tried to read this TU?
6421 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6422 needn't exist in the global table yet). */
6423 if (sig_entry != NULL)
6424 return sig_entry;
6425
a2ce51a0
DE
6426 if (dwp_file->tus == NULL)
6427 return NULL;
ed2dc618 6428 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6429 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6430 if (dwo_entry == NULL)
6431 return NULL;
6432
ed2dc618
SM
6433 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6434 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6435
a2ce51a0
DE
6436 return sig_entry;
6437}
6438
380bca97 6439/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6440 Returns NULL if signature SIG is not present in the table.
6441 It is up to the caller to complain about this. */
348e048f
DE
6442
6443static struct signatured_type *
a2ce51a0 6444lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6445{
518817b3
SM
6446 struct dwarf2_per_objfile *dwarf2_per_objfile
6447 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6448
a2ce51a0
DE
6449 if (cu->dwo_unit
6450 && dwarf2_per_objfile->using_index)
6451 {
6452 /* We're in a DWO/DWP file, and we're using .gdb_index.
6453 These cases require special processing. */
ed2dc618 6454 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6455 return lookup_dwo_signatured_type (cu, sig);
6456 else
6457 return lookup_dwp_signatured_type (cu, sig);
6458 }
6459 else
6460 {
6461 struct signatured_type find_entry, *entry;
348e048f 6462
a2ce51a0
DE
6463 if (dwarf2_per_objfile->signatured_types == NULL)
6464 return NULL;
6465 find_entry.signature = sig;
9a3c8263 6466 entry = ((struct signatured_type *)
b0b6a987
TT
6467 htab_find (dwarf2_per_objfile->signatured_types.get (),
6468 &find_entry));
a2ce51a0
DE
6469 return entry;
6470 }
348e048f 6471}
18a8505e
AT
6472
6473/* Return the address base of the compile unit, which, if exists, is stored
6474 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6475static gdb::optional<ULONGEST>
6476lookup_addr_base (struct die_info *comp_unit_die)
6477{
6478 struct attribute *attr;
6479 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6480 if (attr == nullptr)
6481 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6482 if (attr == nullptr)
6483 return gdb::optional<ULONGEST> ();
6484 return DW_UNSND (attr);
6485}
6486
6487/* Return range lists base of the compile unit, which, if exists, is stored
6488 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6489static ULONGEST
6490lookup_ranges_base (struct die_info *comp_unit_die)
6491{
6492 struct attribute *attr;
6493 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6494 if (attr == nullptr)
6495 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6496 if (attr == nullptr)
6497 return 0;
6498 return DW_UNSND (attr);
6499}
6500
42e7ad6c 6501/* Low level DIE reading support. */
348e048f 6502
d85a05f0
DJ
6503/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6504
6505static void
6506init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6507 struct dwarf2_cu *cu,
3019eac3 6508 struct dwarf2_section_info *section,
685af9cd
TT
6509 struct dwo_file *dwo_file,
6510 struct abbrev_table *abbrev_table)
d85a05f0 6511{
fceca515 6512 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6513 reader->abfd = section->get_bfd_owner ();
d85a05f0 6514 reader->cu = cu;
3019eac3 6515 reader->dwo_file = dwo_file;
dee91e82
DE
6516 reader->die_section = section;
6517 reader->buffer = section->buffer;
f664829e 6518 reader->buffer_end = section->buffer + section->size;
685af9cd 6519 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6520}
6521
c0ab21c2 6522/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6523 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6524 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6525 already.
6526
6527 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6528 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6529 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6530 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6531 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6532 STUB_COMP_DIR may be non-NULL.
3e225074 6533 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6534 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6535 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6536 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6537 kept around for at least as long as *RESULT_READER.
6538
b0c7bfa9
DE
6539 The result is non-zero if a valid (non-dummy) DIE was found. */
6540
6541static int
6542read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6543 struct dwo_unit *dwo_unit,
b0c7bfa9 6544 struct die_info *stub_comp_unit_die,
a2ce51a0 6545 const char *stub_comp_dir,
b0c7bfa9 6546 struct die_reader_specs *result_reader,
d521ce57 6547 const gdb_byte **result_info_ptr,
b0c7bfa9 6548 struct die_info **result_comp_unit_die,
685af9cd 6549 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6550{
ed2dc618 6551 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6552 struct objfile *objfile = dwarf2_per_objfile->objfile;
6553 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6554 bfd *abfd;
d521ce57 6555 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6556 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6557 int i,num_extra_attrs;
6558 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6559 struct die_info *comp_unit_die;
6560
b0aeadb3
DE
6561 /* At most one of these may be provided. */
6562 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6563
b0c7bfa9
DE
6564 /* These attributes aren't processed until later:
6565 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6566 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6567 referenced later. However, these attributes are found in the stub
6568 which we won't have later. In order to not impose this complication
6569 on the rest of the code, we read them here and copy them to the
6570 DWO CU/TU die. */
b0c7bfa9
DE
6571
6572 stmt_list = NULL;
6573 low_pc = NULL;
6574 high_pc = NULL;
6575 ranges = NULL;
6576 comp_dir = NULL;
6577
6578 if (stub_comp_unit_die != NULL)
6579 {
6580 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6581 DWO file. */
6582 if (! this_cu->is_debug_types)
6583 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6584 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6585 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6586 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6587 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6588
18a8505e 6589 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6590
18a8505e
AT
6591 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6592 here (if needed). We need the value before we can process
6593 DW_AT_ranges. */
6594 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6595 }
a2ce51a0
DE
6596 else if (stub_comp_dir != NULL)
6597 {
6598 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6599 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6600 comp_dir->name = DW_AT_comp_dir;
6601 comp_dir->form = DW_FORM_string;
6602 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6603 DW_STRING (comp_dir) = stub_comp_dir;
6604 }
b0c7bfa9
DE
6605
6606 /* Set up for reading the DWO CU/TU. */
6607 cu->dwo_unit = dwo_unit;
685af9cd 6608 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6609 section->read (objfile);
6610 abfd = section->get_bfd_owner ();
9c541725
PA
6611 begin_info_ptr = info_ptr = (section->buffer
6612 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6613 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6614
6615 if (this_cu->is_debug_types)
6616 {
b0c7bfa9
DE
6617 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6618
ed2dc618
SM
6619 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6620 &cu->header, section,
b0c7bfa9 6621 dwo_abbrev_section,
43988095 6622 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6623 /* This is not an assert because it can be caused by bad debug info. */
43988095 6624 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6625 {
6626 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6627 " TU at offset %s [in module %s]"),
a2ce51a0 6628 hex_string (sig_type->signature),
43988095 6629 hex_string (cu->header.signature),
9d8780f0 6630 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6631 bfd_get_filename (abfd));
6632 }
9c541725 6633 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6634 /* For DWOs coming from DWP files, we don't know the CU length
6635 nor the type's offset in the TU until now. */
4057dfde 6636 dwo_unit->length = cu->header.get_length ();
9c541725 6637 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6638
6639 /* Establish the type offset that can be used to lookup the type.
6640 For DWO files, we don't know it until now. */
9c541725
PA
6641 sig_type->type_offset_in_section
6642 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6643 }
6644 else
6645 {
ed2dc618
SM
6646 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6647 &cu->header, section,
b0c7bfa9 6648 dwo_abbrev_section,
43988095 6649 info_ptr, rcuh_kind::COMPILE);
9c541725 6650 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6651 /* For DWOs coming from DWP files, we don't know the CU length
6652 until now. */
4057dfde 6653 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6654 }
6655
685af9cd 6656 *result_dwo_abbrev_table
86de1d91
TT
6657 = abbrev_table::read (objfile, dwo_abbrev_section,
6658 cu->header.abbrev_sect_off);
685af9cd
TT
6659 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6660 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6661
6662 /* Read in the die, but leave space to copy over the attributes
6663 from the stub. This has the benefit of simplifying the rest of
6664 the code - all the work to maintain the illusion of a single
6665 DW_TAG_{compile,type}_unit DIE is done here. */
6666 num_extra_attrs = ((stmt_list != NULL)
6667 + (low_pc != NULL)
6668 + (high_pc != NULL)
6669 + (ranges != NULL)
6670 + (comp_dir != NULL));
6671 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6672 num_extra_attrs);
b0c7bfa9
DE
6673
6674 /* Copy over the attributes from the stub to the DIE we just read in. */
6675 comp_unit_die = *result_comp_unit_die;
6676 i = comp_unit_die->num_attrs;
6677 if (stmt_list != NULL)
6678 comp_unit_die->attrs[i++] = *stmt_list;
6679 if (low_pc != NULL)
6680 comp_unit_die->attrs[i++] = *low_pc;
6681 if (high_pc != NULL)
6682 comp_unit_die->attrs[i++] = *high_pc;
6683 if (ranges != NULL)
6684 comp_unit_die->attrs[i++] = *ranges;
6685 if (comp_dir != NULL)
6686 comp_unit_die->attrs[i++] = *comp_dir;
6687 comp_unit_die->num_attrs += num_extra_attrs;
6688
b4f54984 6689 if (dwarf_die_debug)
bf6af496
DE
6690 {
6691 fprintf_unfiltered (gdb_stdlog,
6692 "Read die from %s@0x%x of %s:\n",
96b79293 6693 section->get_name (),
bf6af496
DE
6694 (unsigned) (begin_info_ptr - section->buffer),
6695 bfd_get_filename (abfd));
b4f54984 6696 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6697 }
6698
b0c7bfa9
DE
6699 /* Skip dummy compilation units. */
6700 if (info_ptr >= begin_info_ptr + dwo_unit->length
6701 || peek_abbrev_code (abfd, info_ptr) == 0)
6702 return 0;
6703
6704 *result_info_ptr = info_ptr;
6705 return 1;
6706}
6707
a084a2a6
AT
6708/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6709 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6710 signature is part of the header. */
6711static gdb::optional<ULONGEST>
6712lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6713{
6714 if (cu->header.version >= 5)
6715 return cu->header.signature;
6716 struct attribute *attr;
6717 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6718 if (attr == nullptr)
6719 return gdb::optional<ULONGEST> ();
6720 return DW_UNSND (attr);
6721}
6722
c0ab21c2 6723/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6724 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6725 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6726
6727static struct dwo_unit *
6728lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6729 struct die_info *comp_unit_die,
6730 const char *dwo_name)
b0c7bfa9
DE
6731{
6732 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6733 struct dwo_unit *dwo_unit;
c0ab21c2 6734 const char *comp_dir;
b0c7bfa9 6735
a2ce51a0
DE
6736 gdb_assert (cu != NULL);
6737
b0c7bfa9 6738 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6739 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6740 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6741
6742 if (this_cu->is_debug_types)
6743 {
6744 struct signatured_type *sig_type;
6745
6746 /* Since this_cu is the first member of struct signatured_type,
6747 we can go from a pointer to one to a pointer to the other. */
6748 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6749 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6750 }
6751 else
6752 {
a084a2a6
AT
6753 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6754 if (!signature.has_value ())
b0c7bfa9
DE
6755 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6756 " [in module %s]"),
e3b94546 6757 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6758 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6759 *signature);
b0c7bfa9
DE
6760 }
6761
b0c7bfa9
DE
6762 return dwo_unit;
6763}
6764
c0ab21c2 6765/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6766 See it for a description of the parameters.
fcd3b13d 6767 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6768
c0ab21c2
TT
6769void
6770cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6771 int use_existing_cu)
a2ce51a0 6772{
a2ce51a0 6773 struct signatured_type *sig_type;
a2ce51a0 6774 struct die_reader_specs reader;
a2ce51a0
DE
6775
6776 /* Verify we can do the following downcast, and that we have the
6777 data we need. */
6778 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6779 sig_type = (struct signatured_type *) this_cu;
6780 gdb_assert (sig_type->dwo_unit != NULL);
6781
6aa5f3a6
DE
6782 if (use_existing_cu && this_cu->cu != NULL)
6783 {
6784 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6785 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6786 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6787 }
6788 else
6789 {
6790 /* If !use_existing_cu, this_cu->cu must be NULL. */
6791 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6792 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6793 }
6794
6795 /* A future optimization, if needed, would be to use an existing
6796 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6797 could share abbrev tables. */
a2ce51a0
DE
6798
6799 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6800 NULL /* stub_comp_unit_die */,
6801 sig_type->dwo_unit->dwo_file->comp_dir,
6802 &reader, &info_ptr,
3e225074 6803 &comp_unit_die,
c0ab21c2 6804 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6805 {
6806 /* Dummy die. */
c0ab21c2 6807 dummy_p = true;
a2ce51a0 6808 }
a2ce51a0
DE
6809}
6810
fd820528 6811/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6812 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6813
f4dc4d17
DE
6814 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6815 Otherwise the table specified in the comp unit header is read in and used.
6816 This is an optimization for when we already have the abbrev table.
6817
dee91e82 6818 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6819 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6820
c0ab21c2
TT
6821cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6822 struct abbrev_table *abbrev_table,
6751ebae 6823 int use_existing_cu,
c0ab21c2
TT
6824 bool skip_partial)
6825 : die_reader_specs {},
6751ebae 6826 m_this_cu (this_cu)
c906108c 6827{
ed2dc618 6828 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6829 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6830 struct dwarf2_section_info *section = this_cu->section;
96b79293 6831 bfd *abfd = section->get_bfd_owner ();
dee91e82 6832 struct dwarf2_cu *cu;
c0ab21c2 6833 const gdb_byte *begin_info_ptr;
dee91e82 6834 struct signatured_type *sig_type = NULL;
4bdcc0c1 6835 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6836 /* Non-zero if CU currently points to a DWO file and we need to
6837 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6838 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6839 int rereading_dwo_cu = 0;
c906108c 6840
b4f54984 6841 if (dwarf_die_debug)
9d8780f0 6842 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6843 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6844 sect_offset_str (this_cu->sect_off));
09406207 6845
a2ce51a0
DE
6846 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6847 file (instead of going through the stub), short-circuit all of this. */
6848 if (this_cu->reading_dwo_directly)
6849 {
6850 /* Narrow down the scope of possibilities to have to understand. */
6851 gdb_assert (this_cu->is_debug_types);
6852 gdb_assert (abbrev_table == NULL);
6751ebae 6853 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6854 return;
6855 }
6856
dee91e82 6857 /* This is cheap if the section is already read in. */
96b79293 6858 section->read (objfile);
dee91e82 6859
9c541725 6860 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6861
6862 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6863
6864 if (use_existing_cu && this_cu->cu != NULL)
6865 {
6866 cu = this_cu->cu;
42e7ad6c
DE
6867 /* If this CU is from a DWO file we need to start over, we need to
6868 refetch the attributes from the skeleton CU.
6869 This could be optimized by retrieving those attributes from when we
6870 were here the first time: the previous comp_unit_die was stored in
6871 comp_unit_obstack. But there's no data yet that we need this
6872 optimization. */
6873 if (cu->dwo_unit != NULL)
6874 rereading_dwo_cu = 1;
dee91e82
DE
6875 }
6876 else
6877 {
6878 /* If !use_existing_cu, this_cu->cu must be NULL. */
6879 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6880 m_new_cu.reset (new dwarf2_cu (this_cu));
6881 cu = m_new_cu.get ();
42e7ad6c 6882 }
dee91e82 6883
b0c7bfa9 6884 /* Get the header. */
9c541725 6885 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6886 {
6887 /* We already have the header, there's no need to read it in again. */
9c541725 6888 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6889 }
6890 else
6891 {
3019eac3 6892 if (this_cu->is_debug_types)
dee91e82 6893 {
ed2dc618
SM
6894 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6895 &cu->header, section,
4bdcc0c1 6896 abbrev_section, info_ptr,
43988095 6897 rcuh_kind::TYPE);
dee91e82 6898
42e7ad6c
DE
6899 /* Since per_cu is the first member of struct signatured_type,
6900 we can go from a pointer to one to a pointer to the other. */
6901 sig_type = (struct signatured_type *) this_cu;
43988095 6902 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6903 gdb_assert (sig_type->type_offset_in_tu
6904 == cu->header.type_cu_offset_in_tu);
6905 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6906
42e7ad6c
DE
6907 /* LENGTH has not been set yet for type units if we're
6908 using .gdb_index. */
4057dfde 6909 this_cu->length = cu->header.get_length ();
3019eac3
DE
6910
6911 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6912 sig_type->type_offset_in_section =
6913 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6914
6915 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6916 }
6917 else
6918 {
ed2dc618
SM
6919 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6920 &cu->header, section,
4bdcc0c1 6921 abbrev_section,
43988095
JK
6922 info_ptr,
6923 rcuh_kind::COMPILE);
dee91e82 6924
9c541725 6925 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6926 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6927 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6928 }
6929 }
10b3939b 6930
6caca83c 6931 /* Skip dummy compilation units. */
dee91e82 6932 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6933 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6934 {
6935 dummy_p = true;
6936 return;
6937 }
6caca83c 6938
433df2d4
DE
6939 /* If we don't have them yet, read the abbrevs for this compilation unit.
6940 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6941 done. */
f4dc4d17 6942 if (abbrev_table != NULL)
685af9cd
TT
6943 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6944 else
f4dc4d17 6945 {
c0ab21c2 6946 m_abbrev_table_holder
86de1d91
TT
6947 = abbrev_table::read (objfile, abbrev_section,
6948 cu->header.abbrev_sect_off);
c0ab21c2 6949 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6950 }
af703f96 6951
dee91e82 6952 /* Read the top level CU/TU die. */
c0ab21c2 6953 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6954 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6955
58f0c718 6956 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6957 {
6958 dummy_p = true;
6959 return;
6960 }
58f0c718 6961
b0c7bfa9 6962 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6963 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6964 table from the DWO file and pass the ownership over to us. It will be
6965 referenced from READER, so we must make sure to free it after we're done
6966 with READER.
6967
b0c7bfa9
DE
6968 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6969 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6970 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6971 if (dwo_name != nullptr)
3019eac3 6972 {
3019eac3 6973 struct dwo_unit *dwo_unit;
b0c7bfa9 6974 struct die_info *dwo_comp_unit_die;
3019eac3 6975
3e225074 6976 if (comp_unit_die->has_children)
6a506a2d 6977 {
b98664d3 6978 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6979 " has children (offset %s) [in module %s]"),
6980 sect_offset_str (this_cu->sect_off),
6981 bfd_get_filename (abfd));
6a506a2d 6982 }
c0ab21c2 6983 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6984 if (dwo_unit != NULL)
3019eac3 6985 {
6a506a2d 6986 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6987 comp_unit_die, NULL,
c0ab21c2 6988 this, &info_ptr,
3e225074 6989 &dwo_comp_unit_die,
c0ab21c2 6990 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6991 {
6992 /* Dummy die. */
c0ab21c2 6993 dummy_p = true;
6a506a2d
DE
6994 return;
6995 }
6996 comp_unit_die = dwo_comp_unit_die;
6997 }
6998 else
6999 {
7000 /* Yikes, we couldn't find the rest of the DIE, we only have
7001 the stub. A complaint has already been logged. There's
7002 not much more we can do except pass on the stub DIE to
7003 die_reader_func. We don't want to throw an error on bad
7004 debug info. */
3019eac3
DE
7005 }
7006 }
c0ab21c2 7007}
3019eac3 7008
6751ebae
TT
7009void
7010cutu_reader::keep ()
c0ab21c2 7011{
b0c7bfa9 7012 /* Done, clean up. */
6751ebae
TT
7013 gdb_assert (!dummy_p);
7014 if (m_new_cu != NULL)
348e048f 7015 {
c0ab21c2
TT
7016 struct dwarf2_per_objfile *dwarf2_per_objfile
7017 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7018 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7019 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7020 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7021 /* The chain owns it now. */
c0ab21c2 7022 m_new_cu.release ();
348e048f 7023 }
dee91e82
DE
7024}
7025
18a8505e
AT
7026/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7027 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7028 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7029
7030 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7031 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7032
7033 We fill in THIS_CU->length.
7034
dee91e82 7035 THIS_CU->cu is always freed when done.
3019eac3 7036 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7037 to care whether it refers to the "main" CU or the DWO CU.
7038
7039 When parent_cu is passed, it is used to provide a default value for
7040 str_offsets_base and addr_base from the parent. */
dee91e82 7041
c0ab21c2
TT
7042cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7043 struct dwarf2_cu *parent_cu,
7044 struct dwo_file *dwo_file)
7045 : die_reader_specs {},
7046 m_this_cu (this_cu)
dee91e82 7047{
ed2dc618 7048 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7049 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7050 struct dwarf2_section_info *section = this_cu->section;
96b79293 7051 bfd *abfd = section->get_bfd_owner ();
33e80786 7052 struct dwarf2_section_info *abbrev_section;
d521ce57 7053 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7054
b4f54984 7055 if (dwarf_die_debug)
9d8780f0 7056 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7057 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7058 sect_offset_str (this_cu->sect_off));
09406207 7059
dee91e82
DE
7060 gdb_assert (this_cu->cu == NULL);
7061
33e80786
DE
7062 abbrev_section = (dwo_file != NULL
7063 ? &dwo_file->sections.abbrev
7064 : get_abbrev_section_for_cu (this_cu));
7065
dee91e82 7066 /* This is cheap if the section is already read in. */
96b79293 7067 section->read (objfile);
dee91e82 7068
c0ab21c2 7069 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7070
9c541725 7071 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7072 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7073 &m_new_cu->header, section,
4bdcc0c1 7074 abbrev_section, info_ptr,
43988095
JK
7075 (this_cu->is_debug_types
7076 ? rcuh_kind::TYPE
7077 : rcuh_kind::COMPILE));
dee91e82 7078
18a8505e
AT
7079 if (parent_cu != nullptr)
7080 {
c0ab21c2
TT
7081 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7082 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7083 }
4057dfde 7084 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7085
7086 /* Skip dummy compilation units. */
7087 if (info_ptr >= begin_info_ptr + this_cu->length
7088 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7089 {
7090 dummy_p = true;
7091 return;
7092 }
72bf9492 7093
c0ab21c2 7094 m_abbrev_table_holder
86de1d91
TT
7095 = abbrev_table::read (objfile, abbrev_section,
7096 m_new_cu->header.abbrev_sect_off);
dee91e82 7097
c0ab21c2
TT
7098 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7099 m_abbrev_table_holder.get ());
3e225074 7100 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7101}
7102
0018ea6f
DE
7103\f
7104/* Type Unit Groups.
dee91e82 7105
0018ea6f
DE
7106 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7107 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7108 so that all types coming from the same compilation (.o file) are grouped
7109 together. A future step could be to put the types in the same symtab as
7110 the CU the types ultimately came from. */
ff013f42 7111
f4dc4d17
DE
7112static hashval_t
7113hash_type_unit_group (const void *item)
7114{
9a3c8263
SM
7115 const struct type_unit_group *tu_group
7116 = (const struct type_unit_group *) item;
f4dc4d17 7117
094b34ac 7118 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7119}
348e048f
DE
7120
7121static int
f4dc4d17 7122eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7123{
9a3c8263
SM
7124 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7125 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7126
094b34ac 7127 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7128}
348e048f 7129
f4dc4d17
DE
7130/* Allocate a hash table for type unit groups. */
7131
eaa5fa8b 7132static htab_up
298e9637 7133allocate_type_unit_groups_table ()
f4dc4d17 7134{
eaa5fa8b
TT
7135 return htab_up (htab_create_alloc (3,
7136 hash_type_unit_group,
7137 eq_type_unit_group,
7138 NULL, xcalloc, xfree));
f4dc4d17 7139}
dee91e82 7140
f4dc4d17
DE
7141/* Type units that don't have DW_AT_stmt_list are grouped into their own
7142 partial symtabs. We combine several TUs per psymtab to not let the size
7143 of any one psymtab grow too big. */
7144#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7145#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7146
094b34ac 7147/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7148 Create the type_unit_group object used to hold one or more TUs. */
7149
7150static struct type_unit_group *
094b34ac 7151create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7152{
518817b3
SM
7153 struct dwarf2_per_objfile *dwarf2_per_objfile
7154 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7155 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7156 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7157 struct type_unit_group *tu_group;
f4dc4d17
DE
7158
7159 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7160 struct type_unit_group);
094b34ac 7161 per_cu = &tu_group->per_cu;
518817b3 7162 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7163
094b34ac
DE
7164 if (dwarf2_per_objfile->using_index)
7165 {
7166 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7167 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7168 }
7169 else
7170 {
9c541725 7171 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7172 dwarf2_psymtab *pst;
528e1572 7173 std::string name;
094b34ac
DE
7174
7175 /* Give the symtab a useful name for debug purposes. */
7176 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7177 name = string_printf ("<type_units_%d>",
7178 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7179 else
528e1572 7180 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7181
528e1572 7182 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7183 pst->anonymous = true;
094b34ac 7184 }
f4dc4d17 7185
094b34ac 7186 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7187 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7188
7189 return tu_group;
7190}
7191
094b34ac
DE
7192/* Look up the type_unit_group for type unit CU, and create it if necessary.
7193 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7194
7195static struct type_unit_group *
ff39bb5e 7196get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7197{
518817b3
SM
7198 struct dwarf2_per_objfile *dwarf2_per_objfile
7199 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7200 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7201 struct type_unit_group *tu_group;
7202 void **slot;
7203 unsigned int line_offset;
7204 struct type_unit_group type_unit_group_for_lookup;
7205
7206 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7207 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7208
7209 /* Do we need to create a new group, or can we use an existing one? */
7210
7211 if (stmt_list)
7212 {
7213 line_offset = DW_UNSND (stmt_list);
7214 ++tu_stats->nr_symtab_sharers;
7215 }
7216 else
7217 {
7218 /* Ugh, no stmt_list. Rare, but we have to handle it.
7219 We can do various things here like create one group per TU or
7220 spread them over multiple groups to split up the expansion work.
7221 To avoid worst case scenarios (too many groups or too large groups)
7222 we, umm, group them in bunches. */
7223 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7224 | (tu_stats->nr_stmt_less_type_units
7225 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7226 ++tu_stats->nr_stmt_less_type_units;
7227 }
7228
094b34ac 7229 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7230 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7231 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7232 &type_unit_group_for_lookup, INSERT);
7233 if (*slot != NULL)
7234 {
9a3c8263 7235 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7236 gdb_assert (tu_group != NULL);
7237 }
7238 else
7239 {
9c541725 7240 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7241 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7242 *slot = tu_group;
7243 ++tu_stats->nr_symtabs;
7244 }
7245
7246 return tu_group;
7247}
0018ea6f
DE
7248\f
7249/* Partial symbol tables. */
7250
7251/* Create a psymtab named NAME and assign it to PER_CU.
7252
7253 The caller must fill in the following details:
7254 dirname, textlow, texthigh. */
7255
891813be 7256static dwarf2_psymtab *
0018ea6f
DE
7257create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7258{
e3b94546 7259 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7260 dwarf2_psymtab *pst;
0018ea6f 7261
891813be 7262 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7263
6d94535f 7264 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7265
7266 /* This is the glue that links PST into GDB's symbol API. */
891813be 7267 pst->per_cu_data = per_cu;
0018ea6f
DE
7268 per_cu->v.psymtab = pst;
7269
7270 return pst;
7271}
7272
c0ab21c2 7273/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7274
7275static void
7276process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7277 const gdb_byte *info_ptr,
0018ea6f 7278 struct die_info *comp_unit_die,
c0ab21c2 7279 enum language pretend_language)
0018ea6f
DE
7280{
7281 struct dwarf2_cu *cu = reader->cu;
518817b3 7282 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7283 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7284 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7285 CORE_ADDR baseaddr;
7286 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7287 dwarf2_psymtab *pst;
3a2b436a 7288 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7289 const char *filename;
0018ea6f 7290
0018ea6f
DE
7291 gdb_assert (! per_cu->is_debug_types);
7292
c0ab21c2 7293 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7294
0018ea6f 7295 /* Allocate a new partial symbol table structure. */
2e927613
TV
7296 gdb::unique_xmalloc_ptr<char> debug_filename;
7297 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7298 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7299 if (filename == NULL)
0018ea6f 7300 filename = "";
2e927613
TV
7301 else if (strcmp (filename, artificial) == 0)
7302 {
7303 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7304 sect_offset_str (per_cu->sect_off),
7305 (char *) NULL));
2e927613
TV
7306 filename = debug_filename.get ();
7307 }
0018ea6f
DE
7308
7309 pst = create_partial_symtab (per_cu, filename);
7310
7311 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7312 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7313
b3b3bada 7314 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7315
7316 dwarf2_find_base_address (comp_unit_die, cu);
7317
7318 /* Possibly set the default values of LOWPC and HIGHPC from
7319 `DW_AT_ranges'. */
3a2b436a
JK
7320 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7321 &best_highpc, cu, pst);
7322 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7323 {
7324 CORE_ADDR low
7325 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7326 - baseaddr);
7327 CORE_ADDR high
7328 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7329 - baseaddr - 1);
7330 /* Store the contiguous range if it is not empty; it can be
7331 empty for CUs with no code. */
d320c2b5
TT
7332 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7333 low, high, pst);
79748972 7334 }
0018ea6f
DE
7335
7336 /* Check if comp unit has_children.
7337 If so, read the rest of the partial symbols from this comp unit.
7338 If not, there's no more debug_info for this comp unit. */
3e225074 7339 if (comp_unit_die->has_children)
0018ea6f
DE
7340 {
7341 struct partial_die_info *first_die;
7342 CORE_ADDR lowpc, highpc;
7343
7344 lowpc = ((CORE_ADDR) -1);
7345 highpc = ((CORE_ADDR) 0);
7346
7347 first_die = load_partial_dies (reader, info_ptr, 1);
7348
7349 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7350 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7351
7352 /* If we didn't find a lowpc, set it to highpc to avoid
7353 complaints from `maint check'. */
7354 if (lowpc == ((CORE_ADDR) -1))
7355 lowpc = highpc;
7356
7357 /* If the compilation unit didn't have an explicit address range,
7358 then use the information extracted from its child dies. */
e385593e 7359 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7360 {
7361 best_lowpc = lowpc;
7362 best_highpc = highpc;
7363 }
7364 }
4ae976d1 7365 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7366 best_lowpc + baseaddr)
7367 - baseaddr);
4ae976d1 7368 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7369 best_highpc + baseaddr)
7370 - baseaddr);
0018ea6f 7371
8763cede 7372 end_psymtab_common (objfile, pst);
0018ea6f 7373
ae640021 7374 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7375 {
7376 int i;
ae640021 7377 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7378
7379 /* Fill in 'dependencies' here; we fill in 'users' in a
7380 post-pass. */
7381 pst->number_of_dependencies = len;
a9342b62
TT
7382 pst->dependencies
7383 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7384 for (i = 0; i < len; ++i)
7385 {
7386 pst->dependencies[i]
7387 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7388 }
0018ea6f 7389
ae640021 7390 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7391 }
7392
7393 /* Get the list of files included in the current compilation unit,
7394 and build a psymtab for each of them. */
7395 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7396
b4f54984 7397 if (dwarf_read_debug)
b926417a
TT
7398 fprintf_unfiltered (gdb_stdlog,
7399 "Psymtab for %s unit @%s: %s - %s"
7400 ", %d global, %d static syms\n",
7401 per_cu->is_debug_types ? "type" : "comp",
7402 sect_offset_str (per_cu->sect_off),
7403 paddress (gdbarch, pst->text_low (objfile)),
7404 paddress (gdbarch, pst->text_high (objfile)),
7405 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7406}
7407
7408/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7409 Process compilation unit THIS_CU for a psymtab. */
7410
7411static void
7412process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7413 bool want_partial_unit,
b93601f3 7414 enum language pretend_language)
0018ea6f
DE
7415{
7416 /* If this compilation unit was already read in, free the
7417 cached copy in order to read it in again. This is
7418 necessary because we skipped some symbols when we first
7419 read in the compilation unit (see load_partial_dies).
7420 This problem could be avoided, but the benefit is unclear. */
7421 if (this_cu->cu != NULL)
7422 free_one_cached_comp_unit (this_cu);
7423
6751ebae 7424 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2
TT
7425
7426 if (reader.dummy_p)
f1902523 7427 {
c0ab21c2 7428 /* Nothing. */
f1902523 7429 }
c0ab21c2 7430 else if (this_cu->is_debug_types)
3e225074
TT
7431 build_type_psymtabs_reader (&reader, reader.info_ptr,
7432 reader.comp_unit_die);
135f5437
TT
7433 else if (want_partial_unit
7434 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7435 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7436 reader.comp_unit_die,
c0ab21c2 7437 pretend_language);
0018ea6f
DE
7438
7439 /* Age out any secondary CUs. */
ed2dc618 7440 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7441}
f4dc4d17
DE
7442
7443/* Reader function for build_type_psymtabs. */
7444
7445static void
7446build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7447 const gdb_byte *info_ptr,
3e225074 7448 struct die_info *type_unit_die)
f4dc4d17 7449{
ed2dc618 7450 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7451 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7452 struct objfile *objfile = dwarf2_per_objfile->objfile;
7453 struct dwarf2_cu *cu = reader->cu;
7454 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7455 struct signatured_type *sig_type;
f4dc4d17
DE
7456 struct type_unit_group *tu_group;
7457 struct attribute *attr;
7458 struct partial_die_info *first_die;
7459 CORE_ADDR lowpc, highpc;
891813be 7460 dwarf2_psymtab *pst;
f4dc4d17 7461
0186c6a7
DE
7462 gdb_assert (per_cu->is_debug_types);
7463 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7464
3e225074 7465 if (! type_unit_die->has_children)
f4dc4d17
DE
7466 return;
7467
7468 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7469 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7470
df07e2c7 7471 if (tu_group->tus == nullptr)
a8b3b8e9 7472 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7473 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7474
7475 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7476 pst = create_partial_symtab (per_cu, "");
6d94535f 7477 pst->anonymous = true;
f4dc4d17
DE
7478
7479 first_die = load_partial_dies (reader, info_ptr, 1);
7480
7481 lowpc = (CORE_ADDR) -1;
7482 highpc = (CORE_ADDR) 0;
7483 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7484
8763cede 7485 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7486}
7487
73051182
DE
7488/* Struct used to sort TUs by their abbreviation table offset. */
7489
7490struct tu_abbrev_offset
7491{
b2bdb8cf
SM
7492 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7493 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7494 {}
7495
7496 signatured_type *sig_type;
73051182
DE
7497 sect_offset abbrev_offset;
7498};
7499
484cf504 7500/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7501
484cf504
TT
7502static bool
7503sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7504 const struct tu_abbrev_offset &b)
73051182 7505{
484cf504 7506 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7507}
7508
7509/* Efficiently read all the type units.
7510 This does the bulk of the work for build_type_psymtabs.
7511
7512 The efficiency is because we sort TUs by the abbrev table they use and
7513 only read each abbrev table once. In one program there are 200K TUs
7514 sharing 8K abbrev tables.
7515
7516 The main purpose of this function is to support building the
7517 dwarf2_per_objfile->type_unit_groups table.
7518 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7519 can collapse the search space by grouping them by stmt_list.
7520 The savings can be significant, in the same program from above the 200K TUs
7521 share 8K stmt_list tables.
7522
7523 FUNC is expected to call get_type_unit_group, which will create the
7524 struct type_unit_group if necessary and add it to
7525 dwarf2_per_objfile->type_unit_groups. */
7526
7527static void
ed2dc618 7528build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7529{
73051182 7530 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7531 abbrev_table_up abbrev_table;
73051182 7532 sect_offset abbrev_offset;
73051182
DE
7533
7534 /* It's up to the caller to not call us multiple times. */
7535 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7536
b2bdb8cf 7537 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7538 return;
7539
7540 /* TUs typically share abbrev tables, and there can be way more TUs than
7541 abbrev tables. Sort by abbrev table to reduce the number of times we
7542 read each abbrev table in.
7543 Alternatives are to punt or to maintain a cache of abbrev tables.
7544 This is simpler and efficient enough for now.
7545
7546 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7547 symtab to use). Typically TUs with the same abbrev offset have the same
7548 stmt_list value too so in practice this should work well.
7549
7550 The basic algorithm here is:
7551
7552 sort TUs by abbrev table
7553 for each TU with same abbrev table:
7554 read abbrev table if first user
7555 read TU top level DIE
7556 [IWBN if DWO skeletons had DW_AT_stmt_list]
7557 call FUNC */
7558
b4f54984 7559 if (dwarf_read_debug)
73051182
DE
7560 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7561
7562 /* Sort in a separate table to maintain the order of all_type_units
7563 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7564 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7565 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7566
7567 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7568 sorted_by_abbrev.emplace_back
7569 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7570 sig_type->per_cu.section,
7571 sig_type->per_cu.sect_off));
73051182 7572
484cf504
TT
7573 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7574 sort_tu_by_abbrev_offset);
73051182 7575
9c541725 7576 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7577
b2bdb8cf 7578 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7579 {
73051182
DE
7580 /* Switch to the next abbrev table if necessary. */
7581 if (abbrev_table == NULL
b2bdb8cf 7582 || tu.abbrev_offset != abbrev_offset)
73051182 7583 {
b2bdb8cf 7584 abbrev_offset = tu.abbrev_offset;
73051182 7585 abbrev_table =
86de1d91
TT
7586 abbrev_table::read (dwarf2_per_objfile->objfile,
7587 &dwarf2_per_objfile->abbrev,
7588 abbrev_offset);
73051182
DE
7589 ++tu_stats->nr_uniq_abbrev_tables;
7590 }
7591
c0ab21c2 7592 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7593 0, false);
c0ab21c2
TT
7594 if (!reader.dummy_p)
7595 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7596 reader.comp_unit_die);
73051182 7597 }
6aa5f3a6 7598}
73051182 7599
6aa5f3a6
DE
7600/* Print collected type unit statistics. */
7601
7602static void
ed2dc618 7603print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7604{
7605 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7606
7607 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7608 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7609 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7610 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7611 tu_stats->nr_uniq_abbrev_tables);
7612 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7613 tu_stats->nr_symtabs);
7614 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7615 tu_stats->nr_symtab_sharers);
7616 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7617 tu_stats->nr_stmt_less_type_units);
7618 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7619 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7620}
7621
f4dc4d17
DE
7622/* Traversal function for build_type_psymtabs. */
7623
7624static int
7625build_type_psymtab_dependencies (void **slot, void *info)
7626{
ed2dc618
SM
7627 struct dwarf2_per_objfile *dwarf2_per_objfile
7628 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7629 struct objfile *objfile = dwarf2_per_objfile->objfile;
7630 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7631 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7632 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7633 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7634 int i;
7635
7636 gdb_assert (len > 0);
197400e8 7637 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7638
7639 pst->number_of_dependencies = len;
a9342b62 7640 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7641 for (i = 0; i < len; ++i)
f4dc4d17 7642 {
df07e2c7 7643 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7644 gdb_assert (iter->per_cu.is_debug_types);
7645 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7646 iter->type_unit_group = tu_group;
f4dc4d17
DE
7647 }
7648
df07e2c7
AB
7649 delete tu_group->tus;
7650 tu_group->tus = nullptr;
348e048f
DE
7651
7652 return 1;
7653}
7654
7655/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7656 Build partial symbol tables for the .debug_types comp-units. */
7657
7658static void
ed2dc618 7659build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7660{
ed2dc618 7661 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7662 return;
7663
ed2dc618 7664 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7665}
f4dc4d17 7666
6aa5f3a6
DE
7667/* Traversal function for process_skeletonless_type_unit.
7668 Read a TU in a DWO file and build partial symbols for it. */
7669
7670static int
7671process_skeletonless_type_unit (void **slot, void *info)
7672{
7673 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7674 struct dwarf2_per_objfile *dwarf2_per_objfile
7675 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7676 struct signatured_type find_entry, *entry;
7677
7678 /* If this TU doesn't exist in the global table, add it and read it in. */
7679
7680 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7681 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7682
7683 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7684 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7685 &find_entry, INSERT);
6aa5f3a6
DE
7686 /* If we've already seen this type there's nothing to do. What's happening
7687 is we're doing our own version of comdat-folding here. */
7688 if (*slot != NULL)
7689 return 1;
7690
7691 /* This does the job that create_all_type_units would have done for
7692 this TU. */
ed2dc618
SM
7693 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7694 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7695 *slot = entry;
7696
7697 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7698 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7699 if (!reader.dummy_p)
7700 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7701 reader.comp_unit_die);
6aa5f3a6
DE
7702
7703 return 1;
7704}
7705
7706/* Traversal function for process_skeletonless_type_units. */
7707
7708static int
7709process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7710{
7711 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7712
7713 if (dwo_file->tus != NULL)
b0b6a987
TT
7714 htab_traverse_noresize (dwo_file->tus.get (),
7715 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7716
7717 return 1;
7718}
7719
7720/* Scan all TUs of DWO files, verifying we've processed them.
7721 This is needed in case a TU was emitted without its skeleton.
7722 Note: This can't be done until we know what all the DWO files are. */
7723
7724static void
ed2dc618 7725process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7726{
7727 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7728 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7729 && dwarf2_per_objfile->dwo_files != NULL)
7730 {
51ac9db5 7731 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7732 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7733 dwarf2_per_objfile);
6aa5f3a6 7734 }
348e048f
DE
7735}
7736
ed2dc618 7737/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7738
7739static void
ed2dc618 7740set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7741{
b76e467d 7742 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7743 {
891813be 7744 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7745
36586728
TT
7746 if (pst == NULL)
7747 continue;
7748
b76e467d 7749 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7750 {
7751 /* Set the 'user' field only if it is not already set. */
7752 if (pst->dependencies[j]->user == NULL)
7753 pst->dependencies[j]->user = pst;
7754 }
7755 }
7756}
7757
93311388
DE
7758/* Build the partial symbol table by doing a quick pass through the
7759 .debug_info and .debug_abbrev sections. */
72bf9492 7760
93311388 7761static void
ed2dc618 7762dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7763{
ed2dc618 7764 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7765
b4f54984 7766 if (dwarf_read_debug)
45cfd468
DE
7767 {
7768 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7769 objfile_name (objfile));
45cfd468
DE
7770 }
7771
76935768
TT
7772 scoped_restore restore_reading_psyms
7773 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7774 true);
98bfdba5 7775
96b79293 7776 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7777
93311388
DE
7778 /* Any cached compilation units will be linked by the per-objfile
7779 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7780 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7781
ed2dc618 7782 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7783
ed2dc618 7784 create_all_comp_units (dwarf2_per_objfile);
c906108c 7785
60606b2c
TT
7786 /* Create a temporary address map on a temporary obstack. We later
7787 copy this to the final obstack. */
8268c778 7788 auto_obstack temp_obstack;
791afaa2
TT
7789
7790 scoped_restore save_psymtabs_addrmap
d320c2b5 7791 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7792 addrmap_create_mutable (&temp_obstack));
72bf9492 7793
b76e467d 7794 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7795 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7796
6aa5f3a6 7797 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7798 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7799
7800 /* Now that all TUs have been processed we can fill in the dependencies. */
7801 if (dwarf2_per_objfile->type_unit_groups != NULL)
7802 {
eaa5fa8b 7803 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7804 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7805 }
7806
b4f54984 7807 if (dwarf_read_debug)
ed2dc618 7808 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7809
ed2dc618 7810 set_partial_user (dwarf2_per_objfile);
95554aad 7811
d320c2b5
TT
7812 objfile->partial_symtabs->psymtabs_addrmap
7813 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7814 objfile->partial_symtabs->obstack ());
791afaa2
TT
7815 /* At this point we want to keep the address map. */
7816 save_psymtabs_addrmap.release ();
ff013f42 7817
b4f54984 7818 if (dwarf_read_debug)
45cfd468 7819 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7820 objfile_name (objfile));
ae038cb0
DJ
7821}
7822
dee91e82
DE
7823/* Load the partial DIEs for a secondary CU into memory.
7824 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7825
dee91e82
DE
7826static void
7827load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7828{
6751ebae 7829 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7830
7831 if (!reader.dummy_p)
7832 {
7833 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7834 language_minimal);
7835
7836 /* Check if comp unit has_children.
7837 If so, read the rest of the partial symbols from this comp unit.
7838 If not, there's no more debug_info for this comp unit. */
3e225074 7839 if (reader.comp_unit_die->has_children)
c0ab21c2 7840 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7841
7842 reader.keep ();
c0ab21c2 7843 }
ae038cb0
DJ
7844}
7845
ae038cb0 7846static void
ed2dc618 7847read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7848 struct dwarf2_section_info *section,
f1902523 7849 struct dwarf2_section_info *abbrev_section,
b76e467d 7850 unsigned int is_dwz)
ae038cb0 7851{
d521ce57 7852 const gdb_byte *info_ptr;
ed2dc618 7853 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7854
b4f54984 7855 if (dwarf_read_debug)
bf6af496 7856 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7857 section->get_name (),
7858 section->get_file_name ());
bf6af496 7859
96b79293 7860 section->read (objfile);
ae038cb0 7861
36586728 7862 info_ptr = section->buffer;
6e70227d 7863
36586728 7864 while (info_ptr < section->buffer + section->size)
ae038cb0 7865 {
ae038cb0 7866 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7867
9c541725 7868 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7869
f1902523 7870 comp_unit_head cu_header;
ed2dc618
SM
7871 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7872 abbrev_section, info_ptr,
7873 rcuh_kind::COMPILE);
ae038cb0
DJ
7874
7875 /* Save the compilation unit for later lookup. */
f1902523
JK
7876 if (cu_header.unit_type != DW_UT_type)
7877 {
7878 this_cu = XOBNEW (&objfile->objfile_obstack,
7879 struct dwarf2_per_cu_data);
7880 memset (this_cu, 0, sizeof (*this_cu));
7881 }
7882 else
7883 {
7884 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7885 struct signatured_type);
7886 memset (sig_type, 0, sizeof (*sig_type));
7887 sig_type->signature = cu_header.signature;
7888 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7889 this_cu = &sig_type->per_cu;
7890 }
7891 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7892 this_cu->sect_off = sect_off;
f1902523 7893 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7894 this_cu->is_dwz = is_dwz;
e3b94546 7895 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7896 this_cu->section = section;
ae038cb0 7897
b76e467d 7898 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7899
7900 info_ptr = info_ptr + this_cu->length;
7901 }
36586728
TT
7902}
7903
7904/* Create a list of all compilation units in OBJFILE.
7905 This is only done for -readnow and building partial symtabs. */
7906
7907static void
ed2dc618 7908create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7909{
b76e467d 7910 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7911 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7912 &dwarf2_per_objfile->abbrev, 0);
36586728 7913
b76e467d 7914 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7915 if (dwz != NULL)
ed2dc618 7916 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7917 1);
c906108c
SS
7918}
7919
5734ee8b 7920/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7921 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7922 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7923 DW_AT_ranges). See the comments of add_partial_subprogram on how
7924 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7925
72bf9492
DJ
7926static void
7927scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7928 CORE_ADDR *highpc, int set_addrmap,
7929 struct dwarf2_cu *cu)
c906108c 7930{
72bf9492 7931 struct partial_die_info *pdi;
c906108c 7932
91c24f0a
DC
7933 /* Now, march along the PDI's, descending into ones which have
7934 interesting children but skipping the children of the other ones,
7935 until we reach the end of the compilation unit. */
c906108c 7936
72bf9492 7937 pdi = first_die;
91c24f0a 7938
72bf9492
DJ
7939 while (pdi != NULL)
7940 {
52356b79 7941 pdi->fixup (cu);
c906108c 7942
f55ee35c 7943 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7944 children, so we need to look at them. Ditto for anonymous
7945 enums. */
933c6fe4 7946
72bf9492 7947 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7948 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7949 || pdi->tag == DW_TAG_imported_unit
7950 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7951 {
72bf9492 7952 switch (pdi->tag)
c906108c
SS
7953 {
7954 case DW_TAG_subprogram:
b1dc1806 7955 case DW_TAG_inlined_subroutine:
cdc07690 7956 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7957 break;
72929c62 7958 case DW_TAG_constant:
c906108c
SS
7959 case DW_TAG_variable:
7960 case DW_TAG_typedef:
91c24f0a 7961 case DW_TAG_union_type:
72bf9492 7962 if (!pdi->is_declaration)
63d06c5c 7963 {
72bf9492 7964 add_partial_symbol (pdi, cu);
63d06c5c
DC
7965 }
7966 break;
c906108c 7967 case DW_TAG_class_type:
680b30c7 7968 case DW_TAG_interface_type:
c906108c 7969 case DW_TAG_structure_type:
72bf9492 7970 if (!pdi->is_declaration)
c906108c 7971 {
72bf9492 7972 add_partial_symbol (pdi, cu);
c906108c 7973 }
b7fee5a3
KS
7974 if ((cu->language == language_rust
7975 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7976 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7977 set_addrmap, cu);
c906108c 7978 break;
91c24f0a 7979 case DW_TAG_enumeration_type:
72bf9492
DJ
7980 if (!pdi->is_declaration)
7981 add_partial_enumeration (pdi, cu);
c906108c
SS
7982 break;
7983 case DW_TAG_base_type:
a02abb62 7984 case DW_TAG_subrange_type:
c906108c 7985 /* File scope base type definitions are added to the partial
c5aa993b 7986 symbol table. */
72bf9492 7987 add_partial_symbol (pdi, cu);
c906108c 7988 break;
d9fa45fe 7989 case DW_TAG_namespace:
cdc07690 7990 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7991 break;
5d7cb8df 7992 case DW_TAG_module:
59c35742
AB
7993 if (!pdi->is_declaration)
7994 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7995 break;
95554aad
TT
7996 case DW_TAG_imported_unit:
7997 {
7998 struct dwarf2_per_cu_data *per_cu;
7999
f4dc4d17
DE
8000 /* For now we don't handle imported units in type units. */
8001 if (cu->per_cu->is_debug_types)
8002 {
8003 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8004 " supported in type units [in module %s]"),
518817b3 8005 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8006 }
8007
e3b94546
SM
8008 per_cu = dwarf2_find_containing_comp_unit
8009 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8010 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8011
8012 /* Go read the partial unit, if needed. */
8013 if (per_cu->v.psymtab == NULL)
135f5437 8014 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8015
ae640021 8016 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8017 }
8018 break;
74921315
KS
8019 case DW_TAG_imported_declaration:
8020 add_partial_symbol (pdi, cu);
8021 break;
c906108c
SS
8022 default:
8023 break;
8024 }
8025 }
8026
72bf9492
DJ
8027 /* If the die has a sibling, skip to the sibling. */
8028
8029 pdi = pdi->die_sibling;
8030 }
8031}
8032
8033/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8034
72bf9492 8035 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8036 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8037 Enumerators are an exception; they use the scope of their parent
8038 enumeration type, i.e. the name of the enumeration type is not
8039 prepended to the enumerator.
91c24f0a 8040
72bf9492
DJ
8041 There are two complexities. One is DW_AT_specification; in this
8042 case "parent" means the parent of the target of the specification,
8043 instead of the direct parent of the DIE. The other is compilers
8044 which do not emit DW_TAG_namespace; in this case we try to guess
8045 the fully qualified name of structure types from their members'
8046 linkage names. This must be done using the DIE's children rather
8047 than the children of any DW_AT_specification target. We only need
8048 to do this for structures at the top level, i.e. if the target of
8049 any DW_AT_specification (if any; otherwise the DIE itself) does not
8050 have a parent. */
8051
8052/* Compute the scope prefix associated with PDI's parent, in
8053 compilation unit CU. The result will be allocated on CU's
8054 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8055 field. NULL is returned if no prefix is necessary. */
15d034d0 8056static const char *
72bf9492
DJ
8057partial_die_parent_scope (struct partial_die_info *pdi,
8058 struct dwarf2_cu *cu)
8059{
15d034d0 8060 const char *grandparent_scope;
72bf9492 8061 struct partial_die_info *parent, *real_pdi;
91c24f0a 8062
72bf9492
DJ
8063 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8064 then this means the parent of the specification DIE. */
8065
8066 real_pdi = pdi;
72bf9492 8067 while (real_pdi->has_specification)
fb816e8b 8068 {
122cf0f2
AB
8069 auto res = find_partial_die (real_pdi->spec_offset,
8070 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8071 real_pdi = res.pdi;
8072 cu = res.cu;
8073 }
72bf9492
DJ
8074
8075 parent = real_pdi->die_parent;
8076 if (parent == NULL)
8077 return NULL;
8078
8079 if (parent->scope_set)
8080 return parent->scope;
8081
52356b79 8082 parent->fixup (cu);
72bf9492 8083
10b3939b 8084 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8085
acebe513
UW
8086 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8087 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8088 Work around this problem here. */
8089 if (cu->language == language_cplus
6e70227d 8090 && parent->tag == DW_TAG_namespace
acebe513
UW
8091 && strcmp (parent->name, "::") == 0
8092 && grandparent_scope == NULL)
8093 {
8094 parent->scope = NULL;
8095 parent->scope_set = 1;
8096 return NULL;
8097 }
8098
0a4b0913 8099 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8100 if (pdi->tag == DW_TAG_enumerator)
8101 /* Enumerators should not get the name of the enumeration as a prefix. */
8102 parent->scope = grandparent_scope;
8103 else if (parent->tag == DW_TAG_namespace
f55ee35c 8104 || parent->tag == DW_TAG_module
72bf9492
DJ
8105 || parent->tag == DW_TAG_structure_type
8106 || parent->tag == DW_TAG_class_type
680b30c7 8107 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8108 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8109 || parent->tag == DW_TAG_enumeration_type
8110 || (cu->language == language_fortran
8111 && parent->tag == DW_TAG_subprogram
8112 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8113 {
8114 if (grandparent_scope == NULL)
8115 parent->scope = parent->name;
8116 else
3e43a32a
MS
8117 parent->scope = typename_concat (&cu->comp_unit_obstack,
8118 grandparent_scope,
f55ee35c 8119 parent->name, 0, cu);
72bf9492 8120 }
72bf9492
DJ
8121 else
8122 {
8123 /* FIXME drow/2004-04-01: What should we be doing with
8124 function-local names? For partial symbols, we should probably be
8125 ignoring them. */
fa9c3fa0
TT
8126 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8127 dwarf_tag_name (parent->tag),
8128 sect_offset_str (pdi->sect_off));
72bf9492 8129 parent->scope = grandparent_scope;
c906108c
SS
8130 }
8131
72bf9492
DJ
8132 parent->scope_set = 1;
8133 return parent->scope;
8134}
8135
8136/* Return the fully scoped name associated with PDI, from compilation unit
8137 CU. The result will be allocated with malloc. */
4568ecf9 8138
43816ebc 8139static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8140partial_die_full_name (struct partial_die_info *pdi,
8141 struct dwarf2_cu *cu)
8142{
15d034d0 8143 const char *parent_scope;
72bf9492 8144
98bfdba5
PA
8145 /* If this is a template instantiation, we can not work out the
8146 template arguments from partial DIEs. So, unfortunately, we have
8147 to go through the full DIEs. At least any work we do building
8148 types here will be reused if full symbols are loaded later. */
8149 if (pdi->has_template_arguments)
8150 {
52356b79 8151 pdi->fixup (cu);
98bfdba5
PA
8152
8153 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8154 {
8155 struct die_info *die;
8156 struct attribute attr;
8157 struct dwarf2_cu *ref_cu = cu;
8158
b64f50a1 8159 /* DW_FORM_ref_addr is using section offset. */
b4069958 8160 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8161 attr.form = DW_FORM_ref_addr;
9c541725 8162 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8163 die = follow_die_ref (NULL, &attr, &ref_cu);
8164
43816ebc 8165 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8166 }
8167 }
8168
72bf9492
DJ
8169 parent_scope = partial_die_parent_scope (pdi, cu);
8170 if (parent_scope == NULL)
8171 return NULL;
8172 else
43816ebc
TT
8173 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8174 pdi->name, 0, cu));
c906108c
SS
8175}
8176
8177static void
72bf9492 8178add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8179{
518817b3
SM
8180 struct dwarf2_per_objfile *dwarf2_per_objfile
8181 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8182 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8183 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8184 CORE_ADDR addr = 0;
15d034d0 8185 const char *actual_name = NULL;
e142c38c
DJ
8186 CORE_ADDR baseaddr;
8187
b3b3bada 8188 baseaddr = objfile->text_section_offset ();
c906108c 8189
43816ebc
TT
8190 gdb::unique_xmalloc_ptr<char> built_actual_name
8191 = partial_die_full_name (pdi, cu);
15d034d0 8192 if (built_actual_name != NULL)
43816ebc 8193 actual_name = built_actual_name.get ();
63d06c5c 8194
72bf9492
DJ
8195 if (actual_name == NULL)
8196 actual_name = pdi->name;
8197
c906108c
SS
8198 switch (pdi->tag)
8199 {
b1dc1806 8200 case DW_TAG_inlined_subroutine:
c906108c 8201 case DW_TAG_subprogram:
79748972
TT
8202 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8203 - baseaddr);
0a4b0913
AB
8204 if (pdi->is_external
8205 || cu->language == language_ada
8206 || (cu->language == language_fortran
8207 && pdi->die_parent != NULL
8208 && pdi->die_parent->tag == DW_TAG_subprogram))
8209 {
8210 /* Normally, only "external" DIEs are part of the global scope.
8211 But in Ada and Fortran, we want to be able to access nested
8212 procedures globally. So all Ada and Fortran subprograms are
8213 stored in the global scope. */
31edb802 8214 add_psymbol_to_list (actual_name,
15d034d0 8215 built_actual_name != NULL,
f47fb265 8216 VAR_DOMAIN, LOC_BLOCK,
79748972 8217 SECT_OFF_TEXT (objfile),
75aedd27 8218 psymbol_placement::GLOBAL,
79748972
TT
8219 addr,
8220 cu->language, objfile);
c906108c
SS
8221 }
8222 else
8223 {
31edb802 8224 add_psymbol_to_list (actual_name,
15d034d0 8225 built_actual_name != NULL,
f47fb265 8226 VAR_DOMAIN, LOC_BLOCK,
79748972 8227 SECT_OFF_TEXT (objfile),
75aedd27 8228 psymbol_placement::STATIC,
1762568f 8229 addr, cu->language, objfile);
c906108c 8230 }
0c1b455e
TT
8231
8232 if (pdi->main_subprogram && actual_name != NULL)
8233 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8234 break;
72929c62 8235 case DW_TAG_constant:
31edb802 8236 add_psymbol_to_list (actual_name,
75aedd27
TT
8237 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8238 -1, (pdi->is_external
8239 ? psymbol_placement::GLOBAL
8240 : psymbol_placement::STATIC),
8241 0, cu->language, objfile);
72929c62 8242 break;
c906108c 8243 case DW_TAG_variable:
95554aad
TT
8244 if (pdi->d.locdesc)
8245 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8246
95554aad 8247 if (pdi->d.locdesc
caac4577
JG
8248 && addr == 0
8249 && !dwarf2_per_objfile->has_section_at_zero)
8250 {
8251 /* A global or static variable may also have been stripped
8252 out by the linker if unused, in which case its address
8253 will be nullified; do not add such variables into partial
8254 symbol table then. */
8255 }
8256 else if (pdi->is_external)
c906108c
SS
8257 {
8258 /* Global Variable.
8259 Don't enter into the minimal symbol tables as there is
8260 a minimal symbol table entry from the ELF symbols already.
8261 Enter into partial symbol table if it has a location
8262 descriptor or a type.
8263 If the location descriptor is missing, new_symbol will create
8264 a LOC_UNRESOLVED symbol, the address of the variable will then
8265 be determined from the minimal symbol table whenever the variable
8266 is referenced.
8267 The address for the partial symbol table entry is not
8268 used by GDB, but it comes in handy for debugging partial symbol
8269 table building. */
8270
95554aad 8271 if (pdi->d.locdesc || pdi->has_type)
31edb802 8272 add_psymbol_to_list (actual_name,
15d034d0 8273 built_actual_name != NULL,
f47fb265 8274 VAR_DOMAIN, LOC_STATIC,
79748972 8275 SECT_OFF_TEXT (objfile),
75aedd27 8276 psymbol_placement::GLOBAL,
79748972 8277 addr, cu->language, objfile);
c906108c
SS
8278 }
8279 else
8280 {
ff908ebf
AW
8281 int has_loc = pdi->d.locdesc != NULL;
8282
8283 /* Static Variable. Skip symbols whose value we cannot know (those
8284 without location descriptors or constant values). */
8285 if (!has_loc && !pdi->has_const_value)
43816ebc 8286 return;
ff908ebf 8287
31edb802 8288 add_psymbol_to_list (actual_name,
15d034d0 8289 built_actual_name != NULL,
f47fb265 8290 VAR_DOMAIN, LOC_STATIC,
79748972 8291 SECT_OFF_TEXT (objfile),
75aedd27 8292 psymbol_placement::STATIC,
79748972 8293 has_loc ? addr : 0,
f47fb265 8294 cu->language, objfile);
c906108c
SS
8295 }
8296 break;
8297 case DW_TAG_typedef:
8298 case DW_TAG_base_type:
a02abb62 8299 case DW_TAG_subrange_type:
31edb802 8300 add_psymbol_to_list (actual_name,
15d034d0 8301 built_actual_name != NULL,
79748972 8302 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8303 psymbol_placement::STATIC,
1762568f 8304 0, cu->language, objfile);
c906108c 8305 break;
74921315 8306 case DW_TAG_imported_declaration:
72bf9492 8307 case DW_TAG_namespace:
31edb802 8308 add_psymbol_to_list (actual_name,
15d034d0 8309 built_actual_name != NULL,
79748972 8310 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8311 psymbol_placement::GLOBAL,
1762568f 8312 0, cu->language, objfile);
72bf9492 8313 break;
530e8392 8314 case DW_TAG_module:
a5fd13a9
BH
8315 /* With Fortran 77 there might be a "BLOCK DATA" module
8316 available without any name. If so, we skip the module as it
8317 doesn't bring any value. */
8318 if (actual_name != nullptr)
31edb802 8319 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8320 built_actual_name != NULL,
8321 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8322 psymbol_placement::GLOBAL,
8323 0, cu->language, objfile);
530e8392 8324 break;
c906108c 8325 case DW_TAG_class_type:
680b30c7 8326 case DW_TAG_interface_type:
c906108c
SS
8327 case DW_TAG_structure_type:
8328 case DW_TAG_union_type:
8329 case DW_TAG_enumeration_type:
fa4028e9
JB
8330 /* Skip external references. The DWARF standard says in the section
8331 about "Structure, Union, and Class Type Entries": "An incomplete
8332 structure, union or class type is represented by a structure,
8333 union or class entry that does not have a byte size attribute
8334 and that has a DW_AT_declaration attribute." */
8335 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8336 return;
fa4028e9 8337
63d06c5c
DC
8338 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8339 static vs. global. */
31edb802 8340 add_psymbol_to_list (actual_name,
15d034d0 8341 built_actual_name != NULL,
79748972 8342 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8343 cu->language == language_cplus
75aedd27
TT
8344 ? psymbol_placement::GLOBAL
8345 : psymbol_placement::STATIC,
1762568f 8346 0, cu->language, objfile);
c906108c 8347
c906108c
SS
8348 break;
8349 case DW_TAG_enumerator:
31edb802 8350 add_psymbol_to_list (actual_name,
15d034d0 8351 built_actual_name != NULL,
79748972 8352 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8353 cu->language == language_cplus
75aedd27
TT
8354 ? psymbol_placement::GLOBAL
8355 : psymbol_placement::STATIC,
1762568f 8356 0, cu->language, objfile);
c906108c
SS
8357 break;
8358 default:
8359 break;
8360 }
8361}
8362
5c4e30ca
DC
8363/* Read a partial die corresponding to a namespace; also, add a symbol
8364 corresponding to that namespace to the symbol table. NAMESPACE is
8365 the name of the enclosing namespace. */
91c24f0a 8366
72bf9492
DJ
8367static void
8368add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8369 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8370 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8371{
72bf9492 8372 /* Add a symbol for the namespace. */
e7c27a73 8373
72bf9492 8374 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8375
8376 /* Now scan partial symbols in that namespace. */
8377
91c24f0a 8378 if (pdi->has_children)
cdc07690 8379 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8380}
8381
5d7cb8df
JK
8382/* Read a partial die corresponding to a Fortran module. */
8383
8384static void
8385add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8386 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8387{
530e8392
KB
8388 /* Add a symbol for the namespace. */
8389
8390 add_partial_symbol (pdi, cu);
8391
f55ee35c 8392 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8393
8394 if (pdi->has_children)
cdc07690 8395 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8396}
8397
b1dc1806
XR
8398/* Read a partial die corresponding to a subprogram or an inlined
8399 subprogram and create a partial symbol for that subprogram.
8400 When the CU language allows it, this routine also defines a partial
8401 symbol for each nested subprogram that this subprogram contains.
8402 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8403 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8404
cdc07690
YQ
8405 PDI may also be a lexical block, in which case we simply search
8406 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8407 Again, this is only performed when the CU language allows this
8408 type of definitions. */
8409
8410static void
8411add_partial_subprogram (struct partial_die_info *pdi,
8412 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8413 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8414{
b1dc1806 8415 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8416 {
8417 if (pdi->has_pc_info)
8418 {
8419 if (pdi->lowpc < *lowpc)
8420 *lowpc = pdi->lowpc;
8421 if (pdi->highpc > *highpc)
8422 *highpc = pdi->highpc;
cdc07690 8423 if (set_addrmap)
5734ee8b 8424 {
518817b3 8425 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8426 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8427 CORE_ADDR baseaddr;
b926417a
TT
8428 CORE_ADDR this_highpc;
8429 CORE_ADDR this_lowpc;
5734ee8b 8430
b3b3bada 8431 baseaddr = objfile->text_section_offset ();
b926417a
TT
8432 this_lowpc
8433 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8434 pdi->lowpc + baseaddr)
8435 - baseaddr);
8436 this_highpc
8437 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8438 pdi->highpc + baseaddr)
8439 - baseaddr);
d320c2b5 8440 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8441 this_lowpc, this_highpc - 1,
9291a0cd 8442 cu->per_cu->v.psymtab);
5734ee8b 8443 }
481860b3
GB
8444 }
8445
8446 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8447 {
bc30ff58 8448 if (!pdi->is_declaration)
e8d05480
JB
8449 /* Ignore subprogram DIEs that do not have a name, they are
8450 illegal. Do not emit a complaint at this point, we will
8451 do so when we convert this psymtab into a symtab. */
8452 if (pdi->name)
8453 add_partial_symbol (pdi, cu);
bc30ff58
JB
8454 }
8455 }
6e70227d 8456
bc30ff58
JB
8457 if (! pdi->has_children)
8458 return;
8459
0a4b0913 8460 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8461 {
8462 pdi = pdi->die_child;
8463 while (pdi != NULL)
8464 {
52356b79 8465 pdi->fixup (cu);
bc30ff58 8466 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8467 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8468 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8469 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8470 pdi = pdi->die_sibling;
8471 }
8472 }
8473}
8474
91c24f0a
DC
8475/* Read a partial die corresponding to an enumeration type. */
8476
72bf9492
DJ
8477static void
8478add_partial_enumeration (struct partial_die_info *enum_pdi,
8479 struct dwarf2_cu *cu)
91c24f0a 8480{
72bf9492 8481 struct partial_die_info *pdi;
91c24f0a
DC
8482
8483 if (enum_pdi->name != NULL)
72bf9492
DJ
8484 add_partial_symbol (enum_pdi, cu);
8485
8486 pdi = enum_pdi->die_child;
8487 while (pdi)
91c24f0a 8488 {
72bf9492 8489 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8490 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8491 else
72bf9492
DJ
8492 add_partial_symbol (pdi, cu);
8493 pdi = pdi->die_sibling;
91c24f0a 8494 }
91c24f0a
DC
8495}
8496
6caca83c
CC
8497/* Return the initial uleb128 in the die at INFO_PTR. */
8498
8499static unsigned int
d521ce57 8500peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8501{
8502 unsigned int bytes_read;
8503
8504 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8505}
8506
685af9cd
TT
8507/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8508 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8509
4bb7a0a7
DJ
8510 Return the corresponding abbrev, or NULL if the number is zero (indicating
8511 an empty DIE). In either case *BYTES_READ will be set to the length of
8512 the initial number. */
8513
8514static struct abbrev_info *
685af9cd
TT
8515peek_die_abbrev (const die_reader_specs &reader,
8516 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8517{
685af9cd 8518 dwarf2_cu *cu = reader.cu;
518817b3 8519 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8520 unsigned int abbrev_number
8521 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8522
8523 if (abbrev_number == 0)
8524 return NULL;
8525
685af9cd 8526 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8527 if (!abbrev)
8528 {
422b9917 8529 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8530 " at offset %s [in module %s]"),
422b9917 8531 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8532 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8533 }
8534
8535 return abbrev;
8536}
8537
93311388
DE
8538/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8539 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8540 DIE. Any children of the skipped DIEs will also be skipped. */
8541
d521ce57
TT
8542static const gdb_byte *
8543skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8544{
4bb7a0a7
DJ
8545 while (1)
8546 {
685af9cd
TT
8547 unsigned int bytes_read;
8548 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8549
4bb7a0a7
DJ
8550 if (abbrev == NULL)
8551 return info_ptr + bytes_read;
8552 else
dee91e82 8553 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8554 }
8555}
8556
93311388
DE
8557/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8558 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8559 abbrev corresponding to that skipped uleb128 should be passed in
8560 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8561 children. */
8562
d521ce57
TT
8563static const gdb_byte *
8564skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8565 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8566{
8567 unsigned int bytes_read;
8568 struct attribute attr;
dee91e82
DE
8569 bfd *abfd = reader->abfd;
8570 struct dwarf2_cu *cu = reader->cu;
d521ce57 8571 const gdb_byte *buffer = reader->buffer;
f664829e 8572 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8573 unsigned int form, i;
8574
8575 for (i = 0; i < abbrev->num_attrs; i++)
8576 {
8577 /* The only abbrev we care about is DW_AT_sibling. */
8578 if (abbrev->attrs[i].name == DW_AT_sibling)
8579 {
18a8505e
AT
8580 bool ignored;
8581 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8582 &ignored);
4bb7a0a7 8583 if (attr.form == DW_FORM_ref_addr)
b98664d3 8584 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8585 else
b9502d3f 8586 {
9c541725
PA
8587 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8588 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8589
8590 if (sibling_ptr < info_ptr)
b98664d3 8591 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
8592 else if (sibling_ptr > reader->buffer_end)
8593 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8594 else
8595 return sibling_ptr;
8596 }
4bb7a0a7
DJ
8597 }
8598
8599 /* If it isn't DW_AT_sibling, skip this attribute. */
8600 form = abbrev->attrs[i].form;
8601 skip_attribute:
8602 switch (form)
8603 {
4bb7a0a7 8604 case DW_FORM_ref_addr:
ae411497
TT
8605 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8606 and later it is offset sized. */
8607 if (cu->header.version == 2)
8608 info_ptr += cu->header.addr_size;
8609 else
8610 info_ptr += cu->header.offset_size;
8611 break;
36586728
TT
8612 case DW_FORM_GNU_ref_alt:
8613 info_ptr += cu->header.offset_size;
8614 break;
ae411497 8615 case DW_FORM_addr:
4bb7a0a7
DJ
8616 info_ptr += cu->header.addr_size;
8617 break;
8618 case DW_FORM_data1:
8619 case DW_FORM_ref1:
8620 case DW_FORM_flag:
8fe0f950 8621 case DW_FORM_strx1:
4bb7a0a7
DJ
8622 info_ptr += 1;
8623 break;
2dc7f7b3 8624 case DW_FORM_flag_present:
43988095 8625 case DW_FORM_implicit_const:
2dc7f7b3 8626 break;
4bb7a0a7
DJ
8627 case DW_FORM_data2:
8628 case DW_FORM_ref2:
8fe0f950 8629 case DW_FORM_strx2:
4bb7a0a7
DJ
8630 info_ptr += 2;
8631 break;
8fe0f950
AT
8632 case DW_FORM_strx3:
8633 info_ptr += 3;
8634 break;
4bb7a0a7
DJ
8635 case DW_FORM_data4:
8636 case DW_FORM_ref4:
8fe0f950 8637 case DW_FORM_strx4:
4bb7a0a7
DJ
8638 info_ptr += 4;
8639 break;
8640 case DW_FORM_data8:
8641 case DW_FORM_ref8:
55f1336d 8642 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8643 info_ptr += 8;
8644 break;
0224619f
JK
8645 case DW_FORM_data16:
8646 info_ptr += 16;
8647 break;
4bb7a0a7 8648 case DW_FORM_string:
9b1c24c8 8649 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8650 info_ptr += bytes_read;
8651 break;
2dc7f7b3 8652 case DW_FORM_sec_offset:
4bb7a0a7 8653 case DW_FORM_strp:
36586728 8654 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8655 info_ptr += cu->header.offset_size;
8656 break;
2dc7f7b3 8657 case DW_FORM_exprloc:
4bb7a0a7
DJ
8658 case DW_FORM_block:
8659 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8660 info_ptr += bytes_read;
8661 break;
8662 case DW_FORM_block1:
8663 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8664 break;
8665 case DW_FORM_block2:
8666 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8667 break;
8668 case DW_FORM_block4:
8669 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8670 break;
336d760d 8671 case DW_FORM_addrx:
cf532bd1 8672 case DW_FORM_strx:
4bb7a0a7
DJ
8673 case DW_FORM_sdata:
8674 case DW_FORM_udata:
8675 case DW_FORM_ref_udata:
3019eac3
DE
8676 case DW_FORM_GNU_addr_index:
8677 case DW_FORM_GNU_str_index:
18a8505e 8678 case DW_FORM_rnglistx:
d521ce57 8679 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8680 break;
8681 case DW_FORM_indirect:
8682 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8683 info_ptr += bytes_read;
8684 /* We need to continue parsing from here, so just go back to
8685 the top. */
8686 goto skip_attribute;
8687
8688 default:
3e43a32a
MS
8689 error (_("Dwarf Error: Cannot handle %s "
8690 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8691 dwarf_form_name (form),
8692 bfd_get_filename (abfd));
8693 }
8694 }
8695
8696 if (abbrev->has_children)
dee91e82 8697 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8698 else
8699 return info_ptr;
8700}
8701
93311388 8702/* Locate ORIG_PDI's sibling.
dee91e82 8703 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8704
d521ce57 8705static const gdb_byte *
dee91e82
DE
8706locate_pdi_sibling (const struct die_reader_specs *reader,
8707 struct partial_die_info *orig_pdi,
d521ce57 8708 const gdb_byte *info_ptr)
91c24f0a
DC
8709{
8710 /* Do we know the sibling already? */
72bf9492 8711
91c24f0a
DC
8712 if (orig_pdi->sibling)
8713 return orig_pdi->sibling;
8714
8715 /* Are there any children to deal with? */
8716
8717 if (!orig_pdi->has_children)
8718 return info_ptr;
8719
4bb7a0a7 8720 /* Skip the children the long way. */
91c24f0a 8721
dee91e82 8722 return skip_children (reader, info_ptr);
91c24f0a
DC
8723}
8724
257e7a09 8725/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8726 not NULL. */
c906108c 8727
891813be
TT
8728void
8729dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8730{
ed2dc618
SM
8731 struct dwarf2_per_objfile *dwarf2_per_objfile
8732 = get_dwarf2_per_objfile (objfile);
8733
077cbab2
TT
8734 gdb_assert (!readin);
8735 /* If this psymtab is constructed from a debug-only objfile, the
8736 has_section_at_zero flag will not necessarily be correct. We
8737 can get the correct value for this flag by looking at the data
8738 associated with the (presumably stripped) associated objfile. */
8739 if (objfile->separate_debug_objfile_backlink)
c906108c 8740 {
077cbab2
TT
8741 struct dwarf2_per_objfile *dpo_backlink
8742 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8743
077cbab2
TT
8744 dwarf2_per_objfile->has_section_at_zero
8745 = dpo_backlink->has_section_at_zero;
8746 }
98bfdba5 8747
8566b89b 8748 expand_psymtab (objfile);
95554aad 8749
ed2dc618 8750 process_cu_includes (dwarf2_per_objfile);
c906108c 8751}
9cdd5dbd
DE
8752\f
8753/* Reading in full CUs. */
c906108c 8754
10b3939b
DJ
8755/* Add PER_CU to the queue. */
8756
8757static void
95554aad
TT
8758queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8759 enum language pretend_language)
10b3939b 8760{
10b3939b 8761 per_cu->queued = 1;
39856def 8762 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8763}
8764
89e63ee4
DE
8765/* If PER_CU is not yet queued, add it to the queue.
8766 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8767 dependency.
0907af0c 8768 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8769 meaning either PER_CU is already queued or it is already loaded.
8770
8771 N.B. There is an invariant here that if a CU is queued then it is loaded.
8772 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8773
8774static int
89e63ee4 8775maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8776 struct dwarf2_per_cu_data *per_cu,
8777 enum language pretend_language)
8778{
8779 /* We may arrive here during partial symbol reading, if we need full
8780 DIEs to process an unusual case (e.g. template arguments). Do
8781 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8782 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8783 {
8784 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8785 return 1;
8786 return 0;
8787 }
8788
8789 /* Mark the dependence relation so that we don't flush PER_CU
8790 too early. */
89e63ee4
DE
8791 if (dependent_cu != NULL)
8792 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8793
8794 /* If it's already on the queue, we have nothing to do. */
8795 if (per_cu->queued)
8796 return 0;
8797
8798 /* If the compilation unit is already loaded, just mark it as
8799 used. */
8800 if (per_cu->cu != NULL)
8801 {
8802 per_cu->cu->last_used = 0;
8803 return 0;
8804 }
8805
8806 /* Add it to the queue. */
8807 queue_comp_unit (per_cu, pretend_language);
8808
8809 return 1;
8810}
8811
10b3939b
DJ
8812/* Process the queue. */
8813
8814static void
ed2dc618 8815process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8816{
b4f54984 8817 if (dwarf_read_debug)
45cfd468
DE
8818 {
8819 fprintf_unfiltered (gdb_stdlog,
8820 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8821 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8822 }
8823
03dd20cc
DJ
8824 /* The queue starts out with one item, but following a DIE reference
8825 may load a new CU, adding it to the end of the queue. */
39856def 8826 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8827 {
39856def
TT
8828 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8829
cc12ce38 8830 if ((dwarf2_per_objfile->using_index
39856def
TT
8831 ? !item.per_cu->v.quick->compunit_symtab
8832 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8833 /* Skip dummy CUs. */
39856def 8834 && item.per_cu->cu != NULL)
f4dc4d17 8835 {
39856def 8836 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8837 unsigned int debug_print_threshold;
247f5c4f 8838 char buf[100];
f4dc4d17 8839
247f5c4f 8840 if (per_cu->is_debug_types)
f4dc4d17 8841 {
247f5c4f
DE
8842 struct signatured_type *sig_type =
8843 (struct signatured_type *) per_cu;
8844
9d8780f0 8845 sprintf (buf, "TU %s at offset %s",
73be47f5 8846 hex_string (sig_type->signature),
9d8780f0 8847 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8848 /* There can be 100s of TUs.
8849 Only print them in verbose mode. */
8850 debug_print_threshold = 2;
f4dc4d17 8851 }
247f5c4f 8852 else
73be47f5 8853 {
9d8780f0
SM
8854 sprintf (buf, "CU at offset %s",
8855 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8856 debug_print_threshold = 1;
8857 }
247f5c4f 8858
b4f54984 8859 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8860 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8861
8862 if (per_cu->is_debug_types)
39856def 8863 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8864 else
39856def 8865 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8866
b4f54984 8867 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8868 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8869 }
10b3939b 8870
39856def
TT
8871 item.per_cu->queued = 0;
8872 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8873 }
8874
b4f54984 8875 if (dwarf_read_debug)
45cfd468
DE
8876 {
8877 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8878 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8879 }
10b3939b
DJ
8880}
8881
10b3939b
DJ
8882/* Read in full symbols for PST, and anything it depends on. */
8883
8566b89b
TT
8884void
8885dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8886{
8566b89b 8887 if (readin)
95554aad
TT
8888 return;
8889
0494dbec 8890 read_dependencies (objfile);
aaa75496 8891
b83470bf
TT
8892 dw2_do_instantiate_symtab (per_cu_data, false);
8893 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8894}
8895
dee91e82
DE
8896/* Trivial hash function for die_info: the hash value of a DIE
8897 is its offset in .debug_info for this objfile. */
10b3939b 8898
dee91e82
DE
8899static hashval_t
8900die_hash (const void *item)
10b3939b 8901{
9a3c8263 8902 const struct die_info *die = (const struct die_info *) item;
6502dd73 8903
9c541725 8904 return to_underlying (die->sect_off);
dee91e82 8905}
63d06c5c 8906
dee91e82
DE
8907/* Trivial comparison function for die_info structures: two DIEs
8908 are equal if they have the same offset. */
98bfdba5 8909
dee91e82
DE
8910static int
8911die_eq (const void *item_lhs, const void *item_rhs)
8912{
9a3c8263
SM
8913 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8914 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8915
9c541725 8916 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8917}
c906108c 8918
c0ab21c2 8919/* Load the DIEs associated with PER_CU into memory. */
c906108c 8920
dee91e82 8921static void
c0ab21c2
TT
8922load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8923 bool skip_partial,
8924 enum language pretend_language)
dee91e82 8925{
c0ab21c2
TT
8926 gdb_assert (! this_cu->is_debug_types);
8927
6751ebae 8928 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8929 if (reader.dummy_p)
8930 return;
8931
8932 struct dwarf2_cu *cu = reader.cu;
8933 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8934
dee91e82
DE
8935 gdb_assert (cu->die_hash == NULL);
8936 cu->die_hash =
8937 htab_create_alloc_ex (cu->header.length / 12,
8938 die_hash,
8939 die_eq,
8940 NULL,
8941 &cu->comp_unit_obstack,
8942 hashtab_obstack_allocate,
8943 dummy_obstack_deallocate);
e142c38c 8944
3e225074 8945 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8946 reader.comp_unit_die->child
8947 = read_die_and_siblings (&reader, reader.info_ptr,
8948 &info_ptr, reader.comp_unit_die);
8949 cu->dies = reader.comp_unit_die;
dee91e82 8950 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8951
8952 /* We try not to read any attributes in this function, because not
9cdd5dbd 8953 all CUs needed for references have been loaded yet, and symbol
10b3939b 8954 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8955 or we won't be able to build types correctly.
8956 Similarly, if we do not read the producer, we can not apply
8957 producer-specific interpretation. */
c0ab21c2 8958 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8959
8960 reader.keep ();
10b3939b
DJ
8961}
8962
3da10d80
KS
8963/* Add a DIE to the delayed physname list. */
8964
8965static void
8966add_to_method_list (struct type *type, int fnfield_index, int index,
8967 const char *name, struct die_info *die,
8968 struct dwarf2_cu *cu)
8969{
8970 struct delayed_method_info mi;
8971 mi.type = type;
8972 mi.fnfield_index = fnfield_index;
8973 mi.index = index;
8974 mi.name = name;
8975 mi.die = die;
c89b44cd 8976 cu->method_list.push_back (mi);
3da10d80
KS
8977}
8978
3693fdb3
PA
8979/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8980 "const" / "volatile". If so, decrements LEN by the length of the
8981 modifier and return true. Otherwise return false. */
8982
8983template<size_t N>
8984static bool
8985check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8986{
8987 size_t mod_len = sizeof (mod) - 1;
8988 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8989 {
8990 len -= mod_len;
8991 return true;
8992 }
8993 return false;
8994}
8995
3da10d80
KS
8996/* Compute the physnames of any methods on the CU's method list.
8997
8998 The computation of method physnames is delayed in order to avoid the
8999 (bad) condition that one of the method's formal parameters is of an as yet
9000 incomplete type. */
9001
9002static void
9003compute_delayed_physnames (struct dwarf2_cu *cu)
9004{
3693fdb3 9005 /* Only C++ delays computing physnames. */
c89b44cd 9006 if (cu->method_list.empty ())
3693fdb3
PA
9007 return;
9008 gdb_assert (cu->language == language_cplus);
9009
52941706 9010 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9011 {
1d06ead6 9012 const char *physname;
3da10d80 9013 struct fn_fieldlist *fn_flp
c89b44cd
TT
9014 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9015 physname = dwarf2_physname (mi.name, mi.die, cu);
9016 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9017 = physname ? physname : "";
3693fdb3
PA
9018
9019 /* Since there's no tag to indicate whether a method is a
9020 const/volatile overload, extract that information out of the
9021 demangled name. */
9022 if (physname != NULL)
9023 {
9024 size_t len = strlen (physname);
9025
9026 while (1)
9027 {
9028 if (physname[len] == ')') /* shortcut */
9029 break;
9030 else if (check_modifier (physname, len, " const"))
c89b44cd 9031 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9032 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9033 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9034 else
9035 break;
9036 }
9037 }
3da10d80 9038 }
c89b44cd
TT
9039
9040 /* The list is no longer needed. */
9041 cu->method_list.clear ();
3da10d80
KS
9042}
9043
a766d390
DE
9044/* Go objects should be embedded in a DW_TAG_module DIE,
9045 and it's not clear if/how imported objects will appear.
9046 To keep Go support simple until that's worked out,
9047 go back through what we've read and create something usable.
9048 We could do this while processing each DIE, and feels kinda cleaner,
9049 but that way is more invasive.
9050 This is to, for example, allow the user to type "p var" or "b main"
9051 without having to specify the package name, and allow lookups
9052 of module.object to work in contexts that use the expression
9053 parser. */
9054
9055static void
9056fixup_go_packaging (struct dwarf2_cu *cu)
9057{
421d1616 9058 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9059 struct pending *list;
9060 int i;
9061
c24bdb02 9062 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9063 list != NULL;
9064 list = list->next)
a766d390
DE
9065 {
9066 for (i = 0; i < list->nsyms; ++i)
9067 {
9068 struct symbol *sym = list->symbol[i];
9069
c1b5c1eb 9070 if (sym->language () == language_go
a766d390
DE
9071 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9072 {
421d1616
TT
9073 gdb::unique_xmalloc_ptr<char> this_package_name
9074 (go_symbol_package_name (sym));
a766d390
DE
9075
9076 if (this_package_name == NULL)
9077 continue;
9078 if (package_name == NULL)
421d1616 9079 package_name = std::move (this_package_name);
a766d390
DE
9080 else
9081 {
518817b3
SM
9082 struct objfile *objfile
9083 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9084 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9085 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9086 (symbol_symtab (sym) != NULL
9087 ? symtab_to_filename_for_display
9088 (symbol_symtab (sym))
e3b94546 9089 : objfile_name (objfile)),
421d1616 9090 this_package_name.get (), package_name.get ());
a766d390
DE
9091 }
9092 }
9093 }
9094 }
9095
9096 if (package_name != NULL)
9097 {
518817b3 9098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9099 const char *saved_package_name
421d1616 9100 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9101 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9102 saved_package_name);
a766d390
DE
9103 struct symbol *sym;
9104
e623cf5d 9105 sym = allocate_symbol (objfile);
d3ecddab 9106 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9107 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9108 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9109 e.g., "main" finds the "main" module and not C's main(). */
9110 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9111 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9112 SYMBOL_TYPE (sym) = type;
9113
c24bdb02 9114 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9115 }
9116}
9117
c9317f21
TT
9118/* Allocate a fully-qualified name consisting of the two parts on the
9119 obstack. */
9120
9121static const char *
9122rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9123{
9124 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9125}
9126
9127/* A helper that allocates a struct discriminant_info to attach to a
9128 union type. */
9129
9130static struct discriminant_info *
9131alloc_discriminant_info (struct type *type, int discriminant_index,
9132 int default_index)
9133{
9134 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9135 gdb_assert (discriminant_index == -1
9136 || (discriminant_index >= 0
9137 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9138 gdb_assert (default_index == -1
c7b15a66 9139 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9140
9141 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9142
9143 struct discriminant_info *disc
9144 = ((struct discriminant_info *)
9145 TYPE_ZALLOC (type,
9146 offsetof (struct discriminant_info, discriminants)
9147 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9148 disc->default_index = default_index;
9149 disc->discriminant_index = discriminant_index;
9150
9151 struct dynamic_prop prop;
9152 prop.kind = PROP_UNDEFINED;
9153 prop.data.baton = disc;
9154
9155 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9156
9157 return disc;
9158}
9159
9160/* Some versions of rustc emitted enums in an unusual way.
9161
9162 Ordinary enums were emitted as unions. The first element of each
9163 structure in the union was named "RUST$ENUM$DISR". This element
9164 held the discriminant.
9165
9166 These versions of Rust also implemented the "non-zero"
9167 optimization. When the enum had two values, and one is empty and
9168 the other holds a pointer that cannot be zero, the pointer is used
9169 as the discriminant, with a zero value meaning the empty variant.
9170 Here, the union's first member is of the form
9171 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9172 where the fieldnos are the indices of the fields that should be
9173 traversed in order to find the field (which may be several fields deep)
9174 and the variantname is the name of the variant of the case when the
9175 field is zero.
9176
9177 This function recognizes whether TYPE is of one of these forms,
9178 and, if so, smashes it to be a variant type. */
9179
9180static void
9181quirk_rust_enum (struct type *type, struct objfile *objfile)
9182{
9183 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9184
9185 /* We don't need to deal with empty enums. */
9186 if (TYPE_NFIELDS (type) == 0)
9187 return;
9188
9189#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9190 if (TYPE_NFIELDS (type) == 1
9191 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9192 {
9193 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9194
9195 /* Decode the field name to find the offset of the
9196 discriminant. */
9197 ULONGEST bit_offset = 0;
9198 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9199 while (name[0] >= '0' && name[0] <= '9')
9200 {
9201 char *tail;
9202 unsigned long index = strtoul (name, &tail, 10);
9203 name = tail;
9204 if (*name != '$'
9205 || index >= TYPE_NFIELDS (field_type)
9206 || (TYPE_FIELD_LOC_KIND (field_type, index)
9207 != FIELD_LOC_KIND_BITPOS))
9208 {
b98664d3 9209 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9210 "[in module %s]"),
9211 TYPE_FIELD_NAME (type, 0),
9212 objfile_name (objfile));
9213 return;
9214 }
9215 ++name;
9216
9217 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9218 field_type = TYPE_FIELD_TYPE (field_type, index);
9219 }
9220
9221 /* Make a union to hold the variants. */
9222 struct type *union_type = alloc_type (objfile);
9223 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9224 TYPE_NFIELDS (union_type) = 3;
9225 TYPE_FIELDS (union_type)
9226 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9227 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9228 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9229
9230 /* Put the discriminant must at index 0. */
9231 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9232 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9233 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9234 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9235
9236 /* The order of fields doesn't really matter, so put the real
9237 field at index 1 and the data-less field at index 2. */
9238 struct discriminant_info *disc
9239 = alloc_discriminant_info (union_type, 0, 1);
9240 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9241 TYPE_FIELD_NAME (union_type, 1)
9242 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9243 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9244 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9245 TYPE_FIELD_NAME (union_type, 1));
9246
9247 const char *dataless_name
9248 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9249 name);
9250 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9251 dataless_name);
9252 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9253 /* NAME points into the original discriminant name, which
9254 already has the correct lifetime. */
9255 TYPE_FIELD_NAME (union_type, 2) = name;
9256 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9257 disc->discriminants[2] = 0;
9258
9259 /* Smash this type to be a structure type. We have to do this
9260 because the type has already been recorded. */
9261 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9262 TYPE_NFIELDS (type) = 1;
9263 TYPE_FIELDS (type)
9264 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9265
9266 /* Install the variant part. */
9267 TYPE_FIELD_TYPE (type, 0) = union_type;
9268 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9269 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9270 }
77c2dba3
TT
9271 /* A union with a single anonymous field is probably an old-style
9272 univariant enum. */
9273 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9274 {
c9317f21
TT
9275 /* Smash this type to be a structure type. We have to do this
9276 because the type has already been recorded. */
9277 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9278
9279 /* Make a union to hold the variants. */
9280 struct type *union_type = alloc_type (objfile);
9281 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9282 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9283 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9284 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9285 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9286
9287 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9288 const char *variant_name
9289 = rust_last_path_segment (TYPE_NAME (field_type));
9290 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9291 TYPE_NAME (field_type)
9292 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9293 TYPE_NAME (type), variant_name);
c9317f21
TT
9294
9295 /* Install the union in the outer struct type. */
9296 TYPE_NFIELDS (type) = 1;
9297 TYPE_FIELDS (type)
9298 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9299 TYPE_FIELD_TYPE (type, 0) = union_type;
9300 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9301 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9302
9303 alloc_discriminant_info (union_type, -1, 0);
9304 }
9305 else
9306 {
9307 struct type *disr_type = nullptr;
9308 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9309 {
9310 disr_type = TYPE_FIELD_TYPE (type, i);
9311
a037790e
TT
9312 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9313 {
9314 /* All fields of a true enum will be structs. */
9315 return;
9316 }
9317 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9318 {
9319 /* Could be data-less variant, so keep going. */
a037790e 9320 disr_type = nullptr;
c9317f21
TT
9321 }
9322 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9323 "RUST$ENUM$DISR") != 0)
9324 {
9325 /* Not a Rust enum. */
9326 return;
9327 }
9328 else
9329 {
9330 /* Found one. */
9331 break;
9332 }
9333 }
9334
9335 /* If we got here without a discriminant, then it's probably
9336 just a union. */
9337 if (disr_type == nullptr)
9338 return;
9339
9340 /* Smash this type to be a structure type. We have to do this
9341 because the type has already been recorded. */
9342 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9343
9344 /* Make a union to hold the variants. */
9345 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9346 struct type *union_type = alloc_type (objfile);
9347 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9348 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9349 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9350 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9351 TYPE_FIELDS (union_type)
9352 = (struct field *) TYPE_ZALLOC (union_type,
9353 (TYPE_NFIELDS (union_type)
9354 * sizeof (struct field)));
9355
9356 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9357 TYPE_NFIELDS (type) * sizeof (struct field));
9358
9359 /* Install the discriminant at index 0 in the union. */
9360 TYPE_FIELD (union_type, 0) = *disr_field;
9361 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9362 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9363
9364 /* Install the union in the outer struct type. */
9365 TYPE_FIELD_TYPE (type, 0) = union_type;
9366 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9367 TYPE_NFIELDS (type) = 1;
9368
9369 /* Set the size and offset of the union type. */
9370 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9371
9372 /* We need a way to find the correct discriminant given a
9373 variant name. For convenience we build a map here. */
9374 struct type *enum_type = FIELD_TYPE (*disr_field);
9375 std::unordered_map<std::string, ULONGEST> discriminant_map;
9376 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9377 {
9378 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9379 {
9380 const char *name
9381 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9382 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9383 }
9384 }
9385
9386 int n_fields = TYPE_NFIELDS (union_type);
9387 struct discriminant_info *disc
9388 = alloc_discriminant_info (union_type, 0, -1);
9389 /* Skip the discriminant here. */
9390 for (int i = 1; i < n_fields; ++i)
9391 {
9392 /* Find the final word in the name of this variant's type.
9393 That name can be used to look up the correct
9394 discriminant. */
9395 const char *variant_name
9396 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9397 i)));
9398
9399 auto iter = discriminant_map.find (variant_name);
9400 if (iter != discriminant_map.end ())
9401 disc->discriminants[i] = iter->second;
9402
bedda9ac 9403 /* Remove the discriminant field, if it exists. */
c9317f21 9404 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9405 if (TYPE_NFIELDS (sub_type) > 0)
9406 {
9407 --TYPE_NFIELDS (sub_type);
9408 ++TYPE_FIELDS (sub_type);
9409 }
c9317f21
TT
9410 TYPE_FIELD_NAME (union_type, i) = variant_name;
9411 TYPE_NAME (sub_type)
9412 = rust_fully_qualify (&objfile->objfile_obstack,
9413 TYPE_NAME (type), variant_name);
9414 }
9415 }
9416}
9417
9418/* Rewrite some Rust unions to be structures with variants parts. */
9419
9420static void
9421rust_union_quirks (struct dwarf2_cu *cu)
9422{
9423 gdb_assert (cu->language == language_rust);
52941706
SM
9424 for (type *type_ : cu->rust_unions)
9425 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9426 /* We don't need this any more. */
9427 cu->rust_unions.clear ();
c9317f21
TT
9428}
9429
95554aad
TT
9430/* Return the symtab for PER_CU. This works properly regardless of
9431 whether we're using the index or psymtabs. */
9432
43f3e411
DE
9433static struct compunit_symtab *
9434get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9435{
ed2dc618 9436 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9437 ? per_cu->v.quick->compunit_symtab
9438 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9439}
9440
9441/* A helper function for computing the list of all symbol tables
9442 included by PER_CU. */
9443
9444static void
4c39bc03 9445recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9446 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9447 struct dwarf2_per_cu_data *per_cu,
43f3e411 9448 struct compunit_symtab *immediate_parent)
95554aad
TT
9449{
9450 void **slot;
43f3e411 9451 struct compunit_symtab *cust;
95554aad
TT
9452
9453 slot = htab_find_slot (all_children, per_cu, INSERT);
9454 if (*slot != NULL)
9455 {
9456 /* This inclusion and its children have been processed. */
9457 return;
9458 }
9459
9460 *slot = per_cu;
9461 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9462 cust = get_compunit_symtab (per_cu);
9463 if (cust != NULL)
ec94af83
DE
9464 {
9465 /* If this is a type unit only add its symbol table if we haven't
9466 seen it yet (type unit per_cu's can share symtabs). */
9467 if (per_cu->is_debug_types)
9468 {
43f3e411 9469 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9470 if (*slot == NULL)
9471 {
43f3e411 9472 *slot = cust;
4c39bc03 9473 result->push_back (cust);
43f3e411
DE
9474 if (cust->user == NULL)
9475 cust->user = immediate_parent;
ec94af83
DE
9476 }
9477 }
9478 else
f9125b6c 9479 {
4c39bc03 9480 result->push_back (cust);
43f3e411
DE
9481 if (cust->user == NULL)
9482 cust->user = immediate_parent;
f9125b6c 9483 }
ec94af83 9484 }
95554aad 9485
ae640021
AB
9486 if (!per_cu->imported_symtabs_empty ())
9487 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9488 {
9489 recursively_compute_inclusions (result, all_children,
9490 all_type_symtabs, ptr, cust);
9491 }
95554aad
TT
9492}
9493
43f3e411 9494/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9495 PER_CU. */
9496
9497static void
43f3e411 9498compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9499{
f4dc4d17
DE
9500 gdb_assert (! per_cu->is_debug_types);
9501
ae640021 9502 if (!per_cu->imported_symtabs_empty ())
95554aad 9503 {
ae640021 9504 int len;
4c39bc03 9505 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9506 htab_t all_children, all_type_symtabs;
43f3e411 9507 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9508
9509 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9510 if (cust == NULL)
95554aad
TT
9511 return;
9512
9513 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9514 NULL, xcalloc, xfree);
ec94af83
DE
9515 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9516 NULL, xcalloc, xfree);
95554aad 9517
ae640021 9518 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9519 {
9520 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9521 all_type_symtabs, ptr, cust);
ec94af83 9522 }
95554aad 9523
ec94af83 9524 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9525 len = result_symtabs.size ();
43f3e411 9526 cust->includes
ed2dc618 9527 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9528 struct compunit_symtab *, len + 1);
4c39bc03
TT
9529 memcpy (cust->includes, result_symtabs.data (),
9530 len * sizeof (compunit_symtab *));
43f3e411 9531 cust->includes[len] = NULL;
95554aad 9532
95554aad 9533 htab_delete (all_children);
ec94af83 9534 htab_delete (all_type_symtabs);
95554aad
TT
9535 }
9536}
9537
9538/* Compute the 'includes' field for the symtabs of all the CUs we just
9539 read. */
9540
9541static void
ed2dc618 9542process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9543{
71b73764 9544 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9545 {
9546 if (! iter->is_debug_types)
43f3e411 9547 compute_compunit_symtab_includes (iter);
f4dc4d17 9548 }
95554aad 9549
c5d0225d 9550 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9551}
9552
9cdd5dbd 9553/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9554 already been loaded into memory. */
9555
9556static void
95554aad
TT
9557process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9558 enum language pretend_language)
10b3939b 9559{
10b3939b 9560 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9561 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9562 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9563 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9564 CORE_ADDR lowpc, highpc;
43f3e411 9565 struct compunit_symtab *cust;
10b3939b 9566 CORE_ADDR baseaddr;
4359dff1 9567 struct block *static_block;
3e29f34a 9568 CORE_ADDR addr;
10b3939b 9569
b3b3bada 9570 baseaddr = objfile->text_section_offset ();
10b3939b 9571
c89b44cd
TT
9572 /* Clear the list here in case something was left over. */
9573 cu->method_list.clear ();
10b3939b 9574
95554aad
TT
9575 cu->language = pretend_language;
9576 cu->language_defn = language_def (cu->language);
9577
c906108c 9578 /* Do line number decoding in read_file_scope () */
10b3939b 9579 process_die (cu->dies, cu);
c906108c 9580
a766d390
DE
9581 /* For now fudge the Go package. */
9582 if (cu->language == language_go)
9583 fixup_go_packaging (cu);
9584
5f48f8f3 9585 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9586 should be complete, and it should now be safe to compute all of the
9587 physnames. */
9588 compute_delayed_physnames (cu);
3da10d80 9589
c9317f21
TT
9590 if (cu->language == language_rust)
9591 rust_union_quirks (cu);
9592
fae299cd
DC
9593 /* Some compilers don't define a DW_AT_high_pc attribute for the
9594 compilation unit. If the DW_AT_high_pc is missing, synthesize
9595 it, by scanning the DIE's below the compilation unit. */
10b3939b 9596 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9597
3e29f34a 9598 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9599 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9600
9601 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9602 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9603 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9604 addrmap to help ensure it has an accurate map of pc values belonging to
9605 this comp unit. */
9606 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9607
c24bdb02 9608 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9609 SECT_OFF_TEXT (objfile),
9610 0);
c906108c 9611
43f3e411 9612 if (cust != NULL)
c906108c 9613 {
df15bd07 9614 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9615
8be455d7
JK
9616 /* Set symtab language to language from DW_AT_language. If the
9617 compilation is from a C file generated by language preprocessors, do
9618 not set the language if it was already deduced by start_subfile. */
43f3e411 9619 if (!(cu->language == language_c
40e3ad0e 9620 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9621 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9622
9623 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9624 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9625 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9626 there were bugs in prologue debug info, fixed later in GCC-4.5
9627 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9628
9629 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9630 needed, it would be wrong due to missing DW_AT_producer there.
9631
9632 Still one can confuse GDB by using non-standard GCC compilation
9633 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9634 */
ab260dad 9635 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9636 cust->locations_valid = 1;
e0d00bc7
JK
9637
9638 if (gcc_4_minor >= 5)
43f3e411 9639 cust->epilogue_unwind_valid = 1;
96408a79 9640
43f3e411 9641 cust->call_site_htab = cu->call_site_htab;
c906108c 9642 }
9291a0cd
TT
9643
9644 if (dwarf2_per_objfile->using_index)
43f3e411 9645 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9646 else
9647 {
891813be 9648 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9649 pst->compunit_symtab = cust;
6d94535f 9650 pst->readin = true;
9291a0cd 9651 }
c906108c 9652
95554aad 9653 /* Push it for inclusion processing later. */
c5d0225d 9654 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9655
9656 /* Not needed any more. */
c24bdb02 9657 cu->reset_builder ();
f4dc4d17 9658}
45cfd468 9659
f4dc4d17
DE
9660/* Generate full symbol information for type unit PER_CU, whose DIEs have
9661 already been loaded into memory. */
9662
9663static void
9664process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9665 enum language pretend_language)
9666{
9667 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9668 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9669 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9670 struct compunit_symtab *cust;
0186c6a7
DE
9671 struct signatured_type *sig_type;
9672
9673 gdb_assert (per_cu->is_debug_types);
9674 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9675
c89b44cd
TT
9676 /* Clear the list here in case something was left over. */
9677 cu->method_list.clear ();
f4dc4d17 9678
f4dc4d17
DE
9679 cu->language = pretend_language;
9680 cu->language_defn = language_def (cu->language);
9681
9682 /* The symbol tables are set up in read_type_unit_scope. */
9683 process_die (cu->dies, cu);
9684
9685 /* For now fudge the Go package. */
9686 if (cu->language == language_go)
9687 fixup_go_packaging (cu);
9688
5f48f8f3 9689 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9690 should be complete, and it should now be safe to compute all of the
9691 physnames. */
9692 compute_delayed_physnames (cu);
f4dc4d17 9693
c9317f21
TT
9694 if (cu->language == language_rust)
9695 rust_union_quirks (cu);
9696
f4dc4d17
DE
9697 /* TUs share symbol tables.
9698 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9699 of it with end_expandable_symtab. Otherwise, complete the addition of
9700 this TU's symbols to the existing symtab. */
43f3e411 9701 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9702 {
c24bdb02
KS
9703 buildsym_compunit *builder = cu->get_builder ();
9704 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9705 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9706
43f3e411 9707 if (cust != NULL)
f4dc4d17
DE
9708 {
9709 /* Set symtab language to language from DW_AT_language. If the
9710 compilation is from a C file generated by language preprocessors,
9711 do not set the language if it was already deduced by
9712 start_subfile. */
43f3e411
DE
9713 if (!(cu->language == language_c
9714 && COMPUNIT_FILETABS (cust)->language != language_c))
9715 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9716 }
9717 }
9718 else
9719 {
c24bdb02 9720 cu->get_builder ()->augment_type_symtab ();
43f3e411 9721 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9722 }
9723
9724 if (dwarf2_per_objfile->using_index)
43f3e411 9725 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9726 else
9727 {
891813be 9728 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9729 pst->compunit_symtab = cust;
6d94535f 9730 pst->readin = true;
45cfd468 9731 }
804d2729
TT
9732
9733 /* Not needed any more. */
c24bdb02 9734 cu->reset_builder ();
c906108c
SS
9735}
9736
95554aad
TT
9737/* Process an imported unit DIE. */
9738
9739static void
9740process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9741{
9742 struct attribute *attr;
9743
f4dc4d17
DE
9744 /* For now we don't handle imported units in type units. */
9745 if (cu->per_cu->is_debug_types)
9746 {
9747 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9748 " supported in type units [in module %s]"),
518817b3 9749 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9750 }
9751
95554aad
TT
9752 attr = dwarf2_attr (die, DW_AT_import, cu);
9753 if (attr != NULL)
9754 {
9c541725
PA
9755 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9756 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9757 dwarf2_per_cu_data *per_cu
e3b94546 9758 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9759 cu->per_cu->dwarf2_per_objfile);
95554aad 9760
69d751e3 9761 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9762 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9763 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9764
ae640021 9765 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9766 }
9767}
9768
4c8aa72d
PA
9769/* RAII object that represents a process_die scope: i.e.,
9770 starts/finishes processing a DIE. */
9771class process_die_scope
adde2bff 9772{
4c8aa72d
PA
9773public:
9774 process_die_scope (die_info *die, dwarf2_cu *cu)
9775 : m_die (die), m_cu (cu)
9776 {
9777 /* We should only be processing DIEs not already in process. */
9778 gdb_assert (!m_die->in_process);
9779 m_die->in_process = true;
9780 }
8c3cb9fa 9781
4c8aa72d
PA
9782 ~process_die_scope ()
9783 {
9784 m_die->in_process = false;
9785
9786 /* If we're done processing the DIE for the CU that owns the line
9787 header, we don't need the line header anymore. */
9788 if (m_cu->line_header_die_owner == m_die)
9789 {
9790 delete m_cu->line_header;
9791 m_cu->line_header = NULL;
9792 m_cu->line_header_die_owner = NULL;
9793 }
9794 }
9795
9796private:
9797 die_info *m_die;
9798 dwarf2_cu *m_cu;
9799};
adde2bff 9800
c906108c
SS
9801/* Process a die and its children. */
9802
9803static void
e7c27a73 9804process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9805{
4c8aa72d 9806 process_die_scope scope (die, cu);
adde2bff 9807
c906108c
SS
9808 switch (die->tag)
9809 {
9810 case DW_TAG_padding:
9811 break;
9812 case DW_TAG_compile_unit:
95554aad 9813 case DW_TAG_partial_unit:
e7c27a73 9814 read_file_scope (die, cu);
c906108c 9815 break;
348e048f
DE
9816 case DW_TAG_type_unit:
9817 read_type_unit_scope (die, cu);
9818 break;
c906108c 9819 case DW_TAG_subprogram:
0a4b0913
AB
9820 /* Nested subprograms in Fortran get a prefix. */
9821 if (cu->language == language_fortran
9822 && die->parent != NULL
9823 && die->parent->tag == DW_TAG_subprogram)
9824 cu->processing_has_namespace_info = true;
9825 /* Fall through. */
c906108c 9826 case DW_TAG_inlined_subroutine:
edb3359d 9827 read_func_scope (die, cu);
c906108c
SS
9828 break;
9829 case DW_TAG_lexical_block:
14898363
L
9830 case DW_TAG_try_block:
9831 case DW_TAG_catch_block:
e7c27a73 9832 read_lexical_block_scope (die, cu);
c906108c 9833 break;
216f72a1 9834 case DW_TAG_call_site:
96408a79
SA
9835 case DW_TAG_GNU_call_site:
9836 read_call_site_scope (die, cu);
9837 break;
c906108c 9838 case DW_TAG_class_type:
680b30c7 9839 case DW_TAG_interface_type:
c906108c
SS
9840 case DW_TAG_structure_type:
9841 case DW_TAG_union_type:
134d01f1 9842 process_structure_scope (die, cu);
c906108c
SS
9843 break;
9844 case DW_TAG_enumeration_type:
134d01f1 9845 process_enumeration_scope (die, cu);
c906108c 9846 break;
134d01f1 9847
f792889a
DJ
9848 /* These dies have a type, but processing them does not create
9849 a symbol or recurse to process the children. Therefore we can
9850 read them on-demand through read_type_die. */
c906108c 9851 case DW_TAG_subroutine_type:
72019c9c 9852 case DW_TAG_set_type:
c906108c 9853 case DW_TAG_array_type:
c906108c 9854 case DW_TAG_pointer_type:
c906108c 9855 case DW_TAG_ptr_to_member_type:
c906108c 9856 case DW_TAG_reference_type:
4297a3f0 9857 case DW_TAG_rvalue_reference_type:
c906108c 9858 case DW_TAG_string_type:
c906108c 9859 break;
134d01f1 9860
c906108c 9861 case DW_TAG_base_type:
a02abb62 9862 case DW_TAG_subrange_type:
cb249c71 9863 case DW_TAG_typedef:
134d01f1
DJ
9864 /* Add a typedef symbol for the type definition, if it has a
9865 DW_AT_name. */
f792889a 9866 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9867 break;
c906108c 9868 case DW_TAG_common_block:
e7c27a73 9869 read_common_block (die, cu);
c906108c
SS
9870 break;
9871 case DW_TAG_common_inclusion:
9872 break;
d9fa45fe 9873 case DW_TAG_namespace:
9068261f 9874 cu->processing_has_namespace_info = true;
e7c27a73 9875 read_namespace (die, cu);
d9fa45fe 9876 break;
5d7cb8df 9877 case DW_TAG_module:
9068261f 9878 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9879 read_module (die, cu);
9880 break;
d9fa45fe 9881 case DW_TAG_imported_declaration:
9068261f 9882 cu->processing_has_namespace_info = true;
74921315
KS
9883 if (read_namespace_alias (die, cu))
9884 break;
86a73007
TT
9885 /* The declaration is not a global namespace alias. */
9886 /* Fall through. */
d9fa45fe 9887 case DW_TAG_imported_module:
9068261f 9888 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9889 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9890 || cu->language != language_fortran))
b98664d3 9891 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9892 dwarf_tag_name (die->tag));
9893 read_import_statement (die, cu);
d9fa45fe 9894 break;
95554aad
TT
9895
9896 case DW_TAG_imported_unit:
9897 process_imported_unit_die (die, cu);
9898 break;
9899
71a3c369
TT
9900 case DW_TAG_variable:
9901 read_variable (die, cu);
9902 break;
9903
c906108c 9904 default:
e7c27a73 9905 new_symbol (die, NULL, cu);
c906108c
SS
9906 break;
9907 }
9908}
ca69b9e6
DE
9909\f
9910/* DWARF name computation. */
c906108c 9911
94af9270
KS
9912/* A helper function for dwarf2_compute_name which determines whether DIE
9913 needs to have the name of the scope prepended to the name listed in the
9914 die. */
9915
9916static int
9917die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9918{
1c809c68
TT
9919 struct attribute *attr;
9920
94af9270
KS
9921 switch (die->tag)
9922 {
9923 case DW_TAG_namespace:
9924 case DW_TAG_typedef:
9925 case DW_TAG_class_type:
9926 case DW_TAG_interface_type:
9927 case DW_TAG_structure_type:
9928 case DW_TAG_union_type:
9929 case DW_TAG_enumeration_type:
9930 case DW_TAG_enumerator:
9931 case DW_TAG_subprogram:
08a76f8a 9932 case DW_TAG_inlined_subroutine:
94af9270 9933 case DW_TAG_member:
74921315 9934 case DW_TAG_imported_declaration:
94af9270
KS
9935 return 1;
9936
9937 case DW_TAG_variable:
c2b0a229 9938 case DW_TAG_constant:
94af9270
KS
9939 /* We only need to prefix "globally" visible variables. These include
9940 any variable marked with DW_AT_external or any variable that
9941 lives in a namespace. [Variables in anonymous namespaces
9942 require prefixing, but they are not DW_AT_external.] */
9943
9944 if (dwarf2_attr (die, DW_AT_specification, cu))
9945 {
9946 struct dwarf2_cu *spec_cu = cu;
9a619af0 9947
94af9270
KS
9948 return die_needs_namespace (die_specification (die, &spec_cu),
9949 spec_cu);
9950 }
9951
1c809c68 9952 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9953 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9954 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9955 return 0;
9956 /* A variable in a lexical block of some kind does not need a
9957 namespace, even though in C++ such variables may be external
9958 and have a mangled name. */
9959 if (die->parent->tag == DW_TAG_lexical_block
9960 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9961 || die->parent->tag == DW_TAG_catch_block
9962 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9963 return 0;
9964 return 1;
94af9270
KS
9965
9966 default:
9967 return 0;
9968 }
9969}
9970
73b9be8b
KS
9971/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9972 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9973 defined for the given DIE. */
9974
9975static struct attribute *
9976dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9977{
9978 struct attribute *attr;
9979
9980 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9981 if (attr == NULL)
9982 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9983
9984 return attr;
9985}
9986
9987/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9988 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9989 defined for the given DIE. */
9990
9991static const char *
9992dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9993{
9994 const char *linkage_name;
9995
9996 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9997 if (linkage_name == NULL)
9998 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9999
10000 return linkage_name;
10001}
10002
94af9270 10003/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10004 compute the physname for the object, which include a method's:
9c37b5ae 10005 - formal parameters (C++),
a766d390 10006 - receiver type (Go),
a766d390
DE
10007
10008 The term "physname" is a bit confusing.
10009 For C++, for example, it is the demangled name.
10010 For Go, for example, it's the mangled name.
94af9270 10011
af6b7be1
JB
10012 For Ada, return the DIE's linkage name rather than the fully qualified
10013 name. PHYSNAME is ignored..
10014
94af9270
KS
10015 The result is allocated on the objfile_obstack and canonicalized. */
10016
10017static const char *
15d034d0
TT
10018dwarf2_compute_name (const char *name,
10019 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10020 int physname)
10021{
518817b3 10022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10023
94af9270
KS
10024 if (name == NULL)
10025 name = dwarf2_name (die, cu);
10026
2ee7123e
DE
10027 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10028 but otherwise compute it by typename_concat inside GDB.
10029 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10030 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10031 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10032 will set the demangled name to the result of dwarf2_full_name, and it is
10033 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10034 if (cu->language == language_ada
10035 || (cu->language == language_fortran && physname))
10036 {
10037 /* For Ada unit, we prefer the linkage name over the name, as
10038 the former contains the exported name, which the user expects
10039 to be able to reference. Ideally, we want the user to be able
10040 to reference this entity using either natural or linkage name,
10041 but we haven't started looking at this enhancement yet. */
73b9be8b 10042 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10043
2ee7123e
DE
10044 if (linkage_name != NULL)
10045 return linkage_name;
f55ee35c
JK
10046 }
10047
94af9270
KS
10048 /* These are the only languages we know how to qualify names in. */
10049 if (name != NULL
9c37b5ae 10050 && (cu->language == language_cplus
c44af4eb
TT
10051 || cu->language == language_fortran || cu->language == language_d
10052 || cu->language == language_rust))
94af9270
KS
10053 {
10054 if (die_needs_namespace (die, cu))
10055 {
0d5cff50 10056 const char *prefix;
34a68019 10057 const char *canonical_name = NULL;
94af9270 10058
d7e74731
PA
10059 string_file buf;
10060
94af9270 10061 prefix = determine_prefix (die, cu);
94af9270
KS
10062 if (*prefix != '\0')
10063 {
43816ebc
TT
10064 gdb::unique_xmalloc_ptr<char> prefixed_name
10065 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10066
43816ebc 10067 buf.puts (prefixed_name.get ());
94af9270
KS
10068 }
10069 else
d7e74731 10070 buf.puts (name);
94af9270 10071
98bfdba5
PA
10072 /* Template parameters may be specified in the DIE's DW_AT_name, or
10073 as children with DW_TAG_template_type_param or
10074 DW_TAG_value_type_param. If the latter, add them to the name
10075 here. If the name already has template parameters, then
10076 skip this step; some versions of GCC emit both, and
10077 it is more efficient to use the pre-computed name.
10078
10079 Something to keep in mind about this process: it is very
10080 unlikely, or in some cases downright impossible, to produce
10081 something that will match the mangled name of a function.
10082 If the definition of the function has the same debug info,
10083 we should be able to match up with it anyway. But fallbacks
10084 using the minimal symbol, for instance to find a method
10085 implemented in a stripped copy of libstdc++, will not work.
10086 If we do not have debug info for the definition, we will have to
10087 match them up some other way.
10088
10089 When we do name matching there is a related problem with function
10090 templates; two instantiated function templates are allowed to
10091 differ only by their return types, which we do not add here. */
10092
10093 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10094 {
10095 struct attribute *attr;
10096 struct die_info *child;
10097 int first = 1;
10098
10099 die->building_fullname = 1;
10100
10101 for (child = die->child; child != NULL; child = child->sibling)
10102 {
10103 struct type *type;
12df843f 10104 LONGEST value;
d521ce57 10105 const gdb_byte *bytes;
98bfdba5
PA
10106 struct dwarf2_locexpr_baton *baton;
10107 struct value *v;
10108
10109 if (child->tag != DW_TAG_template_type_param
10110 && child->tag != DW_TAG_template_value_param)
10111 continue;
10112
10113 if (first)
10114 {
d7e74731 10115 buf.puts ("<");
98bfdba5
PA
10116 first = 0;
10117 }
10118 else
d7e74731 10119 buf.puts (", ");
98bfdba5
PA
10120
10121 attr = dwarf2_attr (child, DW_AT_type, cu);
10122 if (attr == NULL)
10123 {
b98664d3 10124 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10125 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10126 continue;
10127 }
10128 type = die_type (child, cu);
10129
10130 if (child->tag == DW_TAG_template_type_param)
10131 {
c1ec8cea
TT
10132 c_print_type (type, "", &buf, -1, 0, cu->language,
10133 &type_print_raw_options);
98bfdba5
PA
10134 continue;
10135 }
10136
10137 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10138 if (attr == NULL)
10139 {
b98664d3 10140 complaint (_("template parameter missing "
3e43a32a 10141 "DW_AT_const_value"));
d7e74731 10142 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10143 continue;
10144 }
10145
10146 dwarf2_const_value_attr (attr, type, name,
10147 &cu->comp_unit_obstack, cu,
10148 &value, &bytes, &baton);
10149
10150 if (TYPE_NOSIGN (type))
10151 /* GDB prints characters as NUMBER 'CHAR'. If that's
10152 changed, this can use value_print instead. */
d7e74731 10153 c_printchar (value, type, &buf);
98bfdba5
PA
10154 else
10155 {
10156 struct value_print_options opts;
10157
10158 if (baton != NULL)
10159 v = dwarf2_evaluate_loc_desc (type, NULL,
10160 baton->data,
10161 baton->size,
10162 baton->per_cu);
10163 else if (bytes != NULL)
10164 {
10165 v = allocate_value (type);
10166 memcpy (value_contents_writeable (v), bytes,
10167 TYPE_LENGTH (type));
10168 }
10169 else
10170 v = value_from_longest (type, value);
10171
3e43a32a
MS
10172 /* Specify decimal so that we do not depend on
10173 the radix. */
98bfdba5
PA
10174 get_formatted_print_options (&opts, 'd');
10175 opts.raw = 1;
d7e74731 10176 value_print (v, &buf, &opts);
98bfdba5 10177 release_value (v);
98bfdba5
PA
10178 }
10179 }
10180
10181 die->building_fullname = 0;
10182
10183 if (!first)
10184 {
10185 /* Close the argument list, with a space if necessary
10186 (nested templates). */
d7e74731
PA
10187 if (!buf.empty () && buf.string ().back () == '>')
10188 buf.puts (" >");
98bfdba5 10189 else
d7e74731 10190 buf.puts (">");
98bfdba5
PA
10191 }
10192 }
10193
9c37b5ae 10194 /* For C++ methods, append formal parameter type
94af9270 10195 information, if PHYSNAME. */
6e70227d 10196
94af9270 10197 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10198 && cu->language == language_cplus)
94af9270
KS
10199 {
10200 struct type *type = read_type_die (die, cu);
10201
d7e74731 10202 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10203 &type_print_raw_options);
94af9270 10204
9c37b5ae 10205 if (cu->language == language_cplus)
94af9270 10206 {
60430eff
DJ
10207 /* Assume that an artificial first parameter is
10208 "this", but do not crash if it is not. RealView
10209 marks unnamed (and thus unused) parameters as
10210 artificial; there is no way to differentiate
10211 the two cases. */
94af9270
KS
10212 if (TYPE_NFIELDS (type) > 0
10213 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10214 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10215 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10216 0))))
d7e74731 10217 buf.puts (" const");
94af9270
KS
10218 }
10219 }
10220
d7e74731 10221 const std::string &intermediate_name = buf.string ();
94af9270
KS
10222
10223 if (cu->language == language_cplus)
34a68019 10224 canonical_name
322a8516 10225 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10226 &objfile->per_bfd->storage_obstack);
10227
10228 /* If we only computed INTERMEDIATE_NAME, or if
10229 INTERMEDIATE_NAME is already canonical, then we need to
10230 copy it to the appropriate obstack. */
322a8516 10231 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10232 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10233 intermediate_name);
34a68019
TT
10234 else
10235 name = canonical_name;
94af9270
KS
10236 }
10237 }
10238
10239 return name;
10240}
10241
0114d602
DJ
10242/* Return the fully qualified name of DIE, based on its DW_AT_name.
10243 If scope qualifiers are appropriate they will be added. The result
34a68019 10244 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10245 not have a name. NAME may either be from a previous call to
10246 dwarf2_name or NULL.
10247
9c37b5ae 10248 The output string will be canonicalized (if C++). */
0114d602
DJ
10249
10250static const char *
15d034d0 10251dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10252{
94af9270
KS
10253 return dwarf2_compute_name (name, die, cu, 0);
10254}
0114d602 10255
94af9270
KS
10256/* Construct a physname for the given DIE in CU. NAME may either be
10257 from a previous call to dwarf2_name or NULL. The result will be
10258 allocated on the objfile_objstack or NULL if the DIE does not have a
10259 name.
0114d602 10260
9c37b5ae 10261 The output string will be canonicalized (if C++). */
0114d602 10262
94af9270 10263static const char *
15d034d0 10264dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10265{
518817b3 10266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10267 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10268 int need_copy = 1;
10269
10270 /* In this case dwarf2_compute_name is just a shortcut not building anything
10271 on its own. */
10272 if (!die_needs_namespace (die, cu))
10273 return dwarf2_compute_name (name, die, cu, 1);
10274
73b9be8b 10275 mangled = dw2_linkage_name (die, cu);
900e11f9 10276
e98c9e7c
TT
10277 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10278 See https://github.com/rust-lang/rust/issues/32925. */
10279 if (cu->language == language_rust && mangled != NULL
10280 && strchr (mangled, '{') != NULL)
10281 mangled = NULL;
10282
900e11f9
JK
10283 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10284 has computed. */
791afaa2 10285 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10286 if (mangled != NULL)
900e11f9 10287 {
900e11f9 10288
59cc4834
JB
10289 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10290 {
10291 /* Do nothing (do not demangle the symbol name). */
10292 }
10293 else if (cu->language == language_go)
a766d390 10294 {
5e2db402
TT
10295 /* This is a lie, but we already lie to the caller new_symbol.
10296 new_symbol assumes we return the mangled name.
a766d390 10297 This just undoes that lie until things are cleaned up. */
a766d390
DE
10298 }
10299 else
10300 {
0eb876f5
JB
10301 /* Use DMGL_RET_DROP for C++ template functions to suppress
10302 their return type. It is easier for GDB users to search
10303 for such functions as `name(params)' than `long name(params)'.
10304 In such case the minimal symbol names do not match the full
10305 symbol names but for template functions there is never a need
10306 to look up their definition from their declaration so
10307 the only disadvantage remains the minimal symbol variant
10308 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10309 demangled.reset (gdb_demangle (mangled,
10310 (DMGL_PARAMS | DMGL_ANSI
10311 | DMGL_RET_DROP)));
a766d390 10312 }
900e11f9 10313 if (demangled)
791afaa2 10314 canon = demangled.get ();
900e11f9
JK
10315 else
10316 {
10317 canon = mangled;
10318 need_copy = 0;
10319 }
10320 }
10321
10322 if (canon == NULL || check_physname)
10323 {
10324 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10325
10326 if (canon != NULL && strcmp (physname, canon) != 0)
10327 {
10328 /* It may not mean a bug in GDB. The compiler could also
10329 compute DW_AT_linkage_name incorrectly. But in such case
10330 GDB would need to be bug-to-bug compatible. */
10331
b98664d3 10332 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10333 "(from linkage <%s>) - DIE at %s [in module %s]"),
10334 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10335 objfile_name (objfile));
900e11f9
JK
10336
10337 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10338 is available here - over computed PHYSNAME. It is safer
10339 against both buggy GDB and buggy compilers. */
10340
10341 retval = canon;
10342 }
10343 else
10344 {
10345 retval = physname;
10346 need_copy = 0;
10347 }
10348 }
10349 else
10350 retval = canon;
10351
10352 if (need_copy)
021887d8 10353 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 10354
900e11f9 10355 return retval;
0114d602
DJ
10356}
10357
74921315
KS
10358/* Inspect DIE in CU for a namespace alias. If one exists, record
10359 a new symbol for it.
10360
10361 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10362
10363static int
10364read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10365{
10366 struct attribute *attr;
10367
10368 /* If the die does not have a name, this is not a namespace
10369 alias. */
10370 attr = dwarf2_attr (die, DW_AT_name, cu);
10371 if (attr != NULL)
10372 {
10373 int num;
10374 struct die_info *d = die;
10375 struct dwarf2_cu *imported_cu = cu;
10376
10377 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10378 keep inspecting DIEs until we hit the underlying import. */
10379#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10380 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10381 {
10382 attr = dwarf2_attr (d, DW_AT_import, cu);
10383 if (attr == NULL)
10384 break;
10385
10386 d = follow_die_ref (d, attr, &imported_cu);
10387 if (d->tag != DW_TAG_imported_declaration)
10388 break;
10389 }
10390
10391 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10392 {
b98664d3 10393 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10394 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10395 return 0;
10396 }
10397
10398 if (attr != NULL)
10399 {
10400 struct type *type;
9c541725 10401 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10402
9c541725 10403 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10404 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10405 {
10406 /* This declaration is a global namespace alias. Add
10407 a symbol for it whose type is the aliased namespace. */
10408 new_symbol (die, type, cu);
10409 return 1;
10410 }
10411 }
10412 }
10413
10414 return 0;
10415}
10416
22cee43f 10417/* Return the using directives repository (global or local?) to use in the
804d2729 10418 current context for CU.
22cee43f
PMR
10419
10420 For Ada, imported declarations can materialize renamings, which *may* be
10421 global. However it is impossible (for now?) in DWARF to distinguish
10422 "external" imported declarations and "static" ones. As all imported
10423 declarations seem to be static in all other languages, make them all CU-wide
10424 global only in Ada. */
10425
10426static struct using_direct **
804d2729 10427using_directives (struct dwarf2_cu *cu)
22cee43f 10428{
c24bdb02
KS
10429 if (cu->language == language_ada
10430 && cu->get_builder ()->outermost_context_p ())
10431 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10432 else
c24bdb02 10433 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10434}
10435
27aa8d6a
SW
10436/* Read the import statement specified by the given die and record it. */
10437
10438static void
10439read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10440{
518817b3 10441 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10442 struct attribute *import_attr;
32019081 10443 struct die_info *imported_die, *child_die;
de4affc9 10444 struct dwarf2_cu *imported_cu;
27aa8d6a 10445 const char *imported_name;
794684b6 10446 const char *imported_name_prefix;
13387711
SW
10447 const char *canonical_name;
10448 const char *import_alias;
10449 const char *imported_declaration = NULL;
794684b6 10450 const char *import_prefix;
eb1e02fd 10451 std::vector<const char *> excludes;
13387711 10452
27aa8d6a
SW
10453 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10454 if (import_attr == NULL)
10455 {
b98664d3 10456 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10457 dwarf_tag_name (die->tag));
10458 return;
10459 }
10460
de4affc9
CC
10461 imported_cu = cu;
10462 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10463 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10464 if (imported_name == NULL)
10465 {
10466 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10467
10468 The import in the following code:
10469 namespace A
10470 {
10471 typedef int B;
10472 }
10473
10474 int main ()
10475 {
10476 using A::B;
10477 B b;
10478 return b;
10479 }
10480
10481 ...
10482 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10483 <52> DW_AT_decl_file : 1
10484 <53> DW_AT_decl_line : 6
10485 <54> DW_AT_import : <0x75>
10486 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10487 <59> DW_AT_name : B
10488 <5b> DW_AT_decl_file : 1
10489 <5c> DW_AT_decl_line : 2
10490 <5d> DW_AT_type : <0x6e>
10491 ...
10492 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10493 <76> DW_AT_byte_size : 4
10494 <77> DW_AT_encoding : 5 (signed)
10495
10496 imports the wrong die ( 0x75 instead of 0x58 ).
10497 This case will be ignored until the gcc bug is fixed. */
10498 return;
10499 }
10500
82856980
SW
10501 /* Figure out the local name after import. */
10502 import_alias = dwarf2_name (die, cu);
27aa8d6a 10503
794684b6
SW
10504 /* Figure out where the statement is being imported to. */
10505 import_prefix = determine_prefix (die, cu);
10506
10507 /* Figure out what the scope of the imported die is and prepend it
10508 to the name of the imported die. */
de4affc9 10509 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10510
f55ee35c
JK
10511 if (imported_die->tag != DW_TAG_namespace
10512 && imported_die->tag != DW_TAG_module)
794684b6 10513 {
13387711
SW
10514 imported_declaration = imported_name;
10515 canonical_name = imported_name_prefix;
794684b6 10516 }
13387711 10517 else if (strlen (imported_name_prefix) > 0)
12aaed36 10518 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10519 imported_name_prefix,
10520 (cu->language == language_d ? "." : "::"),
10521 imported_name, (char *) NULL);
13387711
SW
10522 else
10523 canonical_name = imported_name;
794684b6 10524
32019081
JK
10525 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10526 for (child_die = die->child; child_die && child_die->tag;
10527 child_die = sibling_die (child_die))
10528 {
10529 /* DWARF-4: A Fortran use statement with a “rename list” may be
10530 represented by an imported module entry with an import attribute
10531 referring to the module and owned entries corresponding to those
10532 entities that are renamed as part of being imported. */
10533
10534 if (child_die->tag != DW_TAG_imported_declaration)
10535 {
b98664d3 10536 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10537 "- DIE at %s [in module %s]"),
10538 sect_offset_str (child_die->sect_off),
10539 objfile_name (objfile));
32019081
JK
10540 continue;
10541 }
10542
10543 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10544 if (import_attr == NULL)
10545 {
b98664d3 10546 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10547 dwarf_tag_name (child_die->tag));
10548 continue;
10549 }
10550
10551 imported_cu = cu;
10552 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10553 &imported_cu);
10554 imported_name = dwarf2_name (imported_die, imported_cu);
10555 if (imported_name == NULL)
10556 {
b98664d3 10557 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10558 "imported name - DIE at %s [in module %s]"),
10559 sect_offset_str (child_die->sect_off),
10560 objfile_name (objfile));
32019081
JK
10561 continue;
10562 }
10563
eb1e02fd 10564 excludes.push_back (imported_name);
32019081
JK
10565
10566 process_die (child_die, cu);
10567 }
10568
804d2729 10569 add_using_directive (using_directives (cu),
22cee43f
PMR
10570 import_prefix,
10571 canonical_name,
10572 import_alias,
10573 imported_declaration,
10574 excludes,
10575 0,
10576 &objfile->objfile_obstack);
27aa8d6a
SW
10577}
10578
5230b05a
WT
10579/* ICC<14 does not output the required DW_AT_declaration on incomplete
10580 types, but gives them a size of zero. Starting with version 14,
10581 ICC is compatible with GCC. */
10582
9068261f 10583static bool
5230b05a
WT
10584producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10585{
10586 if (!cu->checked_producer)
10587 check_producer (cu);
10588
10589 return cu->producer_is_icc_lt_14;
10590}
10591
eb77c9df
AB
10592/* ICC generates a DW_AT_type for C void functions. This was observed on
10593 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10594 which says that void functions should not have a DW_AT_type. */
10595
10596static bool
10597producer_is_icc (struct dwarf2_cu *cu)
10598{
10599 if (!cu->checked_producer)
10600 check_producer (cu);
10601
10602 return cu->producer_is_icc;
10603}
10604
1b80a9fa
JK
10605/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10606 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10607 this, it was first present in GCC release 4.3.0. */
10608
9068261f 10609static bool
1b80a9fa
JK
10610producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10611{
10612 if (!cu->checked_producer)
10613 check_producer (cu);
10614
10615 return cu->producer_is_gcc_lt_4_3;
10616}
10617
d721ba37
PA
10618static file_and_directory
10619find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10620{
d721ba37
PA
10621 file_and_directory res;
10622
9291a0cd
TT
10623 /* Find the filename. Do not use dwarf2_name here, since the filename
10624 is not a source language identifier. */
d721ba37
PA
10625 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10626 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10627
d721ba37
PA
10628 if (res.comp_dir == NULL
10629 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10630 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10631 {
d721ba37
PA
10632 res.comp_dir_storage = ldirname (res.name);
10633 if (!res.comp_dir_storage.empty ())
10634 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10635 }
d721ba37 10636 if (res.comp_dir != NULL)
9291a0cd
TT
10637 {
10638 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10639 directory, get rid of it. */
d721ba37 10640 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10641
d721ba37
PA
10642 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10643 res.comp_dir = cp + 1;
9291a0cd
TT
10644 }
10645
d721ba37
PA
10646 if (res.name == NULL)
10647 res.name = "<unknown>";
10648
10649 return res;
9291a0cd
TT
10650}
10651
f4dc4d17
DE
10652/* Handle DW_AT_stmt_list for a compilation unit.
10653 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10654 COMP_DIR is the compilation directory. LOWPC is passed to
10655 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10656
10657static void
10658handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10659 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10660{
518817b3
SM
10661 struct dwarf2_per_objfile *dwarf2_per_objfile
10662 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10663 struct attribute *attr;
527f3840
JK
10664 struct line_header line_header_local;
10665 hashval_t line_header_local_hash;
527f3840
JK
10666 void **slot;
10667 int decode_mapping;
2ab95328 10668
f4dc4d17
DE
10669 gdb_assert (! cu->per_cu->is_debug_types);
10670
2ab95328 10671 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10672 if (attr == NULL)
10673 return;
10674
9c541725 10675 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10676
10677 /* The line header hash table is only created if needed (it exists to
10678 prevent redundant reading of the line table for partial_units).
10679 If we're given a partial_unit, we'll need it. If we're given a
10680 compile_unit, then use the line header hash table if it's already
10681 created, but don't create one just yet. */
10682
10683 if (dwarf2_per_objfile->line_header_hash == NULL
10684 && die->tag == DW_TAG_partial_unit)
2ab95328 10685 {
527f3840 10686 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10687 .reset (htab_create_alloc (127, line_header_hash_voidp,
10688 line_header_eq_voidp,
10689 free_line_header_voidp,
10690 xcalloc, xfree));
527f3840 10691 }
2ab95328 10692
9c541725 10693 line_header_local.sect_off = line_offset;
527f3840
JK
10694 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10695 line_header_local_hash = line_header_hash (&line_header_local);
10696 if (dwarf2_per_objfile->line_header_hash != NULL)
10697 {
d15acc42 10698 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10699 &line_header_local,
10700 line_header_local_hash, NO_INSERT);
10701
10702 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10703 is not present in *SLOT (since if there is something in *SLOT then
10704 it will be for a partial_unit). */
10705 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10706 {
527f3840 10707 gdb_assert (*slot != NULL);
9a3c8263 10708 cu->line_header = (struct line_header *) *slot;
527f3840 10709 return;
dee91e82 10710 }
2ab95328 10711 }
527f3840
JK
10712
10713 /* dwarf_decode_line_header does not yet provide sufficient information.
10714 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10715 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10716 if (lh == NULL)
527f3840 10717 return;
4c8aa72d
PA
10718
10719 cu->line_header = lh.release ();
10720 cu->line_header_die_owner = die;
527f3840
JK
10721
10722 if (dwarf2_per_objfile->line_header_hash == NULL)
10723 slot = NULL;
10724 else
10725 {
d15acc42 10726 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10727 &line_header_local,
10728 line_header_local_hash, INSERT);
10729 gdb_assert (slot != NULL);
10730 }
10731 if (slot != NULL && *slot == NULL)
10732 {
10733 /* This newly decoded line number information unit will be owned
10734 by line_header_hash hash table. */
10735 *slot = cu->line_header;
4c8aa72d 10736 cu->line_header_die_owner = NULL;
527f3840
JK
10737 }
10738 else
10739 {
10740 /* We cannot free any current entry in (*slot) as that struct line_header
10741 may be already used by multiple CUs. Create only temporary decoded
10742 line_header for this CU - it may happen at most once for each line
10743 number information unit. And if we're not using line_header_hash
10744 then this is what we want as well. */
10745 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10746 }
10747 decode_mapping = (die->tag != DW_TAG_partial_unit);
10748 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10749 decode_mapping);
fff8551c 10750
2ab95328
TT
10751}
10752
95554aad 10753/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10754
c906108c 10755static void
e7c27a73 10756read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10757{
518817b3
SM
10758 struct dwarf2_per_objfile *dwarf2_per_objfile
10759 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10760 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10761 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10762 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10763 CORE_ADDR highpc = ((CORE_ADDR) 0);
10764 struct attribute *attr;
c906108c 10765 struct die_info *child_die;
e142c38c 10766 CORE_ADDR baseaddr;
6e70227d 10767
380618d6 10768 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10769 baseaddr = objfile->text_section_offset ();
c906108c 10770
fae299cd 10771 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10772
10773 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10774 from finish_block. */
2acceee2 10775 if (lowpc == ((CORE_ADDR) -1))
c906108c 10776 lowpc = highpc;
3e29f34a 10777 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10778
d721ba37 10779 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10780
f4b8a18d
KW
10781 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10782 standardised yet. As a workaround for the language detection we fall
10783 back to the DW_AT_producer string. */
10784 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10785 cu->language = language_opencl;
10786
3019eac3
DE
10787 /* Similar hack for Go. */
10788 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10789 set_cu_language (DW_LANG_Go, cu);
10790
c24bdb02 10791 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10792
10793 /* Decode line number information if present. We do this before
10794 processing child DIEs, so that the line header table is available
10795 for DW_AT_decl_file. */
d721ba37 10796 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10797
10798 /* Process all dies in compilation unit. */
10799 if (die->child != NULL)
10800 {
10801 child_die = die->child;
10802 while (child_die && child_die->tag)
10803 {
10804 process_die (child_die, cu);
10805 child_die = sibling_die (child_die);
10806 }
10807 }
10808
10809 /* Decode macro information, if present. Dwarf 2 macro information
10810 refers to information in the line number info statement program
10811 header, so we can only read it if we've read the header
10812 successfully. */
0af92d60
JK
10813 attr = dwarf2_attr (die, DW_AT_macros, cu);
10814 if (attr == NULL)
10815 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10816 if (attr && cu->line_header)
10817 {
10818 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10819 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10820
43f3e411 10821 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10822 }
10823 else
10824 {
10825 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10826 if (attr && cu->line_header)
10827 {
10828 unsigned int macro_offset = DW_UNSND (attr);
10829
43f3e411 10830 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10831 }
10832 }
3019eac3
DE
10833}
10834
c24bdb02
KS
10835void
10836dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10837{
f4dc4d17
DE
10838 struct type_unit_group *tu_group;
10839 int first_time;
3019eac3 10840 struct attribute *attr;
9c541725 10841 unsigned int i;
0186c6a7 10842 struct signatured_type *sig_type;
3019eac3 10843
f4dc4d17 10844 gdb_assert (per_cu->is_debug_types);
0186c6a7 10845 sig_type = (struct signatured_type *) per_cu;
3019eac3 10846
c24bdb02 10847 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10848
f4dc4d17 10849 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10850 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10851 if (sig_type->type_unit_group == NULL)
c24bdb02 10852 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10853 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10854
10855 /* If we've already processed this stmt_list there's no real need to
10856 do it again, we could fake it and just recreate the part we need
10857 (file name,index -> symtab mapping). If data shows this optimization
10858 is useful we can do it then. */
43f3e411 10859 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10860
10861 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10862 debug info. */
fff8551c 10863 line_header_up lh;
f4dc4d17 10864 if (attr != NULL)
3019eac3 10865 {
9c541725 10866 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10867 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10868 }
10869 if (lh == NULL)
10870 {
10871 if (first_time)
c24bdb02 10872 start_symtab ("", NULL, 0);
f4dc4d17
DE
10873 else
10874 {
10875 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10876 gdb_assert (m_builder == nullptr);
804d2729 10877 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10878 m_builder.reset (new struct buildsym_compunit
10879 (COMPUNIT_OBJFILE (cust), "",
10880 COMPUNIT_DIRNAME (cust),
10881 compunit_language (cust),
10882 0, cust));
f4dc4d17 10883 }
f4dc4d17 10884 return;
3019eac3
DE
10885 }
10886
c24bdb02
KS
10887 line_header = lh.release ();
10888 line_header_die_owner = die;
3019eac3 10889
f4dc4d17
DE
10890 if (first_time)
10891 {
c24bdb02 10892 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10893
1fd60fc0
DE
10894 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10895 still initializing it, and our caller (a few levels up)
10896 process_full_type_unit still needs to know if this is the first
10897 time. */
10898
4ac93832
TT
10899 tu_group->symtabs
10900 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10901 struct symtab *, line_header->file_names_size ());
3019eac3 10902
7ba99d21
AT
10903 auto &file_names = line_header->file_names ();
10904 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10905 {
7ba99d21 10906 file_entry &fe = file_names[i];
c24bdb02
KS
10907 dwarf2_start_subfile (this, fe.name,
10908 fe.include_dir (line_header));
10909 buildsym_compunit *b = get_builder ();
10910 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10911 {
4c8aa72d
PA
10912 /* NOTE: start_subfile will recognize when it's been
10913 passed a file it has already seen. So we can't
10914 assume there's a simple mapping from
10915 cu->line_header->file_names to subfiles, plus
10916 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10917 b->get_current_subfile ()->symtab
10918 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10919 }
10920
c24bdb02 10921 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10922 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10923 }
10924 }
10925 else
3019eac3 10926 {
c24bdb02 10927 gdb_assert (m_builder == nullptr);
804d2729 10928 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10929 m_builder.reset (new struct buildsym_compunit
10930 (COMPUNIT_OBJFILE (cust), "",
10931 COMPUNIT_DIRNAME (cust),
10932 compunit_language (cust),
10933 0, cust));
f4dc4d17 10934
7ba99d21
AT
10935 auto &file_names = line_header->file_names ();
10936 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10937 {
7ba99d21 10938 file_entry &fe = file_names[i];
4c8aa72d 10939 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10940 }
3019eac3
DE
10941 }
10942
f4dc4d17
DE
10943 /* The main symtab is allocated last. Type units don't have DW_AT_name
10944 so they don't have a "real" (so to speak) symtab anyway.
10945 There is later code that will assign the main symtab to all symbols
10946 that don't have one. We need to handle the case of a symbol with a
10947 missing symtab (DW_AT_decl_file) anyway. */
10948}
3019eac3 10949
f4dc4d17
DE
10950/* Process DW_TAG_type_unit.
10951 For TUs we want to skip the first top level sibling if it's not the
10952 actual type being defined by this TU. In this case the first top
10953 level sibling is there to provide context only. */
3019eac3 10954
f4dc4d17
DE
10955static void
10956read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10957{
10958 struct die_info *child_die;
3019eac3 10959
f4dc4d17
DE
10960 prepare_one_comp_unit (cu, die, language_minimal);
10961
10962 /* Initialize (or reinitialize) the machinery for building symtabs.
10963 We do this before processing child DIEs, so that the line header table
10964 is available for DW_AT_decl_file. */
c24bdb02 10965 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10966
10967 if (die->child != NULL)
10968 {
10969 child_die = die->child;
10970 while (child_die && child_die->tag)
10971 {
10972 process_die (child_die, cu);
10973 child_die = sibling_die (child_die);
10974 }
10975 }
3019eac3
DE
10976}
10977\f
80626a55
DE
10978/* DWO/DWP files.
10979
10980 http://gcc.gnu.org/wiki/DebugFission
10981 http://gcc.gnu.org/wiki/DebugFissionDWP
10982
10983 To simplify handling of both DWO files ("object" files with the DWARF info)
10984 and DWP files (a file with the DWOs packaged up into one file), we treat
10985 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10986
10987static hashval_t
10988hash_dwo_file (const void *item)
10989{
9a3c8263 10990 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10991 hashval_t hash;
3019eac3 10992
a2ce51a0
DE
10993 hash = htab_hash_string (dwo_file->dwo_name);
10994 if (dwo_file->comp_dir != NULL)
10995 hash += htab_hash_string (dwo_file->comp_dir);
10996 return hash;
3019eac3
DE
10997}
10998
10999static int
11000eq_dwo_file (const void *item_lhs, const void *item_rhs)
11001{
9a3c8263
SM
11002 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11003 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11004
a2ce51a0
DE
11005 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11006 return 0;
11007 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11008 return lhs->comp_dir == rhs->comp_dir;
11009 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11010}
11011
11012/* Allocate a hash table for DWO files. */
11013
51ac9db5 11014static htab_up
298e9637 11015allocate_dwo_file_hash_table ()
3019eac3 11016{
51ac9db5
SM
11017 auto delete_dwo_file = [] (void *item)
11018 {
11019 struct dwo_file *dwo_file = (struct dwo_file *) item;
11020
11021 delete dwo_file;
11022 };
11023
bc68fb19
TT
11024 return htab_up (htab_create_alloc (41,
11025 hash_dwo_file,
11026 eq_dwo_file,
11027 delete_dwo_file,
11028 xcalloc, xfree));
3019eac3
DE
11029}
11030
80626a55
DE
11031/* Lookup DWO file DWO_NAME. */
11032
11033static void **
ed2dc618
SM
11034lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11035 const char *dwo_name,
11036 const char *comp_dir)
80626a55
DE
11037{
11038 struct dwo_file find_entry;
11039 void **slot;
11040
11041 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11042 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11043
0ac5b59e
DE
11044 find_entry.dwo_name = dwo_name;
11045 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11046 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11047 INSERT);
80626a55
DE
11048
11049 return slot;
11050}
11051
3019eac3
DE
11052static hashval_t
11053hash_dwo_unit (const void *item)
11054{
9a3c8263 11055 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11056
11057 /* This drops the top 32 bits of the id, but is ok for a hash. */
11058 return dwo_unit->signature;
11059}
11060
11061static int
11062eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11063{
9a3c8263
SM
11064 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11065 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11066
11067 /* The signature is assumed to be unique within the DWO file.
11068 So while object file CU dwo_id's always have the value zero,
11069 that's OK, assuming each object file DWO file has only one CU,
11070 and that's the rule for now. */
11071 return lhs->signature == rhs->signature;
11072}
11073
11074/* Allocate a hash table for DWO CUs,TUs.
11075 There is one of these tables for each of CUs,TUs for each DWO file. */
11076
b0b6a987 11077static htab_up
298e9637 11078allocate_dwo_unit_table ()
3019eac3
DE
11079{
11080 /* Start out with a pretty small number.
11081 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11082 return htab_up (htab_create_alloc (3,
11083 hash_dwo_unit,
11084 eq_dwo_unit,
11085 NULL, xcalloc, xfree));
3019eac3
DE
11086}
11087
19c3d4c9 11088/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11089
11090static void
19c3d4c9
DE
11091create_dwo_cu_reader (const struct die_reader_specs *reader,
11092 const gdb_byte *info_ptr,
11093 struct die_info *comp_unit_die,
c0ab21c2
TT
11094 struct dwo_file *dwo_file,
11095 struct dwo_unit *dwo_unit)
3019eac3
DE
11096{
11097 struct dwarf2_cu *cu = reader->cu;
9c541725 11098 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11099 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11100
a084a2a6
AT
11101 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11102 if (!signature.has_value ())
3019eac3 11103 {
b98664d3 11104 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11105 " its dwo_id [in module %s]"),
9d8780f0 11106 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11107 return;
11108 }
11109
3019eac3 11110 dwo_unit->dwo_file = dwo_file;
a084a2a6 11111 dwo_unit->signature = *signature;
8a0459fd 11112 dwo_unit->section = section;
9c541725 11113 dwo_unit->sect_off = sect_off;
3019eac3
DE
11114 dwo_unit->length = cu->per_cu->length;
11115
b4f54984 11116 if (dwarf_read_debug)
9d8780f0
SM
11117 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11118 sect_offset_str (sect_off),
9c541725 11119 hex_string (dwo_unit->signature));
3019eac3
DE
11120}
11121
33c5cd75 11122/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11123 Note: This function processes DWO files only, not DWP files. */
3019eac3 11124
33c5cd75 11125static void
ed2dc618 11126create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11127 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11128 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11129{
11130 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11131 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11132
96b79293 11133 section.read (objfile);
33c5cd75 11134 info_ptr = section.buffer;
3019eac3
DE
11135
11136 if (info_ptr == NULL)
33c5cd75 11137 return;
3019eac3 11138
b4f54984 11139 if (dwarf_read_debug)
19c3d4c9
DE
11140 {
11141 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11142 section.get_name (),
11143 section.get_file_name ());
19c3d4c9 11144 }
3019eac3 11145
33c5cd75 11146 end_ptr = info_ptr + section.size;
3019eac3
DE
11147 while (info_ptr < end_ptr)
11148 {
11149 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11150 struct dwo_unit read_unit {};
33c5cd75
DB
11151 struct dwo_unit *dwo_unit;
11152 void **slot;
11153 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11154
11155 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11156 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11157 per_cu.is_debug_types = 0;
33c5cd75
DB
11158 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11159 per_cu.section = &section;
11160
c0ab21c2
TT
11161 cutu_reader reader (&per_cu, cu, &dwo_file);
11162 if (!reader.dummy_p)
11163 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11164 &dwo_file, &read_unit);
33c5cd75
DB
11165 info_ptr += per_cu.length;
11166
11167 // If the unit could not be parsed, skip it.
c0ab21c2 11168 if (read_unit.dwo_file == NULL)
33c5cd75 11169 continue;
3019eac3 11170
33c5cd75 11171 if (cus_htab == NULL)
298e9637 11172 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11173
33c5cd75 11174 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11175 *dwo_unit = read_unit;
b0b6a987 11176 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11177 gdb_assert (slot != NULL);
11178 if (*slot != NULL)
19c3d4c9 11179 {
33c5cd75
DB
11180 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11181 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11182
b98664d3 11183 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11184 " the entry at offset %s, signature %s"),
11185 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11186 hex_string (dwo_unit->signature));
19c3d4c9 11187 }
33c5cd75 11188 *slot = (void *)dwo_unit;
3019eac3 11189 }
3019eac3
DE
11190}
11191
80626a55
DE
11192/* DWP file .debug_{cu,tu}_index section format:
11193 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11194
d2415c6c
DE
11195 DWP Version 1:
11196
80626a55
DE
11197 Both index sections have the same format, and serve to map a 64-bit
11198 signature to a set of section numbers. Each section begins with a header,
11199 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11200 indexes, and a pool of 32-bit section numbers. The index sections will be
11201 aligned at 8-byte boundaries in the file.
11202
d2415c6c
DE
11203 The index section header consists of:
11204
11205 V, 32 bit version number
11206 -, 32 bits unused
11207 N, 32 bit number of compilation units or type units in the index
11208 M, 32 bit number of slots in the hash table
80626a55 11209
d2415c6c 11210 Numbers are recorded using the byte order of the application binary.
80626a55 11211
d2415c6c
DE
11212 The hash table begins at offset 16 in the section, and consists of an array
11213 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11214 order of the application binary). Unused slots in the hash table are 0.
11215 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11216
d2415c6c
DE
11217 The parallel table begins immediately after the hash table
11218 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11219 array of 32-bit indexes (using the byte order of the application binary),
11220 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11221 table contains a 32-bit index into the pool of section numbers. For unused
11222 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11223
73869dc2
DE
11224 The pool of section numbers begins immediately following the hash table
11225 (at offset 16 + 12 * M from the beginning of the section). The pool of
11226 section numbers consists of an array of 32-bit words (using the byte order
11227 of the application binary). Each item in the array is indexed starting
11228 from 0. The hash table entry provides the index of the first section
11229 number in the set. Additional section numbers in the set follow, and the
11230 set is terminated by a 0 entry (section number 0 is not used in ELF).
11231
11232 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11233 section must be the first entry in the set, and the .debug_abbrev.dwo must
11234 be the second entry. Other members of the set may follow in any order.
11235
11236 ---
11237
11238 DWP Version 2:
11239
11240 DWP Version 2 combines all the .debug_info, etc. sections into one,
11241 and the entries in the index tables are now offsets into these sections.
11242 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11243 section.
11244
11245 Index Section Contents:
11246 Header
11247 Hash Table of Signatures dwp_hash_table.hash_table
11248 Parallel Table of Indices dwp_hash_table.unit_table
11249 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11250 Table of Section Sizes dwp_hash_table.v2.sizes
11251
11252 The index section header consists of:
11253
11254 V, 32 bit version number
11255 L, 32 bit number of columns in the table of section offsets
11256 N, 32 bit number of compilation units or type units in the index
11257 M, 32 bit number of slots in the hash table
11258
11259 Numbers are recorded using the byte order of the application binary.
11260
11261 The hash table has the same format as version 1.
11262 The parallel table of indices has the same format as version 1,
11263 except that the entries are origin-1 indices into the table of sections
11264 offsets and the table of section sizes.
11265
11266 The table of offsets begins immediately following the parallel table
11267 (at offset 16 + 12 * M from the beginning of the section). The table is
11268 a two-dimensional array of 32-bit words (using the byte order of the
11269 application binary), with L columns and N+1 rows, in row-major order.
11270 Each row in the array is indexed starting from 0. The first row provides
11271 a key to the remaining rows: each column in this row provides an identifier
11272 for a debug section, and the offsets in the same column of subsequent rows
11273 refer to that section. The section identifiers are:
11274
11275 DW_SECT_INFO 1 .debug_info.dwo
11276 DW_SECT_TYPES 2 .debug_types.dwo
11277 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11278 DW_SECT_LINE 4 .debug_line.dwo
11279 DW_SECT_LOC 5 .debug_loc.dwo
11280 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11281 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11282 DW_SECT_MACRO 8 .debug_macro.dwo
11283
11284 The offsets provided by the CU and TU index sections are the base offsets
11285 for the contributions made by each CU or TU to the corresponding section
11286 in the package file. Each CU and TU header contains an abbrev_offset
11287 field, used to find the abbreviations table for that CU or TU within the
11288 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11289 be interpreted as relative to the base offset given in the index section.
11290 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11291 should be interpreted as relative to the base offset for .debug_line.dwo,
11292 and offsets into other debug sections obtained from DWARF attributes should
11293 also be interpreted as relative to the corresponding base offset.
11294
11295 The table of sizes begins immediately following the table of offsets.
11296 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11297 with L columns and N rows, in row-major order. Each row in the array is
11298 indexed starting from 1 (row 0 is shared by the two tables).
11299
11300 ---
11301
11302 Hash table lookup is handled the same in version 1 and 2:
11303
11304 We assume that N and M will not exceed 2^32 - 1.
11305 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11306
d2415c6c
DE
11307 Given a 64-bit compilation unit signature or a type signature S, an entry
11308 in the hash table is located as follows:
80626a55 11309
d2415c6c
DE
11310 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11311 the low-order k bits all set to 1.
80626a55 11312
d2415c6c 11313 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11314
d2415c6c
DE
11315 3) If the hash table entry at index H matches the signature, use that
11316 entry. If the hash table entry at index H is unused (all zeroes),
11317 terminate the search: the signature is not present in the table.
80626a55 11318
d2415c6c 11319 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11320
d2415c6c 11321 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11322 to stop at an unused slot or find the match. */
80626a55
DE
11323
11324/* Create a hash table to map DWO IDs to their CU/TU entry in
11325 .debug_{info,types}.dwo in DWP_FILE.
11326 Returns NULL if there isn't one.
11327 Note: This function processes DWP files only, not DWO files. */
11328
11329static struct dwp_hash_table *
ed2dc618
SM
11330create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11331 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11332{
11333 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11334 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11335 const gdb_byte *index_ptr, *index_end;
80626a55 11336 struct dwarf2_section_info *index;
73869dc2 11337 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11338 struct dwp_hash_table *htab;
11339
11340 if (is_debug_types)
11341 index = &dwp_file->sections.tu_index;
11342 else
11343 index = &dwp_file->sections.cu_index;
11344
96b79293 11345 if (index->empty ())
80626a55 11346 return NULL;
96b79293 11347 index->read (objfile);
80626a55
DE
11348
11349 index_ptr = index->buffer;
11350 index_end = index_ptr + index->size;
11351
11352 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11353 index_ptr += 4;
11354 if (version == 2)
11355 nr_columns = read_4_bytes (dbfd, index_ptr);
11356 else
11357 nr_columns = 0;
11358 index_ptr += 4;
80626a55
DE
11359 nr_units = read_4_bytes (dbfd, index_ptr);
11360 index_ptr += 4;
11361 nr_slots = read_4_bytes (dbfd, index_ptr);
11362 index_ptr += 4;
11363
73869dc2 11364 if (version != 1 && version != 2)
80626a55 11365 {
21aa081e 11366 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11367 " [in module %s]"),
21aa081e 11368 pulongest (version), dwp_file->name);
80626a55
DE
11369 }
11370 if (nr_slots != (nr_slots & -nr_slots))
11371 {
21aa081e 11372 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11373 " is not power of 2 [in module %s]"),
21aa081e 11374 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11375 }
11376
11377 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11378 htab->version = version;
11379 htab->nr_columns = nr_columns;
80626a55
DE
11380 htab->nr_units = nr_units;
11381 htab->nr_slots = nr_slots;
11382 htab->hash_table = index_ptr;
11383 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11384
11385 /* Exit early if the table is empty. */
11386 if (nr_slots == 0 || nr_units == 0
11387 || (version == 2 && nr_columns == 0))
11388 {
11389 /* All must be zero. */
11390 if (nr_slots != 0 || nr_units != 0
11391 || (version == 2 && nr_columns != 0))
11392 {
b98664d3 11393 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11394 " all zero [in modules %s]"),
11395 dwp_file->name);
11396 }
11397 return htab;
11398 }
11399
11400 if (version == 1)
11401 {
11402 htab->section_pool.v1.indices =
11403 htab->unit_table + sizeof (uint32_t) * nr_slots;
11404 /* It's harder to decide whether the section is too small in v1.
11405 V1 is deprecated anyway so we punt. */
11406 }
11407 else
11408 {
11409 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11410 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11411 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11412 /* Reverse map for error checking. */
11413 int ids_seen[DW_SECT_MAX + 1];
11414 int i;
11415
11416 if (nr_columns < 2)
11417 {
11418 error (_("Dwarf Error: bad DWP hash table, too few columns"
11419 " in section table [in module %s]"),
11420 dwp_file->name);
11421 }
11422 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11423 {
11424 error (_("Dwarf Error: bad DWP hash table, too many columns"
11425 " in section table [in module %s]"),
11426 dwp_file->name);
11427 }
04fd5eed
GB
11428 memset (ids, 255, sizeof_ids);
11429 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11430 for (i = 0; i < nr_columns; ++i)
11431 {
11432 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11433
11434 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11435 {
11436 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11437 " in section table [in module %s]"),
11438 id, dwp_file->name);
11439 }
11440 if (ids_seen[id] != -1)
11441 {
11442 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11443 " id %d in section table [in module %s]"),
11444 id, dwp_file->name);
11445 }
11446 ids_seen[id] = i;
11447 ids[i] = id;
11448 }
11449 /* Must have exactly one info or types section. */
11450 if (((ids_seen[DW_SECT_INFO] != -1)
11451 + (ids_seen[DW_SECT_TYPES] != -1))
11452 != 1)
11453 {
11454 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11455 " DWO info/types section [in module %s]"),
11456 dwp_file->name);
11457 }
11458 /* Must have an abbrev section. */
11459 if (ids_seen[DW_SECT_ABBREV] == -1)
11460 {
11461 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11462 " section [in module %s]"),
11463 dwp_file->name);
11464 }
11465 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11466 htab->section_pool.v2.sizes =
11467 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11468 * nr_units * nr_columns);
11469 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11470 * nr_units * nr_columns))
11471 > index_end)
11472 {
11473 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11474 " [in module %s]"),
11475 dwp_file->name);
11476 }
11477 }
80626a55
DE
11478
11479 return htab;
11480}
11481
11482/* Update SECTIONS with the data from SECTP.
11483
11484 This function is like the other "locate" section routines that are
11485 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11486 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11487
11488 The result is non-zero for success, or zero if an error was found. */
11489
11490static int
73869dc2
DE
11491locate_v1_virtual_dwo_sections (asection *sectp,
11492 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11493{
11494 const struct dwop_section_names *names = &dwop_section_names;
11495
11496 if (section_is_p (sectp->name, &names->abbrev_dwo))
11497 {
11498 /* There can be only one. */
049412e3 11499 if (sections->abbrev.s.section != NULL)
80626a55 11500 return 0;
049412e3 11501 sections->abbrev.s.section = sectp;
fd361982 11502 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11503 }
11504 else if (section_is_p (sectp->name, &names->info_dwo)
11505 || section_is_p (sectp->name, &names->types_dwo))
11506 {
11507 /* There can be only one. */
049412e3 11508 if (sections->info_or_types.s.section != NULL)
80626a55 11509 return 0;
049412e3 11510 sections->info_or_types.s.section = sectp;
fd361982 11511 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11512 }
11513 else if (section_is_p (sectp->name, &names->line_dwo))
11514 {
11515 /* There can be only one. */
049412e3 11516 if (sections->line.s.section != NULL)
80626a55 11517 return 0;
049412e3 11518 sections->line.s.section = sectp;
fd361982 11519 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11520 }
11521 else if (section_is_p (sectp->name, &names->loc_dwo))
11522 {
11523 /* There can be only one. */
049412e3 11524 if (sections->loc.s.section != NULL)
80626a55 11525 return 0;
049412e3 11526 sections->loc.s.section = sectp;
fd361982 11527 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11528 }
11529 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11530 {
11531 /* There can be only one. */
049412e3 11532 if (sections->macinfo.s.section != NULL)
80626a55 11533 return 0;
049412e3 11534 sections->macinfo.s.section = sectp;
fd361982 11535 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11536 }
11537 else if (section_is_p (sectp->name, &names->macro_dwo))
11538 {
11539 /* There can be only one. */
049412e3 11540 if (sections->macro.s.section != NULL)
80626a55 11541 return 0;
049412e3 11542 sections->macro.s.section = sectp;
fd361982 11543 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11544 }
11545 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11546 {
11547 /* There can be only one. */
049412e3 11548 if (sections->str_offsets.s.section != NULL)
80626a55 11549 return 0;
049412e3 11550 sections->str_offsets.s.section = sectp;
fd361982 11551 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11552 }
11553 else
11554 {
11555 /* No other kind of section is valid. */
11556 return 0;
11557 }
11558
11559 return 1;
11560}
11561
73869dc2
DE
11562/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11563 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11564 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11565 This is for DWP version 1 files. */
80626a55
DE
11566
11567static struct dwo_unit *
ed2dc618
SM
11568create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11569 struct dwp_file *dwp_file,
73869dc2
DE
11570 uint32_t unit_index,
11571 const char *comp_dir,
11572 ULONGEST signature, int is_debug_types)
80626a55
DE
11573{
11574 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11575 const struct dwp_hash_table *dwp_htab =
11576 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11577 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11578 const char *kind = is_debug_types ? "TU" : "CU";
11579 struct dwo_file *dwo_file;
11580 struct dwo_unit *dwo_unit;
73869dc2 11581 struct virtual_v1_dwo_sections sections;
80626a55 11582 void **dwo_file_slot;
80626a55
DE
11583 int i;
11584
73869dc2
DE
11585 gdb_assert (dwp_file->version == 1);
11586
b4f54984 11587 if (dwarf_read_debug)
80626a55 11588 {
73869dc2 11589 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11590 kind,
73869dc2 11591 pulongest (unit_index), hex_string (signature),
80626a55
DE
11592 dwp_file->name);
11593 }
11594
19ac8c2e 11595 /* Fetch the sections of this DWO unit.
80626a55
DE
11596 Put a limit on the number of sections we look for so that bad data
11597 doesn't cause us to loop forever. */
11598
73869dc2 11599#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11600 (1 /* .debug_info or .debug_types */ \
11601 + 1 /* .debug_abbrev */ \
11602 + 1 /* .debug_line */ \
11603 + 1 /* .debug_loc */ \
11604 + 1 /* .debug_str_offsets */ \
19ac8c2e 11605 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11606 + 1 /* trailing zero */)
11607
11608 memset (&sections, 0, sizeof (sections));
80626a55 11609
73869dc2 11610 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11611 {
11612 asection *sectp;
11613 uint32_t section_nr =
11614 read_4_bytes (dbfd,
73869dc2
DE
11615 dwp_htab->section_pool.v1.indices
11616 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11617
11618 if (section_nr == 0)
11619 break;
11620 if (section_nr >= dwp_file->num_sections)
11621 {
11622 error (_("Dwarf Error: bad DWP hash table, section number too large"
11623 " [in module %s]"),
11624 dwp_file->name);
11625 }
11626
11627 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11628 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11629 {
11630 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11631 " [in module %s]"),
11632 dwp_file->name);
11633 }
11634 }
11635
11636 if (i < 2
96b79293
TT
11637 || sections.info_or_types.empty ()
11638 || sections.abbrev.empty ())
80626a55
DE
11639 {
11640 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11641 " [in module %s]"),
11642 dwp_file->name);
11643 }
73869dc2 11644 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11645 {
11646 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11647 " [in module %s]"),
11648 dwp_file->name);
11649 }
11650
11651 /* It's easier for the rest of the code if we fake a struct dwo_file and
11652 have dwo_unit "live" in that. At least for now.
11653
11654 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11655 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11656 file, we can combine them back into a virtual DWO file to save space
11657 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11658 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11659
791afaa2
TT
11660 std::string virtual_dwo_name =
11661 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11662 sections.abbrev.get_id (),
11663 sections.line.get_id (),
11664 sections.loc.get_id (),
11665 sections.str_offsets.get_id ());
80626a55 11666 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11667 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11668 virtual_dwo_name.c_str (),
11669 comp_dir);
80626a55
DE
11670 /* Create one if necessary. */
11671 if (*dwo_file_slot == NULL)
11672 {
b4f54984 11673 if (dwarf_read_debug)
80626a55
DE
11674 {
11675 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11676 virtual_dwo_name.c_str ());
80626a55 11677 }
51ac9db5 11678 dwo_file = new struct dwo_file;
efba19b0
TT
11679 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11680 virtual_dwo_name);
0ac5b59e 11681 dwo_file->comp_dir = comp_dir;
80626a55
DE
11682 dwo_file->sections.abbrev = sections.abbrev;
11683 dwo_file->sections.line = sections.line;
11684 dwo_file->sections.loc = sections.loc;
11685 dwo_file->sections.macinfo = sections.macinfo;
11686 dwo_file->sections.macro = sections.macro;
11687 dwo_file->sections.str_offsets = sections.str_offsets;
11688 /* The "str" section is global to the entire DWP file. */
11689 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11690 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11691 there's no need to record it in dwo_file.
11692 Also, we can't simply record type sections in dwo_file because
11693 we record a pointer into the vector in dwo_unit. As we collect more
11694 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11695 for it, invalidating all copies of pointers into the previous
11696 contents. */
80626a55
DE
11697 *dwo_file_slot = dwo_file;
11698 }
11699 else
11700 {
b4f54984 11701 if (dwarf_read_debug)
80626a55
DE
11702 {
11703 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11704 virtual_dwo_name.c_str ());
80626a55 11705 }
9a3c8263 11706 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11707 }
80626a55
DE
11708
11709 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11710 dwo_unit->dwo_file = dwo_file;
11711 dwo_unit->signature = signature;
8d749320
SM
11712 dwo_unit->section =
11713 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11714 *dwo_unit->section = sections.info_or_types;
57d63ce2 11715 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11716
11717 return dwo_unit;
11718}
11719
73869dc2
DE
11720/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11721 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11722 piece within that section used by a TU/CU, return a virtual section
11723 of just that piece. */
11724
11725static struct dwarf2_section_info
ed2dc618
SM
11726create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11727 struct dwarf2_section_info *section,
73869dc2
DE
11728 bfd_size_type offset, bfd_size_type size)
11729{
11730 struct dwarf2_section_info result;
11731 asection *sectp;
11732
11733 gdb_assert (section != NULL);
11734 gdb_assert (!section->is_virtual);
11735
11736 memset (&result, 0, sizeof (result));
11737 result.s.containing_section = section;
dc4ccb6f 11738 result.is_virtual = true;
73869dc2
DE
11739
11740 if (size == 0)
11741 return result;
11742
96b79293 11743 sectp = section->get_bfd_section ();
73869dc2
DE
11744
11745 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11746 bounds of the real section. This is a pretty-rare event, so just
11747 flag an error (easier) instead of a warning and trying to cope. */
11748 if (sectp == NULL
fd361982 11749 || offset + size > bfd_section_size (sectp))
73869dc2 11750 {
73869dc2
DE
11751 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11752 " in section %s [in module %s]"),
fd361982 11753 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11754 objfile_name (dwarf2_per_objfile->objfile));
11755 }
11756
11757 result.virtual_offset = offset;
11758 result.size = size;
11759 return result;
11760}
11761
11762/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11763 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11764 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11765 This is for DWP version 2 files. */
11766
11767static struct dwo_unit *
ed2dc618
SM
11768create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11769 struct dwp_file *dwp_file,
73869dc2
DE
11770 uint32_t unit_index,
11771 const char *comp_dir,
11772 ULONGEST signature, int is_debug_types)
11773{
11774 struct objfile *objfile = dwarf2_per_objfile->objfile;
11775 const struct dwp_hash_table *dwp_htab =
11776 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11777 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11778 const char *kind = is_debug_types ? "TU" : "CU";
11779 struct dwo_file *dwo_file;
11780 struct dwo_unit *dwo_unit;
11781 struct virtual_v2_dwo_sections sections;
11782 void **dwo_file_slot;
73869dc2
DE
11783 int i;
11784
11785 gdb_assert (dwp_file->version == 2);
11786
b4f54984 11787 if (dwarf_read_debug)
73869dc2
DE
11788 {
11789 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11790 kind,
11791 pulongest (unit_index), hex_string (signature),
11792 dwp_file->name);
11793 }
11794
11795 /* Fetch the section offsets of this DWO unit. */
11796
11797 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11798
11799 for (i = 0; i < dwp_htab->nr_columns; ++i)
11800 {
11801 uint32_t offset = read_4_bytes (dbfd,
11802 dwp_htab->section_pool.v2.offsets
11803 + (((unit_index - 1) * dwp_htab->nr_columns
11804 + i)
11805 * sizeof (uint32_t)));
11806 uint32_t size = read_4_bytes (dbfd,
11807 dwp_htab->section_pool.v2.sizes
11808 + (((unit_index - 1) * dwp_htab->nr_columns
11809 + i)
11810 * sizeof (uint32_t)));
11811
11812 switch (dwp_htab->section_pool.v2.section_ids[i])
11813 {
11814 case DW_SECT_INFO:
11815 case DW_SECT_TYPES:
11816 sections.info_or_types_offset = offset;
11817 sections.info_or_types_size = size;
11818 break;
11819 case DW_SECT_ABBREV:
11820 sections.abbrev_offset = offset;
11821 sections.abbrev_size = size;
11822 break;
11823 case DW_SECT_LINE:
11824 sections.line_offset = offset;
11825 sections.line_size = size;
11826 break;
11827 case DW_SECT_LOC:
11828 sections.loc_offset = offset;
11829 sections.loc_size = size;
11830 break;
11831 case DW_SECT_STR_OFFSETS:
11832 sections.str_offsets_offset = offset;
11833 sections.str_offsets_size = size;
11834 break;
11835 case DW_SECT_MACINFO:
11836 sections.macinfo_offset = offset;
11837 sections.macinfo_size = size;
11838 break;
11839 case DW_SECT_MACRO:
11840 sections.macro_offset = offset;
11841 sections.macro_size = size;
11842 break;
11843 }
11844 }
11845
11846 /* It's easier for the rest of the code if we fake a struct dwo_file and
11847 have dwo_unit "live" in that. At least for now.
11848
11849 The DWP file can be made up of a random collection of CUs and TUs.
11850 However, for each CU + set of TUs that came from the same original DWO
11851 file, we can combine them back into a virtual DWO file to save space
11852 (fewer struct dwo_file objects to allocate). Remember that for really
11853 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11854
791afaa2
TT
11855 std::string virtual_dwo_name =
11856 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11857 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11858 (long) (sections.line_size ? sections.line_offset : 0),
11859 (long) (sections.loc_size ? sections.loc_offset : 0),
11860 (long) (sections.str_offsets_size
11861 ? sections.str_offsets_offset : 0));
73869dc2 11862 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11863 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11864 virtual_dwo_name.c_str (),
11865 comp_dir);
73869dc2
DE
11866 /* Create one if necessary. */
11867 if (*dwo_file_slot == NULL)
11868 {
b4f54984 11869 if (dwarf_read_debug)
73869dc2
DE
11870 {
11871 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11872 virtual_dwo_name.c_str ());
73869dc2 11873 }
51ac9db5 11874 dwo_file = new struct dwo_file;
efba19b0
TT
11875 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11876 virtual_dwo_name);
73869dc2
DE
11877 dwo_file->comp_dir = comp_dir;
11878 dwo_file->sections.abbrev =
ed2dc618 11879 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11880 sections.abbrev_offset, sections.abbrev_size);
11881 dwo_file->sections.line =
ed2dc618 11882 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11883 sections.line_offset, sections.line_size);
11884 dwo_file->sections.loc =
ed2dc618 11885 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11886 sections.loc_offset, sections.loc_size);
11887 dwo_file->sections.macinfo =
ed2dc618 11888 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11889 sections.macinfo_offset, sections.macinfo_size);
11890 dwo_file->sections.macro =
ed2dc618 11891 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11892 sections.macro_offset, sections.macro_size);
11893 dwo_file->sections.str_offsets =
ed2dc618
SM
11894 create_dwp_v2_section (dwarf2_per_objfile,
11895 &dwp_file->sections.str_offsets,
73869dc2
DE
11896 sections.str_offsets_offset,
11897 sections.str_offsets_size);
11898 /* The "str" section is global to the entire DWP file. */
11899 dwo_file->sections.str = dwp_file->sections.str;
11900 /* The info or types section is assigned below to dwo_unit,
11901 there's no need to record it in dwo_file.
11902 Also, we can't simply record type sections in dwo_file because
11903 we record a pointer into the vector in dwo_unit. As we collect more
11904 types we'll grow the vector and eventually have to reallocate space
11905 for it, invalidating all copies of pointers into the previous
11906 contents. */
11907 *dwo_file_slot = dwo_file;
11908 }
11909 else
11910 {
b4f54984 11911 if (dwarf_read_debug)
73869dc2
DE
11912 {
11913 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11914 virtual_dwo_name.c_str ());
73869dc2 11915 }
9a3c8263 11916 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11917 }
73869dc2
DE
11918
11919 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11920 dwo_unit->dwo_file = dwo_file;
11921 dwo_unit->signature = signature;
8d749320
SM
11922 dwo_unit->section =
11923 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11924 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11925 is_debug_types
73869dc2
DE
11926 ? &dwp_file->sections.types
11927 : &dwp_file->sections.info,
11928 sections.info_or_types_offset,
11929 sections.info_or_types_size);
11930 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11931
11932 return dwo_unit;
11933}
11934
57d63ce2
DE
11935/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11936 Returns NULL if the signature isn't found. */
80626a55
DE
11937
11938static struct dwo_unit *
ed2dc618
SM
11939lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11940 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11941 ULONGEST signature, int is_debug_types)
80626a55 11942{
57d63ce2
DE
11943 const struct dwp_hash_table *dwp_htab =
11944 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11945 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11946 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11947 uint32_t hash = signature & mask;
11948 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11949 unsigned int i;
11950 void **slot;
870f88f7 11951 struct dwo_unit find_dwo_cu;
80626a55
DE
11952
11953 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11954 find_dwo_cu.signature = signature;
19ac8c2e 11955 slot = htab_find_slot (is_debug_types
48b490f2
TT
11956 ? dwp_file->loaded_tus.get ()
11957 : dwp_file->loaded_cus.get (),
19ac8c2e 11958 &find_dwo_cu, INSERT);
80626a55
DE
11959
11960 if (*slot != NULL)
9a3c8263 11961 return (struct dwo_unit *) *slot;
80626a55
DE
11962
11963 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11964 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11965 {
11966 ULONGEST signature_in_table;
11967
11968 signature_in_table =
57d63ce2 11969 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11970 if (signature_in_table == signature)
11971 {
57d63ce2
DE
11972 uint32_t unit_index =
11973 read_4_bytes (dbfd,
11974 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11975
73869dc2
DE
11976 if (dwp_file->version == 1)
11977 {
ed2dc618
SM
11978 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11979 dwp_file, unit_index,
73869dc2
DE
11980 comp_dir, signature,
11981 is_debug_types);
11982 }
11983 else
11984 {
ed2dc618
SM
11985 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11986 dwp_file, unit_index,
73869dc2
DE
11987 comp_dir, signature,
11988 is_debug_types);
11989 }
9a3c8263 11990 return (struct dwo_unit *) *slot;
80626a55
DE
11991 }
11992 if (signature_in_table == 0)
11993 return NULL;
11994 hash = (hash + hash2) & mask;
11995 }
11996
11997 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11998 " [in module %s]"),
11999 dwp_file->name);
12000}
12001
ab5088bf 12002/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12003 Open the file specified by FILE_NAME and hand it off to BFD for
12004 preliminary analysis. Return a newly initialized bfd *, which
12005 includes a canonicalized copy of FILE_NAME.
80626a55 12006 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12007 SEARCH_CWD is true if the current directory is to be searched.
12008 It will be searched before debug-file-directory.
13aaf454
DE
12009 If successful, the file is added to the bfd include table of the
12010 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12011 If unable to find/open the file, return NULL.
3019eac3
DE
12012 NOTE: This function is derived from symfile_bfd_open. */
12013
192b62ce 12014static gdb_bfd_ref_ptr
ed2dc618
SM
12015try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12016 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12017{
24b9144d 12018 int desc;
9c02c129
DE
12019 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12020 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12021 to debug_file_directory. */
e0cc99a6 12022 const char *search_path;
9c02c129
DE
12023 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12024
e0cc99a6 12025 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12026 if (search_cwd)
12027 {
12028 if (*debug_file_directory != '\0')
e0cc99a6
TT
12029 {
12030 search_path_holder.reset (concat (".", dirname_separator_string,
12031 debug_file_directory,
12032 (char *) NULL));
12033 search_path = search_path_holder.get ();
12034 }
6ac97d4c 12035 else
e0cc99a6 12036 search_path = ".";
6ac97d4c 12037 }
9c02c129 12038 else
e0cc99a6 12039 search_path = debug_file_directory;
3019eac3 12040
24b9144d 12041 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12042 if (is_dwp)
12043 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12044
12045 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12046 desc = openp (search_path, flags, file_name,
3019eac3
DE
12047 O_RDONLY | O_BINARY, &absolute_name);
12048 if (desc < 0)
12049 return NULL;
12050
e0cc99a6
TT
12051 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12052 gnutarget, desc));
9c02c129
DE
12053 if (sym_bfd == NULL)
12054 return NULL;
192b62ce 12055 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12056
192b62ce
TT
12057 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12058 return NULL;
3019eac3 12059
13aaf454
DE
12060 /* Success. Record the bfd as having been included by the objfile's bfd.
12061 This is important because things like demangled_names_hash lives in the
12062 objfile's per_bfd space and may have references to things like symbol
12063 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12064 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12065
3019eac3
DE
12066 return sym_bfd;
12067}
12068
ab5088bf 12069/* Try to open DWO file FILE_NAME.
3019eac3
DE
12070 COMP_DIR is the DW_AT_comp_dir attribute.
12071 The result is the bfd handle of the file.
12072 If there is a problem finding or opening the file, return NULL.
12073 Upon success, the canonicalized path of the file is stored in the bfd,
12074 same as symfile_bfd_open. */
12075
192b62ce 12076static gdb_bfd_ref_ptr
ed2dc618
SM
12077open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12078 const char *file_name, const char *comp_dir)
3019eac3 12079{
80626a55 12080 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12081 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12082 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12083
12084 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12085
12086 if (comp_dir != NULL)
12087 {
43816ebc
TT
12088 gdb::unique_xmalloc_ptr<char> path_to_try
12089 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12090
12091 /* NOTE: If comp_dir is a relative path, this will also try the
12092 search path, which seems useful. */
ed2dc618 12093 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12094 path_to_try.get (),
ed2dc618 12095 0 /*is_dwp*/,
192b62ce 12096 1 /*search_cwd*/));
3019eac3
DE
12097 if (abfd != NULL)
12098 return abfd;
12099 }
12100
12101 /* That didn't work, try debug-file-directory, which, despite its name,
12102 is a list of paths. */
12103
12104 if (*debug_file_directory == '\0')
12105 return NULL;
12106
ed2dc618
SM
12107 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12108 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12109}
12110
80626a55
DE
12111/* This function is mapped across the sections and remembers the offset and
12112 size of each of the DWO debugging sections we are interested in. */
12113
12114static void
12115dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12116{
9a3c8263 12117 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12118 const struct dwop_section_names *names = &dwop_section_names;
12119
12120 if (section_is_p (sectp->name, &names->abbrev_dwo))
12121 {
049412e3 12122 dwo_sections->abbrev.s.section = sectp;
fd361982 12123 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12124 }
12125 else if (section_is_p (sectp->name, &names->info_dwo))
12126 {
049412e3 12127 dwo_sections->info.s.section = sectp;
fd361982 12128 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12129 }
12130 else if (section_is_p (sectp->name, &names->line_dwo))
12131 {
049412e3 12132 dwo_sections->line.s.section = sectp;
fd361982 12133 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12134 }
12135 else if (section_is_p (sectp->name, &names->loc_dwo))
12136 {
049412e3 12137 dwo_sections->loc.s.section = sectp;
fd361982 12138 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12139 }
12140 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12141 {
049412e3 12142 dwo_sections->macinfo.s.section = sectp;
fd361982 12143 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12144 }
12145 else if (section_is_p (sectp->name, &names->macro_dwo))
12146 {
049412e3 12147 dwo_sections->macro.s.section = sectp;
fd361982 12148 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12149 }
12150 else if (section_is_p (sectp->name, &names->str_dwo))
12151 {
049412e3 12152 dwo_sections->str.s.section = sectp;
fd361982 12153 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12154 }
12155 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12156 {
049412e3 12157 dwo_sections->str_offsets.s.section = sectp;
fd361982 12158 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12159 }
12160 else if (section_is_p (sectp->name, &names->types_dwo))
12161 {
12162 struct dwarf2_section_info type_section;
12163
12164 memset (&type_section, 0, sizeof (type_section));
049412e3 12165 type_section.s.section = sectp;
fd361982 12166 type_section.size = bfd_section_size (sectp);
fd5866f6 12167 dwo_sections->types.push_back (type_section);
80626a55
DE
12168 }
12169}
12170
ab5088bf 12171/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12172 by PER_CU. This is for the non-DWP case.
80626a55 12173 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12174
12175static struct dwo_file *
0ac5b59e
DE
12176open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12177 const char *dwo_name, const char *comp_dir)
3019eac3 12178{
ed2dc618 12179 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12180
fb1eb2f9 12181 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12182 if (dbfd == NULL)
12183 {
b4f54984 12184 if (dwarf_read_debug)
80626a55
DE
12185 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12186 return NULL;
12187 }
263db9a1 12188
51ac9db5 12189 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12190 dwo_file->dwo_name = dwo_name;
12191 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12192 dwo_file->dbfd = std::move (dbfd);
3019eac3 12193
fb1eb2f9 12194 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12195 &dwo_file->sections);
3019eac3 12196
18a8505e
AT
12197 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12198 dwo_file->sections.info, dwo_file->cus);
3019eac3 12199
263db9a1 12200 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12201 dwo_file->sections.types, dwo_file->tus);
3019eac3 12202
b4f54984 12203 if (dwarf_read_debug)
80626a55
DE
12204 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12205
263db9a1 12206 return dwo_file.release ();
3019eac3
DE
12207}
12208
80626a55 12209/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12210 size of each of the DWP debugging sections common to version 1 and 2 that
12211 we are interested in. */
3019eac3 12212
80626a55 12213static void
73869dc2
DE
12214dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12215 void *dwp_file_ptr)
3019eac3 12216{
9a3c8263 12217 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12218 const struct dwop_section_names *names = &dwop_section_names;
12219 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12220
80626a55 12221 /* Record the ELF section number for later lookup: this is what the
73869dc2 12222 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12223 gdb_assert (elf_section_nr < dwp_file->num_sections);
12224 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12225
80626a55
DE
12226 /* Look for specific sections that we need. */
12227 if (section_is_p (sectp->name, &names->str_dwo))
12228 {
049412e3 12229 dwp_file->sections.str.s.section = sectp;
fd361982 12230 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12231 }
12232 else if (section_is_p (sectp->name, &names->cu_index))
12233 {
049412e3 12234 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12235 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12236 }
12237 else if (section_is_p (sectp->name, &names->tu_index))
12238 {
049412e3 12239 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12240 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12241 }
12242}
3019eac3 12243
73869dc2
DE
12244/* This function is mapped across the sections and remembers the offset and
12245 size of each of the DWP version 2 debugging sections that we are interested
12246 in. This is split into a separate function because we don't know if we
12247 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12248
12249static void
12250dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12251{
9a3c8263 12252 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12253 const struct dwop_section_names *names = &dwop_section_names;
12254 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12255
12256 /* Record the ELF section number for later lookup: this is what the
12257 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12258 gdb_assert (elf_section_nr < dwp_file->num_sections);
12259 dwp_file->elf_sections[elf_section_nr] = sectp;
12260
12261 /* Look for specific sections that we need. */
12262 if (section_is_p (sectp->name, &names->abbrev_dwo))
12263 {
049412e3 12264 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12265 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12266 }
12267 else if (section_is_p (sectp->name, &names->info_dwo))
12268 {
049412e3 12269 dwp_file->sections.info.s.section = sectp;
fd361982 12270 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12271 }
12272 else if (section_is_p (sectp->name, &names->line_dwo))
12273 {
049412e3 12274 dwp_file->sections.line.s.section = sectp;
fd361982 12275 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12276 }
12277 else if (section_is_p (sectp->name, &names->loc_dwo))
12278 {
049412e3 12279 dwp_file->sections.loc.s.section = sectp;
fd361982 12280 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12281 }
12282 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12283 {
049412e3 12284 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12285 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12286 }
12287 else if (section_is_p (sectp->name, &names->macro_dwo))
12288 {
049412e3 12289 dwp_file->sections.macro.s.section = sectp;
fd361982 12290 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12291 }
12292 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12293 {
049412e3 12294 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12295 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12296 }
12297 else if (section_is_p (sectp->name, &names->types_dwo))
12298 {
049412e3 12299 dwp_file->sections.types.s.section = sectp;
fd361982 12300 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12301 }
12302}
12303
80626a55 12304/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12305
80626a55
DE
12306static hashval_t
12307hash_dwp_loaded_cutus (const void *item)
12308{
9a3c8263 12309 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12310
80626a55
DE
12311 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12312 return dwo_unit->signature;
3019eac3
DE
12313}
12314
80626a55 12315/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12316
80626a55
DE
12317static int
12318eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12319{
9a3c8263
SM
12320 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12321 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12322
80626a55
DE
12323 return dua->signature == dub->signature;
12324}
3019eac3 12325
80626a55 12326/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12327
48b490f2 12328static htab_up
298e9637 12329allocate_dwp_loaded_cutus_table ()
80626a55 12330{
48b490f2
TT
12331 return htab_up (htab_create_alloc (3,
12332 hash_dwp_loaded_cutus,
12333 eq_dwp_loaded_cutus,
12334 NULL, xcalloc, xfree));
80626a55 12335}
3019eac3 12336
ab5088bf
DE
12337/* Try to open DWP file FILE_NAME.
12338 The result is the bfd handle of the file.
12339 If there is a problem finding or opening the file, return NULL.
12340 Upon success, the canonicalized path of the file is stored in the bfd,
12341 same as symfile_bfd_open. */
12342
192b62ce 12343static gdb_bfd_ref_ptr
ed2dc618
SM
12344open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12345 const char *file_name)
ab5088bf 12346{
ed2dc618
SM
12347 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12348 1 /*is_dwp*/,
192b62ce 12349 1 /*search_cwd*/));
6ac97d4c
DE
12350 if (abfd != NULL)
12351 return abfd;
12352
12353 /* Work around upstream bug 15652.
12354 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12355 [Whether that's a "bug" is debatable, but it is getting in our way.]
12356 We have no real idea where the dwp file is, because gdb's realpath-ing
12357 of the executable's path may have discarded the needed info.
12358 [IWBN if the dwp file name was recorded in the executable, akin to
12359 .gnu_debuglink, but that doesn't exist yet.]
12360 Strip the directory from FILE_NAME and search again. */
12361 if (*debug_file_directory != '\0')
12362 {
12363 /* Don't implicitly search the current directory here.
12364 If the user wants to search "." to handle this case,
12365 it must be added to debug-file-directory. */
ed2dc618
SM
12366 return try_open_dwop_file (dwarf2_per_objfile,
12367 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12368 0 /*search_cwd*/);
12369 }
12370
12371 return NULL;
ab5088bf
DE
12372}
12373
80626a55
DE
12374/* Initialize the use of the DWP file for the current objfile.
12375 By convention the name of the DWP file is ${objfile}.dwp.
12376 The result is NULL if it can't be found. */
a766d390 12377
400174b1 12378static std::unique_ptr<struct dwp_file>
ed2dc618 12379open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12380{
12381 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12382
82bf32bc
JK
12383 /* Try to find first .dwp for the binary file before any symbolic links
12384 resolving. */
6c447423
DE
12385
12386 /* If the objfile is a debug file, find the name of the real binary
12387 file and get the name of dwp file from there. */
d721ba37 12388 std::string dwp_name;
6c447423
DE
12389 if (objfile->separate_debug_objfile_backlink != NULL)
12390 {
12391 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12392 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12393
d721ba37 12394 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12395 }
12396 else
d721ba37
PA
12397 dwp_name = objfile->original_name;
12398
12399 dwp_name += ".dwp";
80626a55 12400
ed2dc618 12401 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12402 if (dbfd == NULL
12403 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12404 {
12405 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12406 dwp_name = objfile_name (objfile);
12407 dwp_name += ".dwp";
ed2dc618 12408 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12409 }
12410
80626a55
DE
12411 if (dbfd == NULL)
12412 {
b4f54984 12413 if (dwarf_read_debug)
d721ba37 12414 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12415 return std::unique_ptr<dwp_file> ();
3019eac3 12416 }
400174b1
TT
12417
12418 const char *name = bfd_get_filename (dbfd.get ());
12419 std::unique_ptr<struct dwp_file> dwp_file
12420 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12421
0a0f4c01 12422 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12423 dwp_file->elf_sections =
12424 OBSTACK_CALLOC (&objfile->objfile_obstack,
12425 dwp_file->num_sections, asection *);
12426
400174b1
TT
12427 bfd_map_over_sections (dwp_file->dbfd.get (),
12428 dwarf2_locate_common_dwp_sections,
12429 dwp_file.get ());
80626a55 12430
400174b1
TT
12431 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12432 0);
80626a55 12433
400174b1
TT
12434 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12435 1);
80626a55 12436
73869dc2 12437 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12438 if (dwp_file->cus && dwp_file->tus
12439 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12440 {
12441 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12442 pretty bizarre. We use pulongest here because that's the established
4d65956b 12443 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12444 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12445 " TU version %s [in DWP file %s]"),
12446 pulongest (dwp_file->cus->version),
d721ba37 12447 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12448 }
08302ed2
DE
12449
12450 if (dwp_file->cus)
12451 dwp_file->version = dwp_file->cus->version;
12452 else if (dwp_file->tus)
12453 dwp_file->version = dwp_file->tus->version;
12454 else
12455 dwp_file->version = 2;
73869dc2
DE
12456
12457 if (dwp_file->version == 2)
400174b1
TT
12458 bfd_map_over_sections (dwp_file->dbfd.get (),
12459 dwarf2_locate_v2_dwp_sections,
12460 dwp_file.get ());
73869dc2 12461
298e9637
SM
12462 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12463 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12464
b4f54984 12465 if (dwarf_read_debug)
80626a55
DE
12466 {
12467 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12468 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12469 " %s CUs, %s TUs\n",
12470 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12471 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12472 }
12473
12474 return dwp_file;
3019eac3 12475}
c906108c 12476
ab5088bf
DE
12477/* Wrapper around open_and_init_dwp_file, only open it once. */
12478
12479static struct dwp_file *
ed2dc618 12480get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12481{
12482 if (! dwarf2_per_objfile->dwp_checked)
12483 {
ed2dc618
SM
12484 dwarf2_per_objfile->dwp_file
12485 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12486 dwarf2_per_objfile->dwp_checked = 1;
12487 }
400174b1 12488 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12489}
12490
80626a55
DE
12491/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12492 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12493 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12494 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12495 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12496
12497 This is called, for example, when wanting to read a variable with a
12498 complex location. Therefore we don't want to do file i/o for every call.
12499 Therefore we don't want to look for a DWO file on every call.
12500 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12501 then we check if we've already seen DWO_NAME, and only THEN do we check
12502 for a DWO file.
12503
1c658ad5 12504 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12505 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12506
3019eac3 12507static struct dwo_unit *
80626a55
DE
12508lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12509 const char *dwo_name, const char *comp_dir,
12510 ULONGEST signature, int is_debug_types)
3019eac3 12511{
ed2dc618 12512 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12513 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12514 const char *kind = is_debug_types ? "TU" : "CU";
12515 void **dwo_file_slot;
3019eac3 12516 struct dwo_file *dwo_file;
80626a55 12517 struct dwp_file *dwp_file;
cb1df416 12518
6a506a2d
DE
12519 /* First see if there's a DWP file.
12520 If we have a DWP file but didn't find the DWO inside it, don't
12521 look for the original DWO file. It makes gdb behave differently
12522 depending on whether one is debugging in the build tree. */
cf2c3c16 12523
ed2dc618 12524 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12525 if (dwp_file != NULL)
cf2c3c16 12526 {
80626a55
DE
12527 const struct dwp_hash_table *dwp_htab =
12528 is_debug_types ? dwp_file->tus : dwp_file->cus;
12529
12530 if (dwp_htab != NULL)
12531 {
12532 struct dwo_unit *dwo_cutu =
ed2dc618 12533 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12534 signature, is_debug_types);
80626a55
DE
12535
12536 if (dwo_cutu != NULL)
12537 {
b4f54984 12538 if (dwarf_read_debug)
80626a55
DE
12539 {
12540 fprintf_unfiltered (gdb_stdlog,
12541 "Virtual DWO %s %s found: @%s\n",
12542 kind, hex_string (signature),
12543 host_address_to_string (dwo_cutu));
12544 }
12545 return dwo_cutu;
12546 }
12547 }
12548 }
6a506a2d 12549 else
80626a55 12550 {
6a506a2d 12551 /* No DWP file, look for the DWO file. */
80626a55 12552
ed2dc618
SM
12553 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12554 dwo_name, comp_dir);
6a506a2d 12555 if (*dwo_file_slot == NULL)
80626a55 12556 {
6a506a2d
DE
12557 /* Read in the file and build a table of the CUs/TUs it contains. */
12558 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12559 }
6a506a2d 12560 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12561 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12562
6a506a2d 12563 if (dwo_file != NULL)
19c3d4c9 12564 {
6a506a2d
DE
12565 struct dwo_unit *dwo_cutu = NULL;
12566
12567 if (is_debug_types && dwo_file->tus)
12568 {
12569 struct dwo_unit find_dwo_cutu;
12570
12571 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12572 find_dwo_cutu.signature = signature;
9a3c8263 12573 dwo_cutu
b0b6a987
TT
12574 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12575 &find_dwo_cutu);
6a506a2d 12576 }
33c5cd75 12577 else if (!is_debug_types && dwo_file->cus)
80626a55 12578 {
33c5cd75
DB
12579 struct dwo_unit find_dwo_cutu;
12580
12581 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12582 find_dwo_cutu.signature = signature;
b0b6a987 12583 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12584 &find_dwo_cutu);
6a506a2d
DE
12585 }
12586
12587 if (dwo_cutu != NULL)
12588 {
b4f54984 12589 if (dwarf_read_debug)
6a506a2d
DE
12590 {
12591 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12592 kind, dwo_name, hex_string (signature),
12593 host_address_to_string (dwo_cutu));
12594 }
12595 return dwo_cutu;
80626a55
DE
12596 }
12597 }
2e276125 12598 }
9cdd5dbd 12599
80626a55
DE
12600 /* We didn't find it. This could mean a dwo_id mismatch, or
12601 someone deleted the DWO/DWP file, or the search path isn't set up
12602 correctly to find the file. */
12603
b4f54984 12604 if (dwarf_read_debug)
80626a55
DE
12605 {
12606 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12607 kind, dwo_name, hex_string (signature));
12608 }
3019eac3 12609
6656a72d
DE
12610 /* This is a warning and not a complaint because it can be caused by
12611 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12612 {
12613 /* Print the name of the DWP file if we looked there, helps the user
12614 better diagnose the problem. */
791afaa2 12615 std::string dwp_text;
43942612
DE
12616
12617 if (dwp_file != NULL)
791afaa2
TT
12618 dwp_text = string_printf (" [in DWP file %s]",
12619 lbasename (dwp_file->name));
43942612 12620
9d8780f0 12621 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12622 " [in module %s]"),
12623 kind, dwo_name, hex_string (signature),
791afaa2 12624 dwp_text.c_str (),
43942612 12625 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12626 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12627 }
3019eac3 12628 return NULL;
5fb290d7
DJ
12629}
12630
80626a55
DE
12631/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12632 See lookup_dwo_cutu_unit for details. */
12633
12634static struct dwo_unit *
12635lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12636 const char *dwo_name, const char *comp_dir,
12637 ULONGEST signature)
12638{
12639 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12640}
12641
12642/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12643 See lookup_dwo_cutu_unit for details. */
12644
12645static struct dwo_unit *
12646lookup_dwo_type_unit (struct signatured_type *this_tu,
12647 const char *dwo_name, const char *comp_dir)
12648{
12649 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12650}
12651
89e63ee4
DE
12652/* Traversal function for queue_and_load_all_dwo_tus. */
12653
12654static int
12655queue_and_load_dwo_tu (void **slot, void *info)
12656{
12657 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12658 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12659 ULONGEST signature = dwo_unit->signature;
12660 struct signatured_type *sig_type =
12661 lookup_dwo_signatured_type (per_cu->cu, signature);
12662
12663 if (sig_type != NULL)
12664 {
12665 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12666
12667 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12668 a real dependency of PER_CU on SIG_TYPE. That is detected later
12669 while processing PER_CU. */
12670 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12671 load_full_type_unit (sig_cu);
ae640021 12672 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12673 }
12674
12675 return 1;
12676}
12677
12678/* Queue all TUs contained in the DWO of PER_CU to be read in.
12679 The DWO may have the only definition of the type, though it may not be
12680 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12681 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12682
12683static void
12684queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12685{
12686 struct dwo_unit *dwo_unit;
12687 struct dwo_file *dwo_file;
12688
12689 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12690 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12691 gdb_assert (per_cu->cu != NULL);
12692
12693 dwo_unit = per_cu->cu->dwo_unit;
12694 gdb_assert (dwo_unit != NULL);
12695
12696 dwo_file = dwo_unit->dwo_file;
12697 if (dwo_file->tus != NULL)
b0b6a987
TT
12698 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12699 per_cu);
89e63ee4
DE
12700}
12701
3019eac3 12702/* Read in various DIEs. */
348e048f 12703
d389af10 12704/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12705 Inherit only the children of the DW_AT_abstract_origin DIE not being
12706 already referenced by DW_AT_abstract_origin from the children of the
12707 current DIE. */
d389af10
JK
12708
12709static void
12710inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12711{
12712 struct die_info *child_die;
791afaa2 12713 sect_offset *offsetp;
d389af10
JK
12714 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12715 struct die_info *origin_die;
12716 /* Iterator of the ORIGIN_DIE children. */
12717 struct die_info *origin_child_die;
d389af10 12718 struct attribute *attr;
cd02d79d
PA
12719 struct dwarf2_cu *origin_cu;
12720 struct pending **origin_previous_list_in_scope;
d389af10
JK
12721
12722 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12723 if (!attr)
12724 return;
12725
cd02d79d
PA
12726 /* Note that following die references may follow to a die in a
12727 different cu. */
12728
12729 origin_cu = cu;
12730 origin_die = follow_die_ref (die, attr, &origin_cu);
12731
12732 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12733 symbols in. */
12734 origin_previous_list_in_scope = origin_cu->list_in_scope;
12735 origin_cu->list_in_scope = cu->list_in_scope;
12736
edb3359d
DJ
12737 if (die->tag != origin_die->tag
12738 && !(die->tag == DW_TAG_inlined_subroutine
12739 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12740 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12741 sect_offset_str (die->sect_off),
12742 sect_offset_str (origin_die->sect_off));
d389af10 12743
791afaa2 12744 std::vector<sect_offset> offsets;
d389af10 12745
3ea89b92
PMR
12746 for (child_die = die->child;
12747 child_die && child_die->tag;
12748 child_die = sibling_die (child_die))
12749 {
12750 struct die_info *child_origin_die;
12751 struct dwarf2_cu *child_origin_cu;
12752
12753 /* We are trying to process concrete instance entries:
216f72a1 12754 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12755 it's not relevant to our analysis here. i.e. detecting DIEs that are
12756 present in the abstract instance but not referenced in the concrete
12757 one. */
216f72a1
JK
12758 if (child_die->tag == DW_TAG_call_site
12759 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12760 continue;
12761
c38f313d
DJ
12762 /* For each CHILD_DIE, find the corresponding child of
12763 ORIGIN_DIE. If there is more than one layer of
12764 DW_AT_abstract_origin, follow them all; there shouldn't be,
12765 but GCC versions at least through 4.4 generate this (GCC PR
12766 40573). */
3ea89b92
PMR
12767 child_origin_die = child_die;
12768 child_origin_cu = cu;
c38f313d
DJ
12769 while (1)
12770 {
cd02d79d
PA
12771 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12772 child_origin_cu);
c38f313d
DJ
12773 if (attr == NULL)
12774 break;
cd02d79d
PA
12775 child_origin_die = follow_die_ref (child_origin_die, attr,
12776 &child_origin_cu);
c38f313d
DJ
12777 }
12778
d389af10
JK
12779 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12780 counterpart may exist. */
c38f313d 12781 if (child_origin_die != child_die)
d389af10 12782 {
edb3359d
DJ
12783 if (child_die->tag != child_origin_die->tag
12784 && !(child_die->tag == DW_TAG_inlined_subroutine
12785 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12786 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12787 "different tags"),
9d8780f0
SM
12788 sect_offset_str (child_die->sect_off),
12789 sect_offset_str (child_origin_die->sect_off));
c38f313d 12790 if (child_origin_die->parent != origin_die)
b98664d3 12791 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12792 "different parents"),
9d8780f0
SM
12793 sect_offset_str (child_die->sect_off),
12794 sect_offset_str (child_origin_die->sect_off));
c38f313d 12795 else
791afaa2 12796 offsets.push_back (child_origin_die->sect_off);
d389af10 12797 }
d389af10 12798 }
791afaa2
TT
12799 std::sort (offsets.begin (), offsets.end ());
12800 sect_offset *offsets_end = offsets.data () + offsets.size ();
12801 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12802 if (offsetp[-1] == *offsetp)
b98664d3 12803 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12804 "to DIE %s as their abstract origin"),
12805 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12806
791afaa2 12807 offsetp = offsets.data ();
d389af10
JK
12808 origin_child_die = origin_die->child;
12809 while (origin_child_die && origin_child_die->tag)
12810 {
12811 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12812 while (offsetp < offsets_end
9c541725 12813 && *offsetp < origin_child_die->sect_off)
d389af10 12814 offsetp++;
b64f50a1 12815 if (offsetp >= offsets_end
9c541725 12816 || *offsetp > origin_child_die->sect_off)
d389af10 12817 {
adde2bff
DE
12818 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12819 Check whether we're already processing ORIGIN_CHILD_DIE.
12820 This can happen with mutually referenced abstract_origins.
12821 PR 16581. */
12822 if (!origin_child_die->in_process)
12823 process_die (origin_child_die, origin_cu);
d389af10
JK
12824 }
12825 origin_child_die = sibling_die (origin_child_die);
12826 }
cd02d79d 12827 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12828
12829 if (cu != origin_cu)
12830 compute_delayed_physnames (origin_cu);
d389af10
JK
12831}
12832
c906108c 12833static void
e7c27a73 12834read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12835{
518817b3 12836 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12837 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12838 struct context_stack *newobj;
c906108c
SS
12839 CORE_ADDR lowpc;
12840 CORE_ADDR highpc;
12841 struct die_info *child_die;
edb3359d 12842 struct attribute *attr, *call_line, *call_file;
15d034d0 12843 const char *name;
e142c38c 12844 CORE_ADDR baseaddr;
801e3a5b 12845 struct block *block;
edb3359d 12846 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12847 std::vector<struct symbol *> template_args;
34eaf542 12848 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12849
12850 if (inlined_func)
12851 {
12852 /* If we do not have call site information, we can't show the
12853 caller of this inlined function. That's too confusing, so
12854 only use the scope for local variables. */
12855 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12856 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12857 if (call_line == NULL || call_file == NULL)
12858 {
12859 read_lexical_block_scope (die, cu);
12860 return;
12861 }
12862 }
c906108c 12863
b3b3bada 12864 baseaddr = objfile->text_section_offset ();
e142c38c 12865
94af9270 12866 name = dwarf2_name (die, cu);
c906108c 12867
e8d05480
JB
12868 /* Ignore functions with missing or empty names. These are actually
12869 illegal according to the DWARF standard. */
12870 if (name == NULL)
12871 {
b98664d3 12872 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12873 sect_offset_str (die->sect_off));
e8d05480
JB
12874 return;
12875 }
12876
12877 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12878 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12879 <= PC_BOUNDS_INVALID)
e8d05480 12880 {
ae4d0c03
PM
12881 attr = dwarf2_attr (die, DW_AT_external, cu);
12882 if (!attr || !DW_UNSND (attr))
b98664d3 12883 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12884 "for subprogram DIE at %s"),
12885 sect_offset_str (die->sect_off));
e8d05480
JB
12886 return;
12887 }
c906108c 12888
3e29f34a
MR
12889 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12890 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12891
34eaf542
TT
12892 /* If we have any template arguments, then we must allocate a
12893 different sort of symbol. */
12894 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12895 {
12896 if (child_die->tag == DW_TAG_template_type_param
12897 || child_die->tag == DW_TAG_template_value_param)
12898 {
e623cf5d 12899 templ_func = allocate_template_symbol (objfile);
cf724bc9 12900 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12901 break;
12902 }
12903 }
12904
c24bdb02 12905 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12906 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12907 (struct symbol *) templ_func);
4c2df51b 12908
81873cc8 12909 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12910 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12911 cu->language);
12912
4cecd739
DJ
12913 /* If there is a location expression for DW_AT_frame_base, record
12914 it. */
e142c38c 12915 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12916 if (attr != nullptr)
fe978cb0 12917 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12918
63e43d3a
PMR
12919 /* If there is a location for the static link, record it. */
12920 newobj->static_link = NULL;
12921 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12922 if (attr != nullptr)
63e43d3a 12923 {
224c3ddb
SM
12924 newobj->static_link
12925 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12926 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12927 cu->per_cu->addr_type ());
63e43d3a
PMR
12928 }
12929
c24bdb02 12930 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12931
639d11d3 12932 if (die->child != NULL)
c906108c 12933 {
639d11d3 12934 child_die = die->child;
c906108c
SS
12935 while (child_die && child_die->tag)
12936 {
34eaf542
TT
12937 if (child_die->tag == DW_TAG_template_type_param
12938 || child_die->tag == DW_TAG_template_value_param)
12939 {
12940 struct symbol *arg = new_symbol (child_die, NULL, cu);
12941
f1078f66 12942 if (arg != NULL)
2f4732b0 12943 template_args.push_back (arg);
34eaf542
TT
12944 }
12945 else
12946 process_die (child_die, cu);
c906108c
SS
12947 child_die = sibling_die (child_die);
12948 }
12949 }
12950
d389af10
JK
12951 inherit_abstract_dies (die, cu);
12952
4a811a97
UW
12953 /* If we have a DW_AT_specification, we might need to import using
12954 directives from the context of the specification DIE. See the
12955 comment in determine_prefix. */
12956 if (cu->language == language_cplus
12957 && dwarf2_attr (die, DW_AT_specification, cu))
12958 {
12959 struct dwarf2_cu *spec_cu = cu;
12960 struct die_info *spec_die = die_specification (die, &spec_cu);
12961
12962 while (spec_die)
12963 {
12964 child_die = spec_die->child;
12965 while (child_die && child_die->tag)
12966 {
12967 if (child_die->tag == DW_TAG_imported_module)
12968 process_die (child_die, spec_cu);
12969 child_die = sibling_die (child_die);
12970 }
12971
12972 /* In some cases, GCC generates specification DIEs that
12973 themselves contain DW_AT_specification attributes. */
12974 spec_die = die_specification (spec_die, &spec_cu);
12975 }
12976 }
12977
c24bdb02 12978 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12979 /* Make a block for the local symbols within. */
c24bdb02 12980 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12981 cstk.static_link, lowpc, highpc);
801e3a5b 12982
df8a16a1 12983 /* For C++, set the block's scope. */
45280282
IB
12984 if ((cu->language == language_cplus
12985 || cu->language == language_fortran
c44af4eb
TT
12986 || cu->language == language_d
12987 || cu->language == language_rust)
4d4ec4e5 12988 && cu->processing_has_namespace_info)
195a3f6c
TT
12989 block_set_scope (block, determine_prefix (die, cu),
12990 &objfile->objfile_obstack);
df8a16a1 12991
801e3a5b
JB
12992 /* If we have address ranges, record them. */
12993 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12994
a60f3166 12995 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12996
34eaf542 12997 /* Attach template arguments to function. */
2f4732b0 12998 if (!template_args.empty ())
34eaf542
TT
12999 {
13000 gdb_assert (templ_func != NULL);
13001
2f4732b0 13002 templ_func->n_template_arguments = template_args.size ();
34eaf542 13003 templ_func->template_arguments
8d749320
SM
13004 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13005 templ_func->n_template_arguments);
34eaf542 13006 memcpy (templ_func->template_arguments,
2f4732b0 13007 template_args.data (),
34eaf542 13008 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13009
13010 /* Make sure that the symtab is set on the new symbols. Even
13011 though they don't appear in this symtab directly, other parts
13012 of gdb assume that symbols do, and this is reasonably
13013 true. */
8634679f 13014 for (symbol *sym : template_args)
3e1d3d8c 13015 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13016 }
13017
208d8187
JB
13018 /* In C++, we can have functions nested inside functions (e.g., when
13019 a function declares a class that has methods). This means that
13020 when we finish processing a function scope, we may need to go
13021 back to building a containing block's symbol lists. */
c24bdb02
KS
13022 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13023 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13024
921e78cf
JB
13025 /* If we've finished processing a top-level function, subsequent
13026 symbols go in the file symbol list. */
c24bdb02
KS
13027 if (cu->get_builder ()->outermost_context_p ())
13028 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13029}
13030
13031/* Process all the DIES contained within a lexical block scope. Start
13032 a new scope, process the dies, and then close the scope. */
13033
13034static void
e7c27a73 13035read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13036{
518817b3 13037 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13038 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13039 CORE_ADDR lowpc, highpc;
13040 struct die_info *child_die;
e142c38c
DJ
13041 CORE_ADDR baseaddr;
13042
b3b3bada 13043 baseaddr = objfile->text_section_offset ();
c906108c
SS
13044
13045 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13046 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13047 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13048 be nasty. Might be easier to properly extend generic blocks to
af34e669 13049 describe ranges. */
e385593e
JK
13050 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13051 {
13052 case PC_BOUNDS_NOT_PRESENT:
13053 /* DW_TAG_lexical_block has no attributes, process its children as if
13054 there was no wrapping by that DW_TAG_lexical_block.
13055 GCC does no longer produces such DWARF since GCC r224161. */
13056 for (child_die = die->child;
13057 child_die != NULL && child_die->tag;
13058 child_die = sibling_die (child_die))
13059 process_die (child_die, cu);
13060 return;
13061 case PC_BOUNDS_INVALID:
13062 return;
13063 }
3e29f34a
MR
13064 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13065 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13066
c24bdb02 13067 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13068 if (die->child != NULL)
c906108c 13069 {
639d11d3 13070 child_die = die->child;
c906108c
SS
13071 while (child_die && child_die->tag)
13072 {
e7c27a73 13073 process_die (child_die, cu);
c906108c
SS
13074 child_die = sibling_die (child_die);
13075 }
13076 }
3ea89b92 13077 inherit_abstract_dies (die, cu);
c24bdb02 13078 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13079
c24bdb02
KS
13080 if (*cu->get_builder ()->get_local_symbols () != NULL
13081 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13082 {
801e3a5b 13083 struct block *block
c24bdb02 13084 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13085 cstk.start_addr, highpc);
801e3a5b
JB
13086
13087 /* Note that recording ranges after traversing children, as we
13088 do here, means that recording a parent's ranges entails
13089 walking across all its children's ranges as they appear in
13090 the address map, which is quadratic behavior.
13091
13092 It would be nicer to record the parent's ranges before
13093 traversing its children, simply overriding whatever you find
13094 there. But since we don't even decide whether to create a
13095 block until after we've traversed its children, that's hard
13096 to do. */
13097 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13098 }
c24bdb02
KS
13099 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13100 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13101}
13102
216f72a1 13103/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13104
13105static void
13106read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13107{
518817b3 13108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13109 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13110 CORE_ADDR pc, baseaddr;
13111 struct attribute *attr;
13112 struct call_site *call_site, call_site_local;
13113 void **slot;
13114 int nparams;
13115 struct die_info *child_die;
13116
b3b3bada 13117 baseaddr = objfile->text_section_offset ();
96408a79 13118
216f72a1
JK
13119 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13120 if (attr == NULL)
13121 {
13122 /* This was a pre-DWARF-5 GNU extension alias
13123 for DW_AT_call_return_pc. */
13124 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13125 }
96408a79
SA
13126 if (!attr)
13127 {
b98664d3 13128 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13129 "DIE %s [in module %s]"),
13130 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13131 return;
13132 }
cd6c91b4 13133 pc = attr->value_as_address () + baseaddr;
3e29f34a 13134 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13135
13136 if (cu->call_site_htab == NULL)
13137 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13138 NULL, &objfile->objfile_obstack,
13139 hashtab_obstack_allocate, NULL);
13140 call_site_local.pc = pc;
13141 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13142 if (*slot != NULL)
13143 {
b98664d3 13144 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13145 "DIE %s [in module %s]"),
13146 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13147 objfile_name (objfile));
96408a79
SA
13148 return;
13149 }
13150
13151 /* Count parameters at the caller. */
13152
13153 nparams = 0;
13154 for (child_die = die->child; child_die && child_die->tag;
13155 child_die = sibling_die (child_die))
13156 {
216f72a1
JK
13157 if (child_die->tag != DW_TAG_call_site_parameter
13158 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13159 {
b98664d3 13160 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13161 "DW_TAG_call_site child DIE %s [in module %s]"),
13162 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13163 objfile_name (objfile));
96408a79
SA
13164 continue;
13165 }
13166
13167 nparams++;
13168 }
13169
224c3ddb
SM
13170 call_site
13171 = ((struct call_site *)
13172 obstack_alloc (&objfile->objfile_obstack,
13173 sizeof (*call_site)
13174 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13175 *slot = call_site;
13176 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13177 call_site->pc = pc;
13178
216f72a1
JK
13179 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13180 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13181 {
13182 struct die_info *func_die;
13183
13184 /* Skip also over DW_TAG_inlined_subroutine. */
13185 for (func_die = die->parent;
13186 func_die && func_die->tag != DW_TAG_subprogram
13187 && func_die->tag != DW_TAG_subroutine_type;
13188 func_die = func_die->parent);
13189
216f72a1
JK
13190 /* DW_AT_call_all_calls is a superset
13191 of DW_AT_call_all_tail_calls. */
96408a79 13192 if (func_die
216f72a1 13193 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13194 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13195 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13196 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13197 {
13198 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13199 not complete. But keep CALL_SITE for look ups via call_site_htab,
13200 both the initial caller containing the real return address PC and
13201 the final callee containing the current PC of a chain of tail
13202 calls do not need to have the tail call list complete. But any
13203 function candidate for a virtual tail call frame searched via
13204 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13205 determined unambiguously. */
13206 }
13207 else
13208 {
13209 struct type *func_type = NULL;
13210
13211 if (func_die)
13212 func_type = get_die_type (func_die, cu);
13213 if (func_type != NULL)
13214 {
13215 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13216
13217 /* Enlist this call site to the function. */
13218 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13219 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13220 }
13221 else
b98664d3 13222 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13223 "DIE %s [in module %s]"),
13224 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13225 }
13226 }
13227
216f72a1
JK
13228 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13229 if (attr == NULL)
13230 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13231 if (attr == NULL)
13232 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13233 if (attr == NULL)
216f72a1
JK
13234 {
13235 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13236 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13237 }
96408a79 13238 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13239 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13240 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13241 else if (attr->form_is_block ())
96408a79
SA
13242 {
13243 struct dwarf2_locexpr_baton *dlbaton;
13244
8d749320 13245 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13246 dlbaton->data = DW_BLOCK (attr)->data;
13247 dlbaton->size = DW_BLOCK (attr)->size;
13248 dlbaton->per_cu = cu->per_cu;
13249
13250 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13251 }
cd6c91b4 13252 else if (attr->form_is_ref ())
96408a79 13253 {
96408a79
SA
13254 struct dwarf2_cu *target_cu = cu;
13255 struct die_info *target_die;
13256
ac9ec31b 13257 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13258 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13259 if (die_is_declaration (target_die, target_cu))
13260 {
7d45c7c3 13261 const char *target_physname;
9112db09
JK
13262
13263 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13264 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13265 if (target_physname == NULL)
9112db09 13266 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13267 if (target_physname == NULL)
b98664d3 13268 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13269 "physname, for referencing DIE %s [in module %s]"),
13270 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13271 else
7d455152 13272 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13273 }
13274 else
13275 {
13276 CORE_ADDR lowpc;
13277
13278 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13279 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13280 <= PC_BOUNDS_INVALID)
b98664d3 13281 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13282 "low pc, for referencing DIE %s [in module %s]"),
13283 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13284 else
3e29f34a
MR
13285 {
13286 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13287 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13288 }
96408a79
SA
13289 }
13290 }
13291 else
b98664d3 13292 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13293 "block nor reference, for DIE %s [in module %s]"),
13294 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13295
13296 call_site->per_cu = cu->per_cu;
13297
13298 for (child_die = die->child;
13299 child_die && child_die->tag;
13300 child_die = sibling_die (child_die))
13301 {
96408a79 13302 struct call_site_parameter *parameter;
1788b2d3 13303 struct attribute *loc, *origin;
96408a79 13304
216f72a1
JK
13305 if (child_die->tag != DW_TAG_call_site_parameter
13306 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13307 {
13308 /* Already printed the complaint above. */
13309 continue;
13310 }
13311
13312 gdb_assert (call_site->parameter_count < nparams);
13313 parameter = &call_site->parameter[call_site->parameter_count];
13314
1788b2d3
JK
13315 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13316 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13317 register is contained in DW_AT_call_value. */
96408a79 13318
24c5c679 13319 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13320 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13321 if (origin == NULL)
13322 {
13323 /* This was a pre-DWARF-5 GNU extension alias
13324 for DW_AT_call_parameter. */
13325 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13326 }
cd6c91b4 13327 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13328 {
1788b2d3 13329 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13330
13331 sect_offset sect_off
13332 = (sect_offset) dwarf2_get_ref_die_offset (origin);
4057dfde 13333 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13334 {
13335 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13336 binding can be done only inside one CU. Such referenced DIE
13337 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13338 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13339 "DW_TAG_call_site child DIE %s [in module %s]"),
13340 sect_offset_str (child_die->sect_off),
9c541725 13341 objfile_name (objfile));
d76b7dbc
JK
13342 continue;
13343 }
9c541725
PA
13344 parameter->u.param_cu_off
13345 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13346 }
4fc6c0d5 13347 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13348 {
b98664d3 13349 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13350 "DW_TAG_call_site child DIE %s [in module %s]"),
13351 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13352 continue;
13353 }
24c5c679 13354 else
96408a79 13355 {
24c5c679
JK
13356 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13357 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13358 if (parameter->u.dwarf_reg != -1)
13359 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13360 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13361 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13362 &parameter->u.fb_offset))
13363 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13364 else
13365 {
b98664d3 13366 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13367 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13368 "DW_TAG_call_site child DIE %s "
24c5c679 13369 "[in module %s]"),
9d8780f0 13370 sect_offset_str (child_die->sect_off),
9c541725 13371 objfile_name (objfile));
24c5c679
JK
13372 continue;
13373 }
96408a79
SA
13374 }
13375
216f72a1
JK
13376 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13377 if (attr == NULL)
13378 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13379 if (attr == NULL || !attr->form_is_block ())
96408a79 13380 {
b98664d3 13381 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13382 "DW_TAG_call_site child DIE %s [in module %s]"),
13383 sect_offset_str (child_die->sect_off),
9c541725 13384 objfile_name (objfile));
96408a79
SA
13385 continue;
13386 }
13387 parameter->value = DW_BLOCK (attr)->data;
13388 parameter->value_size = DW_BLOCK (attr)->size;
13389
13390 /* Parameters are not pre-cleared by memset above. */
13391 parameter->data_value = NULL;
13392 parameter->data_value_size = 0;
13393 call_site->parameter_count++;
13394
216f72a1
JK
13395 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13396 if (attr == NULL)
13397 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13398 if (attr != nullptr)
96408a79 13399 {
4fc6c0d5 13400 if (!attr->form_is_block ())
b98664d3 13401 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13402 "DW_TAG_call_site child DIE %s [in module %s]"),
13403 sect_offset_str (child_die->sect_off),
9c541725 13404 objfile_name (objfile));
96408a79
SA
13405 else
13406 {
13407 parameter->data_value = DW_BLOCK (attr)->data;
13408 parameter->data_value_size = DW_BLOCK (attr)->size;
13409 }
13410 }
13411 }
13412}
13413
71a3c369
TT
13414/* Helper function for read_variable. If DIE represents a virtual
13415 table, then return the type of the concrete object that is
13416 associated with the virtual table. Otherwise, return NULL. */
13417
13418static struct type *
13419rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13420{
13421 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13422 if (attr == NULL)
13423 return NULL;
13424
13425 /* Find the type DIE. */
13426 struct die_info *type_die = NULL;
13427 struct dwarf2_cu *type_cu = cu;
13428
cd6c91b4 13429 if (attr->form_is_ref ())
71a3c369
TT
13430 type_die = follow_die_ref (die, attr, &type_cu);
13431 if (type_die == NULL)
13432 return NULL;
13433
13434 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13435 return NULL;
13436 return die_containing_type (type_die, type_cu);
13437}
13438
13439/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13440
13441static void
13442read_variable (struct die_info *die, struct dwarf2_cu *cu)
13443{
13444 struct rust_vtable_symbol *storage = NULL;
13445
13446 if (cu->language == language_rust)
13447 {
13448 struct type *containing_type = rust_containing_type (die, cu);
13449
13450 if (containing_type != NULL)
13451 {
518817b3 13452 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13453
468c0cbb 13454 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13455 initialize_objfile_symbol (storage);
13456 storage->concrete_type = containing_type;
cf724bc9 13457 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13458 }
13459 }
13460
e4a62c65
TV
13461 struct symbol *res = new_symbol (die, NULL, cu, storage);
13462 struct attribute *abstract_origin
13463 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13464 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13465 if (res == NULL && loc && abstract_origin)
13466 {
13467 /* We have a variable without a name, but with a location and an abstract
13468 origin. This may be a concrete instance of an abstract variable
13469 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13470 later. */
13471 struct dwarf2_cu *origin_cu = cu;
13472 struct die_info *origin_die
13473 = follow_die_ref (die, abstract_origin, &origin_cu);
13474 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13475 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13476 }
71a3c369
TT
13477}
13478
43988095
JK
13479/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13480 reading .debug_rnglists.
13481 Callback's type should be:
13482 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13483 Return true if the attributes are present and valid, otherwise,
13484 return false. */
13485
13486template <typename Callback>
13487static bool
13488dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13489 Callback &&callback)
13490{
ed2dc618 13491 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13492 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13493 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13494 bfd *obfd = objfile->obfd;
43988095
JK
13495 /* Base address selection entry. */
13496 CORE_ADDR base;
13497 int found_base;
43988095 13498 const gdb_byte *buffer;
43988095
JK
13499 CORE_ADDR baseaddr;
13500 bool overflow = false;
13501
13502 found_base = cu->base_known;
13503 base = cu->base_address;
13504
96b79293 13505 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13506 if (offset >= dwarf2_per_objfile->rnglists.size)
13507 {
b98664d3 13508 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13509 offset);
13510 return false;
13511 }
13512 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13513
b3b3bada 13514 baseaddr = objfile->text_section_offset ();
43988095
JK
13515
13516 while (1)
13517 {
7814882a
JK
13518 /* Initialize it due to a false compiler warning. */
13519 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13520 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13521 + dwarf2_per_objfile->rnglists.size);
13522 unsigned int bytes_read;
13523
13524 if (buffer == buf_end)
13525 {
13526 overflow = true;
13527 break;
13528 }
13529 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13530 switch (rlet)
13531 {
13532 case DW_RLE_end_of_list:
13533 break;
13534 case DW_RLE_base_address:
13535 if (buffer + cu->header.addr_size > buf_end)
13536 {
13537 overflow = true;
13538 break;
13539 }
c8a7a66f 13540 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13541 found_base = 1;
13542 buffer += bytes_read;
13543 break;
13544 case DW_RLE_start_length:
13545 if (buffer + cu->header.addr_size > buf_end)
13546 {
13547 overflow = true;
13548 break;
13549 }
c8a7a66f
TT
13550 range_beginning = cu->header.read_address (obfd, buffer,
13551 &bytes_read);
43988095
JK
13552 buffer += bytes_read;
13553 range_end = (range_beginning
13554 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13555 buffer += bytes_read;
13556 if (buffer > buf_end)
13557 {
13558 overflow = true;
13559 break;
13560 }
13561 break;
13562 case DW_RLE_offset_pair:
13563 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13564 buffer += bytes_read;
13565 if (buffer > buf_end)
13566 {
13567 overflow = true;
13568 break;
13569 }
13570 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13571 buffer += bytes_read;
13572 if (buffer > buf_end)
13573 {
13574 overflow = true;
13575 break;
13576 }
13577 break;
13578 case DW_RLE_start_end:
13579 if (buffer + 2 * cu->header.addr_size > buf_end)
13580 {
13581 overflow = true;
13582 break;
13583 }
c8a7a66f
TT
13584 range_beginning = cu->header.read_address (obfd, buffer,
13585 &bytes_read);
43988095 13586 buffer += bytes_read;
c8a7a66f 13587 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13588 buffer += bytes_read;
13589 break;
13590 default:
b98664d3 13591 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13592 return false;
13593 }
13594 if (rlet == DW_RLE_end_of_list || overflow)
13595 break;
13596 if (rlet == DW_RLE_base_address)
13597 continue;
13598
13599 if (!found_base)
13600 {
13601 /* We have no valid base address for the ranges
13602 data. */
b98664d3 13603 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13604 return false;
13605 }
13606
13607 if (range_beginning > range_end)
13608 {
13609 /* Inverted range entries are invalid. */
b98664d3 13610 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13611 return false;
13612 }
13613
13614 /* Empty range entries have no effect. */
13615 if (range_beginning == range_end)
13616 continue;
13617
13618 range_beginning += base;
13619 range_end += base;
13620
13621 /* A not-uncommon case of bad debug info.
13622 Don't pollute the addrmap with bad data. */
13623 if (range_beginning + baseaddr == 0
13624 && !dwarf2_per_objfile->has_section_at_zero)
13625 {
b98664d3 13626 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13627 " [in module %s]"), objfile_name (objfile));
13628 continue;
13629 }
13630
13631 callback (range_beginning, range_end);
13632 }
13633
13634 if (overflow)
13635 {
b98664d3 13636 complaint (_("Offset %d is not terminated "
43988095
JK
13637 "for DW_AT_ranges attribute"),
13638 offset);
13639 return false;
13640 }
13641
13642 return true;
13643}
13644
13645/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13646 Callback's type should be:
13647 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13648 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13649
43988095 13650template <typename Callback>
43039443 13651static int
5f46c5a5 13652dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13653 Callback &&callback)
43039443 13654{
ed2dc618 13655 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13656 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13657 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13658 struct comp_unit_head *cu_header = &cu->header;
13659 bfd *obfd = objfile->obfd;
13660 unsigned int addr_size = cu_header->addr_size;
13661 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13662 /* Base address selection entry. */
13663 CORE_ADDR base;
13664 int found_base;
13665 unsigned int dummy;
d521ce57 13666 const gdb_byte *buffer;
ff013f42 13667 CORE_ADDR baseaddr;
43039443 13668
43988095
JK
13669 if (cu_header->version >= 5)
13670 return dwarf2_rnglists_process (offset, cu, callback);
13671
d00adf39
DE
13672 found_base = cu->base_known;
13673 base = cu->base_address;
43039443 13674
96b79293 13675 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13676 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13677 {
b98664d3 13678 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13679 offset);
13680 return 0;
13681 }
dce234bc 13682 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13683
b3b3bada 13684 baseaddr = objfile->text_section_offset ();
ff013f42 13685
43039443
JK
13686 while (1)
13687 {
13688 CORE_ADDR range_beginning, range_end;
13689
c8a7a66f 13690 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13691 buffer += addr_size;
c8a7a66f 13692 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13693 buffer += addr_size;
13694 offset += 2 * addr_size;
13695
13696 /* An end of list marker is a pair of zero addresses. */
13697 if (range_beginning == 0 && range_end == 0)
13698 /* Found the end of list entry. */
13699 break;
13700
13701 /* Each base address selection entry is a pair of 2 values.
13702 The first is the largest possible address, the second is
13703 the base address. Check for a base address here. */
13704 if ((range_beginning & mask) == mask)
13705 {
28d2bfb9
AB
13706 /* If we found the largest possible address, then we already
13707 have the base address in range_end. */
13708 base = range_end;
43039443
JK
13709 found_base = 1;
13710 continue;
13711 }
13712
13713 if (!found_base)
13714 {
13715 /* We have no valid base address for the ranges
13716 data. */
b98664d3 13717 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13718 return 0;
13719 }
13720
9277c30c
UW
13721 if (range_beginning > range_end)
13722 {
13723 /* Inverted range entries are invalid. */
b98664d3 13724 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13725 return 0;
13726 }
13727
13728 /* Empty range entries have no effect. */
13729 if (range_beginning == range_end)
13730 continue;
13731
43039443
JK
13732 range_beginning += base;
13733 range_end += base;
13734
01093045
DE
13735 /* A not-uncommon case of bad debug info.
13736 Don't pollute the addrmap with bad data. */
13737 if (range_beginning + baseaddr == 0
13738 && !dwarf2_per_objfile->has_section_at_zero)
13739 {
b98664d3 13740 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13741 " [in module %s]"), objfile_name (objfile));
01093045
DE
13742 continue;
13743 }
13744
5f46c5a5
JK
13745 callback (range_beginning, range_end);
13746 }
13747
13748 return 1;
13749}
13750
13751/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13752 Return 1 if the attributes are present and valid, otherwise, return 0.
13753 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13754
13755static int
13756dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13757 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13758 dwarf2_psymtab *ranges_pst)
5f46c5a5 13759{
518817b3 13760 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13761 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13762 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13763 int low_set = 0;
13764 CORE_ADDR low = 0;
13765 CORE_ADDR high = 0;
13766 int retval;
13767
13768 retval = dwarf2_ranges_process (offset, cu,
13769 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13770 {
9277c30c 13771 if (ranges_pst != NULL)
3e29f34a
MR
13772 {
13773 CORE_ADDR lowpc;
13774 CORE_ADDR highpc;
13775
79748972
TT
13776 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13777 range_beginning + baseaddr)
13778 - baseaddr);
13779 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13780 range_end + baseaddr)
13781 - baseaddr);
d320c2b5
TT
13782 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13783 lowpc, highpc - 1, ranges_pst);
3e29f34a 13784 }
ff013f42 13785
43039443
JK
13786 /* FIXME: This is recording everything as a low-high
13787 segment of consecutive addresses. We should have a
13788 data structure for discontiguous block ranges
13789 instead. */
13790 if (! low_set)
13791 {
13792 low = range_beginning;
13793 high = range_end;
13794 low_set = 1;
13795 }
13796 else
13797 {
13798 if (range_beginning < low)
13799 low = range_beginning;
13800 if (range_end > high)
13801 high = range_end;
13802 }
5f46c5a5
JK
13803 });
13804 if (!retval)
13805 return 0;
43039443
JK
13806
13807 if (! low_set)
13808 /* If the first entry is an end-of-list marker, the range
13809 describes an empty scope, i.e. no instructions. */
13810 return 0;
13811
13812 if (low_return)
13813 *low_return = low;
13814 if (high_return)
13815 *high_return = high;
13816 return 1;
13817}
13818
3a2b436a
JK
13819/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13820 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13821 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13822
3a2b436a 13823static enum pc_bounds_kind
af34e669 13824dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13825 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13826 dwarf2_psymtab *pst)
c906108c 13827{
518817b3
SM
13828 struct dwarf2_per_objfile *dwarf2_per_objfile
13829 = cu->per_cu->dwarf2_per_objfile;
c906108c 13830 struct attribute *attr;
91da1414 13831 struct attribute *attr_high;
af34e669
DJ
13832 CORE_ADDR low = 0;
13833 CORE_ADDR high = 0;
e385593e 13834 enum pc_bounds_kind ret;
c906108c 13835
91da1414
MW
13836 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13837 if (attr_high)
af34e669 13838 {
e142c38c 13839 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13840 if (attr != nullptr)
91da1414 13841 {
cd6c91b4
TT
13842 low = attr->value_as_address ();
13843 high = attr_high->value_as_address ();
13844 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13845 high += low;
91da1414 13846 }
af34e669
DJ
13847 else
13848 /* Found high w/o low attribute. */
e385593e 13849 return PC_BOUNDS_INVALID;
af34e669
DJ
13850
13851 /* Found consecutive range of addresses. */
3a2b436a 13852 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13853 }
c906108c 13854 else
af34e669 13855 {
e142c38c 13856 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13857 if (attr != NULL)
13858 {
18a8505e 13859 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13860 We take advantage of the fact that DW_AT_ranges does not appear
13861 in DW_TAG_compile_unit of DWO files. */
13862 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13863 unsigned int ranges_offset = (DW_UNSND (attr)
13864 + (need_ranges_base
13865 ? cu->ranges_base
13866 : 0));
2e3cf129 13867
af34e669 13868 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13869 .debug_ranges section. */
2e3cf129 13870 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13871 return PC_BOUNDS_INVALID;
43039443 13872 /* Found discontinuous range of addresses. */
3a2b436a 13873 ret = PC_BOUNDS_RANGES;
af34e669 13874 }
e385593e
JK
13875 else
13876 return PC_BOUNDS_NOT_PRESENT;
af34e669 13877 }
c906108c 13878
48fbe735 13879 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13880 if (high <= low)
e385593e 13881 return PC_BOUNDS_INVALID;
c906108c
SS
13882
13883 /* When using the GNU linker, .gnu.linkonce. sections are used to
13884 eliminate duplicate copies of functions and vtables and such.
13885 The linker will arbitrarily choose one and discard the others.
13886 The AT_*_pc values for such functions refer to local labels in
13887 these sections. If the section from that file was discarded, the
13888 labels are not in the output, so the relocs get a value of 0.
13889 If this is a discarded function, mark the pc bounds as invalid,
13890 so that GDB will ignore it. */
72dca2f5 13891 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13892 return PC_BOUNDS_INVALID;
c906108c
SS
13893
13894 *lowpc = low;
96408a79
SA
13895 if (highpc)
13896 *highpc = high;
af34e669 13897 return ret;
c906108c
SS
13898}
13899
b084d499
JB
13900/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13901 its low and high PC addresses. Do nothing if these addresses could not
13902 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13903 and HIGHPC to the high address if greater than HIGHPC. */
13904
13905static void
13906dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13907 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13908 struct dwarf2_cu *cu)
13909{
13910 CORE_ADDR low, high;
13911 struct die_info *child = die->child;
13912
e385593e 13913 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13914 {
325fac50
PA
13915 *lowpc = std::min (*lowpc, low);
13916 *highpc = std::max (*highpc, high);
b084d499
JB
13917 }
13918
13919 /* If the language does not allow nested subprograms (either inside
13920 subprograms or lexical blocks), we're done. */
13921 if (cu->language != language_ada)
13922 return;
6e70227d 13923
b084d499
JB
13924 /* Check all the children of the given DIE. If it contains nested
13925 subprograms, then check their pc bounds. Likewise, we need to
13926 check lexical blocks as well, as they may also contain subprogram
13927 definitions. */
13928 while (child && child->tag)
13929 {
13930 if (child->tag == DW_TAG_subprogram
13931 || child->tag == DW_TAG_lexical_block)
13932 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13933 child = sibling_die (child);
13934 }
13935}
13936
fae299cd
DC
13937/* Get the low and high pc's represented by the scope DIE, and store
13938 them in *LOWPC and *HIGHPC. If the correct values can't be
13939 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13940
13941static void
13942get_scope_pc_bounds (struct die_info *die,
13943 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13944 struct dwarf2_cu *cu)
13945{
13946 CORE_ADDR best_low = (CORE_ADDR) -1;
13947 CORE_ADDR best_high = (CORE_ADDR) 0;
13948 CORE_ADDR current_low, current_high;
13949
3a2b436a 13950 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13951 >= PC_BOUNDS_RANGES)
fae299cd
DC
13952 {
13953 best_low = current_low;
13954 best_high = current_high;
13955 }
13956 else
13957 {
13958 struct die_info *child = die->child;
13959
13960 while (child && child->tag)
13961 {
13962 switch (child->tag) {
13963 case DW_TAG_subprogram:
b084d499 13964 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13965 break;
13966 case DW_TAG_namespace:
f55ee35c 13967 case DW_TAG_module:
fae299cd
DC
13968 /* FIXME: carlton/2004-01-16: Should we do this for
13969 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13970 that current GCC's always emit the DIEs corresponding
13971 to definitions of methods of classes as children of a
13972 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13973 the DIEs giving the declarations, which could be
13974 anywhere). But I don't see any reason why the
13975 standards says that they have to be there. */
13976 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13977
13978 if (current_low != ((CORE_ADDR) -1))
13979 {
325fac50
PA
13980 best_low = std::min (best_low, current_low);
13981 best_high = std::max (best_high, current_high);
fae299cd
DC
13982 }
13983 break;
13984 default:
0963b4bd 13985 /* Ignore. */
fae299cd
DC
13986 break;
13987 }
13988
13989 child = sibling_die (child);
13990 }
13991 }
13992
13993 *lowpc = best_low;
13994 *highpc = best_high;
13995}
13996
801e3a5b
JB
13997/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13998 in DIE. */
380bca97 13999
801e3a5b
JB
14000static void
14001dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14002 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14003{
518817b3 14004 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14005 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14006 struct attribute *attr;
91da1414 14007 struct attribute *attr_high;
801e3a5b 14008
91da1414
MW
14009 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14010 if (attr_high)
801e3a5b 14011 {
801e3a5b 14012 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14013 if (attr != nullptr)
801e3a5b 14014 {
cd6c91b4
TT
14015 CORE_ADDR low = attr->value_as_address ();
14016 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14017
cd6c91b4 14018 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14019 high += low;
9a619af0 14020
3e29f34a
MR
14021 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14022 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14023 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14024 }
14025 }
14026
14027 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14028 if (attr != nullptr)
801e3a5b 14029 {
18a8505e 14030 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14031 We take advantage of the fact that DW_AT_ranges does not appear
14032 in DW_TAG_compile_unit of DWO files. */
14033 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14034
14035 /* The value of the DW_AT_ranges attribute is the offset of the
14036 address range list in the .debug_ranges section. */
ab435259
DE
14037 unsigned long offset = (DW_UNSND (attr)
14038 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14039
2d5f09ec 14040 std::vector<blockrange> blockvec;
5f46c5a5
JK
14041 dwarf2_ranges_process (offset, cu,
14042 [&] (CORE_ADDR start, CORE_ADDR end)
14043 {
58fdfd2c
JK
14044 start += baseaddr;
14045 end += baseaddr;
5f46c5a5
JK
14046 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14047 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14048 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14049 blockvec.emplace_back (start, end);
5f46c5a5 14050 });
2d5f09ec
KB
14051
14052 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14053 }
14054}
14055
685b1105
JK
14056/* Check whether the producer field indicates either of GCC < 4.6, or the
14057 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14058
685b1105
JK
14059static void
14060check_producer (struct dwarf2_cu *cu)
60d5a603 14061{
38360086 14062 int major, minor;
60d5a603
JK
14063
14064 if (cu->producer == NULL)
14065 {
14066 /* For unknown compilers expect their behavior is DWARF version
14067 compliant.
14068
14069 GCC started to support .debug_types sections by -gdwarf-4 since
14070 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14071 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14072 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14073 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14074 }
b1ffba5a 14075 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14076 {
38360086
MW
14077 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14078 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14079 }
5230b05a 14080 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14081 {
14082 cu->producer_is_icc = true;
14083 cu->producer_is_icc_lt_14 = major < 14;
14084 }
c258c396
JD
14085 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14086 cu->producer_is_codewarrior = true;
685b1105
JK
14087 else
14088 {
14089 /* For other non-GCC compilers, expect their behavior is DWARF version
14090 compliant. */
60d5a603
JK
14091 }
14092
9068261f 14093 cu->checked_producer = true;
685b1105 14094}
ba919b58 14095
685b1105
JK
14096/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14097 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14098 during 4.6.0 experimental. */
14099
9068261f 14100static bool
685b1105
JK
14101producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14102{
14103 if (!cu->checked_producer)
14104 check_producer (cu);
14105
14106 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14107}
14108
c258c396
JD
14109
14110/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14111 with incorrect is_stmt attributes. */
14112
14113static bool
14114producer_is_codewarrior (struct dwarf2_cu *cu)
14115{
14116 if (!cu->checked_producer)
14117 check_producer (cu);
14118
14119 return cu->producer_is_codewarrior;
14120}
14121
405feb71 14122/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14123 DW_AT_accessibility. */
14124
14125static enum dwarf_access_attribute
14126dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14127{
14128 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14129 {
14130 /* The default DWARF 2 accessibility for members is public, the default
14131 accessibility for inheritance is private. */
14132
14133 if (die->tag != DW_TAG_inheritance)
14134 return DW_ACCESS_public;
14135 else
14136 return DW_ACCESS_private;
14137 }
14138 else
14139 {
14140 /* DWARF 3+ defines the default accessibility a different way. The same
14141 rules apply now for DW_TAG_inheritance as for the members and it only
14142 depends on the container kind. */
14143
14144 if (die->parent->tag == DW_TAG_class_type)
14145 return DW_ACCESS_private;
14146 else
14147 return DW_ACCESS_public;
14148 }
14149}
14150
74ac6d43
TT
14151/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14152 offset. If the attribute was not found return 0, otherwise return
14153 1. If it was found but could not properly be handled, set *OFFSET
14154 to 0. */
14155
14156static int
14157handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14158 LONGEST *offset)
14159{
14160 struct attribute *attr;
14161
14162 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14163 if (attr != NULL)
14164 {
14165 *offset = 0;
14166
14167 /* Note that we do not check for a section offset first here.
14168 This is because DW_AT_data_member_location is new in DWARF 4,
14169 so if we see it, we can assume that a constant form is really
14170 a constant and not a section offset. */
cd6c91b4 14171 if (attr->form_is_constant ())
74ac6d43 14172 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14173 else if (attr->form_is_section_offset ())
74ac6d43 14174 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14175 else if (attr->form_is_block ())
74ac6d43
TT
14176 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14177 else
14178 dwarf2_complex_location_expr_complaint ();
14179
14180 return 1;
14181 }
14182
14183 return 0;
14184}
14185
c906108c
SS
14186/* Add an aggregate field to the field list. */
14187
14188static void
107d2387 14189dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14190 struct dwarf2_cu *cu)
6e70227d 14191{
518817b3 14192 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14193 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14194 struct nextfield *new_field;
14195 struct attribute *attr;
14196 struct field *fp;
15d034d0 14197 const char *fieldname = "";
c906108c 14198
7d0ccb61
DJ
14199 if (die->tag == DW_TAG_inheritance)
14200 {
be2daae6
TT
14201 fip->baseclasses.emplace_back ();
14202 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14203 }
14204 else
14205 {
be2daae6
TT
14206 fip->fields.emplace_back ();
14207 new_field = &fip->fields.back ();
7d0ccb61 14208 }
be2daae6 14209
c906108c
SS
14210 fip->nfields++;
14211
e142c38c 14212 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14213 if (attr != nullptr)
c906108c 14214 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14215 else
14216 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14217 if (new_field->accessibility != DW_ACCESS_public)
14218 fip->non_public_fields = 1;
60d5a603 14219
e142c38c 14220 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14221 if (attr != nullptr)
c906108c 14222 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14223 else
14224 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14225
14226 fp = &new_field->field;
a9a9bd0f 14227
e142c38c 14228 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14229 {
74ac6d43
TT
14230 LONGEST offset;
14231
a9a9bd0f 14232 /* Data member other than a C++ static data member. */
6e70227d 14233
c906108c 14234 /* Get type of field. */
e7c27a73 14235 fp->type = die_type (die, cu);
c906108c 14236
d6a843b5 14237 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14238
c906108c 14239 /* Get bit size of field (zero if none). */
e142c38c 14240 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14241 if (attr != nullptr)
c906108c
SS
14242 {
14243 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14244 }
14245 else
14246 {
14247 FIELD_BITSIZE (*fp) = 0;
14248 }
14249
14250 /* Get bit offset of field. */
74ac6d43
TT
14251 if (handle_data_member_location (die, cu, &offset))
14252 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14253 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14254 if (attr != nullptr)
c906108c 14255 {
d5a22e77 14256 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14257 {
14258 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14259 additional bit offset from the MSB of the containing
14260 anonymous object to the MSB of the field. We don't
14261 have to do anything special since we don't need to
14262 know the size of the anonymous object. */
f41f5e61 14263 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14264 }
14265 else
14266 {
14267 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14268 MSB of the anonymous object, subtract off the number of
14269 bits from the MSB of the field to the MSB of the
14270 object, and then subtract off the number of bits of
14271 the field itself. The result is the bit offset of
14272 the LSB of the field. */
c906108c
SS
14273 int anonymous_size;
14274 int bit_offset = DW_UNSND (attr);
14275
e142c38c 14276 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14277 if (attr != nullptr)
c906108c
SS
14278 {
14279 /* The size of the anonymous object containing
14280 the bit field is explicit, so use the
14281 indicated size (in bytes). */
14282 anonymous_size = DW_UNSND (attr);
14283 }
14284 else
14285 {
14286 /* The size of the anonymous object containing
14287 the bit field must be inferred from the type
14288 attribute of the data member containing the
14289 bit field. */
14290 anonymous_size = TYPE_LENGTH (fp->type);
14291 }
f41f5e61
PA
14292 SET_FIELD_BITPOS (*fp,
14293 (FIELD_BITPOS (*fp)
14294 + anonymous_size * bits_per_byte
14295 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14296 }
14297 }
da5b30da
AA
14298 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14299 if (attr != NULL)
14300 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14301 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14302
14303 /* Get name of field. */
39cbfefa
DJ
14304 fieldname = dwarf2_name (die, cu);
14305 if (fieldname == NULL)
14306 fieldname = "";
d8151005
DJ
14307
14308 /* The name is already allocated along with this objfile, so we don't
14309 need to duplicate it for the type. */
14310 fp->name = fieldname;
c906108c
SS
14311
14312 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14313 pointer or virtual base class pointer) to private. */
e142c38c 14314 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14315 {
d48cc9dd 14316 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14317 new_field->accessibility = DW_ACCESS_private;
14318 fip->non_public_fields = 1;
14319 }
14320 }
a9a9bd0f 14321 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14322 {
a9a9bd0f
DC
14323 /* C++ static member. */
14324
14325 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14326 is a declaration, but all versions of G++ as of this writing
14327 (so through at least 3.2.1) incorrectly generate
14328 DW_TAG_variable tags. */
6e70227d 14329
ff355380 14330 const char *physname;
c906108c 14331
a9a9bd0f 14332 /* Get name of field. */
39cbfefa
DJ
14333 fieldname = dwarf2_name (die, cu);
14334 if (fieldname == NULL)
c906108c
SS
14335 return;
14336
254e6b9e 14337 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14338 if (attr
14339 /* Only create a symbol if this is an external value.
14340 new_symbol checks this and puts the value in the global symbol
14341 table, which we want. If it is not external, new_symbol
14342 will try to put the value in cu->list_in_scope which is wrong. */
14343 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14344 {
14345 /* A static const member, not much different than an enum as far as
14346 we're concerned, except that we can support more types. */
14347 new_symbol (die, NULL, cu);
14348 }
14349
2df3850c 14350 /* Get physical name. */
ff355380 14351 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14352
d8151005
DJ
14353 /* The name is already allocated along with this objfile, so we don't
14354 need to duplicate it for the type. */
14355 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14356 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14357 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14358 }
14359 else if (die->tag == DW_TAG_inheritance)
14360 {
74ac6d43 14361 LONGEST offset;
d4b96c9a 14362
74ac6d43
TT
14363 /* C++ base class field. */
14364 if (handle_data_member_location (die, cu, &offset))
14365 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14366 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14367 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14368 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14369 }
2ddeaf8a
TT
14370 else if (die->tag == DW_TAG_variant_part)
14371 {
14372 /* process_structure_scope will treat this DIE as a union. */
14373 process_structure_scope (die, cu);
14374
14375 /* The variant part is relative to the start of the enclosing
14376 structure. */
14377 SET_FIELD_BITPOS (*fp, 0);
14378 fp->type = get_die_type (die, cu);
14379 fp->artificial = 1;
14380 fp->name = "<<variant>>";
c8c81635
TT
14381
14382 /* Normally a DW_TAG_variant_part won't have a size, but our
14383 representation requires one, so set it to the maximum of the
489dbda6
TT
14384 child sizes, being sure to account for the offset at which
14385 each child is seen. */
c8c81635
TT
14386 if (TYPE_LENGTH (fp->type) == 0)
14387 {
14388 unsigned max = 0;
14389 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14390 {
14391 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14392 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14393 if (len > max)
14394 max = len;
14395 }
c8c81635
TT
14396 TYPE_LENGTH (fp->type) = max;
14397 }
2ddeaf8a
TT
14398 }
14399 else
14400 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14401}
14402
883fd55a
KS
14403/* Can the type given by DIE define another type? */
14404
14405static bool
14406type_can_define_types (const struct die_info *die)
14407{
14408 switch (die->tag)
14409 {
14410 case DW_TAG_typedef:
14411 case DW_TAG_class_type:
14412 case DW_TAG_structure_type:
14413 case DW_TAG_union_type:
14414 case DW_TAG_enumeration_type:
14415 return true;
14416
14417 default:
14418 return false;
14419 }
14420}
14421
14422/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14423
14424static void
883fd55a
KS
14425dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14426 struct dwarf2_cu *cu)
6e70227d 14427{
be2daae6
TT
14428 struct decl_field fp;
14429 memset (&fp, 0, sizeof (fp));
98751a41 14430
883fd55a 14431 gdb_assert (type_can_define_types (die));
98751a41 14432
883fd55a 14433 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14434 fp.name = dwarf2_name (die, cu);
14435 fp.type = read_type_die (die, cu);
98751a41 14436
c191a687
KS
14437 /* Save accessibility. */
14438 enum dwarf_access_attribute accessibility;
14439 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14440 if (attr != NULL)
14441 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14442 else
14443 accessibility = dwarf2_default_access_attribute (die, cu);
14444 switch (accessibility)
14445 {
14446 case DW_ACCESS_public:
14447 /* The assumed value if neither private nor protected. */
14448 break;
14449 case DW_ACCESS_private:
be2daae6 14450 fp.is_private = 1;
c191a687
KS
14451 break;
14452 case DW_ACCESS_protected:
be2daae6 14453 fp.is_protected = 1;
c191a687
KS
14454 break;
14455 default:
b98664d3 14456 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14457 }
14458
883fd55a 14459 if (die->tag == DW_TAG_typedef)
be2daae6 14460 fip->typedef_field_list.push_back (fp);
883fd55a 14461 else
be2daae6 14462 fip->nested_types_list.push_back (fp);
98751a41
JK
14463}
14464
c906108c
SS
14465/* Create the vector of fields, and attach it to the type. */
14466
14467static void
fba45db2 14468dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14469 struct dwarf2_cu *cu)
c906108c
SS
14470{
14471 int nfields = fip->nfields;
14472
14473 /* Record the field count, allocate space for the array of fields,
14474 and create blank accessibility bitfields if necessary. */
14475 TYPE_NFIELDS (type) = nfields;
14476 TYPE_FIELDS (type) = (struct field *)
be2daae6 14477 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14478
b4ba55a1 14479 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14480 {
14481 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14482
14483 TYPE_FIELD_PRIVATE_BITS (type) =
14484 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14485 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14486
14487 TYPE_FIELD_PROTECTED_BITS (type) =
14488 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14489 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14490
774b6a14
TT
14491 TYPE_FIELD_IGNORE_BITS (type) =
14492 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14493 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14494 }
14495
14496 /* If the type has baseclasses, allocate and clear a bit vector for
14497 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14498 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14499 {
be2daae6 14500 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14501 unsigned char *pointer;
c906108c
SS
14502
14503 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14504 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14505 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14506 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14507 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14508 }
14509
2ddeaf8a
TT
14510 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14511 {
14512 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14513
be2daae6 14514 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14515 {
be2daae6
TT
14516 struct nextfield &field = fip->fields[index];
14517
14518 if (field.variant.is_discriminant)
2ddeaf8a 14519 di->discriminant_index = index;
be2daae6 14520 else if (field.variant.default_branch)
2ddeaf8a
TT
14521 di->default_index = index;
14522 else
be2daae6 14523 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14524 }
14525 }
14526
be2daae6
TT
14527 /* Copy the saved-up fields into the field vector. */
14528 for (int i = 0; i < nfields; ++i)
c906108c 14529 {
be2daae6
TT
14530 struct nextfield &field
14531 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14532 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14533
be2daae6
TT
14534 TYPE_FIELD (type, i) = field.field;
14535 switch (field.accessibility)
c906108c 14536 {
c5aa993b 14537 case DW_ACCESS_private:
b4ba55a1 14538 if (cu->language != language_ada)
be2daae6 14539 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14540 break;
c906108c 14541
c5aa993b 14542 case DW_ACCESS_protected:
b4ba55a1 14543 if (cu->language != language_ada)
be2daae6 14544 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14545 break;
c906108c 14546
c5aa993b
JM
14547 case DW_ACCESS_public:
14548 break;
c906108c 14549
c5aa993b
JM
14550 default:
14551 /* Unknown accessibility. Complain and treat it as public. */
14552 {
b98664d3 14553 complaint (_("unsupported accessibility %d"),
be2daae6 14554 field.accessibility);
c5aa993b
JM
14555 }
14556 break;
c906108c 14557 }
be2daae6 14558 if (i < fip->baseclasses.size ())
c906108c 14559 {
be2daae6 14560 switch (field.virtuality)
c906108c 14561 {
c5aa993b
JM
14562 case DW_VIRTUALITY_virtual:
14563 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14564 if (cu->language == language_ada)
a73c6dcd 14565 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14566 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14567 break;
c906108c
SS
14568 }
14569 }
c906108c
SS
14570 }
14571}
14572
7d27a96d
TT
14573/* Return true if this member function is a constructor, false
14574 otherwise. */
14575
14576static int
14577dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14578{
14579 const char *fieldname;
fe978cb0 14580 const char *type_name;
7d27a96d
TT
14581 int len;
14582
14583 if (die->parent == NULL)
14584 return 0;
14585
14586 if (die->parent->tag != DW_TAG_structure_type
14587 && die->parent->tag != DW_TAG_union_type
14588 && die->parent->tag != DW_TAG_class_type)
14589 return 0;
14590
14591 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14592 type_name = dwarf2_name (die->parent, cu);
14593 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14594 return 0;
14595
14596 len = strlen (fieldname);
fe978cb0
PA
14597 return (strncmp (fieldname, type_name, len) == 0
14598 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14599}
14600
e35000a7
TBA
14601/* Check if the given VALUE is a recognized enum
14602 dwarf_defaulted_attribute constant according to DWARF5 spec,
14603 Table 7.24. */
14604
14605static bool
14606is_valid_DW_AT_defaulted (ULONGEST value)
14607{
14608 switch (value)
14609 {
14610 case DW_DEFAULTED_no:
14611 case DW_DEFAULTED_in_class:
14612 case DW_DEFAULTED_out_of_class:
14613 return true;
14614 }
14615
3142e908 14616 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14617 return false;
14618}
14619
c906108c
SS
14620/* Add a member function to the proper fieldlist. */
14621
14622static void
107d2387 14623dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14624 struct type *type, struct dwarf2_cu *cu)
c906108c 14625{
518817b3 14626 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14627 struct attribute *attr;
c906108c 14628 int i;
be2daae6 14629 struct fnfieldlist *flp = nullptr;
c906108c 14630 struct fn_field *fnp;
15d034d0 14631 const char *fieldname;
f792889a 14632 struct type *this_type;
60d5a603 14633 enum dwarf_access_attribute accessibility;
c906108c 14634
b4ba55a1 14635 if (cu->language == language_ada)
a73c6dcd 14636 error (_("unexpected member function in Ada type"));
b4ba55a1 14637
2df3850c 14638 /* Get name of member function. */
39cbfefa
DJ
14639 fieldname = dwarf2_name (die, cu);
14640 if (fieldname == NULL)
2df3850c 14641 return;
c906108c 14642
c906108c 14643 /* Look up member function name in fieldlist. */
be2daae6 14644 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14645 {
27bfe10e 14646 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14647 {
14648 flp = &fip->fnfieldlists[i];
14649 break;
14650 }
c906108c
SS
14651 }
14652
be2daae6
TT
14653 /* Create a new fnfieldlist if necessary. */
14654 if (flp == nullptr)
c906108c 14655 {
be2daae6
TT
14656 fip->fnfieldlists.emplace_back ();
14657 flp = &fip->fnfieldlists.back ();
c906108c 14658 flp->name = fieldname;
be2daae6 14659 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14660 }
14661
be2daae6
TT
14662 /* Create a new member function field and add it to the vector of
14663 fnfieldlists. */
14664 flp->fnfields.emplace_back ();
14665 fnp = &flp->fnfields.back ();
3da10d80
KS
14666
14667 /* Delay processing of the physname until later. */
9c37b5ae 14668 if (cu->language == language_cplus)
be2daae6
TT
14669 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14670 die, cu);
3da10d80
KS
14671 else
14672 {
1d06ead6 14673 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14674 fnp->physname = physname ? physname : "";
14675 }
14676
c906108c 14677 fnp->type = alloc_type (objfile);
f792889a
DJ
14678 this_type = read_type_die (die, cu);
14679 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14680 {
f792889a 14681 int nparams = TYPE_NFIELDS (this_type);
c906108c 14682
f792889a 14683 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14684 of the method itself (TYPE_CODE_METHOD). */
14685 smash_to_method_type (fnp->type, type,
f792889a
DJ
14686 TYPE_TARGET_TYPE (this_type),
14687 TYPE_FIELDS (this_type),
14688 TYPE_NFIELDS (this_type),
14689 TYPE_VARARGS (this_type));
c906108c
SS
14690
14691 /* Handle static member functions.
c5aa993b 14692 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14693 member functions. G++ helps GDB by marking the first
14694 parameter for non-static member functions (which is the this
14695 pointer) as artificial. We obtain this information from
14696 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14697 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14698 fnp->voffset = VOFFSET_STATIC;
14699 }
14700 else
b98664d3 14701 complaint (_("member function type missing for '%s'"),
3da10d80 14702 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14703
14704 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14705 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14706 fnp->fcontext = die_containing_type (die, cu);
c906108c 14707
3e43a32a
MS
14708 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14709 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14710
14711 /* Get accessibility. */
e142c38c 14712 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14713 if (attr != nullptr)
aead7601 14714 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14715 else
14716 accessibility = dwarf2_default_access_attribute (die, cu);
14717 switch (accessibility)
c906108c 14718 {
60d5a603
JK
14719 case DW_ACCESS_private:
14720 fnp->is_private = 1;
14721 break;
14722 case DW_ACCESS_protected:
14723 fnp->is_protected = 1;
14724 break;
c906108c
SS
14725 }
14726
b02dede2 14727 /* Check for artificial methods. */
e142c38c 14728 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14729 if (attr && DW_UNSND (attr) != 0)
14730 fnp->is_artificial = 1;
14731
e35000a7
TBA
14732 /* Check for defaulted methods. */
14733 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14734 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14735 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14736
14737 /* Check for deleted methods. */
14738 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14739 if (attr != nullptr && DW_UNSND (attr) != 0)
14740 fnp->is_deleted = 1;
14741
7d27a96d
TT
14742 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14743
0d564a31 14744 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14745 function. For older versions of GCC, this is an offset in the
14746 appropriate virtual table, as specified by DW_AT_containing_type.
14747 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14748 to the object address. */
14749
e142c38c 14750 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14751 if (attr != nullptr)
8e19ed76 14752 {
4fc6c0d5 14753 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14754 {
aec5aa8b
TT
14755 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14756 {
14757 /* Old-style GCC. */
14758 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14759 }
14760 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14761 || (DW_BLOCK (attr)->size > 1
14762 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14763 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14764 {
aec5aa8b
TT
14765 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14766 if ((fnp->voffset % cu->header.addr_size) != 0)
14767 dwarf2_complex_location_expr_complaint ();
14768 else
14769 fnp->voffset /= cu->header.addr_size;
14770 fnp->voffset += 2;
14771 }
14772 else
14773 dwarf2_complex_location_expr_complaint ();
14774
14775 if (!fnp->fcontext)
7e993ebf
KS
14776 {
14777 /* If there is no `this' field and no DW_AT_containing_type,
14778 we cannot actually find a base class context for the
14779 vtable! */
14780 if (TYPE_NFIELDS (this_type) == 0
14781 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14782 {
b98664d3 14783 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14784 "function \"%s\" (offset %s)"),
14785 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14786 }
14787 else
14788 {
14789 fnp->fcontext
14790 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14791 }
14792 }
aec5aa8b 14793 }
cd6c91b4 14794 else if (attr->form_is_section_offset ())
8e19ed76 14795 {
4d3c2250 14796 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14797 }
14798 else
14799 {
4d3c2250
KB
14800 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14801 fieldname);
8e19ed76 14802 }
0d564a31 14803 }
d48cc9dd
DJ
14804 else
14805 {
14806 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14807 if (attr && DW_UNSND (attr))
14808 {
14809 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14810 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14811 "but the vtable offset is not specified"),
9d8780f0 14812 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14814 TYPE_CPLUS_DYNAMIC (type) = 1;
14815 }
14816 }
c906108c
SS
14817}
14818
14819/* Create the vector of member function fields, and attach it to the type. */
14820
14821static void
fba45db2 14822dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14823 struct dwarf2_cu *cu)
c906108c 14824{
b4ba55a1 14825 if (cu->language == language_ada)
a73c6dcd 14826 error (_("unexpected member functions in Ada type"));
b4ba55a1 14827
c906108c
SS
14828 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14829 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14830 TYPE_ALLOC (type,
14831 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14832
be2daae6 14833 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14834 {
be2daae6 14835 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14836 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14837
be2daae6
TT
14838 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14839 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14840 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14841 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14842
14843 for (int k = 0; k < nf.fnfields.size (); ++k)
14844 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14845 }
14846
be2daae6 14847 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14848}
14849
1168df01
JB
14850/* Returns non-zero if NAME is the name of a vtable member in CU's
14851 language, zero otherwise. */
14852static int
14853is_vtable_name (const char *name, struct dwarf2_cu *cu)
14854{
14855 static const char vptr[] = "_vptr";
14856
9c37b5ae
TT
14857 /* Look for the C++ form of the vtable. */
14858 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14859 return 1;
14860
14861 return 0;
14862}
14863
c0dd20ea 14864/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14865 functions, with the ABI-specified layout. If TYPE describes
14866 such a structure, smash it into a member function type.
61049d3b
DJ
14867
14868 GCC shouldn't do this; it should just output pointer to member DIEs.
14869 This is GCC PR debug/28767. */
c0dd20ea 14870
0b92b5bb
TT
14871static void
14872quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14873{
09e2d7c7 14874 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14875
14876 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14877 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14878 return;
c0dd20ea
DJ
14879
14880 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14881 if (TYPE_FIELD_NAME (type, 0) == NULL
14882 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14883 || TYPE_FIELD_NAME (type, 1) == NULL
14884 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14885 return;
c0dd20ea
DJ
14886
14887 /* Find the type of the method. */
0b92b5bb 14888 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14889 if (pfn_type == NULL
14890 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14891 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14892 return;
c0dd20ea
DJ
14893
14894 /* Look for the "this" argument. */
14895 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14896 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14897 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14898 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14899 return;
c0dd20ea 14900
09e2d7c7 14901 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14902 new_type = alloc_type (objfile);
09e2d7c7 14903 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14904 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14905 TYPE_VARARGS (pfn_type));
0b92b5bb 14906 smash_to_methodptr_type (type, new_type);
c0dd20ea 14907}
1168df01 14908
2b4424c3
TT
14909/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14910 appropriate error checking and issuing complaints if there is a
14911 problem. */
14912
14913static ULONGEST
14914get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14915{
14916 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14917
14918 if (attr == nullptr)
14919 return 0;
14920
cd6c91b4 14921 if (!attr->form_is_constant ())
2b4424c3 14922 {
b98664d3 14923 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14924 " - DIE at %s [in module %s]"),
14925 sect_offset_str (die->sect_off),
14926 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14927 return 0;
14928 }
14929
14930 ULONGEST align;
14931 if (attr->form == DW_FORM_sdata)
14932 {
14933 LONGEST val = DW_SND (attr);
14934 if (val < 0)
14935 {
b98664d3 14936 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14937 " - DIE at %s [in module %s]"),
14938 sect_offset_str (die->sect_off),
14939 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14940 return 0;
14941 }
14942 align = val;
14943 }
14944 else
14945 align = DW_UNSND (attr);
14946
14947 if (align == 0)
14948 {
b98664d3 14949 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14950 " - DIE at %s [in module %s]"),
14951 sect_offset_str (die->sect_off),
14952 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14953 return 0;
14954 }
14955 if ((align & (align - 1)) != 0)
14956 {
b98664d3 14957 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14958 " - DIE at %s [in module %s]"),
14959 sect_offset_str (die->sect_off),
14960 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14961 return 0;
14962 }
14963
14964 return align;
14965}
14966
14967/* If the DIE has a DW_AT_alignment attribute, use its value to set
14968 the alignment for TYPE. */
14969
14970static void
14971maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14972 struct type *type)
14973{
14974 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14975 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14976 " - DIE at %s [in module %s]"),
14977 sect_offset_str (die->sect_off),
14978 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14979}
685b1105 14980
e35000a7
TBA
14981/* Check if the given VALUE is a valid enum dwarf_calling_convention
14982 constant for a type, according to DWARF5 spec, Table 5.5. */
14983
14984static bool
14985is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14986{
14987 switch (value)
14988 {
14989 case DW_CC_normal:
14990 case DW_CC_pass_by_reference:
14991 case DW_CC_pass_by_value:
14992 return true;
14993
14994 default:
14995 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14996 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14997 return false;
14998 }
14999}
15000
d0922fcf
TBA
15001/* Check if the given VALUE is a valid enum dwarf_calling_convention
15002 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15003 also according to GNU-specific values (see include/dwarf2.h). */
15004
15005static bool
15006is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15007{
15008 switch (value)
15009 {
15010 case DW_CC_normal:
15011 case DW_CC_program:
15012 case DW_CC_nocall:
15013 return true;
15014
15015 case DW_CC_GNU_renesas_sh:
15016 case DW_CC_GNU_borland_fastcall_i386:
15017 case DW_CC_GDB_IBM_OpenCL:
15018 return true;
15019
15020 default:
15021 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15022 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15023 return false;
15024 }
15025}
15026
c906108c 15027/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15028 (definition) to create a type for the structure or union. Fill in
15029 the type's name and general properties; the members will not be
83655187
DE
15030 processed until process_structure_scope. A symbol table entry for
15031 the type will also not be done until process_structure_scope (assuming
15032 the type has a name).
c906108c 15033
c767944b
DJ
15034 NOTE: we need to call these functions regardless of whether or not the
15035 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15036 structure or union. This gets the type entered into our set of
83655187 15037 user defined types. */
c906108c 15038
f792889a 15039static struct type *
134d01f1 15040read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15041{
518817b3 15042 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15043 struct type *type;
15044 struct attribute *attr;
15d034d0 15045 const char *name;
c906108c 15046
348e048f
DE
15047 /* If the definition of this type lives in .debug_types, read that type.
15048 Don't follow DW_AT_specification though, that will take us back up
15049 the chain and we want to go down. */
45e58e77 15050 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15051 if (attr != nullptr)
348e048f 15052 {
ac9ec31b 15053 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15054
ac9ec31b 15055 /* The type's CU may not be the same as CU.
02142a6c 15056 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15057 return set_die_type (die, type, cu);
15058 }
15059
c0dd20ea 15060 type = alloc_type (objfile);
c906108c 15061 INIT_CPLUS_SPECIFIC (type);
93311388 15062
39cbfefa
DJ
15063 name = dwarf2_name (die, cu);
15064 if (name != NULL)
c906108c 15065 {
987504bb 15066 if (cu->language == language_cplus
c44af4eb
TT
15067 || cu->language == language_d
15068 || cu->language == language_rust)
63d06c5c 15069 {
15d034d0 15070 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15071
15072 /* dwarf2_full_name might have already finished building the DIE's
15073 type. If so, there is no need to continue. */
15074 if (get_die_type (die, cu) != NULL)
15075 return get_die_type (die, cu);
15076
e86ca25f 15077 TYPE_NAME (type) = full_name;
63d06c5c
DC
15078 }
15079 else
15080 {
d8151005
DJ
15081 /* The name is already allocated along with this objfile, so
15082 we don't need to duplicate it for the type. */
e86ca25f 15083 TYPE_NAME (type) = name;
63d06c5c 15084 }
c906108c
SS
15085 }
15086
15087 if (die->tag == DW_TAG_structure_type)
15088 {
15089 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15090 }
15091 else if (die->tag == DW_TAG_union_type)
15092 {
15093 TYPE_CODE (type) = TYPE_CODE_UNION;
15094 }
2ddeaf8a
TT
15095 else if (die->tag == DW_TAG_variant_part)
15096 {
15097 TYPE_CODE (type) = TYPE_CODE_UNION;
15098 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15099 }
c906108c
SS
15100 else
15101 {
4753d33b 15102 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15103 }
15104
0cc2414c
TT
15105 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15106 TYPE_DECLARED_CLASS (type) = 1;
15107
e35000a7
TBA
15108 /* Store the calling convention in the type if it's available in
15109 the die. Otherwise the calling convention remains set to
15110 the default value DW_CC_normal. */
15111 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15112 if (attr != nullptr
15113 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15114 {
15115 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15116 TYPE_CPLUS_CALLING_CONVENTION (type)
15117 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15118 }
15119
e142c38c 15120 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15121 if (attr != nullptr)
c906108c 15122 {
cd6c91b4 15123 if (attr->form_is_constant ())
155bfbd3
JB
15124 TYPE_LENGTH (type) = DW_UNSND (attr);
15125 else
15126 {
15127 /* For the moment, dynamic type sizes are not supported
15128 by GDB's struct type. The actual size is determined
15129 on-demand when resolving the type of a given object,
15130 so set the type's length to zero for now. Otherwise,
15131 we record an expression as the length, and that expression
15132 could lead to a very large value, which could eventually
15133 lead to us trying to allocate that much memory when creating
15134 a value of that type. */
15135 TYPE_LENGTH (type) = 0;
15136 }
c906108c
SS
15137 }
15138 else
15139 {
15140 TYPE_LENGTH (type) = 0;
15141 }
15142
2b4424c3
TT
15143 maybe_set_alignment (cu, die, type);
15144
5230b05a 15145 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15146 {
5230b05a
WT
15147 /* ICC<14 does not output the required DW_AT_declaration on
15148 incomplete types, but gives them a size of zero. */
422b1cb0 15149 TYPE_STUB (type) = 1;
685b1105
JK
15150 }
15151 else
15152 TYPE_STUB_SUPPORTED (type) = 1;
15153
dc718098 15154 if (die_is_declaration (die, cu))
876cecd0 15155 TYPE_STUB (type) = 1;
a6c727b2
DJ
15156 else if (attr == NULL && die->child == NULL
15157 && producer_is_realview (cu->producer))
15158 /* RealView does not output the required DW_AT_declaration
15159 on incomplete types. */
15160 TYPE_STUB (type) = 1;
dc718098 15161
c906108c
SS
15162 /* We need to add the type field to the die immediately so we don't
15163 infinitely recurse when dealing with pointers to the structure
0963b4bd 15164 type within the structure itself. */
1c379e20 15165 set_die_type (die, type, cu);
c906108c 15166
7e314c57
JK
15167 /* set_die_type should be already done. */
15168 set_descriptive_type (type, die, cu);
15169
c767944b
DJ
15170 return type;
15171}
15172
2ddeaf8a
TT
15173/* A helper for process_structure_scope that handles a single member
15174 DIE. */
15175
15176static void
15177handle_struct_member_die (struct die_info *child_die, struct type *type,
15178 struct field_info *fi,
15179 std::vector<struct symbol *> *template_args,
15180 struct dwarf2_cu *cu)
15181{
15182 if (child_die->tag == DW_TAG_member
15183 || child_die->tag == DW_TAG_variable
15184 || child_die->tag == DW_TAG_variant_part)
15185 {
15186 /* NOTE: carlton/2002-11-05: A C++ static data member
15187 should be a DW_TAG_member that is a declaration, but
15188 all versions of G++ as of this writing (so through at
15189 least 3.2.1) incorrectly generate DW_TAG_variable
15190 tags for them instead. */
15191 dwarf2_add_field (fi, child_die, cu);
15192 }
15193 else if (child_die->tag == DW_TAG_subprogram)
15194 {
15195 /* Rust doesn't have member functions in the C++ sense.
15196 However, it does emit ordinary functions as children
15197 of a struct DIE. */
15198 if (cu->language == language_rust)
15199 read_func_scope (child_die, cu);
15200 else
15201 {
15202 /* C++ member function. */
15203 dwarf2_add_member_fn (fi, child_die, type, cu);
15204 }
15205 }
15206 else if (child_die->tag == DW_TAG_inheritance)
15207 {
15208 /* C++ base class field. */
15209 dwarf2_add_field (fi, child_die, cu);
15210 }
15211 else if (type_can_define_types (child_die))
15212 dwarf2_add_type_defn (fi, child_die, cu);
15213 else if (child_die->tag == DW_TAG_template_type_param
15214 || child_die->tag == DW_TAG_template_value_param)
15215 {
15216 struct symbol *arg = new_symbol (child_die, NULL, cu);
15217
15218 if (arg != NULL)
15219 template_args->push_back (arg);
15220 }
15221 else if (child_die->tag == DW_TAG_variant)
15222 {
15223 /* In a variant we want to get the discriminant and also add a
15224 field for our sole member child. */
15225 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15226
bde09ab7 15227 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15228 variant_child != NULL;
15229 variant_child = sibling_die (variant_child))
15230 {
15231 if (variant_child->tag == DW_TAG_member)
15232 {
15233 handle_struct_member_die (variant_child, type, fi,
15234 template_args, cu);
15235 /* Only handle the one. */
15236 break;
15237 }
15238 }
15239
15240 /* We don't handle this but we might as well report it if we see
15241 it. */
15242 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15243 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15244 " - DIE at %s [in module %s]"),
15245 sect_offset_str (child_die->sect_off),
15246 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15247
15248 /* The first field was just added, so we can stash the
15249 discriminant there. */
be2daae6 15250 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15251 if (discr == NULL)
be2daae6 15252 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15253 else
be2daae6 15254 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15255 }
15256}
15257
c767944b
DJ
15258/* Finish creating a structure or union type, including filling in
15259 its members and creating a symbol for it. */
15260
15261static void
15262process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15263{
518817b3 15264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15265 struct die_info *child_die;
c767944b
DJ
15266 struct type *type;
15267
15268 type = get_die_type (die, cu);
15269 if (type == NULL)
15270 type = read_structure_type (die, cu);
15271
2ddeaf8a
TT
15272 /* When reading a DW_TAG_variant_part, we need to notice when we
15273 read the discriminant member, so we can record it later in the
15274 discriminant_info. */
15275 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15276 sect_offset discr_offset {};
3e1d3d8c 15277 bool has_template_parameters = false;
2ddeaf8a
TT
15278
15279 if (is_variant_part)
15280 {
15281 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15282 if (discr == NULL)
15283 {
15284 /* Maybe it's a univariant form, an extension we support.
15285 In this case arrange not to check the offset. */
15286 is_variant_part = false;
15287 }
cd6c91b4 15288 else if (discr->form_is_ref ())
2ddeaf8a
TT
15289 {
15290 struct dwarf2_cu *target_cu = cu;
15291 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15292
15293 discr_offset = target_die->sect_off;
15294 }
15295 else
15296 {
b98664d3 15297 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15298 " - DIE at %s [in module %s]"),
15299 sect_offset_str (die->sect_off),
15300 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15301 is_variant_part = false;
15302 }
15303 }
15304
e142c38c 15305 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15306 {
15307 struct field_info fi;
2f4732b0 15308 std::vector<struct symbol *> template_args;
c906108c 15309
639d11d3 15310 child_die = die->child;
c906108c
SS
15311
15312 while (child_die && child_die->tag)
15313 {
2ddeaf8a 15314 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15315
2ddeaf8a 15316 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15317 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15318
c906108c
SS
15319 child_die = sibling_die (child_die);
15320 }
15321
34eaf542 15322 /* Attach template arguments to type. */
2f4732b0 15323 if (!template_args.empty ())
34eaf542 15324 {
3e1d3d8c 15325 has_template_parameters = true;
34eaf542 15326 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15327 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15328 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15329 = XOBNEWVEC (&objfile->objfile_obstack,
15330 struct symbol *,
15331 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15332 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15333 template_args.data (),
34eaf542
TT
15334 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15335 * sizeof (struct symbol *)));
34eaf542
TT
15336 }
15337
c906108c
SS
15338 /* Attach fields and member functions to the type. */
15339 if (fi.nfields)
e7c27a73 15340 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15341 if (!fi.fnfieldlists.empty ())
c906108c 15342 {
e7c27a73 15343 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15344
c5aa993b 15345 /* Get the type which refers to the base class (possibly this
c906108c 15346 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15347 class from the DW_AT_containing_type attribute. This use of
15348 DW_AT_containing_type is a GNU extension. */
c906108c 15349
e142c38c 15350 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15351 {
e7c27a73 15352 struct type *t = die_containing_type (die, cu);
c906108c 15353
ae6ae975 15354 set_type_vptr_basetype (type, t);
c906108c
SS
15355 if (type == t)
15356 {
c906108c
SS
15357 int i;
15358
15359 /* Our own class provides vtbl ptr. */
15360 for (i = TYPE_NFIELDS (t) - 1;
15361 i >= TYPE_N_BASECLASSES (t);
15362 --i)
15363 {
0d5cff50 15364 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15365
1168df01 15366 if (is_vtable_name (fieldname, cu))
c906108c 15367 {
ae6ae975 15368 set_type_vptr_fieldno (type, i);
c906108c
SS
15369 break;
15370 }
15371 }
15372
15373 /* Complain if virtual function table field not found. */
15374 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15375 complaint (_("virtual function table pointer "
3e43a32a 15376 "not found when defining class '%s'"),
e86ca25f 15377 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15378 }
15379 else
15380 {
ae6ae975 15381 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15382 }
15383 }
f6235d4c 15384 else if (cu->producer
61012eef 15385 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15386 {
15387 /* The IBM XLC compiler does not provide direct indication
15388 of the containing type, but the vtable pointer is
15389 always named __vfp. */
15390
15391 int i;
15392
15393 for (i = TYPE_NFIELDS (type) - 1;
15394 i >= TYPE_N_BASECLASSES (type);
15395 --i)
15396 {
15397 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15398 {
ae6ae975
DE
15399 set_type_vptr_fieldno (type, i);
15400 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15401 break;
15402 }
15403 }
15404 }
c906108c 15405 }
98751a41
JK
15406
15407 /* Copy fi.typedef_field_list linked list elements content into the
15408 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15409 if (!fi.typedef_field_list.empty ())
98751a41 15410 {
be2daae6 15411 int count = fi.typedef_field_list.size ();
98751a41 15412
a0d7a4ff 15413 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15414 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15415 = ((struct decl_field *)
be2daae6
TT
15416 TYPE_ALLOC (type,
15417 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15418 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15419
be2daae6
TT
15420 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15421 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15422 }
c767944b 15423
883fd55a
KS
15424 /* Copy fi.nested_types_list linked list elements content into the
15425 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15426 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15427 {
be2daae6 15428 int count = fi.nested_types_list.size ();
883fd55a
KS
15429
15430 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15431 TYPE_NESTED_TYPES_ARRAY (type)
15432 = ((struct decl_field *)
be2daae6
TT
15433 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15434 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15435
be2daae6
TT
15436 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15437 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15438 }
c906108c 15439 }
63d06c5c 15440
bb5ed363 15441 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15442 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15443 cu->rust_unions.push_back (type);
0b92b5bb 15444
90aeadfc
DC
15445 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15446 snapshots) has been known to create a die giving a declaration
15447 for a class that has, as a child, a die giving a definition for a
15448 nested class. So we have to process our children even if the
15449 current die is a declaration. Normally, of course, a declaration
15450 won't have any children at all. */
134d01f1 15451
ca040673
DE
15452 child_die = die->child;
15453
90aeadfc
DC
15454 while (child_die != NULL && child_die->tag)
15455 {
15456 if (child_die->tag == DW_TAG_member
15457 || child_die->tag == DW_TAG_variable
34eaf542
TT
15458 || child_die->tag == DW_TAG_inheritance
15459 || child_die->tag == DW_TAG_template_value_param
15460 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15461 {
90aeadfc 15462 /* Do nothing. */
134d01f1 15463 }
90aeadfc
DC
15464 else
15465 process_die (child_die, cu);
134d01f1 15466
90aeadfc 15467 child_die = sibling_die (child_die);
134d01f1
DJ
15468 }
15469
fa4028e9
JB
15470 /* Do not consider external references. According to the DWARF standard,
15471 these DIEs are identified by the fact that they have no byte_size
15472 attribute, and a declaration attribute. */
15473 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15474 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15475 {
15476 struct symbol *sym = new_symbol (die, type, cu);
15477
15478 if (has_template_parameters)
15479 {
a776957c
TT
15480 struct symtab *symtab;
15481 if (sym != nullptr)
15482 symtab = symbol_symtab (sym);
15483 else if (cu->line_header != nullptr)
15484 {
15485 /* Any related symtab will do. */
15486 symtab
7ba99d21 15487 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15488 }
15489 else
15490 {
15491 symtab = nullptr;
15492 complaint (_("could not find suitable "
15493 "symtab for template parameter"
15494 " - DIE at %s [in module %s]"),
15495 sect_offset_str (die->sect_off),
15496 objfile_name (objfile));
15497 }
15498
15499 if (symtab != nullptr)
15500 {
15501 /* Make sure that the symtab is set on the new symbols.
15502 Even though they don't appear in this symtab directly,
15503 other parts of gdb assume that symbols do, and this is
15504 reasonably true. */
15505 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15506 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15507 }
3e1d3d8c
TT
15508 }
15509 }
134d01f1
DJ
15510}
15511
55426c9d
JB
15512/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15513 update TYPE using some information only available in DIE's children. */
15514
15515static void
15516update_enumeration_type_from_children (struct die_info *die,
15517 struct type *type,
15518 struct dwarf2_cu *cu)
15519{
60f7655a 15520 struct die_info *child_die;
55426c9d
JB
15521 int unsigned_enum = 1;
15522 int flag_enum = 1;
55426c9d 15523
8268c778 15524 auto_obstack obstack;
55426c9d 15525
60f7655a
DE
15526 for (child_die = die->child;
15527 child_die != NULL && child_die->tag;
15528 child_die = sibling_die (child_die))
55426c9d
JB
15529 {
15530 struct attribute *attr;
15531 LONGEST value;
15532 const gdb_byte *bytes;
15533 struct dwarf2_locexpr_baton *baton;
15534 const char *name;
60f7655a 15535
55426c9d
JB
15536 if (child_die->tag != DW_TAG_enumerator)
15537 continue;
15538
15539 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15540 if (attr == NULL)
15541 continue;
15542
15543 name = dwarf2_name (child_die, cu);
15544 if (name == NULL)
15545 name = "<anonymous enumerator>";
15546
15547 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15548 &value, &bytes, &baton);
15549 if (value < 0)
15550 {
15551 unsigned_enum = 0;
15552 flag_enum = 0;
15553 }
55426c9d 15554 else
edd45eb0
SM
15555 {
15556 if (count_one_bits_ll (value) >= 2)
15557 flag_enum = 0;
edd45eb0 15558 }
55426c9d
JB
15559
15560 /* If we already know that the enum type is neither unsigned, nor
15561 a flag type, no need to look at the rest of the enumerates. */
15562 if (!unsigned_enum && !flag_enum)
15563 break;
55426c9d
JB
15564 }
15565
15566 if (unsigned_enum)
15567 TYPE_UNSIGNED (type) = 1;
15568 if (flag_enum)
15569 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15570}
15571
134d01f1
DJ
15572/* Given a DW_AT_enumeration_type die, set its type. We do not
15573 complete the type's fields yet, or create any symbols. */
c906108c 15574
f792889a 15575static struct type *
134d01f1 15576read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15577{
518817b3 15578 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15579 struct type *type;
c906108c 15580 struct attribute *attr;
0114d602 15581 const char *name;
134d01f1 15582
348e048f
DE
15583 /* If the definition of this type lives in .debug_types, read that type.
15584 Don't follow DW_AT_specification though, that will take us back up
15585 the chain and we want to go down. */
45e58e77 15586 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15587 if (attr != nullptr)
348e048f 15588 {
ac9ec31b 15589 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15590
ac9ec31b 15591 /* The type's CU may not be the same as CU.
02142a6c 15592 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15593 return set_die_type (die, type, cu);
15594 }
15595
c906108c
SS
15596 type = alloc_type (objfile);
15597
15598 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15599 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15600 if (name != NULL)
e86ca25f 15601 TYPE_NAME (type) = name;
c906108c 15602
0626fc76
TT
15603 attr = dwarf2_attr (die, DW_AT_type, cu);
15604 if (attr != NULL)
15605 {
15606 struct type *underlying_type = die_type (die, cu);
15607
15608 TYPE_TARGET_TYPE (type) = underlying_type;
15609 }
15610
e142c38c 15611 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15612 if (attr != nullptr)
c906108c
SS
15613 {
15614 TYPE_LENGTH (type) = DW_UNSND (attr);
15615 }
15616 else
15617 {
15618 TYPE_LENGTH (type) = 0;
15619 }
15620
2b4424c3
TT
15621 maybe_set_alignment (cu, die, type);
15622
137033e9
JB
15623 /* The enumeration DIE can be incomplete. In Ada, any type can be
15624 declared as private in the package spec, and then defined only
15625 inside the package body. Such types are known as Taft Amendment
15626 Types. When another package uses such a type, an incomplete DIE
15627 may be generated by the compiler. */
02eb380e 15628 if (die_is_declaration (die, cu))
876cecd0 15629 TYPE_STUB (type) = 1;
02eb380e 15630
0626fc76
TT
15631 /* Finish the creation of this type by using the enum's children.
15632 We must call this even when the underlying type has been provided
15633 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15634 update_enumeration_type_from_children (die, type, cu);
15635
0626fc76
TT
15636 /* If this type has an underlying type that is not a stub, then we
15637 may use its attributes. We always use the "unsigned" attribute
15638 in this situation, because ordinarily we guess whether the type
15639 is unsigned -- but the guess can be wrong and the underlying type
15640 can tell us the reality. However, we defer to a local size
15641 attribute if one exists, because this lets the compiler override
15642 the underlying type if needed. */
15643 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15644 {
15645 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15646 if (TYPE_LENGTH (type) == 0)
15647 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15648 if (TYPE_RAW_ALIGN (type) == 0
15649 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15650 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15651 }
15652
3d567982
TT
15653 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15654
f792889a 15655 return set_die_type (die, type, cu);
134d01f1
DJ
15656}
15657
15658/* Given a pointer to a die which begins an enumeration, process all
15659 the dies that define the members of the enumeration, and create the
15660 symbol for the enumeration type.
15661
15662 NOTE: We reverse the order of the element list. */
15663
15664static void
15665process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15666{
f792889a 15667 struct type *this_type;
134d01f1 15668
f792889a
DJ
15669 this_type = get_die_type (die, cu);
15670 if (this_type == NULL)
15671 this_type = read_enumeration_type (die, cu);
9dc481d3 15672
639d11d3 15673 if (die->child != NULL)
c906108c 15674 {
9dc481d3
DE
15675 struct die_info *child_die;
15676 struct symbol *sym;
43816ebc 15677 std::vector<struct field> fields;
15d034d0 15678 const char *name;
9dc481d3 15679
639d11d3 15680 child_die = die->child;
c906108c
SS
15681 while (child_die && child_die->tag)
15682 {
15683 if (child_die->tag != DW_TAG_enumerator)
15684 {
e7c27a73 15685 process_die (child_die, cu);
c906108c
SS
15686 }
15687 else
15688 {
39cbfefa
DJ
15689 name = dwarf2_name (child_die, cu);
15690 if (name)
c906108c 15691 {
f792889a 15692 sym = new_symbol (child_die, this_type, cu);
c906108c 15693
43816ebc
TT
15694 fields.emplace_back ();
15695 struct field &field = fields.back ();
c906108c 15696
43816ebc
TT
15697 FIELD_NAME (field) = sym->linkage_name ();
15698 FIELD_TYPE (field) = NULL;
15699 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15700 FIELD_BITSIZE (field) = 0;
c906108c
SS
15701 }
15702 }
15703
15704 child_die = sibling_die (child_die);
15705 }
15706
43816ebc 15707 if (!fields.empty ())
c906108c 15708 {
43816ebc 15709 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15710 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15711 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15712 memcpy (TYPE_FIELDS (this_type), fields.data (),
15713 sizeof (struct field) * fields.size ());
c906108c 15714 }
c906108c 15715 }
134d01f1 15716
6c83ed52
TT
15717 /* If we are reading an enum from a .debug_types unit, and the enum
15718 is a declaration, and the enum is not the signatured type in the
15719 unit, then we do not want to add a symbol for it. Adding a
15720 symbol would in some cases obscure the true definition of the
15721 enum, giving users an incomplete type when the definition is
15722 actually available. Note that we do not want to do this for all
15723 enums which are just declarations, because C++0x allows forward
15724 enum declarations. */
3019eac3 15725 if (cu->per_cu->is_debug_types
6c83ed52
TT
15726 && die_is_declaration (die, cu))
15727 {
52dc124a 15728 struct signatured_type *sig_type;
6c83ed52 15729
c0f78cd4 15730 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15731 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15732 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15733 return;
15734 }
15735
f792889a 15736 new_symbol (die, this_type, cu);
c906108c
SS
15737}
15738
15739/* Extract all information from a DW_TAG_array_type DIE and put it in
15740 the DIE's type field. For now, this only handles one dimensional
15741 arrays. */
15742
f792889a 15743static struct type *
e7c27a73 15744read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15745{
518817b3 15746 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15747 struct die_info *child_die;
7e314c57 15748 struct type *type;
c906108c 15749 struct type *element_type, *range_type, *index_type;
c906108c 15750 struct attribute *attr;
15d034d0 15751 const char *name;
a405673c 15752 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15753 unsigned int bit_stride = 0;
c906108c 15754
e7c27a73 15755 element_type = die_type (die, cu);
c906108c 15756
7e314c57
JK
15757 /* The die_type call above may have already set the type for this DIE. */
15758 type = get_die_type (die, cu);
15759 if (type)
15760 return type;
15761
dc53a7ad
JB
15762 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15763 if (attr != NULL)
a405673c
JB
15764 {
15765 int stride_ok;
09ba997f 15766 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15767
15768 byte_stride_prop
15769 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15770 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15771 prop_type);
a405673c
JB
15772 if (!stride_ok)
15773 {
b98664d3 15774 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15775 " - DIE at %s [in module %s]"),
15776 sect_offset_str (die->sect_off),
518817b3 15777 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15778 /* Ignore this attribute. We will likely not be able to print
15779 arrays of this type correctly, but there is little we can do
15780 to help if we cannot read the attribute's value. */
15781 byte_stride_prop = NULL;
15782 }
15783 }
dc53a7ad
JB
15784
15785 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15786 if (attr != NULL)
15787 bit_stride = DW_UNSND (attr);
15788
c906108c
SS
15789 /* Irix 6.2 native cc creates array types without children for
15790 arrays with unspecified length. */
639d11d3 15791 if (die->child == NULL)
c906108c 15792 {
46bf5051 15793 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15794 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15795 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15796 byte_stride_prop, bit_stride);
f792889a 15797 return set_die_type (die, type, cu);
c906108c
SS
15798 }
15799
791afaa2 15800 std::vector<struct type *> range_types;
639d11d3 15801 child_die = die->child;
c906108c
SS
15802 while (child_die && child_die->tag)
15803 {
15804 if (child_die->tag == DW_TAG_subrange_type)
15805 {
f792889a 15806 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15807
f792889a 15808 if (child_type != NULL)
a02abb62 15809 {
0963b4bd
MS
15810 /* The range type was succesfully read. Save it for the
15811 array type creation. */
791afaa2 15812 range_types.push_back (child_type);
a02abb62 15813 }
c906108c
SS
15814 }
15815 child_die = sibling_die (child_die);
15816 }
15817
15818 /* Dwarf2 dimensions are output from left to right, create the
15819 necessary array types in backwards order. */
7ca2d3a3 15820
c906108c 15821 type = element_type;
7ca2d3a3
DL
15822
15823 if (read_array_order (die, cu) == DW_ORD_col_major)
15824 {
15825 int i = 0;
9a619af0 15826
791afaa2 15827 while (i < range_types.size ())
dc53a7ad 15828 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15829 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15830 }
15831 else
15832 {
791afaa2 15833 size_t ndim = range_types.size ();
7ca2d3a3 15834 while (ndim-- > 0)
dc53a7ad 15835 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15836 byte_stride_prop, bit_stride);
7ca2d3a3 15837 }
c906108c 15838
f5f8a009
EZ
15839 /* Understand Dwarf2 support for vector types (like they occur on
15840 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15841 array type. This is not part of the Dwarf2/3 standard yet, but a
15842 custom vendor extension. The main difference between a regular
15843 array and the vector variant is that vectors are passed by value
15844 to functions. */
e142c38c 15845 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15846 if (attr != nullptr)
ea37ba09 15847 make_vector_type (type);
f5f8a009 15848
dbc98a8b
KW
15849 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15850 implementation may choose to implement triple vectors using this
15851 attribute. */
15852 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15853 if (attr != nullptr)
dbc98a8b
KW
15854 {
15855 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15856 TYPE_LENGTH (type) = DW_UNSND (attr);
15857 else
b98664d3 15858 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15859 "than the total size of elements"));
dbc98a8b
KW
15860 }
15861
39cbfefa
DJ
15862 name = dwarf2_name (die, cu);
15863 if (name)
15864 TYPE_NAME (type) = name;
6e70227d 15865
2b4424c3
TT
15866 maybe_set_alignment (cu, die, type);
15867
0963b4bd 15868 /* Install the type in the die. */
7e314c57
JK
15869 set_die_type (die, type, cu);
15870
15871 /* set_die_type should be already done. */
b4ba55a1
JB
15872 set_descriptive_type (type, die, cu);
15873
7e314c57 15874 return type;
c906108c
SS
15875}
15876
7ca2d3a3 15877static enum dwarf_array_dim_ordering
6e70227d 15878read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15879{
15880 struct attribute *attr;
15881
15882 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15883
435d3d88 15884 if (attr != nullptr)
aead7601 15885 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15886
0963b4bd
MS
15887 /* GNU F77 is a special case, as at 08/2004 array type info is the
15888 opposite order to the dwarf2 specification, but data is still
15889 laid out as per normal fortran.
7ca2d3a3 15890
0963b4bd
MS
15891 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15892 version checking. */
7ca2d3a3 15893
905e0470
PM
15894 if (cu->language == language_fortran
15895 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15896 {
15897 return DW_ORD_row_major;
15898 }
15899
6e70227d 15900 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15901 {
15902 case array_column_major:
15903 return DW_ORD_col_major;
15904 case array_row_major:
15905 default:
15906 return DW_ORD_row_major;
15907 };
15908}
15909
72019c9c 15910/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15911 the DIE's type field. */
72019c9c 15912
f792889a 15913static struct type *
72019c9c
GM
15914read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15915{
7e314c57
JK
15916 struct type *domain_type, *set_type;
15917 struct attribute *attr;
f792889a 15918
7e314c57
JK
15919 domain_type = die_type (die, cu);
15920
15921 /* The die_type call above may have already set the type for this DIE. */
15922 set_type = get_die_type (die, cu);
15923 if (set_type)
15924 return set_type;
15925
15926 set_type = create_set_type (NULL, domain_type);
15927
15928 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15929 if (attr != nullptr)
d09039dd 15930 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15931
2b4424c3
TT
15932 maybe_set_alignment (cu, die, set_type);
15933
f792889a 15934 return set_die_type (die, set_type, cu);
72019c9c 15935}
7ca2d3a3 15936
0971de02
TT
15937/* A helper for read_common_block that creates a locexpr baton.
15938 SYM is the symbol which we are marking as computed.
15939 COMMON_DIE is the DIE for the common block.
15940 COMMON_LOC is the location expression attribute for the common
15941 block itself.
15942 MEMBER_LOC is the location expression attribute for the particular
15943 member of the common block that we are processing.
15944 CU is the CU from which the above come. */
15945
15946static void
15947mark_common_block_symbol_computed (struct symbol *sym,
15948 struct die_info *common_die,
15949 struct attribute *common_loc,
15950 struct attribute *member_loc,
15951 struct dwarf2_cu *cu)
15952{
518817b3
SM
15953 struct dwarf2_per_objfile *dwarf2_per_objfile
15954 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15955 struct objfile *objfile = dwarf2_per_objfile->objfile;
15956 struct dwarf2_locexpr_baton *baton;
15957 gdb_byte *ptr;
15958 unsigned int cu_off;
15959 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15960 LONGEST offset = 0;
15961
15962 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15963 gdb_assert (common_loc->form_is_block ());
15964 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15965 || member_loc->form_is_constant ());
0971de02 15966
8d749320 15967 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15968 baton->per_cu = cu->per_cu;
15969 gdb_assert (baton->per_cu);
15970
15971 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15972
cd6c91b4 15973 if (member_loc->form_is_constant ())
0971de02
TT
15974 {
15975 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15976 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15977 }
15978 else
15979 baton->size += DW_BLOCK (member_loc)->size;
15980
224c3ddb 15981 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15982 baton->data = ptr;
15983
15984 *ptr++ = DW_OP_call4;
9c541725 15985 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15986 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15987 ptr += 4;
15988
cd6c91b4 15989 if (member_loc->form_is_constant ())
0971de02
TT
15990 {
15991 *ptr++ = DW_OP_addr;
15992 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15993 ptr += cu->header.addr_size;
15994 }
15995 else
15996 {
15997 /* We have to copy the data here, because DW_OP_call4 will only
15998 use a DW_AT_location attribute. */
15999 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16000 ptr += DW_BLOCK (member_loc)->size;
16001 }
16002
16003 *ptr++ = DW_OP_plus;
16004 gdb_assert (ptr - baton->data == baton->size);
16005
0971de02 16006 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16007 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16008}
16009
4357ac6c
TT
16010/* Create appropriate locally-scoped variables for all the
16011 DW_TAG_common_block entries. Also create a struct common_block
16012 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16013 is used to separate the common blocks name namespace from regular
4357ac6c 16014 variable names. */
c906108c
SS
16015
16016static void
e7c27a73 16017read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16018{
0971de02
TT
16019 struct attribute *attr;
16020
16021 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16022 if (attr != nullptr)
0971de02
TT
16023 {
16024 /* Support the .debug_loc offsets. */
4fc6c0d5 16025 if (attr->form_is_block ())
0971de02
TT
16026 {
16027 /* Ok. */
16028 }
cd6c91b4 16029 else if (attr->form_is_section_offset ())
0971de02
TT
16030 {
16031 dwarf2_complex_location_expr_complaint ();
16032 attr = NULL;
16033 }
16034 else
16035 {
16036 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16037 "common block member");
16038 attr = NULL;
16039 }
16040 }
16041
639d11d3 16042 if (die->child != NULL)
c906108c 16043 {
518817b3 16044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16045 struct die_info *child_die;
16046 size_t n_entries = 0, size;
16047 struct common_block *common_block;
16048 struct symbol *sym;
74ac6d43 16049
4357ac6c
TT
16050 for (child_die = die->child;
16051 child_die && child_die->tag;
16052 child_die = sibling_die (child_die))
16053 ++n_entries;
16054
16055 size = (sizeof (struct common_block)
16056 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16057 common_block
16058 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16059 size);
4357ac6c
TT
16060 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16061 common_block->n_entries = 0;
16062
16063 for (child_die = die->child;
16064 child_die && child_die->tag;
16065 child_die = sibling_die (child_die))
16066 {
16067 /* Create the symbol in the DW_TAG_common_block block in the current
16068 symbol scope. */
e7c27a73 16069 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16070 if (sym != NULL)
16071 {
16072 struct attribute *member_loc;
16073
16074 common_block->contents[common_block->n_entries++] = sym;
16075
16076 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16077 cu);
16078 if (member_loc)
16079 {
16080 /* GDB has handled this for a long time, but it is
16081 not specified by DWARF. It seems to have been
16082 emitted by gfortran at least as recently as:
16083 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16084 complaint (_("Variable in common block has "
0971de02 16085 "DW_AT_data_member_location "
9d8780f0
SM
16086 "- DIE at %s [in module %s]"),
16087 sect_offset_str (child_die->sect_off),
518817b3 16088 objfile_name (objfile));
0971de02 16089
cd6c91b4 16090 if (member_loc->form_is_section_offset ())
0971de02 16091 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16092 else if (member_loc->form_is_constant ()
4fc6c0d5 16093 || member_loc->form_is_block ())
0971de02 16094 {
435d3d88 16095 if (attr != nullptr)
0971de02
TT
16096 mark_common_block_symbol_computed (sym, die, attr,
16097 member_loc, cu);
16098 }
16099 else
16100 dwarf2_complex_location_expr_complaint ();
16101 }
16102 }
c906108c 16103 }
4357ac6c
TT
16104
16105 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16106 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16107 }
16108}
16109
0114d602 16110/* Create a type for a C++ namespace. */
d9fa45fe 16111
0114d602
DJ
16112static struct type *
16113read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16114{
518817b3 16115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16116 const char *previous_prefix, *name;
9219021c 16117 int is_anonymous;
0114d602
DJ
16118 struct type *type;
16119
16120 /* For extensions, reuse the type of the original namespace. */
16121 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16122 {
16123 struct die_info *ext_die;
16124 struct dwarf2_cu *ext_cu = cu;
9a619af0 16125
0114d602
DJ
16126 ext_die = dwarf2_extension (die, &ext_cu);
16127 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16128
16129 /* EXT_CU may not be the same as CU.
02142a6c 16130 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16131 return set_die_type (die, type, cu);
16132 }
9219021c 16133
e142c38c 16134 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16135
16136 /* Now build the name of the current namespace. */
16137
0114d602
DJ
16138 previous_prefix = determine_prefix (die, cu);
16139 if (previous_prefix[0] != '\0')
16140 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16141 previous_prefix, name, 0, cu);
0114d602
DJ
16142
16143 /* Create the type. */
19f392bc 16144 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16145
60531b24 16146 return set_die_type (die, type, cu);
0114d602
DJ
16147}
16148
22cee43f 16149/* Read a namespace scope. */
0114d602
DJ
16150
16151static void
16152read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16153{
518817b3 16154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16155 int is_anonymous;
9219021c 16156
5c4e30ca
DC
16157 /* Add a symbol associated to this if we haven't seen the namespace
16158 before. Also, add a using directive if it's an anonymous
16159 namespace. */
9219021c 16160
f2f0e013 16161 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16162 {
16163 struct type *type;
16164
0114d602 16165 type = read_type_die (die, cu);
e7c27a73 16166 new_symbol (die, type, cu);
5c4e30ca 16167
e8e80198 16168 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16169 if (is_anonymous)
0114d602
DJ
16170 {
16171 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16172
eb1e02fd 16173 std::vector<const char *> excludes;
804d2729 16174 add_using_directive (using_directives (cu),
22cee43f 16175 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16176 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16177 }
5c4e30ca 16178 }
9219021c 16179
639d11d3 16180 if (die->child != NULL)
d9fa45fe 16181 {
639d11d3 16182 struct die_info *child_die = die->child;
6e70227d 16183
d9fa45fe
DC
16184 while (child_die && child_die->tag)
16185 {
e7c27a73 16186 process_die (child_die, cu);
d9fa45fe
DC
16187 child_die = sibling_die (child_die);
16188 }
16189 }
38d518c9
EZ
16190}
16191
f55ee35c
JK
16192/* Read a Fortran module as type. This DIE can be only a declaration used for
16193 imported module. Still we need that type as local Fortran "use ... only"
16194 declaration imports depend on the created type in determine_prefix. */
16195
16196static struct type *
16197read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16198{
518817b3 16199 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16200 const char *module_name;
f55ee35c
JK
16201 struct type *type;
16202
16203 module_name = dwarf2_name (die, cu);
19f392bc 16204 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16205
f55ee35c
JK
16206 return set_die_type (die, type, cu);
16207}
16208
5d7cb8df
JK
16209/* Read a Fortran module. */
16210
16211static void
16212read_module (struct die_info *die, struct dwarf2_cu *cu)
16213{
16214 struct die_info *child_die = die->child;
530e8392
KB
16215 struct type *type;
16216
16217 type = read_type_die (die, cu);
16218 new_symbol (die, type, cu);
5d7cb8df 16219
5d7cb8df
JK
16220 while (child_die && child_die->tag)
16221 {
16222 process_die (child_die, cu);
16223 child_die = sibling_die (child_die);
16224 }
16225}
16226
38d518c9
EZ
16227/* Return the name of the namespace represented by DIE. Set
16228 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16229 namespace. */
16230
16231static const char *
e142c38c 16232namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16233{
16234 struct die_info *current_die;
16235 const char *name = NULL;
16236
16237 /* Loop through the extensions until we find a name. */
16238
16239 for (current_die = die;
16240 current_die != NULL;
f2f0e013 16241 current_die = dwarf2_extension (die, &cu))
38d518c9 16242 {
96553a0c
DE
16243 /* We don't use dwarf2_name here so that we can detect the absence
16244 of a name -> anonymous namespace. */
7d45c7c3 16245 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16246
38d518c9
EZ
16247 if (name != NULL)
16248 break;
16249 }
16250
16251 /* Is it an anonymous namespace? */
16252
16253 *is_anonymous = (name == NULL);
16254 if (*is_anonymous)
2b1dbab0 16255 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16256
16257 return name;
d9fa45fe
DC
16258}
16259
c906108c
SS
16260/* Extract all information from a DW_TAG_pointer_type DIE and add to
16261 the user defined type vector. */
16262
f792889a 16263static struct type *
e7c27a73 16264read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16265{
518817b3
SM
16266 struct gdbarch *gdbarch
16267 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16268 struct comp_unit_head *cu_header = &cu->header;
c906108c 16269 struct type *type;
8b2dbe47
KB
16270 struct attribute *attr_byte_size;
16271 struct attribute *attr_address_class;
16272 int byte_size, addr_class;
7e314c57
JK
16273 struct type *target_type;
16274
16275 target_type = die_type (die, cu);
c906108c 16276
7e314c57
JK
16277 /* The die_type call above may have already set the type for this DIE. */
16278 type = get_die_type (die, cu);
16279 if (type)
16280 return type;
16281
16282 type = lookup_pointer_type (target_type);
8b2dbe47 16283
e142c38c 16284 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16285 if (attr_byte_size)
16286 byte_size = DW_UNSND (attr_byte_size);
c906108c 16287 else
8b2dbe47
KB
16288 byte_size = cu_header->addr_size;
16289
e142c38c 16290 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16291 if (attr_address_class)
16292 addr_class = DW_UNSND (attr_address_class);
16293 else
16294 addr_class = DW_ADDR_none;
16295
2b4424c3
TT
16296 ULONGEST alignment = get_alignment (cu, die);
16297
16298 /* If the pointer size, alignment, or address class is different
16299 than the default, create a type variant marked as such and set
16300 the length accordingly. */
16301 if (TYPE_LENGTH (type) != byte_size
16302 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16303 && alignment != TYPE_RAW_ALIGN (type))
16304 || addr_class != DW_ADDR_none)
c906108c 16305 {
5e2b427d 16306 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16307 {
16308 int type_flags;
16309
849957d9 16310 type_flags = gdbarch_address_class_type_flags
5e2b427d 16311 (gdbarch, byte_size, addr_class);
876cecd0
TT
16312 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16313 == 0);
8b2dbe47
KB
16314 type = make_type_with_address_space (type, type_flags);
16315 }
16316 else if (TYPE_LENGTH (type) != byte_size)
16317 {
b98664d3 16318 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16319 }
2b4424c3
TT
16320 else if (TYPE_RAW_ALIGN (type) != alignment)
16321 {
b98664d3 16322 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16323 " - DIE at %s [in module %s]"),
16324 sect_offset_str (die->sect_off),
16325 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16326 }
6e70227d 16327 else
9a619af0
MS
16328 {
16329 /* Should we also complain about unhandled address classes? */
16330 }
c906108c 16331 }
8b2dbe47
KB
16332
16333 TYPE_LENGTH (type) = byte_size;
2b4424c3 16334 set_type_align (type, alignment);
f792889a 16335 return set_die_type (die, type, cu);
c906108c
SS
16336}
16337
16338/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16339 the user defined type vector. */
16340
f792889a 16341static struct type *
e7c27a73 16342read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16343{
16344 struct type *type;
16345 struct type *to_type;
16346 struct type *domain;
16347
e7c27a73
DJ
16348 to_type = die_type (die, cu);
16349 domain = die_containing_type (die, cu);
0d5de010 16350
7e314c57
JK
16351 /* The calls above may have already set the type for this DIE. */
16352 type = get_die_type (die, cu);
16353 if (type)
16354 return type;
16355
0d5de010
DJ
16356 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16357 type = lookup_methodptr_type (to_type);
7078baeb
TT
16358 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16359 {
518817b3
SM
16360 struct type *new_type
16361 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16362
16363 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16364 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16365 TYPE_VARARGS (to_type));
16366 type = lookup_methodptr_type (new_type);
16367 }
0d5de010
DJ
16368 else
16369 type = lookup_memberptr_type (to_type, domain);
c906108c 16370
f792889a 16371 return set_die_type (die, type, cu);
c906108c
SS
16372}
16373
4297a3f0 16374/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16375 the user defined type vector. */
16376
f792889a 16377static struct type *
4297a3f0
AV
16378read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16379 enum type_code refcode)
c906108c 16380{
e7c27a73 16381 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16382 struct type *type, *target_type;
c906108c
SS
16383 struct attribute *attr;
16384
4297a3f0
AV
16385 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16386
7e314c57
JK
16387 target_type = die_type (die, cu);
16388
16389 /* The die_type call above may have already set the type for this DIE. */
16390 type = get_die_type (die, cu);
16391 if (type)
16392 return type;
16393
4297a3f0 16394 type = lookup_reference_type (target_type, refcode);
e142c38c 16395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16396 if (attr != nullptr)
c906108c
SS
16397 {
16398 TYPE_LENGTH (type) = DW_UNSND (attr);
16399 }
16400 else
16401 {
107d2387 16402 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16403 }
2b4424c3 16404 maybe_set_alignment (cu, die, type);
f792889a 16405 return set_die_type (die, type, cu);
c906108c
SS
16406}
16407
cf363f18
MW
16408/* Add the given cv-qualifiers to the element type of the array. GCC
16409 outputs DWARF type qualifiers that apply to an array, not the
16410 element type. But GDB relies on the array element type to carry
16411 the cv-qualifiers. This mimics section 6.7.3 of the C99
16412 specification. */
16413
16414static struct type *
16415add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16416 struct type *base_type, int cnst, int voltl)
16417{
16418 struct type *el_type, *inner_array;
16419
16420 base_type = copy_type (base_type);
16421 inner_array = base_type;
16422
16423 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16424 {
16425 TYPE_TARGET_TYPE (inner_array) =
16426 copy_type (TYPE_TARGET_TYPE (inner_array));
16427 inner_array = TYPE_TARGET_TYPE (inner_array);
16428 }
16429
16430 el_type = TYPE_TARGET_TYPE (inner_array);
16431 cnst |= TYPE_CONST (el_type);
16432 voltl |= TYPE_VOLATILE (el_type);
16433 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16434
16435 return set_die_type (die, base_type, cu);
16436}
16437
f792889a 16438static struct type *
e7c27a73 16439read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16440{
f792889a 16441 struct type *base_type, *cv_type;
c906108c 16442
e7c27a73 16443 base_type = die_type (die, cu);
7e314c57
JK
16444
16445 /* The die_type call above may have already set the type for this DIE. */
16446 cv_type = get_die_type (die, cu);
16447 if (cv_type)
16448 return cv_type;
16449
2f608a3a
KW
16450 /* In case the const qualifier is applied to an array type, the element type
16451 is so qualified, not the array type (section 6.7.3 of C99). */
16452 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16453 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16454
f792889a
DJ
16455 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16456 return set_die_type (die, cv_type, cu);
c906108c
SS
16457}
16458
f792889a 16459static struct type *
e7c27a73 16460read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16461{
f792889a 16462 struct type *base_type, *cv_type;
c906108c 16463
e7c27a73 16464 base_type = die_type (die, cu);
7e314c57
JK
16465
16466 /* The die_type call above may have already set the type for this DIE. */
16467 cv_type = get_die_type (die, cu);
16468 if (cv_type)
16469 return cv_type;
16470
cf363f18
MW
16471 /* In case the volatile qualifier is applied to an array type, the
16472 element type is so qualified, not the array type (section 6.7.3
16473 of C99). */
16474 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16475 return add_array_cv_type (die, cu, base_type, 0, 1);
16476
f792889a
DJ
16477 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16478 return set_die_type (die, cv_type, cu);
c906108c
SS
16479}
16480
06d66ee9
TT
16481/* Handle DW_TAG_restrict_type. */
16482
16483static struct type *
16484read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16485{
16486 struct type *base_type, *cv_type;
16487
16488 base_type = die_type (die, cu);
16489
16490 /* The die_type call above may have already set the type for this DIE. */
16491 cv_type = get_die_type (die, cu);
16492 if (cv_type)
16493 return cv_type;
16494
16495 cv_type = make_restrict_type (base_type);
16496 return set_die_type (die, cv_type, cu);
16497}
16498
a2c2acaf
MW
16499/* Handle DW_TAG_atomic_type. */
16500
16501static struct type *
16502read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16503{
16504 struct type *base_type, *cv_type;
16505
16506 base_type = die_type (die, cu);
16507
16508 /* The die_type call above may have already set the type for this DIE. */
16509 cv_type = get_die_type (die, cu);
16510 if (cv_type)
16511 return cv_type;
16512
16513 cv_type = make_atomic_type (base_type);
16514 return set_die_type (die, cv_type, cu);
16515}
16516
c906108c
SS
16517/* Extract all information from a DW_TAG_string_type DIE and add to
16518 the user defined type vector. It isn't really a user defined type,
16519 but it behaves like one, with other DIE's using an AT_user_def_type
16520 attribute to reference it. */
16521
f792889a 16522static struct type *
e7c27a73 16523read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16524{
518817b3 16525 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16526 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16527 struct type *type, *range_type, *index_type, *char_type;
16528 struct attribute *attr;
216a7e6b
AB
16529 struct dynamic_prop prop;
16530 bool length_is_constant = true;
16531 LONGEST length;
16532
16533 /* There are a couple of places where bit sizes might be made use of
16534 when parsing a DW_TAG_string_type, however, no producer that we know
16535 of make use of these. Handling bit sizes that are a multiple of the
16536 byte size is easy enough, but what about other bit sizes? Lets deal
16537 with that problem when we have to. Warn about these attributes being
16538 unsupported, then parse the type and ignore them like we always
16539 have. */
16540 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16541 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16542 {
16543 static bool warning_printed = false;
16544 if (!warning_printed)
16545 {
16546 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16547 "currently supported on DW_TAG_string_type."));
16548 warning_printed = true;
16549 }
16550 }
c906108c 16551
e142c38c 16552 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16553 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16554 {
16555 /* The string length describes the location at which the length of
16556 the string can be found. The size of the length field can be
16557 specified with one of the attributes below. */
16558 struct type *prop_type;
16559 struct attribute *len
16560 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16561 if (len == nullptr)
16562 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16563 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16564 {
16565 /* Pass 0 as the default as we know this attribute is constant
16566 and the default value will not be returned. */
16567 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
09ba997f 16568 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16569 }
16570 else
16571 {
16572 /* If the size is not specified then we assume it is the size of
16573 an address on this target. */
09ba997f 16574 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16575 }
16576
16577 /* Convert the attribute into a dynamic property. */
16578 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16579 length = 1;
16580 else
16581 length_is_constant = false;
16582 }
16583 else if (attr != nullptr)
16584 {
16585 /* This DW_AT_string_length just contains the length with no
16586 indirection. There's no need to create a dynamic property in this
16587 case. Pass 0 for the default value as we know it will not be
16588 returned in this case. */
16589 length = dwarf2_get_attr_constant_value (attr, 0);
16590 }
16591 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16592 {
216a7e6b
AB
16593 /* We don't currently support non-constant byte sizes for strings. */
16594 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16595 }
16596 else
16597 {
216a7e6b
AB
16598 /* Use 1 as a fallback length if we have nothing else. */
16599 length = 1;
c906108c 16600 }
6ccb9162 16601
46bf5051 16602 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16603 if (length_is_constant)
16604 range_type = create_static_range_type (NULL, index_type, 1, length);
16605 else
16606 {
16607 struct dynamic_prop low_bound;
16608
16609 low_bound.kind = PROP_CONST;
16610 low_bound.data.const_val = 1;
16611 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16612 }
3b7538c0
UW
16613 char_type = language_string_char_type (cu->language_defn, gdbarch);
16614 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16615
f792889a 16616 return set_die_type (die, type, cu);
c906108c
SS
16617}
16618
4d804846
JB
16619/* Assuming that DIE corresponds to a function, returns nonzero
16620 if the function is prototyped. */
16621
16622static int
16623prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16624{
16625 struct attribute *attr;
16626
16627 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16628 if (attr && (DW_UNSND (attr) != 0))
16629 return 1;
16630
16631 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16632 is only meaningful for C, but the concept also extends to other
4d804846
JB
16633 languages that allow unprototyped functions (Eg: Objective C).
16634 For all other languages, assume that functions are always
16635 prototyped. */
16636 if (cu->language != language_c
16637 && cu->language != language_objc
16638 && cu->language != language_opencl)
16639 return 1;
16640
16641 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16642 prototyped and unprototyped functions; default to prototyped,
16643 since that is more common in modern code (and RealView warns
16644 about unprototyped functions). */
16645 if (producer_is_realview (cu->producer))
16646 return 1;
16647
16648 return 0;
16649}
16650
c906108c
SS
16651/* Handle DIES due to C code like:
16652
16653 struct foo
c5aa993b
JM
16654 {
16655 int (*funcp)(int a, long l);
16656 int b;
16657 };
c906108c 16658
0963b4bd 16659 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16660
f792889a 16661static struct type *
e7c27a73 16662read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16663{
518817b3 16664 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16665 struct type *type; /* Type that this function returns. */
16666 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16667 struct attribute *attr;
16668
e7c27a73 16669 type = die_type (die, cu);
7e314c57
JK
16670
16671 /* The die_type call above may have already set the type for this DIE. */
16672 ftype = get_die_type (die, cu);
16673 if (ftype)
16674 return ftype;
16675
0c8b41f1 16676 ftype = lookup_function_type (type);
c906108c 16677
4d804846 16678 if (prototyped_function_p (die, cu))
a6c727b2 16679 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16680
c055b101
CV
16681 /* Store the calling convention in the type if it's available in
16682 the subroutine die. Otherwise set the calling convention to
16683 the default value DW_CC_normal. */
16684 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16685 if (attr != nullptr
16686 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16687 TYPE_CALLING_CONVENTION (ftype)
16688 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16689 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16690 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16691 else
16692 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16693
743649fd
MW
16694 /* Record whether the function returns normally to its caller or not
16695 if the DWARF producer set that information. */
16696 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16697 if (attr && (DW_UNSND (attr) != 0))
16698 TYPE_NO_RETURN (ftype) = 1;
16699
76c10ea2
GM
16700 /* We need to add the subroutine type to the die immediately so
16701 we don't infinitely recurse when dealing with parameters
0963b4bd 16702 declared as the same subroutine type. */
76c10ea2 16703 set_die_type (die, ftype, cu);
6e70227d 16704
639d11d3 16705 if (die->child != NULL)
c906108c 16706 {
bb5ed363 16707 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16708 struct die_info *child_die;
8072405b 16709 int nparams, iparams;
c906108c
SS
16710
16711 /* Count the number of parameters.
16712 FIXME: GDB currently ignores vararg functions, but knows about
16713 vararg member functions. */
8072405b 16714 nparams = 0;
639d11d3 16715 child_die = die->child;
c906108c
SS
16716 while (child_die && child_die->tag)
16717 {
16718 if (child_die->tag == DW_TAG_formal_parameter)
16719 nparams++;
16720 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16721 TYPE_VARARGS (ftype) = 1;
c906108c
SS
16722 child_die = sibling_die (child_die);
16723 }
16724
16725 /* Allocate storage for parameters and fill them in. */
16726 TYPE_NFIELDS (ftype) = nparams;
16727 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16728 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16729
8072405b
JK
16730 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16731 even if we error out during the parameters reading below. */
16732 for (iparams = 0; iparams < nparams; iparams++)
16733 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16734
16735 iparams = 0;
639d11d3 16736 child_die = die->child;
c906108c
SS
16737 while (child_die && child_die->tag)
16738 {
16739 if (child_die->tag == DW_TAG_formal_parameter)
16740 {
3ce3b1ba
PA
16741 struct type *arg_type;
16742
16743 /* DWARF version 2 has no clean way to discern C++
16744 static and non-static member functions. G++ helps
16745 GDB by marking the first parameter for non-static
16746 member functions (which is the this pointer) as
16747 artificial. We pass this information to
16748 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16749
16750 DWARF version 3 added DW_AT_object_pointer, which GCC
16751 4.5 does not yet generate. */
e142c38c 16752 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16753 if (attr != nullptr)
c906108c
SS
16754 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16755 else
9c37b5ae 16756 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16757 arg_type = die_type (child_die, cu);
16758
16759 /* RealView does not mark THIS as const, which the testsuite
16760 expects. GCC marks THIS as const in method definitions,
16761 but not in the class specifications (GCC PR 43053). */
16762 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16763 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16764 {
16765 int is_this = 0;
16766 struct dwarf2_cu *arg_cu = cu;
16767 const char *name = dwarf2_name (child_die, cu);
16768
16769 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16770 if (attr != nullptr)
3ce3b1ba
PA
16771 {
16772 /* If the compiler emits this, use it. */
16773 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16774 is_this = 1;
16775 }
16776 else if (name && strcmp (name, "this") == 0)
16777 /* Function definitions will have the argument names. */
16778 is_this = 1;
16779 else if (name == NULL && iparams == 0)
16780 /* Declarations may not have the names, so like
16781 elsewhere in GDB, assume an artificial first
16782 argument is "this". */
16783 is_this = 1;
16784
16785 if (is_this)
16786 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16787 arg_type, 0);
16788 }
16789
16790 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16791 iparams++;
16792 }
16793 child_die = sibling_die (child_die);
16794 }
16795 }
16796
76c10ea2 16797 return ftype;
c906108c
SS
16798}
16799
f792889a 16800static struct type *
e7c27a73 16801read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16802{
518817b3 16803 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16804 const char *name = NULL;
3c8e0968 16805 struct type *this_type, *target_type;
c906108c 16806
94af9270 16807 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16808 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16809 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16810 set_die_type (die, this_type, cu);
3c8e0968
DE
16811 target_type = die_type (die, cu);
16812 if (target_type != this_type)
16813 TYPE_TARGET_TYPE (this_type) = target_type;
16814 else
16815 {
16816 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16817 spec and cause infinite loops in GDB. */
b98664d3 16818 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16819 "- DIE at %s [in module %s]"),
16820 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16821 TYPE_TARGET_TYPE (this_type) = NULL;
16822 }
f792889a 16823 return this_type;
c906108c
SS
16824}
16825
9b790ce7
UW
16826/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16827 (which may be different from NAME) to the architecture back-end to allow
16828 it to guess the correct format if necessary. */
16829
16830static struct type *
16831dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16832 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16833{
16834 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16835 const struct floatformat **format;
16836 struct type *type;
16837
16838 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16839 if (format)
103a685e 16840 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16841 else
77b7c781 16842 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16843
16844 return type;
16845}
16846
eb77c9df
AB
16847/* Allocate an integer type of size BITS and name NAME. */
16848
16849static struct type *
16850dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16851 int bits, int unsigned_p, const char *name)
16852{
16853 struct type *type;
16854
16855 /* Versions of Intel's C Compiler generate an integer type called "void"
16856 instead of using DW_TAG_unspecified_type. This has been seen on
16857 at least versions 14, 17, and 18. */
35ee2dc2
AB
16858 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16859 && strcmp (name, "void") == 0)
eb77c9df
AB
16860 type = objfile_type (objfile)->builtin_void;
16861 else
16862 type = init_integer_type (objfile, bits, unsigned_p, name);
16863
16864 return type;
16865}
16866
8bdc1658
AB
16867/* Initialise and return a floating point type of size BITS suitable for
16868 use as a component of a complex number. The NAME_HINT is passed through
16869 when initialising the floating point type and is the name of the complex
16870 type.
16871
16872 As DWARF doesn't currently provide an explicit name for the components
16873 of a complex number, but it can be helpful to have these components
16874 named, we try to select a suitable name based on the size of the
16875 component. */
16876static struct type *
16877dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16878 struct objfile *objfile,
103a685e
TT
16879 int bits, const char *name_hint,
16880 enum bfd_endian byte_order)
8bdc1658
AB
16881{
16882 gdbarch *gdbarch = get_objfile_arch (objfile);
16883 struct type *tt = nullptr;
16884
35add35e
AB
16885 /* Try to find a suitable floating point builtin type of size BITS.
16886 We're going to use the name of this type as the name for the complex
16887 target type that we are about to create. */
1db455a7 16888 switch (cu->language)
8bdc1658 16889 {
1db455a7
AB
16890 case language_fortran:
16891 switch (bits)
16892 {
16893 case 32:
16894 tt = builtin_f_type (gdbarch)->builtin_real;
16895 break;
16896 case 64:
16897 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16898 break;
16899 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16900 case 128:
16901 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16902 break;
16903 }
8bdc1658 16904 break;
1db455a7
AB
16905 default:
16906 switch (bits)
16907 {
16908 case 32:
16909 tt = builtin_type (gdbarch)->builtin_float;
16910 break;
16911 case 64:
16912 tt = builtin_type (gdbarch)->builtin_double;
16913 break;
16914 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16915 case 128:
16916 tt = builtin_type (gdbarch)->builtin_long_double;
16917 break;
16918 }
8bdc1658
AB
16919 break;
16920 }
16921
35add35e
AB
16922 /* If the type we found doesn't match the size we were looking for, then
16923 pretend we didn't find a type at all, the complex target type we
16924 create will then be nameless. */
a12e5744 16925 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16926 tt = nullptr;
16927
8bdc1658 16928 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16929 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16930}
16931
c906108c
SS
16932/* Find a representation of a given base type and install
16933 it in the TYPE field of the die. */
16934
f792889a 16935static struct type *
e7c27a73 16936read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16937{
518817b3 16938 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16939 struct type *type;
16940 struct attribute *attr;
19f392bc 16941 int encoding = 0, bits = 0;
15d034d0 16942 const char *name;
34877895 16943 gdbarch *arch;
c906108c 16944
e142c38c 16945 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16946 if (attr != nullptr)
34877895 16947 encoding = DW_UNSND (attr);
e142c38c 16948 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16949 if (attr != nullptr)
34877895 16950 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16951 name = dwarf2_name (die, cu);
6ccb9162 16952 if (!name)
34877895 16953 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16954
16955 arch = get_objfile_arch (objfile);
16956 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16957
34877895
PJ
16958 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16959 if (attr)
103a685e
TT
16960 {
16961 int endianity = DW_UNSND (attr);
16962
16963 switch (endianity)
16964 {
16965 case DW_END_big:
16966 byte_order = BFD_ENDIAN_BIG;
16967 break;
16968 case DW_END_little:
16969 byte_order = BFD_ENDIAN_LITTLE;
16970 break;
16971 default:
16972 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16973 break;
16974 }
16975 }
6ccb9162
UW
16976
16977 switch (encoding)
c906108c 16978 {
6ccb9162
UW
16979 case DW_ATE_address:
16980 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16981 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16982 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16983 break;
16984 case DW_ATE_boolean:
19f392bc 16985 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16986 break;
16987 case DW_ATE_complex_float:
103a685e
TT
16988 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16989 byte_order);
19f392bc 16990 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16991 break;
16992 case DW_ATE_decimal_float:
19f392bc 16993 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16994 break;
16995 case DW_ATE_float:
103a685e 16996 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16997 break;
16998 case DW_ATE_signed:
eb77c9df 16999 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17000 break;
17001 case DW_ATE_unsigned:
3b2b8fea
TT
17002 if (cu->language == language_fortran
17003 && name
61012eef 17004 && startswith (name, "character("))
19f392bc
UW
17005 type = init_character_type (objfile, bits, 1, name);
17006 else
eb77c9df 17007 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17008 break;
17009 case DW_ATE_signed_char:
6e70227d 17010 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17011 || cu->language == language_pascal
17012 || cu->language == language_fortran)
19f392bc
UW
17013 type = init_character_type (objfile, bits, 0, name);
17014 else
eb77c9df 17015 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17016 break;
17017 case DW_ATE_unsigned_char:
868a0084 17018 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17019 || cu->language == language_pascal
c44af4eb
TT
17020 || cu->language == language_fortran
17021 || cu->language == language_rust)
19f392bc
UW
17022 type = init_character_type (objfile, bits, 1, name);
17023 else
eb77c9df 17024 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17025 break;
75079b2b 17026 case DW_ATE_UTF:
53e710ac 17027 {
53e710ac
PA
17028 if (bits == 16)
17029 type = builtin_type (arch)->builtin_char16;
17030 else if (bits == 32)
17031 type = builtin_type (arch)->builtin_char32;
17032 else
17033 {
b98664d3 17034 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17035 bits);
eb77c9df 17036 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17037 }
17038 return set_die_type (die, type, cu);
17039 }
75079b2b
TT
17040 break;
17041
6ccb9162 17042 default:
b98664d3 17043 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17044 dwarf_type_encoding_name (encoding));
77b7c781 17045 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17046 break;
c906108c 17047 }
6ccb9162 17048
0114d602 17049 if (name && strcmp (name, "char") == 0)
876cecd0 17050 TYPE_NOSIGN (type) = 1;
0114d602 17051
2b4424c3
TT
17052 maybe_set_alignment (cu, die, type);
17053
103a685e 17054 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17055
f792889a 17056 return set_die_type (die, type, cu);
c906108c
SS
17057}
17058
80180f79
SA
17059/* Parse dwarf attribute if it's a block, reference or constant and put the
17060 resulting value of the attribute into struct bound_prop.
17061 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17062
17063static int
17064attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17065 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17066 struct type *default_type)
80180f79
SA
17067{
17068 struct dwarf2_property_baton *baton;
518817b3
SM
17069 struct obstack *obstack
17070 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17071
9a49df9d
AB
17072 gdb_assert (default_type != NULL);
17073
80180f79
SA
17074 if (attr == NULL || prop == NULL)
17075 return 0;
17076
4fc6c0d5 17077 if (attr->form_is_block ())
80180f79 17078 {
8d749320 17079 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17080 baton->property_type = default_type;
80180f79
SA
17081 baton->locexpr.per_cu = cu->per_cu;
17082 baton->locexpr.size = DW_BLOCK (attr)->size;
17083 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17084 switch (attr->name)
17085 {
17086 case DW_AT_string_length:
17087 baton->locexpr.is_reference = true;
17088 break;
17089 default:
17090 baton->locexpr.is_reference = false;
17091 break;
17092 }
80180f79
SA
17093 prop->data.baton = baton;
17094 prop->kind = PROP_LOCEXPR;
17095 gdb_assert (prop->data.baton != NULL);
17096 }
cd6c91b4 17097 else if (attr->form_is_ref ())
80180f79
SA
17098 {
17099 struct dwarf2_cu *target_cu = cu;
17100 struct die_info *target_die;
17101 struct attribute *target_attr;
17102
17103 target_die = follow_die_ref (die, attr, &target_cu);
17104 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17105 if (target_attr == NULL)
17106 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17107 target_cu);
80180f79
SA
17108 if (target_attr == NULL)
17109 return 0;
17110
df25ebbd 17111 switch (target_attr->name)
80180f79 17112 {
df25ebbd 17113 case DW_AT_location:
cd6c91b4 17114 if (target_attr->form_is_section_offset ())
df25ebbd 17115 {
8d749320 17116 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17117 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17118 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17119 prop->data.baton = baton;
17120 prop->kind = PROP_LOCLIST;
17121 gdb_assert (prop->data.baton != NULL);
17122 }
4fc6c0d5 17123 else if (target_attr->form_is_block ())
df25ebbd 17124 {
8d749320 17125 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17126 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17127 baton->locexpr.per_cu = cu->per_cu;
17128 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17129 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17130 baton->locexpr.is_reference = true;
df25ebbd
JB
17131 prop->data.baton = baton;
17132 prop->kind = PROP_LOCEXPR;
17133 gdb_assert (prop->data.baton != NULL);
17134 }
17135 else
17136 {
17137 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17138 "dynamic property");
17139 return 0;
17140 }
17141 break;
17142 case DW_AT_data_member_location:
17143 {
17144 LONGEST offset;
17145
17146 if (!handle_data_member_location (target_die, target_cu,
17147 &offset))
17148 return 0;
17149
8d749320 17150 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17151 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17152 target_cu);
df25ebbd
JB
17153 baton->offset_info.offset = offset;
17154 baton->offset_info.type = die_type (target_die, target_cu);
17155 prop->data.baton = baton;
17156 prop->kind = PROP_ADDR_OFFSET;
17157 break;
17158 }
80180f79
SA
17159 }
17160 }
cd6c91b4 17161 else if (attr->form_is_constant ())
80180f79
SA
17162 {
17163 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17164 prop->kind = PROP_CONST;
17165 }
17166 else
17167 {
17168 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17169 dwarf2_name (die, cu));
17170 return 0;
17171 }
17172
17173 return 1;
17174}
17175
09ba997f 17176/* See read.h. */
9a49df9d 17177
09ba997f
TT
17178struct type *
17179dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17180{
09ba997f 17181 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17182 struct type *int_type;
17183
17184 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17185#define TRY_TYPE(F) \
17186 int_type = (unsigned_p \
17187 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17188 : objfile_type (objfile)->builtin_ ## F); \
17189 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17190 return int_type
17191
17192 TRY_TYPE (char);
17193 TRY_TYPE (short);
17194 TRY_TYPE (int);
17195 TRY_TYPE (long);
17196 TRY_TYPE (long_long);
17197
17198#undef TRY_TYPE
17199
17200 gdb_assert_not_reached ("unable to find suitable integer type");
17201}
17202
09ba997f 17203/* See read.h. */
11a8b164 17204
09ba997f
TT
17205struct type *
17206dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17207{
09ba997f
TT
17208 int addr_size = this->addr_size ();
17209 return int_type (addr_size, unsigned_p);
11a8b164
AB
17210}
17211
b86352cf
AB
17212/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17213 present (which is valid) then compute the default type based on the
17214 compilation units address size. */
17215
17216static struct type *
17217read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17218{
17219 struct type *index_type = die_type (die, cu);
17220
17221 /* Dwarf-2 specifications explicitly allows to create subrange types
17222 without specifying a base type.
17223 In that case, the base type must be set to the type of
17224 the lower bound, upper bound or count, in that order, if any of these
17225 three attributes references an object that has a type.
17226 If no base type is found, the Dwarf-2 specifications say that
17227 a signed integer type of size equal to the size of an address should
17228 be used.
17229 For the following C code: `extern char gdb_int [];'
17230 GCC produces an empty range DIE.
17231 FIXME: muller/2010-05-28: Possible references to object for low bound,
17232 high bound or count are not yet handled by this code. */
17233 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17234 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17235
17236 return index_type;
17237}
17238
a02abb62
JB
17239/* Read the given DW_AT_subrange DIE. */
17240
f792889a 17241static struct type *
a02abb62
JB
17242read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17243{
4c9ad8c2 17244 struct type *base_type, *orig_base_type;
a02abb62
JB
17245 struct type *range_type;
17246 struct attribute *attr;
729efb13 17247 struct dynamic_prop low, high;
4fae6e18 17248 int low_default_is_valid;
c451ebe5 17249 int high_bound_is_count = 0;
15d034d0 17250 const char *name;
d359392f 17251 ULONGEST negative_mask;
e77813c8 17252
b86352cf
AB
17253 orig_base_type = read_subrange_index_type (die, cu);
17254
4c9ad8c2
TT
17255 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17256 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17257 creating the range type, but we use the result of check_typedef
17258 when examining properties of the type. */
17259 base_type = check_typedef (orig_base_type);
a02abb62 17260
7e314c57
JK
17261 /* The die_type call above may have already set the type for this DIE. */
17262 range_type = get_die_type (die, cu);
17263 if (range_type)
17264 return range_type;
17265
729efb13
SA
17266 low.kind = PROP_CONST;
17267 high.kind = PROP_CONST;
17268 high.data.const_val = 0;
17269
4fae6e18
JK
17270 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17271 omitting DW_AT_lower_bound. */
17272 switch (cu->language)
6e70227d 17273 {
4fae6e18
JK
17274 case language_c:
17275 case language_cplus:
729efb13 17276 low.data.const_val = 0;
4fae6e18
JK
17277 low_default_is_valid = 1;
17278 break;
17279 case language_fortran:
729efb13 17280 low.data.const_val = 1;
4fae6e18
JK
17281 low_default_is_valid = 1;
17282 break;
17283 case language_d:
4fae6e18 17284 case language_objc:
c44af4eb 17285 case language_rust:
729efb13 17286 low.data.const_val = 0;
4fae6e18
JK
17287 low_default_is_valid = (cu->header.version >= 4);
17288 break;
17289 case language_ada:
17290 case language_m2:
17291 case language_pascal:
729efb13 17292 low.data.const_val = 1;
4fae6e18
JK
17293 low_default_is_valid = (cu->header.version >= 4);
17294 break;
17295 default:
729efb13 17296 low.data.const_val = 0;
4fae6e18
JK
17297 low_default_is_valid = 0;
17298 break;
a02abb62
JB
17299 }
17300
e142c38c 17301 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17302 if (attr != nullptr)
9a49df9d 17303 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17304 else if (!low_default_is_valid)
b98664d3 17305 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17306 "- DIE at %s [in module %s]"),
17307 sect_offset_str (die->sect_off),
518817b3 17308 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17309
506f5c41
TV
17310 struct attribute *attr_ub, *attr_count;
17311 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17312 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17313 {
506f5c41 17314 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17315 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17316 {
c451ebe5
SA
17317 /* If bounds are constant do the final calculation here. */
17318 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17319 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17320 else
17321 high_bound_is_count = 1;
c2ff108b 17322 }
506f5c41
TV
17323 else
17324 {
17325 if (attr_ub != NULL)
17326 complaint (_("Unresolved DW_AT_upper_bound "
17327 "- DIE at %s [in module %s]"),
17328 sect_offset_str (die->sect_off),
17329 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17330 if (attr_count != NULL)
17331 complaint (_("Unresolved DW_AT_count "
17332 "- DIE at %s [in module %s]"),
17333 sect_offset_str (die->sect_off),
17334 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17335 }
e77813c8 17336 }
a02abb62 17337
4e962e74
TT
17338 LONGEST bias = 0;
17339 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17340 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17341 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17342
dbb9c2b1
JB
17343 /* Normally, the DWARF producers are expected to use a signed
17344 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17345 But this is unfortunately not always the case, as witnessed
17346 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17347 is used instead. To work around that ambiguity, we treat
17348 the bounds as signed, and thus sign-extend their values, when
17349 the base type is signed. */
6e70227d 17350 negative_mask =
d359392f 17351 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17352 if (low.kind == PROP_CONST
17353 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17354 low.data.const_val |= negative_mask;
17355 if (high.kind == PROP_CONST
17356 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17357 high.data.const_val |= negative_mask;
43bbcdc2 17358
5bbd8269
AB
17359 /* Check for bit and byte strides. */
17360 struct dynamic_prop byte_stride_prop;
17361 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17362 if (attr_byte_stride != nullptr)
17363 {
09ba997f 17364 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17365 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17366 prop_type);
17367 }
17368
17369 struct dynamic_prop bit_stride_prop;
17370 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17371 if (attr_bit_stride != nullptr)
17372 {
17373 /* It only makes sense to have either a bit or byte stride. */
17374 if (attr_byte_stride != nullptr)
17375 {
17376 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17377 "- DIE at %s [in module %s]"),
17378 sect_offset_str (die->sect_off),
17379 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17380 attr_bit_stride = nullptr;
17381 }
17382 else
17383 {
09ba997f 17384 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17385 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17386 prop_type);
17387 }
17388 }
17389
17390 if (attr_byte_stride != nullptr
17391 || attr_bit_stride != nullptr)
17392 {
17393 bool byte_stride_p = (attr_byte_stride != nullptr);
17394 struct dynamic_prop *stride
17395 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17396
17397 range_type
17398 = create_range_type_with_stride (NULL, orig_base_type, &low,
17399 &high, bias, stride, byte_stride_p);
17400 }
17401 else
17402 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17403
c451ebe5
SA
17404 if (high_bound_is_count)
17405 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17406
c2ff108b
JK
17407 /* Ada expects an empty array on no boundary attributes. */
17408 if (attr == NULL && cu->language != language_ada)
729efb13 17409 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17410
39cbfefa
DJ
17411 name = dwarf2_name (die, cu);
17412 if (name)
17413 TYPE_NAME (range_type) = name;
6e70227d 17414
e142c38c 17415 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17416 if (attr != nullptr)
a02abb62
JB
17417 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17418
2b4424c3
TT
17419 maybe_set_alignment (cu, die, range_type);
17420
7e314c57
JK
17421 set_die_type (die, range_type, cu);
17422
17423 /* set_die_type should be already done. */
b4ba55a1
JB
17424 set_descriptive_type (range_type, die, cu);
17425
7e314c57 17426 return range_type;
a02abb62 17427}
6e70227d 17428
f792889a 17429static struct type *
81a17f79
JB
17430read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17431{
17432 struct type *type;
81a17f79 17433
518817b3
SM
17434 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17435 NULL);
0114d602 17436 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17437
74a2f8ff 17438 /* In Ada, an unspecified type is typically used when the description
85102364 17439 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17440 such a type, we treat it as a stub, and try to resolve it later on,
17441 when needed. */
17442 if (cu->language == language_ada)
17443 TYPE_STUB (type) = 1;
17444
f792889a 17445 return set_die_type (die, type, cu);
81a17f79 17446}
a02abb62 17447
639d11d3
DC
17448/* Read a single die and all its descendents. Set the die's sibling
17449 field to NULL; set other fields in the die correctly, and set all
17450 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17451 location of the info_ptr after reading all of those dies. PARENT
17452 is the parent of the die in question. */
17453
17454static struct die_info *
dee91e82 17455read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17456 const gdb_byte *info_ptr,
17457 const gdb_byte **new_info_ptr,
dee91e82 17458 struct die_info *parent)
639d11d3
DC
17459{
17460 struct die_info *die;
d521ce57 17461 const gdb_byte *cur_ptr;
639d11d3 17462
3e225074 17463 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17464 if (die == NULL)
17465 {
17466 *new_info_ptr = cur_ptr;
17467 return NULL;
17468 }
93311388 17469 store_in_ref_table (die, reader->cu);
639d11d3 17470
3e225074 17471 if (die->has_children)
bf6af496 17472 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17473 else
17474 {
17475 die->child = NULL;
17476 *new_info_ptr = cur_ptr;
17477 }
17478
17479 die->sibling = NULL;
17480 die->parent = parent;
17481 return die;
17482}
17483
17484/* Read a die, all of its descendents, and all of its siblings; set
17485 all of the fields of all of the dies correctly. Arguments are as
17486 in read_die_and_children. */
17487
17488static struct die_info *
bf6af496 17489read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17490 const gdb_byte *info_ptr,
17491 const gdb_byte **new_info_ptr,
bf6af496 17492 struct die_info *parent)
639d11d3
DC
17493{
17494 struct die_info *first_die, *last_sibling;
d521ce57 17495 const gdb_byte *cur_ptr;
639d11d3 17496
c906108c 17497 cur_ptr = info_ptr;
639d11d3
DC
17498 first_die = last_sibling = NULL;
17499
17500 while (1)
c906108c 17501 {
639d11d3 17502 struct die_info *die
dee91e82 17503 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17504
1d325ec1 17505 if (die == NULL)
c906108c 17506 {
639d11d3
DC
17507 *new_info_ptr = cur_ptr;
17508 return first_die;
c906108c 17509 }
1d325ec1
DJ
17510
17511 if (!first_die)
17512 first_die = die;
c906108c 17513 else
1d325ec1
DJ
17514 last_sibling->sibling = die;
17515
17516 last_sibling = die;
c906108c 17517 }
c906108c
SS
17518}
17519
bf6af496
DE
17520/* Read a die, all of its descendents, and all of its siblings; set
17521 all of the fields of all of the dies correctly. Arguments are as
17522 in read_die_and_children.
17523 This the main entry point for reading a DIE and all its children. */
17524
17525static struct die_info *
17526read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17527 const gdb_byte *info_ptr,
17528 const gdb_byte **new_info_ptr,
bf6af496
DE
17529 struct die_info *parent)
17530{
17531 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17532 new_info_ptr, parent);
17533
b4f54984 17534 if (dwarf_die_debug)
bf6af496
DE
17535 {
17536 fprintf_unfiltered (gdb_stdlog,
17537 "Read die from %s@0x%x of %s:\n",
96b79293 17538 reader->die_section->get_name (),
bf6af496
DE
17539 (unsigned) (info_ptr - reader->die_section->buffer),
17540 bfd_get_filename (reader->abfd));
b4f54984 17541 dump_die (die, dwarf_die_debug);
bf6af496
DE
17542 }
17543
17544 return die;
17545}
17546
3019eac3
DE
17547/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17548 attributes.
17549 The caller is responsible for filling in the extra attributes
17550 and updating (*DIEP)->num_attrs.
17551 Set DIEP to point to a newly allocated die with its information,
3e225074 17552 except for its child, sibling, and parent fields. */
93311388 17553
d521ce57 17554static const gdb_byte *
3019eac3 17555read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17556 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17557 int num_extra_attrs)
93311388 17558{
b64f50a1 17559 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17560 struct abbrev_info *abbrev;
17561 struct die_info *die;
17562 struct dwarf2_cu *cu = reader->cu;
17563 bfd *abfd = reader->abfd;
17564
9c541725 17565 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17566 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17567 info_ptr += bytes_read;
17568 if (!abbrev_number)
17569 {
17570 *diep = NULL;
93311388
DE
17571 return info_ptr;
17572 }
17573
685af9cd 17574 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17575 if (!abbrev)
348e048f
DE
17576 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17577 abbrev_number,
17578 bfd_get_filename (abfd));
17579
3019eac3 17580 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17581 die->sect_off = sect_off;
93311388
DE
17582 die->tag = abbrev->tag;
17583 die->abbrev = abbrev_number;
3e225074 17584 die->has_children = abbrev->has_children;
93311388 17585
3019eac3
DE
17586 /* Make the result usable.
17587 The caller needs to update num_attrs after adding the extra
17588 attributes. */
93311388
DE
17589 die->num_attrs = abbrev->num_attrs;
17590
18a8505e 17591 std::vector<int> indexes_that_need_reprocess;
93311388 17592 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17593 {
17594 bool need_reprocess;
17595 info_ptr =
17596 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17597 info_ptr, &need_reprocess);
17598 if (need_reprocess)
17599 indexes_that_need_reprocess.push_back (i);
17600 }
17601
17602 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17603 if (attr != nullptr)
17604 cu->str_offsets_base = DW_UNSND (attr);
93311388 17605
18a8505e
AT
17606 auto maybe_addr_base = lookup_addr_base(die);
17607 if (maybe_addr_base.has_value ())
17608 cu->addr_base = *maybe_addr_base;
17609 for (int index : indexes_that_need_reprocess)
17610 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17611 *diep = die;
93311388
DE
17612 return info_ptr;
17613}
17614
3019eac3
DE
17615/* Read a die and all its attributes.
17616 Set DIEP to point to a newly allocated die with its information,
3e225074 17617 except for its child, sibling, and parent fields. */
3019eac3 17618
d521ce57 17619static const gdb_byte *
3019eac3 17620read_full_die (const struct die_reader_specs *reader,
3e225074 17621 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17622{
d521ce57 17623 const gdb_byte *result;
bf6af496 17624
3e225074 17625 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17626
b4f54984 17627 if (dwarf_die_debug)
bf6af496
DE
17628 {
17629 fprintf_unfiltered (gdb_stdlog,
17630 "Read die from %s@0x%x of %s:\n",
96b79293 17631 reader->die_section->get_name (),
bf6af496
DE
17632 (unsigned) (info_ptr - reader->die_section->buffer),
17633 bfd_get_filename (reader->abfd));
b4f54984 17634 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17635 }
17636
17637 return result;
3019eac3 17638}
433df2d4 17639\f
c906108c 17640
72bf9492
DJ
17641/* Returns nonzero if TAG represents a type that we might generate a partial
17642 symbol for. */
17643
17644static int
17645is_type_tag_for_partial (int tag)
17646{
17647 switch (tag)
17648 {
17649#if 0
17650 /* Some types that would be reasonable to generate partial symbols for,
17651 that we don't at present. */
17652 case DW_TAG_array_type:
17653 case DW_TAG_file_type:
17654 case DW_TAG_ptr_to_member_type:
17655 case DW_TAG_set_type:
17656 case DW_TAG_string_type:
17657 case DW_TAG_subroutine_type:
17658#endif
17659 case DW_TAG_base_type:
17660 case DW_TAG_class_type:
680b30c7 17661 case DW_TAG_interface_type:
72bf9492
DJ
17662 case DW_TAG_enumeration_type:
17663 case DW_TAG_structure_type:
17664 case DW_TAG_subrange_type:
17665 case DW_TAG_typedef:
17666 case DW_TAG_union_type:
17667 return 1;
17668 default:
17669 return 0;
17670 }
17671}
17672
17673/* Load all DIEs that are interesting for partial symbols into memory. */
17674
17675static struct partial_die_info *
dee91e82 17676load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17677 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17678{
dee91e82 17679 struct dwarf2_cu *cu = reader->cu;
518817b3 17680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17681 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17682 unsigned int bytes_read;
5afb4e99 17683 unsigned int load_all = 0;
72bf9492
DJ
17684 int nesting_level = 1;
17685
17686 parent_die = NULL;
17687 last_die = NULL;
17688
7adf1e79
DE
17689 gdb_assert (cu->per_cu != NULL);
17690 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17691 load_all = 1;
17692
72bf9492
DJ
17693 cu->partial_dies
17694 = htab_create_alloc_ex (cu->header.length / 12,
17695 partial_die_hash,
17696 partial_die_eq,
17697 NULL,
17698 &cu->comp_unit_obstack,
17699 hashtab_obstack_allocate,
17700 dummy_obstack_deallocate);
17701
72bf9492
DJ
17702 while (1)
17703 {
685af9cd 17704 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17705
17706 /* A NULL abbrev means the end of a series of children. */
17707 if (abbrev == NULL)
17708 {
17709 if (--nesting_level == 0)
cd9983dd
YQ
17710 return first_die;
17711
72bf9492
DJ
17712 info_ptr += bytes_read;
17713 last_die = parent_die;
17714 parent_die = parent_die->die_parent;
17715 continue;
17716 }
17717
98bfdba5
PA
17718 /* Check for template arguments. We never save these; if
17719 they're seen, we just mark the parent, and go on our way. */
17720 if (parent_die != NULL
17721 && cu->language == language_cplus
17722 && (abbrev->tag == DW_TAG_template_type_param
17723 || abbrev->tag == DW_TAG_template_value_param))
17724 {
17725 parent_die->has_template_arguments = 1;
17726
17727 if (!load_all)
17728 {
17729 /* We don't need a partial DIE for the template argument. */
dee91e82 17730 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17731 continue;
17732 }
17733 }
17734
0d99eb77 17735 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17736 Skip their other children. */
17737 if (!load_all
17738 && cu->language == language_cplus
17739 && parent_die != NULL
17740 && parent_die->tag == DW_TAG_subprogram)
17741 {
dee91e82 17742 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17743 continue;
17744 }
17745
5afb4e99
DJ
17746 /* Check whether this DIE is interesting enough to save. Normally
17747 we would not be interested in members here, but there may be
17748 later variables referencing them via DW_AT_specification (for
17749 static members). */
17750 if (!load_all
17751 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17752 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17753 && abbrev->tag != DW_TAG_enumerator
17754 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17755 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17756 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17757 && abbrev->tag != DW_TAG_variable
5afb4e99 17758 && abbrev->tag != DW_TAG_namespace
f55ee35c 17759 && abbrev->tag != DW_TAG_module
95554aad 17760 && abbrev->tag != DW_TAG_member
74921315
KS
17761 && abbrev->tag != DW_TAG_imported_unit
17762 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17763 {
17764 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17765 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17766 continue;
17767 }
17768
6f06d47b
YQ
17769 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17770 abbrev);
cd9983dd 17771
48fbe735 17772 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17773
17774 /* This two-pass algorithm for processing partial symbols has a
17775 high cost in cache pressure. Thus, handle some simple cases
17776 here which cover the majority of C partial symbols. DIEs
17777 which neither have specification tags in them, nor could have
17778 specification tags elsewhere pointing at them, can simply be
17779 processed and discarded.
17780
17781 This segment is also optional; scan_partial_symbols and
17782 add_partial_symbol will handle these DIEs if we chain
17783 them in normally. When compilers which do not emit large
17784 quantities of duplicate debug information are more common,
17785 this code can probably be removed. */
17786
17787 /* Any complete simple types at the top level (pretty much all
17788 of them, for a language without namespaces), can be processed
17789 directly. */
17790 if (parent_die == NULL
cd9983dd
YQ
17791 && pdi.has_specification == 0
17792 && pdi.is_declaration == 0
17793 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17794 || pdi.tag == DW_TAG_base_type
17795 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17796 {
cd9983dd 17797 if (building_psymtab && pdi.name != NULL)
31edb802 17798 add_psymbol_to_list (pdi.name, false,
79748972 17799 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17800 psymbol_placement::STATIC,
1762568f 17801 0, cu->language, objfile);
cd9983dd 17802 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17803 continue;
17804 }
17805
d8228535
JK
17806 /* The exception for DW_TAG_typedef with has_children above is
17807 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17808 type_name_or_error will error on such types later.
d8228535
JK
17809
17810 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17811 it could not find the child DIEs referenced later, this is checked
17812 above. In correct DWARF DW_TAG_typedef should have no children. */
17813
cd9983dd 17814 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17815 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17816 "- DIE at %s [in module %s]"),
cd9983dd 17817 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17818
72bf9492
DJ
17819 /* If we're at the second level, and we're an enumerator, and
17820 our parent has no specification (meaning possibly lives in a
17821 namespace elsewhere), then we can add the partial symbol now
17822 instead of queueing it. */
cd9983dd 17823 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17824 && parent_die != NULL
17825 && parent_die->die_parent == NULL
17826 && parent_die->tag == DW_TAG_enumeration_type
17827 && parent_die->has_specification == 0)
17828 {
cd9983dd 17829 if (pdi.name == NULL)
b98664d3 17830 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17831 else if (building_psymtab)
31edb802 17832 add_psymbol_to_list (pdi.name, false,
79748972 17833 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17834 cu->language == language_cplus
75aedd27
TT
17835 ? psymbol_placement::GLOBAL
17836 : psymbol_placement::STATIC,
1762568f 17837 0, cu->language, objfile);
72bf9492 17838
cd9983dd 17839 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17840 continue;
17841 }
17842
cd9983dd 17843 struct partial_die_info *part_die
6f06d47b 17844 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17845
72bf9492
DJ
17846 /* We'll save this DIE so link it in. */
17847 part_die->die_parent = parent_die;
17848 part_die->die_sibling = NULL;
17849 part_die->die_child = NULL;
17850
17851 if (last_die && last_die == parent_die)
17852 last_die->die_child = part_die;
17853 else if (last_die)
17854 last_die->die_sibling = part_die;
17855
17856 last_die = part_die;
17857
17858 if (first_die == NULL)
17859 first_die = part_die;
17860
17861 /* Maybe add the DIE to the hash table. Not all DIEs that we
17862 find interesting need to be in the hash table, because we
17863 also have the parent/sibling/child chains; only those that we
17864 might refer to by offset later during partial symbol reading.
17865
17866 For now this means things that might have be the target of a
17867 DW_AT_specification, DW_AT_abstract_origin, or
17868 DW_AT_extension. DW_AT_extension will refer only to
17869 namespaces; DW_AT_abstract_origin refers to functions (and
17870 many things under the function DIE, but we do not recurse
17871 into function DIEs during partial symbol reading) and
17872 possibly variables as well; DW_AT_specification refers to
17873 declarations. Declarations ought to have the DW_AT_declaration
17874 flag. It happens that GCC forgets to put it in sometimes, but
17875 only for functions, not for types.
17876
17877 Adding more things than necessary to the hash table is harmless
17878 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17879 wasted time in find_partial_die, when we reread the compilation
17880 unit with load_all_dies set. */
72bf9492 17881
5afb4e99 17882 if (load_all
72929c62 17883 || abbrev->tag == DW_TAG_constant
5afb4e99 17884 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17885 || abbrev->tag == DW_TAG_variable
17886 || abbrev->tag == DW_TAG_namespace
17887 || part_die->is_declaration)
17888 {
17889 void **slot;
17890
17891 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17892 to_underlying (part_die->sect_off),
17893 INSERT);
72bf9492
DJ
17894 *slot = part_die;
17895 }
17896
72bf9492 17897 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17898 we have no reason to follow the children of structures; for other
98bfdba5
PA
17899 languages we have to, so that we can get at method physnames
17900 to infer fully qualified class names, for DW_AT_specification,
17901 and for C++ template arguments. For C++, we also look one level
17902 inside functions to find template arguments (if the name of the
17903 function does not already contain the template arguments).
bc30ff58 17904
0a4b0913
AB
17905 For Ada and Fortran, we need to scan the children of subprograms
17906 and lexical blocks as well because these languages allow the
17907 definition of nested entities that could be interesting for the
17908 debugger, such as nested subprograms for instance. */
72bf9492 17909 if (last_die->has_children
5afb4e99
DJ
17910 && (load_all
17911 || last_die->tag == DW_TAG_namespace
f55ee35c 17912 || last_die->tag == DW_TAG_module
72bf9492 17913 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17914 || (cu->language == language_cplus
17915 && last_die->tag == DW_TAG_subprogram
17916 && (last_die->name == NULL
17917 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17918 || (cu->language != language_c
17919 && (last_die->tag == DW_TAG_class_type
680b30c7 17920 || last_die->tag == DW_TAG_interface_type
72bf9492 17921 || last_die->tag == DW_TAG_structure_type
bc30ff58 17922 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17923 || ((cu->language == language_ada
17924 || cu->language == language_fortran)
bc30ff58
JB
17925 && (last_die->tag == DW_TAG_subprogram
17926 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17927 {
17928 nesting_level++;
17929 parent_die = last_die;
17930 continue;
17931 }
17932
17933 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17934 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17935
17936 /* Back to the top, do it again. */
17937 }
17938}
17939
6f06d47b
YQ
17940partial_die_info::partial_die_info (sect_offset sect_off_,
17941 struct abbrev_info *abbrev)
17942 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17943{
17944}
17945
35cc7ed7
YQ
17946/* Read a minimal amount of information into the minimal die structure.
17947 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17948
48fbe735
YQ
17949const gdb_byte *
17950partial_die_info::read (const struct die_reader_specs *reader,
17951 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17952{
dee91e82 17953 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17954 struct dwarf2_per_objfile *dwarf2_per_objfile
17955 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17956 unsigned int i;
c5aa993b 17957 int has_low_pc_attr = 0;
c906108c 17958 int has_high_pc_attr = 0;
91da1414 17959 int high_pc_relative = 0;
c906108c 17960
18a8505e 17961 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17962 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17963 {
18a8505e
AT
17964 bool need_reprocess;
17965 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17966 info_ptr, &need_reprocess);
17967 /* String and address offsets that need to do the reprocessing have
17968 already been read at this point, so there is no need to wait until
17969 the loop terminates to do the reprocessing. */
17970 if (need_reprocess)
17971 read_attribute_reprocess (reader, &attr_vec[i]);
17972 attribute &attr = attr_vec[i];
c906108c 17973 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17974 partial symbol table. */
c906108c
SS
17975 switch (attr.name)
17976 {
17977 case DW_AT_name:
48fbe735 17978 switch (tag)
71c25dea
TT
17979 {
17980 case DW_TAG_compile_unit:
95554aad 17981 case DW_TAG_partial_unit:
348e048f 17982 case DW_TAG_type_unit:
71c25dea
TT
17983 /* Compilation units have a DW_AT_name that is a filename, not
17984 a source language identifier. */
17985 case DW_TAG_enumeration_type:
17986 case DW_TAG_enumerator:
17987 /* These tags always have simple identifiers already; no need
17988 to canonicalize them. */
48fbe735 17989 name = DW_STRING (&attr);
71c25dea
TT
17990 break;
17991 default:
48fbe735
YQ
17992 {
17993 struct objfile *objfile = dwarf2_per_objfile->objfile;
17994
17995 name
17996 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17997 &objfile->per_bfd->storage_obstack);
17998 }
71c25dea
TT
17999 break;
18000 }
c906108c 18001 break;
31ef98ae 18002 case DW_AT_linkage_name:
c906108c 18003 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18004 /* Note that both forms of linkage name might appear. We
18005 assume they will be the same, and we only store the last
18006 one we see. */
48fbe735 18007 linkage_name = DW_STRING (&attr);
c906108c
SS
18008 break;
18009 case DW_AT_low_pc:
18010 has_low_pc_attr = 1;
cd6c91b4 18011 lowpc = attr.value_as_address ();
c906108c
SS
18012 break;
18013 case DW_AT_high_pc:
18014 has_high_pc_attr = 1;
cd6c91b4
TT
18015 highpc = attr.value_as_address ();
18016 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18017 high_pc_relative = 1;
c906108c
SS
18018 break;
18019 case DW_AT_location:
0963b4bd 18020 /* Support the .debug_loc offsets. */
4fc6c0d5 18021 if (attr.form_is_block ())
8e19ed76 18022 {
48fbe735 18023 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18024 }
cd6c91b4 18025 else if (attr.form_is_section_offset ())
8e19ed76 18026 {
4d3c2250 18027 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18028 }
18029 else
18030 {
4d3c2250
KB
18031 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18032 "partial symbol information");
8e19ed76 18033 }
c906108c 18034 break;
c906108c 18035 case DW_AT_external:
48fbe735 18036 is_external = DW_UNSND (&attr);
c906108c
SS
18037 break;
18038 case DW_AT_declaration:
48fbe735 18039 is_declaration = DW_UNSND (&attr);
c906108c
SS
18040 break;
18041 case DW_AT_type:
48fbe735 18042 has_type = 1;
c906108c
SS
18043 break;
18044 case DW_AT_abstract_origin:
18045 case DW_AT_specification:
72bf9492 18046 case DW_AT_extension:
48fbe735
YQ
18047 has_specification = 1;
18048 spec_offset = dwarf2_get_ref_die_offset (&attr);
18049 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18050 || cu->per_cu->is_dwz);
c906108c
SS
18051 break;
18052 case DW_AT_sibling:
18053 /* Ignore absolute siblings, they might point outside of
18054 the current compile unit. */
18055 if (attr.form == DW_FORM_ref_addr)
b98664d3 18056 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18057 else
b9502d3f 18058 {
48fbe735 18059 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18060 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18061 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18062
18063 if (sibling_ptr < info_ptr)
b98664d3 18064 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18065 else if (sibling_ptr > reader->buffer_end)
18066 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18067 else
48fbe735 18068 sibling = sibling_ptr;
b9502d3f 18069 }
c906108c 18070 break;
fa4028e9 18071 case DW_AT_byte_size:
48fbe735 18072 has_byte_size = 1;
fa4028e9 18073 break;
ff908ebf 18074 case DW_AT_const_value:
48fbe735 18075 has_const_value = 1;
ff908ebf 18076 break;
68511cec
CES
18077 case DW_AT_calling_convention:
18078 /* DWARF doesn't provide a way to identify a program's source-level
18079 entry point. DW_AT_calling_convention attributes are only meant
18080 to describe functions' calling conventions.
18081
18082 However, because it's a necessary piece of information in
0c1b455e
TT
18083 Fortran, and before DWARF 4 DW_CC_program was the only
18084 piece of debugging information whose definition refers to
18085 a 'main program' at all, several compilers marked Fortran
18086 main programs with DW_CC_program --- even when those
18087 functions use the standard calling conventions.
18088
18089 Although DWARF now specifies a way to provide this
18090 information, we support this practice for backward
18091 compatibility. */
68511cec 18092 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18093 && cu->language == language_fortran)
48fbe735 18094 main_subprogram = 1;
68511cec 18095 break;
481860b3
GB
18096 case DW_AT_inline:
18097 if (DW_UNSND (&attr) == DW_INL_inlined
18098 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18099 may_be_inlined = 1;
481860b3 18100 break;
95554aad
TT
18101
18102 case DW_AT_import:
48fbe735 18103 if (tag == DW_TAG_imported_unit)
36586728 18104 {
48fbe735
YQ
18105 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18106 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18107 || cu->per_cu->is_dwz);
18108 }
95554aad
TT
18109 break;
18110
0c1b455e 18111 case DW_AT_main_subprogram:
48fbe735 18112 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18113 break;
18114
05caa1d2
TT
18115 case DW_AT_ranges:
18116 {
18117 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18118 but that requires a full DIE, so instead we just
18119 reimplement it. */
18120 int need_ranges_base = tag != DW_TAG_compile_unit;
18121 unsigned int ranges_offset = (DW_UNSND (&attr)
18122 + (need_ranges_base
18123 ? cu->ranges_base
18124 : 0));
18125
18126 /* Value of the DW_AT_ranges attribute is the offset in the
18127 .debug_ranges section. */
18128 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18129 nullptr))
18130 has_pc_info = 1;
18131 }
18132 break;
18133
c906108c
SS
18134 default:
18135 break;
18136 }
18137 }
18138
10d06d82
TT
18139 /* For Ada, if both the name and the linkage name appear, we prefer
18140 the latter. This lets "catch exception" work better, regardless
18141 of the order in which the name and linkage name were emitted.
18142 Really, though, this is just a workaround for the fact that gdb
18143 doesn't store both the name and the linkage name. */
18144 if (cu->language == language_ada && linkage_name != nullptr)
18145 name = linkage_name;
18146
91da1414 18147 if (high_pc_relative)
48fbe735 18148 highpc += lowpc;
91da1414 18149
9373cf26
JK
18150 if (has_low_pc_attr && has_high_pc_attr)
18151 {
18152 /* When using the GNU linker, .gnu.linkonce. sections are used to
18153 eliminate duplicate copies of functions and vtables and such.
18154 The linker will arbitrarily choose one and discard the others.
18155 The AT_*_pc values for such functions refer to local labels in
18156 these sections. If the section from that file was discarded, the
18157 labels are not in the output, so the relocs get a value of 0.
18158 If this is a discarded function, mark the pc bounds as invalid,
18159 so that GDB will ignore it. */
48fbe735 18160 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18161 {
48fbe735 18162 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18164
b98664d3 18165 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18166 "for DIE at %s [in module %s]"),
48fbe735
YQ
18167 paddress (gdbarch, lowpc),
18168 sect_offset_str (sect_off),
9d8780f0 18169 objfile_name (objfile));
9373cf26
JK
18170 }
18171 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18172 else if (lowpc >= highpc)
9373cf26 18173 {
48fbe735 18174 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18175 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18176
b98664d3 18177 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18178 "for DIE at %s [in module %s]"),
48fbe735
YQ
18179 paddress (gdbarch, lowpc),
18180 paddress (gdbarch, highpc),
18181 sect_offset_str (sect_off),
9c541725 18182 objfile_name (objfile));
9373cf26
JK
18183 }
18184 else
48fbe735 18185 has_pc_info = 1;
9373cf26 18186 }
85cbf3d3 18187
c906108c
SS
18188 return info_ptr;
18189}
18190
72bf9492
DJ
18191/* Find a cached partial DIE at OFFSET in CU. */
18192
d590ff25
YQ
18193struct partial_die_info *
18194dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18195{
18196 struct partial_die_info *lookup_die = NULL;
6f06d47b 18197 struct partial_die_info part_die (sect_off);
72bf9492 18198
9a3c8263 18199 lookup_die = ((struct partial_die_info *)
d590ff25 18200 htab_find_with_hash (partial_dies, &part_die,
9c541725 18201 to_underlying (sect_off)));
72bf9492 18202
72bf9492
DJ
18203 return lookup_die;
18204}
18205
348e048f
DE
18206/* Find a partial DIE at OFFSET, which may or may not be in CU,
18207 except in the case of .debug_types DIEs which do not reference
18208 outside their CU (they do however referencing other types via
55f1336d 18209 DW_FORM_ref_sig8). */
72bf9492 18210
122cf0f2 18211static const struct cu_partial_die_info
9c541725 18212find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18213{
518817b3
SM
18214 struct dwarf2_per_objfile *dwarf2_per_objfile
18215 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18216 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18217 struct dwarf2_per_cu_data *per_cu = NULL;
18218 struct partial_die_info *pd = NULL;
72bf9492 18219
36586728 18220 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18221 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18222 {
d590ff25 18223 pd = cu->find_partial_die (sect_off);
5afb4e99 18224 if (pd != NULL)
fb816e8b 18225 return { cu, pd };
0d99eb77
DE
18226 /* We missed recording what we needed.
18227 Load all dies and try again. */
18228 per_cu = cu->per_cu;
5afb4e99 18229 }
0d99eb77
DE
18230 else
18231 {
18232 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18233 if (cu->per_cu->is_debug_types)
0d99eb77 18234 {
9d8780f0
SM
18235 error (_("Dwarf Error: Type Unit at offset %s contains"
18236 " external reference to offset %s [in module %s].\n"),
18237 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18238 bfd_get_filename (objfile->obfd));
18239 }
9c541725 18240 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18241 dwarf2_per_objfile);
72bf9492 18242
0d99eb77
DE
18243 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18244 load_partial_comp_unit (per_cu);
ae038cb0 18245
0d99eb77 18246 per_cu->cu->last_used = 0;
d590ff25 18247 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18248 }
5afb4e99 18249
dee91e82
DE
18250 /* If we didn't find it, and not all dies have been loaded,
18251 load them all and try again. */
18252
5afb4e99
DJ
18253 if (pd == NULL && per_cu->load_all_dies == 0)
18254 {
5afb4e99 18255 per_cu->load_all_dies = 1;
fd820528
DE
18256
18257 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18258 THIS_CU->cu may already be in use. So we can't just free it and
18259 replace its DIEs with the ones we read in. Instead, we leave those
18260 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18261 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18262 set. */
dee91e82 18263 load_partial_comp_unit (per_cu);
5afb4e99 18264
d590ff25 18265 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18266 }
18267
18268 if (pd == NULL)
18269 internal_error (__FILE__, __LINE__,
9d8780f0 18270 _("could not find partial DIE %s "
3e43a32a 18271 "in cache [from module %s]\n"),
9d8780f0 18272 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18273 return { per_cu->cu, pd };
72bf9492
DJ
18274}
18275
abc72ce4
DE
18276/* See if we can figure out if the class lives in a namespace. We do
18277 this by looking for a member function; its demangled name will
18278 contain namespace info, if there is any. */
18279
18280static void
18281guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18282 struct dwarf2_cu *cu)
18283{
18284 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18285 what template types look like, because the demangler
18286 frequently doesn't give the same name as the debug info. We
18287 could fix this by only using the demangled name to get the
18288 prefix (but see comment in read_structure_type). */
18289
18290 struct partial_die_info *real_pdi;
18291 struct partial_die_info *child_pdi;
18292
18293 /* If this DIE (this DIE's specification, if any) has a parent, then
18294 we should not do this. We'll prepend the parent's fully qualified
18295 name when we create the partial symbol. */
18296
18297 real_pdi = struct_pdi;
18298 while (real_pdi->has_specification)
fb816e8b 18299 {
122cf0f2
AB
18300 auto res = find_partial_die (real_pdi->spec_offset,
18301 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18302 real_pdi = res.pdi;
18303 cu = res.cu;
18304 }
abc72ce4
DE
18305
18306 if (real_pdi->die_parent != NULL)
18307 return;
18308
18309 for (child_pdi = struct_pdi->die_child;
18310 child_pdi != NULL;
18311 child_pdi = child_pdi->die_sibling)
18312 {
18313 if (child_pdi->tag == DW_TAG_subprogram
18314 && child_pdi->linkage_name != NULL)
18315 {
43816ebc
TT
18316 gdb::unique_xmalloc_ptr<char> actual_class_name
18317 (language_class_name_from_physname (cu->language_defn,
18318 child_pdi->linkage_name));
abc72ce4
DE
18319 if (actual_class_name != NULL)
18320 {
518817b3 18321 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18322 struct_pdi->name
021887d8 18323 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 18324 actual_class_name.get ());
abc72ce4
DE
18325 }
18326 break;
18327 }
18328 }
18329}
18330
52356b79
YQ
18331void
18332partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18333{
abc72ce4
DE
18334 /* Once we've fixed up a die, there's no point in doing so again.
18335 This also avoids a memory leak if we were to call
18336 guess_partial_die_structure_name multiple times. */
52356b79 18337 if (fixup_called)
abc72ce4
DE
18338 return;
18339
72bf9492
DJ
18340 /* If we found a reference attribute and the DIE has no name, try
18341 to find a name in the referred to DIE. */
18342
52356b79 18343 if (name == NULL && has_specification)
72bf9492
DJ
18344 {
18345 struct partial_die_info *spec_die;
72bf9492 18346
122cf0f2 18347 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18348 spec_die = res.pdi;
18349 cu = res.cu;
72bf9492 18350
52356b79 18351 spec_die->fixup (cu);
72bf9492
DJ
18352
18353 if (spec_die->name)
18354 {
52356b79 18355 name = spec_die->name;
72bf9492
DJ
18356
18357 /* Copy DW_AT_external attribute if it is set. */
18358 if (spec_die->is_external)
52356b79 18359 is_external = spec_die->is_external;
72bf9492
DJ
18360 }
18361 }
18362
18363 /* Set default names for some unnamed DIEs. */
72bf9492 18364
52356b79
YQ
18365 if (name == NULL && tag == DW_TAG_namespace)
18366 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18367
abc72ce4
DE
18368 /* If there is no parent die to provide a namespace, and there are
18369 children, see if we can determine the namespace from their linkage
122d1940 18370 name. */
abc72ce4 18371 if (cu->language == language_cplus
fd5866f6 18372 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18373 && die_parent == NULL
18374 && has_children
18375 && (tag == DW_TAG_class_type
18376 || tag == DW_TAG_structure_type
18377 || tag == DW_TAG_union_type))
18378 guess_partial_die_structure_name (this, cu);
abc72ce4 18379
53832f31
TT
18380 /* GCC might emit a nameless struct or union that has a linkage
18381 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18382 if (name == NULL
18383 && (tag == DW_TAG_class_type
18384 || tag == DW_TAG_interface_type
18385 || tag == DW_TAG_structure_type
18386 || tag == DW_TAG_union_type)
18387 && linkage_name != NULL)
53832f31 18388 {
43816ebc
TT
18389 gdb::unique_xmalloc_ptr<char> demangled
18390 (gdb_demangle (linkage_name, DMGL_TYPES));
18391 if (demangled != nullptr)
53832f31 18392 {
96408a79
SA
18393 const char *base;
18394
18395 /* Strip any leading namespaces/classes, keep only the base name.
18396 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18397 base = strrchr (demangled.get (), ':');
18398 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18399 base++;
18400 else
43816ebc 18401 base = demangled.get ();
96408a79 18402
518817b3 18403 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 18404 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
18405 }
18406 }
18407
52356b79 18408 fixup_called = 1;
72bf9492
DJ
18409}
18410
18a8505e
AT
18411/* Process the attributes that had to be skipped in the first round. These
18412 attributes are the ones that need str_offsets_base or addr_base attributes.
18413 They could not have been processed in the first round, because at the time
18414 the values of str_offsets_base or addr_base may not have been known. */
18415void read_attribute_reprocess (const struct die_reader_specs *reader,
18416 struct attribute *attr)
18417{
18418 struct dwarf2_cu *cu = reader->cu;
18419 switch (attr->form)
18420 {
18421 case DW_FORM_addrx:
18422 case DW_FORM_GNU_addr_index:
18423 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18424 break;
18425 case DW_FORM_strx:
18426 case DW_FORM_strx1:
18427 case DW_FORM_strx2:
18428 case DW_FORM_strx3:
18429 case DW_FORM_strx4:
18430 case DW_FORM_GNU_str_index:
18431 {
18432 unsigned int str_index = DW_UNSND (attr);
18433 if (reader->dwo_file != NULL)
18434 {
18435 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18436 DW_STRING_IS_CANONICAL (attr) = 0;
18437 }
18438 else
18439 {
18440 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18441 DW_STRING_IS_CANONICAL (attr) = 0;
18442 }
18443 break;
18444 }
18445 default:
18446 gdb_assert_not_reached (_("Unexpected DWARF form."));
18447 }
18448}
18449
a8329558 18450/* Read an attribute value described by an attribute form. */
c906108c 18451
d521ce57 18452static const gdb_byte *
dee91e82
DE
18453read_attribute_value (const struct die_reader_specs *reader,
18454 struct attribute *attr, unsigned form,
18a8505e
AT
18455 LONGEST implicit_const, const gdb_byte *info_ptr,
18456 bool *need_reprocess)
c906108c 18457{
dee91e82 18458 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18459 struct dwarf2_per_objfile *dwarf2_per_objfile
18460 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18461 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18462 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18463 bfd *abfd = reader->abfd;
e7c27a73 18464 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18465 unsigned int bytes_read;
18466 struct dwarf_block *blk;
18a8505e 18467 *need_reprocess = false;
c906108c 18468
aead7601 18469 attr->form = (enum dwarf_form) form;
a8329558 18470 switch (form)
c906108c 18471 {
c906108c 18472 case DW_FORM_ref_addr:
ae411497 18473 if (cu->header.version == 2)
c8a7a66f
TT
18474 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18475 &bytes_read);
ae411497 18476 else
8266302d
TT
18477 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18478 &bytes_read);
ae411497
TT
18479 info_ptr += bytes_read;
18480 break;
36586728 18481 case DW_FORM_GNU_ref_alt:
8266302d 18482 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18483 info_ptr += bytes_read;
18484 break;
ae411497 18485 case DW_FORM_addr:
c8a7a66f 18486 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18487 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18488 info_ptr += bytes_read;
c906108c
SS
18489 break;
18490 case DW_FORM_block2:
7b5a2f43 18491 blk = dwarf_alloc_block (cu);
c906108c
SS
18492 blk->size = read_2_bytes (abfd, info_ptr);
18493 info_ptr += 2;
18494 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18495 info_ptr += blk->size;
18496 DW_BLOCK (attr) = blk;
18497 break;
18498 case DW_FORM_block4:
7b5a2f43 18499 blk = dwarf_alloc_block (cu);
c906108c
SS
18500 blk->size = read_4_bytes (abfd, info_ptr);
18501 info_ptr += 4;
18502 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18503 info_ptr += blk->size;
18504 DW_BLOCK (attr) = blk;
18505 break;
18506 case DW_FORM_data2:
18507 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18508 info_ptr += 2;
18509 break;
18510 case DW_FORM_data4:
18511 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18512 info_ptr += 4;
18513 break;
18514 case DW_FORM_data8:
18515 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18516 info_ptr += 8;
18517 break;
0224619f
JK
18518 case DW_FORM_data16:
18519 blk = dwarf_alloc_block (cu);
18520 blk->size = 16;
18521 blk->data = read_n_bytes (abfd, info_ptr, 16);
18522 info_ptr += 16;
18523 DW_BLOCK (attr) = blk;
18524 break;
2dc7f7b3 18525 case DW_FORM_sec_offset:
8266302d 18526 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18527 info_ptr += bytes_read;
18528 break;
c906108c 18529 case DW_FORM_string:
9b1c24c8 18530 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18531 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18532 info_ptr += bytes_read;
18533 break;
4bdf3d34 18534 case DW_FORM_strp:
36586728
TT
18535 if (!cu->per_cu->is_dwz)
18536 {
ed2dc618
SM
18537 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18538 abfd, info_ptr, cu_header,
36586728
TT
18539 &bytes_read);
18540 DW_STRING_IS_CANONICAL (attr) = 0;
18541 info_ptr += bytes_read;
18542 break;
18543 }
18544 /* FALLTHROUGH */
43988095
JK
18545 case DW_FORM_line_strp:
18546 if (!cu->per_cu->is_dwz)
18547 {
ed2dc618
SM
18548 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18549 abfd, info_ptr,
43988095
JK
18550 cu_header, &bytes_read);
18551 DW_STRING_IS_CANONICAL (attr) = 0;
18552 info_ptr += bytes_read;
18553 break;
18554 }
18555 /* FALLTHROUGH */
36586728
TT
18556 case DW_FORM_GNU_strp_alt:
18557 {
ed2dc618 18558 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18559 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18560 &bytes_read);
36586728 18561
ed2dc618
SM
18562 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18563 dwz, str_offset);
36586728
TT
18564 DW_STRING_IS_CANONICAL (attr) = 0;
18565 info_ptr += bytes_read;
18566 }
4bdf3d34 18567 break;
2dc7f7b3 18568 case DW_FORM_exprloc:
c906108c 18569 case DW_FORM_block:
7b5a2f43 18570 blk = dwarf_alloc_block (cu);
c906108c
SS
18571 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18572 info_ptr += bytes_read;
18573 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18574 info_ptr += blk->size;
18575 DW_BLOCK (attr) = blk;
18576 break;
18577 case DW_FORM_block1:
7b5a2f43 18578 blk = dwarf_alloc_block (cu);
c906108c
SS
18579 blk->size = read_1_byte (abfd, info_ptr);
18580 info_ptr += 1;
18581 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18582 info_ptr += blk->size;
18583 DW_BLOCK (attr) = blk;
18584 break;
18585 case DW_FORM_data1:
18586 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18587 info_ptr += 1;
18588 break;
18589 case DW_FORM_flag:
18590 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18591 info_ptr += 1;
18592 break;
2dc7f7b3
TT
18593 case DW_FORM_flag_present:
18594 DW_UNSND (attr) = 1;
18595 break;
c906108c
SS
18596 case DW_FORM_sdata:
18597 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18598 info_ptr += bytes_read;
18599 break;
18600 case DW_FORM_udata:
18a8505e 18601 case DW_FORM_rnglistx:
c906108c
SS
18602 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18603 info_ptr += bytes_read;
18604 break;
18605 case DW_FORM_ref1:
9c541725 18606 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18607 + read_1_byte (abfd, info_ptr));
c906108c
SS
18608 info_ptr += 1;
18609 break;
18610 case DW_FORM_ref2:
9c541725 18611 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18612 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18613 info_ptr += 2;
18614 break;
18615 case DW_FORM_ref4:
9c541725 18616 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18617 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18618 info_ptr += 4;
18619 break;
613e1657 18620 case DW_FORM_ref8:
9c541725 18621 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18622 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18623 info_ptr += 8;
18624 break;
55f1336d 18625 case DW_FORM_ref_sig8:
ac9ec31b 18626 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18627 info_ptr += 8;
18628 break;
c906108c 18629 case DW_FORM_ref_udata:
9c541725 18630 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18631 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18632 info_ptr += bytes_read;
18633 break;
c906108c 18634 case DW_FORM_indirect:
a8329558
KW
18635 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18636 info_ptr += bytes_read;
43988095
JK
18637 if (form == DW_FORM_implicit_const)
18638 {
18639 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18640 info_ptr += bytes_read;
18641 }
18642 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18643 info_ptr, need_reprocess);
43988095
JK
18644 break;
18645 case DW_FORM_implicit_const:
18646 DW_SND (attr) = implicit_const;
a8329558 18647 break;
336d760d 18648 case DW_FORM_addrx:
3019eac3 18649 case DW_FORM_GNU_addr_index:
18a8505e
AT
18650 *need_reprocess = true;
18651 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18652 info_ptr += bytes_read;
18653 break;
cf532bd1 18654 case DW_FORM_strx:
15f18d14
AT
18655 case DW_FORM_strx1:
18656 case DW_FORM_strx2:
18657 case DW_FORM_strx3:
18658 case DW_FORM_strx4:
3019eac3 18659 case DW_FORM_GNU_str_index:
3019eac3 18660 {
15f18d14
AT
18661 ULONGEST str_index;
18662 if (form == DW_FORM_strx1)
18663 {
18664 str_index = read_1_byte (abfd, info_ptr);
18665 info_ptr += 1;
18666 }
18667 else if (form == DW_FORM_strx2)
18668 {
18669 str_index = read_2_bytes (abfd, info_ptr);
18670 info_ptr += 2;
18671 }
18672 else if (form == DW_FORM_strx3)
18673 {
18674 str_index = read_3_bytes (abfd, info_ptr);
18675 info_ptr += 3;
18676 }
18677 else if (form == DW_FORM_strx4)
18678 {
18679 str_index = read_4_bytes (abfd, info_ptr);
18680 info_ptr += 4;
18681 }
18682 else
18683 {
18684 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18685 info_ptr += bytes_read;
18686 }
18a8505e
AT
18687 *need_reprocess = true;
18688 DW_UNSND (attr) = str_index;
18689 }
3019eac3 18690 break;
c906108c 18691 default:
8a3fe4f8 18692 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18693 dwarf_form_name (form),
18694 bfd_get_filename (abfd));
c906108c 18695 }
28e94949 18696
36586728 18697 /* Super hack. */
cd6c91b4 18698 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18699 attr->form = DW_FORM_GNU_ref_alt;
18700
28e94949
JB
18701 /* We have seen instances where the compiler tried to emit a byte
18702 size attribute of -1 which ended up being encoded as an unsigned
18703 0xffffffff. Although 0xffffffff is technically a valid size value,
18704 an object of this size seems pretty unlikely so we can relatively
18705 safely treat these cases as if the size attribute was invalid and
18706 treat them as zero by default. */
18707 if (attr->name == DW_AT_byte_size
18708 && form == DW_FORM_data4
18709 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18710 {
18711 complaint
b98664d3 18712 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18713 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18714 DW_UNSND (attr) = 0;
18715 }
28e94949 18716
c906108c
SS
18717 return info_ptr;
18718}
18719
a8329558
KW
18720/* Read an attribute described by an abbreviated attribute. */
18721
d521ce57 18722static const gdb_byte *
dee91e82
DE
18723read_attribute (const struct die_reader_specs *reader,
18724 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18725 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18726{
18727 attr->name = abbrev->name;
43988095 18728 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18729 abbrev->implicit_const, info_ptr,
18730 need_reprocess);
a8329558
KW
18731}
18732
c764a876
DE
18733/* Cover function for read_initial_length.
18734 Returns the length of the object at BUF, and stores the size of the
18735 initial length in *BYTES_READ and stores the size that offsets will be in
18736 *OFFSET_SIZE.
18737 If the initial length size is not equivalent to that specified in
18738 CU_HEADER then issue a complaint.
18739 This is useful when reading non-comp-unit headers. */
dd373385 18740
c764a876 18741static LONGEST
d521ce57 18742read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18743 const struct comp_unit_head *cu_header,
18744 unsigned int *bytes_read,
18745 unsigned int *offset_size)
18746{
18747 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18748
18749 gdb_assert (cu_header->initial_length_size == 4
18750 || cu_header->initial_length_size == 8
18751 || cu_header->initial_length_size == 12);
18752
18753 if (cu_header->initial_length_size != *bytes_read)
b98664d3 18754 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18755
c764a876 18756 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18757 return length;
613e1657
KB
18758}
18759
43988095
JK
18760/* Return pointer to string at section SECT offset STR_OFFSET with error
18761 reporting strings FORM_NAME and SECT_NAME. */
18762
d521ce57 18763static const char *
ed2dc618
SM
18764read_indirect_string_at_offset_from (struct objfile *objfile,
18765 bfd *abfd, LONGEST str_offset,
43988095
JK
18766 struct dwarf2_section_info *sect,
18767 const char *form_name,
18768 const char *sect_name)
18769{
96b79293 18770 sect->read (objfile);
43988095
JK
18771 if (sect->buffer == NULL)
18772 error (_("%s used without %s section [in module %s]"),
18773 form_name, sect_name, bfd_get_filename (abfd));
18774 if (str_offset >= sect->size)
18775 error (_("%s pointing outside of %s section [in module %s]"),
18776 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18777 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18778 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18779 return NULL;
43988095
JK
18780 return (const char *) (sect->buffer + str_offset);
18781}
18782
18783/* Return pointer to string at .debug_str offset STR_OFFSET. */
18784
18785static const char *
ed2dc618
SM
18786read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18787 bfd *abfd, LONGEST str_offset)
43988095 18788{
ed2dc618
SM
18789 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18790 abfd, str_offset,
43988095
JK
18791 &dwarf2_per_objfile->str,
18792 "DW_FORM_strp", ".debug_str");
18793}
18794
18795/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18796
18797static const char *
ed2dc618
SM
18798read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18799 bfd *abfd, LONGEST str_offset)
43988095 18800{
ed2dc618
SM
18801 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18802 abfd, str_offset,
43988095
JK
18803 &dwarf2_per_objfile->line_str,
18804 "DW_FORM_line_strp",
18805 ".debug_line_str");
c906108c
SS
18806}
18807
36586728
TT
18808/* Read a string at offset STR_OFFSET in the .debug_str section from
18809 the .dwz file DWZ. Throw an error if the offset is too large. If
18810 the string consists of a single NUL byte, return NULL; otherwise
18811 return a pointer to the string. */
18812
d521ce57 18813static const char *
ed2dc618
SM
18814read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18815 LONGEST str_offset)
36586728 18816{
96b79293 18817 dwz->str.read (objfile);
36586728
TT
18818
18819 if (dwz->str.buffer == NULL)
18820 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18821 "section [in module %s]"),
00f93c44 18822 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18823 if (str_offset >= dwz->str.size)
18824 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18825 ".debug_str section [in module %s]"),
00f93c44 18826 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18827 gdb_assert (HOST_CHAR_BIT == 8);
18828 if (dwz->str.buffer[str_offset] == '\0')
18829 return NULL;
d521ce57 18830 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
18831}
18832
43988095
JK
18833/* Return pointer to string at .debug_str offset as read from BUF.
18834 BUF is assumed to be in a compilation unit described by CU_HEADER.
18835 Return *BYTES_READ_PTR count of bytes read from BUF. */
18836
d521ce57 18837static const char *
ed2dc618
SM
18838read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18839 const gdb_byte *buf,
cf2c3c16
TT
18840 const struct comp_unit_head *cu_header,
18841 unsigned int *bytes_read_ptr)
18842{
8266302d 18843 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18844
ed2dc618 18845 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
18846}
18847
43988095
JK
18848/* Return pointer to string at .debug_line_str offset as read from BUF.
18849 BUF is assumed to be in a compilation unit described by CU_HEADER.
18850 Return *BYTES_READ_PTR count of bytes read from BUF. */
18851
18852static const char *
ed2dc618
SM
18853read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18854 bfd *abfd, const gdb_byte *buf,
43988095
JK
18855 const struct comp_unit_head *cu_header,
18856 unsigned int *bytes_read_ptr)
18857{
8266302d 18858 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18859
ed2dc618
SM
18860 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18861 str_offset);
43988095
JK
18862}
18863
3019eac3 18864/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18865 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18866 ADDR_SIZE is the size of addresses from the CU header. */
18867
18868static CORE_ADDR
ed2dc618 18869read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18870 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18871 int addr_size)
3019eac3
DE
18872{
18873 struct objfile *objfile = dwarf2_per_objfile->objfile;
18874 bfd *abfd = objfile->obfd;
18875 const gdb_byte *info_ptr;
18a8505e 18876 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18877
96b79293 18878 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18879 if (dwarf2_per_objfile->addr.buffer == NULL)
18880 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18881 objfile_name (objfile));
18a8505e
AT
18882 if (addr_base_or_zero + addr_index * addr_size
18883 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18884 error (_("DW_FORM_addr_index pointing outside of "
18885 ".debug_addr section [in module %s]"),
4262abfb 18886 objfile_name (objfile));
3019eac3 18887 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18888 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18889 if (addr_size == 4)
18890 return bfd_get_32 (abfd, info_ptr);
18891 else
18892 return bfd_get_64 (abfd, info_ptr);
18893}
18894
18895/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18896
18897static CORE_ADDR
18898read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18899{
518817b3
SM
18900 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18901 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18902}
18903
18904/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18905
18906static CORE_ADDR
d521ce57 18907read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18908 unsigned int *bytes_read)
18909{
518817b3 18910 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18911 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18912
18913 return read_addr_index (cu, addr_index);
18914}
18915
450a1bfc 18916/* See read.h. */
3019eac3
DE
18917
18918CORE_ADDR
450a1bfc 18919dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18920{
ed2dc618 18921 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18922 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18923 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18924 int addr_size;
18925
3019eac3
DE
18926 /* We need addr_base and addr_size.
18927 If we don't have PER_CU->cu, we have to get it.
18928 Nasty, but the alternative is storing the needed info in PER_CU,
18929 which at this point doesn't seem justified: it's not clear how frequently
18930 it would get used and it would increase the size of every PER_CU.
18931 Entry points like dwarf2_per_cu_addr_size do a similar thing
18932 so we're not in uncharted territory here.
18933 Alas we need to be a bit more complicated as addr_base is contained
18934 in the DIE.
18935
18936 We don't need to read the entire CU(/TU).
18937 We just need the header and top level die.
a1b64ce1 18938
3019eac3 18939 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18940 For now we skip this optimization. */
3019eac3
DE
18941
18942 if (cu != NULL)
18943 {
18944 addr_base = cu->addr_base;
18945 addr_size = cu->header.addr_size;
18946 }
18947 else
18948 {
6751ebae 18949 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18950 addr_base = reader.cu->addr_base;
18951 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18952 }
18953
ed2dc618
SM
18954 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18955 addr_size);
3019eac3
DE
18956}
18957
18a8505e
AT
18958/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18959 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18960 DWO file. */
3019eac3 18961
d521ce57 18962static const char *
18a8505e
AT
18963read_str_index (struct dwarf2_cu *cu,
18964 struct dwarf2_section_info *str_section,
18965 struct dwarf2_section_info *str_offsets_section,
18966 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18967{
518817b3
SM
18968 struct dwarf2_per_objfile *dwarf2_per_objfile
18969 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18970 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18971 const char *objf_name = objfile_name (objfile);
3019eac3 18972 bfd *abfd = objfile->obfd;
d521ce57 18973 const gdb_byte *info_ptr;
3019eac3 18974 ULONGEST str_offset;
cf532bd1 18975 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18976
96b79293
TT
18977 str_section->read (objfile);
18978 str_offsets_section->read (objfile);
73869dc2 18979 if (str_section->buffer == NULL)
18a8505e 18980 error (_("%s used without %s section"
9d8780f0 18981 " in CU at offset %s [in module %s]"),
96b79293 18982 form_name, str_section->get_name (),
18a8505e 18983 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18984 if (str_offsets_section->buffer == NULL)
18a8505e 18985 error (_("%s used without %s section"
9d8780f0 18986 " in CU at offset %s [in module %s]"),
96b79293 18987 form_name, str_section->get_name (),
18a8505e 18988 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18989 info_ptr = (str_offsets_section->buffer
18a8505e 18990 + str_offsets_base
3019eac3
DE
18991 + str_index * cu->header.offset_size);
18992 if (cu->header.offset_size == 4)
18993 str_offset = bfd_get_32 (abfd, info_ptr);
18994 else
18995 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18996 if (str_offset >= str_section->size)
57d63ce2 18997 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18998 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18999 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19000 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19001}
19002
18a8505e
AT
19003/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19004
19005static const char *
19006read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19007{
19008 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19009 ? reader->cu->header.addr_size : 0;
19010 return read_str_index (reader->cu,
19011 &reader->dwo_file->sections.str,
19012 &reader->dwo_file->sections.str_offsets,
19013 str_offsets_base, str_index);
19014}
19015
19016/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19017
19018static const char *
19019read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19020{
19021 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19022 const char *objf_name = objfile_name (objfile);
19023 static const char form_name[] = "DW_FORM_GNU_str_index";
19024 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19025
19026 if (!cu->str_offsets_base.has_value ())
19027 error (_("%s used in Fission stub without %s"
19028 " in CU at offset 0x%lx [in module %s]"),
19029 form_name, str_offsets_attr_name,
19030 (long) cu->header.offset_size, objf_name);
19031
19032 return read_str_index (cu,
19033 &cu->per_cu->dwarf2_per_objfile->str,
19034 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19035 *cu->str_offsets_base, str_index);
19036}
19037
3019eac3
DE
19038/* Return the length of an LEB128 number in BUF. */
19039
19040static int
19041leb128_size (const gdb_byte *buf)
19042{
19043 const gdb_byte *begin = buf;
19044 gdb_byte byte;
19045
19046 while (1)
19047 {
19048 byte = *buf++;
19049 if ((byte & 128) == 0)
19050 return buf - begin;
19051 }
19052}
19053
c906108c 19054static void
e142c38c 19055set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19056{
19057 switch (lang)
19058 {
19059 case DW_LANG_C89:
76bee0cc 19060 case DW_LANG_C99:
0cfd832f 19061 case DW_LANG_C11:
c906108c 19062 case DW_LANG_C:
d1be3247 19063 case DW_LANG_UPC:
e142c38c 19064 cu->language = language_c;
c906108c 19065 break;
9c37b5ae 19066 case DW_LANG_Java:
c906108c 19067 case DW_LANG_C_plus_plus:
0cfd832f
MW
19068 case DW_LANG_C_plus_plus_11:
19069 case DW_LANG_C_plus_plus_14:
e142c38c 19070 cu->language = language_cplus;
c906108c 19071 break;
6aecb9c2
JB
19072 case DW_LANG_D:
19073 cu->language = language_d;
19074 break;
c906108c
SS
19075 case DW_LANG_Fortran77:
19076 case DW_LANG_Fortran90:
b21b22e0 19077 case DW_LANG_Fortran95:
f7de9aab
MW
19078 case DW_LANG_Fortran03:
19079 case DW_LANG_Fortran08:
e142c38c 19080 cu->language = language_fortran;
c906108c 19081 break;
a766d390
DE
19082 case DW_LANG_Go:
19083 cu->language = language_go;
19084 break;
c906108c 19085 case DW_LANG_Mips_Assembler:
e142c38c 19086 cu->language = language_asm;
c906108c
SS
19087 break;
19088 case DW_LANG_Ada83:
8aaf0b47 19089 case DW_LANG_Ada95:
bc5f45f8
JB
19090 cu->language = language_ada;
19091 break;
72019c9c
GM
19092 case DW_LANG_Modula2:
19093 cu->language = language_m2;
19094 break;
fe8e67fd
PM
19095 case DW_LANG_Pascal83:
19096 cu->language = language_pascal;
19097 break;
22566fbd
DJ
19098 case DW_LANG_ObjC:
19099 cu->language = language_objc;
19100 break;
c44af4eb
TT
19101 case DW_LANG_Rust:
19102 case DW_LANG_Rust_old:
19103 cu->language = language_rust;
19104 break;
c906108c
SS
19105 case DW_LANG_Cobol74:
19106 case DW_LANG_Cobol85:
c906108c 19107 default:
e142c38c 19108 cu->language = language_minimal;
c906108c
SS
19109 break;
19110 }
e142c38c 19111 cu->language_defn = language_def (cu->language);
c906108c
SS
19112}
19113
19114/* Return the named attribute or NULL if not there. */
19115
19116static struct attribute *
e142c38c 19117dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19118{
a48e046c 19119 for (;;)
c906108c 19120 {
a48e046c
TT
19121 unsigned int i;
19122 struct attribute *spec = NULL;
19123
19124 for (i = 0; i < die->num_attrs; ++i)
19125 {
19126 if (die->attrs[i].name == name)
19127 return &die->attrs[i];
19128 if (die->attrs[i].name == DW_AT_specification
19129 || die->attrs[i].name == DW_AT_abstract_origin)
19130 spec = &die->attrs[i];
19131 }
19132
19133 if (!spec)
19134 break;
c906108c 19135
f2f0e013 19136 die = follow_die_ref (die, spec, &cu);
f2f0e013 19137 }
c5aa993b 19138
c906108c
SS
19139 return NULL;
19140}
19141
348e048f
DE
19142/* Return the named attribute or NULL if not there,
19143 but do not follow DW_AT_specification, etc.
19144 This is for use in contexts where we're reading .debug_types dies.
19145 Following DW_AT_specification, DW_AT_abstract_origin will take us
19146 back up the chain, and we want to go down. */
19147
19148static struct attribute *
45e58e77 19149dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19150{
19151 unsigned int i;
19152
19153 for (i = 0; i < die->num_attrs; ++i)
19154 if (die->attrs[i].name == name)
19155 return &die->attrs[i];
19156
19157 return NULL;
19158}
19159
7d45c7c3
KB
19160/* Return the string associated with a string-typed attribute, or NULL if it
19161 is either not found or is of an incorrect type. */
19162
19163static const char *
19164dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19165{
19166 struct attribute *attr;
19167 const char *str = NULL;
19168
19169 attr = dwarf2_attr (die, name, cu);
19170
19171 if (attr != NULL)
19172 {
43988095 19173 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19174 || attr->form == DW_FORM_string
cf532bd1 19175 || attr->form == DW_FORM_strx
8fe0f950
AT
19176 || attr->form == DW_FORM_strx1
19177 || attr->form == DW_FORM_strx2
19178 || attr->form == DW_FORM_strx3
19179 || attr->form == DW_FORM_strx4
b3340438 19180 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19181 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19182 str = DW_STRING (attr);
19183 else
b98664d3 19184 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19185 "DIE at %s in module %s"),
19186 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19187 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19188 }
19189
19190 return str;
19191}
19192
a084a2a6 19193/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19194 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19195static const char *
19196dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19197{
19198 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19199 if (dwo_name == nullptr)
19200 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19201 return dwo_name;
19202}
19203
05cf31d1
JB
19204/* Return non-zero iff the attribute NAME is defined for the given DIE,
19205 and holds a non-zero value. This function should only be used for
2dc7f7b3 19206 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19207
19208static int
19209dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19210{
19211 struct attribute *attr = dwarf2_attr (die, name, cu);
19212
19213 return (attr && DW_UNSND (attr));
19214}
19215
3ca72b44 19216static int
e142c38c 19217die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19218{
05cf31d1
JB
19219 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19220 which value is non-zero. However, we have to be careful with
19221 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19222 (via dwarf2_flag_true_p) follows this attribute. So we may
19223 end up accidently finding a declaration attribute that belongs
19224 to a different DIE referenced by the specification attribute,
19225 even though the given DIE does not have a declaration attribute. */
19226 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19227 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19228}
19229
63d06c5c 19230/* Return the die giving the specification for DIE, if there is
f2f0e013 19231 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19232 containing the return value on output. If there is no
19233 specification, but there is an abstract origin, that is
19234 returned. */
63d06c5c
DC
19235
19236static struct die_info *
f2f0e013 19237die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19238{
f2f0e013
DJ
19239 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19240 *spec_cu);
63d06c5c 19241
edb3359d
DJ
19242 if (spec_attr == NULL)
19243 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19244
63d06c5c
DC
19245 if (spec_attr == NULL)
19246 return NULL;
19247 else
f2f0e013 19248 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19249}
c906108c 19250
527f3840
JK
19251/* Stub for free_line_header to match void * callback types. */
19252
19253static void
19254free_line_header_voidp (void *arg)
19255{
9a3c8263 19256 struct line_header *lh = (struct line_header *) arg;
527f3840 19257
fff8551c 19258 delete lh;
527f3840
JK
19259}
19260
83769d0b 19261/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19262
19263static struct dwarf2_section_info *
19264get_debug_line_section (struct dwarf2_cu *cu)
19265{
19266 struct dwarf2_section_info *section;
518817b3
SM
19267 struct dwarf2_per_objfile *dwarf2_per_objfile
19268 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19269
19270 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19271 DWO file. */
19272 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19273 section = &cu->dwo_unit->dwo_file->sections.line;
19274 else if (cu->per_cu->is_dwz)
19275 {
ed2dc618 19276 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19277
19278 section = &dwz->line;
19279 }
19280 else
19281 section = &dwarf2_per_objfile->line;
19282
19283 return section;
19284}
19285
43988095
JK
19286/* Read directory or file name entry format, starting with byte of
19287 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19288 entries count and the entries themselves in the described entry
19289 format. */
19290
19291static void
ed2dc618
SM
19292read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19293 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19294 struct line_header *lh,
19295 const struct comp_unit_head *cu_header,
19296 void (*callback) (struct line_header *lh,
19297 const char *name,
ecfb656c 19298 dir_index d_index,
43988095
JK
19299 unsigned int mod_time,
19300 unsigned int length))
19301{
19302 gdb_byte format_count, formati;
19303 ULONGEST data_count, datai;
19304 const gdb_byte *buf = *bufp;
19305 const gdb_byte *format_header_data;
43988095
JK
19306 unsigned int bytes_read;
19307
19308 format_count = read_1_byte (abfd, buf);
19309 buf += 1;
19310 format_header_data = buf;
19311 for (formati = 0; formati < format_count; formati++)
19312 {
19313 read_unsigned_leb128 (abfd, buf, &bytes_read);
19314 buf += bytes_read;
19315 read_unsigned_leb128 (abfd, buf, &bytes_read);
19316 buf += bytes_read;
19317 }
19318
19319 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19320 buf += bytes_read;
19321 for (datai = 0; datai < data_count; datai++)
19322 {
19323 const gdb_byte *format = format_header_data;
19324 struct file_entry fe;
19325
43988095
JK
19326 for (formati = 0; formati < format_count; formati++)
19327 {
ecfb656c 19328 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19329 format += bytes_read;
43988095 19330
ecfb656c 19331 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19332 format += bytes_read;
ecfb656c
PA
19333
19334 gdb::optional<const char *> string;
19335 gdb::optional<unsigned int> uint;
19336
43988095
JK
19337 switch (form)
19338 {
19339 case DW_FORM_string:
ecfb656c 19340 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19341 buf += bytes_read;
19342 break;
19343
19344 case DW_FORM_line_strp:
ed2dc618
SM
19345 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19346 abfd, buf,
ecfb656c
PA
19347 cu_header,
19348 &bytes_read));
43988095
JK
19349 buf += bytes_read;
19350 break;
19351
19352 case DW_FORM_data1:
ecfb656c 19353 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19354 buf += 1;
19355 break;
19356
19357 case DW_FORM_data2:
ecfb656c 19358 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19359 buf += 2;
19360 break;
19361
19362 case DW_FORM_data4:
ecfb656c 19363 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19364 buf += 4;
19365 break;
19366
19367 case DW_FORM_data8:
ecfb656c 19368 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19369 buf += 8;
19370 break;
19371
7ba99d21
AT
19372 case DW_FORM_data16:
19373 /* This is used for MD5, but file_entry does not record MD5s. */
19374 buf += 16;
19375 break;
19376
43988095 19377 case DW_FORM_udata:
ecfb656c 19378 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19379 buf += bytes_read;
19380 break;
19381
19382 case DW_FORM_block:
19383 /* It is valid only for DW_LNCT_timestamp which is ignored by
19384 current GDB. */
19385 break;
19386 }
ecfb656c
PA
19387
19388 switch (content_type)
19389 {
19390 case DW_LNCT_path:
19391 if (string.has_value ())
19392 fe.name = *string;
19393 break;
19394 case DW_LNCT_directory_index:
19395 if (uint.has_value ())
19396 fe.d_index = (dir_index) *uint;
19397 break;
19398 case DW_LNCT_timestamp:
19399 if (uint.has_value ())
19400 fe.mod_time = *uint;
19401 break;
19402 case DW_LNCT_size:
19403 if (uint.has_value ())
19404 fe.length = *uint;
19405 break;
19406 case DW_LNCT_MD5:
19407 break;
19408 default:
b98664d3 19409 complaint (_("Unknown format content type %s"),
ecfb656c
PA
19410 pulongest (content_type));
19411 }
43988095
JK
19412 }
19413
ecfb656c 19414 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
19415 }
19416
19417 *bufp = buf;
19418}
19419
debd256d 19420/* Read the statement program header starting at OFFSET in
3019eac3 19421 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19422 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19423 Returns NULL if there is a problem reading the header, e.g., if it
19424 has a version we don't understand.
debd256d
JB
19425
19426 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19427 the returned object point into the dwarf line section buffer,
19428 and must not be freed. */
ae2de4f8 19429
fff8551c 19430static line_header_up
9c541725 19431dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19432{
d521ce57 19433 const gdb_byte *line_ptr;
c764a876 19434 unsigned int bytes_read, offset_size;
debd256d 19435 int i;
d521ce57 19436 const char *cur_dir, *cur_file;
3019eac3
DE
19437 struct dwarf2_section_info *section;
19438 bfd *abfd;
518817b3
SM
19439 struct dwarf2_per_objfile *dwarf2_per_objfile
19440 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19441
36586728 19442 section = get_debug_line_section (cu);
96b79293 19443 section->read (dwarf2_per_objfile->objfile);
3019eac3 19444 if (section->buffer == NULL)
debd256d 19445 {
3019eac3 19446 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19447 complaint (_("missing .debug_line.dwo section"));
3019eac3 19448 else
b98664d3 19449 complaint (_("missing .debug_line section"));
debd256d
JB
19450 return 0;
19451 }
19452
fceca515
DE
19453 /* We can't do this until we know the section is non-empty.
19454 Only then do we know we have such a section. */
96b79293 19455 abfd = section->get_bfd_owner ();
fceca515 19456
a738430d
MK
19457 /* Make sure that at least there's room for the total_length field.
19458 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 19459 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 19460 {
4d3c2250 19461 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19462 return 0;
19463 }
19464
fff8551c 19465 line_header_up lh (new line_header ());
debd256d 19466
9c541725 19467 lh->sect_off = sect_off;
527f3840
JK
19468 lh->offset_in_dwz = cu->per_cu->is_dwz;
19469
9c541725 19470 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 19471
a738430d 19472 /* Read in the header. */
6e70227d 19473 lh->total_length =
c764a876
DE
19474 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19475 &bytes_read, &offset_size);
debd256d 19476 line_ptr += bytes_read;
7ba99d21
AT
19477
19478 const gdb_byte *start_here = line_ptr;
19479
3019eac3 19480 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 19481 {
4d3c2250 19482 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19483 return 0;
19484 }
7ba99d21 19485 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
19486 lh->version = read_2_bytes (abfd, line_ptr);
19487 line_ptr += 2;
43988095 19488 if (lh->version > 5)
cd366ee8
DE
19489 {
19490 /* This is a version we don't understand. The format could have
19491 changed in ways we don't handle properly so just punt. */
b98664d3 19492 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
19493 return NULL;
19494 }
43988095
JK
19495 if (lh->version >= 5)
19496 {
19497 gdb_byte segment_selector_size;
19498
19499 /* Skip address size. */
19500 read_1_byte (abfd, line_ptr);
19501 line_ptr += 1;
19502
19503 segment_selector_size = read_1_byte (abfd, line_ptr);
19504 line_ptr += 1;
19505 if (segment_selector_size != 0)
19506 {
b98664d3 19507 complaint (_("unsupported segment selector size %u "
43988095
JK
19508 "in .debug_line section"),
19509 segment_selector_size);
19510 return NULL;
19511 }
19512 }
24aa364d 19513 lh->header_length = read_offset (abfd, line_ptr, offset_size);
c764a876 19514 line_ptr += offset_size;
7ba99d21 19515 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
19516 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19517 line_ptr += 1;
2dc7f7b3
TT
19518 if (lh->version >= 4)
19519 {
19520 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19521 line_ptr += 1;
19522 }
19523 else
19524 lh->maximum_ops_per_instruction = 1;
19525
19526 if (lh->maximum_ops_per_instruction == 0)
19527 {
19528 lh->maximum_ops_per_instruction = 1;
b98664d3 19529 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 19530 "in `.debug_line' section"));
2dc7f7b3
TT
19531 }
19532
debd256d
JB
19533 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19534 line_ptr += 1;
19535 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19536 line_ptr += 1;
19537 lh->line_range = read_1_byte (abfd, line_ptr);
19538 line_ptr += 1;
19539 lh->opcode_base = read_1_byte (abfd, line_ptr);
19540 line_ptr += 1;
fff8551c 19541 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
19542
19543 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19544 for (i = 1; i < lh->opcode_base; ++i)
19545 {
19546 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19547 line_ptr += 1;
19548 }
19549
43988095 19550 if (lh->version >= 5)
debd256d 19551 {
43988095 19552 /* Read directory table. */
ed2dc618
SM
19553 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19554 &cu->header,
b926417a 19555 [] (struct line_header *header, const char *name,
ecfb656c 19556 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19557 unsigned int length)
19558 {
b926417a 19559 header->add_include_dir (name);
fff8551c 19560 });
debd256d 19561
43988095 19562 /* Read file name table. */
ed2dc618
SM
19563 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19564 &cu->header,
b926417a 19565 [] (struct line_header *header, const char *name,
ecfb656c 19566 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19567 unsigned int length)
19568 {
b926417a 19569 header->add_file_name (name, d_index, mod_time, length);
fff8551c 19570 });
43988095
JK
19571 }
19572 else
debd256d 19573 {
43988095
JK
19574 /* Read directory table. */
19575 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19576 {
19577 line_ptr += bytes_read;
fff8551c 19578 lh->add_include_dir (cur_dir);
43988095 19579 }
debd256d
JB
19580 line_ptr += bytes_read;
19581
43988095
JK
19582 /* Read file name table. */
19583 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19584 {
ecfb656c
PA
19585 unsigned int mod_time, length;
19586 dir_index d_index;
43988095
JK
19587
19588 line_ptr += bytes_read;
ecfb656c 19589 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
19590 line_ptr += bytes_read;
19591 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19592 line_ptr += bytes_read;
19593 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19594 line_ptr += bytes_read;
19595
ecfb656c 19596 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
19597 }
19598 line_ptr += bytes_read;
debd256d 19599 }
debd256d 19600
3019eac3 19601 if (line_ptr > (section->buffer + section->size))
b98664d3 19602 complaint (_("line number info header doesn't "
3e43a32a 19603 "fit in `.debug_line' section"));
debd256d 19604
debd256d
JB
19605 return lh;
19606}
c906108c 19607
c6da4cef 19608/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19609 Return the file name of the psymtab for the given file_entry.
c6da4cef 19610 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19611 If space for the result is malloc'd, *NAME_HOLDER will be set.
19612 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19613
d521ce57 19614static const char *
7ba99d21 19615psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19616 const dwarf2_psymtab *pst,
c89b44cd
TT
19617 const char *comp_dir,
19618 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19619{
d521ce57
TT
19620 const char *include_name = fe.name;
19621 const char *include_name_to_compare = include_name;
72b9f47f 19622 const char *pst_filename;
c6da4cef
DE
19623 int file_is_pst;
19624
8c43009f 19625 const char *dir_name = fe.include_dir (lh);
c6da4cef 19626
c89b44cd 19627 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19628 if (!IS_ABSOLUTE_PATH (include_name)
19629 && (dir_name != NULL || comp_dir != NULL))
19630 {
19631 /* Avoid creating a duplicate psymtab for PST.
19632 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19633 Before we do the comparison, however, we need to account
19634 for DIR_NAME and COMP_DIR.
19635 First prepend dir_name (if non-NULL). If we still don't
19636 have an absolute path prepend comp_dir (if non-NULL).
19637 However, the directory we record in the include-file's
19638 psymtab does not contain COMP_DIR (to match the
19639 corresponding symtab(s)).
19640
19641 Example:
19642
19643 bash$ cd /tmp
19644 bash$ gcc -g ./hello.c
19645 include_name = "hello.c"
19646 dir_name = "."
19647 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19648 DW_AT_name = "./hello.c"
19649
19650 */
c6da4cef
DE
19651
19652 if (dir_name != NULL)
19653 {
c89b44cd
TT
19654 name_holder->reset (concat (dir_name, SLASH_STRING,
19655 include_name, (char *) NULL));
19656 include_name = name_holder->get ();
c6da4cef 19657 include_name_to_compare = include_name;
c6da4cef
DE
19658 }
19659 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19660 {
c89b44cd
TT
19661 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19662 include_name, (char *) NULL));
19663 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19664 }
19665 }
19666
19667 pst_filename = pst->filename;
c89b44cd 19668 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19669 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19670 {
c89b44cd
TT
19671 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19672 pst_filename, (char *) NULL));
19673 pst_filename = copied_name.get ();
c6da4cef
DE
19674 }
19675
1e3fad37 19676 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19677
c6da4cef
DE
19678 if (file_is_pst)
19679 return NULL;
19680 return include_name;
19681}
19682
d9b3de22
DE
19683/* State machine to track the state of the line number program. */
19684
6f77053d 19685class lnp_state_machine
d9b3de22 19686{
6f77053d
PA
19687public:
19688 /* Initialize a machine state for the start of a line number
19689 program. */
804d2729
TT
19690 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19691 bool record_lines_p);
6f77053d 19692
8c43009f
PA
19693 file_entry *current_file ()
19694 {
19695 /* lh->file_names is 0-based, but the file name numbers in the
19696 statement program are 1-based. */
6f77053d
PA
19697 return m_line_header->file_name_at (m_file);
19698 }
19699
19700 /* Record the line in the state machine. END_SEQUENCE is true if
19701 we're processing the end of a sequence. */
19702 void record_line (bool end_sequence);
19703
7ab6656f
OJ
19704 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19705 nop-out rest of the lines in this sequence. */
6f77053d
PA
19706 void check_line_address (struct dwarf2_cu *cu,
19707 const gdb_byte *line_ptr,
7ab6656f 19708 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19709
19710 void handle_set_discriminator (unsigned int discriminator)
19711 {
19712 m_discriminator = discriminator;
19713 m_line_has_non_zero_discriminator |= discriminator != 0;
19714 }
19715
19716 /* Handle DW_LNE_set_address. */
19717 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19718 {
19719 m_op_index = 0;
19720 address += baseaddr;
19721 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19722 }
19723
19724 /* Handle DW_LNS_advance_pc. */
19725 void handle_advance_pc (CORE_ADDR adjust);
19726
19727 /* Handle a special opcode. */
19728 void handle_special_opcode (unsigned char op_code);
19729
19730 /* Handle DW_LNS_advance_line. */
19731 void handle_advance_line (int line_delta)
19732 {
19733 advance_line (line_delta);
19734 }
19735
19736 /* Handle DW_LNS_set_file. */
19737 void handle_set_file (file_name_index file);
19738
19739 /* Handle DW_LNS_negate_stmt. */
19740 void handle_negate_stmt ()
19741 {
19742 m_is_stmt = !m_is_stmt;
19743 }
19744
19745 /* Handle DW_LNS_const_add_pc. */
19746 void handle_const_add_pc ();
19747
19748 /* Handle DW_LNS_fixed_advance_pc. */
19749 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19750 {
19751 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19752 m_op_index = 0;
19753 }
19754
19755 /* Handle DW_LNS_copy. */
19756 void handle_copy ()
19757 {
19758 record_line (false);
19759 m_discriminator = 0;
19760 }
19761
19762 /* Handle DW_LNE_end_sequence. */
19763 void handle_end_sequence ()
19764 {
804d2729 19765 m_currently_recording_lines = true;
6f77053d
PA
19766 }
19767
19768private:
19769 /* Advance the line by LINE_DELTA. */
19770 void advance_line (int line_delta)
19771 {
19772 m_line += line_delta;
19773
19774 if (line_delta != 0)
19775 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19776 }
19777
804d2729
TT
19778 struct dwarf2_cu *m_cu;
19779
6f77053d
PA
19780 gdbarch *m_gdbarch;
19781
19782 /* True if we're recording lines.
19783 Otherwise we're building partial symtabs and are just interested in
19784 finding include files mentioned by the line number program. */
19785 bool m_record_lines_p;
19786
8c43009f 19787 /* The line number header. */
6f77053d 19788 line_header *m_line_header;
8c43009f 19789
6f77053d
PA
19790 /* These are part of the standard DWARF line number state machine,
19791 and initialized according to the DWARF spec. */
d9b3de22 19792
6f77053d 19793 unsigned char m_op_index = 0;
7ba99d21
AT
19794 /* The line table index of the current file. */
19795 file_name_index m_file = 1;
6f77053d
PA
19796 unsigned int m_line = 1;
19797
19798 /* These are initialized in the constructor. */
19799
19800 CORE_ADDR m_address;
19801 bool m_is_stmt;
19802 unsigned int m_discriminator;
d9b3de22
DE
19803
19804 /* Additional bits of state we need to track. */
19805
19806 /* The last file that we called dwarf2_start_subfile for.
19807 This is only used for TLLs. */
6f77053d 19808 unsigned int m_last_file = 0;
d9b3de22 19809 /* The last file a line number was recorded for. */
6f77053d 19810 struct subfile *m_last_subfile = NULL;
d9b3de22 19811
804d2729
TT
19812 /* When true, record the lines we decode. */
19813 bool m_currently_recording_lines = false;
d9b3de22
DE
19814
19815 /* The last line number that was recorded, used to coalesce
19816 consecutive entries for the same line. This can happen, for
19817 example, when discriminators are present. PR 17276. */
6f77053d
PA
19818 unsigned int m_last_line = 0;
19819 bool m_line_has_non_zero_discriminator = false;
8c43009f 19820};
d9b3de22 19821
6f77053d
PA
19822void
19823lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19824{
19825 CORE_ADDR addr_adj = (((m_op_index + adjust)
19826 / m_line_header->maximum_ops_per_instruction)
19827 * m_line_header->minimum_instruction_length);
19828 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19829 m_op_index = ((m_op_index + adjust)
19830 % m_line_header->maximum_ops_per_instruction);
19831}
d9b3de22 19832
6f77053d
PA
19833void
19834lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19835{
6f77053d 19836 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19837 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19838 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19839 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19840 / m_line_header->maximum_ops_per_instruction)
19841 * m_line_header->minimum_instruction_length);
19842 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19843 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19844 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19845
258bf0ee 19846 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19847 advance_line (line_delta);
19848 record_line (false);
19849 m_discriminator = 0;
19850}
d9b3de22 19851
6f77053d
PA
19852void
19853lnp_state_machine::handle_set_file (file_name_index file)
19854{
19855 m_file = file;
19856
19857 const file_entry *fe = current_file ();
19858 if (fe == NULL)
19859 dwarf2_debug_line_missing_file_complaint ();
19860 else if (m_record_lines_p)
19861 {
19862 const char *dir = fe->include_dir (m_line_header);
19863
c24bdb02 19864 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19865 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19866 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19867 }
19868}
19869
19870void
19871lnp_state_machine::handle_const_add_pc ()
19872{
19873 CORE_ADDR adjust
19874 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19875
19876 CORE_ADDR addr_adj
19877 = (((m_op_index + adjust)
19878 / m_line_header->maximum_ops_per_instruction)
19879 * m_line_header->minimum_instruction_length);
19880
19881 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19882 m_op_index = ((m_op_index + adjust)
19883 % m_line_header->maximum_ops_per_instruction);
19884}
d9b3de22 19885
a05a36a5
DE
19886/* Return non-zero if we should add LINE to the line number table.
19887 LINE is the line to add, LAST_LINE is the last line that was added,
19888 LAST_SUBFILE is the subfile for LAST_LINE.
19889 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19890 had a non-zero discriminator.
19891
19892 We have to be careful in the presence of discriminators.
19893 E.g., for this line:
19894
19895 for (i = 0; i < 100000; i++);
19896
19897 clang can emit four line number entries for that one line,
19898 each with a different discriminator.
19899 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19900
19901 However, we want gdb to coalesce all four entries into one.
19902 Otherwise the user could stepi into the middle of the line and
19903 gdb would get confused about whether the pc really was in the
19904 middle of the line.
19905
19906 Things are further complicated by the fact that two consecutive
19907 line number entries for the same line is a heuristic used by gcc
19908 to denote the end of the prologue. So we can't just discard duplicate
19909 entries, we have to be selective about it. The heuristic we use is
19910 that we only collapse consecutive entries for the same line if at least
19911 one of those entries has a non-zero discriminator. PR 17276.
19912
19913 Note: Addresses in the line number state machine can never go backwards
19914 within one sequence, thus this coalescing is ok. */
19915
19916static int
804d2729
TT
19917dwarf_record_line_p (struct dwarf2_cu *cu,
19918 unsigned int line, unsigned int last_line,
a05a36a5
DE
19919 int line_has_non_zero_discriminator,
19920 struct subfile *last_subfile)
19921{
c24bdb02 19922 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19923 return 1;
19924 if (line != last_line)
19925 return 1;
19926 /* Same line for the same file that we've seen already.
19927 As a last check, for pr 17276, only record the line if the line
19928 has never had a non-zero discriminator. */
19929 if (!line_has_non_zero_discriminator)
19930 return 1;
19931 return 0;
19932}
19933
804d2729
TT
19934/* Use the CU's builder to record line number LINE beginning at
19935 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19936
19937static void
d9b3de22
DE
19938dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19939 unsigned int line, CORE_ADDR address,
804d2729 19940 struct dwarf2_cu *cu)
252a6764
DE
19941{
19942 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19943
27e0867f
DE
19944 if (dwarf_line_debug)
19945 {
19946 fprintf_unfiltered (gdb_stdlog,
19947 "Recording line %u, file %s, address %s\n",
19948 line, lbasename (subfile->name),
19949 paddress (gdbarch, address));
19950 }
19951
804d2729 19952 if (cu != nullptr)
c24bdb02 19953 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
19954}
19955
19956/* Subroutine of dwarf_decode_lines_1 to simplify it.
19957 Mark the end of a set of line number records.
d9b3de22 19958 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19959 If SUBFILE is NULL the request is ignored. */
19960
19961static void
19962dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19963 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19964{
27e0867f
DE
19965 if (subfile == NULL)
19966 return;
19967
19968 if (dwarf_line_debug)
19969 {
19970 fprintf_unfiltered (gdb_stdlog,
19971 "Finishing current line, file %s, address %s\n",
19972 lbasename (subfile->name),
19973 paddress (gdbarch, address));
19974 }
19975
804d2729 19976 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
19977}
19978
6f77053d
PA
19979void
19980lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19981{
d9b3de22
DE
19982 if (dwarf_line_debug)
19983 {
19984 fprintf_unfiltered (gdb_stdlog,
19985 "Processing actual line %u: file %u,"
94a72be7 19986 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19987 m_line, m_file,
6f77053d 19988 paddress (m_gdbarch, m_address),
94a72be7
AB
19989 m_is_stmt, m_discriminator,
19990 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19991 }
19992
6f77053d 19993 file_entry *fe = current_file ();
8c43009f
PA
19994
19995 if (fe == NULL)
d9b3de22
DE
19996 dwarf2_debug_line_missing_file_complaint ();
19997 /* For now we ignore lines not starting on an instruction boundary.
19998 But not when processing end_sequence for compatibility with the
19999 previous version of the code. */
6f77053d 20000 else if (m_op_index == 0 || end_sequence)
d9b3de22 20001 {
8c43009f 20002 fe->included_p = 1;
94a72be7
AB
20003 if (m_record_lines_p
20004 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 20005 {
c24bdb02 20006 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20007 || end_sequence)
d9b3de22 20008 {
804d2729
TT
20009 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20010 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20011 }
20012
20013 if (!end_sequence)
20014 {
804d2729 20015 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20016 m_line_has_non_zero_discriminator,
20017 m_last_subfile))
d9b3de22 20018 {
c24bdb02 20019 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20020 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20021 builder->get_current_subfile (),
6f77053d 20022 m_line, m_address,
804d2729 20023 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20024 }
c24bdb02 20025 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20026 m_last_line = m_line;
d9b3de22
DE
20027 }
20028 }
20029 }
20030}
20031
804d2729
TT
20032lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20033 line_header *lh, bool record_lines_p)
d9b3de22 20034{
804d2729 20035 m_cu = cu;
6f77053d
PA
20036 m_gdbarch = arch;
20037 m_record_lines_p = record_lines_p;
20038 m_line_header = lh;
d9b3de22 20039
804d2729 20040 m_currently_recording_lines = true;
d9b3de22 20041
d9b3de22
DE
20042 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20043 was a line entry for it so that the backend has a chance to adjust it
20044 and also record it in case it needs it. This is currently used by MIPS
20045 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20046 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20047 m_is_stmt = lh->default_is_stmt;
20048 m_discriminator = 0;
252a6764
DE
20049}
20050
6f77053d
PA
20051void
20052lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20053 const gdb_byte *line_ptr,
7ab6656f 20054 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20055{
7ab6656f
OJ
20056 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20057 the pc range of the CU. However, we restrict the test to only ADDRESS
20058 values of zero to preserve GDB's previous behaviour which is to handle
20059 the specific case of a function being GC'd by the linker. */
924c2928 20060
7ab6656f 20061 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20062 {
20063 /* This line table is for a function which has been
20064 GCd by the linker. Ignore it. PR gdb/12528 */
20065
518817b3 20066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20067 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20068
b98664d3 20069 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20070 line_offset, objfile_name (objfile));
804d2729
TT
20071 m_currently_recording_lines = false;
20072 /* Note: m_currently_recording_lines is left as false until we see
20073 DW_LNE_end_sequence. */
924c2928
DE
20074 }
20075}
20076
f3f5162e 20077/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20078 Process the line number information in LH.
20079 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20080 program in order to set included_p for every referenced header. */
debd256d 20081
c906108c 20082static void
43f3e411
DE
20083dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20084 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20085{
d521ce57
TT
20086 const gdb_byte *line_ptr, *extended_end;
20087 const gdb_byte *line_end;
a8c50c1f 20088 unsigned int bytes_read, extended_len;
699ca60a 20089 unsigned char op_code, extended_op;
e142c38c 20090 CORE_ADDR baseaddr;
518817b3 20091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20092 bfd *abfd = objfile->obfd;
fbf65064 20093 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20094 /* True if we're recording line info (as opposed to building partial
20095 symtabs and just interested in finding include files mentioned by
20096 the line number program). */
20097 bool record_lines_p = !decode_for_pst_p;
e142c38c 20098
b3b3bada 20099 baseaddr = objfile->text_section_offset ();
c906108c 20100
debd256d
JB
20101 line_ptr = lh->statement_program_start;
20102 line_end = lh->statement_program_end;
c906108c
SS
20103
20104 /* Read the statement sequences until there's nothing left. */
20105 while (line_ptr < line_end)
20106 {
6f77053d
PA
20107 /* The DWARF line number program state machine. Reset the state
20108 machine at the start of each sequence. */
804d2729 20109 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20110 bool end_sequence = false;
d9b3de22 20111
8c43009f 20112 if (record_lines_p)
c906108c 20113 {
8c43009f
PA
20114 /* Start a subfile for the current file of the state
20115 machine. */
20116 const file_entry *fe = state_machine.current_file ();
20117
20118 if (fe != NULL)
804d2729 20119 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20120 }
20121
a738430d 20122 /* Decode the table. */
d9b3de22 20123 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20124 {
20125 op_code = read_1_byte (abfd, line_ptr);
20126 line_ptr += 1;
9aa1fe7e 20127
debd256d 20128 if (op_code >= lh->opcode_base)
6e70227d 20129 {
8e07a239 20130 /* Special opcode. */
6f77053d 20131 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20132 }
20133 else switch (op_code)
c906108c
SS
20134 {
20135 case DW_LNS_extended_op:
3e43a32a
MS
20136 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20137 &bytes_read);
473b7be6 20138 line_ptr += bytes_read;
a8c50c1f 20139 extended_end = line_ptr + extended_len;
c906108c
SS
20140 extended_op = read_1_byte (abfd, line_ptr);
20141 line_ptr += 1;
20142 switch (extended_op)
20143 {
20144 case DW_LNE_end_sequence:
6f77053d
PA
20145 state_machine.handle_end_sequence ();
20146 end_sequence = true;
c906108c
SS
20147 break;
20148 case DW_LNE_set_address:
d9b3de22
DE
20149 {
20150 CORE_ADDR address
c8a7a66f 20151 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20152 line_ptr += bytes_read;
6f77053d
PA
20153
20154 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20155 lowpc - baseaddr, address);
6f77053d 20156 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20157 }
c906108c
SS
20158 break;
20159 case DW_LNE_define_file:
debd256d 20160 {
d521ce57 20161 const char *cur_file;
ecfb656c
PA
20162 unsigned int mod_time, length;
20163 dir_index dindex;
6e70227d 20164
3e43a32a
MS
20165 cur_file = read_direct_string (abfd, line_ptr,
20166 &bytes_read);
debd256d 20167 line_ptr += bytes_read;
ecfb656c 20168 dindex = (dir_index)
debd256d
JB
20169 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20170 line_ptr += bytes_read;
20171 mod_time =
20172 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20173 line_ptr += bytes_read;
20174 length =
20175 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20176 line_ptr += bytes_read;
ecfb656c 20177 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20178 }
c906108c 20179 break;
d0c6ba3d 20180 case DW_LNE_set_discriminator:
6f77053d
PA
20181 {
20182 /* The discriminator is not interesting to the
20183 debugger; just ignore it. We still need to
20184 check its value though:
20185 if there are consecutive entries for the same
20186 (non-prologue) line we want to coalesce them.
20187 PR 17276. */
20188 unsigned int discr
20189 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20190 line_ptr += bytes_read;
20191
20192 state_machine.handle_set_discriminator (discr);
20193 }
d0c6ba3d 20194 break;
c906108c 20195 default:
b98664d3 20196 complaint (_("mangled .debug_line section"));
debd256d 20197 return;
c906108c 20198 }
a8c50c1f
DJ
20199 /* Make sure that we parsed the extended op correctly. If e.g.
20200 we expected a different address size than the producer used,
20201 we may have read the wrong number of bytes. */
20202 if (line_ptr != extended_end)
20203 {
b98664d3 20204 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20205 return;
20206 }
c906108c
SS
20207 break;
20208 case DW_LNS_copy:
6f77053d 20209 state_machine.handle_copy ();
c906108c
SS
20210 break;
20211 case DW_LNS_advance_pc:
2dc7f7b3
TT
20212 {
20213 CORE_ADDR adjust
20214 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20215 line_ptr += bytes_read;
6f77053d
PA
20216
20217 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20218 }
c906108c
SS
20219 break;
20220 case DW_LNS_advance_line:
a05a36a5
DE
20221 {
20222 int line_delta
20223 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20224 line_ptr += bytes_read;
6f77053d
PA
20225
20226 state_machine.handle_advance_line (line_delta);
a05a36a5 20227 }
c906108c
SS
20228 break;
20229 case DW_LNS_set_file:
d9b3de22 20230 {
6f77053d 20231 file_name_index file
ecfb656c
PA
20232 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20233 &bytes_read);
d9b3de22 20234 line_ptr += bytes_read;
8c43009f 20235
6f77053d 20236 state_machine.handle_set_file (file);
d9b3de22 20237 }
c906108c
SS
20238 break;
20239 case DW_LNS_set_column:
0ad93d4f 20240 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20241 line_ptr += bytes_read;
20242 break;
20243 case DW_LNS_negate_stmt:
6f77053d 20244 state_machine.handle_negate_stmt ();
c906108c
SS
20245 break;
20246 case DW_LNS_set_basic_block:
c906108c 20247 break;
c2c6d25f
JM
20248 /* Add to the address register of the state machine the
20249 address increment value corresponding to special opcode
a738430d
MK
20250 255. I.e., this value is scaled by the minimum
20251 instruction length since special opcode 255 would have
b021a221 20252 scaled the increment. */
c906108c 20253 case DW_LNS_const_add_pc:
6f77053d 20254 state_machine.handle_const_add_pc ();
c906108c
SS
20255 break;
20256 case DW_LNS_fixed_advance_pc:
3e29f34a 20257 {
6f77053d 20258 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20259 line_ptr += 2;
6f77053d
PA
20260
20261 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20262 }
c906108c 20263 break;
9aa1fe7e 20264 default:
a738430d
MK
20265 {
20266 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20267 int i;
a738430d 20268
debd256d 20269 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20270 {
20271 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20272 line_ptr += bytes_read;
20273 }
20274 }
c906108c
SS
20275 }
20276 }
d9b3de22
DE
20277
20278 if (!end_sequence)
20279 dwarf2_debug_line_missing_end_sequence_complaint ();
20280
20281 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20282 in which case we still finish recording the last line). */
6f77053d 20283 state_machine.record_line (true);
c906108c 20284 }
f3f5162e
DE
20285}
20286
20287/* Decode the Line Number Program (LNP) for the given line_header
20288 structure and CU. The actual information extracted and the type
20289 of structures created from the LNP depends on the value of PST.
20290
20291 1. If PST is NULL, then this procedure uses the data from the program
20292 to create all necessary symbol tables, and their linetables.
20293
20294 2. If PST is not NULL, this procedure reads the program to determine
20295 the list of files included by the unit represented by PST, and
20296 builds all the associated partial symbol tables.
20297
20298 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20299 It is used for relative paths in the line table.
20300 NOTE: When processing partial symtabs (pst != NULL),
20301 comp_dir == pst->dirname.
20302
20303 NOTE: It is important that psymtabs have the same file name (via strcmp)
20304 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20305 symtab we don't use it in the name of the psymtabs we create.
20306 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20307 A good testcase for this is mb-inline.exp.
20308
527f3840
JK
20309 LOWPC is the lowest address in CU (or 0 if not known).
20310
20311 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20312 for its PC<->lines mapping information. Otherwise only the filename
20313 table is read in. */
f3f5162e
DE
20314
20315static void
20316dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20317 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20318 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20319{
518817b3 20320 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20321 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20322
527f3840
JK
20323 if (decode_mapping)
20324 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20325
20326 if (decode_for_pst_p)
20327 {
aaa75496
JB
20328 /* Now that we're done scanning the Line Header Program, we can
20329 create the psymtab of each included file. */
7ba99d21
AT
20330 for (auto &file_entry : lh->file_names ())
20331 if (file_entry.included_p == 1)
aaa75496 20332 {
c89b44cd 20333 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20334 const char *include_name =
7ba99d21
AT
20335 psymtab_include_file_name (lh, file_entry, pst,
20336 comp_dir, &name_holder);
c6da4cef 20337 if (include_name != NULL)
aaa75496
JB
20338 dwarf2_create_include_psymtab (include_name, pst, objfile);
20339 }
20340 }
cb1df416
DJ
20341 else
20342 {
20343 /* Make sure a symtab is created for every file, even files
20344 which contain only variables (i.e. no code with associated
20345 line numbers). */
c24bdb02
KS
20346 buildsym_compunit *builder = cu->get_builder ();
20347 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20348
7ba99d21 20349 for (auto &fe : lh->file_names ())
cb1df416 20350 {
804d2729 20351 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20352 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20353 {
c24bdb02 20354 builder->get_current_subfile ()->symtab
804d2729 20355 = allocate_symtab (cust,
c24bdb02 20356 builder->get_current_subfile ()->name);
43f3e411 20357 }
c24bdb02 20358 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20359 }
20360 }
c906108c
SS
20361}
20362
20363/* Start a subfile for DWARF. FILENAME is the name of the file and
20364 DIRNAME the name of the source directory which contains FILENAME
4d663531 20365 or NULL if not known.
c906108c
SS
20366 This routine tries to keep line numbers from identical absolute and
20367 relative file names in a common subfile.
20368
20369 Using the `list' example from the GDB testsuite, which resides in
20370 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20371 of /srcdir/list0.c yields the following debugging information for list0.c:
20372
c5aa993b 20373 DW_AT_name: /srcdir/list0.c
4d663531 20374 DW_AT_comp_dir: /compdir
357e46e7 20375 files.files[0].name: list0.h
c5aa993b 20376 files.files[0].dir: /srcdir
357e46e7 20377 files.files[1].name: list0.c
c5aa993b 20378 files.files[1].dir: /srcdir
c906108c
SS
20379
20380 The line number information for list0.c has to end up in a single
4f1520fb
FR
20381 subfile, so that `break /srcdir/list0.c:1' works as expected.
20382 start_subfile will ensure that this happens provided that we pass the
20383 concatenation of files.files[1].dir and files.files[1].name as the
20384 subfile's name. */
c906108c
SS
20385
20386static void
804d2729
TT
20387dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20388 const char *dirname)
c906108c 20389{
43816ebc 20390 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20391
4d663531 20392 /* In order not to lose the line information directory,
4f1520fb
FR
20393 we concatenate it to the filename when it makes sense.
20394 Note that the Dwarf3 standard says (speaking of filenames in line
20395 information): ``The directory index is ignored for file names
20396 that represent full path names''. Thus ignoring dirname in the
20397 `else' branch below isn't an issue. */
c906108c 20398
d5166ae1 20399 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20400 {
43816ebc
TT
20401 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20402 filename = copy.get ();
d521ce57 20403 }
c906108c 20404
c24bdb02 20405 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20406}
20407
804d2729
TT
20408/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20409 buildsym_compunit constructor. */
f4dc4d17 20410
c24bdb02
KS
20411struct compunit_symtab *
20412dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20413 CORE_ADDR low_pc)
f4dc4d17 20414{
c24bdb02 20415 gdb_assert (m_builder == nullptr);
43f3e411 20416
c24bdb02
KS
20417 m_builder.reset (new struct buildsym_compunit
20418 (per_cu->dwarf2_per_objfile->objfile,
20419 name, comp_dir, language, low_pc));
93b8bea4 20420
c24bdb02 20421 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20422
c24bdb02
KS
20423 get_builder ()->record_debugformat ("DWARF 2");
20424 get_builder ()->record_producer (producer);
f4dc4d17 20425
c24bdb02 20426 processing_has_namespace_info = false;
43f3e411 20427
c24bdb02 20428 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20429}
20430
4c2df51b
DJ
20431static void
20432var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20433 struct dwarf2_cu *cu)
4c2df51b 20434{
518817b3 20435 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20436 struct comp_unit_head *cu_header = &cu->header;
20437
4c2df51b
DJ
20438 /* NOTE drow/2003-01-30: There used to be a comment and some special
20439 code here to turn a symbol with DW_AT_external and a
20440 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20441 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20442 with some versions of binutils) where shared libraries could have
20443 relocations against symbols in their debug information - the
20444 minimal symbol would have the right address, but the debug info
20445 would not. It's no longer necessary, because we will explicitly
20446 apply relocations when we read in the debug information now. */
20447
20448 /* A DW_AT_location attribute with no contents indicates that a
20449 variable has been optimized away. */
4fc6c0d5 20450 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20451 {
f1e6e072 20452 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20453 return;
20454 }
20455
20456 /* Handle one degenerate form of location expression specially, to
20457 preserve GDB's previous behavior when section offsets are
336d760d
AT
20458 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20459 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20460
4fc6c0d5 20461 if (attr->form_is_block ()
3019eac3
DE
20462 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20463 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20464 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20465 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20466 && (DW_BLOCK (attr)->size
20467 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20468 {
891d2f0b 20469 unsigned int dummy;
4c2df51b 20470
3019eac3 20471 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20472 SET_SYMBOL_VALUE_ADDRESS
20473 (sym, cu->header.read_address (objfile->obfd,
20474 DW_BLOCK (attr)->data + 1,
20475 &dummy));
3019eac3 20476 else
38583298
TT
20477 SET_SYMBOL_VALUE_ADDRESS
20478 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20479 &dummy));
f1e6e072 20480 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20481 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20482 SET_SYMBOL_VALUE_ADDRESS
20483 (sym,
20484 SYMBOL_VALUE_ADDRESS (sym)
20485 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20486 return;
20487 }
20488
20489 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20490 expression evaluator, and use LOC_COMPUTED only when necessary
20491 (i.e. when the value of a register or memory location is
20492 referenced, or a thread-local block, etc.). Then again, it might
20493 not be worthwhile. I'm assuming that it isn't unless performance
20494 or memory numbers show me otherwise. */
20495
f1e6e072 20496 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20497
f1e6e072 20498 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20499 cu->has_loclist = true;
4c2df51b
DJ
20500}
20501
c906108c
SS
20502/* Given a pointer to a DWARF information entry, figure out if we need
20503 to make a symbol table entry for it, and if so, create a new entry
20504 and return a pointer to it.
20505 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20506 used the passed type.
20507 If SPACE is not NULL, use it to hold the new symbol. If it is
20508 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20509
20510static struct symbol *
5e2db402
TT
20511new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20512 struct symbol *space)
c906108c 20513{
518817b3
SM
20514 struct dwarf2_per_objfile *dwarf2_per_objfile
20515 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20516 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20518 struct symbol *sym = NULL;
15d034d0 20519 const char *name;
c906108c
SS
20520 struct attribute *attr = NULL;
20521 struct attribute *attr2 = NULL;
e142c38c 20522 CORE_ADDR baseaddr;
e37fd15a
SW
20523 struct pending **list_to_add = NULL;
20524
edb3359d 20525 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20526
b3b3bada 20527 baseaddr = objfile->text_section_offset ();
c906108c 20528
94af9270 20529 name = dwarf2_name (die, cu);
c906108c
SS
20530 if (name)
20531 {
94af9270 20532 const char *linkagename;
34eaf542 20533 int suppress_add = 0;
94af9270 20534
34eaf542
TT
20535 if (space)
20536 sym = space;
20537 else
e623cf5d 20538 sym = allocate_symbol (objfile);
c906108c 20539 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20540
20541 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20542 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20543 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20544 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20545
f55ee35c
JK
20546 /* Fortran does not have mangling standard and the mangling does differ
20547 between gfortran, iFort etc. */
20548 if (cu->language == language_fortran
468c0cbb
CB
20549 && symbol_get_demangled_name (sym) == NULL)
20550 symbol_set_demangled_name (sym,
cfc594ee 20551 dwarf2_full_name (name, die, cu),
29df156d 20552 NULL);
f55ee35c 20553
c906108c 20554 /* Default assumptions.
c5aa993b 20555 Use the passed type or decode it from the die. */
176620f1 20556 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20557 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20558 if (type != NULL)
20559 SYMBOL_TYPE (sym) = type;
20560 else
e7c27a73 20561 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20562 attr = dwarf2_attr (die,
20563 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20564 cu);
435d3d88 20565 if (attr != nullptr)
c906108c
SS
20566 {
20567 SYMBOL_LINE (sym) = DW_UNSND (attr);
20568 }
cb1df416 20569
edb3359d
DJ
20570 attr = dwarf2_attr (die,
20571 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20572 cu);
435d3d88 20573 if (attr != nullptr)
cb1df416 20574 {
ecfb656c 20575 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20576 struct file_entry *fe;
9a619af0 20577
ecfb656c
PA
20578 if (cu->line_header != NULL)
20579 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20580 else
20581 fe = NULL;
20582
20583 if (fe == NULL)
b98664d3 20584 complaint (_("file index out of range"));
8c43009f
PA
20585 else
20586 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20587 }
20588
c906108c
SS
20589 switch (die->tag)
20590 {
20591 case DW_TAG_label:
e142c38c 20592 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20593 if (attr != nullptr)
3e29f34a
MR
20594 {
20595 CORE_ADDR addr;
20596
cd6c91b4 20597 addr = attr->value_as_address ();
3e29f34a 20598 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20599 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20600 }
0f5238ed
TT
20601 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20602 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20603 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20604 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20605 break;
20606 case DW_TAG_subprogram:
20607 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20608 finish_block. */
f1e6e072 20609 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20610 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20611 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20612 || cu->language == language_ada
20613 || cu->language == language_fortran)
c906108c 20614 {
2cfa0c8d 20615 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20616 Ada and Fortran subprograms, whether marked external or
20617 not, are always stored as a global symbol, because we want
20618 to be able to access them globally. For instance, we want
20619 to be able to break on a nested subprogram without having
20620 to specify the context. */
c24bdb02 20621 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20622 }
20623 else
20624 {
e37fd15a 20625 list_to_add = cu->list_in_scope;
c906108c
SS
20626 }
20627 break;
edb3359d
DJ
20628 case DW_TAG_inlined_subroutine:
20629 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20630 finish_block. */
f1e6e072 20631 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20632 SYMBOL_INLINED (sym) = 1;
481860b3 20633 list_to_add = cu->list_in_scope;
edb3359d 20634 break;
34eaf542
TT
20635 case DW_TAG_template_value_param:
20636 suppress_add = 1;
20637 /* Fall through. */
72929c62 20638 case DW_TAG_constant:
c906108c 20639 case DW_TAG_variable:
254e6b9e 20640 case DW_TAG_member:
0963b4bd
MS
20641 /* Compilation with minimal debug info may result in
20642 variables with missing type entries. Change the
20643 misleading `void' type to something sensible. */
c906108c 20644 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20645 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20646
e142c38c 20647 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20648 /* In the case of DW_TAG_member, we should only be called for
20649 static const members. */
20650 if (die->tag == DW_TAG_member)
20651 {
3863f96c
DE
20652 /* dwarf2_add_field uses die_is_declaration,
20653 so we do the same. */
254e6b9e
DE
20654 gdb_assert (die_is_declaration (die, cu));
20655 gdb_assert (attr);
20656 }
435d3d88 20657 if (attr != nullptr)
c906108c 20658 {
e7c27a73 20659 dwarf2_const_value (attr, sym, cu);
e142c38c 20660 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20661 if (!suppress_add)
34eaf542
TT
20662 {
20663 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20664 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20665 else
e37fd15a 20666 list_to_add = cu->list_in_scope;
34eaf542 20667 }
c906108c
SS
20668 break;
20669 }
e142c38c 20670 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20671 if (attr != nullptr)
c906108c 20672 {
e7c27a73 20673 var_decode_location (attr, sym, cu);
e142c38c 20674 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20675
20676 /* Fortran explicitly imports any global symbols to the local
20677 scope by DW_TAG_common_block. */
20678 if (cu->language == language_fortran && die->parent
20679 && die->parent->tag == DW_TAG_common_block)
20680 attr2 = NULL;
20681
caac4577
JG
20682 if (SYMBOL_CLASS (sym) == LOC_STATIC
20683 && SYMBOL_VALUE_ADDRESS (sym) == 0
20684 && !dwarf2_per_objfile->has_section_at_zero)
20685 {
20686 /* When a static variable is eliminated by the linker,
20687 the corresponding debug information is not stripped
20688 out, but the variable address is set to null;
20689 do not add such variables into symbol table. */
20690 }
20691 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20692 {
4b610737
TT
20693 if (SYMBOL_CLASS (sym) == LOC_STATIC
20694 && (objfile->flags & OBJF_MAINLINE) == 0
20695 && dwarf2_per_objfile->can_copy)
20696 {
20697 /* A global static variable might be subject to
20698 copy relocation. We first check for a local
20699 minsym, though, because maybe the symbol was
20700 marked hidden, in which case this would not
20701 apply. */
20702 bound_minimal_symbol found
20703 = (lookup_minimal_symbol_linkage
987012b8 20704 (sym->linkage_name (), objfile));
4b610737
TT
20705 if (found.minsym != nullptr)
20706 sym->maybe_copied = 1;
20707 }
f55ee35c 20708
1c809c68
TT
20709 /* A variable with DW_AT_external is never static,
20710 but it may be block-scoped. */
804d2729 20711 list_to_add
c24bdb02
KS
20712 = ((cu->list_in_scope
20713 == cu->get_builder ()->get_file_symbols ())
20714 ? cu->get_builder ()->get_global_symbols ()
804d2729 20715 : cu->list_in_scope);
1c809c68 20716 }
c906108c 20717 else
e37fd15a 20718 list_to_add = cu->list_in_scope;
c906108c
SS
20719 }
20720 else
20721 {
20722 /* We do not know the address of this symbol.
c5aa993b
JM
20723 If it is an external symbol and we have type information
20724 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20725 The address of the variable will then be determined from
20726 the minimal symbol table whenever the variable is
20727 referenced. */
e142c38c 20728 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20729
20730 /* Fortran explicitly imports any global symbols to the local
20731 scope by DW_TAG_common_block. */
20732 if (cu->language == language_fortran && die->parent
20733 && die->parent->tag == DW_TAG_common_block)
20734 {
20735 /* SYMBOL_CLASS doesn't matter here because
20736 read_common_block is going to reset it. */
20737 if (!suppress_add)
20738 list_to_add = cu->list_in_scope;
20739 }
20740 else if (attr2 && (DW_UNSND (attr2) != 0)
20741 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20742 {
0fe7935b
DJ
20743 /* A variable with DW_AT_external is never static, but it
20744 may be block-scoped. */
804d2729 20745 list_to_add
c24bdb02
KS
20746 = ((cu->list_in_scope
20747 == cu->get_builder ()->get_file_symbols ())
20748 ? cu->get_builder ()->get_global_symbols ()
804d2729 20749 : cu->list_in_scope);
0fe7935b 20750
f1e6e072 20751 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20752 }
442ddf59
JK
20753 else if (!die_is_declaration (die, cu))
20754 {
20755 /* Use the default LOC_OPTIMIZED_OUT class. */
20756 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20757 if (!suppress_add)
20758 list_to_add = cu->list_in_scope;
442ddf59 20759 }
c906108c
SS
20760 }
20761 break;
20762 case DW_TAG_formal_parameter:
a60f3166
TT
20763 {
20764 /* If we are inside a function, mark this as an argument. If
20765 not, we might be looking at an argument to an inlined function
20766 when we do not have enough information to show inlined frames;
20767 pretend it's a local variable in that case so that the user can
20768 still see it. */
804d2729 20769 struct context_stack *curr
c24bdb02 20770 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20771 if (curr != nullptr && curr->name != nullptr)
20772 SYMBOL_IS_ARGUMENT (sym) = 1;
20773 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20774 if (attr != nullptr)
a60f3166
TT
20775 {
20776 var_decode_location (attr, sym, cu);
20777 }
20778 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20779 if (attr != nullptr)
a60f3166
TT
20780 {
20781 dwarf2_const_value (attr, sym, cu);
20782 }
f346a30d 20783
a60f3166
TT
20784 list_to_add = cu->list_in_scope;
20785 }
c906108c
SS
20786 break;
20787 case DW_TAG_unspecified_parameters:
20788 /* From varargs functions; gdb doesn't seem to have any
20789 interest in this information, so just ignore it for now.
20790 (FIXME?) */
20791 break;
34eaf542
TT
20792 case DW_TAG_template_type_param:
20793 suppress_add = 1;
20794 /* Fall through. */
c906108c 20795 case DW_TAG_class_type:
680b30c7 20796 case DW_TAG_interface_type:
c906108c
SS
20797 case DW_TAG_structure_type:
20798 case DW_TAG_union_type:
72019c9c 20799 case DW_TAG_set_type:
c906108c 20800 case DW_TAG_enumeration_type:
f1e6e072 20801 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20802 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20803
63d06c5c 20804 {
9c37b5ae 20805 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20806 really ever be static objects: otherwise, if you try
20807 to, say, break of a class's method and you're in a file
20808 which doesn't mention that class, it won't work unless
20809 the check for all static symbols in lookup_symbol_aux
20810 saves you. See the OtherFileClass tests in
20811 gdb.c++/namespace.exp. */
20812
e37fd15a 20813 if (!suppress_add)
34eaf542 20814 {
c24bdb02 20815 buildsym_compunit *builder = cu->get_builder ();
804d2729 20816 list_to_add
c24bdb02 20817 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20818 && cu->language == language_cplus
c24bdb02 20819 ? builder->get_global_symbols ()
804d2729 20820 : cu->list_in_scope);
63d06c5c 20821
64382290 20822 /* The semantics of C++ state that "struct foo {
9c37b5ae 20823 ... }" also defines a typedef for "foo". */
64382290 20824 if (cu->language == language_cplus
45280282 20825 || cu->language == language_ada
c44af4eb
TT
20826 || cu->language == language_d
20827 || cu->language == language_rust)
64382290
TT
20828 {
20829 /* The symbol's name is already allocated along
20830 with this objfile, so we don't need to
20831 duplicate it for the type. */
20832 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20833 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20834 }
63d06c5c
DC
20835 }
20836 }
c906108c
SS
20837 break;
20838 case DW_TAG_typedef:
f1e6e072 20839 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20840 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20841 list_to_add = cu->list_in_scope;
63d06c5c 20842 break;
c906108c 20843 case DW_TAG_base_type:
a02abb62 20844 case DW_TAG_subrange_type:
f1e6e072 20845 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20846 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20847 list_to_add = cu->list_in_scope;
c906108c
SS
20848 break;
20849 case DW_TAG_enumerator:
e142c38c 20850 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20851 if (attr != nullptr)
c906108c 20852 {
e7c27a73 20853 dwarf2_const_value (attr, sym, cu);
c906108c 20854 }
63d06c5c
DC
20855 {
20856 /* NOTE: carlton/2003-11-10: See comment above in the
20857 DW_TAG_class_type, etc. block. */
20858
804d2729 20859 list_to_add
c24bdb02 20860 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20861 && cu->language == language_cplus
c24bdb02 20862 ? cu->get_builder ()->get_global_symbols ()
804d2729 20863 : cu->list_in_scope);
63d06c5c 20864 }
c906108c 20865 break;
74921315 20866 case DW_TAG_imported_declaration:
5c4e30ca 20867 case DW_TAG_namespace:
f1e6e072 20868 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20869 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20870 break;
530e8392
KB
20871 case DW_TAG_module:
20872 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20873 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20874 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20875 break;
4357ac6c 20876 case DW_TAG_common_block:
f1e6e072 20877 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20878 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20879 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20880 break;
c906108c
SS
20881 default:
20882 /* Not a tag we recognize. Hopefully we aren't processing
20883 trash data, but since we must specifically ignore things
20884 we don't recognize, there is nothing else we should do at
0963b4bd 20885 this point. */
b98664d3 20886 complaint (_("unsupported tag: '%s'"),
4d3c2250 20887 dwarf_tag_name (die->tag));
c906108c
SS
20888 break;
20889 }
df8a16a1 20890
e37fd15a
SW
20891 if (suppress_add)
20892 {
20893 sym->hash_next = objfile->template_symbols;
20894 objfile->template_symbols = sym;
20895 list_to_add = NULL;
20896 }
20897
20898 if (list_to_add != NULL)
d3cb6808 20899 add_symbol_to_list (sym, list_to_add);
e37fd15a 20900
df8a16a1
DJ
20901 /* For the benefit of old versions of GCC, check for anonymous
20902 namespaces based on the demangled name. */
4d4ec4e5 20903 if (!cu->processing_has_namespace_info
94af9270 20904 && cu->language == language_cplus)
c24bdb02 20905 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20906 }
20907 return (sym);
20908}
20909
98bfdba5
PA
20910/* Given an attr with a DW_FORM_dataN value in host byte order,
20911 zero-extend it as appropriate for the symbol's type. The DWARF
20912 standard (v4) is not entirely clear about the meaning of using
20913 DW_FORM_dataN for a constant with a signed type, where the type is
20914 wider than the data. The conclusion of a discussion on the DWARF
20915 list was that this is unspecified. We choose to always zero-extend
20916 because that is the interpretation long in use by GCC. */
c906108c 20917
98bfdba5 20918static gdb_byte *
ff39bb5e 20919dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20920 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20921{
518817b3 20922 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20923 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20924 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20925 LONGEST l = DW_UNSND (attr);
20926
20927 if (bits < sizeof (*value) * 8)
20928 {
20929 l &= ((LONGEST) 1 << bits) - 1;
20930 *value = l;
20931 }
20932 else if (bits == sizeof (*value) * 8)
20933 *value = l;
20934 else
20935 {
224c3ddb 20936 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20937 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20938 return bytes;
20939 }
20940
20941 return NULL;
20942}
20943
20944/* Read a constant value from an attribute. Either set *VALUE, or if
20945 the value does not fit in *VALUE, set *BYTES - either already
20946 allocated on the objfile obstack, or newly allocated on OBSTACK,
20947 or, set *BATON, if we translated the constant to a location
20948 expression. */
20949
20950static void
ff39bb5e 20951dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20952 const char *name, struct obstack *obstack,
20953 struct dwarf2_cu *cu,
d521ce57 20954 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20955 struct dwarf2_locexpr_baton **baton)
20956{
518817b3 20957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20958 struct comp_unit_head *cu_header = &cu->header;
c906108c 20959 struct dwarf_block *blk;
98bfdba5
PA
20960 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20961 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20962
20963 *value = 0;
20964 *bytes = NULL;
20965 *baton = NULL;
c906108c
SS
20966
20967 switch (attr->form)
20968 {
20969 case DW_FORM_addr:
336d760d 20970 case DW_FORM_addrx:
3019eac3 20971 case DW_FORM_GNU_addr_index:
ac56253d 20972 {
ac56253d
TT
20973 gdb_byte *data;
20974
98bfdba5
PA
20975 if (TYPE_LENGTH (type) != cu_header->addr_size)
20976 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20977 cu_header->addr_size,
98bfdba5 20978 TYPE_LENGTH (type));
ac56253d
TT
20979 /* Symbols of this form are reasonably rare, so we just
20980 piggyback on the existing location code rather than writing
20981 a new implementation of symbol_computed_ops. */
8d749320 20982 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20983 (*baton)->per_cu = cu->per_cu;
20984 gdb_assert ((*baton)->per_cu);
ac56253d 20985
98bfdba5 20986 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20987 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20988 (*baton)->data = data;
ac56253d
TT
20989
20990 data[0] = DW_OP_addr;
20991 store_unsigned_integer (&data[1], cu_header->addr_size,
20992 byte_order, DW_ADDR (attr));
20993 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20994 }
c906108c 20995 break;
4ac36638 20996 case DW_FORM_string:
93b5768b 20997 case DW_FORM_strp:
cf532bd1 20998 case DW_FORM_strx:
3019eac3 20999 case DW_FORM_GNU_str_index:
36586728 21000 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21001 /* DW_STRING is already allocated on the objfile obstack, point
21002 directly to it. */
d521ce57 21003 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21004 break;
c906108c
SS
21005 case DW_FORM_block1:
21006 case DW_FORM_block2:
21007 case DW_FORM_block4:
21008 case DW_FORM_block:
2dc7f7b3 21009 case DW_FORM_exprloc:
0224619f 21010 case DW_FORM_data16:
c906108c 21011 blk = DW_BLOCK (attr);
98bfdba5
PA
21012 if (TYPE_LENGTH (type) != blk->size)
21013 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21014 TYPE_LENGTH (type));
21015 *bytes = blk->data;
c906108c 21016 break;
2df3850c
JM
21017
21018 /* The DW_AT_const_value attributes are supposed to carry the
21019 symbol's value "represented as it would be on the target
21020 architecture." By the time we get here, it's already been
21021 converted to host endianness, so we just need to sign- or
21022 zero-extend it as appropriate. */
21023 case DW_FORM_data1:
3aef2284 21024 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21025 break;
c906108c 21026 case DW_FORM_data2:
3aef2284 21027 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21028 break;
c906108c 21029 case DW_FORM_data4:
3aef2284 21030 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21031 break;
c906108c 21032 case DW_FORM_data8:
3aef2284 21033 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21034 break;
21035
c906108c 21036 case DW_FORM_sdata:
663c44ac 21037 case DW_FORM_implicit_const:
98bfdba5 21038 *value = DW_SND (attr);
2df3850c
JM
21039 break;
21040
c906108c 21041 case DW_FORM_udata:
98bfdba5 21042 *value = DW_UNSND (attr);
c906108c 21043 break;
2df3850c 21044
c906108c 21045 default:
b98664d3 21046 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21047 dwarf_form_name (attr->form));
98bfdba5 21048 *value = 0;
c906108c
SS
21049 break;
21050 }
21051}
21052
2df3850c 21053
98bfdba5
PA
21054/* Copy constant value from an attribute to a symbol. */
21055
2df3850c 21056static void
ff39bb5e 21057dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21058 struct dwarf2_cu *cu)
2df3850c 21059{
518817b3 21060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21061 LONGEST value;
d521ce57 21062 const gdb_byte *bytes;
98bfdba5 21063 struct dwarf2_locexpr_baton *baton;
2df3850c 21064
98bfdba5 21065 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21066 sym->print_name (),
98bfdba5
PA
21067 &objfile->objfile_obstack, cu,
21068 &value, &bytes, &baton);
2df3850c 21069
98bfdba5
PA
21070 if (baton != NULL)
21071 {
98bfdba5 21072 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21073 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21074 }
21075 else if (bytes != NULL)
21076 {
21077 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21078 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21079 }
21080 else
21081 {
21082 SYMBOL_VALUE (sym) = value;
f1e6e072 21083 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21084 }
2df3850c
JM
21085}
21086
c906108c
SS
21087/* Return the type of the die in question using its DW_AT_type attribute. */
21088
21089static struct type *
e7c27a73 21090die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21091{
c906108c 21092 struct attribute *type_attr;
c906108c 21093
e142c38c 21094 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21095 if (!type_attr)
21096 {
518817b3 21097 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21098 /* A missing DW_AT_type represents a void type. */
518817b3 21099 return objfile_type (objfile)->builtin_void;
c906108c 21100 }
348e048f 21101
673bfd45 21102 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21103}
21104
b4ba55a1
JB
21105/* True iff CU's producer generates GNAT Ada auxiliary information
21106 that allows to find parallel types through that information instead
21107 of having to do expensive parallel lookups by type name. */
21108
21109static int
21110need_gnat_info (struct dwarf2_cu *cu)
21111{
de4cb04a
JB
21112 /* Assume that the Ada compiler was GNAT, which always produces
21113 the auxiliary information. */
21114 return (cu->language == language_ada);
b4ba55a1
JB
21115}
21116
b4ba55a1
JB
21117/* Return the auxiliary type of the die in question using its
21118 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21119 attribute is not present. */
21120
21121static struct type *
21122die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21123{
b4ba55a1 21124 struct attribute *type_attr;
b4ba55a1
JB
21125
21126 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21127 if (!type_attr)
21128 return NULL;
21129
673bfd45 21130 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21131}
21132
21133/* If DIE has a descriptive_type attribute, then set the TYPE's
21134 descriptive type accordingly. */
21135
21136static void
21137set_descriptive_type (struct type *type, struct die_info *die,
21138 struct dwarf2_cu *cu)
21139{
21140 struct type *descriptive_type = die_descriptive_type (die, cu);
21141
21142 if (descriptive_type)
21143 {
21144 ALLOCATE_GNAT_AUX_TYPE (type);
21145 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21146 }
21147}
21148
c906108c
SS
21149/* Return the containing type of the die in question using its
21150 DW_AT_containing_type attribute. */
21151
21152static struct type *
e7c27a73 21153die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21154{
c906108c 21155 struct attribute *type_attr;
518817b3 21156 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21157
e142c38c 21158 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21159 if (!type_attr)
21160 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21161 "[in module %s]"), objfile_name (objfile));
33ac96f0 21162
673bfd45 21163 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21164}
21165
ac9ec31b
DE
21166/* Return an error marker type to use for the ill formed type in DIE/CU. */
21167
21168static struct type *
21169build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21170{
518817b3
SM
21171 struct dwarf2_per_objfile *dwarf2_per_objfile
21172 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21173 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21174 char *saved;
ac9ec31b 21175
528e1572
SM
21176 std::string message
21177 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21178 objfile_name (objfile),
21179 sect_offset_str (cu->header.sect_off),
21180 sect_offset_str (die->sect_off));
efba19b0 21181 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21182
19f392bc 21183 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21184}
21185
673bfd45 21186/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21187 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21188 DW_AT_containing_type.
673bfd45
DE
21189 If there is no type substitute an error marker. */
21190
c906108c 21191static struct type *
ff39bb5e 21192lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21193 struct dwarf2_cu *cu)
c906108c 21194{
518817b3
SM
21195 struct dwarf2_per_objfile *dwarf2_per_objfile
21196 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21197 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21198 struct type *this_type;
21199
ac9ec31b
DE
21200 gdb_assert (attr->name == DW_AT_type
21201 || attr->name == DW_AT_GNAT_descriptive_type
21202 || attr->name == DW_AT_containing_type);
21203
673bfd45
DE
21204 /* First see if we have it cached. */
21205
36586728
TT
21206 if (attr->form == DW_FORM_GNU_ref_alt)
21207 {
21208 struct dwarf2_per_cu_data *per_cu;
9c541725 21209 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21210
ed2dc618
SM
21211 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21212 dwarf2_per_objfile);
9c541725 21213 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21214 }
cd6c91b4 21215 else if (attr->form_is_ref ())
673bfd45 21216 {
9c541725 21217 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21218
9c541725 21219 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21220 }
55f1336d 21221 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21222 {
ac9ec31b 21223 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21224
ac9ec31b 21225 return get_signatured_type (die, signature, cu);
673bfd45
DE
21226 }
21227 else
21228 {
b98664d3 21229 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21230 " at %s [in module %s]"),
21231 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21232 objfile_name (objfile));
ac9ec31b 21233 return build_error_marker_type (cu, die);
673bfd45
DE
21234 }
21235
21236 /* If not cached we need to read it in. */
21237
21238 if (this_type == NULL)
21239 {
ac9ec31b 21240 struct die_info *type_die = NULL;
673bfd45
DE
21241 struct dwarf2_cu *type_cu = cu;
21242
cd6c91b4 21243 if (attr->form_is_ref ())
ac9ec31b
DE
21244 type_die = follow_die_ref (die, attr, &type_cu);
21245 if (type_die == NULL)
21246 return build_error_marker_type (cu, die);
21247 /* If we find the type now, it's probably because the type came
3019eac3
DE
21248 from an inter-CU reference and the type's CU got expanded before
21249 ours. */
ac9ec31b 21250 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21251 }
21252
21253 /* If we still don't have a type use an error marker. */
21254
21255 if (this_type == NULL)
ac9ec31b 21256 return build_error_marker_type (cu, die);
673bfd45 21257
f792889a 21258 return this_type;
c906108c
SS
21259}
21260
673bfd45
DE
21261/* Return the type in DIE, CU.
21262 Returns NULL for invalid types.
21263
02142a6c 21264 This first does a lookup in die_type_hash,
673bfd45
DE
21265 and only reads the die in if necessary.
21266
21267 NOTE: This can be called when reading in partial or full symbols. */
21268
f792889a 21269static struct type *
e7c27a73 21270read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21271{
f792889a
DJ
21272 struct type *this_type;
21273
21274 this_type = get_die_type (die, cu);
21275 if (this_type)
21276 return this_type;
21277
673bfd45
DE
21278 return read_type_die_1 (die, cu);
21279}
21280
21281/* Read the type in DIE, CU.
21282 Returns NULL for invalid types. */
21283
21284static struct type *
21285read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21286{
21287 struct type *this_type = NULL;
21288
c906108c
SS
21289 switch (die->tag)
21290 {
21291 case DW_TAG_class_type:
680b30c7 21292 case DW_TAG_interface_type:
c906108c
SS
21293 case DW_TAG_structure_type:
21294 case DW_TAG_union_type:
f792889a 21295 this_type = read_structure_type (die, cu);
c906108c
SS
21296 break;
21297 case DW_TAG_enumeration_type:
f792889a 21298 this_type = read_enumeration_type (die, cu);
c906108c
SS
21299 break;
21300 case DW_TAG_subprogram:
21301 case DW_TAG_subroutine_type:
edb3359d 21302 case DW_TAG_inlined_subroutine:
f792889a 21303 this_type = read_subroutine_type (die, cu);
c906108c
SS
21304 break;
21305 case DW_TAG_array_type:
f792889a 21306 this_type = read_array_type (die, cu);
c906108c 21307 break;
72019c9c 21308 case DW_TAG_set_type:
f792889a 21309 this_type = read_set_type (die, cu);
72019c9c 21310 break;
c906108c 21311 case DW_TAG_pointer_type:
f792889a 21312 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21313 break;
21314 case DW_TAG_ptr_to_member_type:
f792889a 21315 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21316 break;
21317 case DW_TAG_reference_type:
4297a3f0
AV
21318 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21319 break;
21320 case DW_TAG_rvalue_reference_type:
21321 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21322 break;
21323 case DW_TAG_const_type:
f792889a 21324 this_type = read_tag_const_type (die, cu);
c906108c
SS
21325 break;
21326 case DW_TAG_volatile_type:
f792889a 21327 this_type = read_tag_volatile_type (die, cu);
c906108c 21328 break;
06d66ee9
TT
21329 case DW_TAG_restrict_type:
21330 this_type = read_tag_restrict_type (die, cu);
21331 break;
c906108c 21332 case DW_TAG_string_type:
f792889a 21333 this_type = read_tag_string_type (die, cu);
c906108c
SS
21334 break;
21335 case DW_TAG_typedef:
f792889a 21336 this_type = read_typedef (die, cu);
c906108c 21337 break;
a02abb62 21338 case DW_TAG_subrange_type:
f792889a 21339 this_type = read_subrange_type (die, cu);
a02abb62 21340 break;
c906108c 21341 case DW_TAG_base_type:
f792889a 21342 this_type = read_base_type (die, cu);
c906108c 21343 break;
81a17f79 21344 case DW_TAG_unspecified_type:
f792889a 21345 this_type = read_unspecified_type (die, cu);
81a17f79 21346 break;
0114d602
DJ
21347 case DW_TAG_namespace:
21348 this_type = read_namespace_type (die, cu);
21349 break;
f55ee35c
JK
21350 case DW_TAG_module:
21351 this_type = read_module_type (die, cu);
21352 break;
a2c2acaf
MW
21353 case DW_TAG_atomic_type:
21354 this_type = read_tag_atomic_type (die, cu);
21355 break;
c906108c 21356 default:
b98664d3 21357 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21358 dwarf_tag_name (die->tag));
c906108c
SS
21359 break;
21360 }
63d06c5c 21361
f792889a 21362 return this_type;
63d06c5c
DC
21363}
21364
abc72ce4
DE
21365/* See if we can figure out if the class lives in a namespace. We do
21366 this by looking for a member function; its demangled name will
21367 contain namespace info, if there is any.
21368 Return the computed name or NULL.
21369 Space for the result is allocated on the objfile's obstack.
21370 This is the full-die version of guess_partial_die_structure_name.
21371 In this case we know DIE has no useful parent. */
21372
43816ebc 21373static const char *
abc72ce4
DE
21374guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21375{
21376 struct die_info *spec_die;
21377 struct dwarf2_cu *spec_cu;
21378 struct die_info *child;
518817b3 21379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21380
21381 spec_cu = cu;
21382 spec_die = die_specification (die, &spec_cu);
21383 if (spec_die != NULL)
21384 {
21385 die = spec_die;
21386 cu = spec_cu;
21387 }
21388
21389 for (child = die->child;
21390 child != NULL;
21391 child = child->sibling)
21392 {
21393 if (child->tag == DW_TAG_subprogram)
21394 {
73b9be8b 21395 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21396
7d45c7c3 21397 if (linkage_name != NULL)
abc72ce4 21398 {
43816ebc
TT
21399 gdb::unique_xmalloc_ptr<char> actual_name
21400 (language_class_name_from_physname (cu->language_defn,
21401 linkage_name));
21402 const char *name = NULL;
abc72ce4
DE
21403
21404 if (actual_name != NULL)
21405 {
15d034d0 21406 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21407
21408 if (die_name != NULL
43816ebc 21409 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21410 {
21411 /* Strip off the class name from the full name.
21412 We want the prefix. */
21413 int die_name_len = strlen (die_name);
43816ebc
TT
21414 int actual_name_len = strlen (actual_name.get ());
21415 const char *ptr = actual_name.get ();
abc72ce4
DE
21416
21417 /* Test for '::' as a sanity check. */
21418 if (actual_name_len > die_name_len + 2
43816ebc 21419 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21420 name = obstack_strndup (
e3b94546 21421 &objfile->per_bfd->storage_obstack,
43816ebc 21422 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21423 }
21424 }
abc72ce4
DE
21425 return name;
21426 }
21427 }
21428 }
21429
21430 return NULL;
21431}
21432
96408a79
SA
21433/* GCC might emit a nameless typedef that has a linkage name. Determine the
21434 prefix part in such case. See
21435 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21436
a121b7c1 21437static const char *
96408a79
SA
21438anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21439{
21440 struct attribute *attr;
e6a959d6 21441 const char *base;
96408a79
SA
21442
21443 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21444 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21445 return NULL;
21446
7d45c7c3 21447 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21448 return NULL;
21449
73b9be8b 21450 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21451 if (attr == NULL || DW_STRING (attr) == NULL)
21452 return NULL;
21453
21454 /* dwarf2_name had to be already called. */
21455 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21456
21457 /* Strip the base name, keep any leading namespaces/classes. */
21458 base = strrchr (DW_STRING (attr), ':');
21459 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21460 return "";
21461
518817b3 21462 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21463 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21464 DW_STRING (attr),
21465 &base[-1] - DW_STRING (attr));
96408a79
SA
21466}
21467
fdde2d81 21468/* Return the name of the namespace/class that DIE is defined within,
0114d602 21469 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21470
0114d602
DJ
21471 For example, if we're within the method foo() in the following
21472 code:
21473
21474 namespace N {
21475 class C {
21476 void foo () {
21477 }
21478 };
21479 }
21480
21481 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21482
0d5cff50 21483static const char *
e142c38c 21484determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21485{
518817b3
SM
21486 struct dwarf2_per_objfile *dwarf2_per_objfile
21487 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21488 struct die_info *parent, *spec_die;
21489 struct dwarf2_cu *spec_cu;
21490 struct type *parent_type;
a121b7c1 21491 const char *retval;
63d06c5c 21492
9c37b5ae 21493 if (cu->language != language_cplus
c44af4eb
TT
21494 && cu->language != language_fortran && cu->language != language_d
21495 && cu->language != language_rust)
0114d602
DJ
21496 return "";
21497
96408a79
SA
21498 retval = anonymous_struct_prefix (die, cu);
21499 if (retval)
21500 return retval;
21501
0114d602
DJ
21502 /* We have to be careful in the presence of DW_AT_specification.
21503 For example, with GCC 3.4, given the code
21504
21505 namespace N {
21506 void foo() {
21507 // Definition of N::foo.
21508 }
21509 }
21510
21511 then we'll have a tree of DIEs like this:
21512
21513 1: DW_TAG_compile_unit
21514 2: DW_TAG_namespace // N
21515 3: DW_TAG_subprogram // declaration of N::foo
21516 4: DW_TAG_subprogram // definition of N::foo
21517 DW_AT_specification // refers to die #3
21518
21519 Thus, when processing die #4, we have to pretend that we're in
21520 the context of its DW_AT_specification, namely the contex of die
21521 #3. */
21522 spec_cu = cu;
21523 spec_die = die_specification (die, &spec_cu);
21524 if (spec_die == NULL)
21525 parent = die->parent;
21526 else
63d06c5c 21527 {
0114d602
DJ
21528 parent = spec_die->parent;
21529 cu = spec_cu;
63d06c5c 21530 }
0114d602
DJ
21531
21532 if (parent == NULL)
21533 return "";
98bfdba5
PA
21534 else if (parent->building_fullname)
21535 {
21536 const char *name;
21537 const char *parent_name;
21538
21539 /* It has been seen on RealView 2.2 built binaries,
21540 DW_TAG_template_type_param types actually _defined_ as
21541 children of the parent class:
21542
21543 enum E {};
21544 template class <class Enum> Class{};
21545 Class<enum E> class_e;
21546
21547 1: DW_TAG_class_type (Class)
21548 2: DW_TAG_enumeration_type (E)
21549 3: DW_TAG_enumerator (enum1:0)
21550 3: DW_TAG_enumerator (enum2:1)
21551 ...
21552 2: DW_TAG_template_type_param
21553 DW_AT_type DW_FORM_ref_udata (E)
21554
21555 Besides being broken debug info, it can put GDB into an
21556 infinite loop. Consider:
21557
21558 When we're building the full name for Class<E>, we'll start
21559 at Class, and go look over its template type parameters,
21560 finding E. We'll then try to build the full name of E, and
21561 reach here. We're now trying to build the full name of E,
21562 and look over the parent DIE for containing scope. In the
21563 broken case, if we followed the parent DIE of E, we'd again
21564 find Class, and once again go look at its template type
21565 arguments, etc., etc. Simply don't consider such parent die
21566 as source-level parent of this die (it can't be, the language
21567 doesn't allow it), and break the loop here. */
21568 name = dwarf2_name (die, cu);
21569 parent_name = dwarf2_name (parent, cu);
b98664d3 21570 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21571 name ? name : "<unknown>",
21572 parent_name ? parent_name : "<unknown>");
21573 return "";
21574 }
63d06c5c 21575 else
0114d602
DJ
21576 switch (parent->tag)
21577 {
63d06c5c 21578 case DW_TAG_namespace:
0114d602 21579 parent_type = read_type_die (parent, cu);
acebe513
UW
21580 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21581 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21582 Work around this problem here. */
21583 if (cu->language == language_cplus
e86ca25f 21584 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21585 return "";
0114d602 21586 /* We give a name to even anonymous namespaces. */
e86ca25f 21587 return TYPE_NAME (parent_type);
63d06c5c 21588 case DW_TAG_class_type:
680b30c7 21589 case DW_TAG_interface_type:
63d06c5c 21590 case DW_TAG_structure_type:
0114d602 21591 case DW_TAG_union_type:
f55ee35c 21592 case DW_TAG_module:
0114d602 21593 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21594 if (TYPE_NAME (parent_type) != NULL)
21595 return TYPE_NAME (parent_type);
0114d602
DJ
21596 else
21597 /* An anonymous structure is only allowed non-static data
21598 members; no typedefs, no member functions, et cetera.
21599 So it does not need a prefix. */
21600 return "";
abc72ce4 21601 case DW_TAG_compile_unit:
95554aad 21602 case DW_TAG_partial_unit:
abc72ce4
DE
21603 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21604 if (cu->language == language_cplus
fd5866f6 21605 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21606 && die->child != NULL
21607 && (die->tag == DW_TAG_class_type
21608 || die->tag == DW_TAG_structure_type
21609 || die->tag == DW_TAG_union_type))
21610 {
43816ebc 21611 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21612 if (name != NULL)
21613 return name;
21614 }
21615 return "";
0a4b0913
AB
21616 case DW_TAG_subprogram:
21617 /* Nested subroutines in Fortran get a prefix with the name
21618 of the parent's subroutine. */
21619 if (cu->language == language_fortran)
21620 {
21621 if ((die->tag == DW_TAG_subprogram)
21622 && (dwarf2_name (parent, cu) != NULL))
21623 return dwarf2_name (parent, cu);
21624 }
21625 return determine_prefix (parent, cu);
3d567982
TT
21626 case DW_TAG_enumeration_type:
21627 parent_type = read_type_die (parent, cu);
21628 if (TYPE_DECLARED_CLASS (parent_type))
21629 {
e86ca25f
TT
21630 if (TYPE_NAME (parent_type) != NULL)
21631 return TYPE_NAME (parent_type);
3d567982
TT
21632 return "";
21633 }
21634 /* Fall through. */
63d06c5c 21635 default:
8176b9b8 21636 return determine_prefix (parent, cu);
63d06c5c 21637 }
63d06c5c
DC
21638}
21639
3e43a32a
MS
21640/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21641 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21642 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21643 an obconcat, otherwise allocate storage for the result. The CU argument is
21644 used to determine the language and hence, the appropriate separator. */
987504bb 21645
f55ee35c 21646#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21647
21648static char *
f55ee35c
JK
21649typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21650 int physname, struct dwarf2_cu *cu)
63d06c5c 21651{
f55ee35c 21652 const char *lead = "";
5c315b68 21653 const char *sep;
63d06c5c 21654
3e43a32a
MS
21655 if (suffix == NULL || suffix[0] == '\0'
21656 || prefix == NULL || prefix[0] == '\0')
987504bb 21657 sep = "";
45280282
IB
21658 else if (cu->language == language_d)
21659 {
21660 /* For D, the 'main' function could be defined in any module, but it
21661 should never be prefixed. */
21662 if (strcmp (suffix, "D main") == 0)
21663 {
21664 prefix = "";
21665 sep = "";
21666 }
21667 else
21668 sep = ".";
21669 }
f55ee35c
JK
21670 else if (cu->language == language_fortran && physname)
21671 {
21672 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21673 DW_AT_MIPS_linkage_name is preferred and used instead. */
21674
21675 lead = "__";
21676 sep = "_MOD_";
21677 }
987504bb
JJ
21678 else
21679 sep = "::";
63d06c5c 21680
6dd47d34
DE
21681 if (prefix == NULL)
21682 prefix = "";
21683 if (suffix == NULL)
21684 suffix = "";
21685
987504bb
JJ
21686 if (obs == NULL)
21687 {
3e43a32a 21688 char *retval
224c3ddb
SM
21689 = ((char *)
21690 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21691
f55ee35c
JK
21692 strcpy (retval, lead);
21693 strcat (retval, prefix);
6dd47d34
DE
21694 strcat (retval, sep);
21695 strcat (retval, suffix);
63d06c5c
DC
21696 return retval;
21697 }
987504bb
JJ
21698 else
21699 {
21700 /* We have an obstack. */
f55ee35c 21701 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21702 }
63d06c5c
DC
21703}
21704
c906108c
SS
21705/* Return sibling of die, NULL if no sibling. */
21706
f9aca02d 21707static struct die_info *
fba45db2 21708sibling_die (struct die_info *die)
c906108c 21709{
639d11d3 21710 return die->sibling;
c906108c
SS
21711}
21712
71c25dea
TT
21713/* Get name of a die, return NULL if not found. */
21714
15d034d0
TT
21715static const char *
21716dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
21717 struct obstack *obstack)
21718{
21719 if (name && cu->language == language_cplus)
21720 {
2f408ecb 21721 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21722
2f408ecb 21723 if (!canon_name.empty ())
71c25dea 21724 {
2f408ecb 21725 if (canon_name != name)
efba19b0 21726 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
21727 }
21728 }
21729
21730 return name;
c906108c
SS
21731}
21732
96553a0c
DE
21733/* Get name of a die, return NULL if not found.
21734 Anonymous namespaces are converted to their magic string. */
9219021c 21735
15d034d0 21736static const char *
e142c38c 21737dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21738{
21739 struct attribute *attr;
518817b3 21740 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21741
e142c38c 21742 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21743 if ((!attr || !DW_STRING (attr))
96553a0c 21744 && die->tag != DW_TAG_namespace
53832f31
TT
21745 && die->tag != DW_TAG_class_type
21746 && die->tag != DW_TAG_interface_type
21747 && die->tag != DW_TAG_structure_type
21748 && die->tag != DW_TAG_union_type)
71c25dea
TT
21749 return NULL;
21750
21751 switch (die->tag)
21752 {
21753 case DW_TAG_compile_unit:
95554aad 21754 case DW_TAG_partial_unit:
71c25dea
TT
21755 /* Compilation units have a DW_AT_name that is a filename, not
21756 a source language identifier. */
21757 case DW_TAG_enumeration_type:
21758 case DW_TAG_enumerator:
21759 /* These tags always have simple identifiers already; no need
21760 to canonicalize them. */
21761 return DW_STRING (attr);
907af001 21762
96553a0c
DE
21763 case DW_TAG_namespace:
21764 if (attr != NULL && DW_STRING (attr) != NULL)
21765 return DW_STRING (attr);
21766 return CP_ANONYMOUS_NAMESPACE_STR;
21767
907af001
UW
21768 case DW_TAG_class_type:
21769 case DW_TAG_interface_type:
21770 case DW_TAG_structure_type:
21771 case DW_TAG_union_type:
21772 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21773 structures or unions. These were of the form "._%d" in GCC 4.1,
21774 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21775 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21776 if (attr && DW_STRING (attr)
61012eef
GB
21777 && (startswith (DW_STRING (attr), "._")
21778 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21779 return NULL;
53832f31
TT
21780
21781 /* GCC might emit a nameless typedef that has a linkage name. See
21782 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21783 if (!attr || DW_STRING (attr) == NULL)
21784 {
73b9be8b 21785 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21786 if (attr == NULL || DW_STRING (attr) == NULL)
21787 return NULL;
21788
df5c6c50
JK
21789 /* Avoid demangling DW_STRING (attr) the second time on a second
21790 call for the same DIE. */
21791 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21792 {
43816ebc
TT
21793 gdb::unique_xmalloc_ptr<char> demangled
21794 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21795 if (demangled == nullptr)
21796 return nullptr;
43816ebc 21797
e6a959d6 21798 const char *base;
96408a79 21799
53832f31 21800 /* FIXME: we already did this for the partial symbol... */
34a68019 21801 DW_STRING (attr)
021887d8 21802 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 21803 demangled.get ());
53832f31 21804 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
21805
21806 /* Strip any leading namespaces/classes, keep only the base name.
21807 DW_AT_name for named DIEs does not contain the prefixes. */
21808 base = strrchr (DW_STRING (attr), ':');
21809 if (base && base > DW_STRING (attr) && base[-1] == ':')
21810 return &base[1];
21811 else
21812 return DW_STRING (attr);
53832f31
TT
21813 }
21814 }
907af001
UW
21815 break;
21816
71c25dea 21817 default:
907af001
UW
21818 break;
21819 }
21820
21821 if (!DW_STRING_IS_CANONICAL (attr))
21822 {
21823 DW_STRING (attr)
21824 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 21825 &objfile->per_bfd->storage_obstack);
907af001 21826 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21827 }
907af001 21828 return DW_STRING (attr);
9219021c
DC
21829}
21830
21831/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21832 is none. *EXT_CU is the CU containing DIE on input, and the CU
21833 containing the return value on output. */
9219021c
DC
21834
21835static struct die_info *
f2f0e013 21836dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21837{
21838 struct attribute *attr;
9219021c 21839
f2f0e013 21840 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21841 if (attr == NULL)
21842 return NULL;
21843
f2f0e013 21844 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21845}
21846
fa9c3fa0
TT
21847/* A convenience function that returns an "unknown" DWARF name,
21848 including the value of V. STR is the name of the entity being
21849 printed, e.g., "TAG". */
21850
21851static const char *
21852dwarf_unknown (const char *str, unsigned v)
21853{
21854 char *cell = get_print_cell ();
21855 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21856 return cell;
21857}
21858
c906108c
SS
21859/* Convert a DIE tag into its string name. */
21860
f39c6ffd 21861static const char *
aa1ee363 21862dwarf_tag_name (unsigned tag)
c906108c 21863{
f39c6ffd
TT
21864 const char *name = get_DW_TAG_name (tag);
21865
21866 if (name == NULL)
fa9c3fa0 21867 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
21868
21869 return name;
c906108c
SS
21870}
21871
21872/* Convert a DWARF attribute code into its string name. */
21873
f39c6ffd 21874static const char *
aa1ee363 21875dwarf_attr_name (unsigned attr)
c906108c 21876{
f39c6ffd
TT
21877 const char *name;
21878
c764a876 21879#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21880 if (attr == DW_AT_MIPS_fde)
21881 return "DW_AT_MIPS_fde";
21882#else
21883 if (attr == DW_AT_HP_block_index)
21884 return "DW_AT_HP_block_index";
c764a876 21885#endif
f39c6ffd
TT
21886
21887 name = get_DW_AT_name (attr);
21888
21889 if (name == NULL)
fa9c3fa0 21890 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
21891
21892 return name;
c906108c
SS
21893}
21894
21895/* Convert a DWARF value form code into its string name. */
21896
f39c6ffd 21897static const char *
aa1ee363 21898dwarf_form_name (unsigned form)
c906108c 21899{
f39c6ffd
TT
21900 const char *name = get_DW_FORM_name (form);
21901
21902 if (name == NULL)
fa9c3fa0 21903 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
21904
21905 return name;
c906108c
SS
21906}
21907
a121b7c1 21908static const char *
fba45db2 21909dwarf_bool_name (unsigned mybool)
c906108c
SS
21910{
21911 if (mybool)
21912 return "TRUE";
21913 else
21914 return "FALSE";
21915}
21916
21917/* Convert a DWARF type code into its string name. */
21918
f39c6ffd 21919static const char *
aa1ee363 21920dwarf_type_encoding_name (unsigned enc)
c906108c 21921{
f39c6ffd 21922 const char *name = get_DW_ATE_name (enc);
c906108c 21923
f39c6ffd 21924 if (name == NULL)
fa9c3fa0 21925 return dwarf_unknown ("ATE", enc);
c906108c 21926
f39c6ffd 21927 return name;
c906108c 21928}
c906108c 21929
f9aca02d 21930static void
d97bc12b 21931dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21932{
21933 unsigned int i;
21934
d97bc12b 21935 print_spaces (indent, f);
9d8780f0 21936 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21937 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21938 sect_offset_str (die->sect_off));
d97bc12b
DE
21939
21940 if (die->parent != NULL)
21941 {
21942 print_spaces (indent, f);
9d8780f0
SM
21943 fprintf_unfiltered (f, " parent at offset: %s\n",
21944 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21945 }
21946
21947 print_spaces (indent, f);
21948 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21949 dwarf_bool_name (die->child != NULL));
c906108c 21950
d97bc12b
DE
21951 print_spaces (indent, f);
21952 fprintf_unfiltered (f, " attributes:\n");
21953
c906108c
SS
21954 for (i = 0; i < die->num_attrs; ++i)
21955 {
d97bc12b
DE
21956 print_spaces (indent, f);
21957 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21958 dwarf_attr_name (die->attrs[i].name),
21959 dwarf_form_name (die->attrs[i].form));
d97bc12b 21960
c906108c
SS
21961 switch (die->attrs[i].form)
21962 {
c906108c 21963 case DW_FORM_addr:
336d760d 21964 case DW_FORM_addrx:
3019eac3 21965 case DW_FORM_GNU_addr_index:
d97bc12b 21966 fprintf_unfiltered (f, "address: ");
5af949e3 21967 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21968 break;
21969 case DW_FORM_block2:
21970 case DW_FORM_block4:
21971 case DW_FORM_block:
21972 case DW_FORM_block1:
56eb65bd
SP
21973 fprintf_unfiltered (f, "block: size %s",
21974 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21975 break;
2dc7f7b3 21976 case DW_FORM_exprloc:
56eb65bd
SP
21977 fprintf_unfiltered (f, "expression: size %s",
21978 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21979 break;
0224619f
JK
21980 case DW_FORM_data16:
21981 fprintf_unfiltered (f, "constant of 16 bytes");
21982 break;
4568ecf9
DE
21983 case DW_FORM_ref_addr:
21984 fprintf_unfiltered (f, "ref address: ");
21985 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21986 break;
36586728
TT
21987 case DW_FORM_GNU_ref_alt:
21988 fprintf_unfiltered (f, "alt ref address: ");
21989 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21990 break;
10b3939b
DJ
21991 case DW_FORM_ref1:
21992 case DW_FORM_ref2:
21993 case DW_FORM_ref4:
4568ecf9
DE
21994 case DW_FORM_ref8:
21995 case DW_FORM_ref_udata:
d97bc12b 21996 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21997 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21998 break;
c906108c
SS
21999 case DW_FORM_data1:
22000 case DW_FORM_data2:
22001 case DW_FORM_data4:
ce5d95e1 22002 case DW_FORM_data8:
c906108c
SS
22003 case DW_FORM_udata:
22004 case DW_FORM_sdata:
43bbcdc2
PH
22005 fprintf_unfiltered (f, "constant: %s",
22006 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22007 break;
2dc7f7b3
TT
22008 case DW_FORM_sec_offset:
22009 fprintf_unfiltered (f, "section offset: %s",
22010 pulongest (DW_UNSND (&die->attrs[i])));
22011 break;
55f1336d 22012 case DW_FORM_ref_sig8:
ac9ec31b
DE
22013 fprintf_unfiltered (f, "signature: %s",
22014 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22015 break;
c906108c 22016 case DW_FORM_string:
4bdf3d34 22017 case DW_FORM_strp:
43988095 22018 case DW_FORM_line_strp:
cf532bd1 22019 case DW_FORM_strx:
3019eac3 22020 case DW_FORM_GNU_str_index:
36586728 22021 case DW_FORM_GNU_strp_alt:
8285870a 22022 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22023 DW_STRING (&die->attrs[i])
8285870a
JK
22024 ? DW_STRING (&die->attrs[i]) : "",
22025 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22026 break;
22027 case DW_FORM_flag:
22028 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22029 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22030 else
d97bc12b 22031 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22032 break;
2dc7f7b3
TT
22033 case DW_FORM_flag_present:
22034 fprintf_unfiltered (f, "flag: TRUE");
22035 break;
a8329558 22036 case DW_FORM_indirect:
0963b4bd
MS
22037 /* The reader will have reduced the indirect form to
22038 the "base form" so this form should not occur. */
5f48f8f3 22039 fprintf_unfiltered (f,
3e43a32a 22040 "unexpected attribute form: DW_FORM_indirect");
a8329558 22041 break;
663c44ac
JK
22042 case DW_FORM_implicit_const:
22043 fprintf_unfiltered (f, "constant: %s",
22044 plongest (DW_SND (&die->attrs[i])));
22045 break;
c906108c 22046 default:
d97bc12b 22047 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22048 die->attrs[i].form);
d97bc12b 22049 break;
c906108c 22050 }
d97bc12b 22051 fprintf_unfiltered (f, "\n");
c906108c
SS
22052 }
22053}
22054
f9aca02d 22055static void
d97bc12b 22056dump_die_for_error (struct die_info *die)
c906108c 22057{
d97bc12b
DE
22058 dump_die_shallow (gdb_stderr, 0, die);
22059}
22060
22061static void
22062dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22063{
22064 int indent = level * 4;
22065
22066 gdb_assert (die != NULL);
22067
22068 if (level >= max_level)
22069 return;
22070
22071 dump_die_shallow (f, indent, die);
22072
22073 if (die->child != NULL)
c906108c 22074 {
d97bc12b
DE
22075 print_spaces (indent, f);
22076 fprintf_unfiltered (f, " Children:");
22077 if (level + 1 < max_level)
22078 {
22079 fprintf_unfiltered (f, "\n");
22080 dump_die_1 (f, level + 1, max_level, die->child);
22081 }
22082 else
22083 {
3e43a32a
MS
22084 fprintf_unfiltered (f,
22085 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22086 }
22087 }
22088
22089 if (die->sibling != NULL && level > 0)
22090 {
22091 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22092 }
22093}
22094
d97bc12b
DE
22095/* This is called from the pdie macro in gdbinit.in.
22096 It's not static so gcc will keep a copy callable from gdb. */
22097
22098void
22099dump_die (struct die_info *die, int max_level)
22100{
22101 dump_die_1 (gdb_stdlog, 0, max_level, die);
22102}
22103
f9aca02d 22104static void
51545339 22105store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22106{
51545339 22107 void **slot;
c906108c 22108
9c541725
PA
22109 slot = htab_find_slot_with_hash (cu->die_hash, die,
22110 to_underlying (die->sect_off),
b64f50a1 22111 INSERT);
51545339
DJ
22112
22113 *slot = die;
c906108c
SS
22114}
22115
b64f50a1
JK
22116/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22117 required kind. */
22118
22119static sect_offset
ff39bb5e 22120dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22121{
cd6c91b4 22122 if (attr->form_is_ref ())
9c541725 22123 return (sect_offset) DW_UNSND (attr);
93311388 22124
b98664d3 22125 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22126 dwarf_form_name (attr->form));
9c541725 22127 return {};
c906108c
SS
22128}
22129
43bbcdc2
PH
22130/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22131 * the value held by the attribute is not constant. */
a02abb62 22132
43bbcdc2 22133static LONGEST
ff39bb5e 22134dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22135{
663c44ac 22136 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22137 return DW_SND (attr);
22138 else if (attr->form == DW_FORM_udata
22139 || attr->form == DW_FORM_data1
22140 || attr->form == DW_FORM_data2
22141 || attr->form == DW_FORM_data4
22142 || attr->form == DW_FORM_data8)
22143 return DW_UNSND (attr);
22144 else
22145 {
cd6c91b4 22146 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22147 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22148 dwarf_form_name (attr->form));
22149 return default_value;
22150 }
22151}
22152
348e048f
DE
22153/* Follow reference or signature attribute ATTR of SRC_DIE.
22154 On entry *REF_CU is the CU of SRC_DIE.
22155 On exit *REF_CU is the CU of the result. */
22156
22157static struct die_info *
ff39bb5e 22158follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22159 struct dwarf2_cu **ref_cu)
22160{
22161 struct die_info *die;
22162
cd6c91b4 22163 if (attr->form_is_ref ())
348e048f 22164 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22165 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22166 die = follow_die_sig (src_die, attr, ref_cu);
22167 else
22168 {
22169 dump_die_for_error (src_die);
22170 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22171 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22172 }
22173
22174 return die;
03dd20cc
DJ
22175}
22176
5c631832 22177/* Follow reference OFFSET.
673bfd45
DE
22178 On entry *REF_CU is the CU of the source die referencing OFFSET.
22179 On exit *REF_CU is the CU of the result.
22180 Returns NULL if OFFSET is invalid. */
f504f079 22181
f9aca02d 22182static struct die_info *
9c541725 22183follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22184 struct dwarf2_cu **ref_cu)
c906108c 22185{
10b3939b 22186 struct die_info temp_die;
f2f0e013 22187 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22188 struct dwarf2_per_objfile *dwarf2_per_objfile
22189 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22190
348e048f
DE
22191 gdb_assert (cu->per_cu != NULL);
22192
98bfdba5
PA
22193 target_cu = cu;
22194
3019eac3 22195 if (cu->per_cu->is_debug_types)
348e048f
DE
22196 {
22197 /* .debug_types CUs cannot reference anything outside their CU.
22198 If they need to, they have to reference a signatured type via
55f1336d 22199 DW_FORM_ref_sig8. */
4057dfde 22200 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22201 return NULL;
348e048f 22202 }
36586728 22203 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22204 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22205 {
22206 struct dwarf2_per_cu_data *per_cu;
9a619af0 22207
9c541725 22208 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22209 dwarf2_per_objfile);
03dd20cc
DJ
22210
22211 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22212 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22213 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22214
10b3939b
DJ
22215 target_cu = per_cu->cu;
22216 }
98bfdba5
PA
22217 else if (cu->dies == NULL)
22218 {
22219 /* We're loading full DIEs during partial symbol reading. */
22220 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22221 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22222 }
c906108c 22223
f2f0e013 22224 *ref_cu = target_cu;
9c541725 22225 temp_die.sect_off = sect_off;
c24bdb02
KS
22226
22227 if (target_cu != cu)
22228 target_cu->ancestor = cu;
22229
9a3c8263 22230 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22231 &temp_die,
22232 to_underlying (sect_off));
5c631832 22233}
10b3939b 22234
5c631832
JK
22235/* Follow reference attribute ATTR of SRC_DIE.
22236 On entry *REF_CU is the CU of SRC_DIE.
22237 On exit *REF_CU is the CU of the result. */
22238
22239static struct die_info *
ff39bb5e 22240follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22241 struct dwarf2_cu **ref_cu)
22242{
9c541725 22243 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22244 struct dwarf2_cu *cu = *ref_cu;
22245 struct die_info *die;
22246
9c541725 22247 die = follow_die_offset (sect_off,
36586728
TT
22248 (attr->form == DW_FORM_GNU_ref_alt
22249 || cu->per_cu->is_dwz),
22250 ref_cu);
5c631832 22251 if (!die)
9d8780f0
SM
22252 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22253 "at %s [in module %s]"),
22254 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22255 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22256
5c631832
JK
22257 return die;
22258}
22259
d4c9a4f8 22260/* See read.h. */
5c631832
JK
22261
22262struct dwarf2_locexpr_baton
9c541725 22263dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22264 dwarf2_per_cu_data *per_cu,
8b9737bf 22265 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22266 void *baton, bool resolve_abstract_p)
5c631832 22267{
918dd910 22268 struct dwarf2_cu *cu;
5c631832
JK
22269 struct die_info *die;
22270 struct attribute *attr;
22271 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22272 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22273 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22274
918dd910 22275 if (per_cu->cu == NULL)
58f0c718 22276 load_cu (per_cu, false);
918dd910 22277 cu = per_cu->cu;
cc12ce38
DE
22278 if (cu == NULL)
22279 {
22280 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22281 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22282 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22283 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22284 }
918dd910 22285
9c541725 22286 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22287 if (!die)
9d8780f0
SM
22288 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22289 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22290
22291 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22292 if (!attr && resolve_abstract_p
3360b6e7 22293 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22294 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22295 {
22296 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22297 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22298 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22299
3360b6e7
TV
22300 for (const auto &cand_off
22301 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22302 {
3360b6e7
TV
22303 struct dwarf2_cu *cand_cu = cu;
22304 struct die_info *cand
22305 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22306 if (!cand
22307 || !cand->parent
e4a62c65
TV
22308 || cand->parent->tag != DW_TAG_subprogram)
22309 continue;
22310
22311 CORE_ADDR pc_low, pc_high;
22312 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22313 if (pc_low == ((CORE_ADDR) -1))
22314 continue;
22315 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22316 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22317 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22318 continue;
22319
22320 die = cand;
22321 attr = dwarf2_attr (die, DW_AT_location, cu);
22322 break;
22323 }
22324 }
22325
5c631832
JK
22326 if (!attr)
22327 {
e103e986
JK
22328 /* DWARF: "If there is no such attribute, then there is no effect.".
22329 DATA is ignored if SIZE is 0. */
5c631832 22330
e103e986 22331 retval.data = NULL;
5c631832
JK
22332 retval.size = 0;
22333 }
cd6c91b4 22334 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22335 {
22336 struct dwarf2_loclist_baton loclist_baton;
22337 CORE_ADDR pc = (*get_frame_pc) (baton);
22338 size_t size;
22339
22340 fill_in_loclist_baton (cu, &loclist_baton, attr);
22341
22342 retval.data = dwarf2_find_location_expression (&loclist_baton,
22343 &size, pc);
22344 retval.size = size;
22345 }
5c631832
JK
22346 else
22347 {
4fc6c0d5 22348 if (!attr->form_is_block ())
9d8780f0 22349 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22350 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22351 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22352
22353 retval.data = DW_BLOCK (attr)->data;
22354 retval.size = DW_BLOCK (attr)->size;
22355 }
22356 retval.per_cu = cu->per_cu;
918dd910 22357
ed2dc618 22358 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22359
5c631832 22360 return retval;
348e048f
DE
22361}
22362
d4c9a4f8 22363/* See read.h. */
8b9737bf
TT
22364
22365struct dwarf2_locexpr_baton
22366dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22367 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22368 CORE_ADDR (*get_frame_pc) (void *baton),
22369 void *baton)
22370{
9c541725 22371 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22372
9c541725 22373 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22374}
22375
b6807d98
TT
22376/* Write a constant of a given type as target-ordered bytes into
22377 OBSTACK. */
22378
22379static const gdb_byte *
22380write_constant_as_bytes (struct obstack *obstack,
22381 enum bfd_endian byte_order,
22382 struct type *type,
22383 ULONGEST value,
22384 LONGEST *len)
22385{
22386 gdb_byte *result;
22387
22388 *len = TYPE_LENGTH (type);
224c3ddb 22389 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22390 store_unsigned_integer (result, *len, byte_order, value);
22391
22392 return result;
22393}
22394
d4c9a4f8 22395/* See read.h. */
b6807d98
TT
22396
22397const gdb_byte *
9c541725 22398dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22399 dwarf2_per_cu_data *per_cu,
22400 obstack *obstack,
b6807d98
TT
22401 LONGEST *len)
22402{
22403 struct dwarf2_cu *cu;
22404 struct die_info *die;
22405 struct attribute *attr;
22406 const gdb_byte *result = NULL;
22407 struct type *type;
22408 LONGEST value;
22409 enum bfd_endian byte_order;
e3b94546 22410 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22411
b6807d98 22412 if (per_cu->cu == NULL)
58f0c718 22413 load_cu (per_cu, false);
b6807d98 22414 cu = per_cu->cu;
cc12ce38
DE
22415 if (cu == NULL)
22416 {
22417 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22418 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22419 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22420 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22421 }
b6807d98 22422
9c541725 22423 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22424 if (!die)
9d8780f0
SM
22425 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22426 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22427
22428 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22429 if (attr == NULL)
22430 return NULL;
22431
e3b94546 22432 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22433 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22434
22435 switch (attr->form)
22436 {
22437 case DW_FORM_addr:
336d760d 22438 case DW_FORM_addrx:
b6807d98
TT
22439 case DW_FORM_GNU_addr_index:
22440 {
22441 gdb_byte *tem;
22442
22443 *len = cu->header.addr_size;
224c3ddb 22444 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22445 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22446 result = tem;
22447 }
22448 break;
22449 case DW_FORM_string:
22450 case DW_FORM_strp:
cf532bd1 22451 case DW_FORM_strx:
b6807d98
TT
22452 case DW_FORM_GNU_str_index:
22453 case DW_FORM_GNU_strp_alt:
22454 /* DW_STRING is already allocated on the objfile obstack, point
22455 directly to it. */
22456 result = (const gdb_byte *) DW_STRING (attr);
22457 *len = strlen (DW_STRING (attr));
22458 break;
22459 case DW_FORM_block1:
22460 case DW_FORM_block2:
22461 case DW_FORM_block4:
22462 case DW_FORM_block:
22463 case DW_FORM_exprloc:
0224619f 22464 case DW_FORM_data16:
b6807d98
TT
22465 result = DW_BLOCK (attr)->data;
22466 *len = DW_BLOCK (attr)->size;
22467 break;
22468
22469 /* The DW_AT_const_value attributes are supposed to carry the
22470 symbol's value "represented as it would be on the target
22471 architecture." By the time we get here, it's already been
22472 converted to host endianness, so we just need to sign- or
22473 zero-extend it as appropriate. */
22474 case DW_FORM_data1:
22475 type = die_type (die, cu);
22476 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22477 if (result == NULL)
22478 result = write_constant_as_bytes (obstack, byte_order,
22479 type, value, len);
22480 break;
22481 case DW_FORM_data2:
22482 type = die_type (die, cu);
22483 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22484 if (result == NULL)
22485 result = write_constant_as_bytes (obstack, byte_order,
22486 type, value, len);
22487 break;
22488 case DW_FORM_data4:
22489 type = die_type (die, cu);
22490 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22491 if (result == NULL)
22492 result = write_constant_as_bytes (obstack, byte_order,
22493 type, value, len);
22494 break;
22495 case DW_FORM_data8:
22496 type = die_type (die, cu);
22497 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22498 if (result == NULL)
22499 result = write_constant_as_bytes (obstack, byte_order,
22500 type, value, len);
22501 break;
22502
22503 case DW_FORM_sdata:
663c44ac 22504 case DW_FORM_implicit_const:
b6807d98
TT
22505 type = die_type (die, cu);
22506 result = write_constant_as_bytes (obstack, byte_order,
22507 type, DW_SND (attr), len);
22508 break;
22509
22510 case DW_FORM_udata:
22511 type = die_type (die, cu);
22512 result = write_constant_as_bytes (obstack, byte_order,
22513 type, DW_UNSND (attr), len);
22514 break;
22515
22516 default:
b98664d3 22517 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22518 dwarf_form_name (attr->form));
22519 break;
22520 }
22521
22522 return result;
22523}
22524
d4c9a4f8 22525/* See read.h. */
7942e96e
AA
22526
22527struct type *
9c541725 22528dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22529 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22530{
22531 struct dwarf2_cu *cu;
22532 struct die_info *die;
22533
7942e96e 22534 if (per_cu->cu == NULL)
58f0c718 22535 load_cu (per_cu, false);
7942e96e
AA
22536 cu = per_cu->cu;
22537 if (!cu)
22538 return NULL;
22539
9c541725 22540 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22541 if (!die)
22542 return NULL;
22543
22544 return die_type (die, cu);
22545}
22546
8cb5117c 22547/* See read.h. */
8a9b8146
TT
22548
22549struct type *
b64f50a1 22550dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22551 struct dwarf2_per_cu_data *per_cu)
22552{
9c541725 22553 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22554 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22555}
22556
ac9ec31b 22557/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22558 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22559 On exit *REF_CU is the CU of the result.
22560 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22561
22562static struct die_info *
ac9ec31b
DE
22563follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22564 struct dwarf2_cu **ref_cu)
348e048f 22565{
348e048f 22566 struct die_info temp_die;
c24bdb02 22567 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22568 struct die_info *die;
22569
ac9ec31b
DE
22570 /* While it might be nice to assert sig_type->type == NULL here,
22571 we can get here for DW_AT_imported_declaration where we need
22572 the DIE not the type. */
348e048f
DE
22573
22574 /* If necessary, add it to the queue and load its DIEs. */
22575
95554aad 22576 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22577 read_signatured_type (sig_type);
348e048f 22578
348e048f 22579 sig_cu = sig_type->per_cu.cu;
69d751e3 22580 gdb_assert (sig_cu != NULL);
9c541725
PA
22581 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22582 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22583 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22584 to_underlying (temp_die.sect_off));
348e048f
DE
22585 if (die)
22586 {
ed2dc618 22587 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22588 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22589
796a7ff8
DE
22590 /* For .gdb_index version 7 keep track of included TUs.
22591 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22592 if (dwarf2_per_objfile->index_table != NULL
22593 && dwarf2_per_objfile->index_table->version <= 7)
22594 {
ae640021 22595 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22596 }
22597
348e048f 22598 *ref_cu = sig_cu;
c24bdb02
KS
22599 if (sig_cu != cu)
22600 sig_cu->ancestor = cu;
22601
348e048f
DE
22602 return die;
22603 }
22604
ac9ec31b
DE
22605 return NULL;
22606}
22607
22608/* Follow signatured type referenced by ATTR in SRC_DIE.
22609 On entry *REF_CU is the CU of SRC_DIE.
22610 On exit *REF_CU is the CU of the result.
22611 The result is the DIE of the type.
22612 If the referenced type cannot be found an error is thrown. */
22613
22614static struct die_info *
ff39bb5e 22615follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22616 struct dwarf2_cu **ref_cu)
22617{
22618 ULONGEST signature = DW_SIGNATURE (attr);
22619 struct signatured_type *sig_type;
22620 struct die_info *die;
22621
22622 gdb_assert (attr->form == DW_FORM_ref_sig8);
22623
a2ce51a0 22624 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22625 /* sig_type will be NULL if the signatured type is missing from
22626 the debug info. */
22627 if (sig_type == NULL)
22628 {
22629 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22630 " from DIE at %s [in module %s]"),
22631 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22632 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22633 }
22634
22635 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22636 if (die == NULL)
22637 {
22638 dump_die_for_error (src_die);
22639 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22640 " from DIE at %s [in module %s]"),
22641 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22642 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22643 }
22644
22645 return die;
22646}
22647
22648/* Get the type specified by SIGNATURE referenced in DIE/CU,
22649 reading in and processing the type unit if necessary. */
22650
22651static struct type *
22652get_signatured_type (struct die_info *die, ULONGEST signature,
22653 struct dwarf2_cu *cu)
22654{
518817b3
SM
22655 struct dwarf2_per_objfile *dwarf2_per_objfile
22656 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22657 struct signatured_type *sig_type;
22658 struct dwarf2_cu *type_cu;
22659 struct die_info *type_die;
22660 struct type *type;
22661
a2ce51a0 22662 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22663 /* sig_type will be NULL if the signatured type is missing from
22664 the debug info. */
22665 if (sig_type == NULL)
22666 {
b98664d3 22667 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22668 " from DIE at %s [in module %s]"),
22669 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22670 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22671 return build_error_marker_type (cu, die);
22672 }
22673
22674 /* If we already know the type we're done. */
22675 if (sig_type->type != NULL)
22676 return sig_type->type;
22677
22678 type_cu = cu;
22679 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22680 if (type_die != NULL)
22681 {
22682 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22683 is created. This is important, for example, because for c++ classes
22684 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22685 type = read_type_die (type_die, type_cu);
22686 if (type == NULL)
22687 {
b98664d3 22688 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22689 " referenced from DIE at %s [in module %s]"),
22690 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22691 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22692 type = build_error_marker_type (cu, die);
22693 }
22694 }
22695 else
22696 {
b98664d3 22697 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22698 " from DIE at %s [in module %s]"),
22699 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22700 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22701 type = build_error_marker_type (cu, die);
22702 }
22703 sig_type->type = type;
22704
22705 return type;
22706}
22707
22708/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22709 reading in and processing the type unit if necessary. */
22710
22711static struct type *
ff39bb5e 22712get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22713 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22714{
22715 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22716 if (attr->form_is_ref ())
ac9ec31b
DE
22717 {
22718 struct dwarf2_cu *type_cu = cu;
22719 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22720
22721 return read_type_die (type_die, type_cu);
22722 }
22723 else if (attr->form == DW_FORM_ref_sig8)
22724 {
22725 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22726 }
22727 else
22728 {
518817b3
SM
22729 struct dwarf2_per_objfile *dwarf2_per_objfile
22730 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22731
b98664d3 22732 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22733 " at %s [in module %s]"),
22734 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22735 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22736 return build_error_marker_type (cu, die);
22737 }
348e048f
DE
22738}
22739
e5fe5e75 22740/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22741
22742static void
e5fe5e75 22743load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22744{
52dc124a 22745 struct signatured_type *sig_type;
348e048f 22746
f4dc4d17 22747 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22748 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22749
6721b2ec
DE
22750 /* We have the per_cu, but we need the signatured_type.
22751 Fortunately this is an easy translation. */
22752 gdb_assert (per_cu->is_debug_types);
22753 sig_type = (struct signatured_type *) per_cu;
348e048f 22754
6721b2ec 22755 gdb_assert (per_cu->cu == NULL);
348e048f 22756
52dc124a 22757 read_signatured_type (sig_type);
348e048f 22758
6721b2ec 22759 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22760}
22761
3019eac3
DE
22762/* Read in a signatured type and build its CU and DIEs.
22763 If the type is a stub for the real type in a DWO file,
22764 read in the real type from the DWO file as well. */
dee91e82
DE
22765
22766static void
22767read_signatured_type (struct signatured_type *sig_type)
22768{
22769 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22770
3019eac3 22771 gdb_assert (per_cu->is_debug_types);
dee91e82 22772 gdb_assert (per_cu->cu == NULL);
348e048f 22773
6751ebae 22774 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22775
22776 if (!reader.dummy_p)
22777 {
22778 struct dwarf2_cu *cu = reader.cu;
22779 const gdb_byte *info_ptr = reader.info_ptr;
22780
22781 gdb_assert (cu->die_hash == NULL);
22782 cu->die_hash =
22783 htab_create_alloc_ex (cu->header.length / 12,
22784 die_hash,
22785 die_eq,
22786 NULL,
22787 &cu->comp_unit_obstack,
22788 hashtab_obstack_allocate,
22789 dummy_obstack_deallocate);
22790
3e225074 22791 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22792 reader.comp_unit_die->child
22793 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22794 reader.comp_unit_die);
22795 cu->dies = reader.comp_unit_die;
22796 /* comp_unit_die is not stored in die_hash, no need. */
22797
22798 /* We try not to read any attributes in this function, because
22799 not all CUs needed for references have been loaded yet, and
22800 symbol table processing isn't initialized. But we have to
22801 set the CU language, or we won't be able to build types
22802 correctly. Similarly, if we do not read the producer, we can
22803 not apply producer-specific interpretation. */
22804 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22805
22806 reader.keep ();
c0ab21c2
TT
22807 }
22808
7ee85ab1 22809 sig_type->per_cu.tu_read = 1;
c906108c
SS
22810}
22811
c906108c
SS
22812/* Decode simple location descriptions.
22813 Given a pointer to a dwarf block that defines a location, compute
22814 the location and return the value.
22815
4cecd739
DJ
22816 NOTE drow/2003-11-18: This function is called in two situations
22817 now: for the address of static or global variables (partial symbols
22818 only) and for offsets into structures which are expected to be
22819 (more or less) constant. The partial symbol case should go away,
22820 and only the constant case should remain. That will let this
22821 function complain more accurately. A few special modes are allowed
22822 without complaint for global variables (for instance, global
22823 register values and thread-local values).
c906108c
SS
22824
22825 A location description containing no operations indicates that the
4cecd739 22826 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22827 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22828 callers will only want a very basic result and this can become a
21ae7a4d
JK
22829 complaint.
22830
22831 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22832
22833static CORE_ADDR
e7c27a73 22834decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22835{
518817b3 22836 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22837 size_t i;
22838 size_t size = blk->size;
d521ce57 22839 const gdb_byte *data = blk->data;
21ae7a4d
JK
22840 CORE_ADDR stack[64];
22841 int stacki;
22842 unsigned int bytes_read, unsnd;
22843 gdb_byte op;
c906108c 22844
21ae7a4d
JK
22845 i = 0;
22846 stacki = 0;
22847 stack[stacki] = 0;
22848 stack[++stacki] = 0;
22849
22850 while (i < size)
22851 {
22852 op = data[i++];
22853 switch (op)
22854 {
22855 case DW_OP_lit0:
22856 case DW_OP_lit1:
22857 case DW_OP_lit2:
22858 case DW_OP_lit3:
22859 case DW_OP_lit4:
22860 case DW_OP_lit5:
22861 case DW_OP_lit6:
22862 case DW_OP_lit7:
22863 case DW_OP_lit8:
22864 case DW_OP_lit9:
22865 case DW_OP_lit10:
22866 case DW_OP_lit11:
22867 case DW_OP_lit12:
22868 case DW_OP_lit13:
22869 case DW_OP_lit14:
22870 case DW_OP_lit15:
22871 case DW_OP_lit16:
22872 case DW_OP_lit17:
22873 case DW_OP_lit18:
22874 case DW_OP_lit19:
22875 case DW_OP_lit20:
22876 case DW_OP_lit21:
22877 case DW_OP_lit22:
22878 case DW_OP_lit23:
22879 case DW_OP_lit24:
22880 case DW_OP_lit25:
22881 case DW_OP_lit26:
22882 case DW_OP_lit27:
22883 case DW_OP_lit28:
22884 case DW_OP_lit29:
22885 case DW_OP_lit30:
22886 case DW_OP_lit31:
22887 stack[++stacki] = op - DW_OP_lit0;
22888 break;
f1bea926 22889
21ae7a4d
JK
22890 case DW_OP_reg0:
22891 case DW_OP_reg1:
22892 case DW_OP_reg2:
22893 case DW_OP_reg3:
22894 case DW_OP_reg4:
22895 case DW_OP_reg5:
22896 case DW_OP_reg6:
22897 case DW_OP_reg7:
22898 case DW_OP_reg8:
22899 case DW_OP_reg9:
22900 case DW_OP_reg10:
22901 case DW_OP_reg11:
22902 case DW_OP_reg12:
22903 case DW_OP_reg13:
22904 case DW_OP_reg14:
22905 case DW_OP_reg15:
22906 case DW_OP_reg16:
22907 case DW_OP_reg17:
22908 case DW_OP_reg18:
22909 case DW_OP_reg19:
22910 case DW_OP_reg20:
22911 case DW_OP_reg21:
22912 case DW_OP_reg22:
22913 case DW_OP_reg23:
22914 case DW_OP_reg24:
22915 case DW_OP_reg25:
22916 case DW_OP_reg26:
22917 case DW_OP_reg27:
22918 case DW_OP_reg28:
22919 case DW_OP_reg29:
22920 case DW_OP_reg30:
22921 case DW_OP_reg31:
22922 stack[++stacki] = op - DW_OP_reg0;
22923 if (i < size)
22924 dwarf2_complex_location_expr_complaint ();
22925 break;
c906108c 22926
21ae7a4d
JK
22927 case DW_OP_regx:
22928 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22929 i += bytes_read;
22930 stack[++stacki] = unsnd;
22931 if (i < size)
22932 dwarf2_complex_location_expr_complaint ();
22933 break;
c906108c 22934
21ae7a4d 22935 case DW_OP_addr:
c8a7a66f
TT
22936 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22937 &bytes_read);
21ae7a4d
JK
22938 i += bytes_read;
22939 break;
d53d4ac5 22940
21ae7a4d
JK
22941 case DW_OP_const1u:
22942 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22943 i += 1;
22944 break;
22945
22946 case DW_OP_const1s:
22947 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22948 i += 1;
22949 break;
22950
22951 case DW_OP_const2u:
22952 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22953 i += 2;
22954 break;
22955
22956 case DW_OP_const2s:
22957 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22958 i += 2;
22959 break;
d53d4ac5 22960
21ae7a4d
JK
22961 case DW_OP_const4u:
22962 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22963 i += 4;
22964 break;
22965
22966 case DW_OP_const4s:
22967 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22968 i += 4;
22969 break;
22970
585861ea
JK
22971 case DW_OP_const8u:
22972 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22973 i += 8;
22974 break;
22975
21ae7a4d
JK
22976 case DW_OP_constu:
22977 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22978 &bytes_read);
22979 i += bytes_read;
22980 break;
22981
22982 case DW_OP_consts:
22983 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22984 i += bytes_read;
22985 break;
22986
22987 case DW_OP_dup:
22988 stack[stacki + 1] = stack[stacki];
22989 stacki++;
22990 break;
22991
22992 case DW_OP_plus:
22993 stack[stacki - 1] += stack[stacki];
22994 stacki--;
22995 break;
22996
22997 case DW_OP_plus_uconst:
22998 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22999 &bytes_read);
23000 i += bytes_read;
23001 break;
23002
23003 case DW_OP_minus:
23004 stack[stacki - 1] -= stack[stacki];
23005 stacki--;
23006 break;
23007
23008 case DW_OP_deref:
23009 /* If we're not the last op, then we definitely can't encode
23010 this using GDB's address_class enum. This is valid for partial
23011 global symbols, although the variable's address will be bogus
23012 in the psymtab. */
23013 if (i < size)
23014 dwarf2_complex_location_expr_complaint ();
23015 break;
23016
23017 case DW_OP_GNU_push_tls_address:
4aa4e28b 23018 case DW_OP_form_tls_address:
21ae7a4d
JK
23019 /* The top of the stack has the offset from the beginning
23020 of the thread control block at which the variable is located. */
23021 /* Nothing should follow this operator, so the top of stack would
23022 be returned. */
23023 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23024 address will be bogus in the psymtab. Make it always at least
23025 non-zero to not look as a variable garbage collected by linker
23026 which have DW_OP_addr 0. */
21ae7a4d
JK
23027 if (i < size)
23028 dwarf2_complex_location_expr_complaint ();
585861ea 23029 stack[stacki]++;
21ae7a4d
JK
23030 break;
23031
23032 case DW_OP_GNU_uninit:
23033 break;
23034
336d760d 23035 case DW_OP_addrx:
3019eac3 23036 case DW_OP_GNU_addr_index:
49f6c839 23037 case DW_OP_GNU_const_index:
3019eac3
DE
23038 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23039 &bytes_read);
23040 i += bytes_read;
23041 break;
23042
21ae7a4d
JK
23043 default:
23044 {
f39c6ffd 23045 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23046
23047 if (name)
b98664d3 23048 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23049 name);
23050 else
b98664d3 23051 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23052 op);
23053 }
23054
23055 return (stack[stacki]);
d53d4ac5 23056 }
3c6e0cb3 23057
21ae7a4d
JK
23058 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23059 outside of the allocated space. Also enforce minimum>0. */
23060 if (stacki >= ARRAY_SIZE (stack) - 1)
23061 {
b98664d3 23062 complaint (_("location description stack overflow"));
21ae7a4d
JK
23063 return 0;
23064 }
23065
23066 if (stacki <= 0)
23067 {
b98664d3 23068 complaint (_("location description stack underflow"));
21ae7a4d
JK
23069 return 0;
23070 }
23071 }
23072 return (stack[stacki]);
c906108c
SS
23073}
23074
23075/* memory allocation interface */
23076
c906108c 23077static struct dwarf_block *
7b5a2f43 23078dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23079{
8d749320 23080 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23081}
23082
c906108c 23083static struct die_info *
b60c80d6 23084dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23085{
23086 struct die_info *die;
b60c80d6
DJ
23087 size_t size = sizeof (struct die_info);
23088
23089 if (num_attrs > 1)
23090 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23091
b60c80d6 23092 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23093 memset (die, 0, sizeof (struct die_info));
23094 return (die);
23095}
2e276125
JB
23096
23097\f
23098/* Macro support. */
23099
2e276125 23100static struct macro_source_file *
804d2729
TT
23101macro_start_file (struct dwarf2_cu *cu,
23102 int file, int line,
2e276125 23103 struct macro_source_file *current_file,
43f3e411 23104 struct line_header *lh)
2e276125 23105{
233d95b5 23106 /* File name relative to the compilation directory of this source file. */
03075812 23107 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
2e276125 23108
2e276125 23109 if (! current_file)
abc9d0dc 23110 {
fc474241
DE
23111 /* Note: We don't create a macro table for this compilation unit
23112 at all until we actually get a filename. */
c24bdb02 23113 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 23114
abc9d0dc
TT
23115 /* If we have no current file, then this must be the start_file
23116 directive for the compilation unit's main source file. */
03075812 23117 current_file = macro_set_main (macro_table, file_name.get ());
fc474241 23118 macro_define_special (macro_table);
abc9d0dc 23119 }
2e276125 23120 else
03075812 23121 current_file = macro_include (current_file, line, file_name.get ());
6e70227d 23122
2e276125
JB
23123 return current_file;
23124}
23125
2e276125
JB
23126static const char *
23127consume_improper_spaces (const char *p, const char *body)
23128{
23129 if (*p == ' ')
23130 {
b98664d3 23131 complaint (_("macro definition contains spaces "
3e43a32a 23132 "in formal argument list:\n`%s'"),
4d3c2250 23133 body);
2e276125
JB
23134
23135 while (*p == ' ')
23136 p++;
23137 }
23138
23139 return p;
23140}
23141
23142
23143static void
23144parse_macro_definition (struct macro_source_file *file, int line,
23145 const char *body)
23146{
23147 const char *p;
23148
23149 /* The body string takes one of two forms. For object-like macro
23150 definitions, it should be:
23151
23152 <macro name> " " <definition>
23153
23154 For function-like macro definitions, it should be:
23155
23156 <macro name> "() " <definition>
23157 or
23158 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23159
23160 Spaces may appear only where explicitly indicated, and in the
23161 <definition>.
23162
23163 The Dwarf 2 spec says that an object-like macro's name is always
23164 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23165 the space when the macro's definition is the empty string.
2e276125
JB
23166
23167 The Dwarf 2 spec says that there should be no spaces between the
23168 formal arguments in a function-like macro's formal argument list,
23169 but versions of GCC around March 2002 include spaces after the
23170 commas. */
23171
23172
23173 /* Find the extent of the macro name. The macro name is terminated
23174 by either a space or null character (for an object-like macro) or
23175 an opening paren (for a function-like macro). */
23176 for (p = body; *p; p++)
23177 if (*p == ' ' || *p == '(')
23178 break;
23179
23180 if (*p == ' ' || *p == '\0')
23181 {
23182 /* It's an object-like macro. */
23183 int name_len = p - body;
456e800a 23184 std::string name (body, name_len);
2e276125
JB
23185 const char *replacement;
23186
23187 if (*p == ' ')
23188 replacement = body + name_len + 1;
23189 else
23190 {
4d3c2250 23191 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23192 replacement = body + name_len;
23193 }
6e70227d 23194
456e800a 23195 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23196 }
23197 else if (*p == '(')
23198 {
23199 /* It's a function-like macro. */
456e800a 23200 std::string name (body, p - body);
2e276125
JB
23201 int argc = 0;
23202 int argv_size = 1;
8d749320 23203 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23204
23205 p++;
23206
23207 p = consume_improper_spaces (p, body);
23208
23209 /* Parse the formal argument list. */
23210 while (*p && *p != ')')
23211 {
23212 /* Find the extent of the current argument name. */
23213 const char *arg_start = p;
23214
23215 while (*p && *p != ',' && *p != ')' && *p != ' ')
23216 p++;
23217
23218 if (! *p || p == arg_start)
4d3c2250 23219 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23220 else
23221 {
23222 /* Make sure argv has room for the new argument. */
23223 if (argc >= argv_size)
23224 {
23225 argv_size *= 2;
224c3ddb 23226 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23227 }
23228
3f8a7804 23229 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23230 }
23231
23232 p = consume_improper_spaces (p, body);
23233
23234 /* Consume the comma, if present. */
23235 if (*p == ',')
23236 {
23237 p++;
23238
23239 p = consume_improper_spaces (p, body);
23240 }
23241 }
23242
23243 if (*p == ')')
23244 {
23245 p++;
23246
23247 if (*p == ' ')
23248 /* Perfectly formed definition, no complaints. */
456e800a 23249 macro_define_function (file, line, name.c_str (),
6e70227d 23250 argc, (const char **) argv,
2e276125
JB
23251 p + 1);
23252 else if (*p == '\0')
23253 {
23254 /* Complain, but do define it. */
4d3c2250 23255 dwarf2_macro_malformed_definition_complaint (body);
456e800a 23256 macro_define_function (file, line, name.c_str (),
6e70227d 23257 argc, (const char **) argv,
2e276125
JB
23258 p);
23259 }
23260 else
23261 /* Just complain. */
4d3c2250 23262 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23263 }
23264 else
23265 /* Just complain. */
4d3c2250 23266 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23267
2e276125
JB
23268 {
23269 int i;
23270
23271 for (i = 0; i < argc; i++)
23272 xfree (argv[i]);
23273 }
23274 xfree (argv);
23275 }
23276 else
4d3c2250 23277 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23278}
23279
cf2c3c16
TT
23280/* Skip some bytes from BYTES according to the form given in FORM.
23281 Returns the new pointer. */
2e276125 23282
d521ce57
TT
23283static const gdb_byte *
23284skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23285 enum dwarf_form form,
23286 unsigned int offset_size,
23287 struct dwarf2_section_info *section)
2e276125 23288{
cf2c3c16 23289 unsigned int bytes_read;
2e276125 23290
cf2c3c16 23291 switch (form)
2e276125 23292 {
cf2c3c16
TT
23293 case DW_FORM_data1:
23294 case DW_FORM_flag:
23295 ++bytes;
23296 break;
23297
23298 case DW_FORM_data2:
23299 bytes += 2;
23300 break;
23301
23302 case DW_FORM_data4:
23303 bytes += 4;
23304 break;
23305
23306 case DW_FORM_data8:
23307 bytes += 8;
23308 break;
23309
0224619f
JK
23310 case DW_FORM_data16:
23311 bytes += 16;
23312 break;
23313
cf2c3c16
TT
23314 case DW_FORM_string:
23315 read_direct_string (abfd, bytes, &bytes_read);
23316 bytes += bytes_read;
23317 break;
23318
23319 case DW_FORM_sec_offset:
23320 case DW_FORM_strp:
36586728 23321 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23322 bytes += offset_size;
23323 break;
23324
23325 case DW_FORM_block:
23326 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23327 bytes += bytes_read;
23328 break;
23329
23330 case DW_FORM_block1:
23331 bytes += 1 + read_1_byte (abfd, bytes);
23332 break;
23333 case DW_FORM_block2:
23334 bytes += 2 + read_2_bytes (abfd, bytes);
23335 break;
23336 case DW_FORM_block4:
23337 bytes += 4 + read_4_bytes (abfd, bytes);
23338 break;
23339
336d760d 23340 case DW_FORM_addrx:
cf2c3c16 23341 case DW_FORM_sdata:
cf532bd1 23342 case DW_FORM_strx:
cf2c3c16 23343 case DW_FORM_udata:
3019eac3
DE
23344 case DW_FORM_GNU_addr_index:
23345 case DW_FORM_GNU_str_index:
d521ce57 23346 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23347 if (bytes == NULL)
23348 {
23349 dwarf2_section_buffer_overflow_complaint (section);
23350 return NULL;
23351 }
cf2c3c16
TT
23352 break;
23353
663c44ac
JK
23354 case DW_FORM_implicit_const:
23355 break;
23356
cf2c3c16
TT
23357 default:
23358 {
b98664d3 23359 complaint (_("invalid form 0x%x in `%s'"),
96b79293 23360 form, section->get_name ());
cf2c3c16
TT
23361 return NULL;
23362 }
2e276125
JB
23363 }
23364
cf2c3c16
TT
23365 return bytes;
23366}
757a13d0 23367
cf2c3c16
TT
23368/* A helper for dwarf_decode_macros that handles skipping an unknown
23369 opcode. Returns an updated pointer to the macro data buffer; or,
23370 on error, issues a complaint and returns NULL. */
757a13d0 23371
d521ce57 23372static const gdb_byte *
cf2c3c16 23373skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23374 const gdb_byte **opcode_definitions,
23375 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23376 bfd *abfd,
23377 unsigned int offset_size,
23378 struct dwarf2_section_info *section)
23379{
23380 unsigned int bytes_read, i;
23381 unsigned long arg;
d521ce57 23382 const gdb_byte *defn;
2e276125 23383
cf2c3c16 23384 if (opcode_definitions[opcode] == NULL)
2e276125 23385 {
b98664d3 23386 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23387 opcode);
23388 return NULL;
23389 }
2e276125 23390
cf2c3c16
TT
23391 defn = opcode_definitions[opcode];
23392 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23393 defn += bytes_read;
2e276125 23394
cf2c3c16
TT
23395 for (i = 0; i < arg; ++i)
23396 {
aead7601
SM
23397 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23398 (enum dwarf_form) defn[i], offset_size,
f664829e 23399 section);
cf2c3c16
TT
23400 if (mac_ptr == NULL)
23401 {
23402 /* skip_form_bytes already issued the complaint. */
23403 return NULL;
23404 }
23405 }
757a13d0 23406
cf2c3c16
TT
23407 return mac_ptr;
23408}
757a13d0 23409
cf2c3c16
TT
23410/* A helper function which parses the header of a macro section.
23411 If the macro section is the extended (for now called "GNU") type,
23412 then this updates *OFFSET_SIZE. Returns a pointer to just after
23413 the header, or issues a complaint and returns NULL on error. */
757a13d0 23414
d521ce57
TT
23415static const gdb_byte *
23416dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 23417 bfd *abfd,
d521ce57 23418 const gdb_byte *mac_ptr,
cf2c3c16
TT
23419 unsigned int *offset_size,
23420 int section_is_gnu)
23421{
23422 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 23423
cf2c3c16
TT
23424 if (section_is_gnu)
23425 {
23426 unsigned int version, flags;
757a13d0 23427
cf2c3c16 23428 version = read_2_bytes (abfd, mac_ptr);
0af92d60 23429 if (version != 4 && version != 5)
cf2c3c16 23430 {
b98664d3 23431 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
23432 version);
23433 return NULL;
23434 }
23435 mac_ptr += 2;
757a13d0 23436
cf2c3c16
TT
23437 flags = read_1_byte (abfd, mac_ptr);
23438 ++mac_ptr;
23439 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 23440
cf2c3c16
TT
23441 if ((flags & 2) != 0)
23442 /* We don't need the line table offset. */
23443 mac_ptr += *offset_size;
757a13d0 23444
cf2c3c16
TT
23445 /* Vendor opcode descriptions. */
23446 if ((flags & 4) != 0)
23447 {
23448 unsigned int i, count;
757a13d0 23449
cf2c3c16
TT
23450 count = read_1_byte (abfd, mac_ptr);
23451 ++mac_ptr;
23452 for (i = 0; i < count; ++i)
23453 {
23454 unsigned int opcode, bytes_read;
23455 unsigned long arg;
23456
23457 opcode = read_1_byte (abfd, mac_ptr);
23458 ++mac_ptr;
23459 opcode_definitions[opcode] = mac_ptr;
23460 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23461 mac_ptr += bytes_read;
23462 mac_ptr += arg;
23463 }
757a13d0 23464 }
cf2c3c16 23465 }
757a13d0 23466
cf2c3c16
TT
23467 return mac_ptr;
23468}
757a13d0 23469
cf2c3c16 23470/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 23471 including DW_MACRO_import. */
cf2c3c16
TT
23472
23473static void
804d2729 23474dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 23475 bfd *abfd,
d521ce57 23476 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 23477 struct macro_source_file *current_file,
43f3e411 23478 struct line_header *lh,
cf2c3c16 23479 struct dwarf2_section_info *section,
36586728 23480 int section_is_gnu, int section_is_dwz,
cf2c3c16 23481 unsigned int offset_size,
8fc3fc34 23482 htab_t include_hash)
cf2c3c16 23483{
804d2729
TT
23484 struct dwarf2_per_objfile *dwarf2_per_objfile
23485 = cu->per_cu->dwarf2_per_objfile;
4d663531 23486 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
23487 enum dwarf_macro_record_type macinfo_type;
23488 int at_commandline;
d521ce57 23489 const gdb_byte *opcode_definitions[256];
757a13d0 23490
cf2c3c16
TT
23491 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23492 &offset_size, section_is_gnu);
23493 if (mac_ptr == NULL)
23494 {
23495 /* We already issued a complaint. */
23496 return;
23497 }
757a13d0
JK
23498
23499 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23500 GDB is still reading the definitions from command line. First
23501 DW_MACINFO_start_file will need to be ignored as it was already executed
23502 to create CURRENT_FILE for the main source holding also the command line
23503 definitions. On first met DW_MACINFO_start_file this flag is reset to
23504 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23505
23506 at_commandline = 1;
23507
23508 do
23509 {
23510 /* Do we at least have room for a macinfo type byte? */
23511 if (mac_ptr >= mac_end)
23512 {
f664829e 23513 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
23514 break;
23515 }
23516
aead7601 23517 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
23518 mac_ptr++;
23519
cf2c3c16
TT
23520 /* Note that we rely on the fact that the corresponding GNU and
23521 DWARF constants are the same. */
132448f8
SM
23522 DIAGNOSTIC_PUSH
23523 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
23524 switch (macinfo_type)
23525 {
23526 /* A zero macinfo type indicates the end of the macro
23527 information. */
23528 case 0:
23529 break;
2e276125 23530
0af92d60
JK
23531 case DW_MACRO_define:
23532 case DW_MACRO_undef:
23533 case DW_MACRO_define_strp:
23534 case DW_MACRO_undef_strp:
23535 case DW_MACRO_define_sup:
23536 case DW_MACRO_undef_sup:
2e276125 23537 {
891d2f0b 23538 unsigned int bytes_read;
2e276125 23539 int line;
d521ce57 23540 const char *body;
cf2c3c16 23541 int is_define;
2e276125 23542
cf2c3c16
TT
23543 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23544 mac_ptr += bytes_read;
23545
0af92d60
JK
23546 if (macinfo_type == DW_MACRO_define
23547 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
23548 {
23549 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23550 mac_ptr += bytes_read;
23551 }
23552 else
23553 {
23554 LONGEST str_offset;
23555
24aa364d 23556 str_offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16 23557 mac_ptr += offset_size;
2e276125 23558
0af92d60
JK
23559 if (macinfo_type == DW_MACRO_define_sup
23560 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 23561 || section_is_dwz)
36586728 23562 {
ed2dc618
SM
23563 struct dwz_file *dwz
23564 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 23565
ed2dc618
SM
23566 body = read_indirect_string_from_dwz (objfile,
23567 dwz, str_offset);
36586728
TT
23568 }
23569 else
ed2dc618
SM
23570 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23571 abfd, str_offset);
cf2c3c16
TT
23572 }
23573
0af92d60
JK
23574 is_define = (macinfo_type == DW_MACRO_define
23575 || macinfo_type == DW_MACRO_define_strp
23576 || macinfo_type == DW_MACRO_define_sup);
2e276125 23577 if (! current_file)
757a13d0
JK
23578 {
23579 /* DWARF violation as no main source is present. */
b98664d3 23580 complaint (_("debug info with no main source gives macro %s "
757a13d0 23581 "on line %d: %s"),
cf2c3c16
TT
23582 is_define ? _("definition") : _("undefinition"),
23583 line, body);
757a13d0
JK
23584 break;
23585 }
3e43a32a
MS
23586 if ((line == 0 && !at_commandline)
23587 || (line != 0 && at_commandline))
b98664d3 23588 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 23589 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 23590 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
23591 line == 0 ? _("zero") : _("non-zero"), line, body);
23592
955b06fa 23593 if (body == NULL)
7bede828 23594 {
955b06fa
SDJ
23595 /* Fedora's rpm-build's "debugedit" binary
23596 corrupted .debug_macro sections.
23597
23598 For more info, see
23599 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23600 complaint (_("debug info gives %s invalid macro %s "
23601 "without body (corrupted?) at line %d "
23602 "on file %s"),
23603 at_commandline ? _("command-line") : _("in-file"),
23604 is_define ? _("definition") : _("undefinition"),
23605 line, current_file->filename);
7bede828 23606 }
955b06fa
SDJ
23607 else if (is_define)
23608 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
23609 else
23610 {
0af92d60
JK
23611 gdb_assert (macinfo_type == DW_MACRO_undef
23612 || macinfo_type == DW_MACRO_undef_strp
23613 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
23614 macro_undef (current_file, line, body);
23615 }
2e276125
JB
23616 }
23617 break;
23618
0af92d60 23619 case DW_MACRO_start_file:
2e276125 23620 {
891d2f0b 23621 unsigned int bytes_read;
2e276125
JB
23622 int line, file;
23623
23624 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23625 mac_ptr += bytes_read;
23626 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23627 mac_ptr += bytes_read;
23628
3e43a32a
MS
23629 if ((line == 0 && !at_commandline)
23630 || (line != 0 && at_commandline))
b98664d3 23631 complaint (_("debug info gives source %d included "
757a13d0
JK
23632 "from %s at %s line %d"),
23633 file, at_commandline ? _("command-line") : _("file"),
23634 line == 0 ? _("zero") : _("non-zero"), line);
23635
23636 if (at_commandline)
23637 {
0af92d60 23638 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23639 pass one. */
757a13d0
JK
23640 at_commandline = 0;
23641 }
23642 else
804d2729
TT
23643 current_file = macro_start_file (cu, file, line, current_file,
23644 lh);
2e276125
JB
23645 }
23646 break;
23647
0af92d60 23648 case DW_MACRO_end_file:
2e276125 23649 if (! current_file)
b98664d3 23650 complaint (_("macro debug info has an unmatched "
3e43a32a 23651 "`close_file' directive"));
2e276125
JB
23652 else
23653 {
23654 current_file = current_file->included_by;
23655 if (! current_file)
23656 {
cf2c3c16 23657 enum dwarf_macro_record_type next_type;
2e276125
JB
23658
23659 /* GCC circa March 2002 doesn't produce the zero
23660 type byte marking the end of the compilation
23661 unit. Complain if it's not there, but exit no
23662 matter what. */
23663
23664 /* Do we at least have room for a macinfo type byte? */
23665 if (mac_ptr >= mac_end)
23666 {
f664829e 23667 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23668 return;
23669 }
23670
23671 /* We don't increment mac_ptr here, so this is just
23672 a look-ahead. */
aead7601
SM
23673 next_type
23674 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23675 mac_ptr);
2e276125 23676 if (next_type != 0)
b98664d3 23677 complaint (_("no terminating 0-type entry for "
3e43a32a 23678 "macros in `.debug_macinfo' section"));
2e276125
JB
23679
23680 return;
23681 }
23682 }
23683 break;
23684
0af92d60
JK
23685 case DW_MACRO_import:
23686 case DW_MACRO_import_sup:
cf2c3c16
TT
23687 {
23688 LONGEST offset;
8fc3fc34 23689 void **slot;
a036ba48
TT
23690 bfd *include_bfd = abfd;
23691 struct dwarf2_section_info *include_section = section;
d521ce57 23692 const gdb_byte *include_mac_end = mac_end;
a036ba48 23693 int is_dwz = section_is_dwz;
d521ce57 23694 const gdb_byte *new_mac_ptr;
cf2c3c16 23695
24aa364d 23696 offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16
TT
23697 mac_ptr += offset_size;
23698
0af92d60 23699 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 23700 {
ed2dc618 23701 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 23702
96b79293 23703 dwz->macro.read (objfile);
a036ba48 23704
a036ba48 23705 include_section = &dwz->macro;
96b79293 23706 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
23707 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23708 is_dwz = 1;
23709 }
23710
23711 new_mac_ptr = include_section->buffer + offset;
23712 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23713
8fc3fc34
TT
23714 if (*slot != NULL)
23715 {
23716 /* This has actually happened; see
23717 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 23718 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
23719 ".debug_macro section"));
23720 }
23721 else
23722 {
d521ce57 23723 *slot = (void *) new_mac_ptr;
36586728 23724
804d2729 23725 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 23726 include_mac_end, current_file, lh,
36586728 23727 section, section_is_gnu, is_dwz,
4d663531 23728 offset_size, include_hash);
8fc3fc34 23729
d521ce57 23730 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23731 }
cf2c3c16
TT
23732 }
23733 break;
23734
2e276125 23735 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23736 if (!section_is_gnu)
23737 {
23738 unsigned int bytes_read;
2e276125 23739
ac298888
TT
23740 /* This reads the constant, but since we don't recognize
23741 any vendor extensions, we ignore it. */
23742 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23743 mac_ptr += bytes_read;
23744 read_direct_string (abfd, mac_ptr, &bytes_read);
23745 mac_ptr += bytes_read;
2e276125 23746
cf2c3c16
TT
23747 /* We don't recognize any vendor extensions. */
23748 break;
23749 }
23750 /* FALLTHROUGH */
23751
23752 default:
23753 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23754 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23755 section);
23756 if (mac_ptr == NULL)
23757 return;
23758 break;
2e276125 23759 }
132448f8 23760 DIAGNOSTIC_POP
757a13d0 23761 } while (macinfo_type != 0);
2e276125 23762}
8e19ed76 23763
cf2c3c16 23764static void
09262596 23765dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 23766 int section_is_gnu)
cf2c3c16 23767{
518817b3
SM
23768 struct dwarf2_per_objfile *dwarf2_per_objfile
23769 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23770 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
23771 struct line_header *lh = cu->line_header;
23772 bfd *abfd;
d521ce57 23773 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23774 struct macro_source_file *current_file = 0;
23775 enum dwarf_macro_record_type macinfo_type;
23776 unsigned int offset_size = cu->header.offset_size;
d521ce57 23777 const gdb_byte *opcode_definitions[256];
8fc3fc34 23778 void **slot;
09262596
DE
23779 struct dwarf2_section_info *section;
23780 const char *section_name;
23781
23782 if (cu->dwo_unit != NULL)
23783 {
23784 if (section_is_gnu)
23785 {
23786 section = &cu->dwo_unit->dwo_file->sections.macro;
23787 section_name = ".debug_macro.dwo";
23788 }
23789 else
23790 {
23791 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23792 section_name = ".debug_macinfo.dwo";
23793 }
23794 }
23795 else
23796 {
23797 if (section_is_gnu)
23798 {
23799 section = &dwarf2_per_objfile->macro;
23800 section_name = ".debug_macro";
23801 }
23802 else
23803 {
23804 section = &dwarf2_per_objfile->macinfo;
23805 section_name = ".debug_macinfo";
23806 }
23807 }
cf2c3c16 23808
96b79293 23809 section->read (objfile);
cf2c3c16
TT
23810 if (section->buffer == NULL)
23811 {
b98664d3 23812 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
23813 return;
23814 }
96b79293 23815 abfd = section->get_bfd_owner ();
cf2c3c16
TT
23816
23817 /* First pass: Find the name of the base filename.
23818 This filename is needed in order to process all macros whose definition
23819 (or undefinition) comes from the command line. These macros are defined
23820 before the first DW_MACINFO_start_file entry, and yet still need to be
23821 associated to the base file.
23822
23823 To determine the base file name, we scan the macro definitions until we
23824 reach the first DW_MACINFO_start_file entry. We then initialize
23825 CURRENT_FILE accordingly so that any macro definition found before the
23826 first DW_MACINFO_start_file can still be associated to the base file. */
23827
23828 mac_ptr = section->buffer + offset;
23829 mac_end = section->buffer + section->size;
23830
23831 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23832 &offset_size, section_is_gnu);
23833 if (mac_ptr == NULL)
23834 {
23835 /* We already issued a complaint. */
23836 return;
23837 }
23838
23839 do
23840 {
23841 /* Do we at least have room for a macinfo type byte? */
23842 if (mac_ptr >= mac_end)
23843 {
23844 /* Complaint is printed during the second pass as GDB will probably
23845 stop the first pass earlier upon finding
23846 DW_MACINFO_start_file. */
23847 break;
23848 }
23849
aead7601 23850 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23851 mac_ptr++;
23852
23853 /* Note that we rely on the fact that the corresponding GNU and
23854 DWARF constants are the same. */
132448f8
SM
23855 DIAGNOSTIC_PUSH
23856 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
23857 switch (macinfo_type)
23858 {
23859 /* A zero macinfo type indicates the end of the macro
23860 information. */
23861 case 0:
23862 break;
23863
0af92d60
JK
23864 case DW_MACRO_define:
23865 case DW_MACRO_undef:
cf2c3c16
TT
23866 /* Only skip the data by MAC_PTR. */
23867 {
23868 unsigned int bytes_read;
23869
23870 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23871 mac_ptr += bytes_read;
23872 read_direct_string (abfd, mac_ptr, &bytes_read);
23873 mac_ptr += bytes_read;
23874 }
23875 break;
23876
0af92d60 23877 case DW_MACRO_start_file:
cf2c3c16
TT
23878 {
23879 unsigned int bytes_read;
23880 int line, file;
23881
23882 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23883 mac_ptr += bytes_read;
23884 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23885 mac_ptr += bytes_read;
23886
804d2729 23887 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
23888 }
23889 break;
23890
0af92d60 23891 case DW_MACRO_end_file:
cf2c3c16
TT
23892 /* No data to skip by MAC_PTR. */
23893 break;
23894
0af92d60
JK
23895 case DW_MACRO_define_strp:
23896 case DW_MACRO_undef_strp:
23897 case DW_MACRO_define_sup:
23898 case DW_MACRO_undef_sup:
cf2c3c16
TT
23899 {
23900 unsigned int bytes_read;
23901
23902 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23903 mac_ptr += bytes_read;
23904 mac_ptr += offset_size;
23905 }
23906 break;
23907
0af92d60
JK
23908 case DW_MACRO_import:
23909 case DW_MACRO_import_sup:
cf2c3c16 23910 /* Note that, according to the spec, a transparent include
0af92d60 23911 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23912 skip this opcode. */
23913 mac_ptr += offset_size;
23914 break;
23915
23916 case DW_MACINFO_vendor_ext:
23917 /* Only skip the data by MAC_PTR. */
23918 if (!section_is_gnu)
23919 {
23920 unsigned int bytes_read;
23921
23922 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23923 mac_ptr += bytes_read;
23924 read_direct_string (abfd, mac_ptr, &bytes_read);
23925 mac_ptr += bytes_read;
23926 }
23927 /* FALLTHROUGH */
23928
23929 default:
23930 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23931 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23932 section);
23933 if (mac_ptr == NULL)
23934 return;
23935 break;
23936 }
132448f8 23937 DIAGNOSTIC_POP
cf2c3c16
TT
23938 } while (macinfo_type != 0 && current_file == NULL);
23939
23940 /* Second pass: Process all entries.
23941
23942 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23943 command-line macro definitions/undefinitions. This flag is unset when we
23944 reach the first DW_MACINFO_start_file entry. */
23945
fc4007c9
TT
23946 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23947 htab_eq_pointer,
23948 NULL, xcalloc, xfree));
8fc3fc34 23949 mac_ptr = section->buffer + offset;
fc4007c9 23950 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23951 *slot = (void *) mac_ptr;
804d2729 23952 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 23953 current_file, lh, section,
fc4007c9
TT
23954 section_is_gnu, 0, offset_size,
23955 include_hash.get ());
cf2c3c16
TT
23956}
23957
3019eac3
DE
23958/* Return the .debug_loc section to use for CU.
23959 For DWO files use .debug_loc.dwo. */
23960
23961static struct dwarf2_section_info *
23962cu_debug_loc_section (struct dwarf2_cu *cu)
23963{
518817b3
SM
23964 struct dwarf2_per_objfile *dwarf2_per_objfile
23965 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23966
3019eac3 23967 if (cu->dwo_unit)
43988095
JK
23968 {
23969 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23970
43988095
JK
23971 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23972 }
23973 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23974 : &dwarf2_per_objfile->loc);
3019eac3
DE
23975}
23976
8cf6f0b1
TT
23977/* A helper function that fills in a dwarf2_loclist_baton. */
23978
23979static void
23980fill_in_loclist_baton (struct dwarf2_cu *cu,
23981 struct dwarf2_loclist_baton *baton,
ff39bb5e 23982 const struct attribute *attr)
8cf6f0b1 23983{
518817b3
SM
23984 struct dwarf2_per_objfile *dwarf2_per_objfile
23985 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23986 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23987
96b79293 23988 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23989
23990 baton->per_cu = cu->per_cu;
23991 gdb_assert (baton->per_cu);
23992 /* We don't know how long the location list is, but make sure we
23993 don't run off the edge of the section. */
3019eac3
DE
23994 baton->size = section->size - DW_UNSND (attr);
23995 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23996 baton->base_address = cu->base_address;
f664829e 23997 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23998}
23999
4c2df51b 24000static void
ff39bb5e 24001dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24002 struct dwarf2_cu *cu, int is_block)
4c2df51b 24003{
518817b3
SM
24004 struct dwarf2_per_objfile *dwarf2_per_objfile
24005 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24006 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24007 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24008
cd6c91b4 24009 if (attr->form_is_section_offset ()
3019eac3 24010 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24011 the section. If so, fall through to the complaint in the
24012 other branch. */
2c7d5afc 24013 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 24014 {
0d53c4c4 24015 struct dwarf2_loclist_baton *baton;
4c2df51b 24016
8d749320 24017 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24018
8cf6f0b1 24019 fill_in_loclist_baton (cu, baton, attr);
be391dca 24020
d00adf39 24021 if (cu->base_known == 0)
b98664d3 24022 complaint (_("Location list used without "
3e43a32a 24023 "specifying the CU base address."));
4c2df51b 24024
f1e6e072
TT
24025 SYMBOL_ACLASS_INDEX (sym) = (is_block
24026 ? dwarf2_loclist_block_index
24027 : dwarf2_loclist_index);
0d53c4c4
DJ
24028 SYMBOL_LOCATION_BATON (sym) = baton;
24029 }
24030 else
24031 {
24032 struct dwarf2_locexpr_baton *baton;
24033
8d749320 24034 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24035 baton->per_cu = cu->per_cu;
24036 gdb_assert (baton->per_cu);
0d53c4c4 24037
4fc6c0d5 24038 if (attr->form_is_block ())
0d53c4c4
DJ
24039 {
24040 /* Note that we're just copying the block's data pointer
24041 here, not the actual data. We're still pointing into the
6502dd73
DJ
24042 info_buffer for SYM's objfile; right now we never release
24043 that buffer, but when we do clean up properly this may
24044 need to change. */
0d53c4c4
DJ
24045 baton->size = DW_BLOCK (attr)->size;
24046 baton->data = DW_BLOCK (attr)->data;
24047 }
24048 else
24049 {
24050 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24051 sym->natural_name ());
0d53c4c4 24052 baton->size = 0;
0d53c4c4 24053 }
6e70227d 24054
f1e6e072
TT
24055 SYMBOL_ACLASS_INDEX (sym) = (is_block
24056 ? dwarf2_locexpr_block_index
24057 : dwarf2_locexpr_index);
0d53c4c4
DJ
24058 SYMBOL_LOCATION_BATON (sym) = baton;
24059 }
4c2df51b 24060}
6502dd73 24061
09ba997f 24062/* See read.h. */
ae0d2f24
UW
24063
24064struct objfile *
09ba997f 24065dwarf2_per_cu_data::objfile () const
ae0d2f24 24066{
09ba997f 24067 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
24068
24069 /* Return the master objfile, so that we can report and look up the
24070 correct file containing this variable. */
24071 if (objfile->separate_debug_objfile_backlink)
24072 objfile = objfile->separate_debug_objfile_backlink;
24073
24074 return objfile;
24075}
24076
96408a79
SA
24077/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24078 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24079 CU_HEADERP first. */
24080
24081static const struct comp_unit_head *
24082per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 24083 const struct dwarf2_per_cu_data *per_cu)
96408a79 24084{
d521ce57 24085 const gdb_byte *info_ptr;
96408a79
SA
24086
24087 if (per_cu->cu)
24088 return &per_cu->cu->header;
24089
9c541725 24090 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24091
24092 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24093 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24094 rcuh_kind::COMPILE);
96408a79
SA
24095
24096 return cu_headerp;
24097}
24098
09ba997f 24099/* See read.h. */
ae0d2f24 24100
98714339 24101int
09ba997f 24102dwarf2_per_cu_data::addr_size () const
ae0d2f24 24103{
96408a79
SA
24104 struct comp_unit_head cu_header_local;
24105 const struct comp_unit_head *cu_headerp;
c471e790 24106
09ba997f 24107 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24108
24109 return cu_headerp->addr_size;
ae0d2f24
UW
24110}
24111
09ba997f 24112/* See read.h. */
9eae7c52
TT
24113
24114int
09ba997f 24115dwarf2_per_cu_data::offset_size () const
9eae7c52 24116{
96408a79
SA
24117 struct comp_unit_head cu_header_local;
24118 const struct comp_unit_head *cu_headerp;
9c6c53f7 24119
09ba997f 24120 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24121
24122 return cu_headerp->offset_size;
24123}
24124
09ba997f 24125/* See read.h. */
96408a79
SA
24126
24127int
09ba997f 24128dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
24129{
24130 struct comp_unit_head cu_header_local;
24131 const struct comp_unit_head *cu_headerp;
24132
09ba997f 24133 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24134
24135 if (cu_headerp->version == 2)
24136 return cu_headerp->addr_size;
24137 else
24138 return cu_headerp->offset_size;
181cebd4
JK
24139}
24140
09ba997f 24141/* See read.h. */
9aa1f1e3
TT
24142
24143CORE_ADDR
09ba997f 24144dwarf2_per_cu_data::text_offset () const
9aa1f1e3 24145{
09ba997f
TT
24146 struct objfile *objfile = dwarf2_per_objfile->objfile;
24147
24148 return objfile->text_section_offset ();
9aa1f1e3
TT
24149}
24150
09ba997f
TT
24151/* See read.h. */
24152
24153struct type *
24154dwarf2_per_cu_data::addr_type () const
9a49df9d 24155{
09ba997f 24156 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
24157 struct type *void_type = objfile_type (objfile)->builtin_void;
24158 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 24159 int addr_size = this->addr_size ();
9a49df9d
AB
24160
24161 if (TYPE_LENGTH (addr_type) == addr_size)
24162 return addr_type;
24163
09ba997f 24164 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
24165 return addr_type;
24166}
24167
22b6cd70
TT
24168/* A helper function for dwarf2_find_containing_comp_unit that returns
24169 the index of the result, and that searches a vector. It will
24170 return a result even if the offset in question does not actually
24171 occur in any CU. This is separate so that it can be unit
24172 tested. */
ae038cb0 24173
22b6cd70
TT
24174static int
24175dwarf2_find_containing_comp_unit
24176 (sect_offset sect_off,
24177 unsigned int offset_in_dwz,
24178 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24179{
ae038cb0
DJ
24180 int low, high;
24181
ae038cb0 24182 low = 0;
22b6cd70 24183 high = all_comp_units.size () - 1;
ae038cb0
DJ
24184 while (high > low)
24185 {
36586728 24186 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24187 int mid = low + (high - low) / 2;
9a619af0 24188
22b6cd70 24189 mid_cu = all_comp_units[mid];
36586728 24190 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24191 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24192 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24193 high = mid;
24194 else
24195 low = mid + 1;
24196 }
24197 gdb_assert (low == high);
22b6cd70
TT
24198 return low;
24199}
24200
24201/* Locate the .debug_info compilation unit from CU's objfile which contains
24202 the DIE at OFFSET. Raises an error on failure. */
24203
24204static struct dwarf2_per_cu_data *
24205dwarf2_find_containing_comp_unit (sect_offset sect_off,
24206 unsigned int offset_in_dwz,
24207 struct dwarf2_per_objfile *dwarf2_per_objfile)
24208{
24209 int low
24210 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24211 dwarf2_per_objfile->all_comp_units);
24212 struct dwarf2_per_cu_data *this_cu
24213 = dwarf2_per_objfile->all_comp_units[low];
24214
45b8ae0c 24215 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24216 {
36586728 24217 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24218 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24219 "offset %s [in module %s]"),
24220 sect_offset_str (sect_off),
ed2dc618 24221 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24222
9c541725
PA
24223 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24224 <= sect_off);
ae038cb0
DJ
24225 return dwarf2_per_objfile->all_comp_units[low-1];
24226 }
24227 else
24228 {
b76e467d 24229 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24230 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24231 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24232 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24233 return this_cu;
24234 }
24235}
24236
22b6cd70
TT
24237#if GDB_SELF_TEST
24238
24239namespace selftests {
24240namespace find_containing_comp_unit {
24241
24242static void
24243run_test ()
24244{
24245 struct dwarf2_per_cu_data one {};
24246 struct dwarf2_per_cu_data two {};
24247 struct dwarf2_per_cu_data three {};
24248 struct dwarf2_per_cu_data four {};
24249
24250 one.length = 5;
24251 two.sect_off = sect_offset (one.length);
24252 two.length = 7;
24253
24254 three.length = 5;
24255 three.is_dwz = 1;
24256 four.sect_off = sect_offset (three.length);
24257 four.length = 7;
24258 four.is_dwz = 1;
24259
24260 std::vector<dwarf2_per_cu_data *> units;
24261 units.push_back (&one);
24262 units.push_back (&two);
24263 units.push_back (&three);
24264 units.push_back (&four);
24265
24266 int result;
24267
24268 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24269 SELF_CHECK (units[result] == &one);
24270 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24271 SELF_CHECK (units[result] == &one);
24272 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24273 SELF_CHECK (units[result] == &two);
24274
24275 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24276 SELF_CHECK (units[result] == &three);
24277 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24278 SELF_CHECK (units[result] == &three);
24279 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24280 SELF_CHECK (units[result] == &four);
24281}
24282
24283}
24284}
24285
24286#endif /* GDB_SELF_TEST */
24287
23745b47 24288/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24289
fcd3b13d
SM
24290dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24291 : per_cu (per_cu_),
9068261f
AB
24292 mark (false),
24293 has_loclist (false),
24294 checked_producer (false),
24295 producer_is_gxx_lt_4_6 (false),
24296 producer_is_gcc_lt_4_3 (false),
eb77c9df 24297 producer_is_icc (false),
9068261f 24298 producer_is_icc_lt_14 (false),
c258c396 24299 producer_is_codewarrior (false),
9068261f 24300 processing_has_namespace_info (false)
93311388 24301{
fcd3b13d
SM
24302 per_cu->cu = this;
24303}
24304
24305/* Destroy a dwarf2_cu. */
24306
24307dwarf2_cu::~dwarf2_cu ()
24308{
24309 per_cu->cu = NULL;
9816fde3
JK
24310}
24311
24312/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24313
24314static void
95554aad
TT
24315prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24316 enum language pretend_language)
9816fde3
JK
24317{
24318 struct attribute *attr;
24319
24320 /* Set the language we're debugging. */
24321 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24322 if (attr != nullptr)
9816fde3
JK
24323 set_cu_language (DW_UNSND (attr), cu);
24324 else
9cded63f 24325 {
95554aad 24326 cu->language = pretend_language;
9cded63f
TT
24327 cu->language_defn = language_def (cu->language);
24328 }
dee91e82 24329
7d45c7c3 24330 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24331}
24332
ae038cb0
DJ
24333/* Increase the age counter on each cached compilation unit, and free
24334 any that are too old. */
24335
24336static void
ed2dc618 24337age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24338{
24339 struct dwarf2_per_cu_data *per_cu, **last_chain;
24340
24341 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24342 per_cu = dwarf2_per_objfile->read_in_chain;
24343 while (per_cu != NULL)
24344 {
24345 per_cu->cu->last_used ++;
b4f54984 24346 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24347 dwarf2_mark (per_cu->cu);
24348 per_cu = per_cu->cu->read_in_chain;
24349 }
24350
24351 per_cu = dwarf2_per_objfile->read_in_chain;
24352 last_chain = &dwarf2_per_objfile->read_in_chain;
24353 while (per_cu != NULL)
24354 {
24355 struct dwarf2_per_cu_data *next_cu;
24356
24357 next_cu = per_cu->cu->read_in_chain;
24358
24359 if (!per_cu->cu->mark)
24360 {
fcd3b13d 24361 delete per_cu->cu;
ae038cb0
DJ
24362 *last_chain = next_cu;
24363 }
24364 else
24365 last_chain = &per_cu->cu->read_in_chain;
24366
24367 per_cu = next_cu;
24368 }
24369}
24370
24371/* Remove a single compilation unit from the cache. */
24372
24373static void
dee91e82 24374free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24375{
24376 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24377 struct dwarf2_per_objfile *dwarf2_per_objfile
24378 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24379
24380 per_cu = dwarf2_per_objfile->read_in_chain;
24381 last_chain = &dwarf2_per_objfile->read_in_chain;
24382 while (per_cu != NULL)
24383 {
24384 struct dwarf2_per_cu_data *next_cu;
24385
24386 next_cu = per_cu->cu->read_in_chain;
24387
dee91e82 24388 if (per_cu == target_per_cu)
ae038cb0 24389 {
fcd3b13d 24390 delete per_cu->cu;
dee91e82 24391 per_cu->cu = NULL;
ae038cb0
DJ
24392 *last_chain = next_cu;
24393 break;
24394 }
24395 else
24396 last_chain = &per_cu->cu->read_in_chain;
24397
24398 per_cu = next_cu;
24399 }
24400}
24401
dee91e82
DE
24402/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24403 We store these in a hash table separate from the DIEs, and preserve them
24404 when the DIEs are flushed out of cache.
24405
24406 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24407 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24408 or the type may come from a DWO file. Furthermore, while it's more logical
24409 to use per_cu->section+offset, with Fission the section with the data is in
24410 the DWO file but we don't know that section at the point we need it.
24411 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24412 because we can enter the lookup routine, get_die_type_at_offset, from
24413 outside this file, and thus won't necessarily have PER_CU->cu.
24414 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24415
dee91e82 24416struct dwarf2_per_cu_offset_and_type
1c379e20 24417{
dee91e82 24418 const struct dwarf2_per_cu_data *per_cu;
9c541725 24419 sect_offset sect_off;
1c379e20
DJ
24420 struct type *type;
24421};
24422
dee91e82 24423/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24424
24425static hashval_t
dee91e82 24426per_cu_offset_and_type_hash (const void *item)
1c379e20 24427{
9a3c8263
SM
24428 const struct dwarf2_per_cu_offset_and_type *ofs
24429 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24430
9c541725 24431 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24432}
24433
dee91e82 24434/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24435
24436static int
dee91e82 24437per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24438{
9a3c8263
SM
24439 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24440 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24441 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24442 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24443
dee91e82 24444 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24445 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24446}
24447
24448/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24449 table if necessary. For convenience, return TYPE.
24450
24451 The DIEs reading must have careful ordering to:
85102364 24452 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24453 reading current DIE.
24454 * Not trying to dereference contents of still incompletely read in types
24455 while reading in other DIEs.
24456 * Enable referencing still incompletely read in types just by a pointer to
24457 the type without accessing its fields.
24458
24459 Therefore caller should follow these rules:
24460 * Try to fetch any prerequisite types we may need to build this DIE type
24461 before building the type and calling set_die_type.
e71ec853 24462 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24463 possible before fetching more types to complete the current type.
24464 * Make the type as complete as possible before fetching more types. */
1c379e20 24465
f792889a 24466static struct type *
1c379e20
DJ
24467set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24468{
518817b3
SM
24469 struct dwarf2_per_objfile *dwarf2_per_objfile
24470 = cu->per_cu->dwarf2_per_objfile;
dee91e82 24471 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 24472 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
24473 struct attribute *attr;
24474 struct dynamic_prop prop;
1c379e20 24475
b4ba55a1
JB
24476 /* For Ada types, make sure that the gnat-specific data is always
24477 initialized (if not already set). There are a few types where
24478 we should not be doing so, because the type-specific area is
24479 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24480 where the type-specific area is used to store the floatformat).
24481 But this is not a problem, because the gnat-specific information
24482 is actually not needed for these types. */
24483 if (need_gnat_info (cu)
24484 && TYPE_CODE (type) != TYPE_CODE_FUNC
24485 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
24486 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24487 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24488 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
24489 && !HAVE_GNAT_AUX_INFO (type))
24490 INIT_GNAT_SPECIFIC (type);
24491
3f2f83dd
KB
24492 /* Read DW_AT_allocated and set in type. */
24493 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 24494 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24495 {
09ba997f 24496 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24497 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24498 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
24499 }
24500 else if (attr != NULL)
24501 {
b98664d3 24502 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 24503 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24504 sect_offset_str (die->sect_off));
3f2f83dd
KB
24505 }
24506
24507 /* Read DW_AT_associated and set in type. */
24508 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 24509 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24510 {
09ba997f 24511 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24512 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24513 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
24514 }
24515 else if (attr != NULL)
24516 {
b98664d3 24517 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 24518 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24519 sect_offset_str (die->sect_off));
3f2f83dd
KB
24520 }
24521
3cdcd0ce
JB
24522 /* Read DW_AT_data_location and set in type. */
24523 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 24524 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 24525 cu->per_cu->addr_type ()))
50a82047 24526 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 24527
dee91e82 24528 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
24529 dwarf2_per_objfile->die_type_hash
24530 = htab_up (htab_create_alloc (127,
24531 per_cu_offset_and_type_hash,
24532 per_cu_offset_and_type_eq,
24533 NULL, xcalloc, xfree));
1c379e20 24534
dee91e82 24535 ofs.per_cu = cu->per_cu;
9c541725 24536 ofs.sect_off = die->sect_off;
1c379e20 24537 ofs.type = type;
dee91e82 24538 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 24539 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24540 if (*slot)
b98664d3 24541 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24542 sect_offset_str (die->sect_off));
8d749320
SM
24543 *slot = XOBNEW (&objfile->objfile_obstack,
24544 struct dwarf2_per_cu_offset_and_type);
1c379e20 24545 **slot = ofs;
f792889a 24546 return type;
1c379e20
DJ
24547}
24548
9c541725 24549/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24550 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24551
24552static struct type *
9c541725 24553get_die_type_at_offset (sect_offset sect_off,
673bfd45 24554 struct dwarf2_per_cu_data *per_cu)
1c379e20 24555{
dee91e82 24556 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 24557 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 24558
dee91e82 24559 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24560 return NULL;
1c379e20 24561
dee91e82 24562 ofs.per_cu = per_cu;
9c541725 24563 ofs.sect_off = sect_off;
9a3c8263 24564 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 24565 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24566 if (slot)
24567 return slot->type;
24568 else
24569 return NULL;
24570}
24571
02142a6c 24572/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24573 or return NULL if DIE does not have a saved type. */
24574
24575static struct type *
24576get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24577{
9c541725 24578 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24579}
24580
10b3939b
DJ
24581/* Add a dependence relationship from CU to REF_PER_CU. */
24582
24583static void
24584dwarf2_add_dependence (struct dwarf2_cu *cu,
24585 struct dwarf2_per_cu_data *ref_per_cu)
24586{
24587 void **slot;
24588
24589 if (cu->dependencies == NULL)
24590 cu->dependencies
24591 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24592 NULL, &cu->comp_unit_obstack,
24593 hashtab_obstack_allocate,
24594 dummy_obstack_deallocate);
24595
24596 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24597 if (*slot == NULL)
24598 *slot = ref_per_cu;
24599}
1c379e20 24600
f504f079
DE
24601/* Subroutine of dwarf2_mark to pass to htab_traverse.
24602 Set the mark field in every compilation unit in the
ae038cb0
DJ
24603 cache that we must keep because we are keeping CU. */
24604
10b3939b
DJ
24605static int
24606dwarf2_mark_helper (void **slot, void *data)
24607{
24608 struct dwarf2_per_cu_data *per_cu;
24609
24610 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24611
24612 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24613 reading of the chain. As such dependencies remain valid it is not much
24614 useful to track and undo them during QUIT cleanups. */
24615 if (per_cu->cu == NULL)
24616 return 1;
24617
10b3939b
DJ
24618 if (per_cu->cu->mark)
24619 return 1;
9068261f 24620 per_cu->cu->mark = true;
10b3939b
DJ
24621
24622 if (per_cu->cu->dependencies != NULL)
24623 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24624
24625 return 1;
24626}
24627
f504f079
DE
24628/* Set the mark field in CU and in every other compilation unit in the
24629 cache that we must keep because we are keeping CU. */
24630
ae038cb0
DJ
24631static void
24632dwarf2_mark (struct dwarf2_cu *cu)
24633{
24634 if (cu->mark)
24635 return;
9068261f 24636 cu->mark = true;
10b3939b
DJ
24637 if (cu->dependencies != NULL)
24638 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24639}
24640
24641static void
24642dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24643{
24644 while (per_cu)
24645 {
9068261f 24646 per_cu->cu->mark = false;
ae038cb0
DJ
24647 per_cu = per_cu->cu->read_in_chain;
24648 }
72bf9492
DJ
24649}
24650
72bf9492
DJ
24651/* Trivial hash function for partial_die_info: the hash value of a DIE
24652 is its offset in .debug_info for this objfile. */
24653
24654static hashval_t
24655partial_die_hash (const void *item)
24656{
9a3c8263
SM
24657 const struct partial_die_info *part_die
24658 = (const struct partial_die_info *) item;
9a619af0 24659
9c541725 24660 return to_underlying (part_die->sect_off);
72bf9492
DJ
24661}
24662
24663/* Trivial comparison function for partial_die_info structures: two DIEs
24664 are equal if they have the same offset. */
24665
24666static int
24667partial_die_eq (const void *item_lhs, const void *item_rhs)
24668{
9a3c8263
SM
24669 const struct partial_die_info *part_die_lhs
24670 = (const struct partial_die_info *) item_lhs;
24671 const struct partial_die_info *part_die_rhs
24672 = (const struct partial_die_info *) item_rhs;
9a619af0 24673
9c541725 24674 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24675}
24676
3c3bb058
AB
24677struct cmd_list_element *set_dwarf_cmdlist;
24678struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24679
24680static void
981a3fb3 24681set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24682{
b4f54984 24683 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24684 gdb_stdout);
ae038cb0
DJ
24685}
24686
24687static void
981a3fb3 24688show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24689{
b4f54984 24690 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24691}
24692
9291a0cd 24693static void
cd4fb1b2
SM
24694show_check_physname (struct ui_file *file, int from_tty,
24695 struct cmd_list_element *c, const char *value)
9291a0cd 24696{
cd4fb1b2
SM
24697 fprintf_filtered (file,
24698 _("Whether to check \"physname\" is %s.\n"),
24699 value);
9291a0cd
TT
24700}
24701
6c265988 24702void _initialize_dwarf2_read ();
cd4fb1b2 24703void
6c265988 24704_initialize_dwarf2_read ()
9291a0cd 24705{
cd4fb1b2
SM
24706 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24707Set DWARF specific variables.\n\
590042fc 24708Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
24709 &set_dwarf_cmdlist, "maintenance set dwarf ",
24710 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24711
cd4fb1b2 24712 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
24713Show DWARF specific variables.\n\
24714Show DWARF variables such as the cache size."),
cd4fb1b2
SM
24715 &show_dwarf_cmdlist, "maintenance show dwarf ",
24716 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24717
cd4fb1b2
SM
24718 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24719 &dwarf_max_cache_age, _("\
24720Set the upper bound on the age of cached DWARF compilation units."), _("\
24721Show the upper bound on the age of cached DWARF compilation units."), _("\
24722A higher limit means that cached compilation units will be stored\n\
24723in memory longer, and more total memory will be used. Zero disables\n\
24724caching, which can slow down startup."),
24725 NULL,
24726 show_dwarf_max_cache_age,
24727 &set_dwarf_cmdlist,
24728 &show_dwarf_cmdlist);
156942c7 24729
cd4fb1b2
SM
24730 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24731Set debugging of the DWARF reader."), _("\
24732Show debugging of the DWARF reader."), _("\
24733When enabled (non-zero), debugging messages are printed during DWARF\n\
24734reading and symtab expansion. A value of 1 (one) provides basic\n\
24735information. A value greater than 1 provides more verbose information."),
24736 NULL,
24737 NULL,
24738 &setdebuglist, &showdebuglist);
9291a0cd 24739
cd4fb1b2
SM
24740 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24741Set debugging of the DWARF DIE reader."), _("\
24742Show debugging of the DWARF DIE reader."), _("\
24743When enabled (non-zero), DIEs are dumped after they are read in.\n\
24744The value is the maximum depth to print."),
24745 NULL,
24746 NULL,
24747 &setdebuglist, &showdebuglist);
9291a0cd 24748
cd4fb1b2
SM
24749 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24750Set debugging of the dwarf line reader."), _("\
24751Show debugging of the dwarf line reader."), _("\
24752When enabled (non-zero), line number entries are dumped as they are read in.\n\
24753A value of 1 (one) provides basic information.\n\
24754A value greater than 1 provides more verbose information."),
24755 NULL,
24756 NULL,
24757 &setdebuglist, &showdebuglist);
437afbb8 24758
cd4fb1b2
SM
24759 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24760Set cross-checking of \"physname\" code against demangler."), _("\
24761Show cross-checking of \"physname\" code against demangler."), _("\
24762When enabled, GDB's internal \"physname\" code is checked against\n\
24763the demangler."),
24764 NULL, show_check_physname,
24765 &setdebuglist, &showdebuglist);
900e11f9 24766
e615022a
DE
24767 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24768 no_class, &use_deprecated_index_sections, _("\
24769Set whether to use deprecated gdb_index sections."), _("\
24770Show whether to use deprecated gdb_index sections."), _("\
24771When enabled, deprecated .gdb_index sections are used anyway.\n\
24772Normally they are ignored either because of a missing feature or\n\
24773performance issue.\n\
24774Warning: This option must be enabled before gdb reads the file."),
24775 NULL,
24776 NULL,
24777 &setlist, &showlist);
24778
f1e6e072
TT
24779 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24780 &dwarf2_locexpr_funcs);
24781 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24782 &dwarf2_loclist_funcs);
24783
24784 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24785 &dwarf2_block_frame_base_locexpr_funcs);
24786 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24787 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24788
24789#if GDB_SELF_TEST
24790 selftests::register_test ("dw2_expand_symtabs_matching",
24791 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24792 selftests::register_test ("dwarf2_find_containing_comp_unit",
24793 selftests::find_containing_comp_unit::run_test);
c62446b1 24794#endif
6502dd73 24795}
This page took 7.619824 seconds and 4 git commands to generate.