Use bfd_get_filename instead of objfile_name in lookup_dwo_unit
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
9e021579
SM
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
c24bdb02
KS
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
d00adf39 424 /* The header of the compilation unit. */
fcd3b13d 425 struct comp_unit_head header {};
e142c38c 426
d00adf39 427 /* Base address of this compilation unit. */
2b24b6e4 428 gdb::optional<CORE_ADDR> base_address;
d00adf39 429
e142c38c 430 /* The language we are debugging. */
fcd3b13d
SM
431 enum language language = language_unknown;
432 const struct language_defn *language_defn = nullptr;
e142c38c 433
fcd3b13d 434 const char *producer = nullptr;
b0f35d58 435
c24bdb02 436private:
804d2729
TT
437 /* The symtab builder for this CU. This is only non-NULL when full
438 symbols are being read. */
c24bdb02 439 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 440
c24bdb02 441public:
e142c38c
DJ
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
fcd3b13d 451 struct pending **list_in_scope = nullptr;
e142c38c 452
b64f50a1
JK
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 455 htab_t partial_dies = nullptr;
72bf9492
DJ
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
fcd3b13d 459 auto_obstack comp_unit_obstack;
72bf9492 460
ae038cb0
DJ
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
fcd3b13d 465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 466
69d751e3 467 /* Backlink to our per_cu entry. */
ae038cb0
DJ
468 struct dwarf2_per_cu_data *per_cu;
469
9e021579
SM
470 /* The dwarf2_per_objfile that owns this. */
471 struct dwarf2_per_objfile *per_objfile;
472
ae038cb0 473 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 474 int last_used = 0;
ae038cb0 475
b64f50a1
JK
476 /* A hash table of DIE cu_offset for following references with
477 die_info->offset.sect_off as hash. */
fcd3b13d 478 htab_t die_hash = nullptr;
10b3939b
DJ
479
480 /* Full DIEs if read in. */
fcd3b13d 481 struct die_info *dies = nullptr;
10b3939b
DJ
482
483 /* A set of pointers to dwarf2_per_cu_data objects for compilation
484 units referenced by this one. Only set during full symbol processing;
485 partial symbol tables do not have dependencies. */
fcd3b13d 486 htab_t dependencies = nullptr;
10b3939b 487
cb1df416 488 /* Header data from the line table, during full symbol processing. */
fcd3b13d 489 struct line_header *line_header = nullptr;
4c8aa72d 490 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 491 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
492 this is the DW_TAG_compile_unit die for this CU. We'll hold on
493 to the line header as long as this DIE is being processed. See
494 process_die_scope. */
fcd3b13d 495 die_info *line_header_die_owner = nullptr;
cb1df416 496
3da10d80
KS
497 /* A list of methods which need to have physnames computed
498 after all type information has been read. */
c89b44cd 499 std::vector<delayed_method_info> method_list;
3da10d80 500
96408a79 501 /* To be copied to symtab->call_site_htab. */
fcd3b13d 502 htab_t call_site_htab = nullptr;
96408a79 503
034e5797
DE
504 /* Non-NULL if this CU came from a DWO file.
505 There is an invariant here that is important to remember:
506 Except for attributes copied from the top level DIE in the "main"
507 (or "stub") file in preparation for reading the DWO file
18a8505e 508 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
509 Either there isn't a DWO file (in which case this is NULL and the point
510 is moot), or there is and either we're not going to read it (in which
511 case this is NULL) or there is and we are reading it (in which case this
512 is non-NULL). */
fcd3b13d 513 struct dwo_unit *dwo_unit = nullptr;
3019eac3 514
18a8505e 515 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 517 gdb::optional<ULONGEST> addr_base;
3019eac3 518
18a8505e 519 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 520 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 521 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 526 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 528 ULONGEST ranges_base = 0;
2e3cf129 529
41144253 530 /* The DW_AT_loclists_base attribute if present. */
531 ULONGEST loclist_base = 0;
532
c9317f21
TT
533 /* When reading debug info generated by older versions of rustc, we
534 have to rewrite some union types to be struct types with a
535 variant part. This rewriting must be done after the CU is fully
536 read in, because otherwise at the point of rewriting some struct
537 type might not have been fully processed. So, we keep a list of
538 all such types here and process them after expansion. */
539 std::vector<struct type *> rust_unions;
540
18a8505e
AT
541 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
542 files, the value is implicitly zero. For DWARF 5 version DWO files, the
543 value is often implicit and is the size of the header of
544 .debug_str_offsets section (8 or 4, depending on the address size). */
545 gdb::optional<ULONGEST> str_offsets_base;
546
ae038cb0 547 /* Mark used when releasing cached dies. */
9068261f 548 bool mark : 1;
ae038cb0 549
8be455d7
JK
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 554 bool has_loclist : 1;
ba919b58 555
9068261f 556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
9068261f
AB
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 563 bool producer_is_icc : 1;
9068261f 564 bool producer_is_icc_lt_14 : 1;
c258c396 565 bool producer_is_codewarrior : 1;
4d4ec4e5 566
9068261f 567 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
570
9068261f 571 bool processing_has_namespace_info : 1;
d590ff25
YQ
572
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
574
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
577 dwarf2_cu *ancestor;
578
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
581 {
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
585
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
589
590 return nullptr;
591 }
e7c27a73
DJ
592};
593
094b34ac
DE
594/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
596
597struct stmt_list_hash
598{
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
601
602 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 603 sect_offset line_sect_off;
094b34ac
DE
604};
605
5989a64e 606/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
f4dc4d17
DE
607 an object of this type. */
608
609struct type_unit_group
610{
0186c6a7 611 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
197400e8 614 a "per_cu" handle on the symtab. */
094b34ac
DE
615 struct dwarf2_per_cu_data per_cu;
616
0186c6a7
DE
617 /* The TUs that share this DW_AT_stmt_list entry.
618 This is added to while parsing type units to build partial symtabs,
619 and is deleted afterwards and not used again. */
a8b3b8e9 620 std::vector<signatured_type *> *tus;
f4dc4d17 621
43f3e411 622 /* The compunit symtab.
094b34ac 623 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
624 so we create an essentially anonymous symtab as the compunit symtab. */
625 struct compunit_symtab *compunit_symtab;
f4dc4d17 626
094b34ac
DE
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
f4dc4d17 629
f4dc4d17
DE
630 /* The symbol tables for this TU (obtained from the files listed in
631 DW_AT_stmt_list).
632 WARNING: The order of entries here must match the order of entries
633 in the line header. After the first TU using this type_unit_group, the
634 line header for the subsequent TUs is recreated from this. This is done
635 because we need to use the same symtabs for each TU using the same
636 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
637 there's no guarantee the line header doesn't have duplicate entries. */
638 struct symtab **symtabs;
639};
640
73869dc2 641/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
642
643struct dwo_sections
644{
645 struct dwarf2_section_info abbrev;
3019eac3
DE
646 struct dwarf2_section_info line;
647 struct dwarf2_section_info loc;
43988095 648 struct dwarf2_section_info loclists;
09262596
DE
649 struct dwarf2_section_info macinfo;
650 struct dwarf2_section_info macro;
3019eac3
DE
651 struct dwarf2_section_info str;
652 struct dwarf2_section_info str_offsets;
80626a55
DE
653 /* In the case of a virtual DWO file, these two are unused. */
654 struct dwarf2_section_info info;
fd5866f6 655 std::vector<dwarf2_section_info> types;
3019eac3
DE
656};
657
c88ee1f0 658/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
659
660struct dwo_unit
661{
662 /* Backlink to the containing struct dwo_file. */
663 struct dwo_file *dwo_file;
664
665 /* The "id" that distinguishes this CU/TU.
666 .debug_info calls this "dwo_id", .debug_types calls this "signature".
667 Since signatures came first, we stick with it for consistency. */
668 ULONGEST signature;
669
670 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 671 struct dwarf2_section_info *section;
3019eac3 672
9c541725
PA
673 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
674 sect_offset sect_off;
3019eac3
DE
675 unsigned int length;
676
677 /* For types, offset in the type's DIE of the type defined by this TU. */
678 cu_offset type_offset_in_tu;
679};
680
73869dc2
DE
681/* include/dwarf2.h defines the DWP section codes.
682 It defines a max value but it doesn't define a min value, which we
683 use for error checking, so provide one. */
684
685enum dwp_v2_section_ids
686{
687 DW_SECT_MIN = 1
688};
689
80626a55 690/* Data for one DWO file.
57d63ce2
DE
691
692 This includes virtual DWO files (a virtual DWO file is a DWO file as it
693 appears in a DWP file). DWP files don't really have DWO files per se -
694 comdat folding of types "loses" the DWO file they came from, and from
695 a high level view DWP files appear to contain a mass of random types.
696 However, to maintain consistency with the non-DWP case we pretend DWP
697 files contain virtual DWO files, and we assign each TU with one virtual
698 DWO file (generally based on the line and abbrev section offsets -
699 a heuristic that seems to work in practice). */
3019eac3
DE
700
701struct dwo_file
702{
51ac9db5
SM
703 dwo_file () = default;
704 DISABLE_COPY_AND_ASSIGN (dwo_file);
705
18a8505e 706 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
707 For virtual DWO files the name is constructed from the section offsets
708 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
709 from related CU+TUs. */
51ac9db5 710 const char *dwo_name = nullptr;
0ac5b59e
DE
711
712 /* The DW_AT_comp_dir attribute. */
51ac9db5 713 const char *comp_dir = nullptr;
3019eac3 714
80626a55
DE
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 717 gdb_bfd_ref_ptr dbfd;
3019eac3 718
73869dc2
DE
719 /* The sections that make up this DWO file.
720 Remember that for virtual DWO files in DWP V2, these are virtual
721 sections (for lack of a better name). */
51ac9db5 722 struct dwo_sections sections {};
3019eac3 723
33c5cd75
DB
724 /* The CUs in the file.
725 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
726 an extension to handle LLVM's Link Time Optimization output (where
727 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 728 htab_up cus;
3019eac3
DE
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
b0b6a987 732 htab_up tus;
3019eac3
DE
733};
734
80626a55
DE
735/* These sections are what may appear in a DWP file. */
736
737struct dwp_sections
738{
73869dc2 739 /* These are used by both DWP version 1 and 2. */
80626a55
DE
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
73869dc2
DE
743
744 /* These are only used by DWP version 2 files.
745 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
746 sections are referenced by section number, and are not recorded here.
747 In DWP version 2 there is at most one copy of all these sections, each
748 section being (effectively) comprised of the concatenation of all of the
749 individual sections that exist in the version 1 format.
750 To keep the code simple we treat each of these concatenated pieces as a
751 section itself (a virtual section?). */
752 struct dwarf2_section_info abbrev;
753 struct dwarf2_section_info info;
754 struct dwarf2_section_info line;
755 struct dwarf2_section_info loc;
756 struct dwarf2_section_info macinfo;
757 struct dwarf2_section_info macro;
758 struct dwarf2_section_info str_offsets;
759 struct dwarf2_section_info types;
80626a55
DE
760};
761
73869dc2
DE
762/* These sections are what may appear in a virtual DWO file in DWP version 1.
763 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 764
73869dc2 765struct virtual_v1_dwo_sections
80626a55
DE
766{
767 struct dwarf2_section_info abbrev;
768 struct dwarf2_section_info line;
769 struct dwarf2_section_info loc;
770 struct dwarf2_section_info macinfo;
771 struct dwarf2_section_info macro;
772 struct dwarf2_section_info str_offsets;
773 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 774 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
775 struct dwarf2_section_info info_or_types;
776};
777
73869dc2
DE
778/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
779 In version 2, the sections of the DWO files are concatenated together
780 and stored in one section of that name. Thus each ELF section contains
781 several "virtual" sections. */
782
783struct virtual_v2_dwo_sections
784{
785 bfd_size_type abbrev_offset;
786 bfd_size_type abbrev_size;
787
788 bfd_size_type line_offset;
789 bfd_size_type line_size;
790
791 bfd_size_type loc_offset;
792 bfd_size_type loc_size;
793
794 bfd_size_type macinfo_offset;
795 bfd_size_type macinfo_size;
796
797 bfd_size_type macro_offset;
798 bfd_size_type macro_size;
799
800 bfd_size_type str_offsets_offset;
801 bfd_size_type str_offsets_size;
802
803 /* Each DWP hash table entry records one CU or one TU.
804 That is recorded here, and copied to dwo_unit.section. */
805 bfd_size_type info_or_types_offset;
806 bfd_size_type info_or_types_size;
807};
808
80626a55
DE
809/* Contents of DWP hash tables. */
810
811struct dwp_hash_table
812{
73869dc2 813 uint32_t version, nr_columns;
80626a55 814 uint32_t nr_units, nr_slots;
73869dc2
DE
815 const gdb_byte *hash_table, *unit_table;
816 union
817 {
818 struct
819 {
820 const gdb_byte *indices;
821 } v1;
822 struct
823 {
824 /* This is indexed by column number and gives the id of the section
825 in that column. */
826#define MAX_NR_V2_DWO_SECTIONS \
827 (1 /* .debug_info or .debug_types */ \
828 + 1 /* .debug_abbrev */ \
829 + 1 /* .debug_line */ \
830 + 1 /* .debug_loc */ \
831 + 1 /* .debug_str_offsets */ \
832 + 1 /* .debug_macro or .debug_macinfo */)
833 int section_ids[MAX_NR_V2_DWO_SECTIONS];
834 const gdb_byte *offsets;
835 const gdb_byte *sizes;
836 } v2;
837 } section_pool;
80626a55
DE
838};
839
840/* Data for one DWP file. */
841
842struct dwp_file
843{
400174b1
TT
844 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
845 : name (name_),
846 dbfd (std::move (abfd))
847 {
848 }
849
80626a55
DE
850 /* Name of the file. */
851 const char *name;
852
73869dc2 853 /* File format version. */
400174b1 854 int version = 0;
73869dc2 855
93417882 856 /* The bfd. */
400174b1 857 gdb_bfd_ref_ptr dbfd;
80626a55
DE
858
859 /* Section info for this file. */
400174b1 860 struct dwp_sections sections {};
80626a55 861
57d63ce2 862 /* Table of CUs in the file. */
400174b1 863 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
864
865 /* Table of TUs in the file. */
400174b1 866 const struct dwp_hash_table *tus = nullptr;
80626a55 867
19ac8c2e 868 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
869 htab_up loaded_cus;
870 htab_up loaded_tus;
80626a55 871
73869dc2
DE
872 /* Table to map ELF section numbers to their sections.
873 This is only needed for the DWP V1 file format. */
400174b1
TT
874 unsigned int num_sections = 0;
875 asection **elf_sections = nullptr;
80626a55
DE
876};
877
0963b4bd
MS
878/* Struct used to pass misc. parameters to read_die_and_children, et
879 al. which are used for both .debug_info and .debug_types dies.
880 All parameters here are unchanging for the life of the call. This
dee91e82 881 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
882
883struct die_reader_specs
884{
a32a8923 885 /* The bfd of die_section. */
93311388
DE
886 bfd* abfd;
887
888 /* The CU of the DIE we are parsing. */
889 struct dwarf2_cu *cu;
890
80626a55 891 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
892 struct dwo_file *dwo_file;
893
dee91e82 894 /* The section the die comes from.
3019eac3 895 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
896 struct dwarf2_section_info *die_section;
897
898 /* die_section->buffer. */
d521ce57 899 const gdb_byte *buffer;
f664829e
DE
900
901 /* The end of the buffer. */
902 const gdb_byte *buffer_end;
a2ce51a0 903
685af9cd
TT
904 /* The abbreviation table to use when reading the DIEs. */
905 struct abbrev_table *abbrev_table;
93311388
DE
906};
907
c0ab21c2
TT
908/* A subclass of die_reader_specs that holds storage and has complex
909 constructor and destructor behavior. */
910
911class cutu_reader : public die_reader_specs
912{
913public:
914
915 cutu_reader (struct dwarf2_per_cu_data *this_cu,
916 struct abbrev_table *abbrev_table,
6751ebae 917 int use_existing_cu,
c0ab21c2
TT
918 bool skip_partial);
919
920 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
921 struct dwarf2_cu *parent_cu = nullptr,
922 struct dwo_file *dwo_file = nullptr);
923
c0ab21c2
TT
924 DISABLE_COPY_AND_ASSIGN (cutu_reader);
925
926 const gdb_byte *info_ptr = nullptr;
927 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
928 bool dummy_p = false;
929
6751ebae
TT
930 /* Release the new CU, putting it on the chain. This cannot be done
931 for dummy CUs. */
932 void keep ();
933
c0ab21c2 934private:
9e021579
SM
935 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
936 dwarf2_per_objfile *per_objfile,
6751ebae 937 int use_existing_cu);
c0ab21c2
TT
938
939 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
940 std::unique_ptr<dwarf2_cu> m_new_cu;
941
942 /* The ordinary abbreviation table. */
943 abbrev_table_up m_abbrev_table_holder;
944
945 /* The DWO abbreviation table. */
946 abbrev_table_up m_dwo_abbrev_table;
947};
dee91e82 948
c906108c 949/* When we construct a partial symbol table entry we only
0963b4bd 950 need this much information. */
6f06d47b 951struct partial_die_info : public allocate_on_obstack
c906108c 952 {
6f06d47b
YQ
953 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
954
955 /* Disable assign but still keep copy ctor, which is needed
956 load_partial_dies. */
957 partial_die_info& operator=(const partial_die_info& rhs) = delete;
958
52356b79
YQ
959 /* Adjust the partial die before generating a symbol for it. This
960 function may set the is_external flag or change the DIE's
961 name. */
962 void fixup (struct dwarf2_cu *cu);
963
48fbe735
YQ
964 /* Read a minimal amount of information into the minimal die
965 structure. */
966 const gdb_byte *read (const struct die_reader_specs *reader,
967 const struct abbrev_info &abbrev,
968 const gdb_byte *info_ptr);
969
72bf9492 970 /* Offset of this DIE. */
6f06d47b 971 const sect_offset sect_off;
72bf9492
DJ
972
973 /* DWARF-2 tag for this DIE. */
6f06d47b 974 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 975
72bf9492 976 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
977 const unsigned int has_children : 1;
978
72bf9492
DJ
979 unsigned int is_external : 1;
980 unsigned int is_declaration : 1;
981 unsigned int has_type : 1;
982 unsigned int has_specification : 1;
983 unsigned int has_pc_info : 1;
481860b3 984 unsigned int may_be_inlined : 1;
72bf9492 985
0c1b455e
TT
986 /* This DIE has been marked DW_AT_main_subprogram. */
987 unsigned int main_subprogram : 1;
988
72bf9492
DJ
989 /* Flag set if the SCOPE field of this structure has been
990 computed. */
991 unsigned int scope_set : 1;
992
fa4028e9
JB
993 /* Flag set if the DIE has a byte_size attribute. */
994 unsigned int has_byte_size : 1;
995
ff908ebf
AW
996 /* Flag set if the DIE has a DW_AT_const_value attribute. */
997 unsigned int has_const_value : 1;
998
98bfdba5
PA
999 /* Flag set if any of the DIE's children are template arguments. */
1000 unsigned int has_template_arguments : 1;
1001
52356b79 1002 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1003 unsigned int fixup_called : 1;
1004
36586728
TT
1005 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1006 unsigned int is_dwz : 1;
1007
1008 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1009 unsigned int spec_is_dwz : 1;
1010
72bf9492 1011 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1012 sometimes a default name for unnamed DIEs. */
6f06d47b 1013 const char *name = nullptr;
72bf9492 1014
abc72ce4 1015 /* The linkage name, if present. */
6f06d47b 1016 const char *linkage_name = nullptr;
abc72ce4 1017
72bf9492
DJ
1018 /* The scope to prepend to our children. This is generally
1019 allocated on the comp_unit_obstack, so will disappear
1020 when this compilation unit leaves the cache. */
6f06d47b 1021 const char *scope = nullptr;
72bf9492 1022
95554aad
TT
1023 /* Some data associated with the partial DIE. The tag determines
1024 which field is live. */
1025 union
1026 {
1027 /* The location description associated with this DIE, if any. */
1028 struct dwarf_block *locdesc;
1029 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1030 sect_offset sect_off;
6f06d47b 1031 } d {};
72bf9492
DJ
1032
1033 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1034 CORE_ADDR lowpc = 0;
1035 CORE_ADDR highpc = 0;
72bf9492 1036
93311388 1037 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1038 DW_AT_sibling, if any. */
48fbe735
YQ
1039 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1040 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1041 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1042
1043 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1044 DW_AT_specification (or DW_AT_abstract_origin or
1045 DW_AT_extension). */
6f06d47b 1046 sect_offset spec_offset {};
72bf9492
DJ
1047
1048 /* Pointers to this DIE's parent, first child, and next sibling,
1049 if any. */
6f06d47b
YQ
1050 struct partial_die_info *die_parent = nullptr;
1051 struct partial_die_info *die_child = nullptr;
1052 struct partial_die_info *die_sibling = nullptr;
1053
1054 friend struct partial_die_info *
1055 dwarf2_cu::find_partial_die (sect_offset sect_off);
1056
1057 private:
1058 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1059 partial_die_info (sect_offset sect_off)
1060 : partial_die_info (sect_off, DW_TAG_padding, 0)
1061 {
1062 }
1063
1064 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1065 int has_children_)
1066 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1067 {
1068 is_external = 0;
1069 is_declaration = 0;
1070 has_type = 0;
1071 has_specification = 0;
1072 has_pc_info = 0;
1073 may_be_inlined = 0;
1074 main_subprogram = 0;
1075 scope_set = 0;
1076 has_byte_size = 0;
1077 has_const_value = 0;
1078 has_template_arguments = 0;
1079 fixup_called = 0;
1080 is_dwz = 0;
1081 spec_is_dwz = 0;
1082 }
c906108c
SS
1083 };
1084
c906108c
SS
1085/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1086 but this would require a corresponding change in unpack_field_as_long
1087 and friends. */
1088static int bits_per_byte = 8;
1089
9c6a1327
TT
1090struct variant_part_builder;
1091
1092/* When reading a variant, we track a bit more information about the
1093 field, and store it in an object of this type. */
2ddeaf8a
TT
1094
1095struct variant_field
1096{
9c6a1327
TT
1097 int first_field = -1;
1098 int last_field = -1;
1099
1100 /* A variant can contain other variant parts. */
1101 std::vector<variant_part_builder> variant_parts;
1102
2ddeaf8a
TT
1103 /* If we see a DW_TAG_variant, then this will be set if this is the
1104 default branch. */
9c6a1327
TT
1105 bool default_branch = false;
1106 /* If we see a DW_AT_discr_value, then this will be the discriminant
1107 value. */
1108 ULONGEST discriminant_value = 0;
1109 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1110 data. */
1111 struct dwarf_block *discr_list_data = nullptr;
1112};
1113
1114/* This represents a DW_TAG_variant_part. */
1115
1116struct variant_part_builder
1117{
1118 /* The offset of the discriminant field. */
1119 sect_offset discriminant_offset {};
1120
1121 /* Variants that are direct children of this variant part. */
1122 std::vector<variant_field> variants;
1123
1124 /* True if we're currently reading a variant. */
1125 bool processing_variant = false;
2ddeaf8a
TT
1126};
1127
52059ffd
TT
1128struct nextfield
1129{
be2daae6
TT
1130 int accessibility = 0;
1131 int virtuality = 0;
9c6a1327
TT
1132 /* Variant parts need to find the discriminant, which is a DIE
1133 reference. We track the section offset of each field to make
1134 this link. */
1135 sect_offset offset;
be2daae6 1136 struct field field {};
52059ffd
TT
1137};
1138
1139struct fnfieldlist
1140{
be2daae6
TT
1141 const char *name = nullptr;
1142 std::vector<struct fn_field> fnfields;
52059ffd
TT
1143};
1144
c906108c
SS
1145/* The routines that read and process dies for a C struct or C++ class
1146 pass lists of data member fields and lists of member function fields
1147 in an instance of a field_info structure, as defined below. */
1148struct field_info
c5aa993b 1149 {
0963b4bd 1150 /* List of data member and baseclasses fields. */
be2daae6
TT
1151 std::vector<struct nextfield> fields;
1152 std::vector<struct nextfield> baseclasses;
c906108c 1153
85102364 1154 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1155 int non_public_fields = 0;
c906108c 1156
c5aa993b
JM
1157 /* Member function fieldlist array, contains name of possibly overloaded
1158 member function, number of overloaded member functions and a pointer
1159 to the head of the member function field chain. */
be2daae6 1160 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1161
1162 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1163 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1164 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1165
1166 /* Nested types defined by this class and the number of elements in this
1167 list. */
be2daae6 1168 std::vector<struct decl_field> nested_types_list;
317f7127 1169
9c6a1327
TT
1170 /* If non-null, this is the variant part we are currently
1171 reading. */
1172 variant_part_builder *current_variant_part = nullptr;
1173 /* This holds all the top-level variant parts attached to the type
1174 we're reading. */
1175 std::vector<variant_part_builder> variant_parts;
1176
317f7127
TT
1177 /* Return the total number of fields (including baseclasses). */
1178 int nfields () const
1179 {
1180 return fields.size () + baseclasses.size ();
1181 }
c5aa993b 1182 };
c906108c 1183
ae038cb0
DJ
1184/* Loaded secondary compilation units are kept in memory until they
1185 have not been referenced for the processing of this many
1186 compilation units. Set this to zero to disable caching. Cache
1187 sizes of up to at least twenty will improve startup time for
1188 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1189static int dwarf_max_cache_age = 5;
920d2a44 1190static void
b4f54984
DE
1191show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1192 struct cmd_list_element *c, const char *value)
920d2a44 1193{
3e43a32a 1194 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1195 "DWARF compilation units is %s.\n"),
920d2a44
AC
1196 value);
1197}
4390d890 1198\f
c906108c
SS
1199/* local function prototypes */
1200
918dd910
JK
1201static void dwarf2_find_base_address (struct die_info *die,
1202 struct dwarf2_cu *cu);
1203
891813be 1204static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1205 (struct dwarf2_per_cu_data *per_cu, const char *name);
1206
f1902523
JK
1207static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1208 const gdb_byte *info_ptr,
3e225074 1209 struct die_info *type_unit_die);
f1902523 1210
ed2dc618
SM
1211static void dwarf2_build_psymtabs_hard
1212 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1213
72bf9492
DJ
1214static void scan_partial_symbols (struct partial_die_info *,
1215 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1216 int, struct dwarf2_cu *);
c906108c 1217
72bf9492
DJ
1218static void add_partial_symbol (struct partial_die_info *,
1219 struct dwarf2_cu *);
63d06c5c 1220
72bf9492
DJ
1221static void add_partial_namespace (struct partial_die_info *pdi,
1222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1223 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1224
5d7cb8df 1225static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1226 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1227 struct dwarf2_cu *cu);
1228
72bf9492
DJ
1229static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1230 struct dwarf2_cu *cu);
91c24f0a 1231
bc30ff58
JB
1232static void add_partial_subprogram (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1234 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1235
d521ce57 1236static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1237
dee91e82 1238static struct partial_die_info *load_partial_dies
d521ce57 1239 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1240
fb816e8b
TV
1241/* A pair of partial_die_info and compilation unit. */
1242struct cu_partial_die_info
1243{
1244 /* The compilation unit of the partial_die_info. */
1245 struct dwarf2_cu *cu;
1246 /* A partial_die_info. */
1247 struct partial_die_info *pdi;
122cf0f2
AB
1248
1249 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1250 : cu (cu),
1251 pdi (pdi)
405feb71 1252 { /* Nothing. */ }
122cf0f2
AB
1253
1254private:
1255 cu_partial_die_info () = delete;
fb816e8b
TV
1256};
1257
122cf0f2
AB
1258static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1259 struct dwarf2_cu *);
72bf9492 1260
d521ce57
TT
1261static const gdb_byte *read_attribute (const struct die_reader_specs *,
1262 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1263 const gdb_byte *, bool *need_reprocess);
1264
1265static void read_attribute_reprocess (const struct die_reader_specs *reader,
1266 struct attribute *attr);
1267
1268static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1269
ed2dc618
SM
1270static sect_offset read_abbrev_offset
1271 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1272 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1273
ed2dc618
SM
1274static const char *read_indirect_string
1275 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1276 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1277
ed2dc618 1278static const char *read_indirect_string_at_offset
4f44ae6c 1279 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1280
d521ce57
TT
1281static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1282 const gdb_byte *,
3019eac3
DE
1283 unsigned int *);
1284
18a8505e
AT
1285static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1286 ULONGEST str_index);
1287
1288static const char *read_stub_str_index (struct dwarf2_cu *cu,
1289 ULONGEST str_index);
3019eac3 1290
e142c38c 1291static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1292
e142c38c
DJ
1293static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1294 struct dwarf2_cu *);
c906108c 1295
7d45c7c3
KB
1296static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1297 struct dwarf2_cu *cu);
1298
a084a2a6
AT
1299static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1300
05cf31d1
JB
1301static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1302 struct dwarf2_cu *cu);
1303
e142c38c 1304static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1305
e142c38c 1306static struct die_info *die_specification (struct die_info *die,
f2f0e013 1307 struct dwarf2_cu **);
63d06c5c 1308
9c541725 1309static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1310 struct dwarf2_cu *cu);
debd256d 1311
f3f5162e 1312static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1313 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1314 CORE_ADDR, int decode_mapping);
c906108c 1315
804d2729
TT
1316static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1317 const char *);
c906108c 1318
a14ed312 1319static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1320 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1321
ff39bb5e 1322static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1323 struct dwarf2_cu *);
c906108c 1324
ff39bb5e 1325static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1326 struct type *type,
1327 const char *name,
1328 struct obstack *obstack,
12df843f 1329 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1330 const gdb_byte **bytes,
98bfdba5 1331 struct dwarf2_locexpr_baton **baton);
2df3850c 1332
e7c27a73 1333static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1334
b4ba55a1
JB
1335static int need_gnat_info (struct dwarf2_cu *);
1336
3e43a32a
MS
1337static struct type *die_descriptive_type (struct die_info *,
1338 struct dwarf2_cu *);
b4ba55a1
JB
1339
1340static void set_descriptive_type (struct type *, struct die_info *,
1341 struct dwarf2_cu *);
1342
e7c27a73
DJ
1343static struct type *die_containing_type (struct die_info *,
1344 struct dwarf2_cu *);
c906108c 1345
ff39bb5e 1346static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1347 struct dwarf2_cu *);
c906108c 1348
f792889a 1349static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1350
673bfd45
DE
1351static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1352
0d5cff50 1353static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1354
6e70227d 1355static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1356 const char *suffix, int physname,
1357 struct dwarf2_cu *cu);
63d06c5c 1358
e7c27a73 1359static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1360
348e048f
DE
1361static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1362
e7c27a73 1363static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1364
e7c27a73 1365static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1366
96408a79
SA
1367static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1368
71a3c369
TT
1369static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1370
ff013f42 1371static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1372 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1373
41144253 1374/* Return the .debug_loclists section to use for cu. */
1375static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1376
3a2b436a 1377/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1378 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1379enum pc_bounds_kind
1380{
e385593e 1381 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1382 PC_BOUNDS_NOT_PRESENT,
1383
e385593e
JK
1384 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1385 were present but they do not form a valid range of PC addresses. */
1386 PC_BOUNDS_INVALID,
1387
3a2b436a
JK
1388 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1389 PC_BOUNDS_RANGES,
1390
1391 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1392 PC_BOUNDS_HIGH_LOW,
1393};
1394
1395static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *,
891813be 1398 dwarf2_psymtab *);
c906108c 1399
fae299cd
DC
1400static void get_scope_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *);
1403
801e3a5b
JB
1404static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1405 CORE_ADDR, struct dwarf2_cu *);
1406
a14ed312 1407static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1408 struct dwarf2_cu *);
c906108c 1409
a14ed312 1410static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1411 struct type *, struct dwarf2_cu *);
c906108c 1412
a14ed312 1413static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1414 struct die_info *, struct type *,
e7c27a73 1415 struct dwarf2_cu *);
c906108c 1416
a14ed312 1417static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1418 struct type *,
1419 struct dwarf2_cu *);
c906108c 1420
134d01f1 1421static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1422
e7c27a73 1423static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1424
e7c27a73 1425static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1426
5d7cb8df
JK
1427static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1428
804d2729 1429static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1430
27aa8d6a
SW
1431static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1432
74921315
KS
1433static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1434
f55ee35c
JK
1435static struct type *read_module_type (struct die_info *die,
1436 struct dwarf2_cu *cu);
1437
38d518c9 1438static const char *namespace_name (struct die_info *die,
e142c38c 1439 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1440
134d01f1 1441static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1442
7d79de9a
TT
1443static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1444 bool * = nullptr);
c906108c 1445
6e70227d 1446static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1447 struct dwarf2_cu *);
1448
bf6af496 1449static struct die_info *read_die_and_siblings_1
d521ce57 1450 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1451 struct die_info *);
639d11d3 1452
dee91e82 1453static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1454 const gdb_byte *info_ptr,
1455 const gdb_byte **new_info_ptr,
639d11d3
DC
1456 struct die_info *parent);
1457
d521ce57
TT
1458static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1459 struct die_info **, const gdb_byte *,
3e225074 1460 int);
3019eac3 1461
d521ce57 1462static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1463 struct die_info **, const gdb_byte *);
93311388 1464
e7c27a73 1465static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1466
15d034d0 1467static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1468 struct objfile *);
71c25dea 1469
15d034d0 1470static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1471
15d034d0 1472static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1473 struct die_info *die,
1474 struct dwarf2_cu *cu);
1475
ca69b9e6
DE
1476static const char *dwarf2_physname (const char *name, struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
e142c38c 1479static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1480 struct dwarf2_cu **);
9219021c 1481
d97bc12b
DE
1482static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1483
1484static void dump_die_for_error (struct die_info *);
1485
1486static void dump_die_1 (struct ui_file *, int level, int max_level,
1487 struct die_info *);
c906108c 1488
d97bc12b 1489/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1490
51545339 1491static void store_in_ref_table (struct die_info *,
10b3939b 1492 struct dwarf2_cu *);
c906108c 1493
348e048f 1494static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1495 const struct attribute *,
348e048f
DE
1496 struct dwarf2_cu **);
1497
10b3939b 1498static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1499 const struct attribute *,
f2f0e013 1500 struct dwarf2_cu **);
c906108c 1501
348e048f 1502static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1503 const struct attribute *,
348e048f
DE
1504 struct dwarf2_cu **);
1505
ac9ec31b
DE
1506static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1510 const struct attribute *,
ac9ec31b
DE
1511 struct dwarf2_cu *);
1512
e5fe5e75 1513static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1514
52dc124a 1515static void read_signatured_type (struct signatured_type *);
348e048f 1516
63e43d3a
PMR
1517static int attr_to_dynamic_prop (const struct attribute *attr,
1518 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1519 struct dynamic_prop *prop, struct type *type);
63e43d3a 1520
c906108c
SS
1521/* memory allocation interface */
1522
7b5a2f43 1523static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1524
b60c80d6 1525static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1526
43f3e411 1527static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1528
8cf6f0b1
TT
1529static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1530 struct dwarf2_loclist_baton *baton,
ff39bb5e 1531 const struct attribute *attr);
8cf6f0b1 1532
ff39bb5e 1533static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1534 struct symbol *sym,
f1e6e072
TT
1535 struct dwarf2_cu *cu,
1536 int is_block);
4c2df51b 1537
d521ce57
TT
1538static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1539 const gdb_byte *info_ptr,
1540 struct abbrev_info *abbrev);
4bb7a0a7 1541
72bf9492
DJ
1542static hashval_t partial_die_hash (const void *item);
1543
1544static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1545
ae038cb0 1546static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1547 (sect_offset sect_off, unsigned int offset_in_dwz,
1548 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1549
9816fde3 1550static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1551 struct die_info *comp_unit_die,
1552 enum language pretend_language);
93311388 1553
ed2dc618 1554static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1555
dee91e82 1556static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1557
f792889a
DJ
1558static struct type *set_die_type (struct die_info *, struct type *,
1559 struct dwarf2_cu *);
1c379e20 1560
ed2dc618 1561static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1562
ed2dc618 1563static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1564
58f0c718 1565static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1566 enum language);
10b3939b 1567
95554aad
TT
1568static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1569 enum language);
10b3939b 1570
f4dc4d17
DE
1571static void process_full_type_unit (struct dwarf2_per_cu_data *,
1572 enum language);
1573
10b3939b
DJ
1574static void dwarf2_add_dependence (struct dwarf2_cu *,
1575 struct dwarf2_per_cu_data *);
1576
ae038cb0
DJ
1577static void dwarf2_mark (struct dwarf2_cu *);
1578
1579static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1580
b64f50a1 1581static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1582 struct dwarf2_per_cu_data *);
673bfd45 1583
f792889a 1584static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1585
95554aad
TT
1586static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1587 enum language pretend_language);
1588
ed2dc618 1589static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1590
b303c6f6
AB
1591/* Class, the destructor of which frees all allocated queue entries. This
1592 will only have work to do if an error was thrown while processing the
1593 dwarf. If no error was thrown then the queue entries should have all
1594 been processed, and freed, as we went along. */
1595
1596class dwarf2_queue_guard
1597{
1598public:
39856def
TT
1599 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1600 : m_per_objfile (per_objfile)
1601 {
1602 }
b303c6f6
AB
1603
1604 /* Free any entries remaining on the queue. There should only be
1605 entries left if we hit an error while processing the dwarf. */
1606 ~dwarf2_queue_guard ()
1607 {
39856def
TT
1608 /* Ensure that no memory is allocated by the queue. */
1609 std::queue<dwarf2_queue_item> empty;
5989a64e 1610 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1611 }
b303c6f6 1612
39856def 1613 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1614
39856def
TT
1615private:
1616 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1617};
1618
39856def
TT
1619dwarf2_queue_item::~dwarf2_queue_item ()
1620{
1621 /* Anything still marked queued is likely to be in an
1622 inconsistent state, so discard it. */
1623 if (per_cu->queued)
1624 {
1625 if (per_cu->cu != NULL)
1626 free_one_cached_comp_unit (per_cu);
1627 per_cu->queued = 0;
1628 }
1629}
1630
d721ba37
PA
1631/* The return type of find_file_and_directory. Note, the enclosed
1632 string pointers are only valid while this object is valid. */
1633
1634struct file_and_directory
1635{
1636 /* The filename. This is never NULL. */
1637 const char *name;
1638
1639 /* The compilation directory. NULL if not known. If we needed to
1640 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1641 points directly to the DW_AT_comp_dir string attribute owned by
1642 the obstack that owns the DIE. */
1643 const char *comp_dir;
1644
1645 /* If we needed to build a new string for comp_dir, this is what
1646 owns the storage. */
1647 std::string comp_dir_storage;
1648};
1649
1650static file_and_directory find_file_and_directory (struct die_info *die,
1651 struct dwarf2_cu *cu);
9291a0cd 1652
298e9637 1653static htab_up allocate_signatured_type_table ();
1fd400ff 1654
298e9637 1655static htab_up allocate_dwo_unit_table ();
3019eac3 1656
57d63ce2 1657static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1659 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1660 ULONGEST signature, int is_debug_types);
a2ce51a0 1661
ed2dc618
SM
1662static struct dwp_file *get_dwp_file
1663 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1664
3019eac3 1665static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1666 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1667
1668static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1669 (struct signatured_type *, const char *, const char *);
3019eac3 1670
89e63ee4
DE
1671static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1672
263db9a1
TT
1673/* A unique pointer to a dwo_file. */
1674
51ac9db5 1675typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1676
ed2dc618 1677static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1678
1b80a9fa 1679static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1680
1681static void free_line_header_voidp (void *arg);
4390d890
DE
1682\f
1683/* Various complaints about symbol reading that don't abort the process. */
1684
4390d890
DE
1685static void
1686dwarf2_debug_line_missing_file_complaint (void)
1687{
b98664d3 1688 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1689}
1690
1691static void
1692dwarf2_debug_line_missing_end_sequence_complaint (void)
1693{
b98664d3 1694 complaint (_(".debug_line section has line "
4390d890
DE
1695 "program sequence without an end"));
1696}
1697
1698static void
1699dwarf2_complex_location_expr_complaint (void)
1700{
b98664d3 1701 complaint (_("location expression too complex"));
4390d890
DE
1702}
1703
1704static void
1705dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1706 int arg3)
1707{
b98664d3 1708 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1709 arg1, arg2, arg3);
1710}
1711
4390d890
DE
1712static void
1713dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1714{
b98664d3 1715 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1716 arg1, arg2);
1717}
527f3840
JK
1718
1719/* Hash function for line_header_hash. */
1720
1721static hashval_t
1722line_header_hash (const struct line_header *ofs)
1723{
9c541725 1724 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1725}
1726
1727/* Hash function for htab_create_alloc_ex for line_header_hash. */
1728
1729static hashval_t
1730line_header_hash_voidp (const void *item)
1731{
9a3c8263 1732 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1733
1734 return line_header_hash (ofs);
1735}
1736
1737/* Equality function for line_header_hash. */
1738
1739static int
1740line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1741{
9a3c8263
SM
1742 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1743 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1744
9c541725 1745 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1746 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1747}
1748
4390d890 1749\f
9291a0cd 1750
330cdd98
PA
1751/* See declaration. */
1752
5989a64e
SM
1753dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1754 bool can_copy_)
c3699833
SM
1755 : obfd (obfd),
1756 can_copy (can_copy_)
330cdd98
PA
1757{
1758 if (names == NULL)
1759 names = &dwarf2_elf_names;
1760
330cdd98
PA
1761 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1762 locate_sections (obfd, sec, *names);
1763}
1764
5989a64e 1765dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1766{
1767 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1768 free_cached_comp_units ();
1769
b76e467d 1770 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1771 per_cu->imported_symtabs_free ();
fc8e7e75 1772
b2bdb8cf 1773 for (signatured_type *sig_type : all_type_units)
ae640021 1774 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1775
5989a64e 1776 /* Everything else should be on this->obstack. */
330cdd98
PA
1777}
1778
1779/* See declaration. */
1780
1781void
5989a64e 1782dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1783{
1784 dwarf2_per_cu_data *per_cu = read_in_chain;
1785 dwarf2_per_cu_data **last_chain = &read_in_chain;
1786 while (per_cu != NULL)
1787 {
1788 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1789
fcd3b13d 1790 delete per_cu->cu;
330cdd98
PA
1791 *last_chain = next_cu;
1792 per_cu = next_cu;
1793 }
1794}
1795
11ed8cad
TT
1796/* A helper class that calls free_cached_comp_units on
1797 destruction. */
1798
1799class free_cached_comp_units
1800{
1801public:
1802
1803 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1804 : m_per_objfile (per_objfile)
1805 {
1806 }
1807
1808 ~free_cached_comp_units ()
1809 {
5989a64e 1810 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1811 }
1812
1813 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1814
1815private:
1816
1817 dwarf2_per_objfile *m_per_objfile;
1818};
1819
af758d11
SM
1820/* See read.h. */
1821
1822bool
1823dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1824{
1825 gdb_assert (per_cu->index < this->m_symtabs.size ());
1826
1827 return this->m_symtabs[per_cu->index] != nullptr;
1828}
1829
1830/* See read.h. */
1831
1832compunit_symtab *
1833dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1834{
1835 gdb_assert (per_cu->index < this->m_symtabs.size ());
1836
1837 return this->m_symtabs[per_cu->index];
1838}
1839
1840/* See read.h. */
1841
1842void
1843dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1844 compunit_symtab *symtab)
1845{
1846 gdb_assert (per_cu->index < this->m_symtabs.size ());
1847 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1848
1849 this->m_symtabs[per_cu->index] = symtab;
1850}
1851
c906108c 1852/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1853 information and return true if we have enough to do something.
1854 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1855 ELF names are used. CAN_COPY is true for formats where symbol
1856 interposition is possible and so symbol values must follow copy
1857 relocation rules. */
c906108c
SS
1858
1859int
251d32d9 1860dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1861 const struct dwarf2_debug_sections *names,
1862 bool can_copy)
c906108c 1863{
97cbe998
SDJ
1864 if (objfile->flags & OBJF_READNEVER)
1865 return 0;
1866
ed2dc618
SM
1867 struct dwarf2_per_objfile *dwarf2_per_objfile
1868 = get_dwarf2_per_objfile (objfile);
1869
1870 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1871 {
1872 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1873 sharing yet). */
1874 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1875
1876 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1877 }
5bfd760d 1878
5989a64e
SM
1879 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1880 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1881 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1882 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1883}
1884
251d32d9
TG
1885/* When loading sections, we look either for uncompressed section or for
1886 compressed section names. */
233a11ab
CS
1887
1888static int
251d32d9
TG
1889section_is_p (const char *section_name,
1890 const struct dwarf2_section_names *names)
233a11ab 1891{
251d32d9
TG
1892 if (names->normal != NULL
1893 && strcmp (section_name, names->normal) == 0)
1894 return 1;
1895 if (names->compressed != NULL
1896 && strcmp (section_name, names->compressed) == 0)
1897 return 1;
1898 return 0;
233a11ab
CS
1899}
1900
330cdd98 1901/* See declaration. */
c906108c 1902
330cdd98 1903void
5989a64e
SM
1904dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1905 const dwarf2_debug_sections &names)
c906108c 1906{
fd361982 1907 flagword aflag = bfd_section_flags (sectp);
251d32d9 1908
dc7650b8
JK
1909 if ((aflag & SEC_HAS_CONTENTS) == 0)
1910 {
1911 }
950b7495
KS
1912 else if (elf_section_data (sectp)->this_hdr.sh_size
1913 > bfd_get_file_size (abfd))
1914 {
1915 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1916 warning (_("Discarding section %s which has a section size (%s"
1917 ") larger than the file size [in module %s]"),
1918 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1919 bfd_get_filename (abfd));
1920 }
330cdd98 1921 else if (section_is_p (sectp->name, &names.info))
c906108c 1922 {
330cdd98 1923 this->info.s.section = sectp;
fd361982 1924 this->info.size = bfd_section_size (sectp);
c906108c 1925 }
330cdd98 1926 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1927 {
330cdd98 1928 this->abbrev.s.section = sectp;
fd361982 1929 this->abbrev.size = bfd_section_size (sectp);
c906108c 1930 }
330cdd98 1931 else if (section_is_p (sectp->name, &names.line))
c906108c 1932 {
330cdd98 1933 this->line.s.section = sectp;
fd361982 1934 this->line.size = bfd_section_size (sectp);
c906108c 1935 }
330cdd98 1936 else if (section_is_p (sectp->name, &names.loc))
c906108c 1937 {
330cdd98 1938 this->loc.s.section = sectp;
fd361982 1939 this->loc.size = bfd_section_size (sectp);
c906108c 1940 }
330cdd98 1941 else if (section_is_p (sectp->name, &names.loclists))
43988095 1942 {
330cdd98 1943 this->loclists.s.section = sectp;
fd361982 1944 this->loclists.size = bfd_section_size (sectp);
43988095 1945 }
330cdd98 1946 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1947 {
330cdd98 1948 this->macinfo.s.section = sectp;
fd361982 1949 this->macinfo.size = bfd_section_size (sectp);
c906108c 1950 }
330cdd98 1951 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1952 {
330cdd98 1953 this->macro.s.section = sectp;
fd361982 1954 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1955 }
330cdd98 1956 else if (section_is_p (sectp->name, &names.str))
c906108c 1957 {
330cdd98 1958 this->str.s.section = sectp;
fd361982 1959 this->str.size = bfd_section_size (sectp);
c906108c 1960 }
18a8505e
AT
1961 else if (section_is_p (sectp->name, &names.str_offsets))
1962 {
1963 this->str_offsets.s.section = sectp;
1964 this->str_offsets.size = bfd_section_size (sectp);
1965 }
330cdd98 1966 else if (section_is_p (sectp->name, &names.line_str))
43988095 1967 {
330cdd98 1968 this->line_str.s.section = sectp;
fd361982 1969 this->line_str.size = bfd_section_size (sectp);
43988095 1970 }
330cdd98 1971 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1972 {
330cdd98 1973 this->addr.s.section = sectp;
fd361982 1974 this->addr.size = bfd_section_size (sectp);
3019eac3 1975 }
330cdd98 1976 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1977 {
330cdd98 1978 this->frame.s.section = sectp;
fd361982 1979 this->frame.size = bfd_section_size (sectp);
b6af0555 1980 }
330cdd98 1981 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1982 {
330cdd98 1983 this->eh_frame.s.section = sectp;
fd361982 1984 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1985 }
330cdd98 1986 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1987 {
330cdd98 1988 this->ranges.s.section = sectp;
fd361982 1989 this->ranges.size = bfd_section_size (sectp);
af34e669 1990 }
330cdd98 1991 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1992 {
330cdd98 1993 this->rnglists.s.section = sectp;
fd361982 1994 this->rnglists.size = bfd_section_size (sectp);
43988095 1995 }
330cdd98 1996 else if (section_is_p (sectp->name, &names.types))
348e048f 1997 {
8b70b953
TT
1998 struct dwarf2_section_info type_section;
1999
2000 memset (&type_section, 0, sizeof (type_section));
049412e3 2001 type_section.s.section = sectp;
fd361982 2002 type_section.size = bfd_section_size (sectp);
8b70b953 2003
fd5866f6 2004 this->types.push_back (type_section);
348e048f 2005 }
330cdd98 2006 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2007 {
330cdd98 2008 this->gdb_index.s.section = sectp;
fd361982 2009 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2010 }
927aa2e7
JK
2011 else if (section_is_p (sectp->name, &names.debug_names))
2012 {
2013 this->debug_names.s.section = sectp;
fd361982 2014 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2015 }
2016 else if (section_is_p (sectp->name, &names.debug_aranges))
2017 {
2018 this->debug_aranges.s.section = sectp;
fd361982 2019 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2020 }
dce234bc 2021
fd361982
AM
2022 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2023 && bfd_section_vma (sectp) == 0)
330cdd98 2024 this->has_section_at_zero = true;
c906108c
SS
2025}
2026
dce234bc 2027/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2028 SECTION_NAME. */
af34e669 2029
dce234bc 2030void
3017a003
TG
2031dwarf2_get_section_info (struct objfile *objfile,
2032 enum dwarf2_section_enum sect,
d521ce57 2033 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2034 bfd_size_type *sizep)
2035{
5bfd760d 2036 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2037 struct dwarf2_section_info *info;
a3b2a86b
TT
2038
2039 /* We may see an objfile without any DWARF, in which case we just
2040 return nothing. */
2041 if (data == NULL)
2042 {
2043 *sectp = NULL;
2044 *bufp = NULL;
2045 *sizep = 0;
2046 return;
2047 }
3017a003
TG
2048 switch (sect)
2049 {
2050 case DWARF2_DEBUG_FRAME:
5989a64e 2051 info = &data->per_bfd->frame;
3017a003
TG
2052 break;
2053 case DWARF2_EH_FRAME:
5989a64e 2054 info = &data->per_bfd->eh_frame;
3017a003
TG
2055 break;
2056 default:
2057 gdb_assert_not_reached ("unexpected section");
2058 }
dce234bc 2059
96b79293 2060 info->read (objfile);
dce234bc 2061
96b79293 2062 *sectp = info->get_bfd_section ();
dce234bc
PP
2063 *bufp = info->buffer;
2064 *sizep = info->size;
2065}
2066
36586728
TT
2067/* A helper function to find the sections for a .dwz file. */
2068
2069static void
2070locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2071{
9a3c8263 2072 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2073
2074 /* Note that we only support the standard ELF names, because .dwz
2075 is ELF-only (at the time of writing). */
2076 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2077 {
049412e3 2078 dwz_file->abbrev.s.section = sectp;
fd361982 2079 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2080 }
2081 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2082 {
049412e3 2083 dwz_file->info.s.section = sectp;
fd361982 2084 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2085 }
2086 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2087 {
049412e3 2088 dwz_file->str.s.section = sectp;
fd361982 2089 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2092 {
049412e3 2093 dwz_file->line.s.section = sectp;
fd361982 2094 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2097 {
049412e3 2098 dwz_file->macro.s.section = sectp;
fd361982 2099 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2100 }
2ec9a5e0
TT
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2102 {
049412e3 2103 dwz_file->gdb_index.s.section = sectp;
fd361982 2104 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2105 }
927aa2e7
JK
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2107 {
2108 dwz_file->debug_names.s.section = sectp;
fd361982 2109 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2110 }
36586728
TT
2111}
2112
c4973306 2113/* See dwarf2read.h. */
36586728 2114
c4973306 2115struct dwz_file *
c3699833 2116dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2117{
36586728 2118 const char *filename;
acd13123 2119 bfd_size_type buildid_len_arg;
dc294be5
TT
2120 size_t buildid_len;
2121 bfd_byte *buildid;
36586728 2122
c3699833
SM
2123 if (per_bfd->dwz_file != NULL)
2124 return per_bfd->dwz_file.get ();
36586728 2125
4db1a1dc 2126 bfd_set_error (bfd_error_no_error);
791afaa2 2127 gdb::unique_xmalloc_ptr<char> data
c3699833 2128 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2129 &buildid_len_arg, &buildid));
4db1a1dc
TT
2130 if (data == NULL)
2131 {
2132 if (bfd_get_error () == bfd_error_no_error)
2133 return NULL;
2134 error (_("could not read '.gnu_debugaltlink' section: %s"),
2135 bfd_errmsg (bfd_get_error ()));
2136 }
791afaa2
TT
2137
2138 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2139
acd13123
TT
2140 buildid_len = (size_t) buildid_len_arg;
2141
791afaa2 2142 filename = data.get ();
d721ba37
PA
2143
2144 std::string abs_storage;
36586728
TT
2145 if (!IS_ABSOLUTE_PATH (filename))
2146 {
14278e1f 2147 gdb::unique_xmalloc_ptr<char> abs
c3699833 2148 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2149
14278e1f 2150 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2151 filename = abs_storage.c_str ();
36586728
TT
2152 }
2153
dc294be5
TT
2154 /* First try the file name given in the section. If that doesn't
2155 work, try to use the build-id instead. */
ad80db5b 2156 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2157 if (dwz_bfd != NULL)
36586728 2158 {
192b62ce 2159 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2160 dwz_bfd.reset (nullptr);
36586728
TT
2161 }
2162
dc294be5
TT
2163 if (dwz_bfd == NULL)
2164 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2165
0d79cdc4
AM
2166 if (dwz_bfd == nullptr)
2167 {
2168 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2169 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2170
2171 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2172 buildid_len,
2173 origname,
2174 &alt_filename));
2175
2176 if (fd.get () >= 0)
2177 {
2178 /* File successfully retrieved from server. */
ad80db5b 2179 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2180
2181 if (dwz_bfd == nullptr)
2182 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2183 alt_filename.get ());
2184 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2185 dwz_bfd.reset (nullptr);
2186 }
2187 }
2188
dc294be5
TT
2189 if (dwz_bfd == NULL)
2190 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2191 bfd_get_filename (per_bfd->obfd));
dc294be5 2192
7ff8cb8c
TT
2193 std::unique_ptr<struct dwz_file> result
2194 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2195
7ff8cb8c
TT
2196 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2197 result.get ());
36586728 2198
c3699833
SM
2199 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2200 per_bfd->dwz_file = std::move (result);
2201 return per_bfd->dwz_file.get ();
36586728 2202}
9291a0cd 2203\f
7b9f3c50
DE
2204/* DWARF quick_symbols_functions support. */
2205
2206/* TUs can share .debug_line entries, and there can be a lot more TUs than
2207 unique line tables, so we maintain a separate table of all .debug_line
2208 derived entries to support the sharing.
2209 All the quick functions need is the list of file names. We discard the
2210 line_header when we're done and don't need to record it here. */
2211struct quick_file_names
2212{
094b34ac
DE
2213 /* The data used to construct the hash key. */
2214 struct stmt_list_hash hash;
7b9f3c50
DE
2215
2216 /* The number of entries in file_names, real_names. */
2217 unsigned int num_file_names;
2218
2219 /* The file names from the line table, after being run through
2220 file_full_name. */
2221 const char **file_names;
2222
2223 /* The file names from the line table after being run through
2224 gdb_realpath. These are computed lazily. */
2225 const char **real_names;
2226};
2227
2228/* When using the index (and thus not using psymtabs), each CU has an
2229 object of this type. This is used to hold information needed by
2230 the various "quick" methods. */
2231struct dwarf2_per_cu_quick_data
2232{
2233 /* The file table. This can be NULL if there was no file table
2234 or it's currently not read in.
5989a64e 2235 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2236 struct quick_file_names *file_names;
2237
7b9f3c50
DE
2238 /* A temporary mark bit used when iterating over all CUs in
2239 expand_symtabs_matching. */
2240 unsigned int mark : 1;
2241
2242 /* True if we've tried to read the file table and found there isn't one.
2243 There will be no point in trying to read it again next time. */
2244 unsigned int no_file_data : 1;
2245};
2246
094b34ac
DE
2247/* Utility hash function for a stmt_list_hash. */
2248
2249static hashval_t
2250hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2251{
2252 hashval_t v = 0;
2253
2254 if (stmt_list_hash->dwo_unit != NULL)
2255 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2256 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2257 return v;
2258}
2259
2260/* Utility equality function for a stmt_list_hash. */
2261
2262static int
2263eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2264 const struct stmt_list_hash *rhs)
2265{
2266 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2267 return 0;
2268 if (lhs->dwo_unit != NULL
2269 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2270 return 0;
2271
9c541725 2272 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2273}
2274
7b9f3c50
DE
2275/* Hash function for a quick_file_names. */
2276
2277static hashval_t
2278hash_file_name_entry (const void *e)
2279{
9a3c8263
SM
2280 const struct quick_file_names *file_data
2281 = (const struct quick_file_names *) e;
7b9f3c50 2282
094b34ac 2283 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2284}
2285
2286/* Equality function for a quick_file_names. */
2287
2288static int
2289eq_file_name_entry (const void *a, const void *b)
2290{
9a3c8263
SM
2291 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2292 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2293
094b34ac 2294 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2295}
2296
2297/* Delete function for a quick_file_names. */
2298
2299static void
2300delete_file_name_entry (void *e)
2301{
9a3c8263 2302 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2303 int i;
2304
2305 for (i = 0; i < file_data->num_file_names; ++i)
2306 {
2307 xfree ((void*) file_data->file_names[i]);
2308 if (file_data->real_names)
2309 xfree ((void*) file_data->real_names[i]);
2310 }
2311
45940949
TT
2312 /* The space for the struct itself lives on the obstack, so we don't
2313 free it here. */
7b9f3c50
DE
2314}
2315
2316/* Create a quick_file_names hash table. */
2317
5895093f 2318static htab_up
7b9f3c50
DE
2319create_quick_file_names_table (unsigned int nr_initial_entries)
2320{
5895093f
TT
2321 return htab_up (htab_create_alloc (nr_initial_entries,
2322 hash_file_name_entry, eq_file_name_entry,
2323 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2324}
9291a0cd 2325
918dd910
JK
2326/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2327 have to be created afterwards. You should call age_cached_comp_units after
2328 processing PER_CU->CU. dw2_setup must have been already called. */
2329
2330static void
58f0c718 2331load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2332{
3019eac3 2333 if (per_cu->is_debug_types)
e5fe5e75 2334 load_full_type_unit (per_cu);
918dd910 2335 else
58f0c718 2336 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2337
cc12ce38
DE
2338 if (per_cu->cu == NULL)
2339 return; /* Dummy CU. */
2dc860c0
DE
2340
2341 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2342}
2343
97a1449a 2344/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2345
9291a0cd 2346static void
97a1449a
SM
2347dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2348 dwarf2_per_objfile *dwarf2_per_objfile,
2349 bool skip_partial)
9291a0cd 2350{
f4dc4d17
DE
2351 /* Skip type_unit_groups, reading the type units they contain
2352 is handled elsewhere. */
197400e8 2353 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2354 return;
2355
b303c6f6
AB
2356 /* The destructor of dwarf2_queue_guard frees any entries left on
2357 the queue. After this point we're guaranteed to leave this function
2358 with the dwarf queue empty. */
39856def 2359 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2360
af758d11 2361 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad
TT
2362 {
2363 queue_comp_unit (per_cu, language_minimal);
58f0c718 2364 load_cu (per_cu, skip_partial);
89e63ee4
DE
2365
2366 /* If we just loaded a CU from a DWO, and we're working with an index
2367 that may badly handle TUs, load all the TUs in that DWO as well.
2368 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2369 if (!per_cu->is_debug_types
cc12ce38 2370 && per_cu->cu != NULL
89e63ee4 2371 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2372 && dwarf2_per_objfile->per_bfd->index_table != NULL
2373 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2374 /* DWP files aren't supported yet. */
ed2dc618 2375 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2376 queue_and_load_all_dwo_tus (per_cu);
95554aad 2377 }
9291a0cd 2378
ed2dc618 2379 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2380
2381 /* Age the cache, releasing compilation units that have not
2382 been used recently. */
ed2dc618 2383 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2384}
2385
97a1449a
SM
2386/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2387 the per-objfile for which this symtab is instantiated.
2388
2389 Returns the resulting symbol table. */
2fdf6df6 2390
43f3e411 2391static struct compunit_symtab *
97a1449a
SM
2392dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2393 dwarf2_per_objfile *dwarf2_per_objfile,
2394 bool skip_partial)
9291a0cd 2395{
5989a64e 2396 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2397
2398 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2399 {
11ed8cad 2400 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2401 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2402 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2403 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2404 }
f194fefb 2405
af758d11 2406 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2407}
2408
ff4c9fec 2409/* See declaration. */
f4dc4d17 2410
ff4c9fec 2411dwarf2_per_cu_data *
5989a64e 2412dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2413{
b76e467d 2414 if (index >= this->all_comp_units.size ())
ff4c9fec 2415 {
b76e467d 2416 index -= this->all_comp_units.size ();
b2bdb8cf 2417 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2418 return &this->all_type_units[index]->per_cu;
2419 }
f4dc4d17 2420
ff4c9fec
SM
2421 return this->all_comp_units[index];
2422}
f4dc4d17 2423
ff4c9fec 2424/* See declaration. */
2fdf6df6 2425
ff4c9fec 2426dwarf2_per_cu_data *
5989a64e 2427dwarf2_per_bfd::get_cu (int index)
1fd400ff 2428{
b76e467d 2429 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2430
ff4c9fec 2431 return this->all_comp_units[index];
f4dc4d17
DE
2432}
2433
ff4c9fec 2434/* See declaration. */
f4dc4d17 2435
ff4c9fec 2436signatured_type *
5989a64e 2437dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2438{
b2bdb8cf 2439 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2440
ff4c9fec 2441 return this->all_type_units[index];
1fd400ff
TT
2442}
2443
d3473f0c
TT
2444/* See read.h. */
2445
2446dwarf2_per_cu_data *
5989a64e 2447dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2448{
2449 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2450 result->per_bfd = this;
d3473f0c
TT
2451 result->index = m_num_psymtabs++;
2452 return result;
2453}
2454
2455/* See read.h. */
2456
2457signatured_type *
5989a64e 2458dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2459{
2460 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2461 result->per_cu.per_bfd = this;
d3473f0c
TT
2462 result->per_cu.index = m_num_psymtabs++;
2463 return result;
2464}
2465
45940949
TT
2466/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2467 obstack, and constructed with the specified field values. */
4b514bc8
JK
2468
2469static dwarf2_per_cu_data *
ed2dc618 2470create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2471 struct dwarf2_section_info *section,
2472 int is_dwz,
2473 sect_offset sect_off, ULONGEST length)
2474{
5989a64e 2475 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
4b514bc8
JK
2476 the_cu->sect_off = sect_off;
2477 the_cu->length = length;
e3b94546 2478 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8 2479 the_cu->section = section;
5989a64e 2480 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
45940949 2481 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2482 the_cu->is_dwz = is_dwz;
2483 return the_cu;
2484}
2485
2ec9a5e0
TT
2486/* A helper for create_cus_from_index that handles a given list of
2487 CUs. */
2fdf6df6 2488
74a0d9f6 2489static void
12359b5e 2490create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2491 const gdb_byte *cu_list, offset_type n_elements,
2492 struct dwarf2_section_info *section,
b76e467d 2493 int is_dwz)
9291a0cd 2494{
12359b5e 2495 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2496 {
74a0d9f6 2497 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2498
2499 sect_offset sect_off
2500 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2501 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2502 cu_list += 2 * 8;
2503
b76e467d 2504 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2505 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2506 sect_off, length);
5989a64e 2507 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2508 }
9291a0cd
TT
2509}
2510
2ec9a5e0 2511/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2512 the CU objects for this objfile. */
2ec9a5e0 2513
74a0d9f6 2514static void
12359b5e 2515create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2516 const gdb_byte *cu_list, offset_type cu_list_elements,
2517 const gdb_byte *dwz_list, offset_type dwz_elements)
2518{
5989a64e
SM
2519 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2520 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
b76e467d 2521 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2522
12359b5e 2523 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
5989a64e 2524 &dwarf2_per_objfile->per_bfd->info, 0);
2ec9a5e0
TT
2525
2526 if (dwz_elements == 0)
74a0d9f6 2527 return;
2ec9a5e0 2528
c3699833 2529 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
12359b5e 2530 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2531 &dwz->info, 1);
2ec9a5e0
TT
2532}
2533
1fd400ff 2534/* Create the signatured type hash table from the index. */
673bfd45 2535
74a0d9f6 2536static void
12359b5e
SM
2537create_signatured_type_table_from_index
2538 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2539 struct dwarf2_section_info *section,
2540 const gdb_byte *bytes,
2541 offset_type elements)
1fd400ff 2542{
5989a64e
SM
2543 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2544 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2545
298e9637 2546 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2547
12359b5e 2548 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2549 {
52dc124a 2550 struct signatured_type *sig_type;
9c541725 2551 ULONGEST signature;
1fd400ff 2552 void **slot;
9c541725 2553 cu_offset type_offset_in_tu;
1fd400ff 2554
74a0d9f6 2555 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2556 sect_offset sect_off
2557 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2558 type_offset_in_tu
2559 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2560 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2561 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2562 bytes += 3 * 8;
2563
5989a64e 2564 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
52dc124a 2565 sig_type->signature = signature;
9c541725 2566 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2567 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2568 sig_type->per_cu.section = section;
9c541725 2569 sig_type->per_cu.sect_off = sect_off;
e3b94546 2570 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2571 sig_type->per_cu.v.quick
5989a64e 2572 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
1fd400ff
TT
2573 struct dwarf2_per_cu_quick_data);
2574
b0b6a987 2575 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2576 *slot = sig_type;
1fd400ff 2577
5989a64e 2578 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2579 }
2580
5989a64e 2581 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2582}
2583
927aa2e7
JK
2584/* Create the signatured type hash table from .debug_names. */
2585
2586static void
2587create_signatured_type_table_from_debug_names
ed2dc618 2588 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2589 const mapped_debug_names &map,
2590 struct dwarf2_section_info *section,
2591 struct dwarf2_section_info *abbrev_section)
2592{
ed2dc618
SM
2593 struct objfile *objfile = dwarf2_per_objfile->objfile;
2594
96b79293
TT
2595 section->read (objfile);
2596 abbrev_section->read (objfile);
927aa2e7 2597
5989a64e
SM
2598 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2599 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2600
298e9637 2601 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2602
2603 for (uint32_t i = 0; i < map.tu_count; ++i)
2604 {
2605 struct signatured_type *sig_type;
927aa2e7 2606 void **slot;
927aa2e7
JK
2607
2608 sect_offset sect_off
2609 = (sect_offset) (extract_unsigned_integer
2610 (map.tu_table_reordered + i * map.offset_size,
2611 map.offset_size,
2612 map.dwarf5_byte_order));
2613
2614 comp_unit_head cu_header;
ed2dc618
SM
2615 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2616 abbrev_section,
927aa2e7
JK
2617 section->buffer + to_underlying (sect_off),
2618 rcuh_kind::TYPE);
2619
5989a64e 2620 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2621 sig_type->signature = cu_header.signature;
2622 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2623 sig_type->per_cu.is_debug_types = 1;
2624 sig_type->per_cu.section = section;
2625 sig_type->per_cu.sect_off = sect_off;
e3b94546 2626 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7 2627 sig_type->per_cu.v.quick
5989a64e 2628 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2629 struct dwarf2_per_cu_quick_data);
2630
b0b6a987 2631 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2632 *slot = sig_type;
2633
5989a64e 2634 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2635 }
2636
5989a64e 2637 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2638}
2639
9291a0cd
TT
2640/* Read the address map data from the mapped index, and use it to
2641 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2642
9291a0cd 2643static void
ed2dc618
SM
2644create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2645 struct mapped_index *index)
9291a0cd 2646{
ed2dc618 2647 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2648 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2649 const gdb_byte *iter, *end;
9291a0cd 2650 struct addrmap *mutable_map;
9291a0cd
TT
2651 CORE_ADDR baseaddr;
2652
8268c778
PA
2653 auto_obstack temp_obstack;
2654
9291a0cd
TT
2655 mutable_map = addrmap_create_mutable (&temp_obstack);
2656
f00a2de2
PA
2657 iter = index->address_table.data ();
2658 end = iter + index->address_table.size ();
9291a0cd 2659
b3b3bada 2660 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2661
2662 while (iter < end)
2663 {
2664 ULONGEST hi, lo, cu_index;
2665 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2666 iter += 8;
2667 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2668 iter += 8;
2669 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2670 iter += 4;
f652bce2 2671
24a55014 2672 if (lo > hi)
f652bce2 2673 {
b98664d3 2674 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2675 hex_string (lo), hex_string (hi));
24a55014 2676 continue;
f652bce2 2677 }
24a55014 2678
5989a64e 2679 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2680 {
b98664d3 2681 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2682 (unsigned) cu_index);
24a55014 2683 continue;
f652bce2 2684 }
24a55014 2685
79748972
TT
2686 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2687 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2688 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2689 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2690 }
2691
d320c2b5 2692 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2693 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2694}
2695
927aa2e7
JK
2696/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2697 populate the objfile's psymtabs_addrmap. */
2698
2699static void
ed2dc618 2700create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2701 struct dwarf2_section_info *section)
2702{
ed2dc618 2703 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2704 bfd *abfd = objfile->obfd;
08feed99 2705 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2706 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2707
2708 auto_obstack temp_obstack;
2709 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2710
2711 std::unordered_map<sect_offset,
2712 dwarf2_per_cu_data *,
2713 gdb::hash_enum<sect_offset>>
2714 debug_info_offset_to_per_cu;
5989a64e 2715 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2716 {
927aa2e7
JK
2717 const auto insertpair
2718 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2719 if (!insertpair.second)
2720 {
2721 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2722 "debug_info_offset %s, ignoring .debug_aranges."),
2723 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2724 return;
2725 }
2726 }
2727
96b79293 2728 section->read (objfile);
927aa2e7
JK
2729
2730 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2731
2732 const gdb_byte *addr = section->buffer;
2733
2734 while (addr < section->buffer + section->size)
2735 {
2736 const gdb_byte *const entry_addr = addr;
2737 unsigned int bytes_read;
2738
2739 const LONGEST entry_length = read_initial_length (abfd, addr,
2740 &bytes_read);
2741 addr += bytes_read;
2742
2743 const gdb_byte *const entry_end = addr + entry_length;
2744 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2745 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2746 if (addr + entry_length > section->buffer + section->size)
2747 {
47e3f474 2748 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2749 "length %s exceeds section length %s, "
2750 "ignoring .debug_aranges."),
47e3f474
TV
2751 objfile_name (objfile),
2752 plongest (entry_addr - section->buffer),
927aa2e7
JK
2753 plongest (bytes_read + entry_length),
2754 pulongest (section->size));
2755 return;
2756 }
2757
2758 /* The version number. */
2759 const uint16_t version = read_2_bytes (abfd, addr);
2760 addr += 2;
2761 if (version != 2)
2762 {
47e3f474 2763 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2764 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2765 objfile_name (objfile),
2766 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2767 return;
2768 }
2769
2770 const uint64_t debug_info_offset
2771 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2772 addr += offset_size;
2773 const auto per_cu_it
2774 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2775 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2776 {
47e3f474 2777 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2778 "debug_info_offset %s does not exists, "
2779 "ignoring .debug_aranges."),
47e3f474
TV
2780 objfile_name (objfile),
2781 plongest (entry_addr - section->buffer),
927aa2e7
JK
2782 pulongest (debug_info_offset));
2783 return;
2784 }
2785 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2786
2787 const uint8_t address_size = *addr++;
2788 if (address_size < 1 || address_size > 8)
2789 {
47e3f474 2790 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2791 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2792 objfile_name (objfile),
2793 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2794 return;
2795 }
2796
2797 const uint8_t segment_selector_size = *addr++;
2798 if (segment_selector_size != 0)
2799 {
47e3f474 2800 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2801 "segment_selector_size %u is not supported, "
2802 "ignoring .debug_aranges."),
47e3f474
TV
2803 objfile_name (objfile),
2804 plongest (entry_addr - section->buffer),
927aa2e7
JK
2805 segment_selector_size);
2806 return;
2807 }
2808
2809 /* Must pad to an alignment boundary that is twice the address
2810 size. It is undocumented by the DWARF standard but GCC does
2811 use it. */
2812 for (size_t padding = ((-(addr - section->buffer))
2813 & (2 * address_size - 1));
2814 padding > 0; padding--)
2815 if (*addr++ != 0)
2816 {
47e3f474 2817 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2818 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2819 objfile_name (objfile),
2820 plongest (entry_addr - section->buffer));
927aa2e7
JK
2821 return;
2822 }
2823
2824 for (;;)
2825 {
2826 if (addr + 2 * address_size > entry_end)
2827 {
47e3f474 2828 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2829 "address list is not properly terminated, "
2830 "ignoring .debug_aranges."),
47e3f474
TV
2831 objfile_name (objfile),
2832 plongest (entry_addr - section->buffer));
927aa2e7
JK
2833 return;
2834 }
2835 ULONGEST start = extract_unsigned_integer (addr, address_size,
2836 dwarf5_byte_order);
2837 addr += address_size;
2838 ULONGEST length = extract_unsigned_integer (addr, address_size,
2839 dwarf5_byte_order);
2840 addr += address_size;
2841 if (start == 0 && length == 0)
2842 break;
5989a64e 2843 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2844 {
2845 /* Symbol was eliminated due to a COMDAT group. */
2846 continue;
2847 }
2848 ULONGEST end = start + length;
79748972
TT
2849 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2850 - baseaddr);
2851 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2852 - baseaddr);
927aa2e7
JK
2853 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2854 }
2855 }
2856
d320c2b5 2857 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2858 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2859}
2860
9291a0cd
TT
2861/* Find a slot in the mapped index INDEX for the object named NAME.
2862 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2863 constant pool and return true. If NAME cannot be found, return
2864 false. */
2fdf6df6 2865
109483d9 2866static bool
9291a0cd
TT
2867find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2868 offset_type **vec_out)
2869{
0cf03b49 2870 offset_type hash;
9291a0cd 2871 offset_type slot, step;
559a7a62 2872 int (*cmp) (const char *, const char *);
9291a0cd 2873
791afaa2 2874 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2875 if (current_language->la_language == language_cplus
45280282
IB
2876 || current_language->la_language == language_fortran
2877 || current_language->la_language == language_d)
0cf03b49
JK
2878 {
2879 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2880 not contain any. */
a8719064 2881
72998fb3 2882 if (strchr (name, '(') != NULL)
0cf03b49 2883 {
109483d9 2884 without_params = cp_remove_params (name);
0cf03b49 2885
72998fb3 2886 if (without_params != NULL)
791afaa2 2887 name = without_params.get ();
0cf03b49
JK
2888 }
2889 }
2890
559a7a62 2891 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2892 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2893 simulate our NAME being searched is also lowercased. */
2894 hash = mapped_index_string_hash ((index->version == 4
2895 && case_sensitivity == case_sensitive_off
2896 ? 5 : index->version),
2897 name);
2898
f00a2de2
PA
2899 slot = hash & (index->symbol_table.size () - 1);
2900 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2901 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2902
2903 for (;;)
2904 {
9291a0cd 2905 const char *str;
f00a2de2
PA
2906
2907 const auto &bucket = index->symbol_table[slot];
2908 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2909 return false;
9291a0cd 2910
f00a2de2 2911 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2912 if (!cmp (name, str))
9291a0cd
TT
2913 {
2914 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2915 + MAYBE_SWAP (bucket.vec));
109483d9 2916 return true;
9291a0cd
TT
2917 }
2918
f00a2de2 2919 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2920 }
2921}
2922
4485a1c1
SM
2923/* A helper function that reads the .gdb_index from BUFFER and fills
2924 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2925 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2926 ok to use deprecated sections.
2927
2928 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2929 out parameters that are filled in with information about the CU and
2930 TU lists in the section.
2931
4485a1c1 2932 Returns true if all went well, false otherwise. */
2fdf6df6 2933
d33bc52e 2934static bool
3810f182 2935read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2936 bool deprecated_ok,
2937 gdb::array_view<const gdb_byte> buffer,
2938 struct mapped_index *map,
2939 const gdb_byte **cu_list,
2940 offset_type *cu_list_elements,
2941 const gdb_byte **types_list,
2942 offset_type *types_list_elements)
2943{
2944 const gdb_byte *addr = &buffer[0];
82430852 2945
9291a0cd 2946 /* Version check. */
4485a1c1 2947 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2948 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2949 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2950 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2951 indices. */
831adc1f 2952 if (version < 4)
481860b3
GB
2953 {
2954 static int warning_printed = 0;
2955 if (!warning_printed)
2956 {
2957 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2958 filename);
481860b3
GB
2959 warning_printed = 1;
2960 }
2961 return 0;
2962 }
2963 /* Index version 4 uses a different hash function than index version
2964 5 and later.
2965
2966 Versions earlier than 6 did not emit psymbols for inlined
2967 functions. Using these files will cause GDB not to be able to
2968 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2969 indices unless the user has done
2970 "set use-deprecated-index-sections on". */
2ec9a5e0 2971 if (version < 6 && !deprecated_ok)
481860b3
GB
2972 {
2973 static int warning_printed = 0;
2974 if (!warning_printed)
2975 {
e615022a
DE
2976 warning (_("\
2977Skipping deprecated .gdb_index section in %s.\n\
2978Do \"set use-deprecated-index-sections on\" before the file is read\n\
2979to use the section anyway."),
2ec9a5e0 2980 filename);
481860b3
GB
2981 warning_printed = 1;
2982 }
2983 return 0;
2984 }
796a7ff8 2985 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2986 of the TU (for symbols coming from TUs),
2987 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2988 Plus gold-generated indices can have duplicate entries for global symbols,
2989 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2990 These are just performance bugs, and we can't distinguish gdb-generated
2991 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2992
481860b3 2993 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2994 longer backward compatible. */
796a7ff8 2995 if (version > 8)
594e8718 2996 return 0;
9291a0cd 2997
559a7a62 2998 map->version = version;
9291a0cd 2999
4485a1c1 3000 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3001
4485a1c1 3002 int i = 0;
2ec9a5e0
TT
3003 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3004 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3005 / 8);
1fd400ff
TT
3006 ++i;
3007
2ec9a5e0
TT
3008 *types_list = addr + MAYBE_SWAP (metadata[i]);
3009 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3010 - MAYBE_SWAP (metadata[i]))
3011 / 8);
987d643c 3012 ++i;
1fd400ff 3013
f00a2de2
PA
3014 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3015 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3016 map->address_table
3017 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3018 ++i;
3019
f00a2de2
PA
3020 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3021 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3022 map->symbol_table
3023 = gdb::array_view<mapped_index::symbol_table_slot>
3024 ((mapped_index::symbol_table_slot *) symbol_table,
3025 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3026
f00a2de2 3027 ++i;
f9d83a0b 3028 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3029
2ec9a5e0
TT
3030 return 1;
3031}
3032
4485a1c1
SM
3033/* Callback types for dwarf2_read_gdb_index. */
3034
3035typedef gdb::function_view
5989a64e 3036 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3037 get_gdb_index_contents_ftype;
3038typedef gdb::function_view
3039 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3040 get_gdb_index_contents_dwz_ftype;
3041
927aa2e7 3042/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3043 elements of all the CUs and return 1. Otherwise, return 0. */
3044
3045static int
4485a1c1
SM
3046dwarf2_read_gdb_index
3047 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3048 get_gdb_index_contents_ftype get_gdb_index_contents,
3049 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3050{
2ec9a5e0
TT
3051 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3052 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3053 struct dwz_file *dwz;
12359b5e 3054 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3055
4485a1c1 3056 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3057 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3058
3059 if (main_index_contents.empty ())
3060 return 0;
3061
3063847f 3062 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3063 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3064 use_deprecated_index_sections,
3065 main_index_contents, map.get (), &cu_list,
3066 &cu_list_elements, &types_list,
3067 &types_list_elements))
2ec9a5e0
TT
3068 return 0;
3069
0fefef59 3070 /* Don't use the index if it's empty. */
3063847f 3071 if (map->symbol_table.empty ())
0fefef59
DE
3072 return 0;
3073
2ec9a5e0
TT
3074 /* If there is a .dwz file, read it so we can get its CU list as
3075 well. */
c3699833 3076 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3077 if (dwz != NULL)
2ec9a5e0 3078 {
2ec9a5e0
TT
3079 struct mapped_index dwz_map;
3080 const gdb_byte *dwz_types_ignore;
3081 offset_type dwz_types_elements_ignore;
3082
4485a1c1
SM
3083 gdb::array_view<const gdb_byte> dwz_index_content
3084 = get_gdb_index_contents_dwz (objfile, dwz);
3085
3086 if (dwz_index_content.empty ())
3087 return 0;
3088
3810f182 3089 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3090 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3091 &dwz_list, &dwz_list_elements,
3092 &dwz_types_ignore,
3093 &dwz_types_elements_ignore))
2ec9a5e0
TT
3094 {
3095 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3096 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3097 return 0;
3098 }
3099 }
3100
12359b5e
SM
3101 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3102 dwz_list, dwz_list_elements);
1fd400ff 3103
8b70b953
TT
3104 if (types_list_elements)
3105 {
8b70b953
TT
3106 /* We can only handle a single .debug_types when we have an
3107 index. */
5989a64e 3108 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3109 return 0;
3110
5989a64e 3111 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3112
12359b5e
SM
3113 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3114 types_list, types_list_elements);
8b70b953 3115 }
9291a0cd 3116
3063847f 3117 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3118
5989a64e
SM
3119 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3120 dwarf2_per_objfile->per_bfd->using_index = 1;
3121 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3122 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3123
3124 return 1;
3125}
3126
dee91e82 3127/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3128
dee91e82
DE
3129static void
3130dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3131 const gdb_byte *info_ptr,
3e225074 3132 struct die_info *comp_unit_die)
9291a0cd 3133{
dee91e82 3134 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3135 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3136 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3137 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3138 struct attribute *attr;
7b9f3c50
DE
3139 void **slot;
3140 struct quick_file_names *qfn;
9291a0cd 3141
0186c6a7
DE
3142 gdb_assert (! this_cu->is_debug_types);
3143
07261596
TT
3144 /* Our callers never want to match partial units -- instead they
3145 will match the enclosing full CU. */
3146 if (comp_unit_die->tag == DW_TAG_partial_unit)
3147 {
3148 this_cu->v.quick->no_file_data = 1;
3149 return;
3150 }
3151
0186c6a7 3152 lh_cu = this_cu;
7b9f3c50 3153 slot = NULL;
dee91e82 3154
fff8551c 3155 line_header_up lh;
9c541725 3156 sect_offset line_offset {};
fff8551c 3157
dee91e82 3158 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3159 if (attr != nullptr)
9291a0cd 3160 {
7b9f3c50
DE
3161 struct quick_file_names find_entry;
3162
9c541725 3163 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3164
3165 /* We may have already read in this line header (TU line header sharing).
3166 If we have we're done. */
094b34ac 3167 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3168 find_entry.hash.line_sect_off = line_offset;
5989a64e 3169 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3170 &find_entry, INSERT);
3171 if (*slot != NULL)
3172 {
9a3c8263 3173 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3174 return;
7b9f3c50
DE
3175 }
3176
3019eac3 3177 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3178 }
3179 if (lh == NULL)
3180 {
094b34ac 3181 lh_cu->v.quick->no_file_data = 1;
dee91e82 3182 return;
9291a0cd
TT
3183 }
3184
5989a64e 3185 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3186 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3187 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3188 gdb_assert (slot != NULL);
3189 *slot = qfn;
9291a0cd 3190
d721ba37 3191 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3192
aa391654
TT
3193 int offset = 0;
3194 if (strcmp (fnd.name, "<unknown>") != 0)
3195 ++offset;
3196
7ba99d21 3197 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3198 qfn->file_names =
5989a64e 3199 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3200 qfn->num_file_names);
aa391654
TT
3201 if (offset != 0)
3202 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3203 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3204 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3205 fnd.comp_dir).release ();
7b9f3c50 3206 qfn->real_names = NULL;
9291a0cd 3207
094b34ac 3208 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3209}
3210
3211/* A helper for the "quick" functions which attempts to read the line
3212 table for THIS_CU. */
3213
3214static struct quick_file_names *
e4a48d9d 3215dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3216{
0186c6a7
DE
3217 /* This should never be called for TUs. */
3218 gdb_assert (! this_cu->is_debug_types);
3219 /* Nor type unit groups. */
197400e8 3220 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3221
dee91e82
DE
3222 if (this_cu->v.quick->file_names != NULL)
3223 return this_cu->v.quick->file_names;
3224 /* If we know there is no line data, no point in looking again. */
3225 if (this_cu->v.quick->no_file_data)
3226 return NULL;
3227
c0ab21c2
TT
3228 cutu_reader reader (this_cu);
3229 if (!reader.dummy_p)
3e225074 3230 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3231
3232 if (this_cu->v.quick->no_file_data)
3233 return NULL;
3234 return this_cu->v.quick->file_names;
9291a0cd
TT
3235}
3236
3237/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3238 real path for a given file name from the line table. */
2fdf6df6 3239
9291a0cd 3240static const char *
45940949 3241dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3242 struct quick_file_names *qfn, int index)
9291a0cd 3243{
7b9f3c50 3244 if (qfn->real_names == NULL)
5989a64e 3245 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3246 qfn->num_file_names, const char *);
9291a0cd 3247
7b9f3c50 3248 if (qfn->real_names[index] == NULL)
14278e1f 3249 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3250
7b9f3c50 3251 return qfn->real_names[index];
9291a0cd
TT
3252}
3253
3254static struct symtab *
3255dw2_find_last_source_symtab (struct objfile *objfile)
3256{
ed2dc618
SM
3257 struct dwarf2_per_objfile *dwarf2_per_objfile
3258 = get_dwarf2_per_objfile (objfile);
5989a64e 3259 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3260 compunit_symtab *cust
3261 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3262
43f3e411
DE
3263 if (cust == NULL)
3264 return NULL;
ed2dc618 3265
43f3e411 3266 return compunit_primary_filetab (cust);
9291a0cd
TT
3267}
3268
7b9f3c50
DE
3269/* Traversal function for dw2_forget_cached_source_info. */
3270
3271static int
3272dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3273{
7b9f3c50 3274 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3275
7b9f3c50 3276 if (file_data->real_names)
9291a0cd 3277 {
7b9f3c50 3278 int i;
9291a0cd 3279
7b9f3c50 3280 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3281 {
7b9f3c50
DE
3282 xfree ((void*) file_data->real_names[i]);
3283 file_data->real_names[i] = NULL;
9291a0cd
TT
3284 }
3285 }
7b9f3c50
DE
3286
3287 return 1;
3288}
3289
3290static void
3291dw2_forget_cached_source_info (struct objfile *objfile)
3292{
ed2dc618
SM
3293 struct dwarf2_per_objfile *dwarf2_per_objfile
3294 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3295
5989a64e 3296 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3297 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3298}
3299
f8eba3c6
TT
3300/* Helper function for dw2_map_symtabs_matching_filename that expands
3301 the symtabs and calls the iterator. */
3302
3303static int
3304dw2_map_expand_apply (struct objfile *objfile,
3305 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3306 const char *name, const char *real_path,
14bc53a8 3307 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3308{
43f3e411 3309 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3310
3311 /* Don't visit already-expanded CUs. */
af758d11
SM
3312 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3313 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3314 return 0;
3315
3316 /* This may expand more than one symtab, and we want to iterate over
3317 all of them. */
97a1449a 3318 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3319
14bc53a8
PA
3320 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3321 last_made, callback);
f8eba3c6
TT
3322}
3323
3324/* Implementation of the map_symtabs_matching_filename method. */
3325
14bc53a8
PA
3326static bool
3327dw2_map_symtabs_matching_filename
3328 (struct objfile *objfile, const char *name, const char *real_path,
3329 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3330{
c011a4f4 3331 const char *name_basename = lbasename (name);
ed2dc618
SM
3332 struct dwarf2_per_objfile *dwarf2_per_objfile
3333 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3334
848e3e78
DE
3335 /* The rule is CUs specify all the files, including those used by
3336 any TU, so there's no need to scan TUs here. */
f4dc4d17 3337
5989a64e 3338 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3339 {
3d7bb9d9 3340 /* We only need to look at symtabs not already expanded. */
af758d11 3341 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3342 continue;
3343
b76e467d 3344 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3345 if (file_data == NULL)
9291a0cd
TT
3346 continue;
3347
b76e467d 3348 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3349 {
7b9f3c50 3350 const char *this_name = file_data->file_names[j];
da235a7c 3351 const char *this_real_name;
9291a0cd 3352
af529f8f 3353 if (compare_filenames_for_search (this_name, name))
9291a0cd 3354 {
f5b95b50 3355 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3356 callback))
3357 return true;
288e77a7 3358 continue;
4aac40c8 3359 }
9291a0cd 3360
c011a4f4
DE
3361 /* Before we invoke realpath, which can get expensive when many
3362 files are involved, do a quick comparison of the basenames. */
3363 if (! basenames_may_differ
3364 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3365 continue;
3366
45940949
TT
3367 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3368 file_data, j);
da235a7c 3369 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3370 {
da235a7c 3371 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3372 callback))
3373 return true;
288e77a7 3374 continue;
da235a7c 3375 }
9291a0cd 3376
da235a7c
JK
3377 if (real_path != NULL)
3378 {
af529f8f
JK
3379 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3380 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3381 if (this_real_name != NULL
af529f8f 3382 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3383 {
f5b95b50 3384 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3385 callback))
3386 return true;
288e77a7 3387 continue;
9291a0cd
TT
3388 }
3389 }
3390 }
3391 }
3392
14bc53a8 3393 return false;
9291a0cd
TT
3394}
3395
da51c347
DE
3396/* Struct used to manage iterating over all CUs looking for a symbol. */
3397
3398struct dw2_symtab_iterator
9291a0cd 3399{
ed2dc618
SM
3400 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3401 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3402 /* If set, only look for symbols that match that block. Valid values are
3403 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3404 gdb::optional<block_enum> block_index;
da51c347
DE
3405 /* The kind of symbol we're looking for. */
3406 domain_enum domain;
3407 /* The list of CUs from the index entry of the symbol,
3408 or NULL if not found. */
3409 offset_type *vec;
3410 /* The next element in VEC to look at. */
3411 int next;
3412 /* The number of elements in VEC, or zero if there is no match. */
3413 int length;
8943b874
DE
3414 /* Have we seen a global version of the symbol?
3415 If so we can ignore all further global instances.
3416 This is to work around gold/15646, inefficient gold-generated
3417 indices. */
3418 int global_seen;
da51c347 3419};
9291a0cd 3420
2b79f376 3421/* Initialize the index symtab iterator ITER. */
2fdf6df6 3422
9291a0cd 3423static void
da51c347 3424dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3425 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3426 gdb::optional<block_enum> block_index,
da51c347
DE
3427 domain_enum domain,
3428 const char *name)
3429{
ed2dc618 3430 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3431 iter->block_index = block_index;
3432 iter->domain = domain;
3433 iter->next = 0;
8943b874 3434 iter->global_seen = 0;
da51c347 3435
5989a64e 3436 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3437
3438 /* index is NULL if OBJF_READNOW. */
3439 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3440 iter->length = MAYBE_SWAP (*iter->vec);
3441 else
3442 {
3443 iter->vec = NULL;
3444 iter->length = 0;
3445 }
3446}
3447
3448/* Return the next matching CU or NULL if there are no more. */
3449
3450static struct dwarf2_per_cu_data *
3451dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3452{
ed2dc618
SM
3453 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3454
da51c347
DE
3455 for ( ; iter->next < iter->length; ++iter->next)
3456 {
3457 offset_type cu_index_and_attrs =
3458 MAYBE_SWAP (iter->vec[iter->next + 1]);
3459 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3460 gdb_index_symbol_kind symbol_kind =
3461 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3462 /* Only check the symbol attributes if they're present.
3463 Indices prior to version 7 don't record them,
3464 and indices >= 7 may elide them for certain symbols
3465 (gold does this). */
3466 int attrs_valid =
5989a64e 3467 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3468 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3469
3190f0c6 3470 /* Don't crash on bad data. */
5989a64e
SM
3471 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3472 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3473 {
b98664d3 3474 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3475 " [in module %s]"),
3476 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3477 continue;
3478 }
3479
5989a64e 3480 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3481
da51c347 3482 /* Skip if already read in. */
af758d11 3483 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3484 continue;
3485
8943b874
DE
3486 /* Check static vs global. */
3487 if (attrs_valid)
3488 {
2b79f376
SM
3489 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3490
3491 if (iter->block_index.has_value ())
3492 {
3493 bool want_static = *iter->block_index == STATIC_BLOCK;
3494
3495 if (is_static != want_static)
3496 continue;
3497 }
3498
8943b874
DE
3499 /* Work around gold/15646. */
3500 if (!is_static && iter->global_seen)
3501 continue;
3502 if (!is_static)
3503 iter->global_seen = 1;
3504 }
da51c347
DE
3505
3506 /* Only check the symbol's kind if it has one. */
3507 if (attrs_valid)
3508 {
3509 switch (iter->domain)
3510 {
3511 case VAR_DOMAIN:
3512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3513 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3514 /* Some types are also in VAR_DOMAIN. */
3515 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3516 continue;
3517 break;
3518 case STRUCT_DOMAIN:
3519 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3520 continue;
3521 break;
3522 case LABEL_DOMAIN:
3523 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3524 continue;
3525 break;
59c35742
AB
3526 case MODULE_DOMAIN:
3527 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3528 continue;
3529 break;
da51c347
DE
3530 default:
3531 break;
3532 }
3533 }
3534
3535 ++iter->next;
3536 return per_cu;
3537 }
3538
3539 return NULL;
3540}
3541
43f3e411 3542static struct compunit_symtab *
c7f839cb 3543dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3544 const char *name, domain_enum domain)
9291a0cd 3545{
43f3e411 3546 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3547 struct dwarf2_per_objfile *dwarf2_per_objfile
3548 = get_dwarf2_per_objfile (objfile);
9291a0cd 3549
b5ec771e
PA
3550 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3551
ed2dc618
SM
3552 struct dw2_symtab_iterator iter;
3553 struct dwarf2_per_cu_data *per_cu;
da51c347 3554
2b79f376 3555 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3556
ed2dc618
SM
3557 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3558 {
3559 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3560 struct compunit_symtab *stab
3561 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3562 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3563 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3564
ed2dc618
SM
3565 sym = block_find_symbol (block, name, domain,
3566 block_find_non_opaque_type_preferred,
3567 &with_opaque);
b2e2f908 3568
ed2dc618
SM
3569 /* Some caution must be observed with overloaded functions
3570 and methods, since the index will not contain any overload
3571 information (but NAME might contain it). */
da51c347 3572
ed2dc618
SM
3573 if (sym != NULL
3574 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3575 return stab;
3576 if (with_opaque != NULL
3577 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3578 stab_best = stab;
da51c347 3579
ed2dc618 3580 /* Keep looking through other CUs. */
9291a0cd 3581 }
9291a0cd 3582
da51c347 3583 return stab_best;
9291a0cd
TT
3584}
3585
3586static void
3587dw2_print_stats (struct objfile *objfile)
3588{
ed2dc618
SM
3589 struct dwarf2_per_objfile *dwarf2_per_objfile
3590 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3591 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3592 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3593 int count = 0;
9291a0cd 3594
ed2dc618 3595 for (int i = 0; i < total; ++i)
9291a0cd 3596 {
5989a64e 3597 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3598
af758d11 3599 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3600 ++count;
3601 }
e4a48d9d 3602 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3603 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3604}
3605
779bd270
DE
3606/* This dumps minimal information about the index.
3607 It is called via "mt print objfiles".
3608 One use is to verify .gdb_index has been loaded by the
3609 gdb.dwarf2/gdb-index.exp testcase. */
3610
9291a0cd
TT
3611static void
3612dw2_dump (struct objfile *objfile)
3613{
ed2dc618
SM
3614 struct dwarf2_per_objfile *dwarf2_per_objfile
3615 = get_dwarf2_per_objfile (objfile);
3616
5989a64e 3617 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3618 printf_filtered (".gdb_index:");
5989a64e 3619 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3620 {
3621 printf_filtered (" version %d\n",
5989a64e 3622 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3623 }
3624 else
3625 printf_filtered (" faked for \"readnow\"\n");
3626 printf_filtered ("\n");
9291a0cd
TT
3627}
3628
9291a0cd
TT
3629static void
3630dw2_expand_symtabs_for_function (struct objfile *objfile,
3631 const char *func_name)
3632{
ed2dc618
SM
3633 struct dwarf2_per_objfile *dwarf2_per_objfile
3634 = get_dwarf2_per_objfile (objfile);
da51c347 3635
ed2dc618
SM
3636 struct dw2_symtab_iterator iter;
3637 struct dwarf2_per_cu_data *per_cu;
da51c347 3638
2b79f376 3639 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3640
ed2dc618 3641 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3642 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3643
9291a0cd
TT
3644}
3645
3646static void
3647dw2_expand_all_symtabs (struct objfile *objfile)
3648{
ed2dc618
SM
3649 struct dwarf2_per_objfile *dwarf2_per_objfile
3650 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3651 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3652 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3653
ed2dc618 3654 for (int i = 0; i < total_units; ++i)
9291a0cd 3655 {
5989a64e 3656 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3657
58f0c718
TT
3658 /* We don't want to directly expand a partial CU, because if we
3659 read it with the wrong language, then assertion failures can
3660 be triggered later on. See PR symtab/23010. So, tell
3661 dw2_instantiate_symtab to skip partial CUs -- any important
3662 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3663 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3664 }
3665}
3666
3667static void
652a8996
JK
3668dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3669 const char *fullname)
9291a0cd 3670{
ed2dc618
SM
3671 struct dwarf2_per_objfile *dwarf2_per_objfile
3672 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3673
3674 /* We don't need to consider type units here.
3675 This is only called for examining code, e.g. expand_line_sal.
3676 There can be an order of magnitude (or more) more type units
3677 than comp units, and we avoid them if we can. */
3678
5989a64e 3679 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3680 {
3d7bb9d9 3681 /* We only need to look at symtabs not already expanded. */
af758d11 3682 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3683 continue;
3684
b76e467d 3685 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3686 if (file_data == NULL)
9291a0cd
TT
3687 continue;
3688
b76e467d 3689 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3690 {
652a8996
JK
3691 const char *this_fullname = file_data->file_names[j];
3692
3693 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3694 {
97a1449a 3695 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3696 break;
3697 }
3698 }
3699 }
3700}
3701
9a0bacfb
TV
3702static void
3703dw2_expand_symtabs_matching_symbol
3704 (mapped_index_base &index,
3705 const lookup_name_info &lookup_name_in,
3706 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3707 enum search_domain kind,
3708 gdb::function_view<bool (offset_type)> match_callback);
3709
3710static void
3711dw2_expand_symtabs_matching_one
97a1449a
SM
3712 (dwarf2_per_cu_data *per_cu,
3713 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3714 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3715 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3716
9291a0cd 3717static void
199b4314
TT
3718dw2_map_matching_symbols
3719 (struct objfile *objfile,
b054970d 3720 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3721 int global,
3722 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3723 symbol_compare_ftype *ordered_compare)
9291a0cd 3724{
1aa98955
TV
3725 /* Used for Ada. */
3726 struct dwarf2_per_objfile *dwarf2_per_objfile
3727 = get_dwarf2_per_objfile (objfile);
3728
9a0bacfb
TV
3729 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3730
5989a64e 3731 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3732 {
3733 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3734 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3735 using GNU index. */
5989a64e 3736 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3737
9a0bacfb
TV
3738 const char *match_name = name.ada ().lookup_name ().c_str ();
3739 auto matcher = [&] (const char *symname)
3740 {
3741 if (ordered_compare == nullptr)
3742 return true;
3743 return ordered_compare (symname, match_name) == 0;
3744 };
3745
3746 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3747 [&] (offset_type namei)
3748 {
3749 struct dw2_symtab_iterator iter;
3750 struct dwarf2_per_cu_data *per_cu;
3751
3752 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3753 match_name);
3754 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3755 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3756 nullptr);
9a0bacfb
TV
3757 return true;
3758 });
3759 }
3760 else
3761 {
3762 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3763 proceed assuming all symtabs have been read in. */
3764 }
1aa98955
TV
3765
3766 for (compunit_symtab *cust : objfile->compunits ())
3767 {
3768 const struct block *block;
3769
3770 if (cust == NULL)
3771 continue;
3772 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3773 if (!iterate_over_symbols_terminated (block, name,
3774 domain, callback))
3775 return;
3776 }
9291a0cd
TT
3777}
3778
e1ef7d7a
PA
3779/* Starting from a search name, return the string that finds the upper
3780 bound of all strings that start with SEARCH_NAME in a sorted name
3781 list. Returns the empty string to indicate that the upper bound is
3782 the end of the list. */
3783
3784static std::string
3785make_sort_after_prefix_name (const char *search_name)
3786{
3787 /* When looking to complete "func", we find the upper bound of all
3788 symbols that start with "func" by looking for where we'd insert
3789 the closest string that would follow "func" in lexicographical
3790 order. Usually, that's "func"-with-last-character-incremented,
3791 i.e. "fund". Mind non-ASCII characters, though. Usually those
3792 will be UTF-8 multi-byte sequences, but we can't be certain.
3793 Especially mind the 0xff character, which is a valid character in
3794 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3795 rule out compilers allowing it in identifiers. Note that
3796 conveniently, strcmp/strcasecmp are specified to compare
3797 characters interpreted as unsigned char. So what we do is treat
3798 the whole string as a base 256 number composed of a sequence of
3799 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3800 to 0, and carries 1 to the following more-significant position.
3801 If the very first character in SEARCH_NAME ends up incremented
3802 and carries/overflows, then the upper bound is the end of the
3803 list. The string after the empty string is also the empty
3804 string.
3805
3806 Some examples of this operation:
3807
3808 SEARCH_NAME => "+1" RESULT
3809
3810 "abc" => "abd"
3811 "ab\xff" => "ac"
3812 "\xff" "a" "\xff" => "\xff" "b"
3813 "\xff" => ""
3814 "\xff\xff" => ""
3815 "" => ""
3816
3817 Then, with these symbols for example:
3818
3819 func
3820 func1
3821 fund
3822
3823 completing "func" looks for symbols between "func" and
3824 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3825 which finds "func" and "func1", but not "fund".
3826
3827 And with:
3828
3829 funcÿ (Latin1 'ÿ' [0xff])
3830 funcÿ1
3831 fund
3832
3833 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3834 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3835
3836 And with:
3837
3838 ÿÿ (Latin1 'ÿ' [0xff])
3839 ÿÿ1
3840
3841 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3842 the end of the list.
3843 */
3844 std::string after = search_name;
3845 while (!after.empty () && (unsigned char) after.back () == 0xff)
3846 after.pop_back ();
3847 if (!after.empty ())
3848 after.back () = (unsigned char) after.back () + 1;
3849 return after;
3850}
3851
5c58de74 3852/* See declaration. */
61d96d7e 3853
5c58de74
PA
3854std::pair<std::vector<name_component>::const_iterator,
3855 std::vector<name_component>::const_iterator>
44ed8f3e 3856mapped_index_base::find_name_components_bounds
3b00ef10 3857 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3858{
5c58de74
PA
3859 auto *name_cmp
3860 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3861
3b00ef10 3862 const char *lang_name
e0802d59 3863 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3864
3f563c84
PA
3865 /* Comparison function object for lower_bound that matches against a
3866 given symbol name. */
3867 auto lookup_compare_lower = [&] (const name_component &elem,
3868 const char *name)
3869 {
5c58de74 3870 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3871 const char *elem_name = elem_qualified + elem.name_offset;
3872 return name_cmp (elem_name, name) < 0;
3873 };
3874
3875 /* Comparison function object for upper_bound that matches against a
3876 given symbol name. */
3877 auto lookup_compare_upper = [&] (const char *name,
3878 const name_component &elem)
3879 {
5c58de74 3880 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3881 const char *elem_name = elem_qualified + elem.name_offset;
3882 return name_cmp (name, elem_name) < 0;
3883 };
3884
5c58de74
PA
3885 auto begin = this->name_components.begin ();
3886 auto end = this->name_components.end ();
3f563c84
PA
3887
3888 /* Find the lower bound. */
3889 auto lower = [&] ()
3890 {
3b00ef10 3891 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3892 return begin;
3893 else
3b00ef10 3894 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3895 } ();
3896
3897 /* Find the upper bound. */
3898 auto upper = [&] ()
3899 {
5c58de74 3900 if (lookup_name_without_params.completion_mode ())
3f563c84 3901 {
e1ef7d7a
PA
3902 /* In completion mode, we want UPPER to point past all
3903 symbols names that have the same prefix. I.e., with
3904 these symbols, and completing "func":
3905
3906 function << lower bound
3907 function1
3908 other_function << upper bound
3909
3910 We find the upper bound by looking for the insertion
3911 point of "func"-with-last-character-incremented,
3912 i.e. "fund". */
3b00ef10 3913 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3914 if (after.empty ())
3f563c84 3915 return end;
e6b2f5ef
PA
3916 return std::lower_bound (lower, end, after.c_str (),
3917 lookup_compare_lower);
3f563c84
PA
3918 }
3919 else
3b00ef10 3920 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3921 } ();
3922
5c58de74
PA
3923 return {lower, upper};
3924}
3925
3926/* See declaration. */
3927
3928void
44ed8f3e 3929mapped_index_base::build_name_components ()
5c58de74
PA
3930{
3931 if (!this->name_components.empty ())
3932 return;
3933
3934 this->name_components_casing = case_sensitivity;
3935 auto *name_cmp
3936 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3937
3938 /* The code below only knows how to break apart components of C++
3939 symbol names (and other languages that use '::' as
3b00ef10 3940 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3941 auto count = this->symbol_name_count ();
3942 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3943 {
44ed8f3e 3944 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3945 continue;
3946
3947 const char *name = this->symbol_name_at (idx);
3948
3949 /* Add each name component to the name component table. */
3950 unsigned int previous_len = 0;
3b00ef10
TT
3951
3952 if (strstr (name, "::") != nullptr)
3953 {
3954 for (unsigned int current_len = cp_find_first_component (name);
3955 name[current_len] != '\0';
3956 current_len += cp_find_first_component (name + current_len))
3957 {
3958 gdb_assert (name[current_len] == ':');
3959 this->name_components.push_back ({previous_len, idx});
3960 /* Skip the '::'. */
3961 current_len += 2;
3962 previous_len = current_len;
3963 }
3964 }
3965 else
5c58de74 3966 {
3b00ef10
TT
3967 /* Handle the Ada encoded (aka mangled) form here. */
3968 for (const char *iter = strstr (name, "__");
3969 iter != nullptr;
3970 iter = strstr (iter, "__"))
3971 {
3972 this->name_components.push_back ({previous_len, idx});
3973 iter += 2;
3974 previous_len = iter - name;
3975 }
5c58de74 3976 }
3b00ef10 3977
5c58de74
PA
3978 this->name_components.push_back ({previous_len, idx});
3979 }
3980
3981 /* Sort name_components elements by name. */
3982 auto name_comp_compare = [&] (const name_component &left,
3983 const name_component &right)
3984 {
3985 const char *left_qualified = this->symbol_name_at (left.idx);
3986 const char *right_qualified = this->symbol_name_at (right.idx);
3987
3988 const char *left_name = left_qualified + left.name_offset;
3989 const char *right_name = right_qualified + right.name_offset;
3990
3991 return name_cmp (left_name, right_name) < 0;
3992 };
3993
3994 std::sort (this->name_components.begin (),
3995 this->name_components.end (),
3996 name_comp_compare);
3997}
3998
3999/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4000 mapped_index_base instead of the containing objfile. This is split
4001 to a separate function in order to be able to unit test the
4002 name_components matching using a mock mapped_index_base. For each
5c58de74 4003 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4004 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4005
4006static void
4007dw2_expand_symtabs_matching_symbol
44ed8f3e 4008 (mapped_index_base &index,
5c58de74
PA
4009 const lookup_name_info &lookup_name_in,
4010 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4011 enum search_domain kind,
3b00ef10 4012 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4013{
4014 lookup_name_info lookup_name_without_params
4015 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4016
4017 /* Build the symbol name component sorted vector, if we haven't
4018 yet. */
4019 index.build_name_components ();
4020
3f563c84
PA
4021 /* The same symbol may appear more than once in the range though.
4022 E.g., if we're looking for symbols that complete "w", and we have
4023 a symbol named "w1::w2", we'll find the two name components for
4024 that same symbol in the range. To be sure we only call the
4025 callback once per symbol, we first collect the symbol name
4026 indexes that matched in a temporary vector and ignore
4027 duplicates. */
4028 std::vector<offset_type> matches;
3f563c84 4029
3b00ef10
TT
4030 struct name_and_matcher
4031 {
4032 symbol_name_matcher_ftype *matcher;
ecc6c606 4033 const char *name;
3b00ef10
TT
4034
4035 bool operator== (const name_and_matcher &other) const
3f563c84 4036 {
ecc6c606 4037 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4038 }
4039 };
4040
4041 /* A vector holding all the different symbol name matchers, for all
4042 languages. */
4043 std::vector<name_and_matcher> matchers;
4044
4045 for (int i = 0; i < nr_languages; i++)
4046 {
4047 enum language lang_e = (enum language) i;
4048
4049 const language_defn *lang = language_def (lang_e);
4050 symbol_name_matcher_ftype *name_matcher
4051 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4052
3b00ef10
TT
4053 name_and_matcher key {
4054 name_matcher,
4055 lookup_name_without_params.language_lookup_name (lang_e)
4056 };
4057
4058 /* Don't insert the same comparison routine more than once.
4059 Note that we do this linear walk. This is not a problem in
4060 practice because the number of supported languages is
4061 low. */
4062 if (std::find (matchers.begin (), matchers.end (), key)
4063 != matchers.end ())
9291a0cd 4064 continue;
3b00ef10
TT
4065 matchers.push_back (std::move (key));
4066
4067 auto bounds
4068 = index.find_name_components_bounds (lookup_name_without_params,
4069 lang_e);
4070
4071 /* Now for each symbol name in range, check to see if we have a name
4072 match, and if so, call the MATCH_CALLBACK callback. */
4073
4074 for (; bounds.first != bounds.second; ++bounds.first)
4075 {
4076 const char *qualified = index.symbol_name_at (bounds.first->idx);
4077
4078 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4079 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4080 continue;
9291a0cd 4081
3b00ef10
TT
4082 matches.push_back (bounds.first->idx);
4083 }
3f563c84
PA
4084 }
4085
4086 std::sort (matches.begin (), matches.end ());
4087
4088 /* Finally call the callback, once per match. */
4089 ULONGEST prev = -1;
4090 for (offset_type idx : matches)
4091 {
4092 if (prev != idx)
4093 {
3b00ef10
TT
4094 if (!match_callback (idx))
4095 break;
3f563c84
PA
4096 prev = idx;
4097 }
4098 }
4099
4100 /* Above we use a type wider than idx's for 'prev', since 0 and
4101 (offset_type)-1 are both possible values. */
4102 static_assert (sizeof (prev) > sizeof (offset_type), "");
4103}
4104
c62446b1
PA
4105#if GDB_SELF_TEST
4106
4107namespace selftests { namespace dw2_expand_symtabs_matching {
4108
a3c5fafd
PA
4109/* A mock .gdb_index/.debug_names-like name index table, enough to
4110 exercise dw2_expand_symtabs_matching_symbol, which works with the
4111 mapped_index_base interface. Builds an index from the symbol list
4112 passed as parameter to the constructor. */
4113class mock_mapped_index : public mapped_index_base
c62446b1
PA
4114{
4115public:
a3c5fafd
PA
4116 mock_mapped_index (gdb::array_view<const char *> symbols)
4117 : m_symbol_table (symbols)
c62446b1
PA
4118 {}
4119
a3c5fafd 4120 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4121
a3c5fafd 4122 /* Return the number of names in the symbol table. */
632e107b 4123 size_t symbol_name_count () const override
c62446b1 4124 {
a3c5fafd 4125 return m_symbol_table.size ();
c62446b1
PA
4126 }
4127
a3c5fafd 4128 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4129 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4130 {
4131 return m_symbol_table[idx];
4132 }
c62446b1 4133
a3c5fafd
PA
4134private:
4135 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4136};
4137
4138/* Convenience function that converts a NULL pointer to a "<null>"
4139 string, to pass to print routines. */
4140
4141static const char *
4142string_or_null (const char *str)
4143{
4144 return str != NULL ? str : "<null>";
4145}
4146
4147/* Check if a lookup_name_info built from
4148 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4149 index. EXPECTED_LIST is the list of expected matches, in expected
4150 matching order. If no match expected, then an empty list is
4151 specified. Returns true on success. On failure prints a warning
4152 indicating the file:line that failed, and returns false. */
4153
4154static bool
4155check_match (const char *file, int line,
4156 mock_mapped_index &mock_index,
4157 const char *name, symbol_name_match_type match_type,
4158 bool completion_mode,
4159 std::initializer_list<const char *> expected_list)
4160{
4161 lookup_name_info lookup_name (name, match_type, completion_mode);
4162
4163 bool matched = true;
4164
4165 auto mismatch = [&] (const char *expected_str,
4166 const char *got)
4167 {
4168 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4169 "expected=\"%s\", got=\"%s\"\n"),
4170 file, line,
4171 (match_type == symbol_name_match_type::FULL
4172 ? "FULL" : "WILD"),
4173 name, string_or_null (expected_str), string_or_null (got));
4174 matched = false;
4175 };
4176
4177 auto expected_it = expected_list.begin ();
4178 auto expected_end = expected_list.end ();
4179
a3c5fafd 4180 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4181 NULL, ALL_DOMAIN,
4182 [&] (offset_type idx)
4183 {
a3c5fafd 4184 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4185 const char *expected_str
4186 = expected_it == expected_end ? NULL : *expected_it++;
4187
4188 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4189 mismatch (expected_str, matched_name);
3b00ef10 4190 return true;
c62446b1
PA
4191 });
4192
4193 const char *expected_str
4194 = expected_it == expected_end ? NULL : *expected_it++;
4195 if (expected_str != NULL)
4196 mismatch (expected_str, NULL);
4197
4198 return matched;
4199}
4200
4201/* The symbols added to the mock mapped_index for testing (in
4202 canonical form). */
4203static const char *test_symbols[] = {
4204 "function",
4205 "std::bar",
4206 "std::zfunction",
4207 "std::zfunction2",
4208 "w1::w2",
4209 "ns::foo<char*>",
4210 "ns::foo<int>",
4211 "ns::foo<long>",
a20714ff
PA
4212 "ns2::tmpl<int>::foo2",
4213 "(anonymous namespace)::A::B::C",
c62446b1 4214
e1ef7d7a
PA
4215 /* These are used to check that the increment-last-char in the
4216 matching algorithm for completion doesn't match "t1_fund" when
4217 completing "t1_func". */
4218 "t1_func",
4219 "t1_func1",
4220 "t1_fund",
4221 "t1_fund1",
4222
4223 /* A UTF-8 name with multi-byte sequences to make sure that
4224 cp-name-parser understands this as a single identifier ("função"
4225 is "function" in PT). */
4226 u8"u8função",
4227
4228 /* \377 (0xff) is Latin1 'ÿ'. */
4229 "yfunc\377",
4230
4231 /* \377 (0xff) is Latin1 'ÿ'. */
4232 "\377",
4233 "\377\377123",
4234
c62446b1
PA
4235 /* A name with all sorts of complications. Starts with "z" to make
4236 it easier for the completion tests below. */
4237#define Z_SYM_NAME \
4238 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4239 "::tuple<(anonymous namespace)::ui*, " \
4240 "std::default_delete<(anonymous namespace)::ui>, void>"
4241
4242 Z_SYM_NAME
4243};
4244
a3c5fafd
PA
4245/* Returns true if the mapped_index_base::find_name_component_bounds
4246 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4247 in completion mode. */
5c58de74
PA
4248
4249static bool
a3c5fafd 4250check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4251 const char *search_name,
4252 gdb::array_view<const char *> expected_syms)
4253{
4254 lookup_name_info lookup_name (search_name,
4255 symbol_name_match_type::FULL, true);
4256
3b00ef10
TT
4257 auto bounds = index.find_name_components_bounds (lookup_name,
4258 language_cplus);
5c58de74
PA
4259
4260 size_t distance = std::distance (bounds.first, bounds.second);
4261 if (distance != expected_syms.size ())
4262 return false;
4263
4264 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4265 {
4266 auto nc_elem = bounds.first + exp_elem;
4267 const char *qualified = index.symbol_name_at (nc_elem->idx);
4268 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4269 return false;
4270 }
4271
4272 return true;
4273}
4274
4275/* Test the lower-level mapped_index::find_name_component_bounds
4276 method. */
4277
c62446b1 4278static void
5c58de74
PA
4279test_mapped_index_find_name_component_bounds ()
4280{
4281 mock_mapped_index mock_index (test_symbols);
4282
a3c5fafd 4283 mock_index.build_name_components ();
5c58de74
PA
4284
4285 /* Test the lower-level mapped_index::find_name_component_bounds
4286 method in completion mode. */
4287 {
4288 static const char *expected_syms[] = {
4289 "t1_func",
4290 "t1_func1",
5c58de74
PA
4291 };
4292
a3c5fafd 4293 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4294 "t1_func", expected_syms));
4295 }
4296
4297 /* Check that the increment-last-char in the name matching algorithm
4298 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4299 {
4300 static const char *expected_syms1[] = {
4301 "\377",
4302 "\377\377123",
4303 };
a3c5fafd 4304 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4305 "\377", expected_syms1));
4306
4307 static const char *expected_syms2[] = {
4308 "\377\377123",
4309 };
a3c5fafd 4310 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4311 "\377\377", expected_syms2));
4312 }
4313}
4314
4315/* Test dw2_expand_symtabs_matching_symbol. */
4316
4317static void
4318test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4319{
4320 mock_mapped_index mock_index (test_symbols);
4321
4322 /* We let all tests run until the end even if some fails, for debug
4323 convenience. */
4324 bool any_mismatch = false;
4325
4326 /* Create the expected symbols list (an initializer_list). Needed
4327 because lists have commas, and we need to pass them to CHECK,
4328 which is a macro. */
4329#define EXPECT(...) { __VA_ARGS__ }
4330
4331 /* Wrapper for check_match that passes down the current
4332 __FILE__/__LINE__. */
4333#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4334 any_mismatch |= !check_match (__FILE__, __LINE__, \
4335 mock_index, \
4336 NAME, MATCH_TYPE, COMPLETION_MODE, \
4337 EXPECTED_LIST)
4338
4339 /* Identity checks. */
4340 for (const char *sym : test_symbols)
4341 {
4342 /* Should be able to match all existing symbols. */
4343 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4344 EXPECT (sym));
4345
4346 /* Should be able to match all existing symbols with
4347 parameters. */
4348 std::string with_params = std::string (sym) + "(int)";
4349 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4350 EXPECT (sym));
4351
4352 /* Should be able to match all existing symbols with
4353 parameters and qualifiers. */
4354 with_params = std::string (sym) + " ( int ) const";
4355 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4356 EXPECT (sym));
4357
4358 /* This should really find sym, but cp-name-parser.y doesn't
4359 know about lvalue/rvalue qualifiers yet. */
4360 with_params = std::string (sym) + " ( int ) &&";
4361 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4362 {});
4363 }
4364
e1ef7d7a
PA
4365 /* Check that the name matching algorithm for completion doesn't get
4366 confused with Latin1 'ÿ' / 0xff. */
4367 {
4368 static const char str[] = "\377";
4369 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4370 EXPECT ("\377", "\377\377123"));
4371 }
4372
4373 /* Check that the increment-last-char in the matching algorithm for
4374 completion doesn't match "t1_fund" when completing "t1_func". */
4375 {
4376 static const char str[] = "t1_func";
4377 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4378 EXPECT ("t1_func", "t1_func1"));
4379 }
4380
c62446b1
PA
4381 /* Check that completion mode works at each prefix of the expected
4382 symbol name. */
4383 {
4384 static const char str[] = "function(int)";
4385 size_t len = strlen (str);
4386 std::string lookup;
4387
4388 for (size_t i = 1; i < len; i++)
4389 {
4390 lookup.assign (str, i);
4391 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4392 EXPECT ("function"));
4393 }
4394 }
4395
4396 /* While "w" is a prefix of both components, the match function
4397 should still only be called once. */
4398 {
4399 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4400 EXPECT ("w1::w2"));
a20714ff
PA
4401 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4402 EXPECT ("w1::w2"));
c62446b1
PA
4403 }
4404
4405 /* Same, with a "complicated" symbol. */
4406 {
4407 static const char str[] = Z_SYM_NAME;
4408 size_t len = strlen (str);
4409 std::string lookup;
4410
4411 for (size_t i = 1; i < len; i++)
4412 {
4413 lookup.assign (str, i);
4414 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4415 EXPECT (Z_SYM_NAME));
4416 }
4417 }
4418
4419 /* In FULL mode, an incomplete symbol doesn't match. */
4420 {
4421 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4422 {});
4423 }
4424
4425 /* A complete symbol with parameters matches any overload, since the
4426 index has no overload info. */
4427 {
4428 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4429 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4430 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4431 EXPECT ("std::zfunction", "std::zfunction2"));
4432 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4433 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4434 }
4435
4436 /* Check that whitespace is ignored appropriately. A symbol with a
4437 template argument list. */
4438 {
4439 static const char expected[] = "ns::foo<int>";
4440 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4441 EXPECT (expected));
a20714ff
PA
4442 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4443 EXPECT (expected));
c62446b1
PA
4444 }
4445
4446 /* Check that whitespace is ignored appropriately. A symbol with a
4447 template argument list that includes a pointer. */
4448 {
4449 static const char expected[] = "ns::foo<char*>";
4450 /* Try both completion and non-completion modes. */
4451 static const bool completion_mode[2] = {false, true};
4452 for (size_t i = 0; i < 2; i++)
4453 {
4454 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4455 completion_mode[i], EXPECT (expected));
a20714ff
PA
4456 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4457 completion_mode[i], EXPECT (expected));
c62446b1
PA
4458
4459 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4460 completion_mode[i], EXPECT (expected));
a20714ff
PA
4461 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4462 completion_mode[i], EXPECT (expected));
c62446b1
PA
4463 }
4464 }
4465
4466 {
4467 /* Check method qualifiers are ignored. */
4468 static const char expected[] = "ns::foo<char*>";
4469 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4470 symbol_name_match_type::FULL, true, EXPECT (expected));
4471 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4472 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4473 CHECK_MATCH ("foo < char * > ( int ) const",
4474 symbol_name_match_type::WILD, true, EXPECT (expected));
4475 CHECK_MATCH ("foo < char * > ( int ) &&",
4476 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4477 }
4478
4479 /* Test lookup names that don't match anything. */
4480 {
a20714ff
PA
4481 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4482 {});
4483
c62446b1
PA
4484 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4485 {});
4486 }
4487
a20714ff
PA
4488 /* Some wild matching tests, exercising "(anonymous namespace)",
4489 which should not be confused with a parameter list. */
4490 {
4491 static const char *syms[] = {
4492 "A::B::C",
4493 "B::C",
4494 "C",
4495 "A :: B :: C ( int )",
4496 "B :: C ( int )",
4497 "C ( int )",
4498 };
4499
4500 for (const char *s : syms)
4501 {
4502 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4503 EXPECT ("(anonymous namespace)::A::B::C"));
4504 }
4505 }
4506
4507 {
4508 static const char expected[] = "ns2::tmpl<int>::foo2";
4509 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4510 EXPECT (expected));
4511 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4512 EXPECT (expected));
4513 }
4514
c62446b1
PA
4515 SELF_CHECK (!any_mismatch);
4516
4517#undef EXPECT
4518#undef CHECK_MATCH
4519}
4520
5c58de74
PA
4521static void
4522run_test ()
4523{
4524 test_mapped_index_find_name_component_bounds ();
4525 test_dw2_expand_symtabs_matching_symbol ();
4526}
4527
c62446b1
PA
4528}} // namespace selftests::dw2_expand_symtabs_matching
4529
4530#endif /* GDB_SELF_TEST */
4531
4b514bc8
JK
4532/* If FILE_MATCHER is NULL or if PER_CU has
4533 dwarf2_per_cu_quick_data::MARK set (see
4534 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4535 EXPANSION_NOTIFY on it. */
4536
4537static void
4538dw2_expand_symtabs_matching_one
97a1449a
SM
4539 (dwarf2_per_cu_data *per_cu,
4540 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4541 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4542 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4543{
4544 if (file_matcher == NULL || per_cu->v.quick->mark)
4545 {
af758d11 4546 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4547
97a1449a
SM
4548 compunit_symtab *symtab
4549 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4550 gdb_assert (symtab != nullptr);
4b514bc8 4551
af758d11
SM
4552 if (expansion_notify != NULL && symtab_was_null)
4553 expansion_notify (symtab);
4b514bc8
JK
4554 }
4555}
4556
3f563c84
PA
4557/* Helper for dw2_expand_matching symtabs. Called on each symbol
4558 matched, to expand corresponding CUs that were marked. IDX is the
4559 index of the symbol name that matched. */
4560
4561static void
4562dw2_expand_marked_cus
97a1449a 4563 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4564 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4565 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4566 search_domain kind)
4567{
3f563c84
PA
4568 offset_type *vec, vec_len, vec_idx;
4569 bool global_seen = false;
5989a64e 4570 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4571
61920122 4572 vec = (offset_type *) (index.constant_pool
f00a2de2 4573 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4574 vec_len = MAYBE_SWAP (vec[0]);
4575 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4576 {
61920122
PA
4577 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4578 /* This value is only valid for index versions >= 7. */
4579 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4580 gdb_index_symbol_kind symbol_kind =
4581 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4582 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4583 /* Only check the symbol attributes if they're present.
4584 Indices prior to version 7 don't record them,
4585 and indices >= 7 may elide them for certain symbols
4586 (gold does this). */
4587 int attrs_valid =
4588 (index.version >= 7
4589 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4590
4591 /* Work around gold/15646. */
4592 if (attrs_valid)
9291a0cd 4593 {
61920122
PA
4594 if (!is_static && global_seen)
4595 continue;
4596 if (!is_static)
4597 global_seen = true;
4598 }
3190f0c6 4599
61920122
PA
4600 /* Only check the symbol's kind if it has one. */
4601 if (attrs_valid)
4602 {
4603 switch (kind)
8943b874 4604 {
61920122
PA
4605 case VARIABLES_DOMAIN:
4606 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4607 continue;
4608 break;
4609 case FUNCTIONS_DOMAIN:
4610 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4611 continue;
61920122
PA
4612 break;
4613 case TYPES_DOMAIN:
4614 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4615 continue;
4616 break;
59c35742
AB
4617 case MODULES_DOMAIN:
4618 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4619 continue;
4620 break;
61920122
PA
4621 default:
4622 break;
8943b874 4623 }
61920122 4624 }
8943b874 4625
61920122 4626 /* Don't crash on bad data. */
5989a64e
SM
4627 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4628 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4629 {
b98664d3 4630 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4631 " [in module %s]"),
4632 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4633 continue;
4634 }
4635
5989a64e 4636 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4637 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4638 expansion_notify);
61920122
PA
4639 }
4640}
4641
4b514bc8
JK
4642/* If FILE_MATCHER is non-NULL, set all the
4643 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4644 that match FILE_MATCHER. */
4645
61920122 4646static void
4b514bc8 4647dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4648 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4649 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4650{
4b514bc8 4651 if (file_matcher == NULL)
61920122
PA
4652 return;
4653
4b514bc8
JK
4654 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4655 htab_eq_pointer,
4656 NULL, xcalloc, xfree));
4657 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4658 htab_eq_pointer,
4659 NULL, xcalloc, xfree));
61920122 4660
4b514bc8
JK
4661 /* The rule is CUs specify all the files, including those used by
4662 any TU, so there's no need to scan TUs here. */
61920122 4663
5989a64e 4664 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4665 {
927aa2e7
JK
4666 QUIT;
4667
4668 per_cu->v.quick->mark = 0;
4669
4670 /* We only need to look at symtabs not already expanded. */
af758d11 4671 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4672 continue;
4673
b76e467d 4674 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4675 if (file_data == NULL)
4676 continue;
4677
4678 if (htab_find (visited_not_found.get (), file_data) != NULL)
4679 continue;
4680 else if (htab_find (visited_found.get (), file_data) != NULL)
4681 {
4682 per_cu->v.quick->mark = 1;
4683 continue;
4684 }
4685
b76e467d 4686 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4687 {
4688 const char *this_real_name;
4689
4690 if (file_matcher (file_data->file_names[j], false))
4691 {
4692 per_cu->v.quick->mark = 1;
4693 break;
4694 }
4695
4696 /* Before we invoke realpath, which can get expensive when many
4697 files are involved, do a quick comparison of the basenames. */
4698 if (!basenames_may_differ
4699 && !file_matcher (lbasename (file_data->file_names[j]),
4700 true))
4701 continue;
4702
45940949
TT
4703 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4704 file_data, j);
927aa2e7
JK
4705 if (file_matcher (this_real_name, false))
4706 {
4707 per_cu->v.quick->mark = 1;
4708 break;
4709 }
4710 }
4711
b76e467d
SM
4712 void **slot = htab_find_slot (per_cu->v.quick->mark
4713 ? visited_found.get ()
4714 : visited_not_found.get (),
4715 file_data, INSERT);
927aa2e7
JK
4716 *slot = file_data;
4717 }
4718}
4719
4720static void
4721dw2_expand_symtabs_matching
4722 (struct objfile *objfile,
4723 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4724 const lookup_name_info *lookup_name,
927aa2e7
JK
4725 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4727 enum search_domain kind)
4728{
ed2dc618
SM
4729 struct dwarf2_per_objfile *dwarf2_per_objfile
4730 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4731
4732 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4733 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4734 return;
4735
ed2dc618 4736 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4737
c1a66c06
TV
4738 if (symbol_matcher == NULL && lookup_name == NULL)
4739 {
5989a64e 4740 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4741 {
4742 QUIT;
4743
97a1449a
SM
4744 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4745 file_matcher, expansion_notify);
c1a66c06
TV
4746 }
4747 return;
4748 }
4749
5989a64e 4750 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4751
c1a66c06 4752 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4753 symbol_matcher,
4754 kind, [&] (offset_type idx)
4755 {
ed2dc618 4756 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4757 expansion_notify, kind);
3b00ef10 4758 return true;
927aa2e7
JK
4759 });
4760}
4761
4762/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4763 symtab. */
4764
4765static struct compunit_symtab *
4766recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4767 CORE_ADDR pc)
4768{
4769 int i;
4770
4771 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4772 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4773 return cust;
4774
4775 if (cust->includes == NULL)
4776 return NULL;
4777
4778 for (i = 0; cust->includes[i]; ++i)
4779 {
4780 struct compunit_symtab *s = cust->includes[i];
4781
4782 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4783 if (s != NULL)
4784 return s;
4785 }
4786
4787 return NULL;
4788}
4789
4790static struct compunit_symtab *
4791dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4792 struct bound_minimal_symbol msymbol,
4793 CORE_ADDR pc,
4794 struct obj_section *section,
4795 int warn_if_readin)
4796{
4797 struct dwarf2_per_cu_data *data;
4798 struct compunit_symtab *result;
4799
d320c2b5 4800 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4801 return NULL;
4802
b3b3bada 4803 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4804 data = (struct dwarf2_per_cu_data *) addrmap_find
4805 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4806 if (!data)
4807 return NULL;
4808
af758d11
SM
4809 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4810 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4811 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4812 paddress (objfile->arch (), pc));
927aa2e7 4813
97a1449a
SM
4814 result = recursively_find_pc_sect_compunit_symtab
4815 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4816
927aa2e7
JK
4817 gdb_assert (result != NULL);
4818 return result;
4819}
4820
4821static void
4822dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4823 void *data, int need_fullname)
4824{
ed2dc618
SM
4825 struct dwarf2_per_objfile *dwarf2_per_objfile
4826 = get_dwarf2_per_objfile (objfile);
927aa2e7 4827
5989a64e 4828 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4829 {
5989a64e 4830 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4831
4832 htab_up visited (htab_create_alloc (10,
4833 htab_hash_pointer, htab_eq_pointer,
4834 NULL, xcalloc, xfree));
4835
4836 /* The rule is CUs specify all the files, including those used
4837 by any TU, so there's no need to scan TUs here. We can
4838 ignore file names coming from already-expanded CUs. */
4839
5989a64e 4840 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4841 {
af758d11 4842 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4843 {
4844 void **slot = htab_find_slot (visited.get (),
4845 per_cu->v.quick->file_names,
4846 INSERT);
4847
4848 *slot = per_cu->v.quick->file_names;
4849 }
4850 }
4851
5989a64e 4852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4853 {
927aa2e7 4854 /* We only need to look at symtabs not already expanded. */
af758d11 4855 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4856 continue;
4857
b76e467d 4858 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4859 if (file_data == NULL)
4860 continue;
4861
b76e467d 4862 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4863 if (*slot)
4864 {
4865 /* Already visited. */
4866 continue;
4867 }
4868 *slot = file_data;
4869
4870 for (int j = 0; j < file_data->num_file_names; ++j)
4871 {
4872 const char *filename = file_data->file_names[j];
5989a64e 4873 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4874 }
4875 }
4876 }
4877
5989a64e 4878 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4879 {
4880 gdb::unique_xmalloc_ptr<char> this_real_name;
4881
4882 if (need_fullname)
4883 this_real_name = gdb_realpath (filename);
4884 (*fun) (filename, this_real_name.get (), data);
4885 });
4886}
4887
4888static int
4889dw2_has_symbols (struct objfile *objfile)
4890{
4891 return 1;
4892}
4893
4894const struct quick_symbol_functions dwarf2_gdb_index_functions =
4895{
4896 dw2_has_symbols,
4897 dw2_find_last_source_symtab,
4898 dw2_forget_cached_source_info,
4899 dw2_map_symtabs_matching_filename,
4900 dw2_lookup_symbol,
d3214198 4901 NULL,
927aa2e7
JK
4902 dw2_print_stats,
4903 dw2_dump,
927aa2e7
JK
4904 dw2_expand_symtabs_for_function,
4905 dw2_expand_all_symtabs,
4906 dw2_expand_symtabs_with_fullname,
4907 dw2_map_matching_symbols,
4908 dw2_expand_symtabs_matching,
4909 dw2_find_pc_sect_compunit_symtab,
4910 NULL,
4911 dw2_map_symbol_filenames
4912};
4913
4914/* DWARF-5 debug_names reader. */
4915
4916/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4917static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4918
4919/* A helper function that reads the .debug_names section in SECTION
4920 and fills in MAP. FILENAME is the name of the file containing the
4921 section; it is used for error reporting.
4922
4923 Returns true if all went well, false otherwise. */
4924
4925static bool
4926read_debug_names_from_section (struct objfile *objfile,
4927 const char *filename,
4928 struct dwarf2_section_info *section,
4929 mapped_debug_names &map)
4930{
96b79293 4931 if (section->empty ())
927aa2e7
JK
4932 return false;
4933
4934 /* Older elfutils strip versions could keep the section in the main
4935 executable while splitting it for the separate debug info file. */
96b79293 4936 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4937 return false;
4938
96b79293 4939 section->read (objfile);
927aa2e7 4940
08feed99 4941 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4942
4943 const gdb_byte *addr = section->buffer;
4944
96b79293 4945 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4946
4947 unsigned int bytes_read;
4948 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4949 addr += bytes_read;
4950
4951 map.dwarf5_is_dwarf64 = bytes_read != 4;
4952 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4953 if (bytes_read + length != section->size)
4954 {
4955 /* There may be multiple per-CU indices. */
4956 warning (_("Section .debug_names in %s length %s does not match "
4957 "section length %s, ignoring .debug_names."),
4958 filename, plongest (bytes_read + length),
4959 pulongest (section->size));
4960 return false;
4961 }
4962
4963 /* The version number. */
4964 uint16_t version = read_2_bytes (abfd, addr);
4965 addr += 2;
4966 if (version != 5)
4967 {
4968 warning (_("Section .debug_names in %s has unsupported version %d, "
4969 "ignoring .debug_names."),
4970 filename, version);
4971 return false;
4972 }
4973
4974 /* Padding. */
4975 uint16_t padding = read_2_bytes (abfd, addr);
4976 addr += 2;
4977 if (padding != 0)
4978 {
4979 warning (_("Section .debug_names in %s has unsupported padding %d, "
4980 "ignoring .debug_names."),
4981 filename, padding);
4982 return false;
4983 }
4984
4985 /* comp_unit_count - The number of CUs in the CU list. */
4986 map.cu_count = read_4_bytes (abfd, addr);
4987 addr += 4;
4988
4989 /* local_type_unit_count - The number of TUs in the local TU
4990 list. */
4991 map.tu_count = read_4_bytes (abfd, addr);
4992 addr += 4;
4993
4994 /* foreign_type_unit_count - The number of TUs in the foreign TU
4995 list. */
4996 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4997 addr += 4;
4998 if (foreign_tu_count != 0)
4999 {
5000 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5001 "ignoring .debug_names."),
5002 filename, static_cast<unsigned long> (foreign_tu_count));
5003 return false;
5004 }
5005
5006 /* bucket_count - The number of hash buckets in the hash lookup
5007 table. */
5008 map.bucket_count = read_4_bytes (abfd, addr);
5009 addr += 4;
5010
5011 /* name_count - The number of unique names in the index. */
5012 map.name_count = read_4_bytes (abfd, addr);
5013 addr += 4;
5014
5015 /* abbrev_table_size - The size in bytes of the abbreviations
5016 table. */
5017 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5018 addr += 4;
5019
5020 /* augmentation_string_size - The size in bytes of the augmentation
5021 string. This value is rounded up to a multiple of 4. */
5022 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5023 addr += 4;
5024 map.augmentation_is_gdb = ((augmentation_string_size
5025 == sizeof (dwarf5_augmentation))
5026 && memcmp (addr, dwarf5_augmentation,
5027 sizeof (dwarf5_augmentation)) == 0);
5028 augmentation_string_size += (-augmentation_string_size) & 3;
5029 addr += augmentation_string_size;
5030
5031 /* List of CUs */
5032 map.cu_table_reordered = addr;
5033 addr += map.cu_count * map.offset_size;
5034
5035 /* List of Local TUs */
5036 map.tu_table_reordered = addr;
5037 addr += map.tu_count * map.offset_size;
5038
5039 /* Hash Lookup Table */
5040 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5041 addr += map.bucket_count * 4;
5042 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5043 addr += map.name_count * 4;
5044
5045 /* Name Table */
5046 map.name_table_string_offs_reordered = addr;
5047 addr += map.name_count * map.offset_size;
5048 map.name_table_entry_offs_reordered = addr;
5049 addr += map.name_count * map.offset_size;
5050
5051 const gdb_byte *abbrev_table_start = addr;
5052 for (;;)
5053 {
927aa2e7
JK
5054 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5055 addr += bytes_read;
5056 if (index_num == 0)
5057 break;
5058
5059 const auto insertpair
5060 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5061 if (!insertpair.second)
5062 {
5063 warning (_("Section .debug_names in %s has duplicate index %s, "
5064 "ignoring .debug_names."),
5065 filename, pulongest (index_num));
5066 return false;
5067 }
5068 mapped_debug_names::index_val &indexval = insertpair.first->second;
5069 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5070 addr += bytes_read;
5071
5072 for (;;)
5073 {
5074 mapped_debug_names::index_val::attr attr;
5075 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5076 addr += bytes_read;
5077 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5078 addr += bytes_read;
5079 if (attr.form == DW_FORM_implicit_const)
5080 {
5081 attr.implicit_const = read_signed_leb128 (abfd, addr,
5082 &bytes_read);
5083 addr += bytes_read;
5084 }
5085 if (attr.dw_idx == 0 && attr.form == 0)
5086 break;
5087 indexval.attr_vec.push_back (std::move (attr));
5088 }
5089 }
5090 if (addr != abbrev_table_start + abbrev_table_size)
5091 {
5092 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5093 "of size %s vs. written as %u, ignoring .debug_names."),
5094 filename, plongest (addr - abbrev_table_start),
5095 abbrev_table_size);
927aa2e7
JK
5096 return false;
5097 }
5098 map.entry_pool = addr;
5099
5100 return true;
5101}
5102
5103/* A helper for create_cus_from_debug_names that handles the MAP's CU
5104 list. */
5105
5106static void
ed2dc618 5107create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5108 const mapped_debug_names &map,
5109 dwarf2_section_info &section,
b76e467d 5110 bool is_dwz)
927aa2e7 5111{
3ee6bb11
TV
5112 if (!map.augmentation_is_gdb)
5113 {
5114 for (uint32_t i = 0; i < map.cu_count; ++i)
5115 {
5116 sect_offset sect_off
5117 = (sect_offset) (extract_unsigned_integer
5118 (map.cu_table_reordered + i * map.offset_size,
5119 map.offset_size,
5120 map.dwarf5_byte_order));
5121 /* We don't know the length of the CU, because the CU list in a
5122 .debug_names index can be incomplete, so we can't use the start of
5123 the next CU as end of this CU. We create the CUs here with length 0,
5124 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5125 dwarf2_per_cu_data *per_cu
5126 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5127 sect_off, 0);
5989a64e 5128 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5129 }
5130 }
5131
927aa2e7
JK
5132 sect_offset sect_off_prev;
5133 for (uint32_t i = 0; i <= map.cu_count; ++i)
5134 {
5135 sect_offset sect_off_next;
5136 if (i < map.cu_count)
5137 {
5138 sect_off_next
5139 = (sect_offset) (extract_unsigned_integer
5140 (map.cu_table_reordered + i * map.offset_size,
5141 map.offset_size,
5142 map.dwarf5_byte_order));
5143 }
5144 else
5145 sect_off_next = (sect_offset) section.size;
5146 if (i >= 1)
5147 {
5148 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5149 dwarf2_per_cu_data *per_cu
ed2dc618 5150 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5151 sect_off_prev, length);
5989a64e 5152 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5153 }
5154 sect_off_prev = sect_off_next;
5155 }
5156}
5157
5158/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5159 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5160
5161static void
ed2dc618 5162create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5163 const mapped_debug_names &map,
5164 const mapped_debug_names &dwz_map)
5165{
5989a64e
SM
5166 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5167 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5168
ed2dc618 5169 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5989a64e 5170 dwarf2_per_objfile->per_bfd->info,
b76e467d 5171 false /* is_dwz */);
927aa2e7
JK
5172
5173 if (dwz_map.cu_count == 0)
5174 return;
5175
c3699833 5176 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
ed2dc618 5177 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5178 true /* is_dwz */);
927aa2e7
JK
5179}
5180
5181/* Read .debug_names. If everything went ok, initialize the "quick"
5182 elements of all the CUs and return true. Otherwise, return false. */
5183
5184static bool
ed2dc618 5185dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5186{
22ca247e
TT
5187 std::unique_ptr<mapped_debug_names> map
5188 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5189 mapped_debug_names dwz_map (dwarf2_per_objfile);
5190 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5191
5192 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5193 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5194 *map))
927aa2e7
JK
5195 return false;
5196
5197 /* Don't use the index if it's empty. */
22ca247e 5198 if (map->name_count == 0)
927aa2e7
JK
5199 return false;
5200
5201 /* If there is a .dwz file, read it so we can get its CU list as
5202 well. */
c3699833 5203 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5204 if (dwz != NULL)
5205 {
5206 if (!read_debug_names_from_section (objfile,
00f93c44 5207 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5208 &dwz->debug_names, dwz_map))
5209 {
5210 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5211 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5212 return false;
5213 }
5214 }
5215
22ca247e 5216 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5217
22ca247e 5218 if (map->tu_count != 0)
927aa2e7
JK
5219 {
5220 /* We can only handle a single .debug_types when we have an
5221 index. */
5989a64e 5222 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5223 return false;
5224
5989a64e 5225 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5226
5227 create_signatured_type_table_from_debug_names
5989a64e 5228 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5229 }
5230
ed2dc618 5231 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5232 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5233
5989a64e
SM
5234 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5235 dwarf2_per_objfile->per_bfd->using_index = 1;
5236 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5237 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5238
5239 return true;
5240}
5241
927aa2e7
JK
5242/* Type used to manage iterating over all CUs looking for a symbol for
5243 .debug_names. */
5244
5245class dw2_debug_names_iterator
5246{
5247public:
927aa2e7 5248 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5249 gdb::optional<block_enum> block_index,
5250 domain_enum domain,
927aa2e7 5251 const char *name)
2b79f376 5252 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5253 m_addr (find_vec_in_debug_names (map, name))
5254 {}
5255
5256 dw2_debug_names_iterator (const mapped_debug_names &map,
5257 search_domain search, uint32_t namei)
5258 : m_map (map),
5259 m_search (search),
5260 m_addr (find_vec_in_debug_names (map, namei))
5261 {}
5262
3b00ef10
TT
5263 dw2_debug_names_iterator (const mapped_debug_names &map,
5264 block_enum block_index, domain_enum domain,
5265 uint32_t namei)
5266 : m_map (map), m_block_index (block_index), m_domain (domain),
5267 m_addr (find_vec_in_debug_names (map, namei))
5268 {}
5269
927aa2e7
JK
5270 /* Return the next matching CU or NULL if there are no more. */
5271 dwarf2_per_cu_data *next ();
5272
5273private:
5274 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5275 const char *name);
5276 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5277 uint32_t namei);
5278
5279 /* The internalized form of .debug_names. */
5280 const mapped_debug_names &m_map;
5281
2b79f376
SM
5282 /* If set, only look for symbols that match that block. Valid values are
5283 GLOBAL_BLOCK and STATIC_BLOCK. */
5284 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5285
5286 /* The kind of symbol we're looking for. */
5287 const domain_enum m_domain = UNDEF_DOMAIN;
5288 const search_domain m_search = ALL_DOMAIN;
5289
5290 /* The list of CUs from the index entry of the symbol, or NULL if
5291 not found. */
5292 const gdb_byte *m_addr;
5293};
5294
5295const char *
5296mapped_debug_names::namei_to_name (uint32_t namei) const
5297{
5298 const ULONGEST namei_string_offs
5299 = extract_unsigned_integer ((name_table_string_offs_reordered
5300 + namei * offset_size),
5301 offset_size,
5302 dwarf5_byte_order);
4f44ae6c
TT
5303 return read_indirect_string_at_offset (dwarf2_per_objfile,
5304 namei_string_offs);
927aa2e7
JK
5305}
5306
5307/* Find a slot in .debug_names for the object named NAME. If NAME is
5308 found, return pointer to its pool data. If NAME cannot be found,
5309 return NULL. */
5310
5311const gdb_byte *
5312dw2_debug_names_iterator::find_vec_in_debug_names
5313 (const mapped_debug_names &map, const char *name)
5314{
5315 int (*cmp) (const char *, const char *);
5316
54ee4252 5317 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5318 if (current_language->la_language == language_cplus
5319 || current_language->la_language == language_fortran
5320 || current_language->la_language == language_d)
5321 {
5322 /* NAME is already canonical. Drop any qualifiers as
5323 .debug_names does not contain any. */
5324
5325 if (strchr (name, '(') != NULL)
5326 {
54ee4252 5327 without_params = cp_remove_params (name);
927aa2e7 5328 if (without_params != NULL)
54ee4252 5329 name = without_params.get ();
927aa2e7
JK
5330 }
5331 }
5332
5333 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5334
5335 const uint32_t full_hash = dwarf5_djb_hash (name);
5336 uint32_t namei
5337 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5338 (map.bucket_table_reordered
5339 + (full_hash % map.bucket_count)), 4,
5340 map.dwarf5_byte_order);
5341 if (namei == 0)
5342 return NULL;
5343 --namei;
5344 if (namei >= map.name_count)
5345 {
b98664d3 5346 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5347 "[in module %s]"),
5348 namei, map.name_count,
ed2dc618 5349 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5350 return NULL;
5351 }
5352
5353 for (;;)
5354 {
5355 const uint32_t namei_full_hash
5356 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5357 (map.hash_table_reordered + namei), 4,
5358 map.dwarf5_byte_order);
5359 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5360 return NULL;
5361
5362 if (full_hash == namei_full_hash)
5363 {
5364 const char *const namei_string = map.namei_to_name (namei);
5365
5366#if 0 /* An expensive sanity check. */
5367 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5368 {
b98664d3 5369 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5370 "[in module %s]"),
5371 namei, objfile_name (dwarf2_per_objfile->objfile));
5372 return NULL;
5373 }
5374#endif
5375
5376 if (cmp (namei_string, name) == 0)
5377 {
5378 const ULONGEST namei_entry_offs
5379 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5380 + namei * map.offset_size),
5381 map.offset_size, map.dwarf5_byte_order);
5382 return map.entry_pool + namei_entry_offs;
5383 }
5384 }
5385
5386 ++namei;
5387 if (namei >= map.name_count)
5388 return NULL;
5389 }
5390}
5391
5392const gdb_byte *
5393dw2_debug_names_iterator::find_vec_in_debug_names
5394 (const mapped_debug_names &map, uint32_t namei)
5395{
5396 if (namei >= map.name_count)
5397 {
b98664d3 5398 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5399 "[in module %s]"),
5400 namei, map.name_count,
ed2dc618 5401 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5402 return NULL;
5403 }
5404
5405 const ULONGEST namei_entry_offs
5406 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5407 + namei * map.offset_size),
5408 map.offset_size, map.dwarf5_byte_order);
5409 return map.entry_pool + namei_entry_offs;
5410}
5411
5412/* See dw2_debug_names_iterator. */
5413
5414dwarf2_per_cu_data *
5415dw2_debug_names_iterator::next ()
5416{
5417 if (m_addr == NULL)
5418 return NULL;
5419
ed2dc618
SM
5420 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5421 struct objfile *objfile = dwarf2_per_objfile->objfile;
5422 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5423
5424 again:
5425
5426 unsigned int bytes_read;
5427 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5428 m_addr += bytes_read;
5429 if (abbrev == 0)
5430 return NULL;
5431
5432 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5433 if (indexval_it == m_map.abbrev_map.cend ())
5434 {
b98664d3 5435 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5436 "[in module %s]"),
ed2dc618 5437 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5438 return NULL;
5439 }
5440 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5441 enum class symbol_linkage {
5442 unknown,
5443 static_,
5444 extern_,
23c13d42 5445 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5446 dwarf2_per_cu_data *per_cu = NULL;
5447 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5448 {
5449 ULONGEST ull;
5450 switch (attr.form)
5451 {
5452 case DW_FORM_implicit_const:
5453 ull = attr.implicit_const;
5454 break;
5455 case DW_FORM_flag_present:
5456 ull = 1;
5457 break;
5458 case DW_FORM_udata:
5459 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5460 m_addr += bytes_read;
5461 break;
6dc55ce9 5462 case DW_FORM_ref4:
5463 ull = read_4_bytes (abfd, m_addr);
5464 m_addr += 4;
5465 break;
5466 case DW_FORM_ref8:
5467 ull = read_8_bytes (abfd, m_addr);
5468 m_addr += 8;
5469 break;
5470 case DW_FORM_ref_sig8:
5471 ull = read_8_bytes (abfd, m_addr);
5472 m_addr += 8;
5473 break;
927aa2e7 5474 default:
b98664d3 5475 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5476 dwarf_form_name (attr.form),
ed2dc618 5477 objfile_name (objfile));
927aa2e7
JK
5478 return NULL;
5479 }
5480 switch (attr.dw_idx)
5481 {
5482 case DW_IDX_compile_unit:
5483 /* Don't crash on bad data. */
5989a64e 5484 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5485 {
b98664d3 5486 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5487 " [in module %s]"),
5488 pulongest (ull),
5489 objfile_name (dwarf2_per_objfile->objfile));
5490 continue;
5491 }
5989a64e 5492 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5493 break;
8af5c486
JK
5494 case DW_IDX_type_unit:
5495 /* Don't crash on bad data. */
5989a64e 5496 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5497 {
b98664d3 5498 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5499 " [in module %s]"),
5500 pulongest (ull),
5501 objfile_name (dwarf2_per_objfile->objfile));
5502 continue;
5503 }
5989a64e 5504 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5505 break;
6dc55ce9 5506 case DW_IDX_die_offset:
5507 /* In a per-CU index (as opposed to a per-module index), index
5508 entries without CU attribute implicitly refer to the single CU. */
5509 if (per_cu == NULL)
5989a64e 5510 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5511 break;
927aa2e7
JK
5512 case DW_IDX_GNU_internal:
5513 if (!m_map.augmentation_is_gdb)
5514 break;
23c13d42 5515 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5516 break;
5517 case DW_IDX_GNU_external:
5518 if (!m_map.augmentation_is_gdb)
5519 break;
23c13d42 5520 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5521 break;
5522 }
5523 }
5524
5525 /* Skip if already read in. */
af758d11 5526 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5527 goto again;
5528
5529 /* Check static vs global. */
23c13d42 5530 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5531 {
2b79f376 5532 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5533 const bool symbol_is_static =
5534 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5535 if (want_static != symbol_is_static)
2b79f376 5536 goto again;
927aa2e7
JK
5537 }
5538
5539 /* Match dw2_symtab_iter_next, symbol_kind
5540 and debug_names::psymbol_tag. */
5541 switch (m_domain)
5542 {
5543 case VAR_DOMAIN:
5544 switch (indexval.dwarf_tag)
5545 {
5546 case DW_TAG_variable:
5547 case DW_TAG_subprogram:
5548 /* Some types are also in VAR_DOMAIN. */
5549 case DW_TAG_typedef:
5550 case DW_TAG_structure_type:
5551 break;
5552 default:
5553 goto again;
5554 }
5555 break;
5556 case STRUCT_DOMAIN:
5557 switch (indexval.dwarf_tag)
5558 {
5559 case DW_TAG_typedef:
5560 case DW_TAG_structure_type:
5561 break;
5562 default:
5563 goto again;
5564 }
5565 break;
5566 case LABEL_DOMAIN:
5567 switch (indexval.dwarf_tag)
5568 {
5569 case 0:
5570 case DW_TAG_variable:
5571 break;
5572 default:
5573 goto again;
5574 }
5575 break;
59c35742
AB
5576 case MODULE_DOMAIN:
5577 switch (indexval.dwarf_tag)
5578 {
5579 case DW_TAG_module:
5580 break;
5581 default:
5582 goto again;
5583 }
5584 break;
927aa2e7
JK
5585 default:
5586 break;
5587 }
5588
5589 /* Match dw2_expand_symtabs_matching, symbol_kind and
5590 debug_names::psymbol_tag. */
5591 switch (m_search)
4b514bc8 5592 {
927aa2e7
JK
5593 case VARIABLES_DOMAIN:
5594 switch (indexval.dwarf_tag)
4b514bc8 5595 {
927aa2e7
JK
5596 case DW_TAG_variable:
5597 break;
5598 default:
5599 goto again;
4b514bc8 5600 }
927aa2e7
JK
5601 break;
5602 case FUNCTIONS_DOMAIN:
5603 switch (indexval.dwarf_tag)
4b514bc8 5604 {
927aa2e7
JK
5605 case DW_TAG_subprogram:
5606 break;
5607 default:
5608 goto again;
4b514bc8 5609 }
927aa2e7
JK
5610 break;
5611 case TYPES_DOMAIN:
5612 switch (indexval.dwarf_tag)
5613 {
5614 case DW_TAG_typedef:
5615 case DW_TAG_structure_type:
5616 break;
5617 default:
5618 goto again;
5619 }
5620 break;
59c35742
AB
5621 case MODULES_DOMAIN:
5622 switch (indexval.dwarf_tag)
5623 {
5624 case DW_TAG_module:
5625 break;
5626 default:
5627 goto again;
5628 }
927aa2e7
JK
5629 default:
5630 break;
4b514bc8 5631 }
927aa2e7
JK
5632
5633 return per_cu;
4b514bc8 5634}
61920122 5635
927aa2e7 5636static struct compunit_symtab *
c7f839cb 5637dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5638 const char *name, domain_enum domain)
4b514bc8 5639{
ed2dc618
SM
5640 struct dwarf2_per_objfile *dwarf2_per_objfile
5641 = get_dwarf2_per_objfile (objfile);
61920122 5642
5989a64e 5643 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5644 if (!mapp)
61920122 5645 {
927aa2e7
JK
5646 /* index is NULL if OBJF_READNOW. */
5647 return NULL;
5648 }
5649 const auto &map = *mapp;
9291a0cd 5650
2b79f376 5651 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5652
927aa2e7
JK
5653 struct compunit_symtab *stab_best = NULL;
5654 struct dwarf2_per_cu_data *per_cu;
5655 while ((per_cu = iter.next ()) != NULL)
5656 {
5657 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5658 compunit_symtab *stab
5659 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5660 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5661 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5662
927aa2e7
JK
5663 sym = block_find_symbol (block, name, domain,
5664 block_find_non_opaque_type_preferred,
5665 &with_opaque);
9703b513 5666
927aa2e7
JK
5667 /* Some caution must be observed with overloaded functions and
5668 methods, since the index will not contain any overload
5669 information (but NAME might contain it). */
a3ec0bb1 5670
927aa2e7 5671 if (sym != NULL
987012b8 5672 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5673 return stab;
5674 if (with_opaque != NULL
987012b8 5675 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5676 stab_best = stab;
9703b513 5677
927aa2e7 5678 /* Keep looking through other CUs. */
9703b513
TT
5679 }
5680
927aa2e7 5681 return stab_best;
9703b513
TT
5682}
5683
927aa2e7
JK
5684/* This dumps minimal information about .debug_names. It is called
5685 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5686 uses this to verify that .debug_names has been loaded. */
9291a0cd 5687
927aa2e7
JK
5688static void
5689dw2_debug_names_dump (struct objfile *objfile)
5690{
ed2dc618
SM
5691 struct dwarf2_per_objfile *dwarf2_per_objfile
5692 = get_dwarf2_per_objfile (objfile);
5693
5989a64e 5694 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5695 printf_filtered (".debug_names:");
5989a64e 5696 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5697 printf_filtered (" exists\n");
5698 else
5699 printf_filtered (" faked for \"readnow\"\n");
5700 printf_filtered ("\n");
9291a0cd
TT
5701}
5702
9291a0cd 5703static void
927aa2e7
JK
5704dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5705 const char *func_name)
9291a0cd 5706{
ed2dc618
SM
5707 struct dwarf2_per_objfile *dwarf2_per_objfile
5708 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5709
5989a64e
SM
5710 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5711 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5712 {
5989a64e 5713 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5714
2b79f376 5715 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5716
927aa2e7
JK
5717 struct dwarf2_per_cu_data *per_cu;
5718 while ((per_cu = iter.next ()) != NULL)
97a1449a 5719 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5720 }
5721}
24c79950 5722
3b00ef10
TT
5723static void
5724dw2_debug_names_map_matching_symbols
5725 (struct objfile *objfile,
5726 const lookup_name_info &name, domain_enum domain,
5727 int global,
5728 gdb::function_view<symbol_found_callback_ftype> callback,
5729 symbol_compare_ftype *ordered_compare)
5730{
5731 struct dwarf2_per_objfile *dwarf2_per_objfile
5732 = get_dwarf2_per_objfile (objfile);
5733
5734 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5735 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5736 return;
5737
5989a64e 5738 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5739 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5740
5741 const char *match_name = name.ada ().lookup_name ().c_str ();
5742 auto matcher = [&] (const char *symname)
5743 {
5744 if (ordered_compare == nullptr)
5745 return true;
5746 return ordered_compare (symname, match_name) == 0;
5747 };
5748
5749 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5750 [&] (offset_type namei)
5751 {
5752 /* The name was matched, now expand corresponding CUs that were
5753 marked. */
5754 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5755
5756 struct dwarf2_per_cu_data *per_cu;
5757 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5758 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5759 nullptr);
3b00ef10
TT
5760 return true;
5761 });
5762
5763 /* It's a shame we couldn't do this inside the
5764 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5765 that have already been expanded. Instead, this loop matches what
5766 the psymtab code does. */
5989a64e 5767 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5768 {
af758d11
SM
5769 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5770 if (symtab != nullptr)
3b00ef10
TT
5771 {
5772 const struct block *block
af758d11 5773 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5774 if (!iterate_over_symbols_terminated (block, name,
5775 domain, callback))
5776 break;
5777 }
5778 }
5779}
5780
927aa2e7
JK
5781static void
5782dw2_debug_names_expand_symtabs_matching
5783 (struct objfile *objfile,
5784 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5785 const lookup_name_info *lookup_name,
927aa2e7
JK
5786 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5787 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5788 enum search_domain kind)
5789{
ed2dc618
SM
5790 struct dwarf2_per_objfile *dwarf2_per_objfile
5791 = get_dwarf2_per_objfile (objfile);
9291a0cd 5792
927aa2e7 5793 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5794 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5795 return;
9291a0cd 5796
ed2dc618 5797 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5798
c1a66c06
TV
5799 if (symbol_matcher == NULL && lookup_name == NULL)
5800 {
5989a64e 5801 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5802 {
5803 QUIT;
5804
97a1449a
SM
5805 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5806 file_matcher, expansion_notify);
c1a66c06
TV
5807 }
5808 return;
5809 }
5810
5989a64e 5811 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5812
c1a66c06 5813 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5814 symbol_matcher,
5815 kind, [&] (offset_type namei)
927aa2e7 5816 {
927aa2e7
JK
5817 /* The name was matched, now expand corresponding CUs that were
5818 marked. */
5819 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5820
927aa2e7
JK
5821 struct dwarf2_per_cu_data *per_cu;
5822 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5823 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5824 file_matcher, expansion_notify);
3b00ef10 5825 return true;
44ed8f3e 5826 });
9291a0cd
TT
5827}
5828
927aa2e7 5829const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5830{
5831 dw2_has_symbols,
5832 dw2_find_last_source_symtab,
5833 dw2_forget_cached_source_info,
f8eba3c6 5834 dw2_map_symtabs_matching_filename,
927aa2e7 5835 dw2_debug_names_lookup_symbol,
d3214198 5836 NULL,
9291a0cd 5837 dw2_print_stats,
927aa2e7 5838 dw2_debug_names_dump,
927aa2e7 5839 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5840 dw2_expand_all_symtabs,
652a8996 5841 dw2_expand_symtabs_with_fullname,
3b00ef10 5842 dw2_debug_names_map_matching_symbols,
927aa2e7 5843 dw2_debug_names_expand_symtabs_matching,
43f3e411 5844 dw2_find_pc_sect_compunit_symtab,
71a3c369 5845 NULL,
9291a0cd
TT
5846 dw2_map_symbol_filenames
5847};
5848
4485a1c1 5849/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5850 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5851
5852template <typename T>
5853static gdb::array_view<const gdb_byte>
5854get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5855{
5856 dwarf2_section_info *section = &section_owner->gdb_index;
5857
96b79293 5858 if (section->empty ())
4485a1c1
SM
5859 return {};
5860
5861 /* Older elfutils strip versions could keep the section in the main
5862 executable while splitting it for the separate debug info file. */
96b79293 5863 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5864 return {};
5865
96b79293 5866 section->read (obj);
4485a1c1 5867
8bebfcda
PA
5868 /* dwarf2_section_info::size is a bfd_size_type, while
5869 gdb::array_view works with size_t. On 32-bit hosts, with
5870 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5871 is 32-bit. So we need an explicit narrowing conversion here.
5872 This is fine, because it's impossible to allocate or mmap an
5873 array/buffer larger than what size_t can represent. */
5874 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5875}
5876
87d6a7aa
SM
5877/* Lookup the index cache for the contents of the index associated to
5878 DWARF2_OBJ. */
5879
5880static gdb::array_view<const gdb_byte>
5989a64e 5881get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5882{
5883 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5884 if (build_id == nullptr)
5885 return {};
5886
5887 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5888 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5889}
5890
5891/* Same as the above, but for DWZ. */
5892
5893static gdb::array_view<const gdb_byte>
5894get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5895{
5896 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5897 if (build_id == nullptr)
5898 return {};
5899
5900 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5901}
5902
3c0aa29a 5903/* See symfile.h. */
9291a0cd 5904
3c0aa29a
PA
5905bool
5906dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5907{
ed2dc618
SM
5908 struct dwarf2_per_objfile *dwarf2_per_objfile
5909 = get_dwarf2_per_objfile (objfile);
5910
9291a0cd
TT
5911 /* If we're about to read full symbols, don't bother with the
5912 indices. In this case we also don't care if some other debug
5913 format is making psymtabs, because they are all about to be
5914 expanded anyway. */
5915 if ((objfile->flags & OBJF_READNOW))
5916 {
5989a64e 5917 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5918 create_all_comp_units (dwarf2_per_objfile);
5919 create_all_type_units (dwarf2_per_objfile);
5989a64e 5920 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5921 = create_quick_file_names_table
5989a64e 5922 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5923 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5924
5989a64e
SM
5925 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5926 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5927 {
5989a64e 5928 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5929
5989a64e 5930 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5931 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5932 }
5933
5934 /* Return 1 so that gdb sees the "quick" functions. However,
5935 these functions will be no-ops because we will have expanded
5936 all symtabs. */
3c0aa29a
PA
5937 *index_kind = dw_index_kind::GDB_INDEX;
5938 return true;
9291a0cd
TT
5939 }
5940
ed2dc618 5941 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5942 {
5943 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5944 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5945 return true;
5946 }
927aa2e7 5947
4485a1c1 5948 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5949 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5950 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5951 {
5952 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5953 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5954 return true;
5955 }
9291a0cd 5956
87d6a7aa
SM
5957 /* ... otherwise, try to find the index in the index cache. */
5958 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5959 get_gdb_index_contents_from_cache,
5960 get_gdb_index_contents_from_cache_dwz))
5961 {
5962 global_index_cache.hit ();
5963 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5964 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5965 return true;
5966 }
5967
5968 global_index_cache.miss ();
3c0aa29a 5969 return false;
9291a0cd
TT
5970}
5971
5972\f
5973
dce234bc
PP
5974/* Build a partial symbol table. */
5975
5976void
f29dff0a 5977dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5978{
ed2dc618
SM
5979 struct dwarf2_per_objfile *dwarf2_per_objfile
5980 = get_dwarf2_per_objfile (objfile);
c9bf0622 5981
6eee24ce 5982 init_psymbol_list (objfile, 1024);
c906108c 5983
a70b8144 5984 try
c9bf0622
TT
5985 {
5986 /* This isn't really ideal: all the data we allocate on the
5987 objfile's obstack is still uselessly kept around. However,
5988 freeing it seems unsafe. */
906768f9 5989 psymtab_discarder psymtabs (objfile);
ed2dc618 5990 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5991 psymtabs.keep ();
87d6a7aa 5992
af758d11
SM
5993 dwarf2_per_objfile->resize_symtabs ();
5994
87d6a7aa
SM
5995 /* (maybe) store an index in the cache. */
5996 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5997 }
230d2906 5998 catch (const gdb_exception_error &except)
492d29ea
PA
5999 {
6000 exception_print (gdb_stderr, except);
6001 }
c906108c 6002}
c906108c 6003
3b80fe9b
DE
6004/* Find the base address of the compilation unit for range lists and
6005 location lists. It will normally be specified by DW_AT_low_pc.
6006 In DWARF-3 draft 4, the base address could be overridden by
6007 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6008 compilation units with discontinuous ranges. */
6009
6010static void
6011dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6012{
6013 struct attribute *attr;
6014
2b24b6e4 6015 cu->base_address.reset ();
3b80fe9b
DE
6016
6017 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6018 if (attr != nullptr)
2b24b6e4 6019 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6020 else
6021 {
6022 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6023 if (attr != nullptr)
2b24b6e4 6024 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6025 }
6026}
6027
36586728
TT
6028/* Helper function that returns the proper abbrev section for
6029 THIS_CU. */
6030
6031static struct dwarf2_section_info *
6032get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6033{
6034 struct dwarf2_section_info *abbrev;
c3699833 6035 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6036
6037 if (this_cu->is_dwz)
c3699833 6038 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6039 else
c3699833 6040 abbrev = &per_bfd->abbrev;
36586728
TT
6041
6042 return abbrev;
6043}
6044
f4dc4d17
DE
6045/* Fetch the abbreviation table offset from a comp or type unit header. */
6046
6047static sect_offset
ed2dc618
SM
6048read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6049 struct dwarf2_section_info *section,
9c541725 6050 sect_offset sect_off)
f4dc4d17 6051{
96b79293 6052 bfd *abfd = section->get_bfd_owner ();
d521ce57 6053 const gdb_byte *info_ptr;
ac298888 6054 unsigned int initial_length_size, offset_size;
43988095 6055 uint16_t version;
f4dc4d17 6056
96b79293 6057 section->read (dwarf2_per_objfile->objfile);
9c541725 6058 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6059 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6060 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6061 info_ptr += initial_length_size;
6062
6063 version = read_2_bytes (abfd, info_ptr);
6064 info_ptr += 2;
6065 if (version >= 5)
6066 {
6067 /* Skip unit type and address size. */
6068 info_ptr += 2;
6069 }
6070
24aa364d 6071 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6072}
6073
b83470bf
TT
6074/* A partial symtab that is used only for include files. */
6075struct dwarf2_include_psymtab : public partial_symtab
6076{
6077 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6078 : partial_symtab (filename, objfile)
6079 {
6080 }
6081
6082 void read_symtab (struct objfile *objfile) override
6083 {
194d088f
TV
6084 /* It's an include file, no symbols to read for it.
6085 Everything is in the includer symtab. */
6086
6087 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6088 expansion of the includer psymtab. We use the dependencies[0] field to
6089 model the includer. But if we go the regular route of calling
6090 expand_psymtab here, and having expand_psymtab call expand_dependencies
6091 to expand the includer, we'll only use expand_psymtab on the includer
6092 (making it a non-toplevel psymtab), while if we expand the includer via
6093 another path, we'll use read_symtab (making it a toplevel psymtab).
6094 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6095 psymtab, and trigger read_symtab on the includer here directly. */
6096 includer ()->read_symtab (objfile);
b83470bf
TT
6097 }
6098
6099 void expand_psymtab (struct objfile *objfile) override
6100 {
194d088f
TV
6101 /* This is not called by read_symtab, and should not be called by any
6102 expand_dependencies. */
6103 gdb_assert (false);
b83470bf
TT
6104 }
6105
5717c425 6106 bool readin_p (struct objfile *objfile) const override
b83470bf 6107 {
5717c425 6108 return includer ()->readin_p (objfile);
b83470bf
TT
6109 }
6110
5717c425 6111 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6112 {
6113 return nullptr;
6114 }
6115
6116private:
194d088f
TV
6117 partial_symtab *includer () const
6118 {
6119 /* An include psymtab has exactly one dependency: the psymtab that
6120 includes it. */
6121 gdb_assert (this->number_of_dependencies == 1);
6122 return this->dependencies[0];
6123 }
b83470bf
TT
6124};
6125
aaa75496
JB
6126/* Allocate a new partial symtab for file named NAME and mark this new
6127 partial symtab as being an include of PST. */
6128
6129static void
891813be 6130dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6131 struct objfile *objfile)
6132{
b83470bf 6133 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6134
fbd9ab74 6135 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6136 subpst->dirname = pst->dirname;
fbd9ab74 6137
a9342b62 6138 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6139 subpst->dependencies[0] = pst;
6140 subpst->number_of_dependencies = 1;
aaa75496
JB
6141}
6142
6143/* Read the Line Number Program data and extract the list of files
6144 included by the source file represented by PST. Build an include
d85a05f0 6145 partial symtab for each of these included files. */
aaa75496
JB
6146
6147static void
6148dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6149 struct die_info *die,
891813be 6150 dwarf2_psymtab *pst)
aaa75496 6151{
fff8551c 6152 line_header_up lh;
d85a05f0 6153 struct attribute *attr;
aaa75496 6154
d85a05f0 6155 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6156 if (attr != nullptr)
9c541725 6157 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6158 if (lh == NULL)
6159 return; /* No linetable, so no includes. */
6160
79748972
TT
6161 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6162 that we pass in the raw text_low here; that is ok because we're
6163 only decoding the line table to make include partial symtabs, and
6164 so the addresses aren't really used. */
4ae976d1 6165 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6166 pst->raw_text_low (), 1);
aaa75496
JB
6167}
6168
348e048f 6169static hashval_t
52dc124a 6170hash_signatured_type (const void *item)
348e048f 6171{
9a3c8263
SM
6172 const struct signatured_type *sig_type
6173 = (const struct signatured_type *) item;
9a619af0 6174
348e048f 6175 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6176 return sig_type->signature;
348e048f
DE
6177}
6178
6179static int
52dc124a 6180eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6181{
9a3c8263
SM
6182 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6183 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6184
348e048f
DE
6185 return lhs->signature == rhs->signature;
6186}
6187
1fd400ff
TT
6188/* Allocate a hash table for signatured types. */
6189
b0b6a987 6190static htab_up
298e9637 6191allocate_signatured_type_table ()
1fd400ff 6192{
b0b6a987
TT
6193 return htab_up (htab_create_alloc (41,
6194 hash_signatured_type,
6195 eq_signatured_type,
6196 NULL, xcalloc, xfree));
1fd400ff
TT
6197}
6198
d467dd73 6199/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6200
6201static int
d467dd73 6202add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6203{
9a3c8263 6204 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6205 std::vector<signatured_type *> *all_type_units
6206 = (std::vector<signatured_type *> *) datum;
1fd400ff 6207
b2bdb8cf 6208 all_type_units->push_back (sigt);
1fd400ff
TT
6209
6210 return 1;
6211}
6212
78d4d2c5 6213/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6214 and fill them into TYPES_HTAB. It will process only type units,
6215 therefore DW_UT_type. */
c88ee1f0 6216
78d4d2c5 6217static void
ed2dc618
SM
6218create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6219 struct dwo_file *dwo_file,
b0b6a987 6220 dwarf2_section_info *section, htab_up &types_htab,
43988095 6221 rcuh_kind section_kind)
348e048f 6222{
3019eac3 6223 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6224 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6225 bfd *abfd;
6226 const gdb_byte *info_ptr, *end_ptr;
348e048f 6227
4bdcc0c1
DE
6228 abbrev_section = (dwo_file != NULL
6229 ? &dwo_file->sections.abbrev
5989a64e 6230 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6231
b4f54984 6232 if (dwarf_read_debug)
43988095 6233 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6234 section->get_name (),
6235 abbrev_section->get_file_name ());
09406207 6236
96b79293 6237 section->read (objfile);
78d4d2c5 6238 info_ptr = section->buffer;
348e048f 6239
78d4d2c5
JK
6240 if (info_ptr == NULL)
6241 return;
348e048f 6242
78d4d2c5
JK
6243 /* We can't set abfd until now because the section may be empty or
6244 not present, in which case the bfd is unknown. */
96b79293 6245 abfd = section->get_bfd_owner ();
348e048f 6246
c0ab21c2
TT
6247 /* We don't use cutu_reader here because we don't need to read
6248 any dies: the signature is in the header. */
3019eac3 6249
78d4d2c5
JK
6250 end_ptr = info_ptr + section->size;
6251 while (info_ptr < end_ptr)
6252 {
78d4d2c5
JK
6253 struct signatured_type *sig_type;
6254 struct dwo_unit *dwo_tu;
6255 void **slot;
6256 const gdb_byte *ptr = info_ptr;
6257 struct comp_unit_head header;
6258 unsigned int length;
8b70b953 6259
9c541725 6260 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6261
a49dd8dd
JK
6262 /* Initialize it due to a false compiler warning. */
6263 header.signature = -1;
9c541725 6264 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6265
78d4d2c5
JK
6266 /* We need to read the type's signature in order to build the hash
6267 table, but we don't need anything else just yet. */
348e048f 6268
ed2dc618 6269 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6270 abbrev_section, ptr, section_kind);
348e048f 6271
4057dfde 6272 length = header.get_length ();
6caca83c 6273
78d4d2c5
JK
6274 /* Skip dummy type units. */
6275 if (ptr >= info_ptr + length
43988095
JK
6276 || peek_abbrev_code (abfd, ptr) == 0
6277 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6278 {
6279 info_ptr += length;
6280 continue;
6281 }
dee91e82 6282
78d4d2c5
JK
6283 if (types_htab == NULL)
6284 {
6285 if (dwo_file)
298e9637 6286 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6287 else
298e9637 6288 types_htab = allocate_signatured_type_table ();
78d4d2c5 6289 }
8b70b953 6290
78d4d2c5
JK
6291 if (dwo_file)
6292 {
6293 sig_type = NULL;
5989a64e 6294 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6295 struct dwo_unit);
6296 dwo_tu->dwo_file = dwo_file;
43988095 6297 dwo_tu->signature = header.signature;
9c541725 6298 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6299 dwo_tu->section = section;
9c541725 6300 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6301 dwo_tu->length = length;
6302 }
6303 else
6304 {
6305 /* N.B.: type_offset is not usable if this type uses a DWO file.
6306 The real type_offset is in the DWO file. */
6307 dwo_tu = NULL;
5989a64e 6308 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6309 sig_type->signature = header.signature;
9c541725 6310 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6311 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6312 sig_type->per_cu.is_debug_types = 1;
6313 sig_type->per_cu.section = section;
9c541725 6314 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6315 sig_type->per_cu.length = length;
6316 }
6317
b0b6a987 6318 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6319 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6320 INSERT);
6321 gdb_assert (slot != NULL);
6322 if (*slot != NULL)
6323 {
9c541725 6324 sect_offset dup_sect_off;
0349ea22 6325
3019eac3
DE
6326 if (dwo_file)
6327 {
78d4d2c5
JK
6328 const struct dwo_unit *dup_tu
6329 = (const struct dwo_unit *) *slot;
6330
9c541725 6331 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6332 }
6333 else
6334 {
78d4d2c5
JK
6335 const struct signatured_type *dup_tu
6336 = (const struct signatured_type *) *slot;
6337
9c541725 6338 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6339 }
8b70b953 6340
b98664d3 6341 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6342 " the entry at offset %s, signature %s"),
6343 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6344 hex_string (header.signature));
78d4d2c5
JK
6345 }
6346 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6347
78d4d2c5 6348 if (dwarf_read_debug > 1)
9d8780f0
SM
6349 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6350 sect_offset_str (sect_off),
43988095 6351 hex_string (header.signature));
3019eac3 6352
78d4d2c5
JK
6353 info_ptr += length;
6354 }
6355}
3019eac3 6356
78d4d2c5
JK
6357/* Create the hash table of all entries in the .debug_types
6358 (or .debug_types.dwo) section(s).
6359 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6360 otherwise it is NULL.
b3c8eb43 6361
78d4d2c5 6362 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6363
78d4d2c5 6364 Note: This function processes DWO files only, not DWP files. */
348e048f 6365
78d4d2c5 6366static void
ed2dc618
SM
6367create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6368 struct dwo_file *dwo_file,
fd5866f6 6369 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6370 htab_up &types_htab)
78d4d2c5 6371{
fd5866f6
SM
6372 for (dwarf2_section_info &section : type_sections)
6373 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6374 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6375}
6376
6377/* Create the hash table of all entries in the .debug_types section,
6378 and initialize all_type_units.
6379 The result is zero if there is an error (e.g. missing .debug_types section),
6380 otherwise non-zero. */
6381
6382static int
ed2dc618 6383create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6384{
b0b6a987 6385 htab_up types_htab;
3019eac3 6386
ed2dc618 6387 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6388 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6389 rcuh_kind::COMPILE);
ed2dc618 6390 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6391 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6392 if (types_htab == NULL)
6393 {
5989a64e 6394 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6395 return 0;
6396 }
6397
5989a64e 6398 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6399
5989a64e
SM
6400 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6401 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6402 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6403
5989a64e 6404 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6405 add_signatured_type_cu_to_table,
5989a64e 6406 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6407
348e048f
DE
6408 return 1;
6409}
6410
5989a64e 6411/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6412 If SLOT is non-NULL, it is the entry to use in the hash table.
6413 Otherwise we find one. */
6414
6415static struct signatured_type *
ed2dc618
SM
6416add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6417 void **slot)
6aa5f3a6 6418{
5989a64e
SM
6419 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6420 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6421 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6422
5989a64e 6423 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6424
af758d11
SM
6425 dwarf2_per_objfile->resize_symtabs ();
6426
5989a64e 6427 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6428 sig_type->signature = sig;
6429 sig_type->per_cu.is_debug_types = 1;
5989a64e 6430 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6431 {
6432 sig_type->per_cu.v.quick =
5989a64e 6433 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6434 struct dwarf2_per_cu_quick_data);
6435 }
6436
6437 if (slot == NULL)
6438 {
5989a64e 6439 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6440 sig_type, INSERT);
6441 }
6442 gdb_assert (*slot == NULL);
6443 *slot = sig_type;
6444 /* The rest of sig_type must be filled in by the caller. */
6445 return sig_type;
6446}
6447
a2ce51a0
DE
6448/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6449 Fill in SIG_ENTRY with DWO_ENTRY. */
6450
6451static void
ed2dc618 6452fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6453 struct signatured_type *sig_entry,
6454 struct dwo_unit *dwo_entry)
6455{
1859c670
SM
6456 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6457
7ee85ab1 6458 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6459 gdb_assert (! sig_entry->per_cu.queued);
6460 gdb_assert (sig_entry->per_cu.cu == NULL);
1859c670 6461 if (per_bfd->using_index)
6aa5f3a6
DE
6462 {
6463 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6464 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6465 }
6466 else
6467 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6468 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6469 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6470 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6471 gdb_assert (sig_entry->dwo_unit == NULL);
6472
6473 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6474 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6475 sig_entry->per_cu.length = dwo_entry->length;
6476 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6477 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 6478 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6479 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6480 sig_entry->dwo_unit = dwo_entry;
6481}
6482
6483/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6484 If we haven't read the TU yet, create the signatured_type data structure
6485 for a TU to be read in directly from a DWO file, bypassing the stub.
6486 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6487 using .gdb_index, then when reading a CU we want to stay in the DWO file
6488 containing that CU. Otherwise we could end up reading several other DWO
6489 files (due to comdat folding) to process the transitive closure of all the
6490 mentioned TUs, and that can be slow. The current DWO file will have every
6491 type signature that it needs.
a2ce51a0
DE
6492 We only do this for .gdb_index because in the psymtab case we already have
6493 to read all the DWOs to build the type unit groups. */
6494
6495static struct signatured_type *
6496lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6497{
5e22e966 6498 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6499 struct dwo_file *dwo_file;
6500 struct dwo_unit find_dwo_entry, *dwo_entry;
6501 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6502 void **slot;
a2ce51a0 6503
5989a64e 6504 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6505
6aa5f3a6
DE
6506 /* If TU skeletons have been removed then we may not have read in any
6507 TUs yet. */
5989a64e
SM
6508 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6509 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6510
6511 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6512 Use the global signatured_types array to do our own comdat-folding
6513 of types. If this is the first time we're reading this TU, and
6514 the TU has an entry in .gdb_index, replace the recorded data from
6515 .gdb_index with this TU. */
a2ce51a0 6516
a2ce51a0 6517 find_sig_entry.signature = sig;
5989a64e 6518 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6519 &find_sig_entry, INSERT);
9a3c8263 6520 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6521
6522 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6523 read. Don't reassign the global entry to point to this DWO if that's
6524 the case. Also note that if the TU is already being read, it may not
6525 have come from a DWO, the program may be a mix of Fission-compiled
6526 code and non-Fission-compiled code. */
6527
6528 /* Have we already tried to read this TU?
6529 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6530 needn't exist in the global table yet). */
6531 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6532 return sig_entry;
6533
6aa5f3a6
DE
6534 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6535 dwo_unit of the TU itself. */
6536 dwo_file = cu->dwo_unit->dwo_file;
6537
a2ce51a0
DE
6538 /* Ok, this is the first time we're reading this TU. */
6539 if (dwo_file->tus == NULL)
6540 return NULL;
6541 find_dwo_entry.signature = sig;
b0b6a987
TT
6542 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6543 &find_dwo_entry);
a2ce51a0
DE
6544 if (dwo_entry == NULL)
6545 return NULL;
6546
6aa5f3a6
DE
6547 /* If the global table doesn't have an entry for this TU, add one. */
6548 if (sig_entry == NULL)
ed2dc618 6549 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6550
ed2dc618 6551 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6552 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6553 return sig_entry;
6554}
6555
a2ce51a0
DE
6556/* Subroutine of lookup_signatured_type.
6557 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6558 then try the DWP file. If the TU stub (skeleton) has been removed then
6559 it won't be in .gdb_index. */
a2ce51a0
DE
6560
6561static struct signatured_type *
6562lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6563{
5e22e966 6564 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6565 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6566 struct dwo_unit *dwo_entry;
6567 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6568 void **slot;
a2ce51a0 6569
5989a64e 6570 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6571 gdb_assert (dwp_file != NULL);
6572
6aa5f3a6
DE
6573 /* If TU skeletons have been removed then we may not have read in any
6574 TUs yet. */
5989a64e
SM
6575 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6576 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6577
6aa5f3a6 6578 find_sig_entry.signature = sig;
5989a64e 6579 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6580 &find_sig_entry, INSERT);
9a3c8263 6581 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6582
6583 /* Have we already tried to read this TU?
6584 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6585 needn't exist in the global table yet). */
6586 if (sig_entry != NULL)
6587 return sig_entry;
6588
a2ce51a0
DE
6589 if (dwp_file->tus == NULL)
6590 return NULL;
ed2dc618 6591 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6592 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6593 if (dwo_entry == NULL)
6594 return NULL;
6595
ed2dc618
SM
6596 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6597 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6598
a2ce51a0
DE
6599 return sig_entry;
6600}
6601
380bca97 6602/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6603 Returns NULL if signature SIG is not present in the table.
6604 It is up to the caller to complain about this. */
348e048f
DE
6605
6606static struct signatured_type *
a2ce51a0 6607lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6608{
5e22e966 6609 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6610
a2ce51a0 6611 if (cu->dwo_unit
5989a64e 6612 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6613 {
6614 /* We're in a DWO/DWP file, and we're using .gdb_index.
6615 These cases require special processing. */
ed2dc618 6616 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6617 return lookup_dwo_signatured_type (cu, sig);
6618 else
6619 return lookup_dwp_signatured_type (cu, sig);
6620 }
6621 else
6622 {
6623 struct signatured_type find_entry, *entry;
348e048f 6624
5989a64e 6625 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6626 return NULL;
6627 find_entry.signature = sig;
9a3c8263 6628 entry = ((struct signatured_type *)
5989a64e 6629 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6630 &find_entry));
a2ce51a0
DE
6631 return entry;
6632 }
348e048f 6633}
18a8505e 6634
42e7ad6c 6635/* Low level DIE reading support. */
348e048f 6636
d85a05f0
DJ
6637/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6638
6639static void
6640init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6641 struct dwarf2_cu *cu,
3019eac3 6642 struct dwarf2_section_info *section,
685af9cd
TT
6643 struct dwo_file *dwo_file,
6644 struct abbrev_table *abbrev_table)
d85a05f0 6645{
fceca515 6646 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6647 reader->abfd = section->get_bfd_owner ();
d85a05f0 6648 reader->cu = cu;
3019eac3 6649 reader->dwo_file = dwo_file;
dee91e82
DE
6650 reader->die_section = section;
6651 reader->buffer = section->buffer;
f664829e 6652 reader->buffer_end = section->buffer + section->size;
685af9cd 6653 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6654}
6655
c0ab21c2 6656/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6657 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6658 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6659 already.
6660
6661 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6662 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6663 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6664 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6665 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6666 STUB_COMP_DIR may be non-NULL.
3e225074 6667 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6668 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6669 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6670 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6671 kept around for at least as long as *RESULT_READER.
6672
b0c7bfa9
DE
6673 The result is non-zero if a valid (non-dummy) DIE was found. */
6674
6675static int
6676read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6677 struct dwo_unit *dwo_unit,
b0c7bfa9 6678 struct die_info *stub_comp_unit_die,
a2ce51a0 6679 const char *stub_comp_dir,
b0c7bfa9 6680 struct die_reader_specs *result_reader,
d521ce57 6681 const gdb_byte **result_info_ptr,
b0c7bfa9 6682 struct die_info **result_comp_unit_die,
685af9cd 6683 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6684{
ed2dc618 6685 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6686 struct objfile *objfile = dwarf2_per_objfile->objfile;
6687 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6688 bfd *abfd;
d521ce57 6689 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6690 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6691 int i,num_extra_attrs;
6692 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6693 struct die_info *comp_unit_die;
6694
b0aeadb3
DE
6695 /* At most one of these may be provided. */
6696 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6697
b0c7bfa9
DE
6698 /* These attributes aren't processed until later:
6699 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6700 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6701 referenced later. However, these attributes are found in the stub
6702 which we won't have later. In order to not impose this complication
6703 on the rest of the code, we read them here and copy them to the
6704 DWO CU/TU die. */
b0c7bfa9
DE
6705
6706 stmt_list = NULL;
6707 low_pc = NULL;
6708 high_pc = NULL;
6709 ranges = NULL;
6710 comp_dir = NULL;
6711
6712 if (stub_comp_unit_die != NULL)
6713 {
6714 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6715 DWO file. */
6716 if (! this_cu->is_debug_types)
6717 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6718 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6719 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6720 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6721 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6722
a39fdb41 6723 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6724
18a8505e
AT
6725 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6726 here (if needed). We need the value before we can process
6727 DW_AT_ranges. */
a39fdb41 6728 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6729 }
a2ce51a0
DE
6730 else if (stub_comp_dir != NULL)
6731 {
6732 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6733 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6734 comp_dir->name = DW_AT_comp_dir;
6735 comp_dir->form = DW_FORM_string;
6736 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6737 DW_STRING (comp_dir) = stub_comp_dir;
6738 }
b0c7bfa9
DE
6739
6740 /* Set up for reading the DWO CU/TU. */
6741 cu->dwo_unit = dwo_unit;
685af9cd 6742 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6743 section->read (objfile);
6744 abfd = section->get_bfd_owner ();
9c541725
PA
6745 begin_info_ptr = info_ptr = (section->buffer
6746 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6747 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6748
6749 if (this_cu->is_debug_types)
6750 {
b0c7bfa9
DE
6751 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6752
ed2dc618
SM
6753 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6754 &cu->header, section,
b0c7bfa9 6755 dwo_abbrev_section,
43988095 6756 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6757 /* This is not an assert because it can be caused by bad debug info. */
43988095 6758 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6759 {
6760 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6761 " TU at offset %s [in module %s]"),
a2ce51a0 6762 hex_string (sig_type->signature),
43988095 6763 hex_string (cu->header.signature),
9d8780f0 6764 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6765 bfd_get_filename (abfd));
6766 }
9c541725 6767 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6768 /* For DWOs coming from DWP files, we don't know the CU length
6769 nor the type's offset in the TU until now. */
4057dfde 6770 dwo_unit->length = cu->header.get_length ();
9c541725 6771 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6772
6773 /* Establish the type offset that can be used to lookup the type.
6774 For DWO files, we don't know it until now. */
9c541725
PA
6775 sig_type->type_offset_in_section
6776 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6777 }
6778 else
6779 {
ed2dc618
SM
6780 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6781 &cu->header, section,
b0c7bfa9 6782 dwo_abbrev_section,
43988095 6783 info_ptr, rcuh_kind::COMPILE);
9c541725 6784 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6785 /* For DWOs coming from DWP files, we don't know the CU length
6786 until now. */
4057dfde 6787 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6788 }
6789
685af9cd 6790 *result_dwo_abbrev_table
86de1d91
TT
6791 = abbrev_table::read (objfile, dwo_abbrev_section,
6792 cu->header.abbrev_sect_off);
685af9cd
TT
6793 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6794 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6795
6796 /* Read in the die, but leave space to copy over the attributes
6797 from the stub. This has the benefit of simplifying the rest of
6798 the code - all the work to maintain the illusion of a single
6799 DW_TAG_{compile,type}_unit DIE is done here. */
6800 num_extra_attrs = ((stmt_list != NULL)
6801 + (low_pc != NULL)
6802 + (high_pc != NULL)
6803 + (ranges != NULL)
6804 + (comp_dir != NULL));
6805 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6806 num_extra_attrs);
b0c7bfa9
DE
6807
6808 /* Copy over the attributes from the stub to the DIE we just read in. */
6809 comp_unit_die = *result_comp_unit_die;
6810 i = comp_unit_die->num_attrs;
6811 if (stmt_list != NULL)
6812 comp_unit_die->attrs[i++] = *stmt_list;
6813 if (low_pc != NULL)
6814 comp_unit_die->attrs[i++] = *low_pc;
6815 if (high_pc != NULL)
6816 comp_unit_die->attrs[i++] = *high_pc;
6817 if (ranges != NULL)
6818 comp_unit_die->attrs[i++] = *ranges;
6819 if (comp_dir != NULL)
6820 comp_unit_die->attrs[i++] = *comp_dir;
6821 comp_unit_die->num_attrs += num_extra_attrs;
6822
b4f54984 6823 if (dwarf_die_debug)
bf6af496
DE
6824 {
6825 fprintf_unfiltered (gdb_stdlog,
6826 "Read die from %s@0x%x of %s:\n",
96b79293 6827 section->get_name (),
bf6af496
DE
6828 (unsigned) (begin_info_ptr - section->buffer),
6829 bfd_get_filename (abfd));
b4f54984 6830 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6831 }
6832
b0c7bfa9
DE
6833 /* Skip dummy compilation units. */
6834 if (info_ptr >= begin_info_ptr + dwo_unit->length
6835 || peek_abbrev_code (abfd, info_ptr) == 0)
6836 return 0;
6837
6838 *result_info_ptr = info_ptr;
6839 return 1;
6840}
6841
a084a2a6
AT
6842/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6843 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6844 signature is part of the header. */
6845static gdb::optional<ULONGEST>
6846lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6847{
6848 if (cu->header.version >= 5)
6849 return cu->header.signature;
6850 struct attribute *attr;
6851 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6852 if (attr == nullptr)
6853 return gdb::optional<ULONGEST> ();
6854 return DW_UNSND (attr);
6855}
6856
c0ab21c2 6857/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6858 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6859 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6860
6861static struct dwo_unit *
6862lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6863 struct die_info *comp_unit_die,
6864 const char *dwo_name)
b0c7bfa9
DE
6865{
6866 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6867 struct dwo_unit *dwo_unit;
c0ab21c2 6868 const char *comp_dir;
b0c7bfa9 6869
a2ce51a0
DE
6870 gdb_assert (cu != NULL);
6871
b0c7bfa9 6872 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6873 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6874 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6875
6876 if (this_cu->is_debug_types)
6877 {
6878 struct signatured_type *sig_type;
6879
6880 /* Since this_cu is the first member of struct signatured_type,
6881 we can go from a pointer to one to a pointer to the other. */
6882 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6883 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6884 }
6885 else
6886 {
a084a2a6
AT
6887 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6888 if (!signature.has_value ())
b0c7bfa9
DE
6889 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6890 " [in module %s]"),
313bad1b 6891 dwo_name, bfd_get_filename (this_cu->per_bfd->obfd));
b0c7bfa9 6892 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6893 *signature);
b0c7bfa9
DE
6894 }
6895
b0c7bfa9
DE
6896 return dwo_unit;
6897}
6898
c0ab21c2 6899/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6900 See it for a description of the parameters.
fcd3b13d 6901 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6902
c0ab21c2 6903void
9e021579
SM
6904cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6905 dwarf2_per_objfile *per_objfile,
6751ebae 6906 int use_existing_cu)
a2ce51a0 6907{
a2ce51a0 6908 struct signatured_type *sig_type;
a2ce51a0
DE
6909
6910 /* Verify we can do the following downcast, and that we have the
6911 data we need. */
6912 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6913 sig_type = (struct signatured_type *) this_cu;
6914 gdb_assert (sig_type->dwo_unit != NULL);
6915
6aa5f3a6
DE
6916 if (use_existing_cu && this_cu->cu != NULL)
6917 {
6918 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6919 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6920 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6921 }
6922 else
6923 {
6924 /* If !use_existing_cu, this_cu->cu must be NULL. */
6925 gdb_assert (this_cu->cu == NULL);
9e021579 6926 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6aa5f3a6
DE
6927 }
6928
6929 /* A future optimization, if needed, would be to use an existing
6930 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6931 could share abbrev tables. */
a2ce51a0
DE
6932
6933 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6934 NULL /* stub_comp_unit_die */,
6935 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6936 this, &info_ptr,
3e225074 6937 &comp_unit_die,
c0ab21c2 6938 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6939 {
6940 /* Dummy die. */
c0ab21c2 6941 dummy_p = true;
a2ce51a0 6942 }
a2ce51a0
DE
6943}
6944
fd820528 6945/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6946 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6947
f4dc4d17
DE
6948 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6949 Otherwise the table specified in the comp unit header is read in and used.
6950 This is an optimization for when we already have the abbrev table.
6951
dee91e82 6952 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6953 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6954
c0ab21c2
TT
6955cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6956 struct abbrev_table *abbrev_table,
6751ebae 6957 int use_existing_cu,
c0ab21c2
TT
6958 bool skip_partial)
6959 : die_reader_specs {},
6751ebae 6960 m_this_cu (this_cu)
c906108c 6961{
ed2dc618 6962 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6963 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6964 struct dwarf2_section_info *section = this_cu->section;
96b79293 6965 bfd *abfd = section->get_bfd_owner ();
dee91e82 6966 struct dwarf2_cu *cu;
c0ab21c2 6967 const gdb_byte *begin_info_ptr;
dee91e82 6968 struct signatured_type *sig_type = NULL;
4bdcc0c1 6969 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6970 /* Non-zero if CU currently points to a DWO file and we need to
6971 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6972 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6973 int rereading_dwo_cu = 0;
c906108c 6974
b4f54984 6975 if (dwarf_die_debug)
9d8780f0 6976 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6977 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6978 sect_offset_str (this_cu->sect_off));
09406207 6979
a2ce51a0
DE
6980 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6981 file (instead of going through the stub), short-circuit all of this. */
6982 if (this_cu->reading_dwo_directly)
6983 {
6984 /* Narrow down the scope of possibilities to have to understand. */
6985 gdb_assert (this_cu->is_debug_types);
6986 gdb_assert (abbrev_table == NULL);
9e021579 6987 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
a2ce51a0
DE
6988 return;
6989 }
6990
dee91e82 6991 /* This is cheap if the section is already read in. */
96b79293 6992 section->read (objfile);
dee91e82 6993
9c541725 6994 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6995
6996 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6997
6998 if (use_existing_cu && this_cu->cu != NULL)
6999 {
7000 cu = this_cu->cu;
42e7ad6c
DE
7001 /* If this CU is from a DWO file we need to start over, we need to
7002 refetch the attributes from the skeleton CU.
7003 This could be optimized by retrieving those attributes from when we
7004 were here the first time: the previous comp_unit_die was stored in
7005 comp_unit_obstack. But there's no data yet that we need this
7006 optimization. */
7007 if (cu->dwo_unit != NULL)
7008 rereading_dwo_cu = 1;
dee91e82
DE
7009 }
7010 else
7011 {
7012 /* If !use_existing_cu, this_cu->cu must be NULL. */
7013 gdb_assert (this_cu->cu == NULL);
9e021579 7014 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7015 cu = m_new_cu.get ();
42e7ad6c 7016 }
dee91e82 7017
b0c7bfa9 7018 /* Get the header. */
9c541725 7019 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7020 {
7021 /* We already have the header, there's no need to read it in again. */
9c541725 7022 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7023 }
7024 else
7025 {
3019eac3 7026 if (this_cu->is_debug_types)
dee91e82 7027 {
ed2dc618
SM
7028 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7029 &cu->header, section,
4bdcc0c1 7030 abbrev_section, info_ptr,
43988095 7031 rcuh_kind::TYPE);
dee91e82 7032
42e7ad6c
DE
7033 /* Since per_cu is the first member of struct signatured_type,
7034 we can go from a pointer to one to a pointer to the other. */
7035 sig_type = (struct signatured_type *) this_cu;
43988095 7036 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7037 gdb_assert (sig_type->type_offset_in_tu
7038 == cu->header.type_cu_offset_in_tu);
7039 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7040
42e7ad6c
DE
7041 /* LENGTH has not been set yet for type units if we're
7042 using .gdb_index. */
4057dfde 7043 this_cu->length = cu->header.get_length ();
3019eac3
DE
7044
7045 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7046 sig_type->type_offset_in_section =
7047 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7048
7049 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7050 }
7051 else
7052 {
ed2dc618
SM
7053 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7054 &cu->header, section,
4bdcc0c1 7055 abbrev_section,
43988095
JK
7056 info_ptr,
7057 rcuh_kind::COMPILE);
dee91e82 7058
9c541725 7059 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7060 if (this_cu->length == 0)
7061 this_cu->length = cu->header.get_length ();
7062 else
7063 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7064 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7065 }
7066 }
10b3939b 7067
6caca83c 7068 /* Skip dummy compilation units. */
dee91e82 7069 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7070 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7071 {
7072 dummy_p = true;
7073 return;
7074 }
6caca83c 7075
433df2d4
DE
7076 /* If we don't have them yet, read the abbrevs for this compilation unit.
7077 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7078 done. */
f4dc4d17 7079 if (abbrev_table != NULL)
685af9cd
TT
7080 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7081 else
f4dc4d17 7082 {
c0ab21c2 7083 m_abbrev_table_holder
86de1d91
TT
7084 = abbrev_table::read (objfile, abbrev_section,
7085 cu->header.abbrev_sect_off);
c0ab21c2 7086 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7087 }
af703f96 7088
dee91e82 7089 /* Read the top level CU/TU die. */
c0ab21c2 7090 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7091 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7092
58f0c718 7093 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7094 {
7095 dummy_p = true;
7096 return;
7097 }
58f0c718 7098
b0c7bfa9 7099 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7100 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7101 table from the DWO file and pass the ownership over to us. It will be
7102 referenced from READER, so we must make sure to free it after we're done
7103 with READER.
7104
b0c7bfa9
DE
7105 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7106 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7107 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7108 if (dwo_name != nullptr)
3019eac3 7109 {
3019eac3 7110 struct dwo_unit *dwo_unit;
b0c7bfa9 7111 struct die_info *dwo_comp_unit_die;
3019eac3 7112
3e225074 7113 if (comp_unit_die->has_children)
6a506a2d 7114 {
b98664d3 7115 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7116 " has children (offset %s) [in module %s]"),
7117 sect_offset_str (this_cu->sect_off),
7118 bfd_get_filename (abfd));
6a506a2d 7119 }
c0ab21c2 7120 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7121 if (dwo_unit != NULL)
3019eac3 7122 {
6a506a2d 7123 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7124 comp_unit_die, NULL,
c0ab21c2 7125 this, &info_ptr,
3e225074 7126 &dwo_comp_unit_die,
c0ab21c2 7127 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7128 {
7129 /* Dummy die. */
c0ab21c2 7130 dummy_p = true;
6a506a2d
DE
7131 return;
7132 }
7133 comp_unit_die = dwo_comp_unit_die;
7134 }
7135 else
7136 {
7137 /* Yikes, we couldn't find the rest of the DIE, we only have
7138 the stub. A complaint has already been logged. There's
7139 not much more we can do except pass on the stub DIE to
7140 die_reader_func. We don't want to throw an error on bad
7141 debug info. */
3019eac3
DE
7142 }
7143 }
c0ab21c2 7144}
3019eac3 7145
6751ebae
TT
7146void
7147cutu_reader::keep ()
c0ab21c2 7148{
b0c7bfa9 7149 /* Done, clean up. */
6751ebae
TT
7150 gdb_assert (!dummy_p);
7151 if (m_new_cu != NULL)
348e048f 7152 {
c0ab21c2
TT
7153 struct dwarf2_per_objfile *dwarf2_per_objfile
7154 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7155 /* Link this CU into read_in_chain. */
5989a64e
SM
7156 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7157 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7158 /* The chain owns it now. */
c0ab21c2 7159 m_new_cu.release ();
348e048f 7160 }
dee91e82
DE
7161}
7162
18a8505e
AT
7163/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7164 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7165 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7166
7167 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7168 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7169
7170 We fill in THIS_CU->length.
7171
dee91e82 7172 THIS_CU->cu is always freed when done.
3019eac3 7173 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7174 to care whether it refers to the "main" CU or the DWO CU.
7175
7176 When parent_cu is passed, it is used to provide a default value for
7177 str_offsets_base and addr_base from the parent. */
dee91e82 7178
c0ab21c2
TT
7179cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7180 struct dwarf2_cu *parent_cu,
7181 struct dwo_file *dwo_file)
7182 : die_reader_specs {},
7183 m_this_cu (this_cu)
dee91e82 7184{
ed2dc618 7185 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7186 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7187 struct dwarf2_section_info *section = this_cu->section;
96b79293 7188 bfd *abfd = section->get_bfd_owner ();
33e80786 7189 struct dwarf2_section_info *abbrev_section;
d521ce57 7190 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7191
b4f54984 7192 if (dwarf_die_debug)
9d8780f0 7193 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7194 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7195 sect_offset_str (this_cu->sect_off));
09406207 7196
dee91e82
DE
7197 gdb_assert (this_cu->cu == NULL);
7198
33e80786
DE
7199 abbrev_section = (dwo_file != NULL
7200 ? &dwo_file->sections.abbrev
7201 : get_abbrev_section_for_cu (this_cu));
7202
dee91e82 7203 /* This is cheap if the section is already read in. */
96b79293 7204 section->read (objfile);
dee91e82 7205
9e021579 7206 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7207
9c541725 7208 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7209 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7210 &m_new_cu->header, section,
4bdcc0c1 7211 abbrev_section, info_ptr,
43988095
JK
7212 (this_cu->is_debug_types
7213 ? rcuh_kind::TYPE
7214 : rcuh_kind::COMPILE));
dee91e82 7215
18a8505e
AT
7216 if (parent_cu != nullptr)
7217 {
c0ab21c2
TT
7218 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7219 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7220 }
4057dfde 7221 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7222
7223 /* Skip dummy compilation units. */
7224 if (info_ptr >= begin_info_ptr + this_cu->length
7225 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7226 {
7227 dummy_p = true;
7228 return;
7229 }
72bf9492 7230
c0ab21c2 7231 m_abbrev_table_holder
86de1d91
TT
7232 = abbrev_table::read (objfile, abbrev_section,
7233 m_new_cu->header.abbrev_sect_off);
dee91e82 7234
c0ab21c2
TT
7235 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7236 m_abbrev_table_holder.get ());
3e225074 7237 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7238}
7239
0018ea6f
DE
7240\f
7241/* Type Unit Groups.
dee91e82 7242
0018ea6f
DE
7243 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7244 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7245 so that all types coming from the same compilation (.o file) are grouped
7246 together. A future step could be to put the types in the same symtab as
7247 the CU the types ultimately came from. */
ff013f42 7248
f4dc4d17
DE
7249static hashval_t
7250hash_type_unit_group (const void *item)
7251{
9a3c8263
SM
7252 const struct type_unit_group *tu_group
7253 = (const struct type_unit_group *) item;
f4dc4d17 7254
094b34ac 7255 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7256}
348e048f
DE
7257
7258static int
f4dc4d17 7259eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7260{
9a3c8263
SM
7261 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7262 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7263
094b34ac 7264 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7265}
348e048f 7266
f4dc4d17
DE
7267/* Allocate a hash table for type unit groups. */
7268
eaa5fa8b 7269static htab_up
298e9637 7270allocate_type_unit_groups_table ()
f4dc4d17 7271{
eaa5fa8b
TT
7272 return htab_up (htab_create_alloc (3,
7273 hash_type_unit_group,
7274 eq_type_unit_group,
7275 NULL, xcalloc, xfree));
f4dc4d17 7276}
dee91e82 7277
f4dc4d17
DE
7278/* Type units that don't have DW_AT_stmt_list are grouped into their own
7279 partial symtabs. We combine several TUs per psymtab to not let the size
7280 of any one psymtab grow too big. */
7281#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7282#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7283
094b34ac 7284/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7285 Create the type_unit_group object used to hold one or more TUs. */
7286
7287static struct type_unit_group *
094b34ac 7288create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7289{
5e22e966 7290 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7291 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7292 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7293 struct type_unit_group *tu_group;
f4dc4d17 7294
5989a64e 7295 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7296 struct type_unit_group);
094b34ac 7297 per_cu = &tu_group->per_cu;
518817b3 7298 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 7299 per_cu->per_bfd = per_bfd;
f4dc4d17 7300
1859c670 7301 if (per_bfd->using_index)
094b34ac 7302 {
1859c670 7303 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7304 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7305 }
7306 else
7307 {
9c541725 7308 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7309 dwarf2_psymtab *pst;
528e1572 7310 std::string name;
094b34ac
DE
7311
7312 /* Give the symtab a useful name for debug purposes. */
7313 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7314 name = string_printf ("<type_units_%d>",
7315 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7316 else
528e1572 7317 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7318
528e1572 7319 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7320 pst->anonymous = true;
094b34ac 7321 }
f4dc4d17 7322
094b34ac 7323 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7324 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7325
7326 return tu_group;
7327}
7328
094b34ac
DE
7329/* Look up the type_unit_group for type unit CU, and create it if necessary.
7330 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7331
7332static struct type_unit_group *
ff39bb5e 7333get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7334{
5e22e966 7335 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7336 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7337 struct type_unit_group *tu_group;
7338 void **slot;
7339 unsigned int line_offset;
7340 struct type_unit_group type_unit_group_for_lookup;
7341
5989a64e
SM
7342 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7343 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7344
7345 /* Do we need to create a new group, or can we use an existing one? */
7346
7347 if (stmt_list)
7348 {
7349 line_offset = DW_UNSND (stmt_list);
7350 ++tu_stats->nr_symtab_sharers;
7351 }
7352 else
7353 {
7354 /* Ugh, no stmt_list. Rare, but we have to handle it.
7355 We can do various things here like create one group per TU or
7356 spread them over multiple groups to split up the expansion work.
7357 To avoid worst case scenarios (too many groups or too large groups)
7358 we, umm, group them in bunches. */
7359 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7360 | (tu_stats->nr_stmt_less_type_units
7361 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7362 ++tu_stats->nr_stmt_less_type_units;
7363 }
7364
094b34ac 7365 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7366 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7367 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7368 &type_unit_group_for_lookup, INSERT);
7369 if (*slot != NULL)
7370 {
9a3c8263 7371 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7372 gdb_assert (tu_group != NULL);
7373 }
7374 else
7375 {
9c541725 7376 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7377 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7378 *slot = tu_group;
7379 ++tu_stats->nr_symtabs;
7380 }
7381
7382 return tu_group;
7383}
0018ea6f
DE
7384\f
7385/* Partial symbol tables. */
7386
7387/* Create a psymtab named NAME and assign it to PER_CU.
7388
7389 The caller must fill in the following details:
7390 dirname, textlow, texthigh. */
7391
891813be 7392static dwarf2_psymtab *
0018ea6f
DE
7393create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7394{
e3b94546 7395 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7396 dwarf2_psymtab *pst;
0018ea6f 7397
9f4e76a4 7398 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7399
6d94535f 7400 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7401
7402 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7403 per_cu->v.psymtab = pst;
7404
7405 return pst;
7406}
7407
c0ab21c2 7408/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7409
7410static void
7411process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7412 const gdb_byte *info_ptr,
0018ea6f 7413 struct die_info *comp_unit_die,
c0ab21c2 7414 enum language pretend_language)
0018ea6f
DE
7415{
7416 struct dwarf2_cu *cu = reader->cu;
5e22e966 7417 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 7418 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7419 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7420 CORE_ADDR baseaddr;
7421 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7422 dwarf2_psymtab *pst;
3a2b436a 7423 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7424 const char *filename;
0018ea6f 7425
0018ea6f
DE
7426 gdb_assert (! per_cu->is_debug_types);
7427
c0ab21c2 7428 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7429
0018ea6f 7430 /* Allocate a new partial symbol table structure. */
2e927613
TV
7431 gdb::unique_xmalloc_ptr<char> debug_filename;
7432 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7433 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7434 if (filename == NULL)
0018ea6f 7435 filename = "";
2e927613
TV
7436 else if (strcmp (filename, artificial) == 0)
7437 {
7438 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7439 sect_offset_str (per_cu->sect_off),
7440 (char *) NULL));
2e927613
TV
7441 filename = debug_filename.get ();
7442 }
0018ea6f
DE
7443
7444 pst = create_partial_symtab (per_cu, filename);
7445
7446 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7447 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7448
b3b3bada 7449 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7450
7451 dwarf2_find_base_address (comp_unit_die, cu);
7452
7453 /* Possibly set the default values of LOWPC and HIGHPC from
7454 `DW_AT_ranges'. */
3a2b436a
JK
7455 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7456 &best_highpc, cu, pst);
7457 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7458 {
7459 CORE_ADDR low
7460 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7461 - baseaddr);
7462 CORE_ADDR high
7463 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7464 - baseaddr - 1);
7465 /* Store the contiguous range if it is not empty; it can be
7466 empty for CUs with no code. */
d320c2b5
TT
7467 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7468 low, high, pst);
79748972 7469 }
0018ea6f
DE
7470
7471 /* Check if comp unit has_children.
7472 If so, read the rest of the partial symbols from this comp unit.
7473 If not, there's no more debug_info for this comp unit. */
3e225074 7474 if (comp_unit_die->has_children)
0018ea6f
DE
7475 {
7476 struct partial_die_info *first_die;
7477 CORE_ADDR lowpc, highpc;
7478
7479 lowpc = ((CORE_ADDR) -1);
7480 highpc = ((CORE_ADDR) 0);
7481
7482 first_die = load_partial_dies (reader, info_ptr, 1);
7483
7484 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7485 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7486
7487 /* If we didn't find a lowpc, set it to highpc to avoid
7488 complaints from `maint check'. */
7489 if (lowpc == ((CORE_ADDR) -1))
7490 lowpc = highpc;
7491
7492 /* If the compilation unit didn't have an explicit address range,
7493 then use the information extracted from its child dies. */
e385593e 7494 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7495 {
7496 best_lowpc = lowpc;
7497 best_highpc = highpc;
7498 }
7499 }
4ae976d1 7500 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7501 best_lowpc + baseaddr)
7502 - baseaddr);
4ae976d1 7503 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7504 best_highpc + baseaddr)
7505 - baseaddr);
0018ea6f 7506
8763cede 7507 end_psymtab_common (objfile, pst);
0018ea6f 7508
ae640021 7509 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7510 {
7511 int i;
ae640021 7512 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7513
7514 /* Fill in 'dependencies' here; we fill in 'users' in a
7515 post-pass. */
7516 pst->number_of_dependencies = len;
a9342b62
TT
7517 pst->dependencies
7518 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7519 for (i = 0; i < len; ++i)
7520 {
7521 pst->dependencies[i]
7522 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7523 }
0018ea6f 7524
ae640021 7525 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7526 }
7527
7528 /* Get the list of files included in the current compilation unit,
7529 and build a psymtab for each of them. */
7530 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7531
b4f54984 7532 if (dwarf_read_debug)
b926417a
TT
7533 fprintf_unfiltered (gdb_stdlog,
7534 "Psymtab for %s unit @%s: %s - %s"
7535 ", %d global, %d static syms\n",
7536 per_cu->is_debug_types ? "type" : "comp",
7537 sect_offset_str (per_cu->sect_off),
7538 paddress (gdbarch, pst->text_low (objfile)),
7539 paddress (gdbarch, pst->text_high (objfile)),
7540 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7541}
7542
7543/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7544 Process compilation unit THIS_CU for a psymtab. */
7545
7546static void
7547process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7548 bool want_partial_unit,
b93601f3 7549 enum language pretend_language)
0018ea6f
DE
7550{
7551 /* If this compilation unit was already read in, free the
7552 cached copy in order to read it in again. This is
7553 necessary because we skipped some symbols when we first
7554 read in the compilation unit (see load_partial_dies).
7555 This problem could be avoided, but the benefit is unclear. */
7556 if (this_cu->cu != NULL)
7557 free_one_cached_comp_unit (this_cu);
7558
6751ebae 7559 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7560
58990295
TV
7561 switch (reader.comp_unit_die->tag)
7562 {
7563 case DW_TAG_compile_unit:
7564 this_cu->unit_type = DW_UT_compile;
7565 break;
7566 case DW_TAG_partial_unit:
7567 this_cu->unit_type = DW_UT_partial;
7568 break;
7569 default:
7570 abort ();
7571 }
7572
c0ab21c2 7573 if (reader.dummy_p)
f1902523 7574 {
c0ab21c2 7575 /* Nothing. */
f1902523 7576 }
c0ab21c2 7577 else if (this_cu->is_debug_types)
3e225074
TT
7578 build_type_psymtabs_reader (&reader, reader.info_ptr,
7579 reader.comp_unit_die);
135f5437
TT
7580 else if (want_partial_unit
7581 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7582 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7583 reader.comp_unit_die,
c0ab21c2 7584 pretend_language);
0018ea6f 7585
58990295
TV
7586 this_cu->lang = this_cu->cu->language;
7587
0018ea6f 7588 /* Age out any secondary CUs. */
ed2dc618 7589 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7590}
f4dc4d17
DE
7591
7592/* Reader function for build_type_psymtabs. */
7593
7594static void
7595build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7596 const gdb_byte *info_ptr,
3e225074 7597 struct die_info *type_unit_die)
f4dc4d17 7598{
5e22e966 7599 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7600 struct objfile *objfile = dwarf2_per_objfile->objfile;
7601 struct dwarf2_cu *cu = reader->cu;
7602 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7603 struct signatured_type *sig_type;
f4dc4d17
DE
7604 struct type_unit_group *tu_group;
7605 struct attribute *attr;
7606 struct partial_die_info *first_die;
7607 CORE_ADDR lowpc, highpc;
891813be 7608 dwarf2_psymtab *pst;
f4dc4d17 7609
0186c6a7
DE
7610 gdb_assert (per_cu->is_debug_types);
7611 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7612
3e225074 7613 if (! type_unit_die->has_children)
f4dc4d17
DE
7614 return;
7615
052c8bb8 7616 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7617 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7618
df07e2c7 7619 if (tu_group->tus == nullptr)
a8b3b8e9 7620 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7621 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7622
7623 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7624 pst = create_partial_symtab (per_cu, "");
6d94535f 7625 pst->anonymous = true;
f4dc4d17
DE
7626
7627 first_die = load_partial_dies (reader, info_ptr, 1);
7628
7629 lowpc = (CORE_ADDR) -1;
7630 highpc = (CORE_ADDR) 0;
7631 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7632
8763cede 7633 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7634}
7635
73051182
DE
7636/* Struct used to sort TUs by their abbreviation table offset. */
7637
7638struct tu_abbrev_offset
7639{
b2bdb8cf
SM
7640 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7641 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7642 {}
7643
7644 signatured_type *sig_type;
73051182
DE
7645 sect_offset abbrev_offset;
7646};
7647
484cf504 7648/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7649
484cf504
TT
7650static bool
7651sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7652 const struct tu_abbrev_offset &b)
73051182 7653{
484cf504 7654 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7655}
7656
7657/* Efficiently read all the type units.
7658 This does the bulk of the work for build_type_psymtabs.
7659
7660 The efficiency is because we sort TUs by the abbrev table they use and
7661 only read each abbrev table once. In one program there are 200K TUs
7662 sharing 8K abbrev tables.
7663
7664 The main purpose of this function is to support building the
5989a64e 7665 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7666 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7667 can collapse the search space by grouping them by stmt_list.
7668 The savings can be significant, in the same program from above the 200K TUs
7669 share 8K stmt_list tables.
7670
7671 FUNC is expected to call get_type_unit_group, which will create the
7672 struct type_unit_group if necessary and add it to
5989a64e 7673 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7674
7675static void
ed2dc618 7676build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7677{
5989a64e 7678 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7679 abbrev_table_up abbrev_table;
73051182 7680 sect_offset abbrev_offset;
73051182
DE
7681
7682 /* It's up to the caller to not call us multiple times. */
5989a64e 7683 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7684
5989a64e 7685 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7686 return;
7687
7688 /* TUs typically share abbrev tables, and there can be way more TUs than
7689 abbrev tables. Sort by abbrev table to reduce the number of times we
7690 read each abbrev table in.
7691 Alternatives are to punt or to maintain a cache of abbrev tables.
7692 This is simpler and efficient enough for now.
7693
7694 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7695 symtab to use). Typically TUs with the same abbrev offset have the same
7696 stmt_list value too so in practice this should work well.
7697
7698 The basic algorithm here is:
7699
7700 sort TUs by abbrev table
7701 for each TU with same abbrev table:
7702 read abbrev table if first user
7703 read TU top level DIE
7704 [IWBN if DWO skeletons had DW_AT_stmt_list]
7705 call FUNC */
7706
b4f54984 7707 if (dwarf_read_debug)
73051182
DE
7708 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7709
7710 /* Sort in a separate table to maintain the order of all_type_units
7711 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7712 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7713 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7714
5989a64e 7715 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7716 sorted_by_abbrev.emplace_back
7717 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7718 sig_type->per_cu.section,
7719 sig_type->per_cu.sect_off));
73051182 7720
484cf504
TT
7721 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7722 sort_tu_by_abbrev_offset);
73051182 7723
9c541725 7724 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7725
b2bdb8cf 7726 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7727 {
73051182
DE
7728 /* Switch to the next abbrev table if necessary. */
7729 if (abbrev_table == NULL
b2bdb8cf 7730 || tu.abbrev_offset != abbrev_offset)
73051182 7731 {
b2bdb8cf 7732 abbrev_offset = tu.abbrev_offset;
73051182 7733 abbrev_table =
86de1d91 7734 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7735 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7736 abbrev_offset);
73051182
DE
7737 ++tu_stats->nr_uniq_abbrev_tables;
7738 }
7739
c0ab21c2 7740 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7741 0, false);
c0ab21c2
TT
7742 if (!reader.dummy_p)
7743 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7744 reader.comp_unit_die);
73051182 7745 }
6aa5f3a6 7746}
73051182 7747
6aa5f3a6
DE
7748/* Print collected type unit statistics. */
7749
7750static void
ed2dc618 7751print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7752{
5989a64e 7753 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7754
7755 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7756 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7757 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7758 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7759 tu_stats->nr_uniq_abbrev_tables);
7760 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7761 tu_stats->nr_symtabs);
7762 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7763 tu_stats->nr_symtab_sharers);
7764 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7765 tu_stats->nr_stmt_less_type_units);
7766 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7767 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7768}
7769
f4dc4d17
DE
7770/* Traversal function for build_type_psymtabs. */
7771
7772static int
7773build_type_psymtab_dependencies (void **slot, void *info)
7774{
ed2dc618
SM
7775 struct dwarf2_per_objfile *dwarf2_per_objfile
7776 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7777 struct objfile *objfile = dwarf2_per_objfile->objfile;
7778 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7779 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7780 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7781 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7782 int i;
7783
7784 gdb_assert (len > 0);
197400e8 7785 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7786
7787 pst->number_of_dependencies = len;
a9342b62 7788 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7789 for (i = 0; i < len; ++i)
f4dc4d17 7790 {
df07e2c7 7791 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7792 gdb_assert (iter->per_cu.is_debug_types);
7793 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7794 iter->type_unit_group = tu_group;
f4dc4d17
DE
7795 }
7796
df07e2c7
AB
7797 delete tu_group->tus;
7798 tu_group->tus = nullptr;
348e048f
DE
7799
7800 return 1;
7801}
7802
7803/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7804 Build partial symbol tables for the .debug_types comp-units. */
7805
7806static void
ed2dc618 7807build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7808{
ed2dc618 7809 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7810 return;
7811
ed2dc618 7812 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7813}
f4dc4d17 7814
6aa5f3a6
DE
7815/* Traversal function for process_skeletonless_type_unit.
7816 Read a TU in a DWO file and build partial symbols for it. */
7817
7818static int
7819process_skeletonless_type_unit (void **slot, void *info)
7820{
7821 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7822 struct dwarf2_per_objfile *dwarf2_per_objfile
7823 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7824 struct signatured_type find_entry, *entry;
7825
7826 /* If this TU doesn't exist in the global table, add it and read it in. */
7827
5989a64e
SM
7828 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7829 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7830
7831 find_entry.signature = dwo_unit->signature;
5989a64e 7832 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7833 &find_entry, INSERT);
6aa5f3a6
DE
7834 /* If we've already seen this type there's nothing to do. What's happening
7835 is we're doing our own version of comdat-folding here. */
7836 if (*slot != NULL)
7837 return 1;
7838
7839 /* This does the job that create_all_type_units would have done for
7840 this TU. */
ed2dc618
SM
7841 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7842 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7843 *slot = entry;
7844
7845 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7846 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7847 if (!reader.dummy_p)
7848 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7849 reader.comp_unit_die);
6aa5f3a6
DE
7850
7851 return 1;
7852}
7853
7854/* Traversal function for process_skeletonless_type_units. */
7855
7856static int
7857process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7858{
7859 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7860
7861 if (dwo_file->tus != NULL)
b0b6a987
TT
7862 htab_traverse_noresize (dwo_file->tus.get (),
7863 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7864
7865 return 1;
7866}
7867
7868/* Scan all TUs of DWO files, verifying we've processed them.
7869 This is needed in case a TU was emitted without its skeleton.
7870 Note: This can't be done until we know what all the DWO files are. */
7871
7872static void
ed2dc618 7873process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7874{
7875 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7876 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7877 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7878 {
5989a64e 7879 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7880 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7881 dwarf2_per_objfile);
6aa5f3a6 7882 }
348e048f
DE
7883}
7884
ed2dc618 7885/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7886
7887static void
ed2dc618 7888set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7889{
5989a64e 7890 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7891 {
891813be 7892 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7893
36586728
TT
7894 if (pst == NULL)
7895 continue;
7896
b76e467d 7897 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7898 {
7899 /* Set the 'user' field only if it is not already set. */
7900 if (pst->dependencies[j]->user == NULL)
7901 pst->dependencies[j]->user = pst;
7902 }
7903 }
7904}
7905
93311388
DE
7906/* Build the partial symbol table by doing a quick pass through the
7907 .debug_info and .debug_abbrev sections. */
72bf9492 7908
93311388 7909static void
ed2dc618 7910dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7911{
ed2dc618 7912 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7913
b4f54984 7914 if (dwarf_read_debug)
45cfd468
DE
7915 {
7916 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7917 objfile_name (objfile));
45cfd468
DE
7918 }
7919
76935768 7920 scoped_restore restore_reading_psyms
5989a64e 7921 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7922 true);
98bfdba5 7923
5989a64e 7924 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7925
93311388
DE
7926 /* Any cached compilation units will be linked by the per-objfile
7927 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7928 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7929
ed2dc618 7930 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7931
ed2dc618 7932 create_all_comp_units (dwarf2_per_objfile);
c906108c 7933
60606b2c
TT
7934 /* Create a temporary address map on a temporary obstack. We later
7935 copy this to the final obstack. */
8268c778 7936 auto_obstack temp_obstack;
791afaa2
TT
7937
7938 scoped_restore save_psymtabs_addrmap
d320c2b5 7939 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7940 addrmap_create_mutable (&temp_obstack));
72bf9492 7941
5989a64e 7942 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7943 {
7944 if (per_cu->v.psymtab != NULL)
7945 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7946 continue;
7947 process_psymtab_comp_unit (per_cu, false, language_minimal);
7948 }
ff013f42 7949
6aa5f3a6 7950 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7951 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7952
7953 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7954 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7955 {
5989a64e 7956 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7957 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7958 }
7959
b4f54984 7960 if (dwarf_read_debug)
ed2dc618 7961 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7962
ed2dc618 7963 set_partial_user (dwarf2_per_objfile);
95554aad 7964
d320c2b5
TT
7965 objfile->partial_symtabs->psymtabs_addrmap
7966 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7967 objfile->partial_symtabs->obstack ());
791afaa2
TT
7968 /* At this point we want to keep the address map. */
7969 save_psymtabs_addrmap.release ();
ff013f42 7970
b4f54984 7971 if (dwarf_read_debug)
45cfd468 7972 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7973 objfile_name (objfile));
ae038cb0
DJ
7974}
7975
dee91e82
DE
7976/* Load the partial DIEs for a secondary CU into memory.
7977 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7978
dee91e82
DE
7979static void
7980load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7981{
6751ebae 7982 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7983
7984 if (!reader.dummy_p)
7985 {
7986 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7987 language_minimal);
7988
7989 /* Check if comp unit has_children.
7990 If so, read the rest of the partial symbols from this comp unit.
7991 If not, there's no more debug_info for this comp unit. */
3e225074 7992 if (reader.comp_unit_die->has_children)
c0ab21c2 7993 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7994
7995 reader.keep ();
c0ab21c2 7996 }
ae038cb0
DJ
7997}
7998
ae038cb0 7999static void
ed2dc618 8000read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8001 struct dwarf2_section_info *section,
f1902523 8002 struct dwarf2_section_info *abbrev_section,
b76e467d 8003 unsigned int is_dwz)
ae038cb0 8004{
d521ce57 8005 const gdb_byte *info_ptr;
ed2dc618 8006 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8007
b4f54984 8008 if (dwarf_read_debug)
bf6af496 8009 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8010 section->get_name (),
8011 section->get_file_name ());
bf6af496 8012
96b79293 8013 section->read (objfile);
ae038cb0 8014
36586728 8015 info_ptr = section->buffer;
6e70227d 8016
36586728 8017 while (info_ptr < section->buffer + section->size)
ae038cb0 8018 {
ae038cb0 8019 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8020
9c541725 8021 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8022
f1902523 8023 comp_unit_head cu_header;
ed2dc618
SM
8024 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8025 abbrev_section, info_ptr,
8026 rcuh_kind::COMPILE);
ae038cb0
DJ
8027
8028 /* Save the compilation unit for later lookup. */
f1902523 8029 if (cu_header.unit_type != DW_UT_type)
5989a64e 8030 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8031 else
8032 {
5989a64e 8033 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8034 sig_type->signature = cu_header.signature;
8035 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8036 this_cu = &sig_type->per_cu;
8037 }
8038 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8039 this_cu->sect_off = sect_off;
f1902523 8040 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8041 this_cu->is_dwz = is_dwz;
e3b94546 8042 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8043 this_cu->section = section;
ae038cb0 8044
5989a64e 8045 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8046
8047 info_ptr = info_ptr + this_cu->length;
8048 }
36586728
TT
8049}
8050
8051/* Create a list of all compilation units in OBJFILE.
8052 This is only done for -readnow and building partial symtabs. */
8053
8054static void
ed2dc618 8055create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8056{
5989a64e
SM
8057 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8058 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8059 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8060
c3699833 8061 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8062 if (dwz != NULL)
ed2dc618 8063 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8064 1);
c906108c
SS
8065}
8066
5734ee8b 8067/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8068 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8069 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8070 DW_AT_ranges). See the comments of add_partial_subprogram on how
8071 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8072
72bf9492
DJ
8073static void
8074scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8075 CORE_ADDR *highpc, int set_addrmap,
8076 struct dwarf2_cu *cu)
c906108c 8077{
72bf9492 8078 struct partial_die_info *pdi;
c906108c 8079
91c24f0a
DC
8080 /* Now, march along the PDI's, descending into ones which have
8081 interesting children but skipping the children of the other ones,
8082 until we reach the end of the compilation unit. */
c906108c 8083
72bf9492 8084 pdi = first_die;
91c24f0a 8085
72bf9492
DJ
8086 while (pdi != NULL)
8087 {
52356b79 8088 pdi->fixup (cu);
c906108c 8089
f55ee35c 8090 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8091 children, so we need to look at them. Ditto for anonymous
8092 enums. */
933c6fe4 8093
72bf9492 8094 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8095 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8096 || pdi->tag == DW_TAG_imported_unit
8097 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8098 {
72bf9492 8099 switch (pdi->tag)
c906108c
SS
8100 {
8101 case DW_TAG_subprogram:
b1dc1806 8102 case DW_TAG_inlined_subroutine:
cdc07690 8103 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8104 break;
72929c62 8105 case DW_TAG_constant:
c906108c
SS
8106 case DW_TAG_variable:
8107 case DW_TAG_typedef:
91c24f0a 8108 case DW_TAG_union_type:
317d2668
TV
8109 if (!pdi->is_declaration
8110 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8111 {
72bf9492 8112 add_partial_symbol (pdi, cu);
63d06c5c
DC
8113 }
8114 break;
c906108c 8115 case DW_TAG_class_type:
680b30c7 8116 case DW_TAG_interface_type:
c906108c 8117 case DW_TAG_structure_type:
72bf9492 8118 if (!pdi->is_declaration)
c906108c 8119 {
72bf9492 8120 add_partial_symbol (pdi, cu);
c906108c 8121 }
b7fee5a3
KS
8122 if ((cu->language == language_rust
8123 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8124 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8125 set_addrmap, cu);
c906108c 8126 break;
91c24f0a 8127 case DW_TAG_enumeration_type:
72bf9492
DJ
8128 if (!pdi->is_declaration)
8129 add_partial_enumeration (pdi, cu);
c906108c
SS
8130 break;
8131 case DW_TAG_base_type:
a02abb62 8132 case DW_TAG_subrange_type:
c906108c 8133 /* File scope base type definitions are added to the partial
c5aa993b 8134 symbol table. */
72bf9492 8135 add_partial_symbol (pdi, cu);
c906108c 8136 break;
d9fa45fe 8137 case DW_TAG_namespace:
cdc07690 8138 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8139 break;
5d7cb8df 8140 case DW_TAG_module:
59c35742
AB
8141 if (!pdi->is_declaration)
8142 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8143 break;
95554aad
TT
8144 case DW_TAG_imported_unit:
8145 {
8146 struct dwarf2_per_cu_data *per_cu;
8147
f4dc4d17
DE
8148 /* For now we don't handle imported units in type units. */
8149 if (cu->per_cu->is_debug_types)
8150 {
8151 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8152 " supported in type units [in module %s]"),
5e22e966 8153 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8154 }
8155
e3b94546 8156 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8157 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8158
8159 /* Go read the partial unit, if needed. */
8160 if (per_cu->v.psymtab == NULL)
135f5437 8161 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8162
ae640021 8163 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8164 }
8165 break;
74921315
KS
8166 case DW_TAG_imported_declaration:
8167 add_partial_symbol (pdi, cu);
8168 break;
c906108c
SS
8169 default:
8170 break;
8171 }
8172 }
8173
72bf9492
DJ
8174 /* If the die has a sibling, skip to the sibling. */
8175
8176 pdi = pdi->die_sibling;
8177 }
8178}
8179
8180/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8181
72bf9492 8182 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8183 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8184 Enumerators are an exception; they use the scope of their parent
8185 enumeration type, i.e. the name of the enumeration type is not
8186 prepended to the enumerator.
91c24f0a 8187
72bf9492
DJ
8188 There are two complexities. One is DW_AT_specification; in this
8189 case "parent" means the parent of the target of the specification,
8190 instead of the direct parent of the DIE. The other is compilers
8191 which do not emit DW_TAG_namespace; in this case we try to guess
8192 the fully qualified name of structure types from their members'
8193 linkage names. This must be done using the DIE's children rather
8194 than the children of any DW_AT_specification target. We only need
8195 to do this for structures at the top level, i.e. if the target of
8196 any DW_AT_specification (if any; otherwise the DIE itself) does not
8197 have a parent. */
8198
8199/* Compute the scope prefix associated with PDI's parent, in
8200 compilation unit CU. The result will be allocated on CU's
8201 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8202 field. NULL is returned if no prefix is necessary. */
15d034d0 8203static const char *
72bf9492
DJ
8204partial_die_parent_scope (struct partial_die_info *pdi,
8205 struct dwarf2_cu *cu)
8206{
15d034d0 8207 const char *grandparent_scope;
72bf9492 8208 struct partial_die_info *parent, *real_pdi;
91c24f0a 8209
72bf9492
DJ
8210 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8211 then this means the parent of the specification DIE. */
8212
8213 real_pdi = pdi;
72bf9492 8214 while (real_pdi->has_specification)
fb816e8b 8215 {
122cf0f2
AB
8216 auto res = find_partial_die (real_pdi->spec_offset,
8217 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8218 real_pdi = res.pdi;
8219 cu = res.cu;
8220 }
72bf9492
DJ
8221
8222 parent = real_pdi->die_parent;
8223 if (parent == NULL)
8224 return NULL;
8225
8226 if (parent->scope_set)
8227 return parent->scope;
8228
52356b79 8229 parent->fixup (cu);
72bf9492 8230
10b3939b 8231 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8232
acebe513
UW
8233 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8234 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8235 Work around this problem here. */
8236 if (cu->language == language_cplus
6e70227d 8237 && parent->tag == DW_TAG_namespace
acebe513
UW
8238 && strcmp (parent->name, "::") == 0
8239 && grandparent_scope == NULL)
8240 {
8241 parent->scope = NULL;
8242 parent->scope_set = 1;
8243 return NULL;
8244 }
8245
0a4b0913 8246 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8247 if (pdi->tag == DW_TAG_enumerator)
8248 /* Enumerators should not get the name of the enumeration as a prefix. */
8249 parent->scope = grandparent_scope;
8250 else if (parent->tag == DW_TAG_namespace
f55ee35c 8251 || parent->tag == DW_TAG_module
72bf9492
DJ
8252 || parent->tag == DW_TAG_structure_type
8253 || parent->tag == DW_TAG_class_type
680b30c7 8254 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8255 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8256 || parent->tag == DW_TAG_enumeration_type
8257 || (cu->language == language_fortran
8258 && parent->tag == DW_TAG_subprogram
8259 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8260 {
8261 if (grandparent_scope == NULL)
8262 parent->scope = parent->name;
8263 else
3e43a32a
MS
8264 parent->scope = typename_concat (&cu->comp_unit_obstack,
8265 grandparent_scope,
f55ee35c 8266 parent->name, 0, cu);
72bf9492 8267 }
72bf9492
DJ
8268 else
8269 {
8270 /* FIXME drow/2004-04-01: What should we be doing with
8271 function-local names? For partial symbols, we should probably be
8272 ignoring them. */
fa9c3fa0
TT
8273 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8274 dwarf_tag_name (parent->tag),
8275 sect_offset_str (pdi->sect_off));
72bf9492 8276 parent->scope = grandparent_scope;
c906108c
SS
8277 }
8278
72bf9492
DJ
8279 parent->scope_set = 1;
8280 return parent->scope;
8281}
8282
8283/* Return the fully scoped name associated with PDI, from compilation unit
8284 CU. The result will be allocated with malloc. */
4568ecf9 8285
43816ebc 8286static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8287partial_die_full_name (struct partial_die_info *pdi,
8288 struct dwarf2_cu *cu)
8289{
15d034d0 8290 const char *parent_scope;
72bf9492 8291
98bfdba5
PA
8292 /* If this is a template instantiation, we can not work out the
8293 template arguments from partial DIEs. So, unfortunately, we have
8294 to go through the full DIEs. At least any work we do building
8295 types here will be reused if full symbols are loaded later. */
8296 if (pdi->has_template_arguments)
8297 {
52356b79 8298 pdi->fixup (cu);
98bfdba5
PA
8299
8300 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8301 {
8302 struct die_info *die;
8303 struct attribute attr;
8304 struct dwarf2_cu *ref_cu = cu;
8305
b64f50a1 8306 /* DW_FORM_ref_addr is using section offset. */
b4069958 8307 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8308 attr.form = DW_FORM_ref_addr;
9c541725 8309 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8310 die = follow_die_ref (NULL, &attr, &ref_cu);
8311
43816ebc 8312 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8313 }
8314 }
8315
72bf9492
DJ
8316 parent_scope = partial_die_parent_scope (pdi, cu);
8317 if (parent_scope == NULL)
8318 return NULL;
8319 else
43816ebc
TT
8320 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8321 pdi->name, 0, cu));
c906108c
SS
8322}
8323
8324static void
72bf9492 8325add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8326{
5e22e966 8327 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8328 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8329 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8330 CORE_ADDR addr = 0;
15d034d0 8331 const char *actual_name = NULL;
e142c38c
DJ
8332 CORE_ADDR baseaddr;
8333
b3b3bada 8334 baseaddr = objfile->text_section_offset ();
c906108c 8335
43816ebc
TT
8336 gdb::unique_xmalloc_ptr<char> built_actual_name
8337 = partial_die_full_name (pdi, cu);
15d034d0 8338 if (built_actual_name != NULL)
43816ebc 8339 actual_name = built_actual_name.get ();
63d06c5c 8340
72bf9492
DJ
8341 if (actual_name == NULL)
8342 actual_name = pdi->name;
8343
76e288d1
TT
8344 partial_symbol psymbol;
8345 memset (&psymbol, 0, sizeof (psymbol));
8346 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8347 psymbol.ginfo.section = -1;
8348
8349 /* The code below indicates that the psymbol should be installed by
8350 setting this. */
8351 gdb::optional<psymbol_placement> where;
8352
c906108c
SS
8353 switch (pdi->tag)
8354 {
b1dc1806 8355 case DW_TAG_inlined_subroutine:
c906108c 8356 case DW_TAG_subprogram:
79748972
TT
8357 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8358 - baseaddr);
0a4b0913
AB
8359 if (pdi->is_external
8360 || cu->language == language_ada
8361 || (cu->language == language_fortran
8362 && pdi->die_parent != NULL
8363 && pdi->die_parent->tag == DW_TAG_subprogram))
8364 {
8365 /* Normally, only "external" DIEs are part of the global scope.
8366 But in Ada and Fortran, we want to be able to access nested
8367 procedures globally. So all Ada and Fortran subprograms are
8368 stored in the global scope. */
76e288d1 8369 where = psymbol_placement::GLOBAL;
c906108c
SS
8370 }
8371 else
76e288d1
TT
8372 where = psymbol_placement::STATIC;
8373
8374 psymbol.domain = VAR_DOMAIN;
8375 psymbol.aclass = LOC_BLOCK;
8376 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8377 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8378
8379 if (pdi->main_subprogram && actual_name != NULL)
8380 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8381 break;
72929c62 8382 case DW_TAG_constant:
76e288d1
TT
8383 psymbol.domain = VAR_DOMAIN;
8384 psymbol.aclass = LOC_STATIC;
8385 where = (pdi->is_external
8386 ? psymbol_placement::GLOBAL
8387 : psymbol_placement::STATIC);
72929c62 8388 break;
c906108c 8389 case DW_TAG_variable:
95554aad
TT
8390 if (pdi->d.locdesc)
8391 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8392
95554aad 8393 if (pdi->d.locdesc
caac4577 8394 && addr == 0
5989a64e 8395 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8396 {
8397 /* A global or static variable may also have been stripped
8398 out by the linker if unused, in which case its address
8399 will be nullified; do not add such variables into partial
8400 symbol table then. */
8401 }
8402 else if (pdi->is_external)
c906108c
SS
8403 {
8404 /* Global Variable.
8405 Don't enter into the minimal symbol tables as there is
8406 a minimal symbol table entry from the ELF symbols already.
8407 Enter into partial symbol table if it has a location
8408 descriptor or a type.
8409 If the location descriptor is missing, new_symbol will create
8410 a LOC_UNRESOLVED symbol, the address of the variable will then
8411 be determined from the minimal symbol table whenever the variable
8412 is referenced.
8413 The address for the partial symbol table entry is not
8414 used by GDB, but it comes in handy for debugging partial symbol
8415 table building. */
8416
95554aad 8417 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8418 {
8419 psymbol.domain = VAR_DOMAIN;
8420 psymbol.aclass = LOC_STATIC;
8421 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8422 psymbol.ginfo.value.address = addr;
8423 where = psymbol_placement::GLOBAL;
8424 }
c906108c
SS
8425 }
8426 else
8427 {
ff908ebf
AW
8428 int has_loc = pdi->d.locdesc != NULL;
8429
8430 /* Static Variable. Skip symbols whose value we cannot know (those
8431 without location descriptors or constant values). */
8432 if (!has_loc && !pdi->has_const_value)
43816ebc 8433 return;
ff908ebf 8434
76e288d1
TT
8435 psymbol.domain = VAR_DOMAIN;
8436 psymbol.aclass = LOC_STATIC;
8437 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8438 if (has_loc)
8439 psymbol.ginfo.value.address = addr;
8440 where = psymbol_placement::STATIC;
c906108c
SS
8441 }
8442 break;
8443 case DW_TAG_typedef:
8444 case DW_TAG_base_type:
a02abb62 8445 case DW_TAG_subrange_type:
76e288d1
TT
8446 psymbol.domain = VAR_DOMAIN;
8447 psymbol.aclass = LOC_TYPEDEF;
8448 where = psymbol_placement::STATIC;
c906108c 8449 break;
74921315 8450 case DW_TAG_imported_declaration:
72bf9492 8451 case DW_TAG_namespace:
76e288d1
TT
8452 psymbol.domain = VAR_DOMAIN;
8453 psymbol.aclass = LOC_TYPEDEF;
8454 where = psymbol_placement::GLOBAL;
72bf9492 8455 break;
530e8392 8456 case DW_TAG_module:
a5fd13a9
BH
8457 /* With Fortran 77 there might be a "BLOCK DATA" module
8458 available without any name. If so, we skip the module as it
8459 doesn't bring any value. */
8460 if (actual_name != nullptr)
76e288d1
TT
8461 {
8462 psymbol.domain = MODULE_DOMAIN;
8463 psymbol.aclass = LOC_TYPEDEF;
8464 where = psymbol_placement::GLOBAL;
8465 }
530e8392 8466 break;
c906108c 8467 case DW_TAG_class_type:
680b30c7 8468 case DW_TAG_interface_type:
c906108c
SS
8469 case DW_TAG_structure_type:
8470 case DW_TAG_union_type:
8471 case DW_TAG_enumeration_type:
fa4028e9
JB
8472 /* Skip external references. The DWARF standard says in the section
8473 about "Structure, Union, and Class Type Entries": "An incomplete
8474 structure, union or class type is represented by a structure,
8475 union or class entry that does not have a byte size attribute
8476 and that has a DW_AT_declaration attribute." */
8477 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8478 return;
fa4028e9 8479
63d06c5c
DC
8480 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8481 static vs. global. */
76e288d1
TT
8482 psymbol.domain = STRUCT_DOMAIN;
8483 psymbol.aclass = LOC_TYPEDEF;
8484 where = (cu->language == language_cplus
8485 ? psymbol_placement::GLOBAL
8486 : psymbol_placement::STATIC);
c906108c
SS
8487 break;
8488 case DW_TAG_enumerator:
76e288d1
TT
8489 psymbol.domain = VAR_DOMAIN;
8490 psymbol.aclass = LOC_CONST;
8491 where = (cu->language == language_cplus
8492 ? psymbol_placement::GLOBAL
8493 : psymbol_placement::STATIC);
c906108c
SS
8494 break;
8495 default:
8496 break;
8497 }
76e288d1
TT
8498
8499 if (where.has_value ())
8500 {
f049a313
TT
8501 if (built_actual_name != nullptr)
8502 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8503 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8504 psymbol.ginfo.set_linkage_name (actual_name);
8505 else
8506 {
8507 psymbol.ginfo.set_demangled_name (actual_name,
8508 &objfile->objfile_obstack);
8509 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8510 }
76e288d1
TT
8511 add_psymbol_to_list (psymbol, *where, objfile);
8512 }
c906108c
SS
8513}
8514
5c4e30ca
DC
8515/* Read a partial die corresponding to a namespace; also, add a symbol
8516 corresponding to that namespace to the symbol table. NAMESPACE is
8517 the name of the enclosing namespace. */
91c24f0a 8518
72bf9492
DJ
8519static void
8520add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8521 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8522 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8523{
72bf9492 8524 /* Add a symbol for the namespace. */
e7c27a73 8525
72bf9492 8526 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8527
8528 /* Now scan partial symbols in that namespace. */
8529
91c24f0a 8530 if (pdi->has_children)
cdc07690 8531 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8532}
8533
5d7cb8df
JK
8534/* Read a partial die corresponding to a Fortran module. */
8535
8536static void
8537add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8538 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8539{
530e8392
KB
8540 /* Add a symbol for the namespace. */
8541
8542 add_partial_symbol (pdi, cu);
8543
f55ee35c 8544 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8545
8546 if (pdi->has_children)
cdc07690 8547 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8548}
8549
b1dc1806
XR
8550/* Read a partial die corresponding to a subprogram or an inlined
8551 subprogram and create a partial symbol for that subprogram.
8552 When the CU language allows it, this routine also defines a partial
8553 symbol for each nested subprogram that this subprogram contains.
8554 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8555 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8556
cdc07690
YQ
8557 PDI may also be a lexical block, in which case we simply search
8558 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8559 Again, this is only performed when the CU language allows this
8560 type of definitions. */
8561
8562static void
8563add_partial_subprogram (struct partial_die_info *pdi,
8564 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8565 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8566{
b1dc1806 8567 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8568 {
8569 if (pdi->has_pc_info)
8570 {
8571 if (pdi->lowpc < *lowpc)
8572 *lowpc = pdi->lowpc;
8573 if (pdi->highpc > *highpc)
8574 *highpc = pdi->highpc;
cdc07690 8575 if (set_addrmap)
5734ee8b 8576 {
5e22e966 8577 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8578 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8579 CORE_ADDR baseaddr;
b926417a
TT
8580 CORE_ADDR this_highpc;
8581 CORE_ADDR this_lowpc;
5734ee8b 8582
b3b3bada 8583 baseaddr = objfile->text_section_offset ();
b926417a
TT
8584 this_lowpc
8585 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8586 pdi->lowpc + baseaddr)
8587 - baseaddr);
8588 this_highpc
8589 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8590 pdi->highpc + baseaddr)
8591 - baseaddr);
d320c2b5 8592 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8593 this_lowpc, this_highpc - 1,
9291a0cd 8594 cu->per_cu->v.psymtab);
5734ee8b 8595 }
481860b3
GB
8596 }
8597
8598 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8599 {
bc30ff58 8600 if (!pdi->is_declaration)
e8d05480
JB
8601 /* Ignore subprogram DIEs that do not have a name, they are
8602 illegal. Do not emit a complaint at this point, we will
8603 do so when we convert this psymtab into a symtab. */
8604 if (pdi->name)
8605 add_partial_symbol (pdi, cu);
bc30ff58
JB
8606 }
8607 }
6e70227d 8608
bc30ff58
JB
8609 if (! pdi->has_children)
8610 return;
8611
0a4b0913 8612 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8613 {
8614 pdi = pdi->die_child;
8615 while (pdi != NULL)
8616 {
52356b79 8617 pdi->fixup (cu);
bc30ff58 8618 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8619 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8620 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8621 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8622 pdi = pdi->die_sibling;
8623 }
8624 }
8625}
8626
91c24f0a
DC
8627/* Read a partial die corresponding to an enumeration type. */
8628
72bf9492
DJ
8629static void
8630add_partial_enumeration (struct partial_die_info *enum_pdi,
8631 struct dwarf2_cu *cu)
91c24f0a 8632{
72bf9492 8633 struct partial_die_info *pdi;
91c24f0a
DC
8634
8635 if (enum_pdi->name != NULL)
72bf9492
DJ
8636 add_partial_symbol (enum_pdi, cu);
8637
8638 pdi = enum_pdi->die_child;
8639 while (pdi)
91c24f0a 8640 {
72bf9492 8641 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8642 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8643 else
72bf9492
DJ
8644 add_partial_symbol (pdi, cu);
8645 pdi = pdi->die_sibling;
91c24f0a 8646 }
91c24f0a
DC
8647}
8648
6caca83c
CC
8649/* Return the initial uleb128 in the die at INFO_PTR. */
8650
8651static unsigned int
d521ce57 8652peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8653{
8654 unsigned int bytes_read;
8655
8656 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8657}
8658
685af9cd
TT
8659/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8660 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8661
4bb7a0a7
DJ
8662 Return the corresponding abbrev, or NULL if the number is zero (indicating
8663 an empty DIE). In either case *BYTES_READ will be set to the length of
8664 the initial number. */
8665
8666static struct abbrev_info *
685af9cd
TT
8667peek_die_abbrev (const die_reader_specs &reader,
8668 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8669{
685af9cd 8670 dwarf2_cu *cu = reader.cu;
5e22e966 8671 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8672 unsigned int abbrev_number
8673 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8674
8675 if (abbrev_number == 0)
8676 return NULL;
8677
685af9cd 8678 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8679 if (!abbrev)
8680 {
422b9917 8681 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8682 " at offset %s [in module %s]"),
422b9917 8683 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8684 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8685 }
8686
8687 return abbrev;
8688}
8689
93311388
DE
8690/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8691 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8692 DIE. Any children of the skipped DIEs will also be skipped. */
8693
d521ce57
TT
8694static const gdb_byte *
8695skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8696{
4bb7a0a7
DJ
8697 while (1)
8698 {
685af9cd
TT
8699 unsigned int bytes_read;
8700 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8701
4bb7a0a7
DJ
8702 if (abbrev == NULL)
8703 return info_ptr + bytes_read;
8704 else
dee91e82 8705 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8706 }
8707}
8708
93311388
DE
8709/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8710 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8711 abbrev corresponding to that skipped uleb128 should be passed in
8712 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8713 children. */
8714
d521ce57
TT
8715static const gdb_byte *
8716skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8717 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8718{
8719 unsigned int bytes_read;
8720 struct attribute attr;
dee91e82
DE
8721 bfd *abfd = reader->abfd;
8722 struct dwarf2_cu *cu = reader->cu;
d521ce57 8723 const gdb_byte *buffer = reader->buffer;
f664829e 8724 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8725 unsigned int form, i;
8726
8727 for (i = 0; i < abbrev->num_attrs; i++)
8728 {
8729 /* The only abbrev we care about is DW_AT_sibling. */
8730 if (abbrev->attrs[i].name == DW_AT_sibling)
8731 {
18a8505e
AT
8732 bool ignored;
8733 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8734 &ignored);
4bb7a0a7 8735 if (attr.form == DW_FORM_ref_addr)
b98664d3 8736 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8737 else
b9502d3f 8738 {
0826b30a 8739 sect_offset off = attr.get_ref_die_offset ();
9c541725 8740 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8741
8742 if (sibling_ptr < info_ptr)
b98664d3 8743 complaint (_("DW_AT_sibling points backwards"));
22869d73 8744 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8745 reader->die_section->overflow_complaint ();
b9502d3f
WN
8746 else
8747 return sibling_ptr;
8748 }
4bb7a0a7
DJ
8749 }
8750
8751 /* If it isn't DW_AT_sibling, skip this attribute. */
8752 form = abbrev->attrs[i].form;
8753 skip_attribute:
8754 switch (form)
8755 {
4bb7a0a7 8756 case DW_FORM_ref_addr:
ae411497
TT
8757 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8758 and later it is offset sized. */
8759 if (cu->header.version == 2)
8760 info_ptr += cu->header.addr_size;
8761 else
8762 info_ptr += cu->header.offset_size;
8763 break;
36586728
TT
8764 case DW_FORM_GNU_ref_alt:
8765 info_ptr += cu->header.offset_size;
8766 break;
ae411497 8767 case DW_FORM_addr:
4bb7a0a7
DJ
8768 info_ptr += cu->header.addr_size;
8769 break;
8770 case DW_FORM_data1:
8771 case DW_FORM_ref1:
8772 case DW_FORM_flag:
8fe0f950 8773 case DW_FORM_strx1:
4bb7a0a7
DJ
8774 info_ptr += 1;
8775 break;
2dc7f7b3 8776 case DW_FORM_flag_present:
43988095 8777 case DW_FORM_implicit_const:
2dc7f7b3 8778 break;
4bb7a0a7
DJ
8779 case DW_FORM_data2:
8780 case DW_FORM_ref2:
8fe0f950 8781 case DW_FORM_strx2:
4bb7a0a7
DJ
8782 info_ptr += 2;
8783 break;
8fe0f950
AT
8784 case DW_FORM_strx3:
8785 info_ptr += 3;
8786 break;
4bb7a0a7
DJ
8787 case DW_FORM_data4:
8788 case DW_FORM_ref4:
8fe0f950 8789 case DW_FORM_strx4:
4bb7a0a7
DJ
8790 info_ptr += 4;
8791 break;
8792 case DW_FORM_data8:
8793 case DW_FORM_ref8:
55f1336d 8794 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8795 info_ptr += 8;
8796 break;
0224619f
JK
8797 case DW_FORM_data16:
8798 info_ptr += 16;
8799 break;
4bb7a0a7 8800 case DW_FORM_string:
9b1c24c8 8801 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8802 info_ptr += bytes_read;
8803 break;
2dc7f7b3 8804 case DW_FORM_sec_offset:
4bb7a0a7 8805 case DW_FORM_strp:
36586728 8806 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8807 info_ptr += cu->header.offset_size;
8808 break;
2dc7f7b3 8809 case DW_FORM_exprloc:
4bb7a0a7
DJ
8810 case DW_FORM_block:
8811 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8812 info_ptr += bytes_read;
8813 break;
8814 case DW_FORM_block1:
8815 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8816 break;
8817 case DW_FORM_block2:
8818 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8819 break;
8820 case DW_FORM_block4:
8821 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8822 break;
336d760d 8823 case DW_FORM_addrx:
cf532bd1 8824 case DW_FORM_strx:
4bb7a0a7
DJ
8825 case DW_FORM_sdata:
8826 case DW_FORM_udata:
8827 case DW_FORM_ref_udata:
3019eac3
DE
8828 case DW_FORM_GNU_addr_index:
8829 case DW_FORM_GNU_str_index:
18a8505e 8830 case DW_FORM_rnglistx:
41144253 8831 case DW_FORM_loclistx:
d521ce57 8832 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8833 break;
8834 case DW_FORM_indirect:
8835 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8836 info_ptr += bytes_read;
8837 /* We need to continue parsing from here, so just go back to
8838 the top. */
8839 goto skip_attribute;
8840
8841 default:
3e43a32a
MS
8842 error (_("Dwarf Error: Cannot handle %s "
8843 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8844 dwarf_form_name (form),
8845 bfd_get_filename (abfd));
8846 }
8847 }
8848
8849 if (abbrev->has_children)
dee91e82 8850 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8851 else
8852 return info_ptr;
8853}
8854
93311388 8855/* Locate ORIG_PDI's sibling.
dee91e82 8856 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8857
d521ce57 8858static const gdb_byte *
dee91e82
DE
8859locate_pdi_sibling (const struct die_reader_specs *reader,
8860 struct partial_die_info *orig_pdi,
d521ce57 8861 const gdb_byte *info_ptr)
91c24f0a
DC
8862{
8863 /* Do we know the sibling already? */
72bf9492 8864
91c24f0a
DC
8865 if (orig_pdi->sibling)
8866 return orig_pdi->sibling;
8867
8868 /* Are there any children to deal with? */
8869
8870 if (!orig_pdi->has_children)
8871 return info_ptr;
8872
4bb7a0a7 8873 /* Skip the children the long way. */
91c24f0a 8874
dee91e82 8875 return skip_children (reader, info_ptr);
91c24f0a
DC
8876}
8877
257e7a09 8878/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8879 not NULL. */
c906108c 8880
891813be
TT
8881void
8882dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8883{
ed2dc618
SM
8884 struct dwarf2_per_objfile *dwarf2_per_objfile
8885 = get_dwarf2_per_objfile (objfile);
8886
af758d11
SM
8887 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8888
077cbab2
TT
8889 /* If this psymtab is constructed from a debug-only objfile, the
8890 has_section_at_zero flag will not necessarily be correct. We
8891 can get the correct value for this flag by looking at the data
8892 associated with the (presumably stripped) associated objfile. */
8893 if (objfile->separate_debug_objfile_backlink)
c906108c 8894 {
077cbab2
TT
8895 struct dwarf2_per_objfile *dpo_backlink
8896 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8897
5989a64e
SM
8898 dwarf2_per_objfile->per_bfd->has_section_at_zero
8899 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8900 }
98bfdba5 8901
8566b89b 8902 expand_psymtab (objfile);
95554aad 8903
ed2dc618 8904 process_cu_includes (dwarf2_per_objfile);
c906108c 8905}
9cdd5dbd
DE
8906\f
8907/* Reading in full CUs. */
c906108c 8908
10b3939b
DJ
8909/* Add PER_CU to the queue. */
8910
8911static void
95554aad
TT
8912queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8913 enum language pretend_language)
10b3939b 8914{
10b3939b 8915 per_cu->queued = 1;
1859c670 8916 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8917}
8918
89e63ee4
DE
8919/* If PER_CU is not yet queued, add it to the queue.
8920 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8921 dependency.
0907af0c 8922 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8923 meaning either PER_CU is already queued or it is already loaded.
8924
8925 N.B. There is an invariant here that if a CU is queued then it is loaded.
8926 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8927
8928static int
89e63ee4 8929maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8930 struct dwarf2_per_cu_data *per_cu,
8931 enum language pretend_language)
8932{
8933 /* We may arrive here during partial symbol reading, if we need full
8934 DIEs to process an unusual case (e.g. template arguments). Do
8935 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8936 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c
DE
8937 {
8938 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8939 return 1;
8940 return 0;
8941 }
8942
8943 /* Mark the dependence relation so that we don't flush PER_CU
8944 too early. */
89e63ee4
DE
8945 if (dependent_cu != NULL)
8946 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8947
8948 /* If it's already on the queue, we have nothing to do. */
8949 if (per_cu->queued)
8950 return 0;
8951
8952 /* If the compilation unit is already loaded, just mark it as
8953 used. */
8954 if (per_cu->cu != NULL)
8955 {
8956 per_cu->cu->last_used = 0;
8957 return 0;
8958 }
8959
8960 /* Add it to the queue. */
8961 queue_comp_unit (per_cu, pretend_language);
8962
8963 return 1;
8964}
8965
10b3939b
DJ
8966/* Process the queue. */
8967
8968static void
ed2dc618 8969process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8970{
b4f54984 8971 if (dwarf_read_debug)
45cfd468
DE
8972 {
8973 fprintf_unfiltered (gdb_stdlog,
8974 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8975 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8976 }
8977
03dd20cc
DJ
8978 /* The queue starts out with one item, but following a DIE reference
8979 may load a new CU, adding it to the end of the queue. */
5989a64e 8980 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 8981 {
5989a64e 8982 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 8983
af758d11 8984 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 8985 /* Skip dummy CUs. */
39856def 8986 && item.per_cu->cu != NULL)
f4dc4d17 8987 {
39856def 8988 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8989 unsigned int debug_print_threshold;
247f5c4f 8990 char buf[100];
f4dc4d17 8991
247f5c4f 8992 if (per_cu->is_debug_types)
f4dc4d17 8993 {
247f5c4f
DE
8994 struct signatured_type *sig_type =
8995 (struct signatured_type *) per_cu;
8996
9d8780f0 8997 sprintf (buf, "TU %s at offset %s",
73be47f5 8998 hex_string (sig_type->signature),
9d8780f0 8999 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9000 /* There can be 100s of TUs.
9001 Only print them in verbose mode. */
9002 debug_print_threshold = 2;
f4dc4d17 9003 }
247f5c4f 9004 else
73be47f5 9005 {
9d8780f0
SM
9006 sprintf (buf, "CU at offset %s",
9007 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9008 debug_print_threshold = 1;
9009 }
247f5c4f 9010
b4f54984 9011 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9012 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9013
9014 if (per_cu->is_debug_types)
39856def 9015 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 9016 else
39856def 9017 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 9018
b4f54984 9019 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9020 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9021 }
10b3939b 9022
39856def 9023 item.per_cu->queued = 0;
5989a64e 9024 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9025 }
9026
b4f54984 9027 if (dwarf_read_debug)
45cfd468
DE
9028 {
9029 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9030 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9031 }
10b3939b
DJ
9032}
9033
10b3939b
DJ
9034/* Read in full symbols for PST, and anything it depends on. */
9035
8566b89b
TT
9036void
9037dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9038{
af758d11 9039 gdb_assert (!readin_p (objfile));
95554aad 9040
48993951 9041 expand_dependencies (objfile);
aaa75496 9042
97a1449a
SM
9043 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9044 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9045 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9046}
9047
af758d11
SM
9048/* See psympriv.h. */
9049
9050bool
9051dwarf2_psymtab::readin_p (struct objfile *objfile) const
9052{
9053 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9054 return per_objfile->symtab_set_p (per_cu_data);
9055}
9056
9057/* See psympriv.h. */
9058
9059compunit_symtab *
9060dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9061{
9062 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9063 return per_objfile->get_symtab (per_cu_data);
9064}
9065
dee91e82
DE
9066/* Trivial hash function for die_info: the hash value of a DIE
9067 is its offset in .debug_info for this objfile. */
10b3939b 9068
dee91e82
DE
9069static hashval_t
9070die_hash (const void *item)
10b3939b 9071{
9a3c8263 9072 const struct die_info *die = (const struct die_info *) item;
6502dd73 9073
9c541725 9074 return to_underlying (die->sect_off);
dee91e82 9075}
63d06c5c 9076
dee91e82
DE
9077/* Trivial comparison function for die_info structures: two DIEs
9078 are equal if they have the same offset. */
98bfdba5 9079
dee91e82
DE
9080static int
9081die_eq (const void *item_lhs, const void *item_rhs)
9082{
9a3c8263
SM
9083 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9084 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9085
9c541725 9086 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9087}
c906108c 9088
c0ab21c2 9089/* Load the DIEs associated with PER_CU into memory. */
c906108c 9090
dee91e82 9091static void
c0ab21c2
TT
9092load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9093 bool skip_partial,
9094 enum language pretend_language)
dee91e82 9095{
c0ab21c2
TT
9096 gdb_assert (! this_cu->is_debug_types);
9097
6751ebae 9098 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
9099 if (reader.dummy_p)
9100 return;
9101
9102 struct dwarf2_cu *cu = reader.cu;
9103 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9104
dee91e82
DE
9105 gdb_assert (cu->die_hash == NULL);
9106 cu->die_hash =
9107 htab_create_alloc_ex (cu->header.length / 12,
9108 die_hash,
9109 die_eq,
9110 NULL,
9111 &cu->comp_unit_obstack,
9112 hashtab_obstack_allocate,
9113 dummy_obstack_deallocate);
e142c38c 9114
3e225074 9115 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9116 reader.comp_unit_die->child
9117 = read_die_and_siblings (&reader, reader.info_ptr,
9118 &info_ptr, reader.comp_unit_die);
9119 cu->dies = reader.comp_unit_die;
dee91e82 9120 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9121
9122 /* We try not to read any attributes in this function, because not
9cdd5dbd 9123 all CUs needed for references have been loaded yet, and symbol
10b3939b 9124 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9125 or we won't be able to build types correctly.
9126 Similarly, if we do not read the producer, we can not apply
9127 producer-specific interpretation. */
c0ab21c2 9128 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9129
9130 reader.keep ();
10b3939b
DJ
9131}
9132
3da10d80
KS
9133/* Add a DIE to the delayed physname list. */
9134
9135static void
9136add_to_method_list (struct type *type, int fnfield_index, int index,
9137 const char *name, struct die_info *die,
9138 struct dwarf2_cu *cu)
9139{
9140 struct delayed_method_info mi;
9141 mi.type = type;
9142 mi.fnfield_index = fnfield_index;
9143 mi.index = index;
9144 mi.name = name;
9145 mi.die = die;
c89b44cd 9146 cu->method_list.push_back (mi);
3da10d80
KS
9147}
9148
3693fdb3
PA
9149/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9150 "const" / "volatile". If so, decrements LEN by the length of the
9151 modifier and return true. Otherwise return false. */
9152
9153template<size_t N>
9154static bool
9155check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9156{
9157 size_t mod_len = sizeof (mod) - 1;
9158 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9159 {
9160 len -= mod_len;
9161 return true;
9162 }
9163 return false;
9164}
9165
3da10d80
KS
9166/* Compute the physnames of any methods on the CU's method list.
9167
9168 The computation of method physnames is delayed in order to avoid the
9169 (bad) condition that one of the method's formal parameters is of an as yet
9170 incomplete type. */
9171
9172static void
9173compute_delayed_physnames (struct dwarf2_cu *cu)
9174{
3693fdb3 9175 /* Only C++ delays computing physnames. */
c89b44cd 9176 if (cu->method_list.empty ())
3693fdb3
PA
9177 return;
9178 gdb_assert (cu->language == language_cplus);
9179
52941706 9180 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9181 {
1d06ead6 9182 const char *physname;
3da10d80 9183 struct fn_fieldlist *fn_flp
c89b44cd
TT
9184 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9185 physname = dwarf2_physname (mi.name, mi.die, cu);
9186 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9187 = physname ? physname : "";
3693fdb3
PA
9188
9189 /* Since there's no tag to indicate whether a method is a
9190 const/volatile overload, extract that information out of the
9191 demangled name. */
9192 if (physname != NULL)
9193 {
9194 size_t len = strlen (physname);
9195
9196 while (1)
9197 {
9198 if (physname[len] == ')') /* shortcut */
9199 break;
9200 else if (check_modifier (physname, len, " const"))
c89b44cd 9201 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9202 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9203 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9204 else
9205 break;
9206 }
9207 }
3da10d80 9208 }
c89b44cd
TT
9209
9210 /* The list is no longer needed. */
9211 cu->method_list.clear ();
3da10d80
KS
9212}
9213
a766d390
DE
9214/* Go objects should be embedded in a DW_TAG_module DIE,
9215 and it's not clear if/how imported objects will appear.
9216 To keep Go support simple until that's worked out,
9217 go back through what we've read and create something usable.
9218 We could do this while processing each DIE, and feels kinda cleaner,
9219 but that way is more invasive.
9220 This is to, for example, allow the user to type "p var" or "b main"
9221 without having to specify the package name, and allow lookups
9222 of module.object to work in contexts that use the expression
9223 parser. */
9224
9225static void
9226fixup_go_packaging (struct dwarf2_cu *cu)
9227{
421d1616 9228 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9229 struct pending *list;
9230 int i;
9231
c24bdb02 9232 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9233 list != NULL;
9234 list = list->next)
a766d390
DE
9235 {
9236 for (i = 0; i < list->nsyms; ++i)
9237 {
9238 struct symbol *sym = list->symbol[i];
9239
c1b5c1eb 9240 if (sym->language () == language_go
a766d390
DE
9241 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9242 {
421d1616
TT
9243 gdb::unique_xmalloc_ptr<char> this_package_name
9244 (go_symbol_package_name (sym));
a766d390
DE
9245
9246 if (this_package_name == NULL)
9247 continue;
9248 if (package_name == NULL)
421d1616 9249 package_name = std::move (this_package_name);
a766d390
DE
9250 else
9251 {
5e22e966 9252 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9253 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9254 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9255 (symbol_symtab (sym) != NULL
9256 ? symtab_to_filename_for_display
9257 (symbol_symtab (sym))
e3b94546 9258 : objfile_name (objfile)),
421d1616 9259 this_package_name.get (), package_name.get ());
a766d390
DE
9260 }
9261 }
9262 }
9263 }
9264
9265 if (package_name != NULL)
9266 {
5e22e966 9267 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9268 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9269 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9270 saved_package_name);
a766d390
DE
9271 struct symbol *sym;
9272
8c14c3a3 9273 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9274 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9275 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9276 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9277 e.g., "main" finds the "main" module and not C's main(). */
9278 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9279 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9280 SYMBOL_TYPE (sym) = type;
9281
c24bdb02 9282 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9283 }
9284}
9285
c9317f21
TT
9286/* Allocate a fully-qualified name consisting of the two parts on the
9287 obstack. */
9288
9289static const char *
9290rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9291{
9292 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9293}
9294
9c6a1327
TT
9295/* A helper that allocates a variant part to attach to a Rust enum
9296 type. OBSTACK is where the results should be allocated. TYPE is
9297 the type we're processing. DISCRIMINANT_INDEX is the index of the
9298 discriminant. It must be the index of one of the fields of TYPE.
9299 DEFAULT_INDEX is the index of the default field; or -1 if there is
9300 no default. RANGES is indexed by "effective" field number (the
9301 field index, but omitting the discriminant and default fields) and
9302 must hold the discriminant values used by the variants. Note that
9303 RANGES must have a lifetime at least as long as OBSTACK -- either
9304 already allocated on it, or static. */
c9317f21 9305
9c6a1327
TT
9306static void
9307alloc_rust_variant (struct obstack *obstack, struct type *type,
9308 int discriminant_index, int default_index,
9309 gdb::array_view<discriminant_range> ranges)
9310{
9311 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9312 must be handled by the caller. */
9313 gdb_assert (discriminant_index >= 0
1f704f76 9314 && discriminant_index < type->num_fields ());
c9317f21 9315 gdb_assert (default_index == -1
1f704f76 9316 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9317
9c6a1327 9318 /* We have one variant for each non-discriminant field. */
1f704f76 9319 int n_variants = type->num_fields () - 1;
c9317f21 9320
9c6a1327
TT
9321 variant *variants = new (obstack) variant[n_variants];
9322 int var_idx = 0;
9323 int range_idx = 0;
1f704f76 9324 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9325 {
9326 if (i == discriminant_index)
9327 continue;
c9317f21 9328
9c6a1327
TT
9329 variants[var_idx].first_field = i;
9330 variants[var_idx].last_field = i + 1;
9331
9332 /* The default field does not need a range, but other fields do.
9333 We skipped the discriminant above. */
9334 if (i != default_index)
9335 {
9336 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9337 ++range_idx;
9338 }
c9317f21 9339
9c6a1327
TT
9340 ++var_idx;
9341 }
9342
9343 gdb_assert (range_idx == ranges.size ());
9344 gdb_assert (var_idx == n_variants);
9345
9346 variant_part *part = new (obstack) variant_part;
9347 part->discriminant_index = discriminant_index;
9348 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9349 discriminant_index));
9350 part->variants = gdb::array_view<variant> (variants, n_variants);
9351
9352 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9353 gdb::array_view<variant_part> *prop_value
9354 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9355
9c6a1327
TT
9356 struct dynamic_prop prop;
9357 prop.kind = PROP_VARIANT_PARTS;
9358 prop.data.variant_parts = prop_value;
9359
5c54719c 9360 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9361}
9362
9363/* Some versions of rustc emitted enums in an unusual way.
9364
9365 Ordinary enums were emitted as unions. The first element of each
9366 structure in the union was named "RUST$ENUM$DISR". This element
9367 held the discriminant.
9368
9369 These versions of Rust also implemented the "non-zero"
9370 optimization. When the enum had two values, and one is empty and
9371 the other holds a pointer that cannot be zero, the pointer is used
9372 as the discriminant, with a zero value meaning the empty variant.
9373 Here, the union's first member is of the form
9374 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9375 where the fieldnos are the indices of the fields that should be
9376 traversed in order to find the field (which may be several fields deep)
9377 and the variantname is the name of the variant of the case when the
9378 field is zero.
9379
9380 This function recognizes whether TYPE is of one of these forms,
9381 and, if so, smashes it to be a variant type. */
9382
9383static void
9384quirk_rust_enum (struct type *type, struct objfile *objfile)
9385{
78134374 9386 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9387
9388 /* We don't need to deal with empty enums. */
1f704f76 9389 if (type->num_fields () == 0)
c9317f21
TT
9390 return;
9391
9392#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9393 if (type->num_fields () == 1
c9317f21
TT
9394 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9395 {
9396 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9397
9398 /* Decode the field name to find the offset of the
9399 discriminant. */
9400 ULONGEST bit_offset = 0;
9401 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9402 while (name[0] >= '0' && name[0] <= '9')
9403 {
9404 char *tail;
9405 unsigned long index = strtoul (name, &tail, 10);
9406 name = tail;
9407 if (*name != '$'
1f704f76 9408 || index >= field_type->num_fields ()
c9317f21
TT
9409 || (TYPE_FIELD_LOC_KIND (field_type, index)
9410 != FIELD_LOC_KIND_BITPOS))
9411 {
b98664d3 9412 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9413 "[in module %s]"),
9414 TYPE_FIELD_NAME (type, 0),
9415 objfile_name (objfile));
9416 return;
9417 }
9418 ++name;
9419
9420 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9421 field_type = TYPE_FIELD_TYPE (field_type, index);
9422 }
9423
9c6a1327
TT
9424 /* Smash this type to be a structure type. We have to do this
9425 because the type has already been recorded. */
67607e24 9426 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9427 type->set_num_fields (3);
9c6a1327 9428 /* Save the field we care about. */
ceacbf6e 9429 struct field saved_field = type->field (0);
3cabb6b0
SM
9430 type->set_fields
9431 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9432
9c6a1327
TT
9433 /* Put the discriminant at index 0. */
9434 TYPE_FIELD_TYPE (type, 0) = field_type;
9435 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9436 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9437 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9438
9439 /* The order of fields doesn't really matter, so put the real
9440 field at index 1 and the data-less field at index 2. */
ceacbf6e 9441 type->field (1) = saved_field;
9c6a1327 9442 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9443 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9444 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9445 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9446 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9447
9448 const char *dataless_name
7d93a1e0 9449 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9450 name);
9451 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9452 dataless_name);
9c6a1327 9453 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9454 /* NAME points into the original discriminant name, which
9455 already has the correct lifetime. */
9c6a1327 9456 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9457 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9458
9c6a1327
TT
9459 /* Indicate that this is a variant type. */
9460 static discriminant_range ranges[1] = { { 0, 0 } };
9461 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9462 }
77c2dba3
TT
9463 /* A union with a single anonymous field is probably an old-style
9464 univariant enum. */
1f704f76 9465 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9466 {
c9317f21
TT
9467 /* Smash this type to be a structure type. We have to do this
9468 because the type has already been recorded. */
67607e24 9469 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9470
9c6a1327 9471 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9472 const char *variant_name
7d93a1e0 9473 = rust_last_path_segment (field_type->name ());
9c6a1327 9474 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9475 field_type->set_name
9476 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9477 type->name (), variant_name));
c9317f21
TT
9478 }
9479 else
9480 {
9481 struct type *disr_type = nullptr;
1f704f76 9482 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9483 {
9484 disr_type = TYPE_FIELD_TYPE (type, i);
9485
78134374 9486 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9487 {
9488 /* All fields of a true enum will be structs. */
9489 return;
9490 }
1f704f76 9491 else if (disr_type->num_fields () == 0)
c9317f21
TT
9492 {
9493 /* Could be data-less variant, so keep going. */
a037790e 9494 disr_type = nullptr;
c9317f21
TT
9495 }
9496 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9497 "RUST$ENUM$DISR") != 0)
9498 {
9499 /* Not a Rust enum. */
9500 return;
9501 }
9502 else
9503 {
9504 /* Found one. */
9505 break;
9506 }
9507 }
9508
9509 /* If we got here without a discriminant, then it's probably
9510 just a union. */
9511 if (disr_type == nullptr)
9512 return;
9513
9514 /* Smash this type to be a structure type. We have to do this
9515 because the type has already been recorded. */
67607e24 9516 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9517
9c6a1327 9518 /* Make space for the discriminant field. */
ceacbf6e 9519 struct field *disr_field = &disr_type->field (0);
9c6a1327 9520 field *new_fields
1f704f76 9521 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9522 * sizeof (struct field)));
80fc5e77 9523 memcpy (new_fields + 1, type->fields (),
1f704f76 9524 type->num_fields () * sizeof (struct field));
3cabb6b0 9525 type->set_fields (new_fields);
1f704f76 9526 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9527
9528 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9529 type->field (0) = *disr_field;
9c6a1327
TT
9530 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9531 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9532
9533 /* We need a way to find the correct discriminant given a
9534 variant name. For convenience we build a map here. */
9535 struct type *enum_type = FIELD_TYPE (*disr_field);
9536 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9537 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9538 {
9539 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9540 {
9541 const char *name
9542 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9543 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9544 }
9545 }
9546
1f704f76 9547 int n_fields = type->num_fields ();
9c6a1327
TT
9548 /* We don't need a range entry for the discriminant, but we do
9549 need one for every other field, as there is no default
9550 variant. */
9551 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9552 discriminant_range,
9553 n_fields - 1);
c9317f21
TT
9554 /* Skip the discriminant here. */
9555 for (int i = 1; i < n_fields; ++i)
9556 {
9557 /* Find the final word in the name of this variant's type.
9558 That name can be used to look up the correct
9559 discriminant. */
9560 const char *variant_name
7d93a1e0 9561 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9562
9563 auto iter = discriminant_map.find (variant_name);
9564 if (iter != discriminant_map.end ())
9c6a1327
TT
9565 {
9566 ranges[i].low = iter->second;
9567 ranges[i].high = iter->second;
9568 }
c9317f21 9569
bedda9ac 9570 /* Remove the discriminant field, if it exists. */
9c6a1327 9571 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9572 if (sub_type->num_fields () > 0)
bedda9ac 9573 {
5e33d5f4 9574 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9575 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9576 }
9c6a1327 9577 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9578 sub_type->set_name
9579 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9580 type->name (), variant_name));
c9317f21 9581 }
9c6a1327
TT
9582
9583 /* Indicate that this is a variant type. */
9584 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9585 gdb::array_view<discriminant_range> (ranges,
9586 n_fields - 1));
c9317f21
TT
9587 }
9588}
9589
9590/* Rewrite some Rust unions to be structures with variants parts. */
9591
9592static void
9593rust_union_quirks (struct dwarf2_cu *cu)
9594{
9595 gdb_assert (cu->language == language_rust);
52941706 9596 for (type *type_ : cu->rust_unions)
5e22e966 9597 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9598 /* We don't need this any more. */
9599 cu->rust_unions.clear ();
c9317f21
TT
9600}
9601
95554aad
TT
9602/* A helper function for computing the list of all symbol tables
9603 included by PER_CU. */
9604
9605static void
4c39bc03 9606recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9607 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9608 struct dwarf2_per_cu_data *per_cu,
43f3e411 9609 struct compunit_symtab *immediate_parent)
95554aad 9610{
af758d11 9611 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9612 if (*slot != NULL)
9613 {
9614 /* This inclusion and its children have been processed. */
9615 return;
9616 }
9617
9618 *slot = per_cu;
af758d11 9619
95554aad 9620 /* Only add a CU if it has a symbol table. */
af758d11 9621 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
43f3e411 9622 if (cust != NULL)
ec94af83
DE
9623 {
9624 /* If this is a type unit only add its symbol table if we haven't
9625 seen it yet (type unit per_cu's can share symtabs). */
9626 if (per_cu->is_debug_types)
9627 {
43f3e411 9628 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9629 if (*slot == NULL)
9630 {
43f3e411 9631 *slot = cust;
4c39bc03 9632 result->push_back (cust);
43f3e411
DE
9633 if (cust->user == NULL)
9634 cust->user = immediate_parent;
ec94af83
DE
9635 }
9636 }
9637 else
f9125b6c 9638 {
4c39bc03 9639 result->push_back (cust);
43f3e411
DE
9640 if (cust->user == NULL)
9641 cust->user = immediate_parent;
f9125b6c 9642 }
ec94af83 9643 }
95554aad 9644
ae640021
AB
9645 if (!per_cu->imported_symtabs_empty ())
9646 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9647 {
9648 recursively_compute_inclusions (result, all_children,
9649 all_type_symtabs, ptr, cust);
9650 }
95554aad
TT
9651}
9652
43f3e411 9653/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9654 PER_CU. */
9655
9656static void
43f3e411 9657compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9658{
f4dc4d17
DE
9659 gdb_assert (! per_cu->is_debug_types);
9660
ae640021 9661 if (!per_cu->imported_symtabs_empty ())
95554aad 9662 {
ae640021 9663 int len;
4c39bc03 9664 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9665 htab_t all_children, all_type_symtabs;
af758d11 9666 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
95554aad
TT
9667
9668 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9669 if (cust == NULL)
95554aad
TT
9670 return;
9671
9672 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9673 NULL, xcalloc, xfree);
ec94af83
DE
9674 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9675 NULL, xcalloc, xfree);
95554aad 9676
ae640021 9677 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9678 {
9679 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9680 all_type_symtabs, ptr, cust);
ec94af83 9681 }
95554aad 9682
ec94af83 9683 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9684 len = result_symtabs.size ();
43f3e411 9685 cust->includes
ed2dc618 9686 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9687 struct compunit_symtab *, len + 1);
4c39bc03
TT
9688 memcpy (cust->includes, result_symtabs.data (),
9689 len * sizeof (compunit_symtab *));
43f3e411 9690 cust->includes[len] = NULL;
95554aad 9691
95554aad 9692 htab_delete (all_children);
ec94af83 9693 htab_delete (all_type_symtabs);
95554aad
TT
9694 }
9695}
9696
9697/* Compute the 'includes' field for the symtabs of all the CUs we just
9698 read. */
9699
9700static void
ed2dc618 9701process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9702{
5989a64e 9703 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9704 {
9705 if (! iter->is_debug_types)
43f3e411 9706 compute_compunit_symtab_includes (iter);
f4dc4d17 9707 }
95554aad 9708
5989a64e 9709 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9710}
9711
9cdd5dbd 9712/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9713 already been loaded into memory. */
9714
9715static void
95554aad
TT
9716process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9717 enum language pretend_language)
10b3939b 9718{
10b3939b 9719 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9720 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9721 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9722 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9723 CORE_ADDR lowpc, highpc;
43f3e411 9724 struct compunit_symtab *cust;
10b3939b 9725 CORE_ADDR baseaddr;
4359dff1 9726 struct block *static_block;
3e29f34a 9727 CORE_ADDR addr;
10b3939b 9728
b3b3bada 9729 baseaddr = objfile->text_section_offset ();
10b3939b 9730
c89b44cd
TT
9731 /* Clear the list here in case something was left over. */
9732 cu->method_list.clear ();
10b3939b 9733
95554aad
TT
9734 cu->language = pretend_language;
9735 cu->language_defn = language_def (cu->language);
9736
c906108c 9737 /* Do line number decoding in read_file_scope () */
10b3939b 9738 process_die (cu->dies, cu);
c906108c 9739
a766d390
DE
9740 /* For now fudge the Go package. */
9741 if (cu->language == language_go)
9742 fixup_go_packaging (cu);
9743
5f48f8f3 9744 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9745 should be complete, and it should now be safe to compute all of the
9746 physnames. */
9747 compute_delayed_physnames (cu);
3da10d80 9748
c9317f21
TT
9749 if (cu->language == language_rust)
9750 rust_union_quirks (cu);
9751
fae299cd
DC
9752 /* Some compilers don't define a DW_AT_high_pc attribute for the
9753 compilation unit. If the DW_AT_high_pc is missing, synthesize
9754 it, by scanning the DIE's below the compilation unit. */
10b3939b 9755 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9756
3e29f34a 9757 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9758 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9759
9760 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9761 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9762 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9763 addrmap to help ensure it has an accurate map of pc values belonging to
9764 this comp unit. */
9765 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9766
c24bdb02 9767 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9768 SECT_OFF_TEXT (objfile),
9769 0);
c906108c 9770
43f3e411 9771 if (cust != NULL)
c906108c 9772 {
df15bd07 9773 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9774
8be455d7
JK
9775 /* Set symtab language to language from DW_AT_language. If the
9776 compilation is from a C file generated by language preprocessors, do
9777 not set the language if it was already deduced by start_subfile. */
43f3e411 9778 if (!(cu->language == language_c
40e3ad0e 9779 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9780 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9781
9782 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9783 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9784 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9785 there were bugs in prologue debug info, fixed later in GCC-4.5
9786 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9787
9788 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9789 needed, it would be wrong due to missing DW_AT_producer there.
9790
9791 Still one can confuse GDB by using non-standard GCC compilation
9792 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9793 */
ab260dad 9794 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9795 cust->locations_valid = 1;
e0d00bc7
JK
9796
9797 if (gcc_4_minor >= 5)
43f3e411 9798 cust->epilogue_unwind_valid = 1;
96408a79 9799
43f3e411 9800 cust->call_site_htab = cu->call_site_htab;
c906108c 9801 }
9291a0cd 9802
af758d11 9803 dwarf2_per_objfile->set_symtab (per_cu, cust);
c906108c 9804
95554aad 9805 /* Push it for inclusion processing later. */
5989a64e 9806 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
804d2729
TT
9807
9808 /* Not needed any more. */
c24bdb02 9809 cu->reset_builder ();
f4dc4d17 9810}
45cfd468 9811
f4dc4d17
DE
9812/* Generate full symbol information for type unit PER_CU, whose DIEs have
9813 already been loaded into memory. */
9814
9815static void
9816process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9817 enum language pretend_language)
9818{
9819 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9820 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9821 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9822 struct compunit_symtab *cust;
0186c6a7
DE
9823 struct signatured_type *sig_type;
9824
9825 gdb_assert (per_cu->is_debug_types);
9826 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9827
c89b44cd
TT
9828 /* Clear the list here in case something was left over. */
9829 cu->method_list.clear ();
f4dc4d17 9830
f4dc4d17
DE
9831 cu->language = pretend_language;
9832 cu->language_defn = language_def (cu->language);
9833
9834 /* The symbol tables are set up in read_type_unit_scope. */
9835 process_die (cu->dies, cu);
9836
9837 /* For now fudge the Go package. */
9838 if (cu->language == language_go)
9839 fixup_go_packaging (cu);
9840
5f48f8f3 9841 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9842 should be complete, and it should now be safe to compute all of the
9843 physnames. */
9844 compute_delayed_physnames (cu);
f4dc4d17 9845
c9317f21
TT
9846 if (cu->language == language_rust)
9847 rust_union_quirks (cu);
9848
f4dc4d17
DE
9849 /* TUs share symbol tables.
9850 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9851 of it with end_expandable_symtab. Otherwise, complete the addition of
9852 this TU's symbols to the existing symtab. */
43f3e411 9853 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9854 {
c24bdb02
KS
9855 buildsym_compunit *builder = cu->get_builder ();
9856 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9857 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9858
43f3e411 9859 if (cust != NULL)
f4dc4d17
DE
9860 {
9861 /* Set symtab language to language from DW_AT_language. If the
9862 compilation is from a C file generated by language preprocessors,
9863 do not set the language if it was already deduced by
9864 start_subfile. */
43f3e411
DE
9865 if (!(cu->language == language_c
9866 && COMPUNIT_FILETABS (cust)->language != language_c))
9867 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9868 }
9869 }
9870 else
9871 {
c24bdb02 9872 cu->get_builder ()->augment_type_symtab ();
43f3e411 9873 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9874 }
9875
af758d11 9876 dwarf2_per_objfile->set_symtab (per_cu, cust);
804d2729
TT
9877
9878 /* Not needed any more. */
c24bdb02 9879 cu->reset_builder ();
c906108c
SS
9880}
9881
95554aad
TT
9882/* Process an imported unit DIE. */
9883
9884static void
9885process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9886{
9887 struct attribute *attr;
9888
f4dc4d17
DE
9889 /* For now we don't handle imported units in type units. */
9890 if (cu->per_cu->is_debug_types)
9891 {
9892 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9893 " supported in type units [in module %s]"),
5e22e966 9894 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9895 }
9896
95554aad
TT
9897 attr = dwarf2_attr (die, DW_AT_import, cu);
9898 if (attr != NULL)
9899 {
0826b30a 9900 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9901 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9902 dwarf2_per_cu_data *per_cu
5e22e966 9903 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->per_objfile);
95554aad 9904
58990295
TV
9905 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9906 into another compilation unit, at root level. Regard this as a hint,
9907 and ignore it. */
9908 if (die->parent && die->parent->parent == NULL
9909 && per_cu->unit_type == DW_UT_compile
9910 && per_cu->lang == language_cplus)
9911 return;
9912
69d751e3 9913 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9914 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9915 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9916
ae640021 9917 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9918 }
9919}
9920
4c8aa72d
PA
9921/* RAII object that represents a process_die scope: i.e.,
9922 starts/finishes processing a DIE. */
9923class process_die_scope
adde2bff 9924{
4c8aa72d
PA
9925public:
9926 process_die_scope (die_info *die, dwarf2_cu *cu)
9927 : m_die (die), m_cu (cu)
9928 {
9929 /* We should only be processing DIEs not already in process. */
9930 gdb_assert (!m_die->in_process);
9931 m_die->in_process = true;
9932 }
8c3cb9fa 9933
4c8aa72d
PA
9934 ~process_die_scope ()
9935 {
9936 m_die->in_process = false;
9937
9938 /* If we're done processing the DIE for the CU that owns the line
9939 header, we don't need the line header anymore. */
9940 if (m_cu->line_header_die_owner == m_die)
9941 {
9942 delete m_cu->line_header;
9943 m_cu->line_header = NULL;
9944 m_cu->line_header_die_owner = NULL;
9945 }
9946 }
9947
9948private:
9949 die_info *m_die;
9950 dwarf2_cu *m_cu;
9951};
adde2bff 9952
c906108c
SS
9953/* Process a die and its children. */
9954
9955static void
e7c27a73 9956process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9957{
4c8aa72d 9958 process_die_scope scope (die, cu);
adde2bff 9959
c906108c
SS
9960 switch (die->tag)
9961 {
9962 case DW_TAG_padding:
9963 break;
9964 case DW_TAG_compile_unit:
95554aad 9965 case DW_TAG_partial_unit:
e7c27a73 9966 read_file_scope (die, cu);
c906108c 9967 break;
348e048f
DE
9968 case DW_TAG_type_unit:
9969 read_type_unit_scope (die, cu);
9970 break;
c906108c 9971 case DW_TAG_subprogram:
0a4b0913
AB
9972 /* Nested subprograms in Fortran get a prefix. */
9973 if (cu->language == language_fortran
9974 && die->parent != NULL
9975 && die->parent->tag == DW_TAG_subprogram)
9976 cu->processing_has_namespace_info = true;
9977 /* Fall through. */
c906108c 9978 case DW_TAG_inlined_subroutine:
edb3359d 9979 read_func_scope (die, cu);
c906108c
SS
9980 break;
9981 case DW_TAG_lexical_block:
14898363
L
9982 case DW_TAG_try_block:
9983 case DW_TAG_catch_block:
e7c27a73 9984 read_lexical_block_scope (die, cu);
c906108c 9985 break;
216f72a1 9986 case DW_TAG_call_site:
96408a79
SA
9987 case DW_TAG_GNU_call_site:
9988 read_call_site_scope (die, cu);
9989 break;
c906108c 9990 case DW_TAG_class_type:
680b30c7 9991 case DW_TAG_interface_type:
c906108c
SS
9992 case DW_TAG_structure_type:
9993 case DW_TAG_union_type:
134d01f1 9994 process_structure_scope (die, cu);
c906108c
SS
9995 break;
9996 case DW_TAG_enumeration_type:
134d01f1 9997 process_enumeration_scope (die, cu);
c906108c 9998 break;
134d01f1 9999
f792889a
DJ
10000 /* These dies have a type, but processing them does not create
10001 a symbol or recurse to process the children. Therefore we can
10002 read them on-demand through read_type_die. */
c906108c 10003 case DW_TAG_subroutine_type:
72019c9c 10004 case DW_TAG_set_type:
c906108c 10005 case DW_TAG_array_type:
c906108c 10006 case DW_TAG_pointer_type:
c906108c 10007 case DW_TAG_ptr_to_member_type:
c906108c 10008 case DW_TAG_reference_type:
4297a3f0 10009 case DW_TAG_rvalue_reference_type:
c906108c 10010 case DW_TAG_string_type:
c906108c 10011 break;
134d01f1 10012
c906108c 10013 case DW_TAG_base_type:
a02abb62 10014 case DW_TAG_subrange_type:
cb249c71 10015 case DW_TAG_typedef:
134d01f1
DJ
10016 /* Add a typedef symbol for the type definition, if it has a
10017 DW_AT_name. */
f792889a 10018 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10019 break;
c906108c 10020 case DW_TAG_common_block:
e7c27a73 10021 read_common_block (die, cu);
c906108c
SS
10022 break;
10023 case DW_TAG_common_inclusion:
10024 break;
d9fa45fe 10025 case DW_TAG_namespace:
9068261f 10026 cu->processing_has_namespace_info = true;
e7c27a73 10027 read_namespace (die, cu);
d9fa45fe 10028 break;
5d7cb8df 10029 case DW_TAG_module:
9068261f 10030 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10031 read_module (die, cu);
10032 break;
d9fa45fe 10033 case DW_TAG_imported_declaration:
9068261f 10034 cu->processing_has_namespace_info = true;
74921315
KS
10035 if (read_namespace_alias (die, cu))
10036 break;
86a73007
TT
10037 /* The declaration is not a global namespace alias. */
10038 /* Fall through. */
d9fa45fe 10039 case DW_TAG_imported_module:
9068261f 10040 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10041 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10042 || cu->language != language_fortran))
b98664d3 10043 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10044 dwarf_tag_name (die->tag));
10045 read_import_statement (die, cu);
d9fa45fe 10046 break;
95554aad
TT
10047
10048 case DW_TAG_imported_unit:
10049 process_imported_unit_die (die, cu);
10050 break;
10051
71a3c369
TT
10052 case DW_TAG_variable:
10053 read_variable (die, cu);
10054 break;
10055
c906108c 10056 default:
e7c27a73 10057 new_symbol (die, NULL, cu);
c906108c
SS
10058 break;
10059 }
10060}
ca69b9e6
DE
10061\f
10062/* DWARF name computation. */
c906108c 10063
94af9270
KS
10064/* A helper function for dwarf2_compute_name which determines whether DIE
10065 needs to have the name of the scope prepended to the name listed in the
10066 die. */
10067
10068static int
10069die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10070{
1c809c68
TT
10071 struct attribute *attr;
10072
94af9270
KS
10073 switch (die->tag)
10074 {
10075 case DW_TAG_namespace:
10076 case DW_TAG_typedef:
10077 case DW_TAG_class_type:
10078 case DW_TAG_interface_type:
10079 case DW_TAG_structure_type:
10080 case DW_TAG_union_type:
10081 case DW_TAG_enumeration_type:
10082 case DW_TAG_enumerator:
10083 case DW_TAG_subprogram:
08a76f8a 10084 case DW_TAG_inlined_subroutine:
94af9270 10085 case DW_TAG_member:
74921315 10086 case DW_TAG_imported_declaration:
94af9270
KS
10087 return 1;
10088
10089 case DW_TAG_variable:
c2b0a229 10090 case DW_TAG_constant:
94af9270
KS
10091 /* We only need to prefix "globally" visible variables. These include
10092 any variable marked with DW_AT_external or any variable that
10093 lives in a namespace. [Variables in anonymous namespaces
10094 require prefixing, but they are not DW_AT_external.] */
10095
10096 if (dwarf2_attr (die, DW_AT_specification, cu))
10097 {
10098 struct dwarf2_cu *spec_cu = cu;
9a619af0 10099
94af9270
KS
10100 return die_needs_namespace (die_specification (die, &spec_cu),
10101 spec_cu);
10102 }
10103
1c809c68 10104 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10105 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10106 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10107 return 0;
10108 /* A variable in a lexical block of some kind does not need a
10109 namespace, even though in C++ such variables may be external
10110 and have a mangled name. */
10111 if (die->parent->tag == DW_TAG_lexical_block
10112 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10113 || die->parent->tag == DW_TAG_catch_block
10114 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10115 return 0;
10116 return 1;
94af9270
KS
10117
10118 default:
10119 return 0;
10120 }
10121}
10122
73b9be8b
KS
10123/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10124 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10125 defined for the given DIE. */
10126
10127static struct attribute *
10128dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10129{
10130 struct attribute *attr;
10131
10132 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10133 if (attr == NULL)
10134 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10135
10136 return attr;
10137}
10138
10139/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10140 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10141 defined for the given DIE. */
10142
10143static const char *
10144dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10145{
10146 const char *linkage_name;
10147
10148 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10149 if (linkage_name == NULL)
10150 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10151
787de330
TT
10152 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10153 See https://github.com/rust-lang/rust/issues/32925. */
10154 if (cu->language == language_rust && linkage_name != NULL
10155 && strchr (linkage_name, '{') != NULL)
10156 linkage_name = NULL;
10157
73b9be8b
KS
10158 return linkage_name;
10159}
10160
94af9270 10161/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10162 compute the physname for the object, which include a method's:
9c37b5ae 10163 - formal parameters (C++),
a766d390 10164 - receiver type (Go),
a766d390
DE
10165
10166 The term "physname" is a bit confusing.
10167 For C++, for example, it is the demangled name.
10168 For Go, for example, it's the mangled name.
94af9270 10169
af6b7be1
JB
10170 For Ada, return the DIE's linkage name rather than the fully qualified
10171 name. PHYSNAME is ignored..
10172
5989a64e 10173 The result is allocated on the objfile->per_bfd's obstack and
45940949 10174 canonicalized. */
94af9270
KS
10175
10176static const char *
15d034d0
TT
10177dwarf2_compute_name (const char *name,
10178 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10179 int physname)
10180{
5e22e966 10181 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10182
94af9270
KS
10183 if (name == NULL)
10184 name = dwarf2_name (die, cu);
10185
2ee7123e
DE
10186 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10187 but otherwise compute it by typename_concat inside GDB.
10188 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10189 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10190 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10191 will set the demangled name to the result of dwarf2_full_name, and it is
10192 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10193 if (cu->language == language_ada
10194 || (cu->language == language_fortran && physname))
10195 {
10196 /* For Ada unit, we prefer the linkage name over the name, as
10197 the former contains the exported name, which the user expects
10198 to be able to reference. Ideally, we want the user to be able
10199 to reference this entity using either natural or linkage name,
10200 but we haven't started looking at this enhancement yet. */
73b9be8b 10201 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10202
2ee7123e
DE
10203 if (linkage_name != NULL)
10204 return linkage_name;
f55ee35c
JK
10205 }
10206
94af9270
KS
10207 /* These are the only languages we know how to qualify names in. */
10208 if (name != NULL
9c37b5ae 10209 && (cu->language == language_cplus
c44af4eb
TT
10210 || cu->language == language_fortran || cu->language == language_d
10211 || cu->language == language_rust))
94af9270
KS
10212 {
10213 if (die_needs_namespace (die, cu))
10214 {
0d5cff50 10215 const char *prefix;
34a68019 10216 const char *canonical_name = NULL;
94af9270 10217
d7e74731
PA
10218 string_file buf;
10219
94af9270 10220 prefix = determine_prefix (die, cu);
94af9270
KS
10221 if (*prefix != '\0')
10222 {
43816ebc
TT
10223 gdb::unique_xmalloc_ptr<char> prefixed_name
10224 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10225
43816ebc 10226 buf.puts (prefixed_name.get ());
94af9270
KS
10227 }
10228 else
d7e74731 10229 buf.puts (name);
94af9270 10230
98bfdba5
PA
10231 /* Template parameters may be specified in the DIE's DW_AT_name, or
10232 as children with DW_TAG_template_type_param or
10233 DW_TAG_value_type_param. If the latter, add them to the name
10234 here. If the name already has template parameters, then
10235 skip this step; some versions of GCC emit both, and
10236 it is more efficient to use the pre-computed name.
10237
10238 Something to keep in mind about this process: it is very
10239 unlikely, or in some cases downright impossible, to produce
10240 something that will match the mangled name of a function.
10241 If the definition of the function has the same debug info,
10242 we should be able to match up with it anyway. But fallbacks
10243 using the minimal symbol, for instance to find a method
10244 implemented in a stripped copy of libstdc++, will not work.
10245 If we do not have debug info for the definition, we will have to
10246 match them up some other way.
10247
10248 When we do name matching there is a related problem with function
10249 templates; two instantiated function templates are allowed to
10250 differ only by their return types, which we do not add here. */
10251
10252 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10253 {
10254 struct attribute *attr;
10255 struct die_info *child;
10256 int first = 1;
10257
10258 die->building_fullname = 1;
10259
10260 for (child = die->child; child != NULL; child = child->sibling)
10261 {
10262 struct type *type;
12df843f 10263 LONGEST value;
d521ce57 10264 const gdb_byte *bytes;
98bfdba5
PA
10265 struct dwarf2_locexpr_baton *baton;
10266 struct value *v;
10267
10268 if (child->tag != DW_TAG_template_type_param
10269 && child->tag != DW_TAG_template_value_param)
10270 continue;
10271
10272 if (first)
10273 {
d7e74731 10274 buf.puts ("<");
98bfdba5
PA
10275 first = 0;
10276 }
10277 else
d7e74731 10278 buf.puts (", ");
98bfdba5
PA
10279
10280 attr = dwarf2_attr (child, DW_AT_type, cu);
10281 if (attr == NULL)
10282 {
b98664d3 10283 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10284 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10285 continue;
10286 }
10287 type = die_type (child, cu);
10288
10289 if (child->tag == DW_TAG_template_type_param)
10290 {
c1ec8cea
TT
10291 c_print_type (type, "", &buf, -1, 0, cu->language,
10292 &type_print_raw_options);
98bfdba5
PA
10293 continue;
10294 }
10295
10296 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10297 if (attr == NULL)
10298 {
b98664d3 10299 complaint (_("template parameter missing "
3e43a32a 10300 "DW_AT_const_value"));
d7e74731 10301 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10302 continue;
10303 }
10304
10305 dwarf2_const_value_attr (attr, type, name,
10306 &cu->comp_unit_obstack, cu,
10307 &value, &bytes, &baton);
10308
10309 if (TYPE_NOSIGN (type))
10310 /* GDB prints characters as NUMBER 'CHAR'. If that's
10311 changed, this can use value_print instead. */
d7e74731 10312 c_printchar (value, type, &buf);
98bfdba5
PA
10313 else
10314 {
10315 struct value_print_options opts;
10316
10317 if (baton != NULL)
10318 v = dwarf2_evaluate_loc_desc (type, NULL,
10319 baton->data,
10320 baton->size,
10321 baton->per_cu);
10322 else if (bytes != NULL)
10323 {
10324 v = allocate_value (type);
10325 memcpy (value_contents_writeable (v), bytes,
10326 TYPE_LENGTH (type));
10327 }
10328 else
10329 v = value_from_longest (type, value);
10330
3e43a32a
MS
10331 /* Specify decimal so that we do not depend on
10332 the radix. */
98bfdba5
PA
10333 get_formatted_print_options (&opts, 'd');
10334 opts.raw = 1;
d7e74731 10335 value_print (v, &buf, &opts);
98bfdba5 10336 release_value (v);
98bfdba5
PA
10337 }
10338 }
10339
10340 die->building_fullname = 0;
10341
10342 if (!first)
10343 {
10344 /* Close the argument list, with a space if necessary
10345 (nested templates). */
d7e74731
PA
10346 if (!buf.empty () && buf.string ().back () == '>')
10347 buf.puts (" >");
98bfdba5 10348 else
d7e74731 10349 buf.puts (">");
98bfdba5
PA
10350 }
10351 }
10352
9c37b5ae 10353 /* For C++ methods, append formal parameter type
94af9270 10354 information, if PHYSNAME. */
6e70227d 10355
94af9270 10356 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10357 && cu->language == language_cplus)
94af9270
KS
10358 {
10359 struct type *type = read_type_die (die, cu);
10360
d7e74731 10361 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10362 &type_print_raw_options);
94af9270 10363
9c37b5ae 10364 if (cu->language == language_cplus)
94af9270 10365 {
60430eff
DJ
10366 /* Assume that an artificial first parameter is
10367 "this", but do not crash if it is not. RealView
10368 marks unnamed (and thus unused) parameters as
10369 artificial; there is no way to differentiate
10370 the two cases. */
1f704f76 10371 if (type->num_fields () > 0
94af9270 10372 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10373 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10374 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10375 0))))
d7e74731 10376 buf.puts (" const");
94af9270
KS
10377 }
10378 }
10379
d7e74731 10380 const std::string &intermediate_name = buf.string ();
94af9270
KS
10381
10382 if (cu->language == language_cplus)
34a68019 10383 canonical_name
322a8516 10384 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10385 objfile);
34a68019
TT
10386
10387 /* If we only computed INTERMEDIATE_NAME, or if
10388 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10389 intern it. */
322a8516 10390 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10391 name = objfile->intern (intermediate_name);
34a68019
TT
10392 else
10393 name = canonical_name;
94af9270
KS
10394 }
10395 }
10396
10397 return name;
10398}
10399
0114d602
DJ
10400/* Return the fully qualified name of DIE, based on its DW_AT_name.
10401 If scope qualifiers are appropriate they will be added. The result
34a68019 10402 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10403 not have a name. NAME may either be from a previous call to
10404 dwarf2_name or NULL.
10405
9c37b5ae 10406 The output string will be canonicalized (if C++). */
0114d602
DJ
10407
10408static const char *
15d034d0 10409dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10410{
94af9270
KS
10411 return dwarf2_compute_name (name, die, cu, 0);
10412}
0114d602 10413
94af9270
KS
10414/* Construct a physname for the given DIE in CU. NAME may either be
10415 from a previous call to dwarf2_name or NULL. The result will be
10416 allocated on the objfile_objstack or NULL if the DIE does not have a
10417 name.
0114d602 10418
9c37b5ae 10419 The output string will be canonicalized (if C++). */
0114d602 10420
94af9270 10421static const char *
15d034d0 10422dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10423{
5e22e966 10424 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10425 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10426 int need_copy = 1;
10427
10428 /* In this case dwarf2_compute_name is just a shortcut not building anything
10429 on its own. */
10430 if (!die_needs_namespace (die, cu))
10431 return dwarf2_compute_name (name, die, cu, 1);
10432
906bb4c5
TT
10433 if (cu->language != language_rust)
10434 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10435
10436 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10437 has computed. */
791afaa2 10438 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10439 if (mangled != NULL)
900e11f9 10440 {
900e11f9 10441
59cc4834
JB
10442 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10443 {
10444 /* Do nothing (do not demangle the symbol name). */
10445 }
10446 else if (cu->language == language_go)
a766d390 10447 {
5e2db402
TT
10448 /* This is a lie, but we already lie to the caller new_symbol.
10449 new_symbol assumes we return the mangled name.
a766d390 10450 This just undoes that lie until things are cleaned up. */
a766d390
DE
10451 }
10452 else
10453 {
0eb876f5
JB
10454 /* Use DMGL_RET_DROP for C++ template functions to suppress
10455 their return type. It is easier for GDB users to search
10456 for such functions as `name(params)' than `long name(params)'.
10457 In such case the minimal symbol names do not match the full
10458 symbol names but for template functions there is never a need
10459 to look up their definition from their declaration so
10460 the only disadvantage remains the minimal symbol variant
10461 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10462 demangled.reset (gdb_demangle (mangled,
10463 (DMGL_PARAMS | DMGL_ANSI
10464 | DMGL_RET_DROP)));
a766d390 10465 }
900e11f9 10466 if (demangled)
791afaa2 10467 canon = demangled.get ();
900e11f9
JK
10468 else
10469 {
10470 canon = mangled;
10471 need_copy = 0;
10472 }
10473 }
10474
10475 if (canon == NULL || check_physname)
10476 {
10477 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10478
10479 if (canon != NULL && strcmp (physname, canon) != 0)
10480 {
10481 /* It may not mean a bug in GDB. The compiler could also
10482 compute DW_AT_linkage_name incorrectly. But in such case
10483 GDB would need to be bug-to-bug compatible. */
10484
b98664d3 10485 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10486 "(from linkage <%s>) - DIE at %s [in module %s]"),
10487 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10488 objfile_name (objfile));
900e11f9
JK
10489
10490 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10491 is available here - over computed PHYSNAME. It is safer
10492 against both buggy GDB and buggy compilers. */
10493
10494 retval = canon;
10495 }
10496 else
10497 {
10498 retval = physname;
10499 need_copy = 0;
10500 }
10501 }
10502 else
10503 retval = canon;
10504
10505 if (need_copy)
be1e3d3e 10506 retval = objfile->intern (retval);
900e11f9 10507
900e11f9 10508 return retval;
0114d602
DJ
10509}
10510
74921315
KS
10511/* Inspect DIE in CU for a namespace alias. If one exists, record
10512 a new symbol for it.
10513
10514 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10515
10516static int
10517read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10518{
10519 struct attribute *attr;
10520
10521 /* If the die does not have a name, this is not a namespace
10522 alias. */
10523 attr = dwarf2_attr (die, DW_AT_name, cu);
10524 if (attr != NULL)
10525 {
10526 int num;
10527 struct die_info *d = die;
10528 struct dwarf2_cu *imported_cu = cu;
10529
10530 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10531 keep inspecting DIEs until we hit the underlying import. */
10532#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10533 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10534 {
10535 attr = dwarf2_attr (d, DW_AT_import, cu);
10536 if (attr == NULL)
10537 break;
10538
10539 d = follow_die_ref (d, attr, &imported_cu);
10540 if (d->tag != DW_TAG_imported_declaration)
10541 break;
10542 }
10543
10544 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10545 {
b98664d3 10546 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10547 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10548 return 0;
10549 }
10550
10551 if (attr != NULL)
10552 {
10553 struct type *type;
0826b30a 10554 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10555
9c541725 10556 type = get_die_type_at_offset (sect_off, cu->per_cu);
78134374 10557 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10558 {
10559 /* This declaration is a global namespace alias. Add
10560 a symbol for it whose type is the aliased namespace. */
10561 new_symbol (die, type, cu);
10562 return 1;
10563 }
10564 }
10565 }
10566
10567 return 0;
10568}
10569
22cee43f 10570/* Return the using directives repository (global or local?) to use in the
804d2729 10571 current context for CU.
22cee43f
PMR
10572
10573 For Ada, imported declarations can materialize renamings, which *may* be
10574 global. However it is impossible (for now?) in DWARF to distinguish
10575 "external" imported declarations and "static" ones. As all imported
10576 declarations seem to be static in all other languages, make them all CU-wide
10577 global only in Ada. */
10578
10579static struct using_direct **
804d2729 10580using_directives (struct dwarf2_cu *cu)
22cee43f 10581{
c24bdb02
KS
10582 if (cu->language == language_ada
10583 && cu->get_builder ()->outermost_context_p ())
10584 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10585 else
c24bdb02 10586 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10587}
10588
27aa8d6a
SW
10589/* Read the import statement specified by the given die and record it. */
10590
10591static void
10592read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10593{
5e22e966 10594 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10595 struct attribute *import_attr;
32019081 10596 struct die_info *imported_die, *child_die;
de4affc9 10597 struct dwarf2_cu *imported_cu;
27aa8d6a 10598 const char *imported_name;
794684b6 10599 const char *imported_name_prefix;
13387711
SW
10600 const char *canonical_name;
10601 const char *import_alias;
10602 const char *imported_declaration = NULL;
794684b6 10603 const char *import_prefix;
eb1e02fd 10604 std::vector<const char *> excludes;
13387711 10605
27aa8d6a
SW
10606 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10607 if (import_attr == NULL)
10608 {
b98664d3 10609 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10610 dwarf_tag_name (die->tag));
10611 return;
10612 }
10613
de4affc9
CC
10614 imported_cu = cu;
10615 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10616 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10617 if (imported_name == NULL)
10618 {
10619 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10620
10621 The import in the following code:
10622 namespace A
10623 {
10624 typedef int B;
10625 }
10626
10627 int main ()
10628 {
10629 using A::B;
10630 B b;
10631 return b;
10632 }
10633
10634 ...
10635 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10636 <52> DW_AT_decl_file : 1
10637 <53> DW_AT_decl_line : 6
10638 <54> DW_AT_import : <0x75>
10639 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10640 <59> DW_AT_name : B
10641 <5b> DW_AT_decl_file : 1
10642 <5c> DW_AT_decl_line : 2
10643 <5d> DW_AT_type : <0x6e>
10644 ...
10645 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10646 <76> DW_AT_byte_size : 4
10647 <77> DW_AT_encoding : 5 (signed)
10648
10649 imports the wrong die ( 0x75 instead of 0x58 ).
10650 This case will be ignored until the gcc bug is fixed. */
10651 return;
10652 }
10653
82856980
SW
10654 /* Figure out the local name after import. */
10655 import_alias = dwarf2_name (die, cu);
27aa8d6a 10656
794684b6
SW
10657 /* Figure out where the statement is being imported to. */
10658 import_prefix = determine_prefix (die, cu);
10659
10660 /* Figure out what the scope of the imported die is and prepend it
10661 to the name of the imported die. */
de4affc9 10662 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10663
f55ee35c
JK
10664 if (imported_die->tag != DW_TAG_namespace
10665 && imported_die->tag != DW_TAG_module)
794684b6 10666 {
13387711
SW
10667 imported_declaration = imported_name;
10668 canonical_name = imported_name_prefix;
794684b6 10669 }
13387711 10670 else if (strlen (imported_name_prefix) > 0)
12aaed36 10671 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10672 imported_name_prefix,
10673 (cu->language == language_d ? "." : "::"),
10674 imported_name, (char *) NULL);
13387711
SW
10675 else
10676 canonical_name = imported_name;
794684b6 10677
32019081
JK
10678 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10679 for (child_die = die->child; child_die && child_die->tag;
436c571c 10680 child_die = child_die->sibling)
32019081
JK
10681 {
10682 /* DWARF-4: A Fortran use statement with a “rename list” may be
10683 represented by an imported module entry with an import attribute
10684 referring to the module and owned entries corresponding to those
10685 entities that are renamed as part of being imported. */
10686
10687 if (child_die->tag != DW_TAG_imported_declaration)
10688 {
b98664d3 10689 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10690 "- DIE at %s [in module %s]"),
10691 sect_offset_str (child_die->sect_off),
10692 objfile_name (objfile));
32019081
JK
10693 continue;
10694 }
10695
10696 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10697 if (import_attr == NULL)
10698 {
b98664d3 10699 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10700 dwarf_tag_name (child_die->tag));
10701 continue;
10702 }
10703
10704 imported_cu = cu;
10705 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10706 &imported_cu);
10707 imported_name = dwarf2_name (imported_die, imported_cu);
10708 if (imported_name == NULL)
10709 {
b98664d3 10710 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10711 "imported name - DIE at %s [in module %s]"),
10712 sect_offset_str (child_die->sect_off),
10713 objfile_name (objfile));
32019081
JK
10714 continue;
10715 }
10716
eb1e02fd 10717 excludes.push_back (imported_name);
32019081
JK
10718
10719 process_die (child_die, cu);
10720 }
10721
804d2729 10722 add_using_directive (using_directives (cu),
22cee43f
PMR
10723 import_prefix,
10724 canonical_name,
10725 import_alias,
10726 imported_declaration,
10727 excludes,
10728 0,
10729 &objfile->objfile_obstack);
27aa8d6a
SW
10730}
10731
5230b05a
WT
10732/* ICC<14 does not output the required DW_AT_declaration on incomplete
10733 types, but gives them a size of zero. Starting with version 14,
10734 ICC is compatible with GCC. */
10735
9068261f 10736static bool
5230b05a
WT
10737producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10738{
10739 if (!cu->checked_producer)
10740 check_producer (cu);
10741
10742 return cu->producer_is_icc_lt_14;
10743}
10744
eb77c9df
AB
10745/* ICC generates a DW_AT_type for C void functions. This was observed on
10746 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10747 which says that void functions should not have a DW_AT_type. */
10748
10749static bool
10750producer_is_icc (struct dwarf2_cu *cu)
10751{
10752 if (!cu->checked_producer)
10753 check_producer (cu);
10754
10755 return cu->producer_is_icc;
10756}
10757
1b80a9fa
JK
10758/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10759 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10760 this, it was first present in GCC release 4.3.0. */
10761
9068261f 10762static bool
1b80a9fa
JK
10763producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10764{
10765 if (!cu->checked_producer)
10766 check_producer (cu);
10767
10768 return cu->producer_is_gcc_lt_4_3;
10769}
10770
d721ba37
PA
10771static file_and_directory
10772find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10773{
d721ba37
PA
10774 file_and_directory res;
10775
9291a0cd
TT
10776 /* Find the filename. Do not use dwarf2_name here, since the filename
10777 is not a source language identifier. */
d721ba37
PA
10778 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10779 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10780
d721ba37
PA
10781 if (res.comp_dir == NULL
10782 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10783 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10784 {
d721ba37
PA
10785 res.comp_dir_storage = ldirname (res.name);
10786 if (!res.comp_dir_storage.empty ())
10787 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10788 }
d721ba37 10789 if (res.comp_dir != NULL)
9291a0cd
TT
10790 {
10791 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10792 directory, get rid of it. */
d721ba37 10793 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10794
d721ba37
PA
10795 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10796 res.comp_dir = cp + 1;
9291a0cd
TT
10797 }
10798
d721ba37
PA
10799 if (res.name == NULL)
10800 res.name = "<unknown>";
10801
10802 return res;
9291a0cd
TT
10803}
10804
f4dc4d17
DE
10805/* Handle DW_AT_stmt_list for a compilation unit.
10806 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10807 COMP_DIR is the compilation directory. LOWPC is passed to
10808 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10809
10810static void
10811handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10812 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10813{
5e22e966 10814 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10815 struct attribute *attr;
527f3840
JK
10816 struct line_header line_header_local;
10817 hashval_t line_header_local_hash;
527f3840
JK
10818 void **slot;
10819 int decode_mapping;
2ab95328 10820
f4dc4d17
DE
10821 gdb_assert (! cu->per_cu->is_debug_types);
10822
2ab95328 10823 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10824 if (attr == NULL)
10825 return;
10826
9c541725 10827 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10828
10829 /* The line header hash table is only created if needed (it exists to
10830 prevent redundant reading of the line table for partial_units).
10831 If we're given a partial_unit, we'll need it. If we're given a
10832 compile_unit, then use the line header hash table if it's already
10833 created, but don't create one just yet. */
10834
5989a64e 10835 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10836 && die->tag == DW_TAG_partial_unit)
2ab95328 10837 {
5989a64e 10838 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10839 .reset (htab_create_alloc (127, line_header_hash_voidp,
10840 line_header_eq_voidp,
10841 free_line_header_voidp,
10842 xcalloc, xfree));
527f3840 10843 }
2ab95328 10844
9c541725 10845 line_header_local.sect_off = line_offset;
527f3840
JK
10846 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10847 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10848 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10849 {
5989a64e 10850 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10851 &line_header_local,
10852 line_header_local_hash, NO_INSERT);
10853
10854 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10855 is not present in *SLOT (since if there is something in *SLOT then
10856 it will be for a partial_unit). */
10857 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10858 {
527f3840 10859 gdb_assert (*slot != NULL);
9a3c8263 10860 cu->line_header = (struct line_header *) *slot;
527f3840 10861 return;
dee91e82 10862 }
2ab95328 10863 }
527f3840
JK
10864
10865 /* dwarf_decode_line_header does not yet provide sufficient information.
10866 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10867 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10868 if (lh == NULL)
527f3840 10869 return;
4c8aa72d
PA
10870
10871 cu->line_header = lh.release ();
10872 cu->line_header_die_owner = die;
527f3840 10873
5989a64e 10874 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10875 slot = NULL;
10876 else
10877 {
5989a64e 10878 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10879 &line_header_local,
10880 line_header_local_hash, INSERT);
10881 gdb_assert (slot != NULL);
10882 }
10883 if (slot != NULL && *slot == NULL)
10884 {
10885 /* This newly decoded line number information unit will be owned
10886 by line_header_hash hash table. */
10887 *slot = cu->line_header;
4c8aa72d 10888 cu->line_header_die_owner = NULL;
527f3840
JK
10889 }
10890 else
10891 {
10892 /* We cannot free any current entry in (*slot) as that struct line_header
10893 may be already used by multiple CUs. Create only temporary decoded
10894 line_header for this CU - it may happen at most once for each line
10895 number information unit. And if we're not using line_header_hash
10896 then this is what we want as well. */
10897 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10898 }
10899 decode_mapping = (die->tag != DW_TAG_partial_unit);
10900 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10901 decode_mapping);
fff8551c 10902
2ab95328
TT
10903}
10904
95554aad 10905/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10906
c906108c 10907static void
e7c27a73 10908read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10909{
5e22e966 10910 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10911 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10912 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10913 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10914 CORE_ADDR highpc = ((CORE_ADDR) 0);
10915 struct attribute *attr;
c906108c 10916 struct die_info *child_die;
e142c38c 10917 CORE_ADDR baseaddr;
6e70227d 10918
380618d6 10919 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10920 baseaddr = objfile->text_section_offset ();
c906108c 10921
fae299cd 10922 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10923
10924 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10925 from finish_block. */
2acceee2 10926 if (lowpc == ((CORE_ADDR) -1))
c906108c 10927 lowpc = highpc;
3e29f34a 10928 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10929
d721ba37 10930 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10931
f4b8a18d
KW
10932 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10933 standardised yet. As a workaround for the language detection we fall
10934 back to the DW_AT_producer string. */
10935 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10936 cu->language = language_opencl;
10937
3019eac3
DE
10938 /* Similar hack for Go. */
10939 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10940 set_cu_language (DW_LANG_Go, cu);
10941
c24bdb02 10942 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10943
10944 /* Decode line number information if present. We do this before
10945 processing child DIEs, so that the line header table is available
10946 for DW_AT_decl_file. */
d721ba37 10947 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10948
10949 /* Process all dies in compilation unit. */
10950 if (die->child != NULL)
10951 {
10952 child_die = die->child;
10953 while (child_die && child_die->tag)
10954 {
10955 process_die (child_die, cu);
436c571c 10956 child_die = child_die->sibling;
3019eac3
DE
10957 }
10958 }
10959
10960 /* Decode macro information, if present. Dwarf 2 macro information
10961 refers to information in the line number info statement program
10962 header, so we can only read it if we've read the header
10963 successfully. */
0af92d60
JK
10964 attr = dwarf2_attr (die, DW_AT_macros, cu);
10965 if (attr == NULL)
10966 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10967 if (attr && cu->line_header)
10968 {
10969 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10970 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10971
43f3e411 10972 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10973 }
10974 else
10975 {
10976 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10977 if (attr && cu->line_header)
10978 {
10979 unsigned int macro_offset = DW_UNSND (attr);
10980
43f3e411 10981 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10982 }
10983 }
3019eac3
DE
10984}
10985
c24bdb02
KS
10986void
10987dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10988{
f4dc4d17
DE
10989 struct type_unit_group *tu_group;
10990 int first_time;
3019eac3 10991 struct attribute *attr;
9c541725 10992 unsigned int i;
0186c6a7 10993 struct signatured_type *sig_type;
3019eac3 10994
f4dc4d17 10995 gdb_assert (per_cu->is_debug_types);
0186c6a7 10996 sig_type = (struct signatured_type *) per_cu;
3019eac3 10997
c24bdb02 10998 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10999
f4dc4d17 11000 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11001 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11002 if (sig_type->type_unit_group == NULL)
c24bdb02 11003 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11004 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11005
11006 /* If we've already processed this stmt_list there's no real need to
11007 do it again, we could fake it and just recreate the part we need
11008 (file name,index -> symtab mapping). If data shows this optimization
11009 is useful we can do it then. */
43f3e411 11010 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11011
11012 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11013 debug info. */
fff8551c 11014 line_header_up lh;
f4dc4d17 11015 if (attr != NULL)
3019eac3 11016 {
9c541725 11017 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11018 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11019 }
11020 if (lh == NULL)
11021 {
11022 if (first_time)
c24bdb02 11023 start_symtab ("", NULL, 0);
f4dc4d17
DE
11024 else
11025 {
11026 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11027 gdb_assert (m_builder == nullptr);
804d2729 11028 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11029 m_builder.reset (new struct buildsym_compunit
11030 (COMPUNIT_OBJFILE (cust), "",
11031 COMPUNIT_DIRNAME (cust),
11032 compunit_language (cust),
11033 0, cust));
770479f2 11034 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11035 }
f4dc4d17 11036 return;
3019eac3
DE
11037 }
11038
c24bdb02
KS
11039 line_header = lh.release ();
11040 line_header_die_owner = die;
3019eac3 11041
f4dc4d17
DE
11042 if (first_time)
11043 {
c24bdb02 11044 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11045
1fd60fc0
DE
11046 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11047 still initializing it, and our caller (a few levels up)
11048 process_full_type_unit still needs to know if this is the first
11049 time. */
11050
4ac93832
TT
11051 tu_group->symtabs
11052 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11053 struct symtab *, line_header->file_names_size ());
3019eac3 11054
7ba99d21
AT
11055 auto &file_names = line_header->file_names ();
11056 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11057 {
7ba99d21 11058 file_entry &fe = file_names[i];
c24bdb02
KS
11059 dwarf2_start_subfile (this, fe.name,
11060 fe.include_dir (line_header));
11061 buildsym_compunit *b = get_builder ();
11062 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11063 {
4c8aa72d
PA
11064 /* NOTE: start_subfile will recognize when it's been
11065 passed a file it has already seen. So we can't
11066 assume there's a simple mapping from
11067 cu->line_header->file_names to subfiles, plus
11068 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11069 b->get_current_subfile ()->symtab
11070 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11071 }
11072
c24bdb02 11073 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11074 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11075 }
11076 }
11077 else
3019eac3 11078 {
c24bdb02 11079 gdb_assert (m_builder == nullptr);
804d2729 11080 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11081 m_builder.reset (new struct buildsym_compunit
11082 (COMPUNIT_OBJFILE (cust), "",
11083 COMPUNIT_DIRNAME (cust),
11084 compunit_language (cust),
11085 0, cust));
770479f2 11086 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11087
7ba99d21
AT
11088 auto &file_names = line_header->file_names ();
11089 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11090 {
7ba99d21 11091 file_entry &fe = file_names[i];
4c8aa72d 11092 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11093 }
3019eac3
DE
11094 }
11095
f4dc4d17
DE
11096 /* The main symtab is allocated last. Type units don't have DW_AT_name
11097 so they don't have a "real" (so to speak) symtab anyway.
11098 There is later code that will assign the main symtab to all symbols
11099 that don't have one. We need to handle the case of a symbol with a
11100 missing symtab (DW_AT_decl_file) anyway. */
11101}
3019eac3 11102
f4dc4d17
DE
11103/* Process DW_TAG_type_unit.
11104 For TUs we want to skip the first top level sibling if it's not the
11105 actual type being defined by this TU. In this case the first top
11106 level sibling is there to provide context only. */
3019eac3 11107
f4dc4d17
DE
11108static void
11109read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11110{
11111 struct die_info *child_die;
3019eac3 11112
f4dc4d17
DE
11113 prepare_one_comp_unit (cu, die, language_minimal);
11114
11115 /* Initialize (or reinitialize) the machinery for building symtabs.
11116 We do this before processing child DIEs, so that the line header table
11117 is available for DW_AT_decl_file. */
c24bdb02 11118 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11119
11120 if (die->child != NULL)
11121 {
11122 child_die = die->child;
11123 while (child_die && child_die->tag)
11124 {
11125 process_die (child_die, cu);
436c571c 11126 child_die = child_die->sibling;
f4dc4d17
DE
11127 }
11128 }
3019eac3
DE
11129}
11130\f
80626a55
DE
11131/* DWO/DWP files.
11132
11133 http://gcc.gnu.org/wiki/DebugFission
11134 http://gcc.gnu.org/wiki/DebugFissionDWP
11135
11136 To simplify handling of both DWO files ("object" files with the DWARF info)
11137 and DWP files (a file with the DWOs packaged up into one file), we treat
11138 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11139
11140static hashval_t
11141hash_dwo_file (const void *item)
11142{
9a3c8263 11143 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11144 hashval_t hash;
3019eac3 11145
a2ce51a0
DE
11146 hash = htab_hash_string (dwo_file->dwo_name);
11147 if (dwo_file->comp_dir != NULL)
11148 hash += htab_hash_string (dwo_file->comp_dir);
11149 return hash;
3019eac3
DE
11150}
11151
11152static int
11153eq_dwo_file (const void *item_lhs, const void *item_rhs)
11154{
9a3c8263
SM
11155 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11156 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11157
a2ce51a0
DE
11158 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11159 return 0;
11160 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11161 return lhs->comp_dir == rhs->comp_dir;
11162 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11163}
11164
11165/* Allocate a hash table for DWO files. */
11166
51ac9db5 11167static htab_up
298e9637 11168allocate_dwo_file_hash_table ()
3019eac3 11169{
51ac9db5
SM
11170 auto delete_dwo_file = [] (void *item)
11171 {
11172 struct dwo_file *dwo_file = (struct dwo_file *) item;
11173
11174 delete dwo_file;
11175 };
11176
bc68fb19
TT
11177 return htab_up (htab_create_alloc (41,
11178 hash_dwo_file,
11179 eq_dwo_file,
11180 delete_dwo_file,
11181 xcalloc, xfree));
3019eac3
DE
11182}
11183
80626a55
DE
11184/* Lookup DWO file DWO_NAME. */
11185
11186static void **
ed2dc618
SM
11187lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11188 const char *dwo_name,
11189 const char *comp_dir)
80626a55
DE
11190{
11191 struct dwo_file find_entry;
11192 void **slot;
11193
5989a64e
SM
11194 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11195 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11196
0ac5b59e
DE
11197 find_entry.dwo_name = dwo_name;
11198 find_entry.comp_dir = comp_dir;
5989a64e 11199 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11200 INSERT);
80626a55
DE
11201
11202 return slot;
11203}
11204
3019eac3
DE
11205static hashval_t
11206hash_dwo_unit (const void *item)
11207{
9a3c8263 11208 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11209
11210 /* This drops the top 32 bits of the id, but is ok for a hash. */
11211 return dwo_unit->signature;
11212}
11213
11214static int
11215eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11216{
9a3c8263
SM
11217 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11218 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11219
11220 /* The signature is assumed to be unique within the DWO file.
11221 So while object file CU dwo_id's always have the value zero,
11222 that's OK, assuming each object file DWO file has only one CU,
11223 and that's the rule for now. */
11224 return lhs->signature == rhs->signature;
11225}
11226
11227/* Allocate a hash table for DWO CUs,TUs.
11228 There is one of these tables for each of CUs,TUs for each DWO file. */
11229
b0b6a987 11230static htab_up
298e9637 11231allocate_dwo_unit_table ()
3019eac3
DE
11232{
11233 /* Start out with a pretty small number.
11234 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11235 return htab_up (htab_create_alloc (3,
11236 hash_dwo_unit,
11237 eq_dwo_unit,
11238 NULL, xcalloc, xfree));
3019eac3
DE
11239}
11240
19c3d4c9 11241/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11242
11243static void
19c3d4c9
DE
11244create_dwo_cu_reader (const struct die_reader_specs *reader,
11245 const gdb_byte *info_ptr,
11246 struct die_info *comp_unit_die,
c0ab21c2
TT
11247 struct dwo_file *dwo_file,
11248 struct dwo_unit *dwo_unit)
3019eac3
DE
11249{
11250 struct dwarf2_cu *cu = reader->cu;
9c541725 11251 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11252 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11253
a084a2a6
AT
11254 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11255 if (!signature.has_value ())
3019eac3 11256 {
b98664d3 11257 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11258 " its dwo_id [in module %s]"),
9d8780f0 11259 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11260 return;
11261 }
11262
3019eac3 11263 dwo_unit->dwo_file = dwo_file;
a084a2a6 11264 dwo_unit->signature = *signature;
8a0459fd 11265 dwo_unit->section = section;
9c541725 11266 dwo_unit->sect_off = sect_off;
3019eac3
DE
11267 dwo_unit->length = cu->per_cu->length;
11268
b4f54984 11269 if (dwarf_read_debug)
9d8780f0
SM
11270 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11271 sect_offset_str (sect_off),
9c541725 11272 hex_string (dwo_unit->signature));
3019eac3
DE
11273}
11274
33c5cd75 11275/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11276 Note: This function processes DWO files only, not DWP files. */
3019eac3 11277
33c5cd75 11278static void
ed2dc618 11279create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11280 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11281 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11282{
11283 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11284 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11285 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11286
96b79293 11287 section.read (objfile);
33c5cd75 11288 info_ptr = section.buffer;
3019eac3
DE
11289
11290 if (info_ptr == NULL)
33c5cd75 11291 return;
3019eac3 11292
b4f54984 11293 if (dwarf_read_debug)
19c3d4c9
DE
11294 {
11295 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11296 section.get_name (),
11297 section.get_file_name ());
19c3d4c9 11298 }
3019eac3 11299
33c5cd75 11300 end_ptr = info_ptr + section.size;
3019eac3
DE
11301 while (info_ptr < end_ptr)
11302 {
11303 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11304 struct dwo_unit read_unit {};
33c5cd75
DB
11305 struct dwo_unit *dwo_unit;
11306 void **slot;
11307 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11308
11309 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11310 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 11311 per_cu.per_bfd = per_bfd;
3019eac3 11312 per_cu.is_debug_types = 0;
33c5cd75
DB
11313 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11314 per_cu.section = &section;
11315
c0ab21c2
TT
11316 cutu_reader reader (&per_cu, cu, &dwo_file);
11317 if (!reader.dummy_p)
11318 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11319 &dwo_file, &read_unit);
33c5cd75
DB
11320 info_ptr += per_cu.length;
11321
11322 // If the unit could not be parsed, skip it.
c0ab21c2 11323 if (read_unit.dwo_file == NULL)
33c5cd75 11324 continue;
3019eac3 11325
33c5cd75 11326 if (cus_htab == NULL)
298e9637 11327 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11328
1859c670 11329 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11330 struct dwo_unit);
c0ab21c2 11331 *dwo_unit = read_unit;
b0b6a987 11332 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11333 gdb_assert (slot != NULL);
11334 if (*slot != NULL)
19c3d4c9 11335 {
33c5cd75
DB
11336 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11337 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11338
b98664d3 11339 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11340 " the entry at offset %s, signature %s"),
11341 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11342 hex_string (dwo_unit->signature));
19c3d4c9 11343 }
33c5cd75 11344 *slot = (void *)dwo_unit;
3019eac3 11345 }
3019eac3
DE
11346}
11347
80626a55
DE
11348/* DWP file .debug_{cu,tu}_index section format:
11349 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11350
d2415c6c
DE
11351 DWP Version 1:
11352
80626a55
DE
11353 Both index sections have the same format, and serve to map a 64-bit
11354 signature to a set of section numbers. Each section begins with a header,
11355 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11356 indexes, and a pool of 32-bit section numbers. The index sections will be
11357 aligned at 8-byte boundaries in the file.
11358
d2415c6c
DE
11359 The index section header consists of:
11360
11361 V, 32 bit version number
11362 -, 32 bits unused
11363 N, 32 bit number of compilation units or type units in the index
11364 M, 32 bit number of slots in the hash table
80626a55 11365
d2415c6c 11366 Numbers are recorded using the byte order of the application binary.
80626a55 11367
d2415c6c
DE
11368 The hash table begins at offset 16 in the section, and consists of an array
11369 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11370 order of the application binary). Unused slots in the hash table are 0.
11371 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11372
d2415c6c
DE
11373 The parallel table begins immediately after the hash table
11374 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11375 array of 32-bit indexes (using the byte order of the application binary),
11376 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11377 table contains a 32-bit index into the pool of section numbers. For unused
11378 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11379
73869dc2
DE
11380 The pool of section numbers begins immediately following the hash table
11381 (at offset 16 + 12 * M from the beginning of the section). The pool of
11382 section numbers consists of an array of 32-bit words (using the byte order
11383 of the application binary). Each item in the array is indexed starting
11384 from 0. The hash table entry provides the index of the first section
11385 number in the set. Additional section numbers in the set follow, and the
11386 set is terminated by a 0 entry (section number 0 is not used in ELF).
11387
11388 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11389 section must be the first entry in the set, and the .debug_abbrev.dwo must
11390 be the second entry. Other members of the set may follow in any order.
11391
11392 ---
11393
11394 DWP Version 2:
11395
11396 DWP Version 2 combines all the .debug_info, etc. sections into one,
11397 and the entries in the index tables are now offsets into these sections.
11398 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11399 section.
11400
11401 Index Section Contents:
11402 Header
11403 Hash Table of Signatures dwp_hash_table.hash_table
11404 Parallel Table of Indices dwp_hash_table.unit_table
11405 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11406 Table of Section Sizes dwp_hash_table.v2.sizes
11407
11408 The index section header consists of:
11409
11410 V, 32 bit version number
11411 L, 32 bit number of columns in the table of section offsets
11412 N, 32 bit number of compilation units or type units in the index
11413 M, 32 bit number of slots in the hash table
11414
11415 Numbers are recorded using the byte order of the application binary.
11416
11417 The hash table has the same format as version 1.
11418 The parallel table of indices has the same format as version 1,
11419 except that the entries are origin-1 indices into the table of sections
11420 offsets and the table of section sizes.
11421
11422 The table of offsets begins immediately following the parallel table
11423 (at offset 16 + 12 * M from the beginning of the section). The table is
11424 a two-dimensional array of 32-bit words (using the byte order of the
11425 application binary), with L columns and N+1 rows, in row-major order.
11426 Each row in the array is indexed starting from 0. The first row provides
11427 a key to the remaining rows: each column in this row provides an identifier
11428 for a debug section, and the offsets in the same column of subsequent rows
11429 refer to that section. The section identifiers are:
11430
11431 DW_SECT_INFO 1 .debug_info.dwo
11432 DW_SECT_TYPES 2 .debug_types.dwo
11433 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11434 DW_SECT_LINE 4 .debug_line.dwo
11435 DW_SECT_LOC 5 .debug_loc.dwo
11436 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11437 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11438 DW_SECT_MACRO 8 .debug_macro.dwo
11439
11440 The offsets provided by the CU and TU index sections are the base offsets
11441 for the contributions made by each CU or TU to the corresponding section
11442 in the package file. Each CU and TU header contains an abbrev_offset
11443 field, used to find the abbreviations table for that CU or TU within the
11444 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11445 be interpreted as relative to the base offset given in the index section.
11446 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11447 should be interpreted as relative to the base offset for .debug_line.dwo,
11448 and offsets into other debug sections obtained from DWARF attributes should
11449 also be interpreted as relative to the corresponding base offset.
11450
11451 The table of sizes begins immediately following the table of offsets.
11452 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11453 with L columns and N rows, in row-major order. Each row in the array is
11454 indexed starting from 1 (row 0 is shared by the two tables).
11455
11456 ---
11457
11458 Hash table lookup is handled the same in version 1 and 2:
11459
11460 We assume that N and M will not exceed 2^32 - 1.
11461 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11462
d2415c6c
DE
11463 Given a 64-bit compilation unit signature or a type signature S, an entry
11464 in the hash table is located as follows:
80626a55 11465
d2415c6c
DE
11466 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11467 the low-order k bits all set to 1.
80626a55 11468
d2415c6c 11469 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11470
d2415c6c
DE
11471 3) If the hash table entry at index H matches the signature, use that
11472 entry. If the hash table entry at index H is unused (all zeroes),
11473 terminate the search: the signature is not present in the table.
80626a55 11474
d2415c6c 11475 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11476
d2415c6c 11477 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11478 to stop at an unused slot or find the match. */
80626a55
DE
11479
11480/* Create a hash table to map DWO IDs to their CU/TU entry in
11481 .debug_{info,types}.dwo in DWP_FILE.
11482 Returns NULL if there isn't one.
11483 Note: This function processes DWP files only, not DWO files. */
11484
11485static struct dwp_hash_table *
ed2dc618
SM
11486create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11487 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11488{
11489 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11490 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11491 const gdb_byte *index_ptr, *index_end;
80626a55 11492 struct dwarf2_section_info *index;
73869dc2 11493 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11494 struct dwp_hash_table *htab;
11495
11496 if (is_debug_types)
11497 index = &dwp_file->sections.tu_index;
11498 else
11499 index = &dwp_file->sections.cu_index;
11500
96b79293 11501 if (index->empty ())
80626a55 11502 return NULL;
96b79293 11503 index->read (objfile);
80626a55
DE
11504
11505 index_ptr = index->buffer;
11506 index_end = index_ptr + index->size;
11507
11508 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11509 index_ptr += 4;
11510 if (version == 2)
11511 nr_columns = read_4_bytes (dbfd, index_ptr);
11512 else
11513 nr_columns = 0;
11514 index_ptr += 4;
80626a55
DE
11515 nr_units = read_4_bytes (dbfd, index_ptr);
11516 index_ptr += 4;
11517 nr_slots = read_4_bytes (dbfd, index_ptr);
11518 index_ptr += 4;
11519
73869dc2 11520 if (version != 1 && version != 2)
80626a55 11521 {
21aa081e 11522 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11523 " [in module %s]"),
21aa081e 11524 pulongest (version), dwp_file->name);
80626a55
DE
11525 }
11526 if (nr_slots != (nr_slots & -nr_slots))
11527 {
21aa081e 11528 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11529 " is not power of 2 [in module %s]"),
21aa081e 11530 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11531 }
11532
5989a64e 11533 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11534 htab->version = version;
11535 htab->nr_columns = nr_columns;
80626a55
DE
11536 htab->nr_units = nr_units;
11537 htab->nr_slots = nr_slots;
11538 htab->hash_table = index_ptr;
11539 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11540
11541 /* Exit early if the table is empty. */
11542 if (nr_slots == 0 || nr_units == 0
11543 || (version == 2 && nr_columns == 0))
11544 {
11545 /* All must be zero. */
11546 if (nr_slots != 0 || nr_units != 0
11547 || (version == 2 && nr_columns != 0))
11548 {
b98664d3 11549 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11550 " all zero [in modules %s]"),
11551 dwp_file->name);
11552 }
11553 return htab;
11554 }
11555
11556 if (version == 1)
11557 {
11558 htab->section_pool.v1.indices =
11559 htab->unit_table + sizeof (uint32_t) * nr_slots;
11560 /* It's harder to decide whether the section is too small in v1.
11561 V1 is deprecated anyway so we punt. */
11562 }
11563 else
11564 {
11565 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11566 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11567 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11568 /* Reverse map for error checking. */
11569 int ids_seen[DW_SECT_MAX + 1];
11570 int i;
11571
11572 if (nr_columns < 2)
11573 {
11574 error (_("Dwarf Error: bad DWP hash table, too few columns"
11575 " in section table [in module %s]"),
11576 dwp_file->name);
11577 }
11578 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11579 {
11580 error (_("Dwarf Error: bad DWP hash table, too many columns"
11581 " in section table [in module %s]"),
11582 dwp_file->name);
11583 }
04fd5eed
GB
11584 memset (ids, 255, sizeof_ids);
11585 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11586 for (i = 0; i < nr_columns; ++i)
11587 {
11588 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11589
11590 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11591 {
11592 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11593 " in section table [in module %s]"),
11594 id, dwp_file->name);
11595 }
11596 if (ids_seen[id] != -1)
11597 {
11598 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11599 " id %d in section table [in module %s]"),
11600 id, dwp_file->name);
11601 }
11602 ids_seen[id] = i;
11603 ids[i] = id;
11604 }
11605 /* Must have exactly one info or types section. */
11606 if (((ids_seen[DW_SECT_INFO] != -1)
11607 + (ids_seen[DW_SECT_TYPES] != -1))
11608 != 1)
11609 {
11610 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11611 " DWO info/types section [in module %s]"),
11612 dwp_file->name);
11613 }
11614 /* Must have an abbrev section. */
11615 if (ids_seen[DW_SECT_ABBREV] == -1)
11616 {
11617 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11618 " section [in module %s]"),
11619 dwp_file->name);
11620 }
11621 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11622 htab->section_pool.v2.sizes =
11623 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11624 * nr_units * nr_columns);
11625 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11626 * nr_units * nr_columns))
11627 > index_end)
11628 {
11629 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11630 " [in module %s]"),
11631 dwp_file->name);
11632 }
11633 }
80626a55
DE
11634
11635 return htab;
11636}
11637
11638/* Update SECTIONS with the data from SECTP.
11639
11640 This function is like the other "locate" section routines that are
11641 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11642 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11643
11644 The result is non-zero for success, or zero if an error was found. */
11645
11646static int
73869dc2
DE
11647locate_v1_virtual_dwo_sections (asection *sectp,
11648 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11649{
11650 const struct dwop_section_names *names = &dwop_section_names;
11651
11652 if (section_is_p (sectp->name, &names->abbrev_dwo))
11653 {
11654 /* There can be only one. */
049412e3 11655 if (sections->abbrev.s.section != NULL)
80626a55 11656 return 0;
049412e3 11657 sections->abbrev.s.section = sectp;
fd361982 11658 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11659 }
11660 else if (section_is_p (sectp->name, &names->info_dwo)
11661 || section_is_p (sectp->name, &names->types_dwo))
11662 {
11663 /* There can be only one. */
049412e3 11664 if (sections->info_or_types.s.section != NULL)
80626a55 11665 return 0;
049412e3 11666 sections->info_or_types.s.section = sectp;
fd361982 11667 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11668 }
11669 else if (section_is_p (sectp->name, &names->line_dwo))
11670 {
11671 /* There can be only one. */
049412e3 11672 if (sections->line.s.section != NULL)
80626a55 11673 return 0;
049412e3 11674 sections->line.s.section = sectp;
fd361982 11675 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11676 }
11677 else if (section_is_p (sectp->name, &names->loc_dwo))
11678 {
11679 /* There can be only one. */
049412e3 11680 if (sections->loc.s.section != NULL)
80626a55 11681 return 0;
049412e3 11682 sections->loc.s.section = sectp;
fd361982 11683 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11684 }
11685 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11686 {
11687 /* There can be only one. */
049412e3 11688 if (sections->macinfo.s.section != NULL)
80626a55 11689 return 0;
049412e3 11690 sections->macinfo.s.section = sectp;
fd361982 11691 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11692 }
11693 else if (section_is_p (sectp->name, &names->macro_dwo))
11694 {
11695 /* There can be only one. */
049412e3 11696 if (sections->macro.s.section != NULL)
80626a55 11697 return 0;
049412e3 11698 sections->macro.s.section = sectp;
fd361982 11699 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11700 }
11701 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11702 {
11703 /* There can be only one. */
049412e3 11704 if (sections->str_offsets.s.section != NULL)
80626a55 11705 return 0;
049412e3 11706 sections->str_offsets.s.section = sectp;
fd361982 11707 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11708 }
11709 else
11710 {
11711 /* No other kind of section is valid. */
11712 return 0;
11713 }
11714
11715 return 1;
11716}
11717
73869dc2
DE
11718/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11719 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11720 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11721 This is for DWP version 1 files. */
80626a55
DE
11722
11723static struct dwo_unit *
ed2dc618
SM
11724create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11725 struct dwp_file *dwp_file,
73869dc2
DE
11726 uint32_t unit_index,
11727 const char *comp_dir,
11728 ULONGEST signature, int is_debug_types)
80626a55 11729{
73869dc2
DE
11730 const struct dwp_hash_table *dwp_htab =
11731 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11732 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11733 const char *kind = is_debug_types ? "TU" : "CU";
11734 struct dwo_file *dwo_file;
11735 struct dwo_unit *dwo_unit;
73869dc2 11736 struct virtual_v1_dwo_sections sections;
80626a55 11737 void **dwo_file_slot;
80626a55
DE
11738 int i;
11739
73869dc2
DE
11740 gdb_assert (dwp_file->version == 1);
11741
b4f54984 11742 if (dwarf_read_debug)
80626a55 11743 {
73869dc2 11744 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11745 kind,
73869dc2 11746 pulongest (unit_index), hex_string (signature),
80626a55
DE
11747 dwp_file->name);
11748 }
11749
19ac8c2e 11750 /* Fetch the sections of this DWO unit.
80626a55
DE
11751 Put a limit on the number of sections we look for so that bad data
11752 doesn't cause us to loop forever. */
11753
73869dc2 11754#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11755 (1 /* .debug_info or .debug_types */ \
11756 + 1 /* .debug_abbrev */ \
11757 + 1 /* .debug_line */ \
11758 + 1 /* .debug_loc */ \
11759 + 1 /* .debug_str_offsets */ \
19ac8c2e 11760 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11761 + 1 /* trailing zero */)
11762
11763 memset (&sections, 0, sizeof (sections));
80626a55 11764
73869dc2 11765 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11766 {
11767 asection *sectp;
11768 uint32_t section_nr =
11769 read_4_bytes (dbfd,
73869dc2
DE
11770 dwp_htab->section_pool.v1.indices
11771 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11772
11773 if (section_nr == 0)
11774 break;
11775 if (section_nr >= dwp_file->num_sections)
11776 {
11777 error (_("Dwarf Error: bad DWP hash table, section number too large"
11778 " [in module %s]"),
11779 dwp_file->name);
11780 }
11781
11782 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11783 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11784 {
11785 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11786 " [in module %s]"),
11787 dwp_file->name);
11788 }
11789 }
11790
11791 if (i < 2
96b79293
TT
11792 || sections.info_or_types.empty ()
11793 || sections.abbrev.empty ())
80626a55
DE
11794 {
11795 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11796 " [in module %s]"),
11797 dwp_file->name);
11798 }
73869dc2 11799 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11800 {
11801 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11802 " [in module %s]"),
11803 dwp_file->name);
11804 }
11805
11806 /* It's easier for the rest of the code if we fake a struct dwo_file and
11807 have dwo_unit "live" in that. At least for now.
11808
11809 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11810 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11811 file, we can combine them back into a virtual DWO file to save space
11812 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11813 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11814
791afaa2
TT
11815 std::string virtual_dwo_name =
11816 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11817 sections.abbrev.get_id (),
11818 sections.line.get_id (),
11819 sections.loc.get_id (),
11820 sections.str_offsets.get_id ());
80626a55 11821 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11822 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11823 virtual_dwo_name.c_str (),
11824 comp_dir);
80626a55
DE
11825 /* Create one if necessary. */
11826 if (*dwo_file_slot == NULL)
11827 {
b4f54984 11828 if (dwarf_read_debug)
80626a55
DE
11829 {
11830 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11831 virtual_dwo_name.c_str ());
80626a55 11832 }
51ac9db5 11833 dwo_file = new struct dwo_file;
45940949 11834 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11835 dwo_file->comp_dir = comp_dir;
80626a55
DE
11836 dwo_file->sections.abbrev = sections.abbrev;
11837 dwo_file->sections.line = sections.line;
11838 dwo_file->sections.loc = sections.loc;
11839 dwo_file->sections.macinfo = sections.macinfo;
11840 dwo_file->sections.macro = sections.macro;
11841 dwo_file->sections.str_offsets = sections.str_offsets;
11842 /* The "str" section is global to the entire DWP file. */
11843 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11844 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11845 there's no need to record it in dwo_file.
11846 Also, we can't simply record type sections in dwo_file because
11847 we record a pointer into the vector in dwo_unit. As we collect more
11848 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11849 for it, invalidating all copies of pointers into the previous
11850 contents. */
80626a55
DE
11851 *dwo_file_slot = dwo_file;
11852 }
11853 else
11854 {
b4f54984 11855 if (dwarf_read_debug)
80626a55
DE
11856 {
11857 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11858 virtual_dwo_name.c_str ());
80626a55 11859 }
9a3c8263 11860 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11861 }
80626a55 11862
5989a64e 11863 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11864 dwo_unit->dwo_file = dwo_file;
11865 dwo_unit->signature = signature;
8d749320 11866 dwo_unit->section =
5989a64e 11867 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11868 *dwo_unit->section = sections.info_or_types;
57d63ce2 11869 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11870
11871 return dwo_unit;
11872}
11873
73869dc2
DE
11874/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11875 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11876 piece within that section used by a TU/CU, return a virtual section
11877 of just that piece. */
11878
11879static struct dwarf2_section_info
ed2dc618
SM
11880create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11881 struct dwarf2_section_info *section,
73869dc2
DE
11882 bfd_size_type offset, bfd_size_type size)
11883{
11884 struct dwarf2_section_info result;
11885 asection *sectp;
11886
11887 gdb_assert (section != NULL);
11888 gdb_assert (!section->is_virtual);
11889
11890 memset (&result, 0, sizeof (result));
11891 result.s.containing_section = section;
dc4ccb6f 11892 result.is_virtual = true;
73869dc2
DE
11893
11894 if (size == 0)
11895 return result;
11896
96b79293 11897 sectp = section->get_bfd_section ();
73869dc2
DE
11898
11899 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11900 bounds of the real section. This is a pretty-rare event, so just
11901 flag an error (easier) instead of a warning and trying to cope. */
11902 if (sectp == NULL
fd361982 11903 || offset + size > bfd_section_size (sectp))
73869dc2 11904 {
73869dc2
DE
11905 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11906 " in section %s [in module %s]"),
fd361982 11907 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11908 objfile_name (dwarf2_per_objfile->objfile));
11909 }
11910
11911 result.virtual_offset = offset;
11912 result.size = size;
11913 return result;
11914}
11915
11916/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11917 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11918 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11919 This is for DWP version 2 files. */
11920
11921static struct dwo_unit *
ed2dc618
SM
11922create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11923 struct dwp_file *dwp_file,
73869dc2
DE
11924 uint32_t unit_index,
11925 const char *comp_dir,
11926 ULONGEST signature, int is_debug_types)
11927{
73869dc2
DE
11928 const struct dwp_hash_table *dwp_htab =
11929 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11930 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11931 const char *kind = is_debug_types ? "TU" : "CU";
11932 struct dwo_file *dwo_file;
11933 struct dwo_unit *dwo_unit;
11934 struct virtual_v2_dwo_sections sections;
11935 void **dwo_file_slot;
73869dc2
DE
11936 int i;
11937
11938 gdb_assert (dwp_file->version == 2);
11939
b4f54984 11940 if (dwarf_read_debug)
73869dc2
DE
11941 {
11942 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11943 kind,
11944 pulongest (unit_index), hex_string (signature),
11945 dwp_file->name);
11946 }
11947
11948 /* Fetch the section offsets of this DWO unit. */
11949
11950 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11951
11952 for (i = 0; i < dwp_htab->nr_columns; ++i)
11953 {
11954 uint32_t offset = read_4_bytes (dbfd,
11955 dwp_htab->section_pool.v2.offsets
11956 + (((unit_index - 1) * dwp_htab->nr_columns
11957 + i)
11958 * sizeof (uint32_t)));
11959 uint32_t size = read_4_bytes (dbfd,
11960 dwp_htab->section_pool.v2.sizes
11961 + (((unit_index - 1) * dwp_htab->nr_columns
11962 + i)
11963 * sizeof (uint32_t)));
11964
11965 switch (dwp_htab->section_pool.v2.section_ids[i])
11966 {
11967 case DW_SECT_INFO:
11968 case DW_SECT_TYPES:
11969 sections.info_or_types_offset = offset;
11970 sections.info_or_types_size = size;
11971 break;
11972 case DW_SECT_ABBREV:
11973 sections.abbrev_offset = offset;
11974 sections.abbrev_size = size;
11975 break;
11976 case DW_SECT_LINE:
11977 sections.line_offset = offset;
11978 sections.line_size = size;
11979 break;
11980 case DW_SECT_LOC:
11981 sections.loc_offset = offset;
11982 sections.loc_size = size;
11983 break;
11984 case DW_SECT_STR_OFFSETS:
11985 sections.str_offsets_offset = offset;
11986 sections.str_offsets_size = size;
11987 break;
11988 case DW_SECT_MACINFO:
11989 sections.macinfo_offset = offset;
11990 sections.macinfo_size = size;
11991 break;
11992 case DW_SECT_MACRO:
11993 sections.macro_offset = offset;
11994 sections.macro_size = size;
11995 break;
11996 }
11997 }
11998
11999 /* It's easier for the rest of the code if we fake a struct dwo_file and
12000 have dwo_unit "live" in that. At least for now.
12001
12002 The DWP file can be made up of a random collection of CUs and TUs.
12003 However, for each CU + set of TUs that came from the same original DWO
12004 file, we can combine them back into a virtual DWO file to save space
12005 (fewer struct dwo_file objects to allocate). Remember that for really
12006 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12007
791afaa2
TT
12008 std::string virtual_dwo_name =
12009 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12010 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12011 (long) (sections.line_size ? sections.line_offset : 0),
12012 (long) (sections.loc_size ? sections.loc_offset : 0),
12013 (long) (sections.str_offsets_size
12014 ? sections.str_offsets_offset : 0));
73869dc2 12015 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12016 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12017 virtual_dwo_name.c_str (),
12018 comp_dir);
73869dc2
DE
12019 /* Create one if necessary. */
12020 if (*dwo_file_slot == NULL)
12021 {
b4f54984 12022 if (dwarf_read_debug)
73869dc2
DE
12023 {
12024 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12025 virtual_dwo_name.c_str ());
73869dc2 12026 }
51ac9db5 12027 dwo_file = new struct dwo_file;
45940949 12028 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12029 dwo_file->comp_dir = comp_dir;
12030 dwo_file->sections.abbrev =
ed2dc618 12031 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12032 sections.abbrev_offset, sections.abbrev_size);
12033 dwo_file->sections.line =
ed2dc618 12034 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12035 sections.line_offset, sections.line_size);
12036 dwo_file->sections.loc =
ed2dc618 12037 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12038 sections.loc_offset, sections.loc_size);
12039 dwo_file->sections.macinfo =
ed2dc618 12040 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12041 sections.macinfo_offset, sections.macinfo_size);
12042 dwo_file->sections.macro =
ed2dc618 12043 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12044 sections.macro_offset, sections.macro_size);
12045 dwo_file->sections.str_offsets =
ed2dc618
SM
12046 create_dwp_v2_section (dwarf2_per_objfile,
12047 &dwp_file->sections.str_offsets,
73869dc2
DE
12048 sections.str_offsets_offset,
12049 sections.str_offsets_size);
12050 /* The "str" section is global to the entire DWP file. */
12051 dwo_file->sections.str = dwp_file->sections.str;
12052 /* The info or types section is assigned below to dwo_unit,
12053 there's no need to record it in dwo_file.
12054 Also, we can't simply record type sections in dwo_file because
12055 we record a pointer into the vector in dwo_unit. As we collect more
12056 types we'll grow the vector and eventually have to reallocate space
12057 for it, invalidating all copies of pointers into the previous
12058 contents. */
12059 *dwo_file_slot = dwo_file;
12060 }
12061 else
12062 {
b4f54984 12063 if (dwarf_read_debug)
73869dc2
DE
12064 {
12065 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12066 virtual_dwo_name.c_str ());
73869dc2 12067 }
9a3c8263 12068 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12069 }
73869dc2 12070
5989a64e 12071 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12072 dwo_unit->dwo_file = dwo_file;
12073 dwo_unit->signature = signature;
8d749320 12074 dwo_unit->section =
5989a64e 12075 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12076 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12077 is_debug_types
73869dc2
DE
12078 ? &dwp_file->sections.types
12079 : &dwp_file->sections.info,
12080 sections.info_or_types_offset,
12081 sections.info_or_types_size);
12082 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12083
12084 return dwo_unit;
12085}
12086
57d63ce2
DE
12087/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12088 Returns NULL if the signature isn't found. */
80626a55
DE
12089
12090static struct dwo_unit *
ed2dc618
SM
12091lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12092 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12093 ULONGEST signature, int is_debug_types)
80626a55 12094{
57d63ce2
DE
12095 const struct dwp_hash_table *dwp_htab =
12096 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12097 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12098 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12099 uint32_t hash = signature & mask;
12100 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12101 unsigned int i;
12102 void **slot;
870f88f7 12103 struct dwo_unit find_dwo_cu;
80626a55
DE
12104
12105 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12106 find_dwo_cu.signature = signature;
19ac8c2e 12107 slot = htab_find_slot (is_debug_types
48b490f2
TT
12108 ? dwp_file->loaded_tus.get ()
12109 : dwp_file->loaded_cus.get (),
19ac8c2e 12110 &find_dwo_cu, INSERT);
80626a55
DE
12111
12112 if (*slot != NULL)
9a3c8263 12113 return (struct dwo_unit *) *slot;
80626a55
DE
12114
12115 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12116 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12117 {
12118 ULONGEST signature_in_table;
12119
12120 signature_in_table =
57d63ce2 12121 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12122 if (signature_in_table == signature)
12123 {
57d63ce2
DE
12124 uint32_t unit_index =
12125 read_4_bytes (dbfd,
12126 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12127
73869dc2
DE
12128 if (dwp_file->version == 1)
12129 {
ed2dc618
SM
12130 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12131 dwp_file, unit_index,
73869dc2
DE
12132 comp_dir, signature,
12133 is_debug_types);
12134 }
12135 else
12136 {
ed2dc618
SM
12137 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12138 dwp_file, unit_index,
73869dc2
DE
12139 comp_dir, signature,
12140 is_debug_types);
12141 }
9a3c8263 12142 return (struct dwo_unit *) *slot;
80626a55
DE
12143 }
12144 if (signature_in_table == 0)
12145 return NULL;
12146 hash = (hash + hash2) & mask;
12147 }
12148
12149 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12150 " [in module %s]"),
12151 dwp_file->name);
12152}
12153
ab5088bf 12154/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12155 Open the file specified by FILE_NAME and hand it off to BFD for
12156 preliminary analysis. Return a newly initialized bfd *, which
12157 includes a canonicalized copy of FILE_NAME.
80626a55 12158 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12159 SEARCH_CWD is true if the current directory is to be searched.
12160 It will be searched before debug-file-directory.
13aaf454
DE
12161 If successful, the file is added to the bfd include table of the
12162 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12163 If unable to find/open the file, return NULL.
3019eac3
DE
12164 NOTE: This function is derived from symfile_bfd_open. */
12165
192b62ce 12166static gdb_bfd_ref_ptr
ed2dc618
SM
12167try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12168 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12169{
24b9144d 12170 int desc;
9c02c129
DE
12171 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12172 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12173 to debug_file_directory. */
e0cc99a6 12174 const char *search_path;
9c02c129
DE
12175 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12176
e0cc99a6 12177 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12178 if (search_cwd)
12179 {
12180 if (*debug_file_directory != '\0')
e0cc99a6
TT
12181 {
12182 search_path_holder.reset (concat (".", dirname_separator_string,
12183 debug_file_directory,
12184 (char *) NULL));
12185 search_path = search_path_holder.get ();
12186 }
6ac97d4c 12187 else
e0cc99a6 12188 search_path = ".";
6ac97d4c 12189 }
9c02c129 12190 else
e0cc99a6 12191 search_path = debug_file_directory;
3019eac3 12192
24b9144d 12193 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12194 if (is_dwp)
12195 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12196
12197 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12198 desc = openp (search_path, flags, file_name,
3019eac3
DE
12199 O_RDONLY | O_BINARY, &absolute_name);
12200 if (desc < 0)
12201 return NULL;
12202
e0cc99a6
TT
12203 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12204 gnutarget, desc));
9c02c129
DE
12205 if (sym_bfd == NULL)
12206 return NULL;
192b62ce 12207 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12208
192b62ce
TT
12209 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12210 return NULL;
3019eac3 12211
13aaf454
DE
12212 /* Success. Record the bfd as having been included by the objfile's bfd.
12213 This is important because things like demangled_names_hash lives in the
12214 objfile's per_bfd space and may have references to things like symbol
12215 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12216 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12217
3019eac3
DE
12218 return sym_bfd;
12219}
12220
ab5088bf 12221/* Try to open DWO file FILE_NAME.
3019eac3
DE
12222 COMP_DIR is the DW_AT_comp_dir attribute.
12223 The result is the bfd handle of the file.
12224 If there is a problem finding or opening the file, return NULL.
12225 Upon success, the canonicalized path of the file is stored in the bfd,
12226 same as symfile_bfd_open. */
12227
192b62ce 12228static gdb_bfd_ref_ptr
ed2dc618
SM
12229open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12230 const char *file_name, const char *comp_dir)
3019eac3 12231{
80626a55 12232 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12233 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12234 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12235
12236 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12237
12238 if (comp_dir != NULL)
12239 {
43816ebc
TT
12240 gdb::unique_xmalloc_ptr<char> path_to_try
12241 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12242
12243 /* NOTE: If comp_dir is a relative path, this will also try the
12244 search path, which seems useful. */
ed2dc618 12245 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12246 path_to_try.get (),
ed2dc618 12247 0 /*is_dwp*/,
192b62ce 12248 1 /*search_cwd*/));
3019eac3
DE
12249 if (abfd != NULL)
12250 return abfd;
12251 }
12252
12253 /* That didn't work, try debug-file-directory, which, despite its name,
12254 is a list of paths. */
12255
12256 if (*debug_file_directory == '\0')
12257 return NULL;
12258
ed2dc618
SM
12259 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12260 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12261}
12262
80626a55
DE
12263/* This function is mapped across the sections and remembers the offset and
12264 size of each of the DWO debugging sections we are interested in. */
12265
12266static void
12267dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12268{
9a3c8263 12269 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12270 const struct dwop_section_names *names = &dwop_section_names;
12271
12272 if (section_is_p (sectp->name, &names->abbrev_dwo))
12273 {
049412e3 12274 dwo_sections->abbrev.s.section = sectp;
fd361982 12275 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12276 }
12277 else if (section_is_p (sectp->name, &names->info_dwo))
12278 {
049412e3 12279 dwo_sections->info.s.section = sectp;
fd361982 12280 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12281 }
12282 else if (section_is_p (sectp->name, &names->line_dwo))
12283 {
049412e3 12284 dwo_sections->line.s.section = sectp;
fd361982 12285 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12286 }
12287 else if (section_is_p (sectp->name, &names->loc_dwo))
12288 {
049412e3 12289 dwo_sections->loc.s.section = sectp;
fd361982 12290 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12291 }
41144253 12292 else if (section_is_p (sectp->name, &names->loclists_dwo))
12293 {
12294 dwo_sections->loclists.s.section = sectp;
12295 dwo_sections->loclists.size = bfd_section_size (sectp);
12296 }
80626a55
DE
12297 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12298 {
049412e3 12299 dwo_sections->macinfo.s.section = sectp;
fd361982 12300 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12301 }
12302 else if (section_is_p (sectp->name, &names->macro_dwo))
12303 {
049412e3 12304 dwo_sections->macro.s.section = sectp;
fd361982 12305 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12306 }
12307 else if (section_is_p (sectp->name, &names->str_dwo))
12308 {
049412e3 12309 dwo_sections->str.s.section = sectp;
fd361982 12310 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12311 }
12312 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12313 {
049412e3 12314 dwo_sections->str_offsets.s.section = sectp;
fd361982 12315 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12316 }
12317 else if (section_is_p (sectp->name, &names->types_dwo))
12318 {
12319 struct dwarf2_section_info type_section;
12320
12321 memset (&type_section, 0, sizeof (type_section));
049412e3 12322 type_section.s.section = sectp;
fd361982 12323 type_section.size = bfd_section_size (sectp);
fd5866f6 12324 dwo_sections->types.push_back (type_section);
80626a55
DE
12325 }
12326}
12327
ab5088bf 12328/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12329 by PER_CU. This is for the non-DWP case.
80626a55 12330 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12331
12332static struct dwo_file *
0ac5b59e
DE
12333open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12334 const char *dwo_name, const char *comp_dir)
3019eac3 12335{
ed2dc618 12336 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12337
fb1eb2f9 12338 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12339 if (dbfd == NULL)
12340 {
b4f54984 12341 if (dwarf_read_debug)
80626a55
DE
12342 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12343 return NULL;
12344 }
263db9a1 12345
51ac9db5 12346 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12347 dwo_file->dwo_name = dwo_name;
12348 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12349 dwo_file->dbfd = std::move (dbfd);
3019eac3 12350
fb1eb2f9 12351 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12352 &dwo_file->sections);
3019eac3 12353
18a8505e
AT
12354 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12355 dwo_file->sections.info, dwo_file->cus);
3019eac3 12356
263db9a1 12357 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12358 dwo_file->sections.types, dwo_file->tus);
3019eac3 12359
b4f54984 12360 if (dwarf_read_debug)
80626a55
DE
12361 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12362
263db9a1 12363 return dwo_file.release ();
3019eac3
DE
12364}
12365
80626a55 12366/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12367 size of each of the DWP debugging sections common to version 1 and 2 that
12368 we are interested in. */
3019eac3 12369
80626a55 12370static void
73869dc2
DE
12371dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12372 void *dwp_file_ptr)
3019eac3 12373{
9a3c8263 12374 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12375 const struct dwop_section_names *names = &dwop_section_names;
12376 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12377
80626a55 12378 /* Record the ELF section number for later lookup: this is what the
73869dc2 12379 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12380 gdb_assert (elf_section_nr < dwp_file->num_sections);
12381 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12382
80626a55
DE
12383 /* Look for specific sections that we need. */
12384 if (section_is_p (sectp->name, &names->str_dwo))
12385 {
049412e3 12386 dwp_file->sections.str.s.section = sectp;
fd361982 12387 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12388 }
12389 else if (section_is_p (sectp->name, &names->cu_index))
12390 {
049412e3 12391 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12392 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12393 }
12394 else if (section_is_p (sectp->name, &names->tu_index))
12395 {
049412e3 12396 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12397 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12398 }
12399}
3019eac3 12400
73869dc2
DE
12401/* This function is mapped across the sections and remembers the offset and
12402 size of each of the DWP version 2 debugging sections that we are interested
12403 in. This is split into a separate function because we don't know if we
12404 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12405
12406static void
12407dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12408{
9a3c8263 12409 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12410 const struct dwop_section_names *names = &dwop_section_names;
12411 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12412
12413 /* Record the ELF section number for later lookup: this is what the
12414 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12415 gdb_assert (elf_section_nr < dwp_file->num_sections);
12416 dwp_file->elf_sections[elf_section_nr] = sectp;
12417
12418 /* Look for specific sections that we need. */
12419 if (section_is_p (sectp->name, &names->abbrev_dwo))
12420 {
049412e3 12421 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12422 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12423 }
12424 else if (section_is_p (sectp->name, &names->info_dwo))
12425 {
049412e3 12426 dwp_file->sections.info.s.section = sectp;
fd361982 12427 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12428 }
12429 else if (section_is_p (sectp->name, &names->line_dwo))
12430 {
049412e3 12431 dwp_file->sections.line.s.section = sectp;
fd361982 12432 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12433 }
12434 else if (section_is_p (sectp->name, &names->loc_dwo))
12435 {
049412e3 12436 dwp_file->sections.loc.s.section = sectp;
fd361982 12437 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12438 }
12439 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12440 {
049412e3 12441 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12442 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12443 }
12444 else if (section_is_p (sectp->name, &names->macro_dwo))
12445 {
049412e3 12446 dwp_file->sections.macro.s.section = sectp;
fd361982 12447 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12448 }
12449 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12450 {
049412e3 12451 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12452 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12453 }
12454 else if (section_is_p (sectp->name, &names->types_dwo))
12455 {
049412e3 12456 dwp_file->sections.types.s.section = sectp;
fd361982 12457 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12458 }
12459}
12460
80626a55 12461/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12462
80626a55
DE
12463static hashval_t
12464hash_dwp_loaded_cutus (const void *item)
12465{
9a3c8263 12466 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12467
80626a55
DE
12468 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12469 return dwo_unit->signature;
3019eac3
DE
12470}
12471
80626a55 12472/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12473
80626a55
DE
12474static int
12475eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12476{
9a3c8263
SM
12477 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12478 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12479
80626a55
DE
12480 return dua->signature == dub->signature;
12481}
3019eac3 12482
80626a55 12483/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12484
48b490f2 12485static htab_up
298e9637 12486allocate_dwp_loaded_cutus_table ()
80626a55 12487{
48b490f2
TT
12488 return htab_up (htab_create_alloc (3,
12489 hash_dwp_loaded_cutus,
12490 eq_dwp_loaded_cutus,
12491 NULL, xcalloc, xfree));
80626a55 12492}
3019eac3 12493
ab5088bf
DE
12494/* Try to open DWP file FILE_NAME.
12495 The result is the bfd handle of the file.
12496 If there is a problem finding or opening the file, return NULL.
12497 Upon success, the canonicalized path of the file is stored in the bfd,
12498 same as symfile_bfd_open. */
12499
192b62ce 12500static gdb_bfd_ref_ptr
ed2dc618
SM
12501open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12502 const char *file_name)
ab5088bf 12503{
ed2dc618
SM
12504 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12505 1 /*is_dwp*/,
192b62ce 12506 1 /*search_cwd*/));
6ac97d4c
DE
12507 if (abfd != NULL)
12508 return abfd;
12509
12510 /* Work around upstream bug 15652.
12511 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12512 [Whether that's a "bug" is debatable, but it is getting in our way.]
12513 We have no real idea where the dwp file is, because gdb's realpath-ing
12514 of the executable's path may have discarded the needed info.
12515 [IWBN if the dwp file name was recorded in the executable, akin to
12516 .gnu_debuglink, but that doesn't exist yet.]
12517 Strip the directory from FILE_NAME and search again. */
12518 if (*debug_file_directory != '\0')
12519 {
12520 /* Don't implicitly search the current directory here.
12521 If the user wants to search "." to handle this case,
12522 it must be added to debug-file-directory. */
ed2dc618
SM
12523 return try_open_dwop_file (dwarf2_per_objfile,
12524 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12525 0 /*search_cwd*/);
12526 }
12527
12528 return NULL;
ab5088bf
DE
12529}
12530
80626a55
DE
12531/* Initialize the use of the DWP file for the current objfile.
12532 By convention the name of the DWP file is ${objfile}.dwp.
12533 The result is NULL if it can't be found. */
a766d390 12534
400174b1 12535static std::unique_ptr<struct dwp_file>
ed2dc618 12536open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12537{
12538 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12539
82bf32bc
JK
12540 /* Try to find first .dwp for the binary file before any symbolic links
12541 resolving. */
6c447423
DE
12542
12543 /* If the objfile is a debug file, find the name of the real binary
12544 file and get the name of dwp file from there. */
d721ba37 12545 std::string dwp_name;
6c447423
DE
12546 if (objfile->separate_debug_objfile_backlink != NULL)
12547 {
12548 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12549 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12550
d721ba37 12551 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12552 }
12553 else
d721ba37
PA
12554 dwp_name = objfile->original_name;
12555
12556 dwp_name += ".dwp";
80626a55 12557
ed2dc618 12558 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12559 if (dbfd == NULL
12560 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12561 {
12562 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12563 dwp_name = objfile_name (objfile);
12564 dwp_name += ".dwp";
ed2dc618 12565 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12566 }
12567
80626a55
DE
12568 if (dbfd == NULL)
12569 {
b4f54984 12570 if (dwarf_read_debug)
d721ba37 12571 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12572 return std::unique_ptr<dwp_file> ();
3019eac3 12573 }
400174b1
TT
12574
12575 const char *name = bfd_get_filename (dbfd.get ());
12576 std::unique_ptr<struct dwp_file> dwp_file
12577 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12578
0a0f4c01 12579 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12580 dwp_file->elf_sections =
5989a64e 12581 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12582 dwp_file->num_sections, asection *);
12583
400174b1
TT
12584 bfd_map_over_sections (dwp_file->dbfd.get (),
12585 dwarf2_locate_common_dwp_sections,
12586 dwp_file.get ());
80626a55 12587
400174b1
TT
12588 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12589 0);
80626a55 12590
400174b1
TT
12591 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12592 1);
80626a55 12593
73869dc2 12594 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12595 if (dwp_file->cus && dwp_file->tus
12596 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12597 {
12598 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12599 pretty bizarre. We use pulongest here because that's the established
4d65956b 12600 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12601 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12602 " TU version %s [in DWP file %s]"),
12603 pulongest (dwp_file->cus->version),
d721ba37 12604 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12605 }
08302ed2
DE
12606
12607 if (dwp_file->cus)
12608 dwp_file->version = dwp_file->cus->version;
12609 else if (dwp_file->tus)
12610 dwp_file->version = dwp_file->tus->version;
12611 else
12612 dwp_file->version = 2;
73869dc2
DE
12613
12614 if (dwp_file->version == 2)
400174b1
TT
12615 bfd_map_over_sections (dwp_file->dbfd.get (),
12616 dwarf2_locate_v2_dwp_sections,
12617 dwp_file.get ());
73869dc2 12618
298e9637
SM
12619 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12620 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12621
b4f54984 12622 if (dwarf_read_debug)
80626a55
DE
12623 {
12624 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12625 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12626 " %s CUs, %s TUs\n",
12627 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12628 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12629 }
12630
12631 return dwp_file;
3019eac3 12632}
c906108c 12633
ab5088bf
DE
12634/* Wrapper around open_and_init_dwp_file, only open it once. */
12635
12636static struct dwp_file *
ed2dc618 12637get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12638{
5989a64e 12639 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12640 {
5989a64e 12641 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12642 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12643 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12644 }
5989a64e 12645 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12646}
12647
80626a55
DE
12648/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12649 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12650 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12651 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12652 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12653
12654 This is called, for example, when wanting to read a variable with a
12655 complex location. Therefore we don't want to do file i/o for every call.
12656 Therefore we don't want to look for a DWO file on every call.
12657 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12658 then we check if we've already seen DWO_NAME, and only THEN do we check
12659 for a DWO file.
12660
1c658ad5 12661 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12662 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12663
3019eac3 12664static struct dwo_unit *
80626a55
DE
12665lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12666 const char *dwo_name, const char *comp_dir,
12667 ULONGEST signature, int is_debug_types)
3019eac3 12668{
ed2dc618 12669 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12670 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12671 const char *kind = is_debug_types ? "TU" : "CU";
12672 void **dwo_file_slot;
3019eac3 12673 struct dwo_file *dwo_file;
80626a55 12674 struct dwp_file *dwp_file;
cb1df416 12675
6a506a2d
DE
12676 /* First see if there's a DWP file.
12677 If we have a DWP file but didn't find the DWO inside it, don't
12678 look for the original DWO file. It makes gdb behave differently
12679 depending on whether one is debugging in the build tree. */
cf2c3c16 12680
ed2dc618 12681 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12682 if (dwp_file != NULL)
cf2c3c16 12683 {
80626a55
DE
12684 const struct dwp_hash_table *dwp_htab =
12685 is_debug_types ? dwp_file->tus : dwp_file->cus;
12686
12687 if (dwp_htab != NULL)
12688 {
12689 struct dwo_unit *dwo_cutu =
ed2dc618 12690 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12691 signature, is_debug_types);
80626a55
DE
12692
12693 if (dwo_cutu != NULL)
12694 {
b4f54984 12695 if (dwarf_read_debug)
80626a55
DE
12696 {
12697 fprintf_unfiltered (gdb_stdlog,
12698 "Virtual DWO %s %s found: @%s\n",
12699 kind, hex_string (signature),
12700 host_address_to_string (dwo_cutu));
12701 }
12702 return dwo_cutu;
12703 }
12704 }
12705 }
6a506a2d 12706 else
80626a55 12707 {
6a506a2d 12708 /* No DWP file, look for the DWO file. */
80626a55 12709
ed2dc618
SM
12710 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12711 dwo_name, comp_dir);
6a506a2d 12712 if (*dwo_file_slot == NULL)
80626a55 12713 {
6a506a2d
DE
12714 /* Read in the file and build a table of the CUs/TUs it contains. */
12715 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12716 }
6a506a2d 12717 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12718 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12719
6a506a2d 12720 if (dwo_file != NULL)
19c3d4c9 12721 {
6a506a2d
DE
12722 struct dwo_unit *dwo_cutu = NULL;
12723
12724 if (is_debug_types && dwo_file->tus)
12725 {
12726 struct dwo_unit find_dwo_cutu;
12727
12728 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12729 find_dwo_cutu.signature = signature;
9a3c8263 12730 dwo_cutu
b0b6a987
TT
12731 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12732 &find_dwo_cutu);
6a506a2d 12733 }
33c5cd75 12734 else if (!is_debug_types && dwo_file->cus)
80626a55 12735 {
33c5cd75
DB
12736 struct dwo_unit find_dwo_cutu;
12737
12738 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12739 find_dwo_cutu.signature = signature;
b0b6a987 12740 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12741 &find_dwo_cutu);
6a506a2d
DE
12742 }
12743
12744 if (dwo_cutu != NULL)
12745 {
b4f54984 12746 if (dwarf_read_debug)
6a506a2d
DE
12747 {
12748 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12749 kind, dwo_name, hex_string (signature),
12750 host_address_to_string (dwo_cutu));
12751 }
12752 return dwo_cutu;
80626a55
DE
12753 }
12754 }
2e276125 12755 }
9cdd5dbd 12756
80626a55
DE
12757 /* We didn't find it. This could mean a dwo_id mismatch, or
12758 someone deleted the DWO/DWP file, or the search path isn't set up
12759 correctly to find the file. */
12760
b4f54984 12761 if (dwarf_read_debug)
80626a55
DE
12762 {
12763 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12764 kind, dwo_name, hex_string (signature));
12765 }
3019eac3 12766
6656a72d
DE
12767 /* This is a warning and not a complaint because it can be caused by
12768 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12769 {
12770 /* Print the name of the DWP file if we looked there, helps the user
12771 better diagnose the problem. */
791afaa2 12772 std::string dwp_text;
43942612
DE
12773
12774 if (dwp_file != NULL)
791afaa2
TT
12775 dwp_text = string_printf (" [in DWP file %s]",
12776 lbasename (dwp_file->name));
43942612 12777
9d8780f0 12778 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12779 " [in module %s]"),
12780 kind, dwo_name, hex_string (signature),
791afaa2 12781 dwp_text.c_str (),
43942612 12782 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12783 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12784 }
3019eac3 12785 return NULL;
5fb290d7
DJ
12786}
12787
80626a55
DE
12788/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12789 See lookup_dwo_cutu_unit for details. */
12790
12791static struct dwo_unit *
12792lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12793 const char *dwo_name, const char *comp_dir,
12794 ULONGEST signature)
12795{
12796 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12797}
12798
12799/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12800 See lookup_dwo_cutu_unit for details. */
12801
12802static struct dwo_unit *
12803lookup_dwo_type_unit (struct signatured_type *this_tu,
12804 const char *dwo_name, const char *comp_dir)
12805{
12806 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12807}
12808
89e63ee4
DE
12809/* Traversal function for queue_and_load_all_dwo_tus. */
12810
12811static int
12812queue_and_load_dwo_tu (void **slot, void *info)
12813{
12814 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12815 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12816 ULONGEST signature = dwo_unit->signature;
12817 struct signatured_type *sig_type =
12818 lookup_dwo_signatured_type (per_cu->cu, signature);
12819
12820 if (sig_type != NULL)
12821 {
12822 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12823
12824 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12825 a real dependency of PER_CU on SIG_TYPE. That is detected later
12826 while processing PER_CU. */
12827 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12828 load_full_type_unit (sig_cu);
ae640021 12829 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12830 }
12831
12832 return 1;
12833}
12834
12835/* Queue all TUs contained in the DWO of PER_CU to be read in.
12836 The DWO may have the only definition of the type, though it may not be
12837 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12838 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12839
12840static void
12841queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12842{
12843 struct dwo_unit *dwo_unit;
12844 struct dwo_file *dwo_file;
12845
12846 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12847 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12848 gdb_assert (per_cu->cu != NULL);
12849
12850 dwo_unit = per_cu->cu->dwo_unit;
12851 gdb_assert (dwo_unit != NULL);
12852
12853 dwo_file = dwo_unit->dwo_file;
12854 if (dwo_file->tus != NULL)
b0b6a987
TT
12855 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12856 per_cu);
89e63ee4
DE
12857}
12858
3019eac3 12859/* Read in various DIEs. */
348e048f 12860
d389af10 12861/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12862 Inherit only the children of the DW_AT_abstract_origin DIE not being
12863 already referenced by DW_AT_abstract_origin from the children of the
12864 current DIE. */
d389af10
JK
12865
12866static void
12867inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12868{
12869 struct die_info *child_die;
791afaa2 12870 sect_offset *offsetp;
d389af10
JK
12871 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12872 struct die_info *origin_die;
12873 /* Iterator of the ORIGIN_DIE children. */
12874 struct die_info *origin_child_die;
d389af10 12875 struct attribute *attr;
cd02d79d
PA
12876 struct dwarf2_cu *origin_cu;
12877 struct pending **origin_previous_list_in_scope;
d389af10
JK
12878
12879 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12880 if (!attr)
12881 return;
12882
cd02d79d
PA
12883 /* Note that following die references may follow to a die in a
12884 different cu. */
12885
12886 origin_cu = cu;
12887 origin_die = follow_die_ref (die, attr, &origin_cu);
12888
12889 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12890 symbols in. */
12891 origin_previous_list_in_scope = origin_cu->list_in_scope;
12892 origin_cu->list_in_scope = cu->list_in_scope;
12893
edb3359d
DJ
12894 if (die->tag != origin_die->tag
12895 && !(die->tag == DW_TAG_inlined_subroutine
12896 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12897 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12898 sect_offset_str (die->sect_off),
12899 sect_offset_str (origin_die->sect_off));
d389af10 12900
791afaa2 12901 std::vector<sect_offset> offsets;
d389af10 12902
3ea89b92
PMR
12903 for (child_die = die->child;
12904 child_die && child_die->tag;
436c571c 12905 child_die = child_die->sibling)
3ea89b92
PMR
12906 {
12907 struct die_info *child_origin_die;
12908 struct dwarf2_cu *child_origin_cu;
12909
12910 /* We are trying to process concrete instance entries:
216f72a1 12911 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12912 it's not relevant to our analysis here. i.e. detecting DIEs that are
12913 present in the abstract instance but not referenced in the concrete
12914 one. */
216f72a1
JK
12915 if (child_die->tag == DW_TAG_call_site
12916 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12917 continue;
12918
c38f313d
DJ
12919 /* For each CHILD_DIE, find the corresponding child of
12920 ORIGIN_DIE. If there is more than one layer of
12921 DW_AT_abstract_origin, follow them all; there shouldn't be,
12922 but GCC versions at least through 4.4 generate this (GCC PR
12923 40573). */
3ea89b92
PMR
12924 child_origin_die = child_die;
12925 child_origin_cu = cu;
c38f313d
DJ
12926 while (1)
12927 {
cd02d79d
PA
12928 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12929 child_origin_cu);
c38f313d
DJ
12930 if (attr == NULL)
12931 break;
cd02d79d
PA
12932 child_origin_die = follow_die_ref (child_origin_die, attr,
12933 &child_origin_cu);
c38f313d
DJ
12934 }
12935
d389af10
JK
12936 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12937 counterpart may exist. */
c38f313d 12938 if (child_origin_die != child_die)
d389af10 12939 {
edb3359d
DJ
12940 if (child_die->tag != child_origin_die->tag
12941 && !(child_die->tag == DW_TAG_inlined_subroutine
12942 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12943 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12944 "different tags"),
9d8780f0
SM
12945 sect_offset_str (child_die->sect_off),
12946 sect_offset_str (child_origin_die->sect_off));
c38f313d 12947 if (child_origin_die->parent != origin_die)
b98664d3 12948 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12949 "different parents"),
9d8780f0
SM
12950 sect_offset_str (child_die->sect_off),
12951 sect_offset_str (child_origin_die->sect_off));
c38f313d 12952 else
791afaa2 12953 offsets.push_back (child_origin_die->sect_off);
d389af10 12954 }
d389af10 12955 }
791afaa2
TT
12956 std::sort (offsets.begin (), offsets.end ());
12957 sect_offset *offsets_end = offsets.data () + offsets.size ();
12958 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12959 if (offsetp[-1] == *offsetp)
b98664d3 12960 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12961 "to DIE %s as their abstract origin"),
12962 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12963
791afaa2 12964 offsetp = offsets.data ();
d389af10
JK
12965 origin_child_die = origin_die->child;
12966 while (origin_child_die && origin_child_die->tag)
12967 {
12968 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12969 while (offsetp < offsets_end
9c541725 12970 && *offsetp < origin_child_die->sect_off)
d389af10 12971 offsetp++;
b64f50a1 12972 if (offsetp >= offsets_end
9c541725 12973 || *offsetp > origin_child_die->sect_off)
d389af10 12974 {
adde2bff
DE
12975 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12976 Check whether we're already processing ORIGIN_CHILD_DIE.
12977 This can happen with mutually referenced abstract_origins.
12978 PR 16581. */
12979 if (!origin_child_die->in_process)
12980 process_die (origin_child_die, origin_cu);
d389af10 12981 }
436c571c 12982 origin_child_die = origin_child_die->sibling;
d389af10 12983 }
cd02d79d 12984 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12985
12986 if (cu != origin_cu)
12987 compute_delayed_physnames (origin_cu);
d389af10
JK
12988}
12989
c906108c 12990static void
e7c27a73 12991read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12992{
5e22e966 12993 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 12994 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12995 struct context_stack *newobj;
c906108c
SS
12996 CORE_ADDR lowpc;
12997 CORE_ADDR highpc;
12998 struct die_info *child_die;
edb3359d 12999 struct attribute *attr, *call_line, *call_file;
15d034d0 13000 const char *name;
e142c38c 13001 CORE_ADDR baseaddr;
801e3a5b 13002 struct block *block;
edb3359d 13003 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13004 std::vector<struct symbol *> template_args;
34eaf542 13005 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13006
13007 if (inlined_func)
13008 {
13009 /* If we do not have call site information, we can't show the
13010 caller of this inlined function. That's too confusing, so
13011 only use the scope for local variables. */
13012 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13013 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13014 if (call_line == NULL || call_file == NULL)
13015 {
13016 read_lexical_block_scope (die, cu);
13017 return;
13018 }
13019 }
c906108c 13020
b3b3bada 13021 baseaddr = objfile->text_section_offset ();
e142c38c 13022
94af9270 13023 name = dwarf2_name (die, cu);
c906108c 13024
e8d05480
JB
13025 /* Ignore functions with missing or empty names. These are actually
13026 illegal according to the DWARF standard. */
13027 if (name == NULL)
13028 {
b98664d3 13029 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13030 sect_offset_str (die->sect_off));
e8d05480
JB
13031 return;
13032 }
13033
13034 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13035 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13036 <= PC_BOUNDS_INVALID)
e8d05480 13037 {
ae4d0c03
PM
13038 attr = dwarf2_attr (die, DW_AT_external, cu);
13039 if (!attr || !DW_UNSND (attr))
b98664d3 13040 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13041 "for subprogram DIE at %s"),
13042 sect_offset_str (die->sect_off));
e8d05480
JB
13043 return;
13044 }
c906108c 13045
3e29f34a
MR
13046 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13047 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13048
34eaf542
TT
13049 /* If we have any template arguments, then we must allocate a
13050 different sort of symbol. */
436c571c 13051 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13052 {
13053 if (child_die->tag == DW_TAG_template_type_param
13054 || child_die->tag == DW_TAG_template_value_param)
13055 {
8c14c3a3 13056 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13057 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13058 break;
13059 }
13060 }
13061
c24bdb02 13062 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13063 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13064 (struct symbol *) templ_func);
4c2df51b 13065
81873cc8 13066 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13067 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13068 cu->language);
13069
4cecd739
DJ
13070 /* If there is a location expression for DW_AT_frame_base, record
13071 it. */
e142c38c 13072 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13073 if (attr != nullptr)
fe978cb0 13074 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13075
63e43d3a
PMR
13076 /* If there is a location for the static link, record it. */
13077 newobj->static_link = NULL;
13078 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13079 if (attr != nullptr)
63e43d3a 13080 {
224c3ddb
SM
13081 newobj->static_link
13082 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13083 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 13084 cu->per_cu->addr_type ());
63e43d3a
PMR
13085 }
13086
c24bdb02 13087 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13088
639d11d3 13089 if (die->child != NULL)
c906108c 13090 {
639d11d3 13091 child_die = die->child;
c906108c
SS
13092 while (child_die && child_die->tag)
13093 {
34eaf542
TT
13094 if (child_die->tag == DW_TAG_template_type_param
13095 || child_die->tag == DW_TAG_template_value_param)
13096 {
13097 struct symbol *arg = new_symbol (child_die, NULL, cu);
13098
f1078f66 13099 if (arg != NULL)
2f4732b0 13100 template_args.push_back (arg);
34eaf542
TT
13101 }
13102 else
13103 process_die (child_die, cu);
436c571c 13104 child_die = child_die->sibling;
c906108c
SS
13105 }
13106 }
13107
d389af10
JK
13108 inherit_abstract_dies (die, cu);
13109
4a811a97
UW
13110 /* If we have a DW_AT_specification, we might need to import using
13111 directives from the context of the specification DIE. See the
13112 comment in determine_prefix. */
13113 if (cu->language == language_cplus
13114 && dwarf2_attr (die, DW_AT_specification, cu))
13115 {
13116 struct dwarf2_cu *spec_cu = cu;
13117 struct die_info *spec_die = die_specification (die, &spec_cu);
13118
13119 while (spec_die)
13120 {
13121 child_die = spec_die->child;
13122 while (child_die && child_die->tag)
13123 {
13124 if (child_die->tag == DW_TAG_imported_module)
13125 process_die (child_die, spec_cu);
436c571c 13126 child_die = child_die->sibling;
4a811a97
UW
13127 }
13128
13129 /* In some cases, GCC generates specification DIEs that
13130 themselves contain DW_AT_specification attributes. */
13131 spec_die = die_specification (spec_die, &spec_cu);
13132 }
13133 }
13134
c24bdb02 13135 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13136 /* Make a block for the local symbols within. */
c24bdb02 13137 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13138 cstk.static_link, lowpc, highpc);
801e3a5b 13139
df8a16a1 13140 /* For C++, set the block's scope. */
45280282
IB
13141 if ((cu->language == language_cplus
13142 || cu->language == language_fortran
c44af4eb
TT
13143 || cu->language == language_d
13144 || cu->language == language_rust)
4d4ec4e5 13145 && cu->processing_has_namespace_info)
195a3f6c
TT
13146 block_set_scope (block, determine_prefix (die, cu),
13147 &objfile->objfile_obstack);
df8a16a1 13148
801e3a5b
JB
13149 /* If we have address ranges, record them. */
13150 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13151
a60f3166 13152 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13153
34eaf542 13154 /* Attach template arguments to function. */
2f4732b0 13155 if (!template_args.empty ())
34eaf542
TT
13156 {
13157 gdb_assert (templ_func != NULL);
13158
2f4732b0 13159 templ_func->n_template_arguments = template_args.size ();
34eaf542 13160 templ_func->template_arguments
8d749320
SM
13161 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13162 templ_func->n_template_arguments);
34eaf542 13163 memcpy (templ_func->template_arguments,
2f4732b0 13164 template_args.data (),
34eaf542 13165 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13166
13167 /* Make sure that the symtab is set on the new symbols. Even
13168 though they don't appear in this symtab directly, other parts
13169 of gdb assume that symbols do, and this is reasonably
13170 true. */
8634679f 13171 for (symbol *sym : template_args)
3e1d3d8c 13172 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13173 }
13174
208d8187
JB
13175 /* In C++, we can have functions nested inside functions (e.g., when
13176 a function declares a class that has methods). This means that
13177 when we finish processing a function scope, we may need to go
13178 back to building a containing block's symbol lists. */
c24bdb02
KS
13179 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13180 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13181
921e78cf
JB
13182 /* If we've finished processing a top-level function, subsequent
13183 symbols go in the file symbol list. */
c24bdb02
KS
13184 if (cu->get_builder ()->outermost_context_p ())
13185 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13186}
13187
13188/* Process all the DIES contained within a lexical block scope. Start
13189 a new scope, process the dies, and then close the scope. */
13190
13191static void
e7c27a73 13192read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13193{
5e22e966 13194 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13195 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13196 CORE_ADDR lowpc, highpc;
13197 struct die_info *child_die;
e142c38c
DJ
13198 CORE_ADDR baseaddr;
13199
b3b3bada 13200 baseaddr = objfile->text_section_offset ();
c906108c
SS
13201
13202 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13203 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13204 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13205 be nasty. Might be easier to properly extend generic blocks to
af34e669 13206 describe ranges. */
e385593e
JK
13207 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13208 {
13209 case PC_BOUNDS_NOT_PRESENT:
13210 /* DW_TAG_lexical_block has no attributes, process its children as if
13211 there was no wrapping by that DW_TAG_lexical_block.
13212 GCC does no longer produces such DWARF since GCC r224161. */
13213 for (child_die = die->child;
13214 child_die != NULL && child_die->tag;
436c571c 13215 child_die = child_die->sibling)
4f7bc5ed
TT
13216 {
13217 /* We might already be processing this DIE. This can happen
13218 in an unusual circumstance -- where a subroutine A
13219 appears lexically in another subroutine B, but A actually
13220 inlines B. The recursion is broken here, rather than in
13221 inherit_abstract_dies, because it seems better to simply
13222 drop concrete children here. */
13223 if (!child_die->in_process)
13224 process_die (child_die, cu);
13225 }
e385593e
JK
13226 return;
13227 case PC_BOUNDS_INVALID:
13228 return;
13229 }
3e29f34a
MR
13230 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13231 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13232
c24bdb02 13233 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13234 if (die->child != NULL)
c906108c 13235 {
639d11d3 13236 child_die = die->child;
c906108c
SS
13237 while (child_die && child_die->tag)
13238 {
e7c27a73 13239 process_die (child_die, cu);
436c571c 13240 child_die = child_die->sibling;
c906108c
SS
13241 }
13242 }
3ea89b92 13243 inherit_abstract_dies (die, cu);
c24bdb02 13244 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13245
c24bdb02
KS
13246 if (*cu->get_builder ()->get_local_symbols () != NULL
13247 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13248 {
801e3a5b 13249 struct block *block
c24bdb02 13250 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13251 cstk.start_addr, highpc);
801e3a5b
JB
13252
13253 /* Note that recording ranges after traversing children, as we
13254 do here, means that recording a parent's ranges entails
13255 walking across all its children's ranges as they appear in
13256 the address map, which is quadratic behavior.
13257
13258 It would be nicer to record the parent's ranges before
13259 traversing its children, simply overriding whatever you find
13260 there. But since we don't even decide whether to create a
13261 block until after we've traversed its children, that's hard
13262 to do. */
13263 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13264 }
c24bdb02
KS
13265 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13266 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13267}
13268
216f72a1 13269/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13270
13271static void
13272read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13273{
5e22e966 13274 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13275 struct objfile *objfile = per_objfile->objfile;
08feed99 13276 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13277 CORE_ADDR pc, baseaddr;
13278 struct attribute *attr;
13279 struct call_site *call_site, call_site_local;
13280 void **slot;
13281 int nparams;
13282 struct die_info *child_die;
13283
b3b3bada 13284 baseaddr = objfile->text_section_offset ();
96408a79 13285
216f72a1
JK
13286 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13287 if (attr == NULL)
13288 {
13289 /* This was a pre-DWARF-5 GNU extension alias
13290 for DW_AT_call_return_pc. */
13291 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13292 }
96408a79
SA
13293 if (!attr)
13294 {
b98664d3 13295 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13296 "DIE %s [in module %s]"),
13297 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13298 return;
13299 }
cd6c91b4 13300 pc = attr->value_as_address () + baseaddr;
3e29f34a 13301 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13302
13303 if (cu->call_site_htab == NULL)
13304 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13305 NULL, &objfile->objfile_obstack,
13306 hashtab_obstack_allocate, NULL);
13307 call_site_local.pc = pc;
13308 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13309 if (*slot != NULL)
13310 {
b98664d3 13311 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13312 "DIE %s [in module %s]"),
13313 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13314 objfile_name (objfile));
96408a79
SA
13315 return;
13316 }
13317
13318 /* Count parameters at the caller. */
13319
13320 nparams = 0;
13321 for (child_die = die->child; child_die && child_die->tag;
436c571c 13322 child_die = child_die->sibling)
96408a79 13323 {
216f72a1
JK
13324 if (child_die->tag != DW_TAG_call_site_parameter
13325 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13326 {
b98664d3 13327 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13328 "DW_TAG_call_site child DIE %s [in module %s]"),
13329 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13330 objfile_name (objfile));
96408a79
SA
13331 continue;
13332 }
13333
13334 nparams++;
13335 }
13336
224c3ddb
SM
13337 call_site
13338 = ((struct call_site *)
13339 obstack_alloc (&objfile->objfile_obstack,
13340 sizeof (*call_site)
13341 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13342 *slot = call_site;
13343 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13344 call_site->pc = pc;
13345
216f72a1
JK
13346 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13347 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13348 {
13349 struct die_info *func_die;
13350
13351 /* Skip also over DW_TAG_inlined_subroutine. */
13352 for (func_die = die->parent;
13353 func_die && func_die->tag != DW_TAG_subprogram
13354 && func_die->tag != DW_TAG_subroutine_type;
13355 func_die = func_die->parent);
13356
216f72a1
JK
13357 /* DW_AT_call_all_calls is a superset
13358 of DW_AT_call_all_tail_calls. */
96408a79 13359 if (func_die
216f72a1 13360 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13361 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13362 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13363 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13364 {
13365 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13366 not complete. But keep CALL_SITE for look ups via call_site_htab,
13367 both the initial caller containing the real return address PC and
13368 the final callee containing the current PC of a chain of tail
13369 calls do not need to have the tail call list complete. But any
13370 function candidate for a virtual tail call frame searched via
13371 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13372 determined unambiguously. */
13373 }
13374 else
13375 {
13376 struct type *func_type = NULL;
13377
13378 if (func_die)
13379 func_type = get_die_type (func_die, cu);
13380 if (func_type != NULL)
13381 {
78134374 13382 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13383
13384 /* Enlist this call site to the function. */
13385 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13386 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13387 }
13388 else
b98664d3 13389 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13390 "DIE %s [in module %s]"),
13391 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13392 }
13393 }
13394
216f72a1
JK
13395 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13396 if (attr == NULL)
13397 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13398 if (attr == NULL)
13399 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13400 if (attr == NULL)
216f72a1
JK
13401 {
13402 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13403 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13404 }
96408a79 13405 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13406 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13407 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13408 else if (attr->form_is_block ())
96408a79
SA
13409 {
13410 struct dwarf2_locexpr_baton *dlbaton;
13411
8d749320 13412 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13413 dlbaton->data = DW_BLOCK (attr)->data;
13414 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13415 dlbaton->per_objfile = per_objfile;
96408a79
SA
13416 dlbaton->per_cu = cu->per_cu;
13417
13418 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13419 }
cd6c91b4 13420 else if (attr->form_is_ref ())
96408a79 13421 {
96408a79
SA
13422 struct dwarf2_cu *target_cu = cu;
13423 struct die_info *target_die;
13424
ac9ec31b 13425 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13426 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13427 if (die_is_declaration (target_die, target_cu))
13428 {
7d45c7c3 13429 const char *target_physname;
9112db09
JK
13430
13431 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13432 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13433 if (target_physname == NULL)
9112db09 13434 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13435 if (target_physname == NULL)
b98664d3 13436 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13437 "physname, for referencing DIE %s [in module %s]"),
13438 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13439 else
7d455152 13440 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13441 }
13442 else
13443 {
13444 CORE_ADDR lowpc;
13445
13446 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13447 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13448 <= PC_BOUNDS_INVALID)
b98664d3 13449 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13450 "low pc, for referencing DIE %s [in module %s]"),
13451 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13452 else
3e29f34a
MR
13453 {
13454 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13455 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13456 }
96408a79
SA
13457 }
13458 }
13459 else
b98664d3 13460 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13461 "block nor reference, for DIE %s [in module %s]"),
13462 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13463
13464 call_site->per_cu = cu->per_cu;
13465
13466 for (child_die = die->child;
13467 child_die && child_die->tag;
436c571c 13468 child_die = child_die->sibling)
96408a79 13469 {
96408a79 13470 struct call_site_parameter *parameter;
1788b2d3 13471 struct attribute *loc, *origin;
96408a79 13472
216f72a1
JK
13473 if (child_die->tag != DW_TAG_call_site_parameter
13474 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13475 {
13476 /* Already printed the complaint above. */
13477 continue;
13478 }
13479
13480 gdb_assert (call_site->parameter_count < nparams);
13481 parameter = &call_site->parameter[call_site->parameter_count];
13482
1788b2d3
JK
13483 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13484 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13485 register is contained in DW_AT_call_value. */
96408a79 13486
24c5c679 13487 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13488 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13489 if (origin == NULL)
13490 {
13491 /* This was a pre-DWARF-5 GNU extension alias
13492 for DW_AT_call_parameter. */
13493 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13494 }
cd6c91b4 13495 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13496 {
1788b2d3 13497 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13498
0826b30a 13499 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13500 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13501 {
13502 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13503 binding can be done only inside one CU. Such referenced DIE
13504 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13505 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13506 "DW_TAG_call_site child DIE %s [in module %s]"),
13507 sect_offset_str (child_die->sect_off),
9c541725 13508 objfile_name (objfile));
d76b7dbc
JK
13509 continue;
13510 }
9c541725
PA
13511 parameter->u.param_cu_off
13512 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13513 }
4fc6c0d5 13514 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13515 {
b98664d3 13516 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13517 "DW_TAG_call_site child DIE %s [in module %s]"),
13518 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13519 continue;
13520 }
24c5c679 13521 else
96408a79 13522 {
24c5c679
JK
13523 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13524 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13525 if (parameter->u.dwarf_reg != -1)
13526 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13527 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13528 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13529 &parameter->u.fb_offset))
13530 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13531 else
13532 {
b98664d3 13533 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13534 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13535 "DW_TAG_call_site child DIE %s "
24c5c679 13536 "[in module %s]"),
9d8780f0 13537 sect_offset_str (child_die->sect_off),
9c541725 13538 objfile_name (objfile));
24c5c679
JK
13539 continue;
13540 }
96408a79
SA
13541 }
13542
216f72a1
JK
13543 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13544 if (attr == NULL)
13545 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13546 if (attr == NULL || !attr->form_is_block ())
96408a79 13547 {
b98664d3 13548 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13549 "DW_TAG_call_site child DIE %s [in module %s]"),
13550 sect_offset_str (child_die->sect_off),
9c541725 13551 objfile_name (objfile));
96408a79
SA
13552 continue;
13553 }
13554 parameter->value = DW_BLOCK (attr)->data;
13555 parameter->value_size = DW_BLOCK (attr)->size;
13556
13557 /* Parameters are not pre-cleared by memset above. */
13558 parameter->data_value = NULL;
13559 parameter->data_value_size = 0;
13560 call_site->parameter_count++;
13561
216f72a1
JK
13562 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13563 if (attr == NULL)
13564 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13565 if (attr != nullptr)
96408a79 13566 {
4fc6c0d5 13567 if (!attr->form_is_block ())
b98664d3 13568 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13569 "DW_TAG_call_site child DIE %s [in module %s]"),
13570 sect_offset_str (child_die->sect_off),
9c541725 13571 objfile_name (objfile));
96408a79
SA
13572 else
13573 {
13574 parameter->data_value = DW_BLOCK (attr)->data;
13575 parameter->data_value_size = DW_BLOCK (attr)->size;
13576 }
13577 }
13578 }
13579}
13580
71a3c369
TT
13581/* Helper function for read_variable. If DIE represents a virtual
13582 table, then return the type of the concrete object that is
13583 associated with the virtual table. Otherwise, return NULL. */
13584
13585static struct type *
13586rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13587{
13588 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13589 if (attr == NULL)
13590 return NULL;
13591
13592 /* Find the type DIE. */
13593 struct die_info *type_die = NULL;
13594 struct dwarf2_cu *type_cu = cu;
13595
cd6c91b4 13596 if (attr->form_is_ref ())
71a3c369
TT
13597 type_die = follow_die_ref (die, attr, &type_cu);
13598 if (type_die == NULL)
13599 return NULL;
13600
13601 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13602 return NULL;
13603 return die_containing_type (type_die, type_cu);
13604}
13605
13606/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13607
13608static void
13609read_variable (struct die_info *die, struct dwarf2_cu *cu)
13610{
13611 struct rust_vtable_symbol *storage = NULL;
13612
13613 if (cu->language == language_rust)
13614 {
13615 struct type *containing_type = rust_containing_type (die, cu);
13616
13617 if (containing_type != NULL)
13618 {
5e22e966 13619 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13620
8c14c3a3 13621 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13622 storage->concrete_type = containing_type;
cf724bc9 13623 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13624 }
13625 }
13626
e4a62c65
TV
13627 struct symbol *res = new_symbol (die, NULL, cu, storage);
13628 struct attribute *abstract_origin
13629 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13630 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13631 if (res == NULL && loc && abstract_origin)
13632 {
13633 /* We have a variable without a name, but with a location and an abstract
13634 origin. This may be a concrete instance of an abstract variable
13635 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13636 later. */
13637 struct dwarf2_cu *origin_cu = cu;
13638 struct die_info *origin_die
13639 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13640 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13641 per_objfile->per_bfd->abstract_to_concrete
13642 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13643 }
71a3c369
TT
13644}
13645
43988095
JK
13646/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13647 reading .debug_rnglists.
13648 Callback's type should be:
13649 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13650 Return true if the attributes are present and valid, otherwise,
13651 return false. */
13652
13653template <typename Callback>
13654static bool
13655dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13656 Callback &&callback)
13657{
5e22e966 13658 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13659 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13660 bfd *obfd = objfile->obfd;
43988095 13661 /* Base address selection entry. */
2b24b6e4 13662 gdb::optional<CORE_ADDR> base;
43988095 13663 const gdb_byte *buffer;
43988095
JK
13664 CORE_ADDR baseaddr;
13665 bool overflow = false;
13666
43988095
JK
13667 base = cu->base_address;
13668
5989a64e
SM
13669 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13670 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13671 {
b98664d3 13672 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13673 offset);
13674 return false;
13675 }
5989a64e 13676 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13677
b3b3bada 13678 baseaddr = objfile->text_section_offset ();
43988095
JK
13679
13680 while (1)
13681 {
7814882a
JK
13682 /* Initialize it due to a false compiler warning. */
13683 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13684 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13685 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13686 unsigned int bytes_read;
13687
13688 if (buffer == buf_end)
13689 {
13690 overflow = true;
13691 break;
13692 }
13693 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13694 switch (rlet)
13695 {
13696 case DW_RLE_end_of_list:
13697 break;
13698 case DW_RLE_base_address:
13699 if (buffer + cu->header.addr_size > buf_end)
13700 {
13701 overflow = true;
13702 break;
13703 }
c8a7a66f 13704 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13705 buffer += bytes_read;
13706 break;
13707 case DW_RLE_start_length:
13708 if (buffer + cu->header.addr_size > buf_end)
13709 {
13710 overflow = true;
13711 break;
13712 }
c8a7a66f
TT
13713 range_beginning = cu->header.read_address (obfd, buffer,
13714 &bytes_read);
43988095
JK
13715 buffer += bytes_read;
13716 range_end = (range_beginning
13717 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13718 buffer += bytes_read;
13719 if (buffer > buf_end)
13720 {
13721 overflow = true;
13722 break;
13723 }
13724 break;
13725 case DW_RLE_offset_pair:
13726 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13727 buffer += bytes_read;
13728 if (buffer > buf_end)
13729 {
13730 overflow = true;
13731 break;
13732 }
13733 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13734 buffer += bytes_read;
13735 if (buffer > buf_end)
13736 {
13737 overflow = true;
13738 break;
13739 }
13740 break;
13741 case DW_RLE_start_end:
13742 if (buffer + 2 * cu->header.addr_size > buf_end)
13743 {
13744 overflow = true;
13745 break;
13746 }
c8a7a66f
TT
13747 range_beginning = cu->header.read_address (obfd, buffer,
13748 &bytes_read);
43988095 13749 buffer += bytes_read;
c8a7a66f 13750 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13751 buffer += bytes_read;
13752 break;
13753 default:
b98664d3 13754 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13755 return false;
13756 }
13757 if (rlet == DW_RLE_end_of_list || overflow)
13758 break;
13759 if (rlet == DW_RLE_base_address)
13760 continue;
13761
2b24b6e4 13762 if (!base.has_value ())
43988095
JK
13763 {
13764 /* We have no valid base address for the ranges
13765 data. */
b98664d3 13766 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13767 return false;
13768 }
13769
13770 if (range_beginning > range_end)
13771 {
13772 /* Inverted range entries are invalid. */
b98664d3 13773 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13774 return false;
13775 }
13776
13777 /* Empty range entries have no effect. */
13778 if (range_beginning == range_end)
13779 continue;
13780
2b24b6e4
TT
13781 range_beginning += *base;
13782 range_end += *base;
43988095
JK
13783
13784 /* A not-uncommon case of bad debug info.
13785 Don't pollute the addrmap with bad data. */
13786 if (range_beginning + baseaddr == 0
5989a64e 13787 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13788 {
b98664d3 13789 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13790 " [in module %s]"), objfile_name (objfile));
13791 continue;
13792 }
13793
13794 callback (range_beginning, range_end);
13795 }
13796
13797 if (overflow)
13798 {
b98664d3 13799 complaint (_("Offset %d is not terminated "
43988095
JK
13800 "for DW_AT_ranges attribute"),
13801 offset);
13802 return false;
13803 }
13804
13805 return true;
13806}
13807
13808/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13809 Callback's type should be:
13810 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13811 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13812
43988095 13813template <typename Callback>
43039443 13814static int
5f46c5a5 13815dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13816 Callback &&callback)
43039443 13817{
5e22e966
SM
13818 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13819 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13820 struct comp_unit_head *cu_header = &cu->header;
13821 bfd *obfd = objfile->obfd;
13822 unsigned int addr_size = cu_header->addr_size;
13823 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13824 /* Base address selection entry. */
2b24b6e4 13825 gdb::optional<CORE_ADDR> base;
43039443 13826 unsigned int dummy;
d521ce57 13827 const gdb_byte *buffer;
ff013f42 13828 CORE_ADDR baseaddr;
43039443 13829
43988095
JK
13830 if (cu_header->version >= 5)
13831 return dwarf2_rnglists_process (offset, cu, callback);
13832
d00adf39 13833 base = cu->base_address;
43039443 13834
5e22e966
SM
13835 per_objfile->per_bfd->ranges.read (objfile);
13836 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13837 {
b98664d3 13838 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13839 offset);
13840 return 0;
13841 }
5e22e966 13842 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13843
b3b3bada 13844 baseaddr = objfile->text_section_offset ();
ff013f42 13845
43039443
JK
13846 while (1)
13847 {
13848 CORE_ADDR range_beginning, range_end;
13849
c8a7a66f 13850 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13851 buffer += addr_size;
c8a7a66f 13852 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13853 buffer += addr_size;
13854 offset += 2 * addr_size;
13855
13856 /* An end of list marker is a pair of zero addresses. */
13857 if (range_beginning == 0 && range_end == 0)
13858 /* Found the end of list entry. */
13859 break;
13860
13861 /* Each base address selection entry is a pair of 2 values.
13862 The first is the largest possible address, the second is
13863 the base address. Check for a base address here. */
13864 if ((range_beginning & mask) == mask)
13865 {
28d2bfb9
AB
13866 /* If we found the largest possible address, then we already
13867 have the base address in range_end. */
13868 base = range_end;
43039443
JK
13869 continue;
13870 }
13871
2b24b6e4 13872 if (!base.has_value ())
43039443
JK
13873 {
13874 /* We have no valid base address for the ranges
13875 data. */
b98664d3 13876 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13877 return 0;
13878 }
13879
9277c30c
UW
13880 if (range_beginning > range_end)
13881 {
13882 /* Inverted range entries are invalid. */
b98664d3 13883 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13884 return 0;
13885 }
13886
13887 /* Empty range entries have no effect. */
13888 if (range_beginning == range_end)
13889 continue;
13890
2b24b6e4
TT
13891 range_beginning += *base;
13892 range_end += *base;
43039443 13893
01093045
DE
13894 /* A not-uncommon case of bad debug info.
13895 Don't pollute the addrmap with bad data. */
13896 if (range_beginning + baseaddr == 0
5e22e966 13897 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13898 {
b98664d3 13899 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13900 " [in module %s]"), objfile_name (objfile));
01093045
DE
13901 continue;
13902 }
13903
5f46c5a5
JK
13904 callback (range_beginning, range_end);
13905 }
13906
13907 return 1;
13908}
13909
13910/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13911 Return 1 if the attributes are present and valid, otherwise, return 0.
13912 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13913
13914static int
13915dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13916 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13917 dwarf2_psymtab *ranges_pst)
5f46c5a5 13918{
5e22e966 13919 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13920 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13921 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13922 int low_set = 0;
13923 CORE_ADDR low = 0;
13924 CORE_ADDR high = 0;
13925 int retval;
13926
13927 retval = dwarf2_ranges_process (offset, cu,
13928 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13929 {
9277c30c 13930 if (ranges_pst != NULL)
3e29f34a
MR
13931 {
13932 CORE_ADDR lowpc;
13933 CORE_ADDR highpc;
13934
79748972
TT
13935 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13936 range_beginning + baseaddr)
13937 - baseaddr);
13938 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13939 range_end + baseaddr)
13940 - baseaddr);
d320c2b5
TT
13941 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13942 lowpc, highpc - 1, ranges_pst);
3e29f34a 13943 }
ff013f42 13944
43039443
JK
13945 /* FIXME: This is recording everything as a low-high
13946 segment of consecutive addresses. We should have a
13947 data structure for discontiguous block ranges
13948 instead. */
13949 if (! low_set)
13950 {
13951 low = range_beginning;
13952 high = range_end;
13953 low_set = 1;
13954 }
13955 else
13956 {
13957 if (range_beginning < low)
13958 low = range_beginning;
13959 if (range_end > high)
13960 high = range_end;
13961 }
5f46c5a5
JK
13962 });
13963 if (!retval)
13964 return 0;
43039443
JK
13965
13966 if (! low_set)
13967 /* If the first entry is an end-of-list marker, the range
13968 describes an empty scope, i.e. no instructions. */
13969 return 0;
13970
13971 if (low_return)
13972 *low_return = low;
13973 if (high_return)
13974 *high_return = high;
13975 return 1;
13976}
13977
3a2b436a
JK
13978/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13979 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13980 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13981
3a2b436a 13982static enum pc_bounds_kind
af34e669 13983dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13984 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13985 dwarf2_psymtab *pst)
c906108c 13986{
5e22e966 13987 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 13988 struct attribute *attr;
91da1414 13989 struct attribute *attr_high;
af34e669
DJ
13990 CORE_ADDR low = 0;
13991 CORE_ADDR high = 0;
e385593e 13992 enum pc_bounds_kind ret;
c906108c 13993
91da1414
MW
13994 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13995 if (attr_high)
af34e669 13996 {
e142c38c 13997 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13998 if (attr != nullptr)
91da1414 13999 {
cd6c91b4
TT
14000 low = attr->value_as_address ();
14001 high = attr_high->value_as_address ();
14002 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14003 high += low;
91da1414 14004 }
af34e669
DJ
14005 else
14006 /* Found high w/o low attribute. */
e385593e 14007 return PC_BOUNDS_INVALID;
af34e669
DJ
14008
14009 /* Found consecutive range of addresses. */
3a2b436a 14010 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14011 }
c906108c 14012 else
af34e669 14013 {
e142c38c 14014 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14015 if (attr != NULL)
14016 {
18a8505e 14017 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14018 We take advantage of the fact that DW_AT_ranges does not appear
14019 in DW_TAG_compile_unit of DWO files. */
14020 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14021 unsigned int ranges_offset = (DW_UNSND (attr)
14022 + (need_ranges_base
14023 ? cu->ranges_base
14024 : 0));
2e3cf129 14025
af34e669 14026 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14027 .debug_ranges section. */
2e3cf129 14028 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14029 return PC_BOUNDS_INVALID;
43039443 14030 /* Found discontinuous range of addresses. */
3a2b436a 14031 ret = PC_BOUNDS_RANGES;
af34e669 14032 }
e385593e
JK
14033 else
14034 return PC_BOUNDS_NOT_PRESENT;
af34e669 14035 }
c906108c 14036
48fbe735 14037 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14038 if (high <= low)
e385593e 14039 return PC_BOUNDS_INVALID;
c906108c
SS
14040
14041 /* When using the GNU linker, .gnu.linkonce. sections are used to
14042 eliminate duplicate copies of functions and vtables and such.
14043 The linker will arbitrarily choose one and discard the others.
14044 The AT_*_pc values for such functions refer to local labels in
14045 these sections. If the section from that file was discarded, the
14046 labels are not in the output, so the relocs get a value of 0.
14047 If this is a discarded function, mark the pc bounds as invalid,
14048 so that GDB will ignore it. */
5989a64e 14049 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14050 return PC_BOUNDS_INVALID;
c906108c
SS
14051
14052 *lowpc = low;
96408a79
SA
14053 if (highpc)
14054 *highpc = high;
af34e669 14055 return ret;
c906108c
SS
14056}
14057
b084d499
JB
14058/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14059 its low and high PC addresses. Do nothing if these addresses could not
14060 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14061 and HIGHPC to the high address if greater than HIGHPC. */
14062
14063static void
14064dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14065 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14066 struct dwarf2_cu *cu)
14067{
14068 CORE_ADDR low, high;
14069 struct die_info *child = die->child;
14070
e385593e 14071 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14072 {
325fac50
PA
14073 *lowpc = std::min (*lowpc, low);
14074 *highpc = std::max (*highpc, high);
b084d499
JB
14075 }
14076
14077 /* If the language does not allow nested subprograms (either inside
14078 subprograms or lexical blocks), we're done. */
14079 if (cu->language != language_ada)
14080 return;
6e70227d 14081
b084d499
JB
14082 /* Check all the children of the given DIE. If it contains nested
14083 subprograms, then check their pc bounds. Likewise, we need to
14084 check lexical blocks as well, as they may also contain subprogram
14085 definitions. */
14086 while (child && child->tag)
14087 {
14088 if (child->tag == DW_TAG_subprogram
14089 || child->tag == DW_TAG_lexical_block)
14090 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14091 child = child->sibling;
b084d499
JB
14092 }
14093}
14094
fae299cd
DC
14095/* Get the low and high pc's represented by the scope DIE, and store
14096 them in *LOWPC and *HIGHPC. If the correct values can't be
14097 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14098
14099static void
14100get_scope_pc_bounds (struct die_info *die,
14101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14102 struct dwarf2_cu *cu)
14103{
14104 CORE_ADDR best_low = (CORE_ADDR) -1;
14105 CORE_ADDR best_high = (CORE_ADDR) 0;
14106 CORE_ADDR current_low, current_high;
14107
3a2b436a 14108 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14109 >= PC_BOUNDS_RANGES)
fae299cd
DC
14110 {
14111 best_low = current_low;
14112 best_high = current_high;
14113 }
14114 else
14115 {
14116 struct die_info *child = die->child;
14117
14118 while (child && child->tag)
14119 {
14120 switch (child->tag) {
14121 case DW_TAG_subprogram:
b084d499 14122 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14123 break;
14124 case DW_TAG_namespace:
f55ee35c 14125 case DW_TAG_module:
fae299cd
DC
14126 /* FIXME: carlton/2004-01-16: Should we do this for
14127 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14128 that current GCC's always emit the DIEs corresponding
14129 to definitions of methods of classes as children of a
14130 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14131 the DIEs giving the declarations, which could be
14132 anywhere). But I don't see any reason why the
14133 standards says that they have to be there. */
14134 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14135
14136 if (current_low != ((CORE_ADDR) -1))
14137 {
325fac50
PA
14138 best_low = std::min (best_low, current_low);
14139 best_high = std::max (best_high, current_high);
fae299cd
DC
14140 }
14141 break;
14142 default:
0963b4bd 14143 /* Ignore. */
fae299cd
DC
14144 break;
14145 }
14146
436c571c 14147 child = child->sibling;
fae299cd
DC
14148 }
14149 }
14150
14151 *lowpc = best_low;
14152 *highpc = best_high;
14153}
14154
801e3a5b
JB
14155/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14156 in DIE. */
380bca97 14157
801e3a5b
JB
14158static void
14159dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14160 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14161{
5e22e966 14162 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14163 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14164 struct attribute *attr;
91da1414 14165 struct attribute *attr_high;
801e3a5b 14166
91da1414
MW
14167 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14168 if (attr_high)
801e3a5b 14169 {
801e3a5b 14170 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14171 if (attr != nullptr)
801e3a5b 14172 {
cd6c91b4
TT
14173 CORE_ADDR low = attr->value_as_address ();
14174 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14175
cd6c91b4 14176 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14177 high += low;
9a619af0 14178
3e29f34a
MR
14179 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14180 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14181 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14182 }
14183 }
14184
14185 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14186 if (attr != nullptr)
801e3a5b 14187 {
18a8505e 14188 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14189 We take advantage of the fact that DW_AT_ranges does not appear
14190 in DW_TAG_compile_unit of DWO files. */
14191 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14192
14193 /* The value of the DW_AT_ranges attribute is the offset of the
14194 address range list in the .debug_ranges section. */
ab435259
DE
14195 unsigned long offset = (DW_UNSND (attr)
14196 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14197
2d5f09ec 14198 std::vector<blockrange> blockvec;
5f46c5a5
JK
14199 dwarf2_ranges_process (offset, cu,
14200 [&] (CORE_ADDR start, CORE_ADDR end)
14201 {
58fdfd2c
JK
14202 start += baseaddr;
14203 end += baseaddr;
5f46c5a5
JK
14204 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14205 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14206 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14207 blockvec.emplace_back (start, end);
5f46c5a5 14208 });
2d5f09ec
KB
14209
14210 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14211 }
14212}
14213
685b1105
JK
14214/* Check whether the producer field indicates either of GCC < 4.6, or the
14215 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14216
685b1105
JK
14217static void
14218check_producer (struct dwarf2_cu *cu)
60d5a603 14219{
38360086 14220 int major, minor;
60d5a603
JK
14221
14222 if (cu->producer == NULL)
14223 {
14224 /* For unknown compilers expect their behavior is DWARF version
14225 compliant.
14226
14227 GCC started to support .debug_types sections by -gdwarf-4 since
14228 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14229 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14230 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14231 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14232 }
b1ffba5a 14233 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14234 {
38360086
MW
14235 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14236 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14237 }
5230b05a 14238 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14239 {
14240 cu->producer_is_icc = true;
14241 cu->producer_is_icc_lt_14 = major < 14;
14242 }
c258c396
JD
14243 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14244 cu->producer_is_codewarrior = true;
685b1105
JK
14245 else
14246 {
14247 /* For other non-GCC compilers, expect their behavior is DWARF version
14248 compliant. */
60d5a603
JK
14249 }
14250
9068261f 14251 cu->checked_producer = true;
685b1105 14252}
ba919b58 14253
685b1105
JK
14254/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14255 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14256 during 4.6.0 experimental. */
14257
9068261f 14258static bool
685b1105
JK
14259producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14260{
14261 if (!cu->checked_producer)
14262 check_producer (cu);
14263
14264 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14265}
14266
c258c396
JD
14267
14268/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14269 with incorrect is_stmt attributes. */
14270
14271static bool
14272producer_is_codewarrior (struct dwarf2_cu *cu)
14273{
14274 if (!cu->checked_producer)
14275 check_producer (cu);
14276
14277 return cu->producer_is_codewarrior;
14278}
14279
405feb71 14280/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14281 DW_AT_accessibility. */
14282
14283static enum dwarf_access_attribute
14284dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14285{
14286 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14287 {
14288 /* The default DWARF 2 accessibility for members is public, the default
14289 accessibility for inheritance is private. */
14290
14291 if (die->tag != DW_TAG_inheritance)
14292 return DW_ACCESS_public;
14293 else
14294 return DW_ACCESS_private;
14295 }
14296 else
14297 {
14298 /* DWARF 3+ defines the default accessibility a different way. The same
14299 rules apply now for DW_TAG_inheritance as for the members and it only
14300 depends on the container kind. */
14301
14302 if (die->parent->tag == DW_TAG_class_type)
14303 return DW_ACCESS_private;
14304 else
14305 return DW_ACCESS_public;
14306 }
14307}
14308
74ac6d43
TT
14309/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14310 offset. If the attribute was not found return 0, otherwise return
14311 1. If it was found but could not properly be handled, set *OFFSET
14312 to 0. */
14313
14314static int
14315handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14316 LONGEST *offset)
14317{
14318 struct attribute *attr;
14319
14320 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14321 if (attr != NULL)
14322 {
14323 *offset = 0;
14324
14325 /* Note that we do not check for a section offset first here.
14326 This is because DW_AT_data_member_location is new in DWARF 4,
14327 so if we see it, we can assume that a constant form is really
14328 a constant and not a section offset. */
cd6c91b4 14329 if (attr->form_is_constant ())
0826b30a 14330 *offset = attr->constant_value (0);
cd6c91b4 14331 else if (attr->form_is_section_offset ())
74ac6d43 14332 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14333 else if (attr->form_is_block ())
74ac6d43
TT
14334 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14335 else
14336 dwarf2_complex_location_expr_complaint ();
14337
14338 return 1;
14339 }
14340
14341 return 0;
14342}
14343
7d79de9a
TT
14344/* Look for DW_AT_data_member_location and store the results in FIELD. */
14345
14346static void
14347handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14348 struct field *field)
14349{
14350 struct attribute *attr;
14351
14352 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14353 if (attr != NULL)
14354 {
14355 if (attr->form_is_constant ())
14356 {
14357 LONGEST offset = attr->constant_value (0);
14358 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14359 }
14360 else if (attr->form_is_section_offset ())
14361 dwarf2_complex_location_expr_complaint ();
14362 else if (attr->form_is_block ())
14363 {
14364 bool handled;
14365 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14366 if (handled)
14367 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14368 else
14369 {
5e22e966
SM
14370 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14371 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14372 struct dwarf2_locexpr_baton *dlbaton
14373 = XOBNEW (&objfile->objfile_obstack,
14374 struct dwarf2_locexpr_baton);
14375 dlbaton->data = DW_BLOCK (attr)->data;
14376 dlbaton->size = DW_BLOCK (attr)->size;
14377 /* When using this baton, we want to compute the address
14378 of the field, not the value. This is why
14379 is_reference is set to false here. */
14380 dlbaton->is_reference = false;
5e22e966 14381 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14382 dlbaton->per_cu = cu->per_cu;
14383
14384 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14385 }
14386 }
14387 else
14388 dwarf2_complex_location_expr_complaint ();
14389 }
14390}
14391
c906108c
SS
14392/* Add an aggregate field to the field list. */
14393
14394static void
107d2387 14395dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14396 struct dwarf2_cu *cu)
6e70227d 14397{
5e22e966 14398 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14399 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14400 struct nextfield *new_field;
14401 struct attribute *attr;
14402 struct field *fp;
15d034d0 14403 const char *fieldname = "";
c906108c 14404
7d0ccb61
DJ
14405 if (die->tag == DW_TAG_inheritance)
14406 {
be2daae6
TT
14407 fip->baseclasses.emplace_back ();
14408 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14409 }
14410 else
14411 {
be2daae6
TT
14412 fip->fields.emplace_back ();
14413 new_field = &fip->fields.back ();
7d0ccb61 14414 }
be2daae6 14415
9c6a1327
TT
14416 new_field->offset = die->sect_off;
14417
e142c38c 14418 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14419 if (attr != nullptr)
c906108c 14420 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14421 else
14422 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14423 if (new_field->accessibility != DW_ACCESS_public)
14424 fip->non_public_fields = 1;
60d5a603 14425
e142c38c 14426 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14427 if (attr != nullptr)
c906108c 14428 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14429 else
14430 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14431
14432 fp = &new_field->field;
a9a9bd0f 14433
e142c38c 14434 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14435 {
a9a9bd0f 14436 /* Data member other than a C++ static data member. */
6e70227d 14437
c906108c 14438 /* Get type of field. */
e7c27a73 14439 fp->type = die_type (die, cu);
c906108c 14440
d6a843b5 14441 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14442
c906108c 14443 /* Get bit size of field (zero if none). */
e142c38c 14444 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14445 if (attr != nullptr)
c906108c
SS
14446 {
14447 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14448 }
14449 else
14450 {
14451 FIELD_BITSIZE (*fp) = 0;
14452 }
14453
14454 /* Get bit offset of field. */
7d79de9a 14455 handle_data_member_location (die, cu, fp);
e142c38c 14456 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14457 if (attr != nullptr)
c906108c 14458 {
d5a22e77 14459 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14460 {
14461 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14462 additional bit offset from the MSB of the containing
14463 anonymous object to the MSB of the field. We don't
14464 have to do anything special since we don't need to
14465 know the size of the anonymous object. */
f41f5e61 14466 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14467 }
14468 else
14469 {
14470 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14471 MSB of the anonymous object, subtract off the number of
14472 bits from the MSB of the field to the MSB of the
14473 object, and then subtract off the number of bits of
14474 the field itself. The result is the bit offset of
14475 the LSB of the field. */
c906108c
SS
14476 int anonymous_size;
14477 int bit_offset = DW_UNSND (attr);
14478
e142c38c 14479 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14480 if (attr != nullptr)
c906108c
SS
14481 {
14482 /* The size of the anonymous object containing
14483 the bit field is explicit, so use the
14484 indicated size (in bytes). */
14485 anonymous_size = DW_UNSND (attr);
14486 }
14487 else
14488 {
14489 /* The size of the anonymous object containing
14490 the bit field must be inferred from the type
14491 attribute of the data member containing the
14492 bit field. */
14493 anonymous_size = TYPE_LENGTH (fp->type);
14494 }
f41f5e61
PA
14495 SET_FIELD_BITPOS (*fp,
14496 (FIELD_BITPOS (*fp)
14497 + anonymous_size * bits_per_byte
14498 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14499 }
14500 }
da5b30da
AA
14501 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14502 if (attr != NULL)
14503 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14504 + attr->constant_value (0)));
c906108c
SS
14505
14506 /* Get name of field. */
39cbfefa
DJ
14507 fieldname = dwarf2_name (die, cu);
14508 if (fieldname == NULL)
14509 fieldname = "";
d8151005
DJ
14510
14511 /* The name is already allocated along with this objfile, so we don't
14512 need to duplicate it for the type. */
14513 fp->name = fieldname;
c906108c
SS
14514
14515 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14516 pointer or virtual base class pointer) to private. */
e142c38c 14517 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14518 {
d48cc9dd 14519 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14520 new_field->accessibility = DW_ACCESS_private;
14521 fip->non_public_fields = 1;
14522 }
14523 }
a9a9bd0f 14524 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14525 {
a9a9bd0f
DC
14526 /* C++ static member. */
14527
14528 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14529 is a declaration, but all versions of G++ as of this writing
14530 (so through at least 3.2.1) incorrectly generate
14531 DW_TAG_variable tags. */
6e70227d 14532
ff355380 14533 const char *physname;
c906108c 14534
a9a9bd0f 14535 /* Get name of field. */
39cbfefa
DJ
14536 fieldname = dwarf2_name (die, cu);
14537 if (fieldname == NULL)
c906108c
SS
14538 return;
14539
254e6b9e 14540 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14541 if (attr
14542 /* Only create a symbol if this is an external value.
14543 new_symbol checks this and puts the value in the global symbol
14544 table, which we want. If it is not external, new_symbol
14545 will try to put the value in cu->list_in_scope which is wrong. */
14546 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14547 {
14548 /* A static const member, not much different than an enum as far as
14549 we're concerned, except that we can support more types. */
14550 new_symbol (die, NULL, cu);
14551 }
14552
2df3850c 14553 /* Get physical name. */
ff355380 14554 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14555
d8151005
DJ
14556 /* The name is already allocated along with this objfile, so we don't
14557 need to duplicate it for the type. */
14558 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14559 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14560 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14561 }
14562 else if (die->tag == DW_TAG_inheritance)
14563 {
74ac6d43 14564 /* C++ base class field. */
7d79de9a 14565 handle_data_member_location (die, cu, fp);
c906108c 14566 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14567 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14568 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14569 }
2ddeaf8a
TT
14570 else
14571 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14572}
14573
883fd55a
KS
14574/* Can the type given by DIE define another type? */
14575
14576static bool
14577type_can_define_types (const struct die_info *die)
14578{
14579 switch (die->tag)
14580 {
14581 case DW_TAG_typedef:
14582 case DW_TAG_class_type:
14583 case DW_TAG_structure_type:
14584 case DW_TAG_union_type:
14585 case DW_TAG_enumeration_type:
14586 return true;
14587
14588 default:
14589 return false;
14590 }
14591}
14592
14593/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14594
14595static void
883fd55a
KS
14596dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14597 struct dwarf2_cu *cu)
6e70227d 14598{
be2daae6
TT
14599 struct decl_field fp;
14600 memset (&fp, 0, sizeof (fp));
98751a41 14601
883fd55a 14602 gdb_assert (type_can_define_types (die));
98751a41 14603
883fd55a 14604 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14605 fp.name = dwarf2_name (die, cu);
14606 fp.type = read_type_die (die, cu);
98751a41 14607
c191a687
KS
14608 /* Save accessibility. */
14609 enum dwarf_access_attribute accessibility;
14610 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14611 if (attr != NULL)
14612 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14613 else
14614 accessibility = dwarf2_default_access_attribute (die, cu);
14615 switch (accessibility)
14616 {
14617 case DW_ACCESS_public:
14618 /* The assumed value if neither private nor protected. */
14619 break;
14620 case DW_ACCESS_private:
be2daae6 14621 fp.is_private = 1;
c191a687
KS
14622 break;
14623 case DW_ACCESS_protected:
be2daae6 14624 fp.is_protected = 1;
c191a687
KS
14625 break;
14626 default:
b98664d3 14627 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14628 }
14629
883fd55a 14630 if (die->tag == DW_TAG_typedef)
be2daae6 14631 fip->typedef_field_list.push_back (fp);
883fd55a 14632 else
be2daae6 14633 fip->nested_types_list.push_back (fp);
98751a41
JK
14634}
14635
9c6a1327
TT
14636/* A convenience typedef that's used when finding the discriminant
14637 field for a variant part. */
1b95cdb7
SM
14638typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14639 offset_map_type;
9c6a1327
TT
14640
14641/* Compute the discriminant range for a given variant. OBSTACK is
14642 where the results will be stored. VARIANT is the variant to
14643 process. IS_UNSIGNED indicates whether the discriminant is signed
14644 or unsigned. */
14645
14646static const gdb::array_view<discriminant_range>
14647convert_variant_range (struct obstack *obstack, const variant_field &variant,
14648 bool is_unsigned)
14649{
14650 std::vector<discriminant_range> ranges;
14651
14652 if (variant.default_branch)
14653 return {};
14654
14655 if (variant.discr_list_data == nullptr)
14656 {
14657 discriminant_range r
14658 = {variant.discriminant_value, variant.discriminant_value};
14659 ranges.push_back (r);
14660 }
14661 else
14662 {
14663 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14664 variant.discr_list_data->size);
14665 while (!data.empty ())
14666 {
14667 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14668 {
14669 complaint (_("invalid discriminant marker: %d"), data[0]);
14670 break;
14671 }
14672 bool is_range = data[0] == DW_DSC_range;
14673 data = data.slice (1);
14674
14675 ULONGEST low, high;
14676 unsigned int bytes_read;
14677
14678 if (data.empty ())
14679 {
14680 complaint (_("DW_AT_discr_list missing low value"));
14681 break;
14682 }
14683 if (is_unsigned)
14684 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14685 else
14686 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14687 &bytes_read);
14688 data = data.slice (bytes_read);
14689
14690 if (is_range)
14691 {
14692 if (data.empty ())
14693 {
14694 complaint (_("DW_AT_discr_list missing high value"));
14695 break;
14696 }
14697 if (is_unsigned)
14698 high = read_unsigned_leb128 (nullptr, data.data (),
14699 &bytes_read);
14700 else
14701 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14702 &bytes_read);
14703 data = data.slice (bytes_read);
14704 }
14705 else
14706 high = low;
14707
14708 ranges.push_back ({ low, high });
14709 }
14710 }
14711
14712 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14713 ranges.size ());
14714 std::copy (ranges.begin (), ranges.end (), result);
14715 return gdb::array_view<discriminant_range> (result, ranges.size ());
14716}
14717
14718static const gdb::array_view<variant_part> create_variant_parts
14719 (struct obstack *obstack,
14720 const offset_map_type &offset_map,
14721 struct field_info *fi,
14722 const std::vector<variant_part_builder> &variant_parts);
14723
14724/* Fill in a "struct variant" for a given variant field. RESULT is
14725 the variant to fill in. OBSTACK is where any needed allocations
14726 will be done. OFFSET_MAP holds the mapping from section offsets to
14727 fields for the type. FI describes the fields of the type we're
14728 processing. FIELD is the variant field we're converting. */
14729
14730static void
14731create_one_variant (variant &result, struct obstack *obstack,
14732 const offset_map_type &offset_map,
14733 struct field_info *fi, const variant_field &field)
14734{
14735 result.discriminants = convert_variant_range (obstack, field, false);
14736 result.first_field = field.first_field + fi->baseclasses.size ();
14737 result.last_field = field.last_field + fi->baseclasses.size ();
14738 result.parts = create_variant_parts (obstack, offset_map, fi,
14739 field.variant_parts);
14740}
14741
14742/* Fill in a "struct variant_part" for a given variant part. RESULT
14743 is the variant part to fill in. OBSTACK is where any needed
14744 allocations will be done. OFFSET_MAP holds the mapping from
14745 section offsets to fields for the type. FI describes the fields of
14746 the type we're processing. BUILDER is the variant part to be
14747 converted. */
14748
14749static void
14750create_one_variant_part (variant_part &result,
14751 struct obstack *obstack,
14752 const offset_map_type &offset_map,
14753 struct field_info *fi,
14754 const variant_part_builder &builder)
14755{
14756 auto iter = offset_map.find (builder.discriminant_offset);
14757 if (iter == offset_map.end ())
14758 {
14759 result.discriminant_index = -1;
14760 /* Doesn't matter. */
14761 result.is_unsigned = false;
14762 }
14763 else
14764 {
14765 result.discriminant_index = iter->second;
14766 result.is_unsigned
14767 = TYPE_UNSIGNED (FIELD_TYPE
14768 (fi->fields[result.discriminant_index].field));
14769 }
14770
14771 size_t n = builder.variants.size ();
14772 variant *output = new (obstack) variant[n];
14773 for (size_t i = 0; i < n; ++i)
14774 create_one_variant (output[i], obstack, offset_map, fi,
14775 builder.variants[i]);
14776
14777 result.variants = gdb::array_view<variant> (output, n);
14778}
14779
14780/* Create a vector of variant parts that can be attached to a type.
14781 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14782 holds the mapping from section offsets to fields for the type. FI
14783 describes the fields of the type we're processing. VARIANT_PARTS
14784 is the vector to convert. */
14785
14786static const gdb::array_view<variant_part>
14787create_variant_parts (struct obstack *obstack,
14788 const offset_map_type &offset_map,
14789 struct field_info *fi,
14790 const std::vector<variant_part_builder> &variant_parts)
14791{
14792 if (variant_parts.empty ())
14793 return {};
14794
14795 size_t n = variant_parts.size ();
14796 variant_part *result = new (obstack) variant_part[n];
14797 for (size_t i = 0; i < n; ++i)
14798 create_one_variant_part (result[i], obstack, offset_map, fi,
14799 variant_parts[i]);
14800
14801 return gdb::array_view<variant_part> (result, n);
14802}
14803
14804/* Compute the variant part vector for FIP, attaching it to TYPE when
14805 done. */
14806
14807static void
14808add_variant_property (struct field_info *fip, struct type *type,
14809 struct dwarf2_cu *cu)
14810{
14811 /* Map section offsets of fields to their field index. Note the
14812 field index here does not take the number of baseclasses into
14813 account. */
14814 offset_map_type offset_map;
14815 for (int i = 0; i < fip->fields.size (); ++i)
14816 offset_map[fip->fields[i].offset] = i;
14817
5e22e966 14818 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14819 gdb::array_view<variant_part> parts
14820 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14821 fip->variant_parts);
14822
14823 struct dynamic_prop prop;
14824 prop.kind = PROP_VARIANT_PARTS;
14825 prop.data.variant_parts
14826 = ((gdb::array_view<variant_part> *)
14827 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14828
5c54719c 14829 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14830}
14831
c906108c
SS
14832/* Create the vector of fields, and attach it to the type. */
14833
14834static void
fba45db2 14835dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14836 struct dwarf2_cu *cu)
c906108c 14837{
317f7127 14838 int nfields = fip->nfields ();
c906108c
SS
14839
14840 /* Record the field count, allocate space for the array of fields,
14841 and create blank accessibility bitfields if necessary. */
5e33d5f4 14842 type->set_num_fields (nfields);
3cabb6b0
SM
14843 type->set_fields
14844 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14845
b4ba55a1 14846 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14847 {
14848 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14849
14850 TYPE_FIELD_PRIVATE_BITS (type) =
14851 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14852 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14853
14854 TYPE_FIELD_PROTECTED_BITS (type) =
14855 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14856 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14857
774b6a14
TT
14858 TYPE_FIELD_IGNORE_BITS (type) =
14859 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14860 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14861 }
14862
14863 /* If the type has baseclasses, allocate and clear a bit vector for
14864 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14865 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14866 {
be2daae6 14867 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14868 unsigned char *pointer;
c906108c
SS
14869
14870 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14871 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14872 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14873 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14874 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14875 }
14876
9c6a1327
TT
14877 if (!fip->variant_parts.empty ())
14878 add_variant_property (fip, type, cu);
2ddeaf8a 14879
be2daae6
TT
14880 /* Copy the saved-up fields into the field vector. */
14881 for (int i = 0; i < nfields; ++i)
c906108c 14882 {
be2daae6
TT
14883 struct nextfield &field
14884 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14885 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14886
ceacbf6e 14887 type->field (i) = field.field;
be2daae6 14888 switch (field.accessibility)
c906108c 14889 {
c5aa993b 14890 case DW_ACCESS_private:
b4ba55a1 14891 if (cu->language != language_ada)
be2daae6 14892 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14893 break;
c906108c 14894
c5aa993b 14895 case DW_ACCESS_protected:
b4ba55a1 14896 if (cu->language != language_ada)
be2daae6 14897 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14898 break;
c906108c 14899
c5aa993b
JM
14900 case DW_ACCESS_public:
14901 break;
c906108c 14902
c5aa993b
JM
14903 default:
14904 /* Unknown accessibility. Complain and treat it as public. */
14905 {
b98664d3 14906 complaint (_("unsupported accessibility %d"),
be2daae6 14907 field.accessibility);
c5aa993b
JM
14908 }
14909 break;
c906108c 14910 }
be2daae6 14911 if (i < fip->baseclasses.size ())
c906108c 14912 {
be2daae6 14913 switch (field.virtuality)
c906108c 14914 {
c5aa993b
JM
14915 case DW_VIRTUALITY_virtual:
14916 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14917 if (cu->language == language_ada)
a73c6dcd 14918 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14919 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14920 break;
c906108c
SS
14921 }
14922 }
c906108c
SS
14923 }
14924}
14925
7d27a96d
TT
14926/* Return true if this member function is a constructor, false
14927 otherwise. */
14928
14929static int
14930dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14931{
14932 const char *fieldname;
fe978cb0 14933 const char *type_name;
7d27a96d
TT
14934 int len;
14935
14936 if (die->parent == NULL)
14937 return 0;
14938
14939 if (die->parent->tag != DW_TAG_structure_type
14940 && die->parent->tag != DW_TAG_union_type
14941 && die->parent->tag != DW_TAG_class_type)
14942 return 0;
14943
14944 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14945 type_name = dwarf2_name (die->parent, cu);
14946 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14947 return 0;
14948
14949 len = strlen (fieldname);
fe978cb0
PA
14950 return (strncmp (fieldname, type_name, len) == 0
14951 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14952}
14953
e35000a7
TBA
14954/* Check if the given VALUE is a recognized enum
14955 dwarf_defaulted_attribute constant according to DWARF5 spec,
14956 Table 7.24. */
14957
14958static bool
14959is_valid_DW_AT_defaulted (ULONGEST value)
14960{
14961 switch (value)
14962 {
14963 case DW_DEFAULTED_no:
14964 case DW_DEFAULTED_in_class:
14965 case DW_DEFAULTED_out_of_class:
14966 return true;
14967 }
14968
3142e908 14969 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14970 return false;
14971}
14972
c906108c
SS
14973/* Add a member function to the proper fieldlist. */
14974
14975static void
107d2387 14976dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14977 struct type *type, struct dwarf2_cu *cu)
c906108c 14978{
5e22e966 14979 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 14980 struct attribute *attr;
c906108c 14981 int i;
be2daae6 14982 struct fnfieldlist *flp = nullptr;
c906108c 14983 struct fn_field *fnp;
15d034d0 14984 const char *fieldname;
f792889a 14985 struct type *this_type;
60d5a603 14986 enum dwarf_access_attribute accessibility;
c906108c 14987
b4ba55a1 14988 if (cu->language == language_ada)
a73c6dcd 14989 error (_("unexpected member function in Ada type"));
b4ba55a1 14990
2df3850c 14991 /* Get name of member function. */
39cbfefa
DJ
14992 fieldname = dwarf2_name (die, cu);
14993 if (fieldname == NULL)
2df3850c 14994 return;
c906108c 14995
c906108c 14996 /* Look up member function name in fieldlist. */
be2daae6 14997 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14998 {
27bfe10e 14999 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15000 {
15001 flp = &fip->fnfieldlists[i];
15002 break;
15003 }
c906108c
SS
15004 }
15005
be2daae6
TT
15006 /* Create a new fnfieldlist if necessary. */
15007 if (flp == nullptr)
c906108c 15008 {
be2daae6
TT
15009 fip->fnfieldlists.emplace_back ();
15010 flp = &fip->fnfieldlists.back ();
c906108c 15011 flp->name = fieldname;
be2daae6 15012 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15013 }
15014
be2daae6
TT
15015 /* Create a new member function field and add it to the vector of
15016 fnfieldlists. */
15017 flp->fnfields.emplace_back ();
15018 fnp = &flp->fnfields.back ();
3da10d80
KS
15019
15020 /* Delay processing of the physname until later. */
9c37b5ae 15021 if (cu->language == language_cplus)
be2daae6
TT
15022 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15023 die, cu);
3da10d80
KS
15024 else
15025 {
1d06ead6 15026 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15027 fnp->physname = physname ? physname : "";
15028 }
15029
c906108c 15030 fnp->type = alloc_type (objfile);
f792889a 15031 this_type = read_type_die (die, cu);
78134374 15032 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15033 {
1f704f76 15034 int nparams = this_type->num_fields ();
c906108c 15035
f792889a 15036 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15037 of the method itself (TYPE_CODE_METHOD). */
15038 smash_to_method_type (fnp->type, type,
f792889a 15039 TYPE_TARGET_TYPE (this_type),
80fc5e77 15040 this_type->fields (),
1f704f76 15041 this_type->num_fields (),
f792889a 15042 TYPE_VARARGS (this_type));
c906108c
SS
15043
15044 /* Handle static member functions.
c5aa993b 15045 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15046 member functions. G++ helps GDB by marking the first
15047 parameter for non-static member functions (which is the this
15048 pointer) as artificial. We obtain this information from
15049 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15050 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15051 fnp->voffset = VOFFSET_STATIC;
15052 }
15053 else
b98664d3 15054 complaint (_("member function type missing for '%s'"),
3da10d80 15055 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15056
15057 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15058 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15059 fnp->fcontext = die_containing_type (die, cu);
c906108c 15060
3e43a32a
MS
15061 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15062 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15063
15064 /* Get accessibility. */
e142c38c 15065 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15066 if (attr != nullptr)
aead7601 15067 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15068 else
15069 accessibility = dwarf2_default_access_attribute (die, cu);
15070 switch (accessibility)
c906108c 15071 {
60d5a603
JK
15072 case DW_ACCESS_private:
15073 fnp->is_private = 1;
15074 break;
15075 case DW_ACCESS_protected:
15076 fnp->is_protected = 1;
15077 break;
c906108c
SS
15078 }
15079
b02dede2 15080 /* Check for artificial methods. */
e142c38c 15081 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15082 if (attr && DW_UNSND (attr) != 0)
15083 fnp->is_artificial = 1;
15084
e35000a7
TBA
15085 /* Check for defaulted methods. */
15086 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15087 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15088 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15089
15090 /* Check for deleted methods. */
15091 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15092 if (attr != nullptr && DW_UNSND (attr) != 0)
15093 fnp->is_deleted = 1;
15094
7d27a96d
TT
15095 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15096
0d564a31 15097 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15098 function. For older versions of GCC, this is an offset in the
15099 appropriate virtual table, as specified by DW_AT_containing_type.
15100 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15101 to the object address. */
15102
e142c38c 15103 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15104 if (attr != nullptr)
8e19ed76 15105 {
4fc6c0d5 15106 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15107 {
aec5aa8b
TT
15108 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15109 {
15110 /* Old-style GCC. */
15111 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15112 }
15113 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15114 || (DW_BLOCK (attr)->size > 1
15115 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15116 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15117 {
aec5aa8b
TT
15118 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15119 if ((fnp->voffset % cu->header.addr_size) != 0)
15120 dwarf2_complex_location_expr_complaint ();
15121 else
15122 fnp->voffset /= cu->header.addr_size;
15123 fnp->voffset += 2;
15124 }
15125 else
15126 dwarf2_complex_location_expr_complaint ();
15127
15128 if (!fnp->fcontext)
7e993ebf
KS
15129 {
15130 /* If there is no `this' field and no DW_AT_containing_type,
15131 we cannot actually find a base class context for the
15132 vtable! */
1f704f76 15133 if (this_type->num_fields () == 0
7e993ebf
KS
15134 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15135 {
b98664d3 15136 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15137 "function \"%s\" (offset %s)"),
15138 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15139 }
15140 else
15141 {
15142 fnp->fcontext
15143 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15144 }
15145 }
aec5aa8b 15146 }
cd6c91b4 15147 else if (attr->form_is_section_offset ())
8e19ed76 15148 {
4d3c2250 15149 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15150 }
15151 else
15152 {
4d3c2250
KB
15153 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15154 fieldname);
8e19ed76 15155 }
0d564a31 15156 }
d48cc9dd
DJ
15157 else
15158 {
15159 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15160 if (attr && DW_UNSND (attr))
15161 {
15162 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15163 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15164 "but the vtable offset is not specified"),
9d8780f0 15165 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15166 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15167 TYPE_CPLUS_DYNAMIC (type) = 1;
15168 }
15169 }
c906108c
SS
15170}
15171
15172/* Create the vector of member function fields, and attach it to the type. */
15173
15174static void
fba45db2 15175dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15176 struct dwarf2_cu *cu)
c906108c 15177{
b4ba55a1 15178 if (cu->language == language_ada)
a73c6dcd 15179 error (_("unexpected member functions in Ada type"));
b4ba55a1 15180
c906108c
SS
15181 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15182 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15183 TYPE_ALLOC (type,
15184 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15185
be2daae6 15186 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15187 {
be2daae6 15188 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15189 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15190
be2daae6
TT
15191 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15192 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15193 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15194 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15195
15196 for (int k = 0; k < nf.fnfields.size (); ++k)
15197 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15198 }
15199
be2daae6 15200 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15201}
15202
1168df01
JB
15203/* Returns non-zero if NAME is the name of a vtable member in CU's
15204 language, zero otherwise. */
15205static int
15206is_vtable_name (const char *name, struct dwarf2_cu *cu)
15207{
15208 static const char vptr[] = "_vptr";
15209
9c37b5ae
TT
15210 /* Look for the C++ form of the vtable. */
15211 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15212 return 1;
15213
15214 return 0;
15215}
15216
c0dd20ea 15217/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15218 functions, with the ABI-specified layout. If TYPE describes
15219 such a structure, smash it into a member function type.
61049d3b
DJ
15220
15221 GCC shouldn't do this; it should just output pointer to member DIEs.
15222 This is GCC PR debug/28767. */
c0dd20ea 15223
0b92b5bb
TT
15224static void
15225quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15226{
09e2d7c7 15227 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15228
15229 /* Check for a structure with no name and two children. */
1f704f76 15230 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15231 return;
c0dd20ea
DJ
15232
15233 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15234 if (TYPE_FIELD_NAME (type, 0) == NULL
15235 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15236 || TYPE_FIELD_NAME (type, 1) == NULL
15237 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15238 return;
c0dd20ea
DJ
15239
15240 /* Find the type of the method. */
0b92b5bb 15241 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15242 if (pfn_type == NULL
78134374
SM
15243 || pfn_type->code () != TYPE_CODE_PTR
15244 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15245 return;
c0dd20ea
DJ
15246
15247 /* Look for the "this" argument. */
15248 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15249 if (pfn_type->num_fields () == 0
0b92b5bb 15250 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15251 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15252 return;
c0dd20ea 15253
09e2d7c7 15254 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15255 new_type = alloc_type (objfile);
09e2d7c7 15256 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15257 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15258 TYPE_VARARGS (pfn_type));
0b92b5bb 15259 smash_to_methodptr_type (type, new_type);
c0dd20ea 15260}
1168df01 15261
2b4424c3
TT
15262/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15263 appropriate error checking and issuing complaints if there is a
15264 problem. */
15265
15266static ULONGEST
15267get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15268{
15269 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15270
15271 if (attr == nullptr)
15272 return 0;
15273
cd6c91b4 15274 if (!attr->form_is_constant ())
2b4424c3 15275 {
b98664d3 15276 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15277 " - DIE at %s [in module %s]"),
15278 sect_offset_str (die->sect_off),
5e22e966 15279 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15280 return 0;
15281 }
15282
15283 ULONGEST align;
15284 if (attr->form == DW_FORM_sdata)
15285 {
15286 LONGEST val = DW_SND (attr);
15287 if (val < 0)
15288 {
b98664d3 15289 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15290 " - DIE at %s [in module %s]"),
15291 sect_offset_str (die->sect_off),
5e22e966 15292 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15293 return 0;
15294 }
15295 align = val;
15296 }
15297 else
15298 align = DW_UNSND (attr);
15299
15300 if (align == 0)
15301 {
b98664d3 15302 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15303 " - DIE at %s [in module %s]"),
15304 sect_offset_str (die->sect_off),
5e22e966 15305 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15306 return 0;
15307 }
15308 if ((align & (align - 1)) != 0)
15309 {
b98664d3 15310 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15311 " - DIE at %s [in module %s]"),
15312 sect_offset_str (die->sect_off),
5e22e966 15313 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15314 return 0;
15315 }
15316
15317 return align;
15318}
15319
15320/* If the DIE has a DW_AT_alignment attribute, use its value to set
15321 the alignment for TYPE. */
15322
15323static void
15324maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15325 struct type *type)
15326{
15327 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15328 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15329 " - DIE at %s [in module %s]"),
15330 sect_offset_str (die->sect_off),
5e22e966 15331 objfile_name (cu->per_objfile->objfile));
2b4424c3 15332}
685b1105 15333
e35000a7
TBA
15334/* Check if the given VALUE is a valid enum dwarf_calling_convention
15335 constant for a type, according to DWARF5 spec, Table 5.5. */
15336
15337static bool
15338is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15339{
15340 switch (value)
15341 {
15342 case DW_CC_normal:
15343 case DW_CC_pass_by_reference:
15344 case DW_CC_pass_by_value:
15345 return true;
15346
15347 default:
15348 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15349 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15350 return false;
15351 }
15352}
15353
d0922fcf
TBA
15354/* Check if the given VALUE is a valid enum dwarf_calling_convention
15355 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15356 also according to GNU-specific values (see include/dwarf2.h). */
15357
15358static bool
15359is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15360{
15361 switch (value)
15362 {
15363 case DW_CC_normal:
15364 case DW_CC_program:
15365 case DW_CC_nocall:
15366 return true;
15367
15368 case DW_CC_GNU_renesas_sh:
15369 case DW_CC_GNU_borland_fastcall_i386:
15370 case DW_CC_GDB_IBM_OpenCL:
15371 return true;
15372
15373 default:
15374 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15375 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15376 return false;
15377 }
15378}
15379
c906108c 15380/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15381 (definition) to create a type for the structure or union. Fill in
15382 the type's name and general properties; the members will not be
83655187
DE
15383 processed until process_structure_scope. A symbol table entry for
15384 the type will also not be done until process_structure_scope (assuming
15385 the type has a name).
c906108c 15386
c767944b
DJ
15387 NOTE: we need to call these functions regardless of whether or not the
15388 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15389 structure or union. This gets the type entered into our set of
83655187 15390 user defined types. */
c906108c 15391
f792889a 15392static struct type *
134d01f1 15393read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15394{
5e22e966 15395 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15396 struct type *type;
15397 struct attribute *attr;
15d034d0 15398 const char *name;
c906108c 15399
348e048f
DE
15400 /* If the definition of this type lives in .debug_types, read that type.
15401 Don't follow DW_AT_specification though, that will take us back up
15402 the chain and we want to go down. */
052c8bb8 15403 attr = die->attr (DW_AT_signature);
435d3d88 15404 if (attr != nullptr)
348e048f 15405 {
ac9ec31b 15406 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15407
ac9ec31b 15408 /* The type's CU may not be the same as CU.
02142a6c 15409 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15410 return set_die_type (die, type, cu);
15411 }
15412
c0dd20ea 15413 type = alloc_type (objfile);
c906108c 15414 INIT_CPLUS_SPECIFIC (type);
93311388 15415
39cbfefa
DJ
15416 name = dwarf2_name (die, cu);
15417 if (name != NULL)
c906108c 15418 {
987504bb 15419 if (cu->language == language_cplus
c44af4eb
TT
15420 || cu->language == language_d
15421 || cu->language == language_rust)
63d06c5c 15422 {
15d034d0 15423 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15424
15425 /* dwarf2_full_name might have already finished building the DIE's
15426 type. If so, there is no need to continue. */
15427 if (get_die_type (die, cu) != NULL)
15428 return get_die_type (die, cu);
15429
d0e39ea2 15430 type->set_name (full_name);
63d06c5c
DC
15431 }
15432 else
15433 {
d8151005
DJ
15434 /* The name is already allocated along with this objfile, so
15435 we don't need to duplicate it for the type. */
d0e39ea2 15436 type->set_name (name);
63d06c5c 15437 }
c906108c
SS
15438 }
15439
15440 if (die->tag == DW_TAG_structure_type)
15441 {
67607e24 15442 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15443 }
15444 else if (die->tag == DW_TAG_union_type)
15445 {
67607e24 15446 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15447 }
15448 else
15449 {
67607e24 15450 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15451 }
15452
0cc2414c
TT
15453 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15454 TYPE_DECLARED_CLASS (type) = 1;
15455
e35000a7
TBA
15456 /* Store the calling convention in the type if it's available in
15457 the die. Otherwise the calling convention remains set to
15458 the default value DW_CC_normal. */
15459 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15460 if (attr != nullptr
15461 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15462 {
15463 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15464 TYPE_CPLUS_CALLING_CONVENTION (type)
15465 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15466 }
15467
e142c38c 15468 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15469 if (attr != nullptr)
c906108c 15470 {
cd6c91b4 15471 if (attr->form_is_constant ())
155bfbd3
JB
15472 TYPE_LENGTH (type) = DW_UNSND (attr);
15473 else
15474 {
f8e89861
TT
15475 struct dynamic_prop prop;
15476 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15477 cu->per_cu->addr_type ()))
5c54719c 15478 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15479 TYPE_LENGTH (type) = 0;
15480 }
c906108c
SS
15481 }
15482 else
15483 {
15484 TYPE_LENGTH (type) = 0;
15485 }
15486
2b4424c3
TT
15487 maybe_set_alignment (cu, die, type);
15488
5230b05a 15489 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15490 {
5230b05a
WT
15491 /* ICC<14 does not output the required DW_AT_declaration on
15492 incomplete types, but gives them a size of zero. */
422b1cb0 15493 TYPE_STUB (type) = 1;
685b1105
JK
15494 }
15495 else
15496 TYPE_STUB_SUPPORTED (type) = 1;
15497
dc718098 15498 if (die_is_declaration (die, cu))
876cecd0 15499 TYPE_STUB (type) = 1;
a6c727b2
DJ
15500 else if (attr == NULL && die->child == NULL
15501 && producer_is_realview (cu->producer))
15502 /* RealView does not output the required DW_AT_declaration
15503 on incomplete types. */
15504 TYPE_STUB (type) = 1;
dc718098 15505
c906108c
SS
15506 /* We need to add the type field to the die immediately so we don't
15507 infinitely recurse when dealing with pointers to the structure
0963b4bd 15508 type within the structure itself. */
1c379e20 15509 set_die_type (die, type, cu);
c906108c 15510
7e314c57
JK
15511 /* set_die_type should be already done. */
15512 set_descriptive_type (type, die, cu);
15513
c767944b
DJ
15514 return type;
15515}
15516
9c6a1327
TT
15517static void handle_struct_member_die
15518 (struct die_info *child_die,
15519 struct type *type,
15520 struct field_info *fi,
15521 std::vector<struct symbol *> *template_args,
15522 struct dwarf2_cu *cu);
15523
15524/* A helper for handle_struct_member_die that handles
15525 DW_TAG_variant_part. */
15526
15527static void
15528handle_variant_part (struct die_info *die, struct type *type,
15529 struct field_info *fi,
15530 std::vector<struct symbol *> *template_args,
15531 struct dwarf2_cu *cu)
15532{
15533 variant_part_builder *new_part;
15534 if (fi->current_variant_part == nullptr)
15535 {
15536 fi->variant_parts.emplace_back ();
15537 new_part = &fi->variant_parts.back ();
15538 }
15539 else if (!fi->current_variant_part->processing_variant)
15540 {
15541 complaint (_("nested DW_TAG_variant_part seen "
15542 "- DIE at %s [in module %s]"),
15543 sect_offset_str (die->sect_off),
5e22e966 15544 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15545 return;
15546 }
15547 else
15548 {
15549 variant_field &current = fi->current_variant_part->variants.back ();
15550 current.variant_parts.emplace_back ();
15551 new_part = &current.variant_parts.back ();
15552 }
15553
15554 /* When we recurse, we want callees to add to this new variant
15555 part. */
15556 scoped_restore save_current_variant_part
15557 = make_scoped_restore (&fi->current_variant_part, new_part);
15558
15559 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15560 if (discr == NULL)
15561 {
15562 /* It's a univariant form, an extension we support. */
15563 }
15564 else if (discr->form_is_ref ())
15565 {
15566 struct dwarf2_cu *target_cu = cu;
15567 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15568
15569 new_part->discriminant_offset = target_die->sect_off;
15570 }
15571 else
15572 {
15573 complaint (_("DW_AT_discr does not have DIE reference form"
15574 " - DIE at %s [in module %s]"),
15575 sect_offset_str (die->sect_off),
5e22e966 15576 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15577 }
15578
15579 for (die_info *child_die = die->child;
15580 child_die != NULL;
15581 child_die = child_die->sibling)
15582 handle_struct_member_die (child_die, type, fi, template_args, cu);
15583}
15584
15585/* A helper for handle_struct_member_die that handles
15586 DW_TAG_variant. */
15587
15588static void
15589handle_variant (struct die_info *die, struct type *type,
15590 struct field_info *fi,
15591 std::vector<struct symbol *> *template_args,
15592 struct dwarf2_cu *cu)
15593{
15594 if (fi->current_variant_part == nullptr)
15595 {
15596 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15597 "- DIE at %s [in module %s]"),
15598 sect_offset_str (die->sect_off),
5e22e966 15599 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15600 return;
15601 }
15602 if (fi->current_variant_part->processing_variant)
15603 {
15604 complaint (_("nested DW_TAG_variant seen "
15605 "- DIE at %s [in module %s]"),
15606 sect_offset_str (die->sect_off),
5e22e966 15607 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15608 return;
15609 }
15610
15611 scoped_restore save_processing_variant
15612 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15613 true);
15614
15615 fi->current_variant_part->variants.emplace_back ();
15616 variant_field &variant = fi->current_variant_part->variants.back ();
15617 variant.first_field = fi->fields.size ();
15618
15619 /* In a variant we want to get the discriminant and also add a
15620 field for our sole member child. */
15621 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15622 if (discr == nullptr)
15623 {
15624 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15625 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15626 variant.default_branch = true;
15627 else
15628 variant.discr_list_data = DW_BLOCK (discr);
15629 }
15630 else
15631 variant.discriminant_value = DW_UNSND (discr);
15632
15633 for (die_info *variant_child = die->child;
15634 variant_child != NULL;
15635 variant_child = variant_child->sibling)
15636 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15637
15638 variant.last_field = fi->fields.size ();
15639}
15640
2ddeaf8a
TT
15641/* A helper for process_structure_scope that handles a single member
15642 DIE. */
15643
15644static void
15645handle_struct_member_die (struct die_info *child_die, struct type *type,
15646 struct field_info *fi,
15647 std::vector<struct symbol *> *template_args,
15648 struct dwarf2_cu *cu)
15649{
15650 if (child_die->tag == DW_TAG_member
9c6a1327 15651 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15652 {
15653 /* NOTE: carlton/2002-11-05: A C++ static data member
15654 should be a DW_TAG_member that is a declaration, but
15655 all versions of G++ as of this writing (so through at
15656 least 3.2.1) incorrectly generate DW_TAG_variable
15657 tags for them instead. */
15658 dwarf2_add_field (fi, child_die, cu);
15659 }
15660 else if (child_die->tag == DW_TAG_subprogram)
15661 {
15662 /* Rust doesn't have member functions in the C++ sense.
15663 However, it does emit ordinary functions as children
15664 of a struct DIE. */
15665 if (cu->language == language_rust)
15666 read_func_scope (child_die, cu);
15667 else
15668 {
15669 /* C++ member function. */
15670 dwarf2_add_member_fn (fi, child_die, type, cu);
15671 }
15672 }
15673 else if (child_die->tag == DW_TAG_inheritance)
15674 {
15675 /* C++ base class field. */
15676 dwarf2_add_field (fi, child_die, cu);
15677 }
15678 else if (type_can_define_types (child_die))
15679 dwarf2_add_type_defn (fi, child_die, cu);
15680 else if (child_die->tag == DW_TAG_template_type_param
15681 || child_die->tag == DW_TAG_template_value_param)
15682 {
15683 struct symbol *arg = new_symbol (child_die, NULL, cu);
15684
15685 if (arg != NULL)
15686 template_args->push_back (arg);
15687 }
9c6a1327
TT
15688 else if (child_die->tag == DW_TAG_variant_part)
15689 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15690 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15691 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15692}
15693
c767944b
DJ
15694/* Finish creating a structure or union type, including filling in
15695 its members and creating a symbol for it. */
15696
15697static void
15698process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15699{
5e22e966 15700 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15701 struct die_info *child_die;
c767944b
DJ
15702 struct type *type;
15703
15704 type = get_die_type (die, cu);
15705 if (type == NULL)
15706 type = read_structure_type (die, cu);
15707
3e1d3d8c 15708 bool has_template_parameters = false;
e142c38c 15709 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15710 {
15711 struct field_info fi;
2f4732b0 15712 std::vector<struct symbol *> template_args;
c906108c 15713
639d11d3 15714 child_die = die->child;
c906108c
SS
15715
15716 while (child_die && child_die->tag)
15717 {
2ddeaf8a 15718 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15719 child_die = child_die->sibling;
c906108c
SS
15720 }
15721
34eaf542 15722 /* Attach template arguments to type. */
2f4732b0 15723 if (!template_args.empty ())
34eaf542 15724 {
3e1d3d8c 15725 has_template_parameters = true;
34eaf542 15726 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15727 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15728 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15729 = XOBNEWVEC (&objfile->objfile_obstack,
15730 struct symbol *,
15731 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15732 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15733 template_args.data (),
34eaf542
TT
15734 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15735 * sizeof (struct symbol *)));
34eaf542
TT
15736 }
15737
c906108c 15738 /* Attach fields and member functions to the type. */
317f7127 15739 if (fi.nfields () > 0)
e7c27a73 15740 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15741 if (!fi.fnfieldlists.empty ())
c906108c 15742 {
e7c27a73 15743 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15744
c5aa993b 15745 /* Get the type which refers to the base class (possibly this
c906108c 15746 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15747 class from the DW_AT_containing_type attribute. This use of
15748 DW_AT_containing_type is a GNU extension. */
c906108c 15749
e142c38c 15750 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15751 {
e7c27a73 15752 struct type *t = die_containing_type (die, cu);
c906108c 15753
ae6ae975 15754 set_type_vptr_basetype (type, t);
c906108c
SS
15755 if (type == t)
15756 {
c906108c
SS
15757 int i;
15758
15759 /* Our own class provides vtbl ptr. */
1f704f76 15760 for (i = t->num_fields () - 1;
c906108c
SS
15761 i >= TYPE_N_BASECLASSES (t);
15762 --i)
15763 {
0d5cff50 15764 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15765
1168df01 15766 if (is_vtable_name (fieldname, cu))
c906108c 15767 {
ae6ae975 15768 set_type_vptr_fieldno (type, i);
c906108c
SS
15769 break;
15770 }
15771 }
15772
15773 /* Complain if virtual function table field not found. */
15774 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15775 complaint (_("virtual function table pointer "
3e43a32a 15776 "not found when defining class '%s'"),
7d93a1e0 15777 type->name () ? type->name () : "");
c906108c
SS
15778 }
15779 else
15780 {
ae6ae975 15781 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15782 }
15783 }
f6235d4c 15784 else if (cu->producer
61012eef 15785 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15786 {
15787 /* The IBM XLC compiler does not provide direct indication
15788 of the containing type, but the vtable pointer is
15789 always named __vfp. */
15790
15791 int i;
15792
1f704f76 15793 for (i = type->num_fields () - 1;
f6235d4c
EZ
15794 i >= TYPE_N_BASECLASSES (type);
15795 --i)
15796 {
15797 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15798 {
ae6ae975
DE
15799 set_type_vptr_fieldno (type, i);
15800 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15801 break;
15802 }
15803 }
15804 }
c906108c 15805 }
98751a41
JK
15806
15807 /* Copy fi.typedef_field_list linked list elements content into the
15808 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15809 if (!fi.typedef_field_list.empty ())
98751a41 15810 {
be2daae6 15811 int count = fi.typedef_field_list.size ();
98751a41 15812
a0d7a4ff 15813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15814 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15815 = ((struct decl_field *)
be2daae6
TT
15816 TYPE_ALLOC (type,
15817 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15818 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15819
be2daae6
TT
15820 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15821 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15822 }
c767944b 15823
883fd55a
KS
15824 /* Copy fi.nested_types_list linked list elements content into the
15825 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15826 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15827 {
be2daae6 15828 int count = fi.nested_types_list.size ();
883fd55a
KS
15829
15830 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15831 TYPE_NESTED_TYPES_ARRAY (type)
15832 = ((struct decl_field *)
be2daae6
TT
15833 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15834 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15835
be2daae6
TT
15836 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15837 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15838 }
c906108c 15839 }
63d06c5c 15840
bb5ed363 15841 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15842 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15843 cu->rust_unions.push_back (type);
0b92b5bb 15844
90aeadfc
DC
15845 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15846 snapshots) has been known to create a die giving a declaration
15847 for a class that has, as a child, a die giving a definition for a
15848 nested class. So we have to process our children even if the
15849 current die is a declaration. Normally, of course, a declaration
15850 won't have any children at all. */
134d01f1 15851
ca040673
DE
15852 child_die = die->child;
15853
90aeadfc
DC
15854 while (child_die != NULL && child_die->tag)
15855 {
15856 if (child_die->tag == DW_TAG_member
15857 || child_die->tag == DW_TAG_variable
34eaf542
TT
15858 || child_die->tag == DW_TAG_inheritance
15859 || child_die->tag == DW_TAG_template_value_param
15860 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15861 {
90aeadfc 15862 /* Do nothing. */
134d01f1 15863 }
90aeadfc
DC
15864 else
15865 process_die (child_die, cu);
134d01f1 15866
436c571c 15867 child_die = child_die->sibling;
134d01f1
DJ
15868 }
15869
fa4028e9
JB
15870 /* Do not consider external references. According to the DWARF standard,
15871 these DIEs are identified by the fact that they have no byte_size
15872 attribute, and a declaration attribute. */
15873 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15874 || !die_is_declaration (die, cu)
15875 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15876 {
15877 struct symbol *sym = new_symbol (die, type, cu);
15878
15879 if (has_template_parameters)
15880 {
a776957c
TT
15881 struct symtab *symtab;
15882 if (sym != nullptr)
15883 symtab = symbol_symtab (sym);
15884 else if (cu->line_header != nullptr)
15885 {
15886 /* Any related symtab will do. */
15887 symtab
7ba99d21 15888 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15889 }
15890 else
15891 {
15892 symtab = nullptr;
15893 complaint (_("could not find suitable "
15894 "symtab for template parameter"
15895 " - DIE at %s [in module %s]"),
15896 sect_offset_str (die->sect_off),
15897 objfile_name (objfile));
15898 }
15899
15900 if (symtab != nullptr)
15901 {
15902 /* Make sure that the symtab is set on the new symbols.
15903 Even though they don't appear in this symtab directly,
15904 other parts of gdb assume that symbols do, and this is
15905 reasonably true. */
15906 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15907 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15908 }
3e1d3d8c
TT
15909 }
15910 }
134d01f1
DJ
15911}
15912
ed6acedd
TT
15913/* Assuming DIE is an enumeration type, and TYPE is its associated
15914 type, update TYPE using some information only available in DIE's
15915 children. In particular, the fields are computed. */
55426c9d
JB
15916
15917static void
15918update_enumeration_type_from_children (struct die_info *die,
15919 struct type *type,
15920 struct dwarf2_cu *cu)
15921{
60f7655a 15922 struct die_info *child_die;
55426c9d
JB
15923 int unsigned_enum = 1;
15924 int flag_enum = 1;
55426c9d 15925
8268c778 15926 auto_obstack obstack;
ed6acedd 15927 std::vector<struct field> fields;
55426c9d 15928
60f7655a
DE
15929 for (child_die = die->child;
15930 child_die != NULL && child_die->tag;
436c571c 15931 child_die = child_die->sibling)
55426c9d
JB
15932 {
15933 struct attribute *attr;
15934 LONGEST value;
15935 const gdb_byte *bytes;
15936 struct dwarf2_locexpr_baton *baton;
15937 const char *name;
60f7655a 15938
55426c9d
JB
15939 if (child_die->tag != DW_TAG_enumerator)
15940 continue;
15941
15942 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15943 if (attr == NULL)
15944 continue;
15945
15946 name = dwarf2_name (child_die, cu);
15947 if (name == NULL)
15948 name = "<anonymous enumerator>";
15949
15950 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15951 &value, &bytes, &baton);
15952 if (value < 0)
15953 {
15954 unsigned_enum = 0;
15955 flag_enum = 0;
15956 }
55426c9d 15957 else
edd45eb0
SM
15958 {
15959 if (count_one_bits_ll (value) >= 2)
15960 flag_enum = 0;
edd45eb0 15961 }
55426c9d 15962
ed6acedd
TT
15963 fields.emplace_back ();
15964 struct field &field = fields.back ();
15965 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15966 SET_FIELD_ENUMVAL (field, value);
15967 }
15968
15969 if (!fields.empty ())
15970 {
5e33d5f4 15971 type->set_num_fields (fields.size ());
3cabb6b0
SM
15972 type->set_fields
15973 ((struct field *)
15974 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 15975 memcpy (type->fields (), fields.data (),
ed6acedd 15976 sizeof (struct field) * fields.size ());
55426c9d
JB
15977 }
15978
15979 if (unsigned_enum)
15980 TYPE_UNSIGNED (type) = 1;
15981 if (flag_enum)
15982 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15983}
15984
134d01f1
DJ
15985/* Given a DW_AT_enumeration_type die, set its type. We do not
15986 complete the type's fields yet, or create any symbols. */
c906108c 15987
f792889a 15988static struct type *
134d01f1 15989read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15990{
5e22e966 15991 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15992 struct type *type;
c906108c 15993 struct attribute *attr;
0114d602 15994 const char *name;
134d01f1 15995
348e048f
DE
15996 /* If the definition of this type lives in .debug_types, read that type.
15997 Don't follow DW_AT_specification though, that will take us back up
15998 the chain and we want to go down. */
052c8bb8 15999 attr = die->attr (DW_AT_signature);
435d3d88 16000 if (attr != nullptr)
348e048f 16001 {
ac9ec31b 16002 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16003
ac9ec31b 16004 /* The type's CU may not be the same as CU.
02142a6c 16005 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16006 return set_die_type (die, type, cu);
16007 }
16008
c906108c
SS
16009 type = alloc_type (objfile);
16010
67607e24 16011 type->set_code (TYPE_CODE_ENUM);
94af9270 16012 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16013 if (name != NULL)
d0e39ea2 16014 type->set_name (name);
c906108c 16015
0626fc76
TT
16016 attr = dwarf2_attr (die, DW_AT_type, cu);
16017 if (attr != NULL)
16018 {
16019 struct type *underlying_type = die_type (die, cu);
16020
16021 TYPE_TARGET_TYPE (type) = underlying_type;
16022 }
16023
e142c38c 16024 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16025 if (attr != nullptr)
c906108c
SS
16026 {
16027 TYPE_LENGTH (type) = DW_UNSND (attr);
16028 }
16029 else
16030 {
16031 TYPE_LENGTH (type) = 0;
16032 }
16033
2b4424c3
TT
16034 maybe_set_alignment (cu, die, type);
16035
137033e9
JB
16036 /* The enumeration DIE can be incomplete. In Ada, any type can be
16037 declared as private in the package spec, and then defined only
16038 inside the package body. Such types are known as Taft Amendment
16039 Types. When another package uses such a type, an incomplete DIE
16040 may be generated by the compiler. */
02eb380e 16041 if (die_is_declaration (die, cu))
876cecd0 16042 TYPE_STUB (type) = 1;
02eb380e 16043
0626fc76
TT
16044 /* If this type has an underlying type that is not a stub, then we
16045 may use its attributes. We always use the "unsigned" attribute
16046 in this situation, because ordinarily we guess whether the type
16047 is unsigned -- but the guess can be wrong and the underlying type
16048 can tell us the reality. However, we defer to a local size
16049 attribute if one exists, because this lets the compiler override
16050 the underlying type if needed. */
16051 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16052 {
9e7c9a03
HD
16053 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16054 underlying_type = check_typedef (underlying_type);
16055 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16056 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16057 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16058 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16059 && TYPE_RAW_ALIGN (underlying_type) != 0)
16060 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16061 }
16062
3d567982
TT
16063 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16064
ed6acedd
TT
16065 set_die_type (die, type, cu);
16066
16067 /* Finish the creation of this type by using the enum's children.
16068 Note that, as usual, this must come after set_die_type to avoid
16069 infinite recursion when trying to compute the names of the
16070 enumerators. */
16071 update_enumeration_type_from_children (die, type, cu);
16072
16073 return type;
134d01f1
DJ
16074}
16075
16076/* Given a pointer to a die which begins an enumeration, process all
16077 the dies that define the members of the enumeration, and create the
16078 symbol for the enumeration type.
16079
16080 NOTE: We reverse the order of the element list. */
16081
16082static void
16083process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16084{
f792889a 16085 struct type *this_type;
134d01f1 16086
f792889a
DJ
16087 this_type = get_die_type (die, cu);
16088 if (this_type == NULL)
16089 this_type = read_enumeration_type (die, cu);
9dc481d3 16090
639d11d3 16091 if (die->child != NULL)
c906108c 16092 {
9dc481d3 16093 struct die_info *child_die;
15d034d0 16094 const char *name;
9dc481d3 16095
639d11d3 16096 child_die = die->child;
c906108c
SS
16097 while (child_die && child_die->tag)
16098 {
16099 if (child_die->tag != DW_TAG_enumerator)
16100 {
e7c27a73 16101 process_die (child_die, cu);
c906108c
SS
16102 }
16103 else
16104 {
39cbfefa
DJ
16105 name = dwarf2_name (child_die, cu);
16106 if (name)
ed6acedd 16107 new_symbol (child_die, this_type, cu);
c906108c
SS
16108 }
16109
436c571c 16110 child_die = child_die->sibling;
c906108c 16111 }
c906108c 16112 }
134d01f1 16113
6c83ed52
TT
16114 /* If we are reading an enum from a .debug_types unit, and the enum
16115 is a declaration, and the enum is not the signatured type in the
16116 unit, then we do not want to add a symbol for it. Adding a
16117 symbol would in some cases obscure the true definition of the
16118 enum, giving users an incomplete type when the definition is
16119 actually available. Note that we do not want to do this for all
16120 enums which are just declarations, because C++0x allows forward
16121 enum declarations. */
3019eac3 16122 if (cu->per_cu->is_debug_types
6c83ed52
TT
16123 && die_is_declaration (die, cu))
16124 {
52dc124a 16125 struct signatured_type *sig_type;
6c83ed52 16126
c0f78cd4 16127 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16128 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16129 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16130 return;
16131 }
16132
f792889a 16133 new_symbol (die, this_type, cu);
c906108c
SS
16134}
16135
16136/* Extract all information from a DW_TAG_array_type DIE and put it in
16137 the DIE's type field. For now, this only handles one dimensional
16138 arrays. */
16139
f792889a 16140static struct type *
e7c27a73 16141read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16142{
5e22e966 16143 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16144 struct die_info *child_die;
7e314c57 16145 struct type *type;
c906108c 16146 struct type *element_type, *range_type, *index_type;
c906108c 16147 struct attribute *attr;
15d034d0 16148 const char *name;
a405673c 16149 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16150 unsigned int bit_stride = 0;
c906108c 16151
e7c27a73 16152 element_type = die_type (die, cu);
c906108c 16153
7e314c57
JK
16154 /* The die_type call above may have already set the type for this DIE. */
16155 type = get_die_type (die, cu);
16156 if (type)
16157 return type;
16158
dc53a7ad
JB
16159 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16160 if (attr != NULL)
a405673c
JB
16161 {
16162 int stride_ok;
09ba997f 16163 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
16164
16165 byte_stride_prop
16166 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16167 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16168 prop_type);
a405673c
JB
16169 if (!stride_ok)
16170 {
b98664d3 16171 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16172 " - DIE at %s [in module %s]"),
16173 sect_offset_str (die->sect_off),
5e22e966 16174 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16175 /* Ignore this attribute. We will likely not be able to print
16176 arrays of this type correctly, but there is little we can do
16177 to help if we cannot read the attribute's value. */
16178 byte_stride_prop = NULL;
16179 }
16180 }
dc53a7ad
JB
16181
16182 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16183 if (attr != NULL)
16184 bit_stride = DW_UNSND (attr);
16185
c906108c
SS
16186 /* Irix 6.2 native cc creates array types without children for
16187 arrays with unspecified length. */
639d11d3 16188 if (die->child == NULL)
c906108c 16189 {
46bf5051 16190 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16191 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16192 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16193 byte_stride_prop, bit_stride);
f792889a 16194 return set_die_type (die, type, cu);
c906108c
SS
16195 }
16196
791afaa2 16197 std::vector<struct type *> range_types;
639d11d3 16198 child_die = die->child;
c906108c
SS
16199 while (child_die && child_die->tag)
16200 {
16201 if (child_die->tag == DW_TAG_subrange_type)
16202 {
f792889a 16203 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16204
f792889a 16205 if (child_type != NULL)
a02abb62 16206 {
0963b4bd
MS
16207 /* The range type was succesfully read. Save it for the
16208 array type creation. */
791afaa2 16209 range_types.push_back (child_type);
a02abb62 16210 }
c906108c 16211 }
436c571c 16212 child_die = child_die->sibling;
c906108c
SS
16213 }
16214
16215 /* Dwarf2 dimensions are output from left to right, create the
16216 necessary array types in backwards order. */
7ca2d3a3 16217
c906108c 16218 type = element_type;
7ca2d3a3
DL
16219
16220 if (read_array_order (die, cu) == DW_ORD_col_major)
16221 {
16222 int i = 0;
9a619af0 16223
791afaa2 16224 while (i < range_types.size ())
dc53a7ad 16225 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16226 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16227 }
16228 else
16229 {
791afaa2 16230 size_t ndim = range_types.size ();
7ca2d3a3 16231 while (ndim-- > 0)
dc53a7ad 16232 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16233 byte_stride_prop, bit_stride);
7ca2d3a3 16234 }
c906108c 16235
f5f8a009
EZ
16236 /* Understand Dwarf2 support for vector types (like they occur on
16237 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16238 array type. This is not part of the Dwarf2/3 standard yet, but a
16239 custom vendor extension. The main difference between a regular
16240 array and the vector variant is that vectors are passed by value
16241 to functions. */
e142c38c 16242 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16243 if (attr != nullptr)
ea37ba09 16244 make_vector_type (type);
f5f8a009 16245
dbc98a8b
KW
16246 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16247 implementation may choose to implement triple vectors using this
16248 attribute. */
16249 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16250 if (attr != nullptr)
dbc98a8b
KW
16251 {
16252 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16253 TYPE_LENGTH (type) = DW_UNSND (attr);
16254 else
b98664d3 16255 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16256 "than the total size of elements"));
dbc98a8b
KW
16257 }
16258
39cbfefa
DJ
16259 name = dwarf2_name (die, cu);
16260 if (name)
d0e39ea2 16261 type->set_name (name);
6e70227d 16262
2b4424c3
TT
16263 maybe_set_alignment (cu, die, type);
16264
0963b4bd 16265 /* Install the type in the die. */
7e314c57
JK
16266 set_die_type (die, type, cu);
16267
16268 /* set_die_type should be already done. */
b4ba55a1
JB
16269 set_descriptive_type (type, die, cu);
16270
7e314c57 16271 return type;
c906108c
SS
16272}
16273
7ca2d3a3 16274static enum dwarf_array_dim_ordering
6e70227d 16275read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16276{
16277 struct attribute *attr;
16278
16279 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16280
435d3d88 16281 if (attr != nullptr)
aead7601 16282 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16283
0963b4bd
MS
16284 /* GNU F77 is a special case, as at 08/2004 array type info is the
16285 opposite order to the dwarf2 specification, but data is still
16286 laid out as per normal fortran.
7ca2d3a3 16287
0963b4bd
MS
16288 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16289 version checking. */
7ca2d3a3 16290
905e0470
PM
16291 if (cu->language == language_fortran
16292 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16293 {
16294 return DW_ORD_row_major;
16295 }
16296
6e70227d 16297 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16298 {
16299 case array_column_major:
16300 return DW_ORD_col_major;
16301 case array_row_major:
16302 default:
16303 return DW_ORD_row_major;
16304 };
16305}
16306
72019c9c 16307/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16308 the DIE's type field. */
72019c9c 16309
f792889a 16310static struct type *
72019c9c
GM
16311read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16312{
7e314c57
JK
16313 struct type *domain_type, *set_type;
16314 struct attribute *attr;
f792889a 16315
7e314c57
JK
16316 domain_type = die_type (die, cu);
16317
16318 /* The die_type call above may have already set the type for this DIE. */
16319 set_type = get_die_type (die, cu);
16320 if (set_type)
16321 return set_type;
16322
16323 set_type = create_set_type (NULL, domain_type);
16324
16325 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16326 if (attr != nullptr)
d09039dd 16327 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16328
2b4424c3
TT
16329 maybe_set_alignment (cu, die, set_type);
16330
f792889a 16331 return set_die_type (die, set_type, cu);
72019c9c 16332}
7ca2d3a3 16333
0971de02
TT
16334/* A helper for read_common_block that creates a locexpr baton.
16335 SYM is the symbol which we are marking as computed.
16336 COMMON_DIE is the DIE for the common block.
16337 COMMON_LOC is the location expression attribute for the common
16338 block itself.
16339 MEMBER_LOC is the location expression attribute for the particular
16340 member of the common block that we are processing.
16341 CU is the CU from which the above come. */
16342
16343static void
16344mark_common_block_symbol_computed (struct symbol *sym,
16345 struct die_info *common_die,
16346 struct attribute *common_loc,
16347 struct attribute *member_loc,
16348 struct dwarf2_cu *cu)
16349{
5e22e966 16350 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16351 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16352 struct dwarf2_locexpr_baton *baton;
16353 gdb_byte *ptr;
16354 unsigned int cu_off;
08feed99 16355 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16356 LONGEST offset = 0;
16357
16358 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16359 gdb_assert (common_loc->form_is_block ());
16360 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16361 || member_loc->form_is_constant ());
0971de02 16362
8d749320 16363 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16364 baton->per_objfile = per_objfile;
0971de02
TT
16365 baton->per_cu = cu->per_cu;
16366 gdb_assert (baton->per_cu);
16367
16368 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16369
cd6c91b4 16370 if (member_loc->form_is_constant ())
0971de02 16371 {
0826b30a 16372 offset = member_loc->constant_value (0);
0971de02
TT
16373 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16374 }
16375 else
16376 baton->size += DW_BLOCK (member_loc)->size;
16377
224c3ddb 16378 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16379 baton->data = ptr;
16380
16381 *ptr++ = DW_OP_call4;
9c541725 16382 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16383 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16384 ptr += 4;
16385
cd6c91b4 16386 if (member_loc->form_is_constant ())
0971de02
TT
16387 {
16388 *ptr++ = DW_OP_addr;
16389 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16390 ptr += cu->header.addr_size;
16391 }
16392 else
16393 {
16394 /* We have to copy the data here, because DW_OP_call4 will only
16395 use a DW_AT_location attribute. */
16396 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16397 ptr += DW_BLOCK (member_loc)->size;
16398 }
16399
16400 *ptr++ = DW_OP_plus;
16401 gdb_assert (ptr - baton->data == baton->size);
16402
0971de02 16403 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16404 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16405}
16406
4357ac6c
TT
16407/* Create appropriate locally-scoped variables for all the
16408 DW_TAG_common_block entries. Also create a struct common_block
16409 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16410 is used to separate the common blocks name namespace from regular
4357ac6c 16411 variable names. */
c906108c
SS
16412
16413static void
e7c27a73 16414read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16415{
0971de02
TT
16416 struct attribute *attr;
16417
16418 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16419 if (attr != nullptr)
0971de02
TT
16420 {
16421 /* Support the .debug_loc offsets. */
4fc6c0d5 16422 if (attr->form_is_block ())
0971de02
TT
16423 {
16424 /* Ok. */
16425 }
cd6c91b4 16426 else if (attr->form_is_section_offset ())
0971de02
TT
16427 {
16428 dwarf2_complex_location_expr_complaint ();
16429 attr = NULL;
16430 }
16431 else
16432 {
16433 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16434 "common block member");
16435 attr = NULL;
16436 }
16437 }
16438
639d11d3 16439 if (die->child != NULL)
c906108c 16440 {
5e22e966 16441 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16442 struct die_info *child_die;
16443 size_t n_entries = 0, size;
16444 struct common_block *common_block;
16445 struct symbol *sym;
74ac6d43 16446
4357ac6c
TT
16447 for (child_die = die->child;
16448 child_die && child_die->tag;
436c571c 16449 child_die = child_die->sibling)
4357ac6c
TT
16450 ++n_entries;
16451
16452 size = (sizeof (struct common_block)
16453 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16454 common_block
16455 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16456 size);
4357ac6c
TT
16457 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16458 common_block->n_entries = 0;
16459
16460 for (child_die = die->child;
16461 child_die && child_die->tag;
436c571c 16462 child_die = child_die->sibling)
4357ac6c
TT
16463 {
16464 /* Create the symbol in the DW_TAG_common_block block in the current
16465 symbol scope. */
e7c27a73 16466 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16467 if (sym != NULL)
16468 {
16469 struct attribute *member_loc;
16470
16471 common_block->contents[common_block->n_entries++] = sym;
16472
16473 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16474 cu);
16475 if (member_loc)
16476 {
16477 /* GDB has handled this for a long time, but it is
16478 not specified by DWARF. It seems to have been
16479 emitted by gfortran at least as recently as:
16480 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16481 complaint (_("Variable in common block has "
0971de02 16482 "DW_AT_data_member_location "
9d8780f0
SM
16483 "- DIE at %s [in module %s]"),
16484 sect_offset_str (child_die->sect_off),
518817b3 16485 objfile_name (objfile));
0971de02 16486
cd6c91b4 16487 if (member_loc->form_is_section_offset ())
0971de02 16488 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16489 else if (member_loc->form_is_constant ()
4fc6c0d5 16490 || member_loc->form_is_block ())
0971de02 16491 {
435d3d88 16492 if (attr != nullptr)
0971de02
TT
16493 mark_common_block_symbol_computed (sym, die, attr,
16494 member_loc, cu);
16495 }
16496 else
16497 dwarf2_complex_location_expr_complaint ();
16498 }
16499 }
c906108c 16500 }
4357ac6c
TT
16501
16502 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16503 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16504 }
16505}
16506
0114d602 16507/* Create a type for a C++ namespace. */
d9fa45fe 16508
0114d602
DJ
16509static struct type *
16510read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16511{
5e22e966 16512 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16513 const char *previous_prefix, *name;
9219021c 16514 int is_anonymous;
0114d602
DJ
16515 struct type *type;
16516
16517 /* For extensions, reuse the type of the original namespace. */
16518 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16519 {
16520 struct die_info *ext_die;
16521 struct dwarf2_cu *ext_cu = cu;
9a619af0 16522
0114d602
DJ
16523 ext_die = dwarf2_extension (die, &ext_cu);
16524 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16525
16526 /* EXT_CU may not be the same as CU.
02142a6c 16527 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16528 return set_die_type (die, type, cu);
16529 }
9219021c 16530
e142c38c 16531 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16532
16533 /* Now build the name of the current namespace. */
16534
0114d602
DJ
16535 previous_prefix = determine_prefix (die, cu);
16536 if (previous_prefix[0] != '\0')
16537 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16538 previous_prefix, name, 0, cu);
0114d602
DJ
16539
16540 /* Create the type. */
19f392bc 16541 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16542
60531b24 16543 return set_die_type (die, type, cu);
0114d602
DJ
16544}
16545
22cee43f 16546/* Read a namespace scope. */
0114d602
DJ
16547
16548static void
16549read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16550{
5e22e966 16551 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16552 int is_anonymous;
9219021c 16553
5c4e30ca
DC
16554 /* Add a symbol associated to this if we haven't seen the namespace
16555 before. Also, add a using directive if it's an anonymous
16556 namespace. */
9219021c 16557
f2f0e013 16558 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16559 {
16560 struct type *type;
16561
0114d602 16562 type = read_type_die (die, cu);
e7c27a73 16563 new_symbol (die, type, cu);
5c4e30ca 16564
e8e80198 16565 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16566 if (is_anonymous)
0114d602
DJ
16567 {
16568 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16569
eb1e02fd 16570 std::vector<const char *> excludes;
804d2729 16571 add_using_directive (using_directives (cu),
7d93a1e0 16572 previous_prefix, type->name (), NULL,
eb1e02fd 16573 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16574 }
5c4e30ca 16575 }
9219021c 16576
639d11d3 16577 if (die->child != NULL)
d9fa45fe 16578 {
639d11d3 16579 struct die_info *child_die = die->child;
6e70227d 16580
d9fa45fe
DC
16581 while (child_die && child_die->tag)
16582 {
e7c27a73 16583 process_die (child_die, cu);
436c571c 16584 child_die = child_die->sibling;
d9fa45fe
DC
16585 }
16586 }
38d518c9
EZ
16587}
16588
f55ee35c
JK
16589/* Read a Fortran module as type. This DIE can be only a declaration used for
16590 imported module. Still we need that type as local Fortran "use ... only"
16591 declaration imports depend on the created type in determine_prefix. */
16592
16593static struct type *
16594read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16595{
5e22e966 16596 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16597 const char *module_name;
f55ee35c
JK
16598 struct type *type;
16599
16600 module_name = dwarf2_name (die, cu);
19f392bc 16601 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16602
f55ee35c
JK
16603 return set_die_type (die, type, cu);
16604}
16605
5d7cb8df
JK
16606/* Read a Fortran module. */
16607
16608static void
16609read_module (struct die_info *die, struct dwarf2_cu *cu)
16610{
16611 struct die_info *child_die = die->child;
530e8392
KB
16612 struct type *type;
16613
16614 type = read_type_die (die, cu);
16615 new_symbol (die, type, cu);
5d7cb8df 16616
5d7cb8df
JK
16617 while (child_die && child_die->tag)
16618 {
16619 process_die (child_die, cu);
436c571c 16620 child_die = child_die->sibling;
5d7cb8df
JK
16621 }
16622}
16623
38d518c9
EZ
16624/* Return the name of the namespace represented by DIE. Set
16625 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16626 namespace. */
16627
16628static const char *
e142c38c 16629namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16630{
16631 struct die_info *current_die;
16632 const char *name = NULL;
16633
16634 /* Loop through the extensions until we find a name. */
16635
16636 for (current_die = die;
16637 current_die != NULL;
f2f0e013 16638 current_die = dwarf2_extension (die, &cu))
38d518c9 16639 {
96553a0c
DE
16640 /* We don't use dwarf2_name here so that we can detect the absence
16641 of a name -> anonymous namespace. */
7d45c7c3 16642 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16643
38d518c9
EZ
16644 if (name != NULL)
16645 break;
16646 }
16647
16648 /* Is it an anonymous namespace? */
16649
16650 *is_anonymous = (name == NULL);
16651 if (*is_anonymous)
2b1dbab0 16652 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16653
16654 return name;
d9fa45fe
DC
16655}
16656
c906108c
SS
16657/* Extract all information from a DW_TAG_pointer_type DIE and add to
16658 the user defined type vector. */
16659
f792889a 16660static struct type *
e7c27a73 16661read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16662{
5e22e966 16663 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16664 struct comp_unit_head *cu_header = &cu->header;
c906108c 16665 struct type *type;
8b2dbe47
KB
16666 struct attribute *attr_byte_size;
16667 struct attribute *attr_address_class;
16668 int byte_size, addr_class;
7e314c57
JK
16669 struct type *target_type;
16670
16671 target_type = die_type (die, cu);
c906108c 16672
7e314c57
JK
16673 /* The die_type call above may have already set the type for this DIE. */
16674 type = get_die_type (die, cu);
16675 if (type)
16676 return type;
16677
16678 type = lookup_pointer_type (target_type);
8b2dbe47 16679
e142c38c 16680 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16681 if (attr_byte_size)
16682 byte_size = DW_UNSND (attr_byte_size);
c906108c 16683 else
8b2dbe47
KB
16684 byte_size = cu_header->addr_size;
16685
e142c38c 16686 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16687 if (attr_address_class)
16688 addr_class = DW_UNSND (attr_address_class);
16689 else
16690 addr_class = DW_ADDR_none;
16691
2b4424c3
TT
16692 ULONGEST alignment = get_alignment (cu, die);
16693
16694 /* If the pointer size, alignment, or address class is different
16695 than the default, create a type variant marked as such and set
16696 the length accordingly. */
16697 if (TYPE_LENGTH (type) != byte_size
16698 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16699 && alignment != TYPE_RAW_ALIGN (type))
16700 || addr_class != DW_ADDR_none)
c906108c 16701 {
5e2b427d 16702 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16703 {
16704 int type_flags;
16705
849957d9 16706 type_flags = gdbarch_address_class_type_flags
5e2b427d 16707 (gdbarch, byte_size, addr_class);
876cecd0
TT
16708 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16709 == 0);
8b2dbe47
KB
16710 type = make_type_with_address_space (type, type_flags);
16711 }
16712 else if (TYPE_LENGTH (type) != byte_size)
16713 {
b98664d3 16714 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16715 }
2b4424c3
TT
16716 else if (TYPE_RAW_ALIGN (type) != alignment)
16717 {
b98664d3 16718 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16719 " - DIE at %s [in module %s]"),
16720 sect_offset_str (die->sect_off),
5e22e966 16721 objfile_name (cu->per_objfile->objfile));
2b4424c3 16722 }
6e70227d 16723 else
9a619af0
MS
16724 {
16725 /* Should we also complain about unhandled address classes? */
16726 }
c906108c 16727 }
8b2dbe47
KB
16728
16729 TYPE_LENGTH (type) = byte_size;
2b4424c3 16730 set_type_align (type, alignment);
f792889a 16731 return set_die_type (die, type, cu);
c906108c
SS
16732}
16733
16734/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16735 the user defined type vector. */
16736
f792889a 16737static struct type *
e7c27a73 16738read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16739{
16740 struct type *type;
16741 struct type *to_type;
16742 struct type *domain;
16743
e7c27a73
DJ
16744 to_type = die_type (die, cu);
16745 domain = die_containing_type (die, cu);
0d5de010 16746
7e314c57
JK
16747 /* The calls above may have already set the type for this DIE. */
16748 type = get_die_type (die, cu);
16749 if (type)
16750 return type;
16751
78134374 16752 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16753 type = lookup_methodptr_type (to_type);
78134374 16754 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16755 {
5e22e966 16756 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16757
16758 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16759 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16760 TYPE_VARARGS (to_type));
16761 type = lookup_methodptr_type (new_type);
16762 }
0d5de010
DJ
16763 else
16764 type = lookup_memberptr_type (to_type, domain);
c906108c 16765
f792889a 16766 return set_die_type (die, type, cu);
c906108c
SS
16767}
16768
4297a3f0 16769/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16770 the user defined type vector. */
16771
f792889a 16772static struct type *
4297a3f0
AV
16773read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16774 enum type_code refcode)
c906108c 16775{
e7c27a73 16776 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16777 struct type *type, *target_type;
c906108c
SS
16778 struct attribute *attr;
16779
4297a3f0
AV
16780 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16781
7e314c57
JK
16782 target_type = die_type (die, cu);
16783
16784 /* The die_type call above may have already set the type for this DIE. */
16785 type = get_die_type (die, cu);
16786 if (type)
16787 return type;
16788
4297a3f0 16789 type = lookup_reference_type (target_type, refcode);
e142c38c 16790 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16791 if (attr != nullptr)
c906108c
SS
16792 {
16793 TYPE_LENGTH (type) = DW_UNSND (attr);
16794 }
16795 else
16796 {
107d2387 16797 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16798 }
2b4424c3 16799 maybe_set_alignment (cu, die, type);
f792889a 16800 return set_die_type (die, type, cu);
c906108c
SS
16801}
16802
cf363f18
MW
16803/* Add the given cv-qualifiers to the element type of the array. GCC
16804 outputs DWARF type qualifiers that apply to an array, not the
16805 element type. But GDB relies on the array element type to carry
16806 the cv-qualifiers. This mimics section 6.7.3 of the C99
16807 specification. */
16808
16809static struct type *
16810add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16811 struct type *base_type, int cnst, int voltl)
16812{
16813 struct type *el_type, *inner_array;
16814
16815 base_type = copy_type (base_type);
16816 inner_array = base_type;
16817
78134374 16818 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16819 {
16820 TYPE_TARGET_TYPE (inner_array) =
16821 copy_type (TYPE_TARGET_TYPE (inner_array));
16822 inner_array = TYPE_TARGET_TYPE (inner_array);
16823 }
16824
16825 el_type = TYPE_TARGET_TYPE (inner_array);
16826 cnst |= TYPE_CONST (el_type);
16827 voltl |= TYPE_VOLATILE (el_type);
16828 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16829
16830 return set_die_type (die, base_type, cu);
16831}
16832
f792889a 16833static struct type *
e7c27a73 16834read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16835{
f792889a 16836 struct type *base_type, *cv_type;
c906108c 16837
e7c27a73 16838 base_type = die_type (die, cu);
7e314c57
JK
16839
16840 /* The die_type call above may have already set the type for this DIE. */
16841 cv_type = get_die_type (die, cu);
16842 if (cv_type)
16843 return cv_type;
16844
2f608a3a
KW
16845 /* In case the const qualifier is applied to an array type, the element type
16846 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16847 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16848 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16849
f792889a
DJ
16850 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16851 return set_die_type (die, cv_type, cu);
c906108c
SS
16852}
16853
f792889a 16854static struct type *
e7c27a73 16855read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16856{
f792889a 16857 struct type *base_type, *cv_type;
c906108c 16858
e7c27a73 16859 base_type = die_type (die, cu);
7e314c57
JK
16860
16861 /* The die_type call above may have already set the type for this DIE. */
16862 cv_type = get_die_type (die, cu);
16863 if (cv_type)
16864 return cv_type;
16865
cf363f18
MW
16866 /* In case the volatile qualifier is applied to an array type, the
16867 element type is so qualified, not the array type (section 6.7.3
16868 of C99). */
78134374 16869 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16870 return add_array_cv_type (die, cu, base_type, 0, 1);
16871
f792889a
DJ
16872 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16873 return set_die_type (die, cv_type, cu);
c906108c
SS
16874}
16875
06d66ee9
TT
16876/* Handle DW_TAG_restrict_type. */
16877
16878static struct type *
16879read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16880{
16881 struct type *base_type, *cv_type;
16882
16883 base_type = die_type (die, cu);
16884
16885 /* The die_type call above may have already set the type for this DIE. */
16886 cv_type = get_die_type (die, cu);
16887 if (cv_type)
16888 return cv_type;
16889
16890 cv_type = make_restrict_type (base_type);
16891 return set_die_type (die, cv_type, cu);
16892}
16893
a2c2acaf
MW
16894/* Handle DW_TAG_atomic_type. */
16895
16896static struct type *
16897read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16898{
16899 struct type *base_type, *cv_type;
16900
16901 base_type = die_type (die, cu);
16902
16903 /* The die_type call above may have already set the type for this DIE. */
16904 cv_type = get_die_type (die, cu);
16905 if (cv_type)
16906 return cv_type;
16907
16908 cv_type = make_atomic_type (base_type);
16909 return set_die_type (die, cv_type, cu);
16910}
16911
c906108c
SS
16912/* Extract all information from a DW_TAG_string_type DIE and add to
16913 the user defined type vector. It isn't really a user defined type,
16914 but it behaves like one, with other DIE's using an AT_user_def_type
16915 attribute to reference it. */
16916
f792889a 16917static struct type *
e7c27a73 16918read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16919{
5e22e966 16920 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16921 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16922 struct type *type, *range_type, *index_type, *char_type;
16923 struct attribute *attr;
216a7e6b
AB
16924 struct dynamic_prop prop;
16925 bool length_is_constant = true;
16926 LONGEST length;
16927
16928 /* There are a couple of places where bit sizes might be made use of
16929 when parsing a DW_TAG_string_type, however, no producer that we know
16930 of make use of these. Handling bit sizes that are a multiple of the
16931 byte size is easy enough, but what about other bit sizes? Lets deal
16932 with that problem when we have to. Warn about these attributes being
16933 unsupported, then parse the type and ignore them like we always
16934 have. */
16935 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16936 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16937 {
16938 static bool warning_printed = false;
16939 if (!warning_printed)
16940 {
16941 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16942 "currently supported on DW_TAG_string_type."));
16943 warning_printed = true;
16944 }
16945 }
c906108c 16946
e142c38c 16947 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16948 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16949 {
16950 /* The string length describes the location at which the length of
16951 the string can be found. The size of the length field can be
16952 specified with one of the attributes below. */
16953 struct type *prop_type;
16954 struct attribute *len
16955 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16956 if (len == nullptr)
16957 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16958 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16959 {
16960 /* Pass 0 as the default as we know this attribute is constant
16961 and the default value will not be returned. */
0826b30a 16962 LONGEST sz = len->constant_value (0);
09ba997f 16963 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16964 }
16965 else
16966 {
16967 /* If the size is not specified then we assume it is the size of
16968 an address on this target. */
09ba997f 16969 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16970 }
16971
16972 /* Convert the attribute into a dynamic property. */
16973 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16974 length = 1;
16975 else
16976 length_is_constant = false;
16977 }
16978 else if (attr != nullptr)
16979 {
16980 /* This DW_AT_string_length just contains the length with no
16981 indirection. There's no need to create a dynamic property in this
16982 case. Pass 0 for the default value as we know it will not be
16983 returned in this case. */
0826b30a 16984 length = attr->constant_value (0);
216a7e6b
AB
16985 }
16986 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16987 {
216a7e6b 16988 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16989 length = attr->constant_value (1);
c906108c
SS
16990 }
16991 else
16992 {
216a7e6b
AB
16993 /* Use 1 as a fallback length if we have nothing else. */
16994 length = 1;
c906108c 16995 }
6ccb9162 16996
46bf5051 16997 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16998 if (length_is_constant)
16999 range_type = create_static_range_type (NULL, index_type, 1, length);
17000 else
17001 {
17002 struct dynamic_prop low_bound;
17003
17004 low_bound.kind = PROP_CONST;
17005 low_bound.data.const_val = 1;
17006 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17007 }
3b7538c0
UW
17008 char_type = language_string_char_type (cu->language_defn, gdbarch);
17009 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17010
f792889a 17011 return set_die_type (die, type, cu);
c906108c
SS
17012}
17013
4d804846
JB
17014/* Assuming that DIE corresponds to a function, returns nonzero
17015 if the function is prototyped. */
17016
17017static int
17018prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17019{
17020 struct attribute *attr;
17021
17022 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17023 if (attr && (DW_UNSND (attr) != 0))
17024 return 1;
17025
17026 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17027 is only meaningful for C, but the concept also extends to other
4d804846
JB
17028 languages that allow unprototyped functions (Eg: Objective C).
17029 For all other languages, assume that functions are always
17030 prototyped. */
17031 if (cu->language != language_c
17032 && cu->language != language_objc
17033 && cu->language != language_opencl)
17034 return 1;
17035
17036 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17037 prototyped and unprototyped functions; default to prototyped,
17038 since that is more common in modern code (and RealView warns
17039 about unprototyped functions). */
17040 if (producer_is_realview (cu->producer))
17041 return 1;
17042
17043 return 0;
17044}
17045
c906108c
SS
17046/* Handle DIES due to C code like:
17047
17048 struct foo
c5aa993b
JM
17049 {
17050 int (*funcp)(int a, long l);
17051 int b;
17052 };
c906108c 17053
0963b4bd 17054 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17055
f792889a 17056static struct type *
e7c27a73 17057read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17058{
5e22e966 17059 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17060 struct type *type; /* Type that this function returns. */
17061 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17062 struct attribute *attr;
17063
e7c27a73 17064 type = die_type (die, cu);
7e314c57
JK
17065
17066 /* The die_type call above may have already set the type for this DIE. */
17067 ftype = get_die_type (die, cu);
17068 if (ftype)
17069 return ftype;
17070
0c8b41f1 17071 ftype = lookup_function_type (type);
c906108c 17072
4d804846 17073 if (prototyped_function_p (die, cu))
a6c727b2 17074 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17075
c055b101
CV
17076 /* Store the calling convention in the type if it's available in
17077 the subroutine die. Otherwise set the calling convention to
17078 the default value DW_CC_normal. */
17079 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17080 if (attr != nullptr
17081 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17082 TYPE_CALLING_CONVENTION (ftype)
17083 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17084 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17085 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17086 else
17087 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17088
743649fd
MW
17089 /* Record whether the function returns normally to its caller or not
17090 if the DWARF producer set that information. */
17091 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17092 if (attr && (DW_UNSND (attr) != 0))
17093 TYPE_NO_RETURN (ftype) = 1;
17094
76c10ea2
GM
17095 /* We need to add the subroutine type to the die immediately so
17096 we don't infinitely recurse when dealing with parameters
0963b4bd 17097 declared as the same subroutine type. */
76c10ea2 17098 set_die_type (die, ftype, cu);
6e70227d 17099
639d11d3 17100 if (die->child != NULL)
c906108c 17101 {
bb5ed363 17102 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17103 struct die_info *child_die;
8072405b 17104 int nparams, iparams;
c906108c
SS
17105
17106 /* Count the number of parameters.
17107 FIXME: GDB currently ignores vararg functions, but knows about
17108 vararg member functions. */
8072405b 17109 nparams = 0;
639d11d3 17110 child_die = die->child;
c906108c
SS
17111 while (child_die && child_die->tag)
17112 {
17113 if (child_die->tag == DW_TAG_formal_parameter)
17114 nparams++;
17115 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17116 TYPE_VARARGS (ftype) = 1;
436c571c 17117 child_die = child_die->sibling;
c906108c
SS
17118 }
17119
17120 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17121 ftype->set_num_fields (nparams);
3cabb6b0
SM
17122 ftype->set_fields
17123 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17124
8072405b
JK
17125 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17126 even if we error out during the parameters reading below. */
17127 for (iparams = 0; iparams < nparams; iparams++)
17128 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17129
17130 iparams = 0;
639d11d3 17131 child_die = die->child;
c906108c
SS
17132 while (child_die && child_die->tag)
17133 {
17134 if (child_die->tag == DW_TAG_formal_parameter)
17135 {
3ce3b1ba
PA
17136 struct type *arg_type;
17137
17138 /* DWARF version 2 has no clean way to discern C++
17139 static and non-static member functions. G++ helps
17140 GDB by marking the first parameter for non-static
17141 member functions (which is the this pointer) as
17142 artificial. We pass this information to
17143 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17144
17145 DWARF version 3 added DW_AT_object_pointer, which GCC
17146 4.5 does not yet generate. */
e142c38c 17147 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17148 if (attr != nullptr)
c906108c
SS
17149 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17150 else
9c37b5ae 17151 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17152 arg_type = die_type (child_die, cu);
17153
17154 /* RealView does not mark THIS as const, which the testsuite
17155 expects. GCC marks THIS as const in method definitions,
17156 but not in the class specifications (GCC PR 43053). */
17157 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17158 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17159 {
17160 int is_this = 0;
17161 struct dwarf2_cu *arg_cu = cu;
17162 const char *name = dwarf2_name (child_die, cu);
17163
17164 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17165 if (attr != nullptr)
3ce3b1ba
PA
17166 {
17167 /* If the compiler emits this, use it. */
17168 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17169 is_this = 1;
17170 }
17171 else if (name && strcmp (name, "this") == 0)
17172 /* Function definitions will have the argument names. */
17173 is_this = 1;
17174 else if (name == NULL && iparams == 0)
17175 /* Declarations may not have the names, so like
17176 elsewhere in GDB, assume an artificial first
17177 argument is "this". */
17178 is_this = 1;
17179
17180 if (is_this)
17181 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17182 arg_type, 0);
17183 }
17184
17185 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17186 iparams++;
17187 }
436c571c 17188 child_die = child_die->sibling;
c906108c
SS
17189 }
17190 }
17191
76c10ea2 17192 return ftype;
c906108c
SS
17193}
17194
f792889a 17195static struct type *
e7c27a73 17196read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17197{
5e22e966 17198 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17199 const char *name = NULL;
3c8e0968 17200 struct type *this_type, *target_type;
c906108c 17201
94af9270 17202 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17203 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17204 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17205 set_die_type (die, this_type, cu);
3c8e0968
DE
17206 target_type = die_type (die, cu);
17207 if (target_type != this_type)
17208 TYPE_TARGET_TYPE (this_type) = target_type;
17209 else
17210 {
17211 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17212 spec and cause infinite loops in GDB. */
b98664d3 17213 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17214 "- DIE at %s [in module %s]"),
17215 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17216 TYPE_TARGET_TYPE (this_type) = NULL;
17217 }
e4003a34
TV
17218 if (name == NULL)
17219 {
17220 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17221 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17222 Handle these by just returning the target type, rather than
17223 constructing an anonymous typedef type and trying to handle this
17224 elsewhere. */
17225 set_die_type (die, target_type, cu);
17226 return target_type;
17227 }
f792889a 17228 return this_type;
c906108c
SS
17229}
17230
9b790ce7
UW
17231/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17232 (which may be different from NAME) to the architecture back-end to allow
17233 it to guess the correct format if necessary. */
17234
17235static struct type *
17236dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17237 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17238{
08feed99 17239 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17240 const struct floatformat **format;
17241 struct type *type;
17242
17243 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17244 if (format)
103a685e 17245 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17246 else
77b7c781 17247 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17248
17249 return type;
17250}
17251
eb77c9df
AB
17252/* Allocate an integer type of size BITS and name NAME. */
17253
17254static struct type *
17255dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17256 int bits, int unsigned_p, const char *name)
17257{
17258 struct type *type;
17259
17260 /* Versions of Intel's C Compiler generate an integer type called "void"
17261 instead of using DW_TAG_unspecified_type. This has been seen on
17262 at least versions 14, 17, and 18. */
35ee2dc2
AB
17263 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17264 && strcmp (name, "void") == 0)
eb77c9df
AB
17265 type = objfile_type (objfile)->builtin_void;
17266 else
17267 type = init_integer_type (objfile, bits, unsigned_p, name);
17268
17269 return type;
17270}
17271
8bdc1658
AB
17272/* Initialise and return a floating point type of size BITS suitable for
17273 use as a component of a complex number. The NAME_HINT is passed through
17274 when initialising the floating point type and is the name of the complex
17275 type.
17276
17277 As DWARF doesn't currently provide an explicit name for the components
17278 of a complex number, but it can be helpful to have these components
17279 named, we try to select a suitable name based on the size of the
17280 component. */
17281static struct type *
17282dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17283 struct objfile *objfile,
103a685e
TT
17284 int bits, const char *name_hint,
17285 enum bfd_endian byte_order)
8bdc1658 17286{
08feed99 17287 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17288 struct type *tt = nullptr;
17289
35add35e
AB
17290 /* Try to find a suitable floating point builtin type of size BITS.
17291 We're going to use the name of this type as the name for the complex
17292 target type that we are about to create. */
1db455a7 17293 switch (cu->language)
8bdc1658 17294 {
1db455a7
AB
17295 case language_fortran:
17296 switch (bits)
17297 {
17298 case 32:
17299 tt = builtin_f_type (gdbarch)->builtin_real;
17300 break;
17301 case 64:
17302 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17303 break;
17304 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17305 case 128:
17306 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17307 break;
17308 }
8bdc1658 17309 break;
1db455a7
AB
17310 default:
17311 switch (bits)
17312 {
17313 case 32:
17314 tt = builtin_type (gdbarch)->builtin_float;
17315 break;
17316 case 64:
17317 tt = builtin_type (gdbarch)->builtin_double;
17318 break;
17319 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17320 case 128:
17321 tt = builtin_type (gdbarch)->builtin_long_double;
17322 break;
17323 }
8bdc1658
AB
17324 break;
17325 }
17326
35add35e
AB
17327 /* If the type we found doesn't match the size we were looking for, then
17328 pretend we didn't find a type at all, the complex target type we
17329 create will then be nameless. */
a12e5744 17330 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17331 tt = nullptr;
17332
7d93a1e0 17333 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17334 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17335}
17336
c906108c
SS
17337/* Find a representation of a given base type and install
17338 it in the TYPE field of the die. */
17339
f792889a 17340static struct type *
e7c27a73 17341read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17342{
5e22e966 17343 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17344 struct type *type;
17345 struct attribute *attr;
19f392bc 17346 int encoding = 0, bits = 0;
15d034d0 17347 const char *name;
34877895 17348 gdbarch *arch;
c906108c 17349
e142c38c 17350 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17351 if (attr != nullptr)
34877895 17352 encoding = DW_UNSND (attr);
e142c38c 17353 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17354 if (attr != nullptr)
34877895 17355 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17356 name = dwarf2_name (die, cu);
6ccb9162 17357 if (!name)
34877895 17358 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17359
08feed99 17360 arch = objfile->arch ();
103a685e
TT
17361 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17362
34877895
PJ
17363 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17364 if (attr)
103a685e
TT
17365 {
17366 int endianity = DW_UNSND (attr);
17367
17368 switch (endianity)
17369 {
17370 case DW_END_big:
17371 byte_order = BFD_ENDIAN_BIG;
17372 break;
17373 case DW_END_little:
17374 byte_order = BFD_ENDIAN_LITTLE;
17375 break;
17376 default:
17377 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17378 break;
17379 }
17380 }
6ccb9162
UW
17381
17382 switch (encoding)
c906108c 17383 {
6ccb9162
UW
17384 case DW_ATE_address:
17385 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17386 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17387 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17388 break;
17389 case DW_ATE_boolean:
19f392bc 17390 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17391 break;
17392 case DW_ATE_complex_float:
103a685e
TT
17393 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17394 byte_order);
78134374 17395 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17396 {
17397 if (name == nullptr)
17398 {
17399 struct obstack *obstack
5e22e966 17400 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17401 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17402 nullptr);
17403 }
17404 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17405 }
17406 else
17407 type = init_complex_type (name, type);
6ccb9162
UW
17408 break;
17409 case DW_ATE_decimal_float:
19f392bc 17410 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17411 break;
17412 case DW_ATE_float:
103a685e 17413 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17414 break;
17415 case DW_ATE_signed:
eb77c9df 17416 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17417 break;
17418 case DW_ATE_unsigned:
3b2b8fea
TT
17419 if (cu->language == language_fortran
17420 && name
61012eef 17421 && startswith (name, "character("))
19f392bc
UW
17422 type = init_character_type (objfile, bits, 1, name);
17423 else
eb77c9df 17424 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17425 break;
17426 case DW_ATE_signed_char:
6e70227d 17427 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17428 || cu->language == language_pascal
17429 || cu->language == language_fortran)
19f392bc
UW
17430 type = init_character_type (objfile, bits, 0, name);
17431 else
eb77c9df 17432 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17433 break;
17434 case DW_ATE_unsigned_char:
868a0084 17435 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17436 || cu->language == language_pascal
c44af4eb
TT
17437 || cu->language == language_fortran
17438 || cu->language == language_rust)
19f392bc
UW
17439 type = init_character_type (objfile, bits, 1, name);
17440 else
eb77c9df 17441 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17442 break;
75079b2b 17443 case DW_ATE_UTF:
53e710ac 17444 {
53e710ac
PA
17445 if (bits == 16)
17446 type = builtin_type (arch)->builtin_char16;
17447 else if (bits == 32)
17448 type = builtin_type (arch)->builtin_char32;
17449 else
17450 {
b98664d3 17451 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17452 bits);
eb77c9df 17453 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17454 }
17455 return set_die_type (die, type, cu);
17456 }
75079b2b
TT
17457 break;
17458
6ccb9162 17459 default:
b98664d3 17460 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17461 dwarf_type_encoding_name (encoding));
77b7c781 17462 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17463 break;
c906108c 17464 }
6ccb9162 17465
0114d602 17466 if (name && strcmp (name, "char") == 0)
876cecd0 17467 TYPE_NOSIGN (type) = 1;
0114d602 17468
2b4424c3
TT
17469 maybe_set_alignment (cu, die, type);
17470
103a685e 17471 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17472
f792889a 17473 return set_die_type (die, type, cu);
c906108c
SS
17474}
17475
80180f79
SA
17476/* Parse dwarf attribute if it's a block, reference or constant and put the
17477 resulting value of the attribute into struct bound_prop.
17478 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17479
17480static int
17481attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17482 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17483 struct type *default_type)
80180f79
SA
17484{
17485 struct dwarf2_property_baton *baton;
5e22e966 17486 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17487 struct objfile *objfile = per_objfile->objfile;
17488 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17489
9a49df9d
AB
17490 gdb_assert (default_type != NULL);
17491
80180f79
SA
17492 if (attr == NULL || prop == NULL)
17493 return 0;
17494
4fc6c0d5 17495 if (attr->form_is_block ())
80180f79 17496 {
8d749320 17497 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17498 baton->property_type = default_type;
80180f79 17499 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17500 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17501 baton->locexpr.size = DW_BLOCK (attr)->size;
17502 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17503 switch (attr->name)
17504 {
17505 case DW_AT_string_length:
17506 baton->locexpr.is_reference = true;
17507 break;
17508 default:
17509 baton->locexpr.is_reference = false;
17510 break;
17511 }
80180f79
SA
17512 prop->data.baton = baton;
17513 prop->kind = PROP_LOCEXPR;
17514 gdb_assert (prop->data.baton != NULL);
17515 }
cd6c91b4 17516 else if (attr->form_is_ref ())
80180f79
SA
17517 {
17518 struct dwarf2_cu *target_cu = cu;
17519 struct die_info *target_die;
17520 struct attribute *target_attr;
17521
17522 target_die = follow_die_ref (die, attr, &target_cu);
17523 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17524 if (target_attr == NULL)
17525 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17526 target_cu);
80180f79
SA
17527 if (target_attr == NULL)
17528 return 0;
17529
df25ebbd 17530 switch (target_attr->name)
80180f79 17531 {
df25ebbd 17532 case DW_AT_location:
cd6c91b4 17533 if (target_attr->form_is_section_offset ())
df25ebbd 17534 {
8d749320 17535 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17536 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17537 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17538 prop->data.baton = baton;
17539 prop->kind = PROP_LOCLIST;
17540 gdb_assert (prop->data.baton != NULL);
17541 }
4fc6c0d5 17542 else if (target_attr->form_is_block ())
df25ebbd 17543 {
8d749320 17544 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17545 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17546 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17547 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17548 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17549 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17550 baton->locexpr.is_reference = true;
df25ebbd
JB
17551 prop->data.baton = baton;
17552 prop->kind = PROP_LOCEXPR;
17553 gdb_assert (prop->data.baton != NULL);
17554 }
17555 else
17556 {
17557 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17558 "dynamic property");
17559 return 0;
17560 }
17561 break;
17562 case DW_AT_data_member_location:
17563 {
17564 LONGEST offset;
17565
17566 if (!handle_data_member_location (target_die, target_cu,
17567 &offset))
17568 return 0;
17569
8d749320 17570 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17571 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17572 target_cu);
df25ebbd
JB
17573 baton->offset_info.offset = offset;
17574 baton->offset_info.type = die_type (target_die, target_cu);
17575 prop->data.baton = baton;
17576 prop->kind = PROP_ADDR_OFFSET;
17577 break;
17578 }
80180f79
SA
17579 }
17580 }
cd6c91b4 17581 else if (attr->form_is_constant ())
80180f79 17582 {
0826b30a 17583 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17584 prop->kind = PROP_CONST;
17585 }
17586 else
17587 {
17588 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17589 dwarf2_name (die, cu));
17590 return 0;
17591 }
17592
17593 return 1;
17594}
17595
09ba997f 17596/* See read.h. */
9a49df9d 17597
09ba997f
TT
17598struct type *
17599dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17600{
09ba997f 17601 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17602 struct type *int_type;
17603
17604 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17605#define TRY_TYPE(F) \
17606 int_type = (unsigned_p \
17607 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17608 : objfile_type (objfile)->builtin_ ## F); \
17609 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17610 return int_type
17611
17612 TRY_TYPE (char);
17613 TRY_TYPE (short);
17614 TRY_TYPE (int);
17615 TRY_TYPE (long);
17616 TRY_TYPE (long_long);
17617
17618#undef TRY_TYPE
17619
17620 gdb_assert_not_reached ("unable to find suitable integer type");
17621}
17622
09ba997f 17623/* See read.h. */
11a8b164 17624
09ba997f
TT
17625struct type *
17626dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17627{
09ba997f
TT
17628 int addr_size = this->addr_size ();
17629 return int_type (addr_size, unsigned_p);
11a8b164
AB
17630}
17631
b86352cf
AB
17632/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17633 present (which is valid) then compute the default type based on the
17634 compilation units address size. */
17635
17636static struct type *
17637read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17638{
17639 struct type *index_type = die_type (die, cu);
17640
17641 /* Dwarf-2 specifications explicitly allows to create subrange types
17642 without specifying a base type.
17643 In that case, the base type must be set to the type of
17644 the lower bound, upper bound or count, in that order, if any of these
17645 three attributes references an object that has a type.
17646 If no base type is found, the Dwarf-2 specifications say that
17647 a signed integer type of size equal to the size of an address should
17648 be used.
17649 For the following C code: `extern char gdb_int [];'
17650 GCC produces an empty range DIE.
17651 FIXME: muller/2010-05-28: Possible references to object for low bound,
17652 high bound or count are not yet handled by this code. */
78134374 17653 if (index_type->code () == TYPE_CODE_VOID)
09ba997f 17654 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17655
17656 return index_type;
17657}
17658
a02abb62
JB
17659/* Read the given DW_AT_subrange DIE. */
17660
f792889a 17661static struct type *
a02abb62
JB
17662read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17663{
4c9ad8c2 17664 struct type *base_type, *orig_base_type;
a02abb62
JB
17665 struct type *range_type;
17666 struct attribute *attr;
729efb13 17667 struct dynamic_prop low, high;
4fae6e18 17668 int low_default_is_valid;
c451ebe5 17669 int high_bound_is_count = 0;
15d034d0 17670 const char *name;
d359392f 17671 ULONGEST negative_mask;
e77813c8 17672
b86352cf
AB
17673 orig_base_type = read_subrange_index_type (die, cu);
17674
4c9ad8c2
TT
17675 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17676 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17677 creating the range type, but we use the result of check_typedef
17678 when examining properties of the type. */
17679 base_type = check_typedef (orig_base_type);
a02abb62 17680
7e314c57
JK
17681 /* The die_type call above may have already set the type for this DIE. */
17682 range_type = get_die_type (die, cu);
17683 if (range_type)
17684 return range_type;
17685
729efb13
SA
17686 low.kind = PROP_CONST;
17687 high.kind = PROP_CONST;
17688 high.data.const_val = 0;
17689
4fae6e18
JK
17690 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17691 omitting DW_AT_lower_bound. */
17692 switch (cu->language)
6e70227d 17693 {
4fae6e18
JK
17694 case language_c:
17695 case language_cplus:
729efb13 17696 low.data.const_val = 0;
4fae6e18
JK
17697 low_default_is_valid = 1;
17698 break;
17699 case language_fortran:
729efb13 17700 low.data.const_val = 1;
4fae6e18
JK
17701 low_default_is_valid = 1;
17702 break;
17703 case language_d:
4fae6e18 17704 case language_objc:
c44af4eb 17705 case language_rust:
729efb13 17706 low.data.const_val = 0;
4fae6e18
JK
17707 low_default_is_valid = (cu->header.version >= 4);
17708 break;
17709 case language_ada:
17710 case language_m2:
17711 case language_pascal:
729efb13 17712 low.data.const_val = 1;
4fae6e18
JK
17713 low_default_is_valid = (cu->header.version >= 4);
17714 break;
17715 default:
729efb13 17716 low.data.const_val = 0;
4fae6e18
JK
17717 low_default_is_valid = 0;
17718 break;
a02abb62
JB
17719 }
17720
e142c38c 17721 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17722 if (attr != nullptr)
9a49df9d 17723 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17724 else if (!low_default_is_valid)
b98664d3 17725 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17726 "- DIE at %s [in module %s]"),
17727 sect_offset_str (die->sect_off),
5e22e966 17728 objfile_name (cu->per_objfile->objfile));
a02abb62 17729
506f5c41
TV
17730 struct attribute *attr_ub, *attr_count;
17731 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17732 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17733 {
506f5c41 17734 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17735 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17736 {
c451ebe5
SA
17737 /* If bounds are constant do the final calculation here. */
17738 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17739 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17740 else
17741 high_bound_is_count = 1;
c2ff108b 17742 }
506f5c41
TV
17743 else
17744 {
17745 if (attr_ub != NULL)
17746 complaint (_("Unresolved DW_AT_upper_bound "
17747 "- DIE at %s [in module %s]"),
17748 sect_offset_str (die->sect_off),
5e22e966 17749 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17750 if (attr_count != NULL)
17751 complaint (_("Unresolved DW_AT_count "
17752 "- DIE at %s [in module %s]"),
17753 sect_offset_str (die->sect_off),
5e22e966 17754 objfile_name (cu->per_objfile->objfile));
506f5c41 17755 }
e77813c8 17756 }
a02abb62 17757
4e962e74
TT
17758 LONGEST bias = 0;
17759 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17760 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17761 bias = bias_attr->constant_value (0);
4e962e74 17762
dbb9c2b1
JB
17763 /* Normally, the DWARF producers are expected to use a signed
17764 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17765 But this is unfortunately not always the case, as witnessed
17766 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17767 is used instead. To work around that ambiguity, we treat
17768 the bounds as signed, and thus sign-extend their values, when
17769 the base type is signed. */
6e70227d 17770 negative_mask =
d359392f 17771 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17772 if (low.kind == PROP_CONST
17773 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17774 low.data.const_val |= negative_mask;
17775 if (high.kind == PROP_CONST
17776 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17777 high.data.const_val |= negative_mask;
43bbcdc2 17778
5bbd8269
AB
17779 /* Check for bit and byte strides. */
17780 struct dynamic_prop byte_stride_prop;
17781 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17782 if (attr_byte_stride != nullptr)
17783 {
09ba997f 17784 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17785 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17786 prop_type);
17787 }
17788
17789 struct dynamic_prop bit_stride_prop;
17790 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17791 if (attr_bit_stride != nullptr)
17792 {
17793 /* It only makes sense to have either a bit or byte stride. */
17794 if (attr_byte_stride != nullptr)
17795 {
17796 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17797 "- DIE at %s [in module %s]"),
17798 sect_offset_str (die->sect_off),
5e22e966 17799 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17800 attr_bit_stride = nullptr;
17801 }
17802 else
17803 {
09ba997f 17804 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17805 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17806 prop_type);
17807 }
17808 }
17809
17810 if (attr_byte_stride != nullptr
17811 || attr_bit_stride != nullptr)
17812 {
17813 bool byte_stride_p = (attr_byte_stride != nullptr);
17814 struct dynamic_prop *stride
17815 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17816
17817 range_type
17818 = create_range_type_with_stride (NULL, orig_base_type, &low,
17819 &high, bias, stride, byte_stride_p);
17820 }
17821 else
17822 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17823
c451ebe5
SA
17824 if (high_bound_is_count)
17825 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17826
c2ff108b
JK
17827 /* Ada expects an empty array on no boundary attributes. */
17828 if (attr == NULL && cu->language != language_ada)
729efb13 17829 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17830
39cbfefa
DJ
17831 name = dwarf2_name (die, cu);
17832 if (name)
d0e39ea2 17833 range_type->set_name (name);
6e70227d 17834
e142c38c 17835 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17836 if (attr != nullptr)
a02abb62
JB
17837 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17838
2b4424c3
TT
17839 maybe_set_alignment (cu, die, range_type);
17840
7e314c57
JK
17841 set_die_type (die, range_type, cu);
17842
17843 /* set_die_type should be already done. */
b4ba55a1
JB
17844 set_descriptive_type (range_type, die, cu);
17845
7e314c57 17846 return range_type;
a02abb62 17847}
6e70227d 17848
f792889a 17849static struct type *
81a17f79
JB
17850read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17851{
17852 struct type *type;
81a17f79 17853
5e22e966 17854 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17855 type->set_name (dwarf2_name (die, cu));
81a17f79 17856
74a2f8ff 17857 /* In Ada, an unspecified type is typically used when the description
85102364 17858 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17859 such a type, we treat it as a stub, and try to resolve it later on,
17860 when needed. */
17861 if (cu->language == language_ada)
17862 TYPE_STUB (type) = 1;
17863
f792889a 17864 return set_die_type (die, type, cu);
81a17f79 17865}
a02abb62 17866
639d11d3
DC
17867/* Read a single die and all its descendents. Set the die's sibling
17868 field to NULL; set other fields in the die correctly, and set all
17869 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17870 location of the info_ptr after reading all of those dies. PARENT
17871 is the parent of the die in question. */
17872
17873static struct die_info *
dee91e82 17874read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17875 const gdb_byte *info_ptr,
17876 const gdb_byte **new_info_ptr,
dee91e82 17877 struct die_info *parent)
639d11d3
DC
17878{
17879 struct die_info *die;
d521ce57 17880 const gdb_byte *cur_ptr;
639d11d3 17881
3e225074 17882 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17883 if (die == NULL)
17884 {
17885 *new_info_ptr = cur_ptr;
17886 return NULL;
17887 }
93311388 17888 store_in_ref_table (die, reader->cu);
639d11d3 17889
3e225074 17890 if (die->has_children)
bf6af496 17891 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17892 else
17893 {
17894 die->child = NULL;
17895 *new_info_ptr = cur_ptr;
17896 }
17897
17898 die->sibling = NULL;
17899 die->parent = parent;
17900 return die;
17901}
17902
17903/* Read a die, all of its descendents, and all of its siblings; set
17904 all of the fields of all of the dies correctly. Arguments are as
17905 in read_die_and_children. */
17906
17907static struct die_info *
bf6af496 17908read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17909 const gdb_byte *info_ptr,
17910 const gdb_byte **new_info_ptr,
bf6af496 17911 struct die_info *parent)
639d11d3
DC
17912{
17913 struct die_info *first_die, *last_sibling;
d521ce57 17914 const gdb_byte *cur_ptr;
639d11d3 17915
c906108c 17916 cur_ptr = info_ptr;
639d11d3
DC
17917 first_die = last_sibling = NULL;
17918
17919 while (1)
c906108c 17920 {
639d11d3 17921 struct die_info *die
dee91e82 17922 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17923
1d325ec1 17924 if (die == NULL)
c906108c 17925 {
639d11d3
DC
17926 *new_info_ptr = cur_ptr;
17927 return first_die;
c906108c 17928 }
1d325ec1
DJ
17929
17930 if (!first_die)
17931 first_die = die;
c906108c 17932 else
1d325ec1
DJ
17933 last_sibling->sibling = die;
17934
17935 last_sibling = die;
c906108c 17936 }
c906108c
SS
17937}
17938
bf6af496
DE
17939/* Read a die, all of its descendents, and all of its siblings; set
17940 all of the fields of all of the dies correctly. Arguments are as
17941 in read_die_and_children.
17942 This the main entry point for reading a DIE and all its children. */
17943
17944static struct die_info *
17945read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17946 const gdb_byte *info_ptr,
17947 const gdb_byte **new_info_ptr,
bf6af496
DE
17948 struct die_info *parent)
17949{
17950 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17951 new_info_ptr, parent);
17952
b4f54984 17953 if (dwarf_die_debug)
bf6af496
DE
17954 {
17955 fprintf_unfiltered (gdb_stdlog,
17956 "Read die from %s@0x%x of %s:\n",
96b79293 17957 reader->die_section->get_name (),
bf6af496
DE
17958 (unsigned) (info_ptr - reader->die_section->buffer),
17959 bfd_get_filename (reader->abfd));
b4f54984 17960 dump_die (die, dwarf_die_debug);
bf6af496
DE
17961 }
17962
17963 return die;
17964}
17965
3019eac3
DE
17966/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17967 attributes.
17968 The caller is responsible for filling in the extra attributes
17969 and updating (*DIEP)->num_attrs.
17970 Set DIEP to point to a newly allocated die with its information,
3e225074 17971 except for its child, sibling, and parent fields. */
93311388 17972
d521ce57 17973static const gdb_byte *
3019eac3 17974read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17975 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17976 int num_extra_attrs)
93311388 17977{
b64f50a1 17978 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17979 struct abbrev_info *abbrev;
17980 struct die_info *die;
17981 struct dwarf2_cu *cu = reader->cu;
17982 bfd *abfd = reader->abfd;
17983
9c541725 17984 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17985 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17986 info_ptr += bytes_read;
17987 if (!abbrev_number)
17988 {
17989 *diep = NULL;
93311388
DE
17990 return info_ptr;
17991 }
17992
685af9cd 17993 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17994 if (!abbrev)
348e048f
DE
17995 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17996 abbrev_number,
17997 bfd_get_filename (abfd));
17998
3019eac3 17999 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18000 die->sect_off = sect_off;
93311388
DE
18001 die->tag = abbrev->tag;
18002 die->abbrev = abbrev_number;
3e225074 18003 die->has_children = abbrev->has_children;
93311388 18004
3019eac3
DE
18005 /* Make the result usable.
18006 The caller needs to update num_attrs after adding the extra
18007 attributes. */
93311388
DE
18008 die->num_attrs = abbrev->num_attrs;
18009
18a8505e 18010 std::vector<int> indexes_that_need_reprocess;
93311388 18011 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18012 {
18013 bool need_reprocess;
18014 info_ptr =
18015 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18016 info_ptr, &need_reprocess);
18017 if (need_reprocess)
18018 indexes_that_need_reprocess.push_back (i);
18019 }
18020
052c8bb8 18021 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18022 if (attr != nullptr)
18023 cu->str_offsets_base = DW_UNSND (attr);
93311388 18024
41144253 18025 attr = die->attr (DW_AT_loclists_base);
18026 if (attr != nullptr)
18027 cu->loclist_base = DW_UNSND (attr);
18028
a39fdb41 18029 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18030 if (maybe_addr_base.has_value ())
18031 cu->addr_base = *maybe_addr_base;
18032 for (int index : indexes_that_need_reprocess)
18033 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18034 *diep = die;
93311388
DE
18035 return info_ptr;
18036}
18037
3019eac3
DE
18038/* Read a die and all its attributes.
18039 Set DIEP to point to a newly allocated die with its information,
3e225074 18040 except for its child, sibling, and parent fields. */
3019eac3 18041
d521ce57 18042static const gdb_byte *
3019eac3 18043read_full_die (const struct die_reader_specs *reader,
3e225074 18044 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18045{
d521ce57 18046 const gdb_byte *result;
bf6af496 18047
3e225074 18048 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18049
b4f54984 18050 if (dwarf_die_debug)
bf6af496
DE
18051 {
18052 fprintf_unfiltered (gdb_stdlog,
18053 "Read die from %s@0x%x of %s:\n",
96b79293 18054 reader->die_section->get_name (),
bf6af496
DE
18055 (unsigned) (info_ptr - reader->die_section->buffer),
18056 bfd_get_filename (reader->abfd));
b4f54984 18057 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18058 }
18059
18060 return result;
3019eac3 18061}
433df2d4 18062\f
c906108c 18063
72bf9492
DJ
18064/* Returns nonzero if TAG represents a type that we might generate a partial
18065 symbol for. */
18066
18067static int
18068is_type_tag_for_partial (int tag)
18069{
18070 switch (tag)
18071 {
18072#if 0
18073 /* Some types that would be reasonable to generate partial symbols for,
18074 that we don't at present. */
18075 case DW_TAG_array_type:
18076 case DW_TAG_file_type:
18077 case DW_TAG_ptr_to_member_type:
18078 case DW_TAG_set_type:
18079 case DW_TAG_string_type:
18080 case DW_TAG_subroutine_type:
18081#endif
18082 case DW_TAG_base_type:
18083 case DW_TAG_class_type:
680b30c7 18084 case DW_TAG_interface_type:
72bf9492
DJ
18085 case DW_TAG_enumeration_type:
18086 case DW_TAG_structure_type:
18087 case DW_TAG_subrange_type:
18088 case DW_TAG_typedef:
18089 case DW_TAG_union_type:
18090 return 1;
18091 default:
18092 return 0;
18093 }
18094}
18095
18096/* Load all DIEs that are interesting for partial symbols into memory. */
18097
18098static struct partial_die_info *
dee91e82 18099load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18100 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18101{
dee91e82 18102 struct dwarf2_cu *cu = reader->cu;
5e22e966 18103 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18104 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18105 unsigned int bytes_read;
5afb4e99 18106 unsigned int load_all = 0;
72bf9492
DJ
18107 int nesting_level = 1;
18108
18109 parent_die = NULL;
18110 last_die = NULL;
18111
7adf1e79
DE
18112 gdb_assert (cu->per_cu != NULL);
18113 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18114 load_all = 1;
18115
72bf9492
DJ
18116 cu->partial_dies
18117 = htab_create_alloc_ex (cu->header.length / 12,
18118 partial_die_hash,
18119 partial_die_eq,
18120 NULL,
18121 &cu->comp_unit_obstack,
18122 hashtab_obstack_allocate,
18123 dummy_obstack_deallocate);
18124
72bf9492
DJ
18125 while (1)
18126 {
685af9cd 18127 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18128
18129 /* A NULL abbrev means the end of a series of children. */
18130 if (abbrev == NULL)
18131 {
18132 if (--nesting_level == 0)
cd9983dd
YQ
18133 return first_die;
18134
72bf9492
DJ
18135 info_ptr += bytes_read;
18136 last_die = parent_die;
18137 parent_die = parent_die->die_parent;
18138 continue;
18139 }
18140
98bfdba5
PA
18141 /* Check for template arguments. We never save these; if
18142 they're seen, we just mark the parent, and go on our way. */
18143 if (parent_die != NULL
18144 && cu->language == language_cplus
18145 && (abbrev->tag == DW_TAG_template_type_param
18146 || abbrev->tag == DW_TAG_template_value_param))
18147 {
18148 parent_die->has_template_arguments = 1;
18149
18150 if (!load_all)
18151 {
18152 /* We don't need a partial DIE for the template argument. */
dee91e82 18153 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18154 continue;
18155 }
18156 }
18157
0d99eb77 18158 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18159 Skip their other children. */
18160 if (!load_all
18161 && cu->language == language_cplus
18162 && parent_die != NULL
18163 && parent_die->tag == DW_TAG_subprogram)
18164 {
dee91e82 18165 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18166 continue;
18167 }
18168
5afb4e99
DJ
18169 /* Check whether this DIE is interesting enough to save. Normally
18170 we would not be interested in members here, but there may be
18171 later variables referencing them via DW_AT_specification (for
18172 static members). */
18173 if (!load_all
18174 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18175 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18176 && abbrev->tag != DW_TAG_enumerator
18177 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18178 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18179 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18180 && abbrev->tag != DW_TAG_variable
5afb4e99 18181 && abbrev->tag != DW_TAG_namespace
f55ee35c 18182 && abbrev->tag != DW_TAG_module
95554aad 18183 && abbrev->tag != DW_TAG_member
74921315
KS
18184 && abbrev->tag != DW_TAG_imported_unit
18185 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18186 {
18187 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18188 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18189 continue;
18190 }
18191
6f06d47b
YQ
18192 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18193 abbrev);
cd9983dd 18194
48fbe735 18195 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18196
18197 /* This two-pass algorithm for processing partial symbols has a
18198 high cost in cache pressure. Thus, handle some simple cases
18199 here which cover the majority of C partial symbols. DIEs
18200 which neither have specification tags in them, nor could have
18201 specification tags elsewhere pointing at them, can simply be
18202 processed and discarded.
18203
18204 This segment is also optional; scan_partial_symbols and
18205 add_partial_symbol will handle these DIEs if we chain
18206 them in normally. When compilers which do not emit large
18207 quantities of duplicate debug information are more common,
18208 this code can probably be removed. */
18209
18210 /* Any complete simple types at the top level (pretty much all
18211 of them, for a language without namespaces), can be processed
18212 directly. */
18213 if (parent_die == NULL
cd9983dd
YQ
18214 && pdi.has_specification == 0
18215 && pdi.is_declaration == 0
18216 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18217 || pdi.tag == DW_TAG_base_type
18218 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18219 {
cd9983dd 18220 if (building_psymtab && pdi.name != NULL)
31edb802 18221 add_psymbol_to_list (pdi.name, false,
79748972 18222 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18223 psymbol_placement::STATIC,
1762568f 18224 0, cu->language, objfile);
cd9983dd 18225 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18226 continue;
18227 }
18228
d8228535
JK
18229 /* The exception for DW_TAG_typedef with has_children above is
18230 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18231 type_name_or_error will error on such types later.
d8228535
JK
18232
18233 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18234 it could not find the child DIEs referenced later, this is checked
18235 above. In correct DWARF DW_TAG_typedef should have no children. */
18236
cd9983dd 18237 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18238 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18239 "- DIE at %s [in module %s]"),
cd9983dd 18240 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18241
72bf9492
DJ
18242 /* If we're at the second level, and we're an enumerator, and
18243 our parent has no specification (meaning possibly lives in a
18244 namespace elsewhere), then we can add the partial symbol now
18245 instead of queueing it. */
cd9983dd 18246 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18247 && parent_die != NULL
18248 && parent_die->die_parent == NULL
18249 && parent_die->tag == DW_TAG_enumeration_type
18250 && parent_die->has_specification == 0)
18251 {
cd9983dd 18252 if (pdi.name == NULL)
b98664d3 18253 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18254 else if (building_psymtab)
31edb802 18255 add_psymbol_to_list (pdi.name, false,
79748972 18256 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18257 cu->language == language_cplus
75aedd27
TT
18258 ? psymbol_placement::GLOBAL
18259 : psymbol_placement::STATIC,
1762568f 18260 0, cu->language, objfile);
72bf9492 18261
cd9983dd 18262 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18263 continue;
18264 }
18265
cd9983dd 18266 struct partial_die_info *part_die
6f06d47b 18267 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18268
72bf9492
DJ
18269 /* We'll save this DIE so link it in. */
18270 part_die->die_parent = parent_die;
18271 part_die->die_sibling = NULL;
18272 part_die->die_child = NULL;
18273
18274 if (last_die && last_die == parent_die)
18275 last_die->die_child = part_die;
18276 else if (last_die)
18277 last_die->die_sibling = part_die;
18278
18279 last_die = part_die;
18280
18281 if (first_die == NULL)
18282 first_die = part_die;
18283
18284 /* Maybe add the DIE to the hash table. Not all DIEs that we
18285 find interesting need to be in the hash table, because we
18286 also have the parent/sibling/child chains; only those that we
18287 might refer to by offset later during partial symbol reading.
18288
18289 For now this means things that might have be the target of a
18290 DW_AT_specification, DW_AT_abstract_origin, or
18291 DW_AT_extension. DW_AT_extension will refer only to
18292 namespaces; DW_AT_abstract_origin refers to functions (and
18293 many things under the function DIE, but we do not recurse
18294 into function DIEs during partial symbol reading) and
18295 possibly variables as well; DW_AT_specification refers to
18296 declarations. Declarations ought to have the DW_AT_declaration
18297 flag. It happens that GCC forgets to put it in sometimes, but
18298 only for functions, not for types.
18299
18300 Adding more things than necessary to the hash table is harmless
18301 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18302 wasted time in find_partial_die, when we reread the compilation
18303 unit with load_all_dies set. */
72bf9492 18304
5afb4e99 18305 if (load_all
72929c62 18306 || abbrev->tag == DW_TAG_constant
5afb4e99 18307 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18308 || abbrev->tag == DW_TAG_variable
18309 || abbrev->tag == DW_TAG_namespace
18310 || part_die->is_declaration)
18311 {
18312 void **slot;
18313
18314 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18315 to_underlying (part_die->sect_off),
18316 INSERT);
72bf9492
DJ
18317 *slot = part_die;
18318 }
18319
72bf9492 18320 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18321 we have no reason to follow the children of structures; for other
98bfdba5
PA
18322 languages we have to, so that we can get at method physnames
18323 to infer fully qualified class names, for DW_AT_specification,
18324 and for C++ template arguments. For C++, we also look one level
18325 inside functions to find template arguments (if the name of the
18326 function does not already contain the template arguments).
bc30ff58 18327
0a4b0913
AB
18328 For Ada and Fortran, we need to scan the children of subprograms
18329 and lexical blocks as well because these languages allow the
18330 definition of nested entities that could be interesting for the
18331 debugger, such as nested subprograms for instance. */
72bf9492 18332 if (last_die->has_children
5afb4e99
DJ
18333 && (load_all
18334 || last_die->tag == DW_TAG_namespace
f55ee35c 18335 || last_die->tag == DW_TAG_module
72bf9492 18336 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18337 || (cu->language == language_cplus
18338 && last_die->tag == DW_TAG_subprogram
18339 && (last_die->name == NULL
18340 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18341 || (cu->language != language_c
18342 && (last_die->tag == DW_TAG_class_type
680b30c7 18343 || last_die->tag == DW_TAG_interface_type
72bf9492 18344 || last_die->tag == DW_TAG_structure_type
bc30ff58 18345 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18346 || ((cu->language == language_ada
18347 || cu->language == language_fortran)
bc30ff58
JB
18348 && (last_die->tag == DW_TAG_subprogram
18349 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18350 {
18351 nesting_level++;
18352 parent_die = last_die;
18353 continue;
18354 }
18355
18356 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18357 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18358
18359 /* Back to the top, do it again. */
18360 }
18361}
18362
6f06d47b
YQ
18363partial_die_info::partial_die_info (sect_offset sect_off_,
18364 struct abbrev_info *abbrev)
18365 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18366{
18367}
18368
35cc7ed7
YQ
18369/* Read a minimal amount of information into the minimal die structure.
18370 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18371
48fbe735
YQ
18372const gdb_byte *
18373partial_die_info::read (const struct die_reader_specs *reader,
18374 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18375{
dee91e82 18376 struct dwarf2_cu *cu = reader->cu;
5e22e966 18377 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18378 unsigned int i;
c5aa993b 18379 int has_low_pc_attr = 0;
c906108c 18380 int has_high_pc_attr = 0;
91da1414 18381 int high_pc_relative = 0;
c906108c 18382
fd0a254f 18383 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18384 {
e7da7f8f 18385 attribute attr;
18a8505e 18386 bool need_reprocess;
e7da7f8f 18387 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18388 info_ptr, &need_reprocess);
18389 /* String and address offsets that need to do the reprocessing have
18390 already been read at this point, so there is no need to wait until
18391 the loop terminates to do the reprocessing. */
18392 if (need_reprocess)
e7da7f8f 18393 read_attribute_reprocess (reader, &attr);
c906108c 18394 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18395 partial symbol table. */
c906108c
SS
18396 switch (attr.name)
18397 {
18398 case DW_AT_name:
48fbe735 18399 switch (tag)
71c25dea
TT
18400 {
18401 case DW_TAG_compile_unit:
95554aad 18402 case DW_TAG_partial_unit:
348e048f 18403 case DW_TAG_type_unit:
71c25dea
TT
18404 /* Compilation units have a DW_AT_name that is a filename, not
18405 a source language identifier. */
18406 case DW_TAG_enumeration_type:
18407 case DW_TAG_enumerator:
18408 /* These tags always have simple identifiers already; no need
18409 to canonicalize them. */
48fbe735 18410 name = DW_STRING (&attr);
71c25dea
TT
18411 break;
18412 default:
48fbe735
YQ
18413 {
18414 struct objfile *objfile = dwarf2_per_objfile->objfile;
18415
18416 name
be1e3d3e 18417 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18418 }
71c25dea
TT
18419 break;
18420 }
c906108c 18421 break;
31ef98ae 18422 case DW_AT_linkage_name:
c906108c 18423 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18424 /* Note that both forms of linkage name might appear. We
18425 assume they will be the same, and we only store the last
18426 one we see. */
e61108c9 18427 linkage_name = attr.value_as_string ();
787de330
TT
18428 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18429 See https://github.com/rust-lang/rust/issues/32925. */
18430 if (cu->language == language_rust && linkage_name != NULL
18431 && strchr (linkage_name, '{') != NULL)
18432 linkage_name = NULL;
c906108c
SS
18433 break;
18434 case DW_AT_low_pc:
18435 has_low_pc_attr = 1;
cd6c91b4 18436 lowpc = attr.value_as_address ();
c906108c
SS
18437 break;
18438 case DW_AT_high_pc:
18439 has_high_pc_attr = 1;
cd6c91b4
TT
18440 highpc = attr.value_as_address ();
18441 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18442 high_pc_relative = 1;
c906108c
SS
18443 break;
18444 case DW_AT_location:
0963b4bd 18445 /* Support the .debug_loc offsets. */
4fc6c0d5 18446 if (attr.form_is_block ())
8e19ed76 18447 {
48fbe735 18448 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18449 }
cd6c91b4 18450 else if (attr.form_is_section_offset ())
8e19ed76 18451 {
4d3c2250 18452 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18453 }
18454 else
18455 {
4d3c2250
KB
18456 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18457 "partial symbol information");
8e19ed76 18458 }
c906108c 18459 break;
c906108c 18460 case DW_AT_external:
48fbe735 18461 is_external = DW_UNSND (&attr);
c906108c
SS
18462 break;
18463 case DW_AT_declaration:
48fbe735 18464 is_declaration = DW_UNSND (&attr);
c906108c
SS
18465 break;
18466 case DW_AT_type:
48fbe735 18467 has_type = 1;
c906108c
SS
18468 break;
18469 case DW_AT_abstract_origin:
18470 case DW_AT_specification:
72bf9492 18471 case DW_AT_extension:
48fbe735 18472 has_specification = 1;
0826b30a 18473 spec_offset = attr.get_ref_die_offset ();
48fbe735 18474 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18475 || cu->per_cu->is_dwz);
c906108c
SS
18476 break;
18477 case DW_AT_sibling:
18478 /* Ignore absolute siblings, they might point outside of
18479 the current compile unit. */
18480 if (attr.form == DW_FORM_ref_addr)
b98664d3 18481 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18482 else
b9502d3f 18483 {
48fbe735 18484 const gdb_byte *buffer = reader->buffer;
0826b30a 18485 sect_offset off = attr.get_ref_die_offset ();
9c541725 18486 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18487
18488 if (sibling_ptr < info_ptr)
b98664d3 18489 complaint (_("DW_AT_sibling points backwards"));
22869d73 18490 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18491 reader->die_section->overflow_complaint ();
b9502d3f 18492 else
48fbe735 18493 sibling = sibling_ptr;
b9502d3f 18494 }
c906108c 18495 break;
fa4028e9 18496 case DW_AT_byte_size:
48fbe735 18497 has_byte_size = 1;
fa4028e9 18498 break;
ff908ebf 18499 case DW_AT_const_value:
48fbe735 18500 has_const_value = 1;
ff908ebf 18501 break;
68511cec
CES
18502 case DW_AT_calling_convention:
18503 /* DWARF doesn't provide a way to identify a program's source-level
18504 entry point. DW_AT_calling_convention attributes are only meant
18505 to describe functions' calling conventions.
18506
18507 However, because it's a necessary piece of information in
0c1b455e
TT
18508 Fortran, and before DWARF 4 DW_CC_program was the only
18509 piece of debugging information whose definition refers to
18510 a 'main program' at all, several compilers marked Fortran
18511 main programs with DW_CC_program --- even when those
18512 functions use the standard calling conventions.
18513
18514 Although DWARF now specifies a way to provide this
18515 information, we support this practice for backward
18516 compatibility. */
68511cec 18517 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18518 && cu->language == language_fortran)
48fbe735 18519 main_subprogram = 1;
68511cec 18520 break;
481860b3
GB
18521 case DW_AT_inline:
18522 if (DW_UNSND (&attr) == DW_INL_inlined
18523 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18524 may_be_inlined = 1;
481860b3 18525 break;
95554aad
TT
18526
18527 case DW_AT_import:
48fbe735 18528 if (tag == DW_TAG_imported_unit)
36586728 18529 {
0826b30a 18530 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18531 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18532 || cu->per_cu->is_dwz);
18533 }
95554aad
TT
18534 break;
18535
0c1b455e 18536 case DW_AT_main_subprogram:
48fbe735 18537 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18538 break;
18539
05caa1d2
TT
18540 case DW_AT_ranges:
18541 {
18542 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18543 but that requires a full DIE, so instead we just
18544 reimplement it. */
18545 int need_ranges_base = tag != DW_TAG_compile_unit;
18546 unsigned int ranges_offset = (DW_UNSND (&attr)
18547 + (need_ranges_base
18548 ? cu->ranges_base
18549 : 0));
18550
18551 /* Value of the DW_AT_ranges attribute is the offset in the
18552 .debug_ranges section. */
18553 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18554 nullptr))
18555 has_pc_info = 1;
18556 }
18557 break;
18558
c906108c
SS
18559 default:
18560 break;
18561 }
18562 }
18563
10d06d82
TT
18564 /* For Ada, if both the name and the linkage name appear, we prefer
18565 the latter. This lets "catch exception" work better, regardless
18566 of the order in which the name and linkage name were emitted.
18567 Really, though, this is just a workaround for the fact that gdb
18568 doesn't store both the name and the linkage name. */
18569 if (cu->language == language_ada && linkage_name != nullptr)
18570 name = linkage_name;
18571
91da1414 18572 if (high_pc_relative)
48fbe735 18573 highpc += lowpc;
91da1414 18574
9373cf26
JK
18575 if (has_low_pc_attr && has_high_pc_attr)
18576 {
18577 /* When using the GNU linker, .gnu.linkonce. sections are used to
18578 eliminate duplicate copies of functions and vtables and such.
18579 The linker will arbitrarily choose one and discard the others.
18580 The AT_*_pc values for such functions refer to local labels in
18581 these sections. If the section from that file was discarded, the
18582 labels are not in the output, so the relocs get a value of 0.
18583 If this is a discarded function, mark the pc bounds as invalid,
18584 so that GDB will ignore it. */
5989a64e 18585 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18586 {
48fbe735 18587 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18588 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18589
b98664d3 18590 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18591 "for DIE at %s [in module %s]"),
48fbe735
YQ
18592 paddress (gdbarch, lowpc),
18593 sect_offset_str (sect_off),
9d8780f0 18594 objfile_name (objfile));
9373cf26
JK
18595 }
18596 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18597 else if (lowpc >= highpc)
9373cf26 18598 {
48fbe735 18599 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18600 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18601
b98664d3 18602 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18603 "for DIE at %s [in module %s]"),
48fbe735
YQ
18604 paddress (gdbarch, lowpc),
18605 paddress (gdbarch, highpc),
18606 sect_offset_str (sect_off),
9c541725 18607 objfile_name (objfile));
9373cf26
JK
18608 }
18609 else
48fbe735 18610 has_pc_info = 1;
9373cf26 18611 }
85cbf3d3 18612
c906108c
SS
18613 return info_ptr;
18614}
18615
72bf9492
DJ
18616/* Find a cached partial DIE at OFFSET in CU. */
18617
d590ff25
YQ
18618struct partial_die_info *
18619dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18620{
18621 struct partial_die_info *lookup_die = NULL;
6f06d47b 18622 struct partial_die_info part_die (sect_off);
72bf9492 18623
9a3c8263 18624 lookup_die = ((struct partial_die_info *)
d590ff25 18625 htab_find_with_hash (partial_dies, &part_die,
9c541725 18626 to_underlying (sect_off)));
72bf9492 18627
72bf9492
DJ
18628 return lookup_die;
18629}
18630
348e048f
DE
18631/* Find a partial DIE at OFFSET, which may or may not be in CU,
18632 except in the case of .debug_types DIEs which do not reference
18633 outside their CU (they do however referencing other types via
55f1336d 18634 DW_FORM_ref_sig8). */
72bf9492 18635
122cf0f2 18636static const struct cu_partial_die_info
9c541725 18637find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18638{
5e22e966 18639 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18640 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18641 struct dwarf2_per_cu_data *per_cu = NULL;
18642 struct partial_die_info *pd = NULL;
72bf9492 18643
36586728 18644 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18645 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18646 {
d590ff25 18647 pd = cu->find_partial_die (sect_off);
5afb4e99 18648 if (pd != NULL)
fb816e8b 18649 return { cu, pd };
0d99eb77
DE
18650 /* We missed recording what we needed.
18651 Load all dies and try again. */
18652 per_cu = cu->per_cu;
5afb4e99 18653 }
0d99eb77
DE
18654 else
18655 {
18656 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18657 if (cu->per_cu->is_debug_types)
0d99eb77 18658 {
9d8780f0
SM
18659 error (_("Dwarf Error: Type Unit at offset %s contains"
18660 " external reference to offset %s [in module %s].\n"),
18661 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18662 bfd_get_filename (objfile->obfd));
18663 }
9c541725 18664 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18665 dwarf2_per_objfile);
72bf9492 18666
0d99eb77
DE
18667 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18668 load_partial_comp_unit (per_cu);
ae038cb0 18669
0d99eb77 18670 per_cu->cu->last_used = 0;
d590ff25 18671 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18672 }
5afb4e99 18673
dee91e82
DE
18674 /* If we didn't find it, and not all dies have been loaded,
18675 load them all and try again. */
18676
5afb4e99
DJ
18677 if (pd == NULL && per_cu->load_all_dies == 0)
18678 {
5afb4e99 18679 per_cu->load_all_dies = 1;
fd820528
DE
18680
18681 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18682 THIS_CU->cu may already be in use. So we can't just free it and
18683 replace its DIEs with the ones we read in. Instead, we leave those
18684 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18685 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18686 set. */
dee91e82 18687 load_partial_comp_unit (per_cu);
5afb4e99 18688
d590ff25 18689 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18690 }
18691
18692 if (pd == NULL)
18693 internal_error (__FILE__, __LINE__,
9d8780f0 18694 _("could not find partial DIE %s "
3e43a32a 18695 "in cache [from module %s]\n"),
9d8780f0 18696 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18697 return { per_cu->cu, pd };
72bf9492
DJ
18698}
18699
abc72ce4
DE
18700/* See if we can figure out if the class lives in a namespace. We do
18701 this by looking for a member function; its demangled name will
18702 contain namespace info, if there is any. */
18703
18704static void
18705guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18706 struct dwarf2_cu *cu)
18707{
18708 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18709 what template types look like, because the demangler
18710 frequently doesn't give the same name as the debug info. We
18711 could fix this by only using the demangled name to get the
18712 prefix (but see comment in read_structure_type). */
18713
18714 struct partial_die_info *real_pdi;
18715 struct partial_die_info *child_pdi;
18716
18717 /* If this DIE (this DIE's specification, if any) has a parent, then
18718 we should not do this. We'll prepend the parent's fully qualified
18719 name when we create the partial symbol. */
18720
18721 real_pdi = struct_pdi;
18722 while (real_pdi->has_specification)
fb816e8b 18723 {
122cf0f2
AB
18724 auto res = find_partial_die (real_pdi->spec_offset,
18725 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18726 real_pdi = res.pdi;
18727 cu = res.cu;
18728 }
abc72ce4
DE
18729
18730 if (real_pdi->die_parent != NULL)
18731 return;
18732
18733 for (child_pdi = struct_pdi->die_child;
18734 child_pdi != NULL;
18735 child_pdi = child_pdi->die_sibling)
18736 {
18737 if (child_pdi->tag == DW_TAG_subprogram
18738 && child_pdi->linkage_name != NULL)
18739 {
43816ebc
TT
18740 gdb::unique_xmalloc_ptr<char> actual_class_name
18741 (language_class_name_from_physname (cu->language_defn,
18742 child_pdi->linkage_name));
abc72ce4
DE
18743 if (actual_class_name != NULL)
18744 {
5e22e966 18745 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18746 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18747 }
18748 break;
18749 }
18750 }
18751}
18752
25c11aca
TV
18753/* Return true if a DIE with TAG may have the DW_AT_const_value
18754 attribute. */
18755
18756static bool
18757can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18758{
18759 switch (tag)
18760 {
18761 case DW_TAG_constant:
18762 case DW_TAG_enumerator:
18763 case DW_TAG_formal_parameter:
18764 case DW_TAG_template_value_param:
18765 case DW_TAG_variable:
18766 return true;
18767 }
18768
18769 return false;
18770}
18771
52356b79
YQ
18772void
18773partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18774{
abc72ce4
DE
18775 /* Once we've fixed up a die, there's no point in doing so again.
18776 This also avoids a memory leak if we were to call
18777 guess_partial_die_structure_name multiple times. */
52356b79 18778 if (fixup_called)
abc72ce4
DE
18779 return;
18780
72bf9492
DJ
18781 /* If we found a reference attribute and the DIE has no name, try
18782 to find a name in the referred to DIE. */
18783
52356b79 18784 if (name == NULL && has_specification)
72bf9492
DJ
18785 {
18786 struct partial_die_info *spec_die;
72bf9492 18787
122cf0f2 18788 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18789 spec_die = res.pdi;
18790 cu = res.cu;
72bf9492 18791
52356b79 18792 spec_die->fixup (cu);
72bf9492
DJ
18793
18794 if (spec_die->name)
18795 {
52356b79 18796 name = spec_die->name;
72bf9492
DJ
18797
18798 /* Copy DW_AT_external attribute if it is set. */
18799 if (spec_die->is_external)
52356b79 18800 is_external = spec_die->is_external;
72bf9492
DJ
18801 }
18802 }
18803
25c11aca
TV
18804 if (!has_const_value && has_specification
18805 && can_have_DW_AT_const_value_p (tag))
18806 {
18807 struct partial_die_info *spec_die;
18808
18809 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18810 spec_die = res.pdi;
18811 cu = res.cu;
18812
18813 spec_die->fixup (cu);
18814
18815 if (spec_die->has_const_value)
18816 {
18817 /* Copy DW_AT_const_value attribute if it is set. */
18818 has_const_value = spec_die->has_const_value;
18819 }
18820 }
18821
72bf9492 18822 /* Set default names for some unnamed DIEs. */
72bf9492 18823
52356b79
YQ
18824 if (name == NULL && tag == DW_TAG_namespace)
18825 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18826
abc72ce4
DE
18827 /* If there is no parent die to provide a namespace, and there are
18828 children, see if we can determine the namespace from their linkage
122d1940 18829 name. */
abc72ce4 18830 if (cu->language == language_cplus
5e22e966 18831 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18832 && die_parent == NULL
18833 && has_children
18834 && (tag == DW_TAG_class_type
18835 || tag == DW_TAG_structure_type
18836 || tag == DW_TAG_union_type))
18837 guess_partial_die_structure_name (this, cu);
abc72ce4 18838
53832f31
TT
18839 /* GCC might emit a nameless struct or union that has a linkage
18840 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18841 if (name == NULL
18842 && (tag == DW_TAG_class_type
18843 || tag == DW_TAG_interface_type
18844 || tag == DW_TAG_structure_type
18845 || tag == DW_TAG_union_type)
18846 && linkage_name != NULL)
53832f31 18847 {
43816ebc
TT
18848 gdb::unique_xmalloc_ptr<char> demangled
18849 (gdb_demangle (linkage_name, DMGL_TYPES));
18850 if (demangled != nullptr)
53832f31 18851 {
96408a79
SA
18852 const char *base;
18853
18854 /* Strip any leading namespaces/classes, keep only the base name.
18855 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18856 base = strrchr (demangled.get (), ':');
18857 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18858 base++;
18859 else
43816ebc 18860 base = demangled.get ();
96408a79 18861
5e22e966 18862 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18863 name = objfile->intern (base);
53832f31
TT
18864 }
18865 }
18866
52356b79 18867 fixup_called = 1;
72bf9492
DJ
18868}
18869
41144253 18870/* Read the .debug_loclists header contents from the given SECTION in the
18871 HEADER. */
18872static void
18873read_loclist_header (struct loclist_header *header,
18874 struct dwarf2_section_info *section)
18875{
18876 unsigned int bytes_read;
18877 bfd *abfd = section->get_bfd_owner ();
18878 const gdb_byte *info_ptr = section->buffer;
18879 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18880 info_ptr += bytes_read;
18881 header->version = read_2_bytes (abfd, info_ptr);
18882 info_ptr += 2;
18883 header->addr_size = read_1_byte (abfd, info_ptr);
18884 info_ptr += 1;
18885 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18886 info_ptr += 1;
18887 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18888}
18889
18890/* Return the DW_AT_loclists_base value for the CU. */
18891static ULONGEST
18892lookup_loclist_base (struct dwarf2_cu *cu)
18893{
18894 /* For the .dwo unit, the loclist_base points to the first offset following
18895 the header. The header consists of the following entities-
18896 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18897 bit format)
18898 2. version (2 bytes)
18899 3. address size (1 byte)
18900 4. segment selector size (1 byte)
18901 5. offset entry count (4 bytes)
18902 These sizes are derived as per the DWARFv5 standard. */
18903 if (cu->dwo_unit != nullptr)
18904 {
18905 if (cu->header.initial_length_size == 4)
18906 return LOCLIST_HEADER_SIZE32;
18907 return LOCLIST_HEADER_SIZE64;
18908 }
18909 return cu->loclist_base;
18910}
18911
18912/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18913 array of offsets in the .debug_loclists section. */
18914static CORE_ADDR
18915read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18916{
5e22e966 18917 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18918 struct objfile *objfile = dwarf2_per_objfile->objfile;
18919 bfd *abfd = objfile->obfd;
18920 ULONGEST loclist_base = lookup_loclist_base (cu);
18921 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18922
18923 section->read (objfile);
18924 if (section->buffer == NULL)
18925 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18926 "section [in module %s]"), objfile_name (objfile));
18927 struct loclist_header header;
18928 read_loclist_header (&header, section);
18929 if (loclist_index >= header.offset_entry_count)
18930 complaint (_("DW_FORM_loclistx pointing outside of "
18931 ".debug_loclists offset array [in module %s]"),
18932 objfile_name (objfile));
18933 if (loclist_base + loclist_index * cu->header.offset_size
18934 >= section->size)
18935 complaint (_("DW_FORM_loclistx pointing outside of "
18936 ".debug_loclists section [in module %s]"),
18937 objfile_name (objfile));
18938 const gdb_byte *info_ptr
18939 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18940
18941 if (cu->header.offset_size == 4)
18942 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18943 else
18944 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18945}
18946
18a8505e
AT
18947/* Process the attributes that had to be skipped in the first round. These
18948 attributes are the ones that need str_offsets_base or addr_base attributes.
18949 They could not have been processed in the first round, because at the time
18950 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18951static void
18952read_attribute_reprocess (const struct die_reader_specs *reader,
18953 struct attribute *attr)
18a8505e
AT
18954{
18955 struct dwarf2_cu *cu = reader->cu;
18956 switch (attr->form)
18957 {
18958 case DW_FORM_addrx:
18959 case DW_FORM_GNU_addr_index:
18960 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18961 break;
41144253 18962 case DW_FORM_loclistx:
18963 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18964 break;
18a8505e
AT
18965 case DW_FORM_strx:
18966 case DW_FORM_strx1:
18967 case DW_FORM_strx2:
18968 case DW_FORM_strx3:
18969 case DW_FORM_strx4:
18970 case DW_FORM_GNU_str_index:
18971 {
18972 unsigned int str_index = DW_UNSND (attr);
18973 if (reader->dwo_file != NULL)
18974 {
18975 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18976 DW_STRING_IS_CANONICAL (attr) = 0;
18977 }
18978 else
18979 {
18980 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18981 DW_STRING_IS_CANONICAL (attr) = 0;
18982 }
18983 break;
18984 }
18985 default:
18986 gdb_assert_not_reached (_("Unexpected DWARF form."));
18987 }
18988}
18989
a8329558 18990/* Read an attribute value described by an attribute form. */
c906108c 18991
d521ce57 18992static const gdb_byte *
dee91e82
DE
18993read_attribute_value (const struct die_reader_specs *reader,
18994 struct attribute *attr, unsigned form,
18a8505e
AT
18995 LONGEST implicit_const, const gdb_byte *info_ptr,
18996 bool *need_reprocess)
c906108c 18997{
dee91e82 18998 struct dwarf2_cu *cu = reader->cu;
5e22e966 18999 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19000 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19001 bfd *abfd = reader->abfd;
e7c27a73 19002 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19003 unsigned int bytes_read;
19004 struct dwarf_block *blk;
18a8505e 19005 *need_reprocess = false;
c906108c 19006
aead7601 19007 attr->form = (enum dwarf_form) form;
a8329558 19008 switch (form)
c906108c 19009 {
c906108c 19010 case DW_FORM_ref_addr:
ae411497 19011 if (cu->header.version == 2)
c8a7a66f
TT
19012 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19013 &bytes_read);
ae411497 19014 else
8266302d
TT
19015 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19016 &bytes_read);
ae411497
TT
19017 info_ptr += bytes_read;
19018 break;
36586728 19019 case DW_FORM_GNU_ref_alt:
8266302d 19020 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19021 info_ptr += bytes_read;
19022 break;
ae411497 19023 case DW_FORM_addr:
08feed99
TT
19024 {
19025 struct gdbarch *gdbarch = objfile->arch ();
19026 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19027 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19028 info_ptr += bytes_read;
19029 }
c906108c
SS
19030 break;
19031 case DW_FORM_block2:
7b5a2f43 19032 blk = dwarf_alloc_block (cu);
c906108c
SS
19033 blk->size = read_2_bytes (abfd, info_ptr);
19034 info_ptr += 2;
19035 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19036 info_ptr += blk->size;
19037 DW_BLOCK (attr) = blk;
19038 break;
19039 case DW_FORM_block4:
7b5a2f43 19040 blk = dwarf_alloc_block (cu);
c906108c
SS
19041 blk->size = read_4_bytes (abfd, info_ptr);
19042 info_ptr += 4;
19043 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19044 info_ptr += blk->size;
19045 DW_BLOCK (attr) = blk;
19046 break;
19047 case DW_FORM_data2:
19048 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19049 info_ptr += 2;
19050 break;
19051 case DW_FORM_data4:
19052 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19053 info_ptr += 4;
19054 break;
19055 case DW_FORM_data8:
19056 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19057 info_ptr += 8;
19058 break;
0224619f
JK
19059 case DW_FORM_data16:
19060 blk = dwarf_alloc_block (cu);
19061 blk->size = 16;
19062 blk->data = read_n_bytes (abfd, info_ptr, 16);
19063 info_ptr += 16;
19064 DW_BLOCK (attr) = blk;
19065 break;
2dc7f7b3 19066 case DW_FORM_sec_offset:
8266302d 19067 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19068 info_ptr += bytes_read;
19069 break;
41144253 19070 case DW_FORM_loclistx:
19071 {
19072 *need_reprocess = true;
19073 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19074 info_ptr += bytes_read;
19075 }
19076 break;
c906108c 19077 case DW_FORM_string:
9b1c24c8 19078 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19079 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19080 info_ptr += bytes_read;
19081 break;
4bdf3d34 19082 case DW_FORM_strp:
36586728
TT
19083 if (!cu->per_cu->is_dwz)
19084 {
ed2dc618
SM
19085 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19086 abfd, info_ptr, cu_header,
36586728
TT
19087 &bytes_read);
19088 DW_STRING_IS_CANONICAL (attr) = 0;
19089 info_ptr += bytes_read;
19090 break;
19091 }
19092 /* FALLTHROUGH */
43988095
JK
19093 case DW_FORM_line_strp:
19094 if (!cu->per_cu->is_dwz)
19095 {
86c0bb4c
TT
19096 DW_STRING (attr)
19097 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19098 &bytes_read);
43988095
JK
19099 DW_STRING_IS_CANONICAL (attr) = 0;
19100 info_ptr += bytes_read;
19101 break;
19102 }
19103 /* FALLTHROUGH */
36586728
TT
19104 case DW_FORM_GNU_strp_alt:
19105 {
c3699833 19106 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19107 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19108 &bytes_read);
36586728 19109
0314b390 19110 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19111 DW_STRING_IS_CANONICAL (attr) = 0;
19112 info_ptr += bytes_read;
19113 }
4bdf3d34 19114 break;
2dc7f7b3 19115 case DW_FORM_exprloc:
c906108c 19116 case DW_FORM_block:
7b5a2f43 19117 blk = dwarf_alloc_block (cu);
c906108c
SS
19118 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19119 info_ptr += bytes_read;
19120 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19121 info_ptr += blk->size;
19122 DW_BLOCK (attr) = blk;
19123 break;
19124 case DW_FORM_block1:
7b5a2f43 19125 blk = dwarf_alloc_block (cu);
c906108c
SS
19126 blk->size = read_1_byte (abfd, info_ptr);
19127 info_ptr += 1;
19128 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19129 info_ptr += blk->size;
19130 DW_BLOCK (attr) = blk;
19131 break;
19132 case DW_FORM_data1:
19133 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19134 info_ptr += 1;
19135 break;
19136 case DW_FORM_flag:
19137 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19138 info_ptr += 1;
19139 break;
2dc7f7b3
TT
19140 case DW_FORM_flag_present:
19141 DW_UNSND (attr) = 1;
19142 break;
c906108c
SS
19143 case DW_FORM_sdata:
19144 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19145 info_ptr += bytes_read;
19146 break;
19147 case DW_FORM_udata:
18a8505e 19148 case DW_FORM_rnglistx:
c906108c
SS
19149 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19150 info_ptr += bytes_read;
19151 break;
19152 case DW_FORM_ref1:
9c541725 19153 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19154 + read_1_byte (abfd, info_ptr));
c906108c
SS
19155 info_ptr += 1;
19156 break;
19157 case DW_FORM_ref2:
9c541725 19158 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19159 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19160 info_ptr += 2;
19161 break;
19162 case DW_FORM_ref4:
9c541725 19163 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19164 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19165 info_ptr += 4;
19166 break;
613e1657 19167 case DW_FORM_ref8:
9c541725 19168 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19169 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19170 info_ptr += 8;
19171 break;
55f1336d 19172 case DW_FORM_ref_sig8:
ac9ec31b 19173 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19174 info_ptr += 8;
19175 break;
c906108c 19176 case DW_FORM_ref_udata:
9c541725 19177 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19178 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19179 info_ptr += bytes_read;
19180 break;
c906108c 19181 case DW_FORM_indirect:
a8329558
KW
19182 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19183 info_ptr += bytes_read;
43988095
JK
19184 if (form == DW_FORM_implicit_const)
19185 {
19186 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19187 info_ptr += bytes_read;
19188 }
19189 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19190 info_ptr, need_reprocess);
43988095
JK
19191 break;
19192 case DW_FORM_implicit_const:
19193 DW_SND (attr) = implicit_const;
a8329558 19194 break;
336d760d 19195 case DW_FORM_addrx:
3019eac3 19196 case DW_FORM_GNU_addr_index:
18a8505e
AT
19197 *need_reprocess = true;
19198 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19199 info_ptr += bytes_read;
19200 break;
cf532bd1 19201 case DW_FORM_strx:
15f18d14
AT
19202 case DW_FORM_strx1:
19203 case DW_FORM_strx2:
19204 case DW_FORM_strx3:
19205 case DW_FORM_strx4:
3019eac3 19206 case DW_FORM_GNU_str_index:
3019eac3 19207 {
15f18d14
AT
19208 ULONGEST str_index;
19209 if (form == DW_FORM_strx1)
19210 {
19211 str_index = read_1_byte (abfd, info_ptr);
19212 info_ptr += 1;
19213 }
19214 else if (form == DW_FORM_strx2)
19215 {
19216 str_index = read_2_bytes (abfd, info_ptr);
19217 info_ptr += 2;
19218 }
19219 else if (form == DW_FORM_strx3)
19220 {
19221 str_index = read_3_bytes (abfd, info_ptr);
19222 info_ptr += 3;
19223 }
19224 else if (form == DW_FORM_strx4)
19225 {
19226 str_index = read_4_bytes (abfd, info_ptr);
19227 info_ptr += 4;
19228 }
19229 else
19230 {
19231 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19232 info_ptr += bytes_read;
19233 }
18a8505e
AT
19234 *need_reprocess = true;
19235 DW_UNSND (attr) = str_index;
19236 }
3019eac3 19237 break;
c906108c 19238 default:
8a3fe4f8 19239 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19240 dwarf_form_name (form),
19241 bfd_get_filename (abfd));
c906108c 19242 }
28e94949 19243
36586728 19244 /* Super hack. */
cd6c91b4 19245 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19246 attr->form = DW_FORM_GNU_ref_alt;
19247
28e94949
JB
19248 /* We have seen instances where the compiler tried to emit a byte
19249 size attribute of -1 which ended up being encoded as an unsigned
19250 0xffffffff. Although 0xffffffff is technically a valid size value,
19251 an object of this size seems pretty unlikely so we can relatively
19252 safely treat these cases as if the size attribute was invalid and
19253 treat them as zero by default. */
19254 if (attr->name == DW_AT_byte_size
19255 && form == DW_FORM_data4
19256 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19257 {
19258 complaint
b98664d3 19259 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19260 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19261 DW_UNSND (attr) = 0;
19262 }
28e94949 19263
c906108c
SS
19264 return info_ptr;
19265}
19266
a8329558
KW
19267/* Read an attribute described by an abbreviated attribute. */
19268
d521ce57 19269static const gdb_byte *
dee91e82
DE
19270read_attribute (const struct die_reader_specs *reader,
19271 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19272 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19273{
19274 attr->name = abbrev->name;
43988095 19275 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19276 abbrev->implicit_const, info_ptr,
19277 need_reprocess);
a8329558
KW
19278}
19279
43988095
JK
19280/* Return pointer to string at .debug_str offset STR_OFFSET. */
19281
19282static const char *
ed2dc618 19283read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19284 LONGEST str_offset)
43988095 19285{
5989a64e
SM
19286 return dwarf2_per_objfile->per_bfd->str.read_string
19287 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19288}
19289
43988095
JK
19290/* Return pointer to string at .debug_str offset as read from BUF.
19291 BUF is assumed to be in a compilation unit described by CU_HEADER.
19292 Return *BYTES_READ_PTR count of bytes read from BUF. */
19293
d521ce57 19294static const char *
ed2dc618
SM
19295read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19296 const gdb_byte *buf,
cf2c3c16
TT
19297 const struct comp_unit_head *cu_header,
19298 unsigned int *bytes_read_ptr)
19299{
8266302d 19300 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19301
4f44ae6c 19302 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19303}
19304
86c0bb4c 19305/* See read.h. */
43988095 19306
86c0bb4c
TT
19307const char *
19308dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19309 const struct comp_unit_head *cu_header,
19310 unsigned int *bytes_read_ptr)
43988095 19311{
86c0bb4c 19312 bfd *abfd = objfile->obfd;
8266302d 19313 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19314
5989a64e 19315 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19316}
19317
3019eac3 19318/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19319 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19320 ADDR_SIZE is the size of addresses from the CU header. */
19321
19322static CORE_ADDR
ed2dc618 19323read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19324 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19325 int addr_size)
3019eac3
DE
19326{
19327 struct objfile *objfile = dwarf2_per_objfile->objfile;
19328 bfd *abfd = objfile->obfd;
19329 const gdb_byte *info_ptr;
18a8505e 19330 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19331
5989a64e
SM
19332 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19333 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19334 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19335 objfile_name (objfile));
18a8505e 19336 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19337 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19338 error (_("DW_FORM_addr_index pointing outside of "
19339 ".debug_addr section [in module %s]"),
4262abfb 19340 objfile_name (objfile));
5989a64e 19341 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19342 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19343 if (addr_size == 4)
19344 return bfd_get_32 (abfd, info_ptr);
19345 else
19346 return bfd_get_64 (abfd, info_ptr);
19347}
19348
19349/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19350
19351static CORE_ADDR
19352read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19353{
5e22e966 19354 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19355 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19356}
19357
19358/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19359
19360static CORE_ADDR
d521ce57 19361read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19362 unsigned int *bytes_read)
19363{
5e22e966 19364 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19365 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19366
19367 return read_addr_index (cu, addr_index);
19368}
19369
450a1bfc 19370/* See read.h. */
3019eac3
DE
19371
19372CORE_ADDR
450a1bfc 19373dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19374{
ed2dc618 19375 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19376 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19377 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19378 int addr_size;
19379
3019eac3
DE
19380 /* We need addr_base and addr_size.
19381 If we don't have PER_CU->cu, we have to get it.
19382 Nasty, but the alternative is storing the needed info in PER_CU,
19383 which at this point doesn't seem justified: it's not clear how frequently
19384 it would get used and it would increase the size of every PER_CU.
19385 Entry points like dwarf2_per_cu_addr_size do a similar thing
19386 so we're not in uncharted territory here.
19387 Alas we need to be a bit more complicated as addr_base is contained
19388 in the DIE.
19389
19390 We don't need to read the entire CU(/TU).
19391 We just need the header and top level die.
a1b64ce1 19392
3019eac3 19393 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19394 For now we skip this optimization. */
3019eac3
DE
19395
19396 if (cu != NULL)
19397 {
19398 addr_base = cu->addr_base;
19399 addr_size = cu->header.addr_size;
19400 }
19401 else
19402 {
6751ebae 19403 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
19404 addr_base = reader.cu->addr_base;
19405 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19406 }
19407
ed2dc618
SM
19408 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19409 addr_size);
3019eac3
DE
19410}
19411
18a8505e
AT
19412/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19413 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19414 DWO file. */
3019eac3 19415
d521ce57 19416static const char *
18a8505e
AT
19417read_str_index (struct dwarf2_cu *cu,
19418 struct dwarf2_section_info *str_section,
19419 struct dwarf2_section_info *str_offsets_section,
19420 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19421{
5e22e966 19422 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19423 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19424 const char *objf_name = objfile_name (objfile);
3019eac3 19425 bfd *abfd = objfile->obfd;
d521ce57 19426 const gdb_byte *info_ptr;
3019eac3 19427 ULONGEST str_offset;
cf532bd1 19428 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19429
96b79293
TT
19430 str_section->read (objfile);
19431 str_offsets_section->read (objfile);
73869dc2 19432 if (str_section->buffer == NULL)
18a8505e 19433 error (_("%s used without %s section"
9d8780f0 19434 " in CU at offset %s [in module %s]"),
96b79293 19435 form_name, str_section->get_name (),
18a8505e 19436 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19437 if (str_offsets_section->buffer == NULL)
18a8505e 19438 error (_("%s used without %s section"
9d8780f0 19439 " in CU at offset %s [in module %s]"),
96b79293 19440 form_name, str_section->get_name (),
18a8505e 19441 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19442 info_ptr = (str_offsets_section->buffer
18a8505e 19443 + str_offsets_base
3019eac3
DE
19444 + str_index * cu->header.offset_size);
19445 if (cu->header.offset_size == 4)
19446 str_offset = bfd_get_32 (abfd, info_ptr);
19447 else
19448 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19449 if (str_offset >= str_section->size)
57d63ce2 19450 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19451 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19452 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19453 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19454}
19455
18a8505e
AT
19456/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19457
19458static const char *
19459read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19460{
19461 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19462 ? reader->cu->header.addr_size : 0;
19463 return read_str_index (reader->cu,
19464 &reader->dwo_file->sections.str,
19465 &reader->dwo_file->sections.str_offsets,
19466 str_offsets_base, str_index);
19467}
19468
19469/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19470
19471static const char *
19472read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19473{
5e22e966 19474 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19475 const char *objf_name = objfile_name (objfile);
19476 static const char form_name[] = "DW_FORM_GNU_str_index";
19477 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19478
19479 if (!cu->str_offsets_base.has_value ())
19480 error (_("%s used in Fission stub without %s"
19481 " in CU at offset 0x%lx [in module %s]"),
19482 form_name, str_offsets_attr_name,
19483 (long) cu->header.offset_size, objf_name);
19484
19485 return read_str_index (cu,
5e22e966
SM
19486 &cu->per_objfile->per_bfd->str,
19487 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19488 *cu->str_offsets_base, str_index);
19489}
19490
3019eac3
DE
19491/* Return the length of an LEB128 number in BUF. */
19492
19493static int
19494leb128_size (const gdb_byte *buf)
19495{
19496 const gdb_byte *begin = buf;
19497 gdb_byte byte;
19498
19499 while (1)
19500 {
19501 byte = *buf++;
19502 if ((byte & 128) == 0)
19503 return buf - begin;
19504 }
19505}
19506
c906108c 19507static void
e142c38c 19508set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19509{
19510 switch (lang)
19511 {
19512 case DW_LANG_C89:
76bee0cc 19513 case DW_LANG_C99:
0cfd832f 19514 case DW_LANG_C11:
c906108c 19515 case DW_LANG_C:
d1be3247 19516 case DW_LANG_UPC:
e142c38c 19517 cu->language = language_c;
c906108c 19518 break;
9c37b5ae 19519 case DW_LANG_Java:
c906108c 19520 case DW_LANG_C_plus_plus:
0cfd832f
MW
19521 case DW_LANG_C_plus_plus_11:
19522 case DW_LANG_C_plus_plus_14:
e142c38c 19523 cu->language = language_cplus;
c906108c 19524 break;
6aecb9c2
JB
19525 case DW_LANG_D:
19526 cu->language = language_d;
19527 break;
c906108c
SS
19528 case DW_LANG_Fortran77:
19529 case DW_LANG_Fortran90:
b21b22e0 19530 case DW_LANG_Fortran95:
f7de9aab
MW
19531 case DW_LANG_Fortran03:
19532 case DW_LANG_Fortran08:
e142c38c 19533 cu->language = language_fortran;
c906108c 19534 break;
a766d390
DE
19535 case DW_LANG_Go:
19536 cu->language = language_go;
19537 break;
c906108c 19538 case DW_LANG_Mips_Assembler:
e142c38c 19539 cu->language = language_asm;
c906108c
SS
19540 break;
19541 case DW_LANG_Ada83:
8aaf0b47 19542 case DW_LANG_Ada95:
bc5f45f8
JB
19543 cu->language = language_ada;
19544 break;
72019c9c
GM
19545 case DW_LANG_Modula2:
19546 cu->language = language_m2;
19547 break;
fe8e67fd
PM
19548 case DW_LANG_Pascal83:
19549 cu->language = language_pascal;
19550 break;
22566fbd
DJ
19551 case DW_LANG_ObjC:
19552 cu->language = language_objc;
19553 break;
c44af4eb
TT
19554 case DW_LANG_Rust:
19555 case DW_LANG_Rust_old:
19556 cu->language = language_rust;
19557 break;
c906108c
SS
19558 case DW_LANG_Cobol74:
19559 case DW_LANG_Cobol85:
c906108c 19560 default:
e142c38c 19561 cu->language = language_minimal;
c906108c
SS
19562 break;
19563 }
e142c38c 19564 cu->language_defn = language_def (cu->language);
c906108c
SS
19565}
19566
19567/* Return the named attribute or NULL if not there. */
19568
19569static struct attribute *
e142c38c 19570dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19571{
a48e046c 19572 for (;;)
c906108c 19573 {
a48e046c
TT
19574 unsigned int i;
19575 struct attribute *spec = NULL;
19576
19577 for (i = 0; i < die->num_attrs; ++i)
19578 {
19579 if (die->attrs[i].name == name)
19580 return &die->attrs[i];
19581 if (die->attrs[i].name == DW_AT_specification
19582 || die->attrs[i].name == DW_AT_abstract_origin)
19583 spec = &die->attrs[i];
19584 }
19585
19586 if (!spec)
19587 break;
c906108c 19588
f2f0e013 19589 die = follow_die_ref (die, spec, &cu);
f2f0e013 19590 }
c5aa993b 19591
c906108c
SS
19592 return NULL;
19593}
19594
7d45c7c3
KB
19595/* Return the string associated with a string-typed attribute, or NULL if it
19596 is either not found or is of an incorrect type. */
19597
19598static const char *
19599dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19600{
19601 struct attribute *attr;
19602 const char *str = NULL;
19603
19604 attr = dwarf2_attr (die, name, cu);
19605
19606 if (attr != NULL)
19607 {
e61108c9
TT
19608 str = attr->value_as_string ();
19609 if (str == nullptr)
b98664d3 19610 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19611 "DIE at %s in module %s"),
19612 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19613 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19614 }
19615
19616 return str;
19617}
19618
a084a2a6 19619/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19620 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19621static const char *
19622dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19623{
19624 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19625 if (dwo_name == nullptr)
19626 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19627 return dwo_name;
19628}
19629
05cf31d1
JB
19630/* Return non-zero iff the attribute NAME is defined for the given DIE,
19631 and holds a non-zero value. This function should only be used for
2dc7f7b3 19632 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19633
19634static int
19635dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19636{
19637 struct attribute *attr = dwarf2_attr (die, name, cu);
19638
19639 return (attr && DW_UNSND (attr));
19640}
19641
3ca72b44 19642static int
e142c38c 19643die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19644{
05cf31d1
JB
19645 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19646 which value is non-zero. However, we have to be careful with
19647 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19648 (via dwarf2_flag_true_p) follows this attribute. So we may
19649 end up accidently finding a declaration attribute that belongs
19650 to a different DIE referenced by the specification attribute,
19651 even though the given DIE does not have a declaration attribute. */
19652 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19653 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19654}
19655
63d06c5c 19656/* Return the die giving the specification for DIE, if there is
f2f0e013 19657 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19658 containing the return value on output. If there is no
19659 specification, but there is an abstract origin, that is
19660 returned. */
63d06c5c
DC
19661
19662static struct die_info *
f2f0e013 19663die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19664{
f2f0e013
DJ
19665 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19666 *spec_cu);
63d06c5c 19667
edb3359d
DJ
19668 if (spec_attr == NULL)
19669 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19670
63d06c5c
DC
19671 if (spec_attr == NULL)
19672 return NULL;
19673 else
f2f0e013 19674 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19675}
c906108c 19676
527f3840
JK
19677/* Stub for free_line_header to match void * callback types. */
19678
19679static void
19680free_line_header_voidp (void *arg)
19681{
9a3c8263 19682 struct line_header *lh = (struct line_header *) arg;
527f3840 19683
fff8551c 19684 delete lh;
527f3840
JK
19685}
19686
83769d0b 19687/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19688
19689static struct dwarf2_section_info *
19690get_debug_line_section (struct dwarf2_cu *cu)
19691{
19692 struct dwarf2_section_info *section;
5e22e966 19693 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19694
19695 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19696 DWO file. */
19697 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19698 section = &cu->dwo_unit->dwo_file->sections.line;
19699 else if (cu->per_cu->is_dwz)
19700 {
c3699833 19701 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19702
19703 section = &dwz->line;
19704 }
19705 else
5989a64e 19706 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19707
19708 return section;
19709}
19710
debd256d 19711/* Read the statement program header starting at OFFSET in
3019eac3 19712 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19713 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19714 Returns NULL if there is a problem reading the header, e.g., if it
19715 has a version we don't understand.
debd256d
JB
19716
19717 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19718 the returned object point into the dwarf line section buffer,
19719 and must not be freed. */
ae2de4f8 19720
fff8551c 19721static line_header_up
9c541725 19722dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19723{
3019eac3 19724 struct dwarf2_section_info *section;
5e22e966 19725 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19726
36586728 19727 section = get_debug_line_section (cu);
96b79293 19728 section->read (dwarf2_per_objfile->objfile);
3019eac3 19729 if (section->buffer == NULL)
debd256d 19730 {
3019eac3 19731 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19732 complaint (_("missing .debug_line.dwo section"));
3019eac3 19733 else
b98664d3 19734 complaint (_("missing .debug_line section"));
debd256d
JB
19735 return 0;
19736 }
19737
0df7ad3a
TT
19738 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19739 dwarf2_per_objfile, section,
19740 &cu->header);
debd256d 19741}
c906108c 19742
c6da4cef 19743/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19744 Return the file name of the psymtab for the given file_entry.
c6da4cef 19745 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19746 If space for the result is malloc'd, *NAME_HOLDER will be set.
19747 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19748
d521ce57 19749static const char *
7ba99d21 19750psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19751 const dwarf2_psymtab *pst,
c89b44cd
TT
19752 const char *comp_dir,
19753 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19754{
d521ce57
TT
19755 const char *include_name = fe.name;
19756 const char *include_name_to_compare = include_name;
72b9f47f 19757 const char *pst_filename;
c6da4cef
DE
19758 int file_is_pst;
19759
8c43009f 19760 const char *dir_name = fe.include_dir (lh);
c6da4cef 19761
c89b44cd 19762 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19763 if (!IS_ABSOLUTE_PATH (include_name)
19764 && (dir_name != NULL || comp_dir != NULL))
19765 {
19766 /* Avoid creating a duplicate psymtab for PST.
19767 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19768 Before we do the comparison, however, we need to account
19769 for DIR_NAME and COMP_DIR.
19770 First prepend dir_name (if non-NULL). If we still don't
19771 have an absolute path prepend comp_dir (if non-NULL).
19772 However, the directory we record in the include-file's
19773 psymtab does not contain COMP_DIR (to match the
19774 corresponding symtab(s)).
19775
19776 Example:
19777
19778 bash$ cd /tmp
19779 bash$ gcc -g ./hello.c
19780 include_name = "hello.c"
19781 dir_name = "."
19782 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19783 DW_AT_name = "./hello.c"
19784
19785 */
c6da4cef
DE
19786
19787 if (dir_name != NULL)
19788 {
c89b44cd
TT
19789 name_holder->reset (concat (dir_name, SLASH_STRING,
19790 include_name, (char *) NULL));
19791 include_name = name_holder->get ();
c6da4cef 19792 include_name_to_compare = include_name;
c6da4cef
DE
19793 }
19794 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19795 {
c89b44cd
TT
19796 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19797 include_name, (char *) NULL));
19798 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19799 }
19800 }
19801
19802 pst_filename = pst->filename;
c89b44cd 19803 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19804 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19805 {
c89b44cd
TT
19806 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19807 pst_filename, (char *) NULL));
19808 pst_filename = copied_name.get ();
c6da4cef
DE
19809 }
19810
1e3fad37 19811 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19812
c6da4cef
DE
19813 if (file_is_pst)
19814 return NULL;
19815 return include_name;
19816}
19817
d9b3de22
DE
19818/* State machine to track the state of the line number program. */
19819
6f77053d 19820class lnp_state_machine
d9b3de22 19821{
6f77053d
PA
19822public:
19823 /* Initialize a machine state for the start of a line number
19824 program. */
804d2729
TT
19825 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19826 bool record_lines_p);
6f77053d 19827
8c43009f
PA
19828 file_entry *current_file ()
19829 {
19830 /* lh->file_names is 0-based, but the file name numbers in the
19831 statement program are 1-based. */
6f77053d
PA
19832 return m_line_header->file_name_at (m_file);
19833 }
19834
19835 /* Record the line in the state machine. END_SEQUENCE is true if
19836 we're processing the end of a sequence. */
19837 void record_line (bool end_sequence);
19838
7ab6656f
OJ
19839 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19840 nop-out rest of the lines in this sequence. */
6f77053d
PA
19841 void check_line_address (struct dwarf2_cu *cu,
19842 const gdb_byte *line_ptr,
7ab6656f 19843 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19844
19845 void handle_set_discriminator (unsigned int discriminator)
19846 {
19847 m_discriminator = discriminator;
19848 m_line_has_non_zero_discriminator |= discriminator != 0;
19849 }
19850
19851 /* Handle DW_LNE_set_address. */
19852 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19853 {
19854 m_op_index = 0;
19855 address += baseaddr;
19856 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19857 }
19858
19859 /* Handle DW_LNS_advance_pc. */
19860 void handle_advance_pc (CORE_ADDR adjust);
19861
19862 /* Handle a special opcode. */
19863 void handle_special_opcode (unsigned char op_code);
19864
19865 /* Handle DW_LNS_advance_line. */
19866 void handle_advance_line (int line_delta)
19867 {
19868 advance_line (line_delta);
19869 }
19870
19871 /* Handle DW_LNS_set_file. */
19872 void handle_set_file (file_name_index file);
19873
19874 /* Handle DW_LNS_negate_stmt. */
19875 void handle_negate_stmt ()
19876 {
19877 m_is_stmt = !m_is_stmt;
19878 }
19879
19880 /* Handle DW_LNS_const_add_pc. */
19881 void handle_const_add_pc ();
19882
19883 /* Handle DW_LNS_fixed_advance_pc. */
19884 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19885 {
19886 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19887 m_op_index = 0;
19888 }
19889
19890 /* Handle DW_LNS_copy. */
19891 void handle_copy ()
19892 {
19893 record_line (false);
19894 m_discriminator = 0;
19895 }
19896
19897 /* Handle DW_LNE_end_sequence. */
19898 void handle_end_sequence ()
19899 {
804d2729 19900 m_currently_recording_lines = true;
6f77053d
PA
19901 }
19902
19903private:
19904 /* Advance the line by LINE_DELTA. */
19905 void advance_line (int line_delta)
19906 {
19907 m_line += line_delta;
19908
19909 if (line_delta != 0)
19910 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19911 }
19912
804d2729
TT
19913 struct dwarf2_cu *m_cu;
19914
6f77053d
PA
19915 gdbarch *m_gdbarch;
19916
19917 /* True if we're recording lines.
19918 Otherwise we're building partial symtabs and are just interested in
19919 finding include files mentioned by the line number program. */
19920 bool m_record_lines_p;
19921
8c43009f 19922 /* The line number header. */
6f77053d 19923 line_header *m_line_header;
8c43009f 19924
6f77053d
PA
19925 /* These are part of the standard DWARF line number state machine,
19926 and initialized according to the DWARF spec. */
d9b3de22 19927
6f77053d 19928 unsigned char m_op_index = 0;
7ba99d21
AT
19929 /* The line table index of the current file. */
19930 file_name_index m_file = 1;
6f77053d
PA
19931 unsigned int m_line = 1;
19932
19933 /* These are initialized in the constructor. */
19934
19935 CORE_ADDR m_address;
19936 bool m_is_stmt;
19937 unsigned int m_discriminator;
d9b3de22
DE
19938
19939 /* Additional bits of state we need to track. */
19940
19941 /* The last file that we called dwarf2_start_subfile for.
19942 This is only used for TLLs. */
6f77053d 19943 unsigned int m_last_file = 0;
d9b3de22 19944 /* The last file a line number was recorded for. */
6f77053d 19945 struct subfile *m_last_subfile = NULL;
d9b3de22 19946
804d2729
TT
19947 /* When true, record the lines we decode. */
19948 bool m_currently_recording_lines = false;
d9b3de22
DE
19949
19950 /* The last line number that was recorded, used to coalesce
19951 consecutive entries for the same line. This can happen, for
19952 example, when discriminators are present. PR 17276. */
6f77053d
PA
19953 unsigned int m_last_line = 0;
19954 bool m_line_has_non_zero_discriminator = false;
8c43009f 19955};
d9b3de22 19956
6f77053d
PA
19957void
19958lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19959{
19960 CORE_ADDR addr_adj = (((m_op_index + adjust)
19961 / m_line_header->maximum_ops_per_instruction)
19962 * m_line_header->minimum_instruction_length);
19963 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19964 m_op_index = ((m_op_index + adjust)
19965 % m_line_header->maximum_ops_per_instruction);
19966}
d9b3de22 19967
6f77053d
PA
19968void
19969lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19970{
6f77053d 19971 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19972 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19973 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19974 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19975 / m_line_header->maximum_ops_per_instruction)
19976 * m_line_header->minimum_instruction_length);
19977 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19978 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19979 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19980
258bf0ee 19981 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19982 advance_line (line_delta);
19983 record_line (false);
19984 m_discriminator = 0;
19985}
d9b3de22 19986
6f77053d
PA
19987void
19988lnp_state_machine::handle_set_file (file_name_index file)
19989{
19990 m_file = file;
19991
19992 const file_entry *fe = current_file ();
19993 if (fe == NULL)
19994 dwarf2_debug_line_missing_file_complaint ();
19995 else if (m_record_lines_p)
19996 {
19997 const char *dir = fe->include_dir (m_line_header);
19998
c24bdb02 19999 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20000 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20001 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20002 }
20003}
20004
20005void
20006lnp_state_machine::handle_const_add_pc ()
20007{
20008 CORE_ADDR adjust
20009 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20010
20011 CORE_ADDR addr_adj
20012 = (((m_op_index + adjust)
20013 / m_line_header->maximum_ops_per_instruction)
20014 * m_line_header->minimum_instruction_length);
20015
20016 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20017 m_op_index = ((m_op_index + adjust)
20018 % m_line_header->maximum_ops_per_instruction);
20019}
d9b3de22 20020
a05a36a5
DE
20021/* Return non-zero if we should add LINE to the line number table.
20022 LINE is the line to add, LAST_LINE is the last line that was added,
20023 LAST_SUBFILE is the subfile for LAST_LINE.
20024 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20025 had a non-zero discriminator.
20026
20027 We have to be careful in the presence of discriminators.
20028 E.g., for this line:
20029
20030 for (i = 0; i < 100000; i++);
20031
20032 clang can emit four line number entries for that one line,
20033 each with a different discriminator.
20034 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20035
20036 However, we want gdb to coalesce all four entries into one.
20037 Otherwise the user could stepi into the middle of the line and
20038 gdb would get confused about whether the pc really was in the
20039 middle of the line.
20040
20041 Things are further complicated by the fact that two consecutive
20042 line number entries for the same line is a heuristic used by gcc
20043 to denote the end of the prologue. So we can't just discard duplicate
20044 entries, we have to be selective about it. The heuristic we use is
20045 that we only collapse consecutive entries for the same line if at least
20046 one of those entries has a non-zero discriminator. PR 17276.
20047
20048 Note: Addresses in the line number state machine can never go backwards
20049 within one sequence, thus this coalescing is ok. */
20050
20051static int
804d2729
TT
20052dwarf_record_line_p (struct dwarf2_cu *cu,
20053 unsigned int line, unsigned int last_line,
a05a36a5
DE
20054 int line_has_non_zero_discriminator,
20055 struct subfile *last_subfile)
20056{
c24bdb02 20057 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20058 return 1;
20059 if (line != last_line)
20060 return 1;
20061 /* Same line for the same file that we've seen already.
20062 As a last check, for pr 17276, only record the line if the line
20063 has never had a non-zero discriminator. */
20064 if (!line_has_non_zero_discriminator)
20065 return 1;
20066 return 0;
20067}
20068
804d2729
TT
20069/* Use the CU's builder to record line number LINE beginning at
20070 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20071
20072static void
d9b3de22 20073dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20074 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20075 struct dwarf2_cu *cu)
252a6764
DE
20076{
20077 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20078
27e0867f
DE
20079 if (dwarf_line_debug)
20080 {
20081 fprintf_unfiltered (gdb_stdlog,
20082 "Recording line %u, file %s, address %s\n",
20083 line, lbasename (subfile->name),
20084 paddress (gdbarch, address));
20085 }
20086
804d2729 20087 if (cu != nullptr)
8c95582d 20088 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20089}
20090
20091/* Subroutine of dwarf_decode_lines_1 to simplify it.
20092 Mark the end of a set of line number records.
d9b3de22 20093 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20094 If SUBFILE is NULL the request is ignored. */
20095
20096static void
20097dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20098 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20099{
27e0867f
DE
20100 if (subfile == NULL)
20101 return;
20102
20103 if (dwarf_line_debug)
20104 {
20105 fprintf_unfiltered (gdb_stdlog,
20106 "Finishing current line, file %s, address %s\n",
20107 lbasename (subfile->name),
20108 paddress (gdbarch, address));
20109 }
20110
8c95582d 20111 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20112}
20113
6f77053d
PA
20114void
20115lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20116{
d9b3de22
DE
20117 if (dwarf_line_debug)
20118 {
20119 fprintf_unfiltered (gdb_stdlog,
20120 "Processing actual line %u: file %u,"
94a72be7 20121 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20122 m_line, m_file,
6f77053d 20123 paddress (m_gdbarch, m_address),
94a72be7
AB
20124 m_is_stmt, m_discriminator,
20125 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20126 }
20127
6f77053d 20128 file_entry *fe = current_file ();
8c43009f
PA
20129
20130 if (fe == NULL)
d9b3de22
DE
20131 dwarf2_debug_line_missing_file_complaint ();
20132 /* For now we ignore lines not starting on an instruction boundary.
20133 But not when processing end_sequence for compatibility with the
20134 previous version of the code. */
6f77053d 20135 else if (m_op_index == 0 || end_sequence)
d9b3de22 20136 {
8c43009f 20137 fe->included_p = 1;
8c95582d 20138 if (m_record_lines_p)
d9b3de22 20139 {
c24bdb02 20140 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20141 || end_sequence)
d9b3de22 20142 {
804d2729
TT
20143 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20144 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20145 }
20146
20147 if (!end_sequence)
20148 {
8c95582d
AB
20149 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20150
804d2729 20151 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20152 m_line_has_non_zero_discriminator,
20153 m_last_subfile))
d9b3de22 20154 {
c24bdb02 20155 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20156 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20157 builder->get_current_subfile (),
8c95582d 20158 m_line, m_address, is_stmt,
804d2729 20159 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20160 }
c24bdb02 20161 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20162 m_last_line = m_line;
d9b3de22
DE
20163 }
20164 }
20165 }
20166}
20167
804d2729
TT
20168lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20169 line_header *lh, bool record_lines_p)
d9b3de22 20170{
804d2729 20171 m_cu = cu;
6f77053d
PA
20172 m_gdbarch = arch;
20173 m_record_lines_p = record_lines_p;
20174 m_line_header = lh;
d9b3de22 20175
804d2729 20176 m_currently_recording_lines = true;
d9b3de22 20177
d9b3de22
DE
20178 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20179 was a line entry for it so that the backend has a chance to adjust it
20180 and also record it in case it needs it. This is currently used by MIPS
20181 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20182 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20183 m_is_stmt = lh->default_is_stmt;
20184 m_discriminator = 0;
252a6764
DE
20185}
20186
6f77053d
PA
20187void
20188lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20189 const gdb_byte *line_ptr,
7ab6656f 20190 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20191{
7ab6656f
OJ
20192 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20193 the pc range of the CU. However, we restrict the test to only ADDRESS
20194 values of zero to preserve GDB's previous behaviour which is to handle
20195 the specific case of a function being GC'd by the linker. */
924c2928 20196
7ab6656f 20197 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20198 {
20199 /* This line table is for a function which has been
20200 GCd by the linker. Ignore it. PR gdb/12528 */
20201
5e22e966 20202 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20203 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20204
b98664d3 20205 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20206 line_offset, objfile_name (objfile));
804d2729
TT
20207 m_currently_recording_lines = false;
20208 /* Note: m_currently_recording_lines is left as false until we see
20209 DW_LNE_end_sequence. */
924c2928
DE
20210 }
20211}
20212
f3f5162e 20213/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20214 Process the line number information in LH.
20215 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20216 program in order to set included_p for every referenced header. */
debd256d 20217
c906108c 20218static void
43f3e411
DE
20219dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20220 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20221{
d521ce57
TT
20222 const gdb_byte *line_ptr, *extended_end;
20223 const gdb_byte *line_end;
a8c50c1f 20224 unsigned int bytes_read, extended_len;
699ca60a 20225 unsigned char op_code, extended_op;
e142c38c 20226 CORE_ADDR baseaddr;
5e22e966 20227 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20228 bfd *abfd = objfile->obfd;
08feed99 20229 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20230 /* True if we're recording line info (as opposed to building partial
20231 symtabs and just interested in finding include files mentioned by
20232 the line number program). */
20233 bool record_lines_p = !decode_for_pst_p;
e142c38c 20234
b3b3bada 20235 baseaddr = objfile->text_section_offset ();
c906108c 20236
debd256d
JB
20237 line_ptr = lh->statement_program_start;
20238 line_end = lh->statement_program_end;
c906108c
SS
20239
20240 /* Read the statement sequences until there's nothing left. */
20241 while (line_ptr < line_end)
20242 {
6f77053d
PA
20243 /* The DWARF line number program state machine. Reset the state
20244 machine at the start of each sequence. */
804d2729 20245 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20246 bool end_sequence = false;
d9b3de22 20247
8c43009f 20248 if (record_lines_p)
c906108c 20249 {
8c43009f
PA
20250 /* Start a subfile for the current file of the state
20251 machine. */
20252 const file_entry *fe = state_machine.current_file ();
20253
20254 if (fe != NULL)
804d2729 20255 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20256 }
20257
a738430d 20258 /* Decode the table. */
d9b3de22 20259 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20260 {
20261 op_code = read_1_byte (abfd, line_ptr);
20262 line_ptr += 1;
9aa1fe7e 20263
debd256d 20264 if (op_code >= lh->opcode_base)
6e70227d 20265 {
8e07a239 20266 /* Special opcode. */
6f77053d 20267 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20268 }
20269 else switch (op_code)
c906108c
SS
20270 {
20271 case DW_LNS_extended_op:
3e43a32a
MS
20272 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20273 &bytes_read);
473b7be6 20274 line_ptr += bytes_read;
a8c50c1f 20275 extended_end = line_ptr + extended_len;
c906108c
SS
20276 extended_op = read_1_byte (abfd, line_ptr);
20277 line_ptr += 1;
20278 switch (extended_op)
20279 {
20280 case DW_LNE_end_sequence:
6f77053d
PA
20281 state_machine.handle_end_sequence ();
20282 end_sequence = true;
c906108c
SS
20283 break;
20284 case DW_LNE_set_address:
d9b3de22
DE
20285 {
20286 CORE_ADDR address
c8a7a66f 20287 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20288 line_ptr += bytes_read;
6f77053d
PA
20289
20290 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20291 lowpc - baseaddr, address);
6f77053d 20292 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20293 }
c906108c
SS
20294 break;
20295 case DW_LNE_define_file:
debd256d 20296 {
d521ce57 20297 const char *cur_file;
ecfb656c
PA
20298 unsigned int mod_time, length;
20299 dir_index dindex;
6e70227d 20300
3e43a32a
MS
20301 cur_file = read_direct_string (abfd, line_ptr,
20302 &bytes_read);
debd256d 20303 line_ptr += bytes_read;
ecfb656c 20304 dindex = (dir_index)
debd256d
JB
20305 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20306 line_ptr += bytes_read;
20307 mod_time =
20308 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20309 line_ptr += bytes_read;
20310 length =
20311 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20312 line_ptr += bytes_read;
ecfb656c 20313 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20314 }
c906108c 20315 break;
d0c6ba3d 20316 case DW_LNE_set_discriminator:
6f77053d
PA
20317 {
20318 /* The discriminator is not interesting to the
20319 debugger; just ignore it. We still need to
20320 check its value though:
20321 if there are consecutive entries for the same
20322 (non-prologue) line we want to coalesce them.
20323 PR 17276. */
20324 unsigned int discr
20325 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20326 line_ptr += bytes_read;
20327
20328 state_machine.handle_set_discriminator (discr);
20329 }
d0c6ba3d 20330 break;
c906108c 20331 default:
b98664d3 20332 complaint (_("mangled .debug_line section"));
debd256d 20333 return;
c906108c 20334 }
a8c50c1f
DJ
20335 /* Make sure that we parsed the extended op correctly. If e.g.
20336 we expected a different address size than the producer used,
20337 we may have read the wrong number of bytes. */
20338 if (line_ptr != extended_end)
20339 {
b98664d3 20340 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20341 return;
20342 }
c906108c
SS
20343 break;
20344 case DW_LNS_copy:
6f77053d 20345 state_machine.handle_copy ();
c906108c
SS
20346 break;
20347 case DW_LNS_advance_pc:
2dc7f7b3
TT
20348 {
20349 CORE_ADDR adjust
20350 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20351 line_ptr += bytes_read;
6f77053d
PA
20352
20353 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20354 }
c906108c
SS
20355 break;
20356 case DW_LNS_advance_line:
a05a36a5
DE
20357 {
20358 int line_delta
20359 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20360 line_ptr += bytes_read;
6f77053d
PA
20361
20362 state_machine.handle_advance_line (line_delta);
a05a36a5 20363 }
c906108c
SS
20364 break;
20365 case DW_LNS_set_file:
d9b3de22 20366 {
6f77053d 20367 file_name_index file
ecfb656c
PA
20368 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20369 &bytes_read);
d9b3de22 20370 line_ptr += bytes_read;
8c43009f 20371
6f77053d 20372 state_machine.handle_set_file (file);
d9b3de22 20373 }
c906108c
SS
20374 break;
20375 case DW_LNS_set_column:
0ad93d4f 20376 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20377 line_ptr += bytes_read;
20378 break;
20379 case DW_LNS_negate_stmt:
6f77053d 20380 state_machine.handle_negate_stmt ();
c906108c
SS
20381 break;
20382 case DW_LNS_set_basic_block:
c906108c 20383 break;
c2c6d25f
JM
20384 /* Add to the address register of the state machine the
20385 address increment value corresponding to special opcode
a738430d
MK
20386 255. I.e., this value is scaled by the minimum
20387 instruction length since special opcode 255 would have
b021a221 20388 scaled the increment. */
c906108c 20389 case DW_LNS_const_add_pc:
6f77053d 20390 state_machine.handle_const_add_pc ();
c906108c
SS
20391 break;
20392 case DW_LNS_fixed_advance_pc:
3e29f34a 20393 {
6f77053d 20394 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20395 line_ptr += 2;
6f77053d
PA
20396
20397 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20398 }
c906108c 20399 break;
9aa1fe7e 20400 default:
a738430d
MK
20401 {
20402 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20403 int i;
a738430d 20404
debd256d 20405 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20406 {
20407 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20408 line_ptr += bytes_read;
20409 }
20410 }
c906108c
SS
20411 }
20412 }
d9b3de22
DE
20413
20414 if (!end_sequence)
20415 dwarf2_debug_line_missing_end_sequence_complaint ();
20416
20417 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20418 in which case we still finish recording the last line). */
6f77053d 20419 state_machine.record_line (true);
c906108c 20420 }
f3f5162e
DE
20421}
20422
20423/* Decode the Line Number Program (LNP) for the given line_header
20424 structure and CU. The actual information extracted and the type
20425 of structures created from the LNP depends on the value of PST.
20426
20427 1. If PST is NULL, then this procedure uses the data from the program
20428 to create all necessary symbol tables, and their linetables.
20429
20430 2. If PST is not NULL, this procedure reads the program to determine
20431 the list of files included by the unit represented by PST, and
20432 builds all the associated partial symbol tables.
20433
20434 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20435 It is used for relative paths in the line table.
20436 NOTE: When processing partial symtabs (pst != NULL),
20437 comp_dir == pst->dirname.
20438
20439 NOTE: It is important that psymtabs have the same file name (via strcmp)
20440 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20441 symtab we don't use it in the name of the psymtabs we create.
20442 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20443 A good testcase for this is mb-inline.exp.
20444
527f3840
JK
20445 LOWPC is the lowest address in CU (or 0 if not known).
20446
20447 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20448 for its PC<->lines mapping information. Otherwise only the filename
20449 table is read in. */
f3f5162e
DE
20450
20451static void
20452dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20453 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20454 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20455{
5e22e966 20456 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20457 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20458
527f3840
JK
20459 if (decode_mapping)
20460 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20461
20462 if (decode_for_pst_p)
20463 {
aaa75496
JB
20464 /* Now that we're done scanning the Line Header Program, we can
20465 create the psymtab of each included file. */
7ba99d21
AT
20466 for (auto &file_entry : lh->file_names ())
20467 if (file_entry.included_p == 1)
aaa75496 20468 {
c89b44cd 20469 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20470 const char *include_name =
7ba99d21
AT
20471 psymtab_include_file_name (lh, file_entry, pst,
20472 comp_dir, &name_holder);
c6da4cef 20473 if (include_name != NULL)
aaa75496
JB
20474 dwarf2_create_include_psymtab (include_name, pst, objfile);
20475 }
20476 }
cb1df416
DJ
20477 else
20478 {
20479 /* Make sure a symtab is created for every file, even files
20480 which contain only variables (i.e. no code with associated
20481 line numbers). */
c24bdb02
KS
20482 buildsym_compunit *builder = cu->get_builder ();
20483 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20484
7ba99d21 20485 for (auto &fe : lh->file_names ())
cb1df416 20486 {
804d2729 20487 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20488 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20489 {
c24bdb02 20490 builder->get_current_subfile ()->symtab
804d2729 20491 = allocate_symtab (cust,
c24bdb02 20492 builder->get_current_subfile ()->name);
43f3e411 20493 }
c24bdb02 20494 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20495 }
20496 }
c906108c
SS
20497}
20498
20499/* Start a subfile for DWARF. FILENAME is the name of the file and
20500 DIRNAME the name of the source directory which contains FILENAME
4d663531 20501 or NULL if not known.
c906108c
SS
20502 This routine tries to keep line numbers from identical absolute and
20503 relative file names in a common subfile.
20504
20505 Using the `list' example from the GDB testsuite, which resides in
20506 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20507 of /srcdir/list0.c yields the following debugging information for list0.c:
20508
c5aa993b 20509 DW_AT_name: /srcdir/list0.c
4d663531 20510 DW_AT_comp_dir: /compdir
357e46e7 20511 files.files[0].name: list0.h
c5aa993b 20512 files.files[0].dir: /srcdir
357e46e7 20513 files.files[1].name: list0.c
c5aa993b 20514 files.files[1].dir: /srcdir
c906108c
SS
20515
20516 The line number information for list0.c has to end up in a single
4f1520fb
FR
20517 subfile, so that `break /srcdir/list0.c:1' works as expected.
20518 start_subfile will ensure that this happens provided that we pass the
20519 concatenation of files.files[1].dir and files.files[1].name as the
20520 subfile's name. */
c906108c
SS
20521
20522static void
804d2729
TT
20523dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20524 const char *dirname)
c906108c 20525{
43816ebc 20526 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20527
4d663531 20528 /* In order not to lose the line information directory,
4f1520fb
FR
20529 we concatenate it to the filename when it makes sense.
20530 Note that the Dwarf3 standard says (speaking of filenames in line
20531 information): ``The directory index is ignored for file names
20532 that represent full path names''. Thus ignoring dirname in the
20533 `else' branch below isn't an issue. */
c906108c 20534
d5166ae1 20535 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20536 {
43816ebc
TT
20537 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20538 filename = copy.get ();
d521ce57 20539 }
c906108c 20540
c24bdb02 20541 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20542}
20543
804d2729
TT
20544/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20545 buildsym_compunit constructor. */
f4dc4d17 20546
c24bdb02
KS
20547struct compunit_symtab *
20548dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20549 CORE_ADDR low_pc)
f4dc4d17 20550{
c24bdb02 20551 gdb_assert (m_builder == nullptr);
43f3e411 20552
c24bdb02
KS
20553 m_builder.reset (new struct buildsym_compunit
20554 (per_cu->dwarf2_per_objfile->objfile,
20555 name, comp_dir, language, low_pc));
93b8bea4 20556
c24bdb02 20557 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20558
c24bdb02
KS
20559 get_builder ()->record_debugformat ("DWARF 2");
20560 get_builder ()->record_producer (producer);
f4dc4d17 20561
c24bdb02 20562 processing_has_namespace_info = false;
43f3e411 20563
c24bdb02 20564 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20565}
20566
4c2df51b
DJ
20567static void
20568var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20569 struct dwarf2_cu *cu)
4c2df51b 20570{
5e22e966 20571 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20572 struct comp_unit_head *cu_header = &cu->header;
20573
4c2df51b
DJ
20574 /* NOTE drow/2003-01-30: There used to be a comment and some special
20575 code here to turn a symbol with DW_AT_external and a
20576 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20577 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20578 with some versions of binutils) where shared libraries could have
20579 relocations against symbols in their debug information - the
20580 minimal symbol would have the right address, but the debug info
20581 would not. It's no longer necessary, because we will explicitly
20582 apply relocations when we read in the debug information now. */
20583
20584 /* A DW_AT_location attribute with no contents indicates that a
20585 variable has been optimized away. */
4fc6c0d5 20586 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20587 {
f1e6e072 20588 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20589 return;
20590 }
20591
20592 /* Handle one degenerate form of location expression specially, to
20593 preserve GDB's previous behavior when section offsets are
336d760d
AT
20594 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20595 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20596
4fc6c0d5 20597 if (attr->form_is_block ()
3019eac3
DE
20598 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20599 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20600 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20601 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20602 && (DW_BLOCK (attr)->size
20603 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20604 {
891d2f0b 20605 unsigned int dummy;
4c2df51b 20606
3019eac3 20607 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20608 SET_SYMBOL_VALUE_ADDRESS
20609 (sym, cu->header.read_address (objfile->obfd,
20610 DW_BLOCK (attr)->data + 1,
20611 &dummy));
3019eac3 20612 else
38583298
TT
20613 SET_SYMBOL_VALUE_ADDRESS
20614 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20615 &dummy));
f1e6e072 20616 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20617 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20618 SET_SYMBOL_VALUE_ADDRESS
20619 (sym,
20620 SYMBOL_VALUE_ADDRESS (sym)
20621 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20622 return;
20623 }
20624
20625 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20626 expression evaluator, and use LOC_COMPUTED only when necessary
20627 (i.e. when the value of a register or memory location is
20628 referenced, or a thread-local block, etc.). Then again, it might
20629 not be worthwhile. I'm assuming that it isn't unless performance
20630 or memory numbers show me otherwise. */
20631
f1e6e072 20632 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20633
f1e6e072 20634 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20635 cu->has_loclist = true;
4c2df51b
DJ
20636}
20637
c906108c
SS
20638/* Given a pointer to a DWARF information entry, figure out if we need
20639 to make a symbol table entry for it, and if so, create a new entry
20640 and return a pointer to it.
20641 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20642 used the passed type.
20643 If SPACE is not NULL, use it to hold the new symbol. If it is
20644 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20645
20646static struct symbol *
5e2db402
TT
20647new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20648 struct symbol *space)
c906108c 20649{
5e22e966 20650 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20651 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20652 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20653 struct symbol *sym = NULL;
15d034d0 20654 const char *name;
c906108c
SS
20655 struct attribute *attr = NULL;
20656 struct attribute *attr2 = NULL;
e142c38c 20657 CORE_ADDR baseaddr;
e37fd15a
SW
20658 struct pending **list_to_add = NULL;
20659
edb3359d 20660 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20661
b3b3bada 20662 baseaddr = objfile->text_section_offset ();
c906108c 20663
94af9270 20664 name = dwarf2_name (die, cu);
c906108c
SS
20665 if (name)
20666 {
34eaf542 20667 int suppress_add = 0;
94af9270 20668
34eaf542
TT
20669 if (space)
20670 sym = space;
20671 else
8c14c3a3 20672 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20673 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20674
20675 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20676 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20677 /* Fortran does not have mangling standard and the mangling does differ
20678 between gfortran, iFort etc. */
bcfe6157
TT
20679 const char *physname
20680 = (cu->language == language_fortran
20681 ? dwarf2_full_name (name, die, cu)
20682 : dwarf2_physname (name, die, cu));
20683 const char *linkagename = dw2_linkage_name (die, cu);
20684
20685 if (linkagename == nullptr || cu->language == language_ada)
20686 sym->set_linkage_name (physname);
20687 else
20688 {
20689 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20690 sym->set_linkage_name (linkagename);
20691 }
f55ee35c 20692
c906108c 20693 /* Default assumptions.
c5aa993b 20694 Use the passed type or decode it from the die. */
176620f1 20695 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20696 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20697 if (type != NULL)
20698 SYMBOL_TYPE (sym) = type;
20699 else
e7c27a73 20700 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20701 attr = dwarf2_attr (die,
20702 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20703 cu);
435d3d88 20704 if (attr != nullptr)
c906108c
SS
20705 {
20706 SYMBOL_LINE (sym) = DW_UNSND (attr);
20707 }
cb1df416 20708
edb3359d
DJ
20709 attr = dwarf2_attr (die,
20710 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20711 cu);
435d3d88 20712 if (attr != nullptr)
cb1df416 20713 {
ecfb656c 20714 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20715 struct file_entry *fe;
9a619af0 20716
ecfb656c
PA
20717 if (cu->line_header != NULL)
20718 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20719 else
20720 fe = NULL;
20721
20722 if (fe == NULL)
b98664d3 20723 complaint (_("file index out of range"));
8c43009f
PA
20724 else
20725 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20726 }
20727
c906108c
SS
20728 switch (die->tag)
20729 {
20730 case DW_TAG_label:
e142c38c 20731 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20732 if (attr != nullptr)
3e29f34a
MR
20733 {
20734 CORE_ADDR addr;
20735
cd6c91b4 20736 addr = attr->value_as_address ();
3e29f34a 20737 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20738 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20739 }
0f5238ed
TT
20740 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20741 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20742 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20743 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20744 break;
20745 case DW_TAG_subprogram:
20746 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20747 finish_block. */
f1e6e072 20748 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20749 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20750 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20751 || cu->language == language_ada
20752 || cu->language == language_fortran)
c906108c 20753 {
2cfa0c8d 20754 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20755 Ada and Fortran subprograms, whether marked external or
20756 not, are always stored as a global symbol, because we want
20757 to be able to access them globally. For instance, we want
20758 to be able to break on a nested subprogram without having
20759 to specify the context. */
c24bdb02 20760 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20761 }
20762 else
20763 {
e37fd15a 20764 list_to_add = cu->list_in_scope;
c906108c
SS
20765 }
20766 break;
edb3359d
DJ
20767 case DW_TAG_inlined_subroutine:
20768 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20769 finish_block. */
f1e6e072 20770 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20771 SYMBOL_INLINED (sym) = 1;
481860b3 20772 list_to_add = cu->list_in_scope;
edb3359d 20773 break;
34eaf542
TT
20774 case DW_TAG_template_value_param:
20775 suppress_add = 1;
20776 /* Fall through. */
72929c62 20777 case DW_TAG_constant:
c906108c 20778 case DW_TAG_variable:
254e6b9e 20779 case DW_TAG_member:
0963b4bd
MS
20780 /* Compilation with minimal debug info may result in
20781 variables with missing type entries. Change the
20782 misleading `void' type to something sensible. */
78134374 20783 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20784 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20785
e142c38c 20786 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20787 /* In the case of DW_TAG_member, we should only be called for
20788 static const members. */
20789 if (die->tag == DW_TAG_member)
20790 {
3863f96c
DE
20791 /* dwarf2_add_field uses die_is_declaration,
20792 so we do the same. */
254e6b9e
DE
20793 gdb_assert (die_is_declaration (die, cu));
20794 gdb_assert (attr);
20795 }
435d3d88 20796 if (attr != nullptr)
c906108c 20797 {
e7c27a73 20798 dwarf2_const_value (attr, sym, cu);
e142c38c 20799 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20800 if (!suppress_add)
34eaf542
TT
20801 {
20802 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20803 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20804 else
e37fd15a 20805 list_to_add = cu->list_in_scope;
34eaf542 20806 }
c906108c
SS
20807 break;
20808 }
e142c38c 20809 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20810 if (attr != nullptr)
c906108c 20811 {
e7c27a73 20812 var_decode_location (attr, sym, cu);
e142c38c 20813 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20814
20815 /* Fortran explicitly imports any global symbols to the local
20816 scope by DW_TAG_common_block. */
20817 if (cu->language == language_fortran && die->parent
20818 && die->parent->tag == DW_TAG_common_block)
20819 attr2 = NULL;
20820
caac4577
JG
20821 if (SYMBOL_CLASS (sym) == LOC_STATIC
20822 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20823 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20824 {
20825 /* When a static variable is eliminated by the linker,
20826 the corresponding debug information is not stripped
20827 out, but the variable address is set to null;
20828 do not add such variables into symbol table. */
20829 }
20830 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20831 {
4b610737
TT
20832 if (SYMBOL_CLASS (sym) == LOC_STATIC
20833 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20834 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20835 {
20836 /* A global static variable might be subject to
20837 copy relocation. We first check for a local
20838 minsym, though, because maybe the symbol was
20839 marked hidden, in which case this would not
20840 apply. */
20841 bound_minimal_symbol found
20842 = (lookup_minimal_symbol_linkage
987012b8 20843 (sym->linkage_name (), objfile));
4b610737
TT
20844 if (found.minsym != nullptr)
20845 sym->maybe_copied = 1;
20846 }
f55ee35c 20847
1c809c68
TT
20848 /* A variable with DW_AT_external is never static,
20849 but it may be block-scoped. */
804d2729 20850 list_to_add
c24bdb02
KS
20851 = ((cu->list_in_scope
20852 == cu->get_builder ()->get_file_symbols ())
20853 ? cu->get_builder ()->get_global_symbols ()
804d2729 20854 : cu->list_in_scope);
1c809c68 20855 }
c906108c 20856 else
e37fd15a 20857 list_to_add = cu->list_in_scope;
c906108c
SS
20858 }
20859 else
20860 {
20861 /* We do not know the address of this symbol.
c5aa993b
JM
20862 If it is an external symbol and we have type information
20863 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20864 The address of the variable will then be determined from
20865 the minimal symbol table whenever the variable is
20866 referenced. */
e142c38c 20867 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20868
20869 /* Fortran explicitly imports any global symbols to the local
20870 scope by DW_TAG_common_block. */
20871 if (cu->language == language_fortran && die->parent
20872 && die->parent->tag == DW_TAG_common_block)
20873 {
20874 /* SYMBOL_CLASS doesn't matter here because
20875 read_common_block is going to reset it. */
20876 if (!suppress_add)
20877 list_to_add = cu->list_in_scope;
20878 }
20879 else if (attr2 && (DW_UNSND (attr2) != 0)
20880 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20881 {
0fe7935b
DJ
20882 /* A variable with DW_AT_external is never static, but it
20883 may be block-scoped. */
804d2729 20884 list_to_add
c24bdb02
KS
20885 = ((cu->list_in_scope
20886 == cu->get_builder ()->get_file_symbols ())
20887 ? cu->get_builder ()->get_global_symbols ()
804d2729 20888 : cu->list_in_scope);
0fe7935b 20889
f1e6e072 20890 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20891 }
442ddf59
JK
20892 else if (!die_is_declaration (die, cu))
20893 {
20894 /* Use the default LOC_OPTIMIZED_OUT class. */
20895 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20896 if (!suppress_add)
20897 list_to_add = cu->list_in_scope;
442ddf59 20898 }
c906108c
SS
20899 }
20900 break;
20901 case DW_TAG_formal_parameter:
a60f3166
TT
20902 {
20903 /* If we are inside a function, mark this as an argument. If
20904 not, we might be looking at an argument to an inlined function
20905 when we do not have enough information to show inlined frames;
20906 pretend it's a local variable in that case so that the user can
20907 still see it. */
804d2729 20908 struct context_stack *curr
c24bdb02 20909 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20910 if (curr != nullptr && curr->name != nullptr)
20911 SYMBOL_IS_ARGUMENT (sym) = 1;
20912 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20913 if (attr != nullptr)
a60f3166
TT
20914 {
20915 var_decode_location (attr, sym, cu);
20916 }
20917 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20918 if (attr != nullptr)
a60f3166
TT
20919 {
20920 dwarf2_const_value (attr, sym, cu);
20921 }
f346a30d 20922
a60f3166
TT
20923 list_to_add = cu->list_in_scope;
20924 }
c906108c
SS
20925 break;
20926 case DW_TAG_unspecified_parameters:
20927 /* From varargs functions; gdb doesn't seem to have any
20928 interest in this information, so just ignore it for now.
20929 (FIXME?) */
20930 break;
34eaf542
TT
20931 case DW_TAG_template_type_param:
20932 suppress_add = 1;
20933 /* Fall through. */
c906108c 20934 case DW_TAG_class_type:
680b30c7 20935 case DW_TAG_interface_type:
c906108c
SS
20936 case DW_TAG_structure_type:
20937 case DW_TAG_union_type:
72019c9c 20938 case DW_TAG_set_type:
c906108c 20939 case DW_TAG_enumeration_type:
f1e6e072 20940 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20941 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20942
63d06c5c 20943 {
9c37b5ae 20944 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20945 really ever be static objects: otherwise, if you try
20946 to, say, break of a class's method and you're in a file
20947 which doesn't mention that class, it won't work unless
20948 the check for all static symbols in lookup_symbol_aux
20949 saves you. See the OtherFileClass tests in
20950 gdb.c++/namespace.exp. */
20951
e37fd15a 20952 if (!suppress_add)
34eaf542 20953 {
c24bdb02 20954 buildsym_compunit *builder = cu->get_builder ();
804d2729 20955 list_to_add
c24bdb02 20956 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20957 && cu->language == language_cplus
c24bdb02 20958 ? builder->get_global_symbols ()
804d2729 20959 : cu->list_in_scope);
63d06c5c 20960
64382290 20961 /* The semantics of C++ state that "struct foo {
9c37b5ae 20962 ... }" also defines a typedef for "foo". */
64382290 20963 if (cu->language == language_cplus
45280282 20964 || cu->language == language_ada
c44af4eb
TT
20965 || cu->language == language_d
20966 || cu->language == language_rust)
64382290
TT
20967 {
20968 /* The symbol's name is already allocated along
20969 with this objfile, so we don't need to
20970 duplicate it for the type. */
7d93a1e0 20971 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 20972 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 20973 }
63d06c5c
DC
20974 }
20975 }
c906108c
SS
20976 break;
20977 case DW_TAG_typedef:
f1e6e072 20978 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20979 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20980 list_to_add = cu->list_in_scope;
63d06c5c 20981 break;
c906108c 20982 case DW_TAG_base_type:
a02abb62 20983 case DW_TAG_subrange_type:
f1e6e072 20984 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20985 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20986 list_to_add = cu->list_in_scope;
c906108c
SS
20987 break;
20988 case DW_TAG_enumerator:
e142c38c 20989 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20990 if (attr != nullptr)
c906108c 20991 {
e7c27a73 20992 dwarf2_const_value (attr, sym, cu);
c906108c 20993 }
63d06c5c
DC
20994 {
20995 /* NOTE: carlton/2003-11-10: See comment above in the
20996 DW_TAG_class_type, etc. block. */
20997
804d2729 20998 list_to_add
c24bdb02 20999 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21000 && cu->language == language_cplus
c24bdb02 21001 ? cu->get_builder ()->get_global_symbols ()
804d2729 21002 : cu->list_in_scope);
63d06c5c 21003 }
c906108c 21004 break;
74921315 21005 case DW_TAG_imported_declaration:
5c4e30ca 21006 case DW_TAG_namespace:
f1e6e072 21007 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21008 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21009 break;
530e8392
KB
21010 case DW_TAG_module:
21011 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21012 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21013 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21014 break;
4357ac6c 21015 case DW_TAG_common_block:
f1e6e072 21016 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21017 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21018 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21019 break;
c906108c
SS
21020 default:
21021 /* Not a tag we recognize. Hopefully we aren't processing
21022 trash data, but since we must specifically ignore things
21023 we don't recognize, there is nothing else we should do at
0963b4bd 21024 this point. */
b98664d3 21025 complaint (_("unsupported tag: '%s'"),
4d3c2250 21026 dwarf_tag_name (die->tag));
c906108c
SS
21027 break;
21028 }
df8a16a1 21029
e37fd15a
SW
21030 if (suppress_add)
21031 {
21032 sym->hash_next = objfile->template_symbols;
21033 objfile->template_symbols = sym;
21034 list_to_add = NULL;
21035 }
21036
21037 if (list_to_add != NULL)
d3cb6808 21038 add_symbol_to_list (sym, list_to_add);
e37fd15a 21039
df8a16a1
DJ
21040 /* For the benefit of old versions of GCC, check for anonymous
21041 namespaces based on the demangled name. */
4d4ec4e5 21042 if (!cu->processing_has_namespace_info
94af9270 21043 && cu->language == language_cplus)
c24bdb02 21044 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21045 }
21046 return (sym);
21047}
21048
98bfdba5
PA
21049/* Given an attr with a DW_FORM_dataN value in host byte order,
21050 zero-extend it as appropriate for the symbol's type. The DWARF
21051 standard (v4) is not entirely clear about the meaning of using
21052 DW_FORM_dataN for a constant with a signed type, where the type is
21053 wider than the data. The conclusion of a discussion on the DWARF
21054 list was that this is unspecified. We choose to always zero-extend
21055 because that is the interpretation long in use by GCC. */
c906108c 21056
98bfdba5 21057static gdb_byte *
ff39bb5e 21058dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21059 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21060{
5e22e966 21061 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21062 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21063 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21064 LONGEST l = DW_UNSND (attr);
21065
21066 if (bits < sizeof (*value) * 8)
21067 {
21068 l &= ((LONGEST) 1 << bits) - 1;
21069 *value = l;
21070 }
21071 else if (bits == sizeof (*value) * 8)
21072 *value = l;
21073 else
21074 {
224c3ddb 21075 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21076 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21077 return bytes;
21078 }
21079
21080 return NULL;
21081}
21082
21083/* Read a constant value from an attribute. Either set *VALUE, or if
21084 the value does not fit in *VALUE, set *BYTES - either already
21085 allocated on the objfile obstack, or newly allocated on OBSTACK,
21086 or, set *BATON, if we translated the constant to a location
21087 expression. */
21088
21089static void
ff39bb5e 21090dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21091 const char *name, struct obstack *obstack,
21092 struct dwarf2_cu *cu,
d521ce57 21093 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21094 struct dwarf2_locexpr_baton **baton)
21095{
5e22e966 21096 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21097 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21098 struct comp_unit_head *cu_header = &cu->header;
c906108c 21099 struct dwarf_block *blk;
98bfdba5
PA
21100 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21101 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21102
21103 *value = 0;
21104 *bytes = NULL;
21105 *baton = NULL;
c906108c
SS
21106
21107 switch (attr->form)
21108 {
21109 case DW_FORM_addr:
336d760d 21110 case DW_FORM_addrx:
3019eac3 21111 case DW_FORM_GNU_addr_index:
ac56253d 21112 {
ac56253d
TT
21113 gdb_byte *data;
21114
98bfdba5
PA
21115 if (TYPE_LENGTH (type) != cu_header->addr_size)
21116 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21117 cu_header->addr_size,
98bfdba5 21118 TYPE_LENGTH (type));
ac56253d
TT
21119 /* Symbols of this form are reasonably rare, so we just
21120 piggyback on the existing location code rather than writing
21121 a new implementation of symbol_computed_ops. */
8d749320 21122 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21123 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21124 (*baton)->per_cu = cu->per_cu;
21125 gdb_assert ((*baton)->per_cu);
ac56253d 21126
98bfdba5 21127 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21128 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21129 (*baton)->data = data;
ac56253d
TT
21130
21131 data[0] = DW_OP_addr;
21132 store_unsigned_integer (&data[1], cu_header->addr_size,
21133 byte_order, DW_ADDR (attr));
21134 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21135 }
c906108c 21136 break;
4ac36638 21137 case DW_FORM_string:
93b5768b 21138 case DW_FORM_strp:
cf532bd1 21139 case DW_FORM_strx:
3019eac3 21140 case DW_FORM_GNU_str_index:
36586728 21141 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21142 /* DW_STRING is already allocated on the objfile obstack, point
21143 directly to it. */
d521ce57 21144 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21145 break;
c906108c
SS
21146 case DW_FORM_block1:
21147 case DW_FORM_block2:
21148 case DW_FORM_block4:
21149 case DW_FORM_block:
2dc7f7b3 21150 case DW_FORM_exprloc:
0224619f 21151 case DW_FORM_data16:
c906108c 21152 blk = DW_BLOCK (attr);
98bfdba5
PA
21153 if (TYPE_LENGTH (type) != blk->size)
21154 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21155 TYPE_LENGTH (type));
21156 *bytes = blk->data;
c906108c 21157 break;
2df3850c
JM
21158
21159 /* The DW_AT_const_value attributes are supposed to carry the
21160 symbol's value "represented as it would be on the target
21161 architecture." By the time we get here, it's already been
21162 converted to host endianness, so we just need to sign- or
21163 zero-extend it as appropriate. */
21164 case DW_FORM_data1:
3aef2284 21165 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21166 break;
c906108c 21167 case DW_FORM_data2:
3aef2284 21168 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21169 break;
c906108c 21170 case DW_FORM_data4:
3aef2284 21171 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21172 break;
c906108c 21173 case DW_FORM_data8:
3aef2284 21174 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21175 break;
21176
c906108c 21177 case DW_FORM_sdata:
663c44ac 21178 case DW_FORM_implicit_const:
98bfdba5 21179 *value = DW_SND (attr);
2df3850c
JM
21180 break;
21181
c906108c 21182 case DW_FORM_udata:
98bfdba5 21183 *value = DW_UNSND (attr);
c906108c 21184 break;
2df3850c 21185
c906108c 21186 default:
b98664d3 21187 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21188 dwarf_form_name (attr->form));
98bfdba5 21189 *value = 0;
c906108c
SS
21190 break;
21191 }
21192}
21193
2df3850c 21194
98bfdba5
PA
21195/* Copy constant value from an attribute to a symbol. */
21196
2df3850c 21197static void
ff39bb5e 21198dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21199 struct dwarf2_cu *cu)
2df3850c 21200{
5e22e966 21201 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21202 LONGEST value;
d521ce57 21203 const gdb_byte *bytes;
98bfdba5 21204 struct dwarf2_locexpr_baton *baton;
2df3850c 21205
98bfdba5 21206 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21207 sym->print_name (),
98bfdba5
PA
21208 &objfile->objfile_obstack, cu,
21209 &value, &bytes, &baton);
2df3850c 21210
98bfdba5
PA
21211 if (baton != NULL)
21212 {
98bfdba5 21213 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21214 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21215 }
21216 else if (bytes != NULL)
21217 {
21218 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21219 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21220 }
21221 else
21222 {
21223 SYMBOL_VALUE (sym) = value;
f1e6e072 21224 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21225 }
2df3850c
JM
21226}
21227
c906108c
SS
21228/* Return the type of the die in question using its DW_AT_type attribute. */
21229
21230static struct type *
e7c27a73 21231die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21232{
c906108c 21233 struct attribute *type_attr;
c906108c 21234
e142c38c 21235 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21236 if (!type_attr)
21237 {
5e22e966 21238 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21239 /* A missing DW_AT_type represents a void type. */
518817b3 21240 return objfile_type (objfile)->builtin_void;
c906108c 21241 }
348e048f 21242
673bfd45 21243 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21244}
21245
b4ba55a1
JB
21246/* True iff CU's producer generates GNAT Ada auxiliary information
21247 that allows to find parallel types through that information instead
21248 of having to do expensive parallel lookups by type name. */
21249
21250static int
21251need_gnat_info (struct dwarf2_cu *cu)
21252{
de4cb04a
JB
21253 /* Assume that the Ada compiler was GNAT, which always produces
21254 the auxiliary information. */
21255 return (cu->language == language_ada);
b4ba55a1
JB
21256}
21257
b4ba55a1
JB
21258/* Return the auxiliary type of the die in question using its
21259 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21260 attribute is not present. */
21261
21262static struct type *
21263die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21264{
b4ba55a1 21265 struct attribute *type_attr;
b4ba55a1
JB
21266
21267 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21268 if (!type_attr)
21269 return NULL;
21270
673bfd45 21271 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21272}
21273
21274/* If DIE has a descriptive_type attribute, then set the TYPE's
21275 descriptive type accordingly. */
21276
21277static void
21278set_descriptive_type (struct type *type, struct die_info *die,
21279 struct dwarf2_cu *cu)
21280{
21281 struct type *descriptive_type = die_descriptive_type (die, cu);
21282
21283 if (descriptive_type)
21284 {
21285 ALLOCATE_GNAT_AUX_TYPE (type);
21286 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21287 }
21288}
21289
c906108c
SS
21290/* Return the containing type of the die in question using its
21291 DW_AT_containing_type attribute. */
21292
21293static struct type *
e7c27a73 21294die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21295{
c906108c 21296 struct attribute *type_attr;
5e22e966 21297 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21298
e142c38c 21299 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21300 if (!type_attr)
21301 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21302 "[in module %s]"), objfile_name (objfile));
33ac96f0 21303
673bfd45 21304 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21305}
21306
ac9ec31b
DE
21307/* Return an error marker type to use for the ill formed type in DIE/CU. */
21308
21309static struct type *
21310build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21311{
5e22e966 21312 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21313 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21314 char *saved;
ac9ec31b 21315
528e1572
SM
21316 std::string message
21317 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21318 objfile_name (objfile),
21319 sect_offset_str (cu->header.sect_off),
21320 sect_offset_str (die->sect_off));
efba19b0 21321 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21322
19f392bc 21323 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21324}
21325
673bfd45 21326/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21327 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21328 DW_AT_containing_type.
673bfd45
DE
21329 If there is no type substitute an error marker. */
21330
c906108c 21331static struct type *
ff39bb5e 21332lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21333 struct dwarf2_cu *cu)
c906108c 21334{
5e22e966 21335 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21336 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21337 struct type *this_type;
21338
ac9ec31b
DE
21339 gdb_assert (attr->name == DW_AT_type
21340 || attr->name == DW_AT_GNAT_descriptive_type
21341 || attr->name == DW_AT_containing_type);
21342
673bfd45
DE
21343 /* First see if we have it cached. */
21344
36586728
TT
21345 if (attr->form == DW_FORM_GNU_ref_alt)
21346 {
21347 struct dwarf2_per_cu_data *per_cu;
0826b30a 21348 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21349
ed2dc618
SM
21350 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21351 dwarf2_per_objfile);
9c541725 21352 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21353 }
cd6c91b4 21354 else if (attr->form_is_ref ())
673bfd45 21355 {
0826b30a 21356 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21357
9c541725 21358 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21359 }
55f1336d 21360 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21361 {
ac9ec31b 21362 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21363
ac9ec31b 21364 return get_signatured_type (die, signature, cu);
673bfd45
DE
21365 }
21366 else
21367 {
b98664d3 21368 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21369 " at %s [in module %s]"),
21370 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21371 objfile_name (objfile));
ac9ec31b 21372 return build_error_marker_type (cu, die);
673bfd45
DE
21373 }
21374
21375 /* If not cached we need to read it in. */
21376
21377 if (this_type == NULL)
21378 {
ac9ec31b 21379 struct die_info *type_die = NULL;
673bfd45
DE
21380 struct dwarf2_cu *type_cu = cu;
21381
cd6c91b4 21382 if (attr->form_is_ref ())
ac9ec31b
DE
21383 type_die = follow_die_ref (die, attr, &type_cu);
21384 if (type_die == NULL)
21385 return build_error_marker_type (cu, die);
21386 /* If we find the type now, it's probably because the type came
3019eac3
DE
21387 from an inter-CU reference and the type's CU got expanded before
21388 ours. */
ac9ec31b 21389 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21390 }
21391
21392 /* If we still don't have a type use an error marker. */
21393
21394 if (this_type == NULL)
ac9ec31b 21395 return build_error_marker_type (cu, die);
673bfd45 21396
f792889a 21397 return this_type;
c906108c
SS
21398}
21399
673bfd45
DE
21400/* Return the type in DIE, CU.
21401 Returns NULL for invalid types.
21402
02142a6c 21403 This first does a lookup in die_type_hash,
673bfd45
DE
21404 and only reads the die in if necessary.
21405
21406 NOTE: This can be called when reading in partial or full symbols. */
21407
f792889a 21408static struct type *
e7c27a73 21409read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21410{
f792889a
DJ
21411 struct type *this_type;
21412
21413 this_type = get_die_type (die, cu);
21414 if (this_type)
21415 return this_type;
21416
673bfd45
DE
21417 return read_type_die_1 (die, cu);
21418}
21419
21420/* Read the type in DIE, CU.
21421 Returns NULL for invalid types. */
21422
21423static struct type *
21424read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21425{
21426 struct type *this_type = NULL;
21427
c906108c
SS
21428 switch (die->tag)
21429 {
21430 case DW_TAG_class_type:
680b30c7 21431 case DW_TAG_interface_type:
c906108c
SS
21432 case DW_TAG_structure_type:
21433 case DW_TAG_union_type:
f792889a 21434 this_type = read_structure_type (die, cu);
c906108c
SS
21435 break;
21436 case DW_TAG_enumeration_type:
f792889a 21437 this_type = read_enumeration_type (die, cu);
c906108c
SS
21438 break;
21439 case DW_TAG_subprogram:
21440 case DW_TAG_subroutine_type:
edb3359d 21441 case DW_TAG_inlined_subroutine:
f792889a 21442 this_type = read_subroutine_type (die, cu);
c906108c
SS
21443 break;
21444 case DW_TAG_array_type:
f792889a 21445 this_type = read_array_type (die, cu);
c906108c 21446 break;
72019c9c 21447 case DW_TAG_set_type:
f792889a 21448 this_type = read_set_type (die, cu);
72019c9c 21449 break;
c906108c 21450 case DW_TAG_pointer_type:
f792889a 21451 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21452 break;
21453 case DW_TAG_ptr_to_member_type:
f792889a 21454 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21455 break;
21456 case DW_TAG_reference_type:
4297a3f0
AV
21457 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21458 break;
21459 case DW_TAG_rvalue_reference_type:
21460 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21461 break;
21462 case DW_TAG_const_type:
f792889a 21463 this_type = read_tag_const_type (die, cu);
c906108c
SS
21464 break;
21465 case DW_TAG_volatile_type:
f792889a 21466 this_type = read_tag_volatile_type (die, cu);
c906108c 21467 break;
06d66ee9
TT
21468 case DW_TAG_restrict_type:
21469 this_type = read_tag_restrict_type (die, cu);
21470 break;
c906108c 21471 case DW_TAG_string_type:
f792889a 21472 this_type = read_tag_string_type (die, cu);
c906108c
SS
21473 break;
21474 case DW_TAG_typedef:
f792889a 21475 this_type = read_typedef (die, cu);
c906108c 21476 break;
a02abb62 21477 case DW_TAG_subrange_type:
f792889a 21478 this_type = read_subrange_type (die, cu);
a02abb62 21479 break;
c906108c 21480 case DW_TAG_base_type:
f792889a 21481 this_type = read_base_type (die, cu);
c906108c 21482 break;
81a17f79 21483 case DW_TAG_unspecified_type:
f792889a 21484 this_type = read_unspecified_type (die, cu);
81a17f79 21485 break;
0114d602
DJ
21486 case DW_TAG_namespace:
21487 this_type = read_namespace_type (die, cu);
21488 break;
f55ee35c
JK
21489 case DW_TAG_module:
21490 this_type = read_module_type (die, cu);
21491 break;
a2c2acaf
MW
21492 case DW_TAG_atomic_type:
21493 this_type = read_tag_atomic_type (die, cu);
21494 break;
c906108c 21495 default:
b98664d3 21496 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21497 dwarf_tag_name (die->tag));
c906108c
SS
21498 break;
21499 }
63d06c5c 21500
f792889a 21501 return this_type;
63d06c5c
DC
21502}
21503
abc72ce4
DE
21504/* See if we can figure out if the class lives in a namespace. We do
21505 this by looking for a member function; its demangled name will
21506 contain namespace info, if there is any.
21507 Return the computed name or NULL.
21508 Space for the result is allocated on the objfile's obstack.
21509 This is the full-die version of guess_partial_die_structure_name.
21510 In this case we know DIE has no useful parent. */
21511
43816ebc 21512static const char *
abc72ce4
DE
21513guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21514{
21515 struct die_info *spec_die;
21516 struct dwarf2_cu *spec_cu;
21517 struct die_info *child;
5e22e966 21518 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21519
21520 spec_cu = cu;
21521 spec_die = die_specification (die, &spec_cu);
21522 if (spec_die != NULL)
21523 {
21524 die = spec_die;
21525 cu = spec_cu;
21526 }
21527
21528 for (child = die->child;
21529 child != NULL;
21530 child = child->sibling)
21531 {
21532 if (child->tag == DW_TAG_subprogram)
21533 {
73b9be8b 21534 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21535
7d45c7c3 21536 if (linkage_name != NULL)
abc72ce4 21537 {
43816ebc
TT
21538 gdb::unique_xmalloc_ptr<char> actual_name
21539 (language_class_name_from_physname (cu->language_defn,
21540 linkage_name));
21541 const char *name = NULL;
abc72ce4
DE
21542
21543 if (actual_name != NULL)
21544 {
15d034d0 21545 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21546
21547 if (die_name != NULL
43816ebc 21548 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21549 {
21550 /* Strip off the class name from the full name.
21551 We want the prefix. */
21552 int die_name_len = strlen (die_name);
43816ebc
TT
21553 int actual_name_len = strlen (actual_name.get ());
21554 const char *ptr = actual_name.get ();
abc72ce4
DE
21555
21556 /* Test for '::' as a sanity check. */
21557 if (actual_name_len > die_name_len + 2
43816ebc 21558 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21559 name = obstack_strndup (
e3b94546 21560 &objfile->per_bfd->storage_obstack,
43816ebc 21561 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21562 }
21563 }
abc72ce4
DE
21564 return name;
21565 }
21566 }
21567 }
21568
21569 return NULL;
21570}
21571
96408a79
SA
21572/* GCC might emit a nameless typedef that has a linkage name. Determine the
21573 prefix part in such case. See
21574 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21575
a121b7c1 21576static const char *
96408a79
SA
21577anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21578{
21579 struct attribute *attr;
e6a959d6 21580 const char *base;
96408a79
SA
21581
21582 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21583 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21584 return NULL;
21585
7d45c7c3 21586 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21587 return NULL;
21588
73b9be8b 21589 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21590 if (attr == NULL || DW_STRING (attr) == NULL)
21591 return NULL;
21592
21593 /* dwarf2_name had to be already called. */
21594 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21595
21596 /* Strip the base name, keep any leading namespaces/classes. */
21597 base = strrchr (DW_STRING (attr), ':');
21598 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21599 return "";
21600
5e22e966 21601 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21602 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21603 DW_STRING (attr),
21604 &base[-1] - DW_STRING (attr));
96408a79
SA
21605}
21606
fdde2d81 21607/* Return the name of the namespace/class that DIE is defined within,
0114d602 21608 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21609
0114d602
DJ
21610 For example, if we're within the method foo() in the following
21611 code:
21612
21613 namespace N {
21614 class C {
21615 void foo () {
21616 }
21617 };
21618 }
21619
21620 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21621
0d5cff50 21622static const char *
e142c38c 21623determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21624{
5e22e966 21625 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21626 struct die_info *parent, *spec_die;
21627 struct dwarf2_cu *spec_cu;
21628 struct type *parent_type;
a121b7c1 21629 const char *retval;
63d06c5c 21630
9c37b5ae 21631 if (cu->language != language_cplus
c44af4eb
TT
21632 && cu->language != language_fortran && cu->language != language_d
21633 && cu->language != language_rust)
0114d602
DJ
21634 return "";
21635
96408a79
SA
21636 retval = anonymous_struct_prefix (die, cu);
21637 if (retval)
21638 return retval;
21639
0114d602
DJ
21640 /* We have to be careful in the presence of DW_AT_specification.
21641 For example, with GCC 3.4, given the code
21642
21643 namespace N {
21644 void foo() {
21645 // Definition of N::foo.
21646 }
21647 }
21648
21649 then we'll have a tree of DIEs like this:
21650
21651 1: DW_TAG_compile_unit
21652 2: DW_TAG_namespace // N
21653 3: DW_TAG_subprogram // declaration of N::foo
21654 4: DW_TAG_subprogram // definition of N::foo
21655 DW_AT_specification // refers to die #3
21656
21657 Thus, when processing die #4, we have to pretend that we're in
21658 the context of its DW_AT_specification, namely the contex of die
21659 #3. */
21660 spec_cu = cu;
21661 spec_die = die_specification (die, &spec_cu);
21662 if (spec_die == NULL)
21663 parent = die->parent;
21664 else
63d06c5c 21665 {
0114d602
DJ
21666 parent = spec_die->parent;
21667 cu = spec_cu;
63d06c5c 21668 }
0114d602
DJ
21669
21670 if (parent == NULL)
21671 return "";
98bfdba5
PA
21672 else if (parent->building_fullname)
21673 {
21674 const char *name;
21675 const char *parent_name;
21676
21677 /* It has been seen on RealView 2.2 built binaries,
21678 DW_TAG_template_type_param types actually _defined_ as
21679 children of the parent class:
21680
21681 enum E {};
21682 template class <class Enum> Class{};
21683 Class<enum E> class_e;
21684
21685 1: DW_TAG_class_type (Class)
21686 2: DW_TAG_enumeration_type (E)
21687 3: DW_TAG_enumerator (enum1:0)
21688 3: DW_TAG_enumerator (enum2:1)
21689 ...
21690 2: DW_TAG_template_type_param
21691 DW_AT_type DW_FORM_ref_udata (E)
21692
21693 Besides being broken debug info, it can put GDB into an
21694 infinite loop. Consider:
21695
21696 When we're building the full name for Class<E>, we'll start
21697 at Class, and go look over its template type parameters,
21698 finding E. We'll then try to build the full name of E, and
21699 reach here. We're now trying to build the full name of E,
21700 and look over the parent DIE for containing scope. In the
21701 broken case, if we followed the parent DIE of E, we'd again
21702 find Class, and once again go look at its template type
21703 arguments, etc., etc. Simply don't consider such parent die
21704 as source-level parent of this die (it can't be, the language
21705 doesn't allow it), and break the loop here. */
21706 name = dwarf2_name (die, cu);
21707 parent_name = dwarf2_name (parent, cu);
b98664d3 21708 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21709 name ? name : "<unknown>",
21710 parent_name ? parent_name : "<unknown>");
21711 return "";
21712 }
63d06c5c 21713 else
0114d602
DJ
21714 switch (parent->tag)
21715 {
63d06c5c 21716 case DW_TAG_namespace:
0114d602 21717 parent_type = read_type_die (parent, cu);
acebe513
UW
21718 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21719 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21720 Work around this problem here. */
21721 if (cu->language == language_cplus
7d93a1e0 21722 && strcmp (parent_type->name (), "::") == 0)
acebe513 21723 return "";
0114d602 21724 /* We give a name to even anonymous namespaces. */
7d93a1e0 21725 return parent_type->name ();
63d06c5c 21726 case DW_TAG_class_type:
680b30c7 21727 case DW_TAG_interface_type:
63d06c5c 21728 case DW_TAG_structure_type:
0114d602 21729 case DW_TAG_union_type:
f55ee35c 21730 case DW_TAG_module:
0114d602 21731 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21732 if (parent_type->name () != NULL)
21733 return parent_type->name ();
0114d602
DJ
21734 else
21735 /* An anonymous structure is only allowed non-static data
21736 members; no typedefs, no member functions, et cetera.
21737 So it does not need a prefix. */
21738 return "";
abc72ce4 21739 case DW_TAG_compile_unit:
95554aad 21740 case DW_TAG_partial_unit:
abc72ce4
DE
21741 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21742 if (cu->language == language_cplus
5989a64e 21743 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21744 && die->child != NULL
21745 && (die->tag == DW_TAG_class_type
21746 || die->tag == DW_TAG_structure_type
21747 || die->tag == DW_TAG_union_type))
21748 {
43816ebc 21749 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21750 if (name != NULL)
21751 return name;
21752 }
21753 return "";
0a4b0913
AB
21754 case DW_TAG_subprogram:
21755 /* Nested subroutines in Fortran get a prefix with the name
21756 of the parent's subroutine. */
21757 if (cu->language == language_fortran)
21758 {
21759 if ((die->tag == DW_TAG_subprogram)
21760 && (dwarf2_name (parent, cu) != NULL))
21761 return dwarf2_name (parent, cu);
21762 }
21763 return determine_prefix (parent, cu);
3d567982
TT
21764 case DW_TAG_enumeration_type:
21765 parent_type = read_type_die (parent, cu);
21766 if (TYPE_DECLARED_CLASS (parent_type))
21767 {
7d93a1e0
SM
21768 if (parent_type->name () != NULL)
21769 return parent_type->name ();
3d567982
TT
21770 return "";
21771 }
21772 /* Fall through. */
63d06c5c 21773 default:
8176b9b8 21774 return determine_prefix (parent, cu);
63d06c5c 21775 }
63d06c5c
DC
21776}
21777
3e43a32a
MS
21778/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21779 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21780 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21781 an obconcat, otherwise allocate storage for the result. The CU argument is
21782 used to determine the language and hence, the appropriate separator. */
987504bb 21783
f55ee35c 21784#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21785
21786static char *
f55ee35c
JK
21787typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21788 int physname, struct dwarf2_cu *cu)
63d06c5c 21789{
f55ee35c 21790 const char *lead = "";
5c315b68 21791 const char *sep;
63d06c5c 21792
3e43a32a
MS
21793 if (suffix == NULL || suffix[0] == '\0'
21794 || prefix == NULL || prefix[0] == '\0')
987504bb 21795 sep = "";
45280282
IB
21796 else if (cu->language == language_d)
21797 {
21798 /* For D, the 'main' function could be defined in any module, but it
21799 should never be prefixed. */
21800 if (strcmp (suffix, "D main") == 0)
21801 {
21802 prefix = "";
21803 sep = "";
21804 }
21805 else
21806 sep = ".";
21807 }
f55ee35c
JK
21808 else if (cu->language == language_fortran && physname)
21809 {
21810 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21811 DW_AT_MIPS_linkage_name is preferred and used instead. */
21812
21813 lead = "__";
21814 sep = "_MOD_";
21815 }
987504bb
JJ
21816 else
21817 sep = "::";
63d06c5c 21818
6dd47d34
DE
21819 if (prefix == NULL)
21820 prefix = "";
21821 if (suffix == NULL)
21822 suffix = "";
21823
987504bb
JJ
21824 if (obs == NULL)
21825 {
3e43a32a 21826 char *retval
224c3ddb
SM
21827 = ((char *)
21828 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21829
f55ee35c
JK
21830 strcpy (retval, lead);
21831 strcat (retval, prefix);
6dd47d34
DE
21832 strcat (retval, sep);
21833 strcat (retval, suffix);
63d06c5c
DC
21834 return retval;
21835 }
987504bb
JJ
21836 else
21837 {
21838 /* We have an obstack. */
f55ee35c 21839 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21840 }
63d06c5c
DC
21841}
21842
71c25dea
TT
21843/* Get name of a die, return NULL if not found. */
21844
15d034d0
TT
21845static const char *
21846dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21847 struct objfile *objfile)
71c25dea
TT
21848{
21849 if (name && cu->language == language_cplus)
21850 {
596dc4ad
TT
21851 gdb::unique_xmalloc_ptr<char> canon_name
21852 = cp_canonicalize_string (name);
71c25dea 21853
596dc4ad
TT
21854 if (canon_name != nullptr)
21855 name = objfile->intern (canon_name.get ());
71c25dea
TT
21856 }
21857
21858 return name;
c906108c
SS
21859}
21860
96553a0c
DE
21861/* Get name of a die, return NULL if not found.
21862 Anonymous namespaces are converted to their magic string. */
9219021c 21863
15d034d0 21864static const char *
e142c38c 21865dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21866{
21867 struct attribute *attr;
5e22e966 21868 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21869
e142c38c 21870 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21871 if ((!attr || !DW_STRING (attr))
96553a0c 21872 && die->tag != DW_TAG_namespace
53832f31
TT
21873 && die->tag != DW_TAG_class_type
21874 && die->tag != DW_TAG_interface_type
21875 && die->tag != DW_TAG_structure_type
21876 && die->tag != DW_TAG_union_type)
71c25dea
TT
21877 return NULL;
21878
21879 switch (die->tag)
21880 {
21881 case DW_TAG_compile_unit:
95554aad 21882 case DW_TAG_partial_unit:
71c25dea
TT
21883 /* Compilation units have a DW_AT_name that is a filename, not
21884 a source language identifier. */
21885 case DW_TAG_enumeration_type:
21886 case DW_TAG_enumerator:
21887 /* These tags always have simple identifiers already; no need
21888 to canonicalize them. */
21889 return DW_STRING (attr);
907af001 21890
96553a0c
DE
21891 case DW_TAG_namespace:
21892 if (attr != NULL && DW_STRING (attr) != NULL)
21893 return DW_STRING (attr);
21894 return CP_ANONYMOUS_NAMESPACE_STR;
21895
907af001
UW
21896 case DW_TAG_class_type:
21897 case DW_TAG_interface_type:
21898 case DW_TAG_structure_type:
21899 case DW_TAG_union_type:
21900 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21901 structures or unions. These were of the form "._%d" in GCC 4.1,
21902 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21903 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21904 if (attr && DW_STRING (attr)
61012eef
GB
21905 && (startswith (DW_STRING (attr), "._")
21906 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21907 return NULL;
53832f31
TT
21908
21909 /* GCC might emit a nameless typedef that has a linkage name. See
21910 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21911 if (!attr || DW_STRING (attr) == NULL)
21912 {
73b9be8b 21913 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21914 if (attr == NULL || DW_STRING (attr) == NULL)
21915 return NULL;
21916
df5c6c50
JK
21917 /* Avoid demangling DW_STRING (attr) the second time on a second
21918 call for the same DIE. */
21919 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21920 {
43816ebc
TT
21921 gdb::unique_xmalloc_ptr<char> demangled
21922 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21923 if (demangled == nullptr)
21924 return nullptr;
43816ebc 21925
be1e3d3e 21926 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21927 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21928 }
67430cd0
TT
21929
21930 /* Strip any leading namespaces/classes, keep only the base name.
21931 DW_AT_name for named DIEs does not contain the prefixes. */
21932 const char *base = strrchr (DW_STRING (attr), ':');
21933 if (base && base > DW_STRING (attr) && base[-1] == ':')
21934 return &base[1];
21935 else
21936 return DW_STRING (attr);
53832f31 21937 }
907af001
UW
21938 break;
21939
71c25dea 21940 default:
907af001
UW
21941 break;
21942 }
21943
21944 if (!DW_STRING_IS_CANONICAL (attr))
21945 {
be1e3d3e
TT
21946 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21947 objfile);
907af001 21948 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21949 }
907af001 21950 return DW_STRING (attr);
9219021c
DC
21951}
21952
21953/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21954 is none. *EXT_CU is the CU containing DIE on input, and the CU
21955 containing the return value on output. */
9219021c
DC
21956
21957static struct die_info *
f2f0e013 21958dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21959{
21960 struct attribute *attr;
9219021c 21961
f2f0e013 21962 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21963 if (attr == NULL)
21964 return NULL;
21965
f2f0e013 21966 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21967}
21968
f9aca02d 21969static void
d97bc12b 21970dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21971{
21972 unsigned int i;
21973
d97bc12b 21974 print_spaces (indent, f);
9d8780f0 21975 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21976 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21977 sect_offset_str (die->sect_off));
d97bc12b
DE
21978
21979 if (die->parent != NULL)
21980 {
21981 print_spaces (indent, f);
9d8780f0
SM
21982 fprintf_unfiltered (f, " parent at offset: %s\n",
21983 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21984 }
21985
21986 print_spaces (indent, f);
21987 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21988 dwarf_bool_name (die->child != NULL));
c906108c 21989
d97bc12b
DE
21990 print_spaces (indent, f);
21991 fprintf_unfiltered (f, " attributes:\n");
21992
c906108c
SS
21993 for (i = 0; i < die->num_attrs; ++i)
21994 {
d97bc12b
DE
21995 print_spaces (indent, f);
21996 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21997 dwarf_attr_name (die->attrs[i].name),
21998 dwarf_form_name (die->attrs[i].form));
d97bc12b 21999
c906108c
SS
22000 switch (die->attrs[i].form)
22001 {
c906108c 22002 case DW_FORM_addr:
336d760d 22003 case DW_FORM_addrx:
3019eac3 22004 case DW_FORM_GNU_addr_index:
d97bc12b 22005 fprintf_unfiltered (f, "address: ");
5af949e3 22006 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22007 break;
22008 case DW_FORM_block2:
22009 case DW_FORM_block4:
22010 case DW_FORM_block:
22011 case DW_FORM_block1:
56eb65bd
SP
22012 fprintf_unfiltered (f, "block: size %s",
22013 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22014 break;
2dc7f7b3 22015 case DW_FORM_exprloc:
56eb65bd
SP
22016 fprintf_unfiltered (f, "expression: size %s",
22017 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22018 break;
0224619f
JK
22019 case DW_FORM_data16:
22020 fprintf_unfiltered (f, "constant of 16 bytes");
22021 break;
4568ecf9
DE
22022 case DW_FORM_ref_addr:
22023 fprintf_unfiltered (f, "ref address: ");
22024 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22025 break;
36586728
TT
22026 case DW_FORM_GNU_ref_alt:
22027 fprintf_unfiltered (f, "alt ref address: ");
22028 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22029 break;
10b3939b
DJ
22030 case DW_FORM_ref1:
22031 case DW_FORM_ref2:
22032 case DW_FORM_ref4:
4568ecf9
DE
22033 case DW_FORM_ref8:
22034 case DW_FORM_ref_udata:
d97bc12b 22035 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22036 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22037 break;
c906108c
SS
22038 case DW_FORM_data1:
22039 case DW_FORM_data2:
22040 case DW_FORM_data4:
ce5d95e1 22041 case DW_FORM_data8:
c906108c
SS
22042 case DW_FORM_udata:
22043 case DW_FORM_sdata:
43bbcdc2
PH
22044 fprintf_unfiltered (f, "constant: %s",
22045 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22046 break;
2dc7f7b3
TT
22047 case DW_FORM_sec_offset:
22048 fprintf_unfiltered (f, "section offset: %s",
22049 pulongest (DW_UNSND (&die->attrs[i])));
22050 break;
55f1336d 22051 case DW_FORM_ref_sig8:
ac9ec31b
DE
22052 fprintf_unfiltered (f, "signature: %s",
22053 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22054 break;
c906108c 22055 case DW_FORM_string:
4bdf3d34 22056 case DW_FORM_strp:
43988095 22057 case DW_FORM_line_strp:
cf532bd1 22058 case DW_FORM_strx:
3019eac3 22059 case DW_FORM_GNU_str_index:
36586728 22060 case DW_FORM_GNU_strp_alt:
8285870a 22061 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22062 DW_STRING (&die->attrs[i])
8285870a
JK
22063 ? DW_STRING (&die->attrs[i]) : "",
22064 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22065 break;
22066 case DW_FORM_flag:
22067 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22068 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22069 else
d97bc12b 22070 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22071 break;
2dc7f7b3
TT
22072 case DW_FORM_flag_present:
22073 fprintf_unfiltered (f, "flag: TRUE");
22074 break;
a8329558 22075 case DW_FORM_indirect:
0963b4bd
MS
22076 /* The reader will have reduced the indirect form to
22077 the "base form" so this form should not occur. */
5f48f8f3 22078 fprintf_unfiltered (f,
3e43a32a 22079 "unexpected attribute form: DW_FORM_indirect");
a8329558 22080 break;
663c44ac
JK
22081 case DW_FORM_implicit_const:
22082 fprintf_unfiltered (f, "constant: %s",
22083 plongest (DW_SND (&die->attrs[i])));
22084 break;
c906108c 22085 default:
d97bc12b 22086 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22087 die->attrs[i].form);
d97bc12b 22088 break;
c906108c 22089 }
d97bc12b 22090 fprintf_unfiltered (f, "\n");
c906108c
SS
22091 }
22092}
22093
f9aca02d 22094static void
d97bc12b 22095dump_die_for_error (struct die_info *die)
c906108c 22096{
d97bc12b
DE
22097 dump_die_shallow (gdb_stderr, 0, die);
22098}
22099
22100static void
22101dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22102{
22103 int indent = level * 4;
22104
22105 gdb_assert (die != NULL);
22106
22107 if (level >= max_level)
22108 return;
22109
22110 dump_die_shallow (f, indent, die);
22111
22112 if (die->child != NULL)
c906108c 22113 {
d97bc12b
DE
22114 print_spaces (indent, f);
22115 fprintf_unfiltered (f, " Children:");
22116 if (level + 1 < max_level)
22117 {
22118 fprintf_unfiltered (f, "\n");
22119 dump_die_1 (f, level + 1, max_level, die->child);
22120 }
22121 else
22122 {
3e43a32a
MS
22123 fprintf_unfiltered (f,
22124 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22125 }
22126 }
22127
22128 if (die->sibling != NULL && level > 0)
22129 {
22130 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22131 }
22132}
22133
d97bc12b
DE
22134/* This is called from the pdie macro in gdbinit.in.
22135 It's not static so gcc will keep a copy callable from gdb. */
22136
22137void
22138dump_die (struct die_info *die, int max_level)
22139{
22140 dump_die_1 (gdb_stdlog, 0, max_level, die);
22141}
22142
f9aca02d 22143static void
51545339 22144store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22145{
51545339 22146 void **slot;
c906108c 22147
9c541725
PA
22148 slot = htab_find_slot_with_hash (cu->die_hash, die,
22149 to_underlying (die->sect_off),
b64f50a1 22150 INSERT);
51545339
DJ
22151
22152 *slot = die;
c906108c
SS
22153}
22154
348e048f
DE
22155/* Follow reference or signature attribute ATTR of SRC_DIE.
22156 On entry *REF_CU is the CU of SRC_DIE.
22157 On exit *REF_CU is the CU of the result. */
22158
22159static struct die_info *
ff39bb5e 22160follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22161 struct dwarf2_cu **ref_cu)
22162{
22163 struct die_info *die;
22164
cd6c91b4 22165 if (attr->form_is_ref ())
348e048f 22166 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22167 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22168 die = follow_die_sig (src_die, attr, ref_cu);
22169 else
22170 {
22171 dump_die_for_error (src_die);
22172 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22173 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22174 }
22175
22176 return die;
03dd20cc
DJ
22177}
22178
5c631832 22179/* Follow reference OFFSET.
673bfd45
DE
22180 On entry *REF_CU is the CU of the source die referencing OFFSET.
22181 On exit *REF_CU is the CU of the result.
22182 Returns NULL if OFFSET is invalid. */
f504f079 22183
f9aca02d 22184static struct die_info *
9c541725 22185follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22186 struct dwarf2_cu **ref_cu)
c906108c 22187{
10b3939b 22188 struct die_info temp_die;
f2f0e013 22189 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22190 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22191
348e048f
DE
22192 gdb_assert (cu->per_cu != NULL);
22193
98bfdba5
PA
22194 target_cu = cu;
22195
3019eac3 22196 if (cu->per_cu->is_debug_types)
348e048f
DE
22197 {
22198 /* .debug_types CUs cannot reference anything outside their CU.
22199 If they need to, they have to reference a signatured type via
55f1336d 22200 DW_FORM_ref_sig8. */
4057dfde 22201 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22202 return NULL;
348e048f 22203 }
36586728 22204 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22205 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22206 {
22207 struct dwarf2_per_cu_data *per_cu;
9a619af0 22208
9c541725 22209 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22210 dwarf2_per_objfile);
03dd20cc
DJ
22211
22212 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22213 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22214 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22215
10b3939b
DJ
22216 target_cu = per_cu->cu;
22217 }
98bfdba5
PA
22218 else if (cu->dies == NULL)
22219 {
22220 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22221 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
58f0c718 22222 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22223 }
c906108c 22224
f2f0e013 22225 *ref_cu = target_cu;
9c541725 22226 temp_die.sect_off = sect_off;
c24bdb02
KS
22227
22228 if (target_cu != cu)
22229 target_cu->ancestor = cu;
22230
9a3c8263 22231 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22232 &temp_die,
22233 to_underlying (sect_off));
5c631832 22234}
10b3939b 22235
5c631832
JK
22236/* Follow reference attribute ATTR of SRC_DIE.
22237 On entry *REF_CU is the CU of SRC_DIE.
22238 On exit *REF_CU is the CU of the result. */
22239
22240static struct die_info *
ff39bb5e 22241follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22242 struct dwarf2_cu **ref_cu)
22243{
0826b30a 22244 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22245 struct dwarf2_cu *cu = *ref_cu;
22246 struct die_info *die;
22247
9c541725 22248 die = follow_die_offset (sect_off,
36586728
TT
22249 (attr->form == DW_FORM_GNU_ref_alt
22250 || cu->per_cu->is_dwz),
22251 ref_cu);
5c631832 22252 if (!die)
9d8780f0
SM
22253 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22254 "at %s [in module %s]"),
22255 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22256 objfile_name (cu->per_objfile->objfile));
348e048f 22257
5c631832
JK
22258 return die;
22259}
22260
d4c9a4f8 22261/* See read.h. */
5c631832
JK
22262
22263struct dwarf2_locexpr_baton
9c541725 22264dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22265 dwarf2_per_cu_data *per_cu,
8b9737bf 22266 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22267 void *baton, bool resolve_abstract_p)
5c631832 22268{
918dd910 22269 struct dwarf2_cu *cu;
5c631832
JK
22270 struct die_info *die;
22271 struct attribute *attr;
22272 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22273 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22274 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22275
918dd910 22276 if (per_cu->cu == NULL)
58f0c718 22277 load_cu (per_cu, false);
918dd910 22278 cu = per_cu->cu;
cc12ce38
DE
22279 if (cu == NULL)
22280 {
22281 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22282 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22283 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22284 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22285 }
918dd910 22286
9c541725 22287 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22288 if (!die)
9d8780f0
SM
22289 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22290 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22291
22292 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22293 if (!attr && resolve_abstract_p
5989a64e
SM
22294 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22295 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22296 {
22297 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22298 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22299 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22300
3360b6e7 22301 for (const auto &cand_off
5989a64e 22302 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22303 {
3360b6e7
TV
22304 struct dwarf2_cu *cand_cu = cu;
22305 struct die_info *cand
22306 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22307 if (!cand
22308 || !cand->parent
e4a62c65
TV
22309 || cand->parent->tag != DW_TAG_subprogram)
22310 continue;
22311
22312 CORE_ADDR pc_low, pc_high;
22313 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22314 if (pc_low == ((CORE_ADDR) -1))
22315 continue;
22316 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22317 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22318 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22319 continue;
22320
22321 die = cand;
22322 attr = dwarf2_attr (die, DW_AT_location, cu);
22323 break;
22324 }
22325 }
22326
5c631832
JK
22327 if (!attr)
22328 {
e103e986
JK
22329 /* DWARF: "If there is no such attribute, then there is no effect.".
22330 DATA is ignored if SIZE is 0. */
5c631832 22331
e103e986 22332 retval.data = NULL;
5c631832
JK
22333 retval.size = 0;
22334 }
cd6c91b4 22335 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22336 {
22337 struct dwarf2_loclist_baton loclist_baton;
22338 CORE_ADDR pc = (*get_frame_pc) (baton);
22339 size_t size;
22340
22341 fill_in_loclist_baton (cu, &loclist_baton, attr);
22342
22343 retval.data = dwarf2_find_location_expression (&loclist_baton,
22344 &size, pc);
22345 retval.size = size;
22346 }
5c631832
JK
22347 else
22348 {
4fc6c0d5 22349 if (!attr->form_is_block ())
9d8780f0 22350 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22351 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22352 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22353
22354 retval.data = DW_BLOCK (attr)->data;
22355 retval.size = DW_BLOCK (attr)->size;
22356 }
a50264ba 22357 retval.per_objfile = dwarf2_per_objfile;
5c631832 22358 retval.per_cu = cu->per_cu;
918dd910 22359
ed2dc618 22360 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22361
5c631832 22362 return retval;
348e048f
DE
22363}
22364
d4c9a4f8 22365/* See read.h. */
8b9737bf
TT
22366
22367struct dwarf2_locexpr_baton
22368dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22369 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22370 CORE_ADDR (*get_frame_pc) (void *baton),
22371 void *baton)
22372{
9c541725 22373 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22374
9c541725 22375 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22376}
22377
b6807d98
TT
22378/* Write a constant of a given type as target-ordered bytes into
22379 OBSTACK. */
22380
22381static const gdb_byte *
22382write_constant_as_bytes (struct obstack *obstack,
22383 enum bfd_endian byte_order,
22384 struct type *type,
22385 ULONGEST value,
22386 LONGEST *len)
22387{
22388 gdb_byte *result;
22389
22390 *len = TYPE_LENGTH (type);
224c3ddb 22391 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22392 store_unsigned_integer (result, *len, byte_order, value);
22393
22394 return result;
22395}
22396
d4c9a4f8 22397/* See read.h. */
b6807d98
TT
22398
22399const gdb_byte *
9c541725 22400dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22401 dwarf2_per_cu_data *per_cu,
22402 obstack *obstack,
b6807d98
TT
22403 LONGEST *len)
22404{
22405 struct dwarf2_cu *cu;
22406 struct die_info *die;
22407 struct attribute *attr;
22408 const gdb_byte *result = NULL;
22409 struct type *type;
22410 LONGEST value;
22411 enum bfd_endian byte_order;
e3b94546 22412 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22413
b6807d98 22414 if (per_cu->cu == NULL)
58f0c718 22415 load_cu (per_cu, false);
b6807d98 22416 cu = per_cu->cu;
cc12ce38
DE
22417 if (cu == NULL)
22418 {
22419 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22420 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22421 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22422 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22423 }
b6807d98 22424
9c541725 22425 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22426 if (!die)
9d8780f0
SM
22427 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22428 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22429
22430 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22431 if (attr == NULL)
22432 return NULL;
22433
e3b94546 22434 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22435 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22436
22437 switch (attr->form)
22438 {
22439 case DW_FORM_addr:
336d760d 22440 case DW_FORM_addrx:
b6807d98
TT
22441 case DW_FORM_GNU_addr_index:
22442 {
22443 gdb_byte *tem;
22444
22445 *len = cu->header.addr_size;
224c3ddb 22446 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22447 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22448 result = tem;
22449 }
22450 break;
22451 case DW_FORM_string:
22452 case DW_FORM_strp:
cf532bd1 22453 case DW_FORM_strx:
b6807d98
TT
22454 case DW_FORM_GNU_str_index:
22455 case DW_FORM_GNU_strp_alt:
22456 /* DW_STRING is already allocated on the objfile obstack, point
22457 directly to it. */
22458 result = (const gdb_byte *) DW_STRING (attr);
22459 *len = strlen (DW_STRING (attr));
22460 break;
22461 case DW_FORM_block1:
22462 case DW_FORM_block2:
22463 case DW_FORM_block4:
22464 case DW_FORM_block:
22465 case DW_FORM_exprloc:
0224619f 22466 case DW_FORM_data16:
b6807d98
TT
22467 result = DW_BLOCK (attr)->data;
22468 *len = DW_BLOCK (attr)->size;
22469 break;
22470
22471 /* The DW_AT_const_value attributes are supposed to carry the
22472 symbol's value "represented as it would be on the target
22473 architecture." By the time we get here, it's already been
22474 converted to host endianness, so we just need to sign- or
22475 zero-extend it as appropriate. */
22476 case DW_FORM_data1:
22477 type = die_type (die, cu);
22478 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22479 if (result == NULL)
22480 result = write_constant_as_bytes (obstack, byte_order,
22481 type, value, len);
22482 break;
22483 case DW_FORM_data2:
22484 type = die_type (die, cu);
22485 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22486 if (result == NULL)
22487 result = write_constant_as_bytes (obstack, byte_order,
22488 type, value, len);
22489 break;
22490 case DW_FORM_data4:
22491 type = die_type (die, cu);
22492 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22493 if (result == NULL)
22494 result = write_constant_as_bytes (obstack, byte_order,
22495 type, value, len);
22496 break;
22497 case DW_FORM_data8:
22498 type = die_type (die, cu);
22499 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22500 if (result == NULL)
22501 result = write_constant_as_bytes (obstack, byte_order,
22502 type, value, len);
22503 break;
22504
22505 case DW_FORM_sdata:
663c44ac 22506 case DW_FORM_implicit_const:
b6807d98
TT
22507 type = die_type (die, cu);
22508 result = write_constant_as_bytes (obstack, byte_order,
22509 type, DW_SND (attr), len);
22510 break;
22511
22512 case DW_FORM_udata:
22513 type = die_type (die, cu);
22514 result = write_constant_as_bytes (obstack, byte_order,
22515 type, DW_UNSND (attr), len);
22516 break;
22517
22518 default:
b98664d3 22519 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22520 dwarf_form_name (attr->form));
22521 break;
22522 }
22523
22524 return result;
22525}
22526
d4c9a4f8 22527/* See read.h. */
7942e96e
AA
22528
22529struct type *
9c541725 22530dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22531 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22532{
22533 struct dwarf2_cu *cu;
22534 struct die_info *die;
22535
7942e96e 22536 if (per_cu->cu == NULL)
58f0c718 22537 load_cu (per_cu, false);
7942e96e
AA
22538 cu = per_cu->cu;
22539 if (!cu)
22540 return NULL;
22541
9c541725 22542 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22543 if (!die)
22544 return NULL;
22545
22546 return die_type (die, cu);
22547}
22548
8cb5117c 22549/* See read.h. */
8a9b8146
TT
22550
22551struct type *
b64f50a1 22552dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22553 struct dwarf2_per_cu_data *per_cu)
22554{
9c541725 22555 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22556 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22557}
22558
ac9ec31b 22559/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22560 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22561 On exit *REF_CU is the CU of the result.
22562 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22563
22564static struct die_info *
ac9ec31b
DE
22565follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22566 struct dwarf2_cu **ref_cu)
348e048f 22567{
348e048f 22568 struct die_info temp_die;
c24bdb02 22569 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22570 struct die_info *die;
22571
ac9ec31b
DE
22572 /* While it might be nice to assert sig_type->type == NULL here,
22573 we can get here for DW_AT_imported_declaration where we need
22574 the DIE not the type. */
348e048f
DE
22575
22576 /* If necessary, add it to the queue and load its DIEs. */
22577
95554aad 22578 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22579 read_signatured_type (sig_type);
348e048f 22580
348e048f 22581 sig_cu = sig_type->per_cu.cu;
69d751e3 22582 gdb_assert (sig_cu != NULL);
9c541725
PA
22583 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22584 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22585 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22586 to_underlying (temp_die.sect_off));
348e048f
DE
22587 if (die)
22588 {
5e22e966 22589 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
ed2dc618 22590
796a7ff8
DE
22591 /* For .gdb_index version 7 keep track of included TUs.
22592 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22593 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22594 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22595 {
ae640021 22596 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22597 }
22598
348e048f 22599 *ref_cu = sig_cu;
c24bdb02
KS
22600 if (sig_cu != cu)
22601 sig_cu->ancestor = cu;
22602
348e048f
DE
22603 return die;
22604 }
22605
ac9ec31b
DE
22606 return NULL;
22607}
22608
22609/* Follow signatured type referenced by ATTR in SRC_DIE.
22610 On entry *REF_CU is the CU of SRC_DIE.
22611 On exit *REF_CU is the CU of the result.
22612 The result is the DIE of the type.
22613 If the referenced type cannot be found an error is thrown. */
22614
22615static struct die_info *
ff39bb5e 22616follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22617 struct dwarf2_cu **ref_cu)
22618{
22619 ULONGEST signature = DW_SIGNATURE (attr);
22620 struct signatured_type *sig_type;
22621 struct die_info *die;
22622
22623 gdb_assert (attr->form == DW_FORM_ref_sig8);
22624
a2ce51a0 22625 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22626 /* sig_type will be NULL if the signatured type is missing from
22627 the debug info. */
22628 if (sig_type == NULL)
22629 {
22630 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22631 " from DIE at %s [in module %s]"),
22632 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22633 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22634 }
22635
22636 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22637 if (die == NULL)
22638 {
22639 dump_die_for_error (src_die);
22640 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22641 " from DIE at %s [in module %s]"),
22642 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22643 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22644 }
22645
22646 return die;
22647}
22648
22649/* Get the type specified by SIGNATURE referenced in DIE/CU,
22650 reading in and processing the type unit if necessary. */
22651
22652static struct type *
22653get_signatured_type (struct die_info *die, ULONGEST signature,
22654 struct dwarf2_cu *cu)
22655{
5e22e966 22656 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->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 {
5e22e966 22729 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22730
b98664d3 22731 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22732 " at %s [in module %s]"),
22733 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22734 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22735 return build_error_marker_type (cu, die);
22736 }
348e048f
DE
22737}
22738
e5fe5e75 22739/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22740
22741static void
e5fe5e75 22742load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22743{
52dc124a 22744 struct signatured_type *sig_type;
348e048f 22745
f4dc4d17 22746 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22747 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22748
6721b2ec
DE
22749 /* We have the per_cu, but we need the signatured_type.
22750 Fortunately this is an easy translation. */
22751 gdb_assert (per_cu->is_debug_types);
22752 sig_type = (struct signatured_type *) per_cu;
348e048f 22753
6721b2ec 22754 gdb_assert (per_cu->cu == NULL);
348e048f 22755
52dc124a 22756 read_signatured_type (sig_type);
348e048f 22757
6721b2ec 22758 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22759}
22760
3019eac3
DE
22761/* Read in a signatured type and build its CU and DIEs.
22762 If the type is a stub for the real type in a DWO file,
22763 read in the real type from the DWO file as well. */
dee91e82
DE
22764
22765static void
22766read_signatured_type (struct signatured_type *sig_type)
22767{
22768 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22769
3019eac3 22770 gdb_assert (per_cu->is_debug_types);
dee91e82 22771 gdb_assert (per_cu->cu == NULL);
348e048f 22772
6751ebae 22773 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22774
22775 if (!reader.dummy_p)
22776 {
22777 struct dwarf2_cu *cu = reader.cu;
22778 const gdb_byte *info_ptr = reader.info_ptr;
22779
22780 gdb_assert (cu->die_hash == NULL);
22781 cu->die_hash =
22782 htab_create_alloc_ex (cu->header.length / 12,
22783 die_hash,
22784 die_eq,
22785 NULL,
22786 &cu->comp_unit_obstack,
22787 hashtab_obstack_allocate,
22788 dummy_obstack_deallocate);
22789
3e225074 22790 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22791 reader.comp_unit_die->child
22792 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22793 reader.comp_unit_die);
22794 cu->dies = reader.comp_unit_die;
22795 /* comp_unit_die is not stored in die_hash, no need. */
22796
22797 /* We try not to read any attributes in this function, because
22798 not all CUs needed for references have been loaded yet, and
22799 symbol table processing isn't initialized. But we have to
22800 set the CU language, or we won't be able to build types
22801 correctly. Similarly, if we do not read the producer, we can
22802 not apply producer-specific interpretation. */
22803 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22804
22805 reader.keep ();
c0ab21c2
TT
22806 }
22807
7ee85ab1 22808 sig_type->per_cu.tu_read = 1;
c906108c
SS
22809}
22810
c906108c
SS
22811/* Decode simple location descriptions.
22812 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22813 the location and return the value. If COMPUTED is non-null, it is
22814 set to true to indicate that decoding was successful, and false
22815 otherwise. If COMPUTED is null, then this function may emit a
22816 complaint. */
c906108c
SS
22817
22818static CORE_ADDR
7d79de9a 22819decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22820{
5e22e966 22821 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22822 size_t i;
22823 size_t size = blk->size;
d521ce57 22824 const gdb_byte *data = blk->data;
21ae7a4d
JK
22825 CORE_ADDR stack[64];
22826 int stacki;
22827 unsigned int bytes_read, unsnd;
22828 gdb_byte op;
c906108c 22829
7d79de9a
TT
22830 if (computed != nullptr)
22831 *computed = false;
22832
21ae7a4d
JK
22833 i = 0;
22834 stacki = 0;
22835 stack[stacki] = 0;
22836 stack[++stacki] = 0;
22837
22838 while (i < size)
22839 {
22840 op = data[i++];
22841 switch (op)
22842 {
22843 case DW_OP_lit0:
22844 case DW_OP_lit1:
22845 case DW_OP_lit2:
22846 case DW_OP_lit3:
22847 case DW_OP_lit4:
22848 case DW_OP_lit5:
22849 case DW_OP_lit6:
22850 case DW_OP_lit7:
22851 case DW_OP_lit8:
22852 case DW_OP_lit9:
22853 case DW_OP_lit10:
22854 case DW_OP_lit11:
22855 case DW_OP_lit12:
22856 case DW_OP_lit13:
22857 case DW_OP_lit14:
22858 case DW_OP_lit15:
22859 case DW_OP_lit16:
22860 case DW_OP_lit17:
22861 case DW_OP_lit18:
22862 case DW_OP_lit19:
22863 case DW_OP_lit20:
22864 case DW_OP_lit21:
22865 case DW_OP_lit22:
22866 case DW_OP_lit23:
22867 case DW_OP_lit24:
22868 case DW_OP_lit25:
22869 case DW_OP_lit26:
22870 case DW_OP_lit27:
22871 case DW_OP_lit28:
22872 case DW_OP_lit29:
22873 case DW_OP_lit30:
22874 case DW_OP_lit31:
22875 stack[++stacki] = op - DW_OP_lit0;
22876 break;
f1bea926 22877
21ae7a4d
JK
22878 case DW_OP_reg0:
22879 case DW_OP_reg1:
22880 case DW_OP_reg2:
22881 case DW_OP_reg3:
22882 case DW_OP_reg4:
22883 case DW_OP_reg5:
22884 case DW_OP_reg6:
22885 case DW_OP_reg7:
22886 case DW_OP_reg8:
22887 case DW_OP_reg9:
22888 case DW_OP_reg10:
22889 case DW_OP_reg11:
22890 case DW_OP_reg12:
22891 case DW_OP_reg13:
22892 case DW_OP_reg14:
22893 case DW_OP_reg15:
22894 case DW_OP_reg16:
22895 case DW_OP_reg17:
22896 case DW_OP_reg18:
22897 case DW_OP_reg19:
22898 case DW_OP_reg20:
22899 case DW_OP_reg21:
22900 case DW_OP_reg22:
22901 case DW_OP_reg23:
22902 case DW_OP_reg24:
22903 case DW_OP_reg25:
22904 case DW_OP_reg26:
22905 case DW_OP_reg27:
22906 case DW_OP_reg28:
22907 case DW_OP_reg29:
22908 case DW_OP_reg30:
22909 case DW_OP_reg31:
22910 stack[++stacki] = op - DW_OP_reg0;
22911 if (i < size)
7d79de9a
TT
22912 {
22913 if (computed == nullptr)
22914 dwarf2_complex_location_expr_complaint ();
22915 else
22916 return 0;
22917 }
21ae7a4d 22918 break;
c906108c 22919
21ae7a4d
JK
22920 case DW_OP_regx:
22921 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22922 i += bytes_read;
22923 stack[++stacki] = unsnd;
22924 if (i < size)
7d79de9a
TT
22925 {
22926 if (computed == nullptr)
22927 dwarf2_complex_location_expr_complaint ();
22928 else
22929 return 0;
22930 }
21ae7a4d 22931 break;
c906108c 22932
21ae7a4d 22933 case DW_OP_addr:
c8a7a66f
TT
22934 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22935 &bytes_read);
21ae7a4d
JK
22936 i += bytes_read;
22937 break;
d53d4ac5 22938
21ae7a4d
JK
22939 case DW_OP_const1u:
22940 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22941 i += 1;
22942 break;
22943
22944 case DW_OP_const1s:
22945 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22946 i += 1;
22947 break;
22948
22949 case DW_OP_const2u:
22950 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22951 i += 2;
22952 break;
22953
22954 case DW_OP_const2s:
22955 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22956 i += 2;
22957 break;
d53d4ac5 22958
21ae7a4d
JK
22959 case DW_OP_const4u:
22960 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22961 i += 4;
22962 break;
22963
22964 case DW_OP_const4s:
22965 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22966 i += 4;
22967 break;
22968
585861ea
JK
22969 case DW_OP_const8u:
22970 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22971 i += 8;
22972 break;
22973
21ae7a4d
JK
22974 case DW_OP_constu:
22975 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22976 &bytes_read);
22977 i += bytes_read;
22978 break;
22979
22980 case DW_OP_consts:
22981 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22982 i += bytes_read;
22983 break;
22984
22985 case DW_OP_dup:
22986 stack[stacki + 1] = stack[stacki];
22987 stacki++;
22988 break;
22989
22990 case DW_OP_plus:
22991 stack[stacki - 1] += stack[stacki];
22992 stacki--;
22993 break;
22994
22995 case DW_OP_plus_uconst:
22996 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22997 &bytes_read);
22998 i += bytes_read;
22999 break;
23000
23001 case DW_OP_minus:
23002 stack[stacki - 1] -= stack[stacki];
23003 stacki--;
23004 break;
23005
23006 case DW_OP_deref:
23007 /* If we're not the last op, then we definitely can't encode
23008 this using GDB's address_class enum. This is valid for partial
23009 global symbols, although the variable's address will be bogus
23010 in the psymtab. */
23011 if (i < size)
7d79de9a
TT
23012 {
23013 if (computed == nullptr)
23014 dwarf2_complex_location_expr_complaint ();
23015 else
23016 return 0;
23017 }
21ae7a4d
JK
23018 break;
23019
23020 case DW_OP_GNU_push_tls_address:
4aa4e28b 23021 case DW_OP_form_tls_address:
21ae7a4d
JK
23022 /* The top of the stack has the offset from the beginning
23023 of the thread control block at which the variable is located. */
23024 /* Nothing should follow this operator, so the top of stack would
23025 be returned. */
23026 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23027 address will be bogus in the psymtab. Make it always at least
23028 non-zero to not look as a variable garbage collected by linker
23029 which have DW_OP_addr 0. */
21ae7a4d 23030 if (i < size)
7d79de9a
TT
23031 {
23032 if (computed == nullptr)
23033 dwarf2_complex_location_expr_complaint ();
23034 else
23035 return 0;
23036 }
585861ea 23037 stack[stacki]++;
21ae7a4d
JK
23038 break;
23039
23040 case DW_OP_GNU_uninit:
7d79de9a
TT
23041 if (computed != nullptr)
23042 return 0;
21ae7a4d
JK
23043 break;
23044
336d760d 23045 case DW_OP_addrx:
3019eac3 23046 case DW_OP_GNU_addr_index:
49f6c839 23047 case DW_OP_GNU_const_index:
3019eac3
DE
23048 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23049 &bytes_read);
23050 i += bytes_read;
23051 break;
23052
21ae7a4d 23053 default:
7d79de9a
TT
23054 if (computed == nullptr)
23055 {
23056 const char *name = get_DW_OP_name (op);
21ae7a4d 23057
7d79de9a
TT
23058 if (name)
23059 complaint (_("unsupported stack op: '%s'"),
23060 name);
23061 else
23062 complaint (_("unsupported stack op: '%02x'"),
23063 op);
23064 }
21ae7a4d
JK
23065
23066 return (stack[stacki]);
d53d4ac5 23067 }
3c6e0cb3 23068
21ae7a4d
JK
23069 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23070 outside of the allocated space. Also enforce minimum>0. */
23071 if (stacki >= ARRAY_SIZE (stack) - 1)
23072 {
7d79de9a
TT
23073 if (computed == nullptr)
23074 complaint (_("location description stack overflow"));
21ae7a4d
JK
23075 return 0;
23076 }
23077
23078 if (stacki <= 0)
23079 {
7d79de9a
TT
23080 if (computed == nullptr)
23081 complaint (_("location description stack underflow"));
21ae7a4d
JK
23082 return 0;
23083 }
23084 }
7d79de9a
TT
23085
23086 if (computed != nullptr)
23087 *computed = true;
21ae7a4d 23088 return (stack[stacki]);
c906108c
SS
23089}
23090
23091/* memory allocation interface */
23092
c906108c 23093static struct dwarf_block *
7b5a2f43 23094dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23095{
8d749320 23096 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23097}
23098
c906108c 23099static struct die_info *
b60c80d6 23100dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23101{
23102 struct die_info *die;
b60c80d6
DJ
23103 size_t size = sizeof (struct die_info);
23104
23105 if (num_attrs > 1)
23106 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23107
b60c80d6 23108 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23109 memset (die, 0, sizeof (struct die_info));
23110 return (die);
23111}
2e276125
JB
23112
23113\f
a036ba48 23114
c90ec28a 23115/* Macro support. */
cf2c3c16 23116
9eac9650
TT
23117/* An overload of dwarf_decode_macros that finds the correct section
23118 and ensures it is read in before calling the other overload. */
23119
23120static void
23121dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23122 int section_is_gnu)
23123{
5e22e966 23124 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23125 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23126 const struct line_header *lh = cu->line_header;
9eac9650
TT
23127 unsigned int offset_size = cu->header.offset_size;
23128 struct dwarf2_section_info *section;
23129 const char *section_name;
23130
23131 if (cu->dwo_unit != nullptr)
23132 {
23133 if (section_is_gnu)
23134 {
23135 section = &cu->dwo_unit->dwo_file->sections.macro;
23136 section_name = ".debug_macro.dwo";
23137 }
23138 else
23139 {
23140 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23141 section_name = ".debug_macinfo.dwo";
23142 }
23143 }
23144 else
23145 {
23146 if (section_is_gnu)
23147 {
5989a64e 23148 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23149 section_name = ".debug_macro";
23150 }
23151 else
23152 {
5989a64e 23153 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23154 section_name = ".debug_macinfo";
23155 }
23156 }
23157
23158 section->read (objfile);
23159 if (section->buffer == nullptr)
23160 {
23161 complaint (_("missing %s section"), section_name);
23162 return;
23163 }
23164
23165 buildsym_compunit *builder = cu->get_builder ();
23166
23167 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23168 offset_size, offset, section_is_gnu);
23169}
23170
3019eac3
DE
23171/* Return the .debug_loc section to use for CU.
23172 For DWO files use .debug_loc.dwo. */
23173
23174static struct dwarf2_section_info *
23175cu_debug_loc_section (struct dwarf2_cu *cu)
23176{
5e22e966 23177 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23178
3019eac3 23179 if (cu->dwo_unit)
43988095
JK
23180 {
23181 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23182
43988095
JK
23183 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23184 }
5989a64e
SM
23185 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23186 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23187}
23188
8cf6f0b1
TT
23189/* A helper function that fills in a dwarf2_loclist_baton. */
23190
23191static void
23192fill_in_loclist_baton (struct dwarf2_cu *cu,
23193 struct dwarf2_loclist_baton *baton,
ff39bb5e 23194 const struct attribute *attr)
8cf6f0b1 23195{
5e22e966 23196 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23197 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23198
96b79293 23199 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23200
a50264ba 23201 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23202 baton->per_cu = cu->per_cu;
23203 gdb_assert (baton->per_cu);
23204 /* We don't know how long the location list is, but make sure we
23205 don't run off the edge of the section. */
3019eac3
DE
23206 baton->size = section->size - DW_UNSND (attr);
23207 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23208 if (cu->base_address.has_value ())
23209 baton->base_address = *cu->base_address;
23210 else
23211 baton->base_address = 0;
f664829e 23212 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23213}
23214
4c2df51b 23215static void
ff39bb5e 23216dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23217 struct dwarf2_cu *cu, int is_block)
4c2df51b 23218{
5e22e966 23219 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23220 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23221 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23222
cd6c91b4 23223 if (attr->form_is_section_offset ()
3019eac3 23224 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23225 the section. If so, fall through to the complaint in the
23226 other branch. */
2c7d5afc 23227 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23228 {
0d53c4c4 23229 struct dwarf2_loclist_baton *baton;
4c2df51b 23230
8d749320 23231 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23232
8cf6f0b1 23233 fill_in_loclist_baton (cu, baton, attr);
be391dca 23234
2b24b6e4 23235 if (!cu->base_address.has_value ())
b98664d3 23236 complaint (_("Location list used without "
3e43a32a 23237 "specifying the CU base address."));
4c2df51b 23238
f1e6e072
TT
23239 SYMBOL_ACLASS_INDEX (sym) = (is_block
23240 ? dwarf2_loclist_block_index
23241 : dwarf2_loclist_index);
0d53c4c4
DJ
23242 SYMBOL_LOCATION_BATON (sym) = baton;
23243 }
23244 else
23245 {
23246 struct dwarf2_locexpr_baton *baton;
23247
8d749320 23248 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23249 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23250 baton->per_cu = cu->per_cu;
23251 gdb_assert (baton->per_cu);
0d53c4c4 23252
4fc6c0d5 23253 if (attr->form_is_block ())
0d53c4c4
DJ
23254 {
23255 /* Note that we're just copying the block's data pointer
23256 here, not the actual data. We're still pointing into the
6502dd73
DJ
23257 info_buffer for SYM's objfile; right now we never release
23258 that buffer, but when we do clean up properly this may
23259 need to change. */
0d53c4c4
DJ
23260 baton->size = DW_BLOCK (attr)->size;
23261 baton->data = DW_BLOCK (attr)->data;
23262 }
23263 else
23264 {
23265 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23266 sym->natural_name ());
0d53c4c4 23267 baton->size = 0;
0d53c4c4 23268 }
6e70227d 23269
f1e6e072
TT
23270 SYMBOL_ACLASS_INDEX (sym) = (is_block
23271 ? dwarf2_locexpr_block_index
23272 : dwarf2_locexpr_index);
0d53c4c4
DJ
23273 SYMBOL_LOCATION_BATON (sym) = baton;
23274 }
4c2df51b 23275}
6502dd73 23276
09ba997f 23277/* See read.h. */
ae0d2f24
UW
23278
23279struct objfile *
09ba997f 23280dwarf2_per_cu_data::objfile () const
ae0d2f24 23281{
09ba997f 23282 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23283
23284 /* Return the master objfile, so that we can report and look up the
23285 correct file containing this variable. */
23286 if (objfile->separate_debug_objfile_backlink)
23287 objfile = objfile->separate_debug_objfile_backlink;
23288
23289 return objfile;
23290}
23291
96408a79
SA
23292/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23293 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23294 CU_HEADERP first. */
23295
23296static const struct comp_unit_head *
23297per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23298 const struct dwarf2_per_cu_data *per_cu)
96408a79 23299{
d521ce57 23300 const gdb_byte *info_ptr;
96408a79
SA
23301
23302 if (per_cu->cu)
23303 return &per_cu->cu->header;
23304
9c541725 23305 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23306
23307 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23308 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23309 rcuh_kind::COMPILE);
96408a79
SA
23310
23311 return cu_headerp;
23312}
23313
09ba997f 23314/* See read.h. */
ae0d2f24 23315
98714339 23316int
09ba997f 23317dwarf2_per_cu_data::addr_size () const
ae0d2f24 23318{
96408a79
SA
23319 struct comp_unit_head cu_header_local;
23320 const struct comp_unit_head *cu_headerp;
c471e790 23321
09ba997f 23322 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23323
23324 return cu_headerp->addr_size;
ae0d2f24
UW
23325}
23326
09ba997f 23327/* See read.h. */
9eae7c52
TT
23328
23329int
09ba997f 23330dwarf2_per_cu_data::offset_size () const
9eae7c52 23331{
96408a79
SA
23332 struct comp_unit_head cu_header_local;
23333 const struct comp_unit_head *cu_headerp;
9c6c53f7 23334
09ba997f 23335 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23336
23337 return cu_headerp->offset_size;
23338}
23339
09ba997f 23340/* See read.h. */
96408a79
SA
23341
23342int
09ba997f 23343dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23344{
23345 struct comp_unit_head cu_header_local;
23346 const struct comp_unit_head *cu_headerp;
23347
09ba997f 23348 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23349
23350 if (cu_headerp->version == 2)
23351 return cu_headerp->addr_size;
23352 else
23353 return cu_headerp->offset_size;
181cebd4
JK
23354}
23355
09ba997f 23356/* See read.h. */
9aa1f1e3
TT
23357
23358CORE_ADDR
09ba997f 23359dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23360{
09ba997f
TT
23361 struct objfile *objfile = dwarf2_per_objfile->objfile;
23362
23363 return objfile->text_section_offset ();
9aa1f1e3
TT
23364}
23365
09ba997f
TT
23366/* See read.h. */
23367
23368struct type *
23369dwarf2_per_cu_data::addr_type () const
9a49df9d 23370{
09ba997f 23371 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23372 struct type *void_type = objfile_type (objfile)->builtin_void;
23373 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23374 int addr_size = this->addr_size ();
9a49df9d
AB
23375
23376 if (TYPE_LENGTH (addr_type) == addr_size)
23377 return addr_type;
23378
09ba997f 23379 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23380 return addr_type;
23381}
23382
22b6cd70
TT
23383/* A helper function for dwarf2_find_containing_comp_unit that returns
23384 the index of the result, and that searches a vector. It will
23385 return a result even if the offset in question does not actually
23386 occur in any CU. This is separate so that it can be unit
23387 tested. */
ae038cb0 23388
22b6cd70
TT
23389static int
23390dwarf2_find_containing_comp_unit
23391 (sect_offset sect_off,
23392 unsigned int offset_in_dwz,
23393 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23394{
ae038cb0
DJ
23395 int low, high;
23396
ae038cb0 23397 low = 0;
22b6cd70 23398 high = all_comp_units.size () - 1;
ae038cb0
DJ
23399 while (high > low)
23400 {
36586728 23401 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23402 int mid = low + (high - low) / 2;
9a619af0 23403
22b6cd70 23404 mid_cu = all_comp_units[mid];
36586728 23405 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23406 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23407 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23408 high = mid;
23409 else
23410 low = mid + 1;
23411 }
23412 gdb_assert (low == high);
22b6cd70
TT
23413 return low;
23414}
23415
23416/* Locate the .debug_info compilation unit from CU's objfile which contains
23417 the DIE at OFFSET. Raises an error on failure. */
23418
23419static struct dwarf2_per_cu_data *
23420dwarf2_find_containing_comp_unit (sect_offset sect_off,
23421 unsigned int offset_in_dwz,
23422 struct dwarf2_per_objfile *dwarf2_per_objfile)
23423{
23424 int low
23425 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23426 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23427 struct dwarf2_per_cu_data *this_cu
5989a64e 23428 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23429
45b8ae0c 23430 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23431 {
36586728 23432 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23433 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23434 "offset %s [in module %s]"),
23435 sect_offset_str (sect_off),
ed2dc618 23436 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23437
5989a64e 23438 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23439 <= sect_off);
5989a64e 23440 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23441 }
23442 else
23443 {
5989a64e 23444 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23445 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23446 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23447 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23448 return this_cu;
23449 }
23450}
23451
22b6cd70
TT
23452#if GDB_SELF_TEST
23453
23454namespace selftests {
23455namespace find_containing_comp_unit {
23456
23457static void
23458run_test ()
23459{
23460 struct dwarf2_per_cu_data one {};
23461 struct dwarf2_per_cu_data two {};
23462 struct dwarf2_per_cu_data three {};
23463 struct dwarf2_per_cu_data four {};
23464
23465 one.length = 5;
23466 two.sect_off = sect_offset (one.length);
23467 two.length = 7;
23468
23469 three.length = 5;
23470 three.is_dwz = 1;
23471 four.sect_off = sect_offset (three.length);
23472 four.length = 7;
23473 four.is_dwz = 1;
23474
23475 std::vector<dwarf2_per_cu_data *> units;
23476 units.push_back (&one);
23477 units.push_back (&two);
23478 units.push_back (&three);
23479 units.push_back (&four);
23480
23481 int result;
23482
23483 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23484 SELF_CHECK (units[result] == &one);
23485 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23486 SELF_CHECK (units[result] == &one);
23487 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23488 SELF_CHECK (units[result] == &two);
23489
23490 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23491 SELF_CHECK (units[result] == &three);
23492 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23493 SELF_CHECK (units[result] == &three);
23494 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23495 SELF_CHECK (units[result] == &four);
23496}
23497
23498}
23499}
23500
23501#endif /* GDB_SELF_TEST */
23502
9e021579 23503/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23504
9e021579
SM
23505dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23506 dwarf2_per_objfile *per_objfile)
23507 : per_cu (per_cu),
23508 per_objfile (per_objfile),
9068261f
AB
23509 mark (false),
23510 has_loclist (false),
23511 checked_producer (false),
23512 producer_is_gxx_lt_4_6 (false),
23513 producer_is_gcc_lt_4_3 (false),
eb77c9df 23514 producer_is_icc (false),
9068261f 23515 producer_is_icc_lt_14 (false),
c258c396 23516 producer_is_codewarrior (false),
9068261f 23517 processing_has_namespace_info (false)
93311388 23518{
fcd3b13d
SM
23519 per_cu->cu = this;
23520}
23521
23522/* Destroy a dwarf2_cu. */
23523
23524dwarf2_cu::~dwarf2_cu ()
23525{
23526 per_cu->cu = NULL;
9816fde3
JK
23527}
23528
23529/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23530
23531static void
95554aad
TT
23532prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23533 enum language pretend_language)
9816fde3
JK
23534{
23535 struct attribute *attr;
23536
23537 /* Set the language we're debugging. */
23538 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23539 if (attr != nullptr)
9816fde3
JK
23540 set_cu_language (DW_UNSND (attr), cu);
23541 else
9cded63f 23542 {
95554aad 23543 cu->language = pretend_language;
9cded63f
TT
23544 cu->language_defn = language_def (cu->language);
23545 }
dee91e82 23546
7d45c7c3 23547 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23548}
23549
ae038cb0
DJ
23550/* Increase the age counter on each cached compilation unit, and free
23551 any that are too old. */
23552
23553static void
ed2dc618 23554age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23555{
23556 struct dwarf2_per_cu_data *per_cu, **last_chain;
23557
5989a64e
SM
23558 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23559 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23560 while (per_cu != NULL)
23561 {
23562 per_cu->cu->last_used ++;
b4f54984 23563 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23564 dwarf2_mark (per_cu->cu);
23565 per_cu = per_cu->cu->read_in_chain;
23566 }
23567
5989a64e
SM
23568 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23569 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23570 while (per_cu != NULL)
23571 {
23572 struct dwarf2_per_cu_data *next_cu;
23573
23574 next_cu = per_cu->cu->read_in_chain;
23575
23576 if (!per_cu->cu->mark)
23577 {
fcd3b13d 23578 delete per_cu->cu;
ae038cb0
DJ
23579 *last_chain = next_cu;
23580 }
23581 else
23582 last_chain = &per_cu->cu->read_in_chain;
23583
23584 per_cu = next_cu;
23585 }
23586}
23587
23588/* Remove a single compilation unit from the cache. */
23589
23590static void
dee91e82 23591free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23592{
23593 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23594 struct dwarf2_per_objfile *dwarf2_per_objfile
23595 = target_per_cu->dwarf2_per_objfile;
ae038cb0 23596
5989a64e
SM
23597 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23598 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23599 while (per_cu != NULL)
23600 {
23601 struct dwarf2_per_cu_data *next_cu;
23602
23603 next_cu = per_cu->cu->read_in_chain;
23604
dee91e82 23605 if (per_cu == target_per_cu)
ae038cb0 23606 {
fcd3b13d 23607 delete per_cu->cu;
dee91e82 23608 per_cu->cu = NULL;
ae038cb0
DJ
23609 *last_chain = next_cu;
23610 break;
23611 }
23612 else
23613 last_chain = &per_cu->cu->read_in_chain;
23614
23615 per_cu = next_cu;
23616 }
23617}
23618
dee91e82
DE
23619/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23620 We store these in a hash table separate from the DIEs, and preserve them
23621 when the DIEs are flushed out of cache.
23622
23623 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23624 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23625 or the type may come from a DWO file. Furthermore, while it's more logical
23626 to use per_cu->section+offset, with Fission the section with the data is in
23627 the DWO file but we don't know that section at the point we need it.
23628 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23629 because we can enter the lookup routine, get_die_type_at_offset, from
23630 outside this file, and thus won't necessarily have PER_CU->cu.
23631 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23632
dee91e82 23633struct dwarf2_per_cu_offset_and_type
1c379e20 23634{
dee91e82 23635 const struct dwarf2_per_cu_data *per_cu;
9c541725 23636 sect_offset sect_off;
1c379e20
DJ
23637 struct type *type;
23638};
23639
dee91e82 23640/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23641
23642static hashval_t
dee91e82 23643per_cu_offset_and_type_hash (const void *item)
1c379e20 23644{
9a3c8263
SM
23645 const struct dwarf2_per_cu_offset_and_type *ofs
23646 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23647
9c541725 23648 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23649}
23650
dee91e82 23651/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23652
23653static int
dee91e82 23654per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23655{
9a3c8263
SM
23656 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23657 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23658 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23659 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23660
dee91e82 23661 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23662 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23663}
23664
23665/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23666 table if necessary. For convenience, return TYPE.
23667
23668 The DIEs reading must have careful ordering to:
85102364 23669 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23670 reading current DIE.
23671 * Not trying to dereference contents of still incompletely read in types
23672 while reading in other DIEs.
23673 * Enable referencing still incompletely read in types just by a pointer to
23674 the type without accessing its fields.
23675
23676 Therefore caller should follow these rules:
23677 * Try to fetch any prerequisite types we may need to build this DIE type
23678 before building the type and calling set_die_type.
e71ec853 23679 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23680 possible before fetching more types to complete the current type.
23681 * Make the type as complete as possible before fetching more types. */
1c379e20 23682
f792889a 23683static struct type *
1c379e20
DJ
23684set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23685{
5e22e966 23686 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23687 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23688 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23689 struct attribute *attr;
23690 struct dynamic_prop prop;
1c379e20 23691
b4ba55a1
JB
23692 /* For Ada types, make sure that the gnat-specific data is always
23693 initialized (if not already set). There are a few types where
23694 we should not be doing so, because the type-specific area is
23695 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23696 where the type-specific area is used to store the floatformat).
23697 But this is not a problem, because the gnat-specific information
23698 is actually not needed for these types. */
23699 if (need_gnat_info (cu)
78134374
SM
23700 && type->code () != TYPE_CODE_FUNC
23701 && type->code () != TYPE_CODE_FLT
23702 && type->code () != TYPE_CODE_METHODPTR
23703 && type->code () != TYPE_CODE_MEMBERPTR
23704 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23705 && !HAVE_GNAT_AUX_INFO (type))
23706 INIT_GNAT_SPECIFIC (type);
23707
3f2f83dd
KB
23708 /* Read DW_AT_allocated and set in type. */
23709 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23710 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23711 {
09ba997f 23712 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23713 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23714 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23715 }
23716 else if (attr != NULL)
23717 {
b98664d3 23718 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23719 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23720 sect_offset_str (die->sect_off));
3f2f83dd
KB
23721 }
23722
23723 /* Read DW_AT_associated and set in type. */
23724 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23725 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23726 {
09ba997f 23727 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23728 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23729 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23730 }
23731 else if (attr != NULL)
23732 {
b98664d3 23733 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23734 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23735 sect_offset_str (die->sect_off));
3f2f83dd
KB
23736 }
23737
3cdcd0ce
JB
23738 /* Read DW_AT_data_location and set in type. */
23739 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23740 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23741 cu->per_cu->addr_type ()))
5c54719c 23742 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23743
ae090bdb
SM
23744 if (dwarf2_per_objfile->die_type_hash == NULL)
23745 dwarf2_per_objfile->die_type_hash
0335378b
TT
23746 = htab_up (htab_create_alloc (127,
23747 per_cu_offset_and_type_hash,
23748 per_cu_offset_and_type_eq,
23749 NULL, xcalloc, xfree));
1c379e20 23750
dee91e82 23751 ofs.per_cu = cu->per_cu;
9c541725 23752 ofs.sect_off = die->sect_off;
1c379e20 23753 ofs.type = type;
dee91e82 23754 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23755 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23756 if (*slot)
b98664d3 23757 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23758 sect_offset_str (die->sect_off));
8d749320
SM
23759 *slot = XOBNEW (&objfile->objfile_obstack,
23760 struct dwarf2_per_cu_offset_and_type);
1c379e20 23761 **slot = ofs;
f792889a 23762 return type;
1c379e20
DJ
23763}
23764
9c541725 23765/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23766 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23767
23768static struct type *
9c541725 23769get_die_type_at_offset (sect_offset sect_off,
673bfd45 23770 struct dwarf2_per_cu_data *per_cu)
1c379e20 23771{
dee91e82 23772 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23773 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23774
ae090bdb 23775 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23776 return NULL;
1c379e20 23777
dee91e82 23778 ofs.per_cu = per_cu;
9c541725 23779 ofs.sect_off = sect_off;
9a3c8263 23780 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23781 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23782 if (slot)
23783 return slot->type;
23784 else
23785 return NULL;
23786}
23787
02142a6c 23788/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23789 or return NULL if DIE does not have a saved type. */
23790
23791static struct type *
23792get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23793{
9c541725 23794 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23795}
23796
10b3939b
DJ
23797/* Add a dependence relationship from CU to REF_PER_CU. */
23798
23799static void
23800dwarf2_add_dependence (struct dwarf2_cu *cu,
23801 struct dwarf2_per_cu_data *ref_per_cu)
23802{
23803 void **slot;
23804
23805 if (cu->dependencies == NULL)
23806 cu->dependencies
23807 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23808 NULL, &cu->comp_unit_obstack,
23809 hashtab_obstack_allocate,
23810 dummy_obstack_deallocate);
23811
23812 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23813 if (*slot == NULL)
23814 *slot = ref_per_cu;
23815}
1c379e20 23816
f504f079
DE
23817/* Subroutine of dwarf2_mark to pass to htab_traverse.
23818 Set the mark field in every compilation unit in the
ae038cb0
DJ
23819 cache that we must keep because we are keeping CU. */
23820
10b3939b
DJ
23821static int
23822dwarf2_mark_helper (void **slot, void *data)
23823{
23824 struct dwarf2_per_cu_data *per_cu;
23825
23826 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23827
23828 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23829 reading of the chain. As such dependencies remain valid it is not much
23830 useful to track and undo them during QUIT cleanups. */
23831 if (per_cu->cu == NULL)
23832 return 1;
23833
10b3939b
DJ
23834 if (per_cu->cu->mark)
23835 return 1;
9068261f 23836 per_cu->cu->mark = true;
10b3939b
DJ
23837
23838 if (per_cu->cu->dependencies != NULL)
23839 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23840
23841 return 1;
23842}
23843
f504f079
DE
23844/* Set the mark field in CU and in every other compilation unit in the
23845 cache that we must keep because we are keeping CU. */
23846
ae038cb0
DJ
23847static void
23848dwarf2_mark (struct dwarf2_cu *cu)
23849{
23850 if (cu->mark)
23851 return;
9068261f 23852 cu->mark = true;
10b3939b
DJ
23853 if (cu->dependencies != NULL)
23854 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23855}
23856
23857static void
23858dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23859{
23860 while (per_cu)
23861 {
9068261f 23862 per_cu->cu->mark = false;
ae038cb0
DJ
23863 per_cu = per_cu->cu->read_in_chain;
23864 }
72bf9492
DJ
23865}
23866
72bf9492
DJ
23867/* Trivial hash function for partial_die_info: the hash value of a DIE
23868 is its offset in .debug_info for this objfile. */
23869
23870static hashval_t
23871partial_die_hash (const void *item)
23872{
9a3c8263
SM
23873 const struct partial_die_info *part_die
23874 = (const struct partial_die_info *) item;
9a619af0 23875
9c541725 23876 return to_underlying (part_die->sect_off);
72bf9492
DJ
23877}
23878
23879/* Trivial comparison function for partial_die_info structures: two DIEs
23880 are equal if they have the same offset. */
23881
23882static int
23883partial_die_eq (const void *item_lhs, const void *item_rhs)
23884{
9a3c8263
SM
23885 const struct partial_die_info *part_die_lhs
23886 = (const struct partial_die_info *) item_lhs;
23887 const struct partial_die_info *part_die_rhs
23888 = (const struct partial_die_info *) item_rhs;
9a619af0 23889
9c541725 23890 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23891}
23892
3c3bb058
AB
23893struct cmd_list_element *set_dwarf_cmdlist;
23894struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23895
9291a0cd 23896static void
cd4fb1b2
SM
23897show_check_physname (struct ui_file *file, int from_tty,
23898 struct cmd_list_element *c, const char *value)
9291a0cd 23899{
cd4fb1b2
SM
23900 fprintf_filtered (file,
23901 _("Whether to check \"physname\" is %s.\n"),
23902 value);
9291a0cd
TT
23903}
23904
6c265988 23905void _initialize_dwarf2_read ();
cd4fb1b2 23906void
6c265988 23907_initialize_dwarf2_read ()
9291a0cd 23908{
0743fc83 23909 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23910Set DWARF specific variables.\n\
590042fc 23911Configure DWARF variables such as the cache size."),
0743fc83
TT
23912 &set_dwarf_cmdlist, "maintenance set dwarf ",
23913 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23914
0743fc83 23915 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23916Show DWARF specific variables.\n\
23917Show DWARF variables such as the cache size."),
0743fc83
TT
23918 &show_dwarf_cmdlist, "maintenance show dwarf ",
23919 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23920
cd4fb1b2
SM
23921 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23922 &dwarf_max_cache_age, _("\
23923Set the upper bound on the age of cached DWARF compilation units."), _("\
23924Show the upper bound on the age of cached DWARF compilation units."), _("\
23925A higher limit means that cached compilation units will be stored\n\
23926in memory longer, and more total memory will be used. Zero disables\n\
23927caching, which can slow down startup."),
23928 NULL,
23929 show_dwarf_max_cache_age,
23930 &set_dwarf_cmdlist,
23931 &show_dwarf_cmdlist);
156942c7 23932
cd4fb1b2
SM
23933 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23934Set debugging of the DWARF reader."), _("\
23935Show debugging of the DWARF reader."), _("\
23936When enabled (non-zero), debugging messages are printed during DWARF\n\
23937reading and symtab expansion. A value of 1 (one) provides basic\n\
23938information. A value greater than 1 provides more verbose information."),
23939 NULL,
23940 NULL,
23941 &setdebuglist, &showdebuglist);
9291a0cd 23942
cd4fb1b2
SM
23943 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23944Set debugging of the DWARF DIE reader."), _("\
23945Show debugging of the DWARF DIE reader."), _("\
23946When enabled (non-zero), DIEs are dumped after they are read in.\n\
23947The value is the maximum depth to print."),
23948 NULL,
23949 NULL,
23950 &setdebuglist, &showdebuglist);
9291a0cd 23951
cd4fb1b2
SM
23952 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23953Set debugging of the dwarf line reader."), _("\
23954Show debugging of the dwarf line reader."), _("\
23955When enabled (non-zero), line number entries are dumped as they are read in.\n\
23956A value of 1 (one) provides basic information.\n\
23957A value greater than 1 provides more verbose information."),
23958 NULL,
23959 NULL,
23960 &setdebuglist, &showdebuglist);
437afbb8 23961
cd4fb1b2
SM
23962 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23963Set cross-checking of \"physname\" code against demangler."), _("\
23964Show cross-checking of \"physname\" code against demangler."), _("\
23965When enabled, GDB's internal \"physname\" code is checked against\n\
23966the demangler."),
23967 NULL, show_check_physname,
23968 &setdebuglist, &showdebuglist);
900e11f9 23969
e615022a
DE
23970 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23971 no_class, &use_deprecated_index_sections, _("\
23972Set whether to use deprecated gdb_index sections."), _("\
23973Show whether to use deprecated gdb_index sections."), _("\
23974When enabled, deprecated .gdb_index sections are used anyway.\n\
23975Normally they are ignored either because of a missing feature or\n\
23976performance issue.\n\
23977Warning: This option must be enabled before gdb reads the file."),
23978 NULL,
23979 NULL,
23980 &setlist, &showlist);
23981
f1e6e072
TT
23982 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23983 &dwarf2_locexpr_funcs);
23984 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23985 &dwarf2_loclist_funcs);
23986
23987 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23988 &dwarf2_block_frame_base_locexpr_funcs);
23989 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23990 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23991
23992#if GDB_SELF_TEST
23993 selftests::register_test ("dw2_expand_symtabs_matching",
23994 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23995 selftests::register_test ("dwarf2_find_containing_comp_unit",
23996 selftests::find_containing_comp_unit::run_test);
c62446b1 23997#endif
6502dd73 23998}
This page took 7.133169 seconds and 4 git commands to generate.